Invoice rendering for service, and unit testing for Invoice Item quantity

This commit is contained in:
Deon George
2020-02-06 18:31:43 +09:00
parent 5f5d114f42
commit ebd4367975
14 changed files with 541 additions and 236 deletions

View File

@@ -2,15 +2,18 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Collection;
use Exception;
use Illuminate\Database\Eloquent\Collection as DatabaseCollection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use App\Traits\NextKey;
use Leenooks\Carbon;
class Service extends Model
{
@@ -23,8 +26,11 @@ class Service extends Model
protected $dates = [
'date_last_invoice',
'date_next_invoice'
'date_next_invoice'.
'date_start',
'date_end',
];
public $dateFormat = 'U';
protected $table = 'ab_service';
@@ -303,38 +309,124 @@ class Service extends Model
* Return the date for the next invoice
*
* @todo This function negates the need for date_next_invoice
* @return null
* @return Carbon|string
*/
public function getInvoiceNextAttribute()
{
$last = $this->getInvoiceToAttribute();
$date = $last ? $last->addDay() : now();
$date = $last ? $last->addDay() : Carbon::now();
return request()->wantsJson() ? $date->format('Y-m-d') : $date;
}
/**
* Return the end date for the next invoice
*
* @return mixed
* @throws Exception
*/
public function getInvoiceNextEndAttribute()
{
switch ($this->recur_schedule)
{
switch ($this->recur_schedule) {
// Weekly
case 0: $date = $this->getInvoiceNextAttribute()->addWeek(); break;
case 0: $date = $this->product->price_recurr_strict
? $this->getInvoiceNextAttribute()->endOfWeek()
: $this->getInvoiceNextAttribute()->addWeek()->subDay();
break;
// Monthly
case 1: $date = $this->getInvoiceNextAttribute()->addMonth(); break;
case 1:
$date = $this->product->price_recurr_strict
? $this->getInvoiceNextAttribute()->endOfMonth()
: $this->getInvoiceNextAttribute()->addMonth()->subDay();
break;
// Quarterly
case 2: $date = $this->getInvoiceNextAttribute()->addQuarter(); break;
case 2:
$date = $this->product->price_recurr_strict
? $this->getInvoiceNextAttribute()->endOfQuarter()
: $this->getInvoiceNextAttribute()->addQuarter()->subDay();
break;
// Half Yearly
case 3: $date = $this->getInvoiceNextAttribute()->addQuarter(2); break;
case 3:
$date = $this->product->price_recurr_strict
? $this->getInvoiceNextAttribute()->endOfHalf()
: $this->getInvoiceNextAttribute()->addQuarter(2)->subDay();
break;
// Yearly
case 4: $date = $this->getInvoiceNextAttribute()->addYear(); break;
case 4:
$date = $this->product->price_recurr_strict
? $this->getInvoiceNextAttribute()->endOfYear()
: $this->getInvoiceNextAttribute()->addYear()->subDay();
break;
// Two Yearly
case 5: $date = $this->getInvoiceNextAttribute()->addYear(2); break;
// NOTE: price_recurr_strict ignored
case 5: $date = $this->getInvoiceNextAttribute()->addYear(2)->subDay(); break;
// Three Yearly
case 6: $date = $this->getInvoiceNextAttribute()->addYear(3); break;
default: throw new \Exception('Unknown recur_schedule');
// NOTE: price_recurr_strict ignored
case 6: $date = $this->getInvoiceNextAttribute()->addYear(3)->subDay(); break;
default: throw new Exception('Unknown recur_schedule');
}
return $date->subDay();
return $date;
}
public function getInvoiceNextQuantityAttribute()
{
// If we are not rounding to the first day of the cycle, then it is always a full cycle
if (! $this->product->price_recurr_strict)
return 1;
$n = $this->invoice_next->diff($this->invoice_next_end)->days+1;
switch ($this->recur_schedule) {
// Weekly
case 0:
$d = $this->invoice_next_end->diff($this->invoice_next_end->startOfWeek())->days;
break;
// Monthly
case 1:
$d = $this->invoice_next_end->diff($this->invoice_next_end->startOfMonth())->days;
break;
// Quarterly
case 2:
$d = $this->invoice_next_end->diff($this->invoice_next_end->startOfQuarter())->days;
break;
// Half Yearly
case 3:
$d = $this->invoice_next_end->diff($this->invoice_next_end->startOfHalf())->days;
break;
// Yearly
case 4:
$d = $this->invoice_next_end->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;
break;
// Three Yearly
case 6:
$d = $this->invoice_next_end->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);
}
/**
@@ -541,14 +633,24 @@ class Service extends Model
public function getStatusHTMLAttribute(): string
{
$class = NULL;
switch ($this->status)
{
case 'ACTIVE':
$class = 'badge-success';
break;
}
return sprintf('<span class="badge %s">%s</span>',$class,$this->status);
if ($this->isPending())
$class = 'badge-warning';
else
switch ($this->status)
{
case 'ACTIVE':
$class = 'badge-success';
break;
case 'INACTIVE':
$class = 'badge-danger';
break;
}
return $class
? sprintf('<span class="badge %s">%s</span>',$class,$this->status)
: $this->status;
}
/**
@@ -597,7 +699,7 @@ class Service extends Model
return round($value*1.1,2);
}
public function invoices_due(): Collection
public function invoices_due(): DatabaseCollection
{
$this->load('invoice_items.invoice');
@@ -635,29 +737,41 @@ class Service extends Model
*/
public function isPending(): bool
{
return ! $this->active AND ! in_array($this->order_status,$this->inactive_status);
return ! $this->active
AND ! is_null($this->order_status)
AND ! in_array($this->order_status,array_merge($this->inactive_status,['INACTIVE']));
}
public function next_invoice_items(): \Illuminate\Support\Collection
/**
* Generate a collection of invoice_item objects that will be billed for the next invoice
*
* @return Collection
* @throws Exception
*/
public function next_invoice_items(): Collection
{
$result = collect();
$o = new InvoiceItem;
$o->active = TRUE;
$o->service_id = $this->id;
$o->product_id = $this->product_id;
$o->quantity = 1;
$o->item_type = 0;
$o->price_base = $this->price ?: $this->product->price($this->recur_schedule); // @todo change to a method in this class
$o->recurring_schedule = $this->recur_schedule;
$o->date_start = $this->invoice_next;
$o->date_stop = $this->invoice_next_end;
$o->addTaxes();
$result->push($o);
// If the service is active, there will be service charges
if ($this->active or $this->isPending()) {
$o->active = TRUE;
$o->service_id = $this->id;
$o->product_id = $this->product_id;
$o->item_type = 0;
$o->price_base = $this->price ?: $this->product->price($this->recur_schedule); // @todo change to a method in this class
$o->recurring_schedule = $this->recur_schedule;
$o->date_start = $this->invoice_next;
$o->date_stop = $this->invoice_next_end;
$o->quantity = $this->invoice_next_quantity;
foreach ($this->charges->filter(function($item) { return ! $item->processed; }) as $oo)
{
$o->addTaxes();
$result->push($o);
}
// Add additional charges
foreach ($this->charges->filter(function($item) { return ! $item->processed; }) as $oo) {
$o = new InvoiceItem;
$o->active = TRUE;
$o->service_id = $oo->service_id;