]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
Правки в распределении
authorVladimir Fomichev <vladimir.fomichev@erp-flowers.ru>
Tue, 12 Aug 2025 09:03:42 +0000 (12:03 +0300)
committerVladimir Fomichev <vladimir.fomichev@erp-flowers.ru>
Tue, 12 Aug 2025 09:03:42 +0000 (12:03 +0300)
erp24/controllers/MarketplaceController.php
erp24/services/MarketplaceService.php

index 85549b82c61812adf923ebfb720ccdc9b2ec76a6..ade986acb9e56b5c93cb4c1a162c314b73995979 100644 (file)
@@ -50,4 +50,24 @@ class MarketplaceController extends Controller
 
         return $xmlFeed;
     }
+
+    public function actionFeedTest()
+    {
+        $id =  206008;
+
+        $balanceInfo = MarketplaceService::infoForMarketplace(1);
+        var_dump($balanceInfo); die();
+        Yii::error('Баланс ' . json_encode($balanceInfo, JSON_UNESCAPED_UNICODE));
+        $productsInfo = MarketplaceService::getProductsInfoForFeed($id, $balanceInfo);
+        Yii::error('Инфо товаров ' . json_encode($productsInfo, JSON_UNESCAPED_UNICODE));
+        // $productsInfo = MarketplaceService::getAllProductsInfo($id);
+
+
+        $xmlFeed = MarketplaceService::createXMLFeed($productsInfo);
+        Yii::error('Фид ' . json_encode($xmlFeed, JSON_UNESCAPED_UNICODE));
+        Yii::$app->response->format = Response::FORMAT_RAW;
+        Yii::$app->response->headers->add('Content-Type', 'application/xml; charset=utf-8');
+
+        return $xmlFeed;
+    }
 }
\ No newline at end of file
index 3d24d6187db6650c01c47adb8afa87d7713114b3..31ea9032d1d4ceb80e32c89eb410a59cde953479 100644 (file)
@@ -182,30 +182,26 @@ class MarketplaceService
         $balanceStoreIds = array_unique($balanceStoreIds);
 
         foreach ($bouquetComposition as $guid => $products) {
-            $stockRecords = [];
-            foreach ($products as $product_id => $count) {
-                // Если записи остатков найдены, суммируем их
-                foreach ($balanceStoreIds as $storeId) {
-                    if (array_key_exists($storeId, $marketplaceStores)) {
-                        $marketplace = $marketplaceStores[$storeId];
-                        $stockRecords[$product_id] = [
-                            'store_guid' => $storeId,
-                            'count' => $balance2Dim[$storeId][$product_id] ?? 0,
-                            'marketplace_guid' => $marketplace->warehouse_guid
-                        ];
-                    }
+            foreach ($balanceStoreIds as $storeId) {
+                if (!isset($marketplaceStores[$storeId])) {
+                    continue;
                 }
-            }
 
-            $bouquetCount = PHP_INT_MAX;
-            foreach ($stockRecords as $productGuid => $values) {
-                $temp = intval($values['count'] / $products->$productGuid);
-                $bouquetCount = !empty($bouquetCount) ? min($bouquetCount, $temp) : $temp;
-                $store = $values['marketplace_guid'];
-            }
+                $marketplace = $marketplaceStores[$storeId];
+                $bouquetCount = PHP_INT_MAX;
+
+                foreach ($products as $productId => $count) {
+                    $stock = (int)($balance2Dim[$storeId][$productId] ?? 0);
+                    $temp = (int)($stock / $count);
+                    $bouquetCount = min($bouquetCount, $temp);
+                }
 
-            if (!empty($stockRecords) && isset($store) && $bouquetCount > 0) {
-                $stocks[$guid] = ['count' => $bouquetCount, 'store' => $store];
+                if ($bouquetCount > 0 && $bouquetCount !== PHP_INT_MAX) {
+                    $stocks[$storeId][$guid] = [
+                        'count' => $bouquetCount,
+                        'store' => $marketplace->warehouse_guid
+                    ];
+                }
             }
         }
 
@@ -215,79 +211,84 @@ class MarketplaceService
 
         // 6. Массив для хранения гуидов, которые можно отправить
         $availableGuids = [];
+        foreach ($stocks as $storeId => $stock ) {
 
-        foreach ($productsGuids as $ind => $idAndComponents) {
-            $guid = $idAndComponents['id'];
-            if (!array_key_exists($guid, $stocks)) {
-                continue;
-            }
+            foreach ($productsGuids as $ind => $idAndComponents) {
+                $guid = $idAndComponents['id'];
+                if (!array_key_exists($guid, $stock)) {
+                    continue;
+                }
 
-            $stock = $stocks[$guid];
+                $stockRec = $stock[$guid];
 
-            $priority = $priorities[$guid] ?? null;
+                $priority = $priorities[$guid] ?? null;
 
-            if (empty($priority)) {
-                continue;
-            }
-            $minQuanity = $priority['minimal_quantity'];
-            $koefRemain = $priority['reminder_koef'];
+                if (empty($priority)) {
+                    continue;
+                }
+                $minQuanity = $priority['minimal_quantity'];
+                $koefRemain = $priority['reminder_koef'];
 
-            // Учитываем количество букетов с коэффициентом
-            $effectiveStock = floor($stock['count'] / $koefRemain);
+                // Учитываем количество букетов с коэффициентом
+                $effectiveStock = floor($stockRec['count'] / $koefRemain);
 
-            if (array_key_exists($guid, $prices)) {
-                $price = $prices[$guid];
-            } else {
-                continue;
-            }
+                if (array_key_exists($guid, $prices)) {
+                    $price = $prices[$guid];
+                } else {
+                    continue;
+                }
 
-            if (($effectiveStock >= $minQuanity)) {
-                $availableGuids[] = array('guid' => $guid, 'count' => $stock['count'], 'price' => $price, 'store' => $stock['store']);
-            }
+                if (($effectiveStock >= $minQuanity)) {
+                    $availableGuids[$storeId][] = array('guid' => $guid, 'count' => $stockRec['count'], 'price' => $price, 'store' => $stockRec['store']);
+                }
 
+            }
         }
+        //var_dump($availableGuids);die();
 
         $distribution = [];
+        foreach ($availableGuids as $storeId => $availableProducts) {
+            foreach ( $availableProducts as $product) {
+                $guid = $product['guid'];
+                $stock = $stocks[$storeId][$guid];
 
-        foreach ($availableGuids as $product) {
-            $guid = $product['guid'];
-            $stock = $stocks[$guid];
+                $priority = $priorities[$guid] ?? null;
 
-            $priority = $priorities[$guid] ?? null;
-
-            if (empty($priority)) {
-                continue;
-            }
+                if (empty($priority)) {
+                    continue;
+                }
 
-            $minQuanity = $priority['minimal_quantity'];
-            $koefRemain = $priority['reminder_koef'];
+                $minQuanity = $priority['minimal_quantity'];
+                $koefRemain = $priority['reminder_koef'];
 
-            $totalBouquets = intval($stock['count'] / $koefRemain); // С учетом коэффициента
+                $totalBouquets = intval($stock['count'] / $koefRemain); // С учетом коэффициента
 
-            if ($totalBouquets >= 2) {
-                // Равномерное распределение
-                foreach ($marketplaceStores as $store) {
-                    $distribution[$store->guid][$guid] = ($distribution[$store->guid][$guid] ?? 0) + floor($totalBouquets / $marketplaceStoresCnt[$store->guid]['cnt']);
-                }
-            } elseif ($totalBouquets == 1 && $minQuanity == 1) {
-                // Если минимальный остаток 1, отправляем только на Яндекс
-                foreach ($marketplaceStores as $store) {
-                    if ($is_yandex) {
-                        $distribution[$store->guid][$guid] = ($distribution[$store->guid][$guid] ?? 0) + 1;
+                if ($totalBouquets >= 2) {
+                    // Равномерное распределение
+                    foreach ($marketplaceStores as $store) {
+                        $distribution[$store->guid][$guid] = ($distribution[$store->guid][$guid] ?? 0) + floor($totalBouquets / $marketplaceStoresCnt[$store->guid]['cnt']);
                     }
-                }
-            } elseif ($totalBouquets > 1) {
-                // Нечётное количество, большую часть на Яндекс
-                foreach ($marketplaceStores as $store) {
-                    if ($is_yandex) {
-                        $distribution[$store->guid][$guid] = ($distribution[$store->guid][$guid] ?? 0) + ceil($totalBouquets / 2);
-                    } else {
-                        $distribution[$store->guid][$guid] = ($distribution[$store->guid][$guid] ?? 0) + floor($totalBouquets / 2);
+                } elseif ($totalBouquets == 1 && $minQuanity == 1) {
+                    // Если минимальный остаток 1, отправляем только на Яндекс
+                    foreach ($marketplaceStores as $store) {
+                        if ($is_yandex) {
+                            $distribution[$store->guid][$guid] = ($distribution[$store->guid][$guid] ?? 0) + 1;
+                        }
+                    }
+                } elseif ($totalBouquets > 1) {
+                    // Нечётное количество, большую часть на Яндекс
+                    foreach ($marketplaceStores as $store) {
+                        if ($is_yandex) {
+                            $distribution[$store->guid][$guid] = ($distribution[$store->guid][$guid] ?? 0) + ceil($totalBouquets / 2);
+                        } else {
+                            $distribution[$store->guid][$guid] = ($distribution[$store->guid][$guid] ?? 0) + floor($totalBouquets / 2);
+                        }
                     }
                 }
             }
         }
-        //  var_dump($distribution);
+
+         // var_dump($distribution);
 
         return $distribution;
     }