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
*/
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).');
}
}
}

View File

@ -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();