Add interval between remiders sent

This commit is contained in:
David Callizaya 2021-10-30 11:38:57 -04:00
parent 64d84943bd
commit 6118483c4f
1 changed files with 29 additions and 0 deletions

View File

@ -34,6 +34,22 @@ trait InvoiceReminder
return $remindersLimit = (int) core()->getConfigData('sales.invoice_setttings.invoice_reminders.reminders_limit');
}
/**
* Get interval between reminders.
*
* @return string
*/
private function getIntervalBetweenReminders()
{
static $intervalBetweenReminders;
if ($intervalBetweenReminders) {
return $intervalBetweenReminders;
}
return $intervalBetweenReminders = core()->getConfigData('sales.invoice_setttings.invoice_reminders.interval_between_reminders') ?: 'P1D';
}
/**
* Send an Invoice reminder
*
@ -51,6 +67,12 @@ trait InvoiceReminder
$customer = $this->customer;
Mail::queue(new InvoiceOverdueReminder($customer, $this));
$this->reminders++;
// Calculate next reminder date
$date = now();
$interval = $this->getIntervalBetweenReminders();
$date->add($interval);
$date->setTime(0, 0, 0, 0);
$this->next_reminder_date = $date;
$this->save();
}
@ -60,6 +82,13 @@ trait InvoiceReminder
public function scopeInOverdueAndRemindersLimit($query)
{
$query->where('state', '=', 'overdue');
// Filter by next_reminder_date
$query->where(function($query) {
$query->where('next_reminder_at', '<=', now())
->orWhereNull('next_reminder_at');
});
// If the core config have maximum limit of reminders
if ($this->hasOverdueRemindersLimit()) {
$limit = $this->getOverdueRemindersLimit();
return $query->where('reminders', '<', $limit);