delete duplicated invoice

This commit is contained in:
edmondlang
2023-02-27 19:15:36 +08:00
parent 5d79d5808a
commit d03e34a1f4
+18 -21
View File
@@ -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());
}
});