$filters = [
'category' => $request->get('category'),
- 'subcategory' => $request->get('subcategory'),
- 'species' => $request->get('species'),
- 'store_id' => $request->get('store_id'),
+ 'subcategory' => $request->get('subcategory') ?? null,
+ 'species' => $request->get('species') ?? null,
+ 'store_id' => $request->get('store_id') ?? [],
'year' => $request->get('year'),
'month' => $request->get('month'),
'type' => $request->get('type'),
// Обработка даты на год и месяц
if (!empty($filters['year']) && !empty($filters['month'])) {
$filters['plan_date'] = $filters['year'] . '-' . str_pad($filters['month'], 2, '0', STR_PAD_LEFT) . '-01';
+ //var_dump($filters); die();
+ $service = new AutoPlannogrammaService();
+ $data = $service->calculateSpeciesForecastForProductsWithoutHistory($filters['plan_date'], $filters);
- $data = StorePlanService::calculateSpeciesForecastForProductsWithoutHistory(
- $filters['store_id'],
- $filters['month'],
- $filters['year'],
- $filters['category'],
- $filters['subcategory'],
- $filters['species'],
- );
-
- var_dump($data); die();
+ //var_dump($data); die();
$flatData = array_filter($data, function ($row) use ($filters) {
foreach ($filters as $key => $value) {
if (empty($value)) continue;
return array_values($filtered);
}
+
+ /**
+ * Общий расчёт плана для заданной категории товаров без истории.
+ *
+ * @param int $storeId
+ * @param string $yearMonth строка "YYYY-MM", например "2025-03"
+ * @param string $category
+ * @param string|null $subcategory
+ * @param string|null $species
+ * @return array [
+ * [
+ * 'store_id' => …,
+ * 'category' => …,
+ * 'subcategory' => …,
+ * 'species' => …,
+ * 'month' => …,
+ * 'year' => …,
+ * 'sum' => …,
+ * ],
+ * …
+ * ]
+ *
+ */
+ public function calculateSpeciesForecastForProductsWithoutHistory($dateFrom, $filters): array
+ {
+ // Получение ID видимых магазинов
+ $storeIds = array_map(fn($store) => $store->id, $this->getVisibleStores());
+
+ $subcategory = !empty($filters['subcategory']) ? $filters['subcategory'] : null;
+ $species = !empty($filters['species']) ? $filters['species'] : null;
+
+ // Применение фильтра по магазину, если указан
+ if (!empty($filters['store_id'])) {
+ $storeIds = array_intersect($storeIds, [(int)$filters['store_id']]);
+ }
+ $date = new \DateTime($dateFrom);
+ $month = $date->format('m');
+ $year = $date->format('Y');
+
+ $result = [];
+ foreach ($storeIds as $storeId) {
+ $histResult = StorePlanService::calculateHistoricalShare(
+ $storeId,
+ $month,
+ $year,
+ $filters['category'],
+ $subcategory,
+ $species
+ );
+
+ $productsWithoutHistory = $histResult['without_history'] ?? [];
+
+ if (empty($productsWithoutHistory)) {
+ continue;
+ }
+
+ $weightedResults = StorePlanService::calculateWeightedSalesForProductsWithoutHistory(
+ $storeId,
+ $month,
+ $year,
+ $productsWithoutHistory
+ );
+
+
+ if (empty($weightedResults)) {
+ continue;
+ }
+
+ $costs = StorePlanService::calculateCostForProductsWithoutHistory(
+ $storeId, $month, $year, $weightedResults
+ );
+
+ if (!empty($costs)) {
+ $result = array_merge($result, $costs);
+ }
+ }
+
+
+ return $result;
+ }
+
+
}
\ No newline at end of file
* ...
* ]
*/
- private static function getSalesHistory($storeId, $periods, $category, $subcategory, $species)
+ private static function getSalesHistory($storeId, $periods, $category, $subcategory = null, $species = null)
{
$salesHistory = [];
}
- /**
- * Общий расчёт плана для заданной категории товаров без истории.
- *
- * @param int $storeId
- * @param string $yearMonth строка "YYYY-MM", например "2025-03"
- * @param string $category
- * @param string|null $subcategory
- * @param string|null $species
- * @return array [
- * [
- * 'store_id' => …,
- * 'category' => …,
- * 'subcategory' => …,
- * 'species' => …,
- * 'month' => …,
- * 'year' => …,
- * 'sum' => …,
- * ],
- * …
- * ]
- *
- */
- public static function calculateSpeciesForecastForProductsWithoutHistory(
- int $storeId,
- string $month,
- string $year,
- string $category,
- ?string $subcategory,
- ?string $species
- ): array {
-
- $histResult = self::calculateHistoricalShare(
- $storeId,
- $month,
- $year,
- $category,
- $subcategory,
- $species
- );
- $productsWithoutHistory = $histResult['without_history'] ?? [];
-
- if (empty($productsWithoutHistory)) {
- return [];
- }
-
- $weightedResults = self::calculateWeightedSalesForProductsWithoutHistory(
- $storeId,
- $month,
- $year,
- $productsWithoutHistory
- );
- if (empty($weightedResults)) {
- return [];
- }
-
- $costs = self::calculateCostForProductsWithoutHistory(
- $storeId,
- $month,
- $year,
- $weightedResults
- );
-
- return $costs;
- }
/**
* Получает идентификаторы товаров, похожих на указанный товар,
use yii_app\records\CityStore;
use yii_app\records\Products1cNomenclature;
?>
- <h1 class="ms-3 mb-4"><?= Html::encode("Расчет цели месяца для типов (month_species_goal)") ?></h1>
+ <h1 class="ms-3 mb-4"><?= Html::encode("Расчет доли товара без истории (month_nohistory_sale_frcst)") ?></h1>
<?php
$form = ActiveForm::begin(['method' => 'get']); ?>
<div class="row p-3">
['attribute' => 'category', 'label' => 'Категория'],
['attribute' => 'subcategory', 'label' => 'Подкатегория'],
['attribute' => 'species', 'label' => 'Тип'],
- ['attribute' => 'goal', 'label' => 'Сумма План', 'format' => ['decimal', 2]],
+ ['attribute' => 'sum', 'label' => 'Сумма', 'format' => ['decimal', 2]],
];
-if ($filters['type'] == 'writeOffs') {
- $columns = array_merge($columns, [
- ['attribute' => 'old_value', 'label' => 'Сумма до сверки', 'format' => ['decimal', 2]],
- ['attribute' => 'sales_goal', 'label' => 'Сумма продаж', 'format' => ['decimal', 2]],
- ]);
-}
+
?>
<?= GridView::widget([
'dataProvider' => $dataProvider,