check duplicated invoice

This commit is contained in:
edmondlang
2023-02-27 15:56:29 +08:00
parent a810fcad59
commit e227abac59
+59 -2
View File
@@ -778,8 +778,10 @@ Route::get('/invoices/approve', function(Request $request){
$packingList = $invoice->owner;
$order = $packingList->owner;
if(in_array($order->reference, json_decode($request->input('exclude')))){
continue;
if ($request->input('exclude')){
if(in_array($order->reference, json_decode($request->input('exclude')))){
continue;
}
}
try {
@@ -847,3 +849,58 @@ Route::get('/invoices/show-duplicated', function(Request $request){
});
Route::get('/invoices/delete-duplicated', function(Request $request){
$duplicatedInvoiceId = DB::table('transactions')
->where('type', TransactionType::SHIPPING_INVOICE)
->where('status', '!=' , ApprovalStatus::COMPLETED)
->where('deleted_at', null)
->select('owner_id', DB::raw('count(*) as count'))
->groupBy('owner_id')
->having('count', '>', 1)
->get()->pluck('owner_id');
// delete invoice type suspended
$duplicatedInvoice = Transaction::whereIn('owner_id', $duplicatedInvoiceId)->get();
foreach($duplicatedInvoice as $invoice) {
$packingList = $invoice->owner;
$order = $packingList->owner;
$order_marking = $order->reference;
$duplicatedShipingInvoice = $packingList->transactions->where('type', TransactionType::SHIPPING_INVOICE);
if (sizeof($duplicatedShipingInvoice)) {
$paidShippingInvoice = $duplicatedShipingInvoice->where('status', ApprovalStatus::COMPLETED);
if (sizeof($paidShippingInvoice)) {
// if packing list have any paid invoices, delete all the unpaid
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->orderByDesc('created_at')->first()->id ) {
if ($invoice->id != $duplicatedShipingInvoice->sortByDesc('created_at')->first()->id ) {
// delete this invoice
$invoice->delete();
dump('Deleted invoice id -' . $invoice->id);
}
}
}
}
});
Route::get('/invoices/suspend-invoices', function(Request $request){
// $pendingPayment = Transaction::where('type', TransactionType::SHIPPING_INVOICE)->where('status', ApprovalStatus::APPROVED)->get();
$pendingPayment = Transaction::where('type', TransactionType::SHIPPING_INVOICE)->where('status', 5)->get();
foreach ($pendingPayment as $invoice) {
$invoice->status = ApprovalStatus::EXPIRED;
$invoice->save();
}
});