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

255 lines
8.6 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Utils\Traits;
2020-10-27 12:57:12 +01:00
use App\Models\RecurringInvoice;
use Illuminate\Support\Carbon;
/**
* Class MakesReminders.
*/
trait MakesReminders
{
public function setReminder($settings = null)
{
if (! $settings) {
$settings = $this->client->getMergedSettings();
}
if (! $this->isPayable()) {
$this->next_send_date = null;
$this->save();
return; //exit early
}
2020-11-05 11:36:55 +01:00
//@TODO buiuld collection, then ->sort()->first
$nsd = null; //abbreviation for next_send_date
2020-11-05 11:14:30 +01:00
if ($settings->schedule_reminder1 == 'after_invoice_date' &&
$settings->num_days_reminder1 > 0) {
$reminder_date = Carbon::parse($this->date)->addDays($settings->num_days_reminder1);
2020-10-28 11:10:49 +01:00
$nsd = $reminder_date->format('Y-m-d');
if ($reminder_date->lt($nsd)) {
$nsd = $reminder_date->format('Y-m-d');
}
}
2020-11-05 11:14:30 +01:00
if ($settings->schedule_reminder1 == 'before_due_date' &&
$settings->num_days_reminder1 > 0) {
$reminder_date = Carbon::parse($this->due_date)->subDays($settings->num_days_reminder1);
if (! $nsd) {
$nsd = $reminder_date->format('Y-m-d');
}
if ($reminder_date->lt($nsd)) {
$nsd = $reminder_date->format('Y-m-d');
}
}
2020-11-05 11:14:30 +01:00
if ($settings->schedule_reminder1 == 'after_due_date' &&
$settings->num_days_reminder1 > 0) {
$reminder_date = Carbon::parse($this->due_date)->addDays($settings->num_days_reminder1);
if (! $nsd) {
$nsd = $reminder_date->format('Y-m-d');
}
if ($reminder_date->lt($nsd)) {
$nsd = $reminder_date->format('Y-m-d');
}
}
2020-11-05 11:14:30 +01:00
if ($settings->schedule_reminder2 == 'after_invoice_date' &&
$settings->num_days_reminder2 > 0) {
$reminder_date = Carbon::parse($this->date)->addDays($settings->num_days_reminder2);
if (! $nsd) {
$nsd = $reminder_date->format('Y-m-d');
}
if ($reminder_date->lt($nsd)) {
$nsd = $reminder_date->format('Y-m-d');
}
}
2020-11-05 11:14:30 +01:00
if ($settings->schedule_reminder2 == 'before_due_date' &&
$settings->num_days_reminder2 > 0) {
$reminder_date = Carbon::parse($this->due_date)->subDays($settings->num_days_reminder2);
if (! $nsd) {
$nsd = $reminder_date->format('Y-m-d');
}
if ($reminder_date->lt($nsd)) {
$nsd = $reminder_date->format('Y-m-d');
}
}
2020-11-05 11:14:30 +01:00
if ($settings->schedule_reminder2 == 'after_due_date' &&
$settings->num_days_reminder2 > 0) {
$reminder_date = Carbon::parse($this->due_date)->addDays($settings->num_days_reminder2);
if (! $nsd) {
$nsd = $reminder_date->format('Y-m-d');
}
if ($reminder_date->lt($nsd)) {
$nsd = $reminder_date->format('Y-m-d');
}
}
2020-11-05 11:14:30 +01:00
if ($settings->schedule_reminder3 == 'after_invoice_date' &&
$settings->num_days_reminder3 > 0) {
$reminder_date = Carbon::parse($this->date)->addDays($settings->num_days_reminder3);
if (! $nsd) {
$nsd = $reminder_date->format('Y-m-d');
}
if ($reminder_date->lt($nsd)) {
$nsd = $reminder_date->format('Y-m-d');
}
}
2020-11-05 11:14:30 +01:00
if ($settings->schedule_reminder3 == 'before_due_date' &&
$settings->num_days_reminder3 > 0) {
$reminder_date = Carbon::parse($this->due_date)->subDays($settings->num_days_reminder3);
if (! $nsd) {
$nsd = $reminder_date->format('Y-m-d');
}
if ($reminder_date->lt($nsd)) {
$nsd = $reminder_date->format('Y-m-d');
}
}
2020-11-05 11:14:30 +01:00
if ($settings->schedule_reminder3 == 'after_due_date' &&
$settings->num_days_reminder3 > 0) {
$reminder_date = Carbon::parse($this->due_date)->addDays($settings->num_days_reminder3);
if (! $nsd) {
$nsd = $reminder_date->format('Y-m-d');
}
if ($reminder_date->lt($nsd)) {
$nsd = $reminder_date->format('Y-m-d');
}
}
$this->next_send_date = $nsd;
$this->save();
}
public function inReminderWindow($schedule_reminder, $num_days_reminder)
{
switch ($schedule_reminder) {
case 'after_invoice_date':
return Carbon::parse($this->date)->addDays($num_days_reminder)->startOfDay()->eq(Carbon::now()->startOfDay());
break;
case 'before_due_date':
return Carbon::parse($this->due_date)->subDays($num_days_reminder)->startOfDay()->eq(Carbon::now()->startOfDay());
break;
case 'after_due_date':
return Carbon::parse($this->due_date)->addDays($num_days_reminder)->startOfDay()->eq(Carbon::now()->startOfDay());
break;
default:
return null;
break;
}
}
2020-10-27 12:57:12 +01:00
public function calculateTemplate(string $entity_string): string
{
//if invoice is currently a draft, or being marked as sent, this will be the initial email
2020-02-15 12:49:31 +01:00
$client = $this->client;
2020-11-04 10:32:49 +01:00
if($entity_string != 'invoice')
return $entity_string;
//if the invoice
if ($this->inReminderWindow(
$client->getSetting('schedule_reminder1'),
$client->getSetting('num_days_reminder1')
)) {
2020-07-08 05:17:25 +02:00
return 'reminder1';
} elseif ($this->inReminderWindow(
$client->getSetting('schedule_reminder2'),
$client->getSetting('num_days_reminder2')
)) {
2020-07-08 05:17:25 +02:00
return 'reminder2';
} elseif ($this->inReminderWindow(
$client->getSetting('schedule_reminder3'),
$client->getSetting('num_days_reminder3')
)) {
2020-07-08 05:17:25 +02:00
return 'reminder3';
} elseif($this->checkEndlessReminder(
2020-10-27 12:57:12 +01:00
$this->last_sent_date,
$client->getSetting('endless_reminder_frequency_id')
)){
return 'endless_reminder';
}
else {
return $entity_string;
}
//also implement endless reminders here
}
2020-10-27 12:57:12 +01:00
private function checkEndlessReminder($last_sent_date, $endless_reminder_frequency_id) :bool
{
2020-11-05 03:59:19 +01:00
if(Carbon::now()->startOfDay()->eq($this->addTimeInterval($last_sent_date, $endless_reminder_frequency_id)))
2020-10-27 12:57:12 +01:00
return true;
return false;
}
2020-11-05 03:59:19 +01:00
private function addTimeInterval($date, $endless_reminder_frequency_id) :?Carbon
2020-10-27 12:57:12 +01:00
{
2020-11-05 03:59:19 +01:00
if(!$date)
2020-10-27 12:57:12 +01:00
return null;
2020-11-05 03:59:19 +01:00
2020-10-27 12:57:12 +01:00
switch ($endless_reminder_frequency_id) {
case RecurringInvoice::FREQUENCY_WEEKLY:
2020-11-05 03:59:19 +01:00
return Carbon::parse($date)->addWeek()->startOfDay();
2020-10-27 12:57:12 +01:00
case RecurringInvoice::FREQUENCY_TWO_WEEKS:
2020-11-05 03:59:19 +01:00
return Carbon::parse($date)->addWeeks(2)->startOfDay();
2020-10-27 12:57:12 +01:00
case RecurringInvoice::FREQUENCY_FOUR_WEEKS:
2020-11-05 03:59:19 +01:00
return Carbon::parse($date)->addWeeks(4)->startOfDay();
2020-10-27 12:57:12 +01:00
case RecurringInvoice::FREQUENCY_MONTHLY:
2020-11-05 03:59:19 +01:00
return Carbon::parse($date)->addMonthNoOverflow()->startOfDay();
2020-10-27 12:57:12 +01:00
case RecurringInvoice::FREQUENCY_TWO_MONTHS:
2020-11-05 03:59:19 +01:00
return Carbon::parse($date)->addMonthsNoOverflow(2)->startOfDay();
2020-10-27 12:57:12 +01:00
case RecurringInvoice::FREQUENCY_THREE_MONTHS:
2020-11-05 03:59:19 +01:00
return Carbon::parse($date)->addMonthsNoOverflow(3)->startOfDay();
2020-10-27 12:57:12 +01:00
case RecurringInvoice::FREQUENCY_FOUR_MONTHS:
2020-11-05 03:59:19 +01:00
return Carbon::parse($date)->addMonthsNoOverflow(4)->startOfDay();
2020-10-27 12:57:12 +01:00
case RecurringInvoice::FREQUENCY_SIX_MONTHS:
2020-11-05 03:59:19 +01:00
return Carbon::parse($date)->addMonthsNoOverflow(6)->startOfDay();
2020-10-27 12:57:12 +01:00
case RecurringInvoice::FREQUENCY_ANNUALLY:
2020-11-05 03:59:19 +01:00
return Carbon::parse($date)->addYear()->startOfDay();
2020-10-27 12:57:12 +01:00
case RecurringInvoice::FREQUENCY_TWO_YEARS:
2020-11-05 03:59:19 +01:00
return Carbon::parse($date)->addYears(2)->startOfDay();
2020-10-27 12:57:12 +01:00
case RecurringInvoice::FREQUENCY_THREE_YEARS:
2020-11-05 03:59:19 +01:00
return Carbon::parse($date)->addYears(3)->startOfDay();
2020-10-27 12:57:12 +01:00
default:
return null;
}
}
}