fix: 已付清审批按付款合计排除

This commit is contained in:
yueyang 2026-11-13 13:23:33 +08:00
parent 6fc87fbf24
commit f71fa8f669
2 changed files with 60 additions and 0 deletions

View File

@ -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'] ?? '');

View File

@ -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);