From: fomichev Date: Fri, 30 May 2025 06:51:37 +0000 (+0300) Subject: Правки по подкатегориям - расчет с раскрытием товаров X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=871b978bc7e9f258800e2477089c72ee5b2a4550;p=erp24_rep%2Fyii-erp24%2F.git Правки по подкатегориям - расчет с раскрытием товаров --- diff --git a/erp24/controllers/AutoPlannogrammaController.php b/erp24/controllers/AutoPlannogrammaController.php index 4b86952a..ba72ce2f 100644 --- a/erp24/controllers/AutoPlannogrammaController.php +++ b/erp24/controllers/AutoPlannogrammaController.php @@ -522,8 +522,23 @@ class AutoPlannogrammaController extends BaseController $service = new AutoPlannogrammaService(); $data = $service->getMonthSubcategoryShareOrWriteOff($filters['plan_date'], $filters, $filters['type']); + $flatData = []; + //var_dump($data);die(); + foreach ($data as $storeId => $categories) { + foreach ($categories as $row) { + $flatData[] = [ + 'store_id' => (string)$storeId, + 'category' => $row['category'] ?? null, + 'subcategory' => $row['subcategory'] ?? null, + 'base_total_store' => $row['base_total_store'] ?? null, + 'new_total_store' => $row['new_total_store'] ?? null, + 'total_sum' => $row['total_sum'] ?? null, + 'percent' => $row['percent'] ?? null, + ]; + } + } - $flatData = array_filter($data, function ($row) use ($filters) { + $flatData = array_filter($flatData, function ($row) use ($filters) { foreach ($filters as $key => $value) { if (empty($value)) continue; if (!isset($row[$key])) continue; @@ -535,6 +550,7 @@ class AutoPlannogrammaController extends BaseController return true; }); + $dataProvider = new ArrayDataProvider([ 'allModels' => $flatData, 'pagination' => ['pageSize' => 100], diff --git a/erp24/services/AutoPlannogrammaService.php b/erp24/services/AutoPlannogrammaService.php index d3aabeaf..f6fa4cd2 100644 --- a/erp24/services/AutoPlannogrammaService.php +++ b/erp24/services/AutoPlannogrammaService.php @@ -187,7 +187,7 @@ class AutoPlannogrammaService foreach ($items as $it) { $allComponentsProductIds[$storeId][] = $it['product_id']; } - $sums = $this->sumProductsComponentsByCategory($items, $type); + $sums = $this->sumProductsComponentsByGroup($items, $type, 'category'); $allComponentsProductIds[$storeId] = array_values(array_unique($allComponentsProductIds[$storeId])); foreach ($sums as $sumRow) { @@ -316,6 +316,7 @@ class AutoPlannogrammaService 'total_sum' => 'main.total_sum', 'percent' => new Expression('ROUND(CAST(main.total_sum AS DECIMAL) / NULLIF(totals.total, 0), 4)'), 'type' => new Expression(':type', ['type' => $type]), + 'totals_total' => 'totals.total', ]) ->from([ 'main' => (new Query()) @@ -357,7 +358,83 @@ class AutoPlannogrammaService ) ->orderBy('category, subcategory'); - $result = $query->all(); + $rows = $query->all(); + $periods = [ + (clone $base)->modify('-2 year'), + (clone $base)->modify('-1 year'), + ]; + $componentAdds = []; + $componentAddsSumAll = []; + $allComponentsProdIds = []; + + foreach ($storeIds as $sid) { + foreach ($periods as $dt) { + $items = $this->getProductsComponentsInCategory( + $sid, + (int)$dt->format('n'), + (int)$dt->format('Y'), + $type + ); + if (empty($items)) { + continue; + } + + foreach ($items as $it) { + $allComponentsProdIds[$sid][] = $it['product_id']; + } + + + $sums = $this->sumProductsComponentsByGroup($items, $type, 'subcategory'); + $allComponentsProdIds[$sid] = array_unique($allComponentsProdIds[$sid]); + + foreach ($sums as $sr) { + $cat = $sr['category']; + $subcat = $sr['subcategory']; + $val = $sr['sum']; + + $componentAdds[$sid][$cat][$subcat] = + ($componentAdds[$sid][$cat][$subcat] ?? 0) + $val; + + $componentAddsSumAll[$sid][$cat] = + ($componentAddsSumAll[$sid][$cat] ?? 0) + $val; + } + } + } + $result = []; + + foreach ($rows as $r) { + $sid = $r['store_id']; + $cat = $r['category']; + $subcat = $r['subcategory']; + + + $subTotal = (float)$r['total_sum']; + $catTotal = (float)$r['totals_total']; + + + $addCat = $componentAddsSumAll[$sid][$cat] ?? 0.0; + $addSubcat = $componentAdds[$sid][$cat][$subcat] ?? 0.0; + + + $newCatTotal = $catTotal + $addCat; + $newSubTotal = $subTotal + $addSubcat; + + + $percent = $newCatTotal > 0 + ? round($newSubTotal / $newCatTotal, 4) + : 0.0; + + $result[$sid][] = [ + 'category' => $cat, + 'subcategory' => $subcat, + 'total_sum' => $newSubTotal, + 'percent' => $percent, + 'type' => $type, + 'new_total_store' => $newCatTotal, + 'base_total_store' => $catTotal, + 'products_components_list' => implode(',', $allComponentsProdIds[$sid] ?? []), + ]; + } return $result; } @@ -793,7 +870,7 @@ class AutoPlannogrammaService ->andWhere(['not', ['s.operation' => ['Удален', 'Удаление']]]) ->andWhere(['between', 's.date', $monthStart, $monthEnd]) ->andWhere(['not', ['p1c.components' => '']]) - // ->andWhere(['nom.category' => null]) + //->andWhere(['not in', 'nom.category', ['', 'букет', 'сборка', 'сервис']]) ->asArray() ->all(); $componentProducts = $salesProducts; @@ -828,7 +905,7 @@ class AutoPlannogrammaService ) ->andWhere(['between', 'w.date', $monthStart, $monthEnd]) ->andWhere(['ex.entity_id' => $storeId]) - // ->andWhere(['nom.category' => null]) + //->andWhere(['not in', 'nom.category', ['', 'букет', 'сборка', 'сервис']]) ->andWhere(['<>', 'p1c.components', '']) ->asArray() ->all(); @@ -876,6 +953,7 @@ class AutoPlannogrammaService $nomenclatures = Products1cNomenclature::find() ->andWhere(['id' => $guids]) + ->andWhere(['not in', 'category', ['', 'букет', 'сборка', 'сервис']]) ->indexBy('id') ->all(); @@ -917,6 +995,8 @@ class AutoPlannogrammaService 'component_guid' => $guid, 'component_name' => $n?->name, 'component_category' => $n?->category, + 'component_subcategory' => $n?->subcategory, + 'component_species' => $n?->species, 'quantity' => $r['quantity'], 'price' => $price, 'cost' => $cost, @@ -931,16 +1011,18 @@ class AutoPlannogrammaService } - public function sumProductsComponentsByCategory(array $items, string $type): array + public function sumProductsComponentsByGroup(array $items, string $type, string $group = 'category'): array { $aggregated = []; foreach ($items as $row) { - $storeId = $row['store_id']; - $category = $row['component_category']; - $month = $row['month']; - $year = $row['year']; - $operation = $row['operation'] ?? null; + $storeId = $row['store_id']; + $category = $row['component_category']; + $subcategory = $row['component_subcategory']; + $species = $row['component_species']; + $month = $row['month']; + $year = $row['year']; + $operation = $row['operation'] ?? null; $cost = (float)$row['cost']; @@ -951,25 +1033,46 @@ class AutoPlannogrammaService if ($operation === 'Возврат') { $cost = -$cost; } + } + + $keyParts = [$storeId, $category]; + if ($group === 'subcategory' || $group === 'species') { + $keyParts[] = $subcategory; } + if ($group === 'species') { + $keyParts[] = $species; + } + + $keyParts[] = $year; + $keyParts[] = $month; - $key = implode('|', [$storeId, $category, $year, $month]); + $key = implode('|', $keyParts); if (!isset($aggregated[$key])) { + // базовая структура $aggregated[$key] = [ - 'store_id' => $storeId, - 'category' => $category, - 'sum' => 0.0, - 'month' => $month, - 'year' => $year, - 'type' => $type, + 'store_id' => $storeId, + 'category' => $category, + 'sum' => 0.0, + 'month' => $month, + 'year' => $year, + 'type' => $type, ]; + + if ($group === 'subcategory' || $group === 'species') { + $aggregated[$key]['subcategory'] = $subcategory; + } + if ($group === 'species') { + $aggregated[$key]['species'] = $species; + } } + $aggregated[$key]['sum'] += $cost; } + return array_values($aggregated); }