From 97183ae52c128814aeb0d2ae126609217245fcee Mon Sep 17 00:00:00 2001 From: Dillon Ngo Date: Wed, 8 Oct 2025 15:11:22 +0800 Subject: [PATCH] E-Invoice - Fix Sales Invoice Report (with refund) inaccurate with missing records --- .../ExportsSalesInvoiceWithRefundReport.php | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/app/Classes/Modules/Exports/Services/ExportsSalesInvoiceWithRefundReport.php b/app/Classes/Modules/Exports/Services/ExportsSalesInvoiceWithRefundReport.php index ad650adc..513e94e5 100644 --- a/app/Classes/Modules/Exports/Services/ExportsSalesInvoiceWithRefundReport.php +++ b/app/Classes/Modules/Exports/Services/ExportsSalesInvoiceWithRefundReport.php @@ -59,12 +59,27 @@ class ExportsSalesInvoiceWithRefundReport implements FromQuery, WithHeadings, Wi Log::info('ExportsSalesInvoiceWithRefundReport startDate: ' . $startDate->format('Y-m-d H:i:s')); Log::info('ExportsSalesInvoiceWithRefundReport endDate: ' . $endDate->format('Y-m-d H:i:s')); - return Booking::whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED, ApprovalStatus::SUSPENDED]) - ->whereHas('transactions', function ($query) use ($startDate, $endDate) { - $query->payments() - ->where('status', ApprovalStatus::REFUNDED) - ->whereBetween('created_at', [$startDate, $endDate]) - ->latest('created_at'); + // return Booking::whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED, ApprovalStatus::SUSPENDED]) + // ->whereHas('transactions', function ($query) use ($startDate, $endDate) { + // $query->payments() + // ->where('status', ApprovalStatus::REFUNDED) + // ->whereBetween('created_at', [$startDate, $endDate]) + // ->latest('created_at'); + // }); + + return Booking::whereIn('status', [ + ApprovalStatus::APPROVED, + ApprovalStatus::COMPLETED, + ApprovalStatus::SUSPENDED, + ]) + ->whereHas('transactions', function ($transactionQuery) use ($startDate, $endDate) { + $transactionQuery->payments() + ->whereBetween('created_at', [$startDate, $endDate]) + ->latest('created_at') + ->whereHas('transactions', function ($refundQuery) { + $refundQuery->refunds() + ->whereIn('status', [ApprovalStatus::APPROVED]); + }); }); } @@ -83,7 +98,8 @@ class ExportsSalesInvoiceWithRefundReport implements FromQuery, WithHeadings, Wi $company = $booking->company()->first(); // $lastPaymentTransaction = $booking->transactions()->payments()->complete()->latest()->first(); - $lastPaymentTransaction = $booking->transactions()->payments()->where('status', ApprovalStatus::REFUNDED)->latest()->first(); + // $lastPaymentTransaction = $booking->transactions()->payments()->where('status', ApprovalStatus::REFUNDED)->latest()->first(); + $lastPaymentTransaction = $booking->transactions()->payments()->whereIn('status', [ApprovalStatus::COMPLETED, ApprovalStatus::REFUNDED])->latest()->first(); if(!$lastPaymentTransaction){ return $records; }