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

index b42da27c2cad4b798ce376a5394edb0d4cc1fc0e..0fabb8d1f4bc128e9c9593e852cb95809c809c94 100644 (file)
@@ -522,23 +522,9 @@ 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($flatData, function ($row) use ($filters) {
+
+            $flatData = array_filter($data, function ($row) use ($filters) {
                 foreach ($filters as $key => $value) {
                     if (empty($value)) continue;
                     if (!isset($row[$key])) continue;
@@ -646,24 +632,7 @@ class AutoPlannogrammaController extends BaseController
 
             $data = $service->getMonthSpeciesShareOrWriteOff($filters['plan_date'], $filters, $filters['type']);
 
-            $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) {
+            $flatData = array_filter($data, function ($row) use ($filters) {
                 foreach ($filters as $key => $value) {
                     if (empty($value)) continue;
                     if (!isset($row[$key])) continue;
index 0038b6fc0ff40b46ebe62479576ed559aa758f78..c334df198289c721ad0a61334862882abec1eefb 100644 (file)
@@ -424,7 +424,8 @@ class AutoPlannogrammaService
                 ? round($newSubTotal / $newCatTotal, 4)
                 : 0.0;
 
-            $result[$sid][] = [
+            $result[] = [
+                'store_id'                 => $sid,
                 'category'                 => $cat,
                 'subcategory'              => $subcat,
                 'total_sum'                => $newSubTotal,
@@ -678,7 +679,8 @@ class AutoPlannogrammaService
                 ? round($newSpecTotal / $newSubcatTotal, 4)
                 : 0.0;
 
-            $finalResult[$sid][] = [
+            $finalResult[] = [
+                'store_id'                 => $sid,
                 'category'                 => $cat,
                 'subcategory'              => $subcat,
                 'species'                  => $spec ,
@@ -919,20 +921,17 @@ class AutoPlannogrammaService
      *                 component_guid, component_name, component_category,
      *                 quantity, price, cost
      */
-    public function getProductsComponentsInCategory(int $storeId, string $month, string $year, string $type = 'sales'): array
+    public function getProductsComponentsInCategory(int $storeId, string $month, string $year, string $type = self::TYPE_SALES): array
     {
         $region = CityStoreParams::find()
             ->where(['store_id' => $storeId])
             ->one()->address_region;
 
         if (!$region) {
-            // определяем регион по городу
             $cityId = CityStore::find()->select('city_id')->where(['id' => $storeId])->scalar();
-            if ($cityId == 1) {
-                $region = BouquetComposition::REGION_MSK;
-            } else {
-                $region = BouquetComposition::REGION_NN;
-            }
+            $region = $cityId == 1
+                ? BouquetComposition::REGION_MSK
+                : BouquetComposition::REGION_NN;
         }
 
         $monthStart = sprintf('%04d-%02d-01 00:00:00', $year, $month);
@@ -943,7 +942,16 @@ class AutoPlannogrammaService
         if ($type == self::TYPE_SALES) {
             $salesProducts = Sales::find()
                 ->alias('s')
-                ->select(['s.id', 's.date', 's.operation', 'sp.product_id', 'p1c.type', 'p1c.components' , 'p1c.name'])
+                ->select([
+                    's.id',
+                    's.date',
+                    's.operation',
+                    'sp.product_id',
+                    'p1c.type',
+                    'p1c.components',
+                    'p1c.name',
+                    'sp.quantity AS quantity_product'
+                ])
                 ->innerJoin(
                     ['sp' => SalesProducts::tableName()],
                     's.id = sp.check_id'
@@ -960,22 +968,21 @@ class AutoPlannogrammaService
                 ->andWhere(['not', ['s.operation'   => ['Удален', 'Удаление']]])
                 ->andWhere(['between', 's.date', $monthStart, $monthEnd])
                 ->andWhere(['not', ['p1c.components' => '']])
-                //->andWhere(['not in', 'nom.category', ['', 'букет', 'сборка', 'сервис']])
                 ->asArray()
                 ->all();
-        $componentProducts = $salesProducts;
+            $componentProducts = $salesProducts;
         } else {
-
             $writeOffsProducts = WriteOffs::find()
                 ->alias('w')
                 ->select([
-                    'write_off_id'    => 'w.id',
-                    'write_off_date'  => 'w.date',
-                    'product_id'      => 'wp.product_id',
-                    'type'            => 'p1c.type',
-                    'components'      => 'p1c.components',
-                    'product_name'    => 'p1c.name',
-                    'store_id'        => 'ex.entity_id',
+                    'w.id AS write_off_id',
+                    'w.date AS write_off_date',
+                    'wp.product_id',
+                    'p1c.type',
+                    'p1c.components',
+                    'p1c.name AS product_name',
+                    'ex.entity_id AS store_id',
+                    'wp.quantity AS quantity_product'
                 ])
                 ->innerJoin(
                     ['wp' => WriteOffsProducts::tableName()],
@@ -995,19 +1002,16 @@ class AutoPlannogrammaService
                 )
                 ->andWhere(['between', 'w.date', $monthStart, $monthEnd])
                 ->andWhere(['ex.entity_id' => $storeId])
-                //->andWhere(['not in', 'nom.category', ['', 'букет', 'сборка', 'сервис']])
-                ->andWhere(['<>', 'p1c.components', ''])
+                ->andWhere(['not', ['p1c.components' => '']])
                 ->asArray()
                 ->all();
             $componentProducts = $writeOffsProducts;
         }
 
-
         $components = [];
         $rows       = [];
         foreach ($componentProducts as $cp) {
-
-            $js      = trim($cp['components']);
+            $js = trim($cp['components']);
             if ($js === '' || $js[0] !== '{') {
                 continue;
             }
@@ -1020,18 +1024,17 @@ class AutoPlannogrammaService
                 if ($qty <= 0) {
                     continue;
                 }
-
                 $components[$guid] = true;
-
                 $rows[] = [
-                    'sale_id'     => $cp['id'],
-                    'sale_date'   => $cp['date'],
-                    'product_id'  => $cp['product_id'],
-                    'product_name'=> $cp['name'],
-                    'component_guid' => $guid,
-                    'quantity'    => $qty,
-                    'type' => $type,
-                    'operation' => $cp['operation'] ?? '',
+                    'record_id'        => $cp['id'] ?? $cp['write_off_id'],
+                    'sale_date'        => $cp['date'] ?? $cp['write_off_date'],
+                    'product_id'       => $cp['product_id'],
+                    'product_name'     => $cp['name'] ?? $cp['product_name'],
+                    'quantity_product' => $cp['quantity_product'],
+                    'component_guid'   => $guid,
+                    'quantity'         => $qty,
+                    'type'             => $type,
+                    'operation'        => $cp['operation'] ?? '',
                 ];
             }
         }
@@ -1039,8 +1042,8 @@ class AutoPlannogrammaService
         if (empty($rows)) {
             return [];
         }
-        $guids = array_keys($components);
 
+        $guids = array_keys($components);
         $nomenclatures = Products1cNomenclature::find()
             ->andWhere(['id' => $guids])
             ->andWhere(['not in', 'category', ['', 'букет', 'сборка', 'сервис']])
@@ -1049,51 +1052,54 @@ class AutoPlannogrammaService
 
         $priceDynamics = PricesDynamic::find()
             ->andWhere(['region_id' => $region])
-            ->andWhere(['product_id' => array_values( ArrayHelper::getColumn($nomenclatures, 'id') )])
+            ->andWhere(['product_id' => array_values(ArrayHelper::getColumn($nomenclatures, 'id'))])
             ->orderBy(['date_from' => SORT_DESC])
             ->all();
 
         $pricesByProduct = [];
         foreach ($priceDynamics as $pd) {
-            /** @var PricesDynamic $pd */
-            $pid = $pd->product_id;
-            $pricesByProduct[$pid][] = $pd;
+            $pricesByProduct[$pd->product_id][] = $pd;
         }
 
         $result = [];
         foreach ($rows as $r) {
-            $guid = $r['component_guid'];
-            $n   = $nomenclatures[$guid] ?? null;
-            $pid = $n?->id;
+            $guid  = $r['component_guid'];
+            $n     = $nomenclatures[$guid] ?? null;
+            $pid   = $n?->id;
             $price = 0;
-            if ($pid && isset($pricesByProduct[$pid])) {
-                foreach ($pricesByProduct[$pid] as $pd) {
-                    if ($pd->date_from <= $r['sale_date'] && $pd->date_to >= $r['sale_date']) {
+            foreach ($pricesByProduct[$pid] ?? [] as $pd) {
+                if ($pd->date_from <= $r['sale_date'] && $pd->date_to >= $r['sale_date']) {
+                    if ($pid == '2b72702a-792f-11e8-9edd-1c6f659fb563') {
+                        $price = 8.66;
+                    } else {
                         $price = $pd->price;
-                        break;
                     }
+
+
+                    break;
                 }
             }
-            $cost = $r['quantity'] * $price;
+            $cost = $r['quantity'] * $price * $r['quantity_product'];
+            $costComponent = $r['quantity'] * $price;
 
             $result[] = [
-                'store_id'   => $storeId,
-                'sale_id'            => $r['sale_id'],
-                'sale_date'          => $r['sale_date'],
-                'product_id'         => $r['product_id'],
-                'product_name'       => $r['product_name'],
-                '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,
-                'month' => $month,
-                'year' => $year,
-                'type' => $r['type'],
-                'operation' => $r['operation'] ?? '',
+                'store_id'             => $storeId,
+                'record_id'            => $r['record_id'],
+                'sale_date'            => $r['sale_date'],
+                'product_id'           => $r['product_id'],
+                'product_name'         => $r['product_name'],
+                'quantity_product'     => $r['quantity_product'],
+                '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,
+                'component_cost'       => $costComponent,
+                'type'                 => $type,
+                'operation'            => $r['operation'],
             ];
         }
 
@@ -1101,6 +1107,7 @@ class AutoPlannogrammaService
     }
 
 
+
     public function sumProductsComponentsByGroup(array $items, string $type, string $group = 'category'): array
     {
         $aggregated = [];
@@ -1110,8 +1117,6 @@ class AutoPlannogrammaService
             $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'];
@@ -1134,8 +1139,6 @@ class AutoPlannogrammaService
                 $keyParts[] = $species;
             }
 
-            $keyParts[] = $year;
-            $keyParts[] = $month;
 
             $key = implode('|', $keyParts);
 
@@ -1145,8 +1148,6 @@ class AutoPlannogrammaService
                     'store_id'  => $storeId,
                     'category'  => $category,
                     'sum'       => 0.0,
-                    'month'     => $month,
-                    'year'      => $year,
                     'type'      => $type,
                 ];
 
index 5ae0593e974c6a0341c64b5817b5566a5e81bad3..ff28eca6dd98ef9e9ebd3ba104fb7753067fe693 100644 (file)
@@ -95,6 +95,7 @@
 
 
 <?php
+
 $columns = [
     ['attribute' => 'store_id', 'label' => 'Магазин', 'value' => function ($data) {
         return CityStore::findOne($data['store_id'])->name ?? null;
@@ -102,16 +103,37 @@ $columns = [
     ['attribute' => 'category', 'label' => 'Категория'],
     ['attribute' => 'subcategory', 'label' => 'Подкатегория'],
     ['attribute' => 'species', 'label' => 'Тип'],
-    ['attribute' => 'dirtyGoal', 'label' => 'Неочищенная цель', 'format' => ['decimal', 2]],
-    ['attribute' => 'bouquetGoal', 'label' => 'Цель букета', 'format' => ['decimal', 2]],
-    ['attribute' => 'noHistoryGoal', 'label' => 'Товары без истории', 'format' => ['decimal', 2]],
-    ['attribute' => 'goal', 'label' => 'Очищенная цель', 'format' => ['decimal', 2]],
+    [
+        'attribute'=>'dirtyGoal',
+        'label'=>'Неочищенная цель',
+        'format'=>['decimal',2],
+        'pageSummary' => true,
+    ],
+    [
+        'attribute'=>'bouquetGoal',
+        'label'=>'Цель букета',
+        'format'=>['decimal',2],
+        'pageSummary' => true,
+    ],
+    [
+        'attribute'=>'noHistoryGoal',
+        'label'=>'Товары без истории',
+        'format'=>['decimal',2],
+        'pageSummary' => true,
+    ],
+    [
+        'attribute'=>'goal',
+        'label'=>'Очищенная цель',
+        'format'=>['decimal',2],
+        'pageSummary' => true,
+    ],
 ];
 
 
 ?>
 <?= GridView::widget([
     'dataProvider' => $dataProvider,
+    'showPageSummary' => true,
     'columns' => $columns,
 ]); ?>
 <?php