From: Vladimir Fomichev Date: Fri, 26 Sep 2025 15:35:02 +0000 (+0300) Subject: Рассчеты для товаров X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=60357535e614d84a04d83a8e514510e517e6a0f6;p=erp24_rep%2Fyii-erp24%2F.git Рассчеты для товаров --- diff --git a/erp24/services/AutoPlannogrammaService.php b/erp24/services/AutoPlannogrammaService.php index 03be5c99..ad601c8c 100644 --- a/erp24/services/AutoPlannogrammaService.php +++ b/erp24/services/AutoPlannogrammaService.php @@ -1430,6 +1430,34 @@ private function buildCategoryGoals(array $rawGoals, bool $subtractMatrix, int $ ->andWhere(['>=', 'date_to', $dt]) ->asArray() ->column(); + + $actualBySpec = []; + if (!empty($actualProducts)) { + $tuples = (new \yii\db\Query()) + ->select([ + 'pid' => 'p1c.id', + 'category' => 'p1c.category', + 'subcategory'=> 'p1c.subcategory', + 'species' => 'p1c.species', + ]) + ->from(['p1c' => 'products_1c_nomenclature']) + ->leftJoin(['p1' => 'products_1c'], 'p1.id = p1c.id') + ->where(['p1c.id' => $actualProducts]) + ->andWhere(['p1.components' => '']) + ->andWhere(['not in', 'p1c.category', ['', 'букет', 'сборка', 'сервис']]) + ->andWhere(['not', ['p1c.species' => null]]) + ->andWhere(['<>', 'p1c.species', '']) + ->all(); + + foreach ($tuples as $t) { + $cat = $t['category']; + $sub = $t['subcategory']; + $spec = $t['species']; + $pid = $t['pid']; + $actualBySpec[$cat][$sub][$spec][$pid] = true; + } + } + $forecastMap = []; foreach ($productForecastSpecies as $item) { $sid = $item['store_id']; @@ -1452,34 +1480,44 @@ private function buildCategoryGoals(array $rawGoals, bool $subtractMatrix, int $ $week = $w['week']; $wShare = (float)$w['share']; - if ( - !isset( - $forecastMap[$sid], - $forecastMap[$sid][$cat], - $forecastMap[$sid][$cat][$sub], - $forecastMap[$sid][$cat][$sub][$spec] - ) - ) { - continue; + $productsInSpec = $forecastMap[$sid][$cat][$sub][$spec] ?? []; + if (!empty($productsInSpec)) { + foreach ($productsInSpec as $pid => $piecesMon) { + $forecastWeekPieces = round($piecesMon * $wShare, 2); + if (!in_array($pid, $actualProducts)) { + continue; + } + $result[] = [ + 'week' => $week, + 'store_id' => $sid, + 'category' => $cat, + 'subcategory' => $sub, + 'species' => $spec, + 'product_id' => $pid, + 'forecast_month_pieces' => $piecesMon, + 'forecast_week_pieces' => $forecastWeekPieces, + ]; + } } - $productsInSpec = $forecastMap[$sid][$cat][$sub][$spec]; - - foreach ($productsInSpec as $pid => $piecesMon) { - $forecastWeekPieces = round($piecesMon * $wShare, 2); - if (!in_array($pid, $actualProducts)) { - continue; + $specActualSet = $actualBySpec[$cat][$sub][$spec] ?? []; + if (!empty($specActualSet)) { + $missingPids = array_diff(array_keys($specActualSet), array_keys($productsInSpec)); + foreach ($missingPids as $pid) { + if (!in_array($pid, $actualProducts, true)) { + continue; + } + $result[] = [ + 'week' => $week, + 'store_id' => $sid, + 'category' => $cat, + 'subcategory' => $sub, + 'species' => $spec, + 'product_id' => $pid, + 'forecast_month_pieces' => 0.0, + 'forecast_week_pieces' => 0.0, + ]; } - $result[] = [ - 'week' => $week, - 'store_id' => $sid, - 'category' => $cat, - 'subcategory' => $sub, - 'species' => $spec, - 'product_id' => $pid, - 'forecast_month_pieces' => $piecesMon, - 'forecast_week_pieces' => $forecastWeekPieces, - ]; } } @@ -2623,7 +2661,7 @@ private function buildCategoryGoals(array $rawGoals, bool $subtractMatrix, int $ $productForecastSpecies = $this->calculateProductSalesBySpecies($salesProductForecastShare, $cleanedSpeciesGoals); $weeklySales = $this->getHistoricalSpeciesShareByWeek($filters['plan_date'], $filters); - var_dump($weeklySales);die(); + $weeklySalesForecast = $this->calculateWeeklyProductForecastPieces($productForecastSpecies, $weeklySales, $filters); return $weeklySalesForecast;