]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
Правки по подкатегориям - расчет с раскрытием товаров
authorfomichev <vladimir.fomichev@erp-flowers.ru>
Fri, 30 May 2025 06:51:37 +0000 (09:51 +0300)
committerfomichev <vladimir.fomichev@erp-flowers.ru>
Fri, 30 May 2025 06:51:37 +0000 (09:51 +0300)
erp24/controllers/AutoPlannogrammaController.php
erp24/services/AutoPlannogrammaService.php

index 4b86952ae5bfcd69d5b492ccfc19052e00cdd583..ba72ce2f8d556ceb19c0145ffdfe666dabd2045a 100644 (file)
@@ -522,8 +522,23 @@ class AutoPlannogrammaController extends BaseController
             $service = new AutoPlannogrammaService();
 
             $data = $service->getMonthSubcategoryShareOrWriteOff($filters['plan_date'], $filters, $filters['type']);
+            $flatData = [];
+            //var_dump($data);die();
+            foreach ($data as $storeId => $categories) {
+                foreach ($categories as $row) {
+                    $flatData[] = [
+                        'store_id' => (string)$storeId,
+                        'category' => $row['category'] ?? null,
+                        'subcategory' => $row['subcategory'] ?? 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,
+                    ];
+                }
+            }
 
-            $flatData = array_filter($data, function ($row) use ($filters) {
+            $flatData = array_filter($flatData, function ($row) use ($filters) {
                 foreach ($filters as $key => $value) {
                     if (empty($value)) continue;
                     if (!isset($row[$key])) continue;
@@ -535,6 +550,7 @@ class AutoPlannogrammaController extends BaseController
                 return true;
             });
 
+
             $dataProvider = new ArrayDataProvider([
                 'allModels' => $flatData,
                 'pagination' => ['pageSize' => 100],
index d3aabeafb0bba92c6ae011f37a0f2d4d4094d1ae..f6fa4cd278d50112d8cce0300e0bf053787c5a6d 100644 (file)
@@ -187,7 +187,7 @@ class AutoPlannogrammaService
                 foreach ($items as $it) {
                     $allComponentsProductIds[$storeId][] = $it['product_id'];
                 }
-                $sums = $this->sumProductsComponentsByCategory($items, $type);
+                $sums = $this->sumProductsComponentsByGroup($items, $type, 'category');
 
                 $allComponentsProductIds[$storeId] = array_values(array_unique($allComponentsProductIds[$storeId]));
                 foreach ($sums as $sumRow) {
@@ -316,6 +316,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())
@@ -357,7 +358,83 @@ class AutoPlannogrammaService
             )
             ->orderBy('category, subcategory');
 
-        $result = $query->all();
+        $rows = $query->all();
+        $periods = [
+            (clone $base)->modify('-2 year'),
+            (clone $base)->modify('-1 year'),
+        ];
+        $componentAdds        = [];
+        $componentAddsSumAll  = [];
+        $allComponentsProdIds = [];
+
+        foreach ($storeIds as $sid) {
+            foreach ($periods as $dt) {
+                $items = $this->getProductsComponentsInCategory(
+                    $sid,
+                    (int)$dt->format('n'),
+                    (int)$dt->format('Y'),
+                    $type
+                );
+                if (empty($items)) {
+                    continue;
+                }
+
+                foreach ($items as $it) {
+                    $allComponentsProdIds[$sid][] = $it['product_id'];
+                }
+
+
+                $sums = $this->sumProductsComponentsByGroup($items, $type, 'subcategory');
+                $allComponentsProdIds[$sid] = array_unique($allComponentsProdIds[$sid]);
+
+                foreach ($sums as $sr) {
+                    $cat    = $sr['category'];
+                    $subcat = $sr['subcategory'];
+                    $val    = $sr['sum'];
+
+                    $componentAdds[$sid][$cat][$subcat] =
+                        ($componentAdds[$sid][$cat][$subcat] ?? 0) + $val;
+
+                    $componentAddsSumAll[$sid][$cat] =
+                        ($componentAddsSumAll[$sid][$cat] ?? 0) + $val;
+                }
+            }
+        }
+        $result = [];
+
+        foreach ($rows as $r) {
+            $sid       = $r['store_id'];
+            $cat       = $r['category'];
+            $subcat    = $r['subcategory'];
+
+
+            $subTotal  = (float)$r['total_sum'];
+            $catTotal  = (float)$r['totals_total'];
+
+
+            $addCat    = $componentAddsSumAll[$sid][$cat] ?? 0.0;
+            $addSubcat = $componentAdds[$sid][$cat][$subcat]   ?? 0.0;
+
+
+            $newCatTotal = $catTotal + $addCat;
+            $newSubTotal = $subTotal + $addSubcat;
+
+
+            $percent = $newCatTotal > 0
+                ? round($newSubTotal / $newCatTotal, 4)
+                : 0.0;
+
+            $result[$sid][] = [
+                'category'                 => $cat,
+                'subcategory'              => $subcat,
+                'total_sum'                => $newSubTotal,
+                'percent'                  => $percent,
+                'type'                     => $type,
+                'new_total_store'          => $newCatTotal,
+                'base_total_store'         => $catTotal,
+                'products_components_list' => implode(',', $allComponentsProdIds[$sid] ?? []),
+            ];
+        }
 
         return $result;
     }
@@ -793,7 +870,7 @@ class AutoPlannogrammaService
                 ->andWhere(['not', ['s.operation'   => ['Удален', 'Удаление']]])
                 ->andWhere(['between', 's.date', $monthStart, $monthEnd])
                 ->andWhere(['not', ['p1c.components' => '']])
-               // ->andWhere(['nom.category' => null])
+                //->andWhere(['not in', 'nom.category', ['', 'букет', 'сборка', 'сервис']])
                 ->asArray()
                 ->all();
         $componentProducts = $salesProducts;
@@ -828,7 +905,7 @@ class AutoPlannogrammaService
                 )
                 ->andWhere(['between', 'w.date', $monthStart, $monthEnd])
                 ->andWhere(['ex.entity_id' => $storeId])
-               // ->andWhere(['nom.category' => null])
+                //->andWhere(['not in', 'nom.category', ['', 'букет', 'сборка', 'сервис']])
                 ->andWhere(['<>', 'p1c.components', ''])
                 ->asArray()
                 ->all();
@@ -876,6 +953,7 @@ class AutoPlannogrammaService
 
         $nomenclatures = Products1cNomenclature::find()
             ->andWhere(['id' => $guids])
+            ->andWhere(['not in', 'category', ['', 'букет', 'сборка', 'сервис']])
             ->indexBy('id')
             ->all();
 
@@ -917,6 +995,8 @@ class AutoPlannogrammaService
                 'component_guid'     => $guid,
                 'component_name'     => $n?->name,
                 'component_category' => $n?->category,
+                'component_subcategory' => $n?->subcategory,
+                'component_species' => $n?->species,
                 'quantity'           => $r['quantity'],
                 'price'              => $price,
                 'cost'               => $cost,
@@ -931,16 +1011,18 @@ class AutoPlannogrammaService
     }
 
 
-    public function sumProductsComponentsByCategory(array $items, string $type): array
+    public function sumProductsComponentsByGroup(array $items, string $type, string $group = 'category'): array
     {
         $aggregated = [];
 
         foreach ($items as $row) {
-            $storeId  = $row['store_id'];
-            $category = $row['component_category'];
-            $month    = $row['month'];
-            $year     = $row['year'];
-            $operation = $row['operation'] ?? null;
+            $storeId     = $row['store_id'];
+            $category    = $row['component_category'];
+            $subcategory = $row['component_subcategory'];
+            $species     = $row['component_species'];
+            $month       = $row['month'];
+            $year        = $row['year'];
+            $operation   = $row['operation'] ?? null;
 
             $cost = (float)$row['cost'];
 
@@ -951,25 +1033,46 @@ class AutoPlannogrammaService
                 if ($operation === 'Возврат') {
                     $cost = -$cost;
                 }
+            }
+
 
+            $keyParts = [$storeId, $category];
+            if ($group === 'subcategory' || $group === 'species') {
+                $keyParts[] = $subcategory;
             }
+            if ($group === 'species') {
+                $keyParts[] = $species;
+            }
+
+            $keyParts[] = $year;
+            $keyParts[] = $month;
 
-            $key = implode('|', [$storeId, $category, $year, $month]);
+            $key = implode('|', $keyParts);
 
             if (!isset($aggregated[$key])) {
+                // базовая структура
                 $aggregated[$key] = [
-                    'store_id' => $storeId,
-                    'category' => $category,
-                    'sum'      => 0.0,
-                    'month'    => $month,
-                    'year'     => $year,
-                    'type'     => $type,
+                    'store_id'  => $storeId,
+                    'category'  => $category,
+                    'sum'       => 0.0,
+                    'month'     => $month,
+                    'year'      => $year,
+                    'type'      => $type,
                 ];
+
+                if ($group === 'subcategory' || $group === 'species') {
+                    $aggregated[$key]['subcategory'] = $subcategory;
+                }
+                if ($group === 'species') {
+                    $aggregated[$key]['species'] = $species;
+                }
             }
 
+
             $aggregated[$key]['sum'] += $cost;
         }
 
+
         return array_values($aggregated);
     }