1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-09 20:52:56 +01:00

Merge pull request #962 from codedge/#585-Reset-invoice-schema-counter-new-year

Reset the invoice schema counter
This commit is contained in:
Hillel Coren 2016-07-20 11:10:19 +03:00 committed by GitHub
commit f83736478c
2 changed files with 81 additions and 0 deletions

View File

@ -0,0 +1,75 @@
<?php
namespace App\Console\Commands;
use App\Models\Account;
use App\Models\Invoice;
use Carbon\Carbon;
use Illuminate\Console\Command;
class ResetInvoiceSchemaCounter extends Command
{
/**
* The name and signature of the console 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}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Reset the invoice schema counter at the turn of the year.';
/**
* @var Invoice
*/
protected $invoice;
/**
* Create a new command instance.
*
* @param Invoice $invoice
*/
public function __construct(Invoice $invoice)
{
parent::__construct();
$this->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).');
}
}
}

View File

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