]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
[ERP-220] рефактор origin/feature_smirnov_erp-220_remove_supply_defect_from_motivation
authorAlexander Smirnov <fredeom@mail.ru>
Tue, 15 Oct 2024 16:17:34 +0000 (19:17 +0300)
committerAlexander Smirnov <fredeom@mail.ru>
Tue, 15 Oct 2024 16:17:34 +0000 (19:17 +0300)
erp24/services/MotivationService.php

index bab12c0f025dec53a3b1dc18c1c091ce9c79f38d..045937703b98fbcbd0771b2918ec9deafd5ae302 100644 (file)
@@ -476,23 +476,15 @@ class MotivationService
             }
 
             if ($exportImportTable) {
-                $writeOffs = self::getWriteOffs($exportImportTable->export_val, $weekStart, $weekEnd);
+                $writeOffs = self::getWriteOffs($weekStart, $weekEnd, $exportImportTable->export_val);
 
-                $selfCostProduct = SelfCostProduct::find()->select(['price', 'product_guid', 'date'])
-                    ->where(['between', 'date', $weekStart, $weekEnd])
-                    ->andWhere(['store_id' => $store_id])
-                    ->asArray()->all();
-
-                $selfCostProductMap = [];
-                foreach ($selfCostProduct as $scp) {
-                    $selfCostProductMap[$scp['date']][$scp['product_guid']] = floatval($scp['price']);
-                }
+                $selfCostProductMap = self::getSelfCostProductMap($store_id, $weekStart, $weekEnd);
 
                 foreach (MotivationCostsItem::getWriteOffsItems() as $key) {
 
                     if ($key == WriteOffsErp::WRITE_OFFS_TYPE_DELIVERY_BRAK) {
                         if ($store_id == 4 /* Аэродромная */ ) {
-                            $writeOffsResult = self::getWriteOffsAllStores($weekStart, $weekEnd);
+                            $writeOffsResult = self::getWriteOffs($weekStart, $weekEnd);
                         } else {
                             $writeOffsResult = [];
                         }
@@ -1314,21 +1306,14 @@ class MotivationService
         return $selfCostProductMap;
     }
 
-    private static function getWriteOffs($storeGuid, $periodStart, $periodEnd) {
-        $writeOffs = WriteOffs::find()->alias('wo')->select(['wo.type', 'wo.date', 'wop.product_id', 'wop.quantity', 'wop.price'])
+    private static function getWriteOffs($periodStart, $periodEnd, $storeGuid = null) {
+        $writeOffsQuery = WriteOffs::find()->alias('wo')->select(['wo.type', 'wo.date', 'wop.product_id', 'wop.quantity', 'wop.price'])
             ->rightJoin('write_offs_products wop', 'wop.write_offs_id = wo.id')
-            ->where(['between', 'wo.date', $periodStart, $periodEnd])
-            ->andWhere(['wo.store_id' => $storeGuid])
-            ->asArray()->all();
-
-        return $writeOffs;
-    }
-
-    private static function getWriteOffsAllStores($periodStart, $periodEnd) {
-        $writeOffs = WriteOffs::find()->alias('wo')->select(['wo.type', 'wo.date', 'wop.product_id', 'wop.quantity', 'wop.price'])
-            ->rightJoin('write_offs_products wop', 'wop.write_offs_id = wo.id')
-            ->where(['between', 'wo.date', $periodStart, $periodEnd])
-            ->asArray()->all();
+            ->where(['between', 'wo.date', $periodStart, $periodEnd]);
+        if ($storeGuid) {
+            $writeOffsQuery->andWhere(['wo.store_id' => $storeGuid]);
+        }
+        $writeOffs = $writeOffsQuery->asArray()->all();
 
         return $writeOffs;
     }
@@ -1344,14 +1329,14 @@ class MotivationService
 
         foreach ($exportImportTables as $store_id => $store_guid) {
             if (isset($motivations[$store_id])) {
-                $writeOffs = self::getWriteOffs($store_guid, $monthStart, $monthEnd);
+                $writeOffs = self::getWriteOffs($monthStart, $monthEnd, $store_guid);
 
                 $selfCostProductMap = self::getSelfCostProductMap($store_id, $monthStart, $monthEnd);
 
                 foreach (MotivationCostsItem::writeOffsToMotivationItemArray() as $key => $motivationItemType) {
                     if ($key == WriteOffsErp::WRITE_OFFS_TYPE_DELIVERY_BRAK) {
                         if ($store_id == 4 /* Аэродромная */) {
-                            $writeOffsResult = self::getWriteOffsAllStores($monthStart, $monthEnd);
+                            $writeOffsResult = self::getWriteOffs($monthStart, $monthEnd);
                         } else {
                             $writeOffsResult = [];
                         }