From 145a0a76a21b60de81d306f519a50609868d1481 Mon Sep 17 00:00:00 2001 From: Alexander Smirnov Date: Wed, 11 Sep 2024 17:51:43 +0300 Subject: [PATCH] =?utf8?q?[ERP-186]=20=D0=92=D1=8B=D1=87=D0=B8=D1=82=D0=B0?= =?utf8?q?=D0=B5=D0=BC=20=D0=B8=D0=B7=20=D0=BF=D0=BE=D0=B4=D1=81=D1=87?= =?utf8?q?=D1=91=D1=82=D0=B0=20=D0=BF=D1=80=D0=BE=D0=B4=D0=B0=D0=B6=20serv?= =?utf8?q?ice=20=D0=B8=20service=5Fdelivery=20=D0=BA=D0=B0=D1=82=D0=B5?= =?utf8?q?=D0=B3=D0=BE=D1=80=D0=B8=D0=B8=20=D1=82=D0=BE=D0=B2=D0=B0=D1=80?= =?utf8?q?=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- erp24/services/MotivationService.php | 125 ++++++++++++++------------- 1 file changed, 63 insertions(+), 62 deletions(-) diff --git a/erp24/services/MotivationService.php b/erp24/services/MotivationService.php index eba80a07..74590dcb 100644 --- a/erp24/services/MotivationService.php +++ b/erp24/services/MotivationService.php @@ -619,6 +619,18 @@ class MotivationService $monthStart = date("Y-m-d 00:00:00", strtotime($year . '-' . $month . '-1')); $monthEnd = date("Y-m-t 23:59:59", strtotime($year . '-' . $month . '-1')); + // Ищем каталог-гуиды категории services и services_delivery + $productsClass = ProductsClass::find()->select(['category_id', 'tip']) + ->where(['tip' => ['services', 'services_delivery']]) + ->indexBy('category_id') + ->asArray()->all(); + + // Ищем продуктовые гуиды по каталог-гуидам категории services и services_delivery + $products1c = Products1c::find()->select(['id', 'parent_id', 'name']) + ->where(['parent_id' => array_keys($productsClass), 'tip' => 'products']) + ->indexBy(['id']) + ->asArray()->all(); + foreach (range(1, 5) as $ind) { $weekStart = date("Y-m-d 00:00:00", strtotime("+" . (($ind - 1) * 7) . ' days', strtotime($monthStart))); $weekEnd = date("Y-m-d 23:59:59", strtotime("+" . ($ind * 7 - 1) . ' days', strtotime($monthStart))); @@ -641,21 +653,25 @@ class MotivationService $returnSalesIds = ArrayHelper::getColumn($returnSales, 'sales_check'); // Offline sales - $salesOffline = Sales::find()->select(['SUM(summ) as total']) - ->where(['between', 'date', $weekStart, $weekEnd]) - ->andWhere(['order_id' => ['', '0']]) - ->andWhere(['store_id' => $store_id]) - ->andWhere(['operation' => Sales::OPERATION_SALE]) - ->andWhere(['NOT IN', 'id', $returnSalesIds]) + $salesOffline = Sales::find()->alias('s')->select(['SUM(p.summ) as total']) + ->rightJoin('sales_products p', 's.id = p.check_id') + ->where(['between', 's.date', $weekStart, $weekEnd]) + ->andWhere(['s.order_id' => ['', '0']]) + ->andWhere(['s.store_id' => $store_id]) + ->andWhere(['s.operation' => Sales::OPERATION_SALE]) + ->andWhere(['NOT IN', 'p.product_id', array_keys($products1c)]) + ->andWhere(['NOT IN', 's.id', $returnSalesIds]) ->asArray()->one(); // Online sales - $salesOnline = Sales::find()->select(['SUM(summ) as total']) - ->where(['between', 'date', $weekStart, $weekEnd]) - ->andWhere(['NOT IN', 'order_id', ['', '0']]) - ->andWhere(['store_id' => $store_id]) - ->andWhere(['operation' => Sales::OPERATION_SALE]) - ->andWhere(['NOT IN', 'id', $returnSalesIds]) + $salesOnline = Sales::find()->alias('s')->select(['SUM(p.summ) as total']) + ->rightJoin('sales_products p', 's.id = p.check_id') + ->where(['between', 's.date', $weekStart, $weekEnd]) + ->andWhere(['NOT IN', 's.order_id', ['', '0']]) + ->andWhere(['s.store_id' => $store_id]) + ->andWhere(['s.operation' => Sales::OPERATION_SALE]) + ->andWhere(['NOT IN', 'p.product_id', array_keys($products1c)]) + ->andWhere(['NOT IN', 's.id', $returnSalesIds]) ->asArray()->one(); $motivation = Motivation::find()->where(['store_id' => $store_id, 'year' => $year, 'month' => $month])->one(); @@ -1123,6 +1139,18 @@ class MotivationService $monthStart = date("Y-m-d 00:00:00", strtotime($year . '-' . $month . '-1')); $monthEnd = date("Y-m-t 23:59:59", strtotime($year . '-' . $month . '-1')); + // Ищем каталог-гуиды категории services и services_delivery + $productsClass = ProductsClass::find()->select(['category_id', 'tip']) + ->where(['tip' => ['services', 'services_delivery']]) + ->indexBy('category_id') + ->asArray()->all(); + + // Ищем продуктовые гуиды по каталог-гуидам категории services и services_delivery + $products1c = Products1c::find()->select(['id', 'parent_id', 'name']) + ->where(['parent_id' => array_keys($productsClass), 'tip' => 'products']) + ->indexBy(['id']) + ->asArray()->all(); + $sales = Sales::find()->where(['between', 'date', $monthStart, $monthEnd]) ->andWhere(['operation' => Sales::OPERATION_SALE]) ->asArray()->all(); @@ -1132,69 +1160,42 @@ class MotivationService $returnSales = Sales::find()->where(['operation' => Sales::OPERATION_RETURN, 'sales_check' => $salesIds])->all(); $returnSalesIds = ArrayHelper::getColumn($returnSales, 'sales_check'); - $salesOffline = Sales::find()->select(['SUM(summ) as total', 'store_id']) - ->where(['between', 'date', $monthStart, $monthEnd]) - ->andWhere(['operation' => Sales::OPERATION_SALE]) - ->andWhere(['order_id' => ['', '0']]) - ->andWhere(['NOT IN', 'id', $returnSalesIds]) - ->groupBy(['store_id']) + $salesOffline = Sales::find()->alias('s')->select(['SUM(p.summ) as total', 's.store_id']) + ->rightJoin('sales_products p', 's.id = p.check_id') + ->where(['between', 's.date', $monthStart, $monthEnd]) + ->andWhere(['s.operation' => Sales::OPERATION_SALE]) + ->andWhere(['s.order_id' => ['', '0']]) + ->andWhere(['NOT IN', 'p.product_id', array_keys($products1c)]) + ->andWhere(['NOT IN', 's.id', $returnSalesIds]) + ->groupBy(['s.store_id']) ->indexBy('store_id') ->asArray()->all(); - $salesOnline = Sales::find()->select(['SUM(summ) as total', 'store_id']) - ->where(['between', 'date', $monthStart, $monthEnd]) - ->andWhere(['operation' => Sales::OPERATION_SALE]) - ->andWhere(['NOT IN', 'order_id', ['', '0']]) - ->andWhere(['NOT IN', 'id', $returnSalesIds]) - ->groupBy(['store_id']) + $salesOnline = Sales::find()->alias('s')->select(['SUM(p.summ) as total', 's.store_id']) + ->rightJoin('sales_products p', 's.id = p.check_id') + ->where(['between', 's.date', $monthStart, $monthEnd]) + ->andWhere(['s.operation' => Sales::OPERATION_SALE]) + ->andWhere(['NOT IN', 's.order_id', ['', '0']]) + ->andWhere(['NOT IN', 'p.product_id', array_keys($products1c)]) + ->andWhere(['NOT IN', 's.id', $returnSalesIds]) + ->groupBy(['s.store_id']) ->indexBy('store_id') ->asArray()->all(); $motivations = Motivation::find()->where(['year' => $year, 'month' => $month])->indexBy('store_id')->all(); - $motivationValueGroupFact = MotivationValueGroup::find()->where(['alias' => 'fact'])->one(); - $motivationCostsItemOffline = MotivationCostsItem::find()->where(['name' => 'Оффлайн продажи'])->one(); - $motivationCostsItemOnline = MotivationCostsItem::find()->where(['name' => 'Онлайн продажи'])->one(); - /** @var $motivationCostsItemOffline MotivationCostsItem */ - /** @var $motivationCostsItemOnline MotivationCostsItem */ - - $storeIds = ArrayHelper::getColumn(CityStore::find()->where(['visible' => '1'])->all(), 'id'); foreach ($storeIds as $store_id) { if (isset($motivations[$store_id])) { - $correction = self::getMotivationValue($motivations[$store_id]->id, 8, $motivationCostsItemOffline->code); - - $motivationValueOffline = MotivationValue::find()->where(['motivation_id' => $motivations[$store_id]->id, - 'motivation_group_id' => $motivationValueGroupFact->id, 'value_id' => $motivationCostsItemOffline->code])->one(); - if (!$motivationValueOffline) { - $motivationValueOffline = new MotivationValue; - $motivationValueOffline->motivation_id = $motivations[$store_id]->id; - $motivationValueOffline->motivation_group_id = $motivationValueGroupFact->id; - $motivationValueOffline->value_id = $motivationCostsItemOffline->code; - $motivationValueOffline->value_type = $motivationCostsItemOffline->data_type; - } - $motivationValueOffline->value_float = ($salesOffline[$store_id]['total'] ?? 0) + $correction; - $motivationValueOffline->save(); - if ($motivationValueOffline->getErrors()) { - throw new \Exception(Json::encode($motivationValueOffline->getErrors())); - } + $correction = self::getMotivationValue($motivations[$store_id]->id, 8, self::CODE_OFFLINE_SALES); - $correction = self::getMotivationValue($motivations[$store_id]->id, 8, $motivationCostsItemOnline->code); + self::saveOrUpdateMotivationValue($motivations[$store_id]->id, "fact", + self::CODE_OFFLINE_SALES, 'float', ($salesOffline[$store_id]['total'] ?? 0) + $correction); - $motivationValueOnline = MotivationValue::find()->where(['motivation_id' => $motivations[$store_id]->id, - 'motivation_group_id' => $motivationValueGroupFact->id, 'value_id' => $motivationCostsItemOnline->code])->one(); - if (!$motivationValueOnline) { - $motivationValueOnline = new MotivationValue; - $motivationValueOnline->motivation_id = $motivations[$store_id]->id; - $motivationValueOnline->motivation_group_id = $motivationValueGroupFact->id; - $motivationValueOnline->value_id = $motivationCostsItemOnline->code; - $motivationValueOnline->value_type = $motivationCostsItemOnline->data_type; - } - $motivationValueOnline->value_float = ($salesOnline[$store_id]['total'] ?? 0) + $correction; - $motivationValueOnline->save(); - if ($motivationValueOnline->getErrors()) { - throw new \Exception(Json::encode($motivationValueOnline->getErrors())); - } + $correction = self::getMotivationValue($motivations[$store_id]->id, 8, self::CODE_ONLINE_SALES); + + self::saveOrUpdateMotivationValue($motivations[$store_id]->id, "fact", + self::CODE_ONLINE_SALES, 'float', ($salesOnline[$store_id]['total'] ?? 0) + $correction); } } } -- 2.39.5