Fix last day of month recurrence

This commit is contained in:
Hillel Coren 2018-03-13 19:43:39 +02:00
parent 9f5e47cc09
commit e7c61e9c0b
2 changed files with 63 additions and 38 deletions

View File

@ -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
*

View File

@ -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;
}
*/
}