Item.php
3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
namespace App\Models\Common;
use App\Models\Model;
use App\Traits\Currencies;
use Bkwld\Cloner\Cloneable;
use Sofa\Eloquence\Eloquence;
use App\Traits\Media;
class Item extends Model
{
use Cloneable, Currencies, Eloquence, Media;
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.
*
* @var array
*/
protected $fillable = ['company_id', 'name', 'sku', 'description', 'sale_price', 'purchase_price', 'quantity', 'category_id', 'tax_id', 'enabled'];
/**
* Sortable columns.
*
* @var array
*/
protected $sortable = ['name', 'category', 'quantity', 'sale_price', 'purchase_price', 'enabled'];
/**
* Searchable rules.
*
* @var array
*/
protected $searchableColumns = [
'name' => 10,
'sku' => 5,
'description' => 2,
];
public function category()
{
return $this->belongsTo('App\Models\Setting\Category');
}
public function tax()
{
return $this->belongsTo('App\Models\Setting\Tax');
}
public function bill_items()
{
return $this->hasMany('App\Models\Expense\BillItem');
}
public function invoice_items()
{
return $this->hasMany('App\Models\Income\InvoiceItem');
}
/**
* Convert sale price to double.
*
* @param string $value
* @return void
*/
public function setSalePriceAttribute($value)
{
$this->attributes['sale_price'] = (double) $value;
}
/**
* Convert purchase price to double.
*
* @param string $value
* @return void
*/
public function setPurchasePriceAttribute($value)
{
$this->attributes['purchase_price'] = (double) $value;
}
/**
* Get the item id.
*
* @return string
*/
public function getItemIdAttribute()
{
return $this->id;
}
/**
* 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 . "%");
}
});
}
/**
* Scope quantity.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeQuantity($query)
{
return $query->where('quantity', '>', '0');
}
/**
* 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();
}
}