2020-04-01 12:35:06 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
use App\Models\Account;
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
|
|
|
|
class InvoiceGenerate extends Command
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $signature = 'invoice:generate {account?} {--p|preview : Preview}';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description = 'Generate Invoices to be Sent';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new command instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
if ($this->argument('account'))
|
|
|
|
$accounts = collect()->push(Account::find($this->argument('account')));
|
|
|
|
else
|
|
|
|
$accounts = Account::active()->get();
|
|
|
|
|
|
|
|
foreach ($accounts as $o) {
|
|
|
|
$io = new Invoice;
|
2020-05-29 04:10:10 +00:00
|
|
|
$io->account_id = $o->id;
|
2020-04-01 12:35:06 +00:00
|
|
|
|
|
|
|
foreach ($o->services(TRUE)->get() as $so) {
|
|
|
|
foreach ($so->next_invoice_items(FALSE) as $ooo)
|
|
|
|
$io->items->push($ooo);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are no items, no reason to do anything
|
2020-05-29 04:10:10 +00:00
|
|
|
if (! $io->items->count() OR $io->total < 0)
|
2020-04-01 12:35:06 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
$io->account_id = $o->id;
|
|
|
|
|
|
|
|
if ($this->option('preview')) {
|
|
|
|
$this->info(sprintf('Invoice for Account [%d] - [%d] items totalling [%3.2f]',$o->id,$io->items->count(),$io->total));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the invoice
|
|
|
|
$io->site_id = 1; // @todo
|
|
|
|
$io->active = 1;
|
|
|
|
|
|
|
|
$io->pushNew();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|