From: fomichev Date: Tue, 3 Jun 2025 07:37:53 +0000 (+0300) Subject: Прогноз по неделям для вида X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=b3f2d1fc71ee1385ed5e204a66cb66db5912be10;p=erp24_rep%2Fyii-erp24%2F.git Прогноз по неделям для вида --- diff --git a/erp24/controllers/AutoPlannogrammaController.php b/erp24/controllers/AutoPlannogrammaController.php index c362cba8..dba2f6da 100644 --- a/erp24/controllers/AutoPlannogrammaController.php +++ b/erp24/controllers/AutoPlannogrammaController.php @@ -1365,10 +1365,10 @@ class AutoPlannogrammaController extends BaseController $weeklySalesForecast = $service->calculateWeeklyProductForecastPieces($productForecastSpecies, $weeklySales); - //var_dump($weeklySalesForecast); die(); + $weeklySalesForecastFormated = $service->pivotWeeklyForecast($weeklySalesForecast); - $flatData = array_filter($weeklySalesForecast, function ($row) use ($filters) { + $flatData = array_filter($weeklySalesForecastFormated, function ($row) use ($filters) { foreach ($filters as $key => $value) { if (empty($value)) continue; if (!isset($row[$key])) continue; diff --git a/erp24/services/AutoPlannogrammaService.php b/erp24/services/AutoPlannogrammaService.php index 74ea14ab..69f911ed 100644 --- a/erp24/services/AutoPlannogrammaService.php +++ b/erp24/services/AutoPlannogrammaService.php @@ -1221,6 +1221,7 @@ class AutoPlannogrammaService 'subcategory' => $sub, 'species' => $spec, 'product_id' => $pid, + 'forecast_month_pieces' => $piecesMon, 'forecast_week_pieces' => $forecastWeekPieces, ]; } @@ -1230,6 +1231,54 @@ class AutoPlannogrammaService } + public function pivotWeeklyForecast(array $flatRows): array + { + $grouped = []; + + foreach ($flatRows as $row) { + $key = implode('|', [ + $row['store_id'], + $row['category'], + $row['subcategory'], + $row['species'], + $row['product_id'], + ]); + + if (!isset($grouped[$key])) { + $grouped[$key] = [ + 'store_id' => $row['store_id'], + 'category' => $row['category'], + 'subcategory' => $row['subcategory'], + 'species' => $row['species'], + 'product_id' => $row['product_id'], + 'forecast_month_pieces' => $row['forecast_month_pieces'], + ]; + } + + $weekNum = (int)$row['week']; + $fieldName = 'week' . $weekNum; + + $grouped[$key][$fieldName] = (float)$row['forecast_week_pieces']; + } + + $allWeekFields = []; + foreach ($flatRows as $row) { + $allWeekFields['week' . ((int)$row['week'])] = true; + } + $allWeekFields = array_keys($allWeekFields); + + foreach ($grouped as &$group) { + foreach ($allWeekFields as $fieldName) { + if (! isset($group[$fieldName])) { + $group[$fieldName] = 0.0; + } + } + } + unset($group); + + return array_values($grouped); + } + /** * Исторический недельный отчёт и доли по видам с учётом store_id. diff --git a/erp24/views/auto-plannogramma/week-sales-products_forecast.php b/erp24/views/auto-plannogramma/week-sales-products_forecast.php index 321c84fc..9a7a68e5 100644 --- a/erp24/views/auto-plannogramma/week-sales-products_forecast.php +++ b/erp24/views/auto-plannogramma/week-sales-products_forecast.php @@ -1,4 +1,10 @@ getModels(); +if (empty($allModels)) { + echo "Нет данных для отображения."; + return; +} ?>
-

+

'get']); ?>
@@ -95,38 +101,49 @@ 'week', - 'label' => 'Неделя', - ], - [ - 'attribute' => 'store_id', - 'label' => 'Магазин', - 'value' => function ($data) { - return CityStore::findOne($data['store_id'])->name ?? $data['store_id']; - }, - ], - [ - 'attribute' => 'category', - 'label' => 'Категория', - ], - [ - 'attribute' => 'subcategory', - 'label' => 'Подкатегория', - ], - [ - 'attribute' => 'species', - 'label' => 'Тип', - ],['attribute'=>'week','label'=>'Неделя'], - [ - 'attribute'=>'store_id','label'=>'Магазин', - 'value'=>function($data){ return CityStore::findOne($data['store_id'])->name ?? $data['store_id']; } - ], +$first = $allModels[0]; +$weekFields = array_filter(array_keys($first), fn($k) => strncmp($k, 'week', 4) === 0); - ['attribute'=>'product_id','label'=>'GUID Товара'], - ['attribute'=>'forecast_week_pieces','label'=>'Прогноз (шт.)','format'=>['decimal',2]], +usort($weekFields, function($a, $b) { + return (int) substr($a, 4) <=> (int) substr($b, 4); +}); + + +$columns = []; + +$columns[] = [ + 'attribute' => 'store_id', + 'label' => 'Магазин', + 'value' => fn($row) => \yii_app\records\CityStore::findOne($row['store_id'])->name ?? $row['store_id'], +]; +$columns[] = ['attribute' => 'category', 'label' => 'Категория']; +$columns[] = ['attribute' => 'subcategory', 'label' => 'Подкатегория']; +$columns[] = ['attribute' => 'species', 'label' => 'Вид']; +$columns[] = ['attribute' => 'product_id', 'label' => 'GUID товара']; +$columns[] =['attribute' => 'product_id', 'label' => 'Имя Товара', + 'value' => function ($data) { + return \yii_app\records\Products1c::findOne($data['product_id'])->name ?? null; + }, ]; +$columns[] = [ + 'attribute' => 'forecast_month_pieces', + 'label' => 'Прогноз на месяц (шт.)', + 'format' => ['decimal', 2], + 'pageSummary' => true, +]; + + +foreach ($weekFields as $field) { + $weekNum = (int) substr($field, 4); + $columns[] = [ + 'attribute' => $field, + 'label' => "Неделя {$weekNum}", + 'format' => ['decimal', 2], + 'hAlign' => 'center', + 'pageSummary' => true, + ]; +} + echo GridView::widget([ 'dataProvider' => $dataProvider, diff --git a/erp24/views/auto-plannogramma/week-sales-species-share.php b/erp24/views/auto-plannogramma/week-sales-species-share.php index caf31573..40852698 100644 --- a/erp24/views/auto-plannogramma/week-sales-species-share.php +++ b/erp24/views/auto-plannogramma/week-sales-species-share.php @@ -10,7 +10,7 @@ use yii_app\records\CityStore; use yii_app\records\Products1cNomenclature; ?> -

+

'get']); ?>