public function actionControlSpecies()
{
-
$model = new DynamicModel([
- 'storeId','monthFrom','monthTo','type',
- 'category','subcategory','species'
+ 'storeId', 'month', 'type',
+
]);
- $model->addRule(['monthFrom','monthTo','type'], 'required')
- ->addRule('storeId', 'integer')
- ->addRule(['category','subcategory','species'], 'string');
+ $model->addRule(['month', 'type'], 'required')
+ ->addRule('storeId', 'integer');
$storeList = CityStore::find()
->select(['name','id'])
+ ->where(['visible' => CityStore::IS_VISIBLE])
->indexBy('id')
->column();
$monthsList = [];
for ($i = 0; $i < 12; $i++) {
- $m = (new \DateTime("first day of -{$i} month"))->format('Y-m');
- $monthsList[$m] = (new \DateTime("first day of -{$i} month"))
- ->format('F Y');
- }
-
-
- $categoryList = Products1cNomenclature::find()
- ->select('category')
- ->distinct()
- ->indexBy('category')
- ->column();
-
- $subcategoryList = [];
- if ($model->category) {
- $subcategoryList = Products1cNomenclature::find()
- ->where(['category' => $model->category])
- ->select('subcategory')
- ->distinct()
- ->indexBy('subcategory')
- ->column();
- }
-
- $speciesList = [];
- if ($model->category && $model->subcategory) {
- $speciesList = Products1cNomenclature::find()
- ->where([
- 'category' => $model->category,
- 'subcategory' => $model->subcategory
- ])
- ->select('species')
- ->distinct()
- ->indexBy('species')
- ->column();
+ // получаем метку вида "03-2025"
+ $ts = strtotime("first day of -{$i} month");
+ $key = date('m-Y', $ts);
+ $monthsList[$key] = $key;
}
$result = [];
+ $totals = [];
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
- // формируем фильтры
$filters = [];
- if ($model->storeId) $filters['store_id'] = $model->storeId;
- if ($model->category) $filters['category'] = $model->category;
- if ($model->subcategory)$filters['subcategory']= $model->subcategory;
- if ($model->species) $filters['species'] = $model->species;
- //var_dump($model->type); die();
- $dateFrom = $model->monthFrom . '-01';
+ if ($model->storeId) {
+ $filters['store_id'] = $model->storeId;
+ }
+
+
+ list($m, $y) = explode('-', $model->month);
+ $dateFrom = sprintf('%04d-%02d-01', $y, $m);
$dateTo = date('Y-m-t', strtotime($dateFrom));
- $service = new AutoPlannogrammaService();
- $totals = $service->getStoreTotals([1,2,4], '2025-03-01', null, 'writeOffs');
- // var_dump($totals);die();
- $result = $service
- ->getMonthSpeciesShareOrWriteOffDate(
- '2025-03-01',
- '2025-03-31',
+
+ $service = new AutoPlannogrammaService();
+
+ if ($model->storeId) {
+ $totals = $service->getStoreTotals(
+ [$model->storeId],
+ $dateFrom,
null,
- null, // productFilter
- 'writeOffs'
+ $model->type,
+ $dateTo
);
+ }
+
+ $result = $service->getMonthSpeciesShareOrWriteOffDate(
+ $dateFrom,
+ $dateTo,
+ $filters,
+ null,
+ $model->type
+ );
}
return $this->render('control-species', [
'model' => $model,
'result' => $result,
+ 'totals' => $totals,
'storeList' => $storeList,
'monthsList' => $monthsList,
- 'categoryList' => $categoryList,
- 'subcategoryList' => $subcategoryList,
- 'speciesList' => $speciesList,
+
]);
}
+
/**
* Возвращает список подкатегорий для выбранной категории.
* @param string $category
$storeIds = array_intersect($storeIds, [(int)$filters['store_id']]);
}
- $totals = $this->getStoreTotals($storeIds, '2025-03-01', null, 'writeOffs', '2025-03-31');
+ $totals = $this->getStoreTotals($storeIds, $dateFrom, null, $type, $dateTo);
if (empty($totals)) {
return [];
}
'subcategory' => $row['subcategory'],
'species' => $row['species'],
'total_sum' => (float)$row['total_sum'],
- 'percent_of_month'=> round((float)$row['total_sum'] / $total, 4),
+ 'percent_of_month'=> round(((float)$row['total_sum'] / $total) , 4),
];
}
use yii_app\records\Products1c;
/* @var $this yii\web\View */
-/* @var $result array|null */
/* @var $model DynamicModel */
/* @var $storeList array */
/* @var $monthsList array */
-/* @var $categoryList array */
-/* @var $subcategoryList array */
-/* @var $speciesList array */
+/* @var $result array */
+/* @var $totals array */
?>
-<div class="show-history-data p-4">
- <h1>Ð\9fÑ\80овеÑ\80ка даннÑ\8bÑ\85 по видам</h1>
+<div class="control-species p-4">
+ <h1>Ð\9fÑ\80овеÑ\80ка пÑ\80одаж/Ñ\81пиÑ\81аний по видам</h1>
- <?php $form = ActiveForm::begin([
- 'method' => 'post',
- ]); ?>
+ <?php $form = ActiveForm::begin(['method' => 'post']); ?>
<?= $form->field($model, 'storeId')
->dropDownList($storeList, ['prompt' => 'Выберите магазин'])
->label('Магазин') ?>
- <?= $form->field($model, 'monthFrom')
- ->dropDownList($monthsList, ['prompt' => 'Начало периода'])
- ->label('Дата с') ?>
-
- <?= $form->field($model, 'monthTo')
- ->dropDownList($monthsList, ['prompt' => 'Конец периода'])
- ->label('Дата по') ?>
+ <?= $form->field($model, 'month')
+ ->dropDownList($monthsList, ['prompt' => 'Выберите месяц'])
+ ->label('Месяц') ?>
<?= $form->field($model, 'type')
->radioList([
])
->label('Тип данных') ?>
- <?= $form->field($model, 'category')
- ->dropDownList($categoryList, ['prompt' => 'Выберите категорию'])
- ->label('Категория') ?>
-
- <?= $form->field($model, 'subcategory')
- ->dropDownList($subcategoryList, ['prompt' => 'Выберите подкатегорию'])
- ->label('Подкатегория') ?>
-
- <?= $form->field($model, 'species')
- ->dropDownList($speciesList, ['prompt' => 'Выберите вид товара'])
- ->label('Вид товара') ?>
<div class="form-group">
<?= Html::submitButton('Отправить', ['class' => 'btn btn-primary']) ?>
<?php ActiveForm::end(); ?>
+ <?php if (!empty($totals)): ?>
+ <h2>Итоги по магазину</h2>
+ <table class="table table-bordered">
+ <thead>
+ <tr>
+ <th>Магазин</th>
+ <th>Итого сумма</th>
+ </tr>
+ </thead>
+ <tbody>
+ <?php foreach ($totals as $storeId => $sum): ?>
+ <tr>
+ <td><?= Html::encode($storeList[$storeId] ?? $storeId) ?></td>
+ <td><?= Yii::$app->formatter->asDecimal($sum, 2) ?></td>
+ </tr>
+ <?php endforeach; ?>
+ </tbody>
+ </table>
+ <?php endif; ?>
+
<?php if (!empty($result)): ?>
- <h2>РезÑ\83лÑ\8cÑ\82аÑ\82Ñ\8b</h2>
+ <h2>Ð\94еÑ\82ализаÑ\86иÑ\8f по видам</h2>
<table class="table table-striped table-bordered">
<thead>
<tr>
- <th>Магазин</th>
<th>Категория</th>
<th>Подкатегория</th>
<th>Вид товара</th>
<tbody>
<?php foreach ($result as $row): ?>
<tr>
- <td><?= Html::encode($storeList[$row['store_id']] ?? $row['store_id']) ?></td>
<td><?= Html::encode($row['category']) ?></td>
<td><?= Html::encode($row['subcategory']) ?></td>
<td><?= Html::encode($row['species']) ?></td>
</table>
<?php endif; ?>
- <?php
- $this->registerJsFile('/js/category-plan/show-history-data.js', [
- 'position' => \yii\web\View::POS_END
- ]);
- ?>
+
</div>