diff --git a/api/app/Http/Controllers/TaxeStatController.php b/api/app/Http/Controllers/TaxeStatController.php new file mode 100644 index 0000000000000000000000000000000000000000..0491dadd42b27fed63d287d092b0b2915b6c10e1 --- /dev/null +++ b/api/app/Http/Controllers/TaxeStatController.php @@ -0,0 +1,119 @@ +json(['error' => 'Champ invalide'], 400); + } + + $query = Taxe::query(); + + if ($request->has('department_id')) { + $query->where('department_id', $request->input('department_id')); + } + + if ($request->has('year')) { + $query->where('year', $request->input('year')); + } + + $total = $query->sum($column); + + return response()->json([ + 'field' => $field, + 'sum' => $total, + 'filters' => $request->all() + ]); + } + + public function average(Request $request, string $field) + { + $allowedFields = ['tfpnb_amount', 'tfpb_amount', 'th_amount', 'cfe_amount']; + $column = $field . '_amount'; + + if (!in_array($column, $allowedFields)) { + return response()->json(['error' => 'Champ invalide'], 400); + } + + $query = Taxe::query(); + + if ($request->has('department_id')) { + $query->where('department_id', $request->input('department_id')); + } + + if ($request->has('year')) { + $query->where('year', $request->input('year')); + } + + $average = $query->avg($column); + + return response()->json([ + 'field' => $field, + 'average' => round($average, 2), + 'filters' => $request->all() + ]); + } + +public function statsByLocation(Request $request) +{ + $groupBy = $request->input('group_by', 'department'); + + $query = Taxe::query(); + + 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'); + } + + if ($request->has('year')) { + $query->where('taxes.year', $request->input('year')); + } + + $sqlSelects = []; + + $amounts = [ + 'tfpnb_amount' => 'tfpnb', + 'tfpb_amount' => 'tfpb', + 'th_amount' => 'th', + 'cfe_amount' => 'cfe' + ]; + + foreach ($amounts as $col => $alias) { + $sqlSelects[] = "SUM($col) as {$alias}_total_amount"; + $sqlSelects[] = "ROUND(AVG($col), 2) as {$alias}_avg_amount"; + } + + $rates = [ + 'tfpnb_percentage' => 'tfpnb', + 'tfpb_percentage' => 'tfpb', + 'th_percentage' => 'th', + 'cfe_percentage' => 'cfe' + ]; + + foreach ($rates as $col => $alias) { + $sqlSelects[] = "ROUND(AVG($col), 2) as {$alias}_avg_rate"; + } + + $query->addSelect(DB::raw(implode(', ', $sqlSelects))); + + return response()->json([ + 'group_by' => $groupBy, + 'year' => $request->input('year', 'all'), + 'data' => $query->get() + ]); +} +} \ No newline at end of file diff --git a/api/app/Models/Department.php b/api/app/Models/Department.php new file mode 100644 index 0000000000000000000000000000000000000000..09ea02d775047496712843e182e83b125e8baf33 --- /dev/null +++ b/api/app/Models/Department.php @@ -0,0 +1,31 @@ + 'float', + 'tfpb_percentage' => 'float', + 'th_percentage' => 'float', + 'cfe_percentage' => 'float', + + 'tfpnb_amount' => 'float', + 'tfpb_amount' => 'float', + 'th_amount' => 'float', + 'cfe_amount' => 'float', ]; protected $table = 'taxes'; diff --git a/api/bootstrap/app.php b/api/bootstrap/app.php index c1832766e157f3381c0d65bd0484392367b87722..c3928c571872d36e401c71f2eadeada55734a75b 100644 --- a/api/bootstrap/app.php +++ b/api/bootstrap/app.php @@ -7,6 +7,7 @@ use Illuminate\Foundation\Configuration\Middleware; return Application::configure(basePath: dirname(__DIR__)) ->withRouting( web: __DIR__.'/../routes/web.php', + api: __DIR__.'/../routes/api.php', commands: __DIR__.'/../routes/console.php', health: '/up', ) diff --git a/api/database/migrations/2026_01_29_192309_create_taxes_table.php b/api/database/migrations/2026_01_29_192309_create_taxes_table.php index c5fdedc5e31a48ea39f1387acb5ccc1c691888ee..3aacf20450d0057827194174389d0f1314dc753c 100644 --- a/api/database/migrations/2026_01_29_192309_create_taxes_table.php +++ b/api/database/migrations/2026_01_29_192309_create_taxes_table.php @@ -13,19 +13,37 @@ return new class extends Migration { Schema::create('taxes', function (Blueprint $table) { $table->id(); - $table->string('tax'); - $table->integer('amount'); - $table->string('department'); - $table->string('region'); - $table->string('year'); + $table->string('commune_code', 5); + $table->string('commune_name'); + $table->string('department_id', 3); + $table->decimal('tfpnb_amount'); + $table->decimal('tfpnb_percentage', 5, 2); + $table->decimal('tfpb_amount'); + $table->decimal('tfpb_percentage', 5, 2); + $table->decimal('th_amount'); + $table->decimal('th_percentage', 5, 2); + $table->decimal('cfe_amount'); + $table->decimal('cfe_percentage', 5, 2); + $table->integer('year'); + }); + + Schema::create('departments', function (Blueprint $table) { + $table->string('department_id', 3)->primary(); + $table->string('department_name'); + $table->string('region_name'); }); - } + + Schema::table('taxes', function (Blueprint $table) { + $table->foreign('department_id')->references('department_id')->on('departments'); + }); + } /** * Reverse the migrations. */ public function down(): void { Schema::dropIfExists('taxes'); + Schema::dropIfExists('departments'); } }; diff --git a/api/routes/api.php b/api/routes/api.php new file mode 100644 index 0000000000000000000000000000000000000000..bc856bff9a197680d82bac7cde2cd1c90653bec5 --- /dev/null +++ b/api/routes/api.php @@ -0,0 +1,7 @@ +