osb/app/Models/Invoice.php

88 lines
1.6 KiB
PHP
Raw Normal View History

2018-05-20 12:53:14 +00:00
<?php
namespace App\Models;
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 $appends = [
'date_due',
'due',
'invoice_id_url',
'total',
];
protected $visible = [
'date_due',
'due',
'id',
'invoice_id_url',
'total',
];
2018-05-20 12:53:14 +00:00
private $_total = 0;
public function account()
{
return $this->belongsTo(\App\User::class);
}
public function items()
2018-05-20 12:53:14 +00:00
{
return $this->hasMany(InvoiceItem::class);
}
public function paymentitems()
{
return $this->hasMany(PaymentItem::class);
}
public function getDueAttribute()
{
return sprintf('%3.'.$this->currency()->rounding.'f',$this->total - $this->paid);
2018-05-20 12:53:14 +00:00
}
public function getDateDueAttribute()
{
return $this->due_date->format('Y-m-d');
}
public function getInvoiceIdAttribute()
2018-05-20 12:53:14 +00:00
{
return sprintf('%02s-%04s-%04s',$this->site_id,$this->account_id,$this->id);
}
public function getInvoiceIdUrlAttribute()
{
return sprintf('<a href="/u/invoice/view/%s">%s</a>',$this->id,$this->invoice_id);
}
2018-05-20 12:53:14 +00:00
public function getPaidAttribute()
{
return $this->currency()->round($this->paymentitems->sum('alloc_amt'));
}
public function getTotalAttribute()
{
if (! $this->_total)
{
foreach ($this->items as $o)
2018-05-20 12:53:14 +00:00
{
if ($o->active)
2018-05-20 12:53:14 +00:00
$this->_total += $this->currency()->round($o->total);
}
}
return sprintf('%3.'.$this->currency()->rounding.'f',$this->_total);
2018-05-20 12:53:14 +00:00
}
public function currency()
{
return $this->account->country->currency;
}
}