$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
+ ];
+ }
}
}
// 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;
}