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

index ba72ce2f8d556ceb19c0145ffdfe666dabd2045a..b42da27c2cad4b798ce376a5394edb0d4cc1fc0e 100644 (file)
@@ -646,7 +646,24 @@ class AutoPlannogrammaController extends BaseController
 
             $data = $service->getMonthSpeciesShareOrWriteOff($filters['plan_date'], $filters, $filters['type']);
 
-            $flatData = array_filter($data, function ($row) use ($filters) {
+            $flatData = [];
+
+            foreach ($data as $storeId => $categories) {
+                foreach ($categories as $row) {
+                    $flatData[] = [
+                        'store_id' => (string)$storeId,
+                        'category' => $row['category'] ?? null,
+                        'subcategory' => $row['subcategory'] ?? null,
+                        'species' => $row['species'] ?? 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($flatData, function ($row) use ($filters) {
                 foreach ($filters as $key => $value) {
                     if (empty($value)) continue;
                     if (!isset($row[$key])) continue;
index f6fa4cd278d50112d8cce0300e0bf053787c5a6d..0038b6fc0ff40b46ebe62479576ed559aa758f78 100644 (file)
@@ -307,7 +307,7 @@ class AutoPlannogrammaService
                 (clone $base)->modify('-1 year')->format('Y-m-t 23:59:59'),   // год назад, конец месяца
             ],
         ];
-        //var_dump($months);die();
+
         $query = (new Query())
             ->select([
                 'store_id' => 'main.ex_entity_id',
@@ -506,18 +506,20 @@ class AutoPlannogrammaService
         $productTableJoin = $type === self::TYPE_WRITE_OFFS ? ['wp' => 'write_offs_products'] : ['sp' => 'sales_products'];
         $productTableJoinCondition = $type === self::TYPE_WRITE_OFFS ? 'wp.write_offs_id = w.id' : 'sp.check_id = s.id';
 
+        $field = $type === self::TYPE_WRITE_OFFS ? 'w.date' : 's.date';
+        $base  = new \DateTime($dateFrom);
         $months = [
             [
                 'between',
-                $type === self::TYPE_WRITE_OFFS ? 'w.date' : 's.date',
-                (new \DateTime($dateFrom))->modify('-1 year')->format('Y-m-01'),
-                (new \DateTime($dateFrom))->format('Y-m-t'),
+                $field,
+                (clone $base)->modify('-2 year')->format('Y-m-01 00:00:00'),  // два года назад, начало месяца
+                (clone $base)->modify('-2 year')->format('Y-m-t 23:59:59'),   // два года назад, конец месяца
             ],
             [
                 'between',
-                $type === self::TYPE_WRITE_OFFS ? 'w.date' : 's.date',
-                (new \DateTime($dateFrom))->modify('-1 year')->format('Y-m-01'),
-                (new \DateTime($dateFrom))->format('Y-m-t'),
+                $field,
+                (clone $base)->modify('-1 year')->format('Y-m-01 00:00:00'),  // год назад, начало месяца
+                (clone $base)->modify('-1 year')->format('Y-m-t 23:59:59'),   // год назад, конец месяца
             ],
         ];
 
@@ -530,6 +532,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())
@@ -544,8 +547,10 @@ class AutoPlannogrammaService
                     ->leftJoin($productTableJoin, $productTableJoinCondition)
                     ->leftJoin('products_1c_nomenclature p1c', "p1c.id = $productJoinCondition")
                     ->leftJoin('export_import_table ex', $storeJoinCondition)
+                    ->leftJoin('products_1c p1', "p1.id = $productJoinCondition")
                     ->andWhere(['ex.entity_id' => $storeIds])
-                    ->andWhere(['<>', 'p1c.species', ''])
+                    ->andWhere(['p1.components' => ''])
+                    ->andWhere(['not in', 'p1c.category', ['', 'букет', 'сборка', 'сервис']])
                     ->andWhere(['or', ...$months])
                     ->groupBy(['ex.entity_id', 'p1c.category', 'p1c.subcategory', 'p1c.species']),
             ])
@@ -561,8 +566,10 @@ class AutoPlannogrammaService
                     ->leftJoin($productTableJoin, $productTableJoinCondition)
                     ->leftJoin('products_1c_nomenclature p1c', "p1c.id = $productJoinCondition")
                     ->leftJoin('export_import_table ex', $storeJoinCondition)
+                    ->leftJoin('products_1c p1', "p1.id = $productJoinCondition")
                     ->andWhere(['ex.entity_id' => $storeIds])
-                    ->andWhere(['<>', 'p1c.species', ''])
+                    ->andWhere(['p1.components' => ''])
+                    ->andWhere(['not in', 'p1c.category', ['', 'букет', 'сборка', 'сервис']])
                     ->andWhere(['or', ...$months])
                     ->groupBy(['ex.entity_id', 'p1c.category', 'p1c.subcategory'])],
                 'main.ex_entity_id = totals.store_id 
@@ -590,18 +597,101 @@ class AutoPlannogrammaService
                 $percent = $this->adjustWriteOffPercent($percent, $salesPercents[$key] ?? 0);
             }
 
+
             $result[] = [
                 'store_id' => $row['store_id'],
                 'category' => $row['category'],
                 'subcategory' => $row['subcategory'],
                 'species' => $row['species'],
                 'total_sum' => $row['total_sum'],
+                'totals_total' => $row['totals_total'],
                 'percent' => $percent,
                 'type' => $row['type'],
             ];
         }
 
-        return $result;
+        $periods = [
+            (clone $base)->modify('-2 year'),
+            (clone $base)->modify('-1 year'),
+        ];
+
+        $finalResult          = [];
+        $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, 'species');
+                $allComponentsProdIds[$sid] = array_unique($allComponentsProdIds[$sid]);
+
+                foreach ($sums as $sr) {
+                    $cat    = $sr['category'];
+                    $subcat = $sr['subcategory'];
+                    $spec = $sr['species'];
+                    $val    = $sr['sum'];
+
+                    $componentAdds[$sid][$cat][$subcat][$spec] =
+                        ($componentAdds[$sid][$cat][$subcat][$spec] ?? 0) + $val;
+
+                    $componentAddsSumAll[$sid][$cat][$subcat] =
+                        ($componentAddsSumAll[$sid][$cat][$subcat] ?? 0) + $val;
+                }
+            }
+        }
+
+
+        foreach ($result as $r) {
+            $sid       = $r['store_id'];
+            $cat       = $r['category'];
+            $subcat    = $r['subcategory'];
+            $spec      = $r['species'];
+
+            $specTotal  = (float)$r['total_sum'];
+            $subcatTotal  = (float)$r['totals_total'];
+
+
+            $addSubcat    = $componentAddsSumAll[$sid][$cat][$subcat] ?? 0.0;
+            $addSpecies = $componentAdds[$sid][$cat][$subcat][$spec]   ?? 0.0;
+
+
+            $newSubcatTotal = $subcatTotal + $addSubcat;
+            $newSpecTotal = $specTotal + $addSpecies;
+
+
+            $percent = $newSubcatTotal > 0
+                ? round($newSpecTotal / $newSubcatTotal, 4)
+                : 0.0;
+
+            $finalResult[$sid][] = [
+                'category'                 => $cat,
+                'subcategory'              => $subcat,
+                'species'                  => $spec ,
+                'total_sum'                => $newSpecTotal,
+                'percent'                  => $percent,
+                'type'                     => $type,
+                'new_total_store'          => $newSubcatTotal,
+                'base_total_store'         => $subcatTotal,
+                'products_components_list' => implode(',', $allComponentsProdIds[$sid] ?? []),
+            ];
+        }
+
+        return $finalResult;
     }
 
     /**