src/Controller/ImportController2.php line 3293

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use GuzzleHttp\Psr7;
  7. use GuzzleHttp\Client;
  8. use App\Controller\shopware\ProductFlat;
  9. use GuzzleHttp\Exception\RequestException;
  10. use stdClass;
  11. use Symfony\Component\HttpFoundation\Request;
  12. // use Symfony\Component\HttpFoundation\Response;
  13. // use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\Validator\Constraints\DateTime;
  16. use Symfony\Component\HttpFoundation\RedirectResponse;
  17. // use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  18. # E-MAIL
  19. use Symfony\Component\Mailer\MailerInterface;
  20. use Symfony\Component\Mime\Email;
  21. class ImportController2 extends AbstractController
  22. {
  23.     private $client NULL;
  24.     private $header NULL;
  25.     private $base_url// Shop URL
  26.     private $client_id// SW ID
  27.     private $client_secret// SW Secret
  28.     private $client_username// SW Username
  29.     private $client_password// SW Password
  30.     private $shopware_data// Shopware Datensatz
  31.     private $shopware_data_live// Shopware Datenbank LIVE
  32.     private $shopware_data_dump// AKN Datenbank Dump
  33.     private $shopware_data_dump_live// AKN Datenbank Dump LIVE
  34.     private $surface_id// Eigenschaften Gruppe ID Oberfläche
  35.     private $materialgroup_id// Eigenschaften Gruppe ID Materialgruppe
  36.     private $material_id// Eigenschaften Gruppe ID Material
  37.     private $articlegroup_id// Eigenschaften Gruppe ID Artikelgruppe
  38.     private $zulauf_id// Kategorie ID Zulauf Bald Verfügbar
  39.     private $default_category_id// Default Kategorie ID
  40.     private $category_layout_id// Default Kategorie Layout ID
  41.     private $color_id// Eigenschaften Gruppe ID Farbe
  42.     private $fault_exists_id// Eigenschaften Gruppe ID Fehler vorhanden
  43.     private $thickness_id// Eigenschaften Gruppe ID Materialstärke
  44.     private $length_id// Eigenschaften Gruppe ID Länge
  45.     private $width_id// Eigenschaften Gruppe ID Höhe
  46.     private $amount_id// Eigenschaften Gruppe ID Anzahl
  47.     private $surfacearea_id// Eigenschaften Gruppe ID Quadratmeter
  48.     private $meter_id// Eigenschaften Gruppe ID Meter
  49.     private $stonenumber_id// Eigenschaften Gruppe ID Steinnummer
  50.     private $sa_id// Eigenschaften Gruppe ID Sa
  51.     private $squaremeterprice_id// Eigenschaften Gruppe ID Quadratmeterpreis
  52.     private $tax_id// ID der Defaultsteuern
  53.     private $currency_id//ID der Defaultwährung
  54.     private $deliverytime_id//ID der Defaultlieferzeit
  55.     private $saleschannel_id//ID der Default Verkaufskanal
  56.     private $salutation_id// ID der Default Ansprache (Keine Angabe)
  57.     private $payment_method_id// ID der Default Zahlungsmethode (Nachname)
  58.     private $group_id// ID der Default Kundengruppe
  59.     private $country_id// ID des Default Landes (Deutschland)
  60.     private $default_image_filename// Default Bildername
  61.     private $language_id;  // DEUTSCH
  62.     // private $steine_aus_aller_welt_category_id = 'a9bb98a9e33e4a969b71f48bd1702ab7';    // ID der Parent-Kategorie "Steine aus aller Welt"
  63.     function __construct() {
  64.         $this->base_url $_ENV['base_url']; // Base URL
  65.         if(isset($_ENV['client_id'])){
  66.             $this->client_id $_ENV['client_id']; // SW ID
  67.         }
  68.         if(isset($_ENV['client_secret'])){
  69.             $this->client_secret $_ENV['client_secret']; // SW Secret
  70.         }
  71.         if(isset($_ENV['client_username'])){
  72.             $this->client_username $_ENV['client_username']; // SW Username
  73.         }
  74.         if(isset($_ENV['client_password'])){
  75.             $this->client_password $_ENV['client_password']; // SW Password
  76.         }
  77.         $this->shopware_data $_ENV['shopware_data']; // Shopware Datensatz
  78.         $this->shopware_data_live $_ENV['shopware_data_live']; // Shopware Datenbank LIVE
  79.         $this->shopware_data_dump $_ENV['shopware_data_dump']; // AKN Datenbank Dump
  80.         $this->shopware_data_dump_live $_ENV['shopware_data_dump_live']; // AKN Datenbank Dump LIVE
  81.         $this->surface_id $_ENV['surface_id']; // Eigenschaften Gruppe ID Oberfläche
  82.         $this->materialgroup_id $_ENV['materialgroup_id']; // Eigenschaften Gruppe ID Materialgruppe
  83.         $this->material_id $_ENV['material_id']; // Eigenschaften Gruppe ID Material
  84.         $this->articlegroup_id $_ENV['articlegroup_id']; // Eigenschaften Gruppe ID Artikelgruppe
  85.         $this->zulauf_id $_ENV['zulauf_id']; // Kategorie ID Zulauf Bald Verfügbar
  86.         $this->default_category_id $_ENV['default_category_id']; // Default Kategorie ID
  87.         $this->category_layout_id $_ENV['category_layout_id']; // Default Kategorie Layout ID
  88.         $this->color_id $_ENV['color_id']; // Eigenschaften Gruppe ID Farbe
  89.         $this->fault_exists_id $_ENV['fault_exists_id']; // Eigenschaften Gruppe ID Fehler vorhanden
  90.         $this->thickness_id $_ENV['thickness_id']; // Eigenschaften Gruppe ID Materialstärke
  91.         $this->blocknumber_id $_ENV['blocknumber_id']; // Eigenschaften Gruppe ID Blocknummer
  92.         $this->default_image_filename $_ENV['default_image_filename']; // Default Bildername
  93.         $this->language_id $_ENV['language_id']; // ID der Defaultsprache
  94.         $this->length_id=$_ENV['length_id']; // Eigenschaften Gruppe ID Länge
  95.         $this->width_id=$_ENV['width_id']; // Eigenschaften Gruppe ID Höhe
  96.         $this->amount_id=$_ENV['amount_id']; // Eigenschaften Gruppe ID Anzahl
  97.         $this->surfacearea_id=$_ENV['surfacearea_id']; // Eigenschaften Gruppe ID Quadratmeter
  98.         $this->meter_id=$_ENV['meter_id']; // Eigenschaften Gruppe ID Meter
  99.         $this->stonenumber_id $_ENV['stonenumber_id']; // Eigenschaften Gruppe ID Steinnummer
  100.         $this->sa_id=$_ENV['sa_id']; // Eigenschaften Gruppe ID Sa
  101.         $this->squaremeterprice_id=$_ENV['squaremeterprice_id']; // Eigenschaften Gruppe ID Quadratmeterpreis
  102.         $this->tax_id=$_ENV["tax_id"]; //ID der Defaultsteuern
  103.         $this->currency_id=$_ENV["currency_id"]; // ID der Defaultwährung
  104.         $this->deliverytime_id=$_ENV["deliverytime_id"]; //ID der Defaultlieferzeit
  105.         $this->saleschannel_id=$_ENV["saleschannel_id"]; //ID der Default Verkaufskanal
  106.         $this->salutation_id=$_ENV["salutation_id"]; // ID der Default Ansprache (Keine Angabe)
  107.         $this->payment_method_id=$_ENV["payment_method_id"]; // ID der Default Zahlungsmethode (Nachname)
  108.         $this->group_id=$_ENV["group_id"]; // ID der Default Kundengruppe
  109.         $this->country_id=$_ENV["country_id"]; // ID des Default Landes (Deutschland)
  110.     }
  111.     /**
  112.      * Route("/import", name="import")
  113.      */
  114.     public function index(): Response
  115.     {
  116.         return $this->render('import/index.html.twig', [
  117.             'controller_name' => 'ImportController',
  118.         ]);
  119.     }
  120.     /**
  121.      * ######## Verbindungsaufbau zur Shopware-API ########
  122.      * 
  123.      * @return array
  124.      */
  125.     private function client_connect()
  126.     {
  127.         // $client = new Client([ 'timeout' => 5.0, ]);        
  128.         $this->client = new Client([
  129.             'base_uri' => $this->base_url.'api',
  130.         ]);
  131.             // var_dump("123");exit;
  132.         
  133.             // return $this->render('update/index.html.twig', ["return" => ['user' => "segrd"]]);
  134.         if(isset($this->client_username) && isset($this->client_password)){
  135.             try{
  136.                 // var_dump("<pre>");
  137.                 $response $this->client->request('POST''/api/oauth/token', array( 
  138.                     // "debug" => true, 
  139.                     'allow_redirects'=>array('strict'=>true),
  140.                     "form_params" => array(
  141.                     "client_id"=> "administration",
  142.                     "grant_type"=> "password",
  143.                     "scopes"=> "write",
  144.                     "username"=> $this->client_username,
  145.                     "password"=> $this->client_password
  146.                     )
  147.                 ));
  148.                 // $response = $this->client->request('POST', '/api/oauth/token', array( "form_params" => array(
  149.                 //     "debug" => true, 
  150.                 //     "client_id"=> "administration",
  151.                 //     "grant_type"=> "password",
  152.                 //     "scopes"=> "write",
  153.                 //     "username"=> $this->client_username,
  154.                 //     "password"=> $this->client_password
  155.                 // )));
  156.             } catch (RequestException $e) {
  157.                 // echo Psr7\str($e->getRequest());
  158.                 // if ($e->hasResponse()) {
  159.                 //     echo Psr7\str($e->getResponse());
  160.                 // }
  161.                 if ($e->hasResponse()) {
  162.                     $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  163.                 }else{
  164.                     $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  165.                 }
  166.                 $response->headers->set('Content-Type''application/json');
  167.                 return $response;
  168.                 exit;
  169.             }
  170.         }else {
  171.             try{
  172.                 if(isset($this->client_secret) && isset($this->client_id)){
  173.                     $response $this->client->request('POST''/api/oauth/token', array( "form_params" => array(
  174.                         // "debug" => true, 
  175.                         'allow_redirects'=>array('strict'=>true),
  176.                         "grant_type" => "client_credentials",
  177.                         "client_id" =>  $this->client_id,
  178.                         "client_secret" =>  $this->client_secret
  179.                     )));
  180.                 }
  181.             } catch (RequestException $e) {
  182.                 // echo Psr7\str($e->getRequest());
  183.                 // if ($e->hasResponse()) {
  184.                 //     echo Psr7\str($e->getResponse());
  185.                 // }
  186.                 if ($e->hasResponse()) {
  187.                     $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  188.                 }else{
  189.                     $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  190.                 }
  191.                 $response->headers->set('Content-Type''application/json');
  192.                 return $response;
  193.                 exit;
  194.             }
  195.         }
  196.         $response_body json_decode($response->getBody()->getContents());
  197.         //** Header used for every request
  198.         $this->header = array(
  199.             "Accept" => "application/json",
  200.             "Content-type" => "application/json",
  201.             "Authorization" => $response_body->token_type." ".$response_body->access_token,
  202.         );
  203.     }
  204.     private function shop_connect(){
  205.         // var_dump($this->getDoctrine()->getConnection( $this->shopware_data));exit;
  206.         // return $this->getDoctrine()->getConnection( $this->shopware_data);
  207.         return $this->getDoctrine()->getConnection$this->shopware_data_live);
  208.     }
  209.     private function dump_connect(){
  210.         // var_dump($this->getDoctrine()->getConnection( $this->shopware_data));exit;
  211.         // return $this->getDoctrine()->getConnection( $this->shopware_data_dump);
  212.         return $this->getDoctrine()->getConnection$this->shopware_data_dump_live);
  213.     }
  214.     /**
  215.      * get color property id for import
  216.      */
  217.     public function getColorID($mgr_id$mar_id)
  218.     {
  219.         $this->client_connect();
  220.         $conn_shopware $this->shop_connect();
  221.         $conn_akn $this->dump_connect();
  222.         $sql "SELECT *
  223.                 FROM materialcolors
  224.                 WHERE color_id != -1
  225.                 AND mgr_id = '".$mgr_id."'
  226.                 AND mar_id = '".$mar_id."'
  227.             ;";
  228.         $stmt $conn_akn->prepare($sql);
  229.         $stmt->execute();
  230.         $colors $stmt->fetchAll();
  231.         // return $this->render('update/index.html.twig', ["return" => ['success' => $colors ]]);
  232.         if(count($colors) < && !isset($colors[0])){
  233.             // wenn farbe nicht gefunden überprüfe ob nur mit mar_id eine gefunden werden kann
  234.             $sql "SELECT *
  235.                     FROM materialcolors
  236.                     WHERE color_id != -1
  237.                     AND mar_id = '".$mar_id."'
  238.                 ;";
  239.             $stmt $conn_akn->prepare($sql);
  240.             $stmt->execute();
  241.             $colors $stmt->fetchAll();
  242.         }
  243.         if(isset($colors[0]["color_id"]) && !empty($colors[0]["color_id"])){
  244.             $sql "SELECT *
  245.                     FROM `keys`
  246.                     WHERE key_kind = 88
  247.                     AND key_number = '".$colors[0]["color_id"]."'
  248.                 ;";
  249.             $stmt $conn_akn->prepare($sql);
  250.             $stmt->execute();
  251.             $key $stmt->fetchAll();
  252. // return $this->render('update/index.html.twig', ["return" => ['success' => $key ]]);
  253.             if(isset($key[0]["key_text"]) && !empty($key[0]["key_text"])){
  254.                 // farbe wurde gefunden -> überprüfe ob als variante vorhanden
  255.                 $color explode('#',$key[0]["key_text"]);
  256.                 $key[0]["key_text"] = trim($color[0]);
  257.                 // return $key[0]["key_text"];
  258.                 // $this->color_id;
  259.                 $sql "SELECT 
  260.                         *
  261.                         , HEX(id) as id
  262.                         , HEX(language_id) as language_id
  263.                         , HEX(property_group_id) as property_group_id
  264.                     FROM property_group_option
  265.                     INNER JOIN property_group_option_translation  ON (property_group_option.id = property_group_option_translation.property_group_option_id)
  266.                     WHERE LOWER(HEX(property_group_id)) = '".$this->color_id."'
  267.                     AND `name` = '".$key[0]["key_text"]."'
  268.                     ;";
  269.                 $stmt $conn_shopware->prepare($sql);
  270.                 $stmt->execute();
  271.                 $property_color $stmt->fetchAll();
  272.                 // return $this->render('update/index.html.twig', ["return" => ['materials' => $materials, 'search_materials' => $search_materials]]);
  273.                 if(isset($property_color) && !empty($property_color) && count($property_color) > 0){
  274.                     //property vorhanden
  275.                     // var_dump("exist color ".$key[0]["key_text"]);
  276.                 }else{
  277.                     // var_dump("post color ".$key[0]["key_text"]);
  278.                     $post_property $this->client->request('POST''/api/property-group-option', array(
  279.                         'allow_redirects'=>array('strict'=>true),
  280.                         'headers' => $this->header,
  281.                         'body' => json_encode([
  282.                             'groupId' => $this->color_id,
  283.                             'name' => $key[0]["key_text"]
  284.                         ])
  285.                     ));
  286.                     $sql "SELECT 
  287.                             *
  288.                             , LOWER(HEX(id)) as id
  289.                             , HEX(language_id) as language_id
  290.                             , HEX(property_group_id) as property_group_id
  291.                         FROM property_group_option
  292.                         INNER JOIN property_group_option_translation  ON (property_group_option.id = property_group_option_translation.property_group_option_id)
  293.                         WHERE LOWER(HEX(property_group_id)) = '".$this->color_id."'
  294.                         AND `name` = '".$key[0]["key_text"]."'
  295.                         ;";
  296.                     $stmt $conn_shopware->prepare($sql);
  297.                     $stmt->execute();
  298.                     $property_color $stmt->fetchAll();
  299.                 }
  300.                 $property_color[0]["id"] = strtolower($property_color[0]["id"]);
  301.                 // return $this->render('update/index.html.twig', ["return" => ['color' => $property_color ]]);
  302.                 if(isset($property_color[0]["id"]) && !empty($property_color[0]["id"])) {
  303.                     // var_dump("EXISTS");
  304.                     return $property_color[0]["id"];
  305.                 }
  306.             }
  307.         }
  308.         return null;
  309.     }
  310.     
  311.     /**
  312.      * get category id for import
  313.      */
  314.     public function getCategoryId($category$materialgroup_id//article[materialgroup_name]
  315.     {
  316.         // prüfe ob kategorie vorhanden ist, 
  317.         // var_dump($category);
  318.         try {
  319.             $search $this->client->request('GET''/api/category?filter[name]='.$category.'', array( 
  320.                 "headers" => $this->header,
  321.             ));
  322.         } catch (RequestException $e) {
  323.             // echo Psr7\str($e->getRequest());
  324.             // if ($e->hasResponse()) {
  325.             //     echo Psr7\str($e->getResponse());
  326.             // }
  327.             if ($e->hasResponse()) {
  328.                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  329.             }else{
  330.                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  331.             }
  332.             $response->headers->set('Content-Type''application/json');
  333.             return $response;
  334.             exit;
  335.         }
  336.         $categories json_decode($search->getBody()->getContents(),true);
  337.         if(isset($categories["data"][0]["id"]) && !empty($categories["data"][0]["id"])){
  338.         }else{
  339.             $CategoryFlat = new \App\Controller\shopware\CategoryFlat();
  340.             $CategoryFlat->setType("page");
  341.             $CategoryFlat->setName($materialgroup["materialgroup_name"]);
  342.             $CategoryFlat->setCustomFields(array("custom_materialgroup_id" => $materialgroup_id));
  343.             $CategoryFlat->setCmsPageId($this->category_layout_id); 
  344.             $CategoryFlat->setParentId($this->default_category_id);
  345.             $post_category $this->client->request('POST''/api/category', array(
  346.                 // 'debug' => true, 
  347.                 'allow_redirects'=>array('strict'=>true),
  348.                 "headers" => $this->header,
  349.                 "body" => $CategoryFlat,
  350.             ));
  351.             try {
  352.                 $search $this->client->request('GET''/api/category?filter[name]='.$category.'', array( 
  353.                     "headers" => $this->header,
  354.                 ));
  355.             } catch (RequestException $e) {
  356.                 // echo Psr7\str($e->getRequest());
  357.                 // if ($e->hasResponse()) {
  358.                 //     echo Psr7\str($e->getResponse());
  359.                 // }
  360.                 if ($e->hasResponse()) {
  361.                     $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  362.                 }else{
  363.                     $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  364.                 }
  365.                 $response->headers->set('Content-Type''application/json');
  366.                 return $response;
  367.                 exit;
  368.             }
  369.             $categories json_decode($search->getBody()->getContents(),true);
  370.         }
  371.         if(isset($categories["data"][0]["id"]) && !empty($categories["data"][0]["id"])){
  372.             return $categories["data"][0]["id"];
  373.         }else{
  374.             return $this->default_category_id
  375.         }
  376.     }
  377.     /**
  378.      * Fügt Attribute den eingelesenen Artikeln hinzu
  379.      * 
  380.      * Route("/add/attributes", name="add_attributes")
  381.      * 
  382.      * @return mixed
  383.      */
  384.     public function add_attributes()
  385.     {
  386.         // TODO suchtags über suchschlagwörter einbinden
  387.         $this->client_connect();
  388.         $conn_shopware $this->shop_connect();
  389.         $step_count 5;
  390.         $sql "SELECT 
  391.             *
  392.             , LOWER(HEX(product_id)) as product_id
  393.             , HEX(product_version_id) as product_version_id
  394.             , HEX(language_id) as language_id
  395.             , JSON_EXTRACT(product_translation.custom_fields,'$.custom_article_material_name') as custom_article_material_name
  396.             , JSON_EXTRACT(product_translation.custom_fields,'$.custom_article_materialgroup_name') as custom_article_materialgroup_name
  397.             , JSON_EXTRACT(product_translation.custom_fields,'$.custom_article_surface') as custom_article_surface
  398.             , JSON_EXTRACT(product_translation.custom_fields,'$.custom_article_articlegroup_name') as custom_article_articlegroup_name
  399.             -- , JSON_EXTRACT(product_translation.custom_fields,'$.custom_article_picture') as custom_article_picture
  400.             -- materialstärke
  401.         FROM product_translation
  402.         INNER JOIN product ON (product.id = product_id)
  403.         WHERE JSON_EXTRACT(product_translation.custom_fields,'$.custom_article_attributes_added') IS NULL
  404.         AND LOWER(HEX(language_id)) = '2fbb5fe2e29a4d70aa5854ce7ce3e20b'
  405.         LIMIT 10
  406.         ;";
  407.         $stmt $conn_shopware->prepare($sql);
  408.         $stmt->execute();
  409.         $products_sw $stmt->fetchAll();
  410.         // return $this->render('update/index.html.twig', ["return" => ['attribute' => $products_sw ]]);
  411.         foreach($products_sw as $product){
  412.             // produkte abholen die flag nicht haben
  413.             // einzelne customfelder durchgehen und attribute zuordnen
  414.             // nicht vorhandene skippen
  415.             
  416.             
  417.             unset($attribute);
  418.             unset($attributsliste);
  419.             $attributsliste=array();
  420.             $idliste=array();
  421.             if(isset($product["custom_article_material_name"]) && !empty($product["custom_article_material_name"] && $product["custom_article_material_name"] != "null")){
  422.                 // array_push($attributsliste,str_replace('"','',$product["custom_article_material_name"]));
  423.                 // array_push($idliste,$this->material_id);
  424.                 // var_dump($this->material_id);
  425.                 $attributsliste[$this->material_id]=str_replace('"','',$product["custom_article_material_name"]);
  426.             }
  427.             if(isset($product["custom_article_materialgroup_name"]) && !empty($product["custom_article_materialgroup_name"] && $product["custom_article_materialgroup_name"] != "null")){
  428.                 // array_push($attributsliste,str_replace('"','',$product["custom_article_materialgroup_name"]));
  429.                 // array_push($idliste,$this->materialgroup_id);
  430.                 // var_dump($this->materialgroup_id);
  431.                 $attributsliste[$this->materialgroup_id]=str_replace('"','',$product["custom_article_materialgroup_name"]);
  432.             }
  433.             if(isset($product["custom_article_surface"]) && !empty($product["custom_article_surface"]) && $product["custom_article_surface"] != "null"){
  434.                 // array_push($attributsliste,str_replace('"','',$product["custom_article_surface"]));
  435.                 // array_push($idliste,$this->surface_id);
  436.                 // var_dump($product["custom_article_surface"]);
  437.                 $attributsliste[$this->surface_id]=str_replace('"','',$product["custom_article_surface"]);
  438.             }
  439.             if(isset($product["custom_article_articlegroup_name"]) && !empty($product["custom_article_articlegroup_name"] && $product["custom_article_articlegroup_name"] != "null")){
  440.                 // array_push($attributsliste,str_replace('"','',$product["custom_article_articlegroup_name"]));
  441.                 // array_push($idliste,$this->articlegroup_id);
  442.                 // var_dump($this->articlegroup_id);
  443.                 $attributsliste[$this->articlegroup_id]=str_replace('"','',$product["custom_article_articlegroup_name"]);
  444.             }
  445.             // return $this->render('update/index.html.twig', ["return" => ['attribute' => $attributsliste ]]);
  446.             $attribute=array();
  447.             foreach($attributsliste as $key => $attribut){
  448.                 $sql "SELECT 
  449.                         *
  450.                         , LOWER(HEX(id)) as id
  451.                         , HEX(language_id) as language_id
  452.                         , HEX(property_group_id) as property_group_id
  453.                     FROM property_group_option
  454.                     INNER JOIN property_group_option_translation  ON (property_group_option.id = property_group_option_translation.property_group_option_id)
  455.                     -- WHERE LOWER(HEX(property_group_id)) = 'eadafb18e0c34aeb894dafddf1e107a0'
  456.                     WHERE `name` = '".$attribut."'
  457.                     AND LOWER(HEX(property_group_id)) = '".$key."'
  458.                     ;";
  459.                 $stmt $conn_shopware->prepare($sql);
  460.                 $stmt->execute();
  461.                 $search_property $stmt->fetchAll();
  462.                 // return $this->render('update/index.html.twig', ["return" => ['dearch' => $search_property ]]);
  463.                 if(isset($search_property) && is_array($search_property) && count($search_property) > 0) {
  464.                     array_push($attribute,array('id'=>$search_property[0]["id"]));
  465.                 }else{
  466.                     // Attribut nicht in shopware und muss hinzugefügt werden
  467.                     // var_dump("post ".$attribut." ".$key);exit;
  468.                     $post_product $this->client->request('POST''/api/property-group-option', array(
  469.                         'allow_redirects'=>array('strict'=>true),
  470.                         'headers' => $this->header,
  471.                         'body' => json_encode([
  472.                             'groupId' => $key,
  473.                             'name' => $attribut
  474.                         ])
  475.                     ));
  476.                     $sql "SELECT 
  477.                             *
  478.                             , LOWER(HEX(id)) as id
  479.                             , HEX(language_id) as language_id
  480.                             , HEX(property_group_id) as property_group_id
  481.                         FROM property_group_option
  482.                         INNER JOIN property_group_option_translation  ON (property_group_option.id = property_group_option_translation.property_group_option_id)
  483.                         -- WHERE LOWER(HEX(property_group_id)) = 'eadafb18e0c34aeb894dafddf1e107a0'
  484.                         WHERE `name` = '".$attribut."'
  485.                         AND LOWER(HEX(property_group_id)) = '".$key."'
  486.                         ;";
  487.                     $stmt $conn_shopware->prepare($sql);
  488.                     $stmt->execute();
  489.                     $search_property $stmt->fetchAll();
  490.                     // return $this->render('update/index.html.twig', ["return" => ['dearch' => $search_property ]]);
  491.                     array_push($attribute,array('id'=>$search_property[0]["id"]));
  492.                 }
  493.                 
  494.             }
  495.             // return $this->render('update/index.html.twig', ["return" => ['attribute' => $attribute ]]);
  496.             if(isset($product["custom_article_materialgroup_id"]) && !empty($product["custom_article_materialgroup_id"]) &&
  497.                isset($product["custom_article_material_id"]) && !empty($product["custom_article_material_id"])){
  498.                 $farbid=$this->getColorID($product["custom_article_materialgroup_id"],$product["custom_article_material_id"]);
  499.                 
  500.                 var_dump($farbid);exit;
  501.             }
  502.             $ProductFlat = new \App\Controller\shopware\ProductFlat();
  503.             $ProductFlat->setProperties($attribute);
  504.             $ProductFlat->setCustomFields(array(
  505.                 "custom_article_attributes_added" => "1"
  506.             ));
  507.             try {
  508.                 $post_product $this->client->request('PATCH''/api/product/'.$product["product_id"], array(
  509.                     // 'debug' => true,
  510.                     'allow_redirects'=>array('strict'=>true),
  511.                     "headers" => $this->header,
  512.                     "body" => $ProductFlat
  513.                     // 'body' => json_encode([
  514.                     //     'properties' => array(
  515.                     //         $attribute
  516.                     //     )
  517.                     //     ,
  518.                     // 'customFields' => array(
  519.                     //         "custom_article_attributes_added" => "1"
  520.                     //     )
  521.                     // ])
  522.                 ));
  523.             } catch (RequestException $e) {
  524.                 // echo Psr7\str($e->getRequest());
  525.                 // if ($e->hasResponse()) {
  526.                 //     echo Psr7\str($e->getResponse());
  527.                 // }
  528.                 if ($e->hasResponse()) {
  529.                     $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  530.                 }else{
  531.                     $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  532.                 }
  533.                 $response->headers->set('Content-Type''application/json');
  534.                 return $response;
  535.                 exit;
  536.             }
  537.             // return $this->render('update/index.html.twig', ["return" => ['dearch' => $product ]]);
  538.             
  539.             // try{
  540.             //     $post_product = $this->client->request('PATCH', '/api/product/'.$product["product_id"], array(
  541.             //         'headers' => $this->header,
  542.             //         'body' => json_encode([
  543.             //             'customFields' => array(
  544.             //                 "custom_article_attributes_added" => "1"
  545.             //             )
  546.             //         ])
  547.             //     ));
  548.             // } catch (RequestException $e) {
  549.             //     echo Psr7\str($e->getRequest());
  550.             //     if ($e->hasResponse()) {
  551.             //         echo Psr7\str($e->getResponse());
  552.             //     }
  553.             //     exit;
  554.             // }
  555.             // return $this->render('update/index.html.twig', ["return" => ['surfaces' => $product["product_id"]]]);
  556.         }
  557.         
  558.         if(count($products_sw) < 11){
  559.             return $this->redirect('/add/attributes');
  560.         }else{
  561.             return $this->render('update/index.html.twig', ["return" => ['Attribute' => "done" ]]);
  562.         }
  563.         // return $this->render('update/index.html.twig', ["return" => ['attribute' => "fertig" ]]);
  564.     }
  565.     
  566.     /**
  567.      * Importiert Attribute in Shopware
  568.      * 
  569.      * Route("/import/attributes", name="import_attributes")
  570.      * 
  571.      * @return mixed
  572.      */
  573.     public function import_attributes()
  574.     {
  575.         $this->client_connect();
  576.         $conn_shopware $this->shop_connect();
  577.         //gehe alle attribute durch und erstelle eigenschaften in entsprechenden gruppen
  578.         // wahlweise: aufruf wenn eigenschaften produkt zugeordnet werden sollen. nimmt eigenschaften entgegen,
  579.         // gleicht mit vorhandenen ab, erstellt neue und löscht überschüssige
  580.         // Oberflächen
  581.         $sql "SELECT 
  582.                 DISTINCT surface_name
  583.                 FROM surfaces
  584.             ;";
  585.         $stmt $conn_shopware->prepare($sql);
  586.         $stmt->execute();
  587.         $surfaces $stmt->fetchAll();
  588.         $surfaces array_map('current'$surfaces);
  589.         // $surface_id="0fcb8899440540a5aef143da8f82d096";
  590.         foreach($surfaces as $surface){
  591.             $sql "SELECT 
  592.                     *
  593.                     , HEX(id) as id
  594.                     , HEX(language_id) as language_id
  595.                     , HEX(property_group_id) as property_group_id
  596.                 FROM property_group_option
  597.                 INNER JOIN property_group_option_translation  ON (property_group_option.id = property_group_option_translation.property_group_option_id)
  598.                 WHERE LOWER(HEX(property_group_id)) = '".$this->surface_id."'
  599.                 AND `name` = '".$surface."'
  600.                 ;";
  601.             $stmt $conn_shopware->prepare($sql);
  602.             $stmt->execute();
  603.             $search_surfaces $stmt->fetchAll();
  604.             // return $this->render('update/index.html.twig', ["return" => ['surfaces' => $surfaces, 'search_surfaces' => $search_surfaces]]);
  605.             
  606.             if(isset($search_surfaces) && is_array($search_surfaces) && count($search_surfaces) > 0) {
  607.             }else{
  608.                 if(isset($surface) && !empty($surface)){
  609.                 // return $this->render('update/index.html.twig', ["return" => ['surfaces' => $surface, 'search_surfaces' => $search_surfaces]]);
  610.                     $post_product $this->client->request('POST''/api/property-group-option', array(
  611.                         'allow_redirects'=>array('strict'=>true),
  612.                         'headers' => $this->header,
  613.                         'body' => json_encode([
  614.                             'groupId' => $this->surface_id,
  615.                             'name' => $surface
  616.                         ])
  617.                     ));
  618.                 }
  619.             }
  620.         }
  621.         return $this->render('update/index.html.twig', ["return" => ['surfaces' => "surfaces done" ]]);
  622.         // Materialgruppen
  623.         $sql "SELECT 
  624.                 DISTINCT materialgroup_name
  625.                 FROM materialgroups
  626.             ;";
  627.             // OFFSET ".(($page - 1) * $step_count)."
  628.         $stmt $conn_shopware->prepare($sql);
  629.         $stmt->execute();
  630.         $materialgroups $stmt->fetchAll();
  631.         $materialgroups array_map('current'$materialgroups);
  632.         // $materialgroup_id="4d73f7a67c184ca294e778772b860050";
  633.         foreach($materialgroups as $materialgroup){
  634.             $sql "SELECT 
  635.                     *
  636.                     , HEX(id) as id
  637.                     , HEX(language_id) as language_id
  638.                     , HEX(property_group_id) as property_group_id
  639.                 FROM property_group_option
  640.                 INNER JOIN property_group_option_translation  ON (property_group_option.id = property_group_option_translation.property_group_option_id)
  641.                 WHERE LOWER(HEX(property_group_id)) = '".$this->materialgroup_id."'
  642.                 AND `name` = '".$materialgroup."'
  643.                 ;";
  644.             $stmt $conn_shopware->prepare($sql);
  645.             $stmt->execute();
  646.             $search_materialgroups $stmt->fetchAll();
  647.             // return $this->render('update/index.html.twig', ["return" => ['materialgroups' => $materialgroups, 'search_materialgroups' => $search_materialgroups]]);
  648.             if(isset($search_materialgroups) && is_array($search_materialgroups) && count($search_materialgroups) > 0) {
  649.             }else{
  650.                 if(isset($materialgroup) && !empty($materialgroup)){
  651.                     $post_product $this->client->request('POST''/api/property-group-option', array(
  652.                         'allow_redirects'=>array('strict'=>true),
  653.                         'headers' => $this->header,
  654.                         'body' => json_encode([
  655.                             'groupId' => $this->materialgroup_id,
  656.                             'name' => $materialgroup
  657.                         ])
  658.                     ));
  659.                 }
  660.             }
  661.         }
  662.         return $this->render('update/index.html.twig', ["return" => ['materialgroups' => "materialgroups done" ]]);
  663.         // Material
  664.         $sql "SELECT 
  665.                 DISTINCT material_name
  666.                 FROM materials
  667.             ;";
  668.             // OFFSET ".(($page - 1) * $step_count)."
  669.         $stmt $conn_shopware->prepare($sql);
  670.         $stmt->execute();
  671.         $materials $stmt->fetchAll();
  672.         $materials array_map('current'$materials);
  673.         // $material_id="ed4fe3bbe67941e4b4ed47c3308e32f2";
  674.         foreach($materials as $material){
  675.             $sql "SELECT 
  676.                     *
  677.                     , HEX(id) as id
  678.                     , HEX(language_id) as language_id
  679.                     , HEX(property_group_id) as property_group_id
  680.                 FROM property_group_option
  681.                 INNER JOIN property_group_option_translation  ON (property_group_option.id = property_group_option_translation.property_group_option_id)
  682.                 WHERE LOWER(HEX(property_group_id)) = '".$this->material_id."'
  683.                 AND `name` = '".$material."'
  684.                 ;";
  685.             $stmt $conn_shopware->prepare($sql);
  686.             $stmt->execute();
  687.             $search_materials $stmt->fetchAll();
  688.             // return $this->render('update/index.html.twig', ["return" => ['materials' => $materials, 'search_materials' => $search_materials]]);
  689.             if(isset($search_materials) && is_array($search_materials) && count($search_materials) > 0) {
  690.             }else{
  691.                 if(isset($material) && !empty($material)){
  692.                     $post_product $this->client->request('POST''/api/property-group-option', array(
  693.                         'allow_redirects'=>array('strict'=>true),
  694.                         'headers' => $this->header,
  695.                         'body' => json_encode([
  696.                             'groupId' => $this->material_id,
  697.                             'name' => $material
  698.                             ])
  699.                         ));
  700.                 }
  701.             }
  702.         }
  703.         return $this->render('update/index.html.twig', ["return" => ['materials' => "materials done" ]]);
  704.         // Artikelgruppe
  705.         $sql "SELECT 
  706.                 DISTINCT articlegroup_name
  707.                 FROM articlegroups
  708.             ;";
  709.         $stmt $conn_shopware->prepare($sql);
  710.         $stmt->execute();
  711.         $articlegroups $stmt->fetchAll();
  712.         $articlegroups array_map('current'$articlegroups);
  713.         // $articlegroup_id="025caac2d61d49968dc84c2e291adef6";
  714.         foreach($articlegroups as $articlegroup){
  715.             $sql "SELECT 
  716.                     *
  717.                     , HEX(id) as id
  718.                     , HEX(language_id) as language_id
  719.                     , HEX(property_group_id) as property_group_id
  720.                 FROM property_group_option
  721.                 INNER JOIN property_group_option_translation  ON (property_group_option.id = property_group_option_translation.property_group_option_id)
  722.                 WHERE LOWER(HEX(property_group_id)) = '".$this->articlegroup_id."'
  723.                 AND `name` = '".$articlegroup."'
  724.                 ;";
  725.             $stmt $conn_shopware->prepare($sql);
  726.             $stmt->execute();
  727.             $search_articlegroups $stmt->fetchAll();
  728.             // return $this->render('update/index.html.twig', ["return" => ['articlegroups' => $articlegroups, 'search_articlegroups' => $search_articlegroups]]);
  729.             if(isset($search_articlegroups) && is_array($search_articlegroups) && count($search_articlegroups) > 0) {
  730.             }else{
  731.                 $post_product $this->client->request('POST''/api/property-group-option', array(
  732.                     'allow_redirects'=>array('strict'=>true),
  733.                     'headers' => $this->header,
  734.                     'body' => json_encode([
  735.                         'groupId' => $this->articlegroup_id,
  736.                         'name' => $articlegroup
  737.                     ])
  738.                 ));
  739.             }
  740.         }
  741.         // return $this->render('update/index.html.twig', ["return" => ['articlegroups' => "articlegroups done" ]]);
  742.         return $this->render('update/index.html.twig', ["return" => ['surfaces' => $surfaces'materialgroups' => $materialgroups'materials' => $materials'articlegroups' => $articlegroups]]);
  743.     }
  744.     
  745.     /**
  746.      * Erwartet Storage-Array, returned formatierten Array der Eigenschaften für Variantenimport
  747.      */
  748.     public function get_variant_ids($storage)
  749.     {
  750.         $this->client_connect();
  751.         $conn_shopware $this->shop_connect();
  752.         $conn_akn $this->dump_connect();
  753.         $eigenschaften = array();
  754.         // Dicke
  755.         $dicke $storage["thickness"];
  756.         $eigenschaften[$this->thickness_id]=$dicke;
  757.         
  758.         // Fehler j/n
  759.         // $fehler = "Nein";
  760.         // if( $storage["fault_id"] != 0 && !empty($storage["fault_id"]) && $storage["fault_corner"] != 0 && !empty($storage["fault_corner"])){
  761.         //     $fehler = "Ja";
  762.         // }
  763.         // $eigenschaften[$this->fault_exists_id]=$fehler;
  764.         
  765.         // Oberfläche
  766.         $oberflaeche $storage["surface_name"];
  767.         $eigenschaften[$this->surface_id]=$oberflaeche;
  768.          // Artikelgruppe
  769.          if(isset($storage["articlegroup_name"]) && !empty($storage["articlegroup_name"])){
  770.             $artikelgruppe $storage["articlegroup_name"];
  771.             $eigenschaften[$this->articlegroup_id]=$artikelgruppe;
  772.         }
  773.         // // Steinnummer
  774.         // $steinnummer = $storage["stone_number"];
  775.         // $eigenschaften[$this->stonenumber_id]=$steinnummer;
  776.         // Blocknummer
  777.         // if(isset($storage["block_number"]) && !empty($storage["block_number"])){
  778.         //     $blocknummer = $storage["block_number"];
  779.         //     $eigenschaften[$this->blocknumber_id]=$blocknummer;
  780.         // }
  781.         if(isset($storage["block_advice"]) && !empty($storage["block_advice"])){
  782.             $blocknummer $storage["block_advice"];
  783.             //Zerlegt alle nummern in mat+oberflächenzahl+blocknummer und kürzt zeichen nach leerzeichen automatisch raus
  784.             // problemfall: keine oberflächennummer zwischen blocknummer und materialid
  785.             $blocknummer_zerlegt=array();
  786.             preg_match('/([a-zA-Z]+)([0-9,]+)([a-zA-Z0-9,]+)/'$storage["block_advice"], $blocknummer_zerlegt);
  787.             if(strlen(end($blocknummer_zerlegt))>1){
  788.                 $blocknummer end($blocknummer_zerlegt);
  789.                 $eigenschaften[$this->blocknumber_id]=$blocknummer;
  790.             }
  791.         }
  792.         // // Materialfarbe
  793.         if(isset($storage["materialgroup_id"]) && !empty($storage["materialgroup_id"]) &&
  794.         isset($storage["material_id"]) && !empty($storage["material_id"])){
  795.             $sql "SELECT *
  796.                     FROM materialcolors
  797.                     INNER JOIN `keys` ON (materialcolors.color_id = key_number)
  798.                     WHERE color_id != -1
  799.                     AND key_kind = 88
  800.                     AND mgr_id = '".$storage["materialgroup_id"]."'
  801.                     AND mar_id = '".$storage["material_id"]."'
  802.                 ;";
  803.             $stmt $conn_akn->prepare($sql);
  804.             $stmt->execute();
  805.             $colors $stmt->fetchAll();
  806.             // return $this->render('update/index.html.twig', ["return" => ['success' => $colors ]]);
  807.             if(isset($colors) && is_array($colors) && count($colors) > 0){
  808.                 $color explode('#',$colors[0]["key_text"]);
  809.                 $colors[0]["key_text"] = trim($color[0]);
  810.                 $eigenschaften[$this->color_id] = $colors[0]["key_text"];
  811.             }
  812.         }
  813.         $attribute=array();
  814.         foreach($eigenschaften as $key => $eigenschaft){
  815.             $sql "SELECT 
  816.                     *
  817.                     , LOWER(HEX(id)) as id
  818.                     , HEX(language_id) as language_id
  819.                     , HEX(property_group_id) as property_group_id
  820.                 FROM property_group_option
  821.                 INNER JOIN property_group_option_translation  ON (property_group_option.id = property_group_option_translation.property_group_option_id)
  822.                 -- WHERE LOWER(HEX(property_group_id)) = 'eadafb18e0c34aeb894dafddf1e107a0'
  823.                 WHERE `name` = '".$eigenschaft."'
  824.                 AND LOWER(HEX(property_group_id)) = '".$key."'
  825.                 ;";
  826.             $stmt $conn_shopware->prepare($sql);
  827.             $stmt->execute();
  828.             $search_property $stmt->fetchAll();
  829.             // var_dump($search_property);exit;
  830.             // return $this->render('update/index.html.twig', ["return" => ['dearch' => $search_property ]]);
  831.             
  832.             // array("optionId" => "d6c37799803148fe977e42d2fe7cc875",),
  833.             // array("optionId" => "340073aa01544985ab0d1f056c1f8422",)
  834.             
  835.             if(isset($search_property) && is_array($search_property) && count($search_property) > 0) {
  836.                 array_push($attribute,array('optionId'=>$search_property[0]["id"]));
  837.             }else{
  838.                 // Attribut nicht in shopware und muss hinzugefügt werden
  839.                 // var_dump($eigenschaften);
  840.                 // var_dump("post ".$eigenschaft." ".$key);
  841.                 $post_product $this->client->request('POST''/api/property-group-option', array(
  842.                     'allow_redirects'=>array('strict'=>true),
  843.                     'headers' => $this->header,
  844.                     'body' => json_encode([
  845.                         'groupId' => $key,
  846.                         'name' => $eigenschaft
  847.                     ])
  848.                 ));
  849.                 $optionId $post_product->getHeaders(); 
  850.                 // var_dump($optionId);exit;
  851.                 $optionId =  str_replace($this->base_url.'api/property-group-option/','',$optionId["Location"]); // ID des erstellten produktes
  852.                 array_push($attribute,array('optionId'=>$optionId[0]));
  853.             }
  854.         }
  855.         // var_dump($attribute);exit;
  856.         return $attribute;
  857.     }
  858.     /**
  859.      * test
  860.      * 
  861.      * @Route("/teste", name="teste")
  862.      * 
  863.      * @return mixed
  864.      */
  865.     public function teste()
  866.     {
  867.         $this->client_connect();
  868.         $conn_shopware $this->shop_connect();
  869.         // $conn_konfigurator = $this->konfig_connect();
  870.         // try {
  871.         //     $post_customer = $this->client->request('POST', '/api/sales-channel-payment-method', array(
  872.         //         // 'debug' => true,
  873.         //         'allow_redirects'=>array('strict'=>true),
  874.         //         'headers' => $this->header,
  875.         //         'body' => json_encode([
  876.         //             'salesChannelId' => "f8c383f75225488a8f6b89b7d64d6cf2",
  877.         //             'paymentMethodId' => "54008360b783458a9c82db74b1ca3f07"
  878.         //         ])
  879.         //     ));
  880.         //     $location = array_pop( $post_customer->getHeaders()['Location'] );
  881.         //     $payment_method_id = basename($location);
  882.         // } catch (RequestException $e) {
  883.         //     echo Psr7\str($e->getRequest());
  884.         //     if ($e->hasResponse()) {
  885.         //         echo Psr7\str($e->getResponse());
  886.         //     }
  887.         //     exit;
  888.         // }
  889.         $payment_method_id "54008360b783458a9c82db74b1ca3f07";
  890.     //     $sql = "INSERT INTO sales_channel_payment_method (sales_channel_id, payment_method_id)
  891.     //     VALUES (UNHEX('f8c383f75225488a8f6b89b7d64d6cf2'), UNHEX('".$payment_method_id."'))
  892.     // ;";
  893.     // $stmt = $conn_shopware->prepare($sql);
  894.     // $stmt->execute();
  895.         dd($payment_method_id);
  896.         $ProductFlat = new \App\Controller\shopware\ProductFlat();
  897.         $ProductFlat->setActive(true);
  898.         $ProductFlat->setCategories(array(array("id" =>  $categoryId)));
  899.         $ProductFlat->setDeliveryTimeId($this->deliverytime_id);
  900.         $ProductFlat->setDescription($article["article_name_1"]);
  901.         $ProductFlat->setIsCloseout(true);
  902.         $ProductFlat->setMarkAsTopseller(false);
  903.         $ProductFlat->setName($material_name);
  904.         $ProductFlat->setPrice(array(array(
  905.             "currencyId" => $this->currency_id,
  906.             "net" =>  0,
  907.             "gross" => 0,
  908.             "linked" => false
  909.         )));
  910.         $ProductFlat->setProductNumber($article["article_id"]);
  911.         $ProductFlat->setPurchaseUnit(1);
  912.         // $ProductFlat->setRestockTime();
  913.         // $ProductFlat->setUnitId("ff7d882bd1da432bb4e05e21eda66114");
  914.         $ProductFlat->setTaxId($this->tax_id);
  915.         $ProductFlat->setVisibilities(array(array(
  916.             "salesChannelId" => $this->saleschannel_id,
  917.             "visibility" => 30
  918.         )));
  919.         $ProductFlat->setCustomFields(array("custom_article_surface" => "testsurface",));
  920.         $ProductFlat->setHeight($article["thickness"]);
  921.         $ProductFlat->setLength($article["length"]);
  922.         $ProductFlat->setWidth($article["width"]);
  923.         $ProductFlat->setStock(0);
  924.         // $ProductFlat->setMaxPurchase(0);
  925.         if( isset($storage_option_list) && is_array($storage_option_list) && count($storage_option_list)>0){
  926.             $ProductFlat->setConfiguratorGroupConfig(array(array(
  927.                 "id" => "5eb85799b5914547adf6348ff2213d5e",
  928.                 "representation" => "box",
  929.                 "expressionForListings" => false
  930.             )));
  931.             $ProductFlat->setConfiguratorSettings(
  932.                 // array("optionId" => "d6c37799803148fe977e42d2fe7cc875",),
  933.                 // array("optionId" => "340073aa01544985ab0d1f056c1f8422",)
  934.                 $storage_option_list
  935.             );
  936.         }
  937.         // return $this->render('update/index.html.twig', ["return" => ['nummern' => $ProductFlat]]);
  938.         $poststring 'api/product';
  939.         // var_dump("post ".$article["article_id"]);
  940.         // var_dump("<pre>");
  941.         if( isset($material_name) && !empty($material_name)){
  942.             try {
  943.                 $post_product $this->client->request('PATCH''/'.$poststring, array(
  944.                     // "debug" => true,
  945.                     'allow_redirects'=>array('strict'=>true),
  946.                     "headers" => $this->header,
  947.                     "body" => $ProductFlat
  948.                 ));
  949.             } catch (RequestException $e) {
  950.                 // echo Psr7\str($e->getRequest());
  951.                 // if ($e->hasResponse()) {
  952.                 //     echo Psr7\str($e->getResponse());
  953.                 // }
  954.                 if ($e->hasResponse()) {
  955.                     $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  956.                 }else{
  957.                     $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  958.                 }
  959.                 $response->headers->set('Content-Type''application/json');
  960.                 return $response;
  961.                 exit;
  962.             }
  963.             $post_product_id $post_product->getHeaders(); 
  964.             $post_product_id =  str_replace($this->base_url.$poststring.'/','',$post_product_id["Location"]); // ID des erstellten produktes
  965.          }
  966.         return $this->render('update/index.html.twig', ["return" => ['ID' => $groupId ]]);
  967.         // $ProductFlat = new \App\Controller\shopware\ProductFlat();
  968.         // $ProductFlat->setName("testX-Varianten");
  969.         // $ProductFlat->setProductNumber("test_post");
  970.         // $ProductFlat->setStock(10);
  971.         // $ProductFlat->setPrice(array(array(
  972.         //     "currencyId" => "b7d2554b0ce847cd82f3ac9bd1c0dfca",
  973.         //     "gross" => 15,
  974.         //     "net" =>  10,
  975.         //     "linked" => false
  976.         // )));
  977.         // $ProductFlat->setTaxId("c5ddfa75e22b4889b6f628e1df0b347f");
  978.         // $ProductFlat->setConfiguratorGroupConfig(array(array(
  979.         //         "id" => "5eb85799b5914547adf6348ff2213d5e",
  980.         //         "representation" => "box",
  981.         //         "expressionForListings" => false
  982.         // )));
  983.         // $ProductFlat->setConfiguratorSettings(array(
  984.         //     array("optionId" => "d6c37799803148fe977e42d2fe7cc875",),
  985.         //     array("optionId" => "340073aa01544985ab0d1f056c1f8422",)));
  986.                         
  987.         // try {
  988.         //     $post_product = $this->client->request('POST', '/api/product', array(
  989.         //         "headers" => $this->header,
  990.         //         "body" => $ProductFlat
  991.         //         // "form_params" => array($variant_values)
  992.         //     ));
  993.         // } catch (RequestException $e) {
  994.         //     echo "<pre>";
  995.         //     echo Psr7\str($e->getRequest());
  996.         //     if ($e->hasResponse()) {
  997.         //         echo Psr7\str($e->getResponse());
  998.         //     }
  999.         //     exit;
  1000.         // }
  1001.         // $poststring = 'api/product';
  1002.         // $post_product_id = $post_product->getHeaders(); 
  1003.         // $post_product_id =  str_replace($this->base_url.$poststring.'/','',$post_product_id["Location"]); // ID des erstellten produktes
  1004.                    
  1005.         // $ProductFlat = new \App\Controller\shopware\ProductFlat();
  1006.         // $ProductFlat->setName("testX-Varianten 1");
  1007.         // $ProductFlat->setParentId($post_product_id[0]);
  1008.         // $ProductFlat->setProductNumber("test_post_1");
  1009.         // $ProductFlat->setStock(10);
  1010.         // $ProductFlat->setOptions(array(array("id"=> "d6c37799803148fe977e42d2fe7cc875")));
  1011.             
  1012.         // try {
  1013.         //     $post_product = $this->client->request('POST', '/api/product', array(
  1014.         //         "headers" => $this->header,
  1015.         //         "body" => $ProductFlat
  1016.         //     ));
  1017.         // } catch (RequestException $e) {
  1018.         //     echo "<pre>";
  1019.         //     echo Psr7\str($e->getRequest());
  1020.         //     if ($e->hasResponse()) {
  1021.         //         echo Psr7\str($e->getResponse());
  1022.         //     }
  1023.         //     exit;
  1024.         // }
  1025.                               
  1026.         // $ProductFlat = new \App\Controller\shopware\ProductFlat();
  1027.         // // $ProductFlat->setActive(true);
  1028.         // $ProductFlat->setName("testX-Varianten 2");
  1029.         // $ProductFlat->setParentId($post_product_id[0]);
  1030.         // $ProductFlat->setProductNumber("test_post_2");
  1031.         // $ProductFlat->setStock(10);
  1032.         // $ProductFlat->setOptions(array(
  1033.         //     array("id"=> "340073aa01544985ab0d1f056c1f8422") //ID der Varianteneigenschaft
  1034.         // ));
  1035.             
  1036.         // try {
  1037.         //     $post_product = $this->client->request('POST', '/api/product', array(
  1038.         //         "headers" => $this->header,
  1039.         //         "body" => $ProductFlat
  1040.         //     ));
  1041.         // } catch (RequestException $e) {
  1042.         //     echo "<pre>";
  1043.         //     echo Psr7\str($e->getRequest());
  1044.         //     if ($e->hasResponse()) {
  1045.         //         echo Psr7\str($e->getResponse());
  1046.         //     }
  1047.         //     exit;
  1048.         // }
  1049.              
  1050.         
  1051.         $name"9702_Nero Ass Classico geschliffen";
  1052.         // var_dump("<pre>");
  1053.         try {
  1054.             $search $this->client->request('GET''/api/media?filter[fileName]='.$name.'', array( 
  1055.             "headers" => $this->header,
  1056.             ));
  1057.         } catch (RequestException $e) {
  1058.             // echo Psr7\str($e->getRequest());
  1059.             // if ($e->hasResponse()) {
  1060.             //     echo Psr7\str($e->getResponse());
  1061.             // }
  1062.             if ($e->hasResponse()) {
  1063.                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  1064.             }else{
  1065.                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  1066.             }
  1067.             $response->headers->set('Content-Type''application/json');
  1068.             return $response;
  1069.             exit;
  1070.         }
  1071.         $search_picture json_decode($search->getBody()->getContents(),true);
  1072.         $sql "SELECT 
  1073.         *
  1074.         FROM materials 
  1075.         WHERE material_id = 'ERAM'
  1076.         AND materialgroup_id = 'GE'
  1077.         ;";
  1078.         // LIMIT ".$step_count." 
  1079.         // OFFSET ".(($page - 1) * $step_count)."
  1080.         $stmt $conn_shopware->prepare($sql);
  1081.         $stmt->execute();
  1082.         $material $stmt->fetchAll();
  1083.         return $this->render('update/index.html.twig', ["return" => ['nummern' => $material ]]);
  1084.         $sql "SELECT 
  1085.                 *
  1086.                 ,articles.material_id as material_id
  1087.             FROM articles
  1088.             LEFT JOIN materials ON (materials.material_id = articles.material_id)
  1089.             LEFT JOIN materialgroups ON (materialgroups.materialgroup_id = articles.materialgroup_id)
  1090.             LEFT JOIN surfaces ON (surfaces.surface_id = articles.surface_id AND surfaces.materialgroup_id = articles.materialgroup_id)
  1091.             WHERE article_id = '00632'
  1092.             ORDER BY articles.id ASC
  1093.             ;";
  1094.         $stmt $conn_shopware->prepare($sql);
  1095.         $stmt->execute();
  1096.         $articles $stmt->fetchAll();
  1097.         // return $this->render('update/index.html.twig', ["return" => ['nummern' => $artikelnummern, 'zeit' => $articles]]);
  1098.         $sql "SELECT 
  1099.                     *
  1100.             FROM storages 
  1101.             LEFT JOIN surfaces ON (surfaces.surface_id = storages.surface_id  AND storages.materialgroup_id = surfaces.materialgroup_id)
  1102.             LEFT JOIN articlegroups ON (articlegroups.articlegroup_id = storages.articlegroup_id)
  1103.             LEFT JOIN storagefaults ON storagefaults.fault_id = storages.fault_id 
  1104.             WHERE article_id = '".$articles[0]["article_id"]."'
  1105.             ORDER BY storages.id ASC
  1106.             ;";
  1107.         $stmt $conn_shopware->prepare($sql);
  1108.         $stmt->execute();
  1109.         $storages $stmt->fetchAll();
  1110.         return $this->render('update/index.html.twig', ["return" => ['shop' => $articles'storages' => $storages ]]);
  1111.         return $this->render('update/index.html.twig', ["return" => ['shop' => $post_product_id ]]);
  1112.         return $this->render('update/index.html.twig', ["return" => ['shop' => "true" ]]);
  1113.     }
  1114.     /**
  1115.      * Importiert Zulauf Produkte
  1116.      * 
  1117.      * @Route("/import/products_zulauf", name="import_products_zulauf")
  1118.      * 
  1119.      * @return mixed
  1120.      */
  1121.     public function import_products_zulauf() {
  1122.         $this->client_connect();
  1123.         $conn_shopware $this->shop_connect();
  1124.         $conn_akn $this->dump_connect();
  1125.         $step_count 10;
  1126.         
  1127.         $sql "INSERT INTO pixel_dates (FunctionName,StartEnde,UpdateDatetime)
  1128.                 VALUES(?,?,?)";
  1129.         $stmtt $conn_shopware->prepare($sql);
  1130.         $stmtt->execute(array( 
  1131.             "import/products_zulauf",
  1132.             "Start",
  1133.             date('Y-m-d H:i:s')
  1134.         ));
  1135.         # holt alle ZULAUF Artikel ab die eine passende Materialaufschlüsselung haben
  1136.         $sql "SELECT *
  1137.             , articles.material_id as material_id
  1138.             , articles.materialgroup_id as materialgroup_id
  1139.             , storages.id as storage_id
  1140.             -- , CONCAT(storages.article_id, '_' ,storages.id) AS product_number
  1141.             -- , CONCAT('zulauf', '_', storages.material_id, '_', storages.materialgroup_id, '_', storages.id) AS product_number
  1142.             , storages.stone_number AS product_number
  1143.             , CONCAT('Zulauf_', storages.material_id, '_', storages.materialgroup_id) AS product_number_parent
  1144.             -- , COALESCE(NULLIF(storages.picture, ''), NULLIF(materials.picture, ''), 'default.jpg') AS picture   -- select picture
  1145.             , storages.picture AS storages_picture
  1146.             , materials.picture AS materials_picture
  1147.             , COALESCE(NULLIF(materials.picture, ''), 'default.jpg') AS picture_parent   -- select picture
  1148.             FROM articles
  1149.             LEFT JOIN materials ON (articles.material_id = materials.material_id AND articles.materialgroup_id = materials.materialgroup_id )
  1150.             -- LEFT JOIN articlegroups ON (articlegroups.articlegroup_id = articles.article_id)
  1151.             LEFT JOIN articlegroups ON (articles.articlegroup_id = articlegroups.articlegroup_id)
  1152.             LEFT JOIN materialgroups ON (materialgroups.materialgroup_id = articles.materialgroup_id)
  1153.             LEFT JOIN surfaces ON (surfaces.surface_id = articles.surface_id AND surfaces.materialgroup_id = articles.materialgroup_id)
  1154.             INNER JOIN storages ON (articles.article_id = storages.article_id)
  1155.             LEFT JOIN storagefaults ON (storages.fault_id = storagefaults.fault_id)
  1156.             WHERE storages.stone_number IS NOT NULL
  1157.             AND storages.storage = 'ZULAUF'
  1158.             AND storages.upload_done IS NULL
  1159.             AND material_name IS NOT NULL
  1160.             -- AND articles.material_id = 'BCCD'
  1161.             ORDER BY articles.id ASC
  1162.             LIMIT ".$step_count."
  1163.         ;";
  1164.         $stmt $conn_akn->prepare($sql);
  1165.         $stmt->execute();
  1166.         $articles $stmt->fetchAll();
  1167.         // dd($articles);
  1168.         // echo "<pre>"; var_dump($articles); exit;
  1169.         # NEXT STEP
  1170.         if(count($articles) >= $step_count) { 
  1171.             $nextPage true;
  1172.         } else {
  1173.             $nextPage false;
  1174.         }
  1175.         # PICTURE
  1176.         // dd($articles[0]['picture']);
  1177.         // $this->getParameter('kernel.project_dir')."/public/produktbilder"
  1178.         // $materialid = $articles[0]["material_id"];
  1179.         // $materialname = $articles[0]["material_name"];
  1180.         // $materialgruppenname = $articles[0]["materialgroup_name"];
  1181.         // $materialgruppenid = $articles[0]["materialgroup_id"];
  1182.         // $oberflaeche = $articles[0]["surface_name"];
  1183.         
  1184.         foreach($articles as $article) {
  1185.             $productParentId "";
  1186.             $productId "";
  1187.             // $productNumber = $article['article_id']
  1188.             // var_dump($productNumber);
  1189.             # REMOVE SUFFIX FROM PICTURE NAME
  1190.             // if(strpos($article['picture'], '-##-')) {
  1191.             //     $article['picture'] = substr($article['picture'], 0, -5);
  1192.             // }
  1193.             if(strpos($article['storages_picture'], '-##-')) {
  1194.                 $article['storages_picture'] = substr($article['picture'], 0, -5);
  1195.             }
  1196.             if(strpos($article['materials_picture'], '-##-')) {
  1197.                 $article['storages_picture'] = substr($article['picture'], 0, -5);
  1198.             }
  1199.             # END EDIT PICTURE NAME
  1200.             $storage_option_list $this->get_variant_ids($article); //übergibt storage, returned formatierten array für variantenerstellung
  1201.             # CHECK IF PARENT PRODUCT ALREADY EXISTS ###################################
  1202.             // $sql = "SELECT *, LOWER(HEX(id)) as id
  1203.             $sql "SELECT LOWER(HEX(product.id)) as id
  1204.                 FROM product_translation
  1205.                 INNER JOIN product ON (product.id = product_translation.product_id)
  1206.                 INNER JOIN product_category ON ( product_translation.product_id = product_category.product_id)
  1207.                 -- INNER JOIN category ON ( product_category.category_id = category.category_id)
  1208.                 -- INNER JOIN category_translation ON ( product_category.category_id = category_translation.category_id)
  1209.                 WHERE LOWER(HEX(product_category.category_id)) = '".$this->zulauf_id."'
  1210.                 -- AND JSON_EXTRACT(product_translation.custom_fields,'$.custom_article_material_id') = '".$article['material_id']."'
  1211.                 -- AND JSON_EXTRACT(product_translation.custom_fields,'$.custom_article_materialgroup_id')  = '".$article['materialgroup_id']."'
  1212.                 AND product.product_number = '"$article['product_number_parent'] . "'
  1213.                 AND parent_id IS NULL
  1214.                 LIMIT 1
  1215.             ;";
  1216.             $stmt $conn_shopware->prepare($sql);
  1217.             $stmt->execute();
  1218.             $search_product $stmt->fetchAll();
  1219.             # TODO: IF PRODUCT ALREADY EXISTS
  1220.             if(isset($search_product[0]["id"]) && !empty($search_product[0]["id"]) && count($search_product) > 0) {
  1221.                 $productParentId $search_product[0]["id"];
  1222.             }
  1223.             # SET DATA FOR PRODUCT ###################################
  1224.             $ProductFlat = new \App\Controller\shopware\ProductFlat();
  1225.             $ProductFlat->setActive(true);
  1226.             // $ProductFlat->setCategories(array(array("id" => $this->getCategoryId($materialgruppenname, $materialgruppenid))));
  1227.             $ProductFlat->setCategories(array(array("id" => $this->zulauf_id)));
  1228.             $ProductFlat->setDeliveryTimeId($this->deliverytime_id);
  1229.             $ProductFlat->setDescription($article["article_name_1"]);
  1230.             $ProductFlat->setIsCloseout(false); // ABVERKAUF - Wichtig, da Anzahl = 0
  1231.             $ProductFlat->setMarkAsTopseller(false);
  1232.             // $ProductFlat->setName('Zulauf '.$article['product_number_parent'] .' ('.  date('Y-m-d H:m:s') .')');
  1233.             // $ProductFlat->setName('Zulauf '.$article['product_number_parent']);
  1234.             // $ProductFlat->setName('ZULAUF: ' . $article['product_number_parent']);
  1235.             $ProductFlat->setName('Zulauf: ' $article['material_name']);
  1236.             
  1237.             // $ProductFlat->setName('Zulauf '.$materialname);
  1238.             // $ProductFlat->setName('toller lustiger testname');
  1239.             $ProductFlat->setPrice(array(array(
  1240.                 "currencyId" => $this->currency_id,
  1241.                 "net" =>  0,
  1242.                 "gross" => 0,
  1243.                 "linked" => false
  1244.             )));
  1245.             // $ProductFlat->setProductNumber($article['product_number']);
  1246.             // $ProductFlat->setProductNumber($article["materialgroup_id"]);
  1247.             $ProductFlat->setProductNumber($article['product_number_parent']);
  1248.             $ProductFlat->setPurchaseUnit(1);
  1249.             // $ProductFlat->setRestockTime();
  1250.             // $ProductFlat->setUnitId("ff7d882bd1da432bb4e05e21eda66114");
  1251.             $ProductFlat->setTaxId($this->tax_id);
  1252.             // $ProductFlat->setCustomFields();
  1253.             $custom_fields = array();
  1254.             // $custom_fields["custom_article_surface"] = $article["surface_name"];
  1255.             // $custom_fields["custom_article_search_word"] = $article["search_word"]; 
  1256.             // $custom_fields["custom_article_name_1"] = $article["article_name_1"];
  1257.             // $custom_fields["custom_article_name_2"] = $article["article_name_2"];
  1258.             // $custom_fields["custom_article_material_id"] = $article["material_id"];
  1259.             // $custom_fields["custom_article_materialgroup_id"] = $article["materialgroup_id"];
  1260.             // // $custom_fields["custom_article_material_name"] = $materialname;
  1261.             // $custom_fields["custom_article_material_name"] = $article['material_name'];
  1262.             // $custom_fields["custom_article_materialgroup_name"] = $article["materialgroup_name"];
  1263.             // $custom_fields["custom_article_picture"] = $article["picture"];
  1264.             // $custom_fields["custom_article_material_id"] = $article["material_id"];
  1265.             // $custom_fields["custom_article_materialgroup_id"] = $article["materialgroup_id"];
  1266.             $search = array("{""}""("")""/""\\""@"":");
  1267.             $replace = array("""""""""""""""");
  1268.             $custom_fields["custom_article_surface"]            = str_replace($search$replace$article["surface_name"]);
  1269.             $custom_fields["custom_article_search_word"]        = str_replace($search$replace$article["search_word"]);
  1270.             $custom_fields["custom_article_name_1"]             = str_replace($search$replace$article["article_name_1"]);
  1271.             $custom_fields["custom_article_name_2"]             = str_replace($search$replace$article["article_name_2"]);
  1272.             $custom_fields["custom_article_material_id"]        = str_replace($search$replace$article["material_id"]);
  1273.             $custom_fields["custom_article_materialgroup_id"]   = str_replace($search$replace$article["materialgroup_id"]);
  1274.             $custom_fields["custom_article_material_name"]      = str_replace($search$replace$article['material_name']);
  1275.             $custom_fields["custom_article_materialgroup_name"] = str_replace($search$replace$article["materialgroup_name"]);
  1276.             $custom_fields["custom_article_picture"]            = str_replace($search$replace$article["picture"]);
  1277.             $custom_fields["custom_article_id"]                 = str_replace($search$replace$article["article_id"]);
  1278.             $custom_fields["custom_article_stone_number"]       = str_replace($search$replace$article["stone_number"]);
  1279.             $custom_fields["custom_article_block_number"]       = str_replace($search$replace$article["block_advice"]);
  1280.             $custom_fields["custom_article_search_word"]        = str_replace($search$replace$article["search_word"]);
  1281.             $custom_fields["custom_article_advice"]             = str_replace($search$replace$article["advice"]);
  1282.             $custom_fields["custom_article_storage"]            = str_replace($search$replace$article["storage"]);
  1283.             $custom_fields["custom_article_amount_2"]           = str_replace($search$replace$article["amount_2"]);
  1284.             $custom_fields["custom_article_articlegroup_name"]  = str_replace($search$replace$article["articlegroup_name"]);
  1285.             $custom_fields["custom_article_fault_id"]           = str_replace($search$replace$article["fault_name"]);
  1286.             $custom_fields["custom_article_fault_corner"]       = str_replace($search$replace$article["fault_corner"]);
  1287.             $custom_fields["custom_article_additional_advice"]  = str_replace($search$replace$article["additional_advice"]);
  1288.             
  1289.             $ProductFlat->setCustomFields(
  1290.                 $custom_fields
  1291.             );
  1292.             $ProductFlat->setHeight($article["thickness"]);
  1293.             $ProductFlat->setLength($article["length"]);
  1294.             $ProductFlat->setWidth($article["width"]);
  1295.             $ProductFlat->setStock(0);
  1296.             $ProductFlat->setMaxPurchase(0);
  1297.             $ProductFlat->setVisibilities(array(array(
  1298.                 "salesChannelId" => $this->saleschannel_id,
  1299.                 "visibility" => 30
  1300.             )));
  1301.             # UPDATE OR INSERT ###################################
  1302.             $poststring 'api/product';
  1303.             
  1304.             
  1305.             $storage_option_list $this->get_variant_ids($article); //übergibt storage, returned formatierten array für variantenerstellung
  1306.             
  1307.             # PARENT ###################################
  1308.             if( isset($article['material_name']) && trim($article['material_name']) != '') {
  1309.                 if($productParentId == '') {
  1310.                     # INSERT PARENT ARTICLE ##########################
  1311.                     if( isset($storage_option_list) && is_array($storage_option_list) && count($storage_option_list) > 0) {
  1312.                         # TODO: vorbereitung der Varianten
  1313.                         // $ProductFlat->setConfiguratorGroupConfig(array(array(
  1314.                         //     // "id" => "5eb85799b5914547adf6348ff2213d5e",
  1315.                         //     // "representation" => "box",
  1316.                         //     // "expressionForListings" => false
  1317.                         // )));
  1318.                         $ProductFlat->setConfiguratorGroupConfig(array(
  1319.                             array(
  1320.                                 "id" => $this->thickness_id,
  1321.                                 "representation" => "box",
  1322.                                 "expressionForListings" => false
  1323.                             ),
  1324.                             array(
  1325.                                 "id" => $this->articlegroup_id,
  1326.                                 "representation" => "box",
  1327.                                 "expressionForListings" => false
  1328.                             ),
  1329.                             array(
  1330.                                 "id" => $this->surface_id,
  1331.                                 "representation" => "box",
  1332.                                 "expressionForListings" => false
  1333.                             ),
  1334.                             array(
  1335.                                 "id" => $this->length_id,
  1336.                                 "representation" => "box",
  1337.                                 "expressionForListings" => false
  1338.                             ),
  1339.                             array(
  1340.                                 "id" => $this->width_id,
  1341.                                 "representation" => "box",
  1342.                                 "expressionForListings" => false
  1343.                             ),
  1344.                             array(
  1345.                                 "id" => $this->amount_id,
  1346.                                 "representation" => "box",
  1347.                                 "expressionForListings" => false
  1348.                             ),
  1349.                             array(
  1350.                                 "id" => $this->surfacearea_id,
  1351.                                 "representation" => "box",
  1352.                                 "expressionForListings" => false
  1353.                             ),
  1354.                             array(
  1355.                                 "id" => $this->meter_id,
  1356.                                 "representation" => "box",
  1357.                                 "expressionForListings" => false
  1358.                             ),
  1359.                             array(
  1360.                                 "id" => $this->sa_id,
  1361.                                 "representation" => "box",
  1362.                                 "expressionForListings" => false
  1363.                             ),
  1364.                             array(
  1365.                             "id" => $this->blocknumber_id,
  1366.                                 "representation" => "box",
  1367.                                 "expressionForListings" => false
  1368.                             ),
  1369.                             array(
  1370.                                 "id" => $this->material_id,
  1371.                                 "representation" => "box",
  1372.                                 "expressionForListings" => false
  1373.                             ),
  1374.                             array(
  1375.                                 "id" => $this->materialgroup_id,
  1376.                                 "representation" => "box",
  1377.                                 "expressionForListings" => false
  1378.                             ),
  1379.                             array(
  1380.                                 "id" => $this->fault_exists_id,
  1381.                                 "representation" => "box",
  1382.                                 "expressionForListings" => false
  1383.                             ),
  1384.                             array(
  1385.                                 "id" => $this->squaremeterprice_id,
  1386.                                 "representation" => "box",
  1387.                                 "expressionForListings" => false
  1388.                             ),
  1389.                             array(
  1390.                                 "id" => $this->stonenumber_id,
  1391.                                 "representation" => "box",
  1392.                                 "expressionForListings" => false
  1393.                             )
  1394.                         ));
  1395.                         # SET CONFIGURATOR SETTINGS
  1396.                         // dd($storage_option_list);
  1397.                         // $ProductFlat->setConfiguratorSettings($storage_option_list);
  1398.                         
  1399.                         // $sol_option=array();
  1400.                         // foreach($storage_option_list as $key => $sol) {
  1401.                         //     // $sol_option[$key]["productId"] = "";
  1402.                         //     $sol_option[$key]["id"] = $sol["optionId"];
  1403.                         // }
  1404.                         // $ProductFlat->setConfiguratorSettings($sol_option);
  1405.                         
  1406.                         try {
  1407.                             $post_product $this->client->request('POST''/'.$poststring, array(
  1408.                                 //    "debug" => true,
  1409.                                 'allow_redirects'=>array('strict'=>true),
  1410.                                 "headers" => $this->header,
  1411.                                 "body" => $ProductFlat
  1412.                             ));
  1413.                         } catch (RequestException $e) {
  1414.                             // echo Psr7\str($e->getRequest());
  1415.                             // if ($e->hasResponse()) {
  1416.                             //     echo Psr7\str($e->getResponse());
  1417.                             // }
  1418.                             if ($e->hasResponse()) {
  1419.                                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  1420.                             }else{
  1421.                                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  1422.                             }
  1423.                             $response->headers->set('Content-Type''application/json');
  1424.                             return $response;
  1425.                             exit;
  1426.                         }
  1427.                         # PARENT ID
  1428.                         $productParentId $post_product->getHeaders(); 
  1429.                         $productParentId =  str_replace($this->base_url.$poststring.'/','',$productParentId["Location"]); // ID des erstellten produktes
  1430.                         $productParentId $productParentId[0];
  1431.                     }
  1432.                 }
  1433.                 # INSERT VARIANT ##########################
  1434.                 $ProductFlat->setParentId($productParentId);
  1435.                 $ProductFlat->setProductNumber($article["product_number"]);
  1436.                 $ProductFlat->setConfiguratorGroupConfig(null);
  1437.                 $ProductFlat->setConfiguratorSettings(null);
  1438.                 // $ProductFlat->setName('Zulauf '.$article['product_number_parent']);
  1439.                 // $ProductFlat->setName($article['product_number_parent']);
  1440.                 $ProductFlat->setName('Zulauf: ' $article['material_name']);
  1441.                 
  1442.                 # PICTURES AND IMAGES ##########################
  1443.                 
  1444.                 // # storages_picture
  1445.                 // $sql = "SELECT LOWER(HEX(id)) AS id
  1446.                 //     FROM media
  1447.                 //     WHERE file_name = '".pathinfo($article['storages_picture'], PATHINFO_FILENAME)."'
  1448.                 //     LIMIT 1
  1449.                 // ;";
  1450.                 // $stmt = $conn_shopware->prepare($sql);
  1451.                 // $stmt->execute();
  1452.                 // $pictures = $stmt->fetchAll();
  1453.                 // if(!(isset($pictures) && is_array($pictures) && count($pictures) > 0)) {
  1454.                 //     # MATERIALS_PICTURE
  1455.                 //     $sql = "SELECT LOWER(HEX(id)) AS id
  1456.                 //         FROM media
  1457.                 //         WHERE file_name = '".pathinfo($article['materials_picture'], PATHINFO_FILENAME)."'
  1458.                 //         LIMIT 1
  1459.                 //     ;";
  1460.                 //     $stmt = $conn_shopware->prepare($sql);
  1461.                 //     $stmt->execute();
  1462.                 //     $pictures = $stmt->fetchAll();
  1463.                 //     if(!(isset($pictures) && is_array($pictures) && count($pictures) > 0)) {
  1464.                 //         # DEFAULT_IMAGE
  1465.                 //         $sql = "SELECT LOWER(HEX(id)) AS id
  1466.                 //             FROM media
  1467.                 //             WHERE file_name = '".pathinfo($this->default_image_filename, PATHINFO_FILENAME)."'
  1468.                 //             LIMIT 1
  1469.                 //         ;";
  1470.                 //         $stmt = $conn_shopware->prepare($sql);
  1471.                 //         $stmt->execute();
  1472.                 //         $pictures = $stmt->fetchAll();
  1473.                 //     }
  1474.                 // }
  1475.                 // // $sql = "SELECT LOWER(HEX(id)) AS id
  1476.                 // //     FROM media
  1477.                 // //     WHERE file_name = '".pathinfo($article['picture'], PATHINFO_FILENAME)."'
  1478.                 // //     LIMIT 1
  1479.                 // // ;";
  1480.                 // // $stmt = $conn_shopware->prepare($sql);
  1481.                 // // $stmt->execute();
  1482.                 // // $pictures = $stmt->fetchAll();
  1483.                 // if(isset($pictures) && is_array($pictures) && count($pictures) > 0) {
  1484.                 //     $pictureId = $pictures[0]['id'];
  1485.                 //     # CHECK IF COVER IS ALREADY ASSIGNED
  1486.                 //     $sql = "SELECT LOWER(HEX(media.id)) AS media_id,
  1487.                 //         CONCAT (media.file_name, '.', media.file_extension) AS file_name
  1488.                 //         FROM product
  1489.                 //         INNER JOIN product_media ON ( product.cover = product_media.id)
  1490.                 //         INNER JOIN media ON (product_media.media_id = media.id)
  1491.                 //         WHERE LOWER(HEX(product.id)) = '".$productId."'
  1492.                 //         LIMIT 1
  1493.                 //     ;";
  1494.                 //     $stmt = $conn_shopware->prepare($sql);
  1495.                 //     $stmt->execute();
  1496.                 //     $cover_SELECT = $stmt->fetchAll();
  1497.                 //     if( !(isset($cover_SELECT) && is_array($cover_SELECT) && count($cover_SELECT) > 0)
  1498.                 //         || (isset($cover_SELECT['filename']) && strtolower( $article['materials_picture'] ) != strtolower( $cover_SELECT['filename']) ) ) {
  1499.                 //         # NO COVER ASSIGNED, OR DIFFERENT COVER:
  1500.                 //         $cover = new stdClass();
  1501.                 //         $cover->type = "product_media";
  1502.                 //         $cover->mediaId = $pictureId;
  1503.                 //         $ProductFlat->setCover($cover);
  1504.                         
  1505.                 //         $media = new stdClass();
  1506.                 //         $media->type = "product_media";
  1507.                 //         $media->mediaId = $pictureId;
  1508.                 //         $ProductFlat->setMedia(array($media));
  1509.         
  1510.                 //     }
  1511.                 // }
  1512.                 
  1513.                 if( isset($storage_option_list) && is_array($storage_option_list) && count($storage_option_list) > 0) {
  1514.                     $sol_option = [];
  1515.                     foreach($storage_option_list as $key => $sol){
  1516.                         $sol_option[$key]["id"] = $sol["optionId"];
  1517.                     }
  1518.                     $ProductFlat->setOptions$sol_option );
  1519.                     $sql "SELECT LOWER(HEX(product.id)) as id
  1520.                         FROM product
  1521.                         -- WHERE LOWER( product_number ) = LOWER('".$ProductFlat->getProductNumber()."')
  1522.                         WHERE product_number = ".$ProductFlat->getProductNumber()."
  1523.                         LIMIT 1
  1524.                     ;";
  1525.                     $stmt $conn_shopware->prepare($sql);
  1526.                     $stmt->execute();
  1527.                     $product_SELECT $stmt->fetchAll();
  1528.                     
  1529.                     if(isset($product_SELECT) && is_array($product_SELECT) && count($product_SELECT) > 0) {
  1530.                         # UPDATE
  1531.                         $ProductFlat->setVisibilities([]);
  1532.                         try {
  1533.                             $post_product $this->client->request('PATCH''/api/product/'.$product_SELECT[0]['id'], array(
  1534.                                 //    "debug" => true,
  1535.                                 'allow_redirects'=>array('strict' => true),
  1536.                                 "headers" => $this->header,
  1537.                                 "body" => $ProductFlat
  1538.                             ));
  1539.                         } catch (RequestException $e) {
  1540.                             // echo Psr7\str($e->getRequest());
  1541.                             // if ($e->hasResponse()) {
  1542.                             //     echo Psr7\str($e->getResponse());
  1543.                             // }
  1544.                             if ($e->hasResponse()) {
  1545.                                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  1546.                             }else{
  1547.                                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  1548.                             }
  1549.                             $response->headers->set('Content-Type''application/json');
  1550.                             return $response;
  1551.                             exit;
  1552.                         }
  1553.                         $productId $product_SELECT[0]['id'];
  1554.                     } else {
  1555.                         # INSERT
  1556.                         try {
  1557.                             $post_product $this->client->request('POST''/api/product', array(
  1558.                                 //    "debug" => true,
  1559.                                 'allow_redirects'=>array('strict' => true),
  1560.                                 "headers" => $this->header,
  1561.                                 "body" => $ProductFlat
  1562.                             ));
  1563.                         } catch (RequestException $e) {
  1564.                             // echo Psr7\str($e->getRequest());
  1565.                             // if ($e->hasResponse()) {
  1566.                             //     echo Psr7\str($e->getResponse());
  1567.                             // }
  1568.                             if ($e->hasResponse()) {
  1569.                                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  1570.                             }else{
  1571.                                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  1572.                             }
  1573.                             $response->headers->set('Content-Type''application/json');
  1574.                             return $response;
  1575.                             exit;
  1576.                         }
  1577.                         // # PARENT ID
  1578.                         $productId $post_product->getHeaders(); 
  1579.                         $productId str_replace($this->base_url.$poststring.'/','',$productId["Location"]); // ID des erstellten produktes
  1580.                         $productId $productId[0];
  1581.                     }
  1582.                     
  1583.                     //###*** Prüfe auf Bild und füge es hinzu, falls es nicht vorhanden ist.
  1584.                     if(isset( $article['storages_picture']) && !empty( $article['storages_picture']) &&  $article['storages_picture'] != ""){
  1585.                         $this->add_image($article['storages_picture'], $productIdfalse); 
  1586.                     }elseif(isset( $article['materials_picture']) && !empty( $article['materials_picture']) &&  $article['materials_picture'] != ""){
  1587.                         $this->add_image($article['materials_picture'], $productIdfalse); 
  1588.                     }else{
  1589.                         $this->add_image($this->default_image_filename$productIdfalse);
  1590.                     }
  1591.                     // echo "<pre>"; var_dump($ProductFlat); exit;
  1592.                     # UPDATE KEYWORDS ###################
  1593.                     $keywords = [ $article['search_word'] ];
  1594.                     try {
  1595.                         $post_product $this->client->request('PATCH''/api/product/'.$productId, array(
  1596.                             //    "debug" => true,
  1597.                             'allow_redirects'=>array('strict' => true),
  1598.                             'headers' => $this->header,
  1599.                             'body' => json_encode([
  1600.                                 "customSearchKeywords"=> $keywords   // DAS IST KEIN MERGE: WERTE WERDEN KNALLHART ÜBERSCHRIEBEN
  1601.                             ])
  1602.                         ));
  1603.                     } catch (RequestException $e) {
  1604.                         // echo Psr7\str($e->getRequest());
  1605.                         // if ($e->hasResponse()) {
  1606.                         //     echo Psr7\str($e->getResponse());
  1607.                         // }
  1608.                         if ($e->hasResponse()) {
  1609.                             $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  1610.                         }else{
  1611.                             $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  1612.                         }
  1613.                         $response->headers->set('Content-Type''application/json');
  1614.                         return $response;
  1615.                         exit;
  1616.                     }
  1617.                     # END UPDATE KEYWORDS ###################
  1618.                 }
  1619.                 // echo "<pre>"; var_dump(__LINE__); exit;
  1620.                 # UPDATE PARENT, FEHLENDE OPTIONEN IN VARIANTENGENERATOR HINZUFÜGEN ##########################
  1621.                 $sql "SELECT *
  1622.                     , LOWER(HEX(product_id)) as product_id
  1623.                     , LOWER(HEX(property_group_option_id)) as property_group_option_id
  1624.                     FROM product_configurator_setting
  1625.                     WHERE LOWER(HEX(product_id)) = '".$productParentId."'
  1626.                 ;";
  1627.                 $stmt $conn_shopware->prepare($sql);
  1628.                 $stmt->execute();
  1629.                 $config_settings $stmt->fetchAll();
  1630.                 $option_list array_column($storage_option_list'optionId');
  1631.                 $config_list array_column($config_settings'property_group_option_id');
  1632.                 foreach($option_list as $option) {
  1633.                     if(!in_array($option$config_list)) {
  1634.                         try {
  1635.                             $post_product $this->client->request('POST''/api/product/'.$productParentId.'/configurator-settings', array(
  1636.                                 // "debug" => true,
  1637.                                 'allow_redirects'=>array('strict'=>true),
  1638.                                 "headers" => $this->header,
  1639.                                 // 'body' => $ProductFlat
  1640.                                 'body' => json_encode([
  1641.                                     "optionId"=>$option
  1642.                                     // 'name' => strval($i),
  1643.                                     // 'position' => $i
  1644.                                 ])
  1645.                             ));
  1646.                         } catch (RequestException $e) {
  1647.                             // echo Psr7\str($e->getRequest());
  1648.                             // if ($e->hasResponse()) {
  1649.                             //     echo Psr7\str($e->getResponse());
  1650.                             // }
  1651.                             if ($e->hasResponse()) {
  1652.                                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  1653.                             }else{
  1654.                                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  1655.                             }
  1656.                             $response->headers->set('Content-Type''application/json');
  1657.                             return $response;
  1658.                             exit;
  1659.                         }
  1660.                     }
  1661.                 }
  1662.                 # UPDATE DATETIME
  1663.                 $sqlt "UPDATE storages
  1664.                     -- SET datetime_last_imported = '".date('Y-m-d H:i:s')."'
  1665.                     SET upload_done = 1
  1666.                     WHERE stone_number = '".$article['product_number']."'
  1667.                 ;";
  1668.                 $stmtt $conn_akn->prepare($sqlt);
  1669.                 $stmtt->execute();
  1670.                 
  1671.             }
  1672.         }
  1673.         # NEXT PAGE
  1674.         if($nextPage == true){
  1675.             return $this->redirect('/import/products_zulauf');
  1676.         }
  1677.         # CLEAR CACHE ##########################
  1678.         $this->clear_cache();
  1679.         $sql "INSERT INTO pixel_dates (FunctionName,StartEnde,UpdateDatetime)
  1680.                 VALUES(?,?,?)";
  1681.         $stmtt $conn_shopware->prepare($sql);
  1682.         $stmtt->execute(array( 
  1683.             "import/products_zulauf",
  1684.             "ENDE",
  1685.             date('Y-m-d H:i:s')
  1686.         ));
  1687.         # DONE
  1688.         // return json_encode(array( 'success' => true ));
  1689.         $response = new Response(json_encode(array('success' => true)));
  1690.         $response->headers->set('Content-Type''application/json');
  1691.         return $response;
  1692.         // return $this->render('update/index.html.twig', ["return" => ['success' => "Zulaufartikel importiert" ]]);
  1693.     }
  1694.     // public function import_products_zulauf_BACKUP() {
  1695.     //     $this->client_connect();
  1696.     //     $conn_shopware = $this->shop_connect();
  1697.     //     $conn_akn = $this->dump_connect();
  1698.     //     $step_count = 5;
  1699.     //     # holt alle ZULAUF Artikel ab die eine passende Materialaufschlüsselung haben
  1700.     //     $sql = "SELECT *
  1701.     //         , articles.material_id as material_id
  1702.     //         , articles.materialgroup_id as materialgroup_id
  1703.     //         , storages.id as storage_id
  1704.     //         -- , CONCAT(storages.article_id, '_' ,storages.id) AS product_number
  1705.     //         -- , CONCAT('zulauf', '_', storages.material_id, '_', storages.materialgroup_id, '_', storages.id) AS product_number
  1706.     //         , storages.stone_number AS product_number
  1707.     //         , CONCAT('Zulauf_', storages.material_id, '_', storages.materialgroup_id) AS product_number_parent
  1708.     //         -- , COALESCE(NULLIF(storages.picture, ''), NULLIF(materials.picture, ''), 'default.jpg') AS picture   -- select picture
  1709.     //         , storages.picture AS storages_picture
  1710.     //         , materials.picture AS materials_picture
  1711.     //         , COALESCE(NULLIF(materials.picture, ''), 'default.jpg') AS picture_parent   -- select picture
  1712.     //         FROM articles
  1713.     //         LEFT JOIN materials ON (articles.material_id = materials.material_id AND articles.materialgroup_id = materials.materialgroup_id )
  1714.     //         -- LEFT JOIN articlegroups ON (articlegroups.articlegroup_id = articles.article_id)
  1715.     //         LEFT JOIN articlegroups ON (articles.articlegroup_id = articlegroups.articlegroup_id)
  1716.     //         LEFT JOIN materialgroups ON (materialgroups.materialgroup_id = articles.materialgroup_id)
  1717.     //         LEFT JOIN surfaces ON (surfaces.surface_id = articles.surface_id AND surfaces.materialgroup_id = articles.materialgroup_id)
  1718.     //         INNER JOIN storages ON (articles.article_id = storages.article_id)
  1719.     //         LEFT JOIN storagefaults ON (storages.fault_id = storagefaults.fault_id)
  1720.     //         WHERE storages.stone_number IS NOT NULL
  1721.     //         AND storages.storage = 'ZULAUF'
  1722.     //         AND storages.upload_done IS NULL
  1723.     //         AND material_name IS NOT NULL
  1724.     //         -- AND articles.material_id = 'BCCD'
  1725.     //         ORDER BY articles.id ASC
  1726.     //         LIMIT ".$step_count."
  1727.     //     ;";
  1728.     //     $stmt = $conn_akn->prepare($sql);
  1729.     //     $stmt->execute();
  1730.     //     $articles = $stmt->fetchAll();
  1731.     //     # NEXT STEP
  1732.     //     if(count($articles) >= $step_count) { 
  1733.     //         $nextPage = true;
  1734.     //     } else {
  1735.     //         $nextPage = false;
  1736.     //     }
  1737.     //     # PICTURE
  1738.     //     // dd($articles[0]['picture']);
  1739.     //     // $this->getParameter('kernel.project_dir')."/public/produktbilder"
  1740.     //     // $materialid = $articles[0]["material_id"];
  1741.     //     // $materialname = $articles[0]["material_name"];
  1742.     //     // $materialgruppenname = $articles[0]["materialgroup_name"];
  1743.     //     // $materialgruppenid = $articles[0]["materialgroup_id"];
  1744.     //     // $oberflaeche = $articles[0]["surface_name"];
  1745.         
  1746.     //     foreach($articles as $article) {
  1747.     //         $productParentId = "";
  1748.     //         $productId = "";
  1749.     //         // $productNumber = $article['article_id']
  1750.     //         // var_dump($productNumber);
  1751.     //         # REMOVE SUFFIX FROM PICTURE NAME
  1752.     //         // if(strpos($article['picture'], '-##-')) {
  1753.     //         //     $article['picture'] = substr($article['picture'], 0, -5);
  1754.     //         // }
  1755.     //         if(strpos($article['storages_picture'], '-##-')) {
  1756.     //             $article['storages_picture'] = substr($article['picture'], 0, -5);
  1757.     //         }
  1758.     //         if(strpos($article['materials_picture'], '-##-')) {
  1759.     //             $article['storages_picture'] = substr($article['picture'], 0, -5);
  1760.     //         }
  1761.     //         # END EDIT PICTURE NAME
  1762.     //         $storage_option_list = $this->get_variant_ids($article); //übergibt storage, returned formatierten array für variantenerstellung
  1763.     //         # CHECK IF PARENT PRODUCT ALREADY EXISTS ###################################
  1764.     //         // $sql = "SELECT *, LOWER(HEX(id)) as id
  1765.     //         $sql = "SELECT LOWER(HEX(product.id)) as id
  1766.     //             FROM product_translation
  1767.     //             INNER JOIN product ON (product.id = product_translation.product_id)
  1768.     //             INNER JOIN product_category ON ( product_translation.product_id = product_category.product_id)
  1769.     //             -- INNER JOIN category ON ( product_category.category_id = category.category_id)
  1770.     //             -- INNER JOIN category_translation ON ( product_category.category_id = category_translation.category_id)
  1771.     //             WHERE LOWER(HEX(product_category.category_id)) = '".$this->zulauf_id."'
  1772.     //             -- AND JSON_EXTRACT(product_translation.custom_fields,'$.custom_article_material_id') = '".$article['material_id']."'
  1773.     //             -- AND JSON_EXTRACT(product_translation.custom_fields,'$.custom_article_materialgroup_id')  = '".$article['materialgroup_id']."'
  1774.     //             AND product.product_number = '". $article['product_number_parent'] . "'
  1775.     //             AND parent_id IS NULL
  1776.     //             LIMIT 1
  1777.     //         ;";
  1778.     //         $stmt = $conn_shopware->prepare($sql);
  1779.     //         $stmt->execute();
  1780.     //         $search_product = $stmt->fetchAll();
  1781.     //         # TODO: IF PRODUCT ALREADY EXISTS
  1782.     //         if(isset($search_product[0]["id"]) && !empty($search_product[0]["id"]) && count($search_product) > 0) {
  1783.     //             $productParentId = $search_product[0]["id"];
  1784.     //         }
  1785.     //         # SET DATA FOR PRODUCT ###################################
  1786.     //         $ProductFlat = new \App\Controller\shopware\ProductFlat();
  1787.     //         $ProductFlat->setActive(true);
  1788.     //         // $ProductFlat->setCategories(array(array("id" => $this->getCategoryId($materialgruppenname, $materialgruppenid))));
  1789.     //         $ProductFlat->setCategories(array(array("id" => $this->zulauf_id)));
  1790.     //         $ProductFlat->setDeliveryTimeId($this->deliverytime_id);
  1791.     //         $ProductFlat->setDescription($article["article_name_1"]);
  1792.     //         $ProductFlat->setIsCloseout(false); // ABVERKAUF - Wichtig, da Anzahl = 0
  1793.     //         $ProductFlat->setMarkAsTopseller(false);
  1794.     //         // $ProductFlat->setName('Zulauf '.$article['product_number_parent'] .' ('.  date('Y-m-d H:m:s') .')');
  1795.     //         // $ProductFlat->setName('Zulauf '.$article['product_number_parent']);
  1796.     //         // $ProductFlat->setName('ZULAUF: ' . $article['product_number_parent']);
  1797.     //         $ProductFlat->setName('Zulauf: ' . $article['material_name']);
  1798.             
  1799.     //         // $ProductFlat->setName('Zulauf '.$materialname);
  1800.     //         // $ProductFlat->setName('toller lustiger testname');
  1801.     //         $ProductFlat->setPrice(array(array(
  1802.     //             "currencyId" => "b7d2554b0ce847cd82f3ac9bd1c0dfca",
  1803.     //             "net" =>  0,
  1804.     //             "gross" => 0,
  1805.     //             "linked" => false
  1806.     //         )));
  1807.     //         // $ProductFlat->setProductNumber($article['product_number']);
  1808.     //         // $ProductFlat->setProductNumber($article["materialgroup_id"]);
  1809.     //         $ProductFlat->setProductNumber($article['product_number_parent']);
  1810.     //         $ProductFlat->setPurchaseUnit(1);
  1811.     //         // $ProductFlat->setRestockTime();
  1812.     //         // $ProductFlat->setUnitId("ff7d882bd1da432bb4e05e21eda66114");
  1813.     //         $ProductFlat->setTaxId("c5ddfa75e22b4889b6f628e1df0b347f");
  1814.     //         // $ProductFlat->setCustomFields();
  1815.     //         $custom_fields = array();
  1816.     //         // $custom_fields["custom_article_surface"] = $article["surface_name"];
  1817.     //         // $custom_fields["custom_article_search_word"] = $article["search_word"]; 
  1818.     //         // $custom_fields["custom_article_name_1"] = $article["article_name_1"];
  1819.     //         // $custom_fields["custom_article_name_2"] = $article["article_name_2"];
  1820.     //         // $custom_fields["custom_article_material_id"] = $article["material_id"];
  1821.     //         // $custom_fields["custom_article_materialgroup_id"] = $article["materialgroup_id"];
  1822.     //         // // $custom_fields["custom_article_material_name"] = $materialname;
  1823.     //         // $custom_fields["custom_article_material_name"] = $article['material_name'];
  1824.     //         // $custom_fields["custom_article_materialgroup_name"] = $article["materialgroup_name"];
  1825.     //         // $custom_fields["custom_article_picture"] = $article["picture"];
  1826.     //         // $custom_fields["custom_article_material_id"] = $article["material_id"];
  1827.     //         // $custom_fields["custom_article_materialgroup_id"] = $article["materialgroup_id"];
  1828.     //         $custom_fields["custom_article_surface"]            = $article["surface_name"];
  1829.     //         $custom_fields["custom_article_search_word"]        = $article["search_word"];
  1830.     //         $custom_fields["custom_article_name_1"]             = $article["article_name_1"];
  1831.     //         $custom_fields["custom_article_name_2"]             = $article["article_name_2"];
  1832.     //         $custom_fields["custom_article_material_id"]        = $article["material_id"];
  1833.     //         $custom_fields["custom_article_materialgroup_id"]   = $article["materialgroup_id"];
  1834.     //         $custom_fields["custom_article_material_name"]      = $article['material_name'];
  1835.     //         $custom_fields["custom_article_materialgroup_name"] = $article["materialgroup_name"];
  1836.     //         $custom_fields["custom_article_picture"]            = $article["picture"];
  1837.     //         $custom_fields["custom_article_id"]                 = $article["article_id"];
  1838.     //         $custom_fields["custom_article_stone_number"]       = $article["stone_number"];
  1839.     //         $custom_fields["custom_article_block_number"]       = $article["block_advice"];
  1840.     //         $custom_fields["custom_article_search_word"]        = $article["search_word"];
  1841.     //         $custom_fields["custom_article_advice"]             = $article["advice"];
  1842.     //         $custom_fields["custom_article_storage"]            = $article["storage"];
  1843.     //         $custom_fields["custom_article_amount_2"]           = $article["amount_2"];
  1844.     //         $custom_fields["custom_article_articlegroup_name"]  = $article["articlegroup_name"];
  1845.     //         $custom_fields["custom_article_fault_id"]           = $article["fault_name"];
  1846.     //         $custom_fields["custom_article_fault_corner"]       = $article["fault_corner"];
  1847.     //         $custom_fields["custom_article_additional_advice"]  = $article["additional_advice"];
  1848.     //         $ProductFlat->setCustomFields(
  1849.     //             $custom_fields
  1850.     //         );
  1851.     //         # TODO: Materialfarbe
  1852.     //         // //Materialfarbe
  1853.     //         // unset($farbe);
  1854.     //         // if(isset($storage["materialgroup_id"]) && !empty($storage["materialgroup_id"]) &&
  1855.     //         // isset($storage["material_id"]) && !empty($storage["material_id"])){
  1856.     //         //     $sql = "SELECT *
  1857.     //         //             FROM materialcolors
  1858.     //         //             INNER JOIN `keys` ON (materialcolors.color_id = key_number)
  1859.     //         //             WHERE color_id != -1
  1860.     //         //             AND key_kind = 88
  1861.     //         //             AND mgr_id = '".$storage["materialgroup_id"]."'
  1862.     //         //             AND mar_id = '".$storage["material_id"]."'
  1863.     //         //         ;";
  1864.     //         //     $stmt = $conn_shopware->prepare($sql);
  1865.     //         //     $stmt->execute();
  1866.     //         //     $colors = $stmt->fetchAll();
  1867.     //         //     // return $this->render('update/index.html.twig', ["return" => ['success' => $colors ]]);
  1868.     //         //     if(isset($colors) && is_array($colors) && count($colors) > 0){
  1869.     //         //         $farbe = $colors[0]["key_text"];
  1870.     //         //     }
  1871.     //         // }
  1872.     //         // if(isset($farbe) && !empty($farbe)){
  1873.     //         //     $custom_fields["custom_article_material_color"] = $farbe;
  1874.     //         // }
  1875.     //         $ProductFlat->setHeight($article["thickness"]);
  1876.     //         $ProductFlat->setLength($article["length"]);
  1877.     //         $ProductFlat->setWidth($article["width"]);
  1878.     //         $ProductFlat->setStock(0);
  1879.     //         $ProductFlat->setMaxPurchase(0);
  1880.     //         $ProductFlat->setVisibilities(array(array(
  1881.     //             "salesChannelId" => $this->saleschannel_id,
  1882.     //             "visibility" => 30
  1883.     //         )));
  1884.     //         # UPDATE OR INSERT ###################################
  1885.     //         $poststring = 'api/product';
  1886.             
  1887.             
  1888.     //         $storage_option_list = $this->get_variant_ids($article); //übergibt storage, returned formatierten array für variantenerstellung
  1889.             
  1890.     //         # PARENT ###################################
  1891.     //         if( isset($article['material_name']) && trim($article['material_name']) != '') {
  1892.     //             if($productParentId == '') {
  1893.     //                 # INSERT PARENT ARTICLE ##########################
  1894.     //                 if( isset($storage_option_list) && is_array($storage_option_list) && count($storage_option_list) > 0) {
  1895.     //                     # TODO: vorbereitung der Varianten
  1896.     //                     // $ProductFlat->setConfiguratorGroupConfig(array(array(
  1897.     //                     //     // "id" => "5eb85799b5914547adf6348ff2213d5e",
  1898.     //                     //     // "representation" => "box",
  1899.     //                     //     // "expressionForListings" => false
  1900.     //                     // )));
  1901.     //                     $ProductFlat->setConfiguratorGroupConfig(array(
  1902.     //                         // array(
  1903.     //                         //     "id" => $this->thickness_id,
  1904.     //                         //     "representation" => "box",
  1905.     //                         //     "expressionForListings" => false
  1906.     //                         // ),
  1907.     //                         // array(
  1908.     //                         //     "id" => $this->blocknumber_id,
  1909.     //                         //     "representation" => "box",
  1910.     //                         //     "expressionForListings" => false
  1911.     //                         // ),
  1912.     //                         // array(
  1913.     //                         //     "id" => $this->surface_id,
  1914.     //                         //     "representation" => "box",
  1915.     //                         //     "expressionForListings" => false
  1916.     //                         // ),
  1917.     //                         // array(
  1918.     //                         //     "id" => $this->fault_exists_id,
  1919.     //                         //     "representation" => "box",
  1920.     //                         //     "expressionForListings" => false
  1921.     //                         // ),
  1922.     //                         // array(
  1923.     //                         //     "id" => $this->color_id,
  1924.     //                         //     "representation" => "box",
  1925.     //                         //     "expressionForListings" => false
  1926.     //                         // )
  1927.     //                         array(
  1928.     //                             "id" => $this->articlegroup_id,
  1929.     //                             "representation" => "box",
  1930.     //                             "expressionForListings" => false
  1931.     //                         ),
  1932.     //                         array(
  1933.     //                             "id" => $this->surface_id,
  1934.     //                             "representation" => "box",
  1935.     //                             "expressionForListings" => false
  1936.     //                         ),
  1937.     //                         array(
  1938.     //                             "id" => $this->thickness_id,
  1939.     //                             "representation" => "box",
  1940.     //                             "expressionForListings" => false
  1941.     //                         ),
  1942.     //                         // array(
  1943.     //                         //     "id" => $this->stonenumber_id,
  1944.     //                         //     "representation" => "box",
  1945.     //                         //     "expressionForListings" => false
  1946.     //                         // ),
  1947.     //                         array(
  1948.     //                         "id" => $this->blocknumber_id,
  1949.     //                             "representation" => "box",
  1950.     //                             "expressionForListings" => false
  1951.     //                         ),
  1952.     //                         // array(
  1953.     //                         //     "id" => $this->fault_exists_id,
  1954.     //                         //     "representation" => "box",
  1955.     //                         //     "expressionForListings" => false
  1956.     //                         // ),
  1957.     //                         array(
  1958.     //                             "id" => $this->color_id,
  1959.     //                             "representation" => "box",
  1960.     //                             "expressionForListings" => false
  1961.     //                         )
  1962.     //                     ));
  1963.     //                     # SET CONFIGURATOR SETTINGS
  1964.     //                     // dd($storage_option_list);
  1965.     //                     // $ProductFlat->setConfiguratorSettings($storage_option_list);
  1966.                         
  1967.     //                     // $sol_option=array();
  1968.     //                     // foreach($storage_option_list as $key => $sol) {
  1969.     //                     //     // $sol_option[$key]["productId"] = "";
  1970.     //                     //     $sol_option[$key]["id"] = $sol["optionId"];
  1971.     //                     // }
  1972.     //                     // $ProductFlat->setConfiguratorSettings($sol_option);
  1973.                         
  1974.     //                     try {
  1975.     //                         $post_product = $this->client->request('POST', '/'.$poststring, array(
  1976.     //                             //    "debug" => true,
  1977.     //                             'allow_redirects'=>array('strict'=>true),
  1978.     //                             "headers" => $this->header,
  1979.     //                             "body" => $ProductFlat
  1980.     //                         ));
  1981.     //                     } catch (RequestException $e) {
  1982.     //                         echo Psr7\str($e->getRequest());
  1983.     //                         if ($e->hasResponse()) {
  1984.     //                             echo Psr7\str($e->getResponse());
  1985.     //                         }
  1986.     //                         exit;
  1987.     //                     }
  1988.     //                     # PARENT ID
  1989.     //                     $productParentId = $post_product->getHeaders(); 
  1990.     //                     $productParentId =  str_replace($this->base_url.$poststring.'/','',$productParentId["Location"]); // ID des erstellten produktes
  1991.     //                     $productParentId = $productParentId[0];
  1992.     //                 }
  1993.     //             }
  1994.     //             # PARENT COVER ##########################
  1995.     //             // TODO: picture_parent
  1996.     //             # INSERT VARIANT ##########################
  1997.     //             $ProductFlat->setParentId($productParentId);
  1998.     //             $ProductFlat->setProductNumber($article["product_number"]);
  1999.     //             $ProductFlat->setConfiguratorGroupConfig(null);
  2000.     //             $ProductFlat->setConfiguratorSettings(null);
  2001.     //             // $ProductFlat->setName('Zulauf '.$article['product_number_parent']);
  2002.     //             // $ProductFlat->setName($article['product_number_parent']);
  2003.     //             $ProductFlat->setName('Zulauf: ' . $article['material_name']);
  2004.                 
  2005.     //             $cover = new stdClass();
  2006.     //             $cover->type = "product_media";
  2007.     //             $cover->type = "product_media";
  2008.     //             $ProductFlat->setCover($cover);
  2009.                 
  2010.     //             if( isset($storage_option_list) && is_array($storage_option_list) && count($storage_option_list) > 0) {
  2011.     //                 $sol_option = [];
  2012.     //                 foreach($storage_option_list as $key => $sol){
  2013.     //                     $sol_option[$key]["id"] = $sol["optionId"];
  2014.     //                 }
  2015.     //                 $ProductFlat->setOptions( $sol_option );
  2016.     //                 $sql = "SELECT LOWER(HEX(product.id)) as id
  2017.     //                     FROM product
  2018.     //                     -- WHERE LOWER( product_number ) = LOWER('".$ProductFlat->getProductNumber()."')
  2019.     //                     WHERE product_number = ".$ProductFlat->getProductNumber()."
  2020.     //                     LIMIT 1
  2021.     //                 ;";
  2022.     //                 $stmt = $conn_shopware->prepare($sql);
  2023.     //                 $stmt->execute();
  2024.     //                 $product_SELECT = $stmt->fetchAll();
  2025.                     
  2026.     //                 if(isset($product_SELECT) && is_array($product_SELECT) && count($product_SELECT) > 0) {
  2027.     //                     # UPDATE
  2028.     //                     $ProductFlat->setVisibilities([]);
  2029.     //                     try {
  2030.     //                         $post_product = $this->client->request('PATCH', '/api/product/'.$product_SELECT[0]['id'], array(
  2031.     //                             //    "debug" => true,
  2032.     //                             'allow_redirects'=>array('strict' => true),
  2033.     //                             "headers" => $this->header,
  2034.     //                             "body" => $ProductFlat
  2035.     //                         ));
  2036.     //                     } catch (RequestException $e) {
  2037.     //                         echo Psr7\str($e->getRequest());
  2038.     //                         if ($e->hasResponse()) {
  2039.     //                             echo Psr7\str($e->getResponse());
  2040.     //                         }
  2041.     //                         exit;
  2042.     //                     }
  2043.     //                     $productId = $product_SELECT[0]['id'];
  2044.     //                 } else {
  2045.     //                     # INSERT
  2046.     //                     try {
  2047.     //                         $post_product = $this->client->request('POST', '/api/product', array(
  2048.     //                             //    "debug" => true,
  2049.     //                             'allow_redirects'=>array('strict' => true),
  2050.     //                             "headers" => $this->header,
  2051.     //                             "body" => $ProductFlat
  2052.     //                         ));
  2053.     //                     } catch (RequestException $e) {
  2054.     //                         echo Psr7\str($e->getRequest());
  2055.     //                         if ($e->hasResponse()) {
  2056.     //                             echo Psr7\str($e->getResponse());
  2057.     //                         }
  2058.     //                         exit;
  2059.     //                     }
  2060.     //                     // # PARENT ID
  2061.     //                     $productId = $post_product->getHeaders(); 
  2062.     //                     $productId = str_replace($this->base_url.$poststring.'/','',$productId["Location"]); // ID des erstellten produktes
  2063.     //                     $productId = $productId[0];
  2064.     //                 }
  2065.     //                 echo "<pre>"; var_dump($ProductFlat); exit;
  2066.     //                 # UPDATE KEYWORDS ###################
  2067.     //                 $keywords = [ $article['search_word'] ];
  2068.     //                 try {
  2069.     //                     $post_product = $this->client->request('PATCH', '/api/product/'.$productId, array(
  2070.     //                         //    "debug" => true,
  2071.     //                         'allow_redirects'=>array('strict' => true),
  2072.     //                         'headers' => $this->header,
  2073.     //                         'body' => json_encode([
  2074.     //                             "customSearchKeywords"=> $keywords   // DAS IST KEIN MERGE: WERTE WERDEN KNALLHART ÜBERSCHRIEBEN
  2075.     //                         ])
  2076.     //                     ));
  2077.     //                 } catch (RequestException $e) {
  2078.     //                     echo Psr7\str($e->getRequest());
  2079.     //                     if ($e->hasResponse()) {
  2080.     //                         echo Psr7\str($e->getResponse());
  2081.     //                     }
  2082.     //                     exit;
  2083.     //                 }
  2084.     //                 # END UPDATE KEYWORDS ###################
  2085.     //             }
  2086.     //             echo "<pre>"; var_dump(__LINE__); exit;
  2087.     //             # UPDATE PARENT, FEHLENDE OPTIONEN IN VARIANTENGENERATOR HINZUFÜGEN ##########################
  2088.     //             $sql = "SELECT *
  2089.     //                 , LOWER(HEX(product_id)) as product_id
  2090.     //                 , LOWER(HEX(property_group_option_id)) as property_group_option_id
  2091.     //                 FROM product_configurator_setting
  2092.     //                 WHERE LOWER(HEX(product_id)) = '".$productParentId."'
  2093.     //             ;";
  2094.     //             $stmt = $conn_shopware->prepare($sql);
  2095.     //             $stmt->execute();
  2096.     //             $config_settings = $stmt->fetchAll();
  2097.     //             $option_list = array_column($storage_option_list, 'optionId');
  2098.     //             $config_list = array_column($config_settings, 'property_group_option_id');
  2099.     //             foreach($option_list as $option) {
  2100.     //                 if(!in_array($option, $config_list)) {
  2101.     //                     try {
  2102.     //                         $post_product = $this->client->request('POST', '/api/product/'.$productParentId.'/configurator-settings', array(
  2103.     //                             // "debug" => true,
  2104.     //                             'allow_redirects'=>array('strict'=>true),
  2105.     //                             "headers" => $this->header,
  2106.     //                             // 'body' => $ProductFlat
  2107.     //                             'body' => json_encode([
  2108.     //                                 "optionId"=>$option
  2109.     //                                 // 'name' => strval($i),
  2110.     //                                 // 'position' => $i
  2111.     //                             ])
  2112.     //                         ));
  2113.     //                     } catch (RequestException $e) {
  2114.     //                         echo Psr7\str($e->getRequest());
  2115.     //                         if ($e->hasResponse()) {
  2116.     //                             echo Psr7\str($e->getResponse());
  2117.     //                         }
  2118.     //                         exit;
  2119.     //                     }
  2120.     //                 }
  2121.     //             }
  2122.     //             # PICTURES AND IMAGES ##########################
  2123.     //             # storages_picture
  2124.     //             $sql = "SELECT LOWER(HEX(id)) AS id
  2125.     //                 FROM media
  2126.     //                 WHERE file_name = '".pathinfo($article['storages_picture'], PATHINFO_FILENAME)."'
  2127.     //                 LIMIT 1
  2128.     //             ;";
  2129.     //             $stmt = $conn_shopware->prepare($sql);
  2130.     //             $stmt->execute();
  2131.     //             $pictures = $stmt->fetchAll();
  2132.     //             if(!(isset($pictures) && is_array($pictures) && count($pictures) > 0)) {
  2133.     //                 # MATERIALS_PICTURE
  2134.     //                 $sql = "SELECT LOWER(HEX(id)) AS id
  2135.     //                     FROM media
  2136.     //                     WHERE file_name = '".pathinfo($article['materials_picture'], PATHINFO_FILENAME)."'
  2137.     //                     LIMIT 1
  2138.     //                 ;";
  2139.     //                 $stmt = $conn_shopware->prepare($sql);
  2140.     //                 $stmt->execute();
  2141.     //                 $pictures = $stmt->fetchAll();
  2142.     //                 if(!(isset($pictures) && is_array($pictures) && count($pictures) > 0)) {
  2143.     //                     # DEFAULT_IMAGE
  2144.     //                     $sql = "SELECT LOWER(HEX(id)) AS id
  2145.     //                         FROM media
  2146.     //                         WHERE file_name = '".pathinfo($this->default_image_filename, PATHINFO_FILENAME)."'
  2147.     //                         LIMIT 1
  2148.     //                     ;";
  2149.     //                     $stmt = $conn_shopware->prepare($sql);
  2150.     //                     $stmt->execute();
  2151.     //                     $pictures = $stmt->fetchAll();
  2152.     //                 }
  2153.     //             }
  2154.     //             // $sql = "SELECT LOWER(HEX(id)) AS id
  2155.     //             //     FROM media
  2156.     //             //     WHERE file_name = '".pathinfo($article['picture'], PATHINFO_FILENAME)."'
  2157.     //             //     LIMIT 1
  2158.     //             // ;";
  2159.     //             // $stmt = $conn_shopware->prepare($sql);
  2160.     //             // $stmt->execute();
  2161.     //             // $pictures = $stmt->fetchAll();
  2162.     //             if(isset($pictures) && is_array($pictures) && count($pictures) > 0) {
  2163.     //                 $pictureId = $pictures[0]['id'];
  2164.     //                 # CHECK IF COVER IS ALREADY ASSIGNED
  2165.     //                 $sql = "SELECT LOWER(HEX(media.id)) AS media_id,
  2166.     //                     CONCAT (media.file_name, '.', media.file_extension) AS file_name
  2167.     //                     FROM product
  2168.     //                     INNER JOIN product_media ON ( product.cover = product_media.id)
  2169.     //                     INNER JOIN media ON (product_media.media_id = media.id)
  2170.     //                     WHERE LOWER(HEX(product.id)) = '".$productId."'
  2171.     //                     LIMIT 1
  2172.     //                 ;";
  2173.     //                 $stmt = $conn_shopware->prepare($sql);
  2174.     //                 $stmt->execute();
  2175.     //                 $cover_SELECT = $stmt->fetchAll();
  2176.     //                 if( !(isset($cover_SELECT) && is_array($cover_SELECT) && count($cover_SELECT) > 0)
  2177.     //                     || (isset($cover_SELECT['filename']) && strtolower( $article['materials_picture'] ) != strtolower( $cover_SELECT['filename']) ) ) {
  2178.     //                     # NO COVER ASSIGNED, OR DIFFERENT COVER:
  2179.     //                     # ASSIGN IMAGE
  2180.     //                     try{
  2181.     //                         $post_product = $this->client->request('POST', '/api/product-media/', array(
  2182.     //                             // 'debug' => true,
  2183.     //                             'allow_redirects'=>array('strict'=>true),
  2184.     //                             'headers' => $this->header,
  2185.     //                             'body' => json_encode([
  2186.     //                                 'productId' => $productId,
  2187.     //                                 'position' => 1,
  2188.     //                                 'media' => array(
  2189.     //                                     'id' => strtolower($pictureId)
  2190.     //                                 )
  2191.     //                             ])
  2192.     //                         ));
  2193.     //                         $location = array_pop( $post_product->getHeaders()['Location'] );
  2194.     //                         $productmediaId = basename($location);
  2195.     //                     } catch (RequestException $e) {
  2196.     //                         echo Psr7\str($e->getRequest());
  2197.     //                         if ($e->hasResponse()) {
  2198.     //                             echo Psr7\str($e->getResponse());
  2199.     //                         }
  2200.     //                         exit;
  2201.     //                     }
  2202.     //                     # ASSIGN COVER
  2203.     //                     try{
  2204.     //                         // var_dump("cover zuordnen");
  2205.     //                         $post_product = $this->client->request('PATCH', '/api/product/'.$productId, array(
  2206.     //                             // 'debug' => true,
  2207.     //                             'allow_redirects'=>array('strict'=>true),
  2208.     //                             'headers' => $this->header,
  2209.     //                             'body' => json_encode([
  2210.     //                                 'coverId' => strtolower($productmediaId)
  2211.     //                             ])
  2212.     //                         ));
  2213.     //                     } catch (RequestException $e) {
  2214.     //                         echo Psr7\str($e->getRequest());
  2215.     //                         if ($e->hasResponse()) {
  2216.     //                             echo Psr7\str($e->getResponse());
  2217.     //                         }
  2218.     //                         exit;
  2219.     //                     }
  2220.     //                 }
  2221.     //             }
  2222.     //             # UPDATE DATETIME
  2223.     //             $sqlt = "UPDATE storages
  2224.     //                 -- SET datetime_last_imported = '".date('Y-m-d H:i:s')."'
  2225.     //                 SET upload_done = 1
  2226.     //                 WHERE stone_number = '".$article['product_number']."'
  2227.     //             ;";
  2228.     //             $stmtt = $conn_akn->prepare($sqlt);
  2229.     //             $stmtt->execute();
  2230.                 
  2231.     //         }
  2232.     //     }
  2233.     //     # NEXT PAGE
  2234.     //     if($nextPage == true){
  2235.     //         return $this->redirect('/import/products_zulauf');
  2236.     //     }
  2237.     //     # CLEAR CACHE ##########################
  2238.     //     $this->clear_cache();
  2239.     //     # DONE
  2240.     //     $response = new Response(json_encode(array('success' => true)));
  2241.     //     $response->headers->set('Content-Type', 'application/json');
  2242.     //     return $response;
  2243.     //     // return $this->render('update/index.html.twig', ["return" => ['success' => "Zulaufartikel importiert" ]]);
  2244.     // }
  2245.     /**
  2246.      * @Route("/remove/products_zulauf", name="remove_products_zulauf")
  2247.      * @Route("/remove/products_zulauf/{start}")
  2248.      * 
  2249.      * FUNKTION:
  2250.      * Zulaufartikel ist jetzt normaler Artikel => update
  2251.      * Zulaufartikel wurde gelöscht => inaktiv
  2252.      * 
  2253.      * Diese Funktion ist auch bekannt unter dem Namen "Zulaufumwandler"
  2254.      * 
  2255.      * WICHTIGE INFORMATIONEN:
  2256.      * Der Parent einer Variante ist das Material. ABER: für ein Produkt und einen Zulaufartikel gibt es zwei unterschiedliche
  2257.      * Parents! Wird ein Zulaufartikel zu einem normalen Artikel kann also der Fall entstehen, dass der ParentArtikel noch nicht
  2258.      * generiert wurde!!!
  2259.      * 
  2260.      * 
  2261.      * @return mixed
  2262.      */
  2263.     public function remove_products_zulauf($start 0) {
  2264.         $this->client_connect();
  2265.         $conn_shopware $this->shop_connect();
  2266.         $conn_akn $this->dump_connect();
  2267.         $step_count 6;
  2268.         $sql "INSERT INTO pixel_dates (FunctionName,StartEnde,`Page`,UpdateDatetime)
  2269.                 VALUES(?,?,?,?)";
  2270.         $stmtt $conn_shopware->prepare($sql);
  2271.         $stmtt->execute(array( 
  2272.             "remove/products_zulauf",
  2273.             "Start",
  2274.             $start,
  2275.             date('Y-m-d H:i:s')
  2276.         ));
  2277.         # GET ALL ZULAUFARTIKEL FROM SHOPWARE DATABASE
  2278.         $sql "SELECT LOWER(HEX(product_category.product_id)) AS product_id,
  2279.             product_number AS product_number,
  2280.             JSON_EXTRACT(product_translation.custom_fields,'$.custom_article_material_id') AS custom_article_material_id,
  2281.             JSON_EXTRACT(product_translation.custom_fields,'$.custom_article_materialgroup_id') AS custom_article_materialgroup_id
  2282.             FROM product_category
  2283.             INNER JOIN product ON (product_category.product_id = product.id)
  2284.             INNER JOIN product_translation ON (product.id = product_translation.product_id)
  2285.             WHERE LOWER(HEX(product_category.category_id)) = '".$this->zulauf_id."'
  2286.             AND parent_id IS NOT NULL
  2287.             LIMIT ".$step_count."
  2288.             OFFSET ".$start."
  2289.         ;";
  2290.         $stmt $conn_shopware->prepare($sql);
  2291.         $stmt->execute();
  2292.         $product_SELECT $stmt->fetchAll();
  2293.         if(isset($product_SELECT) && is_array($product_SELECT) && count($product_SELECT) > 0) {
  2294.             # CHECK IF ZULAUFARTIKEL IS IN AKN DATABASE
  2295.             foreach($product_SELECT as $product) {
  2296.                 $sql "SELECT *
  2297.                     FROM storages
  2298.                     WHERE stone_number = '".$product['product_number']."'
  2299.                 ;";
  2300.                 $stmt $conn_akn->prepare($sql);
  2301.                 $stmt->execute();
  2302.                 $articles $stmt->fetchAll();
  2303.                 if(isset($articles) && is_array($articles) && count($articles) > 0) {
  2304.                     # PRODUCT IS STILL ACTIVE
  2305.                     $article array_pop($articles);
  2306.                     if( strtoupper($article['storage']) != 'ZULAUF') {
  2307.                         # ZULAUFARTIKEL IST JETZT NORMALER ARTIKEL => REMOVE CATEGORY
  2308.                         try {
  2309.                             $post_product $this->client->request('DELETE''/api/product/'.$product['product_id'].'/categories/'$this->zulauf_id, array(
  2310.                                 'allow_redirects'=>array('strict'=>true),
  2311.                                 "headers" => $this->header,
  2312.                             ));
  2313.                         } catch (RequestException $e) {
  2314.                             // echo Psr7\str($e->getRequest());
  2315.                             // if ($e->hasResponse()) {
  2316.                             //     echo Psr7\str($e->getResponse());
  2317.                             // }
  2318.                             if ($e->hasResponse()) {
  2319.                                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  2320.                             }else{
  2321.                                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  2322.                             }
  2323.                             $response->headers->set('Content-Type''application/json');
  2324.                             return $response;
  2325.                             exit;
  2326.                         }
  2327.                         # SET NEW PARENT ######################
  2328.                         $sql "SELECT LOWER(HEX(product.id)) as id, product_number
  2329.                             FROM product
  2330.                             WHERE product_number = '".$article['material_id'] . "_" $article['materialgroup_id']."'
  2331.                             AND parent_id IS NULL
  2332.                             LIMIT 1
  2333.                         ;";
  2334.                         $stmt $conn_shopware->prepare($sql);
  2335.                         $stmt->execute();
  2336.                         $parent_SEARCH $stmt->fetchAll();
  2337.                         
  2338.                         if(isset($parent_SEARCH) && is_array($parent_SEARCH) && count($parent_SEARCH) > 0) {
  2339.                             # PARENT AS ARTICLE EXISTING
  2340.                             # PARENT ID
  2341.                             $productParentId $parent_SEARCH[0]['id'];
  2342.                         } else {
  2343.                             # PARENT AS ARTICLE IS MISSING
  2344.                             # WE NEED TO ADD THE MISSING PARENT, BUT ONLY WITH THE NECESSARY VALUES ... THE PRODUCT UPDATE WILL BE THE NEXT FUNCTION RUN BY THE CRONJOB!
  2345.                             $ProductParentFlat = new \App\Controller\shopware\ProductFlat();
  2346.                             $ProductParentFlat->setActive(true);
  2347.                             $ProductParentFlat->setProductNumber($article['custom_article_material_id'] . '_' $article['custom_article_materialgroup_id']);
  2348.                             $poststring 'api/product';
  2349.                             try {
  2350.                                 $post_product $this->client->request('POST''/'.$poststring, array(
  2351.                                     //    "debug" => true,
  2352.                                     'allow_redirects'=>array('strict'=>true),
  2353.                                     "headers" => $this->header,
  2354.                                     "body" => $ProductParentFlat
  2355.                                 ));
  2356.                             } catch (RequestException $e) {
  2357.                                 // echo Psr7\str($e->getRequest());
  2358.                                 // if ($e->hasResponse()) {
  2359.                                 //     echo Psr7\str($e->getResponse());
  2360.                                 // }
  2361.                                 if ($e->hasResponse()) {
  2362.                                     $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  2363.                                 }else{
  2364.                                     $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  2365.                                 }
  2366.                             $response->headers->set('Content-Type''application/json');
  2367.                             return $response;
  2368.                                 exit;
  2369.                             }
  2370.     
  2371.                             # PARENT ID
  2372.                             $productParentId $post_product->getHeaders(); 
  2373.                             $productParentId =  str_replace($this->base_url.$poststring.'/'''$productParentId["Location"]); // ID des erstellten produktes
  2374.                             $productParentId $productParentId[0];
  2375.                         }
  2376.                         # END SET NEW PARENT ######################
  2377.                         $ProductFlat = new \App\Controller\shopware\ProductFlat();
  2378.                         $ProductFlat->setProductNumber$product['product_number']);
  2379.                         $ProductFlat->setActive(false);
  2380.                         $ProductFlat->setParentId($productParentId);
  2381.                         # UPDATE
  2382.                         try {
  2383.                             $post_product $this->client->request('PATCH''/api/product/'.$product['product_id'], array(
  2384.                                 //    "debug" => true,
  2385.                                 'allow_redirects'=>array('strict' => true),
  2386.                                 "headers" => $this->header,
  2387.                                 "body" => $ProductFlat
  2388.                             ));
  2389.                         } catch (RequestException $e) {
  2390.                             // echo Psr7\str($e->getRequest());
  2391.                             // if ($e->hasResponse()) {
  2392.                             //     echo Psr7\str($e->getResponse());
  2393.                             // }
  2394.                             if ($e->hasResponse()) {
  2395.                                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  2396.                             }else{
  2397.                                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  2398.                             }
  2399.                         $response->headers->set('Content-Type''application/json');
  2400.                         return $response;
  2401.                             exit;
  2402.                         }
  2403.                     }
  2404.                 } else {
  2405.                     # ZULAUFARTIKEL WURDE GELÖSCHT => DELETE PRODUCT
  2406.                     $ProductFlat = new \App\Controller\shopware\ProductFlat();
  2407.                     $ProductFlat->setActive(false);
  2408.                     $ProductFlat->setProductNumber$product['product_number']);
  2409.                     try {
  2410.                         $post_product $this->client->request('PATCH''/api/product/'.$product['product_id'], array(
  2411.                             //    "debug" => true,
  2412.                             'allow_redirects'=>array('strict' => true),
  2413.                             "headers" => $this->header,
  2414.                             "body" => $ProductFlat
  2415.                         ));
  2416.                     } catch (RequestException $e) {
  2417.                         // echo Psr7\str($e->getRequest());
  2418.                         // if ($e->hasResponse()) {
  2419.                             //     echo Psr7\str($e->getResponse());
  2420.                             // }
  2421.                             if ($e->hasResponse()) {
  2422.                                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  2423.                             }else{
  2424.                                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  2425.                             }
  2426.                         $response->headers->set('Content-Type''application/json');
  2427.                         return $response;
  2428.                         exit;
  2429.                     }
  2430.                 }
  2431.             }
  2432.         }
  2433.         # NEXT PAGE ##########################
  2434.         if(count($product_SELECT) >= $step_count) { 
  2435.             $start $start $step_count;
  2436.             return $this->redirect('/remove/products_zulauf/'.$start);
  2437.         } else {
  2438.             // $nextPage = false;
  2439.         }
  2440.         
  2441.         # CLEAR CACHE ##########################
  2442.         $this->clear_cache();
  2443.         $sql "INSERT INTO pixel_dates (FunctionName,StartEnde,`Page`,UpdateDatetime)
  2444.                 VALUES(?,?,?,?)";
  2445.         $stmtt $conn_shopware->prepare($sql);
  2446.         $stmtt->execute(array( 
  2447.             "remove/products_zulauf",
  2448.             "ENDE",
  2449.             $start,
  2450.             date('Y-m-d H:i:s')
  2451.         ));
  2452.         # DONE
  2453.         $response = new Response(json_encode(array('success' => true)));
  2454.         $response->headers->set('Content-Type''application/json');
  2455.         return $response;
  2456.         return $this->render('update/index.html.twig', ["return" => ['success' => "Zulaufartikel geloescht" ]]);
  2457.     }
  2458.     
  2459.     /**
  2460.      * UPDATE IMAGES FROM AKN TO SHOPWARE
  2461.      * 
  2462.      * @Route("/update/images", name="update_images")
  2463.      * 
  2464.      * @return mixed
  2465.      */
  2466.     public function update_images() {
  2467.         die('this was just a test!');
  2468.         // set_time_limit(200);
  2469.         $this->client_connect();
  2470.         $conn_shopware $this->shop_connect();
  2471.         $media_folder_id "a7916c4069d04139b299328b8bd5c90c";
  2472.         # GET ALL AKN-PRODUCTS
  2473.         $sql "SELECT *
  2474.             , articles.material_id as material_id
  2475.             , articles.materialgroup_id as materialgroup_id
  2476.             , storages.id as storage_id
  2477.             -- , CONCAT(storages.article_id, '_' ,storages.id) AS product_number
  2478.             , CONCAT(storages.material_id, '_', storages.materialgroup_id, '_', storages.id) AS product_number
  2479.             , CONCAT(storages.material_id, '_', storages.materialgroup_id) AS product_number_parent
  2480.             , COALESCE(NULLIF(storages.picture, ''), NULLIF(materials.picture, ''), 'default.jpg') AS picture   -- select picture
  2481.             , COALESCE(NULLIF(materials.picture, ''), 'default.jpg') AS picture_parent   -- select picture
  2482.             FROM articles
  2483.             LEFT JOIN materials ON (articles.material_id = materials.material_id AND articles.materialgroup_id = materials.materialgroup_id )
  2484.             -- LEFT JOIN articlegroups ON (articlegroups.articlegroup_id = articles.article_id)
  2485.             LEFT JOIN articlegroups ON (articles.articlegroup_id = articlegroups.articlegroup_id)
  2486.             LEFT JOIN materialgroups ON (materialgroups.materialgroup_id = articles.materialgroup_id)
  2487.             LEFT JOIN surfaces ON (surfaces.surface_id = articles.surface_id AND surfaces.materialgroup_id = articles.materialgroup_id)
  2488.             INNER JOIN storages ON (articles.article_id = storages.article_id)
  2489.             LEFT JOIN storagefaults ON (storages.fault_id = storagefaults.fault_id)
  2490.             WHERE material_name IS NOT NULL
  2491.             AND articles.material_id = 'ASBL'
  2492.             -- AND storages.datetime_last_imported IS NULL -- not imported
  2493.             ORDER BY articles.id ASC
  2494.         ;";
  2495.         $stmt $conn_shopware->prepare($sql);
  2496.         $stmt->execute();
  2497.         $articles $stmt->fetchAll();
  2498.         // dd($articles);
  2499.         # REMOVE SUFFIX FROM PICTURE NAME
  2500.         foreach($articles as &$article) {
  2501.             if(strpos($article['picture'], '-##-')) {
  2502.                 $article['picture'] = substr($article['picture'], 0, -5);
  2503.             }
  2504.             // 1603_20181108_110050.jpg-
  2505.         }
  2506.         # END EDIT PICTURE NAME
  2507.         foreach($articles as $article) {
  2508.             # UPDATE PARENT
  2509.             $sql "SELECT *,
  2510.                 LOWER(HEX(product.id)) as product_id,
  2511.                 JSON_EXTRACT(product_translation.custom_fields,'$.custom_article_picture') AS custom_article_picture,
  2512.                 CONCAT(media.file_name, '.' , media.file_extension) AS picture
  2513.                 FROM product
  2514.                 INNER JOIN product_translation ON (product.id = product_translation.product_id)
  2515.                 LEFT JOIN product_media ON (product.cover = product_media.id)
  2516.                 INNER JOIN media ON (product_media.media_id = media.id)
  2517.                 WHERE JSON_EXTRACT(product_translation.custom_fields,'$.custom_article_material_id') = '".$article['material_id']."'
  2518.                 AND JSON_EXTRACT(product_translation.custom_fields,'$.custom_article_materialgroup_id') = '".$article['materialgroup_id']."'
  2519.                 -- AND JSON_EXTRACT(product_translation.custom_fields,'$.custom_article_materialgroup_stone_number') = '".$article['stone_number']."'
  2520.                 -- AND JSON_EXTRACT(product_translation.custom_fields,'$.custom_article_materialgroup_surface') = '".$article['surface_name']."'
  2521.                 -- AND JSON_EXTRACT(product_translation.custom_fields,'$.custom_article_materialgroup_fault_id') = '".$article['fault_id']."'
  2522.                 -- AND JSON_EXTRACT(product_translation.custom_fields,'$.custom_article_materialgroup_fault_corner') = '".$article['fault_corner']."'
  2523.                 AND product.width = '"floatval($article['width']) ."'
  2524.                 AND product.height = '"floatval($article['thickness']) ."'
  2525.                 AND product.length = '"floatval($article['length']) ."'
  2526.                 AND product.parent_id IS NOT NULL
  2527.                 -- JSON_EXTRACT(product_translation.custom_fields,'$.custom_article_materialgroup_id') = 
  2528.                 -- WHERE product_number LIKE '".$article['product_number']."'
  2529.                 LIMIT 1
  2530.             ;";
  2531.             $stmt $conn_shopware->prepare($sql);
  2532.             $stmt->execute();
  2533.             $product_SELECT $stmt->fetchAll();
  2534.             if(isset($product_SELECT) && is_array($product_SELECT) && count($product_SELECT) > 0) {
  2535.                 $product $product_SELECT[0];
  2536.                 if( strtolower($article['picture']) != strtolower($product['picture']) ) {
  2537.                     # PICTURE HAS TO BE UPDATED ###########################
  2538.                     $basename $article['picture'];
  2539.                     $pathinfo pathinfo$article['picture'] );
  2540.                     $extension $pathinfo['extension'];
  2541.                     $filename $pathinfo['filename'];
  2542.                     # CHECK IF PICTURE IS ALREADY IN MEDIAPOOL
  2543.                     try {
  2544.                         $search $this->client->request('GET''/api/media?filter[fileName]='.$filename.'', array( 
  2545.                         "headers" => $this->header,
  2546.                         ));
  2547.                     } catch (RequestException $e) {
  2548.                         // echo Psr7\str($e->getRequest());
  2549.                         // if ($e->hasResponse()) {
  2550.                         //     echo Psr7\str($e->getResponse());
  2551.                         // }
  2552.                         if ($e->hasResponse()) {
  2553.                             $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  2554.                         }else{
  2555.                             $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  2556.                         }
  2557.                         $response->headers->set('Content-Type''application/json');
  2558.                         return $response;
  2559.                         exit;
  2560.                     }
  2561.                     $search_picture json_decode($search->getBody()->getContents(), true);
  2562.                     if($search_picture['total'] == 0) {
  2563.                         # NEW PICTURE HAS TO BE UPLOADED
  2564.                         # CHECK IF IMAGE FILE EXISTS ON WEBSPACE
  2565.                         $path '/public/produktbilder/'.$article['picture'];
  2566.                         if( !file_exists$this->getParameter('kernel.project_dir').$path ) ) continue;
  2567.                         $mediaUrl trim('https://'.$_SERVER['HTTP_HOST'].'/produktbilder/'.$basename);
  2568.                         $mediaUrl str_replace(' ''%20'$mediaUrl);
  2569.                         # UPLOAD PICTURE
  2570.                         $post_media $this->client->post('/api/media', array(
  2571.                             // "debug" => true,
  2572.                             'allow_redirects'=>array('strict'=>true),
  2573.                             'headers' => $this->header,
  2574.                             'body' => json_encode([
  2575.                                 'mediaFolderId' => $media_folder_id
  2576.                             ])
  2577.                         ));
  2578.                         $location array_pop$post_media->getHeaders()['Location'] );
  2579.                         $mediaId basename($location);
  2580.                         // dd($mediaId);
  2581.                         try {
  2582.                             $post_media $this->client->request('POST''/api/_action/media/'.$mediaId.'/upload?extension='.$extension.'&fileName='.$filename, array(
  2583.                                 // "debug" => true,
  2584.                                 'allow_redirects'=>array('strict'=>true),
  2585.                                 'headers' => $this->header,
  2586.                                 'body' => json_encode([
  2587.                                     'url' => $mediaUrl,
  2588.                                     'mediaFolderId' => $media_folder_id,
  2589.                                 ])
  2590.                             ));
  2591.                         } catch (RequestException $e) {
  2592.                             // echo Psr7\str($e->getRequest());
  2593.                             // if ($e->hasResponse()) {
  2594.                             //     echo Psr7\str($e->getResponse());
  2595.                             // }
  2596.                             if ($e->hasResponse()) {
  2597.                                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  2598.                             }else{
  2599.                                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  2600.                             }
  2601.                             $response->headers->set('Content-Type''application/json');
  2602.                             return $response;
  2603.                             exit;
  2604.                         }
  2605.                     } else {
  2606.                         # get media Id
  2607.                         try {
  2608.                             // $search = $this->client->request('GET', '/api/media?filter[fileName]='.$articleSW["custom_article_picture"].'', array( 
  2609.                             $search $this->client->request('GET''/api/media?filter[fileName]='.$filename.'', array( 
  2610.                                 'headers' => $this->header,
  2611.                             ));
  2612.                         } catch (RequestException $e) {
  2613.                             // echo Psr7\str($e->getRequest());
  2614.                             // if ($e->hasResponse()) {
  2615.                             //     echo Psr7\str($e->getResponse());
  2616.                             // }
  2617.                             if ($e->hasResponse()) {
  2618.                                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  2619.                             }else{
  2620.                                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  2621.                             }
  2622.                             $response->headers->set('Content-Type''application/json');
  2623.                             return $response;
  2624.                             exit;
  2625.                         }
  2626.                         $search_picture json_decode($search->getBody()->getContents(),true);
  2627.                         $mediaId $search_picture["data"][0]["id"];
  2628.                     }
  2629.                     # ASSIGN IMAGE
  2630.                     try{
  2631.                         $post_product $this->client->request('POST''/api/product-media/', array(
  2632.                             // 'debug' => true,
  2633.                             'allow_redirects'=>array('strict'=>true),
  2634.                             'headers' => $this->header,
  2635.                             'body' => json_encode([
  2636.                                 'productId' => $product['product_id'],
  2637.                                 'position' => 1,
  2638.                                 'media' => array(
  2639.                                     'id' => strtolower($mediaId)
  2640.                                 )
  2641.                             ])
  2642.                         ));
  2643.                         $location array_pop$post_product->getHeaders()['Location'] );
  2644.                         $productmediaId basename($location);
  2645.                     } catch (RequestException $e) {
  2646.                         // echo Psr7\str($e->getRequest());
  2647.                         // if ($e->hasResponse()) {
  2648.                         //     echo Psr7\str($e->getResponse());
  2649.                         // }
  2650.                         if ($e->hasResponse()) {
  2651.                             $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  2652.                         }else{
  2653.                             $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  2654.                         }
  2655.                         $response->headers->set('Content-Type''application/json');
  2656.                         return $response;
  2657.                         exit;
  2658.                     }
  2659.                     # ASSIGN COVER
  2660.                     try{
  2661.                         // var_dump("cover zuordnen");
  2662.                         $post_product $this->client->request('PATCH''/api/product/'.$product['product_id'], array(
  2663.                             // 'debug' => true,
  2664.                             'allow_redirects'=>array('strict'=>true),
  2665.                             'headers' => $this->header,
  2666.                             'body' => json_encode([
  2667.                                 'coverId' => strtolower($productmediaId)
  2668.                             ])
  2669.                         ));
  2670.                     } catch (RequestException $e) {
  2671.                         // echo Psr7\str($e->getRequest());
  2672.                         // if ($e->hasResponse()) {
  2673.                         //     echo Psr7\str($e->getResponse());
  2674.                         // }
  2675.                         if ($e->hasResponse()) {
  2676.                             $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  2677.                         }else{
  2678.                             $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  2679.                         }
  2680.                         $response->headers->set('Content-Type''application/json');
  2681.                         return $response;
  2682.                         exit;
  2683.                     }
  2684.                 }
  2685.             }
  2686.         }
  2687.         dd(__LINE__);
  2688.     }
  2689.     /**
  2690.      * add oder update Bild bei Artikel
  2691.      * 
  2692.      */
  2693.     public function add_image($bildname$produktId$zusatzbild)
  2694.     {
  2695.         $this->client_connect();
  2696.         $conn_shopware $this->shop_connect();
  2697.         if( empty(trim($bildname)) || empty(trim($produktId)) ){
  2698.             return false;
  2699.         }
  2700.         //hole gesetzte Mediafiles im Produkt ab
  2701.         $sql "SELECT 
  2702.                 *
  2703.                 , LOWER(HEX(product_id)) as product_id
  2704.                 , LOWER(HEX(media_id)) as media_id
  2705.             FROM product_media
  2706.             INNER JOIN media ON (product_media.media_id = media.id)
  2707.             WHERE LOWER(HEX(product_id)) = '".$produktId."'
  2708.             ;";
  2709.         $stmt $conn_shopware->prepare($sql);
  2710.         $stmt->execute();
  2711.         $mediaSW $stmt->fetchAll();
  2712.         $bildname str_replace('"','',$bildname);
  2713.         if(strpos($bildname,"-##-")){
  2714.             $bildnamesubstr($bildname,0,-5);
  2715.         }
  2716.         $bildname str_replace('"','',$bildname);
  2717.         $article_file_new preg_replace("/´|`|&/"' '$bildname);
  2718.         $article_file_new preg_replace("/\s\s+/"' '$article_file_new);
  2719.         $article_file_new str_replace(array('ü','ä','ö','+','ß'),array('ue','ae','oe','_','ss'),$article_file_new);
  2720.         $filenameexplode("."$article_file_new);
  2721.         $type array_pop($filename);
  2722.         $filename implode('.'$filename);
  2723.         $filename trim(preg_replace("/\.+$/",'',$filename));
  2724.    
  2725.         // prüfen ob Bild schon vorhanden ist
  2726.         try {
  2727.             $search $this->client->request('GET''/api/media?filter[fileName]='.$filename.'', array( 
  2728.             "headers" => $this->header,
  2729.             ));
  2730.         } catch (RequestException $e) {
  2731.             // echo Psr7\str($e->getRequest());
  2732.             // if ($e->hasResponse()) {
  2733.             //     echo Psr7\str($e->getResponse());
  2734.             // }
  2735.             if ($e->hasResponse()) {
  2736.                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  2737.             }else{
  2738.                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  2739.             }
  2740.             $response->headers->set('Content-Type''application/json');
  2741.             return $response;
  2742.             exit;
  2743.         }
  2744.         $search_picture json_decode($search->getBody()->getContents(),true);
  2745.         if($search_picture["total"] > 0){
  2746.             // return $this->render('update/index.html.twig', ["return" => ['search picture' => $search_picture, 'media url' => $mediaUrl ]]);
  2747.             $Id $search_picture["data"][0]["id"];
  2748.             if($zusatzbild){
  2749.                 // prüfe ob verknüpfung zwischen bild und produkt existiert, wenn nicht erstelle sie
  2750.                 $sql "SELECT 
  2751.                         *
  2752.                         , LOWER(HEX(product_id)) as product_id
  2753.                         , LOWER(HEX(media_id)) as media_id
  2754.                     FROM product_media
  2755.                     INNER JOIN media ON (product_media.media_id = media.id)
  2756.                     WHERE LOWER(HEX(product_id)) = '".$produktId."'
  2757.                     AND file_name = '".$filename."'
  2758.                     ;";
  2759.                 $stmt $conn_shopware->prepare($sql);
  2760.                 $stmt->execute();
  2761.                 $product_media $stmt->fetchAll();
  2762.                 if(isset($product_media) && !empty($product_media) && count($product_media) > 0){
  2763.                 }else{
  2764.                     try{
  2765.                         $post_product $this->client->request('POST''/api/product-media/', array(
  2766.                             // 'debug' => true,
  2767.                             'allow_redirects'=>array('strict'=>true),
  2768.                             'headers' => $this->header,
  2769.                             'body' => json_encode([
  2770.                                 'productId' => $produktId,
  2771.                                 'position' => 2,
  2772.                                 'media' => array(
  2773.                                     'id' => strtolower($Id)
  2774.                                 )
  2775.                             ])
  2776.                         ));
  2777.                         // $location = array_pop( $post_product->getHeaders()['Location'] );
  2778.                         // $productmediaId = basename($location);
  2779.                     } catch (RequestException $e) {
  2780.                         // echo Psr7\str($e->getRequest());
  2781.                         // if ($e->hasResponse()) {
  2782.                         //     echo Psr7\str($e->getResponse());
  2783.                         // }
  2784.                         if ($e->hasResponse()) {
  2785.                             $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  2786.                         }else{
  2787.                             $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  2788.                         }
  2789.                         $response->headers->set('Content-Type''application/json');
  2790.                         return $response;
  2791.                         exit;
  2792.                     }
  2793.                 }
  2794.             }else{
  2795.                 $sql "DELETE
  2796.                     FROM product_media
  2797.                     WHERE LOWER(HEX(product_id)) = '".$produktId."'
  2798.                     ;";
  2799.                 $stmt $conn_shopware->prepare($sql);
  2800.                 $stmt->execute(); 
  2801.                 try{
  2802.                     $post_product $this->client->request('POST''/api/product-media/', array(
  2803.                         // 'debug' => true,
  2804.                         'allow_redirects'=>array('strict'=>true),
  2805.                         'headers' => $this->header,
  2806.                         'body' => json_encode([
  2807.                             'productId' => $produktId,
  2808.                             'position' => 1,
  2809.                             'media' => array(
  2810.                                 'id' => strtolower($Id)
  2811.                             )
  2812.                         ])
  2813.                     ));
  2814.                     $location array_pop$post_product->getHeaders()['Location'] );
  2815.                     $productmediaId basename($location);
  2816.                 } catch (RequestException $e) {
  2817.                     // echo Psr7\str($e->getRequest());
  2818.                     // if ($e->hasResponse()) {
  2819.                     //     echo Psr7\str($e->getResponse());
  2820.                     // }
  2821.                     if ($e->hasResponse()) {
  2822.                         $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  2823.                     }else{
  2824.                         $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  2825.                     }
  2826.                     $response->headers->set('Content-Type''application/json');
  2827.                     return $response;
  2828.                     exit;
  2829.                 }
  2830.                 // ADD COVER                   
  2831.                 try{
  2832.                     // return var_dump("cover zuordnen". $produktId." - ".$productmediaId); 
  2833.                     $post_product $this->client->request('PATCH''/api/product/'.$produktId, array(
  2834.                         // 'debug' => true,
  2835.                         'allow_redirects'=>array('strict'=>true),
  2836.                         'headers' => $this->header,
  2837.                         'body' => json_encode([
  2838.                             'coverId' => strtolower($productmediaId)
  2839.                         ])
  2840.                     ));
  2841.                 } catch (RequestException $e) {
  2842.                     // echo Psr7\str($e->getRequest());
  2843.                     // if ($e->hasResponse()) {
  2844.                     //     echo Psr7\str($e->getResponse());
  2845.                     // }
  2846.                     if ($e->hasResponse()) {
  2847.                         $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  2848.                     }else{
  2849.                         $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  2850.                     }
  2851.                     $response->headers->set('Content-Type''application/json');
  2852.                     return $response;
  2853.                     exit;
  2854.                 }
  2855.             }
  2856.         }
  2857.         return true;
  2858.     }
  2859.     public function clear_cache() {
  2860.         try {
  2861.             $post_product $this->client->request('DELETE''/api/_action/cache', array(
  2862.                 //    "debug" => true,
  2863.                 'allow_redirects'=>array('strict'=>true),
  2864.                 "headers" => $this->header,
  2865.             ));
  2866.         
  2867.         } catch (RequestException $e) {
  2868.             // echo Psr7\str($e->getRequest());
  2869.             // if ($e->hasResponse()) {
  2870.             //     echo Psr7\str($e->getResponse());
  2871.             // }
  2872.             if ($e->hasResponse()) {
  2873.                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  2874.             }else{
  2875.                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  2876.             }
  2877.             $response->headers->set('Content-Type''application/json');
  2878.             return $response;
  2879.             exit;
  2880.         }
  2881.     }
  2882.     /**
  2883.      * Importiert SQL
  2884.      * 
  2885.      * @Route("/import/sql", name="import_sql")
  2886.      * 
  2887.      * @return mixed
  2888.      */
  2889.     public function import_sql() {
  2890.         $this->client_connect();
  2891.         $conn_akn $this->dump_connect();
  2892.         // $conn_shopware = $this->shop_connect();
  2893.         
  2894.         // $sql = "INSERT INTO pixel (pixel_datetime, pixel_function, pixel_text)
  2895.         //     VALUES (now(), '".__FUNCTION__."', 'sql import START')
  2896.         // ;";
  2897.         // $stmt = $conn_akn->prepare($sql);
  2898.         // $stmt->execute();
  2899.         
  2900.         
  2901.         $sql_file $this->getParameter('kernel.project_dir').'/public/sql/import.sql';
  2902.         if( file_exists($sql_file)) {
  2903.             $sql_import file_get_contents($sql_file);
  2904.             $sql_import str_replace(["\n""\r"], ""$sql_import);
  2905.             // $sql_import = str_replace("`", "", $sql_import);
  2906.             // $sql_import = str_replace(", group,", ", `group`,", $sql_import);
  2907.             // $sql_import = str_replace(", index,", ", `index`,", $sql_import);
  2908.             $sql_import explode(';'$sql_import);
  2909.             
  2910.             foreach($sql_import as $query) {
  2911.                 if( trim($query) != '') {
  2912.                     
  2913.                     # DO SQL STUFF #################
  2914.                     
  2915.                     # SKIP
  2916.                     if( str_starts_withstrtolowertrim$query ) ), 'use' )) continue;
  2917.                     // if( str_starts_with( strtolower( trim( $query ) ), 'truncate' )) continue;
  2918.                     $sql $query.';';
  2919.                     // var_dump($query);
  2920.                     $stmt $conn_akn->prepare($sql);
  2921.                     $stmt->execute();
  2922.                     // $data = $stmt->fetchAll();
  2923.                     # TODO: CATCH ERRORS
  2924.                 }
  2925.             }
  2926.         }
  2927.         // $sql = "INSERT INTO pixel (pixel_datetime, pixel_function, pixel_text)
  2928.         //     VALUES (now(), '".__FUNCTION__."', 'sql import DONE')
  2929.         // ;";
  2930.         // $stmt = $conn_akn->prepare($sql);
  2931.         // $stmt->execute();
  2932.         # DONE
  2933.         return $this->render('update/index.html.twig', ["return" => ['success' => "SQL importiert" ]]);
  2934.         
  2935.     }
  2936.     /**
  2937.      * Import Customers
  2938.      * 
  2939.      * @Route("/import/customers", name="import_customers")
  2940.      * 
  2941.      * @return mixed
  2942.      */
  2943.     public function import_customers() {
  2944.         $this->client_connect();
  2945.         $conn_shopware $this->shop_connect();
  2946.         $conn_akn $this->dump_connect();
  2947.         $step_count 50;
  2948.         $sql "INSERT INTO pixel_dates (FunctionName,StartEnde,UpdateDatetime)
  2949.                 VALUES(?,?,?)";
  2950.         $stmtt $conn_shopware->prepare($sql);
  2951.         $stmtt->execute(array( 
  2952.             "import/customers",
  2953.             "Start",
  2954.             date('Y-m-d H:i:s')
  2955.         ));
  2956.         # CONSTANTS
  2957.         $payment_method_id $this->payment_method_id;   // Nachname
  2958.         // $salutation_id = 'f81f1e5f8dee45b2b719f56879544b08';    // Firma
  2959.         $salutation_id $this->salutation_id;    // Keine Angabe
  2960.         $group_id $this->group_id// Standard Kundengruppe
  2961.         $country_id $this->country_id// Deutschland
  2962.         # GET ALL CUSTOMERS
  2963.         $sql "SELECT *
  2964.         , customers.customer_number as customer_number
  2965.         , customers.email as customer_email
  2966.         , k1.key_text as price_list
  2967.         , k2.key_text as salesman
  2968.         , k3.key_text as salesarea
  2969.         , k4.key_text as tour
  2970.         , k5.key_text as payment_term
  2971.         , k6.key_text as delivery_term
  2972.         , k7.key_text as `group`
  2973.             FROM customers
  2974.             LEFT JOIN contacts ON (customers.customer_number = contacts.customer_number)
  2975.             LEFT JOIN `keys` as k1 ON (k1.key_number = customers.price_list AND k1.key_kind = 7)
  2976.             LEFT JOIN `keys` as k2 ON (k2.key_number = customers.salesman AND k2.key_kind = 8)
  2977.             LEFT JOIN `keys` as k3 ON (k3.key_number = customers.sales_area AND k3.key_kind = 9)
  2978.             LEFT JOIN `keys` as k4 ON (k4.key_number = customers.tour AND k4.key_kind = 52)
  2979.             LEFT JOIN `keys` as k5 ON (k5.key_number = customers.payment_term AND k5.key_kind = 4)
  2980.             LEFT JOIN `keys` as k6 ON (k6.key_number = customers.delivery_term AND k6.key_kind = 5)
  2981.             LEFT JOIN `keys` as k7 ON (k7.key_number = customers.group AND k7.key_kind = 3)
  2982.             WHERE customers.customer_number IS NOT NULL
  2983.             AND customers.street IS NOT NULL AND customers.street !=''
  2984.             AND customers.email IS NOT NULL AND customers.email !='' AND customers.email LIKE '%@%'
  2985.             AND name_1 IS NOT NULL AND name_1 != ''
  2986.             AND upload_done IS NULL
  2987.             -- AND customers.customer_number = '14254' -- #TODO: REMOVE THIS LINE!!!
  2988.             LIMIT ".$step_count."
  2989.         ;";
  2990.         $stmt $conn_akn->prepare($sql);
  2991.         $stmt->execute();
  2992.         $customers_SELECT $stmt->fetchAll();
  2993.         // return $this->render('update/index.html.twig', ["return" => [ 'zeit' => $customers_SELECT]]);
  2994.         foreach($customers_SELECT as $customer) {
  2995.             $CustomerFlat = new \App\Controller\shopware\CustomerFlat();
  2996.             $CustomerFlat->setActive(true);
  2997.             // $first_name = $customer['name_1'].' '.$customer['name_2'].' '.$customer['name_3'];
  2998.             // $last_name = 'Import-neu';
  2999.             // $company = '-';
  3000.             $first_name $customer['name_1'];
  3001.             if(isset($customer['name_2']) && !empty($customer['name_2']) && $customer['name_2'] != null){
  3002.                 $last_name $customer['name_2'];
  3003.             }else{
  3004.                 $last_name "-";
  3005.             }
  3006.             if(isset($customer['name_3']) && !empty($customer['name_3']) && $customer['name_3'] != null){
  3007.                 $company $customer['name_3'];
  3008.             }else{
  3009.                 $company "-";
  3010.             }
  3011.             // Erzeuge Titel wenn noch nicht vorhanden
  3012.             if(isset($customer["title"]) && !empty($customer["title"]) && $customer["title"] != null){
  3013.                 $sql "SELECT *
  3014.                     , LOWER(HEX(salutation_id)) as salutation_id
  3015.                     FROM salutation_translation
  3016.                     WHERE LOWER(HEX(language_id)) = '".$this->language_id."'
  3017.                     AND display_name = '".trim($customer["title"])."'
  3018.                     ;";
  3019.                 $stmt $conn_shopware->prepare($sql);
  3020.                 $stmt->execute();
  3021.                 $salutation_SELECT $stmt->fetchAll();
  3022.                     
  3023.                 if(isset($salutation_SELECT) && is_array($salutation_SELECT) && count($salutation_SELECT) > 0){
  3024.                     // wenns gesetzt ist 
  3025.                     $salutation_id $salutation_SELECT[0]["salutation_id"];
  3026.                 }else{
  3027.                     // generiere salutation und setze id
  3028.                     try {
  3029.                         $post_customer $this->client->request('POST''/api/salutation', array(
  3030.                             // 'debug' => true,
  3031.                             'allow_redirects'=>array('strict'=>true),
  3032.                             'headers' => $this->header,
  3033.                             'body' => json_encode([
  3034.                                 'salutationKey' => strtolower($customer["title"]),
  3035.                                 'displayName' => $customer["title"],
  3036.                                 'letterName' => " "
  3037.                             ])
  3038.                         ));
  3039.                         $location array_pop($post_customer->getHeaders()['Location'] );
  3040.                         $salutation_id basename($location);
  3041.                     } catch (RequestException $e) {
  3042.                         // echo Psr7\str($e->getRequest());
  3043.                         // if ($e->hasResponse()) {
  3044.                         //     echo Psr7\str($e->getResponse());
  3045.                         // }
  3046.                         if ($e->hasResponse()) {
  3047.                             $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  3048.                         }else{
  3049.                             $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  3050.                         }
  3051.                         $response->headers->set('Content-Type''application/json');
  3052.                         return $response;
  3053.                         exit;
  3054.                     }
  3055.                 }
  3056.             }else{
  3057.                 $salutation_id $this->salutation_id;    // Keine Angabe
  3058.             }
  3059.             // Erzeuge Standardzahlungsart wenn noch nicht vorhanden
  3060.             if(isset($customer['payment_term']) && !empty($customer['payment_term']) && $customer['payment_term'] != null){
  3061.                 $sql "SELECT *
  3062.                     , LOWER(HEX(payment_method_id)) as payment_method_id
  3063.                     FROM payment_method_translation
  3064.                     WHERE LOWER(HEX(language_id)) = '".$this->language_id."'
  3065.                     AND name = '".trim($customer['payment_term'])."'
  3066.                     ;";
  3067.                 $stmt $conn_shopware->prepare($sql);
  3068.                 $stmt->execute();
  3069.                 $payment_SELECT $stmt->fetchAll();
  3070.                     
  3071.                 if(isset($payment_SELECT) && is_array($payment_SELECT) && count($payment_SELECT) > 0){
  3072.                     // wenns gesetzt ist 
  3073.                     $payment_method_id $payment_SELECT[0]["payment_method_id"];
  3074.                 }else{
  3075.                     // generiere zahlungsart und setze id
  3076.                     try {
  3077.                         $post_customer $this->client->request('POST''/api/payment-method', array(
  3078.                             // 'debug' => true,
  3079.                             'allow_redirects'=>array('strict'=>true),
  3080.                             'headers' => $this->header,
  3081.                             'body' => json_encode([
  3082.                                 'name' => trim($customer['payment_term']),
  3083.                                 'active' => true
  3084.                             ])
  3085.                         ));
  3086.                         $location array_pop$post_customer->getHeaders()['Location'] );
  3087.                         $payment_method_id basename($location);
  3088.                     } catch (RequestException $e) {
  3089.                         // echo Psr7\str($e->getRequest());
  3090.                         // if ($e->hasResponse()) {
  3091.                         //     echo Psr7\str($e->getResponse());
  3092.                         // }
  3093.                         if ($e->hasResponse()) {
  3094.                             $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  3095.                         }else{
  3096.                             $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  3097.                         }
  3098.                         $response->headers->set('Content-Type''application/json');
  3099.                         return $response;
  3100.                         exit;
  3101.                     }
  3102.                     $sql "INSERT INTO sales_channel_payment_method (sales_channel_id, payment_method_id)
  3103.                         VALUES (UNHEX('".$this->saleschannel_id."'), UNHEX('".$payment_method_id."'))
  3104.                     ;";
  3105.                     $stmt $conn_shopware->prepare($sql);
  3106.                     $stmt->execute();
  3107.                    
  3108.                 }
  3109.             }else{
  3110.                 $payment_method_id $this->payment_method_id;    // Rechnung
  3111.             }
  3112.             // Ende Standardzahlungsart
  3113.             // // Erzeuge Standardlieferart wenn noch nicht vorhanden
  3114.             // if(isset($customer['payment_term']) && !empty($customer['payment_term']) && $customer['payment_term'] != null){
  3115.             //     $sql = "SELECT *
  3116.             //         , LOWER(HEX(payment_method_id)) as payment_method_id
  3117.             //         FROM payment_method_translation
  3118.             //         WHERE LOWER(HEX(language_id)) = '".$this->language_id."'
  3119.             //         AND name = '".trim($customer['payment_term'])."'
  3120.             //         ;";
  3121.             //     $stmt = $conn_shopware->prepare($sql);
  3122.             //     $stmt->execute();
  3123.             //     $payment_SELECT = $stmt->fetchAll();
  3124.                     
  3125.             //     if(isset($payment_SELECT) && is_array($payment_SELECT) && count($payment_SELECT) > 0){
  3126.             //         // wenns gesetzt ist 
  3127.             //         $payment_method_id = $payment_SELECT[0]["payment_method_id"];
  3128.             //     }else{
  3129.             //         // generiere zahlungsart und setze id
  3130.             //         try {
  3131.             //             $post_customer = $this->client->request('POST', '/api/payment-method', array(
  3132.             //                 // 'debug' => true,
  3133.             //                 'allow_redirects'=>array('strict'=>true),
  3134.             //                 'headers' => $this->header,
  3135.             //                 'body' => json_encode([
  3136.             //                     'name' => trim($customer['payment_term']),
  3137.             //                     'active' => true
  3138.             //                 ])
  3139.             //             ));
  3140.             //             $location = array_pop( $post_customer->getHeaders()['Location'] );
  3141.             //             $payment_method_id = basename($location);
  3142.             //         } catch (RequestException $e) {
  3143.             //             echo Psr7\str($e->getRequest());
  3144.             //             if ($e->hasResponse()) {
  3145.             //                 echo Psr7\str($e->getResponse());
  3146.             //             }
  3147.             //             exit;
  3148.             //         }
  3149.             //         $sql = "INSERT INTO sales_channel_payment_method (sales_channel_id, payment_method_id)
  3150.             //             VALUES (UNHEX('f8c383f75225488a8f6b89b7d64d6cf2'), UNHEX('".$payment_method_id."'))
  3151.             //         ;";
  3152.             //         $stmt = $conn_shopware->prepare($sql);
  3153.             //         $stmt->execute();
  3154.                     
  3155.             //     }
  3156.             // }else{
  3157.             //     $payment_method_id = $this->payment_method_id;    // Rechnung
  3158.             // }
  3159.             // // Ende Standardlieferart
  3160.             // Erzeuge Kundengruppe wenn noch nicht vorhanden
  3161.             if(isset($customer['group']) && !empty($customer['group']) && $customer['group'] != null){
  3162.                 $sql "SELECT *
  3163.                     , LOWER(HEX(customer_group_id)) as customer_group_id
  3164.                     FROM customer_group_translation
  3165.                     WHERE LOWER(HEX(language_id)) = '".$this->language_id."'
  3166.                     AND name = '".trim($customer['group'])."'
  3167.                     ;";
  3168.                 $stmt $conn_shopware->prepare($sql);
  3169.                 $stmt->execute();
  3170.                 $customer_group_SELECT $stmt->fetchAll();
  3171.                     
  3172.                 if(isset($customer_group_SELECT) && is_array($customer_group_SELECT) && count($customer_group_SELECT) > 0){
  3173.                     // wenns gesetzt ist 
  3174.                     $group_id $customer_group_SELECT[0]["customer_group_id"];
  3175.                 }else{
  3176.                     // generiere kundengruppe und setze id
  3177.                     try {
  3178.                         $post_customer $this->client->request('POST''/api/customer-group', array(
  3179.                             // 'debug' => true,
  3180.                             'allow_redirects'=>array('strict'=>true),
  3181.                             'headers' => $this->header,
  3182.                             'body' => json_encode([
  3183.                                 'name' => trim($customer['group'])
  3184.                             ])
  3185.                         ));
  3186.                         $location array_pop$post_customer->getHeaders()['Location'] );
  3187.                         $group_id basename($location);
  3188.                     } catch (RequestException $e) {
  3189.                         // echo Psr7\str($e->getRequest());
  3190.                         // if ($e->hasResponse()) {
  3191.                         //     echo Psr7\str($e->getResponse());
  3192.                         // }
  3193.                         if ($e->hasResponse()) {
  3194.                             $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  3195.                         }else{
  3196.                             $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  3197.                         }
  3198.                         $response->headers->set('Content-Type''application/json');
  3199.                         return $response;
  3200.                         exit;
  3201.                     }
  3202.                 }
  3203.             }else{
  3204.                 $group_id $this->group_id;    // Default Kundengruppe
  3205.             }
  3206.             // Ende Kundengruppe
  3207.             // return $this->render('update/index.html.twig', ["return" => [ 'zeit' => $group_id]]);
  3208.             # GET LANGUAGE ID
  3209.             $language_name = ( trim($customer['country_indicator']) == '' || $customer['country_indicator'] == 'DE') ? 'Deutsch' 'English';
  3210.             $sql "SELECT LOWER(HEX(id)) AS language_id, language.name
  3211.                 FROM language
  3212.                 WHERE language.name = '".$language_name."'
  3213.             ;";
  3214.             $stmt $conn_shopware->prepare($sql);
  3215.             $stmt->execute();
  3216.             $language_SELECT $stmt->fetchAll();
  3217.             $language_id $language_SELECT[0]['language_id']??$this->language_id;
  3218.             $CustomerFlat->setLanguageId($language_id);
  3219.             # END GET LANGUAGE ID
  3220.             # THIS VALUES SHOULD NOT BE BLANK:
  3221.             $CustomerFlat->setGroupId($group_id);
  3222.             $CustomerFlat->setDefaultPaymentMethodId($payment_method_id);
  3223.             $CustomerFlat->setSalesChannelId($this->saleschannel_id);
  3224.             // $CustomerFlat->setDefaultBillingAddressId();
  3225.             // $CustomerFlat->setDefaultShippingAddressId();
  3226.             $CustomerFlat->setSalutationId($salutation_id);
  3227.             $CustomerFlat->setCustomerNumber($customer['customer_number']);
  3228.             $CustomerFlat->setLastName($last_name);
  3229.             # END THIS VALUES SHOULD NOT BE BLANK:
  3230.             
  3231.             $CustomerFlat->setFirstName(trim($first_name));
  3232.             if(strpos($customer['email'],"@")){
  3233.                 $CustomerFlat->setEmail($customer['email']);
  3234.             }elseif(strpos($customer['customer_email'],"@")){
  3235.                 $CustomerFlat->setEmail($customer['customer_email']);
  3236.             }else{
  3237.                  # UPDATE FAILED BECAUSE EMAIL IS INVALID
  3238.                 $sqlt "UPDATE customers
  3239.                         SET upload_done = 1, upload_failed = 1
  3240.                         WHERE customer_number = '".$customer['customer_number']."'
  3241.                         ;";
  3242.                 $stmtt $conn_akn->prepare($sqlt);
  3243.                 $stmtt->execute();
  3244.                 continue;
  3245.             }
  3246.             $CustomerFlat->setCompany($company);
  3247.             # CUSTOM FIELDS
  3248.             $CustomerFlat->setCustomFields(array(
  3249.                 'custom_customer_telefon_1' => $customer['telefon_1']??'',
  3250.                 'custom_customer_telefon_2' => $customer['telefon_2']??'',
  3251.                 'custom_customer_fax' => $customer['fax']??'',
  3252.                 'custom_customer_homepage' => $customer['homepage']??'',
  3253.                 'custom_customer_payment_term' => $customer['payment_term']??'',
  3254.                 'custom_customer_delivery_term' => $customer['delivery_term']??'',
  3255.                 'custom_customer_price_list' => $customer['price_list']??'',
  3256.                 'custom_customer_salesman' => $customer['salesman']??'',
  3257.                 'custom_customer_salesarea' => $customer['salesarea']??'',
  3258.                 'custom_customer_tour' => $customer['tour']??'',
  3259.                 'custom_customer_grouping' => $customer['grouping']??'',
  3260.             ));
  3261.             # END CUSTOM FIELDS
  3262.             # LIEFERADRESSE
  3263.             $CustomerShippingAddressFlat = new \App\Controller\shopware\CustomerAddressFlat();
  3264.             # GET COUNTRY ID
  3265.             if(trim($customer['country_indicator']) == ''$customer['country_indicator'] = 'DE';
  3266.             $sql "SELECT LOWER(HEX(id)) AS country_id, country.iso
  3267.                 FROM country
  3268.                 -- WHERE country.iso = 'DE'
  3269.                 WHERE country.iso = '" $customer['country_indicator'] . "'
  3270.             ;";
  3271.             $stmt $conn_shopware->prepare($sql);
  3272.             $stmt->execute();
  3273.             $country_SELECT $stmt->fetchAll();
  3274.             if(!isset($country_SELECT[0]['country_id']) || empty($country_SELECT[0]['country_id']) || $country_SELECT[0]['country_id'] == ''){
  3275.                 // dd($customer);
  3276.                  # UPDATE FAILED BECAUSE COUNTRY CODE IS INVALID
  3277.                  $sqlt "UPDATE customers
  3278.                         SET upload_done = 1, upload_failed = 2
  3279.                         WHERE customer_number = '".$customer['customer_number']."'
  3280.                         ;";
  3281.                 $stmtt $conn_akn->prepare($sqlt);
  3282.                 $stmtt->execute();
  3283.                 continue;
  3284.             }
  3285.             $country_id $country_SELECT[0]['country_id']??$country_id;
  3286.             $CustomerShippingAddressFlat->setCountryId(strtolower($country_id));
  3287.             $CustomerShippingAddressFlat->setSalutationId($salutation_id);
  3288.             $CustomerShippingAddressFlat->setFirstName(trim($first_name));
  3289.             $CustomerShippingAddressFlat->setLastName($last_name);
  3290.             $CustomerShippingAddressFlat->setCompany($company);
  3291.             $CustomerShippingAddressFlat->setZipcode($customer['postcode']);
  3292.             $CustomerShippingAddressFlat->setCity($customer['place']);
  3293.             $CustomerShippingAddressFlat->setStreet($customer['street']);
  3294.             $CustomerShippingAddressFlat->setPhoneNumber($customer['telefon_1']);
  3295.             # END LIEFERADRESSE
  3296.             # CHECK IF CUSTOMER ALREADY EXISTS
  3297.             $sql "SELECT LOWER(HEX(id)) AS customer_id
  3298.                 FROM customer
  3299.                 WHERE customer_number = '".$customer['customer_number']."'
  3300.                 LIMIT 1
  3301.             ;";
  3302.             $stmt $conn_shopware->prepare($sql);
  3303.             $stmt->execute();
  3304.             $shopware_customer_SELECT $stmt->fetchAll();
  3305.             if(!(isset($shopware_customer_SELECT) && is_array($shopware_customer_SELECT) && count($shopware_customer_SELECT) > 0)) {
  3306.                 # INSERT ##########################
  3307.                 $CustomerFlat->setDefaultShippingAddress($CustomerShippingAddressFlat);
  3308.                 // $CustomerFlat->setDefaultBillingAddress($CustomerShippingAddressFlat);
  3309.                 try {
  3310.                     $post_customer $this->client->request('POST''/api/customer', array(
  3311.                         // 'debug' => true,
  3312.                         'allow_redirects'=>array('strict'=>true),
  3313.                         'headers' => $this->header,
  3314.                         'body' => $CustomerFlat
  3315.                     ));
  3316.                 } catch (RequestException $e) {
  3317.                     // echo Psr7\str($e->getRequest());
  3318.                     // if ($e->hasResponse()) {
  3319.                     //     echo Psr7\str($e->getResponse());
  3320.                     // }
  3321.                     if ($e->hasResponse()) {
  3322.                         $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  3323.                     }else{
  3324.                         $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  3325.                     }
  3326.                     $response->headers->set('Content-Type''application/json');
  3327.                     return $response;
  3328.                     exit;
  3329.                 }
  3330.             } else {
  3331.                 $customer_id strtolower($shopware_customer_SELECT[0]['customer_id']);
  3332.                 # UPDATE ##########################
  3333.                 try {
  3334.                     $post_product $this->client->request('PATCH''/api/customer/'.$customer_id.'', array(
  3335.                         "headers" => $this->header,
  3336.                         "body" => $CustomerFlat
  3337.                     ));
  3338.                 } catch (RequestException $e) {
  3339.                     // echo Psr7\str($e->getRequest());
  3340.                     // if ($e->hasResponse()) {
  3341.                     //     echo Psr7\str($e->getResponse());
  3342.                     // }
  3343.                     if ($e->hasResponse()) {
  3344.                         $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  3345.                     }else{
  3346.                         $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  3347.                     }
  3348.                     $response->headers->set('Content-Type''application/json');
  3349.                     return $response;
  3350.                     exit;
  3351.                 }
  3352.                 # UPDATE Adresse ##########################
  3353.                 # GET ADRESS-ID
  3354.                 $sql "SELECT LOWer(HEX(id)) AS address_id
  3355.                     FROM customer_address
  3356.                     WHERE LOWER(HEX(customer_id)) = '".$customer_id."'
  3357.                 ;";
  3358.                 
  3359.                 $stmt $conn_shopware->prepare($sql);
  3360.                 $stmt->execute();
  3361.                 $address_SELECT $stmt->fetchAll();
  3362.                 if(isset($address_SELECT) && is_array($address_SELECT) && count($address_SELECT) > 0) {
  3363.                     $addresses $address_SELECT;
  3364.                     # UPDATE Adresses with new values
  3365.                     foreach( $addresses as $address) {
  3366.                         $address_id $address['address_id'];
  3367.                         try {
  3368.                             $post_product $this->client->request('PATCH''/api/customer/'.$customer_id.'/addresses/'.$address_id.'', array(
  3369.                                 "headers" => $this->header,
  3370.                                 "body" => $CustomerShippingAddressFlat
  3371.                             ));
  3372.                         } catch (RequestException $e) {
  3373.                             // echo Psr7\str($e->getRequest());
  3374.                             // if ($e->hasResponse()) {
  3375.                             //     echo Psr7\str($e->getResponse());
  3376.                             // }
  3377.                             if ($e->hasResponse()) {
  3378.                                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  3379.                             }else{
  3380.                                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  3381.                             }
  3382.                             $response->headers->set('Content-Type''application/json');
  3383.                             return $response;
  3384.                             exit;
  3385.                         }
  3386.                     }
  3387.                 }
  3388.             }
  3389.             # UPDATE DATETIME ##########################
  3390.             $sqlt "UPDATE customers
  3391.                 -- SET datetime_last_imported = '".date('Y-m-d H:i:s')."'
  3392.                 SET upload_done = 1
  3393.                 WHERE customer_number = '".$customer['customer_number']."'
  3394.             ;";
  3395.             $stmtt $conn_akn->prepare($sqlt);
  3396.             $stmtt->execute();
  3397.             // dd(__LINE__);
  3398.         }
  3399.         # NEXT PAGE ##########################
  3400.         if(count($customers_SELECT) >= $step_count) { 
  3401.             // $nextPage = true;
  3402.             return $this->redirect('/import/customers');
  3403.         } else {
  3404.             // $nextPage = false;
  3405.         }
  3406.         // setze 
  3407.         $sqlt "UPDATE customers
  3408.                 SET upload_done = 1, upload_failed = 1
  3409.                 WHERE customer_number = ''
  3410.                 OR (upload_done IS NULL AND upload_failed IS NULL)
  3411.                 ;";
  3412.         $stmtt $conn_akn->prepare($sqlt);
  3413.         $stmtt->execute();
  3414.         # CLEAR CACHE ##########################
  3415.         $this->clear_cache();
  3416.         $sql "INSERT INTO pixel_dates (FunctionName,StartEnde,UpdateDatetime)
  3417.                 VALUES(?,?,?)";
  3418.         $stmtt $conn_shopware->prepare($sql);
  3419.         $stmtt->execute(array( 
  3420.             "import/customers",
  3421.             "ENDE",
  3422.             date('Y-m-d H:i:s')
  3423.         ));
  3424.         # DONE ##########################
  3425.         $response = new Response(json_encode(array('success' => true)));
  3426.         $response->headers->set('Content-Type''application/json');
  3427.         return $response;
  3428.         return $this->render('update/index.html.twig', ["return" => ['success' => "Kunden importiert" ]]);
  3429.     }
  3430.     /**
  3431.      * update_categories
  3432.      * 
  3433.      * @Route("/update/categories", name="update_categories")
  3434.      * @Route("/update/categories/{start}")
  3435.      * 
  3436.      * @return mixed
  3437.      */
  3438.     public function update_categories($start 0) {
  3439.         $this->client_connect();
  3440.         $conn_shopware $this->shop_connect();
  3441.         $step_count 3;
  3442.         # GET ALL CATEGORIES
  3443.         $sql "SELECT LOWER(HEX(id)) AS category_id, category_translation.name, active
  3444.             FROM category
  3445.             INNER JOIN category_translation ON (category.id = category_translation.category_id)
  3446.             WHERE LOWER(HEX(parent_id)) = '".$this->default_category_id."'
  3447.             LIMIT ".$step_count."
  3448.             OFFSET ".$start."
  3449.             ;";
  3450.             // -- WHERE LOWER(HEX(parent_id)) = '".$this->steine_aus_aller_welt_category_id."'
  3451.         $stmt $conn_shopware->prepare($sql);
  3452.         $stmt->execute();
  3453.         $categories_SELECT $stmt->fetchAll();
  3454.         // echo "<pre>"; var_dump($categories_SELECT); exit;
  3455.         if(isset($categories_SELECT) && is_array($categories_SELECT) && count($categories_SELECT) > 0) {
  3456.             $categories $categories_SELECT;
  3457.             foreach($categories as $category) {
  3458.                 # SELECT PRODUCTS WITH CATEGORY
  3459.                 $sql "SELECT LOWER(HEX(id)) AS category_id, category_translation.name
  3460.                     FROM category
  3461.                     INNER JOIN category_translation ON (category.id = category_translation.category_id)
  3462.                     WHERE LOWER(HEX(parent_id)) = '".$this->default_category_id."'
  3463.                 ;";
  3464.                 // -- WHERE LOWER(HEX(parent_id)) = '".$this->steine_aus_aller_welt_category_id."'
  3465.                 $sql "SELECT product_number
  3466.                     FROM product
  3467.                     INNER JOIN product_category ON (product.id = product_category.product_id)
  3468.                     INNER JOIN category ON (product_category.category_id = category.id)
  3469.                     WHERE LOWER(HEX(category.id)) = '".$category['category_id']."'
  3470.                     LIMIT 3
  3471.                 ;";
  3472.         
  3473.                 $stmt $conn_shopware->prepare($sql);
  3474.                 $stmt->execute();
  3475.                 $products_SELECT $stmt->fetchAll();
  3476.                 # IF NO PRODUCT DISABLE CATEGORY
  3477.                 $CategoryFlat = new \App\Controller\shopware\CategoryFlat();
  3478.                 // if (isset($products_SELECT) && is_array($products_SELECT) && count($products_SELECT) > 0) {
  3479.                 // }
  3480.                 // if ($category["category_id"] == "91bb5a08283d40a3bf1ec0a4759660b1") {
  3481.                 //     echo "<pre>"; var_dump($category); var_dump($products_SELECT); exit;
  3482.                 // }
  3483.                 $commit false;
  3484.                 if(isset($products_SELECT) && is_array($products_SELECT) && count($products_SELECT) > 0) {
  3485.                     # ACTIVATE
  3486.                     if ($category["active"] == 0) {
  3487.                         $CategoryFlat->setActive(true);
  3488.                         $commit true;
  3489.                     }
  3490.                 } else {
  3491.                     # DISABLE
  3492.                     if ($category["active"] == 1) {
  3493.                         $CategoryFlat->setActive(false);
  3494.                         $commit true;
  3495.                     }
  3496.                 }
  3497.                 // $CategoryFlat->setActive(true);
  3498.                 if ($commit == true) {
  3499.                     try {
  3500.                         $post_product $this->client->request('PATCH''/api/category/'.$category['category_id'], array(
  3501.                             //    "debug" => true,
  3502.                             'allow_redirects'=>array('strict' => true),
  3503.                             'headers' => $this->header,
  3504.                             'body' => $CategoryFlat,
  3505.                         ));
  3506.                     } catch (RequestException $e) {
  3507.                         // echo Psr7\str($e->getRequest());
  3508.                         // if ($e->hasResponse()) {
  3509.                         //     echo Psr7\str($e->getResponse());
  3510.                         // }
  3511.                         if ($e->hasResponse()) {
  3512.                             $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  3513.                         }else{
  3514.                             $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  3515.                         }
  3516.                         $response->headers->set('Content-Type''application/json');
  3517.                         return $response;
  3518.                         exit;
  3519.                     }
  3520.                 }
  3521.             }
  3522.         }
  3523.         # NEXT PAGE ##########################
  3524.         if(count($categories_SELECT) >= $step_count) { 
  3525.             $start $start $step_count;
  3526.             return $this->redirect('/update/categories/'$start);
  3527.         }
  3528.         # CLEAR CACHE ##########################
  3529.         $this->clear_cache();
  3530.         # DONE ##########################
  3531.         return $this->redirect('/categories/visibility');
  3532.         // $response = new Response(json_encode(array('success' => true)));
  3533.         // $response->headers->set('Content-Type', 'application/json');
  3534.         // return $response;
  3535.         
  3536.         // return $this->render('update/index.html.twig', ["return" => ['success' => "Kunden importiert" ]]);
  3537.     }
  3538.     /**
  3539.      * Pixelproductions testet
  3540.      * 
  3541.      * @Route("/pixel_test", name="pixel_test")
  3542.      * 
  3543.      * @return mixed
  3544.      */
  3545.     public function pixel_test() {
  3546.         die('No testing!');
  3547.         $directory $this->getParameter('kernel.project_dir').'/public/produktbilder';
  3548.         $files array_values(array_diffscandir($directory), array('.','..')));
  3549.         $test_file $files[0];     // 2319_P1030278 Nero Impala Rustenburg geflammt 2´cm 218070548.jpg
  3550.         // $test_file = html_entity_decode($test_file, ENT_QUOTES);
  3551.         // $test_file = preg_replace('/(\'|&#0*39;)/', '', $test_file);
  3552.         // $test_file = preg_replace("/´/", '', $test_file);
  3553.         $test_file '2319_P1030278 Nero Impala Rustenburg geflammt 2´cm 218070548.jpg';
  3554.         $new_filename preg_replace("/´/"''$test_file);
  3555.         dd($new_filename);
  3556.     }
  3557.     /**
  3558.      * Pixelproductions testet
  3559.      * 
  3560.      * @Route("/pixel_mail", name="pixel_mail")
  3561.      * 
  3562.      * @return mixed
  3563.      */
  3564.     public function sendEmail(MailerInterface $mailer): Response {
  3565.         die('No EMails!');
  3566.         // var_dump( urlencode('bN+y6M=T8b4s') ); exit;    // bN%2By6M%3DT8b4s
  3567.         // var_dump( urlencode('helpdesk@pixelproductions.de') ); exit;    // helpdesk%40pixelproductions.de
  3568.         $email = (new Email())
  3569.             ->from('cw@pixelproductions.de')
  3570.             ->to('mo@pixelproductions.de')
  3571.             //->cc('cc@example.com')
  3572.             //->bcc('bcc@example.com')
  3573.             //->replyTo('fabien@example.com')
  3574.             //->priority(Email::PRIORITY_HIGH)
  3575.             ->subject('Time for Symfony Mailer!')
  3576.             ->text('Sending emails is fun again!')
  3577.             ->html('<p>See Twig integration for better HTML integration!</p>');
  3578.         $mailer->send($email);
  3579.         // ...
  3580.     }
  3581.  
  3582. }