]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
[ERP-140] рефактор
authorAlexander Smirnov <fredeom@mail.ru>
Wed, 4 Sep 2024 05:28:28 +0000 (08:28 +0300)
committerAlexander Smirnov <fredeom@mail.ru>
Wed, 4 Sep 2024 05:28:28 +0000 (08:28 +0300)
erp24/services/MotivationService.php

index ffe487adcb44ec027362561811e522f529b6d06e..0a34bb15d5ff6fa7243b6689db6fd23ded12dc11 100644 (file)
@@ -1236,68 +1236,68 @@ class MotivationService
             ->indexBy(['id'])
             ->asArray()->all();
 
-        // Ищем продажи по продуктовым гуидам по каталог-гуидам категории service
-        $sales = Sales::find()->alias('s')
-            ->leftJoin('sales_products p', 'p.check_id = s.id')
-            ->where(['between', 's.date', $monthStart, $monthEnd])
-            ->andWhere(['p.product_id' => array_keys($products1c)])
-            ->andWhere(['s.operation' => Sales::OPERATION_SALE])
-            ->asArray()->all();
-        $salesIds = ArrayHelper::getColumn($sales, 'id');
-
-        // Ищем чеки-возврат на текущие чеки
-        $returnSales = Sales::find()->where(['operation' => Sales::OPERATION_RETURN, 'sales_check' => $salesIds])->all();
-        $returnSalesIds = ArrayHelper::getColumn($returnSales, 'sales_check');
-
-        // Ищем продукты из категории services
-        $salesProduct = SalesProducts::find()->alias('p')
-            ->select(['SUM(p.summ) as total', 'store_id'])
-            ->leftJoin('sales s', 's.id = p.check_id')
-            ->where(['check_id' => $salesIds])
-            ->andWhere(['NOT IN', 'check_id', $returnSalesIds])
-            ->andWhere(['product_id' => array_keys($products1c)])
-            ->groupBy(['store_id'])
-            ->indexBy('store_id')
-            ->asArray()->all();
-
-        // Ищем продажи по продуктовым гуидам по каталог-гуидам категории services_delivery
-        $salesDelivery = Sales::find()->alias('s')
-            ->leftJoin('sales_products p', 'p.check_id = s.id')
-            ->where(['between', 's.date', $monthStart, $monthEnd])
-            ->andWhere(['p.product_id' => array_keys($products1cDelivery)])
-            ->andWhere(['s.operation' => Sales::OPERATION_SALE])
-            ->asArray()->all();
-        $salesIdsDelivery = ArrayHelper::getColumn($salesDelivery, 'id');
-
-        // Ищем чеки-возврат на текущие чеки
-        $returnSalesDelivery = Sales::find()->where(['operation' => Sales::OPERATION_RETURN, 'sales_check' => $salesIdsDelivery])->all();
-        $returnSalesIdsDelivery = ArrayHelper::getColumn($returnSalesDelivery, 'sales_check');
-
-        // Ищем продукты из категории services_delivery
-        $salesProductDelivery = SalesProducts::find()->alias('p')
-            ->select(['SUM(p.summ) as total', 'store_id'])
-            ->leftJoin('sales s', 's.id = p.check_id')
-            ->where(['check_id' => $salesIdsDelivery])
-            ->andWhere(['NOT IN', 'check_id', $returnSalesIdsDelivery])
-            ->andWhere(['product_id' => array_keys($products1cDelivery)])
-            ->groupBy(['store_id'])
-            ->indexBy('store_id')
-            ->asArray()->all();
-
         $motivations = Motivation::find()->where(['year' => $year, 'month' => $month])->indexBy('store_id')->all();
 
         $storeIds = ArrayHelper::getColumn(CityStore::find()->where(['visible' => '1'])->all(), 'id');
         foreach ($storeIds as $store_id) {
             if (isset($motivations[$store_id])) {
+                // Ищем продажи по продуктовым гуидам по каталог-гуидам категории service
+                $sales = Sales::find()->alias('s')
+                    ->leftJoin('sales_products p', 'p.check_id = s.id')
+                    ->where(['between', 's.date', $monthStart, $monthEnd])
+                    ->andWhere(['p.product_id' => array_keys($products1c)])
+                    ->andWhere(['s.store_id' => $store_id])
+                    ->andWhere(['s.operation' => Sales::OPERATION_SALE])
+                    ->asArray()->all();
+                $salesIds = ArrayHelper::getColumn($sales, 'id');
+
+                // Ищем чеки-возврат на текущие чеки
+                $returnSales = Sales::find()->where(['operation' => Sales::OPERATION_RETURN, 'sales_check' => $salesIds])->all();
+                $returnSalesIds = ArrayHelper::getColumn($returnSales, 'sales_check');
+
+                // Ищем продукты из категории services
+                $salesProduct = SalesProducts::find()->alias('p')
+                    ->select(['SUM(p.summ) as total'])
+                    ->leftJoin('sales s', 's.id = p.check_id')
+                    ->where(['check_id' => $salesIds])
+                    ->andWhere(['NOT IN', 'check_id', $returnSalesIds])
+                    ->andWhere(['product_id' => array_keys($products1c)])
+                    ->andWhere(['s.store_id' => $store_id])
+                    ->asArray()->one();
+
                 $correction = self::getMotivationValue($motivations[$store_id]->id, 8, self::CODE_ASSEMBLY_SERVICES);
 
                 self::saveOrUpdateMotivationValue($motivations[$store_id]->id, "fact",
-                    self::CODE_ASSEMBLY_SERVICES, "float", ($salesProduct[$store_id]['total'] ?? 0) + $correction);
+                    self::CODE_ASSEMBLY_SERVICES, "float", ($salesProduct['total'] ?? 0) + $correction);
+
+                // Ищем продажи по продуктовым гуидам по каталог-гуидам категории services_delivery
+                $salesDelivery = Sales::find()->alias('s')
+                    ->leftJoin('sales_products p', 'p.check_id = s.id')
+                    ->where(['between', 's.date', $monthStart, $monthEnd])
+                    ->andWhere(['p.product_id' => array_keys($products1cDelivery)])
+                    ->andWhere(['s.store_id' => $store_id])
+                    ->andWhere(['s.operation' => Sales::OPERATION_SALE])
+                    ->asArray()->all();
+                $salesIdsDelivery = ArrayHelper::getColumn($salesDelivery, 'id');
+
+                // Ищем чеки-возврат на текущие чеки
+                $returnSalesDelivery = Sales::find()->where(['operation' => Sales::OPERATION_RETURN, 'sales_check' => $salesIdsDelivery])->all();
+                $returnSalesIdsDelivery = ArrayHelper::getColumn($returnSalesDelivery, 'sales_check');
+
+                // Ищем продукты из категории services_delivery
+                $salesProductDelivery = SalesProducts::find()->alias('p')
+                    ->select(['SUM(p.summ) as total'])
+                    ->leftJoin('sales s', 's.id = p.check_id')
+                    ->where(['check_id' => $salesIdsDelivery])
+                    ->andWhere(['NOT IN', 'check_id', $returnSalesIdsDelivery])
+                    ->andWhere(['product_id' => array_keys($products1cDelivery)])
+                    ->andWhere(['s.store_id' => $store_id])
+                    ->asArray()->one();
 
                 $correctionDelivery = self::getMotivationValue($motivations[$store_id]->id, 8, self::CODE_DELIVERY_SERVICES);
 
                 self::saveOrUpdateMotivationValue($motivations[$store_id]->id, "fact",
-                    self::CODE_DELIVERY_SERVICES, "float", ($salesProductDelivery[$store_id]['total'] ?? 0) + $correctionDelivery);
+                    self::CODE_DELIVERY_SERVICES, "float", ($salesProductDelivery['total'] ?? 0) + $correctionDelivery);
             }
         }
     }