akaunting/app/Models/Document/DocumentItem.php

148 lines
3.3 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
2020-12-24 01:28:38 +03:00
namespace App\Models\Document;
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;
2020-12-24 01:28:38 +03:00
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
2017-09-14 22:21:00 +03:00
2020-12-24 01:28:38 +03:00
class DocumentItem extends Model
2017-09-14 22:21:00 +03:00
{
2019-11-16 10:21:14 +03:00
use Cloneable, Currencies;
2017-09-14 22:21:00 +03:00
2020-12-24 01:28:38 +03:00
protected $table = 'document_items';
2017-09-14 22:21:00 +03:00
2020-12-24 22:55:11 +03:00
protected $appends = ['discount'];
2020-03-26 16:20:31 +03:00
protected $fillable = [
'company_id',
2020-12-24 01:28:38 +03:00
'type',
'document_id',
'item_id',
'name',
2020-12-24 01:28:38 +03:00
'description',
'quantity',
'price',
'total',
'tax',
'discount_rate',
'discount_type',
2021-09-07 10:33:34 +03:00
'created_from',
'created_by',
];
2019-11-16 10:21:14 +03:00
/**
2020-12-24 01:28:38 +03:00
* The attributes that should be cast.
2019-11-16 10:21:14 +03:00
*
* @var array
*/
2020-12-24 01:28:38 +03:00
protected $casts = [
'price' => 'double',
'total' => 'double',
'tax' => 'double',
];
/**
* @var array
*/
2019-11-16 10:21:14 +03:00
public $cloneable_relations = ['taxes'];
2017-09-14 22:21:00 +03:00
public static function boot()
{
parent::boot();
2020-12-24 01:28:38 +03:00
static::retrieved(
function ($model) {
$model->setTaxIds();
}
);
2022-02-25 19:53:35 +03:00
static::saving(
function ($model) {
$model->offsetUnset('tax_ids');
}
);
}
2020-12-24 01:28:38 +03:00
public function document()
2017-09-14 22:21:00 +03:00
{
2022-06-10 19:26:32 +03:00
return $this->belongsTo('App\Models\Document\Document')->withoutGlobalScope('App\Scopes\Document');
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
{
2020-12-24 01:28:38 +03:00
return $this->hasMany('App\Models\Document\DocumentItemTax', 'document_item_id', 'id');
2018-10-22 16:57:35 +03:00
}
2020-12-24 01:28:38 +03:00
public function scopeType(Builder $query, string $type)
2017-10-21 14:23:57 +03:00
{
2021-09-10 09:41:15 +03:00
return $query->where($this->qualifyColumn('type'), '=', $type);
2017-10-21 14:23:57 +03:00
}
2020-12-24 01:28:38 +03:00
public function scopeInvoice(Builder $query)
2017-10-21 14:23:57 +03:00
{
2021-09-10 09:41:15 +03:00
return $query->where($this->qualifyColumn('type'), '=', Document::INVOICE_TYPE);
}
2020-12-24 01:28:38 +03:00
public function scopeBill(Builder $query)
{
2021-09-10 09:41:15 +03:00
return $query->where($this->qualifyColumn('type'), '=', Document::BILL_TYPE);
2017-10-21 14:23:57 +03:00
}
2020-12-24 01:28:38 +03:00
public function getDiscountAttribute(): string
{
if (setting('localisation.percent_position', 'after') === 'after') {
2020-03-26 16:20:31 +03:00
$text = ($this->discount_type === 'normal') ? $this->discount_rate . '%' : $this->discount_rate;
} else {
2020-03-26 16:20:31 +03:00
$text = ($this->discount_type === 'normal') ? '%' . $this->discount_rate : $this->discount_rate;
}
return $text;
}
2020-12-24 01:28:38 +03:00
public function getDiscountRateAttribute(int $value = 0)
{
$discount_rate = 0;
switch (setting('localisation.discount_location', 'total')) {
case 'no':
case 'total':
$discount_rate = 0;
break;
case 'both':
2020-12-24 01:28:38 +03:00
case 'item':
$discount_rate = $value;
break;
}
return $discount_rate;
}
/**
* Convert tax to Array.
*/
public function setTaxIds()
{
$tax_ids = [];
foreach ($this->taxes as $tax) {
$tax_ids[] = (string) $tax->tax_id;
}
2020-12-24 01:28:38 +03:00
$this->setAttribute('tax_ids', $tax_ids);
}
2020-03-16 13:44:15 +03:00
public function onCloning($src, $child = null)
{
2020-12-24 01:28:38 +03:00
unset($this->tax_ids);
2020-03-16 13:44:15 +03:00
}
2017-09-14 22:21:00 +03:00
}