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

index ade986acb9e56b5c93cb4c1a162c314b73995979..53a2bd4cdd649aa7c1d3af895035e95dbcc2f2a8 100644 (file)
@@ -56,7 +56,7 @@ class MarketplaceController extends Controller
         $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));
index 31ea9032d1d4ceb80e32c89eb410a59cde953479..14979abff95a970015a8ba056f317da2f3ed0151 100644 (file)
@@ -162,13 +162,20 @@ class MarketplaceService
 
         // 4. Проверка остатков
 
-        $marketplaceStores = array_column(MarketplaceStore::findAll(['warehouse_id' => $marketId]), null, 'guid');
+        $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();
+        $balancesAll = Balances::find()
+            ->where(['product_id' => $componentsGuids, 'store_id' => array_keys($marketplaceStores)])
+            ->asArray()
+            ->all();
 
 
         $balance2Dim = [];
@@ -192,8 +199,8 @@ class MarketplaceService
 
                 foreach ($products as $productId => $count) {
                     $stock = (int)($balance2Dim[$storeId][$productId] ?? 0);
-                    $temp = (int)($stock / $count);
-                    $bouquetCount = min($bouquetCount, $temp);
+                    $temp  = intdiv($stock, max(1, (int)$count));
+                    $bouquetCount = is_null($bouquetCount) ? $temp : min($bouquetCount, $temp);
                 }
 
                 if ($bouquetCount > 0 && $bouquetCount !== PHP_INT_MAX) {
@@ -244,51 +251,63 @@ class MarketplaceService
 
             }
         }
-        //var_dump($availableGuids);die();
 
         $distribution = [];
         foreach ($availableGuids as $storeId => $availableProducts) {
-            foreach ( $availableProducts as $product) {
-                $guid = $product['guid'];
-                $stock = $stocks[$storeId][$guid];
+            foreach ($availableProducts as $product) {
+                $guid  = $product['guid'] ?? null;
+                if (!$guid) {
+                    continue;
+                }
 
-                $priority = $priorities[$guid] ?? null;
+                // Остаток букетов по складу
+                $stockRec = $stocks[$storeId][$guid] ?? null;
+                if (!$stockRec) {
+                    continue;
+                }
 
-                if (empty($priority)) {
+                // Приоритеты
+                $priority = $priorities[$guid] ?? null;
+                if (!$priority) {
                     continue;
                 }
 
-                $minQuanity = $priority['minimal_quantity'];
-                $koefRemain = $priority['reminder_koef'];
+                $minQty = (int)($priority['minimal_quantity'] ?? 0);
+                $coef   = (float)($priority['reminder_koef'] ?? 1);
+                if ($coef <= 0) {
+                    continue;
+                }
 
-                $totalBouquets = intval($stock['count'] / $koefRemain); // С учетом коэффициента
+                // Сколько букетов реально можно выставить с учётом коэффициента
+                $total = (int) floor(((int)$stockRec['count']) / $coef);
+                if ($total <= 0 || $total < $minQty) {
+                    continue;
+                }
 
-                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 ($is_yandex) {
+                    // Яндекс-канал
+                    if ($total === 1 && $minQty <= 1) {
+                        $alloc = 1; // один — весь Яндексу
+                    } else {
+                        $alloc = (int) ceil($total / 2); // остаток у Яндекса
                     }
-                } 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);
-                        }
+                } else {
+                    // Flowwow-канал
+                    if ($total === 1) {
+                        $alloc = 0; // один — не даём Flowwow, приоритет у Яндекса
+                    } else {
+                        $alloc = (int) floor($total / 2);
                     }
                 }
+
+                if ($alloc > 0) {
+                    $distribution[$storeId][$guid] = ($distribution[$storeId][$guid] ?? 0) + $alloc;
+                }
             }
         }
 
-         // var_dump($distribution);
+         //var_dump($distribution);
 
         return $distribution;
     }