This commit is contained in:
denisdulici 2018-05-14 09:46:37 +03:00
parent 651194cd56
commit 72ed09c352
2 changed files with 43 additions and 14 deletions

View File

@ -205,12 +205,16 @@ class Items extends Controller
$currency = Currency::where('code', $currency_code)->first(); $currency = Currency::where('code', $currency_code)->first();
$filter_data = [ $autocomplete = Item::autocomplete([
'name' => $query, 'name' => $query,
'sku' => $query, 'sku' => $query,
]; ]);
$items = Item::getItems($filter_data); if ($type == 'invoice') {
$autocomplete->quantity();
}
$items = $autocomplete->get();
if ($items) { if ($items) {
foreach ($items as $item) { foreach ($items as $item) {

View File

@ -14,6 +14,13 @@ class Item extends Model
protected $table = 'items'; protected $table = 'items';
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = ['item_id'];
/** /**
* Attributes that should be mass-assignable. * Attributes that should be mass-assignable.
* *
@ -81,23 +88,41 @@ class Item extends Model
$this->attributes['purchase_price'] = (double) $value; $this->attributes['purchase_price'] = (double) $value;
} }
public static function getItems($filter_data = array()) /**
* Get the item id.
*
* @return string
*/
public function getItemIdAttribute()
{ {
if (empty($filter_data)) { return $this->id;
return Item::all(); }
}
$query = Item::select('id as item_id', 'name', 'sku', 'sale_price', 'purchase_price', 'tax_id'); /**
* Scope autocomplete.
$query->where('quantity', '>', '0'); *
* @param \Illuminate\Database\Eloquent\Builder $query
$query->where(function ($query) use ($filter_data) { * @param array $filter
foreach ($filter_data as $key => $value) { * @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 . "%"); $query->orWhere($key, 'LIKE', "%" . $value . "%");
} }
}); });
}
return $query->get(); /**
* Scope quantity.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeQuantity($query)
{
return $query->where('quantity', '>', '0');
} }
/** /**