]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
ERP-360 Сборка страницы автопм
authormarina <m.zozirova@gmail.com>
Tue, 10 Jun 2025 09:32:22 +0000 (12:32 +0300)
committermarina <m.zozirova@gmail.com>
Tue, 10 Jun 2025 09:32:22 +0000 (12:32 +0300)
erp24/commands/CronController.php
erp24/records/Autoplannogramma.php

index 504c82539a269e5606000d8726b545552500e520..830f0cb23d83e8f2dc8f75aafc31d1b63da7c0be 100644 (file)
@@ -1561,29 +1561,41 @@ class CronController extends Controller
 
         return ExitCode::OK;
     }
-
     public function actionAutoplannogrammaCalculate()
     {
-        $month = date('m');
-        $year = date('Y');
-        $planDate = $year . '-' . str_pad($month, 2, '0', STR_PAD_LEFT) . '-01';
-        $this->stdout("Начало расчетов автопланнограммы\n", BaseConsole::FG_GREEN);
+        $date = new \DateTime();
+        $date->modify('+2 months');
+
+        $planDate = $date->format('Y-m-01');
+        $month = $date->format('m');
+        $year = $date->format('Y');
+
+        $this->stdout("Начало расчетов автопланограммы для $planDate\n", BaseConsole::FG_GREEN);
+
         $service = new AutoPlannogrammaService();
         $stores = CityStore::findAll(['visible' => CityStore::IS_VISIBLE]);
+
         foreach ($stores as $store) {
-            $this->stdout("Начало рассчетов автопланограммы для магазина  $store->id\n", BaseConsole::FG_GREEN);
-            $forecast = $service->calculateFullForecastForWeek([
-                'month' => $month,
-                'year' => $year,
-                'type' => AutoPlannogrammaService::TYPE_SALES,
-                'store_id' => $store->id,
-                'category' => null,
-                'subcategory' => null,
-                'species' => null,
-                'plan_date' => $planDate
-            ]);
-            $this->stdout("Рассчитана автопланограмма для магазина  $store->id\n", BaseConsole::FG_GREEN);
-//            $this->stdout(print_r($forecast, true)); die();
+            $this->stdout("Начало расчетов автопланограммы для магазина ID: {$store->id} ({$store->name})\n", BaseConsole::FG_YELLOW);
+
+            try {
+                $forecast = $service->calculateFullForecastForWeek([
+                    'month' => $month,
+                    'year' => $year,
+                    'type' => AutoPlannogrammaService::TYPE_SALES,
+                    'store_id' => $store->id,
+                    'category' => null,
+                    'subcategory' => null,
+                    'species' => null,
+                    'plan_date' => $planDate
+                ]);
+
+                $this->stdout("Рассчитана автопланограмма для магазина {$store->name}\n", BaseConsole::FG_GREEN);
+            } catch (\Throwable $e) {
+                $this->stderr("Ошибка при расчёте прогноза: {$e->getMessage()}\n", BaseConsole::FG_RED);
+                Yii::error("Ошибка при расчёте прогноза: " . $e->getMessage(), __METHOD__);
+                continue;
+            }
 
             foreach ($forecast as $item) {
                 $model = new Autoplannogramma();
@@ -1594,15 +1606,25 @@ class CronController extends Controller
                 $model->quantity = $item['forecast_week_pieces'];
                 $model->quantity_forecast = $item['forecast_week_pieces'];
                 $model->is_archive = false;
-                $model->capacity_type = 'max';
+                $model->capacity_type = 1;
 
+                foreach ($item as $key => $value) {
+                    $this->stdout($key . ": {$value}\n", BaseConsole::FG_GREEN);
+                }
                 if (!$model->save()) {
-                    Yii::error("Ошибка сохранения: " . json_encode($model->errors), __METHOD__);
-                } else {
-                    Yii::error("Ошибка сохранения: " . json_encode($model->errors), __METHOD__);
+                    $errors = [];
+                    foreach ($model->getErrors() as $attr => $attrErrors) {
+                        foreach ($attrErrors as $error) {
+                            $errors[] = "$attr: $error";
+                        }
+                    }
+                    $errorMessage = implode('; ', $errors);
+                    $this->stderr("Ошибка при сохранении модели: $errorMessage\n", BaseConsole::FG_RED);
+                    Yii::error("Ошибка сохранения Autoplannogramma: $errorMessage", __METHOD__);
                 }
             }
-            Yii::error("Сохранена автопланограмма для магазина $store->name");
+            $this->stdout("Сохранена автопланограмма для магазина {$store->name}\n", BaseConsole::FG_GREEN);
         }
+        $this->stdout("Расчет и сохранение автопланограммы завершены\n", BaseConsole::FG_GREEN);
     }
 }
index bc399356d8f375691db6d1b34e17228d185af182..63b15580b1ce1789239e09111d6d2e823cc37527 100644 (file)
@@ -43,9 +43,11 @@ class Autoplannogramma extends \yii\db\ActiveRecord
     public function rules()
     {
         return [
-            [['week', 'month', 'year', 'store_id', 'capacity_type', 'quantity', 'quantity_forecast', 'created_by', 'updated_by'], 'integer'],
+            [['week', 'month', 'year', 'store_id', 'capacity_type', 'created_by', 'updated_by'], 'integer'],
             [['is_archive', 'auto_forecast'], 'boolean'],
+            [[ 'quantity', 'quantity_forecast'], 'number'],
             [['created_at', 'updated_at'], 'safe'],
+            [['auto_forecast'], 'default', 'value' => true],
             [['product_id'], 'string', 'max' => 255],
         ];
     }