1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Console/Commands/SendRenewalInvoices.php

136 lines
3.8 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
namespace App\Console\Commands;
2015-05-08 10:21:29 +02:00
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;
2017-01-30 20:40:43 +01:00
use Illuminate\Console\Command;
use Utils;
2017-05-01 14:46:57 +02:00
use Symfony\Component\Console\Input\InputOption;
2015-05-08 10:21:29 +02:00
/**
2017-01-30 20:40:43 +01:00
* 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.
*
2017-01-30 20:40:43 +01:00
* @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...');
2017-05-01 14:17:52 +02:00
if ($database = $this->option('database')) {
config(['database.default' => $database]);
}
2016-04-17 04:09:01 +02:00
// get all accounts with plans expiring in 10 days
2016-12-27 12:13:57 +01:00
$companies = Company::whereRaw("datediff(plan_expires, curdate()) = 10 and (plan = 'pro' or plan = 'enterprise')")
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) {
2017-01-30 20:40:43 +01:00
if (! count($company->accounts)) {
2016-04-17 04:09:01 +02:00
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
2017-01-30 20:40:43 +01: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');
2016-12-18 20:39:25 +01:00
if ($errorEmail = env('ERROR_EMAIL')) {
2017-05-01 17:35:06 +02:00
\Mail::raw('EOM', function ($message) use ($errorEmail, $database) {
2016-12-18 20:39:25 +01:00
$message->to($errorEmail)
->from(CONTACT_EMAIL)
2017-05-01 17:35:06 +02:00
->subject("SendRenewalInvoices [{$database}]: Finished successfully");
2016-12-18 20:39:25 +01:00
});
}
2015-05-08 10:21:29 +02:00
}
/**
* @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()
{
2017-05-01 14:17:52 +02:00
return [
['database', null, InputOption::VALUE_OPTIONAL, 'Database', null],
];
2015-05-08 10:21:29 +02:00
}
}