fix duplicated invoices

This commit is contained in:
Omair Saleh
2023-02-28 12:03:17 +08:00
parent cbda7aa6ba
commit d61a0c2104
3 changed files with 21 additions and 89 deletions
+13 -28
View File
@@ -807,7 +807,7 @@ Route::get('/invoices/show-duplicated', function(Request $request){
->having('count', '>', 1)
->get();
// delete invoice type suspended
// delete invoice type suspended
echo '<table>';
echo "<tr>";
@@ -835,7 +835,7 @@ Route::get('/invoices/show-duplicated', function(Request $request){
echo "<td style='border:1px solid'>Invoice Owner ID</td>";
echo "<td style='border:1px solid'>Invoice Owner ID</td>";
echo "</tr>";
foreach($duplicatedInvoice as $invoice) {
$order = $invoice->owner->owner;
echo "<tr>";
@@ -851,48 +851,33 @@ Route::get('/invoices/show-duplicated', function(Request $request){
Route::get('/invoices/delete-duplicated', function(Request $request){
$duplicatedInvoiceId = DB::table('transactions')
$duplicatedInvoices = 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');
->get();
// delete invoice type suspended
// delete invoice type suspended
$duplicatedInvoice = Transaction::whereIn('owner_id', $duplicatedInvoiceId)->get();
foreach($duplicatedInvoice as $invoice) {
$packingList = $invoice->owner;
$duplicatedInvoices = Transaction::whereIn('owner_id', $duplicatedInvoices->pluck('owner_id'))->get()->groupBy('owner_id');
foreach($duplicatedInvoices as $invoice) {
$packingList = $invoice->first()->owner;
$order = $packingList->owner;
$order_marking = $order->reference;
$paidInvoice = $packingList->transactions()->where('type', TransactionType::SHIPPING_INVOICE)->where('status', ApprovalStatus::COMPLETED)->get();
// 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])->get()->sortByDesc('created_at');
$unpaidInvoices = $packingList->transactions()->where('type', TransactionType::SHIPPING_INVOICE)->whereIn('status', [ApprovalStatus::PENDING_SUBMISSION, ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::APPROVED])->orderBy('id', 'desc')->get();
print_r('unpaidInvoices');
dump($unpaidInvoices);
if (count($paidInvoice)) {
// delete all other invoice
print_r('unpaidInvoices - has count paidInvoice');
dump($unpaidInvoices);
// $unpaidInvoices->delete();
$packingList->transactions()->where('type', TransactionType::SHIPPING_INVOICE)->whereIn('status', [ApprovalStatus::PENDING_SUBMISSION, ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::APPROVED])->delete();
continue;
}
// does not have paid invoice, then delete all but the latest
$unpaidInvoicesIds = $unpaidInvoices->pluck('id')->toArray();
$latestInvoiceId = array_shift($unpaidInvoicesIds);
array_shift($unpaidInvoicesIds);
print_r('unpaidInvoicesIds');
dump($unpaidInvoicesIds);
// Transaction::whereIn('id', $unpaidInvoicesIds)->delete();
// dump(Transaction::whereIn('id', $unpaidInvoicesIds)->get());
Transaction::whereIn('id', $unpaidInvoicesIds)->delete();
}
});
});