48 lines
1.8 KiB
PHP
48 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\API\v1\Admin\Allowance;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Controllers\Util;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class SummaryController extends Controller
|
|
{
|
|
public function __invoke(Request $request)
|
|
{
|
|
$electionId = (int) Util::getCurrentElection();
|
|
|
|
$q = DB::table('allowance_payouts as ap')
|
|
->leftJoin('users as u', 'u.id', '=', 'ap.paid_by_admin_id')
|
|
->where('ap.election_id', $electionId)
|
|
->where('ap.status', 'paid')
|
|
->selectRaw('ap.paid_by_admin_id as admin_id')
|
|
->selectRaw('MAX(u.name) as admin_name')
|
|
->selectRaw('MAX(u.email) as admin_email')
|
|
->selectRaw('COUNT(*) as payouts_count')
|
|
->selectRaw('COALESCE(SUM(ap.amount_cents), 0) as total_amount_cents')
|
|
->selectRaw("COALESCE(SUM(CASE WHEN ap.method = 'cash' THEN ap.amount_cents ELSE 0 END), 0) as cash_amount_cents")
|
|
->selectRaw("COALESCE(SUM(CASE WHEN ap.method = 'cash' THEN 1 ELSE 0 END), 0) as cash_count")
|
|
->selectRaw("COALESCE(SUM(CASE WHEN ap.method = 'bank' THEN ap.amount_cents ELSE 0 END), 0) as bank_amount_cents")
|
|
->selectRaw("COALESCE(SUM(CASE WHEN ap.method = 'bank' THEN 1 ELSE 0 END), 0) as bank_count")
|
|
->groupBy('ap.paid_by_admin_id')
|
|
->orderByDesc('total_amount_cents');
|
|
|
|
// Optional: filter by method (?method=cash|bank)
|
|
$method = $request->query('method');
|
|
if ($method === 'cash' || $method === 'bank') {
|
|
$q->where('ap.method', $method);
|
|
}
|
|
|
|
$rows = $q->get();
|
|
|
|
return response()->json([
|
|
'status' => 'success',
|
|
'election_id' => $electionId,
|
|
'rows' => $rows,
|
|
]);
|
|
}
|
|
}
|
|
|