From 790766c0289867e1ee85c55be5fd6822c7578dd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20Lo=CC=88sken?= Date: Sat, 9 Jul 2016 23:38:33 +0200 Subject: [PATCH 1/3] Reset the invoice schema counter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Fix #585 and resolve #585 - Reset the invoice schema counter back to „1“ at the turn of the year - Implemented as command the also call it manually --- .../Commands/ResetInvoiceSchemaCounter.php | 65 +++++++++++++++++++ app/Console/Kernel.php | 5 ++ 2 files changed, 70 insertions(+) create mode 100644 app/Console/Commands/ResetInvoiceSchemaCounter.php diff --git a/app/Console/Commands/ResetInvoiceSchemaCounter.php b/app/Console/Commands/ResetInvoiceSchemaCounter.php new file mode 100644 index 0000000000..e5e0bf2179 --- /dev/null +++ b/app/Console/Commands/ResetInvoiceSchemaCounter.php @@ -0,0 +1,65 @@ +account = $account; + $this->invoice = $invoice; + } + + /** + * Execute the console command. + * + * @return mixed + */ + public function handle() + { + $latestInvoice = $this->invoice->latest()->first(); + $invoiceYear = Carbon::parse($latestInvoice->created_at)->year; + + if(Carbon::now()->year > $invoiceYear || $this->option('force')) { + $this->account->invoice_number_counter = 1; + $this->account->update(); + } + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 179e423388..fe91080b8e 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -15,6 +15,7 @@ class Kernel extends ConsoleKernel 'App\Console\Commands\SendRecurringInvoices', 'App\Console\Commands\RemoveOrphanedDocuments', 'App\Console\Commands\ResetData', + 'App\Console\Commands\ResetInvoiceSchemaCounter', 'App\Console\Commands\CheckData', 'App\Console\Commands\PruneData', 'App\Console\Commands\CreateTestData', @@ -52,5 +53,9 @@ class Kernel extends ConsoleKernel ->sendOutputTo($logFile) ->daily(); } + + $schedule + ->command('ninja:reset-invoice-schema-counter') + ->daily(); } } From 5146fab45d12f1bf3121809e3336ffea717e174d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20Lo=CC=88sken?= Date: Sat, 9 Jul 2016 23:54:59 +0200 Subject: [PATCH 2/3] - Added account selection by passing a parameter to the command - Added an success message --- .../Commands/ResetInvoiceSchemaCounter.php | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/app/Console/Commands/ResetInvoiceSchemaCounter.php b/app/Console/Commands/ResetInvoiceSchemaCounter.php index e5e0bf2179..2f3a231d18 100644 --- a/app/Console/Commands/ResetInvoiceSchemaCounter.php +++ b/app/Console/Commands/ResetInvoiceSchemaCounter.php @@ -15,6 +15,7 @@ class ResetInvoiceSchemaCounter extends Command * @var string */ protected $signature = 'ninja:reset-invoice-schema-counter + {account : The ID of the account} {--force : Force setting the counter back to "1", regardless if the year changed}'; /** @@ -24,11 +25,6 @@ class ResetInvoiceSchemaCounter extends Command */ protected $description = 'Reset the invoice schema counter at the turn of the year.'; - /** - * @var Account - */ - protected $account; - /** * @var Invoice */ @@ -37,13 +33,11 @@ class ResetInvoiceSchemaCounter extends Command /** * Create a new command instance. * - * @param Account $account * @param Invoice $invoice */ - public function __construct(Account $account, Invoice $invoice) + public function __construct(Invoice $invoice) { parent::__construct(); - $this->account = $account; $this->invoice = $invoice; } @@ -58,8 +52,10 @@ class ResetInvoiceSchemaCounter extends Command $invoiceYear = Carbon::parse($latestInvoice->created_at)->year; if(Carbon::now()->year > $invoiceYear || $this->option('force')) { - $this->account->invoice_number_counter = 1; - $this->account->update(); + $account = Account::find($this->argument('account'))->first(); + $account->invoice_number_counter = 1; + $account->update(); + $this->info('The counter has been resetted successfully.'); } } } 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 3/3] 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 2f3a231d18..ed849a25a2 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 fe91080b8e..5dcc14da2f 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();