Initial invoice rendering

This commit is contained in:
Deon George
2018-08-01 17:09:38 +10:00
parent 1cde2a888a
commit c444e1d218
16 changed files with 500 additions and 28 deletions

View File

@@ -7,8 +7,8 @@ use Illuminate\Database\Eloquent\Model;
class Invoice extends Model
{
protected $table = 'ab_invoice';
protected $dates = ['due_date'];
protected $with = ['items.taxes','account.country.currency','paymentitems'];
protected $dates = ['date_orig','due_date'];
protected $with = ['items.taxes','items.product','items.service','account.country.currency','paymentitems'];
protected $appends = [
'date_due',
@@ -26,6 +26,7 @@ class Invoice extends Model
];
private $_total = 0;
private $_total_tax = 0;
public function account()
{
@@ -52,14 +53,29 @@ class Invoice extends Model
return $this->due_date->format('Y-m-d');
}
public function getInvoiceDateAttribute()
{
return $this->date_orig->format('Y-m-d');
}
public function getInvoiceIdAttribute()
{
return sprintf('%02s-%04s-%04s',$this->site_id,$this->account_id,$this->id);
return sprintf('%06s',$this->id);
}
public function getInvoiceAccountIdAttribute()
{
return sprintf('%02s-%04s-%06s',$this->site_id,$this->account_id,$this->invoice_id);
}
public function getInvoiceIdUrlAttribute()
{
return sprintf('<a href="/u/invoice/view/%s">%s</a>',$this->id,$this->invoice_id);
return sprintf('<a href="/u/invoice/%s">%s</a>',$this->id,$this->invoice_account_id);
}
public function getInvoiceTextAttribute()
{
return sprintf('Thank you for using %s for your Internet Services.',config('SITE_SETUP')->site_name);
}
public function getPaidAttribute()
@@ -67,6 +83,25 @@ class Invoice extends Model
return $this->currency()->round($this->paymentitems->sum('alloc_amt'));
}
public function getSubTotalAttribute()
{
return sprintf('%3.'.$this->currency()->rounding.'f',$this->total-$this->tax_total);
}
public function getTaxTotalAttribute()
{
if (! $this->_total_tax)
{
foreach ($this->items as $o)
{
if ($o->active)
$this->_total_tax += $this->currency()->round($o->tax);
}
}
return sprintf('%3.'.$this->currency()->rounding.'f',$this->_total_tax);
}
public function getTotalAttribute()
{
if (! $this->_total)
@@ -85,4 +120,44 @@ class Invoice extends Model
{
return $this->account->country->currency;
}
public function products()
{
$return = collect();
foreach ($this->items->groupBy('product_id') as $o)
{
$po = $o->first()->product;
$po->count = count($o->pluck('service_id')->unique());
$return->push($po);
}
$lo = $this->account->user->language;
return $return->sortBy(function ($item) use ($lo) {
return $item->name($lo);
});
}
public function product_services(Product $po)
{
$return = collect();
foreach ($this->items->filter(function ($item) use ($po) {
return $item->product_id == $po->id;
}) as $o)
{
$so = $o->service;
$return->push($so);
};
return $return->unique()->sortBy('name');
}
public function product_service_items(Product $po,Service $so)
{
return $this->items->filter(function ($item) use ($po,$so) {
return $item->product_id == $po->id AND $item->service_id == $so->id;
})->filter()->sortBy('item_type');
}
}