2019-12-18 03:45:18 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-12-18 03:45:18 +01:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-12-18 03:45:18 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-12-18 03:45:18 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Utils\Traits;
|
|
|
|
|
2020-10-27 12:57:12 +01:00
|
|
|
use App\Models\RecurringInvoice;
|
2019-12-18 03:45:18 +01:00
|
|
|
use Illuminate\Support\Carbon;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class MakesReminders.
|
2023-10-26 04:57:44 +02:00
|
|
|
*
|
2019-12-18 03:45:18 +01:00
|
|
|
*/
|
|
|
|
trait MakesReminders
|
|
|
|
{
|
2023-07-27 09:33:55 +02:00
|
|
|
/**
|
|
|
|
* @param string $schedule_reminder
|
|
|
|
* @param string $num_days_reminder
|
|
|
|
* @return ?bool
|
|
|
|
*/
|
2020-02-15 10:01:15 +01:00
|
|
|
public function inReminderWindow($schedule_reminder, $num_days_reminder)
|
|
|
|
{
|
2023-08-06 03:44:52 +02:00
|
|
|
/** @var \App\Models\Invoice | \App\Models\Quote | \App\Models\RecurringInvoice | \App\Models\Credit $this **/
|
2023-07-27 09:33:55 +02:00
|
|
|
$offset = $this->client->timezone_offset();
|
|
|
|
|
2020-02-15 10:01:15 +01:00
|
|
|
switch ($schedule_reminder) {
|
|
|
|
case 'after_invoice_date':
|
2024-06-24 13:10:52 +02:00
|
|
|
return Carbon::parse($this->date)->addDays((int)$num_days_reminder)->startOfDay()->addSeconds($offset)->isSameDay(Carbon::now());
|
2020-02-15 10:01:15 +01:00
|
|
|
case 'before_due_date':
|
2023-09-24 08:51:35 +02:00
|
|
|
$partial_or_due_date = ($this->partial > 0 && isset($this->partial_due_date)) ? $this->partial_due_date : $this->due_date;
|
2024-06-24 13:10:52 +02:00
|
|
|
return Carbon::parse($partial_or_due_date)->subDays((int)$num_days_reminder)->startOfDay()->addSeconds($offset)->isSameDay(Carbon::now());
|
2020-02-15 10:01:15 +01:00
|
|
|
case 'after_due_date':
|
2023-09-24 08:51:35 +02:00
|
|
|
$partial_or_due_date = ($this->partial > 0 && isset($this->partial_due_date)) ? $this->partial_due_date : $this->due_date;
|
2024-06-24 13:10:52 +02:00
|
|
|
return Carbon::parse($partial_or_due_date)->addDays((int)$num_days_reminder)->startOfDay()->addSeconds($offset)->isSameDay(Carbon::now());
|
2020-02-15 10:01:15 +01:00
|
|
|
default:
|
2020-04-15 02:30:52 +02:00
|
|
|
return null;
|
2020-02-15 10:01:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-27 12:57:12 +01:00
|
|
|
public function calculateTemplate(string $entity_string): string
|
2020-02-15 10:01:15 +01:00
|
|
|
{
|
2023-08-06 03:44:52 +02:00
|
|
|
|
2023-08-06 04:20:18 +02:00
|
|
|
/** @var \App\Models\Invoice | \App\Models\Quote | \App\Models\RecurringInvoice | \App\Models\Credit $this **/
|
2020-02-15 12:49:31 +01:00
|
|
|
$client = $this->client;
|
2020-02-15 10:01:15 +01:00
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
if ($entity_string != 'invoice') {
|
2020-11-04 10:32:49 +01:00
|
|
|
return $entity_string;
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2020-11-04 10:32:49 +01:00
|
|
|
|
2020-11-04 23:48:46 +01:00
|
|
|
if ($this->inReminderWindow(
|
2020-03-21 06:37:30 +01:00
|
|
|
$client->getSetting('schedule_reminder1'),
|
|
|
|
$client->getSetting('num_days_reminder1')
|
2022-06-21 11:57:17 +02:00
|
|
|
) && ! $this->reminder1_sent) {
|
2020-07-08 05:17:25 +02:00
|
|
|
return 'reminder1';
|
2020-11-04 23:48:46 +01:00
|
|
|
} elseif ($this->inReminderWindow(
|
2020-03-21 06:37:30 +01:00
|
|
|
$client->getSetting('schedule_reminder2'),
|
|
|
|
$client->getSetting('num_days_reminder2')
|
2022-06-21 11:57:17 +02:00
|
|
|
) && ! $this->reminder2_sent) {
|
2020-07-08 05:17:25 +02:00
|
|
|
return 'reminder2';
|
2020-11-04 23:48:46 +01:00
|
|
|
} elseif ($this->inReminderWindow(
|
2020-03-21 06:37:30 +01:00
|
|
|
$client->getSetting('schedule_reminder3'),
|
|
|
|
$client->getSetting('num_days_reminder3')
|
2022-06-21 11:57:17 +02:00
|
|
|
) && ! $this->reminder3_sent) {
|
2020-07-08 05:17:25 +02:00
|
|
|
return 'reminder3';
|
2020-11-25 15:19:52 +01:00
|
|
|
} elseif ($this->checkEndlessReminder(
|
2021-06-10 03:15:21 +02:00
|
|
|
$this->reminder_last_sent,
|
2020-10-27 12:57:12 +01:00
|
|
|
$client->getSetting('endless_reminder_frequency_id')
|
2020-11-25 15:19:52 +01:00
|
|
|
)) {
|
2020-10-27 12:57:12 +01:00
|
|
|
return 'endless_reminder';
|
2020-11-25 15:19:52 +01:00
|
|
|
} else {
|
2020-10-27 12:57:12 +01:00
|
|
|
return $entity_string;
|
2020-02-15 10:01:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-10-27 12:57:12 +01:00
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
private function checkEndlessReminder($last_sent_date, $endless_reminder_frequency_id): bool
|
2022-06-21 11:57:17 +02:00
|
|
|
{
|
2024-07-22 02:26:03 +02:00
|
|
|
$interval = $this->addTimeInterval($last_sent_date, $endless_reminder_frequency_id);
|
|
|
|
|
2024-08-22 08:45:06 +02:00
|
|
|
if(is_null($interval)) {
|
2024-06-22 06:53:45 +02:00
|
|
|
return false;
|
2024-08-22 08:45:06 +02:00
|
|
|
}
|
|
|
|
|
2024-07-22 02:26:03 +02:00
|
|
|
if (Carbon::now()->startOfDay()->eq($interval)) {
|
2020-10-27 12:57:12 +01:00
|
|
|
return true;
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2020-10-27 12:57:12 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
private function addTimeInterval($date, $endless_reminder_frequency_id): ?Carbon
|
2022-06-21 11:57:17 +02:00
|
|
|
{
|
|
|
|
if (! $date) {
|
2020-10-27 12:57:12 +01:00
|
|
|
return null;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
|
|
|
|
2020-10-27 12:57:12 +01:00
|
|
|
switch ($endless_reminder_frequency_id) {
|
2021-06-10 03:15:21 +02:00
|
|
|
case RecurringInvoice::FREQUENCY_DAILY:
|
2022-06-21 11:57:17 +02:00
|
|
|
return Carbon::parse($date)->addDay()->startOfDay();
|
2023-02-16 02:36:09 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2019-12-18 03:45:18 +01:00
|
|
|
}
|