$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();
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;
} 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])) {
}
$details[$type] = $block;
}
+
$details['forecast'] = ['quantity' => $quantity];
$total = (float)sprintf('%.2f', $total);
'modify' => null,
'total' => ceil($total),
], false);
- $model->save();
+ if (!$model->save()) {
+ Yii::error("Ошибка сохранения планограммы: " . json_encode($model->errors), __METHOD__);
+ }
$rows[] = [
'week' => $week,
];
}
+
} catch (Throwable $e) {
Yii::error($e->getMessage(), __METHOD__);
throw $e;