1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00
invoiceninja/app/Console/Commands/ChargeRenewalInvoices.php

100 lines
2.3 KiB
PHP
Raw Normal View History

2016-07-21 14:35:23 +02:00
<?php namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Ninja\Mailers\ContactMailer as Mailer;
use App\Ninja\Repositories\AccountRepository;
use App\Services\PaymentService;
use App\Models\Invoice;
2016-07-16 22:19:43 +02:00
use App\Models\Account;
/**
* Class ChargeRenewalInvoices
*/
class ChargeRenewalInvoices extends Command
{
/**
* @var string
*/
protected $name = 'ninja:charge-renewals';
/**
* @var string
*/
protected $description = 'Charge renewal invoices';
/**
* @var Mailer
*/
protected $mailer;
/**
* @var AccountRepository
*/
protected $accountRepo;
/**
* @var PaymentService
*/
protected $paymentService;
/**
* ChargeRenewalInvoices constructor.
* @param Mailer $mailer
* @param AccountRepository $repo
* @param PaymentService $paymentService
*/
public function __construct(Mailer $mailer, AccountRepository $repo, PaymentService $paymentService)
{
parent::__construct();
$this->mailer = $mailer;
$this->accountRepo = $repo;
$this->paymentService = $paymentService;
}
public function fire()
{
$this->info(date('Y-m-d').' ChargeRenewalInvoices...');
2016-07-16 22:19:43 +02:00
$ninjaAccount = $this->accountRepo->getNinjaAccount();
$invoices = Invoice::whereAccountId($ninjaAccount->id)
->whereDueDate(date('Y-m-d'))
->with('client')
->orderBy('id')
->get();
$this->info(count($invoices).' invoices found');
foreach ($invoices as $invoice) {
2016-07-16 22:19:43 +02:00
2016-07-21 14:35:23 +02:00
// check if account has switched to free since the invoice was created
2016-07-16 22:19:43 +02:00
$account = Account::find($invoice->client->public_id);
$company = $account->company;
if ( ! $company->plan || $company->plan == PLAN_FREE) {
continue;
}
$this->info("Charging invoice {$invoice->invoice_number}");
$this->paymentService->autoBillInvoice($invoice);
}
$this->info('Done');
}
/**
* @return array
*/
protected function getArguments()
{
return [];
}
/**
* @return array
*/
protected function getOptions()
{
return [];
}
}