]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
[ERP-130] Дебаг 011
authorAlexander Smirnov <fredeom@mail.ru>
Mon, 2 Sep 2024 12:19:09 +0000 (15:19 +0300)
committerAlexander Smirnov <fredeom@mail.ru>
Mon, 2 Sep 2024 12:19:09 +0000 (15:19 +0300)
erp24/services/MotivationService.php

index 0afaf5549549fcf607010ea0638d5b46ba67a6c0..161f5dcf7db172fb635e03cd8d9bcaed682b3152 100644 (file)
@@ -454,39 +454,37 @@ class MotivationService
             }
 
             if ($exportImportTable) {
-                $writeOffs = WriteOffs::find()->select(['items', 'type', 'date'])
-                    ->where(['between', 'date', $weekStart, $weekEnd])
-                    ->andWhere(['store_id' => $exportImportTable->export_val])
+                $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', $weekStart, $weekEnd])
+                    ->andWhere(['wo.store_id' => $exportImportTable->export_val])
                     ->asArray()->all();
 
-                $selfCostProduct = SelfCostProduct::find()->select(['price', 'product_guid'])
+                $selfCostProduct = SelfCostProduct::find()->select(['price', 'product_guid, date'])
                     ->where(['between', 'date', $weekStart, $weekEnd])
                     ->andWhere(['store_id' => $store_id])
-                    ->indexBy('product_guid')
                     ->asArray()->all();
 
+                $selfCostProductMap = [];
+                foreach ($selfCostProduct as $scp) {
+                    $selfCostProductMap[$scp['date']][$scp['product_guid']] = floatval($scp['price']);
+                }
+
                 foreach (MotivationCostsItem::getWriteOffsItems() as $key) {
                     $sum = 0;
                     foreach($writeOffs as $data) {
                         if (($data['type'] ?? '') != $key) {
                             continue;
                         }
-                        if (!empty($data)) {
-                            try {
-                                $json = Json::decode($data['items'] ?? '[]');
+                        $sum += ($selfCostProductMap[$data['date']][$data['product_id']] ?? 0) * ($data['quantity'] ?? 0);
+                    }
 
-                                foreach ($json as $itemArr) {
-                                    $sum += floatval(($selfCostProduct[$itemArr['product_id']]["price"] ?? 0)) * ($itemArr['quantity'] ?? 0);
-                                }
-                            } catch (\Exception $ex) {}
-                        }
-                        $motivationItemType = MotivationCostsItem::writeOffsToMotivationItemMap($key);
-                        $motivationCostsItem = MotivationCostsItem::find()->where(['name' => $motivationItemType])->one();
-                        /** @var $motivationCostsItem MotivationCostsItem */
-                        if ($motivation && $motivationCostsItem) {
-                            self::saveOrUpdateMotivationValue($motivation->id, 'week' . $ind, $motivationCostsItem->code,
-                                MotivationCostsItem::DATA_TYPE_FLOAT, $sum);
-                        }
+                    $motivationItemType = MotivationCostsItem::writeOffsToMotivationItemMap($key);
+                    $motivationCostsItem = MotivationCostsItem::find()->where(['name' => $motivationItemType])->one();
+                    /** @var $motivationCostsItem MotivationCostsItem */
+                    if ($motivation && $motivationCostsItem) {
+                        self::saveOrUpdateMotivationValue($motivation->id, 'week' . $ind, $motivationCostsItem->code,
+                            MotivationCostsItem::DATA_TYPE_FLOAT, $sum);
                     }
                 }
             }