diff --git a/routes/web.php b/routes/web.php index a2e35cf9..523b1d56 100644 --- a/routes/web.php +++ b/routes/web.php @@ -869,30 +869,27 @@ Route::get('/invoices/delete-duplicated', function(Request $request){ $order = $packingList->owner; $order_marking = $order->reference; - // delete for order_marking 165005587 only - if ($order_marking == 165005587) { - $duplicatedShipingInvoice = $packingList->transactions->where('type', TransactionType::SHIPPING_INVOICE); + $paidInvoice = $packingList->transactions->where('type', TransactionType::SHIPPING_INVOICE)->where('status', ApprovalStatus::COMPLETED)->get(); - if (sizeof($duplicatedShipingInvoice)) { - $paidShippingInvoice = $duplicatedShipingInvoice->where('status', ApprovalStatus::COMPLETED); + // check inside packingList has how many unpaid invoice + $unpaidInvoices = $packingList->transactions->where('type', TransactionType::SHIPPING_INVOICE)->whereIn('status', [ApprovalStatus::PENDING_SUBMISSION, ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::APPROVED])->sortByDesc('created_at')->get(); - if (sizeof($paidShippingInvoice)) { - // if packing list have any paid invoices, delete all the unpaid + dump($unpaidInvoices); - if ($invoice->id != $paidShippingInvoice->id ) { - // delete this invoice - $invoice->delete(); - dump('Deleted invoice id -' . $invoice->id); - } - } else { - // if all unpaid, leave the latest one, delete all - if ($invoice->id != $duplicatedShipingInvoice->sortByDesc('created_at')->first()->id ) { - // delete this invoice - $invoice->delete(); - dump('Deleted invoice id -' . $invoice->id); - } - } - } + if (count($paidInvoice)) { + // delete all other invoice + dump($unpaidInvoices); + // $unpaidInvoices->delete(); + continue; } + + // does not have paid invoice, then delete all but the latest + $unpaidInvoicesIds = $unpaidInvoices->pluck('id'); + $latestInvoiceId = array_shift($unpaidInvoicesIds); + + dump($unpaidInvoicesIds); + + // Transaction::whereIn('id', $unpaidInvoicesIds)->delete(); + // dump(Transaction::whereIn('id', $unpaidInvoicesIds)->get()); } }); \ No newline at end of file