]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
Прогноз по неделям для вида
authorfomichev <vladimir.fomichev@erp-flowers.ru>
Tue, 3 Jun 2025 07:37:53 +0000 (10:37 +0300)
committerfomichev <vladimir.fomichev@erp-flowers.ru>
Tue, 3 Jun 2025 07:37:53 +0000 (10:37 +0300)
erp24/controllers/AutoPlannogrammaController.php
erp24/services/AutoPlannogrammaService.php
erp24/views/auto-plannogramma/week-sales-products_forecast.php
erp24/views/auto-plannogramma/week-sales-species-share.php

index c362cba8c6e97a1d62a36ed29ad08643cf74523b..dba2f6da08339d12c5d5a660076eafaaf2e60131 100644 (file)
@@ -1365,10 +1365,10 @@ class AutoPlannogrammaController extends BaseController
 
             $weeklySalesForecast = $service->calculateWeeklyProductForecastPieces($productForecastSpecies, $weeklySales);
 
-            //var_dump($weeklySalesForecast); die();
 
+            $weeklySalesForecastFormated = $service->pivotWeeklyForecast($weeklySalesForecast);
 
-            $flatData = array_filter($weeklySalesForecast, function ($row) use ($filters) {
+            $flatData = array_filter($weeklySalesForecastFormated, function ($row) use ($filters) {
                 foreach ($filters as $key => $value) {
                     if (empty($value)) continue;
                     if (!isset($row[$key])) continue;
index 74ea14aba1c91ace443eae85cb54cfa68892b8c3..69f911ed70e64187b254cdb10beba153f3e8476d 100644 (file)
@@ -1221,6 +1221,7 @@ class AutoPlannogrammaService
                     'subcategory'          => $sub,
                     'species'              => $spec,
                     'product_id'           => $pid,
+                    'forecast_month_pieces' => $piecesMon,
                     'forecast_week_pieces' => $forecastWeekPieces,
                 ];
             }
@@ -1230,6 +1231,54 @@ class AutoPlannogrammaService
     }
 
 
+    public function pivotWeeklyForecast(array $flatRows): array
+    {
+        $grouped = [];
+
+        foreach ($flatRows as $row) {
+            $key = implode('|', [
+                $row['store_id'],
+                $row['category'],
+                $row['subcategory'],
+                $row['species'],
+                $row['product_id'],
+            ]);
+
+            if (!isset($grouped[$key])) {
+                $grouped[$key] = [
+                    'store_id'               => $row['store_id'],
+                    'category'               => $row['category'],
+                    'subcategory'            => $row['subcategory'],
+                    'species'                => $row['species'],
+                    'product_id'             => $row['product_id'],
+                    'forecast_month_pieces'  => $row['forecast_month_pieces'],
+                ];
+            }
+
+            $weekNum   = (int)$row['week'];
+            $fieldName = 'week' . $weekNum;
+
+            $grouped[$key][$fieldName] = (float)$row['forecast_week_pieces'];
+        }
+
+        $allWeekFields = [];
+        foreach ($flatRows as $row) {
+            $allWeekFields['week' . ((int)$row['week'])] = true;
+        }
+        $allWeekFields = array_keys($allWeekFields);
+
+          foreach ($grouped as &$group) {
+            foreach ($allWeekFields as $fieldName) {
+                if (! isset($group[$fieldName])) {
+                    $group[$fieldName] = 0.0;
+                }
+            }
+        }
+        unset($group);
+
+        return array_values($grouped);
+    }
+
 
     /**
      * Исторический недельный отчёт и доли по видам с учётом store_id.
index 321c84fc5d63d70832cd6f7c51e8f0716dd34d32..9a7a68e5fb10453a35708c37c77d39b40edd62fc 100644 (file)
@@ -1,4 +1,10 @@
 <?php
+/* @var yii\data\ArrayDataProvider $dataProvider */
+$allModels = $dataProvider->getModels();
+if (empty($allModels)) {
+    echo "Нет данных для отображения.";
+    return;
+}
 ?>
     <div class="filter-form" style="margin-bottom: 20px;">
         <?php use kartik\date\DatePicker;
@@ -10,7 +16,7 @@
         use yii_app\records\CityStore;
         use yii_app\records\Products1cNomenclature;
         ?>
-        <h1 class="ms-3 mb-4"><?= Html::encode("Расчет прогноза товаров внутри вида (month_goods_sales_share)") ?></h1>
+        <h1 class="ms-3 mb-4"><?= Html::encode("Расчет прогноза товаров внутри вида по неделям (week_goods_sales_forecast)") ?></h1>
         <?php
         $form = ActiveForm::begin(['method' => 'get']); ?>
         <div class="row p-3">
 
 
 <?php
-$columns = [
-    [
-        'attribute' => 'week',
-        'label' => 'Неделя',
-    ],
-    [
-        'attribute' => 'store_id',
-        'label' => 'Магазин',
-        'value' => function ($data) {
-            return CityStore::findOne($data['store_id'])->name ?? $data['store_id'];
-        },
-    ],
-    [
-        'attribute' => 'category',
-        'label' => 'Категория',
-    ],
-    [
-        'attribute' => 'subcategory',
-        'label' => 'Подкатегория',
-    ],
-    [
-        'attribute' => 'species',
-        'label' => 'Тип',
-    ],['attribute'=>'week','label'=>'Неделя'],
-    [
-        'attribute'=>'store_id','label'=>'Магазин',
-        'value'=>function($data){ return CityStore::findOne($data['store_id'])->name ?? $data['store_id']; }
-    ],
+$first = $allModels[0];
+$weekFields = array_filter(array_keys($first), fn($k) => strncmp($k, 'week', 4) === 0);
 
-    ['attribute'=>'product_id','label'=>'GUID Товара'],
-    ['attribute'=>'forecast_week_pieces','label'=>'Прогноз (шт.)','format'=>['decimal',2]],
+usort($weekFields, function($a, $b) {
+    return (int) substr($a, 4) <=> (int) substr($b, 4);
+});
+
+
+$columns = [];
+
+$columns[] = [
+    'attribute' => 'store_id',
+    'label'     => 'Магазин',
+    'value'     => fn($row) => \yii_app\records\CityStore::findOne($row['store_id'])->name ?? $row['store_id'],
+];
+$columns[] = ['attribute' => 'category', 'label' => 'Категория'];
+$columns[] = ['attribute' => 'subcategory', 'label' => 'Подкатегория'];
+$columns[] = ['attribute' => 'species', 'label' => 'Вид'];
+$columns[] = ['attribute' => 'product_id', 'label' => 'GUID товара'];
+$columns[] =['attribute' => 'product_id', 'label' => 'Имя Товара',
+    'value' => function ($data) {
+        return \yii_app\records\Products1c::findOne($data['product_id'])->name ?? null;
+    },
 ];
+$columns[] = [
+    'attribute' => 'forecast_month_pieces',
+    'label'     => 'Прогноз на месяц (шт.)',
+    'format'    => ['decimal', 2],
+    'pageSummary' => true,
+];
+
+
+foreach ($weekFields as $field) {
+    $weekNum = (int) substr($field, 4);
+    $columns[] = [
+        'attribute' => $field,
+        'label'     => "Неделя {$weekNum}",
+        'format'    => ['decimal', 2],
+        'hAlign'    => 'center',
+        'pageSummary' => true,
+    ];
+}
+
 
 echo GridView::widget([
     'dataProvider' => $dataProvider,
index caf31573818ae02cc29f1f9f82dcc6ed5cd27a9b..40852698f4769868a24a92776cc79a91e2a8a1b3 100644 (file)
@@ -10,7 +10,7 @@
         use yii_app\records\CityStore;
         use yii_app\records\Products1cNomenclature;
         ?>
-        <h1 class="ms-3 mb-4"><?= Html::encode("РаÑ\81Ñ\87еÑ\82 Ð¿Ñ\80огноза Ñ\82оваÑ\80ов Ð²Ð½Ñ\83Ñ\82Ñ\80и Ð²Ð¸Ð´Ð° (month_goods_sales_share)") ?></h1>
+        <h1 class="ms-3 mb-4"><?= Html::encode("РаÑ\81Ñ\87еÑ\82 Ð´Ð¾Ð»Ð¸ Ð¿Ñ\80одаж Ð½ÐµÐ´ÐµÐ»Ð¸ Ð²Ð½Ñ\83Ñ\82Ñ\80и Ð²Ð¸Ð´Ð° (week_sale_share") ?></h1>
         <?php
         $form = ActiveForm::begin(['method' => 'get']); ?>
         <div class="row p-3">