]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
Рефакторинг
authorAlexander Smirnov <fredeom@mail.ru>
Wed, 7 Aug 2024 10:14:09 +0000 (13:14 +0300)
committerAlexander Smirnov <fredeom@mail.ru>
Wed, 7 Aug 2024 10:14:09 +0000 (13:14 +0300)
erp24/services/MotivationService.php

index 6e2371b322fba415abcc72a603d6d3ada002d02c..a259fec87c67d96c05085f04e288de08988c405b 100644 (file)
@@ -3,6 +3,7 @@
 namespace yii_app\services;
 
 use PhpOffice\PhpSpreadsheet\IOFactory;
+use yii\helpers\Json;
 use yii_app\records\ExportImportTable;
 use yii_app\records\Motivation;
 use yii_app\records\MotivationValue;
@@ -334,6 +335,8 @@ class MotivationService
         $monthEnd = date("Y-m-t 23:59:59", strtotime($year . '-' . $month . '-1'));
 
         $motivation = Motivation::find()->where(['store_id' => $store_id, 'year' => $year, 'month' => $month])->one();
+        $exportImportTable = ExportImportTable::find()->select(['export_val'])->where(['entity' => 'city_store', 'entity_id' => $store_id, 'export_id' => 1])->one();
+        /** @var $exportImportTable ExportImportTable */
 
         foreach (range(1, 5) as $ind) {
             $weekStart = date("Y-m-d 00:00:00", strtotime("+" . (($ind - 1) * 7) . ' days', strtotime($monthStart)));
@@ -341,15 +344,16 @@ class MotivationService
             if ($weekEnd > $monthEnd) {
                 $weekEnd = $monthEnd;
             }
+            if ($weekStart > $monthEnd) {
+                continue;
+            }
 
             $motivationValueGroup = MotivationValueGroup::find()->where(['alias' => 'week' . $ind])->one();
 
-            $eit = ExportImportTable::find()->select(['export_val'])->where(['entity' => 'city_store', 'entity_id' => $store_id, 'export_id' => 1])->one();
-            /** @var $eit ExportImportTable */
-            if ($eit) {
+            if ($exportImportTable) {
                 $writeOffs = WriteOffs::find()->select(['sum(summ) as total', 'type'])
                     ->where(['between', 'date', $weekStart, $weekEnd])
-                    ->andWhere(['store_id' => $eit->export_val])
+                    ->andWhere(['store_id' => $exportImportTable->export_val])
                     ->groupBy(['type'])
                     ->indexBy('type')
                     ->asArray()->all();
@@ -374,12 +378,16 @@ class MotivationService
                         $motivationValue->value_float = $data['total'];
                         $motivationValue->save();
                         if ($motivationValue->getErrors()) {
-                            var_dump($motivationValue->getErrors());
-                            die;
+                            throw new \Exception(Json::encode($motivationValue->getErrors()));
                         }
                     }
                 }
             }
         }
+
+        $motivation->save();
+        if ($motivation->getErrors()) {
+            throw new \Exception(Json::encode($motivation->getErrors()));
+        }
     }
 }