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

index 8392ff4897fff09de814a87b77f9a4278a31dd90..bfc58c157d9b8322159faf34bed3c070fd129a06 100644 (file)
@@ -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 = [];