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

@@ -75,7 +75,8 @@ class Account extends Model
/**
* Search for a record
*
* @param User $uo
* @param $query
* @param string $term
* @return
*/
public function scopeSearch($query,string $term)

View File

@@ -4,6 +4,8 @@ namespace App\Models\Base;
use Illuminate\Database\Eloquent\Model;
use App\Models\Service;
abstract class ServiceType extends Model
{
public $timestamps = FALSE;
@@ -17,4 +19,28 @@ abstract class ServiceType extends Model
{
return $this->morphOne(Service::class,'type','model','id');
}
/** SCOPES */
/**
* Search for a record
*
* @param $query
* @param string $term
* @return
*/
public function scopeSearch($query,string $term)
{
return $query
->with(['service'])
->join('ab_service','ab_service.id','=',$this->getTable().'.service_id')
->Where('ab_service.id','like','%'.$term.'%');
}
/** ATTRIBUTES **/
public function getTypeAttribute()
{
return strtolower((new \ReflectionClass($this))->getShortName());
}
}

View File

@@ -57,7 +57,7 @@ class Product extends Model
private function getDefaultLanguage()
{
return config('SITE_SETUP')->language;
return config('SITE_SETUP')->language();
}
public function getDescriptionAttribute()

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[]
*

View File

@@ -12,16 +12,45 @@ class Adsl extends \App\Models\Base\ServiceType
// @todo column service_id can be removed.
protected $table = 'ab_service__adsl';
protected $dates = ['service_connect_date','service_contract_date'];
/** SCOPES */
/**
* Search for a record
*
* @param $query
* @param string $term
* @return
*/
public function scopeSearch($query,string $term)
{
// Build our where clause
return parent::scopeSearch($query,$term)
->orwhere('service_number','like','%'.$term.'%')
->orWhere('service_address','like','%'.$term.'%')
->orWhere('ipaddress','like','%'.$term.'%');
}
/** ATTRIBUTES **/
/**
* @deprecated use getNameFullAttribute()
*/
public function getFullNameAttribute()
{
return ($this->service_number AND $this->service_address)
? sprintf('%s: %s',$this->service_number, $this->service_address)
: $this->name;
return $this->getNameFullAttribute();
}
public function getNameAttribute()
{
return $this->service_number ?: $this->service_address;
}
public function getNameFullAttribute()
{
return ($this->service_number AND $this->service_address)
? sprintf('%s: %s',$this->service_number, $this->service_address)
: $this->name;
}
}

View File

@@ -23,6 +23,9 @@ class Site extends Model
public function language()
{
if (! $this->id)
return Language::find(1);
return $this->belongsTo(Language::class);
}