]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
Вывод прогноза по товарам
authorfomichev <vladimir.fomichev@erp-flowers.ru>
Tue, 13 May 2025 15:10:50 +0000 (18:10 +0300)
committerfomichev <vladimir.fomichev@erp-flowers.ru>
Tue, 13 May 2025 15:10:50 +0000 (18:10 +0300)
erp24/controllers/AutoPlannogrammaController.php
erp24/services/AutoPlannogrammaService.php
erp24/views/auto-plannogramma/control-species.php

index 3d8f9b77d402d985493b132fdf112e5dd0ce8bd7..0164aeaeab6362ad241c626629604f69ccebf25e 100644 (file)
@@ -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,
index 64cb910a6fe8a4435e5268b736789155db9b4c82..d236060a7aa6222f3d9762e1502d282656bb76da 100644 (file)
@@ -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;
+
     }
 
+
+
 }
index 0ce69abd5fd0814c422594e8326329c6cd75c8f2..d2aa771194b2b0a03b59e39d1e7c9a685f43ee0f 100644 (file)
@@ -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;
             </tbody>
         </table>
     <?php endif; ?>
+    <?php if (!empty($weeksProductForecast)): ?>
+        <h2>Прогноз по неделям по неделям</h2>
+    <table class="table table-bordered table-striped">
+        <thead>
+        <tr>
+            <th>Категория</th>
+            <th>Подкатегория</th>
+            <th>Вид</th>
+            <th>Товар (ID)</th>
+            <th>Неделя</th>
+            <th>Цель</th>
+            <th>Цена</th>
+            <th>Прогноз</th>
+        </tr>
+        </thead>
+        <tbody>
+        <?php foreach ($weeksProductForecast as $row): ?>
+            <tr>
+                <td><?= Html::encode($row['category']) ?></td>
+                <td><?= Html::encode($row['subcategory']) ?></td>
+                <td><?= Html::encode($row['species']) ?></td>
+                <td>
+                    <?= Html::encode($row['name']) ?>
+                    <br><small class="text-muted">(<?= Html::encode($row['product_id']) ?>)</small>
+                </td>
+                <td><?= Html::encode($row['week']) ?></td>
+                <td><?= Html::encode($row['goal']) ?></td>
+                <td><?= Html::encode($row['price']) ?></td>
+                <td><?= Html::encode($row['forecast']) ?></td>
+            </tr>
+        <?php endforeach; ?>
+        </tbody>
+    </table>
+    <?php endif; ?>
 </div>