ConsumablesController.php
Go to the documentation of this file.
1 <?php
2 
3 namespace App\Http\Controllers;
4 
10 use App\Models\User;
11 use Auth;
12 use Config;
13 use DB;
14 use Input;
15 use Lang;
16 use Mail;
17 use Redirect;
18 use Slack;
19 use Str;
20 use View;
21 
29 {
38  public function getIndex()
39  {
40  return View::make('consumables/index');
41  }
42 
43 
52  public function getCreate()
53  {
54  // Show the page
55  $category_list = array('' => '') + DB::table('categories')->where('category_type', '=', 'consumable')->whereNull('deleted_at')->orderBy('name', 'ASC')->lists('name', 'id');
56  $company_list = Helper::companyList();
57  $location_list = Helper::locationsList();
58 
59  return View::make('consumables/edit')
60  ->with('consumable', new Consumable)
61  ->with('category_list', $category_list)
62  ->with('company_list', $company_list)
63  ->with('location_list', $location_list);
64  }
65 
66 
75  public function postCreate()
76  {
77  $consumable = new Consumable();
78  $consumable->name = e(Input::get('name'));
79  $consumable->category_id = e(Input::get('category_id'));
80  $consumable->location_id = e(Input::get('location_id'));
81  $consumable->company_id = Company::getIdForCurrentUser(Input::get('company_id'));
82  $consumable->order_number = e(Input::get('order_number'));
83  $consumable->min_amt = e(Input::get('min_amt'));
84 
85  if (e(Input::get('purchase_date')) == '') {
86  $consumable->purchase_date = null;
87  } else {
88  $consumable->purchase_date = e(Input::get('purchase_date'));
89  }
90 
91  if (e(Input::get('purchase_cost')) == '0.00') {
92  $consumable->purchase_cost = null;
93  } else {
94  $consumable->purchase_cost = e(Input::get('purchase_cost'));
95  }
96 
97  $consumable->qty = e(Input::get('qty'));
98  $consumable->user_id = Auth::user()->id;
99 
100  // Was the consumable created?
101  if ($consumable->save()) {
102  // Redirect to the new consumable page
103  return Redirect::to("admin/consumables")->with('success', trans('admin/consumables/message.create.success'));
104  }
105 
106  return Redirect::back()->withInput()->withErrors($consumable->getErrors());
107 
108 
109  }
110 
120  public function getEdit($consumableId = null)
121  {
122  // Check if the consumable exists
123  if (is_null($consumable = Consumable::find($consumableId))) {
124  // Redirect to the blogs management page
125  return Redirect::to('admin/consumables')->with('error', trans('admin/consumables/message.does_not_exist'));
126  } elseif (!Company::isCurrentUserHasAccess($consumable)) {
127  return Redirect::to('admin/consumables')->with('error', trans('general.insufficient_permissions'));
128  }
129 
130  $category_list = Helper::categoryList();
131  $company_list = Helper::companyList();
132  $location_list = Helper::locationsList();
133 
134  return View::make('consumables/edit', compact('consumable'))
135  ->with('category_list', $category_list)
136  ->with('company_list', $company_list)
137  ->with('location_list', $location_list);
138  }
139 
140 
150  public function postEdit($consumableId = null)
151  {
152  if (is_null($consumable = Consumable::find($consumableId))) {
153  return Redirect::to('admin/consumables')->with('error', trans('admin/consumables/message.does_not_exist'));
154  } elseif (!Company::isCurrentUserHasAccess($consumable)) {
155  return Redirect::to('admin/consumables')->with('error', trans('general.insufficient_permissions'));
156  }
157 
158  $consumable->name = e(Input::get('name'));
159  $consumable->category_id = e(Input::get('category_id'));
160  $consumable->location_id = e(Input::get('location_id'));
161  $consumable->company_id = Company::getIdForCurrentUser(Input::get('company_id'));
162  $consumable->order_number = e(Input::get('order_number'));
163  $consumable->min_amt = e(Input::get('min_amt'));
164 
165  if (e(Input::get('purchase_date')) == '') {
166  $consumable->purchase_date = null;
167  } else {
168  $consumable->purchase_date = e(Input::get('purchase_date'));
169  }
170 
171  if (e(Input::get('purchase_cost')) == '0.00') {
172  $consumable->purchase_cost = null;
173  } else {
174  $consumable->purchase_cost = e(Input::get('purchase_cost'));
175  }
176 
177  $consumable->qty = e(Input::get('qty'));
178 
179  if ($consumable->save()) {
180  return Redirect::to("admin/consumables")->with('success', trans('admin/consumables/message.update.success'));
181  }
182 
183  return Redirect::back()->withInput()->withErrors($consumable->getErrors());
184 
185  }
186 
195  public function getDelete($consumableId)
196  {
197  // Check if the blog post exists
198  if (is_null($consumable = Consumable::find($consumableId))) {
199  // Redirect to the blogs management page
200  return Redirect::to('admin/consumables')->with('error', trans('admin/consumables/message.not_found'));
201  } elseif (!Company::isCurrentUserHasAccess($consumable)) {
202  return Redirect::to('admin/consumables')->with('error', trans('general.insufficient_permissions'));
203  }
204 
205  $consumable->delete();
206 
207  // Redirect to the locations management page
208  return Redirect::to('admin/consumables')->with('success', trans('admin/consumables/message.delete.success'));
209 
210  }
211 
212 
213 
223  public function getView($consumableId = null)
224  {
225  $consumable = Consumable::find($consumableId);
226 
227  if (isset($consumable->id)) {
228 
229 
230  if (!Company::isCurrentUserHasAccess($consumable)) {
231  return Redirect::to('admin/consumables')->with('error', trans('general.insufficient_permissions'));
232  } else {
233  return View::make('consumables/view', compact('consumable'));
234  }
235  } else {
236  // Prepare the error message
237  $error = trans('admin/consumables/message.does_not_exist', compact('id'));
238 
239  // Redirect to the user management page
240  return Redirect::route('consumables')->with('error', $error);
241  }
242 
243 
244  }
245 
255  public function getCheckout($consumableId)
256  {
257  // Check if the consumable exists
258  if (is_null($consumable = Consumable::find($consumableId))) {
259  // Redirect to the consumable management page with error
260  return Redirect::to('consumables')->with('error', trans('admin/consumables/message.not_found'));
261  } elseif (!Company::isCurrentUserHasAccess($consumable)) {
262  return Redirect::to('admin/consumables')->with('error', trans('general.insufficient_permissions'));
263  }
264 
265  // Get the dropdown of users and then pass it to the checkout view
266  $users_list = Helper::usersList();
267 
268  return View::make('consumables/checkout', compact('consumable'))->with('users_list', $users_list);
269 
270  }
271 
281  public function postCheckout($consumableId)
282  {
283  // Check if the consumable exists
284  if (is_null($consumable = Consumable::find($consumableId))) {
285  // Redirect to the consumable management page with error
286  return Redirect::to('consumables')->with('error', trans('admin/consumables/message.not_found'));
287  } elseif (!Company::isCurrentUserHasAccess($consumable)) {
288  return Redirect::to('admin/consumables')->with('error', trans('general.insufficient_permissions'));
289  }
290 
291  $admin_user = Auth::user();
292  $assigned_to = e(Input::get('assigned_to'));
293 
294  // Check if the user exists
295  if (is_null($user = User::find($assigned_to))) {
296  // Redirect to the consumable management page with error
297  return Redirect::to('admin/consumables')->with('error', trans('admin/consumables/message.user_does_not_exist'));
298  }
299 
300  // Update the consumable data
301  $consumable->assigned_to = e(Input::get('assigned_to'));
302 
303  $consumable->users()->attach($consumable->id, array(
304  'consumable_id' => $consumable->id,
305  'user_id' => $admin_user->id,
306  'assigned_to' => e(Input::get('assigned_to'))));
307 
308  $logaction = new Actionlog();
309  $logaction->consumable_id = $consumable->id;
310  $logaction->checkedout_to = $consumable->assigned_to;
311  $logaction->asset_type = 'consumable';
312  $logaction->location_id = $user->location_id;
313  $logaction->user_id = Auth::user()->id;
314  $logaction->note = e(Input::get('note'));
315 
316  $settings = Setting::getSettings();
317 
318  if ($settings->slack_endpoint) {
319 
320  $slack_settings = [
321  'username' => $settings->botname,
322  'channel' => $settings->slack_channel,
323  'link_names' => true
324  ];
325 
326  $client = new \Maknz\Slack\Client($settings->slack_endpoint, $slack_settings);
327 
328  try {
329  $client->attach([
330  'color' => 'good',
331  'fields' => [
332  [
333  'title' => 'Checked Out:',
334  'value' => strtoupper($logaction->asset_type).' <'.config('app.url').'/admin/consumables/'.$consumable->id.'/view'.'|'.$consumable->name.'> checked out to <'.config('app.url').'/admin/users/'.$user->id.'/view|'.$user->fullName().'> by <'.config('app.url').'/admin/users/'.$admin_user->id.'/view'.'|'.$admin_user->fullName().'>.'
335  ],
336  [
337  'title' => 'Note:',
338  'value' => e($logaction->note)
339  ],
340  ]
341  ])->send('Consumable Checked Out');
342 
343  } catch (Exception $e) {
344 
345  }
346  }
347 
348 
349  $log = $logaction->logaction('checkout');
350 
351  $consumable_user = DB::table('consumables_users')->where('assigned_to', '=', $consumable->assigned_to)->where('consumable_id', '=', $consumable->id)->first();
352 
353  $data['log_id'] = $logaction->id;
354  $data['eula'] = $consumable->getEula();
355  $data['first_name'] = $user->first_name;
356  $data['item_name'] = $consumable->name;
357  $data['checkout_date'] = $logaction->created_at;
358  $data['item_tag'] = '';
359  $data['expected_checkin'] = '';
360  $data['note'] = $logaction->note;
361  $data['require_acceptance'] = $consumable->requireAcceptance();
362 
363 
364  if (($consumable->requireAcceptance()=='1') || ($consumable->getEula())) {
365 
366  Mail::send('emails.accept-asset', $data, function ($m) use ($user) {
367  $m->to($user->email, $user->first_name . ' ' . $user->last_name);
368  $m->subject('Confirm consumable delivery');
369  });
370  }
371 
372  // Redirect to the new consumable page
373  return Redirect::to("admin/consumables")->with('success', trans('admin/consumables/message.checkout.success'));
374 
375 
376 
377  }
378 
379 
389  public function getDatatable()
390  {
391  $consumables = Consumable::select('consumables.*')->whereNull('consumables.deleted_at')
392  ->with('company', 'location', 'category', 'users');
393 
394  if (Input::has('search')) {
395  $consumables = $consumables->TextSearch(e(Input::get('search')));
396  }
397 
398  if (Input::has('offset')) {
399  $offset = e(Input::get('offset'));
400  } else {
401  $offset = 0;
402  }
403 
404  if (Input::has('limit')) {
405  $limit = e(Input::get('limit'));
406  } else {
407  $limit = 50;
408  }
409 
410  $allowed_columns = ['id','name','order_number','min_amt','purchase_date','purchase_cost','companyName','category'];
411  $order = Input::get('order') === 'asc' ? 'asc' : 'desc';
412  $sort = in_array(Input::get('sort'), $allowed_columns) ? Input::get('sort') : 'created_at';
413 
414  switch ($sort) {
415  case 'category':
416  $consumables = $consumables->OrderCategory($order);
417  break;
418  case 'location':
419  $consumables = $consumables->OrderLocation($order);
420  break;
421  case 'companyName':
422  $consumables = $consumables->OrderCompany($order);
423  break;
424  default:
425  $consumables = $consumables->orderBy($sort, $order);
426  break;
427  }
428 
429  $consumCount = $consumables->count();
430  $consumables = $consumables->skip($offset)->take($limit)->get();
431 
432  $rows = array();
433 
434  foreach ($consumables as $consumable) {
435  $actions = '<nobr><a href="'.route('checkout/consumable', $consumable->id).'" style="margin-right:5px;" class="btn btn-info btn-sm" '.(($consumable->numRemaining() > 0 ) ? '' : ' disabled').'>'.trans('general.checkout').'</a><a href="'.route('update/consumable', $consumable->id).'" class="btn btn-warning btn-sm" style="margin-right:5px;"><i class="fa fa-pencil icon-white"></i></a><a data-html="false" class="btn delete-asset btn-danger btn-sm" data-toggle="modal" href="'.route('delete/consumable', $consumable->id).'" data-content="'.trans('admin/consumables/message.delete.confirm').'" data-title="'.trans('general.delete').' '.htmlspecialchars($consumable->name).'?" onClick="return false;"><i class="fa fa-trash icon-white"></i></a></nobr>';
436  $company = $consumable->company;
437 
438  $rows[] = array(
439  'id' => $consumable->id,
440  'name' => (string)link_to('admin/consumables/'.$consumable->id.'/view', e($consumable->name)),
441  'location' => ($consumable->location) ? e($consumable->location->name) : '',
442  'min_amt' => e($consumable->min_amt),
443  'qty' => e($consumable->qty),
444  'category' => ($consumable->category) ? e($consumable->category->name) : 'Missing category',
445  'order_number' => e($consumable->order_number),
446  'purchase_date' => e($consumable->purchase_date),
447  'purchase_cost' => ($consumable->purchase_cost!='') ? number_format($consumable->purchase_cost, 2): '' ,
448  'numRemaining' => $consumable->numRemaining(),
449  'actions' => $actions,
450  'companyName' => is_null($company) ? '' : e($company->name),
451  );
452  }
453 
454  $data = array('total' => $consumCount, 'rows' => $rows);
455 
456  return $data;
457 
458  }
459 
469  public function getDataView($consumableId)
470  {
471  //$consumable = Consumable::find($consumableID);
472  $consumable = Consumable::with(array('consumableAssigments'=>
473  function ($query) {
474  $query->orderBy('created_at', 'DESC');
475  },
476  'consumableAssigments.admin'=> function ($query) {
477  },
478  'consumableAssigments.user'=> function ($query) {
479  },
480  ))->find($consumableId);
481 
482  // $consumable->load('consumableAssigments.admin','consumableAssigments.user');
483 
484  if (!Company::isCurrentUserHasAccess($consumable)) {
485  return ['total' => 0, 'rows' => []];
486  }
487 
488  $rows = array();
489 
490  foreach ($consumable->consumableAssigments as $consumable_assignment) {
491  $rows[] = array(
492  'name' => (string)link_to('/admin/users/'.$consumable_assignment->user->id.'/view', e($consumable_assignment->user->fullName())),
493  'created_at' => ($consumable_assignment->created_at->format('Y-m-d H:i:s')=='-0001-11-30 00:00:00') ? '' : $consumable_assignment->created_at->format('Y-m-d H:i:s'),
494  'admin' => ($consumable_assignment->admin) ? e($consumable_assignment->admin->fullName()) : '',
495  );
496  }
497 
498  $consumableCount = $consumable->users->count();
499  $data = array('total' => $consumableCount, 'rows' => $rows);
500  return $data;
501  }
502 }
Model for the Actionlog (the table that keeps a historical log of checkouts, checkins, and updates).
Definition: Actionlog.php:15
postCreate()
Validate and store new consumable data.
static getIdForCurrentUser($unescaped_input)
Definition: Company.php:81
static categoryList()
Definition: Helper.php:70
getCheckout($consumableId)
Return a view to checkout a consumable to a user.
static locationsList()
Definition: Helper.php:94
static companyList()
Definition: Helper.php:61
postEdit($consumableId=null)
Returns a form view to edit a consumable.
static isCurrentUserHasAccess($companyable)
Definition: Company.php:96
postCheckout($consumableId)
Saves the checkout information.
static getSettings()
Definition: Setting.php:33
getView($consumableId=null)
Return a view to display component information.
getCreate()
Return a view to display the form view to create a new consumable.
getDataView($consumableId)
Returns a JSON response containing details on the users associated with this consumable.
getEdit($consumableId=null)
Returns a form view to edit a consumable.
getDelete($consumableId)
Delete a consumable.
getDatatable()
Returns the JSON response containing the the consumables data.
static usersList()
Definition: Helper.php:138
This controller handles all actions related to Consumables for the Snipe-IT Asset Management applicat...
getIndex()
Return a view to display component information.