diff --git a/routes/web.php b/routes/web.php index f03d368c..3f5c86df 100644 --- a/routes/web.php +++ b/routes/web.php @@ -860,14 +860,24 @@ Route::get('/invoices/delete-duplicated', function(Request $request){ $paidInvoice = $packingList->transactions()->where('type', TransactionType::SHIPPING_INVOICE)->where('status', ApprovalStatus::COMPLETED)->get(); - $unpaidInvoices = $packingList->transactions()->where('type', TransactionType::SHIPPING_INVOICE)->whereIn('status', [ApprovalStatus::PENDING_SUBMISSION, ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::APPROVED])->orderBy('id', 'desc')->get(); - + // if have completed invoice (verified payment) if (count($paidInvoice)) { $packingList->transactions()->where('type', TransactionType::SHIPPING_INVOICE)->where('status', '!=', ApprovalStatus::COMPLETED)->delete(); continue; } + // if have unverified payment + $paymentHistory = $packingList->transactions()->whereHas('transactions', function($query){ + return $query->where('type', TransactionType::PAYMENT)->whereIn('status', [ApprovalStatus::PENDING_SUBMISSION ,ApprovalStatus::APPROVED, ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::COMPLETED, ApprovalStatus::REJECTED]); + })->get(); + if (count($paymentHistory)) { + $packingList->transactions()->where('type', TransactionType::SHIPPING_INVOICE)->where('id', '!=', $paymentHistory->first()->id)->delete(); + continue; + } + // delete duplicated invoices + $unpaidInvoices = $packingList->transactions()->where('type', TransactionType::SHIPPING_INVOICE)->whereIn('status', [ApprovalStatus::PENDING_SUBMISSION, ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::APPROVED])->orderBy('id', 'desc')->get(); + $unpaidInvoicesIds = $unpaidInvoices->pluck('id')->toArray(); array_shift($unpaidInvoicesIds); Transaction::whereIn('id', $unpaidInvoicesIds)->delete();