From d00408b2e974c80c78f3b8ecd424fb934e6fc934 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20Lo=CC=88sken?= Date: Sun, 10 Jul 2016 11:16:40 +0200 Subject: [PATCH] Make the command work for one/multiple accounts --- .../Commands/ResetInvoiceSchemaCounter.php | 26 ++++++++++++++----- app/Console/Kernel.php | 1 + 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/app/Console/Commands/ResetInvoiceSchemaCounter.php b/app/Console/Commands/ResetInvoiceSchemaCounter.php index 2f3a231d188e..ed849a25a26f 100644 --- a/app/Console/Commands/ResetInvoiceSchemaCounter.php +++ b/app/Console/Commands/ResetInvoiceSchemaCounter.php @@ -15,7 +15,7 @@ class ResetInvoiceSchemaCounter extends Command * @var string */ protected $signature = 'ninja:reset-invoice-schema-counter - {account : The ID of the account} + {account? : The ID of the account} {--force : Force setting the counter back to "1", regardless if the year changed}'; /** @@ -48,14 +48,28 @@ class ResetInvoiceSchemaCounter extends Command */ public function handle() { + $force = $this->option('force'); + $account = $this->argument('account'); + + $accounts = null; + + if ($account) { + $accounts = Account::find($account)->get(); + } else { + $accounts = Account::all(); + } + $latestInvoice = $this->invoice->latest()->first(); $invoiceYear = Carbon::parse($latestInvoice->created_at)->year; - if(Carbon::now()->year > $invoiceYear || $this->option('force')) { - $account = Account::find($this->argument('account'))->first(); - $account->invoice_number_counter = 1; - $account->update(); - $this->info('The counter has been resetted successfully.'); + if(Carbon::now()->year > $invoiceYear || $force) { + $accounts->transform(function ($a) { + /** @var Account $a */ + $a->invoice_number_counter = 1; + $a->update(); + }); + + $this->info('The counter has been resetted successfully for '.$accounts->count().' account(s).'); } } } diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index fe91080b8e41..5dcc14da2f6f 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -54,6 +54,7 @@ class Kernel extends ConsoleKernel ->daily(); } + // Reset the invoice schema counter at the turn of the year $schedule ->command('ninja:reset-invoice-schema-counter') ->daily();