From 06026c804e8d601e758a52e8ab2d6db38dcf09a3 Mon Sep 17 00:00:00 2001 From: fomichev Date: Tue, 13 May 2025 18:10:50 +0300 Subject: [PATCH] =?utf8?q?=D0=92=D1=8B=D0=B2=D0=BE=D0=B4=20=D0=BF=D1=80?= =?utf8?q?=D0=BE=D0=B3=D0=BD=D0=BE=D0=B7=D0=B0=20=D0=BF=D0=BE=20=D1=82?= =?utf8?q?=D0=BE=D0=B2=D0=B0=D1=80=D0=B0=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../AutoPlannogrammaController.php | 32 +++++++- erp24/services/AutoPlannogrammaService.php | 79 +++++++++++++------ .../auto-plannogramma/control-species.php | 36 +++++++++ 3 files changed, 118 insertions(+), 29 deletions(-) diff --git a/erp24/controllers/AutoPlannogrammaController.php b/erp24/controllers/AutoPlannogrammaController.php index 3d8f9b77..0164aeae 100644 --- a/erp24/controllers/AutoPlannogrammaController.php +++ b/erp24/controllers/AutoPlannogrammaController.php @@ -239,6 +239,8 @@ class AutoPlannogrammaController extends BaseController $weeksShareResult = []; $weeksGoalResult = []; $monthCategoryShareResult = []; + $weeksProductForecast = []; + if ($model->load(Yii::$app->request->post()) && $model->validate()) { $filters = []; @@ -381,17 +383,41 @@ class AutoPlannogrammaController extends BaseController } + foreach ($weeksData as $r) { + $forecasts = $service->calculateWeekForecastSpeciesProducts($r['category'], $r['subcategory'], $r['species'], $r['store_id'], $r['weekly_goal']); + foreach ($forecasts as $forecast) { + $weeksProductForecast[] = [ + 'category' => $forecast['category'] ?? '', + 'subcategory' => $forecast['subcategory'] ?? '', + 'species' => $forecast['species'] ?? '', + 'product_id' => $forecast['product_id'] ?? '', + 'name' => $forecast['name'] ?? '', + 'price' => $forecast['price'] ?? '', + 'goal' => $forecast['goal'] ?? 0, + 'forecast' => $forecast['forecast'] ?? 0, + 'week' => $r['week'], + ]; + } + } - + usort($weeksProductForecast, function($a, $b) { + foreach (['category','subcategory','species','name','week'] as $key) { + $va = $a[$key]; + $vb = $b[$key]; + if ($va < $vb) return -1; + if ($va > $vb) return 1; + } + return 0; + }); } -//var_dump($weeksData); die(); +//var_dump($weeksProductForecast); die(); return $this->render('control-species', [ 'model' => $model, 'result' => $monthResult, 'weeksData' => $weeksData, 'monthCategoryShare' => $monthCategoryShareResult, - // 'weeksGoalResult' => $weeksGoalResult, + 'weeksProductForecast' => $weeksProductForecast, 'totals' => $totals, 'storeList' => $storeList, 'monthsList' => $monthsList, diff --git a/erp24/services/AutoPlannogrammaService.php b/erp24/services/AutoPlannogrammaService.php index 64cb910a..d236060a 100644 --- a/erp24/services/AutoPlannogrammaService.php +++ b/erp24/services/AutoPlannogrammaService.php @@ -6,6 +6,8 @@ use yii\db\Expression; use yii\db\Query; use yii\helpers\ArrayHelper; use yii_app\records\CityStore; +use yii_app\records\PricesDynamic; +use yii_app\records\Products1cNomenclature; use yii_app\records\SalesWriteOffsPlan; class AutoPlannogrammaService @@ -1251,35 +1253,60 @@ var_dump($totals); die(); } - /** - * Разворачивает вложенный многомерный массив исторических данных - * в плоский список записей. - * - * @param array $historical - * @return array [ ['week'=>..., 'store_id'=>..., 'category'=>..., 'subcategory'=>..., 'species'=>..., 'sum'=>...], ... ] - */ - private function flattenHistorical(array $historical): array + public static function calculateWeekForecastSpeciesProducts($category, $subcategory, $species, $storeId, $goal) { - $flat = []; - foreach ($historical as $week => $byStore) { - foreach ($byStore as $storeId => $byCat) { - foreach ($byCat as $category => $bySub) { - foreach ($bySub as $subcategory => $bySpec) { - foreach ($bySpec as $species => $sum) { - $flat[] = [ - 'week' => $week, - 'store_id' => $storeId, - 'category' => $category, - 'subcategory' => $subcategory, - 'species' => $species, - 'sum' => $sum, - ]; - } - } - } + $speciesProductForecast = []; + $products = Products1cNomenclature::find() + ->select(['id', 'name']) + ->where(['category' => $category]) + ->andWhere(['subcategory' => $subcategory]) + ->andWhere(['species' => $species]) + ->indexBy('id') + ->asArray() + ->all(); + + $productsIds = ArrayHelper::getColumn($products, 'id'); + if (CityStore::find()->where(['id' => $storeId])->one()->city_id == 1342) { + $region = 52; + } elseif (CityStore::find()->where(['id' => $storeId])->one()->city_id == 1) { + $region = 77; + } else { + $region = null; + } + $priceRecords = PricesDynamic::find() + ->select(['product_id', 'price']) + ->where(['product_id' => $productsIds]) + ->andWhere(['active' => 1]) + ->andWhere(['or', ['region_id' => $region], ['region_id' => null]]) + ->indexBy('product_id') + ->asArray() + ->all(); + + //var_dump($products); die(); + foreach ($priceRecords as $id => $record) { + if ($goal == 0 || (int)$record['price'] == 0) { + $forecast = 0; + } else { + $forecast = round(max($goal / (float)$record['price'], 1), 0); } + $speciesProductForecast[] = [ + 'category' => $category, + 'subcategory' => $subcategory, + 'species' => $species, + 'product_id' => $record['product_id'], + 'name' => $products[$id]['name'], + 'price' => $record['price'] ?? $goal ?? 1, + 'goal' => $goal ?? 0, + 'forecast' => $forecast + + ]; + } - return $flat; + + return $speciesProductForecast; + } + + } diff --git a/erp24/views/auto-plannogramma/control-species.php b/erp24/views/auto-plannogramma/control-species.php index 0ce69abd..d2aa7711 100644 --- a/erp24/views/auto-plannogramma/control-species.php +++ b/erp24/views/auto-plannogramma/control-species.php @@ -16,6 +16,8 @@ use yii_app\records\Products1c; /* @var $weeksData array */ /* @var $weeksShareResult array */ /* @var $weeksGoalResult array */ +/* @var array $weeksProductForecast */ +/* @var array $monthCategoryShare */ ?> @@ -102,4 +104,38 @@ use yii_app\records\Products1c; + +

Прогноз по неделям по неделям

+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
КатегорияПодкатегорияВидТовар (ID)НеделяЦельЦенаПрогноз
+ +
() +
+ -- 2.39.5