// 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 = [];
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) {
}
}
- //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;
}