diff --git a/app/Console/Commands/ResetInvoiceSchemaCounter.php b/app/Console/Commands/ResetInvoiceSchemaCounter.php new file mode 100644 index 000000000000..ed849a25a26f --- /dev/null +++ b/app/Console/Commands/ResetInvoiceSchemaCounter.php @@ -0,0 +1,75 @@ +invoice = $invoice; + } + + /** + * Execute the console command. + * + * @return mixed + */ + 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 || $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 179e423388f1..5dcc14da2f6f 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,10 @@ class Kernel extends ConsoleKernel ->sendOutputTo($logFile) ->daily(); } + + // Reset the invoice schema counter at the turn of the year + $schedule + ->command('ninja:reset-invoice-schema-counter') + ->daily(); } }