2021-05-26 02:35:39 +02:00
|
|
|
<?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
|
2021-05-26 02:35:39 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\Invoice;
|
|
|
|
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Services\AbstractService;
|
|
|
|
use Carbon\Carbon;
|
|
|
|
|
|
|
|
class UpdateReminder extends AbstractService
|
|
|
|
{
|
|
|
|
public $invoice;
|
|
|
|
|
|
|
|
public $settings;
|
|
|
|
|
2021-05-26 04:37:16 +02:00
|
|
|
public function __construct(Invoice $invoice, $settings = null)
|
2021-05-26 02:35:39 +02:00
|
|
|
{
|
|
|
|
$this->invoice = $invoice;
|
|
|
|
$this->settings = $settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
|
|
|
|
if (! $this->settings) {
|
|
|
|
$this->settings = $this->invoice->client->getMergedSettings();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $this->invoice->isPayable()) {
|
|
|
|
$this->invoice->next_send_date = null;
|
|
|
|
$this->invoice->save();
|
|
|
|
|
2021-05-26 03:32:01 +02:00
|
|
|
return $this->invoice; //exit early
|
2021-05-26 02:35:39 +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) &&
|
|
|
|
$this->settings->schedule_reminder1 == 'after_invoice_date' &&
|
|
|
|
$this->settings->num_days_reminder1 > 0) {
|
2021-06-11 00:20:46 +02:00
|
|
|
$reminder_date = Carbon::parse($this->invoice->date)->startOfDay()->addDays($this->settings->num_days_reminder1)->addSeconds($offset);
|
2021-05-26 02:35:39 +02:00
|
|
|
|
|
|
|
if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date)));
|
2021-06-11 00:20:46 +02:00
|
|
|
$date_collection->push($reminder_date);
|
2021-05-26 02:35:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($this->invoice->reminder1_sent) &&
|
|
|
|
$this->settings->schedule_reminder1 == 'before_due_date' &&
|
|
|
|
$this->settings->num_days_reminder1 > 0) {
|
2021-06-11 00:20:46 +02:00
|
|
|
$reminder_date = Carbon::parse($this->invoice->due_date)->startOfDay()->subDays($this->settings->num_days_reminder1)->addSeconds($offset);
|
2021-05-26 02:35:39 +02:00
|
|
|
|
|
|
|
if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date)));
|
2021-06-11 00:20:46 +02:00
|
|
|
$date_collection->push($reminder_date);
|
2021-05-26 02:35:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($this->invoice->reminder1_sent) &&
|
|
|
|
$this->settings->schedule_reminder1 == 'after_due_date' &&
|
|
|
|
$this->settings->num_days_reminder1 > 0) {
|
2021-06-11 00:20:46 +02:00
|
|
|
$reminder_date = Carbon::parse($this->invoice->due_date)->startOfDay()->addDays($this->settings->num_days_reminder1)->addSeconds($offset);
|
2021-05-26 02:35:39 +02:00
|
|
|
|
|
|
|
if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date)));
|
2021-06-11 00:20:46 +02:00
|
|
|
$date_collection->push($reminder_date);
|
2021-05-26 02:35:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($this->invoice->reminder2_sent) &&
|
|
|
|
$this->settings->schedule_reminder2 == 'after_invoice_date' &&
|
|
|
|
$this->settings->num_days_reminder2 > 0) {
|
2021-06-11 00:20:46 +02:00
|
|
|
$reminder_date = Carbon::parse($this->invoice->date)->startOfDay()->addDays($this->settings->num_days_reminder2)->addSeconds($offset);
|
2021-05-26 02:35:39 +02:00
|
|
|
|
|
|
|
if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date)));
|
2021-06-11 00:20:46 +02:00
|
|
|
$date_collection->push($reminder_date);
|
2021-05-26 02:35:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($this->invoice->reminder2_sent) &&
|
|
|
|
$this->settings->schedule_reminder2 == 'before_due_date' &&
|
|
|
|
$this->settings->num_days_reminder2 > 0) {
|
2021-06-11 00:20:46 +02:00
|
|
|
$reminder_date = Carbon::parse($this->invoice->due_date)->startOfDay()->subDays($this->settings->num_days_reminder2)->addSeconds($offset);
|
2021-05-26 02:35:39 +02:00
|
|
|
|
|
|
|
if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date)));
|
2021-06-11 00:20:46 +02:00
|
|
|
$date_collection->push($reminder_date);
|
2021-05-26 02:35:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($this->invoice->reminder2_sent) &&
|
|
|
|
$this->settings->schedule_reminder2 == 'after_due_date' &&
|
|
|
|
$this->settings->num_days_reminder2 > 0) {
|
2021-06-11 00:20:46 +02:00
|
|
|
$reminder_date = Carbon::parse($this->invoice->due_date)->startOfDay()->addDays($this->settings->num_days_reminder2)->addSeconds($offset);
|
2021-05-26 02:35:39 +02:00
|
|
|
|
|
|
|
if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date)));
|
2021-06-11 00:20:46 +02:00
|
|
|
$date_collection->push($reminder_date);
|
2021-05-26 02:35:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($this->invoice->reminder3_sent) &&
|
|
|
|
$this->settings->schedule_reminder3 == 'after_invoice_date' &&
|
|
|
|
$this->settings->num_days_reminder3 > 0) {
|
2021-06-11 00:20:46 +02:00
|
|
|
$reminder_date = Carbon::parse($this->invoice->date)->startOfDay()->addDays($this->settings->num_days_reminder3)->addSeconds($offset);
|
2021-05-26 02:35:39 +02:00
|
|
|
|
|
|
|
if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date)));
|
2021-06-11 00:20:46 +02:00
|
|
|
$date_collection->push($reminder_date);
|
2021-05-26 02:35:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($this->invoice->reminder3_sent) &&
|
|
|
|
$this->settings->schedule_reminder3 == 'before_due_date' &&
|
|
|
|
$this->settings->num_days_reminder3 > 0) {
|
2021-06-11 00:20:46 +02:00
|
|
|
$reminder_date = Carbon::parse($this->invoice->due_date)->startOfDay()->subDays($this->settings->num_days_reminder3)->addSeconds($offset);
|
2021-05-26 02:35:39 +02:00
|
|
|
|
|
|
|
if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date)));
|
2021-06-11 00:20:46 +02:00
|
|
|
$date_collection->push($reminder_date);
|
2021-05-26 02:35:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($this->invoice->reminder3_sent) &&
|
|
|
|
$this->settings->schedule_reminder3 == 'after_due_date' &&
|
|
|
|
$this->settings->num_days_reminder3 > 0) {
|
2021-06-11 00:20:46 +02:00
|
|
|
$reminder_date = Carbon::parse($this->invoice->due_date)->startOfDay()->addDays($this->settings->num_days_reminder3)->addSeconds($offset);
|
2021-05-26 02:35:39 +02:00
|
|
|
|
|
|
|
if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date)));
|
2021-06-11 00:20:46 +02:00
|
|
|
$date_collection->push($reminder_date);
|
2021-05-26 02:35:39 +02:00
|
|
|
}
|
|
|
|
|
2021-06-15 03:09:12 +02:00
|
|
|
if($date_collection->count() >=1 && $date_collection->sort()->first()->gte(now()))
|
2021-06-15 00:06:01 +02:00
|
|
|
$this->invoice->next_send_date = $date_collection->sort()->first();
|
|
|
|
else
|
|
|
|
$this->invoice->next_send_date = null;
|
|
|
|
|
2021-05-26 02:35:39 +02:00
|
|
|
return $this->invoice;
|
|
|
|
}
|
|
|
|
}
|