}
}
+ $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']}";
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);
}
$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;