diff --git a/config/logging.php b/config/logging.php index 088c204e..fb872693 100644 --- a/config/logging.php +++ b/config/logging.php @@ -99,6 +99,11 @@ return [ 'emergency' => [ 'path' => storage_path('logs/laravel.log'), ], + 'regenerateInvoice' => [ + 'driver' => 'single', + 'path' => storage_path('logs/regenerateInvoice.log'), + 'level' => 'info', + ], ], ]; diff --git a/routes/web.php b/routes/web.php index b1188854..0c2cc915 100644 --- a/routes/web.php +++ b/routes/web.php @@ -28,6 +28,7 @@ use App\Classes\Modules\Bookings\Processors\CreatePurchaseOrderFor1688OrderProce use App\Classes\Modules\Documents\Services\DeletesDocument; use App\Classes\Modules\Transactions\Processors\CreateInvoiceTransactionProcessorWithInvoiceNo; use App\Classes\Modules\Transactions\Services\DeletesTransaction; +use Illuminate\Support\Facades\Log; /* |-------------------------------------------------------------------------- @@ -488,41 +489,48 @@ Route::get('/payments/manual', function(){ Route::get('/invoice/fix', function(){ set_time_limit(14400); - $bookings = Booking::where('status', ApprovalStatus::COMPLETED) + $processed_invoice = 1; + + Booking::where('status', ApprovalStatus::COMPLETED) ->whereDate('updated_at', '>=', Carbon::parse('01-01-2023')) - ->take(10) // to test on development - ->get(); + ->orderBy('id') + ->chunk(100, function ($bookings) use ($processed_invoice){ + foreach ($bookings as $booking) { - foreach($bookings as $booking){ - $booking->transactions()->whereIn('transactions.type', [TransactionType::INVOICE, TransactionType::SUPPLIER_DELIVER])->delete(); - $booking->documents()->whereIn('document_type', [DocumentType::INVOICE, DocumentType::PURCHASE_ORDER, DocumentType::DELIVER_ORDER, DocumentType::SUPPLIER_DELIVER_ORDER])->delete(); - $booking->status = ApprovalStatus::APPROVED; - $booking->save(); + Log::channel('regenerateInvoice')->info('Counter ' . $processed_invoice); + $processed_invoice += 1; - $deletedInvoice = $booking->transactions() - ->whereIn('type', [TransactionType::INVOICE]) - ->onlyTrashed() - ->orderBy('created_at', 'asc') - ->first(); + $booking->transactions()->whereIn('transactions.type', [TransactionType::INVOICE, TransactionType::SUPPLIER_DELIVER])->delete(); + $booking->documents()->whereIn('document_type', [DocumentType::INVOICE, DocumentType::PURCHASE_ORDER, DocumentType::DELIVER_ORDER, DocumentType::SUPPLIER_DELIVER_ORDER])->delete(); + $booking->status = ApprovalStatus::APPROVED; + $booking->save(); - if ($deletedInvoice) { - $bill_no = $deletedInvoice->bill_no; + $deletedInvoice = $booking->transactions() + ->whereIn('type', [TransactionType::INVOICE]) + ->onlyTrashed() + ->orderBy('created_at', 'asc') + ->first(); - // check if bill_no ends with '-deleted' - if (str_ends_with($bill_no, '-deleted')) { - $bill_no = str_replace('-deleted', '', $bill_no); + if ($deletedInvoice) { + $bill_no = $deletedInvoice->bill_no; + + // check if bill_no ends with '-deleted' + if (str_ends_with($bill_no, '-deleted')) { + $bill_no = str_replace('-deleted', '', $bill_no); + } + + $deletedInvoice->bill_no = $bill_no . '-deleted'; + $deletedInvoice->save(); + + (App()->make(CreateInvoiceTransactionProcessorWithInvoiceNo::class))->execute($booking, $bill_no); + dump('regenerated invoice. Booking Marking - ' . $booking->marking . '. Bill_no - ' . $bill_no . '. Old bill_no - ' . $deletedInvoice->bill_no); + } else { + (App()->make(CreateInvoiceTransactionProcessor::class))->execute($booking); + dump('regenerated new invoice. Booking Marking - ' . $booking->marking); + } } - - $deletedInvoice->bill_no = $bill_no . '-deleted'; - (App()->make(CreateInvoiceTransactionProcessorWithInvoiceNo::class))->execute($booking, $bill_no); - $deletedInvoice->save(); - dump('regenerated invoice. Booking Marking - ' . $booking->marking . '. Bill_no - ' . $bill_no . '. Old bill_no - ' . $deletedInvoice->bill_no); - } else { - (App()->make(CreateInvoiceTransactionProcessor::class))->execute($booking); - dump('regenerated new invoice. Booking Marking - ' . $booking->marking); } - - } + ); })->name('invoice.fix'); Route::get('/1688/fix/{reference}', function($reference){