From: marina Date: Tue, 10 Jun 2025 09:32:22 +0000 (+0300) Subject: ERP-360 Сборка страницы автопм X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=5ed554cdbb78a84f2d1218056407e574aef93f63;p=erp24_rep%2Fyii-erp24%2F.git ERP-360 Сборка страницы автопм --- 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], ]; }