]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
Вывод доп данных
authorfomichev <vladimir.fomichev@erp-flowers.ru>
Tue, 13 May 2025 10:04:36 +0000 (13:04 +0300)
committerfomichev <vladimir.fomichev@erp-flowers.ru>
Tue, 13 May 2025 10:04:36 +0000 (13:04 +0300)
erp24/controllers/AutoPlannogrammaController.php
erp24/services/AutoPlannogrammaService.php
erp24/views/auto-plannogramma/control-species.php

index ffd94059fd552094f26ed42d3cf964964448f348..13a38e8535bf2f8097b4a734b8aa6ca4549e7a6a 100644 (file)
@@ -238,7 +238,7 @@ class AutoPlannogrammaController extends BaseController
         $weeksData = [];
         $weeksShareResult = [];
         $weeksGoalResult = [];
-
+        $monthCategoryShareResult = [];
         if ($model->load(Yii::$app->request->post()) && $model->validate()) {
             $filters = [];
 
@@ -264,20 +264,7 @@ class AutoPlannogrammaController extends BaseController
                 );
             }
 
-            $monthResult = $service->getMonthSpeciesShareOrWriteOffDate(
-                $dateFrom,
-                $dateTo,
-                $filters,
-                null,
-                $model->type
-            );
-            $speciesMonthTotals = [];
-            foreach ($monthResult as $monthRow) {
-                $speciesMonthTotals[$monthRow['store_id']]
-                [$monthRow['category']]
-                [$monthRow['subcategory']]
-                [$monthRow['species']] = $monthRow['total_sum'] ;
-            }
+
 
             $monthSpeciesGoals = $service->calculateFullGoalChain($filters);
             $monthSpeciesGoalsMap = [];
@@ -285,70 +272,92 @@ class AutoPlannogrammaController extends BaseController
                 $monthSpeciesGoalsMap[$monthSpeciesGoal['store_id']]
                 [$monthSpeciesGoal['category']]
                 [$monthSpeciesGoal['subcategory']]
-                [$monthSpeciesGoal['species']] = $monthSpeciesGoal['goal'] ; // переименовать
+                [$monthSpeciesGoal['species']] = $monthSpeciesGoal['goal'] ;
             }
 
 
-            foreach (range(1, 5) as $ind) {
-                $weekStart = date("Y-m-d 00:00:00", strtotime("+" . (($ind - 1) * 7) . ' days', strtotime($dateFrom)));
-                $weekEnd = date("Y-m-d 23:59:59", strtotime("+" . ($ind * 7 - 1) . ' days', strtotime($dateFrom)));
-                if ($weekEnd > $dateTo) {
-                    $weekEnd = $dateTo;
+
+
+            $weeksShareResult = $service->getHistoricalWeeklySpeciesShare($model->month, $filters, null, 'writeOffs');
+            $weeksData = $service->calculateWeeklySpeciesGoals($weeksShareResult['weeksData'], $monthSpeciesGoals) ;
+
+            $datePlan = $filters['plan_date'];
+            $monthCategoryShare = $service->getMonthCategoryShareOrWriteOffWeighted($datePlan, $filters, null, $filters['type']);
+            $monthCategoryGoal = $service->getMonthCategoryGoal($monthCategoryShare, $datePlan, $filters);
+            foreach ($monthCategoryShare as $sid => $cats) {
+                foreach($cats as $cat) {
+                    $monthCategoryShareResult[$sid][$cat['category']]['total_sum_cat'] = $cat['total_sum_cat'];
+                    $monthCategoryShareResult[$sid][$cat['category']]['share_of_total'] = $cat['share_of_total'];
                 }
-                if ($weekStart > $dateTo) {
-                    continue;
+
+            }
+            //var_dump($monthCategoryShareResult); die();
+            $monthSubcategoryShare = $service->getMonthSubcategoryShareOrWriteOffWeighted($datePlan, $filters, null, $filters['type']);
+            $monthSubcategoryGoal = $service->getMonthSubcategoryGoal($monthSubcategoryShare, $monthCategoryGoal);
+
+            if ($filters['type'] === 'writeOffs') {
+                $salesSubShare        = $service->getMonthSubcategoryShareOrWriteOffWeighted($datePlan, $filters, null, 'sales');
+                $salesSubGoal         = $service->getMonthSubcategoryGoal($salesSubShare, $monthCategoryGoal);
+
+                $catGoalMap = [];
+                foreach ($monthCategoryGoal as $row) {
+                    $catGoalMap[$row['category']] = $row['goal'];
                 }
-                 $weekResult = $service->getMonthSpeciesShareOrWriteOffDate(
-                    $weekStart,
-                    $weekEnd,
-                    $filters,
-                    null,
-                    $model->type
-                );
-                $weeksData[$ind] = $weekResult;
-                if (!$weekResult) {
-                    continue;
+                $salesSubGoalMap = [];
+                foreach ($salesSubGoal as $row) {
+                    $salesSubGoalMap[$row['category']][$row['subcategory']] = $row['goal'];
                 }
 
-                $weeksShareResult[$ind] = [];
-                $weeksGoalResult[$ind] = [];
-                foreach ($weekResult as $weekRow) {
-                    $monthSum = $speciesMonthTotals
-                    [$weekRow['store_id']]
-                    [$weekRow['category']]
-                    [$weekRow['subcategory']]
-                    [$weekRow['species']] ?? null;
-
-                    $monthGoal = $monthSpeciesGoalsMap
-                    [$weekRow['store_id']]
-                    [$weekRow['category']]
-                    [$weekRow['subcategory']]
-                    [$weekRow['species']] ?? null;
-
-
-                    if ($monthSum) {
-                        $weeksShareResult[$ind]
-                        [$weekRow['category']]
-                        [$weekRow['subcategory']]
-                        [$weekRow['species']] = $weekRow['total_sum'] / $monthSum;
-                    }
 
-                    if ($monthSum && $monthGoal) {
-                        $weeksGoalResult[$ind]
-                        [$weekRow['category']]
-                        [$weekRow['subcategory']]
-                        [$weekRow['species']] = ($weekRow['total_sum'] / $monthSum) * $monthGoal;
-                    }
+                foreach ($monthSubcategoryShare as &$row) {
+                    $cat = $row['category'];
+                    $sub = $row['subcategory'];
 
-                }
+                    $writeShare = $row['percent_of_month'];
 
+                    $writeGoal  = ($catGoalMap[$cat] ?? 0) * $writeShare;
+                    $saleGoal   = $salesSubGoalMap[$cat][$sub] ?? 0;
+
+                    if ($saleGoal > 0 && $writeGoal > 0.1 * $saleGoal) {
+                        $row['share'] = 0.1;
+                    }
+                }
+                unset($row);
+                $monthSubcategoryGoal = $service->getMonthSubcategoryGoal($monthSubcategoryShare, $monthCategoryGoal);
             }
 
-            $weeksShareResult = $service->getHistoricalWeeklySpeciesShare($model->month, $filters, null, 'writeOffs');
-            $weeksData = $service->calculateWeeklySpeciesGoals($weeksShareResult['weeksData'], $monthSpeciesGoals) ;
+            $monthSpeciesShare = $service->getMonthSpeciesShareOrWriteOffWeighted($datePlan, $datePlan, $filters, null, $filters['type']);
+            $monthSpeciesGoal = $service->getMonthSpeciesGoalDirty($monthSpeciesShare, $monthSubcategoryGoal);
+            if ($filters['type'] === 'writeOffs') {
+                $salesSpecShare = $service->getMonthSpeciesShareOrWriteOffWeighted($datePlan, $datePlan, $filters, null, 'sales');
+                $salesSpecGoal  = $service->getMonthSpeciesGoalDirty($salesSpecShare, $monthSubcategoryGoal);
 
+                $subGoalMap = [];
+                foreach ($monthSubcategoryGoal as $row) {
+                    $subGoalMap[$row['category']][$row['subcategory']] = $row['goal'];
+                }
+                $salesSpecGoalMap = [];
+                foreach ($salesSpecGoal as $row) {
+                    $salesSpecGoalMap[$row['category']][$row['subcategory']][$row['species']] = $row['goal'];
+                }
 
+                foreach ($monthSpeciesShare as &$row) {
+                    $cat  = $row['category'];
+                    $sub  = $row['subcategory'];
+                    $spec = $row['species'];
 
+                    $writeShare = $row['percent_of_month'];
+                    $writeGoal  = ($subGoalMap[$cat][$sub] ?? 0) * $writeShare;
+                    $saleGoal   = $salesSpecGoalMap[$cat][$sub][$spec] ?? 0;
+
+                    if ($saleGoal > 0 && $writeGoal > 0.1 * $saleGoal) {
+                        $row['share'] = 0.1;
+                    }
+                }
+                unset($row);
+
+                $monthSpeciesGoal = $service->getMonthSpeciesGoalDirty($monthSpeciesShare, $monthSubcategoryGoal);
+            }
 
 
 
@@ -361,7 +370,7 @@ class AutoPlannogrammaController extends BaseController
             'model'           => $model,
             'result'          => $monthResult,
             'weeksData'       => $weeksData,
-           //'monthGoal' => $monthSpeciesGoalsMap,
+           'monthCategoryShare' => $monthCategoryShareResult,
            // 'weeksGoalResult' => $weeksGoalResult,
             'totals'          => $totals,
             'storeList'       => $storeList,
index cbf87ff6203c64074e7453ef1f4fa37b66b5a57d..310deaa045ba1a5d4fa1494b6ccbdf6601f9f577 100644 (file)
@@ -177,9 +177,7 @@ var_dump($totals); die();
             $start = date('Y-m-01 00:00:00', strtotime("-{$offsetMonths} months", $baseMonth));
             $end = date('Y-m-t 23:59:59', strtotime($start));
 
-            $monthStoreTotals = $this->getStoreTotals(
-                $storeIds, $start, $productFilter, $type, $end
-            );
+
 
             $q = (new Query())
                 ->select([
@@ -223,28 +221,33 @@ var_dump($totals); die();
                 $sum = (float)$r['month_sum'] * $w;
                 $weightedSums[$sid][$cat] = ($weightedSums[$sid][$cat] ?? 0) + $sum;
             }
-            foreach ($monthStoreTotals as $sid => $monthStoreTotal) {
-                $monthStoreTotalsWeighted[$sid] = ($monthStoreTotalsWeighted[$sid] ?? 0) + ($monthStoreTotal * $w);
-
-            }
 
         }
 
         $result = [];
         foreach ($weightedSums as $storeId => $cats) {
             $grand = array_sum($cats) ?: 1;
+
+            $unlabeledSum = $cats[''] ?? 0;
+            $labeledCount = count($cats) - (isset($cats['']) ? 1 : 0);
+
+            $distribution = $labeledCount > 0 ? $unlabeledSum / $labeledCount : 0;
+
             foreach ($cats as $category => $weightedSum) {
+                if ($category === '') {
+                    continue;
+                }
+
+                $adjustedSum = $weightedSum + $distribution;
+
                 $result[$storeId][] = [
                     'category' => $category,
                     'total_sum_cat' => $weightedSum,
-                    'total_sum_store' => $monthStoreTotalsWeighted[$storeId],
-                    'query_total_sum_store' => $grand,
-                    'share_of_total' => round($weightedSum / $grand, 4),
-                    'share_of_total_control' => round($weightedSum / $monthStoreTotalsWeighted[$storeId], 4),
+                    'total_sum_store' => $grand,
+                    'share_of_total' => round($adjustedSum / $grand, 4),
                 ];
             }
         }
-
         return $result;
     }
 
index a92d97b244d41cc610959e91b80095d71cf6cc5a..50acfe622ea64aa47e8cd0117e9fb37ca204b87d 100644 (file)
@@ -81,7 +81,7 @@ use yii_app\records\Products1c;
         <tbody>
         <?php foreach ($result as $row): ?>
         <tr>
-            <td><?= Html::encode($row['category']) ?></td>
+            <td><?= Html::encode($row['category']) ?> <?=  $monthCategoryShare[$row['category']]['total_sum_cat'] ?>  <?=  Yii::$app->formatter->asPercent($monthCategoryShare[$row['category']]['share_of_total'], 2) ?></td>
             <td><?= Html::encode($row['subcategory']) ?></td>
             <td><?= Html::encode($row['species']) ?></td>
             <td><?= Yii::$app->formatter->asDecimal($row['total_sum'], 2) ?></td>
@@ -108,7 +108,7 @@ use yii_app\records\Products1c;
             <tbody>
             <?php foreach ($weeksData as $r): ?>
                 <tr>
-                    <td><?= Html::encode($r['category']) ?></td>
+                    <td><?= Html::encode($r['category']) ?>  <?=  $monthCategoryShare[$model->storeId][$r['category']]['total_sum_cat'] ?>  <?=  Yii::$app->formatter->asPercent($monthCategoryShare[$model->storeId][$r['category']]['share_of_total'], 2) ?></td>
                     <td><?= Html::encode($r['subcategory']) ?></td>
                     <td><?= Html::encode($r['species']) ?></td>
                     <td><?= $r['week'] ?>  неделя - начало <?= \yii_app\services\AutoPlannogrammaService::getIsoWeekStart((int)date('Y'), (int)$r['week']) ?></td>