From: fomichev Date: Wed, 9 Apr 2025 14:20:38 +0000 (+0300) Subject: Правки в представлении X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=08999b5c4d717853f5dbe1f5818537276d16ef67;p=erp24_rep%2Fyii-erp24%2F.git Правки в представлении --- diff --git a/erp24/services/StorePlanService.php b/erp24/services/StorePlanService.php index efe67fab..a292ae57 100755 --- a/erp24/services/StorePlanService.php +++ b/erp24/services/StorePlanService.php @@ -245,7 +245,7 @@ class StorePlanService $baseDate = strtotime("{$selectedYear}-{$selectedMonth}-01"); // Получаем периоды за 3 предыдущих месяца. - $periods = self::getPeriods($baseDate, 3); + $periods = self::getPeriods($baseDate, 3, true); // Получаем историю продаж для каждого периода. $salesHistory = self::getSalesHistory($storeId, $periods, $category, $subcategory, $species); @@ -255,40 +255,49 @@ class StorePlanService } /** - * Формирует периоды (месяц + недели) за заданное количество предыдущих месяцев от базовой даты. + * Универсальный метод для формирования периодов (месяцев) с датами. * - * @param int $baseDate Timestamp базовой даты. - * @param int $count Количество периодов для получения (по умолчанию 3). + * @param int $baseDate Timestamp базовой даты. + * @param int $count Количество предыдущих месяцев. + * @param bool $withWeeks Если true — добавляются данные по неделям месяца. + * @param bool $withWeights Если true — добавляется вес для каждого периода. * - * @return array Массив периодов с ключами вида "YYYY-MM". + * @return array Ассоциативный массив периодов с ключами вида "YYYY-MM". */ - private static function getPeriods($baseDate, $count = 3) + private static function getPeriods($baseDate, $count, $withWeeks = false, $withWeights = false) { $periods = []; - for ($i = $count; $i >= 1; $i--) { + for ($i = 1; $i <= $count; $i++) { $timestamp = strtotime("-{$i} month", $baseDate); - $month = date('m', $timestamp); $year = date('Y', $timestamp); + $month = date('m', $timestamp); - $periodKey = "{$year}-{$month}"; - $periods[$periodKey] = [ + $periodKey = sprintf('%04d-%02d', $year, $month); + $periodData = [ 'year' => $year, 'month' => $month, - 'weeks' => [], ]; - $daysInMonth = cal_days_in_month(CAL_GREGORIAN, (int)$month, (int)$year); - $weeksCount = $daysInMonth > 28 ? 5 : 4; + if ($withWeights) { + $periodData['weight'] = $count - $i + 1; + } + + if ($withWeeks) { + $weeks = []; + $daysInMonth = cal_days_in_month(CAL_GREGORIAN, (int)$month, (int)$year); - for ($weekNumber = 1; $weekNumber <= $weeksCount; $weekNumber++) { - $range = Motivation::getWeekRange(null, $weekNumber, (int)$month, (int)$year); - $periods[$periodKey]['weeks'][$weekNumber - 1] = $range; + $weeksCount = $daysInMonth > 28 ? 5 : 4; + for ($weekNumber = 1; $weekNumber <= $weeksCount; $weekNumber++) { + $weeks[] = Motivation::getWeekRange(null, $weekNumber, (int)$month, (int)$year); + } + $periodData['weeks'] = $weeks; } + + $periods[$periodKey] = $periodData; } return $periods; } - /** * Получает историю продаж за каждый период по неделям. * @@ -442,7 +451,7 @@ class StorePlanService public static function calculateWeightedSalesForProductsWithoutHistory($storeId, $selectedMonth, $productsWithoutHistory) { $targetDate = strtotime(date('Y') . "-$selectedMonth-01"); - $periods = self::generateWeightedPeriods($targetDate, 3); + $periods = self::getPeriods($targetDate, 3); $weightedResults = []; @@ -476,39 +485,6 @@ class StorePlanService return $weightedResults; } - /** - * Генерирует периоды для расчёта продаж. - * - * @param int $targetDate Timestamp целевой даты. - * @param int $count Количество предыдущих месяцев. - * @param bool $withWeight Если true – к каждому периоду добавляется вес (ключ 'weight'). - * - * @return array Ассоциативный массив, где ключ – "YYYY-MM", а значение – массив с ключами 'year', 'month' - * и, если $withWeight равен true, 'weight'. - */ - private static function generateWeightedPeriods($targetDate, $count = 3, $withWeight = false) - { - $periods = []; - for ($i = 1; $i <= $count; $i++) { - $timestamp = strtotime("-{$i} month", $targetDate); - $year = date('Y', $timestamp); - $month = date('m', $timestamp); - $periodKey = sprintf('%04d-%02d', $year, $month); - - $periodData = [ - 'year' => $year, - 'month' => $month, - ]; - - if ($withWeight) { - // Вес рассчитывается как 4 - $i, то есть для ближайшего месяца – вес 3, затем 2 и 1. - $periodData['weight'] = 4 - $i; - } - - $periods[$periodKey] = $periodData; - } - return $periods; - } /** * Получает идентификаторы товаров, похожих на указанный товар, @@ -699,7 +675,7 @@ class StorePlanService { $targetDate = strtotime(date('Y') . "-$selectedMonth-01"); - $weightedPeriods = self::generateWeightedPeriods($targetDate, 3, true); + $weightedPeriods = self::getPeriods($targetDate, 3, false,true); $productsData = []; $globalTotal = 0; diff --git a/erp24/views/category-plan/show-history-data.php b/erp24/views/category-plan/show-history-data.php index 8b6db46c..073c37b3 100644 --- a/erp24/views/category-plan/show-history-data.php +++ b/erp24/views/category-plan/show-history-data.php @@ -202,7 +202,7 @@ JS; Медианы продаж:
Продажи за месяц:
$salesArray): ?> - Месяц :
+ Месяц :