'subcategory' => $sub,
'species' => $spec,
'product_id' => $pid,
+ 'forecast_month_pieces' => $piecesMon,
'forecast_week_pieces' => $forecastWeekPieces,
];
}
}
+ 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.
<?php
+/* @var yii\data\ArrayDataProvider $dataProvider */
+$allModels = $dataProvider->getModels();
+if (empty($allModels)) {
+ echo "Нет данных для отображения.";
+ return;
+}
?>
<div class="filter-form" style="margin-bottom: 20px;">
<?php use kartik\date\DatePicker;
use yii_app\records\CityStore;
use yii_app\records\Products1cNomenclature;
?>
- <h1 class="ms-3 mb-4"><?= Html::encode("Расчет прогноза товаров внутри вида (month_goods_sales_share)") ?></h1>
+ <h1 class="ms-3 mb-4"><?= Html::encode("Расчет прогноза товаров внутри вида по неделям (week_goods_sales_forecast)") ?></h1>
<?php
$form = ActiveForm::begin(['method' => 'get']); ?>
<div class="row p-3">
<?php
-$columns = [
- [
- 'attribute' => '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,