]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
[ERP-186] Вычитаем из подсчёта продаж service и service_delivery категории товаров feature_smirnov_erp-186_sales_without_service_and_service_delivery origin/feature_smirnov_erp-186_sales_without_service_and_service_delivery
authorAlexander Smirnov <fredeom@mail.ru>
Wed, 11 Sep 2024 14:51:43 +0000 (17:51 +0300)
committerAlexander Smirnov <fredeom@mail.ru>
Wed, 11 Sep 2024 14:51:43 +0000 (17:51 +0300)
erp24/services/MotivationService.php

index eba80a076d8ffd27d22035eac6ec842ff17b4caf..74590dcbf70e824f00bc9e9125463b906b2e0e5a 100644 (file)
@@ -619,6 +619,18 @@ class MotivationService
         $monthStart = date("Y-m-d 00:00:00", strtotime($year . '-' . $month . '-1'));
         $monthEnd = date("Y-m-t 23:59:59", strtotime($year . '-' . $month . '-1'));
 
+        // Ищем каталог-гуиды категории services и services_delivery
+        $productsClass = ProductsClass::find()->select(['category_id', 'tip'])
+            ->where(['tip' => ['services', 'services_delivery']])
+            ->indexBy('category_id')
+            ->asArray()->all();
+
+        // Ищем продуктовые гуиды по каталог-гуидам категории services и services_delivery
+        $products1c = Products1c::find()->select(['id', 'parent_id', 'name'])
+            ->where(['parent_id' => array_keys($productsClass), 'tip' => 'products'])
+            ->indexBy(['id'])
+            ->asArray()->all();
+
         foreach (range(1, 5) as $ind) {
             $weekStart = date("Y-m-d 00:00:00", strtotime("+" . (($ind - 1) * 7) . ' days', strtotime($monthStart)));
             $weekEnd = date("Y-m-d 23:59:59", strtotime("+" . ($ind * 7 - 1) . ' days', strtotime($monthStart)));
@@ -641,21 +653,25 @@ class MotivationService
             $returnSalesIds = ArrayHelper::getColumn($returnSales, 'sales_check');
 
             // Offline sales
-            $salesOffline = Sales::find()->select(['SUM(summ) as total'])
-                ->where(['between', 'date', $weekStart, $weekEnd])
-                ->andWhere(['order_id' => ['', '0']])
-                ->andWhere(['store_id' => $store_id])
-                ->andWhere(['operation' => Sales::OPERATION_SALE])
-                ->andWhere(['NOT IN', 'id', $returnSalesIds])
+            $salesOffline = Sales::find()->alias('s')->select(['SUM(p.summ) as total'])
+                ->rightJoin('sales_products p', 's.id = p.check_id')
+                ->where(['between', 's.date', $weekStart, $weekEnd])
+                ->andWhere(['s.order_id' => ['', '0']])
+                ->andWhere(['s.store_id' => $store_id])
+                ->andWhere(['s.operation' => Sales::OPERATION_SALE])
+                ->andWhere(['NOT IN', 'p.product_id', array_keys($products1c)])
+                ->andWhere(['NOT IN', 's.id', $returnSalesIds])
                 ->asArray()->one();
 
             // Online sales
-            $salesOnline = Sales::find()->select(['SUM(summ) as total'])
-                ->where(['between', 'date', $weekStart, $weekEnd])
-                ->andWhere(['NOT IN', 'order_id', ['', '0']])
-                ->andWhere(['store_id' => $store_id])
-                ->andWhere(['operation' => Sales::OPERATION_SALE])
-                ->andWhere(['NOT IN', 'id', $returnSalesIds])
+            $salesOnline = Sales::find()->alias('s')->select(['SUM(p.summ) as total'])
+                ->rightJoin('sales_products p', 's.id = p.check_id')
+                ->where(['between', 's.date', $weekStart, $weekEnd])
+                ->andWhere(['NOT IN', 's.order_id', ['', '0']])
+                ->andWhere(['s.store_id' => $store_id])
+                ->andWhere(['s.operation' => Sales::OPERATION_SALE])
+                ->andWhere(['NOT IN', 'p.product_id', array_keys($products1c)])
+                ->andWhere(['NOT IN', 's.id', $returnSalesIds])
                 ->asArray()->one();
 
             $motivation = Motivation::find()->where(['store_id' => $store_id, 'year' => $year, 'month' => $month])->one();
@@ -1123,6 +1139,18 @@ class MotivationService
         $monthStart = date("Y-m-d 00:00:00", strtotime($year . '-' . $month . '-1'));
         $monthEnd = date("Y-m-t 23:59:59", strtotime($year . '-' . $month . '-1'));
 
+        // Ищем каталог-гуиды категории services и services_delivery
+        $productsClass = ProductsClass::find()->select(['category_id', 'tip'])
+            ->where(['tip' => ['services', 'services_delivery']])
+            ->indexBy('category_id')
+            ->asArray()->all();
+
+        // Ищем продуктовые гуиды по каталог-гуидам категории services и services_delivery
+        $products1c = Products1c::find()->select(['id', 'parent_id', 'name'])
+            ->where(['parent_id' => array_keys($productsClass), 'tip' => 'products'])
+            ->indexBy(['id'])
+            ->asArray()->all();
+
         $sales = Sales::find()->where(['between', 'date', $monthStart, $monthEnd])
             ->andWhere(['operation' => Sales::OPERATION_SALE])
             ->asArray()->all();
@@ -1132,69 +1160,42 @@ class MotivationService
         $returnSales = Sales::find()->where(['operation' => Sales::OPERATION_RETURN, 'sales_check' => $salesIds])->all();
         $returnSalesIds = ArrayHelper::getColumn($returnSales, 'sales_check');
 
-        $salesOffline = Sales::find()->select(['SUM(summ) as total', 'store_id'])
-            ->where(['between', 'date', $monthStart, $monthEnd])
-            ->andWhere(['operation' => Sales::OPERATION_SALE])
-            ->andWhere(['order_id' => ['', '0']])
-            ->andWhere(['NOT IN', 'id', $returnSalesIds])
-            ->groupBy(['store_id'])
+        $salesOffline = Sales::find()->alias('s')->select(['SUM(p.summ) as total', 's.store_id'])
+            ->rightJoin('sales_products p', 's.id = p.check_id')
+            ->where(['between', 's.date', $monthStart, $monthEnd])
+            ->andWhere(['s.operation' => Sales::OPERATION_SALE])
+            ->andWhere(['s.order_id' => ['', '0']])
+            ->andWhere(['NOT IN', 'p.product_id', array_keys($products1c)])
+            ->andWhere(['NOT IN', 's.id', $returnSalesIds])
+            ->groupBy(['s.store_id'])
             ->indexBy('store_id')
             ->asArray()->all();
 
-        $salesOnline = Sales::find()->select(['SUM(summ) as total', 'store_id'])
-            ->where(['between', 'date', $monthStart, $monthEnd])
-            ->andWhere(['operation' => Sales::OPERATION_SALE])
-            ->andWhere(['NOT IN', 'order_id', ['', '0']])
-            ->andWhere(['NOT IN', 'id', $returnSalesIds])
-            ->groupBy(['store_id'])
+        $salesOnline = Sales::find()->alias('s')->select(['SUM(p.summ) as total', 's.store_id'])
+            ->rightJoin('sales_products p', 's.id = p.check_id')
+            ->where(['between', 's.date', $monthStart, $monthEnd])
+            ->andWhere(['s.operation' => Sales::OPERATION_SALE])
+            ->andWhere(['NOT IN', 's.order_id', ['', '0']])
+            ->andWhere(['NOT IN', 'p.product_id', array_keys($products1c)])
+            ->andWhere(['NOT IN', 's.id', $returnSalesIds])
+            ->groupBy(['s.store_id'])
             ->indexBy('store_id')
             ->asArray()->all();
 
         $motivations = Motivation::find()->where(['year' => $year, 'month' => $month])->indexBy('store_id')->all();
 
-        $motivationValueGroupFact = MotivationValueGroup::find()->where(['alias' => 'fact'])->one();
-        $motivationCostsItemOffline = MotivationCostsItem::find()->where(['name' => 'Оффлайн продажи'])->one();
-        $motivationCostsItemOnline = MotivationCostsItem::find()->where(['name' => 'Онлайн продажи'])->one();
-        /** @var $motivationCostsItemOffline MotivationCostsItem */
-        /** @var $motivationCostsItemOnline MotivationCostsItem */
-
-
         $storeIds = ArrayHelper::getColumn(CityStore::find()->where(['visible' => '1'])->all(), 'id');
         foreach ($storeIds as $store_id) {
             if (isset($motivations[$store_id])) {
-                $correction = self::getMotivationValue($motivations[$store_id]->id, 8, $motivationCostsItemOffline->code);
-
-                $motivationValueOffline = MotivationValue::find()->where(['motivation_id' => $motivations[$store_id]->id,
-                    'motivation_group_id' => $motivationValueGroupFact->id, 'value_id' => $motivationCostsItemOffline->code])->one();
-                if (!$motivationValueOffline) {
-                    $motivationValueOffline = new MotivationValue;
-                    $motivationValueOffline->motivation_id = $motivations[$store_id]->id;
-                    $motivationValueOffline->motivation_group_id = $motivationValueGroupFact->id;
-                    $motivationValueOffline->value_id = $motivationCostsItemOffline->code;
-                    $motivationValueOffline->value_type = $motivationCostsItemOffline->data_type;
-                }
-                $motivationValueOffline->value_float = ($salesOffline[$store_id]['total'] ?? 0) + $correction;
-                $motivationValueOffline->save();
-                if ($motivationValueOffline->getErrors()) {
-                    throw new \Exception(Json::encode($motivationValueOffline->getErrors()));
-                }
+                $correction = self::getMotivationValue($motivations[$store_id]->id, 8, self::CODE_OFFLINE_SALES);
 
-                $correction = self::getMotivationValue($motivations[$store_id]->id, 8, $motivationCostsItemOnline->code);
+                self::saveOrUpdateMotivationValue($motivations[$store_id]->id, "fact",
+                    self::CODE_OFFLINE_SALES, 'float', ($salesOffline[$store_id]['total'] ?? 0) + $correction);
 
-                $motivationValueOnline = MotivationValue::find()->where(['motivation_id' => $motivations[$store_id]->id,
-                    'motivation_group_id' => $motivationValueGroupFact->id, 'value_id' => $motivationCostsItemOnline->code])->one();
-                if (!$motivationValueOnline) {
-                    $motivationValueOnline = new MotivationValue;
-                    $motivationValueOnline->motivation_id = $motivations[$store_id]->id;
-                    $motivationValueOnline->motivation_group_id = $motivationValueGroupFact->id;
-                    $motivationValueOnline->value_id = $motivationCostsItemOnline->code;
-                    $motivationValueOnline->value_type = $motivationCostsItemOnline->data_type;
-                }
-                $motivationValueOnline->value_float = ($salesOnline[$store_id]['total'] ?? 0) + $correction;
-                $motivationValueOnline->save();
-                if ($motivationValueOnline->getErrors()) {
-                    throw new \Exception(Json::encode($motivationValueOnline->getErrors()));
-                }
+                $correction = self::getMotivationValue($motivations[$store_id]->id, 8, self::CODE_ONLINE_SALES);
+
+                self::saveOrUpdateMotivationValue($motivations[$store_id]->id, "fact",
+                    self::CODE_ONLINE_SALES, 'float', ($salesOnline[$store_id]['total'] ?? 0) + $correction);
             }
         }
     }