->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'];
$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,
- ];
}
}
$productForecastSpecies = $this->calculateProductSalesBySpecies($salesProductForecastShare, $cleanedSpeciesGoals);
$weeklySales = $this->getHistoricalSpeciesShareByWeek($filters['plan_date'], $filters);
- var_dump($weeklySales);die();
+
$weeklySalesForecast = $this->calculateWeeklyProductForecastPieces($productForecastSpecies, $weeklySales, $filters);
return $weeklySalesForecast;