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'];
|
2018-06-19 12:31:49 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-06-19 12:31:49 +00:00
|
|
|
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()
|
|
|
|
{
|
2018-06-19 12:31:49 +00:00
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
2018-06-19 12:31:49 +00:00
|
|
|
public function getInvoiceIdAttribute()
|
2018-05-20 12:53:14 +00:00
|
|
|
{
|
|
|
|
return sprintf('%02s-%04s-%04s',$this->site_id,$this->account_id,$this->id);
|
|
|
|
}
|
|
|
|
|
2018-06-19 12:31:49 +00:00
|
|
|
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)
|
|
|
|
{
|
2018-06-19 12:31:49 +00:00
|
|
|
foreach ($this->items as $o)
|
2018-05-20 12:53:14 +00:00
|
|
|
{
|
2018-06-19 12:31:49 +00:00
|
|
|
if ($o->active)
|
2018-05-20 12:53:14 +00:00
|
|
|
$this->_total += $this->currency()->round($o->total);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-19 12:31:49 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|