]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
[ERP-90] Сбор данных по продажам
authorAlexander Smirnov <fredeom@mail.ru>
Thu, 8 Aug 2024 06:06:43 +0000 (09:06 +0300)
committerAlexander Smirnov <fredeom@mail.ru>
Thu, 8 Aug 2024 06:06:43 +0000 (09:06 +0300)
erp24/actions/motivation/IndexAction.php
erp24/services/MotivationService.php

index f745fa062253454a944aac23ccc3b4ceb772d390..323c018d8a41590eaafa3f7252beb15d8073b789 100644 (file)
@@ -87,6 +87,9 @@ class IndexAction extends Action
             $model->month = intval($model->month);
         }
 
+        // Сбор данных по продажам
+        MotivationService::calculateSales($model->store_id, $model->year, $model->month);
+
         $showTable = false;
         $motivationData = [];
         $daysInMonth = null;
index 06e071aa8c8fedcbb9658b000d963de733d1dc08..1566477bc0453b080e85d488931d7de2fc3e4f82 100644 (file)
@@ -3,11 +3,14 @@
 namespace yii_app\services;
 
 use PhpOffice\PhpSpreadsheet\IOFactory;
+use yii\helpers\ArrayHelper;
+use yii\helpers\Json;
 use yii_app\records\Motivation;
 use yii_app\records\MotivationValue;
 use yii_app\records\MotivationValueGroup;
 use yii_app\records\CityStore;
 use yii_app\records\MotivationCostsItem;
+use yii_app\records\Sales;
 
 
 class MotivationService
@@ -326,4 +329,69 @@ class MotivationService
 
         return compact('errors');
     }
+
+    public static function calculateSales($store_id, $year, $month) {
+        $monthStart = date("Y-m-d 00:00:00", strtotime($year . '-' . $month . '-1'));
+        $monthEnd = date("Y-m-t 23:59:59", strtotime($year . '-' . $month . '-1'));
+
+        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:50:50", strtotime("+" . ($ind * 7 - 1) . ' days', strtotime($monthStart)));
+            if ($weekEnd > $monthEnd) {
+                $weekEnd = $monthEnd;
+            }
+
+            $sales = Sales::find()
+                ->where(['between', 'date', $weekStart, $weekEnd])
+                ->andWhere(['store_id' => $store_id])
+                ->andWhere(['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');
+
+            // 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])
+                ->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])
+                ->asArray()->one();
+
+            $motivation = Motivation::find()->where(['store_id' => $store_id, 'year' => $year, 'month' => $month])->one();
+            $motivationValueGroup = MotivationValueGroup::find()->where(['alias' => 'week' . $ind])->one();
+            foreach (['Оффлайн продажи', 'Онлайн продажи'] as $topicInd => $topic) {
+                $motivationCostsItem = MotivationCostsItem::find()->where(['name' => $topic])->one();
+                /** @var $motivationCostsItem MotivationCostsItem */
+                if ($motivation) {
+                    $motivationValue = MotivationValue::find()->where(['motivation_id' => $motivation->id,
+                        'motivation_group_id' => $motivationValueGroup->id, 'value_id' => $motivationCostsItem->code])->one();
+                    if (!$motivationValue) {
+                        $motivationValue = new MotivationValue;
+                        $motivationValue->motivation_id = $motivation->id;
+                        $motivationValue->motivation_group_id = $motivationValueGroup->id;
+                        $motivationValue->value_id = $motivationCostsItem->code;
+                        $motivationValue->value_type = $motivationCostsItem->data_type;
+                    }
+                    $motivationValue->value_float = [$salesOffline, $salesOnline][$topicInd]['total'];
+                    $motivationValue->save();
+                    if ($motivationValue->getErrors()) {
+                        throw new \Exception(Json::encode($motivationValue->getErrors()));
+                    }
+                }
+            }
+        }
+    }
 }