From: Vladimir Fomichev Date: Tue, 12 Aug 2025 09:03:42 +0000 (+0300) Subject: Правки в распределении X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=27c5f9807a1ecbf690be022ab28ac7f3871a565e;p=erp24_rep%2Fyii-erp24%2F.git Правки в распределении --- diff --git a/erp24/controllers/MarketplaceController.php b/erp24/controllers/MarketplaceController.php index 85549b82..ade986ac 100644 --- a/erp24/controllers/MarketplaceController.php +++ b/erp24/controllers/MarketplaceController.php @@ -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 diff --git a/erp24/services/MarketplaceService.php b/erp24/services/MarketplaceService.php index 3d24d618..31ea9032 100644 --- a/erp24/services/MarketplaceService.php +++ b/erp24/services/MarketplaceService.php @@ -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; }