From 5ed554cdbb78a84f2d1218056407e574aef93f63 Mon Sep 17 00:00:00 2001 From: marina Date: Tue, 10 Jun 2025 12:32:22 +0300 Subject: [PATCH] =?utf8?q?ERP-360=20=D0=A1=D0=B1=D0=BE=D1=80=D0=BA=D0=B0?= =?utf8?q?=20=D1=81=D1=82=D1=80=D0=B0=D0=BD=D0=B8=D1=86=D1=8B=20=D0=B0?= =?utf8?q?=D0=B2=D1=82=D0=BE=D0=BF=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- erp24/commands/CronController.php | 68 ++++++++++++++++++++---------- erp24/records/Autoplannogramma.php | 4 +- 2 files changed, 48 insertions(+), 24 deletions(-) diff --git a/erp24/commands/CronController.php b/erp24/commands/CronController.php index 504c8253..830f0cb2 100644 --- a/erp24/commands/CronController.php +++ b/erp24/commands/CronController.php @@ -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); } } diff --git a/erp24/records/Autoplannogramma.php b/erp24/records/Autoplannogramma.php index bc399356..63b15580 100644 --- a/erp24/records/Autoplannogramma.php +++ b/erp24/records/Autoplannogramma.php @@ -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], ]; } -- 2.39.5