diff --git a/routes/web.php b/routes/web.php index 7325c90b..0efe0616 100644 --- a/routes/web.php +++ b/routes/web.php @@ -766,3 +766,71 @@ Route::get('/customer/vouchers/{marking}', function ($marking) { })->name('customer.reward'); Route::get('transaction/{id}/credit_note/download', 'Transactions\GenerateCreditNotePdfController@download')->name('transaction.credit_note.download'); + +Route::get('/invoice/{marking}/{started_at}/{ended_at}/fix', function($marking, $started_at, $ended_at) { + + set_time_limit(14400); + $processed_invoice = 1; + + if (is_null($marking) || empty($marking)) { + return 'Error - Marking is empty'; + } + + if (is_null($started_at) || empty($started_at)) { + return 'Error - Start Date is empty'; + } + + if (is_null($ended_at) || empty($ended_at)) { + return 'Error - End Date is empty'; + } + + $company = Company::where('reference', $marking)->first(); + if (!$company) { + return 'Error - Marking not found'; + } + dump('Company Id - ' . $company->id); + + // dd($marking, $started_at, $ended_at); + + $bookings = $company->bookings() + ->where('status', ApprovalStatus::COMPLETED) + ->whereDate('updated_at', '>=', Carbon::parse($started_at)) + ->whereDate('updated_at', '<=', Carbon::parse($ended_at)) + ->orderBy('id') + ->chunk(100, function ($bookings) use (&$processed_invoice) { + foreach ($bookings as $booking) { + + Log::channel('regenerateInvoice')->info('Counter ' . $processed_invoice); + dump('Counter ' . $processed_invoice); + $processed_invoice += 1; + + $booking->status = ApprovalStatus::APPROVED; + $booking->save(); + + $firstInvoice = $booking->transactions() + ->whereIn('type', [TransactionType::INVOICE]) + ->withTrashed() + ->orderBy('created_at', 'asc') + ->first(); + + // get the first bill_no + $firstBillNo = $firstInvoice->bill_no; + if (strpos($firstBillNo, '-deleted') !== false) { + $firstBillNo = substr($firstBillNo, 0, strpos($firstBillNo, '-deleted')); + } + + // update currentInvoice bill_no to '-deleted-' + $currentInvoice = $booking->transactions()->where('type', TransactionType::INVOICE)->first(); + $currentInvoice->bill_no = $currentInvoice->bill_no ."-deleted-" . (string)(Carbon::now()->timestamp); + $currentInvoice->save(); + + $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(); + + (App()->make(CreateInvoiceTransactionWithInvoiceNoProcessor::class))->execute($booking, $firstBillNo); + dump('regenerated invoice. Booking Marking - ' . $booking->marking . '. Bill_no - ' . $firstBillNo . '. Old bill_no - ' . $currentInvoice->bill_no); + Log::channel('regenerateInvoice')->info('regenerated invoice. Booking Marking - ' . $booking->marking . '. Bill_no - ' . $firstBillNo . '. Old bill_no - ' . $currentInvoice->bill_no); + } + } + ); +})->name('invoice.fix.byCustomerMarking'); \ No newline at end of file