osb/app/Models/InvoiceItem.php

46 lines
697 B
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class InvoiceItem extends Model
{
protected $table = 'ab_invoice_item';
protected $with = ['taxes'];
private $_tax = 0;
public function invoice()
{
return $this->belongsTo(Invoice::class);
}
public function taxes()
{
return $this->hasMany(InvoiceItemTax::class);
}
public function getSubTotalAttribute()
{
return $this->quantity * $this->price_base;
}
public function getTaxAttribute()
{
if (! $this->_tax)
{
foreach ($this->taxes as $o)
{
$this->_tax += $o->amount;
}
}
return $this->_tax;
}
public function getTotalAttribute()
{
return $this->tax + $this->sub_total;
}
}