src/Controller/ImportController.php line 3369

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 Symfony\Component\HttpFoundation\Request;
  11. // use Symfony\Component\HttpFoundation\Response;
  12. // use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\HttpFoundation\JsonResponse;
  14. use Symfony\Component\Validator\Constraints\DateTime;
  15. use Symfony\Component\HttpFoundation\RedirectResponse;
  16. // use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  17. # E-MAIL
  18. use Symfony\Component\Mailer\MailerInterface;
  19. use Symfony\Component\Mime\Email;
  20. class ImportController extends AbstractController
  21. {
  22.     private $client NULL;
  23.     private $header NULL;
  24.     private $base_url// Shop URL
  25.     private $client_id// SW ID
  26.     private $client_secret// SW Secret
  27.     private $client_username// SW Username
  28.     private $client_password// SW Password
  29.     private $shopware_data// Shopware Datenbank
  30.     private $shopware_data_live// Shopware Datenbank LIVE
  31.     private $shopware_data_dump// AKN Datenbank Dump
  32.     private $shopware_data_dump_live// AKN Datenbank Dump LIVE
  33.     private $property_group_id// Eigenschaften Gruppe ID Varianten
  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 $material_category_layout_id//Default Materialkategorie Layout ID 
  41.     private $category_layout_id// Default Kategorie Layout ID
  42.     // private $default_online_id; // Default Onlinelager ID 
  43.     private $color_id// Eigenschaften Gruppe ID Farbe
  44.     private $fault_exists_id// Eigenschaften Gruppe ID Fehler vorhanden
  45.     private $thickness_id// Eigenschaften Gruppe ID Materialstärke
  46.     private $blocknumber_id// Eigenschaften Gruppe ID Blocknummer
  47.     private $stonenumber_id// Eigenschaften Gruppe ID Steinnummer
  48.     private $default_image_filename// Default Bildername
  49.     private $language_id// ID der Defaultsprache
  50.     private $length_id// Eigenschaften Gruppe ID Länge
  51.     private $width_id// Eigenschaften Gruppe ID Höhe
  52.     private $amount_id// Eigenschaften Gruppe ID Anzahl
  53.     private $surfacearea_id// Eigenschaften Gruppe ID Quadratmeter
  54.     private $meter_id// Eigenschaften Gruppe ID Meter
  55.     private $sa_id// Eigenschaften Gruppe ID Sa
  56.     private $squaremeterprice_id// Eigenschaften Gruppe ID Quadratmeterpreis
  57.     private $artikeldaten_innen_id// Eigenschaften Gruppe ID Innen
  58.     private $artikeldaten_aussen_id// Eigenschaften Gruppe ID Außen
  59.     private $artikeldaten_ids// Liste der Artikeldaten EIgenschaften
  60.     private $tax_id// ID der Defaultsteuern
  61.     private $currency_id//ID der Defaultwährung
  62.     private $deliverytime_id// ID der Defaultlieferzeit
  63.     private $saleschannel_id// ID der Default Verkaufskanal
  64.     private $salutation_id// ID der Default Ansprache (Keine Angabe)
  65.     private $payment_method_id// ID der Default Zahlungsmethode (Nachname)
  66.     private $group_id// ID der Default Kundengruppe
  67.     private $country_id// ID des Default Landes (Deutschland)
  68.     private $media_folder_id// ID des Mediafolders für den Bilderupload
  69.  
  70.     function __construct() {
  71.         $this->base_url $_ENV['base_url']; // Base URL
  72.         if(isset($_ENV['client_id'])){
  73.             $this->client_id $_ENV['client_id']; // SW ID
  74.         }
  75.         if(isset($_ENV['client_secret'])){
  76.             $this->client_secret $_ENV['client_secret']; // SW Secret
  77.         }
  78.         if(isset($_ENV['client_username'])){
  79.             $this->client_username $_ENV['client_username']; // SW Username
  80.         }
  81.         if(isset($_ENV['client_password'])){
  82.             $this->client_password $_ENV['client_password']; // SW Password
  83.         }
  84.         $this->shopware_data $_ENV['shopware_data']; // Shopware Datenbank
  85.         $this->shopware_data_live $_ENV['shopware_data_live']; // Shopware Datenbank LIVE
  86.         $this->shopware_data_dump $_ENV['shopware_data_dump']; // AKN Datenbank Dump
  87.         $this->shopware_data_dump_live $_ENV['shopware_data_dump_live']; // AKN Datenbank Dump LIVE
  88.         $this->surface_id $_ENV['surface_id']; // Eigenschaften Gruppe ID Oberfläche
  89.         $this->materialgroup_id $_ENV['materialgroup_id']; // Eigenschaften Gruppe ID Materialgruppe
  90.         $this->material_id $_ENV['material_id']; // Eigenschaften Gruppe ID Material
  91.         $this->articlegroup_id $_ENV['articlegroup_id']; // Eigenschaften Gruppe ID Artikelgruppe
  92.         $this->zulauf_id $_ENV['zulauf_id']; // Kategorie ID Zulauf Bald Verfügbar
  93.         $this->default_category_id $_ENV['default_category_id']; // Default Kategorie ID
  94.         // $this->default_online_id = $_ENV['default_online_id']; // Default Onlinelager ID 
  95.         $this->category_layout_id $_ENV['category_layout_id']; // Default Kategorie Layout ID
  96.         $this->material_category_layout_id $_ENV["material_category_layout_id"]; // Default Materialkategorie Layout ID 
  97.         $this->color_id $_ENV['color_id']; // Eigenschaften Gruppe ID Farbe
  98.         $this->fault_exists_id $_ENV['fault_exists_id']; // Eigenschaften Gruppe ID Fehler vorhanden
  99.         $this->thickness_id $_ENV['thickness_id']; // Eigenschaften Gruppe ID Materialstärke
  100.         $this->blocknumber_id $_ENV['blocknumber_id']; // Eigenschaften Gruppe ID Blocknummer
  101.         $this->stonenumber_id $_ENV['stonenumber_id']; // Eigenschaften Gruppe ID Steinnummer
  102.         $this->default_image_filename $_ENV['default_image_filename']; // Default Bildername
  103.         $this->language_id $_ENV['language_id']; // ID der Defaultsprache
  104.         $this->length_id=$_ENV['length_id']; // Eigenschaften Gruppe ID Länge
  105.         $this->width_id=$_ENV['width_id']; // Eigenschaften Gruppe ID Höhe
  106.         $this->amount_id=$_ENV['amount_id']; // Eigenschaften Gruppe ID Anzahl
  107.         $this->surfacearea_id=$_ENV['surfacearea_id']; // Eigenschaften Gruppe ID Quadratmeter
  108.         $this->meter_id=$_ENV['meter_id']; // Eigenschaften Gruppe ID Meter
  109.         $this->sa_id=$_ENV['sa_id']; // Eigenschaften Gruppe ID Sa
  110.         $this->squaremeterprice_id=$_ENV['squaremeterprice_id']; // Eigenschaften Gruppe ID Quadratmeterpreis
  111.         $this->artikeldaten_innen_id=$_ENV['artikeldaten_innen_id']; // Eigenschaften Gruppe ID Innen
  112.         $this->artikeldaten_aussen_id=$_ENV['artikeldaten_aussen_id']; // Eigenschaften Gruppe ID Außen
  113.         $this->tax_id=$_ENV["tax_id"]; //ID der Defaultsteuern
  114.         $this->currency_id=$_ENV["currency_id"]; // ID der Defaultwährung
  115.         $this->deliverytime_id=$_ENV["deliverytime_id"]; //ID der Defaultlieferzeit
  116.         $this->saleschannel_id=$_ENV["saleschannel_id"]; //ID der Default Verkaufskanal
  117.         $this->salutation_id=$_ENV["salutation_id"]; // ID der Default Ansprache (Keine Angabe)
  118.         $this->payment_method_id=$_ENV["payment_method_id"]; // ID der Default Zahlungsmethode (Nachname)
  119.         $this->group_id=$_ENV["group_id"]; // ID der Default Kundengruppe
  120.         $this->country_id=$_ENV["country_id"]; // ID des Default Landes (Deutschland)
  121.         $this->media_folder_id=$_ENV["media_folder_id"]; // ID des Mediafolders für den Bilderupload
  122.         $this->artikeldaten_ids=array();
  123.             array_push($this->artikeldaten_ids,$_ENV['artikeldaten_innen_kuechen_ja']); // Eigenschaft Innen Küchen Ja ID
  124.             array_push($this->artikeldaten_ids,$_ENV['artikeldaten_innen_kuechen_nein']); // Eigenschaft Innen Küchen Nein ID
  125.             array_push($this->artikeldaten_ids,$_ENV['artikeldaten_innen_fenster_ja']); // Eigenschaft Innen Fenster Ja ID
  126.             array_push($this->artikeldaten_ids,$_ENV['artikeldaten_innen_fenster_nein']); // Eigenschaft Innen Fenster Nein ID
  127.             array_push($this->artikeldaten_ids,$_ENV['artikeldaten_innen_bad_ja']); // Eigenschaft Innen Bad Ja ID
  128.             array_push($this->artikeldaten_ids,$_ENV['artikeldaten_innen_bad_nein']); // Eigenschaft Innen Bad Nein ID
  129.             array_push($this->artikeldaten_ids,$_ENV['artikeldaten_innen_fussboden_ja']); // Eigenschaft Innen Fußboden Ja ID
  130.             array_push($this->artikeldaten_ids,$_ENV['artikeldaten_innen_fussboden_nein']); // Eigenschaft Innen Fußboden Nein ID
  131.             array_push($this->artikeldaten_ids,$_ENV['artikeldaten_innen_boden_ja']); // Eigenschaft Innen Boden Ja ID
  132.             array_push($this->artikeldaten_ids,$_ENV['artikeldaten_innen_boden_nein']); // Eigenschaft Innen Boden Nein ID
  133.             array_push($this->artikeldaten_ids,$_ENV['artikeldaten_aussen_boden_ja']); // Eigenschaft Außen Boden Ja ID
  134.             array_push($this->artikeldaten_ids,$_ENV['artikeldaten_aussen_boden_nein']); // Eigenschaft Außen Boden Nein ID
  135.             array_push($this->artikeldaten_ids,$_ENV['artikeldaten_aussen_fassade_ja']); // Eigenschaft Außen Fassade Ja ID
  136.             array_push($this->artikeldaten_ids,$_ENV['artikeldaten_aussen_fassade_nein']); // Eigenschaft Außen Fassade Nein ID
  137.             array_push($this->artikeldaten_ids,$_ENV['artikeldaten_aussen_fenster_ja']); // Eigenschaft Außen Fenster Ja ID
  138.             array_push($this->artikeldaten_ids,$_ENV['artikeldaten_aussen_fenster_nein']); // Eigenschaft Außen Fenster Nein ID
  139.             array_push($this->artikeldaten_ids,$_ENV['artikeldaten_aussen_stufen_ja']); // Eigenschaft Außen Stufen Ja ID
  140.             array_push($this->artikeldaten_ids,$_ENV['artikeldaten_aussen_stufen_nein']); // Eigenschaft Außen Stufen Nein ID
  141.             array_push($this->artikeldaten_ids,$_ENV['artikeldaten_aussen_salz_ja']); // Eigenschaft Außen Salz Ja ID
  142.             array_push($this->artikeldaten_ids,$_ENV['artikeldaten_aussen_salz_nein']); // Eigenschaft Außen Salz Nein ID 
  143.     }
  144.     /**
  145.      * @Route("/import", name="import")
  146.      */
  147.     public function index(): Response
  148.     {
  149.         return $this->render('import/index.html.twig', [
  150.             'controller_name' => 'ImportController',
  151.         ]);
  152.     }
  153.     /**
  154.      * ######## Verbindungsaufbau zur Shopware-API ########
  155.      * @return array
  156.      */
  157.     private function client_connect()
  158.     {
  159.         // $client = new Client([ 'timeout' => 5.0, ]);        
  160.         $this->client = new Client([
  161.             'base_uri' => $this->base_url.'api',
  162.         ]);
  163.         // if(isset($this->client_username) && isset($this->client_password)){
  164.             if(isset($this->client_secret) && isset($this->client_id)){
  165.                 try{
  166.                     // if(isset($this->client_secret) && isset($this->client_id)){
  167.                     $response $this->client->request('POST''/api/oauth/token', array( "form_params" => array(
  168.                         // "debug" => true, 
  169.                         'allow_redirects'=>array('strict'=>true),
  170.                         "grant_type" => "client_credentials",
  171.                         "client_id" =>  $this->client_id,
  172.                         "client_secret" =>  $this->client_secret
  173.                     )));
  174.                 // }
  175.         } catch (RequestException $e) {
  176.             // echo Psr7\str($e->getRequest());
  177.             // if ($e->hasResponse()) {
  178.                 //     echo Psr7\str($e->getResponse());
  179.                 // }
  180.                 if ($e->hasResponse()) {
  181.                     $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  182.                 }else{
  183.                     $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  184.                 }
  185.                 $response->headers->set('Content-Type''application/json');
  186.                 return $response;
  187.                 exit;
  188.             }
  189.         }else {
  190.             try{
  191.                 if(isset($this->client_username) && isset($this->client_password)){
  192.                     // var_dump("<pre>");
  193.                     $response $this->client->request('POST''/api/oauth/token', array( 
  194.                         // "debug" => true, 
  195.                         'allow_redirects'=>array('strict'=>true),
  196.                         "form_params" => array(
  197.                             "client_id"=> "administration",
  198.                             "grant_type"=> "password",
  199.                             "scopes"=> "write",
  200.                             "username"=> $this->client_username,
  201.                             "password"=> $this->client_password
  202.                         )
  203.                     ));
  204.                 }
  205.             } catch (RequestException $e) {
  206.                 // echo Psr7\str($e->getRequest());
  207.                 // if ($e->hasResponse()) {
  208.                 //     echo Psr7\str($e->getResponse());
  209.                 // }
  210.                 if ($e->hasResponse()) {
  211.                     $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  212.                 }else{
  213.                     $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  214.                 }
  215.                 $response->headers->set('Content-Type''application/json');
  216.                 dd($response);
  217.                 return $response;
  218.                 exit;
  219.             }
  220.         }
  221.         // exit;
  222.         $response_body json_decode($response->getBody()->getContents());
  223.         //** Header used for every request
  224.         $this->header = array(
  225.             "Accept" => "application/json",
  226.             "Content-type" => "application/json",
  227.             // "indexing-behavior" => "use-queue-indexing",
  228.             "Authorization" => $response_body->token_type." ".$response_body->access_token,
  229.         );
  230.     }
  231.     private function shop_connect(){
  232.         // return $this->getDoctrine()->getConnection( $this->shopware_data); // DEV
  233.         return $this->getDoctrine()->getConnection$this->shopware_data_live); // LIVE
  234.     }
  235.     private function dump_connect(){
  236.         // return $this->getDoctrine()->getConnection( $this->shopware_data_dump); // DEV
  237.         return $this->getDoctrine()->getConnection$this->shopware_data_dump_live); // LIVE
  238.     }
  239.     /**
  240.      * Importiert Kategorien anhand der Materialgruppen
  241.      * 
  242.      * @Route("/import/category", name="import_category")
  243.      * 
  244.      * @return mixed
  245.      */
  246.     public function import_category()
  247.     {
  248.         $this->client_connect();
  249.         $conn_shopware $this->shop_connect();
  250.         $conn_akn $this->dump_connect();
  251.        
  252.         $sql "INSERT INTO pixel_dates (FunctionName,StartEnde,UpdateDatetime)
  253.                 VALUES(?,?,?)";
  254.         $stmtt $conn_shopware->prepare($sql);
  255.         $stmtt->execute(array( 
  256.             "import/category",
  257.             "Start",
  258.             date('Y-m-d H:i:s')
  259.         ));
  260.         $sql "SELECT 
  261.                 *
  262.                 , m.id as materialsid
  263.             FROM materials AS m
  264.             LEFT JOIN materialgroups AS mg ON (mg.materialgroup_id = m.materialgroup_id)
  265.             WHERE upload_done IS NULL
  266.             ORDER BY material_name DESC
  267.             ;";
  268.         $stmt $conn_akn->prepare($sql);
  269.         $stmt->execute();
  270.         $materialgroups $stmt->fetchAll();
  271.         // dd($materialgroups);
  272.         foreach($materialgroups as $material){
  273.             $kategorie=$this->checkCategory($material["materialgroup_name"], $material["materialgroup_id"], $material["material_name"], $material["material_id"], $material["picture"],true);
  274.             
  275.             $sqlt "UPDATE materials SET
  276.                     upload_done = ?
  277.             WHERE id = '".$material["materialsid"]."';";
  278.             $stmtt $conn_akn->prepare($sqlt);
  279.             $stmtt->execute(array(1));
  280.         }
  281.         $sql "INSERT INTO pixel_dates (FunctionName,StartEnde,UpdateDatetime)
  282.                 VALUES(?,?,?)";
  283.         $stmtt $conn_shopware->prepare($sql);
  284.         $stmtt->execute(array( 
  285.             "import/category",
  286.             "ENDE",
  287.             date('Y-m-d H:i:s')
  288.         ));
  289.         $response = new Response(json_encode(array('success' => true)));
  290.         $response->headers->set('Content-Type''application/json');
  291.         return $response;
  292.         return $this->render('update/index.html.twig', ["return" => ['Materialgruppen' => "done"]]);
  293.     }
  294.     
  295.     /**
  296.      * get color property id for import
  297.      */
  298.     public function getColorID($mgr_id$mar_id)
  299.     {
  300.         $this->client_connect();
  301.         $conn_shopware $this->shop_connect();
  302.         $conn_akn $this->dump_connect();
  303.         $sql "SELECT *
  304.                 FROM materialcolors
  305.                 WHERE color_id != -1
  306.                 AND mgr_id = '".$mgr_id."'
  307.                 AND mar_id = '".$mar_id."'
  308.             ;";
  309.         $stmt $conn_akn->prepare($sql);
  310.         $stmt->execute();
  311.         $colors $stmt->fetchAll();
  312.         // return $this->render('update/index.html.twig', ["return" => ['success' => $colors ]]);
  313.         if(count($colors) < && !isset($colors[0])){
  314.             // wenn farbe nicht gefunden überprüfe ob nur mit mar_id eine gefunden werden kann
  315.             $sql "SELECT *
  316.                     FROM materialcolors
  317.                     WHERE color_id != -1
  318.                     AND mar_id = '".$mar_id."'
  319.                 ;";
  320.             $stmt $conn_akn->prepare($sql);
  321.             $stmt->execute();
  322.             $colors $stmt->fetchAll();
  323.         }
  324.         if(isset($colors[0]["color_id"]) && !empty($colors[0]["color_id"])){
  325.             $sql "SELECT *
  326.                     FROM `keys`
  327.                     WHERE key_kind = 88
  328.                     AND key_number = '".$colors[0]["color_id"]."'
  329.                 ;";
  330.             $stmt $conn_akn->prepare($sql);
  331.             $stmt->execute();
  332.             $key $stmt->fetchAll();
  333.             $color explode('#',$key[0]["key_text"]);
  334.             $farbe trim($color[0]);
  335.             // return $this->render('update/index.html.twig', ["return" => ['success' => $key ]]);
  336.             if(isset($farbe) && !empty($farbe)){ // überprüfe ob als Variante vorhanden ist
  337.                 $sql "SELECT 
  338.                         *
  339.                         , HEX(id) as id
  340.                         , HEX(language_id) as language_id
  341.                         , HEX(property_group_id) as property_group_id
  342.                     FROM property_group_option
  343.                     INNER JOIN property_group_option_translation  ON (property_group_option.id = property_group_option_translation.property_group_option_id)
  344.                     WHERE LOWER(HEX(property_group_id)) = '".$this->color_id."'
  345.                     AND `name` = '".$farbe."'
  346.                     ;";
  347.                 $stmt $conn_shopware->prepare($sql);
  348.                 $stmt->execute();
  349.                 $property_color $stmt->fetchAll();
  350.                 // return $this->render('update/index.html.twig', ["return" => ['materials' => $materials, 'search_materials' => $search_materials]]);
  351.                 if(isset($property_color) && !empty($property_color) && count($property_color) > 0){
  352.                     // property vorhanden
  353.                 }else{
  354.                     $post_property $this->client->request('POST''/api/property-group-option', array(
  355.                         'allow_redirects'=>array('strict'=>true),
  356.                         'headers' => $this->header,
  357.                         'body' => json_encode([
  358.                             'groupId' => $this->color_id,
  359.                             'name' => $farbe
  360.                         ])
  361.                     ));
  362.                     $sql "SELECT 
  363.                             *
  364.                             , LOWER(HEX(id)) as id
  365.                             , HEX(language_id) as language_id
  366.                             , HEX(property_group_id) as property_group_id
  367.                         FROM property_group_option
  368.                         INNER JOIN property_group_option_translation  ON (property_group_option.id = property_group_option_translation.property_group_option_id)
  369.                         WHERE LOWER(HEX(property_group_id)) = '".$this->color_id."'
  370.                         AND `name` = '".$farbe."'
  371.                         ;";
  372.                     $stmt $conn_shopware->prepare($sql);
  373.                     $stmt->execute();
  374.                     $property_color $stmt->fetchAll();
  375.                 }
  376.                 $property_color[0]["id"] = strtolower($property_color[0]["id"]);
  377.                 // return $this->render('update/index.html.twig', ["return" => ['color' => $property_color ]]);
  378.                 if(isset($property_color[0]["id"]) && !empty($property_color[0]["id"])) {
  379.                     return $property_color[0]["id"];
  380.                 }
  381.             }
  382.         }
  383.         return null;
  384.     }
  385.     
  386.     /**
  387.      * Prüfe ob materialsettings weitere Kategorien beinhält und gib Liste der entsprechenden IDs zurück
  388.      */
  389.     public function getAdditionalCategories($materialgroup_id$material_id){
  390.         $conn_akn $this->dump_connect();
  391.         $additionalCategories=array();
  392.         $sql "SELECT 
  393.                   mgr_id
  394.                 , mar_id
  395.                 , m1.material_name AS material_name
  396.                 , amgr_id
  397.                 , amar_id
  398.                 , m2.material_name AS material_name_match
  399.                 , m2.picture AS picture
  400.                 , materialgroups.materialgroup_name
  401.             FROM materialsettings
  402.             INNER JOIN materials AS m1 ON (m1.materialgroup_id = materialsettings.mgr_id AND m1.material_id = materialsettings.mar_id)
  403.             INNER JOIN materials AS m2 ON (m2.materialgroup_id = materialsettings.amgr_id AND m2.material_id = materialsettings.amar_id)
  404.             LEFT JOIN materialgroups ON (materialgroups.materialgroup_id = materialsettings.amgr_id)
  405.             WHERE mgr_id = '".$materialgroup_id."'
  406.             AND mar_id = '".$material_id."'
  407.             ;";
  408.         $stmt $conn_akn->prepare($sql);
  409.         $stmt->execute();
  410.         $categoryMatch $stmt->fetchAll();
  411.         foreach($categoryMatch as $category){
  412.             if(isset($category["material_name_match"]) && !empty($category["material_name_match"]) && $category["material_name_match"] != ''){
  413.                 $catId $this->checkCategory($category["materialgroup_name"], $category["amgr_id"], $category["material_name_match"], $category["amar_id"], $category["picture"],false);
  414.                 if($catId != NULL){
  415.                     array_push($additionalCategories,$catId);
  416.                 }
  417.             }
  418.         }
  419.         // dd($additionalCategories);
  420.         if(empty($additionalCategories)){
  421.             return null;
  422.         }else{
  423.             return $additionalCategories;
  424.         }
  425.     }
  426.     /**
  427.      * check if categories exist, if not create them and return id of material category
  428.      * pattern: materialgroup -> material -> article:block_number
  429.      */
  430.     public function checkCategory($materialgroup_name$materialgroup_id$material_name$material_id$material_picture$update){
  431.         $conn_shopware $this->shop_connect();
  432.         $matgroup_id null;
  433.         $mat_id null;
  434.         $picture_id=null;
  435.         if(empty(trim($materialgroup_name)) || empty(trim($materialgroup_id)) ||
  436.            empty(trim($material_name)) || empty(trim($material_id))){
  437.             return null
  438.         }
  439.         try {
  440.             $search $this->client->request('GET''/api/category?filter[name]='.$materialgroup_name.'', array( 
  441.                 "headers" => $this->header,
  442.             ));
  443.         } catch (RequestException $e) {
  444.             // echo Psr7\str($e->getRequest());
  445.             // if ($e->hasResponse()) {
  446.             //         echo Psr7\str($e->getResponse());
  447.             //     }
  448.             if ($e->hasResponse()) {
  449.                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  450.             }else{
  451.                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  452.             }
  453.             $response->headers->set('Content-Type''application/json');
  454.             return $response;
  455.             exit;
  456.         }
  457.         $categories json_decode($search->getBody()->getContents(),true);
  458.         $sql "SELECT *
  459.                 , LOWER(HEX(id)) as id
  460.                 , LOWER(HEX(category_id)) as category_id
  461.                 , LOWER(HEX(parent_id)) as parent_id
  462.             FROM category
  463.             INNER JOIN category_translation ON (category.id = category_translation.category_id)
  464.             WHERE LOWER(HEX(language_id)) = '".$this->language_id."'
  465.             AND JSON_EXTRACT(category_translation.custom_fields,'$.custom_materialgroup_id') = '".$materialgroup_id."'
  466.             AND name = '".$materialgroup_name."'
  467.             AND LOWER(HEX(parent_id)) = '".$this->default_category_id."'
  468.             ;";
  469.         $stmt $conn_shopware->prepare($sql);
  470.         $stmt->execute();
  471.         $group_category $stmt->fetchAll();
  472.         if(isset($material_picture) && !empty($material_picture)){
  473.             $pictureexplode("."$material_picture);
  474.             $type array_pop($picture);
  475.             $picture implode('.'$picture);
  476.             $picture trim(preg_replace("/\.+$/",'',$picture));
  477.             
  478.             try {
  479.                 $search $this->client->request('GET''/api/media?filter[fileName]='.$picture.'', array( 
  480.                     "headers" => $this->header,
  481.                 ));
  482.             } catch (RequestException $e) {
  483.                 // echo Psr7\str($e->getRequest());
  484.                 // if ($e->hasResponse()) {
  485.                 //         echo Psr7\str($e->getResponse());
  486.                 //     }
  487.                 if ($e->hasResponse()) {
  488.                     $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  489.                 }else{
  490.                     $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  491.                 }
  492.                 $response->headers->set('Content-Type''application/json');
  493.                 return $response;
  494.                 exit;
  495.             }
  496.             $search_picture json_decode($search->getBody()->getContents(),true);
  497.                 
  498.             // dd($search_picture);
  499.             if($search_picture["total"] > 0){
  500.                 // return $this->render('update/index.html.twig', ["return" => ['search picture' => $search_picture, 'media url' => $mediaUrl ]]);
  501.                 $picture_id $search_picture["data"][0]["id"];
  502.             }
  503.             // dd($picture_id);
  504.         }
  505.         // dd($group_category);
  506.         if(!is_array($group_category) || empty($group_category) || count($group_category) < 1){ //erstelle Hauptkategorie wenn noch nicht vorhanden
  507.             $CategoryFlat = new \App\Controller\shopware\CategoryFlat();
  508.             $CategoryFlat->setType("page");
  509.             $CategoryFlat->setName($materialgroup_name);
  510.             $CategoryFlat->setCustomFields(array("custom_materialgroup_id" => $materialgroup_id));
  511.             $CategoryFlat->setCmsPageId($this->category_layout_id); 
  512.             $CategoryFlat->setParentId($this->default_category_id);
  513.             $post_category $this->client->request('POST''/api/category', array(
  514.                 // 'debug' => true, 
  515.                 'allow_redirects'=>array('strict'=>true),
  516.                 "headers" => $this->header,
  517.                 "body" => $CategoryFlat,
  518.             ));
  519.             $location array_pop$post_category->getHeaders()['Location'] );
  520.             $matgroup_id basename($location);
  521.         }else{ // prüfe Material
  522.             $matgroup_id $group_category[0]["id"];
  523.         }
  524.         $sql "SELECT 
  525.                 *
  526.                 , LOWER(HEX(id)) as id
  527.                 , LOWER(HEX(category_id)) as category_id
  528.                 , LOWER(HEX(parent_id)) as parent_id
  529.             FROM category
  530.             INNER JOIN category_translation ON (category.id = category_translation.category_id)
  531.             WHERE LOWER(HEX(language_id)) = '".$this->language_id."'
  532.             AND JSON_EXTRACT(category_translation.custom_fields,'$.custom_material_id') = '".$material_id."'
  533.             AND name = '".$material_name."'
  534.             AND LOWER(HEX(parent_id)) = '".$matgroup_id."'
  535.             ;";
  536.         $stmt $conn_shopware->prepare($sql);
  537.         $stmt->execute();
  538.         $material_category $stmt->fetchAll();
  539.         // dd($material_category);
  540.         if(!is_array($material_category) || empty($material_category) || count($material_category) < 1){ //erstelle Kategorie
  541.             $CategoryFlat = new \App\Controller\shopware\CategoryFlat();
  542.             $CategoryFlat->setType("page");
  543.             $CategoryFlat->setName($material_name);
  544.             if($picture_id!= null && !empty($picture_id)){
  545.                 $CategoryFlat->setMediaId($picture_id);
  546.             }
  547.             $CategoryFlat->setCustomFields(array("custom_material_id" => $material_id));
  548.             $CategoryFlat->setCmsPageId($this->material_category_layout_id); 
  549.             $CategoryFlat->setParentId($matgroup_id);
  550.             $post_category $this->client->request('POST''/api/category', array(
  551.                 // 'debug' => true, 
  552.                 'allow_redirects'=>array('strict'=>true),
  553.                 "headers" => $this->header,
  554.                 "body" => $CategoryFlat,
  555.             ));
  556.             $location array_pop$post_category->getHeaders()['Location'] );
  557.             $mat_id basename($location);
  558.         }else{ // prüfe Material
  559.             if($update==true){
  560.                 $CategoryFlat = new \App\Controller\shopware\CategoryFlat();
  561.                 $CategoryFlat->setName($material_name);
  562.                 if($picture_id!= null && !empty($picture_id)){
  563.                     $CategoryFlat->setMediaId($picture_id);
  564.                 }
  565.                 $CategoryFlat->setCustomFields(array("custom_material_id" => $material_id));
  566.                 $CategoryFlat->setCmsPageId($this->material_category_layout_id); 
  567.                 $CategoryFlat->setParentId($matgroup_id);
  568.                 // return $this->render('update/index.html.twig', ["return" => [ 'Kategorien' => $CategoryFlat]]);
  569.                 
  570.                 try {
  571.                     $post_category $this->client->request('PATCH''/api/category/'.$material_category[0]["id"].'', array(
  572.                         'allow_redirects'=>array('strict'=>true),
  573.                         "headers" => $this->header,
  574.                         "body" => $CategoryFlat
  575.                     ));
  576.                 } catch (RequestException $e) {
  577.                     // echo Psr7\str($e->getRequest());
  578.                     // if ($e->hasResponse()) {
  579.                     //         echo Psr7\str($e->getResponse());
  580.                     //     }
  581.                     if ($e->hasResponse()) {
  582.                         $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  583.                     }else{
  584.                         $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  585.                     }
  586.                     $response->headers->set('Content-Type''application/json');
  587.                     return $response;
  588.                     exit;
  589.                 }
  590.             }
  591.             $mat_id $material_category[0]["id"];
  592.         }
  593.         // dd($mat_id);
  594.         return $mat_id;
  595.     }
  596.     /**
  597.      * get category id for import
  598.      */
  599.     public function getCategoryId($category$materialgroup_id//article[materialgroup_name]
  600.     {
  601.         // prüfe ob kategorie vorhanden ist, 
  602.         if(empty(trim($category)) || empty(trim($materialgroup_id)) ){
  603.             return null
  604.         }
  605.         try {
  606.             $search $this->client->request('GET''/api/category?filter[name]='.$category.'', array( 
  607.                 "headers" => $this->header,
  608.             ));
  609.         } catch (RequestException $e) {
  610.             // echo Psr7\str($e->getRequest());
  611.             // if ($e->hasResponse()) {
  612.             //     echo Psr7\str($e->getResponse());
  613.             // }
  614.             if ($e->hasResponse()) {
  615.                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  616.             }else{
  617.                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  618.             }
  619.             $response->headers->set('Content-Type''application/json');
  620.             return $response;
  621.             exit;
  622.         }
  623.         $categories json_decode($search->getBody()->getContents(),true);
  624.         if(isset($categories["data"][0]["id"]) && !empty($categories["data"][0]["id"])){
  625.         }else{
  626.             $CategoryFlat = new \App\Controller\shopware\CategoryFlat();
  627.             $CategoryFlat->setType("page");
  628.             $CategoryFlat->setName($category);
  629.             $CategoryFlat->setCustomFields(array("custom_materialgroup_id" => $materialgroup_id));
  630.             $CategoryFlat->setCmsPageId($this->category_layout_id); 
  631.             $CategoryFlat->setParentId($this->default_category_id);
  632.             $post_category $this->client->request('POST''/api/category', array(
  633.                 // 'debug' => true, 
  634.                 'allow_redirects'=>array('strict'=>true),
  635.                 "headers" => $this->header,
  636.                 "body" => $CategoryFlat,
  637.             ));
  638.             try {
  639.                 $search $this->client->request('GET''/api/category?filter[name]='.$category.'', array( 
  640.                     "headers" => $this->header,
  641.                 ));
  642.             } catch (RequestException $e) {
  643.                 // echo Psr7\str($e->getRequest());
  644.                 // if ($e->hasResponse()) {
  645.                 //     echo Psr7\str($e->getResponse());
  646.                 // }
  647.                 if ($e->hasResponse()) {
  648.                     $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  649.                 }else{
  650.                     $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  651.                 }
  652.                 $response->headers->set('Content-Type''application/json');
  653.                 return $response;
  654.                 exit;
  655.             }
  656.             $categories json_decode($search->getBody()->getContents(),true);
  657.         }
  658.         if(isset($categories["data"][0]["id"]) && !empty($categories["data"][0]["id"])){
  659.             return $categories["data"][0]["id"];
  660.         }else{
  661.             return $this->default_category_id
  662.         }
  663.     }
  664.     /**
  665.      * add oder update Bild bei Artikel
  666.      * 
  667.      */
  668.     public function add_image($bildname$produktId$zusatzbild)
  669.     {
  670.         // dd($bildname, $produktId, $zusatzbild);
  671.         $this->client_connect();
  672.         $conn_shopware $this->shop_connect();
  673.         if( empty(trim($bildname)) || empty(trim($produktId)) ){
  674.             return false;
  675.         }
  676.         //hole gesetzte Mediafiles im Produkt ab
  677.         $sql "SELECT 
  678.                 *
  679.                 , LOWER(HEX(product_id)) as product_id
  680.                 , LOWER(HEX(media_id)) as media_id
  681.             FROM product_media
  682.             INNER JOIN media ON (product_media.media_id = media.id)
  683.             WHERE LOWER(HEX(product_id)) = '".$produktId."'
  684.             ;";
  685.         $stmt $conn_shopware->prepare($sql);
  686.         $stmt->execute();
  687.         $mediaSW $stmt->fetchAll();
  688.         $bildname str_replace('"','',$bildname);
  689.         if(strpos($bildname,"-##-")){
  690.             $bildnamesubstr($bildname,0,-5);
  691.         }
  692.         $bildname str_replace('"','',$bildname);
  693.         $article_file_new preg_replace("/´|`|&/"' '$bildname);
  694.         $article_file_new preg_replace("/\s\s+/"' '$article_file_new);
  695.         $article_file_new str_replace(array('ü','ä','ö','+','ß'),array('ue','ae','oe','_','ss'),$article_file_new);
  696.         $filenameexplode("."$article_file_new);
  697.         $type array_pop($filename);
  698.         $filename implode('.'$filename);
  699.         $filename trim(preg_replace("/\.+$/",'',$filename));
  700.    
  701.         // prüfen ob Bild schon vorhanden ist
  702.         try {
  703.             $search $this->client->request('GET''/api/media?filter[fileName]='.$filename.'', array( 
  704.             "headers" => $this->header,
  705.             ));
  706.         } catch (RequestException $e) {
  707.             // echo Psr7\str($e->getRequest());
  708.             // if ($e->hasResponse()) {
  709.             //     echo Psr7\str($e->getResponse());
  710.             // }
  711.             if ($e->hasResponse()) {
  712.                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  713.             }else{
  714.                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  715.             }
  716.             $response->headers->set('Content-Type''application/json');
  717.             return $response;
  718.             exit;
  719.         }
  720.         $search_picture json_decode($search->getBody()->getContents(),true);
  721.         if($search_picture["total"] > 0){
  722.             // return $this->render('update/index.html.twig', ["return" => ['search picture' => $search_picture, 'media url' => $mediaUrl ]]);
  723.             $Id $search_picture["data"][0]["id"];
  724.             if($zusatzbild){
  725.                 // prüfe ob verknüpfung zwischen bild und produkt existiert, wenn nicht erstelle sie
  726.                 $sql "SELECT 
  727.                         *
  728.                         , LOWER(HEX(product_id)) as product_id
  729.                         , LOWER(HEX(media_id)) as media_id
  730.                     FROM product_media
  731.                     INNER JOIN media ON (product_media.media_id = media.id)
  732.                     WHERE LOWER(HEX(product_id)) = '".$produktId."'
  733.                     AND file_name = '".$filename."'
  734.                     ;";
  735.                 $stmt $conn_shopware->prepare($sql);
  736.                 $stmt->execute();
  737.                 $product_media $stmt->fetchAll();
  738.                 if(isset($product_media) && !empty($product_media) && count($product_media) > 0){
  739.                 }else{
  740.                     try{
  741.                         $post_product $this->client->request('POST''/api/product-media/', array(
  742.                             // 'debug' => true,
  743.                             'allow_redirects'=>array('strict'=>true),
  744.                             'headers' => $this->header,
  745.                             'body' => json_encode([
  746.                                 'productId' => $produktId,
  747.                                 'position' => 2,
  748.                                 'media' => array(
  749.                                     'id' => strtolower($Id)
  750.                                 )
  751.                             ])
  752.                         ));
  753.                         // $location = array_pop( $post_product->getHeaders()['Location'] );
  754.                         // $productmediaId = basename($location);
  755.                     } catch (RequestException $e) {
  756.                         // echo Psr7\str($e->getRequest());
  757.                         // if ($e->hasResponse()) {
  758.                         //     echo Psr7\str($e->getResponse());
  759.                         // }
  760.                         if ($e->hasResponse()) {
  761.                             $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  762.                         }else{
  763.                             $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  764.                         }
  765.                         $response->headers->set('Content-Type''application/json');
  766.                         return $response;
  767.                         exit;
  768.                     }
  769.                 }
  770.             }else{
  771.                 $sql "DELETE
  772.                     FROM product_media
  773.                     WHERE LOWER(HEX(product_id)) = '".$produktId."'
  774.                     ;";
  775.                 $stmt $conn_shopware->prepare($sql);
  776.                 $stmt->execute(); 
  777.                 try{
  778.                     $post_product $this->client->request('POST''/api/product-media/', array(
  779.                         // 'debug' => true,
  780.                         'allow_redirects'=>array('strict'=>true),
  781.                         'headers' => $this->header,
  782.                         'body' => json_encode([
  783.                             'productId' => $produktId,
  784.                             'position' => 1,
  785.                             'media' => array(
  786.                                 'id' => strtolower($Id)
  787.                             )
  788.                         ])
  789.                     ));
  790.                     $location array_pop$post_product->getHeaders()['Location'] );
  791.                     $productmediaId basename($location);
  792.                 } catch (RequestException $e) {
  793.                     // echo Psr7\str($e->getRequest());
  794.                     // if ($e->hasResponse()) {
  795.                     //     echo Psr7\str($e->getResponse());
  796.                     // }
  797.                     if ($e->hasResponse()) {
  798.                         $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  799.                     }else{
  800.                         $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  801.                     }
  802.                     $response->headers->set('Content-Type''application/json');
  803.                     return $response;
  804.                     exit;
  805.                 }
  806.                 // ADD COVER                   
  807.                 try{
  808.                     // return var_dump("cover zuordnen". $produktId." - ".$productmediaId); 
  809.                     $post_product $this->client->request('PATCH''/api/product/'.$produktId, array(
  810.                         // 'debug' => true,
  811.                         'allow_redirects'=>array('strict'=>true),
  812.                         'headers' => $this->header,
  813.                         'body' => json_encode([
  814.                             'coverId' => strtolower($productmediaId)
  815.                         ])
  816.                     ));
  817.                 } catch (RequestException $e) {
  818.                     // echo Psr7\str($e->getRequest());
  819.                     // if ($e->hasResponse()) {
  820.                     //     echo Psr7\str($e->getResponse());
  821.                     // }
  822.                     if ($e->hasResponse()) {
  823.                         $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  824.                     }else{
  825.                         $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  826.                     }
  827.                     $response->headers->set('Content-Type''application/json');
  828.                     return $response;
  829.                     exit;
  830.                 }
  831.             }
  832.         }
  833.         return true;
  834.     }
  835.     public function clean_url($string) {
  836.         $entities = array('%20','%21''%2A''%27''%28''%29''%3B''%3A''%40''%26''%3D''%2B''%24''%2C''%2F''%3F''%25''%23''%5B''%5D','ae','oe','ue');
  837.         $replacements = array(" ","!""*""'""("")"";"":""@""&""=""+""$"",""/""?""%""#""[""]","ä","ö","ü");
  838.         $string str_replace($entities$replacements$string);
  839.         return $string;
  840.     }
  841.     /**
  842.      * Bildimport
  843.      * 
  844.      * @Route("/import/images", name="import_images")
  845.      * 
  846.      * @return mixed
  847.      */
  848.     public function import_images()
  849.     {
  850.         // exit;
  851.         $this->client_connect();
  852.         $conn_shopware $this->shop_connect();
  853.         $step_count 100;
  854.         $picturetypes = array("jpg""JPG""jpeg""png""PNG""webp""gif""svg""bmp""tiff""tif""eps");
  855.         $valid_ext = array('png','jpeg','jpg');
  856.         $sql "INSERT INTO pixel_dates (FunctionName,StartEnde,UpdateDatetime)
  857.                 VALUES(?,?,?)";
  858.         $stmtt $conn_shopware->prepare($sql);
  859.         $stmtt->execute(array( 
  860.             "import/images",
  861.             "Start",
  862.             date('Y-m-d H:i:s')
  863.         ));
  864.         // Namen aller Bilddateien vom FTP holen
  865.         $article_files scandir($this->getParameter('kernel.project_dir')."/public/komprimiert");
  866.         $article_files array_diff($article_files, array('.','..'));
  867.         // var_dump($article_files);exit;
  868.         // Alle Produktbilder in SW
  869.         if(isset($article_files) && !empty($article_files) && count($article_files) > 0){
  870.             $sql "SELECT 
  871.                     file_name
  872.                     , file_extension
  873.                 FROM media
  874.                 ORDER BY file_name
  875.                 -- LIMIT ".$step_count."
  876.             ;";
  877.                 // --WHERE LOWER(HEX(media_folder_id)) = '".$this->media_folder_id."'
  878.             $stmt $conn_shopware->prepare($sql);
  879.             $stmt->execute();
  880.             $article $stmt->fetchAll();
  881.          
  882.             if(count($article_files) > $step_count){
  883.                 
  884.                 $article_files array_slice($article_files0$step_count);
  885.             }
  886.             // return $this->render('update/index.html.twig', ["return" => ['folder' => $article_files, 'sw' => $article ]]);
  887.             foreach($article_files as $article_file){
  888.                 // var_dump($article_file);
  889.                 // dd($article_files);
  890.                 // $article_file = '2319_P1030278 Nero Im+pala Rus`tenburg   ä  ö    ü  gefla&mmt 2´cm 218070548.jpg';
  891.                 $article_file_new preg_replace("/´|`|&/"' '$article_file);
  892.                 $article_file_new preg_replace("/\s\s+/"' '$article_file_new);
  893.                 $article_file_new str_replace(array('ü','ä','ö','+','ß'),array('ue','ae','oe','_','ss'),$article_file_new);
  894.                 $filenameexplode("."$article_file_new);
  895.                 $type array_pop($filename);
  896.                 $filename implode('.'$filename);
  897.                 $filename trim(preg_replace("/\.+$/",'',$filename));
  898.                 $article_file_new $filename.".".$type;
  899.                 rename($this->getParameter('kernel.project_dir')."/public/komprimiert/".$article_file,$this->getParameter('kernel.project_dir')."/public/komprimiert/".$filename.".".$type);
  900.                 
  901.                 //#### DATEIKOMPRIMIERUNG
  902.                
  903.                 // $location = $this->getParameter('kernel.project_dir')."/public/komprimiert/".$article_file_new;
  904.                 // $file_extension = pathinfo($location, PATHINFO_EXTENSION);
  905.                 // $file_extension = strtolower($file_extension);
  906.                 // // return $this->render('update/index.html.twig', ["return" => ['location' => $location ]]);
  907.                 // if(in_array($file_extension,$valid_ext)){
  908.                 //     $info = getimagesize($location);
  909.                 //     if ($info['mime'] == 'image/jpeg') 
  910.                 //     $image = imagecreatefromjpeg($location);
  911.                 
  912.                 //     elseif ($info['mime'] == 'image/png') 
  913.                 //     $image = imagecreatefrompng($location);
  914.                 
  915.                 //     imagejpeg($image, $location, 60); // image, path, quality 1-100
  916.                 // }else{
  917.                 //     echo "Invalid file type.";
  918.                 // }
  919.                 // return $this->render('update/index.html.twig', ["return" => [ 'article' => $article_files]]);
  920.                 
  921.                 if(in_array($type,$picturetypes)){
  922.                     $article_file_clean $this->clean_url($article_file_new);
  923.                     // $mediaUrl = trim('https://'.$_SERVER['HTTP_HOST'].'/komprimiert/'.$article_file_clean);
  924.                     $mediaUrl trim('https://'.$_SERVER['HTTP_HOST'].'/komprimiert/'.$article_file_clean);
  925.                     $mediaUrl str_replace(' ','%20',$mediaUrl);
  926.                     $mediaUrl str_replace('ä','ae',$mediaUrl);
  927.                     $mediaUrl str_replace('ö','oe',$mediaUrl);
  928.                     $mediaUrl str_replace('ü','ue',$mediaUrl);
  929.                     $mediaUrl str_replace('ß','ss',$mediaUrl);
  930.                     if(!in_array($filename,array_column($article'file_name') )){ //trim column?
  931.                         // Bild noch nicht vorhanden -> Lade hoch
  932.                         // return $this->render('update/index.html.twig', ["return" => [ 'article' => $this->header]]);
  933.                         $post_media $this->client->post('/api/media', array(
  934.                             // "debug" => true,
  935.                             'allow_redirects'=>array('strict'=>true),
  936.                             'headers' => $this->header,
  937.                             'body' => json_encode([
  938.                                 'mediaFolderId' => $this->media_folder_id
  939.                             ])
  940.                         ));
  941.                         $location array_pop$post_media->getHeaders()['Location'] );
  942.                         $mediaId basename($location);
  943.                         try{
  944.                             $post_media $this->client->request('POST''/api/_action/media/'.$mediaId.'/upload?extension='.$type.'&fileName='.$filename, array(
  945.                                 // "debug" => true,
  946.                                 'allow_redirects'=>array('strict'=>true),
  947.                                 'headers' => $this->header,
  948.                                 'body' => json_encode([
  949.                                     'url' => $mediaUrl,
  950.                                     'mediaFolderId' => $this->media_folder_id,
  951.                                 ])
  952.                             ));
  953.                         } catch (RequestException $e) {
  954.                             //TODO error report in tabelle schreiben
  955.                             // dd("test3");
  956.                             // echo Psr7\str($e->getRequest());
  957.                             // if ($e->hasResponse()) {
  958.                             //     echo Psr7\str($e->getResponse());
  959.                             // }
  960.                             if ($e->hasResponse()) {
  961.                                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  962.                             }else{
  963.                                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  964.                             }
  965.                             $response->headers->set('Content-Type''application/json');
  966.                             return $response;
  967.                             exit;
  968.                         } 
  969.                         //move file in uploaded folder
  970.                         // move_uploaded_file($this->getParameter('kernel.project_dir')."/public/komprimiert/".$article_file,$this->getParameter('kernel.project_dir')."/public/bilder_hochgeladen/".$article_file);
  971.                     }else{
  972.                         // bild ist nicht im bildordner vorhanden
  973.                         try {
  974.                             $search $this->client->request('GET''/api/media?filter[fileName]='.$filename.'', array( 
  975.                             // $search = $this->client->request('GET', '/api/media/6d98e8d9d2184172a5acc4a32095e843', array( 
  976.                             "headers" => $this->header,
  977.                             ));
  978.                         } catch (RequestException $e) {
  979.                             // //TODO error report in tabelle schreiben
  980.                             // dd("test2");
  981.                             // echo Psr7\str($e->getRequest());
  982.                             // if ($e->hasResponse()) {
  983.                             //     echo Psr7\str($e->getResponse());
  984.                             // }
  985.                             if ($e->hasResponse()) {
  986.                                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  987.                             }else{
  988.                                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  989.                             }
  990.                             $response->headers->set('Content-Type''application/json');
  991.                             return $response;
  992.                             exit;
  993.                         }
  994.                         $search_picture json_decode($search->getBody()->getContents(),true);
  995.                         // return $this->render('update/index.html.twig', ["return" => ['last product id' => $search_picture ]]);
  996.                         if(isset($search_picture) && !empty($search_picture) && count($search_picture) > 0){
  997.                             $post_product_id $search_picture["data"][0]["id"];
  998.                             if(count($search_picture) > 1){
  999.                                 // suche datensatz mit der richtigen dateiendung
  1000.                                 foreach($search_picture["data"] as $search_picture_file){
  1001.                                     if($search_picture_file["fileExtension"] == $type){
  1002.                                         $post_product_id $search_picture_file["id"]; 
  1003.                                         continue;
  1004.                                     }
  1005.                                 }
  1006.                             }
  1007.                             try{
  1008.                                 // $post_product = $this->client->request('POST', 'api/_action/media/'.$search_picture["data"][0]["id"].'/upload?extension='.$type.'&fileName='.$filename, array(
  1009.                                 $post_product $this->client->request('POST''api/_action/media/'.$post_product_id.'/upload?extension='.$type.'&fileName='.$filename, array(
  1010.                                     // 'debug' => true,
  1011.                                     'allow_redirects'=>array('strict'=>true),
  1012.                                     'headers' => $this->header,
  1013.                                     'body' => json_encode([
  1014.                                         'url' => $mediaUrl,
  1015.                                         'mediaFolderId' => $this->media_folder_id,
  1016.                                     ])
  1017.                                 ));
  1018.                             } catch (RequestException $e) {
  1019.                                 // //TODO error report in tabelle schreiben
  1020.                                 // dd($mediaUrl);
  1021.                                 // echo Psr7\str($e->getRequest());
  1022.                                 // if ($e->hasResponse()) {
  1023.                                 //     echo Psr7\str($e->getResponse());
  1024.                                 // }
  1025.                                 if ($e->hasResponse()) {
  1026.                                     $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  1027.                                 }else{
  1028.                                     $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  1029.                                 }
  1030.                                 $response->headers->set('Content-Type''application/json');
  1031.                                 return $response;
  1032.                                 exit;
  1033.                             }
  1034.                         }
  1035.                     }
  1036.                 }else{
  1037.                     //TODO report, nicht unterstützter filetype
  1038.                     // var_dump("filetype not supportet! ".$type);exit;
  1039.                 }
  1040.                 //delete file in uploaded folder
  1041.                 // exit;
  1042.                 // move_uploaded_file($this->getParameter('kernel.project_dir')."/public/komprimiert/".$article_file,$this->getParameter('kernel.project_dir')."/public/bilder_hochgeladen/".$article_file);
  1043.                 // dd($article_file_new);
  1044.                 unlink($this->getParameter('kernel.project_dir')."/public/komprimiert/".$article_file_new);
  1045.             }
  1046.             if(count($article) >= $step_count){
  1047.                 return $this->redirect('/import/images');
  1048.             }
  1049.         }else{
  1050.             $sql "SELECT MAX(UpdateDatetime) as lastdate FROM pixel_dates
  1051.                     WHERE FunctionName = 'import/images'
  1052.                     AND StartEnde = 'ENDE'";
  1053.             $stmt $conn_shopware->prepare($sql);
  1054.             $stmt->execute();
  1055.             $date $stmt->fetchAll();
  1056.             // dd($date[0]["lastdate"],  date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s'). '-3 minutes')));
  1057.             if($date[0]["lastdate"]<date('Y-m-d H:i:s'strtotime(date('Y-m-d H:i:s'). '-7 hours'))){
  1058.                 // Setze Endzeit nur Wenn letzter Durchlauf 7 Stunden her ist.
  1059.                 $sql "INSERT INTO pixel_dates (FunctionName,StartEnde,UpdateDatetime)
  1060.                         VALUES(?,?,?)";
  1061.                 $stmtt $conn_shopware->prepare($sql);
  1062.                 $stmtt->execute(array( 
  1063.                     "import/images",
  1064.                     "ENDE",
  1065.                     date('Y-m-d H:i:s')
  1066.                 ));
  1067.             }
  1068.             return $this->redirect('/copy/image');
  1069.         }
  1070.         // return $this->render('update/index.html.twig', ["return" => ['last product id' => count($articlesSW) ]]);
  1071.         $response = new Response(json_encode(array('success' => true)));
  1072.         $response->headers->set('Content-Type''application/json');
  1073.         return $response;
  1074.         return $this->render('update/index.html.twig', ["return" => ['Bilderimport' => "done" ]]);
  1075.     }
  1076.     /**
  1077.      * Fügt Attribute den eingelesenen Artikeln hinzu
  1078.      * 
  1079.      * @Route("/add/attributes", name="add_attributes")
  1080.      * 
  1081.      * @return mixed
  1082.      */
  1083.     public function add_attributes()
  1084.     {
  1085.         $this->client_connect();
  1086.         $conn_shopware $this->shop_connect();
  1087.         $step_count 5;
  1088.         $sql "SELECT 
  1089.             *
  1090.             , LOWER(HEX(product_id)) as product_id
  1091.             , HEX(product_version_id) as product_version_id
  1092.             , HEX(language_id) as language_id
  1093.             , JSON_EXTRACT(product_translation.custom_fields,'$.custom_article_material_name') as custom_article_material_name
  1094.             , JSON_EXTRACT(product_translation.custom_fields,'$.custom_article_materialgroup_name') as custom_article_materialgroup_name
  1095.             , JSON_EXTRACT(product_translation.custom_fields,'$.custom_article_surface') as custom_article_surface
  1096.             , JSON_EXTRACT(product_translation.custom_fields,'$.custom_article_articlegroup_name') as custom_article_articlegroup_name
  1097.             -- , JSON_EXTRACT(product_translation.custom_fields,'$.custom_article_picture') as custom_article_picture
  1098.             -- materialstärke
  1099.         FROM product_translation
  1100.         INNER JOIN product ON (product.id = product_id)
  1101.         WHERE JSON_EXTRACT(product_translation.custom_fields,'$.custom_article_attributes_added') IS NULL
  1102.         AND LOWER(HEX(language_id)) = '".$this->language_id."'
  1103.         LIMIT 10
  1104.         ;";
  1105.         $stmt $conn_shopware->prepare($sql);
  1106.         $stmt->execute();
  1107.         $products_sw $stmt->fetchAll();
  1108.         // return $this->render('update/index.html.twig', ["return" => ['attribute' => $products_sw ]]);
  1109.         foreach($products_sw as $product){
  1110.             // produkte abholen die flag nicht haben
  1111.             // einzelne customfelder durchgehen und attribute zuordnen
  1112.             // nicht vorhandene skippen
  1113.             
  1114.             unset($attribute);
  1115.             unset($attributsliste);
  1116.             $attributsliste=array();
  1117.             $idliste=array();
  1118.             if(isset($product["custom_article_material_name"]) && !empty($product["custom_article_material_name"] && $product["custom_article_material_name"] != "null")){
  1119.                 // array_push($attributsliste,str_replace('"','',$product["custom_article_material_name"]));
  1120.                 // array_push($idliste,$this->material_id);
  1121.                 // var_dump($this->material_id);
  1122.                 $attributsliste[$this->material_id]=str_replace('"','',$product["custom_article_material_name"]);
  1123.             }
  1124.             if(isset($product["custom_article_materialgroup_name"]) && !empty($product["custom_article_materialgroup_name"] && $product["custom_article_materialgroup_name"] != "null")){
  1125.                 // array_push($attributsliste,str_replace('"','',$product["custom_article_materialgroup_name"]));
  1126.                 // array_push($idliste,$this->materialgroup_id);
  1127.                 // var_dump($this->materialgroup_id);
  1128.                 $attributsliste[$this->materialgroup_id]=str_replace('"','',$product["custom_article_materialgroup_name"]);
  1129.             }
  1130.             if(isset($product["custom_article_surface"]) && !empty($product["custom_article_surface"]) && $product["custom_article_surface"] != "null"){
  1131.                 // array_push($attributsliste,str_replace('"','',$product["custom_article_surface"]));
  1132.                 // array_push($idliste,$this->surface_id);
  1133.                 // var_dump($product["custom_article_surface"]);
  1134.                 $attributsliste[$this->surface_id]=str_replace('"','',$product["custom_article_surface"]);
  1135.             }
  1136.             if(isset($product["custom_article_articlegroup_name"]) && !empty($product["custom_article_articlegroup_name"] && $product["custom_article_articlegroup_name"] != "null")){
  1137.                 // array_push($attributsliste,str_replace('"','',$product["custom_article_articlegroup_name"]));
  1138.                 // array_push($idliste,$this->articlegroup_id);
  1139.                 // var_dump($this->articlegroup_id);
  1140.                 $attributsliste[$this->articlegroup_id]=str_replace('"','',$product["custom_article_articlegroup_name"]);
  1141.             }
  1142.             // return $this->render('update/index.html.twig', ["return" => ['attribute' => $attributsliste ]]);
  1143.             $attribute=array();
  1144.             foreach($attributsliste as $key => $attribut){
  1145.                 $sql "SELECT 
  1146.                         *
  1147.                         , LOWER(HEX(id)) as id
  1148.                         , HEX(language_id) as language_id
  1149.                         , HEX(property_group_id) as property_group_id
  1150.                     FROM property_group_option
  1151.                     INNER JOIN property_group_option_translation  ON (property_group_option.id = property_group_option_translation.property_group_option_id)
  1152.                     -- WHERE LOWER(HEX(property_group_id)) = 'eadafb18e0c34aeb894dafddf1e107a0'
  1153.                     WHERE `name` = '".$attribut."'
  1154.                     AND LOWER(HEX(property_group_id)) = '".$key."'
  1155.                     ;";
  1156.                 $stmt $conn_shopware->prepare($sql);
  1157.                 $stmt->execute();
  1158.                 $search_property $stmt->fetchAll();
  1159.                 // return $this->render('update/index.html.twig', ["return" => ['dearch' => $search_property ]]);
  1160.                 if(isset($search_property) && is_array($search_property) && count($search_property) > 0) {
  1161.                     array_push($attribute,array('id'=>$search_property[0]["id"]));
  1162.                 }else{
  1163.                     // Attribut nicht in shopware und muss hinzugefügt werden
  1164.                     // var_dump("post ".$attribut." ".$key);exit;
  1165.                     $post_product $this->client->request('POST''/api/property-group-option', array(
  1166.                         'allow_redirects'=>array('strict'=>true),
  1167.                         'headers' => $this->header,
  1168.                         'body' => json_encode([
  1169.                             'groupId' => $key,
  1170.                             'name' => $attribut
  1171.                         ])
  1172.                     ));
  1173.                     $sql "SELECT 
  1174.                             *
  1175.                             , LOWER(HEX(id)) as id
  1176.                             , HEX(language_id) as language_id
  1177.                             , HEX(property_group_id) as property_group_id
  1178.                         FROM property_group_option
  1179.                         INNER JOIN property_group_option_translation  ON (property_group_option.id = property_group_option_translation.property_group_option_id)
  1180.                         -- WHERE LOWER(HEX(property_group_id)) = 'eadafb18e0c34aeb894dafddf1e107a0'
  1181.                         WHERE `name` = '".$attribut."'
  1182.                         AND LOWER(HEX(property_group_id)) = '".$key."'
  1183.                         ;";
  1184.                     $stmt $conn_shopware->prepare($sql);
  1185.                     $stmt->execute();
  1186.                     $search_property $stmt->fetchAll();
  1187.                     // return $this->render('update/index.html.twig', ["return" => ['dearch' => $search_property ]]);
  1188.                     array_push($attribute,array('id'=>$search_property[0]["id"]));
  1189.                 }
  1190.             }
  1191.             // return $this->render('update/index.html.twig', ["return" => ['attribute' => $attribute ]]);
  1192.             if(isset($product["custom_article_materialgroup_id"]) && !empty($product["custom_article_materialgroup_id"]) &&
  1193.                isset($product["custom_article_material_id"]) && !empty($product["custom_article_material_id"])){
  1194.                 $farbid=$this->getColorID($product["custom_article_materialgroup_id"],$product["custom_article_material_id"]);
  1195.             }
  1196.             $ProductFlat = new \App\Controller\shopware\ProductFlat();
  1197.             $ProductFlat->setProperties($attribute);
  1198.             $ProductFlat->setCustomFields(array(
  1199.                 "custom_article_attributes_added" => "1"
  1200.             ));
  1201.             try {
  1202.                 $post_product $this->client->request('PATCH''/api/product/'.$product["product_id"], array(
  1203.                     // 'debug' => true,
  1204.                     'allow_redirects'=>array('strict'=>true),
  1205.                     "headers" => $this->header,
  1206.                     "body" => $ProductFlat
  1207.                 ));
  1208.             } catch (RequestException $e) {
  1209.                 // echo Psr7\str($e->getRequest());
  1210.                 // if ($e->hasResponse()) {
  1211.                 //     echo Psr7\str($e->getResponse());
  1212.                 // }
  1213.                 if ($e->hasResponse()) {
  1214.                     $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  1215.                 }else{
  1216.                     $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  1217.                 }
  1218.                 $response->headers->set('Content-Type''application/json');
  1219.                 return $response;
  1220.                 exit;
  1221.             }
  1222.             // return $this->render('update/index.html.twig', ["return" => ['dearch' => $product ]]);
  1223.         }
  1224.         if(count($products_sw) < 11){
  1225.             return $this->redirect('/add/attributes');
  1226.         }else{
  1227.             return $this->render('update/index.html.twig', ["return" => ['Attribute' => "done" ]]);
  1228.         }
  1229.     }
  1230.     
  1231.     /**
  1232.      * Importiert Attribute in Shopware
  1233.      * 
  1234.      * @Route("/import/attributes", name="import_attributes")
  1235.      * 
  1236.      * @return mixed
  1237.      */
  1238.     public function import_attributes()
  1239.     {
  1240.         $this->client_connect();
  1241.         $conn_shopware $this->shop_connect();
  1242.         $conn_akn $this->dump_connect();
  1243.         //gehe alle attribute durch und erstelle eigenschaften in entsprechenden gruppen
  1244.         // wahlweise: aufruf wenn eigenschaften produkt zugeordnet werden sollen. nimmt eigenschaften entgegen,
  1245.         // gleicht mit vorhandenen ab, erstellt neue und löscht überschüssige
  1246.         // Oberflächen
  1247.         $sql "SELECT 
  1248.                 DISTINCT surface_name
  1249.                 FROM surfaces
  1250.             ;";
  1251.         $stmt $conn_akn->prepare($sql);
  1252.         $stmt->execute();
  1253.         $surfaces $stmt->fetchAll();
  1254.         $surfaces array_map('current'$surfaces);
  1255.         // $surface_id="0fcb8899440540a5aef143da8f82d096";
  1256.         foreach($surfaces as $surface){
  1257.             $sql "SELECT 
  1258.                     *
  1259.                     , HEX(id) as id
  1260.                     , HEX(language_id) as language_id
  1261.                     , HEX(property_group_id) as property_group_id
  1262.                 FROM property_group_option
  1263.                 INNER JOIN property_group_option_translation  ON (property_group_option.id = property_group_option_translation.property_group_option_id)
  1264.                 WHERE LOWER(HEX(property_group_id)) = '".$this->surface_id."'
  1265.                 AND `name` = '".$surface."'
  1266.                 ;";
  1267.             $stmt $conn_shopware->prepare($sql);
  1268.             $stmt->execute();
  1269.             $search_surfaces $stmt->fetchAll();
  1270.             // return $this->render('update/index.html.twig', ["return" => ['surfaces' => $surfaces, 'search_surfaces' => $search_surfaces]]);
  1271.             
  1272.             if(isset($search_surfaces) && is_array($search_surfaces) && count($search_surfaces) > 0) {
  1273.             }else{
  1274.                 if(isset($surface) && !empty($surface)){
  1275.                 // return $this->render('update/index.html.twig', ["return" => ['surfaces' => $surface, 'search_surfaces' => $search_surfaces]]);
  1276.                     $post_product $this->client->request('POST''/api/property-group-option', array(
  1277.                         'allow_redirects'=>array('strict'=>true),
  1278.                         'headers' => $this->header,
  1279.                         'body' => json_encode([
  1280.                             'groupId' => $this->surface_id,
  1281.                             'name' => $surface
  1282.                         ])
  1283.                     ));
  1284.                 }
  1285.             }
  1286.         }
  1287.         return $this->render('update/index.html.twig', ["return" => ['surfaces' => "surfaces done" ]]);
  1288.         // Materialgruppen
  1289.         $sql "SELECT 
  1290.                 DISTINCT materialgroup_name
  1291.                 FROM materialgroups
  1292.             ;";
  1293.             // OFFSET ".(($page - 1) * $step_count)."
  1294.         $stmt $conn_akn->prepare($sql);
  1295.         $stmt->execute();
  1296.         $materialgroups $stmt->fetchAll();
  1297.         $materialgroups array_map('current'$materialgroups);
  1298.         // $materialgroup_id="4d73f7a67c184ca294e778772b860050";
  1299.         foreach($materialgroups as $materialgroup){
  1300.             $sql "SELECT 
  1301.                     *
  1302.                     , HEX(id) as id
  1303.                     , HEX(language_id) as language_id
  1304.                     , HEX(property_group_id) as property_group_id
  1305.                 FROM property_group_option
  1306.                 INNER JOIN property_group_option_translation  ON (property_group_option.id = property_group_option_translation.property_group_option_id)
  1307.                 WHERE LOWER(HEX(property_group_id)) = '".$this->materialgroup_id."'
  1308.                 AND `name` = '".$materialgroup."'
  1309.                 ;";
  1310.             $stmt $conn_shopware->prepare($sql);
  1311.             $stmt->execute();
  1312.             $search_materialgroups $stmt->fetchAll();
  1313.             // return $this->render('update/index.html.twig', ["return" => ['materialgroups' => $materialgroups, 'search_materialgroups' => $search_materialgroups]]);
  1314.             if(isset($search_materialgroups) && is_array($search_materialgroups) && count($search_materialgroups) > 0) {
  1315.             }else{
  1316.                 if(isset($materialgroup) && !empty($materialgroup)){
  1317.                     $post_product $this->client->request('POST''/api/property-group-option', array(
  1318.                         'allow_redirects'=>array('strict'=>true),
  1319.                         'headers' => $this->header,
  1320.                         'body' => json_encode([
  1321.                             'groupId' => $this->materialgroup_id,
  1322.                             'name' => $materialgroup
  1323.                         ])
  1324.                     ));
  1325.                 }
  1326.             }
  1327.         }
  1328.         return $this->render('update/index.html.twig', ["return" => ['materialgroups' => "materialgroups done" ]]);
  1329.         // Material
  1330.         $sql "SELECT 
  1331.                 DISTINCT material_name
  1332.                 FROM materials
  1333.             ;";
  1334.             // OFFSET ".(($page - 1) * $step_count)."
  1335.         $stmt $conn_akn->prepare($sql);
  1336.         $stmt->execute();
  1337.         $materials $stmt->fetchAll();
  1338.         $materials array_map('current'$materials);
  1339.         // $material_id="ed4fe3bbe67941e4b4ed47c3308e32f2";
  1340.         foreach($materials as $material){
  1341.             $sql "SELECT 
  1342.                     *
  1343.                     , HEX(id) as id
  1344.                     , HEX(language_id) as language_id
  1345.                     , HEX(property_group_id) as property_group_id
  1346.                 FROM property_group_option
  1347.                 INNER JOIN property_group_option_translation  ON (property_group_option.id = property_group_option_translation.property_group_option_id)
  1348.                 WHERE LOWER(HEX(property_group_id)) = '".$this->material_id."'
  1349.                 AND `name` = '".$material."'
  1350.                 ;";
  1351.             $stmt $conn_shopware->prepare($sql);
  1352.             $stmt->execute();
  1353.             $search_materials $stmt->fetchAll();
  1354.             // return $this->render('update/index.html.twig', ["return" => ['materials' => $materials, 'search_materials' => $search_materials]]);
  1355.             if(isset($search_materials) && is_array($search_materials) && count($search_materials) > 0) {
  1356.             }else{
  1357.                 if(isset($material) && !empty($material)){
  1358.                     $post_product $this->client->request('POST''/api/property-group-option', array(
  1359.                         'allow_redirects'=>array('strict'=>true),
  1360.                         'headers' => $this->header,
  1361.                         'body' => json_encode([
  1362.                             'groupId' => $this->material_id,
  1363.                             'name' => $material
  1364.                             ])
  1365.                         ));
  1366.                 }
  1367.             }
  1368.         }
  1369.         return $this->render('update/index.html.twig', ["return" => ['materials' => "materials done" ]]);
  1370.         // Artikelgruppe
  1371.         $sql "SELECT 
  1372.                 DISTINCT articlegroup_name
  1373.                 FROM articlegroups
  1374.             ;";
  1375.         $stmt $conn_akn->prepare($sql);
  1376.         $stmt->execute();
  1377.         $articlegroups $stmt->fetchAll();
  1378.         $articlegroups array_map('current'$articlegroups);
  1379.         // $articlegroup_id="025caac2d61d49968dc84c2e291adef6";
  1380.         foreach($articlegroups as $articlegroup){
  1381.             $sql "SELECT 
  1382.                     *
  1383.                     , HEX(id) as id
  1384.                     , HEX(language_id) as language_id
  1385.                     , HEX(property_group_id) as property_group_id
  1386.                 FROM property_group_option
  1387.                 INNER JOIN property_group_option_translation  ON (property_group_option.id = property_group_option_translation.property_group_option_id)
  1388.                 WHERE LOWER(HEX(property_group_id)) = '".$this->articlegroup_id."'
  1389.                 AND `name` = '".$articlegroup."'
  1390.                 ;";
  1391.             $stmt $conn_shopware->prepare($sql);
  1392.             $stmt->execute();
  1393.             $search_articlegroups $stmt->fetchAll();
  1394.             // return $this->render('update/index.html.twig', ["return" => ['articlegroups' => $articlegroups, 'search_articlegroups' => $search_articlegroups]]);
  1395.             if(isset($search_articlegroups) && is_array($search_articlegroups) && count($search_articlegroups) > 0) {
  1396.             }else{
  1397.                 $post_product $this->client->request('POST''/api/property-group-option', array(
  1398.                     'allow_redirects'=>array('strict'=>true),
  1399.                     'headers' => $this->header,
  1400.                     'body' => json_encode([
  1401.                         'groupId' => $this->articlegroup_id,
  1402.                         'name' => $articlegroup
  1403.                     ])
  1404.                 ));
  1405.             }
  1406.         }
  1407.         // return $this->render('update/index.html.twig', ["return" => ['articlegroups' => "articlegroups done" ]]);
  1408.         return $this->render('update/index.html.twig', ["return" => ['surfaces' => $surfaces'materialgroups' => $materialgroups'materials' => $materials'articlegroups' => $articlegroups]]);
  1409.     }
  1410.     /**
  1411.      * Erwartet Storage-Array, returned formatierten Array der Eigenschaften für Variantenimport
  1412.      */
  1413.     public function get_variant_ids($storage)
  1414.     {
  1415.         $this->client_connect();
  1416.         $conn_shopware $this->shop_connect();
  1417.         $conn_akn $this->dump_connect();
  1418.         $eigenschaften = array();
  1419.         // Materialname
  1420.         if(isset($storage["material_name"]) && !empty($storage["material_name"])){
  1421.             $eigenschaften[$this->material_id]=$storage["material_name"];
  1422.         }
  1423.         // Materialgruppenname
  1424.         if(isset($storage["materialgroup_name"]) && !empty($storage["materialgroup_name"])){
  1425.             $eigenschaften[$this->materialgroup_id]=$storage["materialgroup_name"];
  1426.         }
  1427.         // Steinnummer
  1428.         if(isset($storage["stone_number"]) && !empty($storage["stone_number"])){
  1429.             $eigenschaften[$this->stonenumber_id]=$storage["stone_number"];
  1430.         }
  1431.         // Dicke
  1432.         if(isset($storage["thickness"]) && !empty($storage["thickness"])){
  1433.             $eigenschaften[$this->thickness_id]=$storage["thickness"];
  1434.         }
  1435.         // Artikeldaten Innen/Außen
  1436.         if(isset($storage["material_id"]) && !empty($storage["material_id"]) && isset($storage["materialgroup_id"]) && !empty($storage["materialgroup_id"])){
  1437.             $sql "SELECT *
  1438.                     FROM artikeldaten
  1439.                     WHERE material_group = '".$storage["materialgroup_id"]."'
  1440.                     AND material = '".$storage["material_id"]."'
  1441.                     LIMIT 1
  1442.                 ;";
  1443.             $stmt $conn_akn->prepare($sql);
  1444.             $stmt->execute();
  1445.             $artikeldaten $stmt->fetchAll();
  1446.             // dd($storage["material_id"],$storage["materialgroup_id"],$artikeldaten);
  1447.                 if(isset($artikeldaten) && is_array($artikeldaten) && count($artikeldaten) > 0){
  1448.                     $eigenschaften[$this->artikeldaten_innen_id] = array();
  1449.                     $eigenschaften[$this->artikeldaten_aussen_id] = array();
  1450.                     $artikeldaten[0] = array_values($artikeldaten[0]);
  1451.                     $i 0;
  1452.                     foreach($artikeldaten[0] as $key => $artikeldaten_select){
  1453.                         if($key == || $key == || $key == || $key == 13){
  1454.                             continue;
  1455.                         }
  1456.                         if($key 8){
  1457.                             if($artikeldaten_select == 1){
  1458.                                 array_push($eigenschaften[$this->artikeldaten_innen_id], $this->artikeldaten_ids[$i]);
  1459.                             }elseif($artikeldaten_select == 0){
  1460.                                 array_push($eigenschaften[$this->artikeldaten_innen_id], $this->artikeldaten_ids[$i+1]);
  1461.                             }
  1462.                         }else{
  1463.                             if($artikeldaten_select == 1){
  1464.                                 array_push($eigenschaften[$this->artikeldaten_aussen_id], $this->artikeldaten_ids[$i]);
  1465.                             }elseif($artikeldaten_select == 0){
  1466.                                 array_push($eigenschaften[$this->artikeldaten_aussen_id], $this->artikeldaten_ids[$i+1]);
  1467.                             }
  1468.                         }
  1469.                         $i $i+2;
  1470.                     }
  1471.                 // dd($artikeldaten[0], $eigenschaften[$this->artikeldaten_innen_id], $eigenschaften[$this->artikeldaten_aussen_id]);
  1472.             }
  1473.         }
  1474.         // Länge
  1475.         if(isset($storage["length"]) && !empty($storage["length"])){
  1476.             $eigenschaften[$this->length_id]=$storage["length"];
  1477.         }
  1478.         // Höhe
  1479.         if(isset($storage["width"]) && !empty($storage["width"])){
  1480.             $eigenschaften[$this->width_id]=$storage["width"];
  1481.         }
  1482.         // Anzahl
  1483.         if(isset($storage["amount_1"]) && !empty($storage["amount_1"])){
  1484.             $eigenschaften[$this->amount_id]=strval(intval($storage["amount_1"]));
  1485.         }
  1486.         // Quadratmeterpreis
  1487.         if(isset($storage["price"]) && !empty($storage["price"])){
  1488.             $eigenschaften[$this->squaremeterprice_id]=$storage["price"];
  1489.         }
  1490.         // Quadratmeter oder Meter oder Sa
  1491.         if(isset($storage["einheit_2"]) && !empty($storage["einheit_2"]) && isset($storage["amount_2"]) && !empty($storage["amount_2"])){
  1492.            // wenn einheit 2 gesetzt ist prüfe welchen inhalt und ordne entsprechend die id zu 1/2/14
  1493.            if(trim($storage["einheit_2"]) == "m²"){
  1494.                 // Quadratmeter
  1495.                 $eigenschaften[$this->surfacearea_id]=strval(round($storage["amount_2"],2));
  1496.             }elseif(trim($storage["einheit_2"]) == "m"){
  1497.                 // Meter 5ce33389c17e4f298ef14a3d1f0230f7
  1498.                 $eigenschaften[$this->meter_id]=strval(round($storage["amount_2"],2));
  1499.             }elseif(trim($storage["einheit_2"]) == "Sa"){
  1500.                 // Sa  b7b4812b66e7482e83e0d12b016710cd
  1501.                 $eigenschaften[$this->sa_id]=strval(round($storage["amount_2"],2));
  1502.            }
  1503.         }
  1504.         // if(isset($storage["amount_2"]) && !empty($storage["amount_2"])){
  1505.         //     $eigenschaften[$this->surfacearea_id]=strval(round($storage["amount_2"],2));
  1506.         // }
  1507.         // Oberfläche
  1508.         if(isset($storage["surface_name"]) && !empty($storage["surface_name"])){
  1509.             $eigenschaften[$this->surface_id]=$storage["surface_name"];
  1510.         }
  1511.         // Artikelgruppe
  1512.         if(isset($storage["articlegroup_name"]) && !empty($storage["articlegroup_name"])){
  1513.             $eigenschaften[$this->articlegroup_id]=$storage["articlegroup_name"];
  1514.         }
  1515.         
  1516.         // Fehler j/n
  1517.         $fehler "Nein";
  1518.         if( isset($storage["fault_name"]) && strlen($storage["fault_name"]) > && !empty($storage["fault_name"]) && isset($storage["fault_corner"]) &&  strlen($storage["fault_corner"]) > && !empty($storage["fault_corner"])){
  1519.             $eigenschaften[$this->fault_exists_id]=$storage["fault_name"].": ".$storage["fault_corner"];
  1520.         }else{
  1521.             $eigenschaften[$this->fault_exists_id]="Nein";
  1522.         }
  1523.         // Blocknummer
  1524.         if(isset($storage["block_advice"]) && !empty($storage["block_advice"])){
  1525.             $blocknummer $storage["block_advice"];
  1526.             //Zerlegt alle nummern in mat+oberflächenzahl+blocknummer und kürzt zeichen nach leerzeichen automatisch raus
  1527.             $blocknummer_zerlegt=array();
  1528.             preg_match('/([a-zA-Z]+)([0-9,]+)([a-zA-Z0-9,]+)/'$storage["block_advice"], $blocknummer_zerlegt);
  1529.             if(strlen(end($blocknummer_zerlegt))>1){
  1530.                 $blocknummer end($blocknummer_zerlegt);
  1531.                 $eigenschaften[$this->blocknumber_id]=$blocknummer;
  1532.             }
  1533.         }
  1534.         if(isset($storage["materialgroup_id"]) && !empty($storage["materialgroup_id"]) &&
  1535.         isset($storage["material_id"]) && !empty($storage["material_id"])){
  1536.             $sql "SELECT *
  1537.                     FROM materialcolors
  1538.                     INNER JOIN `keys` ON (materialcolors.color_id = key_number)
  1539.                     WHERE color_id != -1
  1540.                     AND key_kind = 88
  1541.                     AND mgr_id = '".$storage["materialgroup_id"]."'
  1542.                     AND mar_id = '".$storage["material_id"]."'
  1543.                 ;";
  1544.             $stmt $conn_akn->prepare($sql);
  1545.             $stmt->execute();
  1546.             $colors $stmt->fetchAll();
  1547.             
  1548.             // if(isset($colors) && is_array($colors) && count($colors) > 0){
  1549.                 //     $color = explode('#',$colors[0]["key_text"]);
  1550.                 //     $eigenschaften[$this->color_id] = trim($color[0]);
  1551.                 // }
  1552.                 if(isset($colors) && is_array($colors) && count($colors) > 0){
  1553.                     $eigenschaften[$this->color_id] = array();
  1554.                     foreach($colors as $colors_select){
  1555.                         unset($color);
  1556.                         $color explode('#',$colors_select["key_text"]);
  1557.                         array_push($eigenschaften[$this->color_id], trim($color[0]));
  1558.                     }
  1559.                 // dd($color);
  1560.                 // dd($eigenschaften[$this->color_id]);
  1561.             }
  1562.         }
  1563.         $attribute=array();
  1564.         foreach($eigenschaften as $key => $eigenschaft){
  1565.             if($key==$this->color_id){
  1566.                 // wenn key color ist dann geh kompletten array damit durch
  1567.                 foreach($eigenschaft as $farbeigenschaft){
  1568.                     $sql "SELECT 
  1569.                             *
  1570.                             , LOWER(HEX(id)) as id
  1571.                             , HEX(language_id) as language_id
  1572.                             , HEX(property_group_id) as property_group_id
  1573.                         FROM property_group_option
  1574.                         INNER JOIN property_group_option_translation  ON (property_group_option.id = property_group_option_translation.property_group_option_id)
  1575.                         -- WHERE LOWER(HEX(property_group_id)) = 'eadafb18e0c34aeb894dafddf1e107a0'
  1576.                         WHERE `name` = '".$farbeigenschaft."'
  1577.                         AND LOWER(HEX(property_group_id)) = '".$key."'
  1578.                         ;";
  1579.                     $stmt $conn_shopware->prepare($sql);
  1580.                     $stmt->execute();
  1581.                     $search_property $stmt->fetchAll();
  1582.                     // var_dump($search_property);exit;
  1583.                     // return $this->render('update/index.html.twig', ["return" => ['dearch' => $search_property ]]);
  1584.                     
  1585.                     // array("optionId" => "d6c37799803148fe977e42d2fe7cc875",),
  1586.                     // array("optionId" => "340073aa01544985ab0d1f056c1f8422",)
  1587.                     
  1588.                     if(isset($search_property) && is_array($search_property) && count($search_property) > 0) {
  1589.                         array_push($attribute,array('optionId'=>$search_property[0]["id"]));
  1590.                     }else{
  1591.                         // Attribut nicht in shopware und muss hinzugefügt werden
  1592.                         // var_dump($eigenschaften);
  1593.                         // var_dump($eigenschaft." post ".$key);
  1594.                         $post_product $this->client->request('POST''/api/property-group-option', array(
  1595.                             'allow_redirects'=>array('strict'=>true),
  1596.                             'headers' => $this->header,
  1597.                             'body' => json_encode([
  1598.                                 'groupId' => $key,
  1599.                                 'name' => $farbeigenschaft
  1600.                             ])
  1601.                         ));
  1602.                         $optionId $post_product->getHeaders(); 
  1603.                         // var_dump($optionId);exit;
  1604.                         $optionId =  str_replace($this->base_url.'api/property-group-option/','',$optionId["Location"]); // ID des erstellten produktes
  1605.                         array_push($attribute,array('optionId'=>$optionId[0]));
  1606.                     }
  1607.                 }
  1608.             }elseif($key==$this->artikeldaten_innen_id||$key==$this->artikeldaten_aussen_id){
  1609.                 foreach($eigenschaft as $artikeldateneigenschaft){
  1610.                     $sql "SELECT 
  1611.                             *
  1612.                             , LOWER(HEX(id)) as id
  1613.                             , HEX(language_id) as language_id
  1614.                             , HEX(property_group_id) as property_group_id
  1615.                         FROM property_group_option
  1616.                         INNER JOIN property_group_option_translation  ON (property_group_option.id = property_group_option_translation.property_group_option_id)
  1617.                         -- WHERE LOWER(HEX(property_group_id)) = 'eadafb18e0c34aeb894dafddf1e107a0'
  1618.                         WHERE LOWER(HEX(id)) = '".$artikeldateneigenschaft."'
  1619.                         AND LOWER(HEX(property_group_id)) = '".$key."'
  1620.                         ;";
  1621.                     $stmt $conn_shopware->prepare($sql);
  1622.                     $stmt->execute();
  1623.                     $search_property $stmt->fetchAll();
  1624.                     // var_dump($search_property);exit;
  1625.                     if(isset($search_property) && is_array($search_property) && count($search_property) > 0) {
  1626.                         array_push($attribute,array('optionId'=>$search_property[0]["id"]));
  1627.                     }
  1628.                 }
  1629.             }else{
  1630.                 $sql "SELECT 
  1631.                         *
  1632.                         , LOWER(HEX(id)) as id
  1633.                         , HEX(language_id) as language_id
  1634.                         , HEX(property_group_id) as property_group_id
  1635.                     FROM property_group_option
  1636.                     INNER JOIN property_group_option_translation  ON (property_group_option.id = property_group_option_translation.property_group_option_id)
  1637.                     -- WHERE LOWER(HEX(property_group_id)) = 'eadafb18e0c34aeb894dafddf1e107a0'
  1638.                     WHERE `name` = '".$eigenschaft."'
  1639.                     AND LOWER(HEX(property_group_id)) = '".$key."'
  1640.                     ;";
  1641.                 $stmt $conn_shopware->prepare($sql);
  1642.                 $stmt->execute();
  1643.                 $search_property $stmt->fetchAll();
  1644.                 // var_dump($search_property);exit;
  1645.                 // return $this->render('update/index.html.twig', ["return" => ['dearch' => $search_property ]]);
  1646.                 
  1647.                 // array("optionId" => "d6c37799803148fe977e42d2fe7cc875",),
  1648.                 // array("optionId" => "340073aa01544985ab0d1f056c1f8422",)
  1649.                 
  1650.                 if(isset($search_property) && is_array($search_property) && count($search_property) > 0) {
  1651.                     array_push($attribute,array('optionId'=>$search_property[0]["id"]));
  1652.                 }else{
  1653.                     // Attribut nicht in shopware und muss hinzugefügt werden
  1654.                     // var_dump($key);
  1655.                     // var_dump($eigenschaft." post ".$key);
  1656.                     try{
  1657.                         $post_product $this->client->request('POST''/api/property-group-option', array(
  1658.                             'allow_redirects'=>array('strict'=>true),
  1659.                             'headers' => $this->header,
  1660.                             'body' => json_encode([
  1661.                                 'groupId' => $key,
  1662.                                 'name' => $eigenschaft
  1663.                             ])
  1664.                         ));
  1665.                         // $optionId = $post_product->getHeaders(); 
  1666.                         // var_dump( $post_product->getHeaders());
  1667.                         // $location = basename(array_pop( $post_product->getHeaders()['Location'] ));
  1668.                         // dd($location, $key, $eigenschaft);exit;
  1669.                         $sql "SELECT 
  1670.                                 *
  1671.                                 , LOWER(HEX(property_group_option_id)) as property_group_option_id
  1672.                             FROM  property_group_option_translation
  1673.                             WHERE `name` = '".$eigenschaft."'
  1674.                             ;";
  1675.                         $stmt $conn_shopware->prepare($sql);
  1676.                         $stmt->execute();
  1677.                         $search_property $stmt->fetchAll();
  1678.                         if(isset($search_property) && is_array($search_property) && count($search_property)>0){
  1679.                             array_push($attribute,array('optionId'=>$search_property[0]['property_group_option_id']));
  1680.                         }
  1681.                         // $optionId =  str_replace($this->base_url.'api/property-group-option/','',$optionId["Location"]); // ID des erstellten produktes
  1682.                         // array_push($attribute,array('optionId'=>$location));
  1683.                         // array_push($attribute,array('optionId'=>$optionId[0]));
  1684.                     } catch (RequestException $e) {
  1685.                         // echo Psr7\str($e->getRequest());
  1686.                         // if ($e->hasResponse()) {
  1687.                         //     echo Psr7\str($e->getResponse());
  1688.                         // }
  1689.                         // exit;
  1690.                     }
  1691.                 }
  1692.             }
  1693.         }
  1694.         // var_dump($attribute);exit;
  1695.         return $attribute;
  1696.     }
  1697.      /**
  1698.      * Prüft Sichtbarkeit generierter Kategorien und blendet diese entsprechend der Produkte ein/aus
  1699.      * 
  1700.      * @Route("/categories/visibility", name="categories_visibility")
  1701.      * 
  1702.      * @return mixed
  1703.      */
  1704.     public function categories_visibility()
  1705.     {
  1706.         $this->client_connect();
  1707.         $conn_shopware $this->shop_connect();
  1708.         $sql "SELECT 
  1709.                 *
  1710.                 , LOWER(HEX(category_id)) as category_id
  1711.             FROM category_translation
  1712.             INNER JOIN category ON (category.id = category_id)
  1713.             WHERE path LIKE '%".$this->default_category_id."%'
  1714.             AND LOWER(HEX(language_id)) = '".$this->language_id."'
  1715.         ;";
  1716.         $stmt $conn_shopware->prepare($sql);
  1717.         $stmt->execute();
  1718.         $categories $stmt->fetchAll();
  1719.         // return $this->render('update/index.html.twig', ["return" => [ 'sort' => $categories]]);
  1720.         foreach($categories as $category){
  1721.             $sql "SELECT 
  1722.                     count(id) as count
  1723.                 FROM product
  1724.                 WHERE category_tree LIKE '%".$category['category_id']."%'
  1725.                 ;";
  1726.             $stmt $conn_shopware->prepare($sql);
  1727.             $stmt->execute();
  1728.             $product $stmt->fetchAll();
  1729.             // return $this->render('update/index.html.twig', ["return" => [ 'sort' => $product]]);
  1730.             unset($CategoryFlat);
  1731.             if($product[0]["count"]>0){ 
  1732.                 // Produkt in Kategorie vorhanden
  1733.                 if($category["active"] != 1){
  1734.                     // setze aktiv
  1735.                     $CategoryFlat = new \App\Controller\shopware\CategoryFlat();
  1736.                     $CategoryFlat->setActive(true);
  1737.                 }    
  1738.             }else{
  1739.                 // kein Produkt in Kategorie vorhanden
  1740.                 // verhindere inaktiv setzen der default Kategorie
  1741.                 if($category["active"] != &&  $category['category_id'] != $this->default_category_id){
  1742.                     // setze inaktiv
  1743.                     $CategoryFlat = new \App\Controller\shopware\CategoryFlat();
  1744.                     $CategoryFlat->setActive(false);
  1745.                 }
  1746.             }
  1747.             // return $this->render('update/index.html.twig', ["return" => [ 'Kategorien' => $CategoryFlat]]);
  1748.             if(isset($CategoryFlat)){
  1749.                 try {
  1750.                     $post_category $this->client->request('PATCH''/api/category/'.$category['category_id'].'', array(
  1751.                         'allow_redirects'=>array('strict'=>true),
  1752.                         "headers" => $this->header,
  1753.                         "body" => $CategoryFlat
  1754.                     ));
  1755.                 } catch (RequestException $e) {
  1756.                     // echo Psr7\str($e->getRequest());
  1757.                     // if ($e->hasResponse()) {
  1758.                     //     echo Psr7\str($e->getResponse());
  1759.                     // }
  1760.                     if ($e->hasResponse()) {
  1761.                         $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  1762.                     }else{
  1763.                         $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  1764.                     }
  1765.                     $response->headers->set('Content-Type''application/json');
  1766.                     return $response;
  1767.                     exit;
  1768.                 }
  1769.             }
  1770.         }
  1771.         return $this->render('update/index.html.twig', ["return" => [ 'Kategorien' => "Kategorien Aktualisiert"]]);
  1772.     }
  1773.     /**
  1774.      * Kopiert die Bilddateien von AKN in den produktbilder Ordner zur weiteren Verarbeitung
  1775.      * 
  1776.      * @Route("/copy/image", name="copy_image")
  1777.      * 
  1778.      * @return mixed
  1779.      */
  1780.     public function copy_image(){
  1781.         // exit;
  1782.         $conn_shopware $this->shop_connect();
  1783.         // $location = "/var/www/vhosts/pixelproductions.de/sw6.pixelproductions.de/akn_upload/Bilder"; // DEV
  1784.         // $location = "/home/aknnadbt/shop.akn-natursteine.de/UPLOAD/Bilder"; // LIVE
  1785.         // $location = "/var/www/vhosts/akn-natursteine.de/shop.akn-natursteine.de/UPLOAD/Bilder"; // LIVE NACH UMZUG
  1786.         $location "/var/www/vhosts/swconnect.akn-natursteine.de/httpdocs/public/UPLOAD/Bilder"// LIVE NACH IONOS UMZUG
  1787.         $scan scandir($location);
  1788.         // return $this->render('update/index.html.twig', ["return" => [ 'zeit' => $scan]]);
  1789.         $target_location $this->getParameter('kernel.project_dir')."/public/produktbilder";
  1790.         $target scandir($target_location);
  1791.         $scan array_diff($scan$target);
  1792.         // Wenn produktbilder leer und akn_upload nicht leer -> kopiere Bilder in produktbilder
  1793.         if(count($target)<&& count($scan)>2){  
  1794.             // dd($target,$scan);
  1795.             // TODO timestamp 1d für bilder
  1796.             
  1797.             $sql "SELECT MAX(UpdateDatetime) as lastdate FROM pixel_dates
  1798.                     WHERE FunctionName = 'import/images'
  1799.                     AND StartEnde = 'ENDE'";
  1800.             $stmt $conn_shopware->prepare($sql);
  1801.             $stmt->execute();
  1802.             $date $stmt->fetchAll();
  1803.             // dd($date[0]["lastdate"],  date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s'). '-0 hours')));
  1804.             if ($date[0]["lastdate"]< date('Y-m-d H:i:s'strtotime(date('Y-m-d H:i:s'). '-5 hours'))){
  1805.                 foreach($scan as $scan_file){
  1806.                     copy($location."/".$scan_file,$target_location."/".$scan_file);
  1807.                 }
  1808.             }else{
  1809.                 // finished
  1810.                 $response = new Response(json_encode(array('success' => true'last Date' => $date[0]["lastdate"])));
  1811.                 $response->headers->set('Content-Type''application/json');
  1812.                 return $response;
  1813.             }
  1814.             
  1815.             // dd("hey"); 
  1816.         }
  1817.         // dd($target,$scan);
  1818.         //#### DATEIKOMPRIMIERUNG
  1819.         $article_files scandir($this->getParameter('kernel.project_dir')."/public/produktbilder");
  1820.         $article_files array_diff($article_files, array('.','..','Thumbs.db'));
  1821.         $valid_ext = array('png','jpeg','jpg');
  1822.         $article_files array_slice($article_files,0,100);
  1823.         if(count($article_files)>0){
  1824.             foreach($article_files as $baseimage){
  1825.                 $location $this->getParameter('kernel.project_dir')."/public/produktbilder/".$baseimage;
  1826.                 $destination $this->getParameter('kernel.project_dir')."/public/komprimiert/".$baseimage;
  1827.                 $file_extension pathinfo($locationPATHINFO_EXTENSION);
  1828.                 $file_extension strtolower($file_extension);
  1829.                 // return $this->render('update/index.html.twig', ["return" => ['location' => $location ]]);
  1830.                 try{
  1831.                 if(in_array($file_extension,$valid_ext) && !file_exists($destination)){
  1832.                     $info = @getimagesize($location);
  1833.                     if($info == false){ //Datei kann nicht gelesen werden oder ist 0 Bytes groß
  1834.                         unlink($location);
  1835.                     }else{
  1836.                         if ($info['mime'] == 'image/jpeg'
  1837.                         $image imagecreatefromjpeg($location);
  1838.                     
  1839.                         elseif ($info['mime'] == 'image/png'
  1840.                         $image imagecreatefrompng($location);
  1841.                     
  1842.                         imagejpeg($image$destination60); // image, path, quality 1-100
  1843.                     }
  1844.                     // unlink($location);
  1845.                 }
  1846.                 // else{
  1847.                 //     echo "Invalid file type.".$location;
  1848.                 // }
  1849.                 unlink($location);
  1850.                 } catch (RequestException $e) {
  1851.                     // echo Psr7\str($e->getRequest());
  1852.                     // if ($e->hasResponse()) {
  1853.                     //     echo Psr7\str($e->getResponse());
  1854.                     // }
  1855.                     if ($e->hasResponse()) {
  1856.                         $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  1857.                     }else{
  1858.                         $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  1859.                     }
  1860.                     $response->headers->set('Content-Type''application/json');
  1861.                     return $response;
  1862.                     exit;
  1863.                 }
  1864.             }
  1865.             return $this->redirect('/copy/image');
  1866.         }
  1867.         //    return $this->render('update/index.html.twig', ["return" => [ 'article' => $article_files]]);
  1868.            return $this->redirect('/import/images');
  1869.         // $response = new Response(json_encode(array('success' => true)));
  1870.         // $response->headers->set('Content-Type', 'application/json');
  1871.         // return $response;
  1872.         // return $this->render('update/index.html.twig', ["return" => [ 'zeit' =>$target, 'scan' => $scan]]);
  1873.         // return $this->render('update/index.html.twig', ["return" => [ 'zeit' => 'done']]);
  1874.     }
  1875.     /**
  1876.      * Kopiert die Bilddateien von AKN in den produktbilder Ordner zur weiteren Verarbeitung
  1877.      * 
  1878.      * @Route("/copy/image2", name="copy_image2")
  1879.      * 
  1880.      * @return mixed
  1881.      */
  1882.     public function copy_image2(){
  1883.         exit;
  1884.         // $location = "/var/www/vhosts/pixelproductions.de/sw6.pixelproductions.de/akn_upload/Bilder"; // DEV
  1885.         // $location = "/home/aknnadbt/shop.akn-natursteine.de/UPLOAD/Bilder"; // LIVE
  1886.         $location "/var/www/vhosts/akn-natursteine.de/shop.akn-natursteine.de/UPLOAD/Bilder"// LIVE NACH UMZUG
  1887.         $scan scandir($location);
  1888.         // return $this->render('update/index.html.twig', ["return" => [ 'zeit' => $scan]]);
  1889.         $target_location $this->getParameter('kernel.project_dir')."/public/produktbilder";
  1890.         $target scandir($target_location);
  1891.         $scan array_diff($scan$target);
  1892.         // Wenn produktbilder leer und akn_upload nicht leer -> kopiere Bilder in produktbilder
  1893.         if(count($target)<&& count($scan)>2){   
  1894.             foreach($scan as $scan_file){
  1895.                 copy($location."/".$scan_file,$target_location."/".$scan_file);
  1896.             }
  1897.         }
  1898.         //#### DATEIKOMPRIMIERUNG
  1899.         $article_files scandir($this->getParameter('kernel.project_dir')."/public/produktbilder");
  1900.         $article_files array_diff($article_files, array('.','..','Thumbs.db'));
  1901.         $valid_ext = array('png','jpeg','jpg');
  1902.         foreach($article_files as $baseimage){
  1903.            $location $this->getParameter('kernel.project_dir')."/public/produktbilder/".$baseimage;
  1904.            $destination $this->getParameter('kernel.project_dir')."/public/komprimiert/".$baseimage;
  1905.            $file_extension pathinfo($locationPATHINFO_EXTENSION);
  1906.            $file_extension strtolower($file_extension);
  1907.            // return $this->render('update/index.html.twig', ["return" => ['location' => $location ]]);
  1908.            if(in_array($file_extension,$valid_ext)){
  1909.                $info getimagesize($location);
  1910.                if ($info['mime'] == 'image/jpeg'
  1911.                $image imagecreatefromjpeg($location);
  1912.            
  1913.                elseif ($info['mime'] == 'image/png'
  1914.                $image imagecreatefrompng($location);
  1915.            
  1916.                imagejpeg($image$destination60); // image, path, quality 1-100
  1917.                unlink($this->getParameter('kernel.project_dir')."/public/produktbilder/".$baseimage);
  1918.            }else{
  1919.                echo "Invalid file type.";
  1920.            }
  1921.         }
  1922.            // return $this->render('update/index.html.twig', ["return" => [ 'article' => $article_files]]);
  1923.         // return $this->render('update/index.html.twig', ["return" => [ 'zeit' =>$target, 'scan' => $scan]]);
  1924.         return $this->render('update/index.html.twig', ["return" => [ 'zeit' => 'done']]);
  1925.     }
  1926.     
  1927.     /**
  1928.      * Importiert und updated Produkte und ihre Varianten
  1929.      * 
  1930.      * @Route("/add/multiple_pictures", name="add_multiple_pictures")
  1931.      * 
  1932.      * @return mixed
  1933.      */
  1934.     public function add_multiple_pictures()
  1935.     {
  1936.         // $this->client_connect();
  1937.         $conn_shopware $this->shop_connect();
  1938.         $conn_akn $this->dump_connect();
  1939.         $step_count 3;
  1940.         $sql "INSERT INTO pixel_dates (FunctionName,StartEnde,UpdateDatetime)
  1941.                 VALUES(?,?,?)";
  1942.         $stmtt $conn_shopware->prepare($sql);
  1943.         $stmtt->execute(array( 
  1944.             "add/multiple_pictures",
  1945.             "Start",
  1946.             date('Y-m-d H:i:s')
  1947.         ));
  1948.         $sql "SELECT *
  1949.             FROM storpictures
  1950.             WHERE upload_done IS NULL
  1951.             AND upload_failed IS NULL
  1952.             -- LIMIT ".$step_count."
  1953.             ;";
  1954.         $stmt $conn_akn->prepare($sql);
  1955.         $stmt->execute();
  1956.         $pictures $stmt->fetchAll();
  1957.         // return $this->render('update/index.html.twig', ["return" => [ 'article' => $pictures]]);
  1958.         foreach($pictures as $key => $picture){
  1959.             $sql "SELECT *
  1960.                 FROM storages
  1961.                 WHERE block_advice = '".$picture["ref"]."' 
  1962.                 OR stone_number = '".$picture["ref"]."'
  1963.                 AND upload_done = 1
  1964.                 AND upload_failed IS NULL
  1965.                 LIMIT 1
  1966.                 ;";
  1967.             $stmt $conn_akn->prepare($sql);
  1968.             $stmt->execute();
  1969.             $storages $stmt->fetchAll();
  1970.             // return $this->render('update/index.html.twig', ["return" => [ 'article' => $storages]]);
  1971.             if(!isset($storages) || empty($storages) || count($storages) < 1){
  1972.                 unset($pictures[$key]);
  1973.             }
  1974.         }
  1975.         array_splice($pictures,$step_count);
  1976.         foreach($pictures as $picture){
  1977.             $sql "SELECT *
  1978.                 ,  LOWER(HEX(product_id)) as product_id
  1979.                 ,  REPLACE(JSON_EXTRACT(product_translation.custom_fields,'$.custom_article_block_number'), '\"', '') as custom_article_block_number
  1980.                 ,  REPLACE(JSON_EXTRACT(product_translation.custom_fields,'$.custom_article_stone_number'), '\"', '') as custom_article_stone_number
  1981.                     --  LOWER(HEX(id)) as id
  1982.                 FROM product_translation
  1983.                 WHERE REPLACE(JSON_EXTRACT(product_translation.custom_fields,'$.custom_article_block_number'), '\"', '') = '".$picture["ref"]."' 
  1984.                 OR REPLACE(JSON_EXTRACT(product_translation.custom_fields,'$.custom_article_stone_number'), '\"', '') = '".$picture["ref"]."'
  1985.                 ;";
  1986.             $stmt $conn_shopware->prepare($sql);
  1987.             $stmt->execute();
  1988.             $search_product $stmt->fetchAll();
  1989.             if(isset($search_product) && !empty($search_product) && count($search_product) > ){
  1990.             // if(isset($search_product) && !empty($search_product) && count($search_product) > 0){
  1991.                 foreach($search_product as $variant){
  1992.                     $this->add_image($picture["picture"], $variant["product_id"], true);
  1993.                 }
  1994.                 $sqlt "UPDATE storpictures SET
  1995.                         upload_done = ?
  1996.                 WHERE id = '".$picture["id"]."';";
  1997.                 $stmtt $conn_akn->prepare($sqlt);
  1998.                 $stmtt->execute(array(1));
  1999.             }else{
  2000.                 $sqlt "UPDATE storpictures SET
  2001.                         upload_done = ?,
  2002.                         upload_failed = ?
  2003.                 WHERE id = '".$picture["id"]."';";
  2004.                 $stmtt $conn_akn->prepare($sqlt);
  2005.                 $stmtt->execute(array(1,1));
  2006.             }
  2007.        }
  2008.         if(count($pictures) >= $step_count){
  2009.             return $this->redirect('/add/multiple_pictures');
  2010.         }
  2011.         $sqlt "UPDATE storpictures SET
  2012.                 upload_done = ?,
  2013.                 upload_failed = ?
  2014.         -- WHERE stone_number = ''
  2015.         WHERE (upload_done IS NULL AND upload_failed IS NULL)
  2016.         ;";
  2017.         $stmtt $conn_akn->prepare($sqlt);
  2018.         $stmtt->execute(array( 1,1));
  2019.         $sql "INSERT INTO pixel_dates (FunctionName,StartEnde,UpdateDatetime)
  2020.                 VALUES(?,?,?)";
  2021.         $stmtt $conn_shopware->prepare($sql);
  2022.         $stmtt->execute(array( 
  2023.             "add/multiple_pictures",
  2024.             "ENDE",
  2025.             date('Y-m-d H:i:s')
  2026.         ));
  2027.         $response = new Response(json_encode(array('success' => true)));
  2028.         $response->headers->set('Content-Type''application/json');
  2029.         return $response;
  2030.         return $this->render('update/index.html.twig', ["return" => [ 'multiple_pictures' => "Extrabilderupload Done"]]);
  2031.     }
  2032.     /**
  2033.      * Generiert Crosselling Liste bei SQL-Import
  2034.      * 
  2035.      * @return mixed
  2036.      */
  2037.     public function createCrosssellingList()
  2038.     {
  2039.         $conn_shopware $this->shop_connect();
  2040.         $conn_akn $this->dump_connect();
  2041.         $sql "TRUNCATE TABLE crossselling";
  2042.         $stmt $conn_akn->prepare($sql);
  2043.         $stmt->execute();
  2044.         
  2045.         $sql "SELECT *
  2046.         , LOWER(HEX(parent_id)) as parent_id
  2047.             FROM product
  2048.             WHERE category_tree  LIKE '%".$this->default_category_id."%'
  2049.             AND parent_id IS NOT NULL
  2050.             AND option_ids IS NOT NULL
  2051.             GROUP BY parent_id
  2052.             ;";
  2053.         $stmt $conn_shopware->prepare($sql);
  2054.         $stmt->execute();
  2055.         $products $stmt->fetchAll();
  2056.         foreach($products as $product){
  2057.             $sql "SELECT LOWER(HEX(id)) as id
  2058.                 FROM property_group_option
  2059.                 WHERE LOWER(HEX(property_group_id)) = '".$this->color_id."'
  2060.                 AND  LOWER(HEX(id)) IN ('".implode("', '",json_decode($product["option_ids"]))."')
  2061.                 ;";
  2062.                 $stmt $conn_shopware->prepare($sql);
  2063.                 $stmt->execute();
  2064.             $colors $stmt->fetchAll();
  2065.             $colors array_map('current'$colors);
  2066.             foreach($colors as $color){
  2067.                 $sql "INSERT INTO crossselling (parent_id, color_id)
  2068.                     VALUES ('".$product["parent_id"]."','".$color."')
  2069.                 ;";
  2070.                 $stmt $conn_akn->prepare($sql);
  2071.                 $stmt->execute();
  2072.             }
  2073.         }
  2074.     }
  2075.     /**
  2076.      * Fügt Crossselling abhängig von den Materialfarben hinzu
  2077.      * 
  2078.      * @Route("/cross/selling", name="cross_selling")
  2079.      * 
  2080.      * @return mixed
  2081.      */
  2082.     public function cross_selling()
  2083.     {
  2084.         $this->client_connect();
  2085.         $conn_shopware $this->shop_connect();
  2086.         $conn_akn $this->dump_connect();
  2087.         $step_count 20;
  2088.         // Für Restart einmal einkommentieren und ausführen (erfolgt automatisch bei jedem SQL_Import)
  2089.         // $this->createCrosssellingList();exit;
  2090.         $sql "SELECT *
  2091.             FROM crossselling
  2092.             WHERE bearbeitet IS NULL
  2093.             LIMIT ".$step_count."
  2094.             ;";
  2095.         $stmt $conn_akn->prepare($sql);
  2096.         $stmt->execute();
  2097.         $sellings $stmt->fetchAll();
  2098.         foreach($sellings as $selling){
  2099.             // return $this->render('update/index.html.twig', ["return" => [ 'article' => $selling ]]);
  2100.             $sql "SELECT *
  2101.             , LOWER(HEX(product_stream_id)) as product_stream_id
  2102.             FROM product_stream_translation
  2103.             WHERE JSON_EXTRACT(product_stream_translation.custom_fields,'$.custom_color_id') = '".$selling["color_id"]."'
  2104.             AND (JSON_EXTRACT(product_stream_translation.custom_fields,'$.custom_usage_id') IS NULL
  2105.                 OR JSON_EXTRACT(product_stream_translation.custom_fields,'$.custom_usage_id') = '' )
  2106.             LIMIT 1
  2107.             ";
  2108.             $stmt $conn_shopware->prepare($sql);
  2109.             $stmt->execute();
  2110.             $product_stream $stmt->fetchAll();
  2111.             // return $this->render('update/index.html.twig', ["return" => [ 'article' => $product_stream ]]);
  2112.             if(isset($product_stream) && !empty($product_stream) && count($product_stream) > 0){
  2113.                 $sql "SELECT
  2114.                         LOWER(HEX(id)) AS id
  2115.                     FROM product_cross_selling
  2116.                     LEFT JOIN product_cross_selling_translation ON (product_cross_selling_translation.product_cross_selling_id = product_cross_selling.id)
  2117.                     WHERE LOWER(HEX(product_id)) = '".$selling["parent_id"]."'
  2118.                     AND name = '".$product_stream[0]["name"]."'
  2119.                     ";
  2120.                 $stmt $conn_shopware->prepare($sql);
  2121.                 $stmt->execute();
  2122.                 $search_stream $stmt->fetchAll();
  2123.             
  2124.                 // return $this->render('update/index.html.twig', ["return" => [ 'farbe' => $product_stream, 'article' => $search_stream ]]);
  2125.                 $body json_encode(array(
  2126.                     "name" => $product_stream[0]["name"],
  2127.                     "sortBy" => "name",
  2128.                     "sortDirection" => "ASC",
  2129.                     "limit" => 24,
  2130.                     "active" => true,
  2131.                     "productId" => $selling["parent_id"],
  2132.                     "type" => "productStream",
  2133.                     "productStreamId" => $product_stream[0]["product_stream_id"]
  2134.                 ));
  2135.                 if(isset($search_stream[0]["id"]) && !empty($search_stream[0]["id"]) && $search_stream[0]["id"] != null){
  2136.                 
  2137.                     try{
  2138.                         $post_country $this->client->request('PATCH''/api/product-cross-selling/'.$search_stream[0]["id"].'', array(
  2139.                             'allow_redirects'=>array('strict'=>true),
  2140.                             "headers" => $this->header,
  2141.                             "body" => $body
  2142.                         ));
  2143.                     } catch (RequestException $e) {
  2144.                         // echo Psr7\str($e->getRequest());
  2145.                         // if ($e->hasResponse()) {
  2146.                         //     echo Psr7\str($e->getResponse());
  2147.                         // }
  2148.                         if ($e->hasResponse()) {
  2149.                             $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  2150.                         }else{
  2151.                             $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  2152.                         }
  2153.                         $response->headers->set('Content-Type''application/json');
  2154.                         return $response;
  2155.                         exit;
  2156.                     }
  2157.                 }else{
  2158.                     try{
  2159.                         $post_country $this->client->request('POST''/api/product-cross-selling', array(
  2160.                             'allow_redirects'=>array('strict'=>true),
  2161.                             "headers" => $this->header,
  2162.                             "body" => $body
  2163.                         ));
  2164.                     } catch (RequestException $e) {
  2165.                         // echo Psr7\str($e->getRequest());
  2166.                         // if ($e->hasResponse()) {
  2167.                         //     echo Psr7\str($e->getResponse());
  2168.                         // }
  2169.                         if ($e->hasResponse()) {
  2170.                             $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  2171.                         }else{
  2172.                             $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  2173.                         }
  2174.                         $response->headers->set('Content-Type''application/json');
  2175.                         return $response;
  2176.                         exit;
  2177.                     }
  2178.                     $location array_pop$post_country->getHeaders()['Location'] );
  2179.                 }
  2180.                 $sqlt "UPDATE crossselling SET
  2181.                         bearbeitet = ?
  2182.                 WHERE id = ".$selling["id"].";";
  2183.                 $stmtt $conn_akn->prepare($sqlt);
  2184.                 $stmtt->execute(array(1));
  2185.             }else{
  2186.                 // dynamische produktegruppe für farbe nicht vorhanden
  2187.                 $sqlt "UPDATE crossselling SET
  2188.                         bearbeitet = ?
  2189.                 WHERE id = ".$selling["id"].";";
  2190.                 $stmtt $conn_akn->prepare($sqlt);
  2191.                 $stmtt->execute(array(2));
  2192.             }
  2193.             // return $this->render('update/index.html.twig', ["return" => [ 'article' => $selling ]]);
  2194.         }
  2195.         if(count($sellings) >= $step_count){
  2196.             return $this->redirect('/cross/selling');
  2197.         }
  2198.         return $this->render('update/index.html.twig', ["return" => [ 'Crosselling' => 'Done' ]]);
  2199.     }
  2200.     /**
  2201.      * Deaktiviert/Löscht nicht mehr vorhandene Artikel
  2202.      * 
  2203.      * @Route("/delete/products", name="delete_products")
  2204.      * 
  2205.      * @return mixed
  2206.      */
  2207.     public function delete_products()
  2208.     {
  2209.         // dd("test");
  2210.         $this->client_connect();
  2211.         $conn_shopware $this->shop_connect();
  2212.         $conn_akn $this->dump_connect();
  2213.         $sql "INSERT INTO pixel_dates (FunctionName,StartEnde,UpdateDatetime)
  2214.                 VALUES(?,?,?)";
  2215.         $stmtt $conn_shopware->prepare($sql);
  2216.         $stmtt->execute(array( 
  2217.             "delete/products",
  2218.             "Start",
  2219.             date('Y-m-d H:i:s')
  2220.         ));
  2221.         $sql "SELECT stone_number
  2222.             FROM storages
  2223.             WHERE stone_number !=''
  2224.             AND stone_number IS NOT NULL
  2225.             ;";
  2226.         $stmt $conn_akn->prepare($sql);
  2227.         $stmt->execute();
  2228.         $storages $stmt->fetchAll();
  2229.         $storages array_map('current'$storages);
  2230.         if(isset($storages) && !empty($storages) && count($storages) > 0){
  2231.             // $sql = "DELETE FROM product 
  2232.             //     WHERE product_number NOT IN ('".implode("', '",$storages)."')
  2233.             //     AND child_count IS NULL
  2234.             // ;";
  2235.             // SELEKTIERE MANUELL ANGELEGTE PRODUKTE UND FÜGE SIE AUSNAHMEN HINZU DAMIT DIESE NICHT MEHR DEAKTIVIERT WERDEN
  2236.             $sql "SELECT
  2237.                     product_number
  2238.                 FROM product_translation
  2239.                 INNER JOIN product ON (product.id = product_id)
  2240.                 WHERE LOWER(HEX(language_id)) = '2fbb5fe2e29a4d70aa5854ce7ce3e20b'
  2241.                             --     and name= 'test'
  2242.                 AND (JSON_EXTRACT(product_translation.custom_fields,'$.custom_article_material_name') = '' 
  2243.                 OR JSON_EXTRACT(product_translation.custom_fields,'$.custom_article_material_name') IS NULL)
  2244.                 ;";
  2245.             $stmt $conn_shopware->prepare($sql);
  2246.             $stmt->execute();
  2247.             $additional_products $stmt->fetchAll();
  2248.             $additional_products array_map('current'$additional_products);
  2249.            
  2250.             $storages array_merge($storages,$additional_products);
  2251.             
  2252.             $sql "UPDATE product 
  2253.                 SET active = 0
  2254.                 WHERE product_number NOT IN ('".implode("', '",$storages)."')
  2255.                 AND child_count IS NULL
  2256.             ;";
  2257.             // $sql = "SELECT product_number
  2258.             //     FROM product
  2259.             //     WHERE product_number NOT IN ('".implode("', '",$storages)."')
  2260.             //     AND child_count IS NULL
  2261.             // ;";
  2262.             $stmt $conn_shopware->prepare($sql);
  2263.             $stmt->execute();
  2264.             $product $stmt->fetchAll();
  2265.             $product array_map('current'$product);
  2266.             // dd($product);
  2267.             $sql "INSERT INTO pixel_dates (FunctionName,StartEnde,UpdateDatetime)
  2268.                     VALUES(?,?,?)";
  2269.             $stmtt $conn_shopware->prepare($sql);
  2270.             $stmtt->execute(array( 
  2271.                 "delete/products",
  2272.                 "ENDE",
  2273.                 date('Y-m-d H:i:s')
  2274.             ));
  2275.         //    dd($product);
  2276.             // return $this->render('update/index.html.twig', ["return" => [ 'storages' => $storages, 'result' => $product]]);
  2277.             return $this->render('update/index.html.twig', ["return" => [ 'result' => "Produkte deaktiviert"]]);
  2278.         }  
  2279.         $sql "INSERT INTO pixel_dates (FunctionName,StartEnde,UpdateDatetime)
  2280.                 VALUES(?,?,?)";
  2281.         $stmtt $conn_shopware->prepare($sql);
  2282.         $stmtt->execute(array( 
  2283.             "delete/products",
  2284.             "ENDE",
  2285.             date('Y-m-d H:i:s')
  2286.         )); 
  2287.         $response = new Response(json_encode(array('success' => true)));
  2288.         $response->headers->set('Content-Type''application/json');
  2289.         return $response;
  2290.         return $this->render('update/index.html.twig', ["return" => [ 'result' => "storages leer"]]);
  2291.     }
  2292.     /**
  2293.      * Importiert und updated Produkte und ihre Varianten
  2294.      * 
  2295.      * @Route("/update/products", name="update_products")
  2296.      * 
  2297.      * @return mixed
  2298.      */
  2299.     public function update_products()
  2300.     {
  2301.         // echo "<pre>"; var_dump(__LINE__); exit;
  2302.         $this->client_connect();
  2303.         $conn_shopware $this->shop_connect();
  2304.         $conn_akn $this->dump_connect();
  2305.         $step_count 5;
  2306.         $productParentId "";
  2307.         $storage_option_list=array();
  2308.         $next_page=false;
  2309.         $sql "INSERT INTO pixel_dates (FunctionName,StartEnde,UpdateDatetime)
  2310.                 VALUES(?,?,?)";
  2311.         $stmtt $conn_shopware->prepare($sql);
  2312.         $stmtt->execute(array( 
  2313.             "update/products",
  2314.             "Start",
  2315.             date('Y-m-d H:i:s')
  2316.         ));
  2317.         
  2318.         // holt alle Artikel ab die eine passende Materialaufschlüsselung haben
  2319.         $sql "SELECT *
  2320.                 , articles.material_id as material_id
  2321.                 , articles.materialgroup_id as materialgroup_id
  2322.                 , storages.id as storage_id
  2323.                 , storages.stone_number AS product_number
  2324.                 -- , CONCAT(storages.material_id, '_', storages.materialgroup_id) AS product_number_parent
  2325.                 , CONCAT(storages.material_id, '_', storages.materialgroup_id, '_') AS product_number_parent
  2326.                 -- , COALESCE(NULLIF(storages.picture, ''), NULLIF(materials.picture, ''), 'default.jpg') AS picture--select picture
  2327.                 , storages.picture AS storages_picture
  2328.                 , materials.picture AS materials_picture
  2329.                 , storages.advice AS advice
  2330.                 , materials.advice AS description_advice
  2331.                 , COALESCE(NULLIF(materials.picture, ''), 'default_art.jpg') AS picture_parent   -- select picture
  2332.                 , key1.key_text AS color_text
  2333.                 , key2.key_text AS einheit_2
  2334.             FROM articles
  2335.             LEFT JOIN materials ON (articles.material_id = materials.material_id AND articles.materialgroup_id = materials.materialgroup_id )
  2336.             -- LEFT JOIN articlegroups ON (articlegroups.articlegroup_id = articles.article_id)
  2337.             LEFT JOIN articlegroups ON (articles.articlegroup_id = articlegroups.articlegroup_id)
  2338.             LEFT JOIN materialgroups ON (materialgroups.materialgroup_id = articles.materialgroup_id)
  2339.             LEFT JOIN surfaces ON (surfaces.surface_id = articles.surface_id AND surfaces.materialgroup_id = articles.materialgroup_id)
  2340.             INNER JOIN storages ON (articles.article_id = storages.article_id)
  2341.             LEFT JOIN storagefaults ON (storages.fault_id = storagefaults.fault_id)
  2342.             LEFT JOIN materialcolors  ON (articles.material_id = materialcolors.mar_id AND articles.materialgroup_id = materialcolors.mgr_id )
  2343.             LEFT JOIN `keys` key1 ON (materialcolors.color_id = key1.key_number AND key1.key_kind = 88)
  2344.             LEFT JOIN `keys` key2 ON (storages.quantity_unit_2 = key2.key_number AND key2.key_kind = 1)
  2345.             LEFT JOIN artikeldaten ON (artikeldaten.material_group = articles.materialgroup_id AND artikeldaten.material = articles.material_id)
  2346.             WHERE storages.stone_number IS NOT NULL
  2347.             -- AND storages.stone_number = '219102526'
  2348.             -- AND articles.material_id = 'BEHA'
  2349.             -- AND materials.advice !=''
  2350.             AND storages.stone_number !=''
  2351.             AND storages.storage != 'ZULAUF'
  2352.             -- AND storages.upload_done IS NULL
  2353.             AND storages.upload_done = 4
  2354.             -- OR storages.upload_done = 4
  2355.             -- AND articles.upload_done IS NULL
  2356.             AND storages.upload_failed IS NULL
  2357.             AND material_name IS NOT NULL
  2358.             -- AND block_advice IS NOT NULL
  2359.             -- AND block_advice != ''
  2360.             ORDER BY storages.upload_done DESC, articles.id ASC
  2361.             LIMIT ".$step_count."
  2362.             ;";
  2363.         $stmt $conn_akn->prepare($sql);
  2364.         $stmt->execute();
  2365.         $articles $stmt->fetchAll();
  2366.         // holt alle Artikel ab die eine passende Materialaufschlüsselung haben
  2367.         // return $this->render('update/index.html.twig', ["return" => ['article' => $articles]]);
  2368.         if(count($articles)>0){
  2369.             $next_page=true;
  2370.         }
  2371.         foreach($articles as $article){
  2372.             //prüfe auf blocknummer. wenn existiert dann füge an product_number_parent an, wenn nicht continue
  2373.             $blocknummer=null;
  2374.             $blocknummer_zerlegt=array();
  2375.             if(strpos($article["description_advice"],"\n") !== false || strpos($article["description_advice"],"\r") !== false){
  2376.                 $article["description_advice"] = preg_replace('/\\r\\n|\\n|\\r/'' <br>'$article["description_advice"]); // \r\n in <br> umwandeln
  2377.             }
  2378.             // return $this->render('update/index.html.twig', ["return" => ['article' => $article]]);
  2379.             if(isset($article["block_advice"]) && !empty($article["block_advice"]) && $article["block_advice"]!=null){
  2380.                 preg_match('/([a-zA-Z]+)([0-9,]+)([a-zA-Z0-9,]+)/'$article["block_advice"], $blocknummer_zerlegt);
  2381.                 // dd($blocknummer_zerlegt);
  2382.                 if(strlen(end($blocknummer_zerlegt))>1){
  2383.                     $blocknummer end($blocknummer_zerlegt);
  2384.                 }
  2385.             }
  2386.             if($article["materialgroup_name"] == null || empty($article["materialgroup_name"]) || $article["stone_number"] == null || empty($article["stone_number"])){
  2387.                 // ungültiges Material - Abbruch
  2388.                 $sqlt "UPDATE storages SET
  2389.                         upload_done = ?,
  2390.                         upload_failed = ?
  2391.                 WHERE article_id = '".$article["article_id"]."';";
  2392.                 $stmtt $conn_akn->prepare($sqlt);
  2393.                 $stmtt->execute(array(1,1));
  2394.                 continue;
  2395.             }
  2396.             if($blocknummer == null){
  2397.                 // nimm article id statt blocknummer
  2398.                 $article["product_number_parent"].=$article["article_id"];
  2399.             }else{
  2400.                 $article["product_number_parent"].=$blocknummer;
  2401.             }
  2402.             // dd($blocknummer, $article["product_number_parent"]);
  2403.             $kategorie $this->checkCategory($article["materialgroup_name"], $article["materialgroup_id"],
  2404.                                     $article["material_name"], $article["material_id"], $article["materials_picture"],false);
  2405.             // return $this->render('update/index.html.twig', ["return" => [ 'zeit' => $article]]);
  2406.             unset($productParentId);
  2407.             $storage_option_list=array();
  2408.             $storage_option_list $this->get_variant_ids($article); //übergibt storage, returned formatierten array für variantenerstellung
  2409.             // suche ob parent existiert
  2410.             $sql "SELECT 
  2411.                      LOWER(HEX(id)) as id
  2412.                 -- , LOWER(HEX(category_id)) as category_id
  2413.                 FROM product
  2414.                 -- JOIN product_category ON (product_id = id)
  2415.                 -- WHERE product_number = '".$article["material_id"]."_".$article["materialgroup_id"]."_".$blocknummer."'
  2416.                 WHERE product_number = '".$article["product_number_parent"]."'
  2417.                 LIMIT 1
  2418.                 ;";
  2419.             $stmt $conn_shopware->prepare($sql);
  2420.             $stmt->execute();
  2421.             $search_product $stmt->fetchAll();
  2422.             // Prüfe ob es Kategoriesynonyme gibt und füge die hinzo
  2423.             $kats $this->getAdditionalCategories($article["materialgroup_id"], $article["material_id"]);
  2424.             $kategories=array();
  2425.             $kategories[0]["id"]=$kategorie;
  2426.             // $kategories[1]["id"]= $this->default_online_id; //ONLINELAGER ID ENTFERNT
  2427.             if(isset($kats) && is_array($kats) && count($kats)>0){
  2428.                 foreach($kats as $key => $kat){
  2429.                     $kategories[$key+1]["id"]=$kat;
  2430.                 }
  2431.             }
  2432.             
  2433.  
  2434.             // return $this->render('update/index.html.twig', ["return" => [ 'kats' => $kategories, 'articles' => $article, 'article_id' => $search_product]]);
  2435.             if(isset($search_product[0]["id"]) && !empty($search_product[0]["id"]) && count($search_product) > 0){
  2436.                 // baseartikel vorhanden, varianten überprüfen und updaten
  2437.                 $productParentId $search_product[0]["id"];
  2438.             }else{
  2439.                 // baseartikel muss erstellt werden
  2440.                 $ProductFlat = new \App\Controller\shopware\ProductFlat();
  2441.                 $ProductFlat->setActive(true);
  2442.                 // $ProductFlat->setCategories(array(array("id" =>  $kategorie)));
  2443.                 // if(isset($kategories) && is_array($kategories) && count($kategories) > 0){
  2444.                     $ProductFlat->setCategories($kategories);
  2445.                 // }else{
  2446.                     // $ProductFlat->setCategories(array(array("id" =>  $kategorie)));
  2447.                 // }
  2448.                 $ProductFlat->setDeliveryTimeId($this->deliverytime_id);
  2449.                 if($article["bestellbar"]==1){
  2450.                     $ProductFlat->setIsCloseout(false);
  2451.                 }else{
  2452.                     $ProductFlat->setIsCloseout(true);
  2453.                 }
  2454.                 $ProductFlat->setMarkAsTopseller(false);
  2455.                 $ProductFlat->setName($article["material_name"]);
  2456.                 $ProductFlat->setDescription($article["description_advice"]);
  2457.                 $ProductFlat->setPrice(array(array(
  2458.                     "currencyId" => $this->currency_id,
  2459.                     "net" =>  0,
  2460.                     "gross" => 0,
  2461.                     "linked" => false
  2462.                 )));
  2463.                 $ProductFlat->setProductNumber($article["product_number_parent"]);
  2464.                 $ProductFlat->setPurchaseUnit(1);
  2465.                 $ProductFlat->setTaxId($this->tax_id);
  2466.                 $ProductFlat->setVisibilities(array(array(
  2467.                     "salesChannelId" => $this->saleschannel_id,
  2468.                     "visibility" => 30
  2469.                 )));
  2470.                 $custom_fields=array();
  2471.                 $custom_fields["custom_article_surface"] = $article["surface_name"];
  2472.                 $custom_fields["custom_article_search_word"] = $article["search_word"]; 
  2473.                 $custom_fields["custom_article_name_1"] = $article["article_name_1"];
  2474.                 $custom_fields["custom_article_name_2"] = $article["article_name_2"];
  2475.                 $custom_fields["custom_article_material_id"] = $article["material_id"];
  2476.                 $custom_fields["custom_article_materialgroup_id"] = $article["materialgroup_id"];
  2477.                 $custom_fields["custom_article_material_name"] = $article["material_name"];
  2478.                 $custom_fields["custom_article_materialgroup_name"] = $article["materialgroup_name"];
  2479.                 $custom_fields["custom_article_picture"] = "default_art.jpg";
  2480.                 $ProductFlat->setCustomFields(
  2481.                     $custom_fields
  2482.                 );
  2483.                 $ProductFlat->setHeight($article["thickness"]);
  2484.                 $ProductFlat->setLength($article["length"]);
  2485.                 $ProductFlat->setWidth($article["width"]);
  2486.                 $ProductFlat->setStock(0);
  2487.                 //    return $this->render('update/index.html.twig', ["return" => ['nummern' => $ProductFlat]]);
  2488.                 $poststring 'api/product';
  2489.                 if( isset($article["material_name"]) && !empty($article["material_name"])){
  2490.                     try {
  2491.                         $post_product $this->client->request('POST''/'.$poststring, array(
  2492.                         //    "debug" => true,
  2493.                             'allow_redirects'=>array('strict'=>true),
  2494.                             "headers" => $this->header,
  2495.                             "body" => $ProductFlat
  2496.                         ));
  2497.                     } catch (RequestException $e) {
  2498.                         // echo Psr7\str($e->getRequest());
  2499.                         if ($e->hasResponse()) {
  2500.                             $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  2501.                         }else{
  2502.                             $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  2503.                         }
  2504.                         $response->headers->set('Content-Type''application/json');
  2505.                         return $response;
  2506.                         exit;
  2507.                     }
  2508.                     $productParentId $post_product->getHeaders(); 
  2509.                     $productParentId =  str_replace($this->base_url.$poststring.'/','',$productParentId["Location"]); // ID des erstellten produktes
  2510.                     $productParentId $productParentId[0];
  2511.                 }
  2512.             }
  2513.             // Lösche Kategorien vorab zum überschreiben
  2514.             $sql "UPDATE product 
  2515.                 SET category_ids = NULL,
  2516.                     category_tree = NULL 
  2517.                 WHERE product_number = '".$article['product_number_parent']."'
  2518.             ;";
  2519.             $stmt $conn_shopware->prepare($sql);
  2520.             $stmt->execute();
  2521.             $sql "DELETE
  2522.                 FROM product_category
  2523.                 WHERE LOWER(HEX(product_id)) = '".$productParentId."'
  2524.                 ;";
  2525.             $stmt $conn_shopware->prepare($sql);
  2526.             $stmt->execute(); 
  2527.             // ANFANG PATCH MAINPRODUKT
  2528.             $ProductFlat = new \App\Controller\shopware\ProductFlat();
  2529.             $ProductFlat->setActive(true);
  2530.             // if(isset($kategories) && is_array($kategories) && count($kategories) > 0){
  2531.                 $ProductFlat->setCategories($kategories);
  2532.             // }else{
  2533.                 // $ProductFlat->setCategories(array(array("id" =>  $kategorie)));
  2534.             // }
  2535.             $ProductFlat->setDeliveryTimeId($this->deliverytime_id);
  2536.             if($article["bestellbar"]==1){
  2537.                 $ProductFlat->setIsCloseout(false);
  2538.             }else{
  2539.                 $ProductFlat->setIsCloseout(true);
  2540.             }
  2541.             $ProductFlat->setMarkAsTopseller(false);
  2542.             $ProductFlat->setName($article["material_name"]);
  2543.             $ProductFlat->setDescription($article["description_advice"]);
  2544.             $ProductFlat->setPrice(array(array(
  2545.                 "currencyId" => $this->currency_id,
  2546.                 "net" =>  0,
  2547.                 "gross" => 0,
  2548.                 "linked" => false
  2549.             )));
  2550.             $ProductFlat->setProductNumber($article["product_number_parent"]);
  2551.             $ProductFlat->setPurchaseUnit(1);
  2552.             $ProductFlat->setTaxId($this->tax_id);
  2553.             $custom_fields=array();
  2554.             $custom_fields["custom_article_surface"] = $article["surface_name"];
  2555.             $custom_fields["custom_article_search_word"] = $article["search_word"]; 
  2556.             $custom_fields["custom_article_name_1"] = $article["article_name_1"];
  2557.             $custom_fields["custom_article_name_2"] = $article["article_name_2"];
  2558.             $custom_fields["custom_article_material_id"] = $article["material_id"];
  2559.             $custom_fields["custom_article_materialgroup_id"] = $article["materialgroup_id"];
  2560.             $custom_fields["custom_article_material_name"] = $article["material_name"];
  2561.             $custom_fields["custom_article_materialgroup_name"] = $article["materialgroup_name"];
  2562.             if(isset($article["storages_picture"]) && !empty($article["storages_picture"]) && $article["storages_picture"] != null){
  2563.                 $custom_fields["custom_article_picture"] = $article["storages_picture"];
  2564.             }elseif(isset($article["materials_picture"]) && !empty($article["materials_picture"]) && $article["materials_picture"] != null){
  2565.                 $custom_fields["custom_article_picture"] = $article["materials_picture"];
  2566.             }else{ 
  2567.                 $custom_fields["custom_article_picture"] = $article["picture_parent"];
  2568.             }
  2569.             $ProductFlat->setCustomFields(
  2570.                 $custom_fields
  2571.             );
  2572.             $ProductFlat->setHeight($article["thickness"]);
  2573.             $ProductFlat->setLength($article["length"]);
  2574.             $ProductFlat->setWidth($article["width"]);
  2575.             $ProductFlat->setStock(0);
  2576.         
  2577.             if(isset($storage_option_list) && is_array($storage_option_list) && count($storage_option_list)>0){
  2578.                 $ProductFlat->setConfiguratorGroupConfig(array(
  2579.                     array(
  2580.                         "id" => $this->thickness_id,
  2581.                         "representation" => "box",
  2582.                         "expressionForListings" => false
  2583.                     ),
  2584.                     array(
  2585.                         "id" => $this->articlegroup_id,
  2586.                         "representation" => "box",
  2587.                         "expressionForListings" => false
  2588.                     ),
  2589.                     array(
  2590.                         "id" => $this->surface_id,
  2591.                         "representation" => "box",
  2592.                         "expressionForListings" => false
  2593.                     ),
  2594.                     array(
  2595.                         "id" => $this->length_id,
  2596.                         "representation" => "box",
  2597.                         "expressionForListings" => false
  2598.                     ),
  2599.                     array(
  2600.                         "id" => $this->width_id,
  2601.                         "representation" => "box",
  2602.                         "expressionForListings" => false
  2603.                     ),
  2604.                     array(
  2605.                         "id" => $this->amount_id,
  2606.                         "representation" => "box",
  2607.                         "expressionForListings" => false
  2608.                     ),
  2609.                     array(
  2610.                         "id" => $this->surfacearea_id,
  2611.                         "representation" => "box",
  2612.                         "expressionForListings" => false
  2613.                     ),
  2614.                     array(
  2615.                         "id" => $this->meter_id,
  2616.                         "representation" => "box",
  2617.                         "expressionForListings" => false
  2618.                     ),
  2619.                     array(
  2620.                         "id" => $this->sa_id,
  2621.                         "representation" => "box",
  2622.                         "expressionForListings" => false
  2623.                     ),
  2624.                     array(
  2625.                     "id" => $this->blocknumber_id,
  2626.                         "representation" => "box",
  2627.                         "expressionForListings" => false
  2628.                     ),
  2629.                     array(
  2630.                         "id" => $this->material_id,
  2631.                         "representation" => "box",
  2632.                         "expressionForListings" => false
  2633.                     ),
  2634.                     array(
  2635.                         "id" => $this->materialgroup_id,
  2636.                         "representation" => "box",
  2637.                         "expressionForListings" => false
  2638.                     ),
  2639.                     array(
  2640.                         "id" => $this->fault_exists_id,
  2641.                         "representation" => "box",
  2642.                         "expressionForListings" => false
  2643.                     ),
  2644.                     array(
  2645.                         "id" => $this->squaremeterprice_id,
  2646.                         "representation" => "box",
  2647.                         "expressionForListings" => false
  2648.                     ),
  2649.                     array(
  2650.                         "id" => $this->stonenumber_id,
  2651.                         "representation" => "box",
  2652.                         "expressionForListings" => false
  2653.                     ),
  2654.                     array(
  2655.                         "id" => $this->artikeldaten_innen_id,
  2656.                         "representation" => "box",
  2657.                         "expressionForListings" => false
  2658.                     ),
  2659.                     array(
  2660.                         "id" => $this->artikeldaten_aussen_id,
  2661.                         "representation" => "box",
  2662.                         "expressionForListings" => false
  2663.                     )
  2664.                 ));
  2665.             }
  2666.             // return $this->render('update/index.html.twig', ["return" => ['nummern' => $ProductFlat]]);
  2667.             $poststring 'api/product';
  2668.             if( isset($article["material_name"]) && !empty($article["material_name"])){
  2669.                 try {
  2670.                     $post_product $this->client->request('PATCH''/'.$poststring.'/'.$productParentId, array(
  2671.                         // "debug" => true,
  2672.                         'allow_redirects'=>array('strict'=>true),
  2673.                         "headers" => $this->header,
  2674.                         "body" => $ProductFlat
  2675.                     ));
  2676.                 } catch (RequestException $e) {
  2677.                     // echo Psr7\str($e->getRequest());
  2678.                     if ($e->hasResponse()) {
  2679.                         $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  2680.                     }else{
  2681.                         $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  2682.                     }
  2683.                     $response->headers->set('Content-Type''application/json');
  2684.                     return $response;
  2685.                     exit;
  2686.                 }
  2687.             }
  2688.             // ENDE PATCH MAINPRODUKT
  2689.             // Prüfe ob Variante vorhanden ist
  2690.             $sql "SELECT
  2691.                     *
  2692.                     , LOWER(HEX(product_id)) as product_id
  2693.                     , HEX(product_version_id) as product_version_id
  2694.                     , LOWER(HEX(parent_id)) as parent_id
  2695.                     , HEX(language_id) as language_id
  2696.                     , JSON_EXTRACT(product_translation.custom_fields,'$.custom_article_picture') as custom_article_picture
  2697.                     , JSON_EXTRACT(product_translation.custom_fields,'$.custom_article_picture_unavailable') as custom_article_picture_unavailable
  2698.                 FROM product_translation
  2699.                 INNER JOIN product ON (product.id = product_id)
  2700.                 WHERE product_number = '".$article["stone_number"]."'
  2701.                 AND LOWER(HEX(language_id)) = '".$this->language_id."'
  2702.                 LIMIT 3
  2703.                 ;";
  2704.             $stmt $conn_shopware->prepare($sql);
  2705.             $stmt->execute();
  2706.             $articlesSW $stmt->fetchAll();
  2707.             // return $this->render('update/index.html.twig', ["return" => ['storages' => $articlesSW]]);
  2708.             unset($farbid);
  2709.             $variant_id ="";
  2710.             // dd($articlesSW);
  2711.             //** STORAGE ERSTELLEN 
  2712.             if(!isset($articlesSW) || empty($articlesSW) || count($articlesSW)<){
  2713.                 // return $this->render('update/index.html.twig', ["return" => ['storages' => $storage_option_list]]);
  2714.                 // variantenartikel aktualisieren
  2715.                 $ProductFlat = new \App\Controller\shopware\ProductFlat();
  2716.                 $ProductFlat->setName($article["material_name"]);
  2717.                 $ProductFlat->setParentId($productParentId);
  2718.                 $ProductFlat->setActive(true);
  2719.                 $ProductFlat->setProductNumber($article["stone_number"]); //stone_number einzigartig
  2720.                 if(isset($article["amount_1"]) && !empty($article["amount_1"]) && isset($article["reserved_amount_1"]) && !empty($article["reserved_amount_1"]) ){
  2721.                     $stock $article["amount_1"]-$article["reserved_amount_1"];
  2722.                     $ProductFlat->setStock(intval($stock));
  2723.                 }else{
  2724.                     $ProductFlat->setStock(intval($article["amount_1"]));
  2725.                 }
  2726.                 if($article["bestellbar"]==1){
  2727.                     $ProductFlat->setIsCloseout(false);
  2728.                 }else{
  2729.                     $ProductFlat->setIsCloseout(true);
  2730.                 }
  2731.                 $ProductFlat->setHeight($article["thickness"]);
  2732.                 $ProductFlat->setLength($article["length"]);
  2733.                 $ProductFlat->setWidth($article["width"]);
  2734.                 if(isset($article["restprice"]) && !empty(intval($article["restprice"])) && intval($article["restprice"])>0){
  2735.                     $base_price $article["restprice"];
  2736.                 }else{
  2737.                     $base_price $article["price"];
  2738.                 }
  2739.                 if(isset($stock) && !empty(intval($stock)) && intval($stock)!=0){
  2740.                     $price_calc round(($base_price*$article["amount_2"])/intval($stock),2);
  2741.                 }else{
  2742.                     $price_calc round(($base_price*$article["amount_2"]),2);
  2743.                 }
  2744.                 $ProductFlat->setPrice(array(array(
  2745.                     "currencyId" => $this->currency_id,
  2746.                     "net" =>  $price_calc,
  2747.                     "gross" => $price_calc,
  2748.                     "linked" => false
  2749.                 )));
  2750.                 $sol_option=array();
  2751.                 foreach($storage_option_list as $key => $sol){
  2752.                     $sol_option[$key]["id"]=$sol["optionId"];
  2753.                 }
  2754.                 // dd($sol_option);
  2755.                 $ProductFlat->setOptions($sol_option);
  2756.                 $ProductFlat->setProperties($sol_option);
  2757.                 if( $article["fault_id"] != && !empty($article["fault_id"]) && $article["fault_corner"] != && !empty($article["fault_corner"])){
  2758.                     //prüfe ob beides gesetzt, wenn ja definier beides, wenn nicht beides leer
  2759.                     $custom_fields["custom_article_fault_id"] = $article["fault_name"];
  2760.                     $custom_fields["custom_article_fault_corner"] = $article["fault_corner"];
  2761.                     $custom_fields["custom_article_fault_exists"] = 1;
  2762.                 }else{
  2763.                     $custom_fields["custom_article_fault_exists"] = 0;
  2764.                 }
  2765.                 $custom_fields=array();
  2766.                 $custom_fields["custom_article_id"] = $article["article_id"];
  2767.                 $custom_fields["custom_article_material_id"] = $article["material_id"];
  2768.                 $custom_fields["custom_article_materialgroup_id"] = $article["materialgroup_id"];
  2769.                 $custom_fields["custom_article_material_name"] = $article["material_name"];
  2770.                 $custom_fields["custom_article_materialgroup_name"] = $article["materialgroup_name"];
  2771.                 $custom_fields["custom_article_surface"] = $article["surface_name"]; 
  2772.                 $custom_fields["custom_article_stone_number"] = $article["stone_number"];
  2773.                 $custom_fields["custom_article_block_number"] = $article["block_advice"];
  2774.                 $custom_fields["custom_article_search_word"] = $article["search_word"];
  2775.                 $custom_fields["custom_article_advice"] = $article["advice"];
  2776.                 $custom_fields["custom_article_storage"] = $article["storage"];
  2777.                 if(isset($article["amount_2"]) && !empty($article["amount_2"]) && isset($article["reserved_amount_2"]) && !empty($article["reserved_amount_2"]) ){
  2778.                     $amount2 $article["amount_2"]-$article["reserved_amount_2"];
  2779.                     $custom_fields["custom_article_amount_2"] = strval($amount2);
  2780.                 }else{
  2781.                     $custom_fields["custom_article_amount_2"] = $article["amount_2"];
  2782.                 }
  2783.                 $custom_fields["custom_article_einheit_2"] = $article["einheit_2"];
  2784.                 $custom_fields["custom_article_articlegroup_name"] = $article["articlegroup_name"];
  2785.                 $custom_fields["custom_article_fault_id"] = $article["fault_name"];
  2786.                 $custom_fields["custom_article_fault_corner"] = $article["fault_corner"];
  2787.                 $custom_fields["custom_article_name_1"] = $article["article_name_1"];
  2788.                 $custom_fields["custom_article_name_2"] = $article["article_name_2"];
  2789.                 $custom_fields["custom_article_additional_advice"] = $article["additional_advice"];
  2790.                 if(isset($article["storages_picture"]) && !empty($article["storages_picture"]) && $article["storages_picture"] != null){
  2791.                     $custom_fields["custom_article_picture"] = $article["storages_picture"];
  2792.                 }elseif(isset($storags["materials_picture"]) && !empty($article["materials_picture"]) && $article["materials_picture"] != null){
  2793.                     $custom_fields["custom_article_picture"] = $article["materials_picture"];
  2794.                 }else{ 
  2795.                     $custom_fields["custom_article_picture"] = $article["picture_parent"];
  2796.                 }
  2797.                 $custom_fields["custom_article_price_per_unit"]=$article["price"];
  2798.                 $custom_fields["custom_article_price_calculation"]=$article["length"]." cm * ".$article["width"]." cm * ".$article["price"]."€ = ".$price_calc."€";
  2799.                 if(isset($article["color_text"]) && !empty($article["color_text"])){
  2800.                     $color explode('#',$article["color_text"]);
  2801.                     $article["color_text"] = trim($color[0]);
  2802.                     $custom_fields["custom_article_material_color"] = $article["color_text"];
  2803.                 }
  2804.                 $ProductFlat->setCustomFields($custom_fields);
  2805.                 try {
  2806.                     //#* Post Variante
  2807.                     $post_product $this->client->request('POST''/api/product', array(
  2808.                         // "debug" => true,
  2809.                         'allow_redirects'=>array('strict'=>true),
  2810.                         "headers" => $this->header,
  2811.                         "body" => $ProductFlat
  2812.                     ));
  2813.                 } catch (RequestException $e) {
  2814.                    // echo Psr7\str($e->getRequest());
  2815.                     if ($e->hasResponse()) {
  2816.                         $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  2817.                     }else{
  2818.                         $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  2819.                     }
  2820.                     $response->headers->set('Content-Type''application/json');
  2821.                     return $response;
  2822.                     exit;
  2823.                 }
  2824.                 $location array_pop$post_product->getHeaders()['Location'] );
  2825.                 $variant_id basename($location);
  2826.             }else{
  2827.                 $variant_id $articlesSW[0]["product_id"];
  2828.                 //vorher options und properties bereinigen
  2829.                 $sql "DELETE
  2830.                     FROM product_option
  2831.                     WHERE LOWER(HEX(product_id)) = '".$variant_id."'
  2832.                     ;";
  2833.                 $stmt $conn_shopware->prepare($sql);
  2834.                 $stmt->execute(); 
  2835.                 $sql "DELETE
  2836.                     FROM product_property
  2837.                     WHERE LOWER(HEX(product_id)) = '".$variant_id."'
  2838.                     ;";
  2839.                 $stmt $conn_shopware->prepare($sql);
  2840.                 $stmt->execute(); 
  2841.                 //### variante updaten
  2842.                 $ProductFlat = new \App\Controller\shopware\ProductFlat();
  2843.                 $ProductFlat->setName($article["material_name"]);
  2844.                 $ProductFlat->setParentId($productParentId);
  2845.                 $ProductFlat->setActive(true);
  2846.                 $ProductFlat->setProductNumber($article["stone_number"]); //stone_number unique
  2847.                 if(isset($article["amount_1"]) && !empty($article["amount_1"]) && isset($article["reserved_amount_1"]) && !empty($article["reserved_amount_1"]) ){
  2848.                     $stock $article["amount_1"]-$article["reserved_amount_1"];
  2849.                     $ProductFlat->setStock(intval($stock));
  2850.                 }else{
  2851.                     $ProductFlat->setStock(intval($article["amount_1"]));
  2852.                 }
  2853.                 if($article["bestellbar"]==1){
  2854.                     $ProductFlat->setIsCloseout(false);
  2855.                 }else{
  2856.                     $ProductFlat->setIsCloseout(true);
  2857.                 }
  2858.                 $ProductFlat->setHeight($article["thickness"]);
  2859.                 $ProductFlat->setLength($article["length"]);
  2860.                 $ProductFlat->setWidth($article["width"]);
  2861.                 if(isset($article["restprice"]) && !empty(intval($article["restprice"])) && intval($article["restprice"])>0){
  2862.                     $base_price $article["restprice"];
  2863.                 }else{
  2864.                     $base_price $article["price"];
  2865.                 }
  2866.                 if(isset($stock) && !empty(intval($stock)) && intval($stock)!=0){
  2867.                     $price_calc round(($base_price*$article["amount_2"])/intval($stock),2);
  2868.                 }else{
  2869.                     $price_calc round(($base_price*$article["amount_2"]),2);
  2870.                 }
  2871.                 $ProductFlat->setPrice(array(array(
  2872.                     "currencyId" => $this->currency_id,
  2873.                     "net" =>  $price_calc,
  2874.                     "gross" => $price_calc,
  2875.                     "linked" => false
  2876.                 )));
  2877.                 $sol_option=array();
  2878.                 foreach($storage_option_list as $key => $sol){
  2879.                     $sol_option[$key]["id"]=$sol["optionId"];
  2880.                 }
  2881.                 $ProductFlat->setOptions($sol_option);
  2882.                 $ProductFlat->setProperties($sol_option);
  2883.                 if( $article["fault_id"] != && !empty($article["fault_id"]) && $article["fault_corner"] != && !empty($article["fault_corner"])){
  2884.                     //prüfe ob beides gesetzt, wenn ja definier beides, wenn nicht beides leer
  2885.                     $custom_fields["custom_article_fault_id"] = $article["fault_name"];
  2886.                     $custom_fields["custom_article_fault_corner"] = $article["fault_corner"];
  2887.                     $custom_fields["custom_article_fault_exists"] = 1;
  2888.                 }else{
  2889.                     $custom_fields["custom_article_fault_exists"] = 0;
  2890.                 }
  2891.                 $custom_fields=array();
  2892.                 $custom_fields["custom_article_id"] = $article["article_id"];
  2893.                 $custom_fields["custom_article_material_id"] = $article["material_id"];
  2894.                 $custom_fields["custom_article_materialgroup_id"] = $article["materialgroup_id"];
  2895.                 $custom_fields["custom_article_material_name"] = $article["material_name"];
  2896.                 $custom_fields["custom_article_materialgroup_name"] = $article["materialgroup_name"];
  2897.                 $custom_fields["custom_article_surface"] = $article["surface_name"]; 
  2898.                 $custom_fields["custom_article_stone_number"] = $article["stone_number"];
  2899.                 $custom_fields["custom_article_block_number"] = $article["block_advice"];
  2900.                 $custom_fields["custom_article_search_word"] = $article["search_word"];
  2901.                 $custom_fields["custom_article_advice"] = $article["advice"];
  2902.                 $custom_fields["custom_article_storage"] = $article["storage"];
  2903.                 if(isset($article["amount_2"]) && !empty($article["amount_2"]) && isset($article["reserved_amount_2"]) && !empty($article["reserved_amount_2"]) ){
  2904.                     $amount2 $article["amount_2"]-$article["reserved_amount_2"];
  2905.                     $custom_fields["custom_article_amount_2"] = strval($amount2);
  2906.                 }else{
  2907.                     $custom_fields["custom_article_amount_2"] = $article["amount_2"];
  2908.                 }
  2909.                 $custom_fields["custom_article_einheit_2"] = $article["einheit_2"];
  2910.                 $custom_fields["custom_article_articlegroup_name"] = $article["articlegroup_name"];
  2911.                 $custom_fields["custom_article_fault_id"] = $article["fault_name"];
  2912.                 $custom_fields["custom_article_fault_corner"] = $article["fault_corner"];
  2913.                 $custom_fields["custom_article_name_1"] = $article["article_name_1"];
  2914.                 $custom_fields["custom_article_name_2"] = $article["article_name_2"];
  2915.                 $custom_fields["custom_article_additional_advice"] = $article["additional_advice"];
  2916.                 if(isset($article["storages_picture"]) && !empty($article["storages_picture"]) && $article["storages_picture"] != null){
  2917.                     $custom_fields["custom_article_picture"] = $article["storages_picture"];
  2918.                 }elseif(isset($article["materials_picture"]) && !empty($article["materials_picture"]) && $article["materials_picture"] != null){
  2919.                     $custom_fields["custom_article_picture"] = $article["materials_picture"];
  2920.                 }else{ 
  2921.                     $custom_fields["custom_article_picture"] = $article["picture_parent"];
  2922.                 }
  2923.                 if(isset($article["color_text"]) && !empty($article["color_text"])){
  2924.                     $color explode('#',$article["color_text"]);
  2925.                     $article["color_text"] = trim($color[0]);
  2926.                     $custom_fields["custom_article_material_color"] = $article["color_text"];
  2927.                 }
  2928.                 $custom_fields["custom_article_price_per_unit"]=$article["price"];
  2929.                 $custom_fields["custom_article_price_calculation"]=$article["length"]." cm * ".$article["width"]." cm * ".$article["price"]."€ = ".$price_calc."€";
  2930.                 $ProductFlat->setCustomFields($custom_fields);
  2931.                 try {
  2932.                     //#* Patch Variante
  2933.                     $post_product $this->client->request('PATCH''/api/product/'.$variant_id, array(
  2934.                         // "debug" => true,
  2935.                         'allow_redirects'=>array('strict'=>true),
  2936.                         "headers" => $this->header,
  2937.                         "body" => $ProductFlat
  2938.                         // "form_params" => array($variant_values)
  2939.                     ));
  2940.                 } catch (RequestException $e) {
  2941.                    // echo Psr7\str($e->getRequest());
  2942.                    if ($e->hasResponse()) {
  2943.                     $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  2944.                     }else{
  2945.                         $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  2946.                     }
  2947.                     $response->headers->set('Content-Type''application/json');
  2948.                     return $response;
  2949.                     exit;
  2950.                 }
  2951.             }
  2952.             # UPDATE KEYWORDS ###################
  2953.             if(isset($variant_id) && !empty($variant_id) && $variant_id != ""){
  2954.                 if(isset($blocknummer) && !empty($blocknummer) && $blocknummer != ""){
  2955.                     $keywords = [ $article['search_word'], $blocknummer ];
  2956.                 }else{
  2957.                     $keywords = [ $article['search_word'] ];
  2958.                 }
  2959.                 try {
  2960.                     $post_product $this->client->request('PATCH''/api/product/'.$variant_id, array(
  2961.                         //    "debug" => true,
  2962.                         'allow_redirects'=>array('strict' => true),
  2963.                         'headers' => $this->header,
  2964.                         'body' => json_encode([
  2965.                             "customSearchKeywords"=> $keywords   // DAS IST KEIN MERGE: WERTE WERDEN KNALLHART ÜBERSCHRIEBEN
  2966.                         ])
  2967.                     ));
  2968.                 } catch (RequestException $e) {
  2969.                     // echo Psr7\str($e->getRequest());
  2970.                     // return $this->render('update/index.html.twig', ["return" => [ 'request' => Psr7\str($e->getRequest()]]);
  2971.                     if ($e->hasResponse()) {
  2972.                         $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  2973.                     }else{
  2974.                         $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  2975.                     }
  2976.                     $response->headers->set('Content-Type''application/json');
  2977.                     return $response;
  2978.                     exit;
  2979.                 }
  2980.             }
  2981.             # END UPDATE KEYWORDS ###################
  2982.   
  2983.             //###*** Prüfe auf Bild und füge es hinzu, falls es nicht vorhanden ist.
  2984.             if(isset( $article['storages_picture']) && !empty( $article['storages_picture']) &&  $article['storages_picture'] != ""){
  2985.                 $this->add_image($article['storages_picture'], $variant_idfalse); 
  2986.             }elseif(isset( $article['materials_picture']) && !empty( $article['materials_picture']) &&  $article['materials_picture'] != ""){
  2987.                 $this->add_image($article['materials_picture'], $variant_idfalse); 
  2988.             }else{
  2989.                 $this->add_image($this->default_image_filename$variant_idfalse);
  2990.             }
  2991.             // product_configurator_setting matchen und option ids abgleichen + erstellen wenn nicht vorhanden
  2992.             $sql "SELECT 
  2993.                     *
  2994.                     , LOWER(HEX(product_id)) as product_id
  2995.                     , LOWER(HEX(property_group_option_id)) as property_group_option_id
  2996.                     FROM product_configurator_setting
  2997.                     WHERE LOWER(HEX(product_id)) = '".$productParentId."'
  2998.                 ;";
  2999.             $stmt $conn_shopware->prepare($sql);
  3000.             $stmt->execute();
  3001.             $config_settings $stmt->fetchAll();
  3002.             $option_list array_column($storage_option_list'optionId');
  3003.             $config_list array_column($config_settings'property_group_option_id');
  3004.             // return $this->render('update/index.html.twig', ["return" => [ 'storag' => $config_settings, 'liste' => $config_list]]);
  3005.             // fehlende optionen in variantengenerator hinzufügen
  3006.             foreach($option_list as $option){
  3007.                 if(!in_array($option,$config_list)){
  3008.                     try {
  3009.                         $post_product $this->client->request('POST''/api/product/'.$productParentId.'/configurator-settings', array(
  3010.                             // "debug" => true,
  3011.                             'allow_redirects'=>array('strict'=>true),
  3012.                             "headers" => $this->header,
  3013.                             'body' => json_encode([
  3014.                                 "optionId"=>$option
  3015.                             ])
  3016.                         ));
  3017.                     } catch (RequestException $e) {
  3018.                         // echo Psr7\str($e->getRequest());
  3019.                         // if ($e->hasResponse()) {
  3020.                         //     echo Psr7\str($e->getResponse());
  3021.                         // }
  3022.                     }
  3023.                 }
  3024.             }
  3025.             // return $this->render('update/index.html.twig', ["return" => [ 'storag' => $config_settings, "optionlist" => $storage_option_list]]);
  3026.             //### Setze Date bei Storages
  3027.             $sqlt "UPDATE storages SET
  3028.                     upload_done = ?
  3029.             WHERE id = '".$article["storage_id"]."'
  3030.             ;";
  3031.             $stmtt $conn_akn->prepare($sqlt);
  3032.             $stmtt->execute(array( 
  3033.                 1
  3034.             ));
  3035.         }
  3036.         // return $this->render('update/index.html.twig', ["return" => [ 'storag' => "pagedone"]]);
  3037.         if($next_page==true){
  3038.             return $this->redirect('/update/products');
  3039.         }
  3040.         //### ENDE Produktimport
  3041.         // exit;
  3042.         //setze alle storages auf upload failed die keine stone number besitzen
  3043.         $sqlt "UPDATE storages SET
  3044.                 upload_done = ?,
  3045.                 upload_failed = ?
  3046.         -- WHERE stone_number = ''
  3047.         WHERE (upload_done IS NULL AND upload_failed IS NULL) OR (upload_done = 4 AND upload_failed IS NULL)
  3048.         ;";
  3049.         $stmtt $conn_akn->prepare($sqlt);
  3050.         $stmtt->execute(array( 
  3051.             1,
  3052.             3
  3053.         ));
  3054.      
  3055.         // $sql = "SELECT 
  3056.         //         *
  3057.         //         FROM pixel_dates
  3058.         //         WHERE FunctionName = 'update_products'
  3059.         //     ;";
  3060.         // $stmt = $conn_shopware->prepare($sql);
  3061.         // $stmt->execute();
  3062.         // $date_select = $stmt->fetchAll();
  3063.         // if(isset($date_select) && !empty($date_select) && count($date_select) > 0){
  3064.         //     $sqlt = "UPDATE pixel_dates SET
  3065.         //             UpdateDatetime = ?
  3066.         //     WHERE FunctionName = 'update_products'
  3067.         //     ;";
  3068.         //     $stmtt = $conn_shopware->prepare($sqlt);
  3069.         //     $stmtt->execute(array( 
  3070.         //         date('Y-m-d H:i:s')
  3071.         //     ));
  3072.         // }else{
  3073.         //     $sql = "INSERT INTO pixel_dates (FunctionName,UpdateDatetime)
  3074.         //             VALUES(?,?)";
  3075.         //     $stmtt = $conn_shopware->prepare($sql);
  3076.         //     $stmtt->execute(array( 
  3077.         //         "update_products done",
  3078.         //         date('Y-m-d H:i:s')
  3079.         //     ));
  3080.         // }
  3081.         $sql "INSERT INTO pixel_dates (FunctionName,StartEnde,UpdateDatetime)
  3082.                 VALUES(?,?,?)";
  3083.         $stmtt $conn_shopware->prepare($sql);
  3084.         $stmtt->execute(array( 
  3085.             "update/products",
  3086.             "ENDE",
  3087.             date('Y-m-d H:i:s')
  3088.         ));
  3089.        
  3090.         $response = new Response(json_encode(array('success' => true)));
  3091.         $response->headers->set('Content-Type''application/json');
  3092.         return $response;
  3093.         return $this->render('update/index.html.twig', ["return" => [ 'status' => "done"]]);
  3094.     }
  3095.     /**
  3096.      * Leert die Logs im Shopware Backend
  3097.      * 
  3098.      * @Route("/clear/logs", name="clear_logs")
  3099.      * 
  3100.      * @return mixed
  3101.      */
  3102.     function clear_logs()
  3103.     {
  3104.         // $this->client_connect();
  3105.         $conn_shopware $this->shop_connect();
  3106.         $sql "TRUNCATE TABLE log_entry";
  3107.         $stmt $conn_shopware->prepare($sql);
  3108.         $stmt->execute();
  3109.         return $this->render('update/index.html.twig', ["return" => [ 'status' => "Logs erfolgreich geleert."]]);
  3110.     }
  3111.     /**
  3112.      * Leert die Tabellen zur Hintergrundverarbeitung von Shopware
  3113.      * 
  3114.      * @Route("/clear/queue", name="clear_queue")
  3115.      * 
  3116.      * @return mixed
  3117.      */
  3118.     function clear_queue()
  3119.     {
  3120.         // $this->client_connect();
  3121.         $conn_shopware $this->shop_connect();
  3122.         $sql "TRUNCATE TABLE enqueue";
  3123.         $stmt $conn_shopware->prepare($sql);
  3124.         $stmt->execute();
  3125.         $sql "TRUNCATE TABLE message_queue_stats";
  3126.         $stmt $conn_shopware->prepare($sql);
  3127.         $stmt->execute();
  3128.         return $this->render('update/index.html.twig', ["return" => [ 'status' => "Queue erfolgreich geleert."]]);
  3129.     }
  3130.     /**
  3131.      * Sortiert Kategorien in Shopware
  3132.      * 
  3133.      * @Route("/sort/categories/{page}", name="sort_categories")
  3134.      * 
  3135.      * @return mixed
  3136.      */
  3137.     function sort_categories($page)
  3138.     {
  3139.         // hole alle kategorien in steine aus aller welt ab,
  3140.         // hole für jede alle kategorien ab die diese als parent haben
  3141.         // erstelle array aus id => name
  3142.         // sortiere namen asc/desc
  3143.         // update after_category_id. erste kategorie leer, rest id vom vorgänger
  3144.         $this->client_connect();
  3145.         $conn_shopware $this->shop_connect();
  3146.         // $step_count = 3;
  3147.         # GET ALL CATEGORIES
  3148.         $sql "SELECT LOWER(HEX(id)) AS category_id, category_translation.name, LOWER(HEX(after_category_id)) AS after_category_id
  3149.             FROM category
  3150.             INNER JOIN category_translation ON (category.id = category_translation.category_id)
  3151.             WHERE LOWER(HEX(parent_id)) = '".$this->default_category_id."'
  3152.             AND LOWER(HEX(language_id)) = '".$this->language_id."'
  3153.             ;";
  3154.         $stmt $conn_shopware->prepare($sql);
  3155.         $stmt->execute();
  3156.         $categories_SELECT $stmt->fetchAll();
  3157.         $keys array_column($categories_SELECT'name');
  3158.         array_multisort($keysSORT_ASC$categories_SELECT);
  3159.         // dd($categories_SELECT);
  3160.         // if(isset($categories_SELECT) && is_array($categories_SELECT) && count($categories_SELECT) > 0) {
  3161.         //     foreach($categories_SELECT as $category) {
  3162.         if(isset($categories_SELECT[$page]) && is_array($categories_SELECT[$page]) && count($categories_SELECT[$page]) > 0) {
  3163.             // foreach($categories_SELECT as $category) {
  3164.                 // dd($category["category_id"]);
  3165.                     // -- WHERE LOWER(HEX(parent_id)) = '".$category["category_id"]."'
  3166.                 $sql "SELECT LOWER(HEX(id)) AS category_id, category_translation.name, LOWER(HEX(after_category_id)) AS after_category_id
  3167.                     FROM category
  3168.                     INNER JOIN category_translation ON (category.id = category_translation.category_id)
  3169.                     WHERE LOWER(HEX(parent_id)) = '".$categories_SELECT[$page]["category_id"]."'
  3170.                     -- WHERE LOWER(HEX(parent_id)) = '91bb5a08283d40a3bf1ec0a4759660b1'
  3171.                     AND LOWER(HEX(language_id)) = '".$this->language_id."'
  3172.                     ;";
  3173.                 $stmt $conn_shopware->prepare($sql);
  3174.                 $stmt->execute();
  3175.                 $subcategories $stmt->fetchAll();
  3176.                 $keys array_column($subcategories'name');
  3177.                 array_multisort($keysSORT_ASC$subcategories);
  3178.                 // dd($subcategories);
  3179.                 //setze after ids neu
  3180.                 foreach($subcategories as $key => $subcategory){
  3181.                     $CategoryFlat = new \App\Controller\shopware\CategoryFlat();
  3182.                     if($key==0){
  3183.                         //setze id auf null
  3184.                         $CategoryFlat->setAfterCategoryId(null);
  3185.                     }else{
  3186.                         // setze id auf $key-1
  3187.                         $CategoryFlat->setAfterCategoryId($subcategories[$key-1]["category_id"]);
  3188.                         // var_dump("patch ".$subcategory["name"]. " folgt ".$subcategories[$key-1]["name"]);
  3189.                     }
  3190.                     // try{
  3191.                     $post_category $this->client->request('PATCH''/api/category/'.$subcategory["category_id"], array(
  3192.                         // 'debug' => true, 
  3193.                         'allow_redirects'=>array('strict'=>true),
  3194.                         "headers" => $this->header,
  3195.                         "body" => $CategoryFlat,
  3196.                     ));
  3197.                     // } catch (RequestException $e) {
  3198.                     //     echo Psr7\str($e->getRequest());
  3199.                     //     if ($e->hasResponse()) {
  3200.                     //         echo Psr7\str($e->getResponse());
  3201.                     //     }
  3202.                     //     exit;
  3203.                     // }
  3204.                 }
  3205.                 //    dd($categories_SELECT[$page+1]);    
  3206.             if(isset($categories_SELECT[$page+1]) && is_array($categories_SELECT[$page+1]) && count($categories_SELECT[$page+1]) > 0){
  3207.                 return $this->redirect('/sort/categories/'.($page+1));
  3208.             }
  3209.             //sortiere parents
  3210.             foreach($categories_SELECT as $key => $category){
  3211.                 $CategoryFlat = new \App\Controller\shopware\CategoryFlat();
  3212.                 if($key==0){
  3213.                     //setze id auf null
  3214.                     $CategoryFlat->setAfterCategoryId(null);
  3215.                 }else{
  3216.                     // setze id auf $key-1
  3217.                     $CategoryFlat->setAfterCategoryId($categories_SELECT[$key-1]["category_id"]);
  3218.                     // var_dump("patchober ".$subcategory["name"]. " folgt ".$subcategories[$key-1]["name"]);
  3219.                 }
  3220.                 $post_category $this->client->request('PATCH''/api/category/'.$category["category_id"], array(
  3221.                     // 'debug' => true, 
  3222.                     'allow_redirects'=>array('strict'=>true),
  3223.                     "headers" => $this->header,
  3224.                     "body" => $CategoryFlat,
  3225.                 ));
  3226.             }
  3227.         }
  3228.         # CLEAR CACHE ##########################
  3229.         $this->clear_cache();
  3230.         return $this->render('update/index.html.twig', ["return" => [ 'status' => $subcategories'key' => $keys]]);
  3231.     }
  3232.     public function clear_cache() {
  3233.         try {
  3234.             $post_product $this->client->request('DELETE''/api/_action/cache', array(
  3235.                 //    "debug" => true,
  3236.                 'allow_redirects'=>array('strict'=>true),
  3237.                 "headers" => $this->header,
  3238.             ));
  3239.         
  3240.         } catch (RequestException $e) {
  3241.             // echo Psr7\str($e->getRequest());
  3242.             // if ($e->hasResponse()) {
  3243.             //     echo Psr7\str($e->getResponse());
  3244.             // }
  3245.             if ($e->hasResponse()) {
  3246.                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  3247.             }else{
  3248.                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  3249.             }
  3250.             $response->headers->set('Content-Type''application/json');
  3251.             return $response;
  3252.             exit;
  3253.         }
  3254.     }
  3255.     /**
  3256.      * Importiert SQL
  3257.      * 
  3258.      * @Route("/sql_import", name="sql_import")
  3259.      * 
  3260.      * @return mixed
  3261.      */
  3262.     public function sql_import(MailerInterface $mailer) {
  3263.         $this->client_connect();
  3264.         $conn_akn $this->dump_connect();
  3265.         $conn_shopware $this->shop_connect();
  3266.         // $location_sql = "/var/www/vhosts/pixelproductions.de/sw6.pixelproductions.de/akn_upload/"; // DEV
  3267.         // $location_sql = "/home/aknnadbt/shop.akn-natursteine.de/UPLOAD/"; // LIVE
  3268.         // $location_sql = "/var/www/vhosts/akn-natursteine.de/shop.akn-natursteine.de/UPLOAD/"; // LIVE NACH UMZUG
  3269.         $location_sql "/var/www/vhosts/swconnect.akn-natursteine.de/httpdocs/public/UPLOAD/"// LIVE NACH IONOS UMZUG
  3270.         $target_location $this->getParameter('kernel.project_dir')."/public/sql/";
  3271.         $file_name "import";
  3272.         // dd("connected");
  3273.         $sql "INSERT INTO pixel_dates (FunctionName,StartEnde,UpdateDatetime)
  3274.                 VALUES(?,?,?)";
  3275.         $stmtt $conn_shopware->prepare($sql);
  3276.         $stmtt->execute(array( 
  3277.             "sql_import",
  3278.             "Start",
  3279.             date('Y-m-d H:i:s')
  3280.         ));
  3281.         if( (file_exists($target_location.$file_name."_backup.sql") && file_exists($location_sql.$file_name.".sql") &&
  3282.         filemtime($location_sql.$file_name.".sql") > filemtime($target_location.$file_name."_backup.sql")) || 
  3283.         (!file_exists($target_location.$file_name."_backup.sql") && file_exists($location_sql.$file_name.".sql")) ){
  3284.             // wenn kein backupfile existiert oder der importfile neuer als der backupfile ist importiere sql
  3285.             copy($location_sql.$file_name.".sql",$target_location.$file_name.".sql");
  3286.         }else{
  3287.             $sql "INSERT INTO pixel_dates (FunctionName,StartEnde,UpdateDatetime)
  3288.                     VALUES(?,?,?)";
  3289.             $stmtt $conn_shopware->prepare($sql);
  3290.             $stmtt->execute(array( 
  3291.                 "sql_import",
  3292.                 "ENDE",
  3293.                 date('Y-m-d H:i:s')
  3294.             ));
  3295.             echo "Keine (neue) Datei vorhanden!";
  3296.             exit;
  3297.         }
  3298.         
  3299.         // #### copy tables for update
  3300.         $sql "TRUNCATE TABLE storages_alt";
  3301.         $stmt $conn_akn->prepare($sql);
  3302.         $stmt->execute();
  3303.         
  3304.         $sql "INSERT INTO storages_alt SELECT * FROM storages;";
  3305.         $stmt $conn_akn->prepare($sql);
  3306.         $stmt->execute();
  3307.         $sql_file $this->getParameter('kernel.project_dir').'/public/sql/import.sql';
  3308.         if( file_exists($sql_file)) {
  3309.             $sql_import file_get_contents($sql_file);
  3310.             // $sql_import = str_replace(["\n", "\r"], "", $sql_import);
  3311.             // $sql_import = str_replace("`", "", $sql_import);
  3312.             $sql_import explode(';'$sql_import);
  3313.             
  3314.             foreach($sql_import as $key => $query) {
  3315.                 if( trim($query) != '') {
  3316.                     // dd($query);
  3317.                     try{   
  3318.                         # DO SQL STUFF #################
  3319.                         # SKIP
  3320.                         if( str_starts_withstrtolowertrim$query ) ), 'use' )) continue;
  3321.                         // if( str_starts_with( strtolower( trim( $query ) ), 'truncate' )) continue;
  3322.                         $sql $query.';';
  3323.                         $stmt $conn_akn->prepare($sql);
  3324.                         $stmt->execute();
  3325.                         // $data = $stmt->fetchAll();
  3326.                         
  3327.                         # TODO: CATCH ERRORS
  3328.                     } catch (RequestException $e) {
  3329.                         
  3330.                         $sql "INSERT INTO pixel_dates (FunctionName,StartEnde,UpdateDatetime)
  3331.                                 VALUES(?,?,?)";
  3332.                         $stmtt $conn_shopware->prepare($sql);
  3333.                         $stmtt->execute(array( 
  3334.                             "sql_import FEHLER AUFGETRETEN IN ZEILE ".$key." DER SQL",
  3335.                             "ERROR",
  3336.                             date('Y-m-d H:i:s')
  3337.                         ));
  3338.                         
  3339.                         $this->restore_sql();
  3340.                         //lade alte sql wieder rein
  3341.                         // echo Psr7\str($e->getRequest());
  3342.                         // sende errer als mailtext
  3343.                         // echo Psr7\str($e->getResponse());
  3344.                         
  3345.                         if ($e->hasResponse()) {
  3346.                             $email = (new Email())
  3347.                             ->from('mo@pixelproductions.de')
  3348.                             ->to('mo@pixelproductions.de')
  3349.                             //->cc('cc@example.com')
  3350.                             //->bcc('bcc@example.com')
  3351.                             //->replyTo('fabien@example.com')
  3352.                             //->priority(Email::PRIORITY_HIGH)
  3353.                             ->subject('Fehler beim SQL Import bei AKN')
  3354.                             ->text('Es ist ein Fehler aufgetreten.')
  3355.                             ->html('Beim SQL-Import ist ein Fehler aufgetreten.<br>
  3356.                             Wann? '.date('Y-m-d H:i:s').' (Serverzeit)<br>
  3357.                             letzte Backupdatei wird eingespielt.<br>
  3358.                             Fehler:<br>'.Psr7\str($e->getResponse()));
  3359.                             
  3360.                             $mailer->send($email);
  3361.                         }
  3362.                         exit;
  3363.                     }
  3364.                 }
  3365.             }
  3366.             //file umbenennen nach erfolgreichem einspielen
  3367.             rename($target_location.$file_name.".sql",$target_location.$file_name."_backup.sql");
  3368.             
  3369.             // #### check differences in tables
  3370.             $sql "SELECT stone_number 
  3371.             FROM storages 
  3372.             WHERE 
  3373.             (`kind`,article_id,materialgroup_id,material_id,surface_id,`length`,width,thickness,origin,stone_number,block_number,advice,storage,quantity_unit_1,quantity_unit_2,gross_length,gross_width,gross_thickness,amount_1,amount_2,reserved_amount_1,reserved_amount_2,articlegroup_id,fault_id,fault_corner,picture,restprice,price,block_advice,additional_advice)
  3374.             NOT IN (SELECT 
  3375.             `kind`,article_id,materialgroup_id,material_id,surface_id,`length`,width,thickness,origin,stone_number,block_number,advice,storage,quantity_unit_1,quantity_unit_2,gross_length,gross_width,gross_thickness,amount_1,amount_2,reserved_amount_1,reserved_amount_2,articlegroup_id,fault_id,fault_corner,picture,restprice,price,block_advice,additional_advice
  3376.             FROM storages_alt) 
  3377.             AND stone_number IS NOT NULL
  3378.             AND stone_number !=''
  3379.                 ;";
  3380.             $stmt $conn_akn->prepare($sql);
  3381.             $stmt->execute();
  3382.             $changed_rows $stmt->fetchAll();
  3383.             // return $this->render('update/index.html.twig', ["return" => ['test' => $changed_rows ]]);
  3384.             $changed_rows array_column($changed_rows'stone_number');
  3385.             // set priority to changed rows
  3386.             $sql "UPDATE storages
  3387.                     SET upload_done = 4 
  3388.                     WHERE stone_number IN ('".implode("', '",$changed_rows)."');";
  3389.             $stmt $conn_akn->prepare($sql);
  3390.             $stmt->execute();
  3391.             
  3392.             // #### check differences in artikeldaten
  3393.             $this->createProductfinderList();
  3394.             $sql "SELECT material,material_group
  3395.             FROM artikeldaten
  3396.             WHERE 
  3397.             (material,material_group,innen_kueche,innen_fenster,innen_bad,innen_fussboden,innen_boden,aussen_boden,aussen_fassade,aussen_fenster,aussen_stufen,aussen_salz,bestellbar
  3398.             )
  3399.             NOT IN (SELECT 
  3400.             material,material_group,innen_kueche,innen_fenster,innen_bad,innen_fussboden,innen_boden,aussen_boden,aussen_fassade,aussen_fenster,aussen_stufen,aussen_salz,bestellbar
  3401.             FROM artikeldaten_alt)
  3402.                 ;";
  3403.             $stmt $conn_akn->prepare($sql);
  3404.             $stmt->execute();
  3405.             $changed_rows $stmt->fetchAll();
  3406.             // return $this->render('update/index.html.twig', ["return" => ['test' => $changed_rows ]]);
  3407.             foreach($changed_rows as $row){
  3408.                 // set priority to changed rows
  3409.                 $sql "UPDATE storages
  3410.                         SET upload_done = 4 
  3411.                         WHERE material_id = '".$row["material"]."'
  3412.                         AND materialgroup_id = '".$row["material_group"]."'
  3413.                         ;";
  3414.                 $stmt $conn_akn->prepare($sql);
  3415.                 $stmt->execute();
  3416.             }
  3417.         }
  3418.         $this->createCrosssellingList();
  3419.         $sql "INSERT INTO pixel_dates (FunctionName,StartEnde,UpdateDatetime)
  3420.                 VALUES(?,?,?)";
  3421.         $stmtt $conn_shopware->prepare($sql);
  3422.         $stmtt->execute(array( 
  3423.             "sql_import",
  3424.             "ENDE",
  3425.             date('Y-m-d H:i:s')
  3426.         ));
  3427.         return $this->redirect('/excel_import');
  3428.         # DONE
  3429.         $response = new Response(json_encode(array('success' => true)));
  3430.         $response->headers->set('Content-Type''application/json');
  3431.         return $response;
  3432.         return $this->render('update/index.html.twig', ["return" => ['success' => "SQL importiert" ]]);
  3433.         
  3434.     }
  3435.     /**
  3436.      * Importiert EXCEL
  3437.      * 
  3438.      * @Route("/excel_import", name="excel_import")
  3439.      * 
  3440.      * @return mixed
  3441.      */
  3442.     public function excel_import(MailerInterface $mailer) {
  3443.         $this->client_connect();
  3444.         $conn_akn $this->dump_connect();
  3445.         $conn_shopware $this->shop_connect();
  3446.         // $location_csv = "/var/www/vhosts/akn-natursteine.de/shop.akn-natursteine.de/UPLOAD/"; // LIVE NACH UMZUG
  3447.         $location_csv "/var/www/vhosts/swconnect.akn-natursteine.de/httpdocs/public/UPLOAD/"// LIVE NACH IONOS UMZUG
  3448.         $target_location $this->getParameter('kernel.project_dir')."/public/excel/";
  3449.         $file_name "Artikeldaten";
  3450.         // $filetype = ".xlsx";
  3451.         $filetype ".csv";
  3452.         $sql "INSERT INTO pixel_dates (FunctionName,StartEnde,UpdateDatetime)
  3453.                 VALUES(?,?,?)";
  3454.         $stmtt $conn_shopware->prepare($sql);
  3455.         $stmtt->execute(array( 
  3456.                 "excel_import",
  3457.                 "Start",
  3458.                 date('Y-m-d H:i:s')
  3459.             ));
  3460.         if( (file_exists($target_location.$file_name."_backup".$filetype) && file_exists($location_csv.$file_name.$filetype) &&
  3461.         filemtime($location_csv.$file_name.$filetype) > filemtime($target_location.$file_name."_backup".$filetype)) || 
  3462.         (!file_exists($target_location.$file_name."_backup".$filetype) && file_exists($location_csv.$file_name.$filetype)) ){
  3463.             // wenn kein backupfile existiert oder der importfile neuer als der backupfile ist importiere sql
  3464.             copy($location_csv.$file_name.$filetype,$target_location.$file_name.$filetype);
  3465.         }else{
  3466.             $sql "INSERT INTO pixel_dates (FunctionName,StartEnde,UpdateDatetime)
  3467.                     VALUES(?,?,?)";
  3468.             $stmtt $conn_shopware->prepare($sql);
  3469.             $stmtt->execute(array( 
  3470.                 "sql_import",
  3471.                 "ENDE",
  3472.                 date('Y-m-d H:i:s')
  3473.             ));
  3474.             echo "Keine (neue) Datei vorhanden!";
  3475.             exit;
  3476.         }
  3477.         if(file_exists($target_location.$file_name.$filetype) ){
  3478.             
  3479.             $sql "TRUNCATE TABLE artikeldaten";
  3480.             $stmt $conn_akn->prepare($sql);
  3481.             $stmt->execute();
  3482.             $excel_file $target_location.$file_name.$filetype;
  3483.         }else{
  3484.             $sql "INSERT INTO pixel_dates (FunctionName,StartEnde,UpdateDatetime)
  3485.                     VALUES(?,?,?)";
  3486.             $stmtt $conn_shopware->prepare($sql);
  3487.             $stmtt->execute(array( 
  3488.                 "sql_import",
  3489.                 "ENDE",
  3490.                 date('Y-m-d H:i:s')
  3491.             ));
  3492.             echo "Datei nicht vorhanden!";
  3493.             exit;
  3494.         }
  3495.         if( file_exists($excel_file)) {
  3496.             $excel_import file_get_contents($excel_file);
  3497.             $lines array_map("rtrim"explode("\n"$excel_import));
  3498.             foreach($lines as $key => $query) {
  3499.                 if($key == 0|| $key == 1){
  3500.                     continue;
  3501.                 }
  3502.                 if( trim($query) != '') {
  3503.                     $line explode(';',$query);
  3504.                     for ($i=4$i <13 $i++) { 
  3505.                         if($line[$i]=="j"){
  3506.                             $line[$i]=1;
  3507.                         }elseif($line[$i]=="n"){
  3508.                             $line[$i]=0;
  3509.                         }else{
  3510.                             $line[$i]=2;
  3511.                         }
  3512.                     }
  3513.                     if($line[17]=="x"){
  3514.                         $line[17]=1;
  3515.                     }else{
  3516.                         $line[17]=0;
  3517.                     }
  3518.                     try{   
  3519.                         $sql "INSERT INTO artikeldaten (material,material_group,innen_kueche,innen_fenster,innen_bad,innen_fussboden,innen_boden,aussen_boden,aussen_fassade,aussen_fenster,aussen_stufen,aussen_salz,bestellbar)
  3520.                                 VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)";
  3521.                         $stmtt $conn_akn->prepare($sql);
  3522.                         $stmtt->execute(array( 
  3523.                             $line[2],
  3524.                             $line[0],
  3525.                             $line[4],
  3526.                             $line[5],
  3527.                             $line[6],
  3528.                             $line[7],
  3529.                             $line[8],
  3530.                             $line[9],
  3531.                             $line[10],
  3532.                             $line[11],
  3533.                             $line[12],
  3534.                             $line[13],
  3535.                             $line[17]
  3536.                         ));
  3537.                         # TODO: CATCH ERRORS
  3538.                     } catch (RequestException $e) {
  3539.                         $sql "INSERT INTO pixel_dates (FunctionName,StartEnde,UpdateDatetime)
  3540.                                 VALUES(?,?,?)";
  3541.                         $stmtt $conn_shopware->prepare($sql);
  3542.                         $stmtt->execute(array( 
  3543.                             "csv_import FEHLER AUFGETRETEN IN ZEILE ".$key." DER SQL",
  3544.                             "ERROR",
  3545.                             date('Y-m-d H:i:s')
  3546.                         ));
  3547.                         $this->restore_excel();
  3548.                         if ($e->hasResponse()) {
  3549.                             $email = (new Email())
  3550.                             ->from('mo@pixelproductions.de')
  3551.                             ->to('mo@pixelproductions.de')
  3552.                             //->cc('cc@example.com')
  3553.                             //->bcc('bcc@example.com')
  3554.                             //->replyTo('fabien@example.com')
  3555.                             //->priority(Email::PRIORITY_HIGH)
  3556.                             ->subject('Fehler beim SQL Import bei AKN')
  3557.                             ->text('Es ist ein Fehler aufgetreten.')
  3558.                             ->html('Beim SQL-Import ist ein Fehler aufgetreten.<br>
  3559.                             Wann? '.date('Y-m-d H:i:s').' (Serverzeit)<br>
  3560.                             letzte Backupdatei wird eingespielt.<br>
  3561.                             Fehler:<br>'.Psr7\str($e->getResponse()));
  3562.                             
  3563.                             $mailer->send($email);
  3564.                         }
  3565.                         exit;
  3566.                     }
  3567.                 }
  3568.             }
  3569.         }
  3570.         //file umbenennen nach erfolgreichem einspielen
  3571.         rename($target_location.$file_name.$filetype,$target_location.$file_name."_backup".$filetype);
  3572.         $sql "INSERT INTO pixel_dates (FunctionName,StartEnde,UpdateDatetime)
  3573.                 VALUES(?,?,?)";
  3574.         $stmtt $conn_shopware->prepare($sql);
  3575.         $stmtt->execute(array( 
  3576.             "csv_import",
  3577.             "ENDE",
  3578.             date('Y-m-d H:i:s')
  3579.         ));
  3580.         # DONE
  3581.         $response = new Response(json_encode(array('success' => true)));
  3582.         $response->headers->set('Content-Type''application/json');
  3583.         return $response;
  3584.         return $this->render('update/index.html.twig', ["return" => ['success' => "CSV importiert" ]]);
  3585.         
  3586.     }
  3587.     /**
  3588.      * Stellt den Stand der SQL wieder her
  3589.      */
  3590.     public function restore_sql()
  3591.     {
  3592.         $this->client_connect();
  3593.         $conn_akn $this->dump_connect();
  3594.         $conn_shopware $this->shop_connect();
  3595.         $sql "INSERT INTO pixel_dates (FunctionName,StartEnde,UpdateDatetime)
  3596.                 VALUES(?,?,?)";
  3597.         $stmtt $conn_shopware->prepare($sql);
  3598.         $stmtt->execute(array( 
  3599.             "restore_sql",
  3600.             "Start",
  3601.             date('Y-m-d H:i:s')
  3602.         ));
  3603.         $location $this->getParameter('kernel.project_dir')."/public/sql/";
  3604.         $file_name "import";
  3605.         // $file_name = "/import_backup.sql";
  3606.         if(file_exists($location.$file_name."_backup.sql")){
  3607.             $sql_import file_get_contents($location.$file_name."_backup.sql");
  3608.             $sql_import str_replace(["\n""\r"], ""$sql_import);
  3609.             // $sql_import = str_replace("`", "", $sql_import);
  3610.             $sql_import explode(';'$sql_import);
  3611.             
  3612.             foreach($sql_import as $key => $query) {
  3613.                 if( trim($query) != '') {
  3614.                     try{   
  3615.                         # SKIP
  3616.                         if( str_starts_withstrtolowertrim$query ) ), 'use' )) continue;
  3617.                         $sql $query.';';
  3618.                         $stmt $conn_akn->prepare($sql);
  3619.                         $stmt->execute();
  3620.                         // $data = $stmt->fetchAll();
  3621.                     } catch (RequestException $e) {
  3622.                         //lade alte sql wieder rein
  3623.                         $sql "INSERT INTO pixel_dates (FunctionName,StartEnde,UpdateDatetime)
  3624.                                 VALUES(?,?,?)";
  3625.                         $stmtt $conn_shopware->prepare($sql);
  3626.                         $stmtt->execute(array( 
  3627.                             "restore_sql FEHLER BEIM SQL WIEDERHERSTELLEN IN ZEILE ".$key." DER SQL",
  3628.                             "ERROR",
  3629.                             date('Y-m-d H:i:s')
  3630.                         ));
  3631.                         if ($e->hasResponse()) {
  3632.                             // sende errer als mailtext
  3633.                             $nachricht Psr7\str($e->getResponse());
  3634.                             $nachricht wordwrap($nachricht70"\r\n");
  3635.                             mail('mo@pixelproductions.de''AKN SQL RESTORE ERROR'$nachricht);
  3636.                         }
  3637.                         // exit;
  3638.                     }
  3639.                 }
  3640.             }
  3641.             // dd("no copy!");
  3642.             $sql "INSERT INTO pixel_dates (FunctionName,StartEnde,UpdateDatetime)
  3643.                                 VALUES(?,?,?)";
  3644.                         $stmtt $conn_shopware->prepare($sql);
  3645.                         $stmtt->execute(array( 
  3646.                             "restore_sql",
  3647.                             "ENDE",
  3648.                             date('Y-m-d H:i:s')
  3649.                         ));
  3650.             //file umbenennen nach erfolgreichem einspielen
  3651.             copy($location.$file_name."_backup.sql",$location.$file_name.".sql");
  3652.         }
  3653.     }
  3654.     /**
  3655.      * Stellt den Stand der CSV wieder her
  3656.      */
  3657.     public function restore_excel()
  3658.     {
  3659.         $this->client_connect();
  3660.         $conn_akn $this->dump_connect();
  3661.         $conn_shopware $this->shop_connect();
  3662.         $sql "INSERT INTO pixel_dates (FunctionName,StartEnde,UpdateDatetime)
  3663.                 VALUES(?,?,?)";
  3664.         $stmtt $conn_shopware->prepare($sql);
  3665.         $stmtt->execute(array( 
  3666.             "restore_excel",
  3667.             "Start",
  3668.             date('Y-m-d H:i:s')
  3669.         ));
  3670.         $location $this->getParameter('kernel.project_dir')."/public/excel/";
  3671.         $file_name "Artikeldaten";
  3672.         $filetype ".csv";
  3673.         // $file_name = "/import_backup.sql";
  3674.         if(file_exists($location.$file_name."_backup".$filetype)){
  3675.             $excel_import file_get_contents($location.$file_name."_backup".$filetype);
  3676.             $lines array_map("rtrim"explode("\n"$excel_import));
  3677.             foreach($lines as $key => $query) {
  3678.                 if($key == 0|| $key == 1){
  3679.                     continue;
  3680.                 }
  3681.                 if( trim($query) != '') {
  3682.                     $line explode(';',$query);
  3683.                     for ($i=4$i <13 $i++) { 
  3684.                         if($line[$i]=="j"){
  3685.                             $line[$i]=1;
  3686.                         }elseif($line[$i]=="n"){
  3687.                             $line[$i]=0;
  3688.                         }else{
  3689.                             $line[$i]=2;
  3690.                         }
  3691.                     }
  3692.                     try{   
  3693.                         $sql "INSERT INTO artikeldaten (material,material_group,innen_kueche,innen_fenster,innen_bad,innen_fussboden,innen_boden,aussen_boden,aussen_fassade,aussen_fenster,aussen_stufen,aussen_salz)
  3694.                                 VALUES(?,?,?,?,?,?,?,?,?,?,?,?)";
  3695.                         $stmtt $conn_akn->prepare($sql);
  3696.                         $stmtt->execute(array( 
  3697.                             $line[2],
  3698.                             $line[0],
  3699.                             $line[4],
  3700.                             $line[5],
  3701.                             $line[6],
  3702.                             $line[7],
  3703.                             $line[8],
  3704.                             $line[9],
  3705.                             $line[10],
  3706.                             $line[11],
  3707.                             $line[12],
  3708.                             $line[13]
  3709.                         ));
  3710.                         # TODO: CATCH ERRORS
  3711.                         } catch (RequestException $e) {
  3712.                             $sql "INSERT INTO pixel_dates (FunctionName,StartEnde,UpdateDatetime)
  3713.                                     VALUES(?,?,?)";
  3714.                             $stmtt $conn_shopware->prepare($sql);
  3715.                             $stmtt->execute(array( 
  3716.                                 "restore_excel FEHLER BEIM CSV WIEDERHERSTELLEN IN ZEILE ".$key." DER CSV",
  3717.                                 "ERROR",
  3718.                                 date('Y-m-d H:i:s')
  3719.                             ));
  3720.                             exit;
  3721.                         }
  3722.                     }
  3723.                     }
  3724.             // dd("no copy!");
  3725.             $sql "INSERT INTO pixel_dates (FunctionName,StartEnde,UpdateDatetime)
  3726.                                 VALUES(?,?,?)";
  3727.                         $stmtt $conn_shopware->prepare($sql);
  3728.                         $stmtt->execute(array( 
  3729.                             "restore_excel",
  3730.                             "ENDE",
  3731.                             date('Y-m-d H:i:s')
  3732.                         ));
  3733.             //file umbenennen nach erfolgreichem einspielen
  3734.             copy($location.$file_name."_backup".$filetype,$location.$file_name.$filetype);
  3735.         }
  3736.     }
  3737.      /**
  3738.      * Löscht alle doppelten Bildverknüpfungen in den Artikeln und behält die Neuste
  3739.      * 
  3740.      * @Route("/clean/pictures", name="clean_pictures")
  3741.      * 
  3742.      * @return mixed
  3743.      */
  3744.     public function clean_pictures(){
  3745.         $conn_shopware $this->shop_connect();
  3746.         $sql "SELECT *, count(id) as count, max(created_at) as maxi, lower(hex(product_id)) as product_id, lower(hex(media_id)) as media_id
  3747.             FROM product_media
  3748.             GROUP BY product_id, media_id
  3749.             ORDER BY count(id) DESC
  3750.             ;";
  3751.         $stmt $conn_shopware->prepare($sql);
  3752.         $stmt->execute();
  3753.         $articles $stmt->fetchAll();
  3754.         // return $this->render('update/index.html.twig', ["return" => ['such123e' => $articles[0] ]]);
  3755.         foreach($articles as $article){
  3756.             if($article["count"]>1){
  3757.                 $sql "DELETE
  3758.                 FROM product_media
  3759.                 WHERE lower(hex(product_id))= '".$article["product_id"]."'
  3760.                 AND lower(hex(media_id))= '".$article["media_id"]."'
  3761.                 AND created_at != '".$article["maxi"]."'
  3762.                 ;";
  3763.                 $stmt $conn_shopware->prepare($sql);
  3764.                 $stmt->execute();
  3765.                 $articl $stmt->fetchAll();
  3766.                 // return $this->render('update/index.html.twig', ["return" => ['su4che' => $articl ]]);
  3767.             }else{
  3768.                 return $this->render('update/index.html.twig', ["return" => ['cleanup' => "done"]]);
  3769.             }
  3770.         }
  3771.         return $this->render('update/index.html.twig', ["return" => ['cleanup' => "done"]]);
  3772.     }
  3773.      /**
  3774.      * Entfernt doppelte Bildverlinkungen in Artikeln
  3775.      * 
  3776.      * @Route("/remove/duplicate", name="remove_duplicate")
  3777.      * 
  3778.      * @return mixed
  3779.      */
  3780.     public function remove_duplicate()
  3781.     {
  3782.         $this->client_connect();
  3783.         $conn_shopware $this->shop_connect();
  3784.         
  3785.         $sql "SELECT *, count(id) as count, max(created_at) as maxi, lower(hex(product_id)) as product_id, lower(hex(media_id)) as media_id
  3786.                 FROM product_media
  3787.                 GROUP BY product_id, media_id
  3788.                 ORDER BY count(id) DESC
  3789.                 ;";
  3790.         $stmt $conn_shopware->prepare($sql);
  3791.         $stmt->execute();
  3792.         $articles $stmt->fetchAll();
  3793.         
  3794.         // return $this->render('update/index.html.twig', ["return" => ['such123e' => $articles[0] ]]);
  3795.         foreach($articles as $article){
  3796.             if($article["count"]>1){
  3797.                 $sql "DELETE
  3798.                 FROM product_media
  3799.                 WHERE lower(hex(product_id))= '".$article["product_id"]."'
  3800.                 AND lower(hex(media_id))= '".$article["media_id"]."'
  3801.                 AND created_at != '".$article["maxi"]."'
  3802.                 ;";
  3803.                 $stmt $conn_shopware->prepare($sql);
  3804.                 $stmt->execute();
  3805.                 $articl $stmt->fetchAll();
  3806.                 // return $this->render('update/index.html.twig', ["return" => ['su4che' => $articl ]]);
  3807.             }else{
  3808.                 var_dump("done");exit;
  3809.             }
  3810.         }
  3811.         return $this->render('update/index.html.twig', ["return" => ['remove' => "done" ]]);
  3812.     }
  3813.     /**
  3814.      * test
  3815.      * 
  3816.      * @Route("/testcompare", name="testcompare")
  3817.      * 
  3818.      * @return mixed
  3819.      */
  3820.     public function testcompare()
  3821.     {
  3822.         $conn_shopware $this->shop_connect();
  3823.         $conn_akn $this->dump_connect();
  3824.         // $sql = "TRUNCATE TABLE storages_alt";
  3825.         // $stmt = $conn_akn->prepare($sql);
  3826.         // $stmt->execute();
  3827.         // $sql = "INSERT INTO storages_alt SELECT * FROM storages;";
  3828.         // $stmt = $conn_akn->prepare($sql);
  3829.         // $stmt->execute();
  3830.         // $sql = "SELECT count(id) from storages_alt;";
  3831.         // $stmt = $conn_akn->prepare($sql);
  3832.         // $stmt->execute();
  3833.         // $count = $stmt->fetchAll();
  3834.         // return $this->render('update/index.html.twig', ["return" => ['test' => $count ]]);
  3835.         //*** SQL IMPORT KOMPLETT
  3836.         // $sql = "SELECT 
  3837.         //    `kind`,article_id,materialgroup_id,material_id,surface_id,`length`,width,thickness,origin,stone_number,block_number,advice,storage,quantity_unit_1,quantity_unit_2,gross_length,gross_width,gross_thickness,amount_1,amount_2,reserved_amount_1,reserved_amount_2,articlegroup_id,fault_id,fault_corner,picture,restprice,price,block_advice,additional_advice 
  3838.         // FROM
  3839.         //  (
  3840.         //    SELECT 
  3841.         //    `kind`,article_id,materialgroup_id,material_id,surface_id,`length`,width,thickness,origin,stone_number,block_number,advice,storage,quantity_unit_1,quantity_unit_2,gross_length,gross_width,gross_thickness,amount_1,amount_2,reserved_amount_1,reserved_amount_2,articlegroup_id,fault_id,fault_corner,picture,restprice,price,block_advice,additional_advice 
  3842.         //    FROM storages
  3843.         //    UNION ALL
  3844.         //    SELECT
  3845.         //    `kind`,article_id,materialgroup_id,material_id,surface_id,`length`,width,thickness,origin,stone_number,block_number,advice,storage,quantity_unit_1,quantity_unit_2,gross_length,gross_width,gross_thickness,amount_1,amount_2,reserved_amount_1,reserved_amount_2,articlegroup_id,fault_id,fault_corner,picture,restprice,price,block_advice,additional_advice 
  3846.         //    FROM storages_alt
  3847.         // )  storages
  3848.         // GROUP BY 
  3849.         //    `kind`,article_id,materialgroup_id,material_id,surface_id,`length`,width,thickness,origin,stone_number,block_number,advice,storage,quantity_unit_1,quantity_unit_2,gross_length,gross_width,gross_thickness,amount_1,amount_2,reserved_amount_1,reserved_amount_2,articlegroup_id,fault_id,fault_corner,picture,restprice,price,block_advice,additional_advice 
  3850.         // HAVING COUNT(*) = 1
  3851.         //     ;";
  3852.         $sql "SELECT
  3853.         stone_number 
  3854.         FROM storages 
  3855.         WHERE 
  3856.         (`kind`,article_id,materialgroup_id,material_id,surface_id,`length`,width,thickness,origin,stone_number,block_number,advice,storage,quantity_unit_1,quantity_unit_2,gross_length,gross_width,gross_thickness,amount_1,amount_2,reserved_amount_1,reserved_amount_2,articlegroup_id,fault_id,fault_corner,picture,restprice,price,block_advice,additional_advice)
  3857.         NOT IN (SELECT 
  3858.         `kind`,article_id,materialgroup_id,material_id,surface_id,`length`,width,thickness,origin,stone_number,block_number,advice,storage,quantity_unit_1,quantity_unit_2,gross_length,gross_width,gross_thickness,amount_1,amount_2,reserved_amount_1,reserved_amount_2,articlegroup_id,fault_id,fault_corner,picture,restprice,price,block_advice,additional_advice
  3859.          FROM storages_alt)
  3860.          AND stone_number IS NOT NULL
  3861.          AND stone_number !=''
  3862.             ;";
  3863.         $stmt $conn_akn->prepare($sql);
  3864.         $stmt->execute();
  3865.         $changed_rows $stmt->fetchAll();
  3866.         return $this->render('update/index.html.twig', ["return" => ['test' => $changed_rows ]]);
  3867.         // $changed_rows = array_column($changed_rows, 'stone_number');
  3868.         // $sql = "UPDATE storages
  3869.         //         SET upload_done = 4 
  3870.         //         WHERE stone_number IN ('".implode("', '",$changed_rows)."');";
  3871.         // $stmt = $conn_akn->prepare($sql);
  3872.         // $stmt->execute();
  3873.         // // bei produktimport nimm die datensätze zuerst, die 4 dort gesestzt haben
  3874.         // $sql = "SELECT *
  3875.         //         FROM storages
  3876.         //         WHERE stone_number IN ('".implode("', '",$changed_rows)."');";
  3877.         // $stmt = $conn_akn->prepare($sql);
  3878.         // $stmt->execute();
  3879.         // $articles = $stmt->fetchAll();
  3880.   
  3881.     }
  3882.     public function getProductFinderCategory($verwendung$farbe$farbname){
  3883.         // nimmt verwendungs id und farb id, prüft ob kategorie im produktfinder existiert, erstellt diese wenn es nicht der Fall ist und returned ID oder Null
  3884.         
  3885.         if(empty(trim($verwendung)) || empty(trim($farbe)) ){
  3886.             return null
  3887.         }
  3888.         $this->client_connect();
  3889.         $conn_shopware $this->shop_connect();
  3890.         // $streamId = $this->checkProductStream($verwendung,$farbe, $farbname);
  3891.         // Kategorie: muss in 999967b27c834a0d897751a9e8517f8b (Produktfinder) enthalten sein
  3892.         // darf nicht 8cadf3490764413e90872546f312ee6a (Innen) oder f408046cfc9f4960926682177c055397 (außen) sein
  3893.         // suche über custom felder bei den kategorien... kombination farbe und verwendung
  3894.         // wenn nicht gefunden dann suche nur verwendung
  3895.             // wenn gefunden erstelle kategorie mit farbe und stream und returne id  der kategorie farbe+verwendung
  3896.             // wenn nicht gefunden returne null
  3897.         $sql "SELECT *
  3898.                 , LOWER(HEX(id)) as id
  3899.                 , LOWER(HEX(category_id)) as category_id
  3900.                 , LOWER(HEX(parent_id)) as parent_id
  3901.             FROM category
  3902.             INNER JOIN category_translation ON (category.id = category_translation.category_id)
  3903.             WHERE LOWER(HEX(language_id)) = '".$this->language_id."'
  3904.             AND JSON_EXTRACT(category_translation.custom_fields,'$.custom_color_id') = '".$farbe."'
  3905.             AND JSON_EXTRACT(category_translation.custom_fields,'$.custom_usage_id') = '".$verwendung."'
  3906.             ;";
  3907.         $stmt $conn_shopware->prepare($sql);
  3908.         $stmt->execute();
  3909.         $productCategory $stmt->fetchAll();
  3910.         // $sql = "SELECT name
  3911.         //     FROM category_translation
  3912.         //     WHERE LOWER(HEX(category_id)) = '".$productCategory[0]["parent_id"]."'
  3913.         //     ;";
  3914.         // $stmt = $conn_shopware->prepare($sql);
  3915.         // $stmt->execute();
  3916.         // $parentName = $stmt->fetchAll();
  3917.         
  3918.             $sql "SELECT *
  3919.                     , LOWER(HEX(id)) as id
  3920.                     , LOWER(HEX(category_id)) as category_id
  3921.                     , LOWER(HEX(parent_id)) as parent_id
  3922.                     , LOWER(HEX(product_stream_id)) as product_stream_id
  3923.                 FROM category
  3924.                 INNER JOIN category_translation ON (category.id = category_translation.category_id)
  3925.                 WHERE LOWER(HEX(language_id)) = '".$this->language_id."'
  3926.                 AND JSON_EXTRACT(category_translation.custom_fields,'$.custom_usage_id') = '".$verwendung."'
  3927.                 AND (JSON_EXTRACT(category_translation.custom_fields,'$.custom_color_id') = '' OR JSON_EXTRACT(category_translation.custom_fields,'$.custom_color_id') IS NULL)
  3928.                 ;";
  3929.             $stmt $conn_shopware->prepare($sql);
  3930.             $stmt->execute();
  3931.             $productFinderCategory $stmt->fetchAll();
  3932. // dd($verwendung,$productFinderCategory);
  3933.             $sql "SELECT name
  3934.                 FROM category_translation
  3935.                 WHERE LOWER(HEX(category_id)) = '".$productFinderCategory[0]["parent_id"]."'
  3936.                 ;";
  3937.             $stmt $conn_shopware->prepare($sql);
  3938.             $stmt->execute();
  3939.             $parentName $stmt->fetchAll();
  3940.             $streamId $this->checkProductStream($verwendung,$farbe$parentName[0]["name"].' '.$productFinderCategory[0]["name"].' '.$farbname);
  3941.         if(!is_array($productCategory) || empty($productCategory) || count($productCategory) < 1){ //prüfe auf usage id
  3942.             // dd($productFinderCategory);
  3943.             if(is_array($productFinderCategory) && !empty($productFinderCategory) && count($productFinderCategory) > 0){ //erstelle Kategorie
  3944.               
  3945.                 $body json_encode(array(
  3946.                     "name" => $farbname,
  3947.                     "type" => "page",
  3948.                     "customFields" => array(
  3949.                         "custom_color_id" => $farbe,
  3950.                         "custom_usage_id" => $verwendung
  3951.                     ),
  3952.                     "cmsPageId" => $this->material_category_layout_id,
  3953.                     "parentId" => $productFinderCategory[0]["id"],
  3954.                     "productStreamId" => $streamId,
  3955.                     "productAssignmentType" => "product_stream"
  3956.                 ));
  3957.     
  3958.                 try{
  3959.                     $post_product_stream $this->client->request('POST''/api/category/', array(
  3960.                         'allow_redirects'=>array('strict'=>true),
  3961.                         "headers" => $this->header,
  3962.                         "body" => $body
  3963.                     ));
  3964.                     $location array_pop$post_product_stream->getHeaders()['Location'] );
  3965.                     return basename($location);
  3966.                 } catch (RequestException $e) {
  3967.                     // echo Psr7\str($e->getRequest());
  3968.                     // if ($e->hasResponse()) {
  3969.                     //     echo Psr7\str($e->getResponse());
  3970.                     // }
  3971.                     if ($e->hasResponse()) {
  3972.                         $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  3973.                     }else{
  3974.                         $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  3975.                     }
  3976.                     $response->headers->set('Content-Type''application/json');
  3977.                     return $response;
  3978.                     exit;
  3979.                 }
  3980.               
  3981.               
  3982.               
  3983.               
  3984.             //     $CategoryFlat = new \App\Controller\shopware\CategoryFlat();
  3985.             //     $CategoryFlat->setType("page");
  3986.             //     $CategoryFlat->setName($farbname);
  3987.             //     $CategoryFlat->setCustomFields(array(
  3988.             //         "custom_color_id" => $farbe,
  3989.             //         "custom_usage_id" => $verwendung
  3990.             //     ));
  3991.             //     $CategoryFlat->setCmsPageId($this->material_category_layout_id); 
  3992.             //     $CategoryFlat->setParentId($productFinderCategory[0]["id"]);
  3993.             //     $CategoryFlat->setProductStreamId($streamId);
  3994.             //     $CategoryFlat->setProductAssignmentType("product_stream");
  3995.     
  3996.             //     $post_category = $this->client->request('POST', '/api/category', array(
  3997.             //         // 'debug' => true, 
  3998.             //         'allow_redirects'=>array('strict'=>true),
  3999.             //         "headers" => $this->header,
  4000.             //         "body" => $CategoryFlat,
  4001.             //     ));
  4002.             //     $location = array_pop( $post_category->getHeaders()['Location'] );
  4003.                 
  4004.                 return basename($location);
  4005.             }else{ // return null
  4006.                 return null;
  4007.             }
  4008.             // dd($mat_id);
  4009.         }else{ // Kategorie existiert, returne ID
  4010.         
  4011.             if($productCategory[0]["product_stream_id"] != $streamId){
  4012.             $body json_encode(array(
  4013.                 "productStreamId" => $streamId,
  4014.                 "productAssignmentType" => "product_stream"
  4015.             ));
  4016.             try{
  4017.                 $post_product_stream $this->client->request('PATCH''/api/category/'.$productCategory[0]["id"], array(
  4018.                     'allow_redirects'=>array('strict'=>true),
  4019.                     "headers" => $this->header,
  4020.                     "body" => $body
  4021.                 ));
  4022.                 $location array_pop$post_product_stream->getHeaders()['Location'] );
  4023.                 return basename($location);
  4024.             } catch (RequestException $e) {
  4025.                 // echo Psr7\str($e->getRequest());
  4026.                 // if ($e->hasResponse()) {
  4027.                 //     echo Psr7\str($e->getResponse());
  4028.                 // }
  4029.                 if ($e->hasResponse()) {
  4030.                     $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  4031.                 }else{
  4032.                     $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  4033.                 }
  4034.                 $response->headers->set('Content-Type''application/json');
  4035.                 return $response;
  4036.                 exit;
  4037.             }
  4038.                 // $CategoryFlat = new \App\Controller\shopware\CategoryFlat();
  4039.                 // $CategoryFlat->setProductStreamId($streamId);
  4040.                 // $CategoryFlat->setProductAssignmentType("product_stream");
  4041.                 // $post_category = $this->client->request('PATCH', '/api/category/'.$productCategory[0]["id"], array(
  4042.                 //     // 'debug' => true, 
  4043.                 //     'allow_redirects'=>array('strict'=>true),
  4044.                 //     "headers" => $this->header,
  4045.                 //     "body" => $CategoryFlat,
  4046.                 // ));
  4047.            }
  4048.             return $productCategory[0]["id"];
  4049.         }
  4050.         // bei post product stream missachten, apipatch durchführen
  4051.         return null;
  4052.     }
  4053.     public function checkProductStream($verwendung$farbe$streamname){
  4054.         // Prüft of product stream bereits vorhanden ist, erstellt ihn falls dies nicht der Fall ist und returned die ID oder NULL, falls es ein problem gibt
  4055.         $this->client_connect();
  4056.         $conn_shopware $this->shop_connect();
  4057.         $sql "SELECT *
  4058.             , LOWER(HEX(product_stream_id)) as product_stream_id
  4059.                 FROM product_stream_translation
  4060.                 WHERE JSON_EXTRACT(product_stream_translation.custom_fields,'$.custom_color_id') = '".$farbe."'
  4061.                 AND JSON_EXTRACT(product_stream_translation.custom_fields,'$.custom_usage_id') = '".$verwendung."'
  4062.                 LIMIT 1
  4063.                 ";
  4064.         $stmt $conn_shopware->prepare($sql);
  4065.         $stmt->execute();
  4066.         $product_stream $stmt->fetchAll();
  4067.         if(isset($product_stream) && !empty($product_stream) && count($product_stream) > 0){
  4068.             return $product_stream[0]["product_stream_id"];
  4069.         }else{
  4070.             $body json_encode(array(
  4071.                 "name" => $streamname,
  4072.                 "filters" => array(array(
  4073.                     "type" => "multi",
  4074.                     "queries" => array(array(
  4075.                         "type" => "multi",
  4076.                         "queries" => array(array(
  4077.                                 "type" => "equals",
  4078.                                 "field" => "product.properties.id",
  4079.                                 "value" => $verwendung
  4080.                             ),
  4081.                             array( "type" => "multi",
  4082.                                 "queries" => array(array(
  4083.                                     "type" => "equals",
  4084.                                     "field" => "product.properties.id",
  4085.                                     "value" => $farbe
  4086.                                 )),
  4087.                                 "operator" => "OR"
  4088.                             )
  4089.                         ),
  4090.                         "operator" => "AND"
  4091.                     )),
  4092.                     "operator" => "OR"
  4093.                 )),
  4094.                 "customFields" => array(
  4095.                     "custom_color_id" => $farbe,
  4096.                     "custom_usage_id" => $verwendung
  4097.                 ),
  4098.                 "apiAlias" => "product_stream"
  4099.             ));
  4100.             try{
  4101.                 $post_product_stream $this->client->request('POST''/api/product-stream/', array(
  4102.                     'allow_redirects'=>array('strict'=>true),
  4103.                     "headers" => $this->header,
  4104.                     "body" => $body
  4105.                 ));
  4106.                 $location array_pop$post_product_stream->getHeaders()['Location'] );
  4107.                 return basename($location);
  4108.             } catch (RequestException $e) {
  4109.                 // echo Psr7\str($e->getRequest());
  4110.                 // if ($e->hasResponse()) {
  4111.                 //     echo Psr7\str($e->getResponse());
  4112.                 // }
  4113.                 // if ($e->hasResponse()) {
  4114.                 //     $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  4115.                 // }else{
  4116.                 //     $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  4117.                 // }
  4118.                 // $response->headers->set('Content-Type', 'application/json');
  4119.                 // return $response;
  4120.                 // exit;
  4121.             }
  4122.         }
  4123.         return NULL;
  4124.     }
  4125.     public function createProductfinderList(){
  4126.         // erstellt Liste zum generieren der Produktfinderkategorien
  4127.         $conn_shopware $this->shop_connect();
  4128.         $conn_akn $this->dump_connect();
  4129.         $sql "TRUNCATE TABLE produktfinder_alt";
  4130.         $stmt $conn_akn->prepare($sql);
  4131.         $stmt->execute();
  4132.         $sql "INSERT INTO produktfinder_alt SELECT * FROM produktfinder;";
  4133.         $stmt $conn_akn->prepare($sql);
  4134.         $stmt->execute();
  4135.         $sql "TRUNCATE TABLE produktfinder";
  4136.         $stmt $conn_akn->prepare($sql);
  4137.         $stmt->execute();
  4138.     
  4139.         $sql "SELECT *
  4140.             , LOWER(HEX(category_id)) as category_id
  4141.             , JSON_EXTRACT(category_translation.custom_fields,'$.custom_usage_id') as verwendung
  4142.             FROM category_translation
  4143.             WHERE JSON_EXTRACT(category_translation.custom_fields,'$.custom_usage_id') IS NOT NULL
  4144.             AND JSON_EXTRACT(category_translation.custom_fields,'$.custom_usage_id') != ''
  4145.             AND (JSON_EXTRACT(category_translation.custom_fields,'$.custom_color_id') IS NULL
  4146.                 OR JSON_EXTRACT(category_translation.custom_fields,'$.custom_color_id') = '' )
  4147.             ";
  4148.         $stmt $conn_shopware->prepare($sql);
  4149.         $stmt->execute();
  4150.         $finderCategories $stmt->fetchAll();
  4151.         $sql "SELECT 
  4152.                 *
  4153.                 , LOWER(HEX(id)) as id
  4154.                 , LOWER(HEX(language_id)) as language_id
  4155.                 , LOWER(HEX(property_group_option_id)) as property_group_option_id
  4156.             FROM property_group_option
  4157.             INNER JOIN property_group_option_translation  ON (property_group_option.id = property_group_option_translation.property_group_option_id)
  4158.             WHERE LOWER(HEX(property_group_id)) = '".$this->color_id."'
  4159.             ;";
  4160.         $stmt $conn_shopware->prepare($sql);
  4161.         $stmt->execute();
  4162.         $property_colors $stmt->fetchAll();
  4163.         
  4164.         foreach($finderCategories as $finderCategory){
  4165.             foreach($property_colors as $property_color){
  4166.                 $sql "INSERT INTO produktfinder (category_id,color_id,Farbname)
  4167.                     VALUES ('".str_replace('"','',$finderCategory["verwendung"])."','".$property_color["property_group_option_id"]."','".$property_color["name"]."')
  4168.                 ;";
  4169.                 $stmt $conn_akn->prepare($sql);
  4170.                 $stmt->execute();
  4171.             }
  4172.         }
  4173.     }
  4174.     /**
  4175.      * Erstellt, updated und (de)aktiviert Kategorien im Produktfinder 
  4176.      * 
  4177.      * @Route("/update/productfinder", name="update_productfinder")
  4178.      * 
  4179.      * @return mixed
  4180.      */
  4181.     public function update_productfinder()
  4182.     {
  4183.         $this->client_connect();
  4184.         $conn_shopware $this->shop_connect();
  4185.         $conn_akn $this->dump_connect();
  4186.         // gehe alle farben mit allen kategorien durch und erstelle/prüfe kategorien
  4187.         // blende alle kategorien aus die keine zugeordneten produkte haben
  4188.         // return $this->render('update/index.html.twig', ["return" => [ 'article' => $finderCategories, 'color' => $property_colors]]);
  4189.         $sql "SELECT COUNT(id) as count
  4190.             FROM produktfinder
  4191.             ;";
  4192.         $stmt $conn_akn->prepare($sql);
  4193.         $stmt->execute();
  4194.         $productfinderCount $stmt->fetchAll();
  4195.         if($productfinderCount[0]["count"] == 0){
  4196.             $this->createProductfinderList();
  4197.         }
  4198.             $sql "SELECT *
  4199.                 FROM produktfinder
  4200.                 WHERE done IS NULL
  4201.                 ;";
  4202.             $stmt $conn_akn->prepare($sql);
  4203.             $stmt->execute();
  4204.             $productfinder $stmt->fetchAll();
  4205.         foreach($productfinder as $productfinder_row){
  4206.             // bearbeite und setze done auf 1
  4207.             $categoryId $this->getProductFinderCategory($productfinder_row["category_id"], $productfinder_row["color_id"],$productfinder_row["Farbname"]);
  4208.             $sql "SELECT *
  4209.                     , LOWER(HEX(id)) as id
  4210.                     , LOWER(HEX(product_stream_id)) as product_stream_id
  4211.                 FROM category
  4212.                 WHERE LOWER(HEX(id)) = '".$categoryId."'
  4213.                 LIMIT 1
  4214.                 ;";
  4215.             $stmt $conn_shopware->prepare($sql);
  4216.             $stmt->execute();
  4217.             $category $stmt->fetchAll();
  4218.             $streamId $category[0]["product_stream_id"];
  4219.             
  4220.             $sql " SELECT COUNT(*) AS count
  4221.             FROM product_stream_mapping
  4222.             WHERE LOWER(HEX(product_stream_id)) = '".$streamId."'
  4223.                 ;";
  4224.             $stmt $conn_shopware->prepare($sql);
  4225.             $stmt->execute();
  4226.             $stream_mapping $stmt->fetchAll();
  4227.             $CategoryFlat = new \App\Controller\shopware\CategoryFlat();
  4228.            
  4229.             if(!isset($stream_mapping[0]["count"]) || empty($stream_mapping[0]["count"]) || $stream_mapping[0]["count"] < 1){
  4230.                 // deaktiviere Kategorie wenn Produktgruppe leer ist
  4231.                 $CategoryFlat->setActive(false);
  4232.             }else{
  4233.                 $CategoryFlat->setActive(true);
  4234.             }
  4235.   
  4236.             try {
  4237.                 $post_category $this->client->request('PATCH''/api/category/'.$categoryId.'', array(
  4238.                     'allow_redirects'=>array('strict'=>true),
  4239.                     "headers" => $this->header,
  4240.                     "body" => $CategoryFlat
  4241.                 ));
  4242.             } catch (RequestException $e) {
  4243.                 if ($e->hasResponse()) {
  4244.                     $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  4245.                 }else{
  4246.                     $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  4247.                 }
  4248.                 $response->headers->set('Content-Type''application/json');
  4249.                 return $response;
  4250.                 exit;
  4251.             }
  4252.         
  4253.             $sqlt "UPDATE produktfinder SET
  4254.                     done = 1
  4255.             WHERE id = ".$productfinder_row["id"].";";
  4256.             $stmtt $conn_akn->prepare($sqlt);
  4257.             $stmtt->execute();
  4258.         }
  4259.         return $this->render('update/index.html.twig', ["return" => [ 'success' => true]]);
  4260.     }
  4261.     /**
  4262.      * test
  4263.      * 
  4264.      * @Route("/test", name="test")
  4265.      * 
  4266.      * @return mixed
  4267.      */
  4268.     public function test()
  4269.     { 
  4270.         //komprimierungstest
  4271.         // var_dump("test");exit;
  4272.         $this->client_connect();
  4273.         $conn_shopware $this->shop_connect();
  4274.         $conn_akn $this->dump_connect();
  4275.         // $conn_konfigurator = $this->konfig_connect();
  4276.         $sql "SELECT material,material_group
  4277.         FROM artikeldaten
  4278.         WHERE 
  4279.         (material,material_group,innen_kueche,innen_fenster,innen_bad,innen_fussboden,innen_boden,aussen_boden,aussen_fassade,aussen_fenster,aussen_stufen,aussen_salz,bestellbar
  4280.         )
  4281.         NOT IN (SELECT 
  4282.         material,material_group,innen_kueche,innen_fenster,innen_bad,innen_fussboden,innen_boden,aussen_boden,aussen_fassade,aussen_fenster,aussen_stufen,aussen_salz,bestellbar
  4283.         FROM artikeldaten_alt)
  4284.             ;";
  4285.         $stmt $conn_akn->prepare($sql);
  4286.         $stmt->execute();
  4287.         $changed_rows $stmt->fetchAll();
  4288.         return $this->render('update/index.html.twig', ["return" => ['test' => $changed_rows ]]);
  4289.         foreach($changed_rows as $row){
  4290.             // set priority to changed rows
  4291.             $sql "UPDATE storages
  4292.                     SET upload_done = 4 
  4293.                     WHERE material_id = '".$row["material"]."'
  4294.                     AND materialgroup_id = '".$row["material_group"]."'
  4295.                     ;";
  4296.             $stmt $conn_akn->prepare($sql);
  4297.             $stmt->execute();
  4298.         }
  4299.         // $body = json_encode(array(
  4300.         //     "name" => "test"
  4301.         //     // "sortBy" => "name",
  4302.         //     // "sortDirection" => "ASC",
  4303.         //     // "limit" => 24,
  4304.         //     // "active" => true
  4305.         //     // "type" => "productStream",
  4306.         // ));
  4307.         // $body = json_encode(array(
  4308.         //     "name" => "test",
  4309.         //     "filters" => array(
  4310.         //         array(
  4311.         //         "type" => "multi",
  4312.         //         "queries" => array(
  4313.         //             array(
  4314.         //             "type" => "multi",
  4315.         //             "queries" => array(
  4316.         //                 array(
  4317.         //                 "type" => "equals",
  4318.         //                 "field" => "product.properties.id",
  4319.         //                 "value" => "f00c5dcbc7e444f38e06ea9f14aaa11a" // INNEN/AUßEN XX:Ja
  4320.         //                 ),
  4321.         //                 array(
  4322.         //                     "type" => "multi",
  4323.         //                     "queries" => array(
  4324.         //                     array(
  4325.         //                         "type" => "equals",
  4326.         //                         "field" => "product.properties.id",
  4327.         //                         "value" => "8ce1035e43b84d8395788e9f8b22c2fa" //FARBE
  4328.         //                         )
  4329.         //                     ),
  4330.         //                     "operator" => "OR"
  4331.         //                 )
  4332.         //             ),
  4333.         //             "operator" => "AND"
  4334.         //             )
  4335.         //         ),
  4336.         //         "operator" => "OR"
  4337.         //         )
  4338.         //     ),
  4339.         //     "apiAlias" => "product_stream"
  4340.         // ));
  4341.         // try{
  4342.         //     $post_country = $this->client->request('POST', '/api/product-stream/', array(
  4343.         //         'allow_redirects'=>array('strict'=>true),
  4344.         //         "headers" => $this->header,
  4345.         //         "body" => $body
  4346.         //     ));
  4347.         // } catch (RequestException $e) {
  4348.         //     // echo Psr7\str($e->getRequest());
  4349.         //     // if ($e->hasResponse()) {
  4350.         //     //     echo Psr7\str($e->getResponse());
  4351.         //     // }
  4352.         //     if ($e->hasResponse()) {
  4353.         //         $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  4354.         //     }else{
  4355.         //         $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  4356.         //     }
  4357.         //     $response->headers->set('Content-Type', 'application/json');
  4358.         //     return $response;
  4359.         //     exit;
  4360.         // }
  4361.         // try {
  4362.         //     $search = $this->client->request('GET', '/api/product-stream', array( 
  4363.         //         "headers" => $this->header,
  4364.         //     ));
  4365.         //     $categories = json_decode($search->getBody()->getContents(),true);
  4366.         // } catch (RequestException $e) {
  4367.         //     // echo Psr7\str($e->getRequest());
  4368.         //     // if ($e->hasResponse()) {
  4369.         //     //         echo Psr7\str($e->getResponse());
  4370.         //     //     }
  4371.         //     if ($e->hasResponse()) {
  4372.         //         $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  4373.         //     }else{
  4374.         //         $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  4375.         //     }
  4376.         //     $response->headers->set('Content-Type', 'application/json');
  4377.         //     return $response;
  4378.         //     exit;
  4379.         // }
  4380.         // try {
  4381.         //     $search = $this->client->request('GET', '/api/category?filter[name]=testkat', array( 
  4382.         //         "headers" => $this->header,
  4383.         //     ));
  4384.         // } catch (RequestException $e) {
  4385.         //     // echo Psr7\str($e->getRequest());
  4386.         //     // if ($e->hasResponse()) {
  4387.         //     //         echo Psr7\str($e->getResponse());
  4388.         //     //     }
  4389.         //     if ($e->hasResponse()) {
  4390.         //         $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  4391.         //     }else{
  4392.         //         $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  4393.         //     }
  4394.         //     $response->headers->set('Content-Type', 'application/json');
  4395.         //     return $response;
  4396.         //     exit;
  4397.         // }
  4398.         // $categories = json_decode($search->getBody()->getContents(),true);
  4399.         
  4400.         $verwendung "f00c5dcbc7e444f38e06ea9f14aaa11a";
  4401.         $farbe "8ce1035e43b84d8395788e9f8b22c2fa";
  4402.         $categories $this->getProductFinderCategory($verwendung$farbe"TESTGRAU");
  4403.     
  4404.     return $this->render('update/index.html.twig', ["return" => [ 'article' => $categories]]);
  4405.         //____________________________________________________________________________
  4406.     //     $sql = "SELECT 
  4407.     //             LOWER(HEX(id)) as id
  4408.     //             ,product_number
  4409.     //             ,stock
  4410.     //         FROM product
  4411.     //         ORDER BY product_number asc
  4412.     //         ;";
  4413.     //     $stmt = $conn_shopware->prepare($sql);
  4414.     //     $stmt->execute();
  4415.     //     $search_product = $stmt->fetchAll();
  4416.     //     $sql = "SELECT 
  4417.     //     id
  4418.     //     ,stone_number
  4419.     //     ,amount_1
  4420.     //     FROM storages
  4421.     //     WHERE stone_number IN ('".implode("', '",array_column($search_product, 'product_number'))."')
  4422.     //     ORDER BY stone_number asc
  4423.     //     ;";
  4424.     // $stmt = $conn_akn->prepare($sql);
  4425.     // $stmt->execute();
  4426.     // $articles = $stmt->fetchAll();
  4427.     
  4428.     return $this->render('update/index.html.twig', ["return" => [ 'article' => $this->header]]);
  4429.     try {
  4430.         $search $this->client->request('GET''/api/category', array( 
  4431.             "headers" => $this->header,
  4432.         ));
  4433.     } catch (RequestException $e) {
  4434.         // echo Psr7\str($e->getRequest());
  4435.         // if ($e->hasResponse()) {
  4436.         //         echo Psr7\str($e->getResponse());
  4437.         //     }
  4438.         if ($e->hasResponse()) {
  4439.             $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  4440.         }else{
  4441.             $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  4442.         }
  4443.         $response->headers->set('Content-Type''application/json');
  4444.         return $response;
  4445.         exit;
  4446.     }
  4447. return $this->render('update/index.html.twig', ["return" => [ 'article' => $search]]);
  4448.         return $this->render('update/index.html.twig', ["return" => ['test' => 'test' ]]);
  4449.         $sql "SELECT *
  4450.                 , articles.material_id as material_id
  4451.                 , articles.materialgroup_id as materialgroup_id
  4452.                 , storages.id as storage_id
  4453.                 , storages.stone_number AS product_number
  4454.                 , CONCAT(storages.material_id, '_', storages.materialgroup_id, '_') AS product_number_parent
  4455.                 , storages.picture AS storages_picture
  4456.                 , materials.picture AS materials_picture
  4457.                 , COALESCE(NULLIF(materials.picture, ''), 'default_art.jpg') AS picture_parent   -- select picture
  4458.                 , key2.key_text AS einheit_2
  4459.             FROM articles
  4460.             LEFT JOIN materials ON (articles.material_id = materials.material_id AND articles.materialgroup_id = materials.materialgroup_id )
  4461.             LEFT JOIN articlegroups ON (articles.articlegroup_id = articlegroups.articlegroup_id)
  4462.             LEFT JOIN materialgroups ON (materialgroups.materialgroup_id = articles.materialgroup_id)
  4463.             LEFT JOIN surfaces ON (surfaces.surface_id = articles.surface_id AND surfaces.materialgroup_id = articles.materialgroup_id)
  4464.             INNER JOIN storages ON (articles.article_id = storages.article_id)
  4465.             LEFT JOIN storagefaults ON (storages.fault_id = storagefaults.fault_id)
  4466.             LEFT JOIN materialcolors  ON (articles.material_id = materialcolors.mar_id AND articles.materialgroup_id = materialcolors.mgr_id )
  4467.             LEFT JOIN `keys` key1 ON (materialcolors.color_id = key1.key_number AND key1.key_kind = 88)
  4468.             LEFT JOIN `keys` key2 ON (materialcolors.color_id = key2.key_number AND key2.key_kind = 1)
  4469.             WHERE storages.stone_number IS NOT NULL
  4470.             AND storages.stone_number !=''
  4471.             AND storages.storage != 'ZULAUF'
  4472.             -- AND storages.upload_done IS NULL
  4473.             AND storages.upload_failed IS NULL
  4474.             AND material_name IS NOT NULL
  4475.             -- AND block_advice IS NOT NULL
  4476.             -- AND block_advice != ''
  4477.             -- AND articles.material_id = 'NEIM'
  4478.             AND articles.material_id = 'CAMI'
  4479.             ORDER BY articles.id ASC
  4480.             ;";
  4481.         $stmt $conn_akn->prepare($sql);
  4482.         $stmt->execute();
  4483.         $articles $stmt->fetchAll();
  4484.         $vergleichsarray=array();
  4485.         $testarray=array();
  4486.         $parentarray=array();
  4487.         foreach($articles as $key => $article){
  4488.             $blocknummer=null;
  4489.             $blocknummer_zerlegt=array();
  4490.             preg_match('/([a-zA-Z]+)([0-9,]+)([a-zA-Z0-9,]+)/'$article["block_advice"], $blocknummer_zerlegt);
  4491.             // var_dump($blocknummer_zerlegt);
  4492.             if(strlen(end($blocknummer_zerlegt))>1){
  4493.                 $blocknummer end($blocknummer_zerlegt);
  4494.             }
  4495.             if($blocknummer == null){
  4496.                 $article["product_number_parent"].=$article["article_id"];
  4497.                 $articles[$key]["product_number_parent"].=$article["article_id"];
  4498.             }else{
  4499.                 $article["product_number_parent"].=$blocknummer;
  4500.                 $articles[$key]["product_number_parent"].=$blocknummer;
  4501.             }
  4502.             $sql "SELECT 
  4503.                     LOWER(HEX(id)) as id
  4504.                     ,product_number
  4505.                 FROM product
  4506.                 JOIN product_category ON (product_id = id)
  4507.                 WHERE product_number = '".$article["product_number_parent"]."'
  4508.                 LIMIT 1
  4509.                 ;";
  4510.             $stmt $conn_shopware->prepare($sql);
  4511.             $stmt->execute();
  4512.             $search_product $stmt->fetchAll();
  4513.             array_push($vergleichsarray,$article["product_number_parent"]);
  4514.             if(count($search_product)>&& !empty($search_product)){
  4515.                 // dd($search_product);
  4516.                 array_push($testarray,$search_product[0]["product_number"]);
  4517.                 $parentarray[$search_product[0]["product_number"]]=$search_product[0]["id"];
  4518.             }
  4519.         }
  4520.             $zuErstellendeParents array_diff($vergleichsarray$testarray);
  4521.             if(empty($zuErstellendeParents)){
  4522.                 //alle varianten können importiert werden
  4523.                 $importjson=array();
  4524.                 foreach($articles as $article){
  4525.                 // dd($article["product_number_parent"]);
  4526.                     unset($custom_fields);
  4527.                     if( $article["fault_id"] != && !empty($article["fault_id"]) && $article["fault_corner"] != && !empty($article["fault_corner"])){
  4528.                         //prüfe ob beides gesetzt, wenn ja definier beides, wenn nicht beides leer
  4529.                         $custom_fields["custom_article_fault_id"] = $article["fault_name"];
  4530.                         $custom_fields["custom_article_fault_corner"] = $article["fault_corner"];
  4531.                         $custom_fields["custom_article_fault_exists"] = 1;
  4532.                     }else{
  4533.                         $custom_fields["custom_article_fault_exists"] = 0;
  4534.                     }
  4535.                     $custom_fields=array();
  4536.                     $custom_fields["custom_article_id"] = $article["article_id"];
  4537.                     $custom_fields["custom_article_material_id"] = $article["material_id"];
  4538.                     $custom_fields["custom_article_materialgroup_id"] = $article["materialgroup_id"];
  4539.                     $custom_fields["custom_article_material_name"] = $article["material_name"];
  4540.                     $custom_fields["custom_article_materialgroup_name"] = $article["materialgroup_name"];
  4541.                     $custom_fields["custom_article_surface"] = $article["surface_name"]; 
  4542.                     $custom_fields["custom_article_stone_number"] = $article["stone_number"];
  4543.                     $custom_fields["custom_article_block_number"] = $article["block_advice"];
  4544.                     $custom_fields["custom_article_search_word"] = $article["search_word"];
  4545.                     $custom_fields["custom_article_advice"] = $article["advice"];
  4546.                     $custom_fields["custom_article_storage"] = $article["storage"];
  4547.                     $custom_fields["custom_article_amount_2"] = $article["amount_2"];
  4548.                     $custom_fields["custom_article_einheit_2"] = $article["einheit_2"];
  4549.                     $custom_fields["custom_article_articlegroup_name"] = $article["articlegroup_name"];
  4550.                     $custom_fields["custom_article_fault_id"] = $article["fault_name"];
  4551.                     $custom_fields["custom_article_fault_corner"] = $article["fault_corner"];
  4552.                     $custom_fields["custom_article_name_1"] = $article["article_name_1"];
  4553.                     $custom_fields["custom_article_name_2"] = $article["article_name_2"];
  4554.                     $custom_fields["custom_article_additional_advice"] = $article["additional_advice"];
  4555.                     if(isset($article["storages_picture"]) && !empty($article["storages_picture"]) && $article["storages_picture"] != null){
  4556.                         $custom_fields["custom_article_picture"] = $article["storages_picture"];
  4557.                     }elseif(isset($storags["materials_picture"]) && !empty($article["materials_picture"]) && $article["materials_picture"] != null){
  4558.                         $custom_fields["custom_article_picture"] = $article["materials_picture"];
  4559.                     }else{ 
  4560.                         $custom_fields["custom_article_picture"] = $article["picture_parent"];
  4561.                     }
  4562.                     if(isset($article["color_text"]) && !empty($article["color_text"])){
  4563.                         $color explode('#',$article["color_text"]);
  4564.                         $article["color_text"] = trim($color[0]);
  4565.                         $custom_fields["custom_article_material_color"] = $article["color_text"];
  4566.                     }
  4567.                     $storage_option_list=array();
  4568.                     $storage_option_list $this->get_variant_ids($article);
  4569.                     $sol_option=array();
  4570.                     foreach($storage_option_list as $key => $sol){
  4571.                         $sol_option[$key]["id"]=$sol["optionId"];
  4572.                     }
  4573. // dd($sol_option);
  4574.                     $variant =[   
  4575.                         // "id"=> "393eb570026d4076a1ceaf6fe48bdd05", //patch
  4576.                         "name" => $article["material_name"],
  4577.                         "parentId" => $parentarray[$article["product_number_parent"]],
  4578.                         "productNumber"=> $article["stone_number"],
  4579.                         "stock"=> intval($article["amount_1"]),
  4580.                         "isCloseout"=> true,
  4581.                         "height" => $article["thickness"],
  4582.                         "length" => $article["length"],
  4583.                         "width"=> $article["width"],
  4584.                         "price"=> array(array(
  4585.                             "currencyId" => $this->currency_id,
  4586.                             "net" =>  round($article["price"],2),
  4587.                             "gross" => round($article["price"],2),
  4588.                             "linked" => false
  4589.                         )),
  4590.                         // TODO OPTIONS
  4591.                         // PROPERTIES
  4592.                         "options" => $sol_option,
  4593.                         "properties" => $sol_option,
  4594.                         // "options" => array(array(
  4595.                         //     "id" => "dee3b8bac6fe48a094f7abf465a6c981",
  4596.                         //     "group" => array(
  4597.                         //         "id" => "8f9f42eddaa843bba8baa0ec33b2d6ed"
  4598.                         //     )
  4599.                         // )),
  4600.                         "customFields" => $custom_fields
  4601.                         // array(
  4602.                         //     "custom_article_surface" => "test",
  4603.                         //     "custom_article_name_1" => "name",
  4604.                         //     "custom_article_material_name" => "mati",
  4605.                         //     "custom_article_picture" => "default_art.jpg"
  4606.                         // ),
  4607.                         // ende patch - nachfolgende nur bei parents gesetzt
  4608.                         // "categories"=> array(
  4609.                         //     "data"=>array(
  4610.                         //         "type" => "category",
  4611.                         //         "id" => "c7446ea908604e06a8f28a6c42fd1171",
  4612.                         //     )
  4613.                         // ),
  4614.                         // "taxId"=> "c5ddfa75e22b4889b6f628e1df0b347f",
  4615.                         // VISIBILITY WIRFT FEHLER WENN BEREITS GESETZT BEIM PATCH -> NUR SETZEN WENN ID LEER IST (PRODUKT NOCH NICHT EXISTIERT)
  4616.                         // "visibilities" => array(
  4617.                         //     array(
  4618.                         //         "productId" => "393eb570026d4076a1ceaf6fe48bdd05",
  4619.                         //         "salesChannelId" => "f8c383f75225488a8f6b89b7d64d6cf2",
  4620.                         //         "visibility" => 30
  4621.                         //     )
  4622.                         // ),
  4623.                         // ,"children" => array($variante)
  4624.             
  4625.                     ];
  4626.                     $sql "SELECT 
  4627.                             LOWER(HEX(id)) as id
  4628.                             ,product_number
  4629.                         FROM product
  4630.                         WHERE product_number = '".$article["stone_number"]."'
  4631.                         LIMIT 1
  4632.                         ;";
  4633.                     $stmt $conn_shopware->prepare($sql);
  4634.                     $stmt->execute();
  4635.                     $search_variant $stmt->fetchAll();
  4636.                     // var_dump($article["stone_number"]);
  4637.                     // var_dump($search_variant);
  4638.                     if(isset($search_variant[0]["id"]) && !empty($search_variant[0]["id"])){
  4639.                         $variant["id"]=$search_variant[0]["id"];
  4640.                     }
  4641.                 array_push($importjson,$variant);
  4642.                 }
  4643.                 try {
  4644.                     // dd($importjson);
  4645.                     $search $this->client->request('POST''/api/_action/sync', array( 
  4646.                     // $search = $this->client->request('GET', '/api/media/6d98e8d9d2184172a5acc4a32095e843', array( 
  4647.                     "debug" => true,
  4648.                     "headers" => $this->header,
  4649.                     "body" => json_encode([
  4650.                         array(
  4651.                         "action"=> "upsert",
  4652.                         "entity"=> "product",
  4653.                         // im payload mit kommas getrennt datensätze von parents mit integrierten childs angeben
  4654.                         "payload"=> 
  4655.                         $importjson
  4656.                         //   array(
  4657.                         //         $parent
  4658.                         //     // z.b.    ,$parent2
  4659.                         //     )
  4660.                         )
  4661.                         ])
  4662.                     ));
  4663.                 } catch (RequestException $e) {
  4664.                     // echo Psr7\str($e->getRequest());
  4665.                     // if ($e->hasResponse()) {
  4666.                     //     echo Psr7\str($e->getResponse());
  4667.                     // }
  4668.                     if ($e->hasResponse()) {
  4669.                         $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  4670.                     }else{
  4671.                         $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  4672.                     }
  4673.                     $response->headers->set('Content-Type''application/json');
  4674.                     return $response;
  4675.                     exit;
  4676.                 }
  4677.                 $search_picture json_decode($search->getBody()->getContents(),true);
  4678.             
  4679.                 return $this->render('update/index.html.twig', ["return" => ['suche' => $search_picture ]]);
  4680.             }
  4681.         // return $this->render('update/index.html.twig', ["return" => ['vergleich' => $vergleichsarray, 'test' => $testarray ]]);
  4682.         return $this->render('update/index.html.twig', ["return" => ['fehlparents' => $zuErstellendeParents'parentarray' =>  $parentarray ]]);
  4683.         // STAPELVERARBEITUNG HIER!!!!!!!
  4684.         // $custom_fields["custom_article_surface"] = "test";
  4685.         // $custom_fields["custom_article_name_1"] = "name";
  4686.         // $custom_fields["custom_article_material_id"] = "mat";
  4687.         // $custom_fields["custom_article_picture"] = "default_art.jpg";
  4688.         // $variante =[   
  4689.         //     "id"=> "192754b5c0864581b64c92a0076cdce1",
  4690.         //     "taxId"=> "c5ddfa75e22b4889b6f628e1df0b347f",
  4691.         //     "price"=> array(array(
  4692.         //         "currencyId" => "b7d2554b0ce847cd82f3ac9bd1c0dfca",
  4693.         //         "net" =>  0,
  4694.         //         "gross" => 0,
  4695.         //         "linked" => false
  4696.         //     )),
  4697.         //     "productNumber"=> "1234566789",
  4698.         //     "stock"=> 0,
  4699.         //     "name" => "Test2"
  4700.         //     ] ;
  4701.         $parent =[   
  4702.             "id"=> "393eb570026d4076a1ceaf6fe48bdd05"//patch
  4703.             "name" => "Test",
  4704.             "parentId" => "3ab3a8fd7b0046679a71d378ef334ba8",
  4705.             "productNumber"=> "1234567890",
  4706.             "stock"=> 0,
  4707.             "isCloseout"=> true,
  4708.             "height" => 9,
  4709.             "length" => 10,
  4710.             "width"=> 8,
  4711.             "price"=> array(array(
  4712.                 "currencyId" => "b7d2554b0ce847cd82f3ac9bd1c0dfca",
  4713.                 "net" =>  0,
  4714.                 "gross" => 0,
  4715.                 "linked" => false
  4716.             )),
  4717.             // OPTIONS
  4718.             // PROPERTIES
  4719.             "customFields" =>array(
  4720.                 "custom_article_surface" => "test",
  4721.                 "custom_article_name_1" => "name",
  4722.                 "custom_article_material_name" => "mati",
  4723.                 "custom_article_picture" => "default_art.jpg"
  4724.             ),
  4725.             // ende patch - nachfolgende nur bei parents gesetzt
  4726.             "categories"=> array(
  4727.                 "data"=>array(
  4728.                     "type" => "category",
  4729.                     "id" => "c7446ea908604e06a8f28a6c42fd1171",
  4730.                 )
  4731.             ),
  4732.             "taxId"=> "c5ddfa75e22b4889b6f628e1df0b347f",
  4733.             // VISIBILITY WIRFT FEHLER WENN BEREITS GESETZT BEIM PATCH -> NUR SETZEN WENN ID LEER IST (PRODUKT NOCH NICHT EXISTIERT)
  4734.             // "visibilities" => array(
  4735.             //     array(
  4736.             //         "productId" => "393eb570026d4076a1ceaf6fe48bdd05",
  4737.             //         "salesChannelId" => "f8c383f75225488a8f6b89b7d64d6cf2",
  4738.             //         "visibility" => 30
  4739.             //     )
  4740.             // ),
  4741.             // ,"children" => array($variante)
  4742.         ];
  4743.        
  4744.         // return $this->render('update/index.html.twig', ["return" => ['suchee' => array($parent,$variante),  'product' => $test]]);
  4745.         // return $this->render('update/index.html.twig', ["return" => ['suchee' => array($parent,$variante),  'product' => $ProductFlat]]);
  4746.         try {
  4747.             $search $this->client->request('POST''/api/_action/sync', array( 
  4748.             // $search = $this->client->request('GET', '/api/media/6d98e8d9d2184172a5acc4a32095e843', array( 
  4749.             "debug" => true,
  4750.             "headers" => $this->header,
  4751.             "body" => json_encode([
  4752.                 array(
  4753.                     "action"=> "upsert",
  4754.                     "entity"=> "product",
  4755.                     // im payload mit kommas getrennt datensätze von parents mit integrierten childs angeben
  4756.                     "payload"=> 
  4757.                     array(
  4758.                         $parent
  4759.                     // z.b.    ,$parent2
  4760.                     )
  4761.                 )
  4762.                 ])
  4763.             ));
  4764.         } catch (RequestException $e) {
  4765.             // echo Psr7\str($e->getRequest());
  4766.             // if ($e->hasResponse()) {
  4767.             //     echo Psr7\str($e->getResponse());
  4768.             // }
  4769.             if ($e->hasResponse()) {
  4770.                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()), 'response' => Psr7\str($e->getResponse()))));
  4771.             }else{
  4772.                 $response = new Response(json_encode(array('request' => Psr7\str($e->getRequest()))));
  4773.             }
  4774.             $response->headers->set('Content-Type''application/json');
  4775.             return $response;
  4776.             exit;
  4777.         }
  4778.         $search_picture json_decode($search->getBody()->getContents(),true);
  4779.     
  4780.         return $this->render('update/index.html.twig', ["return" => ['suche' => $search_picture ]]);
  4781.         
  4782.         return $this->render('update/index.html.twig', ["return" => ['test' => "ende" ]]);
  4783.     }
  4784. }