]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
Фид тестирование
authorfomichev <vladimir.fomichev@erp-flowers.ru>
Tue, 12 Nov 2024 10:44:14 +0000 (13:44 +0300)
committerfomichev <vladimir.fomichev@erp-flowers.ru>
Tue, 12 Nov 2024 10:44:14 +0000 (13:44 +0300)
erp24/media/controllers/FlowwowController.php
erp24/services/MarketplaceService.php

index 0b65b8c572922f7cda006eb8840a5393bb5f3260..b8211a658edcdc5fdfc23891f6fc72afd7c588e0 100644 (file)
@@ -15,9 +15,11 @@ class FlowwowController extends Controller
     public function actionFeed($id)
     {
 
+        $balanceInfo = MarketplaceService::infoForMarketplace(1);
 
+        $productsInfo = MarketplaceService::getProductsInfoForFeed($id,$balanceInfo);
 
-        $productsInfo = MarketplaceService::getAllProductsInfo($id);
+       // $productsInfo = MarketplaceService::getAllProductsInfo($id);
 
 
         $xmlFeed = MarketplaceService::createXMLFeed($productsInfo);
index 59db72fe6e65c1ee70ae8b4b646b70f121d51e1a..6aeefa2b49a2331685150cb059a9f637c4cc56f8 100644 (file)
@@ -107,7 +107,7 @@ class MarketplaceService
             }
         }
 
-      //  var_dump($prices);
+        //var_dump($prices);
 
         // 3. Получение состава букетов
         $bouquetComposition = [];
@@ -130,10 +130,14 @@ class MarketplaceService
         // 4. Проверка остатков
 
         $marketplaceStores = array_column(MarketplaceStore::findAll(['warehouse_id' => $marketId]), null, 'guid');
+
         $marketplaceStoresCnt = MarketplaceStore::find()->select(['guid', 'COUNT(warehouse_id) as cnt'])->groupBy(['guid'])->indexBy('guid')->asArray()->all();
+
         $stocks = [];
 
         $balancesAll = Balances::find()->where(['product_id' => $componentsGuids, 'store_id' => array_keys($marketplaceStores)])->asArray()->all();
+
+
         $balance2Dim = [];
         $balanceStoreIds = [];
         foreach ($balancesAll as $balance) {
@@ -141,9 +145,9 @@ class MarketplaceService
             $balance2Dim[$balance['store_id']][$balance['product_id']] = $balance['quantity'];
             $balanceStoreIds[] = $balance['store_id'];
         }
-
+        //var_dump( $balanceStoreIds );
         $balanceStoreIds = array_unique($balanceStoreIds);
-        //var_dump($balanceStoreIds);
+
         foreach ($bouquetComposition as $guid => $products) {
             $stockRecords = [];
             foreach ($products as $product_id => $count) {
@@ -171,11 +175,11 @@ class MarketplaceService
                 $stocks[$guid] = ['count' => $bouquetCount, 'store' => $store];
             }
         }
-
+        //var_dump(ArrayHelper::getColumn($productsGuids, 'id'));
         // 5. Получение приоритетов
         $priorities = MarketplacePriority::find()
             ->where(['guid' => ArrayHelper::getColumn($productsGuids, 'id')])->indexBy('guid')->asArray()->all();
-
+        //var_dump($priorities);
         // 6. Массив для хранения гуидов, которые можно отправить
         $availableGuids = [];
 
@@ -209,7 +213,7 @@ class MarketplaceService
             }
 
         }
-
+       // var_dump($availableGuids);
         $distribution = [];
 
         foreach ($availableGuids as $product) {
@@ -226,7 +230,7 @@ class MarketplaceService
             $koefRemain = $priority['reminder_koef'];
 
             $totalBouquets = intval($stock['count'] / $koefRemain); // С учетом коэффициента
-//            echo "<pre>"; var_dump($availableGuids); var_dump($totalBouquets); var_dump($stock); die;
+         //   echo "<pre>"; var_dump($availableGuids); var_dump($totalBouquets); var_dump($stock); die;
             if ($totalBouquets >= 2) {
                 // Равномерное распределение
                 foreach ($marketplaceStores as $store) {
@@ -250,6 +254,7 @@ class MarketplaceService
                 }
             }
         }
+      //  var_dump($distribution);
 
         return Json::encode($distribution);
     }
@@ -354,6 +359,121 @@ class MarketplaceService
     }
 
 
+    public static function getProductsInfoForFeed(int $warehouseGuid, $storeData)
+    {
+
+
+        if (is_string($storeData)) {
+            $storeData = json_decode($storeData, true);
+
+
+            if (json_last_error() !== JSON_ERROR_NONE) {
+                Yii::error('Невалидный JSON в storeData: ' . json_last_error_msg(), __METHOD__);
+                return [];
+            }
+        }
+
+
+        if (!is_array($storeData)) {
+            Yii::error('Invalid data format for storeData; expected array.', __METHOD__);
+            return [];
+        }
+
+        $store = MarketplaceStore::findOne(['warehouse_guid' => $warehouseGuid]);
+        if (!$store) {
+            Yii::error("Не найден склад для GUID {$warehouseGuid}", __METHOD__);
+            return [];
+        }
+        $storeGuid = $store->guid;
+
+
+        if (!isset($storeData[$storeGuid])) {
+            Yii::warning("Не найдено данных store GUID {$storeGuid}", __METHOD__);
+            return [];
+        }
+
+
+        $productQuantities = $storeData[$storeGuid];
+
+
+        $products = Products1c::find()
+            ->where(['id' => array_keys($productQuantities)])
+            ->andWhere(['!=', 'components', ''])
+            ->all();
+
+        $result = [];
+
+        foreach ($products as $product) {
+            $properties = MarketplaceService::getProductPropertiesByGuid($product->id);
+            if (!$properties) {
+                $message = "Товар с GUID {$product->id} не имеет свойств в MatrixErpProperty и был исключен из фида.";
+                Yii::error($message, __METHOD__);
+
+                InfoLogService::setInfoLog(
+                    __FILE__,
+                    __LINE__,
+                    $message,
+                    'Missing properties error'
+                );
+                continue;
+            }
+
+            $price = MarketplaceService::getProductPrice($product->id);
+
+            /*  if ($price == 0) {
+                $message = "У товара {$product->id} отсутствует цена и он будет исключен из фида.";
+                Yii::error($message, __METHOD__);
+
+
+                InfoLogService::setInfoLog(
+                    __FILE__,
+                    __LINE__,
+                    $message,
+                    'Zero price error'
+                );
+                continue;
+            }*/
+
+            $components = json_decode($product->components, true);
+            $composition = [];
+
+            foreach ($components as $componentId => $quantity) {
+                $component = Products1c::findOne(['id' => $componentId]);
+                if ($component && $quantity > 0) {
+                    $composition[] = [
+                        'name' => $component->name,
+                        'quantity' => $quantity,
+                        'unit' => 'шт'
+                    ];
+                }
+            }
+
+
+            $availableQty = $productQuantities[$product->id] ?? 0;
+
+            $result[] = [
+                'id' => $product->id,
+                'name' => $properties['displayName'],
+                'pictures' => [$properties['imageUrl']],
+                'price' => $price,
+                'oldprice' => MarketplaceService::getProductOldPrice($product->id),
+                'description' => MarketplaceService::getProductDescription($product->id),
+                'qty' => $availableQty,
+                'amount' => $availableQty,
+                'weight' => MarketplaceService::getProductWeight($product->id),
+                'minorder' => MarketplaceService::getProductMinOrder($product->id),
+                'composition' => $composition,
+                'available' => MarketplaceService::getProductAvailability($product->id),
+                'category_id' => MarketplaceService::getProductCategory($product->id),
+                'category_name' => $properties['flowwowSubcategory'],
+                'params' => MarketplaceService::getProductParams($product->id),
+                'productLink' => MarketplaceService::getProductLinkByGuid($product->id),
+            ];
+        }
+
+        return $result;
+    }
+
     /**
      * Статический метод для создания XML-фида на основе информации о продуктах.
      *
@@ -422,7 +542,8 @@ class MarketplaceService
 
             // Добавление веса и количества
             $offer->addChild('weight', $product['weight']);
-            //  $offer->addChild('qty', $product['qty']);
+            $offer->addChild('qty', $product['qty']);
+
             // $offer->addChild('amount', $product['amount']);
             //   $offer->addChild('cost', $product['amount']);