From 57373510f113a2c86be99cbfb8fce3db517539e8 Mon Sep 17 00:00:00 2001 From: Alexander Smirnov Date: Tue, 13 Aug 2024 16:16:38 +0300 Subject: [PATCH] =?utf8?q?[ERP-140]=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?utf8?q?=D0=B5=D0=BD=20=D1=87=D0=B0=D1=81=D1=82=D0=B8=D1=87=D0=BD=D0=BE?= =?utf8?q?=20=D1=84=D0=B0=D0=BA=D1=82=20=D0=BC=D0=BE=D1=82=D0=B8=D0=B2?= =?utf8?q?=D0=B0=D1=86=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../scripts/tasks/task_32_motivation_fact.php | 104 ++++++++ erp24/services/MotivationService.php | 247 ++++++++++++++++++ 2 files changed, 351 insertions(+) create mode 100644 erp24/scripts/tasks/task_32_motivation_fact.php diff --git a/erp24/scripts/tasks/task_32_motivation_fact.php b/erp24/scripts/tasks/task_32_motivation_fact.php new file mode 100644 index 00000000..7c99a103 --- /dev/null +++ b/erp24/scripts/tasks/task_32_motivation_fact.php @@ -0,0 +1,104 @@ +getMessage() . ' >>> ' . $e->getLine(); +} + + + +if (empty($schedulerTaskLog)) { + $schedulerTaskLog = new SchedulerTaskLog(); + $schedulerTaskLog->setTaskNum($taskNum) + ->setName('Task 0' . $taskNum) + ->setDate($dateTask) + ->setDateStart($dateTaskStart) + ->setDateStop($dateTaskStop) + ->setDescription($description) + ->setError($error) + ->setInfo($infoText) + ->setLog($log) + ; +} else { + $schedulerTaskLog->setDateStop($dateTaskStop) + ->setDescription($description) + ->setError($error) + ->setInfo($infoText) + ->setLog($log) + ; +} +$validate = $schedulerTaskLog->validate(); +if ($validate) { + $schedulerTaskLog->save(); +} diff --git a/erp24/services/MotivationService.php b/erp24/services/MotivationService.php index 0ada3d5e..66a665ca 100644 --- a/erp24/services/MotivationService.php +++ b/erp24/services/MotivationService.php @@ -519,4 +519,251 @@ class MotivationService } } } + + public static function calculateMonthSales($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')); + + $sales = Sales::find()->where(['between', 'date', $monthStart, $monthEnd]) + ->andWhere(['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'); + + $salesOffline = Sales::find()->select(['SUM(summ) as total']) + ->where(['between', 'date', $monthStart, $monthEnd]) + ->andWhere(['operation' => Sales::OPERATION_SALE]) + ->andWhere(['order_id' => ['', '0']]) + ->andWhere(['NOT IN', 'check_id', $returnSalesIds]) + ->groupBy(['store_id']) + ->indexBy('store_id') + ->asArray()->all(); + + $salesOnline = Sales::find()->select(['SUM(summ) as total']) + ->where(['between', 'date', $monthStart, $monthEnd]) + ->andWhere(['operation' => Sales::OPERATION_SALE]) + ->andWhere(['NOT IN', 'order_id', ['', '0']]) + ->andWhere(['NOT IN', 'check_id', $returnSalesIds]) + ->groupBy(['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])) { + if (isset($salesOffline[$store_id])) { + $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']; + $motivationValueOffline->save(); + if ($motivationValueOffline->getErrors()) { + throw new \Exception(Json::encode($motivationValueOffline->getErrors())); + } + } + if (isset($salesOnline[$store_id])) { + $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']; + $motivationValueOnline->save(); + if ($motivationValueOnline->getErrors()) { + throw new \Exception(Json::encode($motivationValueOnline->getErrors())); + } + } + } + } + } + + public static function calculateMonthServices($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')); + + // Ищем каталог-гуиды категории services + $productsClass = ProductsClass::find()->select(['category_id', 'tip']) + ->where(['tip' => 'services']) + ->indexBy('category_id') + ->asArray()->all(); + + // Ищем продуктовые гуиды по каталог-гуидам категории service + $products1c = Products1c::find()->select(['id', 'parent_id', 'name']) + ->where(['parent_id' => array_keys($productsClass), 'tip' => 'products']) + ->indexBy(['id']) + ->asArray()->all(); + + // Ищем каталог-гуиды категории services_delivery + $productsClassDelivery = ProductsClass::find()->select(['category_id', 'tip']) + ->where(['tip' => 'services_delivery']) + ->indexBy('category_id') + ->asArray()->all(); + + // Ищем продуктовые гуиды по каталог-гуидам категории service_delivery + $products1cDelivery = Products1c::find()->select(['id', 'parent_id', 'name']) + ->where(['parent_id' => array_keys($productsClassDelivery), 'tip' => 'products']) + ->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(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)]) + ->groupBy(['s.store_id']) + ->indexBy('s.store_id') + ->asArray()->one(); + + // Ищем продажи по продуктовым гуидам по каталог-гуидам категории 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(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)]) + ->groupBy(['s.store_id']) + ->indexBy('s.store_id') + ->asArray()->one(); + + $motivations = Motivation::find()->where(['year' => $year, 'month' => $month])->indexBy('store_id')->all(); + + $motivationValueGroupFact = MotivationValueGroup::find()->where(['alias' => 'fact'])->one(); + $motivationCostsItemServices = MotivationCostsItem::find()->where(['name' => 'Услуги по сборке'])->one(); + $motivationCostsItemServicesDelivery = MotivationCostsItem::find()->where(['name' => 'Услуги по доставке'])->one(); + /** @var $motivationCostsItemServices MotivationCostsItem */ + /** @var $motivationCostsItemServicesDelivery MotivationCostsItem */ + + $storeIds = ArrayHelper::getColumn(CityStore::find()->where(['visible' => '1'])->all(), 'id'); + foreach ($storeIds as $store_id) { + if (isset($motivations[$store_id])) { + if (isset($salesProduct[$store_id])) { + $motivationValueService = MotivationValue::find()->where(['motivation_id' => $motivations[$store_id]->id, + 'motivation_group_id' => $motivationValueGroupFact->id, 'value_id' => $motivationCostsItemServices->code])->one(); + if (!$motivationValueService) { + $motivationValueService = new MotivationValue; + $motivationValueService->motivation_id = $motivations[$store_id]->id; + $motivationValueService->motivation_group_id = $motivationValueGroupFact->id; + $motivationValueService->value_id = $motivationCostsItemServices->code; + $motivationValueService->value_type = $motivationCostsItemServices->data_type; + } + $motivationValueService->value_float = $salesProduct[$store_id]['total']; + $motivationValueService->save(); + if ($motivationValueService->getErrors()) { + throw new \Exception(Json::encode($motivationValueService->getErrors())); + } + } + if (isset($salesProductDelivery[$store_id])) { + $motivationValueServiceDelivery = MotivationValue::find()->where(['motivation_id' => $motivations[$store_id]->id, + 'motivation_group_id' => $motivationValueGroupFact->id, 'value_id' => $motivationCostsItemServicesDelivery->code])->one(); + if (!$motivationValueServiceDelivery) { + $motivationValueServiceDelivery = new MotivationValue; + $motivationValueServiceDelivery->motivation_id = $motivations[$store_id]->id; + $motivationValueServiceDelivery->motivation_group_id = $motivationValueGroupFact->id; + $motivationValueServiceDelivery->value_id = $motivationCostsItemServicesDelivery->code; + $motivationValueServiceDelivery->value_type = $motivationCostsItemServicesDelivery->data_type; + } + $motivationValueServiceDelivery->value_float = $salesProductDelivery[$store_id]['total']; + $motivationValueServiceDelivery->save(); + if ($motivationValueServiceDelivery->getErrors()) { + throw new \Exception(Json::encode($motivationValueServiceDelivery->getErrors())); + } + } + } + } + } + + 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')); + + $motivations = Motivation::find()->where(['year' => $year, 'month' => $month])->indexBy('store_id')->all(); + $motivationValueGroup = MotivationValueGroup::find()->where(['alias' => 'fact'])->one(); + $exportImportTables = ExportImportTable::find()->select(['export_val'])->where(['entity' => 'city_store', + 'entity_id' => array_keys($motivations), 'export_id' => 1])->indexBy('entity_id')->all(); + /** @var $exportImportTables ExportImportTable[] */ + + foreach ($exportImportTables as $store_id => $store_guid) { + $writeOffs = WriteOffs::find()->select(['sum(summ) as total', 'type']) + ->where(['between', 'date', $monthStart, $monthEnd]) + ->andWhere(['store_id' => $store_guid]) + ->groupBy(['type']) + ->indexBy('type') + ->asArray()->all(); + + foreach ($writeOffs as $key => $data) { + $motivationItemType = MotivationCostsItem::writeOffsToMotivationItemMap($key); + if (empty($motivationItemType)) { + continue; + } + $motivationCostsItem = MotivationCostsItem::find()->where(['name' => $motivationItemType])->one(); + /** @var $motivationCostsItem MotivationCostsItem */ + if (isset($motivations[$store_id])) { + $motivationValue = MotivationValue::find()->where(['motivation_id' => $motivations[$store_id]->id, + 'motivation_group_id' => $motivationValueGroup->id, 'value_id' => $motivationCostsItem->code])->one(); + if (!$motivationValue) { + $motivationValue = new MotivationValue; + $motivationValue->motivation_id = $motivations[$store_id]->id; + $motivationValue->motivation_group_id = $motivationValueGroup->id; + $motivationValue->value_id = $motivationCostsItem->code; + $motivationValue->value_type = $motivationCostsItem->data_type; + } + $motivationValue->value_float = $data['total']; + $motivationValue->save(); + if ($motivationValue->getErrors()) { + throw new \Exception(Json::encode($motivationValue->getErrors())); + } + } + } + } + } } -- 2.39.5