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

109 lines
2.6 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
namespace App\Console\Commands;
2015-09-17 21:01:06 +02:00
use App\Models\Invoice;
2015-09-17 21:01:06 +02:00
use App\Ninja\Mailers\ContactMailer as Mailer;
use App\Ninja\Repositories\AccountRepository;
2015-09-17 21:01:06 +02:00
use App\Ninja\Repositories\InvoiceRepository;
2017-01-30 20:40:43 +01:00
use Illuminate\Console\Command;
2015-09-17 21:01:06 +02:00
/**
2017-01-30 20:40:43 +01:00
* Class SendReminders.
*/
2015-09-17 21:01:06 +02:00
class SendReminders extends Command
{
/**
* @var string
*/
2015-09-17 21:01:06 +02:00
protected $name = 'ninja:send-reminders';
/**
* @var string
*/
2015-09-17 21:01:06 +02:00
protected $description = 'Send reminder emails';
/**
* @var Mailer
*/
2015-09-17 21:01:06 +02:00
protected $mailer;
/**
* @var InvoiceRepository
*/
2015-09-17 21:01:06 +02:00
protected $invoiceRepo;
2017-01-31 12:33:46 +01:00
/**
* @var accountRepository
*/
2015-09-17 21:01:06 +02:00
protected $accountRepo;
/**
* SendReminders constructor.
2017-01-30 20:40:43 +01:00
*
* @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()
{
$this->info(date('Y-m-d') . ' Running SendReminders...');
2015-09-17 21:01:06 +02:00
$accounts = $this->accountRepo->findWithReminders();
$this->info(count($accounts) . ' accounts found');
2015-09-17 21:01:06 +02:00
/** @var \App\Models\Account $account */
2015-09-17 21:01:06 +02:00
foreach ($accounts as $account) {
2017-01-30 20:40:43 +01: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);
$this->info($account->name . ': ' . count($invoices) . ' invoices found');
2015-09-17 21:01:06 +02:00
/** @var Invoice $invoice */
2015-09-17 21:01:06 +02:00
foreach ($invoices as $invoice) {
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');
2017-01-31 12:33:46 +01:00
if ($errorEmail = env('ERROR_EMAIL')) {
\Mail::raw('EOM', function ($message) use ($errorEmail) {
$message->to($errorEmail)
->from(CONTACT_EMAIL)
->subject('SendReminders: Finished successfully');
});
}
2015-09-17 21:01:06 +02:00
}
/**
* @return array
*/
2015-09-17 21:01:06 +02:00
protected function getArguments()
{
return [];
2015-09-17 21:01:06 +02:00
}
/**
* @return array
*/
2015-09-17 21:01:06 +02:00
protected function getOptions()
{
return [];
2015-09-17 21:01:06 +02:00
}
}