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

220 lines
6.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
2017-12-20 21:36:29 +01:00
use App\Libraries\CurlUtils;
2017-11-23 09:10:14 +01:00
use Carbon;
use Str;
2017-12-21 10:59:03 +01:00
use Cache;
use App\Models\Invoice;
2017-12-20 21:36:29 +01:00
use App\Models\Currency;
2015-09-17 21:01:06 +02:00
use App\Ninja\Mailers\ContactMailer as Mailer;
2017-11-23 09:10:14 +01:00
use App\Ninja\Mailers\UserMailer;
use App\Ninja\Repositories\AccountRepository;
2015-09-17 21:01:06 +02:00
use App\Ninja\Repositories\InvoiceRepository;
2017-11-23 09:10:14 +01:00
use App\Models\ScheduledReport;
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;
2017-11-23 09:10:14 +01:00
use App\Jobs\ExportReportResults;
2017-11-23 11:23:19 +01:00
use App\Jobs\RunReport;
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
*/
2017-11-23 09:10:14 +01:00
public function __construct(Mailer $mailer, InvoiceRepository $invoiceRepo, AccountRepository $accountRepo, UserMailer $userMailer)
2015-09-17 21:01:06 +02:00
{
parent::__construct();
$this->mailer = $mailer;
$this->invoiceRepo = $invoiceRepo;
$this->accountRepo = $accountRepo;
2017-11-23 09:10:14 +01:00
$this->userMailer = $userMailer;
2015-09-17 21:01:06 +02:00
}
public function fire()
{
2017-10-24 09:59:26 +02:00
$this->info(date('r') . ' 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-11-23 09:10:14 +01:00
$this->chargeLateFees();
2017-12-25 20:46:59 +01:00
$this->sendReminderEmails();
2017-11-23 09:10:14 +01:00
$this->sendScheduledReports();
2017-12-20 21:36:29 +01:00
$this->loadExchangeRates();
2017-11-23 09:10:14 +01:00
$this->info('Done');
if ($errorEmail = env('ERROR_EMAIL')) {
\Mail::raw('EOM', function ($message) use ($errorEmail, $database) {
$message->to($errorEmail)
->from(CONTACT_EMAIL)
->subject("SendReminders [{$database}]: Finished successfully");
});
}
}
private function chargeLateFees()
{
2017-07-18 20:15:51 +02:00
$accounts = $this->accountRepo->findWithFees();
$this->info($accounts->count() . ' accounts found with fees');
2017-07-18 20:15:51 +02:00
foreach ($accounts as $account) {
if (! $account->hasFeature(FEATURE_EMAIL_TEMPLATES_REMINDERS)) {
continue;
}
$invoices = $this->invoiceRepo->findNeedingReminding($account, false);
$this->info($account->name . ': ' . $invoices->count() . ' invoices found');
2017-07-18 20:15:51 +02:00
foreach ($invoices as $invoice) {
if ($reminder = $account->getInvoiceReminder($invoice, false)) {
$this->info('Charge fee: ' . $invoice->id);
2017-12-04 22:21:59 +01:00
$account->loadLocalizationSettings($invoice->client); // support trans to add fee line item
2017-07-18 20:15:51 +02:00
$number = preg_replace('/[^0-9]/', '', $reminder);
2017-12-04 22:21:59 +01:00
2017-07-18 20:15:51 +02:00
$amount = $account->account_email_settings->{"late_fee{$number}_amount"};
$percent = $account->account_email_settings->{"late_fee{$number}_percent"};
$this->invoiceRepo->setLateFee($invoice, $amount, $percent);
}
}
}
2017-11-23 09:10:14 +01:00
}
2017-07-18 20:15:51 +02:00
2017-12-25 20:46:59 +01:00
private function sendReminderEmails()
2017-11-23 09:10:14 +01:00
{
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
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;
}
2017-12-26 11:03:23 +01:00
// standard reminders
2015-09-17 21:01:06 +02:00
$invoices = $this->invoiceRepo->findNeedingReminding($account);
$this->info($account->name . ': ' . $invoices->count() . ' invoices found');
2015-09-17 21:01:06 +02:00
foreach ($invoices as $invoice) {
if ($reminder = $account->getInvoiceReminder($invoice)) {
2017-12-26 11:11:58 +01:00
if ($invoice->last_sent_date == date('Y-m-d')) {
continue;
}
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);
}
}
2017-12-26 11:03:23 +01:00
// endless reminders
$invoices = $this->invoiceRepo->findNeedingEndlessReminding($account);
$this->info($account->name . ': ' . $invoices->count() . ' endless invoices found');
2017-12-26 11:03:23 +01:00
foreach ($invoices as $invoice) {
2017-12-26 11:11:58 +01:00
if ($invoice->last_sent_date == date('Y-m-d')) {
continue;
}
2017-12-26 11:03:23 +01:00
$this->info('Send email: ' . $invoice->id);
$this->mailer->sendInvoice($invoice, 'reminder4');
}
2015-09-17 21:01:06 +02:00
}
2017-11-23 09:10:14 +01:00
}
2015-09-17 21:01:06 +02:00
2017-11-23 09:10:14 +01:00
private function sendScheduledReports()
{
2017-11-23 12:35:58 +01:00
$scheduledReports = ScheduledReport::where('send_date', '<=', date('Y-m-d'))
->with('user', 'account.company')
->get();
$this->info($scheduledReports->count() . ' scheduled reports');
2017-11-23 09:10:14 +01:00
foreach ($scheduledReports as $scheduledReport) {
2017-11-23 12:35:58 +01:00
$user = $scheduledReport->user;
$account = $scheduledReport->account;
if (! $account->hasFeature(FEATURE_REPORTS)) {
continue;
}
2017-11-23 11:23:19 +01:00
$config = (array) json_decode($scheduledReport->config);
$reportType = $config['report_type'];
2017-01-31 12:33:46 +01:00
2017-11-23 11:23:19 +01:00
$report = dispatch(new RunReport($scheduledReport->user, $reportType, $config, true));
$file = dispatch(new ExportReportResults($scheduledReport->user, $config['export_format'], $reportType, $report->exportParams));
2017-11-23 09:10:14 +01:00
if ($file) {
$this->userMailer->sendScheduledReport($scheduledReport, $file);
}
2017-11-23 11:23:19 +01:00
$scheduledReport->updateSendDate();
2017-01-31 12:33:46 +01:00
}
2015-09-17 21:01:06 +02:00
}
2017-12-20 21:36:29 +01:00
private function loadExchangeRates()
{
$this->info('Loading latest exchange rates...');
2017-12-21 09:31:32 +01:00
$data = CurlUtils::get(config('ninja.exchange_rates_url'));
2017-12-20 21:36:29 +01:00
$data = json_decode($data);
Currency::whereCode(config('ninja.exchange_rates_base'))->update(['exchange_rate' => 1]);
2017-12-20 21:36:29 +01:00
foreach ($data->rates as $code => $rate) {
Currency::whereCode($code)->update(['exchange_rate' => $rate]);
}
2017-12-21 10:59:03 +01:00
2018-01-15 10:25:15 +01:00
CurlUtils::get(SITE_URL . '?clear_cache=true');
2017-12-20 21:36:29 +01: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
}
}