Search Results

Go to the documentation of this file.
1 <?php
2 namespace App\Models;
3 
7 
13 class Accessory extends Model
14 {
15  use SoftDeletes;
16  use CompanyableTrait;
17 
18  protected $dates = ['deleted_at'];
19  protected $table = 'accessories';
20 
24  public $rules = array(
25  'name' => 'required|min:3|max:255',
26  'qty' => 'required|integer|min:1',
27  'category_id' => 'required|integer',
28  'company_id' => 'integer',
29  'min_amt' => 'integer|min:1',
30  );
31 
32 
40  protected $injectUniqueIdentifier = true;
41  use ValidatingTrait;
42 
48  protected $fillable = ['name','qty','category_id'];
49 
50  public function company()
51  {
52  return $this->belongsTo('\App\Models\Company', 'company_id');
53  }
54 
55  public function location()
56  {
57  return $this->belongsTo('\App\Models\Location', 'location_id');
58  }
59 
60  public function category()
61  {
62  return $this->belongsTo('\App\Models\Category', 'category_id')->where('category_type', '=', 'accessory');
63  }
64 
68  public function assetlog()
69  {
70  return $this->hasMany('\App\Models\Actionlog', 'accessory_id')->where('asset_type', '=', 'accessory')->orderBy('created_at', 'desc')->withTrashed();
71  }
72 
73 
74  public function users()
75  {
76  return $this->belongsToMany('\App\Models\User', 'accessories_users', 'accessory_id', 'assigned_to')->withPivot('id')->withTrashed();
77  }
78 
79  public function hasUsers()
80  {
81  return $this->belongsToMany('\App\Models\User', 'accessories_users', 'accessory_id', 'assigned_to')->count();
82  }
83 
84  public function checkin_email()
85  {
86  return $this->category->checkin_email;
87  }
88 
89  public function requireAcceptance()
90  {
91  return $this->category->require_acceptance;
92  }
93 
94  public function getEula()
95  {
96 
97  $Parsedown = new \Parsedown();
98 
99  if ($this->category->eula_text) {
100  return $Parsedown->text(e($this->category->eula_text));
101  } elseif ((Setting::getSettings()->default_eula_text) && ($this->category->use_default_eula=='1')) {
102  return $Parsedown->text(e(Setting::getSettings()->default_eula_text));
103  } else {
104  return null;
105  }
106 
107  }
108 
109  public function numRemaining()
110  {
111  $checkedout = $this->users->count();
112  $total = $this->qty;
113  $remaining = $total - $checkedout;
114  return $remaining;
115  }
116 
125  public function scopeTextSearch($query, $search)
126  {
127  $search = explode('+', $search);
128 
129  return $query->where(function ($query) use ($search) {
130 
131  foreach ($search as $search) {
132  $query->whereHas('category', function ($query) use ($search) {
133  $query->where('categories.name', 'LIKE', '%'.$search.'%');
134  })->orWhere(function ($query) use ($search) {
135  $query->whereHas('company', function ($query) use ($search) {
136  $query->where('companies.name', 'LIKE', '%'.$search.'%');
137  });
138  })->orWhere(function ($query) use ($search) {
139  $query->whereHas('assetlog', function ($query) use ($search) {
140  $query->where('action_type', '=', 'checkout')
141  ->where('created_at', 'LIKE', '%'.$search.'%');
142  });
143  })->orWhere(function ($query) use ($search) {
144  $query->whereHas('location', function ($query) use ($search) {
145  $query->where('locations.name', 'LIKE', '%'.$search.'%');
146  });
147  })->orWhere('accessories.name', 'LIKE', '%'.$search.'%')
148  ->orWhere('accessories.order_number', 'LIKE', '%'.$search.'%')
149  ->orWhere('accessories.purchase_cost', 'LIKE', '%'.$search.'%')
150  ->orWhere('accessories.purchase_date', 'LIKE', '%'.$search.'%');
151  }
152  });
153  }
154 
163  public function scopeOrderCompany($query, $order)
164  {
165  return $query->leftJoin('companies', 'accessories.company_id', '=', 'companies.id')->orderBy('companies.name', $order);
166  }
167 
176  public function scopeOrderCategory($query, $order)
177  {
178  return $query->leftJoin('categories', 'accessories.category_id', '=', 'categories.id')->orderBy('categories.name', $order);
179  }
180 
189  public function scopeOrderLocation($query, $order)
190  {
191  return $query->leftJoin('locations', 'consumables.location_id', '=', 'locations.id')->orderBy('locations.name', $order);
192  }
193 }
scopeOrderCompany($query, $order)
Query builder scope to order on company.
Definition: Accessory.php:163
assetlog()
Get action logs for this accessory.
Definition: Accessory.php:68
static getSettings()
Definition: Setting.php:33
scopeTextSearch($query, $search)
Query builder scope to search on text.
Definition: Accessory.php:125
scopeOrderLocation($query, $order)
Query builder scope to order on company.
Definition: Accessory.php:189
$rules
Accessory validation rules.
Definition: Accessory.php:24
scopeOrderCategory($query, $order)
Query builder scope to order on company.
Definition: Accessory.php:176
Model for Accessories.
Definition: Accessory.php:13