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();
$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);
}
}
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],
];
}