From ce6ffb05802f24e46dfad48e9fd58ca7942f582d Mon Sep 17 00:00:00 2001
From: OpenXE <>
Date: Sun, 21 Jan 2024 13:36:58 +0100
Subject: [PATCH] bugfix crossselling shopexport

---
 upgrade/data/db_schema.json | 116 ++++++++++++++++++++++++++++++++++++
 www/lib/class.remote.php    |  12 +++-
 2 files changed, 125 insertions(+), 3 deletions(-)

diff --git a/upgrade/data/db_schema.json b/upgrade/data/db_schema.json
index aa0d5d3b..46c5dda1 100644
--- a/upgrade/data/db_schema.json
+++ b/upgrade/data/db_schema.json
@@ -30933,6 +30933,122 @@
                 }
             ]
         },
+        {
+            "name": "crossselling_artikel",
+            "collation": "utf8mb3_general_ci",
+            "type": "BASE TABLE",
+            "columns": [
+                {
+                    "Field": "id",
+                    "Type": "int(11)",
+                    "Collation": null,
+                    "Null": "NO",
+                    "Key": "PRI",
+                    "Default": null,
+                    "Extra": "auto_increment",
+                    "Privileges": "select,insert,update,references",
+                    "Comment": ""
+                },
+                {
+                    "Field": "aktiv",
+                    "Type": "int(1)",
+                    "Collation": null,
+                    "Null": "NO",
+                    "Key": "",
+                    "Default": null,
+                    "Extra": "",
+                    "Privileges": "select,insert,update,references",
+                    "Comment": ""
+                },
+                {
+                    "Field": "art",
+                    "Type": "int(11)",
+                    "Collation": null,
+                    "Null": "NO",
+                    "Key": "",
+                    "Default": null,
+                    "Extra": "",
+                    "Privileges": "select,insert,update,references",
+                    "Comment": ""
+                },
+                {
+                    "Field": "artikel",
+                    "Type": "int(11)",
+                    "Collation": null,
+                    "Null": "NO",
+                    "Key": "",
+                    "Default": null,
+                    "Extra": "",
+                    "Privileges": "select,insert,update,references",
+                    "Comment": ""
+                },
+                {
+                    "Field": "crosssellingartikel",
+                    "Type": "int(11)",
+                    "Collation": null,
+                    "Null": "NO",
+                    "Key": "",
+                    "Default": null,
+                    "Extra": "",
+                    "Privileges": "select,insert,update,references",
+                    "Comment": ""
+                },
+                {
+                    "Field": "shop",
+                    "Type": "int(11)",
+                    "Collation": null,
+                    "Null": "NO",
+                    "Key": "",
+                    "Default": null,
+                    "Extra": "",
+                    "Privileges": "select,insert,update,references",
+                    "Comment": ""
+                },
+                {
+                    "Field": "sort",
+                    "Type": "int(11)",
+                    "Collation": null,
+                    "Null": "NO",
+                    "Key": "",
+                    "Default": null,
+                    "Extra": "",
+                    "Privileges": "select,insert,update,references",
+                    "Comment": ""
+                },
+                {
+                    "Field": "bemerkung",
+                    "Type": "varchar(255)",
+                    "Collation": "utf8mb3_general_ci",
+                    "Null": "NO",
+                    "Key": "",
+                    "Default": null,
+                    "Extra": "",
+                    "Privileges": "select,insert,update,references",
+                    "Comment": ""
+                },
+                {
+                    "Field": "gegenseitigzuweisen",
+                    "Type": "int(1)",
+                    "Collation": null,
+                    "Null": "NO",
+                    "Key": "",
+                    "Default": null,
+                    "Extra": "",
+                    "Privileges": "select,insert,update,references",
+                    "Comment": ""
+                }
+            ],
+            "keys": [
+                {
+                    "Key_name": "PRIMARY",
+                    "Index_type": "BTREE",
+                    "columns": [
+                        "id"
+                    ],
+                    "Non_unique": ""
+                }
+            ]
+        },
         {
             "name": "datei",
             "collation": "utf8mb3_general_ci",
diff --git a/www/lib/class.remote.php b/www/lib/class.remote.php
index c2f30bbd..18000c0f 100644
--- a/www/lib/class.remote.php
+++ b/www/lib/class.remote.php
@@ -1586,14 +1586,20 @@ class Remote
           $data[$i]['crosssellingartikel'] = [];
         }
 
-        $gegenseitigzugewiesen = $this->app->DB->SelectArr("SELECT a.id, a.nummer, ak.bezeichnung as kategorie, a.name_de, a.name_en, ca.art, ca.gegenseitigzuweisen, af.nummer AS fremdnummer
+       
+        $sql = 
+        
+        "SELECT a.id, a.nummer, ak.bezeichnung as kategorie, a.name_de, a.name_en, ca.art, ca.gegenseitigzuweisen, af.nummer AS fremdnummer
             FROM crossselling_artikel ca 
             JOIN artikel a ON ca.artikel = a.id 
             LEFT JOIN artikelkategorien ak ON CONCAT(ak.id,'_kat') = a.typ 
             LEFT JOIN (SELECT af.id,af.nummer,af.artikel,af.shopid FROM artikelnummer_fremdnummern af JOIN (SELECT artikel, MAX(shopid) AS maxid FROM artikelnummer_fremdnummern WHERE aktiv=1 AND (shopid=0 OR shopid=2) GROUP BY artikel) x ON x.artikel = af.artikel AND af.shopid=x.maxid WHERE af.aktiv = 1) af ON af.artikel = a.id            
+            LEFT JOIN (SELECT nummer,artikel FROM artikelnummer_fremdnummern WHERE shopid=0 OR shopid='$id' ORDER BY shopid DESC LIMIT 1 ) af2 ON af2.artikel = a.id
             WHERE ca.crosssellingartikel='" . $tmp->GetId() . "' AND ca.gegenseitigzuweisen=1 AND (ca.shop='$id' OR ca.shop='0') 
-            LEFT JOIN (SELECT nummer,artikel FROM artikelnummer_fremdnummern WHERE shopid=0 OR shopid='$id' ORDER BY shopid DESC LIMIT 1 ) af ON af.artikel = a.id
-            GROUP BY ca.artikel, ca.art");
+            GROUP BY ca.artikel, ca.art";
+
+        $gegenseitigzugewiesen = $this->app->DB->SelectArr($sql);
+
         if (!empty($gegenseitigzugewiesen)) {
           foreach ($gegenseitigzugewiesen as $gegenseitigzugewiesenercrosssellingartikel) {
             $data[$i]['crosssellingartikel'][] = $gegenseitigzugewiesenercrosssellingartikel;