From: Alexander Smirnov Date: Tue, 15 Oct 2024 14:18:21 +0000 (+0300) Subject: [ERP-220] рефактор X-Git-Tag: 1.6~36^2~2 X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=0112885743f05fe685187dc22efcc75a064746c5;p=erp24_rep%2Fyii-erp24%2F.git [ERP-220] рефактор --- diff --git a/erp24/services/MotivationService.php b/erp24/services/MotivationService.php index 1b8427ce..24034773 100644 --- a/erp24/services/MotivationService.php +++ b/erp24/services/MotivationService.php @@ -1307,6 +1307,30 @@ class MotivationService } } + private static function getSelfCostProductMap($store_id, $periodStart, $periodEnd) { + $selfCostProduct = SelfCostProduct::find()->select(['price', 'product_guid', 'date']) + ->where(['between', 'date', $periodStart, $periodEnd]) + ->andWhere(['store_id' => $store_id]) + ->asArray()->all(); + + $selfCostProductMap = []; + foreach ($selfCostProduct as $scp) { + $selfCostProductMap[$scp['date']][$scp['product_guid']] = floatval($scp['price']); + } + + 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']) + ->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; + } + public static function calculateMonthDefect($year, $month) { $monthStart = date("Y-m-d 00:00:00", strtotime($year . '-' . $month . '-1')); $monthEnd = date("Y-m-t 23:59:59", strtotime($year . '-' . $month . '-1')); @@ -1318,26 +1342,14 @@ class MotivationService foreach ($exportImportTables as $store_id => $store_guid) { if (isset($motivations[$store_id])) { - $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', $monthStart, $monthEnd]) - ->andWhere(['wo.store_id' => $store_guid]) - ->asArray()->all(); + $writeOffs = self::getWriteOffs($store_guid, $monthStart, $monthEnd); - $selfCostProduct = SelfCostProduct::find()->select(['price', 'product_guid', 'date']) - ->where(['between', 'date', $monthStart, $monthEnd]) - ->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, $monthStart, $monthEnd); foreach (MotivationCostsItem::writeOffsToMotivationItemArray() as $key => $motivationItemType) { if ($key == WriteOffsErp::WRITE_OFFS_TYPE_DELIVERY_BRAK) { if ($store_id == 4 /* Аэродромная */) { - $writeOffsResult = $writeOffs = WriteOffs::find()->alias('wo')->select(['wo.type', 'wo.date', 'wop.product_id', 'wop.quantity', 'wop.price']) + $writeOffsResult = 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', $monthStart, $monthEnd]) ->asArray()->all(); @@ -1948,15 +1960,7 @@ class MotivationService ->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']); - } + $selfCostProductMap = self::getSelfCostProductMap($storeId, $startDate, $endDate); $sum = 0; foreach($writeOffs as $data) {