1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Jobs/Util/ReminderJob.php

206 lines
7.3 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Jobs\Util;
2021-07-01 01:14:30 +02:00
use App\DataMapper\InvoiceItem;
use App\Events\Invoice\InvoiceWasEmailed;
2021-05-19 02:15:29 +02:00
use App\Jobs\Entity\EmailEntity;
use App\Libraries\MultiDB;
use App\Models\Invoice;
2020-07-08 14:02:16 +02:00
use App\Utils\Ninja;
2021-07-01 01:14:30 +02:00
use App\Utils\Traits\MakesDates;
2021-05-25 23:31:17 +02:00
use App\Utils\Traits\MakesReminders;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Carbon;
2021-08-19 00:02:28 +02:00
use Illuminate\Support\Facades\App;
class ReminderJob implements ShouldQueue
{
2021-07-01 01:14:30 +02:00
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, MakesReminders, MakesDates;
2021-08-09 00:44:51 +02:00
public $tries = 1;
public function __construct()
{
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
if (! config('ninja.db.multi_db_enabled')) {
$this->processReminders();
} else {
//multiDB environment, need to
foreach (MultiDB::$dbs as $db) {
MultiDB::setDB($db);
2021-08-09 00:44:51 +02:00
nlog("set db {$db}");
$this->processReminders();
}
}
}
private function processReminders()
{
2021-06-21 07:57:14 +02:00
nlog("Sending invoice reminders " . now()->format('Y-m-d h:i:s'));
Invoice::where('next_send_date', '<=', now()->toDateTimeString())
2021-06-26 02:32:38 +02:00
->whereNull('deleted_at')
2021-06-15 00:06:01 +02:00
->where('is_deleted', 0)
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL])
->where('balance', '>', 0)
->whereHas('client', function ($query) {
$query->where('is_deleted',0)
->where('deleted_at', NULL);
})
->whereHas('company', function ($query) {
$query->where('is_disabled',0);
})
2021-06-15 00:06:01 +02:00
->with('invitations')->cursor()->each(function ($invoice) {
if ($invoice->isPayable()) {
2020-11-25 15:19:52 +01:00
$reminder_template = $invoice->calculateTemplate('invoice');
2021-01-18 11:59:24 +01:00
$invoice->service()->touchReminder($reminder_template)->save();
2021-07-01 01:14:30 +02:00
$invoice = $this->calcLateFee($invoice, $reminder_template);
2022-01-10 02:47:16 +01:00
$invoice->service()->touchPdf();
2021-07-20 06:49:04 +02:00
//check if this reminder needs to be emailed
//15-01-2022 - insert addition if block if send_reminders is definitely set
if(in_array($reminder_template, ['reminder1','reminder2','reminder3','reminder_endless']) && $invoice->client->getSetting("enable_".$reminder_template) && $invoice->client->getSetting("send_reminders") && $invoice->company->account->isPaidHostedClient())
2021-07-20 06:49:04 +02:00
{
$invoice->invitations->each(function ($invitation) use ($invoice, $reminder_template) {
EmailEntity::dispatch($invitation, $invitation->company, $reminder_template);
nlog("Firing reminder email for invoice {$invoice->number}");
});
2021-07-20 06:49:04 +02:00
if ($invoice->invitations->count() > 0) {
event(new InvoiceWasEmailed($invoice->invitations->first(), $invoice->company, Ninja::eventVars(), $reminder_template));
}
2020-11-25 15:19:52 +01:00
}
2021-05-26 02:35:39 +02:00
$invoice->service()->setReminder()->save();
2021-05-25 23:31:17 +02:00
} else {
$invoice->next_send_date = null;
$invoice->save();
}
});
}
2021-07-01 01:14:30 +02:00
/**
* Calculates the late if - if any - and rebuilds the invoice
*
* @param Invoice $invoice
* @param string $template
* @return Invoice
*/
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');
break;
default:
$late_fee_amount = 0;
$late_fee_percent = 0;
break;
}
return $this->setLateFee($invoice, $late_fee_amount, $late_fee_percent);
}
/**
* Applies the late fee to the invoice line items
*
* @param Invoice $invoice
* @param float $amount The fee amount
* @param float $percent The fee percentage amount
*
* @return Invoice
*/
private function setLateFee($invoice, $amount, $percent) :Invoice
{
2021-12-14 10:54:23 +01:00
2021-08-19 00:02:28 +02:00
App::forgetInstance('translator');
$t = app('translator');
$t->replace(Ninja::transformTranslations($invoice->client->getMergedSettings()));
2021-12-14 10:54:23 +01:00
App::setLocale($invoice->client->locale());
2021-08-19 00:02:28 +02:00
2021-07-01 01:14:30 +02:00
$temp_invoice_balance = $invoice->balance;
if ($amount <= 0 && $percent <= 0) {
return $invoice;
}
$fee = $amount;
if ($invoice->partial > 0) {
$fee += round($invoice->partial * $percent / 100, 2);
} else {
$fee += round($invoice->balance * $percent / 100, 2);
}
$invoice_item = new InvoiceItem;
$invoice_item->type_id = '5';
$invoice_item->product_key = trans('texts.fee');
$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*/
2021-08-17 06:35:40 +02:00
$invoice->calc()->getInvoice()->save();
$invoice->fresh();
2021-08-11 06:48:54 +02:00
$invoice->service()->deletePdf();
2021-08-21 06:01:32 +02:00
/* Refresh the client here to ensure the balance is fresh */
$client = $invoice->client;
$client = $client->fresh();
2021-08-10 23:48:34 +02:00
nlog("adjusting client balance and invoice balance by ". ($invoice->balance - $temp_invoice_balance));
2021-08-21 06:01:32 +02:00
$client->service()->updateBalance($invoice->balance - $temp_invoice_balance)->save();
2021-07-23 02:31:30 +02:00
$invoice->ledger()->updateInvoiceBalance($invoice->balance - $temp_invoice_balance, "Late Fee Adjustment for invoice {$invoice->number}");
2021-07-01 01:14:30 +02:00
return $invoice;
}
}