]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
Правки в представлении
authorfomichev <vladimir.fomichev@erp-flowers.ru>
Wed, 9 Apr 2025 14:20:38 +0000 (17:20 +0300)
committerfomichev <vladimir.fomichev@erp-flowers.ru>
Wed, 9 Apr 2025 14:20:38 +0000 (17:20 +0300)
erp24/services/StorePlanService.php
erp24/views/category-plan/show-history-data.php

index efe67fab83fa4c599d47fe092dba1d9819e583db..a292ae579ade1631247dbd2b30686d42725bdac6 100755 (executable)
@@ -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
     }
 
     /**
-     * Ð¤Ð¾Ñ\80миÑ\80Ñ\83еÑ\82 Ð¿ÐµÑ\80иодÑ\8b (меÑ\81Ñ\8fÑ\86 + Ð½ÐµÐ´ÐµÐ»Ð¸) Ð·Ð° Ð·Ð°Ð´Ð°Ð½Ð½Ð¾Ðµ ÐºÐ¾Ð»Ð¸Ñ\87еÑ\81Ñ\82во Ð¿Ñ\80едÑ\8bдÑ\83Ñ\89иÑ\85 Ð¼ÐµÑ\81Ñ\8fÑ\86ев Ð¾Ñ\82 Ð±Ð°Ð·Ð¾Ð²Ð¾Ð¹ Ð´Ð°Ñ\82Ñ\8b.
+     * Ð£Ð½Ð¸Ð²ÐµÑ\80Ñ\81алÑ\8cнÑ\8bй Ð¼ÐµÑ\82од Ð´Ð»Ñ\8f Ñ\84оÑ\80миÑ\80ованиÑ\8f Ð¿ÐµÑ\80иодов (меÑ\81Ñ\8fÑ\86ев) Ñ\81 Ð´Ð°Ñ\82ами.
      *
-     * @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 Ð\9cассив периодов с ключами вида "YYYY-MM".
+     * @return array Ð\90Ñ\81Ñ\81оÑ\86иаÑ\82ивнÑ\8bй Ð¼ассив периодов с ключами вида "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;
index 8b6db46c809ca88f42dee0783b67f0b75db2e06a..073c37b3e536cf8fd80ead515f8efd4bbbe99539 100644 (file)
@@ -202,7 +202,7 @@ JS;
                             <strong>Медианы продаж:</strong> <?= implode(', ', $weighted[$product['guid']]['medianSales']) ?><br>
                             <strong>Продажи за месяц:</strong><br>
                             <?php foreach ($weighted[$product['guid']]['salesValues'] as $monthIndex => $salesArray): ?>
-                                Месяц <?= $monthIndex+1 ?>: <?= implode(', ', $salesArray) ?><br>
+                                Месяц <?= $monthIndex ?>: <?= implode(', ', $salesArray) ?><br>
                             <?php endforeach; ?>
                         <?php endif; ?>
                     </td>