From: fomichev Date: Wed, 21 May 2025 13:05:49 +0000 (+0300) Subject: Коректировки по товарам без истории и букетам X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=30b4d31022c4b9d00663987b3f84da74718e2d4f;p=erp24_rep%2Fyii-erp24%2F.git Коректировки по товарам без истории и букетам --- diff --git a/erp24/controllers/BouquetController.php b/erp24/controllers/BouquetController.php index f6bb6404..5bf10532 100644 --- a/erp24/controllers/BouquetController.php +++ b/erp24/controllers/BouquetController.php @@ -331,7 +331,7 @@ class BouquetController extends Controller $model->storeId = $storeId; $model->matrix = $matrix; - $result = StorePlanService::getBouquetSpiecesMonthGoalFromForecast($month, $year, $storeId, $matrix); + $result = StorePlanService::getBouquetSpiecesMonthGoalFromForecast($month, $year, $storeId, $matrix); $stores = CityStore::find() ->select(['id', 'name']) diff --git a/erp24/controllers/CategoryPlanController.php b/erp24/controllers/CategoryPlanController.php index b2b52b9d..d5989dc7 100644 --- a/erp24/controllers/CategoryPlanController.php +++ b/erp24/controllers/CategoryPlanController.php @@ -364,7 +364,7 @@ class CategoryPlanController extends Controller { } } } - + // var_dump($weightedResults); die(); return $this->render('show-history-data', [ 'result' => $result, 'weighted' => $weightedResults, diff --git a/erp24/services/AutoPlannogrammaService.php b/erp24/services/AutoPlannogrammaService.php index 38bc39d2..4cdaf1a0 100644 --- a/erp24/services/AutoPlannogrammaService.php +++ b/erp24/services/AutoPlannogrammaService.php @@ -604,11 +604,11 @@ class AutoPlannogrammaService // ——————— WEIGHTED SALES ———————— $t2 = hrtime(true); - $weightedResults = StorePlanService::calculateWeightedSalesForProductsWithoutHistory( + $weightedResults = StorePlanService::calculateMedianSalesForProductsWithoutHistory( $storeId, $month, $year, $productsWithoutHistory ); $dur = (hrtime(true) - $t2) / 1e6; - Yii::warning("calculateWeightedSalesForProductsWithoutHistory for store {$storeId}: {$dur} ms\n"); + Yii::warning("calculateMedianSalesForProductsWithoutHistory for store {$storeId}: {$dur} ms\n"); if (empty($weightedResults)) { continue; diff --git a/erp24/services/StorePlanService.php b/erp24/services/StorePlanService.php index 6b7208a1..0911a597 100755 --- a/erp24/services/StorePlanService.php +++ b/erp24/services/StorePlanService.php @@ -497,12 +497,12 @@ class StorePlanService } $dur = (hrtime(true) - $t2) / 1e6; Yii::warning("calculateMedianSalesForPeriod for product {$guid} {$storeId}: {$dur} ms\n"); - $weights = [3, 2, 1]; + $t3 = hrtime(true); - $weightedValue = self::computeWeightedValue($medianSales, $weights); + $median3 = self::calculateMedianSalesOverPeriods($storeId, $similarProductIds, $periods); $weightedResults[$guid] = [ - 'weightedValue' => $weightedValue, + 'weightedValue' => $median3, 'medianSales' => $medianSales, 'salesValues' => $salesValuesForEachMonth, ]; @@ -612,7 +612,7 @@ class StorePlanService $accumulator = []; foreach ($weightedProductsWithoutHistory as $guid => $info) { - $quantity = (float)$info['weightedValue']; + $quantity = (float)$info; if ($quantity <= 0) { continue; } @@ -696,6 +696,59 @@ class StorePlanService return $query->column(); } + + public static function calculateMedianSalesForProductsWithoutHistory( + int $storeId, + int $selectedMonth, + int $selectedYear, + array $productsWithoutHistory + ): array { + $targetDate = strtotime("{$selectedYear}-{$selectedMonth}-01"); + $periods = self::getPeriods($targetDate, 3); + + $results = []; + foreach ($productsWithoutHistory as $prod) { + $guid = $prod['guid']; + $similar = self::getSimilarProductIDs($guid); + if (empty($similar)) { + $results[$guid] = 0; + continue; + } + + $median3 = self::calculateMedianSalesOverPeriods($storeId, $similar, $periods); + $results[$guid] = $median3; + } + + return $results; + } + + private static function calculateMedianSalesOverPeriods(int $storeId, array $similarProductIds, array $periods): float + { + $allValues = []; + foreach ($periods as $monthInfo) { + list(, $salesValues) = self::calculateMedianSalesForPeriod( + $storeId, + $similarProductIds, + $monthInfo + ); + $allValues = array_merge($allValues, $salesValues); + } + $filtered = array_filter($allValues, fn($v) => $v > 0); + + sort($filtered, SORT_NUMERIC); + $n = count($filtered); + if ($n === 0) { + return 0.0; + } + $mid = (int) floor($n / 2); + if ($n % 2 === 1) { + return $filtered[$mid]; + } + return ($filtered[$mid - 1] + $filtered[$mid]) / 2; + } + + + /** * Вычисляет медианное значение продаж для похожих товаров в заданном периоде. * @@ -714,19 +767,6 @@ class StorePlanService $monthInfo['month'], cal_days_in_month(CAL_GREGORIAN, $monthInfo['month'], $monthInfo['year'])); -/* $salesValues = []; - - foreach ($similarProductIds as $simProdId) { - $sales = Sales::find()->alias('s') - ->innerJoin('sales_products sp', 's.id = sp.check_id') - ->innerJoin('products_1c_nomenclature p1c', 'p1c.id = sp.product_id') - ->where(['p1c.id' => $simProdId]) - ->andWhere(['s.store_id' => $storeId]) - ->andWhere(['between', 's.date', $startDate . ' 00:00:00', $endDate . ' 23:59:59']) - ->andWhere(['order_id' => ['', '0']]) - ->count(); - $salesValues[] = (int)$sales; - }*/ $initTime = (hrtime(true) - $t0) / 1e6; // миллисекунды Yii::warning( "Init (calculateMedianSalesForPeriod): {$initTime} ms\n"); $t1 = hrtime(true); diff --git a/erp24/views/bouquet/month-goal-forecast.php b/erp24/views/bouquet/month-goal-forecast.php index 6b329a84..ea8d4b5c 100644 --- a/erp24/views/bouquet/month-goal-forecast.php +++ b/erp24/views/bouquet/month-goal-forecast.php @@ -20,6 +20,26 @@ $saleTypesLabels = [ 3 => 'Маркетплейсы', ]; +$finalResult = []; +if ($result['final']) { + foreach ($result['final'] as $storeId => $categoryData) { + foreach ($categoryData as $category => $subcategoryData) { + foreach ($subcategoryData as $subcategory => $speciesData) { + foreach ($speciesData as $species => $salesData) { + $finalResult[$storeId][] = [ + + 'category' =>$category, + 'subcategory' =>$subcategory, + 'species' =>$species, + 'goal' =>$salesData, + ]; + } + } + } + + } +} + ?>

Цели букета на месяц: /

@@ -57,19 +77,18 @@ $saleTypesLabels = [

Финальные суммы по магазинам

- $speciesData): ?> + $speciesData): ?>

Магазин:

new ArrayDataProvider([ - 'allModels' => array_map(fn($species, $sum) => [ - 'species' => $species, - 'sum' => $sum, - ], array_keys($speciesData), $speciesData), + 'allModels' => $speciesData, 'pagination' => false, ]), 'columns' => [ + ['attribute' => 'category', 'label' => 'Категория'], + ['attribute' => 'subcategory', 'label' => 'Подкатегория'], ['attribute' => 'species', 'label' => 'Вид продукции'], - ['attribute' => 'sum', 'label' => 'Сумма'], + ['attribute' => 'goal', 'label' => 'Сумма', 'format' => ['decimal', 2]], ], ]) ?>