From: marina Date: Tue, 17 Jun 2025 12:36:39 +0000 (+0300) Subject: Merge branch 'develop' into feature_zozirova_erp-360_autoplannogramma_build X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=1870aec661445209e943cc2f7261e93023e6fc43;p=erp24_rep%2Fyii-erp24%2F.git Merge branch 'develop' into feature_zozirova_erp-360_autoplannogramma_build # Conflicts: # erp24/commands/CronController.php --- 1870aec661445209e943cc2f7261e93023e6fc43 diff --cc erp24/commands/CronController.php index f14d68b0,9aa5e1bb..26184896 --- a/erp24/commands/CronController.php +++ b/erp24/commands/CronController.php @@@ -1597,40 -1598,30 +1599,55 @@@ class CronController extends Controlle Yii::error("Ошибка при расчёте прогноза: " . $e->getMessage(), __METHOD__); continue; } + $writeOffsForecastMap = []; + foreach ($writeOffsForecast as $itemForecast) { + $itemWeek = $itemForecast['week']; + $itemGuid = $itemForecast['product_id']; + $writeoffsForecast = $itemForecast['forecast']; + $writeOffsForecastMap[$itemWeek][$itemGuid] = $writeoffsForecast; + + } + $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']; - $model->quantity = $item['forecast_week_pieces']; - $model->quantity_forecast = $item['forecast_week_pieces']; - $model->is_archive = false; - $model->capacity_type = 1; - if (isset($writeOffsForecastMap[$item['week']]) && isset($writeOffsForecastMap[$item['week']][$item['product_id']])) { - $model->writeoffs_forecast = $writeOffsForecastMap[$item['week']][$item['product_id']]; - } else { - $model->writeoffs_forecast = 0; + $key = $item['week'] . '_' . $item['product_id']; + $model = $existingRecords[$key] ?? null; + $this->stderr(implode("\n", $item), BaseConsole::FG_GREEN); + 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']; ++ if (isset($writeOffsForecastMap[$item['week']]) && isset($writeOffsForecastMap[$item['week']][$item['product_id']])) { ++ $model->writeoffs_forecast = $writeOffsForecastMap[$item['week']][$item['product_id']]; ++ } else { ++ $model->writeoffs_forecast = 0; ++ } + } + ++ ++ + $needsUpdate = $model->quantity_forecast != $item['forecast_week_pieces']; + + if ($needsUpdate) { + $model->quantity_forecast = $item['forecast_week_pieces']; } if (!$model->save()) {