2021-05-26 02:35:39 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @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)
|
2021-05-26 02:35:39 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2021-05-26 02:35:39 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\Invoice;
|
|
|
|
|
|
|
|
use App\Models\Invoice;
|
2024-06-21 11:40:56 +02:00
|
|
|
use App\Models\Quote;
|
2021-06-23 06:55:12 +02:00
|
|
|
use App\Models\RecurringInvoice;
|
2021-05-26 02:35:39 +02:00
|
|
|
use App\Services\AbstractService;
|
|
|
|
use Carbon\Carbon;
|
|
|
|
|
|
|
|
class UpdateReminder extends AbstractService
|
|
|
|
{
|
2024-06-24 01:52:35 +02:00
|
|
|
public function __construct(public Invoice $invoice, public mixed $settings = null)
|
2021-05-26 02:35:39 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-10-12 03:55:11 +02:00
|
|
|
/* We only support setting reminders based on the due date, not the partial due date */
|
2021-05-26 02:35:39 +02:00
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
if (! $this->settings) {
|
|
|
|
$this->settings = $this->invoice->client->getMergedSettings();
|
|
|
|
}
|
|
|
|
|
2022-05-16 10:53:01 +02:00
|
|
|
if (! $this->invoice->isPayable() || $this->invoice->status_id == Invoice::STATUS_DRAFT) {
|
2021-05-26 02:35:39 +02:00
|
|
|
$this->invoice->next_send_date = null;
|
2021-10-14 08:54:38 +02:00
|
|
|
$this->invoice->saveQuietly();
|
2021-05-26 02:35:39 +02:00
|
|
|
|
2021-05-26 03:32:01 +02:00
|
|
|
return $this->invoice; //exit early
|
2021-05-26 02:35:39 +02:00
|
|
|
}
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($this->invoice->next_send_date) {
|
2021-08-01 00:44:04 +02:00
|
|
|
$this->invoice->next_send_date = null;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-08-01 00:44:04 +02:00
|
|
|
|
2021-06-11 00:20:46 +02:00
|
|
|
$offset = $this->invoice->client->timezone_offset();
|
|
|
|
|
2021-05-26 02:35:39 +02:00
|
|
|
$date_collection = collect();
|
|
|
|
|
|
|
|
if (is_null($this->invoice->reminder1_sent) &&
|
2022-06-09 02:20:18 +02:00
|
|
|
$this->settings->schedule_reminder1 == 'after_invoice_date') {
|
2024-09-22 22:33:30 +02:00
|
|
|
$reminder_date = Carbon::parse($this->invoice->date)->startOfDay()->addDays((int)$this->settings->num_days_reminder1)->addSeconds($offset);
|
2021-05-26 02:35:39 +02:00
|
|
|
|
2023-09-24 06:54:24 +02:00
|
|
|
if ($reminder_date->gt(now())) {
|
2022-06-21 11:57:17 +02:00
|
|
|
$date_collection->push($reminder_date);
|
|
|
|
}
|
2021-05-26 02:35:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($this->invoice->reminder1_sent) &&
|
2023-09-24 06:54:24 +02:00
|
|
|
($this->invoice->partial_due_date || $this->invoice->due_date) &&
|
2022-06-09 02:20:18 +02:00
|
|
|
$this->settings->schedule_reminder1 == 'before_due_date') {
|
2023-09-24 06:54:24 +02:00
|
|
|
$partial_or_due_date = ($this->invoice->partial > 0 && isset($this->invoice->partial_due_date)) ? $this->invoice->partial_due_date : $this->invoice->due_date;
|
2024-09-22 22:33:30 +02:00
|
|
|
$reminder_date = Carbon::parse($partial_or_due_date)->startOfDay()->subDays((int)$this->settings->num_days_reminder1)->addSeconds($offset);
|
2023-10-26 04:57:44 +02:00
|
|
|
// nlog("1. {$reminder_date->format('Y-m-d')}");
|
2021-05-26 02:35:39 +02:00
|
|
|
|
2023-09-24 06:54:24 +02:00
|
|
|
if ($reminder_date->gt(now())) {
|
2022-06-21 11:57:17 +02:00
|
|
|
$date_collection->push($reminder_date);
|
|
|
|
}
|
2021-05-26 02:35:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($this->invoice->reminder1_sent) &&
|
2023-09-24 06:54:24 +02:00
|
|
|
($this->invoice->partial_due_date || $this->invoice->due_date) &&
|
2022-06-09 02:20:18 +02:00
|
|
|
$this->settings->schedule_reminder1 == 'after_due_date') {
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-09-24 06:54:24 +02:00
|
|
|
$partial_or_due_date = ($this->invoice->partial > 0 && isset($this->invoice->partial_due_date)) ? $this->invoice->partial_due_date : $this->invoice->due_date;
|
2024-09-22 22:33:30 +02:00
|
|
|
$reminder_date = Carbon::parse($partial_or_due_date)->startOfDay()->addDays((int)$this->settings->num_days_reminder1)->addSeconds($offset);
|
2023-10-26 04:57:44 +02:00
|
|
|
// nlog("2. {$reminder_date->format('Y-m-d')}");
|
2021-05-26 02:35:39 +02:00
|
|
|
|
2023-09-24 06:54:24 +02:00
|
|
|
if ($reminder_date->gt(now())) {
|
2022-06-21 11:57:17 +02:00
|
|
|
$date_collection->push($reminder_date);
|
|
|
|
}
|
2021-05-26 02:35:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($this->invoice->reminder2_sent) &&
|
2022-06-09 02:20:18 +02:00
|
|
|
$this->settings->schedule_reminder2 == 'after_invoice_date') {
|
2024-09-22 22:33:30 +02:00
|
|
|
$reminder_date = Carbon::parse($this->invoice->date)->startOfDay()->addDays((int)$this->settings->num_days_reminder2)->addSeconds($offset);
|
2021-05-26 02:35:39 +02:00
|
|
|
|
2023-09-24 06:54:24 +02:00
|
|
|
if ($reminder_date->gt(now())) {
|
2022-06-21 11:57:17 +02:00
|
|
|
$date_collection->push($reminder_date);
|
|
|
|
}
|
2021-05-26 02:35:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($this->invoice->reminder2_sent) &&
|
2023-09-24 06:54:24 +02:00
|
|
|
($this->invoice->partial_due_date || $this->invoice->due_date) &&
|
2022-06-09 02:20:18 +02:00
|
|
|
$this->settings->schedule_reminder2 == 'before_due_date') {
|
2021-05-26 02:35:39 +02:00
|
|
|
|
2023-09-24 06:54:24 +02:00
|
|
|
$partial_or_due_date = ($this->invoice->partial > 0 && isset($this->invoice->partial_due_date)) ? $this->invoice->partial_due_date : $this->invoice->due_date;
|
2024-09-22 22:33:30 +02:00
|
|
|
$reminder_date = Carbon::parse($partial_or_due_date)->startOfDay()->subDays((int)$this->settings->num_days_reminder2)->addSeconds($offset);
|
2023-10-26 04:57:44 +02:00
|
|
|
// nlog("3. {$reminder_date->format('Y-m-d')}");
|
2023-09-24 06:54:24 +02:00
|
|
|
|
|
|
|
if ($reminder_date->gt(now())) {
|
2022-05-16 12:38:04 +02:00
|
|
|
$date_collection->push($reminder_date);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-05-26 02:35:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($this->invoice->reminder2_sent) &&
|
2023-09-24 06:54:24 +02:00
|
|
|
($this->invoice->partial_due_date || $this->invoice->due_date) &&
|
2022-06-09 02:20:18 +02:00
|
|
|
$this->settings->schedule_reminder2 == 'after_due_date') {
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-09-24 06:54:24 +02:00
|
|
|
$partial_or_due_date = ($this->invoice->partial > 0 && isset($this->invoice->partial_due_date)) ? $this->invoice->partial_due_date : $this->invoice->due_date;
|
2024-09-22 22:33:30 +02:00
|
|
|
$reminder_date = Carbon::parse($partial_or_due_date)->startOfDay()->addDays((int)$this->settings->num_days_reminder2)->addSeconds($offset);
|
2023-10-26 04:57:44 +02:00
|
|
|
// nlog("4. {$reminder_date->format('Y-m-d')}");
|
2021-05-26 02:35:39 +02:00
|
|
|
|
2023-09-24 06:54:24 +02:00
|
|
|
if ($reminder_date->gt(now())) {
|
2022-06-21 11:57:17 +02:00
|
|
|
$date_collection->push($reminder_date);
|
|
|
|
}
|
2021-05-26 02:35:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($this->invoice->reminder3_sent) &&
|
2022-06-09 02:20:18 +02:00
|
|
|
$this->settings->schedule_reminder3 == 'after_invoice_date') {
|
2024-09-22 22:33:30 +02:00
|
|
|
$reminder_date = Carbon::parse($this->invoice->date)->startOfDay()->addDays((int)$this->settings->num_days_reminder3)->addSeconds($offset);
|
2021-05-26 02:35:39 +02:00
|
|
|
|
2023-09-24 06:54:24 +02:00
|
|
|
if ($reminder_date->gt(now())) {
|
2022-06-21 11:57:17 +02:00
|
|
|
$date_collection->push($reminder_date);
|
|
|
|
}
|
2021-05-26 02:35:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($this->invoice->reminder3_sent) &&
|
2023-09-24 06:54:24 +02:00
|
|
|
($this->invoice->partial_due_date || $this->invoice->due_date) &&
|
2022-06-09 02:20:18 +02:00
|
|
|
$this->settings->schedule_reminder3 == 'before_due_date') {
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-09-24 06:54:24 +02:00
|
|
|
$partial_or_due_date = ($this->invoice->partial > 0 && isset($this->invoice->partial_due_date)) ? $this->invoice->partial_due_date : $this->invoice->due_date;
|
2024-09-22 22:33:30 +02:00
|
|
|
$reminder_date = Carbon::parse($partial_or_due_date)->startOfDay()->subDays((int)$this->settings->num_days_reminder3)->addSeconds($offset);
|
2023-10-26 04:57:44 +02:00
|
|
|
// nlog("5. {$reminder_date->format('Y-m-d')}");
|
2021-05-26 02:35:39 +02:00
|
|
|
|
2023-09-24 06:54:24 +02:00
|
|
|
if ($reminder_date->gt(now())) {
|
2022-06-21 11:57:17 +02:00
|
|
|
$date_collection->push($reminder_date);
|
|
|
|
}
|
2021-05-26 02:35:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($this->invoice->reminder3_sent) &&
|
2023-09-24 06:54:24 +02:00
|
|
|
($this->invoice->partial_due_date || $this->invoice->due_date) &&
|
2022-06-09 02:20:18 +02:00
|
|
|
$this->settings->schedule_reminder3 == 'after_due_date') {
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-09-24 06:54:24 +02:00
|
|
|
$partial_or_due_date = ($this->invoice->partial > 0 && isset($this->invoice->partial_due_date)) ? $this->invoice->partial_due_date : $this->invoice->due_date;
|
2024-09-22 22:33:30 +02:00
|
|
|
$reminder_date = Carbon::parse($partial_or_due_date)->startOfDay()->addDays((int)$this->settings->num_days_reminder3)->addSeconds($offset);
|
2023-10-26 04:57:44 +02:00
|
|
|
// nlog("6. {$reminder_date->format('Y-m-d')}");
|
2021-05-26 02:35:39 +02:00
|
|
|
|
2023-09-24 06:54:24 +02:00
|
|
|
if ($reminder_date->gt(now())) {
|
2022-06-21 11:57:17 +02:00
|
|
|
$date_collection->push($reminder_date);
|
|
|
|
}
|
2021-05-26 02:35:39 +02:00
|
|
|
}
|
|
|
|
|
2021-06-23 06:55:12 +02:00
|
|
|
if ($this->invoice->last_sent_date &&
|
2022-11-07 11:00:21 +01:00
|
|
|
$this->settings->enable_reminder_endless &&
|
2024-10-06 12:13:52 +02:00
|
|
|
(int) $this->settings->endless_reminder_frequency_id > 0 &&
|
2022-12-01 21:30:24 +01:00
|
|
|
($this->invoice->reminder1_sent || $this->settings->schedule_reminder1 == "" || !$this->settings->enable_reminder1) &&
|
|
|
|
($this->invoice->reminder2_sent || $this->settings->schedule_reminder2 == "" || !$this->settings->enable_reminder2) &&
|
|
|
|
($this->invoice->reminder3_sent || $this->settings->schedule_reminder3 == "" || !$this->settings->enable_reminder3)) {
|
2024-09-22 22:33:30 +02:00
|
|
|
$reminder_date = $this->addTimeInterval($this->invoice->last_sent_date, (int) $this->settings->endless_reminder_frequency_id)->addSeconds($offset);
|
2021-06-23 06:55:12 +02:00
|
|
|
|
2024-09-22 22:33:30 +02:00
|
|
|
if ($reminder_date && $reminder_date->gt(now())) {
|
|
|
|
$date_collection->push($reminder_date);
|
2021-07-20 06:49:04 +02:00
|
|
|
}
|
2021-06-23 06:55:12 +02:00
|
|
|
}
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($date_collection->count() >= 1 && $date_collection->sort()->first()->gte(now())) {
|
2024-09-22 22:33:30 +02:00
|
|
|
$this->invoice->next_send_date = $date_collection->sort()->first();
|
2022-06-21 11:57:17 +02:00
|
|
|
} else {
|
2021-06-15 00:06:01 +02:00
|
|
|
$this->invoice->next_send_date = null;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
|
|
|
|
2021-05-26 02:35:39 +02:00
|
|
|
return $this->invoice;
|
|
|
|
}
|
2021-06-23 06:55:12 +02:00
|
|
|
|
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) {
|
2021-06-23 06:55:12 +02:00
|
|
|
return null;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
|
|
|
|
2021-06-23 06:55:12 +02:00
|
|
|
switch ($endless_reminder_frequency_id) {
|
|
|
|
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:
|
2021-06-23 06:55:12 +02:00
|
|
|
return Carbon::parse($date)->addWeek()->startOfDay();
|
|
|
|
case RecurringInvoice::FREQUENCY_TWO_WEEKS:
|
|
|
|
return Carbon::parse($date)->addWeeks(2)->startOfDay();
|
|
|
|
case RecurringInvoice::FREQUENCY_FOUR_WEEKS:
|
|
|
|
return Carbon::parse($date)->addWeeks(4)->startOfDay();
|
|
|
|
case RecurringInvoice::FREQUENCY_MONTHLY:
|
|
|
|
return Carbon::parse($date)->addMonthNoOverflow()->startOfDay();
|
|
|
|
case RecurringInvoice::FREQUENCY_TWO_MONTHS:
|
|
|
|
return Carbon::parse($date)->addMonthsNoOverflow(2)->startOfDay();
|
|
|
|
case RecurringInvoice::FREQUENCY_THREE_MONTHS:
|
|
|
|
return Carbon::parse($date)->addMonthsNoOverflow(3)->startOfDay();
|
|
|
|
case RecurringInvoice::FREQUENCY_FOUR_MONTHS:
|
|
|
|
return Carbon::parse($date)->addMonthsNoOverflow(4)->startOfDay();
|
|
|
|
case RecurringInvoice::FREQUENCY_SIX_MONTHS:
|
|
|
|
return Carbon::parse($date)->addMonthsNoOverflow(6)->startOfDay();
|
|
|
|
case RecurringInvoice::FREQUENCY_ANNUALLY:
|
|
|
|
return Carbon::parse($date)->addYear()->startOfDay();
|
|
|
|
case RecurringInvoice::FREQUENCY_TWO_YEARS:
|
|
|
|
return Carbon::parse($date)->addYears(2)->startOfDay();
|
|
|
|
case RecurringInvoice::FREQUENCY_THREE_YEARS:
|
|
|
|
return Carbon::parse($date)->addYears(3)->startOfDay();
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|