From d4dcc3b86ef70c2ce22fbdcb1dc0ff6176011fa4 Mon Sep 17 00:00:00 2001 From: fomichev Date: Tue, 20 May 2025 17:51:50 +0300 Subject: [PATCH] =?utf8?q?=D0=92=D1=8B=D0=B2=D0=BE=D0=B4=208=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 | 20 ++--- erp24/services/AutoPlannogrammaService.php | 82 +++++++++++++++++++ erp24/services/StorePlanService.php | 66 +-------------- erp24/views/auto-plannogramma/8.php | 11 +-- 4 files changed, 93 insertions(+), 86 deletions(-) diff --git a/erp24/controllers/AutoPlannogrammaController.php b/erp24/controllers/AutoPlannogrammaController.php index dd5451fa..c96a8fa1 100644 --- a/erp24/controllers/AutoPlannogrammaController.php +++ b/erp24/controllers/AutoPlannogrammaController.php @@ -608,9 +608,9 @@ class AutoPlannogrammaController extends BaseController $filters = [ 'category' => $request->get('category'), - 'subcategory' => $request->get('subcategory'), - 'species' => $request->get('species'), - 'store_id' => $request->get('store_id'), + 'subcategory' => $request->get('subcategory') ?? null, + 'species' => $request->get('species') ?? null, + 'store_id' => $request->get('store_id') ?? [], 'year' => $request->get('year'), 'month' => $request->get('month'), 'type' => $request->get('type'), @@ -625,17 +625,11 @@ class AutoPlannogrammaController extends BaseController // Обработка даты на год и месяц if (!empty($filters['year']) && !empty($filters['month'])) { $filters['plan_date'] = $filters['year'] . '-' . str_pad($filters['month'], 2, '0', STR_PAD_LEFT) . '-01'; + //var_dump($filters); die(); + $service = new AutoPlannogrammaService(); + $data = $service->calculateSpeciesForecastForProductsWithoutHistory($filters['plan_date'], $filters); - $data = StorePlanService::calculateSpeciesForecastForProductsWithoutHistory( - $filters['store_id'], - $filters['month'], - $filters['year'], - $filters['category'], - $filters['subcategory'], - $filters['species'], - ); - - var_dump($data); die(); + //var_dump($data); die(); $flatData = array_filter($data, function ($row) use ($filters) { foreach ($filters as $key => $value) { if (empty($value)) continue; diff --git a/erp24/services/AutoPlannogrammaService.php b/erp24/services/AutoPlannogrammaService.php index 5bb2ddc1..28709a9e 100644 --- a/erp24/services/AutoPlannogrammaService.php +++ b/erp24/services/AutoPlannogrammaService.php @@ -540,4 +540,86 @@ class AutoPlannogrammaService return array_values($filtered); } + + /** + * Общий расчёт плана для заданной категории товаров без истории. + * + * @param int $storeId + * @param string $yearMonth строка "YYYY-MM", например "2025-03" + * @param string $category + * @param string|null $subcategory + * @param string|null $species + * @return array [ + * [ + * 'store_id' => …, + * 'category' => …, + * 'subcategory' => …, + * 'species' => …, + * 'month' => …, + * 'year' => …, + * 'sum' => …, + * ], + * … + * ] + * + */ + public function calculateSpeciesForecastForProductsWithoutHistory($dateFrom, $filters): array + { + // Получение ID видимых магазинов + $storeIds = array_map(fn($store) => $store->id, $this->getVisibleStores()); + + $subcategory = !empty($filters['subcategory']) ? $filters['subcategory'] : null; + $species = !empty($filters['species']) ? $filters['species'] : null; + + // Применение фильтра по магазину, если указан + if (!empty($filters['store_id'])) { + $storeIds = array_intersect($storeIds, [(int)$filters['store_id']]); + } + $date = new \DateTime($dateFrom); + $month = $date->format('m'); + $year = $date->format('Y'); + + $result = []; + foreach ($storeIds as $storeId) { + $histResult = StorePlanService::calculateHistoricalShare( + $storeId, + $month, + $year, + $filters['category'], + $subcategory, + $species + ); + + $productsWithoutHistory = $histResult['without_history'] ?? []; + + if (empty($productsWithoutHistory)) { + continue; + } + + $weightedResults = StorePlanService::calculateWeightedSalesForProductsWithoutHistory( + $storeId, + $month, + $year, + $productsWithoutHistory + ); + + + if (empty($weightedResults)) { + continue; + } + + $costs = StorePlanService::calculateCostForProductsWithoutHistory( + $storeId, $month, $year, $weightedResults + ); + + if (!empty($costs)) { + $result = array_merge($result, $costs); + } + } + + + return $result; + } + + } \ No newline at end of file diff --git a/erp24/services/StorePlanService.php b/erp24/services/StorePlanService.php index 6068c9db..fad3130d 100755 --- a/erp24/services/StorePlanService.php +++ b/erp24/services/StorePlanService.php @@ -332,7 +332,7 @@ class StorePlanService * ... * ] */ - private static function getSalesHistory($storeId, $periods, $category, $subcategory, $species) + private static function getSalesHistory($storeId, $periods, $category, $subcategory = null, $species = null) { $salesHistory = []; @@ -640,71 +640,7 @@ class StorePlanService } - /** - * Общий расчёт плана для заданной категории товаров без истории. - * - * @param int $storeId - * @param string $yearMonth строка "YYYY-MM", например "2025-03" - * @param string $category - * @param string|null $subcategory - * @param string|null $species - * @return array [ - * [ - * 'store_id' => …, - * 'category' => …, - * 'subcategory' => …, - * 'species' => …, - * 'month' => …, - * 'year' => …, - * 'sum' => …, - * ], - * … - * ] - * - */ - public static function calculateSpeciesForecastForProductsWithoutHistory( - int $storeId, - string $month, - string $year, - string $category, - ?string $subcategory, - ?string $species - ): array { - - $histResult = self::calculateHistoricalShare( - $storeId, - $month, - $year, - $category, - $subcategory, - $species - ); - $productsWithoutHistory = $histResult['without_history'] ?? []; - - if (empty($productsWithoutHistory)) { - return []; - } - - $weightedResults = self::calculateWeightedSalesForProductsWithoutHistory( - $storeId, - $month, - $year, - $productsWithoutHistory - ); - if (empty($weightedResults)) { - return []; - } - - $costs = self::calculateCostForProductsWithoutHistory( - $storeId, - $month, - $year, - $weightedResults - ); - - return $costs; - } /** * Получает идентификаторы товаров, похожих на указанный товар, diff --git a/erp24/views/auto-plannogramma/8.php b/erp24/views/auto-plannogramma/8.php index 8e2519ce..aa89abba 100644 --- a/erp24/views/auto-plannogramma/8.php +++ b/erp24/views/auto-plannogramma/8.php @@ -10,7 +10,7 @@ use yii_app\records\CityStore; use yii_app\records\Products1cNomenclature; ?> -

+

'get']); ?>
@@ -102,15 +102,10 @@ $columns = [ ['attribute' => 'category', 'label' => 'Категория'], ['attribute' => 'subcategory', 'label' => 'Подкатегория'], ['attribute' => 'species', 'label' => 'Тип'], - ['attribute' => 'goal', 'label' => 'Сумма План', 'format' => ['decimal', 2]], + ['attribute' => 'sum', 'label' => 'Сумма', 'format' => ['decimal', 2]], ]; -if ($filters['type'] == 'writeOffs') { - $columns = array_merge($columns, [ - ['attribute' => 'old_value', 'label' => 'Сумма до сверки', 'format' => ['decimal', 2]], - ['attribute' => 'sales_goal', 'label' => 'Сумма продаж', 'format' => ['decimal', 2]], - ]); -} + ?> $dataProvider, -- 2.39.5