From 37e1e325c2428c0b27de1e8a20eaec9e8b4783ee Mon Sep 17 00:00:00 2001 From: edmondlang Date: Thu, 7 Sep 2023 00:16:19 +0800 Subject: [PATCH] test fix invoice --- app/Console/Commands/RegenerateInvoice.php | 43 +++++++++------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/app/Console/Commands/RegenerateInvoice.php b/app/Console/Commands/RegenerateInvoice.php index 827299a6..1fa1e706 100644 --- a/app/Console/Commands/RegenerateInvoice.php +++ b/app/Console/Commands/RegenerateInvoice.php @@ -46,31 +46,28 @@ class RegenerateInvoice extends Command */ public function handle() { - // Allocate sufficient memory as needed - // ini_set('memory_limit', '256M'); - + set_time_limit(14400); $processed_invoice = 1; Booking::where('status', ApprovalStatus::COMPLETED) - ->whereDate('updated_at', '>=', Carbon::parse('2023-01-01')) + ->whereDate('updated_at', '>=', Carbon::parse('01-01-2023')) ->orderBy('id') - ->chunk(100, function ($bookings) use (&$processed_invoice) { + ->chunk(100, function ($bookings) use ($processed_invoice) { foreach ($bookings as $booking) { - $logMessage = 'Counter ' . $processed_invoice; - $this->printAndLog($logMessage); - $processed_invoice++; - if ( - !$booking->transactions()->where('transactions.type', TransactionType::INVOICE)->exists() - || $booking->transactions()->where('transactions.type', TransactionType::INVOICE)->first()->created_at->greaterThanOrEqualTo(Carbon::parse('2023-09-06')) + Log::channel('regenerateInvoice')->info('Counter ' . $processed_invoice); + $processed_invoice += 1; + + if ($booking->transactions()->where('transactions.type', TransactionType::INVOICE) + ->first() + ->created_at + ->greaterThanOrEqualTo(Carbon::parse('2023-09-06')) ) { continue; } - // Delete transactions and documents in one query $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(); @@ -82,6 +79,8 @@ class RegenerateInvoice extends Command 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); } @@ -92,26 +91,20 @@ class RegenerateInvoice extends Command $existing_invoice_bill_no = Transaction::where('bill_no', $bill_no)->first(); if ($existing_invoice_bill_no) { - $this->printAndLog('Delete existing bill_no'); - $this->printAndLog(json_encode($existing_invoice_bill_no)); + Log::channel('regenerateInvoice')->info('Delete existing bill_no'); + Log::channel('regenerateInvoice')->info(json_encode($existing_invoice_bill_no)); $existing_invoice_bill_no->forceDelete(); } (App()->make(CreateInvoiceTransactionProcessorWithInvoiceNo::class))->execute($booking, $bill_no); - $logMessage = 'Regenerated invoice. Booking Marking - ' . $booking->marking . '. Bill_no - ' . $bill_no . '. Old bill_no - ' . $deletedInvoice->bill_no; - $this->printAndLog($logMessage); + dump('regenerated invoice. Booking Marking - ' . $booking->marking . '. Bill_no - ' . $bill_no . '. Old bill_no - ' . $deletedInvoice->bill_no); + Log::channel('regenerateInvoice')->info('regenerated invoice. Booking Marking - ' . $booking->marking . '. Bill_no - ' . $bill_no . '. Old bill_no - ' . $deletedInvoice->bill_no); } else { (App()->make(CreateInvoiceTransactionProcessor::class))->execute($booking); - $logMessage = 'Regenerated new invoice. Booking Marking - ' . $booking->marking; - $this->printAndLog($logMessage); + dump('regenerated new invoice. Booking Marking - ' . $booking->marking); + Log::channel('regenerateInvoice')->info('regenerated new invoice. Booking Marking - ' . $booking->marking); } } }); } - - public function printAndLog($string) - { - // $this->info($string); - Log::channel('regenerateInvoice')->info($string); - } }