2017-01-30 20:40:43 +01:00
|
|
|
<?php
|
2016-02-22 20:34:21 +01:00
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
use App\Models\Account;
|
|
|
|
use App\Models\Invoice;
|
2016-02-22 20:34:21 +01:00
|
|
|
use App\Ninja\Mailers\ContactMailer as Mailer;
|
|
|
|
use App\Ninja\Repositories\AccountRepository;
|
|
|
|
use App\Services\PaymentService;
|
2017-01-30 20:40:43 +01:00
|
|
|
use Illuminate\Console\Command;
|
2017-04-16 16:22:07 +02:00
|
|
|
use Carbon;
|
2017-05-01 14:46:57 +02:00
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
2016-02-22 20:34:21 +01:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
2017-01-30 20:40:43 +01:00
|
|
|
* Class ChargeRenewalInvoices.
|
2016-07-03 18:11:58 +02:00
|
|
|
*/
|
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.
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
|
|
|
* @param Mailer $mailer
|
2016-07-03 18:11:58 +02:00
|
|
|
* @param AccountRepository $repo
|
2017-01-30 20:40:43 +01:00
|
|
|
* @param PaymentService $paymentService
|
2016-07-03 18:11:58 +02:00
|
|
|
*/
|
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;
|
|
|
|
}
|
|
|
|
|
2019-09-13 01:30:10 +02:00
|
|
|
public function handle()
|
2016-02-22 20:34:21 +01:00
|
|
|
{
|
2017-10-24 09:59:26 +02:00
|
|
|
$this->info(date('r').' ChargeRenewalInvoices...');
|
2016-02-22 20:34:21 +01:00
|
|
|
|
2017-05-01 14:17:52 +02:00
|
|
|
if ($database = $this->option('database')) {
|
|
|
|
config(['database.default' => $database]);
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
2018-01-16 14:48:56 +01:00
|
|
|
$this->info($invoices->count() . ' invoices found');
|
2016-02-22 20:34:21 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
2017-01-30 17:05:31 +01:00
|
|
|
if (! $account) {
|
2016-07-21 15:04:23 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-07-16 22:19:43 +02:00
|
|
|
$company = $account->company;
|
2017-01-30 17:05:31 +01:00
|
|
|
if (! $company->plan || $company->plan == PLAN_FREE) {
|
2016-07-16 22:19:43 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-04-16 16:22:07 +02:00
|
|
|
if (Carbon::parse($company->plan_expires)->isFuture()) {
|
|
|
|
$this->info('Skipping invoice ' . $invoice->invoice_number . ' [plan not expired]');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-12-22 18:42:21 +01:00
|
|
|
$this->info("Charging invoice {$invoice->invoice_number}");
|
2017-01-30 17:05:31 +01:00
|
|
|
if (! $this->paymentService->autoBillInvoice($invoice)) {
|
2016-12-22 18:42:21 +01:00
|
|
|
$this->info('Failed to auto-bill, emailing invoice');
|
|
|
|
$this->mailer->sendInvoice($invoice);
|
2016-09-12 07:51:41 +02:00
|
|
|
}
|
2016-02-22 20:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->info('Done');
|
2016-12-18 20:39:25 +01:00
|
|
|
|
|
|
|
if ($errorEmail = env('ERROR_EMAIL')) {
|
|
|
|
\Mail::raw('EOM', function ($message) use ($errorEmail) {
|
|
|
|
$message->to($errorEmail)
|
|
|
|
->from(CONTACT_EMAIL)
|
|
|
|
->subject('ChargeRenewalInvoices: Finished successfully');
|
|
|
|
});
|
|
|
|
}
|
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 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()
|
|
|
|
{
|
2017-05-01 14:17:52 +02:00
|
|
|
return [
|
|
|
|
['database', null, InputOption::VALUE_OPTIONAL, 'Database', null],
|
|
|
|
];
|
2016-02-22 20:34:21 +01:00
|
|
|
}
|
|
|
|
}
|