From: Alexander Smirnov Date: Wed, 4 Sep 2024 05:28:28 +0000 (+0300) Subject: [ERP-140] рефактор X-Git-Tag: 1.4~5^2~6 X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=487e09e29973c67d3c2e2642ff14341c92f0d99b;p=erp24_rep%2Fyii-erp24%2F.git [ERP-140] рефактор --- diff --git a/erp24/services/MotivationService.php b/erp24/services/MotivationService.php index ffe487ad..0a34bb15 100644 --- a/erp24/services/MotivationService.php +++ b/erp24/services/MotivationService.php @@ -1236,68 +1236,68 @@ class MotivationService ->indexBy(['id']) ->asArray()->all(); - // Ищем продажи по продуктовым гуидам по каталог-гуидам категории service - $sales = Sales::find()->alias('s') - ->leftJoin('sales_products p', 'p.check_id = s.id') - ->where(['between', 's.date', $monthStart, $monthEnd]) - ->andWhere(['p.product_id' => array_keys($products1c)]) - ->andWhere(['s.operation' => Sales::OPERATION_SALE]) - ->asArray()->all(); - $salesIds = ArrayHelper::getColumn($sales, 'id'); - - // Ищем чеки-возврат на текущие чеки - $returnSales = Sales::find()->where(['operation' => Sales::OPERATION_RETURN, 'sales_check' => $salesIds])->all(); - $returnSalesIds = ArrayHelper::getColumn($returnSales, 'sales_check'); - - // Ищем продукты из категории services - $salesProduct = SalesProducts::find()->alias('p') - ->select(['SUM(p.summ) as total', 'store_id']) - ->leftJoin('sales s', 's.id = p.check_id') - ->where(['check_id' => $salesIds]) - ->andWhere(['NOT IN', 'check_id', $returnSalesIds]) - ->andWhere(['product_id' => array_keys($products1c)]) - ->groupBy(['store_id']) - ->indexBy('store_id') - ->asArray()->all(); - - // Ищем продажи по продуктовым гуидам по каталог-гуидам категории services_delivery - $salesDelivery = Sales::find()->alias('s') - ->leftJoin('sales_products p', 'p.check_id = s.id') - ->where(['between', 's.date', $monthStart, $monthEnd]) - ->andWhere(['p.product_id' => array_keys($products1cDelivery)]) - ->andWhere(['s.operation' => Sales::OPERATION_SALE]) - ->asArray()->all(); - $salesIdsDelivery = ArrayHelper::getColumn($salesDelivery, 'id'); - - // Ищем чеки-возврат на текущие чеки - $returnSalesDelivery = Sales::find()->where(['operation' => Sales::OPERATION_RETURN, 'sales_check' => $salesIdsDelivery])->all(); - $returnSalesIdsDelivery = ArrayHelper::getColumn($returnSalesDelivery, 'sales_check'); - - // Ищем продукты из категории services_delivery - $salesProductDelivery = SalesProducts::find()->alias('p') - ->select(['SUM(p.summ) as total', 'store_id']) - ->leftJoin('sales s', 's.id = p.check_id') - ->where(['check_id' => $salesIdsDelivery]) - ->andWhere(['NOT IN', 'check_id', $returnSalesIdsDelivery]) - ->andWhere(['product_id' => array_keys($products1cDelivery)]) - ->groupBy(['store_id']) - ->indexBy('store_id') - ->asArray()->all(); - $motivations = Motivation::find()->where(['year' => $year, 'month' => $month])->indexBy('store_id')->all(); $storeIds = ArrayHelper::getColumn(CityStore::find()->where(['visible' => '1'])->all(), 'id'); foreach ($storeIds as $store_id) { if (isset($motivations[$store_id])) { + // Ищем продажи по продуктовым гуидам по каталог-гуидам категории service + $sales = Sales::find()->alias('s') + ->leftJoin('sales_products p', 'p.check_id = s.id') + ->where(['between', 's.date', $monthStart, $monthEnd]) + ->andWhere(['p.product_id' => array_keys($products1c)]) + ->andWhere(['s.store_id' => $store_id]) + ->andWhere(['s.operation' => Sales::OPERATION_SALE]) + ->asArray()->all(); + $salesIds = ArrayHelper::getColumn($sales, 'id'); + + // Ищем чеки-возврат на текущие чеки + $returnSales = Sales::find()->where(['operation' => Sales::OPERATION_RETURN, 'sales_check' => $salesIds])->all(); + $returnSalesIds = ArrayHelper::getColumn($returnSales, 'sales_check'); + + // Ищем продукты из категории services + $salesProduct = SalesProducts::find()->alias('p') + ->select(['SUM(p.summ) as total']) + ->leftJoin('sales s', 's.id = p.check_id') + ->where(['check_id' => $salesIds]) + ->andWhere(['NOT IN', 'check_id', $returnSalesIds]) + ->andWhere(['product_id' => array_keys($products1c)]) + ->andWhere(['s.store_id' => $store_id]) + ->asArray()->one(); + $correction = self::getMotivationValue($motivations[$store_id]->id, 8, self::CODE_ASSEMBLY_SERVICES); self::saveOrUpdateMotivationValue($motivations[$store_id]->id, "fact", - self::CODE_ASSEMBLY_SERVICES, "float", ($salesProduct[$store_id]['total'] ?? 0) + $correction); + self::CODE_ASSEMBLY_SERVICES, "float", ($salesProduct['total'] ?? 0) + $correction); + + // Ищем продажи по продуктовым гуидам по каталог-гуидам категории services_delivery + $salesDelivery = Sales::find()->alias('s') + ->leftJoin('sales_products p', 'p.check_id = s.id') + ->where(['between', 's.date', $monthStart, $monthEnd]) + ->andWhere(['p.product_id' => array_keys($products1cDelivery)]) + ->andWhere(['s.store_id' => $store_id]) + ->andWhere(['s.operation' => Sales::OPERATION_SALE]) + ->asArray()->all(); + $salesIdsDelivery = ArrayHelper::getColumn($salesDelivery, 'id'); + + // Ищем чеки-возврат на текущие чеки + $returnSalesDelivery = Sales::find()->where(['operation' => Sales::OPERATION_RETURN, 'sales_check' => $salesIdsDelivery])->all(); + $returnSalesIdsDelivery = ArrayHelper::getColumn($returnSalesDelivery, 'sales_check'); + + // Ищем продукты из категории services_delivery + $salesProductDelivery = SalesProducts::find()->alias('p') + ->select(['SUM(p.summ) as total']) + ->leftJoin('sales s', 's.id = p.check_id') + ->where(['check_id' => $salesIdsDelivery]) + ->andWhere(['NOT IN', 'check_id', $returnSalesIdsDelivery]) + ->andWhere(['product_id' => array_keys($products1cDelivery)]) + ->andWhere(['s.store_id' => $store_id]) + ->asArray()->one(); $correctionDelivery = self::getMotivationValue($motivations[$store_id]->id, 8, self::CODE_DELIVERY_SERVICES); self::saveOrUpdateMotivationValue($motivations[$store_id]->id, "fact", - self::CODE_DELIVERY_SERVICES, "float", ($salesProductDelivery[$store_id]['total'] ?? 0) + $correctionDelivery); + self::CODE_DELIVERY_SERVICES, "float", ($salesProductDelivery['total'] ?? 0) + $correctionDelivery); } } }