Here is my code:
add_hook('ClientAreaPageInvoices', 1, function($vars) {
$user_id = $_SESSION["uid"];
$today = Carbon::now()->format('Y-m-d');
$query_1 = Capsule::table('tblinvoices')
->select(Capsule::raw('SUM(total) as total_aberto'))
->where(array('userid' => $user_id, 'status' => 'Unpaid'))
->get();
$query_2 = Capsule::table('tblinvoices')
->select(Capsule::raw('SUM(total) as total_vencido'))
->where(array('userid' => $user_id, 'status' => 'Unpaid'))
->whereDate('duedate', '<=', $today)
->get();
$query_3 = Capsule::table('tblinvoices')
->select(Capsule::raw('SUM(total) as total_pago'))
->where(array('userid' => $user_id, 'status' => 'Paid'))
->get();
$array_1 = json_decode(json_encode($query_1), True);
$array_2 = json_decode(json_encode($query_2), True);
$array_3 = json_decode(json_encode($query_3), True);
$totalAberto = $array_1[0]['total_aberto'];
$totalVencido = $array_2[0]['total_vencido'];
$totalPago = $array_3[0]['total_pago'];
return array ('totalAberto' => $totalAberto,
'totalVencido' => $totalVencido,
'totalPago' => $totalPago);
});
Is there any way to simplify the entire code in general?
It works well - I just want to learn other practices to make this type of consultation in a more optimized, and simplified way.