From e6183602eb6a4405c97c210bb512266c683708ba Mon Sep 17 00:00:00 2001 From: fomichev Date: Mon, 23 Jun 2025 15:33:30 +0300 Subject: [PATCH] =?utf8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20=D0=B8=20?= =?utf8?q?=D0=BF=D0=BE=D0=BB=D1=83=D1=87=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BA?= =?utf8?q?=D0=BE=D0=BC=D0=BF=D0=BE=D0=BD=D0=B5=D0=BD=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- erp24/controllers/CategoryPlanController.php | 95 +++++++++++++++++--- erp24/services/AutoPlannogrammaService.php | 13 ++- 2 files changed, 91 insertions(+), 17 deletions(-) diff --git a/erp24/controllers/CategoryPlanController.php b/erp24/controllers/CategoryPlanController.php index 710c819c..37388c50 100644 --- a/erp24/controllers/CategoryPlanController.php +++ b/erp24/controllers/CategoryPlanController.php @@ -20,6 +20,7 @@ use yii_app\records\SalesWriteOffsPlan; use yii_app\records\StoreDynamic; use yii_app\records\WriteOffs; use yii_app\records\WriteOffsErp; +use yii_app\services\AutoPlannogrammaService; use yii_app\services\StorePlanService; class CategoryPlanController extends Controller { @@ -43,7 +44,7 @@ class CategoryPlanController extends Controller { ]); $model->load(Yii::$app->request->get()); - + $service = new AutoPlannogrammaService(); $isEditable = date($model->year . '-' . $model->month . '-d') > date('Y-m-d') && ( (date('d') < 25) || (date('Y-m-d', strtotime('-1 month', strtotime(date($model->year . '-' . $model->month . '-d')))) > date('Y-m-d'))); @@ -53,7 +54,7 @@ class CategoryPlanController extends Controller { $currentDate = new \DateTime(); - $startDate = (clone $currentDate)->modify('-4 months')->modify('first day of this month'); + $startDate = (clone $currentDate)->modify('-3 months')->modify('first day of this month'); $endDate = (clone $startDate)->modify('+2 months')->modify('last day of this month'); @@ -64,7 +65,7 @@ class CategoryPlanController extends Controller { "COUNT(*) as cnt", "SUM(CASE WHEN operation='Продажа' THEN s.summ ELSE (CASE WHEN operation='Возврат' THEN -s.summ ELSE 0 END) END) as total", "s.store_id", - "(CASE WHEN p1c.type = 'Матрица' THEN 'Матрица' ELSE p1cn.category END) as type", + "p1cn.category as type", "TO_CHAR(s.date, 'YYYY-MM') as month" ]) ->leftJoin('sales_products sp', 's.id = sp.check_id') @@ -72,22 +73,71 @@ class CategoryPlanController extends Controller { ->leftJoin('products_1c_nomenclature p1cn', 'p1cn.id = sp.product_id') ->where(['between', 's.date', $date_start, $date_end]) ->andWhere(['order_id' => ['', '0']]) - ->andWhere(['not in', 'p1cn.category', ['', 'сборка', 'сервис']]) + ->andWhere(['p1c.components' => '']) + ->andWhere(['not in', 'p1cn.category', ['', 'букет', 'сборка', 'сервис']]) ->andWhere(['s.store_id' => $model->store_id]) ->groupBy([ 's.store_id', "TO_CHAR(s.date, 'YYYY-MM')", - new \yii\db\Expression(" - CASE - WHEN p1c.type = 'Матрица' THEN 'Матрица' - ELSE p1cn.category - END - ") + "p1cn.category" + ]) + ->orderBy(['month' => SORT_ASC, 'type' => SORT_ASC]) + ->asArray() + ->all(); + + $salesMatrix = Sales::find()->alias('s')->select([ + "COUNT(*) as cnt", + "SUM(CASE WHEN operation='Продажа' THEN s.summ ELSE (CASE WHEN operation='Возврат' THEN -s.summ ELSE 0 END) END) as total", + "s.store_id", + "p1c.type as type", + "TO_CHAR(s.date, 'YYYY-MM') as month" + ]) + ->leftJoin('sales_products sp', 's.id = sp.check_id') + ->leftJoin('products_1c p1c', 'p1c.id = sp.product_id') + ->where(['between', 's.date', $date_start, $date_end]) + ->andWhere(['order_id' => ['', '0']]) + ->andWhere(['p1c.type' => 'Матрица']) + ->andWhere(['s.store_id' => $model->store_id]) + ->groupBy([ + 's.store_id', + "TO_CHAR(s.date, 'YYYY-MM')", + "p1c.type" ]) ->orderBy(['month' => SORT_ASC, 'type' => SORT_ASC]) ->asArray() ->all(); + + $compoundSalesData = []; + for ($i = 0; $i < 3; $i++) { +; + $dateToStart = (clone $startDate)->modify("+$i months"); + $compoundSalesYear = $dateToStart->format('Y'); + $compoundSalesMonth = $dateToStart->format('m'); + $compoundSales = $service->getProductsComponentsInCategory($model->store_id, $compoundSalesMonth, $compoundSalesYear); + //var_dump($compoundSales); die(); + $offline = []; + $online = []; + foreach ($compoundSales as $compoundSale) { + if (in_array($compoundSale['order_id'], ['', '0'], true)) { + $offline[] = $compoundSale; + } else { + $online[] = $compoundSale; + } + } + $dateKey = $compoundSalesYear . '-' . $compoundSalesMonth; + $offlineCompoundSum = $service->sumProductsComponentsByGroup($offline, AutoPlannogrammaService::TYPE_SALES); + $onlineCompoundSum = $service->sumProductsComponentsByGroup($online, AutoPlannogrammaService::TYPE_SALES); + + $compoundSalesData['offline'][$dateKey] = $offlineCompoundSum; + $compoundSalesData['online'][$dateKey] = $onlineCompoundSum; + + + + } + //var_dump($compoundSalesData); die(); + + $types = []; $mnths = []; foreach ($sales as $sale) { @@ -96,14 +146,30 @@ class CategoryPlanController extends Controller { } $mnths = array_keys($mnths); sort($mnths); + $countMnths = count($mnths); $weights = []; foreach ($mnths as $ind => $month) { $weights[$month] = $ind + 1; } $table = []; foreach ($sales as $sale) { - $table[$sale['store_id']][$sale['type']] = ($table[$sale['store_id']][$sale['type']] ?? 0) + $weights[$sale['month']] * $sale['total'] / 6; + $table[$sale['store_id']][$sale['type']] = ($table[$sale['store_id']][$sale['type']] ?? 0) + $weights[$sale['month']] * $sale['total'] / $countMnths; + } + foreach ($salesMatrix as $saleMatrix) { + $table[$saleMatrix['store_id']][$saleMatrix['type']] = ($table[$saleMatrix['store_id']][$saleMatrix['type']] ?? 0) + $weights[$saleMatrix['month']] * $saleMatrix['total'] / $countMnths; } + //var_dump($table); die(); + foreach ($compoundSalesData['offline'] as $monthKey => $categories) { + $w = $weights[$monthKey]; + foreach ($categories as $item) { + $store = $item['store_id']; + $type = $item['category']; + $sum = (float)$item['sum']; + $table[$store][$type] = ($table[$store][$type] ?? 0) + + $w * $sum / $countMnths; + } + } + // var_dump($table); die(); ///////////////////////////////////////////////////////////////////////////////////// $salesOnline = Sales::find()->alias('s')->select([ "COUNT(*) as cnt", @@ -133,10 +199,13 @@ class CategoryPlanController extends Controller { ->asArray() ->all(); + + + $tableOnline = []; foreach ($salesOnline as $sale) { $types[$sale['type']] = 1; - $tableOnline[$sale['store_id']][$sale['type']] = ($tableOnline[$sale['store_id']][$sale['type']] ?? 0) + $weights[$sale['month']] * $sale['total'] / 6; + $tableOnline[$sale['store_id']][$sale['type']] = ($tableOnline[$sale['store_id']][$sale['type']] ?? 0) + $weights[$sale['month']] * $sale['total'] / 3; } ///////////////////////////////////////////////////////////////////////////////////// $eit = ExportImportTable::find()->where(['entity' => 'city_store', 'export_id' => 1, 'entity_id' => $model->store_id])->one(); @@ -167,7 +236,7 @@ class CategoryPlanController extends Controller { $tableWriteOffs = []; foreach ($writeOffs as $writeoff) { $types[$writeoff['p1ctype']] = 1; - $tableWriteOffs[$writeoff['p1ctype']] = ($tableWriteOffs[$writeoff['p1ctype']] ?? 0) + $weights[$writeoff['month']] * $writeoff['total'] / 6; + $tableWriteOffs[$writeoff['p1ctype']] = ($tableWriteOffs[$writeoff['p1ctype']] ?? 0) + $weights[$writeoff['month']] * $writeoff['total'] / 3; } $types = array_keys($types); ///////////////////////////////////////////////////////////////////////////////////// diff --git a/erp24/services/AutoPlannogrammaService.php b/erp24/services/AutoPlannogrammaService.php index 6d60b524..db390ec9 100644 --- a/erp24/services/AutoPlannogrammaService.php +++ b/erp24/services/AutoPlannogrammaService.php @@ -2020,6 +2020,7 @@ class AutoPlannogrammaService 's.id', 's.date', 's.operation', + 's.order_id', 'sp.product_id', 'p1c.type', 'p1c.components', @@ -2109,6 +2110,7 @@ class AutoPlannogrammaService 'quantity' => $qty, 'type' => $type, 'operation' => $cp['operation'] ?? '', + 'order_id' => $cp['order_id'] ?? '', ]; } } @@ -2127,8 +2129,8 @@ class AutoPlannogrammaService $priceDynamics = PricesDynamic::find() ->andWhere(['region_id' => $region]) ->andWhere(['product_id' => array_values(ArrayHelper::getColumn($nomenclatures, 'id'))]) - ->andWhere(['<=', 'date_from', $monthEnd]) - ->andWhere(['>=', 'date_to', $monthStart]) + //->andWhere(['<=', 'date_from', $monthEnd]) + // ->andWhere(['>=', 'date_to', $monthStart]) ->orderBy(['date_from' => SORT_DESC]) ->all(); @@ -2143,7 +2145,7 @@ class AutoPlannogrammaService $guid = $componentDataRecord['component_guid']; $nomenclatureData = $nomenclatures[$guid] ?? null; $productId = $nomenclatureData?->id; - $price = 0; + $price = null; $dailyPrices = []; foreach ($pricesByProduct[$productId] ?? [] as $priceRecordForProduct) { if (defined($heliumConstant) && $productId == constant($heliumConstant)) { @@ -2162,7 +2164,9 @@ class AutoPlannogrammaService } if (!empty($dailyPrices)) { $price = min($dailyPrices); - } else { + } + + if ($price === null) { $price = 0.0; } $cost = $componentDataRecord['quantity'] * $price * $componentDataRecord['quantity_product']; @@ -2186,6 +2190,7 @@ class AutoPlannogrammaService 'component_cost' => $costComponent, 'type' => $type, 'operation' => $componentDataRecord['operation'], + 'order_id' => $componentDataRecord['order_id'], ]; } -- 2.39.5