From: fomichev Date: Mon, 26 May 2025 12:55:19 +0000 (+0300) Subject: Вывод 1 этапа по категориям X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=b2b774a1b6f72c54a89b0a0920c12a057cc890f9;p=erp24_rep%2Fyii-erp24%2F.git Вывод 1 этапа по категориям --- diff --git a/erp24/controllers/AutoPlannogrammaController.php b/erp24/controllers/AutoPlannogrammaController.php index 57b6fad8..fa70fd18 100644 --- a/erp24/controllers/AutoPlannogrammaController.php +++ b/erp24/controllers/AutoPlannogrammaController.php @@ -238,6 +238,8 @@ class AutoPlannogrammaController extends BaseController $flatData[] = [ 'store_id' => (string)$storeId, 'category' => $row['category'] ?? null, + 'base_total_store' => $row['base_total_store'] ?? null, + 'new_total_store' => $row['new_total_store'] ?? null, 'total_sum' => $row['total_sum'] ?? null, 'percent' => $row['percent'] ?? null, ]; diff --git a/erp24/services/AutoPlannogrammaService.php b/erp24/services/AutoPlannogrammaService.php index 0b5d8372..0fe26cbe 100644 --- a/erp24/services/AutoPlannogrammaService.php +++ b/erp24/services/AutoPlannogrammaService.php @@ -116,6 +116,7 @@ class AutoPlannogrammaService 'total_sum' => 'main.total_sum', 'percent' => new Expression('ROUND(CAST(main.total_sum AS DECIMAL) / NULLIF(totals.total, 0), 4)'), 'type' => new Expression(':type', ['type' => $type]), + 'totals_total' => 'totals.total' ]) ->from([ 'main' => (new Query()) @@ -150,19 +151,60 @@ class AutoPlannogrammaService ->andWhere(['<=', "$alias.date", (new \DateTime($month3 . '-01'))->modify('last day of this month')->format('Y-m-d')]) ->groupBy(['ex.entity_id']), ], 'main.ex_entity_id = totals.store_id') - ->orderBy('category'); + ->orderBy('category'); // Выполнение запроса и форматирование $rows = $query->all(); - $result = []; - foreach ($rows as $row) { - $storeId = $row['store_id']; - $result[$storeId][] = [ - 'category' => $row['category'], - 'total_sum' => $row['total_sum'], - 'percent' => $row['percent'], - 'type' => $row['type'], + $months = [$month3 => 3]; + $months[$month2] = 2; + $months[$month1] = 1; + + $componentAdds = []; + $componentAddsSumAll = []; + + foreach ($storeIds as $storeId) { + foreach ($months as $m => $weight) { + [$year, $mon] = explode('-', $m); + $items = $this->getUnmarkedProductsComponents( + (int)$storeId, + (int)$mon, + (int)$year, + $type + ); + $sums = $this->sumUnmarkedProductsComponentsByCategory($items, $type); + + foreach ($sums as $sumRow) { + $sid = $sumRow['store_id']; + $cat = $sumRow['category']; + $val = $sumRow['sum'] * $weight; + + $componentAdds[$sid][$cat] = ($componentAdds[$sid][$cat] ?? 0) + $val; + $componentAddsSumAll[$sid] = ($componentAddsSumAll[$sid] ?? 0) + $val; + } + } + } + + $result = []; + foreach ($rows as $r) { + $sid = $r['store_id']; + $baseTotal = (float)$r['totals_total']; + $addAllCategories = $componentAddsSumAll[$sid] ?? 0.0; + $newDenominator = $baseTotal + $addAllCategories; + + $newTotal = (float)$r['total_sum'] + + ($componentAdds[$sid][$r['category']] ?? 0); + $percent = $newDenominator > 0 + ? round($newTotal / $newDenominator, 4) + : 0.0; + + $result[$sid][] = [ + 'category' => $r['category'], + 'total_sum' => $newTotal, + 'percent' => $percent, + 'type' => $r['type'], + 'new_total_store' => $newDenominator, + 'base_total_store' => $baseTotal ]; } diff --git a/erp24/views/auto-plannogramma/1.php b/erp24/views/auto-plannogramma/1.php index 68f18ce9..e7c7432a 100644 --- a/erp24/views/auto-plannogramma/1.php +++ b/erp24/views/auto-plannogramma/1.php @@ -73,7 +73,9 @@ return CityStore::findOne($data['store_id'])->name ?? null; }], ['attribute' => 'category', 'label' => 'Категория'], - ['attribute' => 'total_sum', 'label' => 'Сумма', 'format' => ['decimal', 2]], + ['attribute' => 'base_total_store', 'label' => 'Сумма продаж', 'format' => ['decimal', 2]], + ['attribute' => 'new_total_store', 'label' => 'Сумма с немаркированными', 'format' => ['decimal', 2]], + ['attribute' => 'total_sum', 'label' => 'Сумма категории', 'format' => ['decimal', 2]], ['attribute' => 'percent', 'label' => 'Доля', 'format' => ['percent', 2]], ], ]); ?>