1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-24 02:11:34 +02:00
invoiceninja/app/Console/Commands/SendRenewalInvoices.php

78 lines
2.3 KiB
PHP
Raw Normal View History

2015-05-08 10:21:29 +02:00
<?php namespace App\Console\Commands;
use DB;
use DateTime;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use App\Models\Account;
use App\Ninja\Mailers\ContactMailer as Mailer;
use App\Ninja\Repositories\AccountRepository;
class SendRenewalInvoices extends Command
{
protected $name = 'ninja:send-renewals';
protected $description = 'Send renewal invoices';
protected $mailer;
protected $accountRepo;
public function __construct(Mailer $mailer, AccountRepository $repo)
{
parent::__construct();
$this->mailer = $mailer;
$this->accountRepo = $repo;
}
public function fire()
{
$this->info(date('Y-m-d').' Running SendRenewalInvoices...');
$today = new DateTime();
2015-11-12 21:36:28 +01:00
$sentTo = [];
2015-05-08 10:21:29 +02:00
2015-10-11 16:41:09 +02:00
// get all accounts with pro plans expiring in 10 days
2015-11-12 21:36:28 +01:00
$accounts = Account::whereRaw('datediff(curdate(), pro_plan_paid) = 355')
->orderBy('id')
->get();
2015-05-08 10:21:29 +02:00
$this->info(count($accounts).' accounts found');
2015-05-31 14:37:29 +02:00
2015-05-08 10:21:29 +02:00
foreach ($accounts as $account) {
2015-11-12 21:36:28 +01:00
// don't send multiple invoices to multi-company users
if ($userAccountId = $this->accountRepo->getUserAccountId($account)) {
if (isset($sentTo[$userAccountId])) {
continue;
} else {
$sentTo[$userAccountId] = true;
}
}
2015-05-08 10:21:29 +02:00
$client = $this->accountRepo->getNinjaClient($account);
$invitation = $this->accountRepo->createNinjaInvoice($client, $account);
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();
$this->mailer->sendInvoice($invoice);
2015-11-12 21:36:28 +01:00
$this->info("Sent invoice to {$client->getDisplayName()}");
2015-05-08 10:21:29 +02:00
}
$this->info('Done');
}
protected function getArguments()
{
return array(
//array('example', InputArgument::REQUIRED, 'An example argument.'),
);
}
protected function getOptions()
{
return array(
//array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null),
);
}
}