From 67adddb7533d8afcb832001e667595731982a5ed Mon Sep 17 00:00:00 2001 From: Nur Izzati Date: Tue, 15 Jul 2025 10:40:11 +0800 Subject: [PATCH] tambah baik code yang gadai tebus masa --- app/Services/Reports/GadaianService.php | 148 ++++++++++++++---------- 1 file changed, 86 insertions(+), 62 deletions(-) diff --git a/app/Services/Reports/GadaianService.php b/app/Services/Reports/GadaianService.php index bf7755d..1faa5ed 100644 --- a/app/Services/Reports/GadaianService.php +++ b/app/Services/Reports/GadaianService.php @@ -196,49 +196,82 @@ class GadaianService public function getCawanganReportGadaiTebusMasa() { $cawangan_connection = Cawangan::where('kodcaw', $this->kod_cawangan)->firstOrFail()->mysql; + $online_connection = Helper::getOnlineArrahnConnection(); $startDate = $this->startDate; $endDate = $this->endDate; - // Query asas - cari gadaian yang ditebus hari sama - $query_total_gadaitebusmasa = DB::connection($cawangan_connection)->table('gadai') - ->join('online_arrahn.transaction as trans', function ($join) { - $join->on('gadai.norujukan', '=', 'trans.norujukan') - ->where('trans.trans_type', '=', 'T'); - }) - ->whereRaw('DATE(gadai.t_gadai) = DATE(trans.created_at)') - ->when($startDate && $endDate, function ($query) use ($startDate, $endDate) { - return $query->whereBetween('gadai.t_gadai', [$startDate, $endDate]); - }) - ->select( - 'gadai.t_gadai', - 'gadai.norujukan', - 'gadai.nokp', - 'gadai.semakbarcode', - 'gadai.masa', - 'gadai.berat', - 'gadai.nilaimarhun', - 'gadai.pinjaman', - 'gadai.upah12', - 'gadai.percentpinj', - 'gadai.teller', - 'gadai.bakipjm', - 'gadai.bakiupah', - DB::raw('TIME(trans.created_at) as masa_tebus'), - DB::raw('TIMEDIFF(TIME(trans.created_at), gadai.masa) as beza_minit') - ) - ->orderByRaw('beza_minit ASC'); + // 1. Ambil data gadai dari cawangan + $gadai_data = DB::connection($cawangan_connection)->table('gadai') + ->whereBetween('t_gadai', [$startDate, $endDate]) + ->get(); - // Dapatkan hasil penuh - $total_gadaitebusmasa = $query_total_gadaitebusmasa->get(); + if ($gadai_data->isEmpty()) { + return response()->json([ + 'query' => [ + 'startDate' => Helper::formatDate($startDate), + 'endDate' => Helper::formatDate($endDate), + ], + 'cawangan_detail' => $this->getCawanganDetail($cawangan_connection), + 'gadaian' => [], + 'summary_gadaian' => [], + ]); + } - // Data tambahan - $customer_info = $this->getCustomerInfo($total_gadaitebusmasa); - $marhun_gadaitebusmasa = $this->getMarhunGadaiTebusMasa($total_gadaitebusmasa, $cawangan_connection); + // 2. Ambil norujukan & tarikh dari gadai + $norujukans = $gadai_data->pluck('norujukan')->unique(); + $tarikh_set = $gadai_data->pluck('t_gadai')->map(fn($d) => date('Y-m-d', strtotime($d)))->unique(); + + // 3. Ambil transaksi tebus dari online server + $transaksi_tebus = DB::connection($online_connection)->table('transaction') + ->where('trans_type', 'T') + ->whereIn('norujukan', $norujukans) + ->whereIn(DB::raw('DATE(created_at)'), $tarikh_set) + ->get() + ->groupBy(fn($trans) => $trans->norujukan . '|' . date('Y-m-d', strtotime($trans->created_at))); + + // 4. Padankan + $matched = collect(); + + foreach ($gadai_data as $item) { + $tarikh = date('Y-m-d', strtotime($item->t_gadai)); + $key = $item->norujukan . '|' . $tarikh; + + if ($transaksi_tebus->has($key)) { + $trans = $transaksi_tebus[$key]->first(); // satu transaksi je + + $masa_gadai = $item->masa; + $masa_tebus = date('H:i:s', strtotime($trans->created_at)); + $beza_saat = abs(strtotime($masa_tebus) - strtotime($masa_gadai)); + $beza_masa = gmdate("H:i:s", $beza_saat); + + $matched->push((object)[ + 't_gadai' => $item->t_gadai, + 'norujukan' => $item->norujukan, + 'nokp' => $item->nokp, + 'semakbarcode' => $item->semakbarcode, + 'masa' => $masa_gadai, + 'masa_tebus' => $masa_tebus, + 'beza_minit' => $beza_masa, + 'berat' => $item->berat, + 'nilaimarhun' => $item->nilaimarhun, + 'pinjaman' => $item->pinjaman, + 'upah12' => $item->upah12, + 'percentpinj' => $item->percentpinj, + 'teller' => $item->teller, + 'bakipjm' => $item->bakipjm, + 'bakiupah' => $item->bakiupah, + ]); + } + } + + // 5. Data tambahan + $customer_info = $this->getCustomerInfo($matched); + $marhun_info = $this->getMarhunGadaiTebusMasa($matched, $cawangan_connection); $cawangan_detail = $this->getCawanganDetail($cawangan_connection); - $total_summary_gadaitebusmasa = $this->getTotalSummaryGadaiTebusMasa($query_total_gadaitebusmasa); + $summary = $this->getTotalSummaryGadaiTebusMasa($matched); - // Gabung data - $total_gadaitebusmasa = $total_gadaitebusmasa->zip($customer_info, $marhun_gadaitebusmasa)->map(function ($data) { + // 6. Gabung akhir + $report_data = $matched->zip($customer_info, $marhun_info)->map(function ($data) { [$gadai, $customer, $marhun] = $data; return [ @@ -247,7 +280,7 @@ class GadaianService 'alamat' => $customer['alamat1'] ?? '-', 'no_kp' => $gadai->nokp, 'masa' => $gadai->masa, - 'masa_tebus' => $gadai->masa_tebus ?? '-', + 'masa_tebus' => $gadai->masa_tebus, 'beza_minit' => $gadai->beza_minit, 'marhun' => $marhun, 'berat' => $gadai->berat, @@ -261,17 +294,20 @@ class GadaianService ]; }); - $data = [ + $report_data = $report_data->sortBy(function ($item) { + list($hours, $minutes, $seconds) = explode(':', $item['beza_minit']); + return ((int)$hours * 3600) + ((int)$minutes * 60) + (int)$seconds; + })->values(); + + return response()->json([ 'query' => [ 'startDate' => Helper::formatDate($startDate), 'endDate' => Helper::formatDate($endDate), ], 'cawangan_detail' => $cawangan_detail, - 'gadaian' => $total_gadaitebusmasa, - 'summary_gadaian' => $total_summary_gadaitebusmasa, - ]; - - return json_encode($data); + 'gadaian' => $report_data, + 'summary_gadaian' => $summary, + ]); } private function formatBezaMinit($minit) @@ -313,27 +349,15 @@ class GadaianService return $total_gadaitebusmasa; } - private function getTotalSummaryGadaiTebusMasa($query_total_gadaitebusmasa) + private function getTotalSummaryGadaiTebusMasa($collection) { - $summary = (clone $query_total_gadaitebusmasa) - ->selectRaw(' - COUNT(gadai.norujukan) as total_gadaitebusmasa, - SUM(gadai.berat) as total_berat, - SUM(gadai.nilaimarhun) as total_nilai_marhun, - SUM(gadai.pinjaman) as total_pembiayaan, - SUM(gadai.bakipjm) as total_bakipjm, - SUM(gadai.bakiupah) as total_keuntungan - ') - ->first(); - - // Convert stdClass to array, and fallback to 0 if null return [ - 'total_gadaitebusmasa' => $summary->total_gadaitebusmasa ?? 0, - 'total_berat' => $summary->total_berat ?? 0, - 'total_nilai_marhun' => $summary->total_nilai_marhun ?? 0, - 'total_pembiayaan' => $summary->total_pembiayaan ?? 0, - 'total_bakipjm' => $summary->total_bakipjm ?? 0, - 'total_keuntungan' => $summary->total_keuntungan ?? 0, + 'total_gadaitebusmasa' => $collection->count(), + 'total_berat' => $collection->sum('berat'), + 'total_nilai_marhun' => $collection->sum('nilaimarhun'), + 'total_pembiayaan' => $collection->sum('pinjaman'), + 'total_bakipjm' => $collection->sum('bakipjm'), + 'total_keuntungan' => $collection->sum('bakiupah'), ]; }