diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 17eb780d8ff8..92aef492f6a1 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -1128,32 +1128,6 @@ class Invoice extends EntityModel implements BalanceAffecting return $dates; } - /** - * @return null - */ - public function getNextSendDate() - { - if (! $this->is_public) { - return null; - } - - if ($this->start_date && ! $this->last_sent_date) { - $startDate = $this->getOriginal('start_date') . ' ' . $this->account->recurring_hour . ':00:00'; - - return $this->account->getDateTime($startDate); - } - - if (! $schedule = $this->getSchedule()) { - return null; - } - - if (count($schedule) < 2) { - return null; - } - - return $schedule[1]->getStart(); - } - /** * @param null $invoice_date * diff --git a/app/Models/Traits/HasRecurrence.php b/app/Models/Traits/HasRecurrence.php index e2620b8d2de6..ebe78533b5cf 100644 --- a/app/Models/Traits/HasRecurrence.php +++ b/app/Models/Traits/HasRecurrence.php @@ -14,6 +14,7 @@ trait HasRecurrence /** * @return bool */ + /* public function shouldSendToday() { if (! $this->user->confirmed) { @@ -78,6 +79,68 @@ trait HasRecurrence return false; } + */ + + public function shouldSendToday() + { + if (! $this->user->confirmed) { + return false; + } + + $account = $this->account; + $timezone = $account->getTimezone(); + + if (! $this->start_date || Carbon::parse($this->start_date, $timezone)->isFuture()) { + return false; + } + + if ($this->end_date && Carbon::parse($this->end_date, $timezone)->isPast()) { + return false; + } + + if (! $this->last_sent_date) { + return true; + } else { + // check we don't send a few hours early due to timezone difference + if (Utils::isNinja() && Carbon::now()->format('Y-m-d') != Carbon::now($timezone)->format('Y-m-d')) { + return false; + } + + $nextSendDate = $this->getNextSendDate(); + + if (! $nextSendDate) { + return false; + } + + return $this->account->getDateTime() >= $nextSendDate; + } + } + + /** + * @return null + */ + public function getNextSendDate() + { + if (! $this->is_public) { + return null; + } + + if ($this->start_date && ! $this->last_sent_date) { + $startDate = $this->getOriginal('start_date') . ' ' . $this->account->recurring_hour . ':00:00'; + + return $this->account->getDateTime($startDate); + } + + if (! $schedule = $this->getSchedule()) { + return null; + } + + if (count($schedule) < 2) { + return null; + } + + return $schedule[1]->getStart(); + } /** * @return string @@ -125,16 +188,4 @@ trait HasRecurrence return $rule; } - - /* - public function shouldSendToday() - { - if (!$nextSendDate = $this->getNextSendDate()) { - return false; - } - - return $this->account->getDateTime() >= $nextSendDate; - } - */ - }