Start of service display

This commit is contained in:
Deon George
2019-06-29 10:14:12 +10:00
parent a426c7b1a4
commit 6103b61265
14 changed files with 426 additions and 33 deletions

View File

@@ -2,6 +2,8 @@
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
@@ -95,6 +97,37 @@ class Service extends Model
return $this->belongsTo(Account::class);
}
public function invoice_items($active=TRUE)
{
$query = $this->hasMany(InvoiceItem::class)
->orderBy('date_orig');
if ($active)
$query->where('active','=',TRUE);
return $query;
}
/**
* Invoices for this service
*
*/
public function invoices($active=TRUE)
{
$query = $this->hasManyThrough(Invoice::class,InvoiceItem::class,NULL,'id',NULL,'invoice_id')
->distinct('id')
->where('ab_invoice.site_id','=',$this->site_id)
->where('ab_invoice_item.site_id','=',$this->site_id)
->orderBy('date_orig')
->orderBy('due_date');
if ($active)
$query->where('ab_invoice_item.active','=',TRUE)
->where('ab_invoice.active','=',TRUE);
return $query;
}
/**
* Account that ordered the service
*
@@ -105,16 +138,6 @@ class Service extends Model
return $this->belongsTo(Account::class);
}
/**
* Tenant that the service belongs to
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function site()
{
return $this->belongsTo(Site::class);
}
/**
* Product of the service
*
@@ -125,6 +148,16 @@ class Service extends Model
return $this->belongsTo(Product::class);
}
/**
* Tenant that the service belongs to
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function site()
{
return $this->belongsTo(Site::class);
}
/**
* Return a child model with details of the service
*
@@ -180,6 +213,25 @@ class Service extends Model
return $this->getUrlAdminAttribute();
}
public function getBillingPriceAttribute(): float
{
if ($this->price)
return $this->addtax($this->price);
dd($this->product->price_group,$this);
return $this->cost;
}
/**
* Return the service billing period
*
* @return string
*/
public function getBillingPeriodAttribute(): string
{
return Arr::get($this->product->PricePeriods(),$this->recur_schedule,'Unknown');
}
/**
* Date the service expires, also represents when it is paid up to
*
@@ -191,23 +243,24 @@ class Service extends Model
}
/**
* Services Unique Identifier
* Return the date for the next invoice
*
* @return string
* @todo This function negates the need for date_next_invoice
* @return null
*/
public function getSIDAttribute(): string
public function getInvoiceNextAttribute(): Carbon
{
return sprintf('%02s-%04s.%05s',$this->site_id,$this->account_id,$this->id);
$last = $this->getInvoiceToAttribute();
return $last ? $last->addDay() : now();
}
/**
* Return the date for the next invoice
*
* @return null
* Get the date that the service has been invoiced to
*/
public function getInvoiceNextAttribute()
public function getInvoiceToAttribute()
{
return $this->date_next_invoice ? $this->date_next_invoice->format('Y-m-d') : NULL;
return $this->invoice_items->count() ? $this->invoice_items->last()->date_stop : NULL;
}
/**
@@ -294,6 +347,16 @@ class Service extends Model
return $this->getSIDAttribute();
}
/**
* Services Unique Identifier
*
* @return string
*/
public function getSIDAttribute(): string
{
return sprintf('%02s-%04s.%05s',$this->site_id,$this->account_id,$this->id);
}
/**
* Return the Service Status
*
@@ -319,6 +382,24 @@ class Service extends Model
: '';
}
/**
* Return a HTML status box
*
* @return string
*/
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);
}
/**
* URL used by an admin to administer the record
*
@@ -353,6 +434,25 @@ class Service extends Model
/** FUNCTIONS **/
/**
* Add applicable tax to the cost
*
* @todo This needs to be calculated, not fixed at 1.1
* @param float $value
* @return float
*/
public function addTax(float $value): float
{
return round($value*1.1,2);
}
public function invoices_due(): Collection
{
return $this->invoice_items->filter(function($item) {
return $item->invoice->due > 0;
});
}
/**
* Determine if a service is active. It is active, if active=1, or the order_status is not in inactive_status[]
*