From e5c28ed3ec52395a825fecd4a63d5609bcdca929 Mon Sep 17 00:00:00 2001 From: marina Date: Wed, 6 Nov 2024 09:49:24 +0300 Subject: [PATCH] =?utf8?q?ERP-153=20=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?utf8?q?=D0=BE=D0=B2=D0=B0=D1=82=D1=8C=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4?= =?utf8?q?=20=D0=B4=D0=BB=D1=8F=20=D1=81=D0=B1=D0=BE=D1=80=D0=B0=20=D0=B8?= =?utf8?q?=D0=BD=D1=84=D0=BE=D1=80=D0=BC=D0=B0=D1=86=D0=B8=D0=B8=20=D0=BE?= =?utf8?q?=D0=B1=20=D0=BE=D1=81=D1=82=D0=B0=D1=82=D0=BA=D0=B0=D1=85=20?= =?utf8?q?=D0=B8=20=D1=81=D1=82=D0=BE=D0=B8=D0=BC=D0=BE=D1=81=D1=82=D0=B8?= =?utf8?q?=20=D1=82=D0=BE=D0=B2=D0=B0=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../marketplace/InfoAboutStocksAndPrice.php | 179 ++++++++++++++++++ erp24/controllers/MarketplaceController.php | 1 + erp24/records/ProductsClass.php | 4 + 3 files changed, 184 insertions(+) create mode 100644 erp24/actions/marketplace/InfoAboutStocksAndPrice.php diff --git a/erp24/actions/marketplace/InfoAboutStocksAndPrice.php b/erp24/actions/marketplace/InfoAboutStocksAndPrice.php new file mode 100644 index 00000000..ed46ff97 --- /dev/null +++ b/erp24/actions/marketplace/InfoAboutStocksAndPrice.php @@ -0,0 +1,179 @@ +request->post(); + $marketId = $post['marketId']; + $is_yandex = $post['is_yandex']; + + if (!array_key_exists($marketId, MarketplaceStore::getWarehouseId())) + return; + + $marketplacesCount = count(MarketplaceStore::getWarehouseId()); + + // 1. Получение гуидов букетов + $productsGroup = ProductsClass::find() + ->orWhere(['ilike', 'tip', ProductsClass::MARKETPLACE]) + ->orWhere(['ilike', 'tip', ProductsClass::MARKETPLACE_ADDITIONAL]) + ->select('category_id') + ->asArray(); + + $productsGuids = Products1c::find() + ->andWhere(['in', 'parent_id', $productsGroup]) + ->andWhere(['<>', 'components', '']) + ->select('id') + ->column(); + + // 2. Получение цен на букеты + $prices = ArrayHelper::map(Prices::findAll(['product_id' => $productsGuids]), 'product_id', 'price'); + + // 3. Получение состава букетов + $bouquetComposition = []; + foreach ($productsGuids as $guid) { + $components = Products1c::find() + ->andWhere(['id' => $guid]) + ->select('components') + ->column(); + + // Проверяем, что массив не пустой и строка валидна + if (!empty($components) && !in_array('', $components, true)) { + $bouquetComposition[$guid] = json_decode($components[0]); // Сохраняем компоненты + } + } + + // 4. Проверка остатков + + $marketplaceStores = array_column(MarketplaceStore::findAll(['warehouse_id' => $marketId]), null, 'guid'); + + $stocks = []; + + foreach ($bouquetComposition as $guid => $products) { + $stockRecords = []; + + + foreach ($products as $product_id => $count) { + // Получаем записи остатков для текущего product_id + $balances = Balances::findAll(['product_id' => $product_id]); + + // Если записи остатков найдены, суммируем их + foreach ($balances as $balance) { + if (array_key_exists($balance->store_id, $marketplaceStores)) { + $marketplace = $marketplaceStores[$balance->store_id]; + $stockRecords[$product_id] = ['store_guid' => $marketplace, 'count' => $balance->quantity, 'marketplace_guid' => $marketplace->warehouse_guid]; + } + } + } + + + foreach ($stockRecords as $productGuid => $values) { + $temp = intval($values['count'] / $products->$productGuid); + $bouquetCount = !empty($bouquetCount) ? min($bouquetCount, $temp) : $temp; + $store = $values['marketplace_guid']; + } + + if ($bouquetCount > 0) { + $stocks[$guid] = ['count' => $bouquetCount, 'store' => $store]; + } + } + + // 5. Получение приоритетов + $priorities = MarketplacePriority::findAll(['guid' => $productsGuids]); + + // 6. Массив для хранения гуидов, которые можно отправить + $availableGuids = []; + + foreach ($productsGuids as $guid) { + if (!array_key_exists($guid, $stocks)) { + continue; + } + + $stock = $stocks[$guid]; + + $priority = MarketplacePriority::findOne(['guid' => $guid]); + + if (empty($priority)) { + continue; + } + $minQuanity = $priority->minimal_quantity; + $koefRemain = $priority->reminder_koef; + + // Учитываем количество букетов с коэффициентом + $effectiveStock = floor($stock['count'] / $koefRemain); + + + if (array_key_exists($guid, $prices)) { + $price = $prices[$guid]; + } else { + continue; + } + + if ($effectiveStock >= 2) { + // Если остаток позволяет, добавляем в доступные + $availableGuids[] = array('guid' => $guid, 'count' => $stock['count'], 'price' => $price, 'store' => $stock['store']); + } elseif ($effectiveStock == 1 && $minQuanity == 1) { + // Если минимальный остаток 1, добавляем в доступные + $availableGuids[] = array('guid' => $guid, 'count' => $stock['count'], 'price' => $price, 'store' => $stock['store']); + } elseif ($effectiveStock > 1) { + // Нечётное количество, добавляем в доступные + $availableGuids[] = array('guid' => $guid, 'count' => $stock['count'], 'price' => $price, 'store' => $stock['store']);// Добав// Добавляем GUID + } + + } + + $distribution = []; + + foreach ($availableGuids as $product) { + $stock = $stocks[$product['guid']]; + + $priority = MarketplacePriority::findOne(['guid' => $guid]); + + if (empty($priority)) { + continue; + } + + $minQuanity = $priority->minimal_quantity; + $koefRemain = $priority->reminder_koef; + + $totalBouquets = intval($stock['count'] / $koefRemain); // С учетом коэффициента + + if ($totalBouquets >= 2) { + // Равномерное распределение + foreach ($marketplaceStores as $store) { + $distribution[$store->guid] = ($distribution[$store->guid] ?? 0) + floor($totalBouquets / count($marketplaceStores)); + } + } elseif ($totalBouquets == 1 && $minQuanity == 1) { + // Если минимальный остаток 1, отправляем только на Яндекс + foreach ($marketplaceStores as $store) { + if ($is_yandex) { + $distribution[$store->guid] = ($distribution[$store->guid] ?? 0) + 1; + } + } + } elseif ($totalBouquets > 1) { + // Нечётное количество, большую часть на Яндекс + foreach ($marketplaceStores as $store) { + if ($is_yandex) { + $distribution[$store->guid] = ($distribution[$store->guid] ?? 0) + ceil($totalBouquets / 2); + } else { + $distribution[$store->guid] = ($distribution[$store->guid] ?? 0) + floor($totalBouquets / 2); + } + } + } + } + + return $availableGuids; + } +} diff --git a/erp24/controllers/MarketplaceController.php b/erp24/controllers/MarketplaceController.php index 765fad6d..82eca99d 100644 --- a/erp24/controllers/MarketplaceController.php +++ b/erp24/controllers/MarketplaceController.php @@ -9,6 +9,7 @@ class MarketplaceController extends Controller public function actions() { return [ 'priority' => \yii_app\actions\marketplace\PriorityAction::class, + 'infoAboutStocksAndPrice' => \yii_app\actions\marketplace\InfoAboutStocksAndPrice::class, ]; } } \ No newline at end of file diff --git a/erp24/records/ProductsClass.php b/erp24/records/ProductsClass.php index ab7a8db9..fbf54cce 100755 --- a/erp24/records/ProductsClass.php +++ b/erp24/records/ProductsClass.php @@ -12,6 +12,10 @@ use Yii; */ class ProductsClass extends \yii\db\ActiveRecord { + + public const MARKETPLACE = 'marketplace'; + public const MARKETPLACE_ADDITIONAL = 'marketplace_additional'; + /** * {@inheritdoc} */ -- 2.39.5