More Service display

This commit is contained in:
Deon George
2019-07-02 15:28:27 +10:00
parent 6103b61265
commit 59a8ef2476
14 changed files with 311 additions and 14 deletions

View File

@@ -2,7 +2,6 @@
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
@@ -20,9 +19,9 @@ class Service extends Model
public $incrementing = FALSE;
protected $table = 'ab_service';
protected $with = ['product.descriptions','account.language'];
protected $dates = ['date_last_invoice','date_next_invoice'];
public $dateFormat = 'U';
protected $with = ['product.descriptions','account.language','type'];
protected $casts = [
'order_info'=>'array',
@@ -97,9 +96,23 @@ class Service extends Model
return $this->belongsTo(Account::class);
}
/**
* Return Charges associated with this Service
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function charges()
{
return $this->hasMany(Charge::class)
->where('active','=',TRUE)
->orderBy('date_orig');
}
// @todo changed to invoiced_items
public function invoice_items($active=TRUE)
{
$query = $this->hasMany(InvoiceItem::class)
->where('item_type','=',0)
->orderBy('date_orig');
if ($active)
@@ -131,6 +144,7 @@ class Service extends Model
/**
* Account that ordered the service
*
* @todo changed to orderedby
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function orderby()
@@ -215,11 +229,7 @@ class Service extends Model
public function getBillingPriceAttribute(): float
{
if ($this->price)
return $this->addtax($this->price);
dd($this->product->price_group,$this);
return $this->cost;
return $this->addTax($this->price ?: $this->product->price($this->recur_schedule));
}
/**
@@ -248,11 +258,36 @@ class Service extends Model
* @todo This function negates the need for date_next_invoice
* @return null
*/
public function getInvoiceNextAttribute(): Carbon
public function getInvoiceNextAttribute()
{
$last = $this->getInvoiceToAttribute();
$date = $last ? $last->addDay() : now();
return $last ? $last->addDay() : now();
return request()->wantsJson() ? $date->format('Y-m-d') : $date;
}
public function getInvoiceNextEndAttribute()
{
switch ($this->recur_schedule)
{
// Weekly
case 0: $date = $this->getInvoiceNextAttribute()->addWeek(); break;
// Monthly
case 1: $date = $this->getInvoiceNextAttribute()->addMonth(); break;
// Quarterly
case 2: $date = $this->getInvoiceNextAttribute()->addQuarter(); break;
// Half Yearly
case 3: $date = $this->getInvoiceNextAttribute()->addQuarter(2); break;
// Yearly
case 4: $date = $this->getInvoiceNextAttribute()->addYear(); break;
// Two Yearly
case 5: $date = $this->getInvoiceNextAttribute()->addYear(2); break;
// Three Yearly
case 6: $date = $this->getInvoiceNextAttribute()->addYear(3); break;
default: throw new \Exception('Unknown recur_schedule');
}
return $date->subDay();
}
/**
@@ -263,6 +298,11 @@ class Service extends Model
return $this->invoice_items->count() ? $this->invoice_items->last()->date_stop : NULL;
}
public function getNameAttribute(): string
{
return $this->product->name_short.': '.$this->getNameShortAttribute();
}
/**
* Return the short name for the service.
*
@@ -448,6 +488,8 @@ class Service extends Model
public function invoices_due(): Collection
{
$this->load('invoice_items.invoice');
return $this->invoice_items->filter(function($item) {
return $item->invoice->due > 0;
});
@@ -464,6 +506,28 @@ class Service extends Model
return $this->active OR ($this->order_status AND ! in_array($this->order_status,$this->inactive_status));
}
public function next_invoice_items()
{
$result = collect();
$result->push([
'item'=>0,
'desc'=>sprintf('Product/Service [%s->%s]',
$this->invoice_next->format('Y-m-d'),
$this->invoice_next_end->format('Y-m-d')),
'amt'=>$this->getBillingPriceAttribute()]);
foreach ($this->charges->filter(function($item) { return ! $item->processed; }) as $o)
{
$result->push([
'item'=>5, // @tod Charges
'desc'=>sprintf('%d@%3.2f - %s',$o->quantity,$this->addTax($o->amount),$o->name),
'amt'=>$this->addTax($o->amount*$o->quantity)]);
}
return $result;
}
/**
* @todo
* @param string $status