Search Results

Go to the documentation of this file.
1 <?php
2 namespace App\Models;
3 
13 
14 class Consumable extends Model
15 {
16  use SoftDeletes;
17  use CompanyableTrait;
18 
19  protected $dates = ['deleted_at'];
20  protected $table = 'consumables';
21 
22 
26  public $rules = array(
27  'name' => 'required|min:3|max:255',
28  'qty' => 'required|integer|min:0',
29  'category_id' => 'required|integer',
30  'company_id' => 'integer',
31  'min_amt' => 'integer|min:1',
32  );
33 
41  protected $injectUniqueIdentifier = true;
42  use ValidatingTrait;
43 
49  protected $fillable = ['name','qty','company_id','category_id'];
50 
51 
52  public function admin()
53  {
54  return $this->belongsTo('\App\Models\User', 'user_id');
55  }
56 
57  public function consumableAssigments()
58  {
59  return $this->hasMany('\App\Models\ConsumableAssignment');
60  }
61 
62  public function company()
63  {
64  return $this->belongsTo('\App\Models\Company', 'company_id');
65  }
66 
67  public function location()
68  {
69  return $this->belongsTo('\App\Models\Location', 'location_id');
70  }
71 
72  public function category()
73  {
74  return $this->belongsTo('\App\Models\Category', 'category_id');
75  }
76 
80  public function assetlog()
81  {
82  return $this->hasMany('\App\Models\Actionlog', 'consumable_id')->where('asset_type', '=', 'consumable')->orderBy('created_at', 'desc')->withTrashed();
83  }
84 
85 
86  public function users()
87  {
88  return $this->belongsToMany('\App\Models\User', 'consumables_users', 'consumable_id', 'assigned_to')->withPivot('user_id')->withTrashed()->withTimestamps();
89  }
90 
91  public function hasUsers()
92  {
93  return $this->belongsToMany('\App\Models\User', 'consumables_users', 'consumable_id', 'assigned_to')->count();
94  }
95 
96 
97  public function requireAcceptance()
98  {
99  return $this->category->require_acceptance;
100  }
101 
102  public function getEula()
103  {
104 
105  $Parsedown = new \Parsedown();
106 
107  if ($this->category->eula_text) {
108  return $Parsedown->text(e($this->category->eula_text));
109  } elseif ((Setting::getSettings()->default_eula_text) && ($this->category->use_default_eula=='1')) {
110  return $Parsedown->text(e(Setting::getSettings()->default_eula_text));
111  } else {
112  return null;
113  }
114 
115  }
116 
117  public function numRemaining()
118  {
119  $checkedout = $this->users->count();
120  $total = $this->qty;
121  $remaining = $total - $checkedout;
122  return $remaining;
123  }
124 
141  public function scopeTextSearch($query, $search)
142  {
143  $search = explode(' ', $search);
144 
145  return $query->where(function ($query) use ($search) {
146 
147  foreach ($search as $search) {
148  $query->whereHas('category', function ($query) use ($search) {
149  $query->where('categories.name', 'LIKE', '%'.$search.'%');
150  })->orWhere(function ($query) use ($search) {
151  $query->whereHas('company', function ($query) use ($search) {
152  $query->where('companies.name', 'LIKE', '%'.$search.'%');
153  });
154  })->orWhere(function ($query) use ($search) {
155  $query->whereHas('location', function ($query) use ($search) {
156  $query->where('locations.name', 'LIKE', '%'.$search.'%');
157  });
158  })->orWhere('consumables.name', 'LIKE', '%'.$search.'%')
159  ->orWhere('consumables.order_number', 'LIKE', '%'.$search.'%')
160  ->orWhere('consumables.purchase_cost', 'LIKE', '%'.$search.'%')
161  ->orWhere('consumables.purchase_date', 'LIKE', '%'.$search.'%');
162  }
163  });
164  }
165 
174  public function scopeOrderCategory($query, $order)
175  {
176  return $query->join('categories', 'consumables.category_id', '=', 'categories.id')->orderBy('categories.name', $order);
177  }
178 
187  public function scopeOrderLocation($query, $order)
188  {
189  return $query->leftJoin('locations', 'consumables.location_id', '=', 'locations.id')->orderBy('locations.name', $order);
190  }
191 
192 
201  public function scopeOrderCompany($query, $order)
202  {
203  return $query->leftJoin('companies', 'consumables.company_id', '=', 'companies.id')->orderBy('companies.name', $order);
204  }
205 }
scopeOrderCompany($query, $order)
Query builder scope to order on company.
Definition: Consumable.php:201
assetlog()
Get action logs for this consumable.
Definition: Consumable.php:80
scopeOrderCategory($query, $order)
Query builder scope to order on company.
Definition: Consumable.php:174
static getSettings()
Definition: Setting.php:33
scopeOrderLocation($query, $order)
Query builder scope to order on company.
Definition: Consumable.php:187
$rules
Category validation rules.
Definition: Consumable.php:26
scopeTextSearch($query, $search)
Query builder scope to search on text.
Definition: Consumable.php:141