2017-09-14 22:21:00 +03:00
|
|
|
<?php
|
|
|
|
|
2019-12-31 15:49:09 +03:00
|
|
|
namespace App\Models\Purchase;
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
use App\Abstracts\Model;
|
2017-09-14 22:21:00 +03:00
|
|
|
use App\Traits\Currencies;
|
2019-11-16 10:21:14 +03:00
|
|
|
use Bkwld\Cloner\Cloneable;
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
class BillItem extends Model
|
|
|
|
{
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
use Cloneable, Currencies;
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
protected $table = 'bill_items';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attributes that should be mass-assignable.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2019-11-16 16:41:26 +03:00
|
|
|
protected $fillable = ['company_id', 'bill_id', 'item_id', 'name', 'quantity', 'price', 'total', 'tax'];
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clonable relationships.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $cloneable_relations = ['taxes'];
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2020-03-10 17:25:22 +03:00
|
|
|
public static function boot()
|
|
|
|
{
|
|
|
|
parent::boot();
|
|
|
|
|
|
|
|
static::retrieved(function($model) {
|
|
|
|
$model->setTaxIds();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
public function bill()
|
|
|
|
{
|
2019-12-31 15:49:09 +03:00
|
|
|
return $this->belongsTo('App\Models\Purchase\Bill');
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function item()
|
|
|
|
{
|
2020-01-20 23:50:29 +03:00
|
|
|
return $this->belongsTo('App\Models\Common\Item')->withDefault(['name' => trans('general.na')]);
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
2019-01-07 18:38:34 +03:00
|
|
|
public function taxes()
|
2018-10-22 16:57:35 +03:00
|
|
|
{
|
2019-12-31 15:49:09 +03:00
|
|
|
return $this->hasMany('App\Models\Purchase\BillItemTax', 'bill_item_id', 'id');
|
2018-10-22 16:57:35 +03:00
|
|
|
}
|
|
|
|
|
2017-10-21 14:23:57 +03:00
|
|
|
/**
|
|
|
|
* Convert price to double.
|
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setPriceAttribute($value)
|
|
|
|
{
|
2018-08-07 12:30:21 +03:00
|
|
|
$this->attributes['price'] = (double) $value;
|
2017-10-21 14:23:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert total to double.
|
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setTotalAttribute($value)
|
|
|
|
{
|
2018-08-07 12:30:21 +03:00
|
|
|
$this->attributes['total'] = (double) $value;
|
2018-07-10 18:30:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert tax to double.
|
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setTaxAttribute($value)
|
|
|
|
{
|
2018-08-07 12:30:21 +03:00
|
|
|
$this->attributes['tax'] = (double) $value;
|
2017-10-21 14:23:57 +03:00
|
|
|
}
|
2020-03-10 17:25:22 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert tax to Array.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setTaxIds()
|
|
|
|
{
|
|
|
|
$tax_ids = [];
|
|
|
|
|
|
|
|
foreach ($this->taxes as $tax) {
|
|
|
|
$tax_ids[] = (string) $tax->tax_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->setAttribute('tax_id', $tax_ids);
|
|
|
|
}
|
2020-03-16 13:44:15 +03:00
|
|
|
|
|
|
|
public function onCloning($src, $child = null)
|
|
|
|
{
|
|
|
|
unset($this->tax_id);
|
|
|
|
}
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|