238 lines
5.6 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;
2022-06-21 10:00:20 +03:00
use App\Utilities\Str;
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 relationships that should always be loaded.
*
* @var array
*/
protected $with = ['taxes'];
2018-06-10 02:48:51 +03:00
/**
* 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
*/
2022-06-01 10:15:55 +03:00
protected $fillable = ['company_id', 'type', '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 = [
2023-03-16 16:36:13 +03:00
'sale_price' => 'double',
'purchase_price' => 'double',
'enabled' => 'boolean',
'deleted_at' => 'datetime',
2020-11-13 15:15:27 +03:00
];
2018-06-10 02:48:51 +03:00
/**
* Sortable columns.
*
* @var array
*/
2022-06-01 10:15:55 +03:00
protected $sortable = ['name', 'category.name', '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()
{
2022-07-22 00:52:06 +03:00
return $this->belongsTo('App\Models\Setting\Category')->withoutGlobalScope('App\Scopes\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);
}
2022-06-01 10:15:55 +03:00
public function scopeBilling($query, $billing)
{
return $query->where($billing . '_price', '=', null);
}
public function scopePriceType($query, $price_type)
{
return $query->whereNotNull($price_type . '_price');
}
2023-05-26 13:26:48 +03:00
public function scopeType($query, $type)
{
if (empty($type)) {
return $query;
}
return $query->whereIn('type', $type);
}
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.*');
}
2022-06-21 10:00:20 +03:00
public function getInitialsAttribute($value)
{
return Str::getInitials($this->name);
}
2018-06-10 02:48:51 +03:00
/**
* 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
2022-06-01 10:15:55 +03:00
/**
* Get the line actions.
*
* @return array
*/
public function getLineActionsAttribute()
{
$actions = [];
$actions[] = [
'title' => trans('general.edit'),
'icon' => 'edit',
'url' => route('items.edit', $this->id),
'permission' => 'update-common-items',
2022-09-06 13:54:56 +03:00
'attributes' => [
'id' => 'index-line-actions-edit-item-' . $this->id,
],
2022-06-01 10:15:55 +03:00
];
$actions[] = [
'title' => trans('general.duplicate'),
'icon' => 'file_copy',
'url' => route('items.duplicate', $this->id),
'permission' => 'create-common-items',
2022-09-06 13:54:56 +03:00
'attributes' => [
'id' => 'index-line-actions-duplicate-item-' . $this->id,
],
2022-06-01 10:15:55 +03:00
];
$actions[] = [
'type' => 'delete',
'icon' => 'delete',
'route' => 'items.destroy',
'permission' => 'delete-common-items',
2022-09-06 13:54:56 +03:00
'attributes' => [
'id' => 'index-line-actions-delete-item-' . $this->id,
],
2022-06-01 10:15:55 +03:00
'model' => $this,
];
return $actions;
}
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
}