From 5e4303330f2e0924a34c4c26ffc331c9b6905267 Mon Sep 17 00:00:00 2001 From: Alexander Smirnov Date: Mon, 2 Sep 2024 15:19:09 +0300 Subject: [PATCH] =?utf8?q?[ERP-130]=20=D0=94=D0=B5=D0=B1=D0=B0=D0=B3=20011?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- erp24/services/MotivationService.php | 38 +++++++++++++--------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/erp24/services/MotivationService.php b/erp24/services/MotivationService.php index 0afaf554..161f5dcf 100644 --- a/erp24/services/MotivationService.php +++ b/erp24/services/MotivationService.php @@ -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); } } } -- 2.39.5