1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 21:52:35 +01:00
invoiceninja/app/Console/Commands/SendRenewalInvoices.php

119 lines
3.2 KiB
PHP
Raw Normal View History

2015-05-08 10:21:29 +02:00
<?php namespace App\Console\Commands;
2016-07-11 19:08:43 +02:00
use Utils;
2015-05-08 10:21:29 +02:00
use Illuminate\Console\Command;
2016-04-17 04:09:01 +02:00
use App\Models\Company;
2015-05-08 10:21:29 +02:00
use App\Ninja\Mailers\ContactMailer as Mailer;
use App\Ninja\Repositories\AccountRepository;
/**
* Class SendRenewalInvoices
*/
2015-05-08 10:21:29 +02:00
class SendRenewalInvoices extends Command
{
/**
* @var string
*/
2015-05-08 10:21:29 +02:00
protected $name = 'ninja:send-renewals';
/**
* @var string
*/
2015-05-08 10:21:29 +02:00
protected $description = 'Send renewal invoices';
/**
* @var Mailer
*/
2015-05-08 10:21:29 +02:00
protected $mailer;
/**
* @var AccountRepository
*/
2015-05-08 10:21:29 +02:00
protected $accountRepo;
/**
* SendRenewalInvoices constructor.
*
* @param Mailer $mailer
* @param AccountRepository $repo
*/
2015-05-08 10:21:29 +02:00
public function __construct(Mailer $mailer, AccountRepository $repo)
{
parent::__construct();
$this->mailer = $mailer;
$this->accountRepo = $repo;
}
2016-07-11 19:08:43 +02:00
2015-05-08 10:21:29 +02:00
public function fire()
{
$this->info(date('Y-m-d').' Running SendRenewalInvoices...');
2016-04-17 04:09:01 +02:00
// get all accounts with plans expiring in 10 days
$companies = Company::whereRaw('datediff(plan_expires, curdate()) = 10')
2015-11-12 21:36:28 +01:00
->orderBy('id')
->get();
2016-07-14 11:46:00 +02:00
$this->info(count($companies).' companies found renewing in 10 days');
2015-05-31 14:37:29 +02:00
2016-04-17 04:09:01 +02:00
foreach ($companies as $company) {
if (!count($company->accounts)) {
continue;
2015-11-12 21:36:28 +01:00
}
2016-07-11 19:08:43 +02:00
2016-04-17 04:09:01 +02:00
$account = $company->accounts->sortBy('id')->first();
2016-07-11 19:08:43 +02:00
$plan = [];
$plan['plan'] = $company->plan;
$plan['term'] = $company->plan_term;
$plan['num_users'] = $company->num_users;
$plan['price'] = min($company->plan_price, Utils::getPlanPrice($plan));
2016-04-17 04:09:01 +02:00
if ($company->pending_plan) {
2016-07-11 19:08:43 +02:00
$plan['plan'] = $company->pending_plan;
$plan['term'] = $company->pending_term;
$plan['num_users'] = $company->pending_num_users;
$plan['price'] = min($company->pending_plan_price, Utils::getPlanPrice($plan));
2016-04-17 04:09:01 +02:00
}
2016-07-11 19:08:43 +02:00
if ($plan['plan'] == PLAN_FREE || !$plan['plan'] || !$plan['term'] || !$plan['price']){
2016-04-17 04:09:01 +02:00
continue;
}
2016-07-11 19:08:43 +02:00
2015-05-08 10:21:29 +02:00
$client = $this->accountRepo->getNinjaClient($account);
2016-07-11 19:08:43 +02:00
$invitation = $this->accountRepo->createNinjaInvoice($client, $account, $plan, 0, false);
2015-10-11 16:41:09 +02:00
// set the due date to 10 days from now
$invoice = $invitation->invoice;
$invoice->due_date = date('Y-m-d', strtotime('+ 10 days'));
$invoice->save();
2016-07-11 19:08:43 +02:00
$term = $plan['term'];
$plan = $plan['plan'];
2016-04-21 21:01:17 +02:00
if ($term == PLAN_TERM_YEARLY) {
$this->mailer->sendInvoice($invoice);
$this->info("Sent {$term}ly {$plan} invoice to {$client->getDisplayName()}");
} else {
$this->info("Created {$term}ly {$plan} invoice for {$client->getDisplayName()}");
}
2015-05-08 10:21:29 +02:00
}
$this->info('Done');
}
/**
* @return array
*/
2015-05-08 10:21:29 +02:00
protected function getArguments()
{
return [];
2015-05-08 10:21:29 +02:00
}
/**
* @return array
*/
2015-05-08 10:21:29 +02:00
protected function getOptions()
{
return [];
2015-05-08 10:21:29 +02:00
}
}