From e2d1426d1978818c2b330676d50b726db77cc0bd Mon Sep 17 00:00:00 2001 From: Omair Saleh Date: Tue, 28 Feb 2023 12:58:18 +0800 Subject: [PATCH] fix duplicated invoices --- routes/web.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/routes/web.php b/routes/web.php index 3f769d28..2a1f476a 100644 --- a/routes/web.php +++ b/routes/web.php @@ -871,13 +871,23 @@ dd($duplicatedInvoices); $unpaidInvoices = $packingList->transactions()->where('type', TransactionType::SHIPPING_INVOICE)->whereIn('status', [ApprovalStatus::PENDING_SUBMISSION, ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::APPROVED])->orderBy('id', 'desc')->get(); if (count($paidInvoice)) { - $packingList->transactions()->where('type', TransactionType::SHIPPING_INVOICE)->whereIn('status', [ApprovalStatus::PENDING_SUBMISSION, ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::APPROVED])->delete(); + $packingList->transactions()->where('type', TransactionType::SHIPPING_INVOICE)->where('status', '!=', ApprovalStatus::COMPLETED)->delete(); continue; } + // delete duplicated invoices $unpaidInvoicesIds = $unpaidInvoices->pluck('id')->toArray(); array_shift($unpaidInvoicesIds); + Transaction::whereIn('id', $unpaidInvoicesIds)->delete(); + + // delete expired invoices + $packingList->transactions()->where('type', TransactionType::SHIPPING_INVOICE)->where('status', '!=', ApprovalStatus::EXPIRED)->delete(); + + // delete suspended invoices + $suspendedInvoices = $packingList->transactions()->where('type', TransactionType::SHIPPING_INVOICE)->where('status', '!=', ApprovalStatus::SUSPENDED)->orderBy('id', 'desc')->get(); + $suspendedInvoicesIds = $suspendedInvoices->pluck('id')->toArray(); + array_shift($suspendedInvoicesIds); + Transaction::whereIn('id', $suspendedInvoicesIds)->delete(); - Transaction::whereIn('id', $unpaidInvoicesIds)->delete(); } });