diff --git a/app/Console/Commands/AutoGenerateInvoice.php b/app/Console/Commands/AutoGenerateInvoice.php new file mode 100644 index 00000000..df0ff65b --- /dev/null +++ b/app/Console/Commands/AutoGenerateInvoice.php @@ -0,0 +1,82 @@ +make(ListsPackingLists::class))->execute(['does_not_have_transaction_type' => 1, 'type' => 2]); + + if (count($packingLists)) { + $this->info(Carbon::now() . ' : Auto generate invoice cron starrted.'); + $start = new Carbon(); + + foreach ($packingLists as $packingList) { + $order = $packingList->owner; + if (!($order instanceof Order)) { + $this->info('Failed to generate invoice for reference' . $order->reference . '. It is not an instance of Order.'); + continue; + } + + $companyModule = $order->companyModule; + $billingAddress = $companyModule->addresses()->where('type', \App\Classes\ValueObjects\Constants\AddressType::BILLING)->first(); + $deliveryAddress = $order->addresses()->where('status', ApprovalStatus::APPROVED)->first(); + + $postCodes = \App\Models\SegmentConstant::whereIn('reference', ['CENTER_POSTCODE', 'OUTSTATION_POSTCODE'])->get()->pluck('value')->flatten(); + + if (!!$billingAddress && in_array($deliveryAddress->postcode, $postCodes->toArray())) { + try { + (App()->make(CreateInvoiceTransactionProcessor::class))->execute($packingList); + $this->info('Invoice generated for reference ' . $order->reference . '.'); + } catch (Exception $exception) { + $this->info('Failed to generate invoice for reference ' . $order->reference . '. Exception: ' . $exception->getMessage()); + } + } + $this->info('Failed to generate invoice for reference ' . $order->reference . '. Billing Address is not defined / Postcode area is not defined.'); + } + + $end = new Carbon(); + $elapsedTime = $start->diff($end)->format('%H:%I:%S'); + + $this->info(Carbon::now() . ' : Done generating invoice. ElapsedTime: ' . $elapsedTime . '.'); + } + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 8817b51d..2986eb25 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -43,7 +43,10 @@ class Kernel extends ConsoleKernel ->withoutOverlapping() ->appendOutputTo (storage_path().'/logs/departure_email.log'); - + $schedule->command('invoice:generate') + ->hourly() + ->withoutOverlapping() + ->appendOutputTo (storage_path().'/logs/auto_generate_invoice.log'); } /** diff --git a/routes/web.php b/routes/web.php index 3106e6c5..686931af 100644 --- a/routes/web.php +++ b/routes/web.php @@ -703,9 +703,9 @@ Route::get('/billplz/fix', function(){ }); -Route::get('/invoices/fix', function(){ +Route::get('/invoices/fix/{company_module_id}', function($company_module_id) { - $orders = Order::whereIn('company_module_id', [294, 2703])->get(); + $orders = Order::whereIn('company_module_id', json_decode($company_module_id))->get(); foreach ($orders as $order){ $invoices = $order->transactions()->where('transactions.type', TransactionType::SHIPPING_INVOICE)->get();