134 lines
3.0 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
namespace App\Models\Item;
use App\Models\Model;
use App\Traits\Currencies;
2017-11-26 15:20:17 +03:00
use Bkwld\Cloner\Cloneable;
2017-09-14 22:21:00 +03:00
use Sofa\Eloquence\Eloquence;
use Plank\Mediable\Mediable;
2017-09-14 22:21:00 +03:00
class Item extends Model
{
use Cloneable, Currencies, Eloquence, Mediable;
2017-09-14 22:21:00 +03:00
protected $table = 'items';
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
protected $fillable = ['company_id', 'name', 'sku', 'description', 'sale_price', 'purchase_price', 'quantity', 'category_id', 'tax_id', 'picture', 'enabled'];
/**
* Sortable columns.
*
* @var array
*/
2017-09-29 23:32:51 +03:00
protected $sortable = ['name', 'category', 'quantity', 'sale_price', 'purchase_price', 'enabled'];
2017-09-14 22:21:00 +03:00
/**
* Searchable rules.
*
* @var array
*/
protected $searchableColumns = [
'name' => 10,
'sku' => 5,
'description' => 2,
];
public function category()
{
return $this->belongsTo('App\Models\Setting\Category');
}
2017-10-08 23:22:05 +03:00
public function tax()
2017-09-14 22:21:00 +03:00
{
2017-10-08 23:22:05 +03:00
return $this->belongsTo('App\Models\Setting\Tax');
2017-09-14 22:21:00 +03:00
}
2017-10-09 11:17:05 +03:00
public function bill_items()
2017-09-14 22:21:00 +03:00
{
2017-10-09 11:17:05 +03:00
return $this->hasMany('App\Models\Expense\BillItem');
2017-09-14 22:21:00 +03:00
}
2017-10-09 11:17:05 +03:00
public function invoice_items()
2017-09-14 22:21:00 +03:00
{
2017-10-09 11:17:05 +03:00
return $this->hasMany('App\Models\Income\InvoiceItem');
2017-09-14 22:21:00 +03:00
}
2017-10-04 01:25:03 +03:00
/**
2017-10-21 14:23:57 +03:00
* Convert sale price to double.
2017-10-04 01:25:03 +03:00
*
* @param string $value
* @return void
*/
public function setSalePriceAttribute($value)
{
2017-10-21 14:23:57 +03:00
$this->attributes['sale_price'] = (double) $value;
2017-10-04 01:25:03 +03:00
}
/**
2017-10-21 14:23:57 +03:00
* Convert purchase price to double.
2017-10-04 01:25:03 +03:00
*
* @param string $value
* @return void
*/
public function setPurchasePriceAttribute($value)
{
2017-10-21 14:23:57 +03:00
$this->attributes['purchase_price'] = (double) $value;
2017-10-04 01:25:03 +03:00
}
2017-09-14 22:21:00 +03:00
public static function getItems($filter_data = array())
{
if (empty($filter_data)) {
return Item::all();
}
$query = Item::select('id as item_id', 'name', 'sale_price', 'purchase_price', 'tax_id');
$query->where('quantity', '>', '0');
foreach ($filter_data as $key => $value) {
$query->where($key, 'LIKE', "%" . $value . "%");
}
return $query->get();
}
2017-09-29 23:32:51 +03:00
/**
* 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)) {
return $value;
}
if (!$this->hasMedia('picture')) {
return false;
}
return $this->getMedia('picture')->last();
}
2017-09-14 22:21:00 +03:00
}