]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
Расчет планограм из вебинтерфейса - обновление feature_fomichev_erp_445_OS_autoplannogramm origin/feature_fomichev_erp_445_OS_autoplannogramm
authorVladimir Fomichev <vladimir.fomichev@erp-flowers.ru>
Mon, 28 Jul 2025 12:33:49 +0000 (15:33 +0300)
committerVladimir Fomichev <vladimir.fomichev@erp-flowers.ru>
Mon, 28 Jul 2025 12:33:49 +0000 (15:33 +0300)
erp24/controllers/AutoPlannogrammaController.php

index bd97ce38ae797879e64fecf20b941231e1f1e656..45db5b4ca3061d115d627cd55b9182f7b60cab60 100644 (file)
@@ -1763,8 +1763,17 @@ class AutoPlannogrammaController extends BaseController
                 $writeOffsForecast = $service->getWeeklyProductsWriteoffsForecast($month, $year, $forecast, $store_id);
                 $salesForecast = $service->getWeeklyBouquetProductsSalesForecast($month, $year, $store_id);
 
+                $weeks = array_unique(array_column($forecast, 'week'));
+                $productIds = array_unique(array_column($forecast, 'product_id'));
+
                 $existing = Autoplannogramma::find()
-                    ->where(['month' => $month, 'year' => $year, 'store_id' => $store_id, 'week' => array_unique(array_column($forecast, 'week'))])
+                    ->where([
+                        'month' => $month,
+                        'year' => $year,
+                        'store_id' => $store_id,
+                    ])
+                    ->andWhere(['in', 'week', $weeks])
+                    ->andWhere(['in', 'product_id', $productIds])
                     ->indexBy(fn($r) => $r->week . '_' . $r->product_id)
                     ->all();
 
@@ -1772,13 +1781,14 @@ class AutoPlannogrammaController extends BaseController
                 foreach ($forecast as $item) {
                     $key = $item['week'] . '_' . $item['product_id'];
                     $model = $existing[$key] ?? new Autoplannogramma();
+
                     $quantity = (float)($item['forecast_week_pieces'] ?? 0);
                     $productId = $item['product_id'];
                     $week = $item['week'];
 
-
                     $details = [];
                     $total = $quantity;
+
                     if (!empty($writeOffsForecast[$productId][$week]['writeOffs'])) {
                         $wo = $writeOffsForecast[$productId][$week]['writeOffs'];
                         $details['writeOffs']['quantity'] = $wo;
@@ -1786,6 +1796,7 @@ class AutoPlannogrammaController extends BaseController
                     } else {
                         $details['writeOffs']['quantity'] = 0;
                     }
+
                     foreach (['offline', 'online', 'marketplace'] as $type) {
                         $block = ['share' => 0, 'quantity' => 0, 'groups' => []];
                         if (isset($salesForecast[$store_id][$productId][$type]) && is_array($salesForecast[$store_id][$productId][$type])) {
@@ -1799,6 +1810,7 @@ class AutoPlannogrammaController extends BaseController
                         }
                         $details[$type] = $block;
                     }
+
                     $details['forecast'] = ['quantity' => $quantity];
                     $total = (float)sprintf('%.2f', $total);
 
@@ -1815,8 +1827,10 @@ class AutoPlannogrammaController extends BaseController
                         'modify' => null,
                         'total' => ceil($total),
                     ], false);
-                    $model->save();
 
+                    if (!$model->save()) {
+                        Yii::error("Ошибка сохранения планограммы: " . json_encode($model->errors), __METHOD__);
+                    }
 
                     $rows[] = [
                         'week' => $week,
@@ -1827,6 +1841,7 @@ class AutoPlannogrammaController extends BaseController
                     ];
                 }
 
+
             } catch (Throwable $e) {
                 Yii::error($e->getMessage(), __METHOD__);
                 throw $e;