162 lines
3.7 KiB
PHP
Raw Normal View History

2018-06-10 02:48:51 +03:00
<?php
namespace App\Models\Common;
2019-11-16 10:21:14 +03:00
use App\Abstracts\Model;
2020-12-24 01:28:38 +03:00
use App\Models\Document\Document;
2018-06-10 02:48:51 +03:00
use App\Traits\Currencies;
use App\Traits\Media;
2019-11-16 10:21:14 +03:00
use Bkwld\Cloner\Cloneable;
2020-10-14 17:07:59 +03:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
2018-06-10 02:48:51 +03:00
class Item extends Model
{
2020-10-14 17:07:59 +03:00
use Cloneable, Currencies, HasFactory, Media;
2018-06-10 02:48:51 +03:00
protected $table = 'items';
/**
* The accessors to append to the model's array form.
*
* @var array
*/
2020-12-09 13:58:55 +03:00
protected $appends = ['item_id', 'tax_ids'];
2018-06-10 02:48:51 +03:00
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
2021-09-07 10:33:34 +03:00
protected $fillable = ['company_id', 'name', 'description', 'sale_price', 'purchase_price', 'category_id', 'enabled', 'created_from', 'created_by'];
2018-06-10 02:48:51 +03:00
2020-11-13 15:15:27 +03:00
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'sale_price' => 'double',
'purchase_price' => 'double',
'enabled' => 'boolean',
];
2018-06-10 02:48:51 +03:00
/**
* Sortable columns.
*
* @var array
*/
2019-11-16 10:21:14 +03:00
protected $sortable = ['name', 'category', 'sale_price', 'purchase_price', 'enabled'];
2018-06-10 02:48:51 +03:00
2020-12-08 13:30:06 +03:00
/**
* @var array
*/
public $cloneable_relations = ['taxes'];
2018-06-10 02:48:51 +03:00
public function category()
{
2020-01-20 22:58:49 +03:00
return $this->belongsTo('App\Models\Setting\Category')->withDefault(['name' => trans('general.na')]);
2018-06-10 02:48:51 +03:00
}
2020-12-08 13:30:06 +03:00
public function taxes()
2018-06-10 02:48:51 +03:00
{
2020-12-08 13:30:06 +03:00
return $this->hasMany('App\Models\Common\ItemTax');
2018-06-10 02:48:51 +03:00
}
2020-12-24 01:28:38 +03:00
public function document_items()
{
return $this->hasMany('App\Models\Document\DocumentItem');
}
2018-06-10 02:48:51 +03:00
public function bill_items()
{
2020-12-24 01:28:38 +03:00
return $this->document_items()->where('type', Document::BILL_TYPE);
2018-06-10 02:48:51 +03:00
}
public function invoice_items()
{
2020-12-24 01:28:38 +03:00
return $this->document_items()->where('type', Document::INVOICE_TYPE);
2018-06-10 02:48:51 +03:00
}
2020-01-20 22:58:49 +03:00
public function scopeName($query, $name)
{
return $query->where('name', '=', $name);
}
2018-06-10 02:48:51 +03:00
/**
* Get the item id.
*
* @return string
*/
public function getItemIdAttribute()
{
return $this->id;
}
2020-12-08 13:30:06 +03:00
/**
* Get the item id.
*
* @return string
*/
public function getTaxIdsAttribute()
{
return $this->taxes()->pluck('tax_id');
}
2018-06-10 02:48:51 +03:00
/**
* Scope autocomplete.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param array $filter
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeAutocomplete($query, $filter)
{
return $query->where(function ($query) use ($filter) {
foreach ($filter as $key => $value) {
$query->orWhere($key, 'LIKE', "%" . $value . "%");
}
});
}
/**
* Sort by category name
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param $direction
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function categorySortable($query, $direction)
{
return $query->join('categories', 'categories.id', '=', 'items.category_id')
->orderBy('name', $direction)
->select('items.*');
}
/**
* Get the current balance.
*
* @return string
*/
public function getPictureAttribute($value)
{
if (!empty($value) && !$this->hasMedia('picture')) {
return $value;
} elseif (!$this->hasMedia('picture')) {
return false;
}
return $this->getMedia('picture')->last();
}
2020-10-14 17:07:59 +03:00
/**
* Create a new factory instance for the model.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
protected static function newFactory()
{
return \Database\Factories\Item::new();
}
2018-06-10 02:48:51 +03:00
}