delete duplicate invoice logic

This commit is contained in:
edmondlang
2023-03-09 10:21:40 +08:00
parent 9936e42574
commit 3d15a8e312
+12 -2
View File
@@ -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();