$weeksShareResult = [];
$weeksGoalResult = [];
$monthCategoryShareResult = [];
+ $weeksProductForecast = [];
+
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$filters = [];
}
+ foreach ($weeksData as $r) {
+ $forecasts = $service->calculateWeekForecastSpeciesProducts($r['category'], $r['subcategory'], $r['species'], $r['store_id'], $r['weekly_goal']);
+ foreach ($forecasts as $forecast) {
+ $weeksProductForecast[] = [
+ 'category' => $forecast['category'] ?? '',
+ 'subcategory' => $forecast['subcategory'] ?? '',
+ 'species' => $forecast['species'] ?? '',
+ 'product_id' => $forecast['product_id'] ?? '',
+ 'name' => $forecast['name'] ?? '',
+ 'price' => $forecast['price'] ?? '',
+ 'goal' => $forecast['goal'] ?? 0,
+ 'forecast' => $forecast['forecast'] ?? 0,
+ 'week' => $r['week'],
+ ];
+ }
+ }
-
+ usort($weeksProductForecast, function($a, $b) {
+ foreach (['category','subcategory','species','name','week'] as $key) {
+ $va = $a[$key];
+ $vb = $b[$key];
+ if ($va < $vb) return -1;
+ if ($va > $vb) return 1;
+ }
+ return 0;
+ });
}
-//var_dump($weeksData); die();
+//var_dump($weeksProductForecast); die();
return $this->render('control-species', [
'model' => $model,
'result' => $monthResult,
'weeksData' => $weeksData,
'monthCategoryShare' => $monthCategoryShareResult,
- // 'weeksGoalResult' => $weeksGoalResult,
+ 'weeksProductForecast' => $weeksProductForecast,
'totals' => $totals,
'storeList' => $storeList,
'monthsList' => $monthsList,
use yii\db\Query;
use yii\helpers\ArrayHelper;
use yii_app\records\CityStore;
+use yii_app\records\PricesDynamic;
+use yii_app\records\Products1cNomenclature;
use yii_app\records\SalesWriteOffsPlan;
class AutoPlannogrammaService
}
- /**
- * Разворачивает вложенный многомерный массив исторических данных
- * в плоский список записей.
- *
- * @param array $historical
- * @return array [ ['week'=>..., 'store_id'=>..., 'category'=>..., 'subcategory'=>..., 'species'=>..., 'sum'=>...], ... ]
- */
- private function flattenHistorical(array $historical): array
+ public static function calculateWeekForecastSpeciesProducts($category, $subcategory, $species, $storeId, $goal)
{
- $flat = [];
- foreach ($historical as $week => $byStore) {
- foreach ($byStore as $storeId => $byCat) {
- foreach ($byCat as $category => $bySub) {
- foreach ($bySub as $subcategory => $bySpec) {
- foreach ($bySpec as $species => $sum) {
- $flat[] = [
- 'week' => $week,
- 'store_id' => $storeId,
- 'category' => $category,
- 'subcategory' => $subcategory,
- 'species' => $species,
- 'sum' => $sum,
- ];
- }
- }
- }
+ $speciesProductForecast = [];
+ $products = Products1cNomenclature::find()
+ ->select(['id', 'name'])
+ ->where(['category' => $category])
+ ->andWhere(['subcategory' => $subcategory])
+ ->andWhere(['species' => $species])
+ ->indexBy('id')
+ ->asArray()
+ ->all();
+
+ $productsIds = ArrayHelper::getColumn($products, 'id');
+ if (CityStore::find()->where(['id' => $storeId])->one()->city_id == 1342) {
+ $region = 52;
+ } elseif (CityStore::find()->where(['id' => $storeId])->one()->city_id == 1) {
+ $region = 77;
+ } else {
+ $region = null;
+ }
+ $priceRecords = PricesDynamic::find()
+ ->select(['product_id', 'price'])
+ ->where(['product_id' => $productsIds])
+ ->andWhere(['active' => 1])
+ ->andWhere(['or', ['region_id' => $region], ['region_id' => null]])
+ ->indexBy('product_id')
+ ->asArray()
+ ->all();
+
+ //var_dump($products); die();
+ foreach ($priceRecords as $id => $record) {
+ if ($goal == 0 || (int)$record['price'] == 0) {
+ $forecast = 0;
+ } else {
+ $forecast = round(max($goal / (float)$record['price'], 1), 0);
}
+ $speciesProductForecast[] = [
+ 'category' => $category,
+ 'subcategory' => $subcategory,
+ 'species' => $species,
+ 'product_id' => $record['product_id'],
+ 'name' => $products[$id]['name'],
+ 'price' => $record['price'] ?? $goal ?? 1,
+ 'goal' => $goal ?? 0,
+ 'forecast' => $forecast
+
+ ];
+
}
- return $flat;
+
+ return $speciesProductForecast;
+
}
+
+
}
/* @var $weeksData array */
/* @var $weeksShareResult array */
/* @var $weeksGoalResult array */
+/* @var array $weeksProductForecast */
+/* @var array $monthCategoryShare */
?>
</tbody>
</table>
<?php endif; ?>
+ <?php if (!empty($weeksProductForecast)): ?>
+ <h2>Прогноз по неделям по неделям</h2>
+ <table class="table table-bordered table-striped">
+ <thead>
+ <tr>
+ <th>Категория</th>
+ <th>Подкатегория</th>
+ <th>Вид</th>
+ <th>Товар (ID)</th>
+ <th>Неделя</th>
+ <th>Цель</th>
+ <th>Цена</th>
+ <th>Прогноз</th>
+ </tr>
+ </thead>
+ <tbody>
+ <?php foreach ($weeksProductForecast as $row): ?>
+ <tr>
+ <td><?= Html::encode($row['category']) ?></td>
+ <td><?= Html::encode($row['subcategory']) ?></td>
+ <td><?= Html::encode($row['species']) ?></td>
+ <td>
+ <?= Html::encode($row['name']) ?>
+ <br><small class="text-muted">(<?= Html::encode($row['product_id']) ?>)</small>
+ </td>
+ <td><?= Html::encode($row['week']) ?></td>
+ <td><?= Html::encode($row['goal']) ?></td>
+ <td><?= Html::encode($row['price']) ?></td>
+ <td><?= Html::encode($row['forecast']) ?></td>
+ </tr>
+ <?php endforeach; ?>
+ </tbody>
+ </table>
+ <?php endif; ?>
</div>