From f77078f12734731e44e6ccf3d639689cd86728fb Mon Sep 17 00:00:00 2001 From: fomichev Date: Wed, 21 May 2025 12:24:20 +0300 Subject: [PATCH] =?utf8?q?=D0=92=D1=8B=D0=B2=D0=BE=D0=B4=209=20=D1=88?= =?utf8?q?=D0=B0=D0=B3=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../AutoPlannogrammaController.php | 4 +-- erp24/services/AutoPlannogrammaService.php | 35 +++++++++++++------ erp24/services/StorePlanService.php | 23 ++++++++++-- erp24/views/auto-plannogramma/8.php | 2 +- erp24/views/auto-plannogramma/9.php | 5 ++- 5 files changed, 53 insertions(+), 16 deletions(-) diff --git a/erp24/controllers/AutoPlannogrammaController.php b/erp24/controllers/AutoPlannogrammaController.php index e6410cdb..48ecc5eb 100644 --- a/erp24/controllers/AutoPlannogrammaController.php +++ b/erp24/controllers/AutoPlannogrammaController.php @@ -728,9 +728,9 @@ class AutoPlannogrammaController extends BaseController } //var_dump($bouquetSpeciesForecast); die(); $noHistoryProductData = $service->calculateSpeciesForecastForProductsWithoutHistory($filters['plan_date'], $filters); - var_dump($noHistoryProductData); die(); - $cleanedSpeciesGoals = $service->subtractSpeciesGoals($data, $bouquetSpeciesForecast, $noHistoryProductData); + $cleanedSpeciesGoals = $service->subtractSpeciesGoals($data, $bouquetSpeciesForecast, $noHistoryProductData); + //var_dump($cleanedSpeciesGoals); die(); $flatData = array_filter($cleanedSpeciesGoals, function ($row) use ($filters) { foreach ($filters as $key => $value) { diff --git a/erp24/services/AutoPlannogrammaService.php b/erp24/services/AutoPlannogrammaService.php index e41516a5..38bc39d2 100644 --- a/erp24/services/AutoPlannogrammaService.php +++ b/erp24/services/AutoPlannogrammaService.php @@ -2,6 +2,7 @@ namespace yii_app\services; +use Yii; use yii\db\Expression; use yii\db\Query; use yii\helpers\ArrayHelper; @@ -565,6 +566,7 @@ class AutoPlannogrammaService */ public function calculateSpeciesForecastForProductsWithoutHistory($dateFrom, $filters): array { + $t0 = hrtime(true); // Получение ID видимых магазинов $storeIds = array_map(fn($store) => $store->id, $this->getVisibleStores()); @@ -580,7 +582,10 @@ class AutoPlannogrammaService $year = $date->format('Y'); $result = []; + $initTime = (hrtime(true) - $t0) / 1e6; // миллисекунды + Yii::warning( "Init (getVisibleStores + filters): {$initTime} ms\n"); foreach ($storeIds as $storeId) { + $t1 = hrtime(true); $histResult = StorePlanService::calculateHistoricalShare( $storeId, $month, @@ -589,34 +594,41 @@ class AutoPlannogrammaService $subcategory, $species ); + $dur = (hrtime(true) - $t1) / 1e6; + Yii::warning( "calculateHistoricalShare for store {$storeId}: {$dur} ms\n"); $productsWithoutHistory = $histResult['without_history'] ?? []; - if (empty($productsWithoutHistory)) { continue; } + // ——————— WEIGHTED SALES ———————— + $t2 = hrtime(true); $weightedResults = StorePlanService::calculateWeightedSalesForProductsWithoutHistory( - $storeId, - $month, - $year, - $productsWithoutHistory + $storeId, $month, $year, $productsWithoutHistory ); - + $dur = (hrtime(true) - $t2) / 1e6; + Yii::warning("calculateWeightedSalesForProductsWithoutHistory for store {$storeId}: {$dur} ms\n"); if (empty($weightedResults)) { continue; } + // ——————— COST CALCULATION ———————— + $t3 = hrtime(true); $costs = StorePlanService::calculateCostForProductsWithoutHistory( $storeId, $month, $year, $weightedResults ); + $dur = (hrtime(true) - $t3) / 1e6; + Yii::warning( "calculateCostForProductsWithoutHistory for store {$storeId}: {$dur} ms\n"); if (!empty($costs)) { $result = array_merge($result, $costs); } } + $totalTime = (hrtime(true) - $t0) / 1e6; + Yii::warning( "Total calculateSpeciesForecastForProductsWithoutHistory: {$totalTime} ms\n"); return $result; } @@ -629,8 +641,8 @@ class AutoPlannogrammaService return $mapped; } - public function subtractSpeciesGoals(array $data, array $forecast, array $noHistory): array { - $forecastMap = $this->mapGoalsBySpecies($forecast); + public function subtractSpeciesGoals(array $data, array $bouquetForecast, array $noHistory): array { + $bouquetForecastMap = $this->mapGoalsBySpecies($bouquetForecast); $noHistoryMap = $this->mapGoalsBySpecies($noHistory); $result = []; @@ -641,10 +653,10 @@ class AutoPlannogrammaService $species = $row['species']; $originalGoal = $row['goal']; - $forecastGoal = $forecastMap[$storeId][$category][$subcategory][$species] ?? 0; + $bouquetForecastGoal = $bouquetForecastMap[$storeId][$category][$subcategory][$species] ?? 0; $noHistoryGoal = $noHistoryMap[$storeId][$category][$subcategory][$species] ?? 0; - $cleanGoal = $originalGoal - $forecastGoal - $noHistoryGoal; + $cleanGoal = $originalGoal - ($bouquetForecastGoal + $noHistoryGoal); $result[] = [ @@ -652,6 +664,9 @@ class AutoPlannogrammaService 'category' => $category, 'subcategory' => $subcategory, 'species' => $species, + 'dirtyGoal' => $originalGoal, + 'bouquetGoal' => $bouquetForecastGoal, + 'noHistoryGoal' => $noHistoryGoal, 'goal' => $cleanGoal ]; } diff --git a/erp24/services/StorePlanService.php b/erp24/services/StorePlanService.php index e505ea5c..200f810e 100755 --- a/erp24/services/StorePlanService.php +++ b/erp24/services/StorePlanService.php @@ -4,6 +4,7 @@ namespace yii_app\services; use DateTime; use yii\db\Expression; +use yii\db\Query; use yii\helpers\ArrayHelper; use yii_app\records\BouquetComposition; use yii_app\records\BouquetCompositionMatrixTypeHistory; @@ -630,10 +631,10 @@ class StorePlanService 'species' => $sp, 'month' => $selectedMonth, 'year' => $selectedYear, - 'sum' => 0.0, + 'goal' => 0.0, ]; } - $accumulator[$key]['sum'] += $cost; + $accumulator[$key]['goal'] += $cost; } return array_values($accumulator); @@ -714,6 +715,24 @@ class StorePlanService $salesValues[] = (int)$sales; } + /* $rows = (new Query()) + ->select(['sp.product_id', 'cnt' => 'COUNT(*)']) + ->from(['s' => 'sales']) + ->innerJoin(['sp' => 'sales_products'], 's.id = sp.check_id') + ->where(['sp.product_id' => $similarProductIds]) + ->andWhere(['s.store_id' => $storeId]) + ->andWhere(['between', 's.date', $startDate . ' 00:00:00', $endDate . ' 23:59:59']) + ->andWhere(['not', ['s.order_id' => ['','0']]]) + ->groupBy('sp.product_id') + ->indexBy('product_id') + ->all();*/ + + //var_dump($similarProductIds, $storeId, $startDate, $endDate);die(); +// $salesValues = []; +// foreach ($similarProductIds as $id) { +// $salesValues[] = isset($rows[$id]) ? (int)$rows[$id]['cnt'] : 0; +// } + $nonZeroSales = array_filter($salesValues, function($val) { return $val > 0; }); diff --git a/erp24/views/auto-plannogramma/8.php b/erp24/views/auto-plannogramma/8.php index aa89abba..885a80ed 100644 --- a/erp24/views/auto-plannogramma/8.php +++ b/erp24/views/auto-plannogramma/8.php @@ -102,7 +102,7 @@ $columns = [ ['attribute' => 'category', 'label' => 'Категория'], ['attribute' => 'subcategory', 'label' => 'Подкатегория'], ['attribute' => 'species', 'label' => 'Тип'], - ['attribute' => 'sum', 'label' => 'Сумма', 'format' => ['decimal', 2]], + ['attribute' => 'goal', 'label' => 'Сумма', 'format' => ['decimal', 2]], ]; diff --git a/erp24/views/auto-plannogramma/9.php b/erp24/views/auto-plannogramma/9.php index 620d8198..5ae0593e 100644 --- a/erp24/views/auto-plannogramma/9.php +++ b/erp24/views/auto-plannogramma/9.php @@ -102,7 +102,10 @@ $columns = [ ['attribute' => 'category', 'label' => 'Категория'], ['attribute' => 'subcategory', 'label' => 'Подкатегория'], ['attribute' => 'species', 'label' => 'Тип'], - ['attribute' => 'goal', 'label' => 'Сумма', 'format' => ['decimal', 2]], + ['attribute' => 'dirtyGoal', 'label' => 'Неочищенная цель', 'format' => ['decimal', 2]], + ['attribute' => 'bouquetGoal', 'label' => 'Цель букета', 'format' => ['decimal', 2]], + ['attribute' => 'noHistoryGoal', 'label' => 'Товары без истории', 'format' => ['decimal', 2]], + ['attribute' => 'goal', 'label' => 'Очищенная цель', 'format' => ['decimal', 2]], ]; -- 2.39.5