]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
[ERP-140] Расходные материалы (обеспечение продаж) пополнены аналитикой из 1с розница
authorAlexander Smirnov <fredeom@mail.ru>
Tue, 3 Sep 2024 09:10:27 +0000 (12:10 +0300)
committerAlexander Smirnov <fredeom@mail.ru>
Tue, 3 Sep 2024 09:10:27 +0000 (12:10 +0300)
erp24/services/MotivationService.php

index b13d9cb3a691b6c0f391b4cd7b3a0c9604981486..301b7cfd75b8e60c8bb044186224f601691acd8c 100644 (file)
@@ -1510,14 +1510,21 @@ class MotivationService
     }
 
     public static function calculateMonthMaterials($year, $month) {
+        $monthStart = date("Y-m-d 00:00:00", strtotime("$year-$month-01"));
+        $monthEnd = date("Y-m-t 23:59:59", strtotime("$year-$month-01"));
+
         $motivations = Motivation::find()
             ->where(['year' => $year, 'month' => $month])
             ->all();
 
         foreach ($motivations as $motivation) {
+            /** @var $motivation Motivation */
+
             $materials = self::getMotivationValue($motivation->id, 10, self::CODE_CONSUMABLES_SALES_SUPPORT);
+            $consumablesSalesSupport = self::getCostConsumablesSalesSupportByStore($monthStart, $monthEnd, $motivation->store_id);
+
             self::saveOrUpdateMotivationValue($motivation->id,
-                "fact", self::CODE_CONSUMABLES_SALES_SUPPORT, "float", $materials);
+                "fact", self::CODE_CONSUMABLES_SALES_SUPPORT, "float", $materials + $consumablesSalesSupport);
         }
     }
 
@@ -1844,6 +1851,39 @@ class MotivationService
         return $sum;
     }
 
+    public static function getCostConsumablesSalesSupportByStore($startDate, $endDate, $storeId) {
+        $exportImportTable = ExportImportTable::find()->select(['export_val'])->where(['entity' => 'city_store', 'entity_id' => $storeId, 'export_id' => 1])->one();
+
+        if ($exportImportTable) {
+            $writeOffs = WriteOffs::find()->alias('wo')->select(['wo.type', 'wo.date', 'wop.product_id', 'wop.quantity'])
+                ->rightJoin('write_offs_products wop', 'wop.write_offs_id = wo.id')
+                ->where(['between', 'wo.date', $startDate, $endDate])
+                ->andWhere(['wo.store_id' => $exportImportTable->export_val])
+                ->andWhere(['type' => 'Расходные материалы (обеспечение продаж)'])
+                ->asArray()->all();
+
+            $selfCostProduct = SelfCostProduct::find()->select(['price', 'product_guid', 'date'])
+                ->where(['between', 'date', $startDate, $endDate])
+                ->andWhere(['store_id' => $storeId])
+                ->asArray()->all();
+
+            $selfCostProductMap = [];
+            foreach ($selfCostProduct as $scp) {
+                $selfCostProductMap[$scp['date']][$scp['product_guid']] = floatval($scp['price']);
+            }
+
+            $sum = 0;
+            foreach($writeOffs as $data) {
+                $sum += ($selfCostProductMap[date("Y-m-d", strtotime($data['date']))][$data['product_id']] ?? 0)
+                    * ($data['quantity'] ?? 0);
+            }
+
+            return $sum;
+        }
+
+        return 0;
+    }
+
     /**
      * Сохраняет мотивацию по себестоимости товара для указанного магазина и месяца.
      *