From: marina Date: Tue, 17 Jun 2025 12:19:33 +0000 (+0300) Subject: ERP-360 Сборка страницы автопм X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=64ad639c8921187cd734b99c38f72062606d3228;p=erp24_rep%2Fyii-erp24%2F.git ERP-360 Сборка страницы автопм --- diff --git a/erp24/commands/CronController.php b/erp24/commands/CronController.php index 8392ff48..bfc58c15 100644 --- a/erp24/commands/CronController.php +++ b/erp24/commands/CronController.php @@ -1597,17 +1597,37 @@ class CronController extends Controller continue; } + $existingRecords = []; + $records = Autoplannogramma::find() + ->where([ + 'month' => $month, + 'year' => $year, + 'store_id' => $store->id, + ]) + ->andWhere(['week' => array_unique(array_column($forecast, 'week'))]) + ->all(); + + foreach ($records as $record) { + $existingRecords[$record->week . '_' . $record->product_id] = $record; + } + foreach ($forecast as $item) { - $model = new Autoplannogramma(); - $model->month = $month; - $model->year = $year; - $model->week = $item['week']; - $model->product_id = $item['product_id']; - $model->store_id = $item['store_id']; + $key = $item['week'] . '_' . $item['product_id']; + $model = $existingRecords[$key] ?? null; + + if (!$model) { + $model = new Autoplannogramma(); + $model->month = $month; + $model->year = $year; + $model->week = $item['week']; + $model->product_id = $item['product_id']; + $model->store_id = $item['store_id']; + $model->is_archive = false; + $model->capacity_type = 1; + } + $model->quantity = $item['forecast_week_pieces']; $model->quantity_forecast = $item['forecast_week_pieces']; - $model->is_archive = false; - $model->capacity_type = 1; if (!$model->save()) { $errors = [];