1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-17 00:22:51 +01:00
invoiceninja/app/Jobs/Ninja/SendReminders.php

325 lines
11 KiB
PHP
Raw Normal View History

2020-11-04 09:43:20 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2022-04-27 05:20:41 +02:00
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
2020-11-04 09:43:20 +01:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2020-11-04 09:43:20 +01:00
*/
namespace App\Jobs\Ninja;
use App\DataMapper\InvoiceItem;
use App\Events\Invoice\InvoiceWasEmailed;
use App\Jobs\Entity\EmailEntity;
2021-06-10 03:15:21 +02:00
use App\Jobs\Util\WebhookHandler;
2020-11-04 09:43:20 +01:00
use App\Libraries\MultiDB;
2021-03-16 22:15:43 +01:00
use App\Models\Account;
2020-11-04 10:32:49 +01:00
use App\Models\Invoice;
2020-12-07 11:43:21 +01:00
use App\Models\Webhook;
use App\Utils\Ninja;
use App\Utils\Traits\MakesDates;
2020-11-05 03:59:19 +01:00
use App\Utils\Traits\MakesReminders;
2020-11-04 09:43:20 +01:00
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
2020-11-05 02:33:31 +01:00
use Illuminate\Support\Carbon;
2021-12-14 10:54:23 +01:00
use Illuminate\Support\Facades\App;
2020-11-04 09:43:20 +01:00
2021-07-01 01:14:30 +02:00
//@DEPRECATED
2020-11-04 09:43:20 +01:00
class SendReminders implements ShouldQueue
{
2020-11-05 03:59:19 +01:00
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, MakesDates, MakesReminders;
2020-11-04 09:43:20 +01:00
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
nlog('Sending reminders '.Carbon::now()->format('Y-m-d h:i:s'));
2020-11-04 09:43:20 +01:00
if (! config('ninja.db.multi_db_enabled')) {
2020-11-04 10:32:49 +01:00
$this->sendReminderEmails();
2020-11-04 09:43:20 +01:00
} else {
//multiDB environment, need to
2020-11-25 15:19:52 +01:00
foreach (MultiDB::$dbs as $db) {
2020-11-04 09:43:20 +01:00
MultiDB::setDB($db);
2020-11-04 10:32:49 +01:00
$this->sendReminderEmails();
2020-11-04 09:43:20 +01:00
}
}
}
2020-11-04 10:32:49 +01:00
private function sendReminderEmails()
{
$invoices = Invoice::where('is_deleted', 0)
->where('balance', '>', 0)
->whereDate('next_send_date', '<=', now()->startOfDay())
->whereNotNull('next_send_date')
2020-11-05 02:33:31 +01:00
->with('client')
2020-11-04 10:32:49 +01:00
->cursor();
//we only need invoices that are payable
2020-11-25 15:19:52 +01:00
$invoices->filter(function ($invoice) {
2020-11-04 10:32:49 +01:00
return $invoice->isPayable();
2020-11-25 15:19:52 +01:00
})->each(function ($invoice) {
$reminder_template = $invoice->calculateTemplate('invoice');
2020-11-04 10:32:49 +01:00
2020-12-29 22:10:03 +01:00
nlog("hitting a reminder for {$invoice->number} with template {$reminder_template}");
2020-11-25 15:19:52 +01:00
if (in_array($reminder_template, ['reminder1', 'reminder2', 'reminder3', 'endless_reminder'])) {
$this->sendReminder($invoice, $reminder_template);
2021-06-10 03:15:21 +02:00
WebhookHandler::dispatch(Webhook::EVENT_REMIND_INVOICE, $invoice, $invoice->company);
2020-11-25 15:19:52 +01:00
}
});
}
private function checkSendSetting($invoice, $template)
{
switch ($template) {
case 'reminder1':
return $invoice->client->getSetting('enable_reminder1');
break;
case 'reminder2':
return $invoice->client->getSetting('enable_reminder2');
break;
case 'reminder3':
return $invoice->client->getSetting('enable_reminder3');
break;
case 'endless_reminder':
return $invoice->client->getSetting('enable_reminder_endless');
2020-11-25 15:19:52 +01:00
break;
default:
return false;
break;
}
}
2020-11-04 10:32:49 +01:00
2020-11-05 03:59:19 +01:00
/**
* Create a collection of all possible reminder dates
* and pass back the first one in chronology
2020-11-25 15:19:52 +01:00
*
2020-11-05 03:59:19 +01:00
* @param Invoice $invoice
2020-11-25 15:19:52 +01:00
* @return Carbon $date
2020-11-05 03:59:19 +01:00
*/
2020-11-05 02:33:31 +01:00
private function calculateNextSendDate($invoice)
{
2020-11-05 02:33:31 +01:00
$dates = collect();
$settings = $invoice->client->getMergedSettings();
2020-11-05 03:59:19 +01:00
$set_reminder1 = false;
$set_reminder2 = false;
$set_reminder3 = false;
2020-11-05 02:33:31 +01:00
if ((int) $settings->schedule_reminder1 > 0) {
$next_reminder_date = $this->calculateScheduledDate($invoice, $settings->schedule_reminder1, (int) $settings->num_days_reminder1);
2020-11-05 02:33:31 +01:00
2021-06-10 03:15:21 +02:00
if ($next_reminder_date && $next_reminder_date->gt(Carbon::parse($invoice->last_sent_date)));
2020-11-25 15:19:52 +01:00
$dates->push($next_reminder_date);
2020-11-05 02:33:31 +01:00
if (! $invoice->reminder1_sent) {
2020-11-05 03:59:19 +01:00
$set_reminder1 = true;
2020-11-25 15:19:52 +01:00
}
2020-11-05 03:59:19 +01:00
}
2020-11-05 02:33:31 +01:00
if ((int) $settings->num_days_reminder2 > 0) {
$next_reminder_date = $this->calculateScheduledDate($invoice, $settings->schedule_reminder2, (int) $settings->num_days_reminder2);
2020-11-05 03:59:19 +01:00
2021-06-10 03:15:21 +02:00
if ($next_reminder_date && $next_reminder_date->gt(Carbon::parse($invoice->last_sent_date)));
2020-11-25 15:19:52 +01:00
$dates->push($next_reminder_date);
2020-11-04 10:32:49 +01:00
if (! $invoice->reminder2_sent) {
2021-06-10 03:15:21 +02:00
$set_reminder2 = true;
2020-11-25 15:19:52 +01:00
}
2020-11-05 03:59:19 +01:00
}
if ((int) $settings->num_days_reminder3 > 0) {
$next_reminder_date = $this->calculateScheduledDate($invoice, $settings->schedule_reminder3, (int) $settings->num_days_reminder3);
2020-11-05 03:59:19 +01:00
2021-06-10 03:15:21 +02:00
if ($next_reminder_date && $next_reminder_date->gt(Carbon::parse($invoice->last_sent_date)));
2020-11-25 15:19:52 +01:00
$dates->push($next_reminder_date);
2020-11-05 02:33:31 +01:00
if (! $invoice->reminder3_sent) {
2020-11-05 03:59:19 +01:00
$set_reminder3 = true;
2020-11-25 15:19:52 +01:00
}
2020-11-05 03:59:19 +01:00
}
//If all the available reminders have fired, we then start to fire the endless reminders
if ((int) $settings->endless_reminder_frequency_id > 0 && ! $set_reminder1 && ! $set_reminder2 && ! $set_reminder3) {
$dates->push($this->addTimeInterval($invoice->last_sent_date, (int) $settings->endless_reminder_frequency_id));
2020-11-05 03:59:19 +01:00
}
2020-11-05 02:33:31 +01:00
2020-11-05 03:59:19 +01:00
//order the dates ascending and get first one
return $dates->sort()->first();
}
/**
* Helper method which switches values based on the $schedule_reminder
2020-11-25 15:19:52 +01:00
* @param Invoice $invoice
* @param string $schedule_reminder
* @param int $num_days_reminder
* @return Carbon $date
2020-11-05 03:59:19 +01:00
*/
private function calculateScheduledDate($invoice, $schedule_reminder, $num_days_reminder) :?Carbon
2020-11-05 02:33:31 +01:00
{
2021-06-11 00:20:46 +02:00
$offset = $invoice->client->timezone_offset();
2021-06-10 03:15:21 +02:00
2020-11-05 02:33:31 +01:00
switch ($schedule_reminder) {
case 'after_invoice_date':
2021-06-11 00:20:46 +02:00
return Carbon::parse($invoice->date)->addDays($num_days_reminder)->startOfDay()->addSeconds($offset);
2020-11-05 02:33:31 +01:00
break;
case 'before_due_date':
2021-06-11 00:20:46 +02:00
return Carbon::parse($invoice->due_date)->subDays($num_days_reminder)->startOfDay()->addSeconds($offset);
2020-11-05 02:33:31 +01:00
break;
case 'after_due_date':
2021-06-11 00:20:46 +02:00
return Carbon::parse($invoice->due_date)->addDays($num_days_reminder)->startOfDay()->addSeconds($offset);
2020-11-05 02:33:31 +01:00
break;
default:
return null;
break;
}
}
2020-11-05 03:59:19 +01:00
/**
* Sends the reminder and/or late fee for the invoice.
2020-11-25 15:19:52 +01:00
*
* @param Invoice $invoice
* @param string $template
* @return void
2020-11-05 03:59:19 +01:00
*/
private function sendReminder($invoice, $template) :void
{
$invoice = $this->calcLateFee($invoice, $template);
2020-11-04 10:32:49 +01:00
2020-11-25 15:19:52 +01:00
$invoice->invitations->each(function ($invitation) use ($template, $invoice) {
//only send if enable_reminder setting is toggled to yes
2021-03-16 22:15:43 +01:00
if ($this->checkSendSetting($invoice, $template) && $invoice->company->account->hasFeature(Account::FEATURE_EMAIL_TEMPLATES_REMINDERS)) {
nlog('firing email');
2020-11-07 11:13:04 +01:00
EmailEntity::dispatch($invitation, $invitation->company, $template)->delay(10);
2020-11-07 11:13:04 +01:00
}
});
2020-11-25 15:19:52 +01:00
if ($this->checkSendSetting($invoice, $template)) {
event(new InvoiceWasEmailed($invoice->invitations->first(), $invoice->company, Ninja::eventVars(), $template));
2020-11-25 15:19:52 +01:00
}
2020-11-25 15:19:52 +01:00
$invoice->last_sent_date = now();
$invoice->next_send_date = $this->calculateNextSendDate($invoice);
2020-11-25 15:19:52 +01:00
if (in_array($template, ['reminder1', 'reminder2', 'reminder3'])) {
$invoice->{$template.'_sent'} = now();
2020-11-25 15:19:52 +01:00
}
2021-06-10 03:15:21 +02:00
$invoice->service()->touchReminder($template)->save();
2021-06-10 03:15:21 +02:00
// $invoice->save();
}
2020-11-05 03:59:19 +01:00
/**
* Calculates the late if - if any - and rebuilds the invoice
2020-11-25 15:19:52 +01:00
*
* @param Invoice $invoice
* @param string $template
* @return Invoice
2020-11-05 03:59:19 +01:00
*/
private function calcLateFee($invoice, $template) :Invoice
{
$late_fee_amount = 0;
$late_fee_percent = 0;
switch ($template) {
case 'reminder1':
$late_fee_amount = $invoice->client->getSetting('late_fee_amount1');
$late_fee_percent = $invoice->client->getSetting('late_fee_percent1');
break;
case 'reminder2':
$late_fee_amount = $invoice->client->getSetting('late_fee_amount2');
$late_fee_percent = $invoice->client->getSetting('late_fee_percent2');
break;
case 'reminder3':
$late_fee_amount = $invoice->client->getSetting('late_fee_amount3');
$late_fee_percent = $invoice->client->getSetting('late_fee_percent3');
break;
case 'endless_reminder':
$late_fee_amount = $invoice->client->getSetting('late_fee_endless_amount');
$late_fee_percent = $invoice->client->getSetting('late_fee_endless_percent');
2020-11-25 15:19:52 +01:00
break;
default:
$late_fee_amount = 0;
$late_fee_percent = 0;
break;
}
return $this->setLateFee($invoice, $late_fee_amount, $late_fee_percent);
}
2020-11-05 03:59:19 +01:00
/**
* Applies the late fee to the invoice line items
2020-11-25 15:19:52 +01:00
*
* @param Invoice $invoice
2020-11-05 03:59:19 +01:00
* @param float $amount The fee amount
* @param float $percent The fee percentage amount
2020-11-25 15:19:52 +01:00
*
* @return Invoice
2020-11-05 03:59:19 +01:00
*/
private function setLateFee($invoice, $amount, $percent) :Invoice
{
2021-12-14 10:54:23 +01:00
App::forgetInstance('translator');
$t = app('translator');
$t->replace(Ninja::transformTranslations($invoice->client->getMergedSettings()));
App::setLocale($invoice->client->locale());
2020-11-11 01:13:39 +01:00
$temp_invoice_balance = $invoice->balance;
2020-11-25 15:19:52 +01:00
if ($amount <= 0 && $percent <= 0) {
return $invoice;
2020-11-25 15:19:52 +01:00
}
$fee = $amount;
2020-11-25 15:19:52 +01:00
if ($invoice->partial > 0) {
$fee += round($invoice->partial * $percent / 100, 2);
2020-11-25 15:19:52 +01:00
} else {
$fee += round($invoice->balance * $percent / 100, 2);
2020-11-25 15:19:52 +01:00
}
$invoice_item = new InvoiceItem;
$invoice_item->type_id = '5';
$invoice_item->product_key = trans('texts.fee');
2021-02-23 11:04:39 +01:00
$invoice_item->notes = ctrans('texts.late_fee_added', ['date' => $this->translateDate(now()->startOfDay(), $invoice->client->date_format(), $invoice->client->locale())]);
$invoice_item->quantity = 1;
$invoice_item->cost = $fee;
$invoice_items = $invoice->line_items;
$invoice_items[] = $invoice_item;
$invoice->line_items = $invoice_items;
/**Refresh Invoice values*/
$invoice = $invoice->calc()->getInvoice();
2021-12-14 10:54:23 +01:00
$invoice->client->service()->updateBalance($invoice->balance - $temp_invoice_balance)->save();
$invoice->ledger()->updateInvoiceBalance($invoice->balance - $temp_invoice_balance, "Late Fee Adjustment for invoice {$invoice->number}");
2020-11-11 01:13:39 +01:00
return $invoice;
2020-11-04 10:32:49 +01:00
}
2020-11-25 15:19:52 +01:00
}