Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
namespace App\Services;
use App\Models\Taxe;
use Illuminate\Support\Facades\DB;
use InvalidArgumentException;
class TaxeStatService
{
/**
* Calculate sum for a specific tax field with optional filters.
*/
public function calculateSum(string $field, ?string $departmentId = null, ?int $year = null): float
{
$column = $this->validateAndGetColumn($field);
$query = $this->buildBaseQuery($departmentId, $year);
return $query->sum($column);
}
/**
* Calculate average for a specific tax field with optional filters.
*/
public function calculateAverage(string $field, ?string $departmentId = null, ?int $year = null): float
{
$column = $this->validateAndGetColumn($field);
$query = $this->buildBaseQuery($departmentId, $year);
return round($query->avg($column), 2);
}
/**
* Get statistics grouped by location (department or region).
*/
public function getStatsByLocation(string $groupBy = 'department', ?int $year = null): array
{
// Validate groupBy parameter (whitelist approach)
if (!in_array($groupBy, ['department', 'region'], true)) {
throw new InvalidArgumentException('Le paramètre group_by doit être "department" ou "region".');
}
$query = Taxe::query();
// Setup grouping
if ($groupBy === 'region') {
$query->join('departments', 'taxes.department_id', '=', 'departments.department_id');
$query->groupBy('departments.region_name');
$query->select('departments.region_name as location');
} else {
$query->groupBy('taxes.department_id');
$query->select('taxes.department_id as location');
}
// Apply year filter if provided
if ($year !== null) {
$query->where('taxes.year', $year);
}
// Build aggregate selects
$sqlSelects = $this->buildAggregateSelects();
$query->addSelect(DB::raw(implode(', ', $sqlSelects)));
return $query->get()->toArray();
}
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* Get time series data: average tax rate by year for a given region.
*/
public function getTimeSeries(string $region, string $taxType, ?int $startYear = null, ?int $endYear = null): array
{
$this->validateAndGetColumn($taxType);
$rateColumn = $taxType . '_percentage';
$query = Taxe::query()
->join('departments', 'taxes.department_id', '=', 'departments.department_id')
->where('departments.region_name', $region)
->groupBy('taxes.year')
->orderBy('taxes.year')
->select('taxes.year', DB::raw("ROUND(AVG(taxes.{$rateColumn}), 2) as avg_rate"));
if ($startYear !== null) {
$query->where('taxes.year', '>=', $startYear);
}
if ($endYear !== null) {
$query->where('taxes.year', '<=', $endYear);
}
return $query->get()->toArray();
}
/**
* Get correlation data: rate vs amount per commune for scatter plot.
*/
public function getCorrelation(string $departmentId, string $taxType, int $year): array
{
$this->validateAndGetColumn($taxType);
$rateColumn = $taxType . '_percentage';
$amountColumn = $taxType . '_amount';
return Taxe::query()
->where('department_id', $departmentId)
->where('year', $year)
->select('commune_name', "{$rateColumn} as rate", "{$amountColumn} as amount")
->get()
->toArray();
}
/**
* Get distribution data: total collected volume per region for pie chart.
*/
public function getDistribution(string $taxType, ?int $year = null): array
{
$this->validateAndGetColumn($taxType);
$amountColumn = $taxType . '_amount';
$query = Taxe::query()
->join('departments', 'taxes.department_id', '=', 'departments.department_id')
->groupBy('departments.region_name')
->select('departments.region_name as region', DB::raw("SUM(taxes.{$amountColumn}) as total_amount"))
->orderByDesc('total_amount');
if ($year !== null) {
$query->where('taxes.year', $year);
}
return $query->get()->toArray();
}
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* Validate field and return the corresponding column name.
*/
private function validateAndGetColumn(string $field): string
{
if (!in_array($field, Taxe::ALLOWED_STAT_FIELDS)) {
throw new InvalidArgumentException('Champ invalide');
}
return $field . '_amount';
}
/**
* Build base query with optional filters.
*/
private function buildBaseQuery(?string $departmentId, ?int $year)
{
$query = Taxe::query();
if ($departmentId !== null) {
$query->where('department_id', $departmentId);
}
if ($year !== null) {
$query->where('year', $year);
}
return $query;
}
/**
* Build aggregate SELECT statements for stats query.
*/
private function buildAggregateSelects(): array
{
$sqlSelects = [];
// Aggregate amount fields
foreach (Taxe::AMOUNT_FIELDS as $col => $alias) {
$sqlSelects[] = "SUM({$col}) as {$alias}_total_amount";
$sqlSelects[] = "ROUND(AVG({$col}), 2) as {$alias}_avg_amount";
}
// Aggregate percentage fields
foreach (Taxe::PERCENTAGE_FIELDS as $col => $alias) {
$sqlSelects[] = "ROUND(AVG({$col}), 2) as {$alias}_avg_rate";
}
return $sqlSelects;
}
}