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

138 lines
3.9 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;
2017-05-01 14:46:57 +02:00
use Symfony\Component\Console\Input\InputOption;
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
2017-05-01 14:17:52 +02:00
if ($database = $this->option('database')) {
config(['database.default' => $database]);
}
2017-07-18 20:15:51 +02:00
$accounts = $this->accountRepo->findWithFees();
$this->info(count($accounts) . ' accounts found with fees');
foreach ($accounts as $account) {
if (! $account->hasFeature(FEATURE_EMAIL_TEMPLATES_REMINDERS)) {
continue;
}
$invoices = $this->invoiceRepo->findNeedingReminding($account, false);
$this->info($account->name . ': ' . count($invoices) . ' invoices found');
foreach ($invoices as $invoice) {
if ($reminder = $account->getInvoiceReminder($invoice, false)) {
$this->info('Charge fee: ' . $invoice->id);
$number = preg_replace('/[^0-9]/', '', $reminder);
$amount = $account->account_email_settings->{"late_fee{$number}_amount"};
$percent = $account->account_email_settings->{"late_fee{$number}_percent"};
$this->invoiceRepo->setLateFee($invoice, $amount, $percent);
}
}
}
2015-09-17 21:01:06 +02:00
$accounts = $this->accountRepo->findWithReminders();
2017-07-18 20:15:51 +02:00
$this->info(count($accounts) . ' accounts found with reminders');
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)) {
2017-07-18 20:15:51 +02:00
$this->info('Send email: ' . $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')) {
2017-05-01 17:35:06 +02:00
\Mail::raw('EOM', function ($message) use ($errorEmail, $database) {
2017-01-31 12:33:46 +01:00
$message->to($errorEmail)
->from(CONTACT_EMAIL)
2017-05-01 17:35:06 +02:00
->subject("SendReminders [{$database}]: Finished successfully");
2017-01-31 12:33:46 +01:00
});
}
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()
{
2017-05-01 14:17:52 +02:00
return [
['database', null, InputOption::VALUE_OPTIONAL, 'Database', null],
];
2015-09-17 21:01:06 +02:00
}
}