]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
Вывод 1 этапа по категориям origin/feature_fomichev_erp-419_month_clear_goal
authorfomichev <vladimir.fomichev@erp-flowers.ru>
Mon, 26 May 2025 12:55:19 +0000 (15:55 +0300)
committerfomichev <vladimir.fomichev@erp-flowers.ru>
Mon, 26 May 2025 12:55:19 +0000 (15:55 +0300)
erp24/controllers/AutoPlannogrammaController.php
erp24/services/AutoPlannogrammaService.php
erp24/views/auto-plannogramma/1.php

index 57b6fad8cc4f75d4bfef2f0c92903a1e6b793f65..fa70fd18acbbcf5b0e6c39093a35941feca2d177 100644 (file)
@@ -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,
                     ];
index 0b5d8372f95aafb3053e66e0a6dbce165ea4eaeb..0fe26cbef477b540fd580b759d66cb8b8d3b5303 100644 (file)
@@ -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
             ];
         }
 
index 68f18ce9d98263103ede252ebcab3983aae2228d..e7c7432a9851c70cfca689c1bbab18e00e3cb37f 100644 (file)
@@ -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]],
     ],
 ]); ?>