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(); } });