From 09a59931bbd2cafa331981a20ad5ebff3559ffa4 Mon Sep 17 00:00:00 2001 From: Vladimir Fomichev Date: Fri, 26 Sep 2025 18:11:28 +0300 Subject: [PATCH] =?utf8?q?=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D0=B0?= =?utf8?q?=20=D0=BD=D0=B5=D0=B4=D0=B5=D0=BB=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- erp24/services/AutoPlannogrammaService.php | 71 ++++++++++++++++++++-- 1 file changed, 67 insertions(+), 4 deletions(-) diff --git a/erp24/services/AutoPlannogrammaService.php b/erp24/services/AutoPlannogrammaService.php index 16077d7c..03be5c99 100644 --- a/erp24/services/AutoPlannogrammaService.php +++ b/erp24/services/AutoPlannogrammaService.php @@ -1326,6 +1326,68 @@ private function buildCategoryGoals(array $rawGoals, bool $subtractMatrix, int $ } } + $monthStart = sprintf('%04d-%02d-01 00:00:00', $year, $month); + $monthEnd = date('Y-m-d 23:59:59', strtotime("$monthStart +1 month -1 second")); + + $actualGuids = Products1cNomenclatureActuality::find() + ->select(['guid']) + ->andWhere(['<=', 'date_from', $monthEnd]) + ->andWhere(['or', ['date_to' => null], ['>=', 'date_to', $monthStart]]) + ->asArray() + ->column(); + + if (!empty($actualGuids)) { + $allSpeciesTuples = (new \yii\db\Query()) + ->select([ + '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' => $actualGuids]) + ->andWhere(['p1.components' => '']) + ->andWhere(['not in', 'p1c.category', ['', 'букет', 'сборка', 'сервис']]) + ->andWhere(['not', ['p1c.species' => null]]) + ->andWhere(['<>', 'p1c.species', '']) + ->groupBy(['p1c.category', 'p1c.subcategory', 'p1c.species']) + ->all(); + + $existingKeys = []; + foreach ($result as $row) { + $existingKeys[$row['store_id'] . '|' . $row['category'] . '|' . $row['subcategory'] . '|' . $row['species']] = true; + } + + $weeksCount = max(1, count($targetRanges)); + $evenShare = round(1.0 / $weeksCount, 6); + + foreach ($storeIds as $sid) { + foreach ($allSpeciesTuples as $tpl) { + $cat = $tpl['category']; + $sub = $tpl['subcategory']; + $spec = $tpl['species']; + + $baseKey = $sid . '|' . $cat . '|' . $sub . '|' . $spec; + + if (!isset($existingKeys[$baseKey])) { + foreach ($targetRanges as $posIndex => $range) { + $isoWeekNumber = $range['index']; + $result[] = [ + 'store_id' => $sid, + 'category' => $cat, + 'subcategory'=> $sub, + 'species' => $spec, + 'week' => $isoWeekNumber, + 'share' => $evenShare, + 'sumMonth' => 0.0, + 'sumWeek' => 0.0, + ]; + } + } + } + } + } + $grouped = []; foreach ($result as $idx => $row) { $key = "{$row['store_id']}|{$row['category']}|{$row['subcategory']}|{$row['species']}"; @@ -1336,10 +1398,11 @@ private function buildCategoryGoals(array $rawGoals, bool $subtractMatrix, int $ foreach ($indices as $i) { $sumPercent += $result[$i]['share']; } + if ($sumPercent < 1.0) { - $diff = 1.0 - round($sumPercent, 4); + $diff = 1.0 - round($sumPercent, 4); $count = count($indices); - $add = round($diff / $count, 4); + $add = round($diff / $count, 4); foreach ($indices as $i) { $result[$i]['share'] = round($result[$i]['share'] + $add, 6); } @@ -2558,9 +2621,9 @@ private function buildCategoryGoals(array $rawGoals, bool $subtractMatrix, int $ $salesProductForecastShare = $this->calculateProductForecastShare($noHistoryProductData, $historyProductData); $productForecastSpecies = $this->calculateProductSalesBySpecies($salesProductForecastShare, $cleanedSpeciesGoals); - var_dump($productForecastSpecies); die(); - $weeklySales = $this->getHistoricalSpeciesShareByWeek($filters['plan_date'], $filters); + $weeklySales = $this->getHistoricalSpeciesShareByWeek($filters['plan_date'], $filters); + var_dump($weeklySales);die(); $weeklySalesForecast = $this->calculateWeeklyProductForecastPieces($productForecastSpecies, $weeklySales, $filters); return $weeklySalesForecast; -- 2.39.5