2016-07-21 14:35:23 +02:00
|
|
|
<?php namespace App\Console\Commands;
|
2016-02-22 20:34:21 +01:00
|
|
|
|
|
|
|
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;
|
2016-09-12 07:51:41 +02:00
|
|
|
use Exception;
|
2016-02-22 20:34:21 +01:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* Class ChargeRenewalInvoices
|
|
|
|
*/
|
2016-02-22 20:34:21 +01:00
|
|
|
class ChargeRenewalInvoices extends Command
|
|
|
|
{
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-02-22 20:34:21 +01:00
|
|
|
protected $name = 'ninja:charge-renewals';
|
2016-07-03 18:11:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-02-22 20:34:21 +01:00
|
|
|
protected $description = 'Charge renewal invoices';
|
2016-07-03 18:11:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Mailer
|
|
|
|
*/
|
2016-02-22 20:34:21 +01:00
|
|
|
protected $mailer;
|
2016-07-03 18:11:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var AccountRepository
|
|
|
|
*/
|
2016-02-22 20:34:21 +01:00
|
|
|
protected $accountRepo;
|
2016-07-03 18:11:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var PaymentService
|
|
|
|
*/
|
2016-02-22 20:34:21 +01:00
|
|
|
protected $paymentService;
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* ChargeRenewalInvoices constructor.
|
|
|
|
* @param Mailer $mailer
|
|
|
|
* @param AccountRepository $repo
|
|
|
|
* @param PaymentService $paymentService
|
|
|
|
*/
|
2016-02-22 20:34:21 +01:00
|
|
|
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)
|
2016-02-22 20:34:21 +01:00
|
|
|
->whereDueDate(date('Y-m-d'))
|
2016-08-01 09:50:18 +02:00
|
|
|
->where('balance', '>', 0)
|
2016-02-22 20:34:21 +01:00
|
|
|
->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 15:04: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);
|
2016-07-21 15:04:23 +02:00
|
|
|
|
|
|
|
if ( ! $account) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-07-16 22:19:43 +02:00
|
|
|
$company = $account->company;
|
|
|
|
if ( ! $company->plan || $company->plan == PLAN_FREE) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-09-12 07:51:41 +02:00
|
|
|
try {
|
|
|
|
$this->info("Charging invoice {$invoice->invoice_number}");
|
|
|
|
$this->paymentService->autoBillInvoice($invoice);
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
$this->info('Error: ' . $exception->getMessage());
|
|
|
|
}
|
2016-02-22 20:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->info('Done');
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-02-22 20:34:21 +01:00
|
|
|
protected function getArguments()
|
|
|
|
{
|
2016-07-03 18:11:58 +02:00
|
|
|
return [];
|
2016-02-22 20:34:21 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-02-22 20:34:21 +01:00
|
|
|
protected function getOptions()
|
|
|
|
{
|
2016-07-03 18:11:58 +02:00
|
|
|
return [];
|
2016-02-22 20:34:21 +01:00
|
|
|
}
|
|
|
|
}
|