$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;
return true;
});
+
$dataProvider = new ArrayDataProvider([
'allModels' => $flatData,
'pagination' => ['pageSize' => 100],
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) {
'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())
)
->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;
}
->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;
)
->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();
$nomenclatures = Products1cNomenclature::find()
->andWhere(['id' => $guids])
+ ->andWhere(['not in', 'category', ['', 'букет', 'сборка', 'сервис']])
->indexBy('id')
->all();
'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,
}
- 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'];
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);
}