From 12eee821bbfc90fbfa4d5953245ca5a947f31652 Mon Sep 17 00:00:00 2001 From: Vladimir Fomichev Date: Tue, 12 Aug 2025 14:36:24 +0300 Subject: [PATCH] =?utf8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20=D0=B2=20?= =?utf8?q?=D1=80=D0=B0=D1=81=D0=BF=D1=80=D0=B5=D0=B4=D0=B5=D0=BB=D0=B5?= =?utf8?q?=D0=BD=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- erp24/controllers/MarketplaceController.php | 2 +- erp24/services/MarketplaceService.php | 85 +++++++++++++-------- 2 files changed, 53 insertions(+), 34 deletions(-) diff --git a/erp24/controllers/MarketplaceController.php b/erp24/controllers/MarketplaceController.php index ade986ac..53a2bd4c 100644 --- a/erp24/controllers/MarketplaceController.php +++ b/erp24/controllers/MarketplaceController.php @@ -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)); diff --git a/erp24/services/MarketplaceService.php b/erp24/services/MarketplaceService.php index 31ea9032..14979abf 100644 --- a/erp24/services/MarketplaceService.php +++ b/erp24/services/MarketplaceService.php @@ -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; } -- 2.39.5