]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
Расчет долей недели
authorfomichev <vladimir.fomichev@erp-flowers.ru>
Thu, 8 May 2025 12:08:09 +0000 (15:08 +0300)
committerfomichev <vladimir.fomichev@erp-flowers.ru>
Thu, 8 May 2025 12:08:09 +0000 (15:08 +0300)
erp24/controllers/AutoPlannogrammaController.php
erp24/services/AutoPlannogrammaService.php
erp24/views/auto-plannogramma/control-species.php

index 0533a4586a12fa4086166a5c0bdd18de960636fd..5c099c2a530f20f4ddf847303254de1cf0d521ad 100644 (file)
@@ -344,22 +344,22 @@ class AutoPlannogrammaController extends BaseController
 
             }
 
-            $weeksShareResult = $service->getWeeksSpeciesShareOrWriteOff($dateFrom, $dateTo, $filters, null, 'writeOffs');
+            $weeksShareResult = $service->getHistoricalWeeklySpeciesShare($model->month, $filters, null, 'writeOffs');
             $weeksData = $weeksShareResult['weeksData'];
-            $weeksShareResult = $weeksShareResult['weeksShare'];
+           // $weeksShareResult = $weeksShareResult['weeksShare'];
 
 
 
 
 
         }
-//var_dump($monthSpeciesGoalsMap); die();
+//var_dump($weeksData); die();
         return $this->render('control-species', [
             'model'           => $model,
             'result'          => $monthResult,
             'weeksData'       => $weeksData,
-            'weeksShareResult' => $weeksShareResult,
-            'weeksGoalResult' => $weeksGoalResult,
+           // 'weeksShareResult' => $weeksShareResult,
+           // 'weeksGoalResult' => $weeksGoalResult,
             'totals'          => $totals,
             'storeList'       => $storeList,
             'monthsList'      => $monthsList,
index cab82f6d25fbcef6659844225375fd3f682f2f4b..74de0b0ef3957af39922faa2b91f08fd63c4f7df 100644 (file)
@@ -930,7 +930,7 @@ var_dump($totals); die();
 
         $dateFrom = sprintf('%04d-%02d-01 00:00:00', $year, $month);
         $dateTo   = date('Y-m-d H:i:s', strtotime($dateFrom . ' +1 month -1 second'));
-
+        //var_dump($monthYear); die();
         $stores   = $this->getVisibleStores();
         $storeIds = array_map(fn($s)=>$s->id, $stores);
         if (!empty($filters['store_id'])) {
@@ -953,7 +953,7 @@ var_dump($totals); die();
             }
 
             $query = (new Query())->select([
-                'week'        => new Expression('$weekIndex'),
+                'week' => new Expression((string)$ind),
                 'store_id'    => 'ex.entity_id',
                 'category'    => 'p1c.category',
                 'subcategory' => 'p1c.subcategory',
@@ -1008,16 +1008,15 @@ var_dump($totals); die();
     }
 
     /**
-     * Строит исторический недельный отчёт за аналогичные месяцы предыдущих лет (2020–year-1)
-     * и вычисляет долю каждой недели от исторического месячного итога по видам.
+     * Исторический недельный отчёт и доли по видам без вложенных циклов.
      *
-     * @param string      $monthYear     месяц-год в формате MM-YYYY, например '03-2025'
-     * @param array|null  $filters       опциональные фильтры ['store_id'=>...]
-     * @param array|null  $productFilter опциональный фильтр по product_id
-     * @param string      $type          'sales' или 'writeOffs'
+     * @param string      $monthYear     месяц-год в формате MM-YYYY
+     * @param array|null  $filters
+     * @param array|null  $productFilter
+     * @param string      $type
      * @return array{
-     *   'historicalWeekly' => array<int, array>,  // суммарные суммы за каждый week
-     *   'weeklyShare'      => array<int, array>   // доли недель относительно исторического месяца
+     *   'historicalWeekly' => array<int, array>,
+     *   'weeklyShare'      => array<int, array>
      * }
      */
     public function getHistoricalWeeklySpeciesShare(
@@ -1026,6 +1025,7 @@ var_dump($totals); die();
         ?array $productFilter = null,
         string $type = 'sales'
     ): array {
+
         [$monthStr, $yearStr] = explode('-', $monthYear);
         $month = (int)$monthStr;
         $year  = (int)$yearStr;
@@ -1033,57 +1033,73 @@ var_dump($totals); die();
         $historical = [];
         for ($yr = 2020; $yr < $year; $yr++) {
             $mYear = sprintf('%02d-%d', $month, $yr);
-            $weekData = $this->getWeeklySpeciesDataForMonth($mYear, $filters, $productFilter, $type);
-            foreach ($weekData as $row) {
-                $idx = $row['week'];
-                $sid = $row['store_id'];
-                $cat = $row['category'];
-                $sub = $row['subcategory'];
+            $weeklyData = $this->getWeeklySpeciesDataForMonth($mYear, $filters, $productFilter, $type);
+            foreach ($weeklyData as $row) {
+                $idx  = $row['week'];
+                $historical[$idx] ??= [];
+                $cat  = $row['category'];
+                $sub  = $row['subcategory'];
                 $spec = $row['species'];
-                $historical[$idx][$sid][$cat][$sub][$spec] =
-                    ($historical[$idx][$sid][$cat][$sub][$spec] ?? 0)
-                    + $row['sum'];
+
+                $historical[$idx][$cat][$sub][$spec] =
+                    ($historical[$idx][$cat][$sub][$spec] ?? 0) + $row['sum'];
             }
         }
 
+
         $dateFrom = sprintf('%04d-%02d-01 00:00:00', $year, $month);
         $dateTo   = date('Y-m-d H:i:s', strtotime($dateFrom . ' +1 month -1 second'));
         $monthWeighted = $this->getMonthSpeciesShareOrWriteOffWeighted(
             $dateFrom, $dateTo, $filters, $productFilter, $type
         );
+
         $monthMap = [];
         foreach ($monthWeighted as $m) {
-            $monthMap[
-            $m['store_id']
-            ][
-            $m['category']
-            ][
-            $m['subcategory']
-            ][
-            $m['species']
-            ] = $m['total_sum'];
+            $cat  = $m['category'];
+            $sub  = $m['subcategory'];
+            $spec = $m['species'];
+            $monthMap[$cat][$sub][$spec] = ($monthMap[$cat][$sub][$spec] ?? 0) + $m['total_sum'];
         }
 
-        $flat = $this->flattenHistorical($historical);
 
-        $weeksShare = [];
-        foreach ($flat as $row) {
-            $w    = $row['week'];
-            $sid  = $row['store_id'];
-            $cat  = $row['category'];
-            $sub  = $row['subcategory'];
-            $spec = $row['species'];
-            $sum  = $row['sum'];
-
-            $monthSum = $monthMap[$sid][$cat][$sub][$spec] ?? 0;
-            if ($monthSum > 0) {
-                $weeksShare[$w][$sid][$cat][$sub][$spec] = round($sum / $monthSum, 4);
+        $flatRows = [];
+        foreach ($historical as $week => $byCat) {
+            foreach ($byCat as $cat => $bySub) {
+                foreach ($bySub as $sub => $bySpec) {
+                    foreach ($bySpec as $spec => $sumWeek) {
+                        $sumMonth = $monthMap[$cat][$sub][$spec] ?? 0;
+                        if ($sumMonth <= 0) {
+                            continue;
+                        }
+                        $flatRows[] = [
+                            'week'        => $week,
+                            'category'    => $cat,
+                            'subcategory' => $sub,
+                            'species'     => $spec,
+                            'percent'     => round($sumWeek / $sumMonth, 4),
+                        ];
+                    }
+                }
             }
         }
+        usort($flatRows, function(array $a, array $b): int {
+            $cmp = strcmp($a['category'], $b['category']);
+            if ($cmp !== 0) {
+                return $cmp;
+            }
+            $cmp = strcmp($a['subcategory'], $b['subcategory']);
+            if ($cmp !== 0) {
+                return $cmp;
+            }
+            $cmp = strcmp($a['species'], $b['species']);
+            if ($cmp !== 0) {
+                return $cmp;
+            }
+            return $a['week'] <=> $b['week'];
+        });
 
         return [
-            'historicalWeekly' => $historical,
-            'weeklyShare'      => $weeksShare,
+            'weeksData' => $flatRows,
         ];
     }
 
index 2ecafb347785119278748129c7fcff1e66cbec61..b73ac84fa2476ffcd4f117585ecd35370c9aecc8 100644 (file)
@@ -93,50 +93,27 @@ use yii_app\records\Products1c;
     <?php endif; ?>
     <?php if (!empty($weeksData)): ?>
         <h2>Результаты по неделям</h2>
-        <?php foreach ($weeksData as $weekNum => $weekRows): ?>
-            <h3>Неделя <?= $weekNum ?></h3>
-            <?php if (empty($weekRows)): ?>
-                <p>Нет данных за эту неделю.</p>
-            <?php else: ?>
-                <table class="table table-striped table-bordered">
-                    <thead>
-                    <tr>
-                        <th>Категория</th>
-                        <th>Подкатегория</th>
-                        <th>Вид товара</th>
-                        <th>Сумма</th>
-                        <th>Доля недели</th>
-                        <th>Цель недели</th>
-                    </tr>
-                    </thead>
-                    <tbody>
-                    <?php foreach ($weekRows as $row): ?>
-                        <tr>
-                            <td><?= Html::encode($row['category']) ?></td>
-                            <td><?= Html::encode($row['subcategory']) ?></td>
-                            <td><?= Html::encode($row['species']) ?></td>
-                            <td><?= Yii::$app->formatter->asDecimal($row['total_sum'], 2) ?></td>
-                            <td>
-                                <?= isset($weeksShareResult[$weekNum])
-                                    ? Yii::$app->formatter->asPercent(
-                                            $weeksShareResult[$weekNum][$row['category']][$row['subcategory']][$row['species']],
-                                            2
-                                    )
-                                    : '-' ?>
-                            </td>
-                            <td>
-                                <?= isset($weeksGoalResult[$weekNum])
-                                    ? Yii::$app->formatter->asPercent(
-                                        $weeksGoalResult[$weekNum][$row['category']][$row['subcategory']][$row['species']],
-                                        2
-                                    )
-                                    : '-' ?>
-                            </td>
-                        </tr>
-                    <?php endforeach; ?>
-                    </tbody>
-                </table>
-            <?php endif; ?>
-        <?php endforeach; ?>
+        <table class="table table-striped table-bordered">
+            <thead>
+            <tr>
+                <th>Категория</th>
+                <th>Подкатегория</th>
+                <th>Вид</th>
+                <th>Неделя</th>
+                <th>Доля (%)</th>
+            </tr>
+            </thead>
+            <tbody>
+            <?php foreach ($weeksData as $r): ?>
+                <tr>
+                    <td><?= Html::encode($r['category']) ?></td>
+                    <td><?= Html::encode($r['subcategory']) ?></td>
+                    <td><?= Html::encode($r['species']) ?></td>
+                    <td><?= $r['week'] ?></td>
+                    <td><?= Yii::$app->formatter->asPercent($r['percent'], 1) ?></td>
+                </tr>
+            <?php endforeach; ?>
+            </tbody>
+        </table>
     <?php endif; ?>
 </div>