Fix billing calculations when services have an end date

This commit is contained in:
Deon George 2020-07-06 13:56:23 +10:00
parent d7829b93b9
commit 39a230f94c
No known key found for this signature in database
GPG Key ID: 7670E8DC27415254

View File

@ -530,7 +530,7 @@ class Service extends Model
}
// If the invoice has an end date, our invoice period shouldnt be greater than that.
if ($this->date_end AND $date > $this->date_end)
if ($this->date_end AND $this->date_end < $date)
$date = $this->date_end;
return $date;
@ -547,45 +547,42 @@ class Service extends Model
switch ($this->recur_schedule) {
// Weekly
case 0:
$d = $this->invoice_next_end->diff($this->invoice_next_end->startOfWeek())->days;
$d = $this->invoice_next->addWeek()->diff($this->invoice_next_end->startOfWeek())->days;
break;
// Monthly
case 1:
$d = $this->invoice_next_end->diff($this->invoice_next_end->startOfMonth())->days;
$d = $this->invoice_next->addMonth()->diff($this->invoice_next_end->startOfMonth())->days;
break;
// Quarterly
case 2:
$d = $this->invoice_next_end->diff($this->invoice_next_end->startOfQuarter())->days;
$d = $this->invoice_next->addQuarter()->diff($this->invoice_next_end->startOfQuarter())->days;
break;
// Half Yearly
case 3:
$d = $this->invoice_next_end->diff($this->invoice_next_end->startOfHalf())->days;
$d = $this->invoice_next->addHalf()->diff($this->invoice_next_end->startOfHalf())->days;
break;
// Yearly
case 4:
$d = $this->invoice_next_end->diff($this->invoice_next_end->startOfYear())->days;
$d = $this->invoice_next->addYear()->diff($this->invoice_next_end->startOfYear())->days;
break;
// Two Yearly
case 5:
$d = $this->invoice_next_end->diff($this->invoice_next_end->subyear(2))->days-1;
$d = $this->invoice_next->addYear(2)->diff($this->invoice_next_end->subyear(2))->days-1;
break;
// Three Yearly
case 6:
$d = $this->invoice_next_end->diff($this->invoice_next_end->subyear(3))->days-1;
$d = $this->invoice_next->addYear(3)->diff($this->invoice_next_end->subyear(3))->days-1;
break;
default: throw new Exception('Unknown recur_schedule');
}
// Include the start date and end date.
$d += 1;
return round($n/$d,2);
}