2016-07-21 14:35:23 +02:00
|
|
|
<?php namespace App\Console\Commands;
|
2015-09-17 21:01:06 +02:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
use App\Models\Invoice;
|
2015-09-17 21:01:06 +02:00
|
|
|
use Illuminate\Console\Command;
|
|
|
|
use App\Ninja\Mailers\ContactMailer as Mailer;
|
2016-07-03 18:11:58 +02:00
|
|
|
use App\Ninja\Repositories\AccountRepository;
|
2015-09-17 21:01:06 +02:00
|
|
|
use App\Ninja\Repositories\InvoiceRepository;
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* Class SendReminders
|
|
|
|
*/
|
2015-09-17 21:01:06 +02:00
|
|
|
class SendReminders extends Command
|
|
|
|
{
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2015-09-17 21:01:06 +02:00
|
|
|
protected $name = 'ninja:send-reminders';
|
2016-07-03 18:11:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2015-09-17 21:01:06 +02:00
|
|
|
protected $description = 'Send reminder emails';
|
2016-07-03 18:11:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Mailer
|
|
|
|
*/
|
2015-09-17 21:01:06 +02:00
|
|
|
protected $mailer;
|
2016-07-03 18:11:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var InvoiceRepository
|
|
|
|
*/
|
2015-09-17 21:01:06 +02:00
|
|
|
protected $invoiceRepo;
|
2016-07-03 18:11:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var accountRepository
|
|
|
|
*/
|
2015-09-17 21:01:06 +02:00
|
|
|
protected $accountRepo;
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* SendReminders constructor.
|
|
|
|
* @param Mailer $mailer
|
|
|
|
* @param InvoiceRepository $invoiceRepo
|
|
|
|
* @param accountRepository $accountRepo
|
|
|
|
*/
|
2015-09-17 21:01:06 +02:00
|
|
|
public function __construct(Mailer $mailer, InvoiceRepository $invoiceRepo, AccountRepository $accountRepo)
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->mailer = $mailer;
|
|
|
|
$this->invoiceRepo = $invoiceRepo;
|
|
|
|
$this->accountRepo = $accountRepo;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function fire()
|
|
|
|
{
|
2016-07-03 18:11:58 +02:00
|
|
|
$this->info(date('Y-m-d') . ' Running SendReminders...');
|
2015-09-17 21:01:06 +02:00
|
|
|
|
|
|
|
$accounts = $this->accountRepo->findWithReminders();
|
2016-07-03 18:11:58 +02:00
|
|
|
$this->info(count($accounts) . ' accounts found');
|
2015-09-17 21:01:06 +02:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/** @var \App\Models\Account $account */
|
2015-09-17 21:01:06 +02:00
|
|
|
foreach ($accounts as $account) {
|
2016-04-19 04:35:18 +02:00
|
|
|
if (!$account->hasFeature(FEATURE_EMAIL_TEMPLATES_REMINDERS)) {
|
2015-10-13 09:11:44 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-09-17 21:01:06 +02:00
|
|
|
$invoices = $this->invoiceRepo->findNeedingReminding($account);
|
2016-07-03 18:11:58 +02:00
|
|
|
$this->info($account->name . ': ' . count($invoices) . ' invoices found');
|
2015-09-17 21:01:06 +02:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/** @var Invoice $invoice */
|
2015-09-17 21:01:06 +02:00
|
|
|
foreach ($invoices as $invoice) {
|
2015-12-28 11:15:56 +01:00
|
|
|
if ($reminder = $account->getInvoiceReminder($invoice)) {
|
2015-11-17 14:42:54 +01:00
|
|
|
$this->info('Send to ' . $invoice->id);
|
2015-09-17 21:01:06 +02:00
|
|
|
$this->mailer->sendInvoice($invoice, $reminder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->info('Done');
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2015-09-17 21:01:06 +02:00
|
|
|
protected function getArguments()
|
|
|
|
{
|
2016-07-03 18:11:58 +02:00
|
|
|
return [];
|
2015-09-17 21:01:06 +02:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2015-09-17 21:01:06 +02:00
|
|
|
protected function getOptions()
|
|
|
|
{
|
2016-07-03 18:11:58 +02:00
|
|
|
return [];
|
2015-09-17 21:01:06 +02:00
|
|
|
}
|
|
|
|
}
|