From 25459797db3f2382c0404ea8923e6d1d86c586d2 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 1 Jul 2021 09:14:30 +1000 Subject: [PATCH] Add late fees back into reminders --- app/Http/Controllers/PaymentController.php | 3 - app/Jobs/Ninja/SendReminders.php | 1 + app/Jobs/Util/ReminderJob.php | 91 +++++++++++++++++++++- app/Repositories/PaymentRepository.php | 2 +- app/Services/Payment/PaymentService.php | 2 +- 5 files changed, 93 insertions(+), 6 deletions(-) diff --git a/app/Http/Controllers/PaymentController.php b/app/Http/Controllers/PaymentController.php index 3e5b0908b584..e0ab42bdc038 100644 --- a/app/Http/Controllers/PaymentController.php +++ b/app/Http/Controllers/PaymentController.php @@ -208,9 +208,6 @@ class PaymentController extends BaseController { $payment = $this->payment_repo->save($request->all(), PaymentFactory::create(auth()->user()->company()->id, auth()->user()->id)); - if($request->has('email_receipt') && $request->input('email_receipt') == 'true' && !$payment->client->getSetting('client_manual_payment_notification')) - $payment->service()->sendEmail(); - return $this->itemResponse($payment); } diff --git a/app/Jobs/Ninja/SendReminders.php b/app/Jobs/Ninja/SendReminders.php index 46b02dd589b5..cba282e06d27 100644 --- a/app/Jobs/Ninja/SendReminders.php +++ b/app/Jobs/Ninja/SendReminders.php @@ -29,6 +29,7 @@ use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Carbon; +//@DEPRECATED class SendReminders implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, MakesDates, MakesReminders; diff --git a/app/Jobs/Util/ReminderJob.php b/app/Jobs/Util/ReminderJob.php index 7097b7f87375..dd0b1d521764 100644 --- a/app/Jobs/Util/ReminderJob.php +++ b/app/Jobs/Util/ReminderJob.php @@ -11,11 +11,13 @@ namespace App\Jobs\Util; +use App\DataMapper\InvoiceItem; use App\Events\Invoice\InvoiceWasEmailed; use App\Jobs\Entity\EmailEntity; use App\Libraries\MultiDB; use App\Models\Invoice; use App\Utils\Ninja; +use App\Utils\Traits\MakesDates; use App\Utils\Traits\MakesReminders; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; @@ -26,7 +28,7 @@ use Illuminate\Support\Carbon; class ReminderJob implements ShouldQueue { - use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, MakesReminders; + use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, MakesReminders, MakesDates; public function __construct() { @@ -65,6 +67,8 @@ class ReminderJob implements ShouldQueue if ($invoice->isPayable()) { $reminder_template = $invoice->calculateTemplate('invoice'); $invoice->service()->touchReminder($reminder_template)->save(); + + $invoice = $this->calcLateFee($invoice, $reminder_template); $invoice->invitations->each(function ($invitation) use ($invoice, $reminder_template) { EmailEntity::dispatch($invitation, $invitation->company, $reminder_template); @@ -84,4 +88,89 @@ class ReminderJob implements ShouldQueue }); } + + + /** + * 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 + { + $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*/ + $invoice = $invoice->calc()->getInvoice(); + + $invoice->client->service()->updateBalance($this->invoice->balance - $temp_invoice_balance)->save(); + $invoice->ledger()->updateInvoiceBalance($this->invoice->balance - $temp_invoice_balance, "Late Fee Adjustment for invoice {$this->invoice->number}"); + + return $invoice; + } } diff --git a/app/Repositories/PaymentRepository.php b/app/Repositories/PaymentRepository.php index e17cff099658..55c77be9748f 100644 --- a/app/Repositories/PaymentRepository.php +++ b/app/Repositories/PaymentRepository.php @@ -157,7 +157,7 @@ class PaymentRepository extends BaseRepository { if ( ! $is_existing_payment && ! $this->import_mode ) { - if ($payment->client->getSetting('client_manual_payment_notification')) + if (array_key_exists('email_receipt', $data) && $data['email_receipt'] == true) $payment->service()->sendEmail(); event( new PaymentWasCreated( $payment, $payment->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null) ) ); diff --git a/app/Services/Payment/PaymentService.php b/app/Services/Payment/PaymentService.php index f6aba1e7f4ac..b6f7d3af7225 100644 --- a/app/Services/Payment/PaymentService.php +++ b/app/Services/Payment/PaymentService.php @@ -49,7 +49,7 @@ class PaymentService } public function sendEmail($contact = null) - { + {nlog("merp"); return (new SendEmail($this->payment, $contact))->run(); }