diff --git a/app/Console/Commands/AutoGenerateInvoice.php b/app/Console/Commands/AutoGenerateInvoice.php new file mode 100644 index 00000000..56ab74ff --- /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 started.'); + $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'); } /**