Make the command work for one/multiple accounts

This commit is contained in:
Holger Lösken 2016-07-10 11:16:40 +02:00
parent 5146fab45d
commit d00408b2e9
2 changed files with 21 additions and 6 deletions

View File

@ -15,7 +15,7 @@ class ResetInvoiceSchemaCounter extends Command
* @var string * @var string
*/ */
protected $signature = 'ninja:reset-invoice-schema-counter 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}'; {--force : Force setting the counter back to "1", regardless if the year changed}';
/** /**
@ -48,14 +48,28 @@ class ResetInvoiceSchemaCounter extends Command
*/ */
public function handle() 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(); $latestInvoice = $this->invoice->latest()->first();
$invoiceYear = Carbon::parse($latestInvoice->created_at)->year; $invoiceYear = Carbon::parse($latestInvoice->created_at)->year;
if(Carbon::now()->year > $invoiceYear || $this->option('force')) { if(Carbon::now()->year > $invoiceYear || $force) {
$account = Account::find($this->argument('account'))->first(); $accounts->transform(function ($a) {
$account->invoice_number_counter = 1; /** @var Account $a */
$account->update(); $a->invoice_number_counter = 1;
$this->info('The counter has been resetted successfully.'); $a->update();
});
$this->info('The counter has been resetted successfully for '.$accounts->count().' account(s).');
} }
} }
} }

View File

@ -54,6 +54,7 @@ class Kernel extends ConsoleKernel
->daily(); ->daily();
} }
// Reset the invoice schema counter at the turn of the year
$schedule $schedule
->command('ninja:reset-invoice-schema-counter') ->command('ninja:reset-invoice-schema-counter')
->daily(); ->daily();