diff --git a/modules/EcommerceFinance/Repositories/DingProcessDetailMirrorRepository.php b/modules/EcommerceFinance/Repositories/DingProcessDetailMirrorRepository.php index 32357246..6627d0be 100644 --- a/modules/EcommerceFinance/Repositories/DingProcessDetailMirrorRepository.php +++ b/modules/EcommerceFinance/Repositories/DingProcessDetailMirrorRepository.php @@ -525,6 +525,61 @@ class DingProcessDetailMirrorRepository ) ->where('paid_approval.cashier_status', 'completed'); }); + if ($this->structuredPaymentsEnabled()) { + $query->whereNotExists(function (Builder $fullyPaidApproval): void { + $fullyPaidApproval + ->select('payment_fully_paid.business_id') + ->from('ding_process_payment_mirror as payment_fully_paid') + ->whereColumn( + 'payment_fully_paid.business_id', + 'ding_process_detail_mirror.business_id', + ) + ->whereColumn( + 'payment_fully_paid.source_scope', + 'ding_process_detail_mirror.source_scope', + ) + ->groupBy( + 'payment_fully_paid.business_id', + 'payment_fully_paid.source_scope', + ) + ->havingRaw( + 'SUM(COALESCE(payment_fully_paid.amount, 0)) > 0', + ) + ->havingRaw( + 'SUM(COALESCE(payment_fully_paid.amount, 0)) + 0.01 >= (' + . 'SELECT SUM(COALESCE(detail_fully_paid.amount, 0)) ' + . 'FROM ding_process_detail_mirror AS detail_fully_paid ' + . 'WHERE detail_fully_paid.business_id = ding_process_detail_mirror.business_id ' + . 'AND detail_fully_paid.source_scope = ding_process_detail_mirror.source_scope' + . ')', + ); + }); + } else { + $query->whereNotExists(function (Builder $fullyPaidApproval): void { + $fullyPaidApproval + ->select('fully_paid_approval.business_id') + ->from('ding_process_detail_mirror as fully_paid_approval') + ->whereColumn( + 'fully_paid_approval.business_id', + 'ding_process_detail_mirror.business_id', + ) + ->whereColumn( + 'fully_paid_approval.source_scope', + 'ding_process_detail_mirror.source_scope', + ) + ->groupBy( + 'fully_paid_approval.business_id', + 'fully_paid_approval.source_scope', + ) + ->havingRaw( + 'SUM(COALESCE(fully_paid_approval.bank_amount, 0)) > 0', + ) + ->havingRaw( + 'SUM(COALESCE(fully_paid_approval.bank_amount, 0)) + 0.01 ' + . '>= SUM(COALESCE(fully_paid_approval.amount, 0))', + ); + }); + } } $paidStart = $this->filterDate($filters['paid_start'] ?? ''); diff --git a/tests/Unit/EcommerceFinance/DingProcessDetailMirrorRepositoryTest.php b/tests/Unit/EcommerceFinance/DingProcessDetailMirrorRepositoryTest.php index 4cb1d651..0f5e90c7 100644 --- a/tests/Unit/EcommerceFinance/DingProcessDetailMirrorRepositoryTest.php +++ b/tests/Unit/EcommerceFinance/DingProcessDetailMirrorRepositoryTest.php @@ -128,6 +128,11 @@ class DingProcessDetailMirrorRepositoryTest extends TestCase public function test_paid_approvals_are_excluded_unless_explicitly_included(): void { + DB::connection('business_mirror') + ->table('ding_process_detail_mirror') + ->where('business_id', '202601011200000000001') + ->update(['cashier_status' => 'pending']); + $pending = $this->repository->paginate([ 'include_paid' => '', ], 1, 50);