68 lines
1.2 KiB
PHP
68 lines
1.2 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace App\Models;
|
||
|
|
||
|
use Illuminate\Database\Eloquent\Model;
|
||
|
|
||
|
class Invoice extends Model
|
||
|
{
|
||
|
protected $table = 'ab_invoice';
|
||
|
protected $dates = ['due_date'];
|
||
|
protected $with = ['invoiceitems.taxes','account.country.currency','paymentitems'];
|
||
|
|
||
|
private $_total = 0;
|
||
|
|
||
|
public function account()
|
||
|
{
|
||
|
return $this->belongsTo(\App\User::class);
|
||
|
}
|
||
|
|
||
|
public function invoiceitems()
|
||
|
{
|
||
|
return $this->hasMany(InvoiceItem::class);
|
||
|
}
|
||
|
|
||
|
public function paymentitems()
|
||
|
{
|
||
|
return $this->hasMany(PaymentItem::class);
|
||
|
}
|
||
|
|
||
|
public function getDueAttribute()
|
||
|
{
|
||
|
return $this->currency()->round($this->total - $this->paid);
|
||
|
}
|
||
|
|
||
|
public function getDateDueAttribute()
|
||
|
{
|
||
|
return $this->due_date->format('Y-m-d');
|
||
|
}
|
||
|
|
||
|
public function getInvoiceNumberAttribute()
|
||
|
{
|
||
|
return sprintf('%02s-%04s-%04s',$this->site_id,$this->account_id,$this->id);
|
||
|
}
|
||
|
|
||
|
public function getPaidAttribute()
|
||
|
{
|
||
|
return $this->currency()->round($this->paymentitems->sum('alloc_amt'));
|
||
|
}
|
||
|
|
||
|
public function getTotalAttribute()
|
||
|
{
|
||
|
if (! $this->_total)
|
||
|
{
|
||
|
foreach ($this->invoiceitems as $o)
|
||
|
{
|
||
|
//if ($o->active)
|
||
|
$this->_total += $this->currency()->round($o->total);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return $this->_total;
|
||
|
}
|
||
|
|
||
|
public function currency()
|
||
|
{
|
||
|
return $this->account->country->currency;
|
||
|
}
|
||
|
}
|