From 7c09250c91c5218c595a8a9867a189e0b8d96bb0 Mon Sep 17 00:00:00 2001 From: Nur Izzati Date: Sun, 10 Aug 2025 11:40:21 +0800 Subject: [PATCH] improve code laporan SA --- app/Http/Controllers/StokAuditController.php | 45 ++++++++++++++------ 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/app/Http/Controllers/StokAuditController.php b/app/Http/Controllers/StokAuditController.php index 0f23e61..d0188c1 100644 --- a/app/Http/Controllers/StokAuditController.php +++ b/app/Http/Controllers/StokAuditController.php @@ -63,29 +63,46 @@ class StokAuditController extends Controller ['taglel', '=', 0], ]) ->orderBy('t_gadai', 'ASC') - ->orderBy('sirig', 'ASC'); + ->orderBy('sirig', 'ASC') + ->get(); - // Dapatkan tarikh terkini untuk trans_type 'T' dan 'A' + // Dapatkan semua norujukan awal + $arraynorujukan = $audit->pluck('norujukan')->toArray(); + + // Dapatkan tarikh terkini untuk setiap norujukan ikut trans_type T $tarikhTerkiniT = Transaction::on('mysql7') - ->where('trans_type', 'T') - ->max('created_at'); + ->select('norujukan', DB::raw('MAX(created_at) as tarikh_max')) + ->where('trans_type', 'T') + ->whereIn('norujukan', $arraynorujukan) + ->groupBy('norujukan') + ->pluck('tarikh_max', 'norujukan') + ->toArray(); + // Dapatkan tarikh terkini untuk setiap norujukan ikut trans_type A $tarikhTerkiniA = Transaction::on('mysql7') - ->where('trans_type', 'A') - ->max('created_at'); + ->select('norujukan', DB::raw('MAX(created_at) as tarikh_max')) + ->where('trans_type', 'A') + ->whereIn('norujukan', $arraynorujukan) + ->groupBy('norujukan') + ->pluck('tarikh_max', 'norujukan') + ->toArray(); + + // Filter audit ikut tarikh masing-masing + $rekodaudit = $audit->filter(function ($item) use ($tarikhTerkiniT, $tarikhTerkiniA) { + $norujukan = $item->norujukan; - // Filter audit berdasarkan tiada transaksi T/A pada tarikh terkini - $rekodaudit = $audit->get()->filter(function ($item) use ($cawangan, $tarikhTerkiniT, $tarikhTerkiniA) { - $adaTebusTerkini = Transaction::on('mysql7') - ->where('norujukan', $item->norujukan) + $adaTebusTerkini = isset($tarikhTerkiniT[$norujukan]) && + Transaction::on('mysql7') + ->where('norujukan', $norujukan) ->where('trans_type', 'T') - ->whereDate('created_at', '>=', date('Y-m-d', strtotime($tarikhTerkiniT))) + ->whereDate('created_at', '>=', date('Y-m-d', strtotime($tarikhTerkiniT[$norujukan]))) ->exists(); - $adaAnsuranTerkini = Transaction::on('mysql7') - ->where('norujukan', $item->norujukan) + $adaAnsuranTerkini = isset($tarikhTerkiniA[$norujukan]) && + Transaction::on('mysql7') + ->where('norujukan', $norujukan) ->where('trans_type', 'A') - ->whereDate('created_at', '>=', date('Y-m-d', strtotime($tarikhTerkiniA))) + ->whereDate('created_at', '>=', date('Y-m-d', strtotime($tarikhTerkiniA[$norujukan]))) ->exists(); return !$adaTebusTerkini && !$adaAnsuranTerkini;