From 72825eae8f15a33e954457a7c3a48748659d6e0b Mon Sep 17 00:00:00 2001 From: fomichev Date: Thu, 8 May 2025 15:08:09 +0300 Subject: [PATCH] =?utf8?q?=D0=A0=D0=B0=D1=81=D1=87=D0=B5=D1=82=20=D0=B4?= =?utf8?q?=D0=BE=D0=BB=D0=B5=D0=B9=20=D0=BD=D0=B5=D0=B4=D0=B5=D0=BB=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../AutoPlannogrammaController.php | 10 +- erp24/services/AutoPlannogrammaService.php | 102 ++++++++++-------- .../auto-plannogramma/control-species.php | 67 ++++-------- 3 files changed, 86 insertions(+), 93 deletions(-) diff --git a/erp24/controllers/AutoPlannogrammaController.php b/erp24/controllers/AutoPlannogrammaController.php index 0533a458..5c099c2a 100644 --- a/erp24/controllers/AutoPlannogrammaController.php +++ b/erp24/controllers/AutoPlannogrammaController.php @@ -344,22 +344,22 @@ class AutoPlannogrammaController extends BaseController } - $weeksShareResult = $service->getWeeksSpeciesShareOrWriteOff($dateFrom, $dateTo, $filters, null, 'writeOffs'); + $weeksShareResult = $service->getHistoricalWeeklySpeciesShare($model->month, $filters, null, 'writeOffs'); $weeksData = $weeksShareResult['weeksData']; - $weeksShareResult = $weeksShareResult['weeksShare']; + // $weeksShareResult = $weeksShareResult['weeksShare']; } -//var_dump($monthSpeciesGoalsMap); die(); +//var_dump($weeksData); die(); return $this->render('control-species', [ 'model' => $model, 'result' => $monthResult, 'weeksData' => $weeksData, - 'weeksShareResult' => $weeksShareResult, - 'weeksGoalResult' => $weeksGoalResult, + // 'weeksShareResult' => $weeksShareResult, + // 'weeksGoalResult' => $weeksGoalResult, 'totals' => $totals, 'storeList' => $storeList, 'monthsList' => $monthsList, diff --git a/erp24/services/AutoPlannogrammaService.php b/erp24/services/AutoPlannogrammaService.php index cab82f6d..74de0b0e 100644 --- a/erp24/services/AutoPlannogrammaService.php +++ b/erp24/services/AutoPlannogrammaService.php @@ -930,7 +930,7 @@ var_dump($totals); die(); $dateFrom = sprintf('%04d-%02d-01 00:00:00', $year, $month); $dateTo = date('Y-m-d H:i:s', strtotime($dateFrom . ' +1 month -1 second')); - + //var_dump($monthYear); die(); $stores = $this->getVisibleStores(); $storeIds = array_map(fn($s)=>$s->id, $stores); if (!empty($filters['store_id'])) { @@ -953,7 +953,7 @@ var_dump($totals); die(); } $query = (new Query())->select([ - 'week' => new Expression('$weekIndex'), + 'week' => new Expression((string)$ind), 'store_id' => 'ex.entity_id', 'category' => 'p1c.category', 'subcategory' => 'p1c.subcategory', @@ -1008,16 +1008,15 @@ var_dump($totals); die(); } /** - * Строит исторический недельный отчёт за аналогичные месяцы предыдущих лет (2020–year-1) - * и вычисляет долю каждой недели от исторического месячного итога по видам. + * Исторический недельный отчёт и доли по видам без вложенных циклов. * - * @param string $monthYear месяц-год в формате MM-YYYY, например '03-2025' - * @param array|null $filters опциональные фильтры ['store_id'=>...] - * @param array|null $productFilter опциональный фильтр по product_id - * @param string $type 'sales' или 'writeOffs' + * @param string $monthYear месяц-год в формате MM-YYYY + * @param array|null $filters + * @param array|null $productFilter + * @param string $type * @return array{ - * 'historicalWeekly' => array, // суммарные суммы за каждый week - * 'weeklyShare' => array // доли недель относительно исторического месяца + * 'historicalWeekly' => array, + * 'weeklyShare' => array * } */ public function getHistoricalWeeklySpeciesShare( @@ -1026,6 +1025,7 @@ var_dump($totals); die(); ?array $productFilter = null, string $type = 'sales' ): array { + [$monthStr, $yearStr] = explode('-', $monthYear); $month = (int)$monthStr; $year = (int)$yearStr; @@ -1033,57 +1033,73 @@ var_dump($totals); die(); $historical = []; for ($yr = 2020; $yr < $year; $yr++) { $mYear = sprintf('%02d-%d', $month, $yr); - $weekData = $this->getWeeklySpeciesDataForMonth($mYear, $filters, $productFilter, $type); - foreach ($weekData as $row) { - $idx = $row['week']; - $sid = $row['store_id']; - $cat = $row['category']; - $sub = $row['subcategory']; + $weeklyData = $this->getWeeklySpeciesDataForMonth($mYear, $filters, $productFilter, $type); + foreach ($weeklyData as $row) { + $idx = $row['week']; + $historical[$idx] ??= []; + $cat = $row['category']; + $sub = $row['subcategory']; $spec = $row['species']; - $historical[$idx][$sid][$cat][$sub][$spec] = - ($historical[$idx][$sid][$cat][$sub][$spec] ?? 0) - + $row['sum']; + + $historical[$idx][$cat][$sub][$spec] = + ($historical[$idx][$cat][$sub][$spec] ?? 0) + $row['sum']; } } + $dateFrom = sprintf('%04d-%02d-01 00:00:00', $year, $month); $dateTo = date('Y-m-d H:i:s', strtotime($dateFrom . ' +1 month -1 second')); $monthWeighted = $this->getMonthSpeciesShareOrWriteOffWeighted( $dateFrom, $dateTo, $filters, $productFilter, $type ); + $monthMap = []; foreach ($monthWeighted as $m) { - $monthMap[ - $m['store_id'] - ][ - $m['category'] - ][ - $m['subcategory'] - ][ - $m['species'] - ] = $m['total_sum']; + $cat = $m['category']; + $sub = $m['subcategory']; + $spec = $m['species']; + $monthMap[$cat][$sub][$spec] = ($monthMap[$cat][$sub][$spec] ?? 0) + $m['total_sum']; } - $flat = $this->flattenHistorical($historical); - $weeksShare = []; - foreach ($flat as $row) { - $w = $row['week']; - $sid = $row['store_id']; - $cat = $row['category']; - $sub = $row['subcategory']; - $spec = $row['species']; - $sum = $row['sum']; - - $monthSum = $monthMap[$sid][$cat][$sub][$spec] ?? 0; - if ($monthSum > 0) { - $weeksShare[$w][$sid][$cat][$sub][$spec] = round($sum / $monthSum, 4); + $flatRows = []; + foreach ($historical as $week => $byCat) { + foreach ($byCat as $cat => $bySub) { + foreach ($bySub as $sub => $bySpec) { + foreach ($bySpec as $spec => $sumWeek) { + $sumMonth = $monthMap[$cat][$sub][$spec] ?? 0; + if ($sumMonth <= 0) { + continue; + } + $flatRows[] = [ + 'week' => $week, + 'category' => $cat, + 'subcategory' => $sub, + 'species' => $spec, + 'percent' => round($sumWeek / $sumMonth, 4), + ]; + } + } } } + usort($flatRows, function(array $a, array $b): int { + $cmp = strcmp($a['category'], $b['category']); + if ($cmp !== 0) { + return $cmp; + } + $cmp = strcmp($a['subcategory'], $b['subcategory']); + if ($cmp !== 0) { + return $cmp; + } + $cmp = strcmp($a['species'], $b['species']); + if ($cmp !== 0) { + return $cmp; + } + return $a['week'] <=> $b['week']; + }); return [ - 'historicalWeekly' => $historical, - 'weeklyShare' => $weeksShare, + 'weeksData' => $flatRows, ]; } diff --git a/erp24/views/auto-plannogramma/control-species.php b/erp24/views/auto-plannogramma/control-species.php index 2ecafb34..b73ac84f 100644 --- a/erp24/views/auto-plannogramma/control-species.php +++ b/erp24/views/auto-plannogramma/control-species.php @@ -93,50 +93,27 @@ use yii_app\records\Products1c;

Результаты по неделям

- $weekRows): ?> -

Неделя

- -

Нет данных за эту неделю.

- - - - - - - - - - - - - - - - - - - - - - - - -
КатегорияПодкатегорияВид товараСуммаДоля неделиЦель недели
formatter->asDecimal($row['total_sum'], 2) ?> - formatter->asPercent( - $weeksShareResult[$weekNum][$row['category']][$row['subcategory']][$row['species']], - 2 - ) - : '-' ?> - - formatter->asPercent( - $weeksGoalResult[$weekNum][$row['category']][$row['subcategory']][$row['species']], - 2 - ) - : '-' ?> -
- - + + + + + + + + + + + + + + + + + + + + + +
КатегорияПодкатегорияВидНеделяДоля (%)
formatter->asPercent($r['percent'], 1) ?>
-- 2.39.5