2019-12-18 03:45:18 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2020-01-07 01:13:47 +01:00
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-12-18 03:45:18 +01:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Utils\Traits;
|
|
|
|
|
|
|
|
use Illuminate\Support\Carbon;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class MakesReminders.
|
|
|
|
*/
|
|
|
|
trait MakesReminders
|
|
|
|
{
|
|
|
|
public function setReminder($settings = null)
|
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
if (!$settings) {
|
2019-12-18 03:45:18 +01:00
|
|
|
$settings = $this->client->getMergedSettings();
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-12-18 03:45:18 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if (!$this->isPayable()) {
|
2019-12-18 03:45:18 +01:00
|
|
|
$this->next_send_date = null;
|
|
|
|
$this->save();
|
|
|
|
return; //exit early
|
|
|
|
}
|
|
|
|
|
|
|
|
$nsd = null;
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($settings->enable_reminder1 !== false &&
|
2019-12-18 03:45:18 +01:00
|
|
|
$settings->schedule_reminder1 == 'after_invoice_date' &&
|
2019-12-30 22:59:12 +01:00
|
|
|
$settings->num_days_reminder1 > 0) {
|
2019-12-18 03:45:18 +01:00
|
|
|
$reminder_date = Carbon::parse($this->date)->addDays($settings->num_days_reminder1);
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if (!$nsd) {
|
2019-12-18 03:45:18 +01:00
|
|
|
$nsd = $reminder_date;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-12-18 03:45:18 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($reminder_date->lt($nsd)) {
|
2019-12-18 03:45:18 +01:00
|
|
|
$nsd = $reminder_date;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-12-18 03:45:18 +01:00
|
|
|
}
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($settings->enable_reminder1 !== false &&
|
2019-12-18 03:45:18 +01:00
|
|
|
$settings->schedule_reminder1 == 'before_due_date' &&
|
2019-12-30 22:59:12 +01:00
|
|
|
$settings->num_days_reminder1 > 0) {
|
2019-12-18 03:45:18 +01:00
|
|
|
$reminder_date = Carbon::parse($this->due_date)->subDays($settings->num_days_reminder1);
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if (!$nsd) {
|
2019-12-18 03:45:18 +01:00
|
|
|
$nsd = $reminder_date;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-12-18 03:45:18 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($reminder_date->lt($nsd)) {
|
2019-12-18 03:45:18 +01:00
|
|
|
$nsd = $reminder_date;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-12-18 03:45:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($settings->enable_reminder1 !== false &&
|
2019-12-18 03:45:18 +01:00
|
|
|
$settings->schedule_reminder1 == 'after_due_date' &&
|
2019-12-30 22:59:12 +01:00
|
|
|
$settings->num_days_reminder1 > 0) {
|
2019-12-18 03:45:18 +01:00
|
|
|
$reminder_date = Carbon::parse($this->due_date)->addDays($settings->num_days_reminder1);
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if (!$nsd) {
|
2019-12-18 03:45:18 +01:00
|
|
|
$nsd = $reminder_date;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-12-18 03:45:18 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($reminder_date->lt($nsd)) {
|
2019-12-18 03:45:18 +01:00
|
|
|
$nsd = $reminder_date;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-12-18 03:45:18 +01:00
|
|
|
}
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($settings->enable_reminder2 !== false &&
|
2019-12-18 03:45:18 +01:00
|
|
|
$settings->schedule_reminder2 == 'after_invoice_date' &&
|
2019-12-30 22:59:12 +01:00
|
|
|
$settings->num_days_reminder2 > 0) {
|
2019-12-18 03:45:18 +01:00
|
|
|
$reminder_date = Carbon::parse($this->date)->addDays($settings->num_days_reminder2);
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if (!$nsd) {
|
2019-12-18 03:45:18 +01:00
|
|
|
$nsd = $reminder_date;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-12-18 03:45:18 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($reminder_date->lt($nsd)) {
|
2019-12-18 03:45:18 +01:00
|
|
|
$nsd = $reminder_date;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-12-18 03:45:18 +01:00
|
|
|
}
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($settings->enable_reminder2 !== false &&
|
2019-12-18 03:45:18 +01:00
|
|
|
$settings->schedule_reminder2 == 'before_due_date' &&
|
2019-12-30 22:59:12 +01:00
|
|
|
$settings->num_days_reminder2 > 0) {
|
2019-12-18 03:45:18 +01:00
|
|
|
$reminder_date = Carbon::parse($this->due_date)->subDays($settings->num_days_reminder2);
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if (!$nsd) {
|
2019-12-18 03:45:18 +01:00
|
|
|
$nsd = $reminder_date;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-12-18 03:45:18 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($reminder_date->lt($nsd)) {
|
2019-12-18 03:45:18 +01:00
|
|
|
$nsd = $reminder_date;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-12-18 03:45:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($settings->enable_reminder2 !== false &&
|
2019-12-18 03:45:18 +01:00
|
|
|
$settings->schedule_reminder2 == 'after_due_date' &&
|
2019-12-30 22:59:12 +01:00
|
|
|
$settings->num_days_reminder2 > 0) {
|
2019-12-18 03:45:18 +01:00
|
|
|
$reminder_date = Carbon::parse($this->due_date)->addDays($settings->num_days_reminder2);
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if (!$nsd) {
|
2019-12-18 03:45:18 +01:00
|
|
|
$nsd = $reminder_date;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-12-18 03:45:18 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($reminder_date->lt($nsd)) {
|
2019-12-18 03:45:18 +01:00
|
|
|
$nsd = $reminder_date;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-12-18 03:45:18 +01:00
|
|
|
}
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($settings->enable_reminder3 !== false &&
|
2019-12-18 03:45:18 +01:00
|
|
|
$settings->schedule_reminder3 == 'after_invoice_date' &&
|
2019-12-30 22:59:12 +01:00
|
|
|
$settings->num_days_reminder3 > 0) {
|
2019-12-18 03:45:18 +01:00
|
|
|
$reminder_date = Carbon::parse($this->date)->addDays($settings->num_days_reminder3);
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if (!$nsd) {
|
2019-12-18 03:45:18 +01:00
|
|
|
$nsd = $reminder_date;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-12-18 03:45:18 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($reminder_date->lt($nsd)) {
|
2019-12-18 03:45:18 +01:00
|
|
|
$nsd = $reminder_date;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-12-18 03:45:18 +01:00
|
|
|
}
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($settings->enable_reminder3 !== false &&
|
2019-12-18 03:45:18 +01:00
|
|
|
$settings->schedule_reminder3 == 'before_due_date' &&
|
2019-12-30 22:59:12 +01:00
|
|
|
$settings->num_days_reminder3 > 0) {
|
2019-12-18 03:45:18 +01:00
|
|
|
$reminder_date = Carbon::parse($this->due_date)->subDays($settings->num_days_reminder3);
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if (!$nsd) {
|
2019-12-18 03:45:18 +01:00
|
|
|
$nsd = $reminder_date;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-12-18 03:45:18 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($reminder_date->lt($nsd)) {
|
2019-12-18 03:45:18 +01:00
|
|
|
$nsd = $reminder_date;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-12-18 03:45:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($settings->enable_reminder3 !== false &&
|
2019-12-18 03:45:18 +01:00
|
|
|
$settings->schedule_reminder3 == 'after_due_date' &&
|
2019-12-30 22:59:12 +01:00
|
|
|
$settings->num_days_reminder3 > 0) {
|
2019-12-18 03:45:18 +01:00
|
|
|
$reminder_date = Carbon::parse($this->due_date)->addDays($settings->num_days_reminder3);
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if (!$nsd) {
|
2019-12-18 03:45:18 +01:00
|
|
|
$nsd = $reminder_date;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-12-18 03:45:18 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($reminder_date->lt($nsd)) {
|
2019-12-18 03:45:18 +01:00
|
|
|
$nsd = $reminder_date;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-12-18 03:45:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->next_send_date = $nsd;
|
|
|
|
$this->save();
|
|
|
|
}
|
|
|
|
}
|