AssetMaintenancesController.php
Go to the documentation of this file.
1 <?php
2 namespace App\Http\Controllers;
3 
5 use Carbon\Carbon;
7 use DB;
8 use Input;
9 use Lang;
10 use Log;
11 use Mail;
12 use Redirect;
13 use Response;
14 use Slack;
15 use Str;
17 use TCPDF;
18 use Validator;
19 use View;
23 
31 {
32 
42  private static function getInsufficientPermissionsRedirect()
43  {
44  return redirect()->route('asset_maintenances')
45  ->with('error', trans('general.insufficient_permissions'));
46  }
47 
59  public function getIndex()
60  {
61 
62  return View::make('asset_maintenances/index');
63  }
64 
65 
75  public function getDatatable()
76  {
77  $maintenances = AssetMaintenance::with('asset', 'supplier', 'asset.company')
78  ->whereNull('deleted_at');
79 
80  if (Input::has('search')) {
81  $maintenances = $maintenances->TextSearch(e(Input::get('search')));
82  }
83 
84  if (Input::has('offset')) {
85  $offset = e(Input::get('offset'));
86  } else {
87  $offset = 0;
88  }
89 
90  if (Input::has('limit')) {
91  $limit = e(Input::get('limit'));
92  } else {
93  $limit = 50;
94  }
95 
96  $allowed_columns = ['id','title','asset_maintenance_time','asset_maintenance_type','cost','start_date','completion_date','notes'];
97  $order = Input::get('order') === 'asc' ? 'asc' : 'desc';
98  $sort = in_array(Input::get('sort'), $allowed_columns) ? e(Input::get('sort')) : 'created_at';
99 
100  $maintenances->orderBy($sort, $order);
101 
102  $maintenancesCount = $maintenances->count();
103  $maintenances = $maintenances->skip($offset)->take($limit)->get();
104 
105  $rows = array();
106  $settings = Setting::getSettings();
107 
108  foreach ($maintenances as $maintenance) {
109 
110  $actions = '<nobr><a href="'.route('update/asset_maintenance', $maintenance->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/asset_maintenance', $maintenance->id).'" data-content="'.trans('admin/asset_maintenances/message.delete.confirm').'" data-title="'.trans('general.delete').' '.htmlspecialchars($maintenance->title).'?" onClick="return false;"><i class="fa fa-trash icon-white"></i></a></nobr>';
111 
112  if (($maintenance->cost) && ($maintenance->asset->assetloc) && ($maintenance->asset->assetloc->currency!='')) {
113  $maintenance_cost = $maintenance->asset->assetloc->currency.$maintenance->cost;
114  } else {
115  $maintenance_cost = $settings->default_currency.$maintenance->cost;
116  }
117 
118  $company = $maintenance->asset->company;
119 
120  $rows[] = array(
121  'id' => $maintenance->id,
122  'asset_name' => (string)link_to('/hardware/'.$maintenance->asset->id.'/view', $maintenance->asset->showAssetName()) ,
123  'title' => $maintenance->title,
124  'notes' => $maintenance->notes,
125  'supplier' => $maintenance->supplier->name,
126  'cost' => $maintenance_cost,
127  'asset_maintenance_type' => e($maintenance->asset_maintenance_type),
128  'start_date' => $maintenance->start_date,
129  'asset_maintenance_time' => $maintenance->asset_maintenance_time,
130  'completion_date' => $maintenance->completion_date,
131  'actions' => $actions,
132  'companyName' => is_null($company) ? '' : $company->name
133  );
134  }
135 
136  $data = array('total' => $maintenancesCount, 'rows' => $rows);
137  return $data;
138 
139  }
140 
150  public function getCreate($assetId = null)
151  {
152  // Prepare Asset Maintenance Type List
153  $assetMaintenanceType = [
154  '' => 'Select an asset maintenance type',
156  // Mark the selected asset, if it came in
157  $selectedAsset = $assetId;
158  // Get the possible assets using a left join to get a list of assets and some other helpful info
159  $asset = Company::scopeCompanyables(DB::table('assets'), 'assets.company_id')
160  ->leftJoin('users', 'users.id', '=', 'assets.assigned_to')
161  ->leftJoin('models', 'assets.model_id', '=', 'models.id')
162  ->select(
163  'assets.id',
164  'assets.name',
165  'first_name',
166  'last_name',
167  'asset_tag',
168  DB::raw('concat(first_name," ",last_name) as full_name, assets.id as id, models.name as modelname')
169  )
170  ->whereNull('assets.deleted_at')
171  ->get();
172  $asset_array = json_decode(json_encode($asset), true);
173  $asset_element[ '' ] = 'Please select an asset';
174 
175  // Build a list out of the data results
176  for ($x = 0; $x < count($asset_array); $x++) {
177 
178  if ($asset_array[ $x ][ 'full_name' ] != '') {
179  $full_name = ' (' . $asset_array[ $x ][ 'full_name' ] . ') ' . $asset_array[ $x ][ 'modelname' ];
180  } else {
181  $full_name = ' (Unassigned) ' . $asset_array[ $x ][ 'modelname' ];
182  }
183  $asset_element[ $asset_array[ $x ][ 'id' ] ] =
184  $asset_array[ $x ][ 'asset_tag' ] . ' - ' . $asset_array[ $x ][ 'name' ] . $full_name;
185  }
186 
187  // Get Supplier List
188  $supplier_list = Helper::suppliersList();
189 
190  // Render the view
191  return View::make('asset_maintenances/edit')
192  ->with('asset_list', $asset_element)
193  ->with('selectedAsset', $selectedAsset)
194  ->with('supplier_list', $supplier_list)
195  ->with('assetMaintenanceType', $assetMaintenanceType)
196  ->with('assetMaintenance', new AssetMaintenance);
197  }
198 
208  public function postCreate()
209  {
210 
211  // get the POST data
212  $new = Input::all();
213 
214  // create a new model instance
215  $assetMaintenance = new AssetMaintenance();
216 
217 
218  if (e(Input::get('supplier_id')) == '') {
219  $assetMaintenance->supplier_id = null;
220  } else {
221  $assetMaintenance->supplier_id = e(Input::get('supplier_id'));
222  }
223 
224  if (e(Input::get('is_warranty')) == '') {
225  $assetMaintenance->is_warranty = 0;
226  } else {
227  $assetMaintenance->is_warranty = e(Input::get('is_warranty'));
228  }
229 
230  if (e(Input::get('cost')) == '') {
231  $assetMaintenance->cost = '';
232  } else {
233  $assetMaintenance->cost = e(Input::get('cost'));
234  }
235 
236  if (e(Input::get('notes')) == '') {
237  $assetMaintenance->notes = null;
238  } else {
239  $assetMaintenance->notes = e(Input::get('notes'));
240  }
241 
242  $asset = Asset::find(e(Input::get('asset_id')));
243 
244  if (!Company::isCurrentUserHasAccess($asset)) {
245  return static::getInsufficientPermissionsRedirect();
246  }
247 
248  // Save the asset maintenance data
249  $assetMaintenance->asset_id = e(Input::get('asset_id'));
250  $assetMaintenance->asset_maintenance_type = e(Input::get('asset_maintenance_type'));
251  $assetMaintenance->title = e(Input::get('title'));
252  $assetMaintenance->start_date = e(Input::get('start_date'));
253  $assetMaintenance->completion_date = e(Input::get('completion_date'));
254 
255  if (( $assetMaintenance->completion_date == "" )
256  || ( $assetMaintenance->completion_date == "0000-00-00" )
257  ) {
258  $assetMaintenance->completion_date = null;
259  }
260 
261  if (( $assetMaintenance->completion_date !== "" )
262  && ( $assetMaintenance->completion_date !== "0000-00-00" )
263  && ( $assetMaintenance->start_date !== "" )
264  && ( $assetMaintenance->start_date !== "0000-00-00" )
265  ) {
266  $startDate = Carbon::parse($assetMaintenance->start_date);
267  $completionDate = Carbon::parse($assetMaintenance->completion_date);
268  $assetMaintenance->asset_maintenance_time = $completionDate->diffInDays($startDate);
269  }
270 
271  // Was the asset maintenance created?
272  if ($assetMaintenance->save()) {
273 
274  // Redirect to the new asset maintenance page
275  return Redirect::to("admin/asset_maintenances")
276  ->with('success', trans('admin/asset_maintenances/message.create.success'));
277  }
278 
279  return redirect()->back()->withInput()->withErrors($assetMaintenance->getErrors());
280 
281 
282 
283 
284  }
285 
296  public function getEdit($assetMaintenanceId = null)
297  {
298  // Check if the asset maintenance exists
299  if (is_null($assetMaintenance = AssetMaintenance::find($assetMaintenanceId))) {
300  // Redirect to the improvement management page
301  return Redirect::to('admin/asset_maintenances')
302  ->with('error', trans('admin/asset_maintenances/message.not_found'));
303  } elseif (!Company::isCurrentUserHasAccess($assetMaintenance->asset)) {
304  return static::getInsufficientPermissionsRedirect();
305  }
306 
307  if ($assetMaintenance->completion_date == '0000-00-00') {
308  $assetMaintenance->completion_date = null;
309  }
310 
311  if ($assetMaintenance->start_date == '0000-00-00') {
312  $assetMaintenance->start_date = null;
313  }
314 
315  if ($assetMaintenance->cost == '0.00') {
316  $assetMaintenance->cost = null;
317  }
318 
319  // Prepare Improvement Type List
320  $assetMaintenanceType = [
321  '' => 'Select an improvement type',
323 
324  // Get the possible assets using a left join to get a list of assets and some other helpful info
325  $asset = Company::scopeCompanyables(DB::table('assets'), 'assets.company_id')
326  ->leftJoin('users', 'users.id', '=', 'assets.assigned_to')
327  ->leftJoin('models', 'assets.model_id', '=', 'models.id')
328  ->select(
329  'assets.id',
330  'assets.name',
331  'first_name',
332  'last_name',
333  'asset_tag',
334  DB::raw('concat(first_name," ",last_name) as full_name, assets.id as id, models.name as modelname')
335  )
336  ->whereNull('assets.deleted_at')
337  ->get();
338  $asset_array = json_decode(json_encode($asset), true);
339  $asset_element[ '' ] = 'Please select an asset';
340 
341  // Build a list out of the data results
342  for ($x = 0; $x < count($asset_array); $x++) {
343 
344  if ($asset_array[ $x ][ 'full_name' ] != '') {
345  $full_name = ' (' . $asset_array[ $x ][ 'full_name' ] . ') ' . $asset_array[ $x ][ 'modelname' ];
346  } else {
347  $full_name = ' (Unassigned) ' . $asset_array[ $x ][ 'modelname' ];
348  }
349  $asset_element[ $asset_array[ $x ][ 'id' ] ] =
350  $asset_array[ $x ][ 'asset_tag' ] . ' - ' . $asset_array[ $x ][ 'name' ] . $full_name;
351  }
352  // Get Supplier List
353  $supplier_list = Helper::suppliersList();
354 
355  // Render the view
356  return View::make('asset_maintenances/edit')
357  ->with('asset_list', $asset_element)
358  ->with('selectedAsset', null)
359  ->with('supplier_list', $supplier_list)
360  ->with('assetMaintenanceType', $assetMaintenanceType)
361  ->with('assetMaintenance', $assetMaintenance);
362 
363  }
364 
375  public function postEdit($assetMaintenanceId = null)
376  {
377 
378  // get the POST data
379  $new = Input::all();
380 
381  // Check if the asset maintenance exists
382  if (is_null($assetMaintenance = AssetMaintenance::find($assetMaintenanceId))) {
383  // Redirect to the asset maintenance management page
384  return Redirect::to('admin/asset_maintenances')
385  ->with('error', trans('admin/asset_maintenances/message.not_found'));
386  } elseif (!Company::isCurrentUserHasAccess($assetMaintenance->asset)) {
387  return static::getInsufficientPermissionsRedirect();
388  }
389 
390 
391 
392  if (e(Input::get('supplier_id')) == '') {
393  $assetMaintenance->supplier_id = null;
394  } else {
395  $assetMaintenance->supplier_id = e(Input::get('supplier_id'));
396  }
397 
398  if (e(Input::get('is_warranty')) == '') {
399  $assetMaintenance->is_warranty = 0;
400  } else {
401  $assetMaintenance->is_warranty = e(Input::get('is_warranty'));
402  }
403 
404  if (e(Input::get('cost')) == '') {
405  $assetMaintenance->cost = '';
406  } else {
407  $assetMaintenance->cost = e(Input::get('cost'));
408  }
409 
410  if (e(Input::get('notes')) == '') {
411  $assetMaintenance->notes = null;
412  } else {
413  $assetMaintenance->notes = e(Input::get('notes'));
414  }
415 
416  $asset = Asset::find(e(Input::get('asset_id')));
417 
418  if (!Company::isCurrentUserHasAccess($asset)) {
419  return static::getInsufficientPermissionsRedirect();
420  }
421 
422  // Save the asset maintenance data
423  $assetMaintenance->asset_id = e(Input::get('asset_id'));
424  $assetMaintenance->asset_maintenance_type = e(Input::get('asset_maintenance_type'));
425  $assetMaintenance->title = e(Input::get('title'));
426  $assetMaintenance->start_date = e(Input::get('start_date'));
427  $assetMaintenance->completion_date = e(Input::get('completion_date'));
428 
429  if (( $assetMaintenance->completion_date == "" )
430  || ( $assetMaintenance->completion_date == "0000-00-00" )
431  ) {
432  $assetMaintenance->completion_date = null;
433  if (( $assetMaintenance->asset_maintenance_time !== 0 )
434  || ( !is_null($assetMaintenance->asset_maintenance_time) )
435  ) {
436  $assetMaintenance->asset_maintenance_time = null;
437  }
438  }
439 
440  if (( $assetMaintenance->completion_date !== "" )
441  && ( $assetMaintenance->completion_date !== "0000-00-00" )
442  && ( $assetMaintenance->start_date !== "" )
443  && ( $assetMaintenance->start_date !== "0000-00-00" )
444  ) {
445  $startDate = Carbon::parse($assetMaintenance->start_date);
446  $completionDate = Carbon::parse($assetMaintenance->completion_date);
447  $assetMaintenance->asset_maintenance_time = $completionDate->diffInDays($startDate);
448  }
449 
450  // Was the asset maintenance created?
451  if ($assetMaintenance->save()) {
452 
453  // Redirect to the new asset maintenance page
454  return Redirect::to("admin/asset_maintenances")
455  ->with('success', trans('admin/asset_maintenances/message.create.success'));
456  }
457  return Redirect::back() ->withInput()->withErrors($assetMaintenance->getErrors());
458 
459 
460  }
461 
471  public function getDelete($assetMaintenanceId)
472  {
473  // Check if the asset maintenance exists
474  if (is_null($assetMaintenance = AssetMaintenance::find($assetMaintenanceId))) {
475  // Redirect to the asset maintenance management page
476  return Redirect::to('admin/asset_maintenances')
477  ->with('error', trans('admin/asset_maintenances/message.not_found'));
478  } elseif (!Company::isCurrentUserHasAccess($assetMaintenance->asset)) {
479  return static::getInsufficientPermissionsRedirect();
480  }
481 
482  // Delete the asset maintenance
483  $assetMaintenance->delete();
484 
485  // Redirect to the asset_maintenance management page
486  return Redirect::to('admin/asset_maintenances')
487  ->with('success', trans('admin/asset_maintenances/message.delete.success'));
488  }
489 
499  public function getView($assetMaintenanceId)
500  {
501  // Check if the asset maintenance exists
502  if (is_null($assetMaintenance = AssetMaintenance::find($assetMaintenanceId))) {
503  // Redirect to the asset maintenance management page
504  return Redirect::to('admin/asset_maintenances')
505  ->with('error', trans('admin/asset_maintenances/message.not_found'));
506  } elseif (!Company::isCurrentUserHasAccess($assetMaintenance->asset)) {
507  return static::getInsufficientPermissionsRedirect();
508  }
509 
510  return View::make('asset_maintenances/view')->with('assetMaintenance', $assetMaintenance);
511  }
512 }
getDatatable()
Generates the JSON response for asset maintenances listing view.
getView($assetMaintenanceId)
View an asset maintenance.
getIndex()
Returns a view that invokes the ajax tables which actually contains the content for the asset mainten...
postCreate()
Validates and stores the new asset maintenance.
static getImprovementOptions()
getImprovementOptions
Model for Asset Maintenances.
This controller handles all actions related to Asset Maintenance for the Snipe-IT Asset Management ap...
getCreate($assetId=null)
Returns a form view to create a new asset maintenance.
getEdit($assetMaintenanceId=null)
Returns a form view to edit a selected asset maintenance.
static isCurrentUserHasAccess($companyable)
Definition: Company.php:96
static getSettings()
Definition: Setting.php:33
static scopeCompanyables($query, $column= 'company_id')
Definition: Company.php:130
getDelete($assetMaintenanceId)
Delete an asset maintenance.
static suppliersList()
Definition: Helper.php:79
postEdit($assetMaintenanceId=null)
Validates and stores an update to an asset maintenance.