LicensesController.php
Go to the documentation of this file.
1 <?php
2 namespace App\Http\Controllers;
3 
4 use Assets;
5 use Input;
6 use Lang;
11 use DB;
12 use Redirect;
18 use Validator;
19 use View;
20 use Response;
21 use Slack;
22 use Config;
23 use Session;
25 use Auth;
26 
34 {
35 
45  public function getIndex()
46  {
47  // Show the page
48  return View::make('licenses/index');
49  }
50 
51 
60  public function getCreate()
61  {
62 
63  $depreciation_list = Helper::depreciationList();
64  $supplier_list = Helper::suppliersList();
65  $maintained_list = array('' => 'Maintained', '1' => 'Yes', '0' => 'No');
66  $company_list = Helper::companyList();
67 
68  return View::make('licenses/edit')
69  //->with('license_options',$license_options)
70  ->with('depreciation_list', $depreciation_list)
71  ->with('supplier_list', $supplier_list)
72  ->with('maintained_list', $maintained_list)
73  ->with('company_list', $company_list)
74  ->with('license', new License);
75  }
76 
77 
87  public function postCreate()
88  {
89 
90 
91  // get the POST data
92  $new = Input::all();
93 
94  // create a new model instance
95  $license = new License();
96 
97  if (e(Input::get('purchase_cost')) == '') {
98  $license->purchase_cost = null;
99  } else {
100  $license->purchase_cost = e(Input::get('purchase_cost'));
101  }
102 
103  if (e(Input::get('supplier_id')) == '') {
104  $license->supplier_id = null;
105  } else {
106  $license->supplier_id = e(Input::get('supplier_id'));
107  }
108 
109  if (e(Input::get('maintained')) == '') {
110  $license->maintained = 0;
111  } else {
112  $license->maintained = e(Input::get('maintained'));
113  }
114 
115  if (e(Input::get('reassignable')) == '') {
116  $license->reassignable = 0;
117  } else {
118  $license->reassignable = e(Input::get('reassignable'));
119  }
120 
121  if (e(Input::get('purchase_order')) == '') {
122  $license->purchase_order = '';
123  } else {
124  $license->purchase_order = e(Input::get('purchase_order'));
125  }
126 
127  // Save the license data
128  $license->name = e(Input::get('name'));
129  $license->serial = e(Input::get('serial'));
130  $license->license_email = e(Input::get('license_email'));
131  $license->license_name = e(Input::get('license_name'));
132  $license->notes = e(Input::get('notes'));
133  $license->order_number = e(Input::get('order_number'));
134  $license->seats = e(Input::get('seats'));
135  $license->purchase_date = e(Input::get('purchase_date'));
136  $license->purchase_order = e(Input::get('purchase_order'));
137  $license->depreciation_id = e(Input::get('depreciation_id'));
138  $license->company_id = Company::getIdForCurrentUser(Input::get('company_id'));
139  $license->expiration_date = e(Input::get('expiration_date'));
140  $license->user_id = Auth::user()->id;
141 
142  if (($license->purchase_date == "") || ($license->purchase_date == "0000-00-00")) {
143  $license->purchase_date = null;
144  }
145 
146  if (($license->expiration_date == "") || ($license->expiration_date == "0000-00-00")) {
147  $license->expiration_date = null;
148  }
149 
150  if (($license->purchase_cost == "") || ($license->purchase_cost == "0.00")) {
151  $license->purchase_cost = null;
152  }
153 
154  // Was the license created?
155  if ($license->save()) {
156 
157  $insertedId = $license->id;
158  // Save the license seat data
159  for ($x=0; $x<$license->seats; $x++) {
160  $license_seat = new LicenseSeat();
161  $license_seat->license_id = $insertedId;
162  $license_seat->user_id = Auth::user()->id;
163  $license_seat->assigned_to = null;
164  $license_seat->notes = null;
165  $license_seat->save();
166  }
167 
168 
169  // Redirect to the new license page
170  return Redirect::to("admin/licenses")->with('success', trans('admin/licenses/message.create.success'));
171  }
172 
173  return Redirect::back()->withInput()->withErrors($license->getErrors());
174 
175  }
176 
186  public function getEdit($licenseId = null)
187  {
188  // Check if the license exists
189  if (is_null($license = License::find($licenseId))) {
190  // Redirect to the blogs management page
191  return Redirect::to('admin/licenses')->with('error', trans('admin/licenses/message.does_not_exist'));
192  } elseif (!Company::isCurrentUserHasAccess($license)) {
193  return Redirect::to('admin/licenses')->with('error', trans('general.insufficient_permissions'));
194  }
195 
196  if ($license->purchase_date == "0000-00-00") {
197  $license->purchase_date = null;
198  }
199 
200  if ($license->purchase_cost == "0.00") {
201  $license->purchase_cost = null;
202  }
203 
204  // Show the page
205  $license_options = array('' => 'Top Level') + DB::table('assets')->where('id', '!=', $licenseId)->pluck('name', 'id');
206  $depreciation_list = array('0' => trans('admin/licenses/form.no_depreciation')) + Depreciation::pluck('name', 'id')->toArray();
207  $supplier_list = array('' => 'Select Supplier') + Supplier::orderBy('name', 'asc')->pluck('name', 'id')->toArray();
208  $maintained_list = array('' => 'Maintained', '1' => 'Yes', '0' => 'No');
209  $company_list = Helper::companyList();
210 
211  return View::make('licenses/edit', compact('license'))
212  ->with('license_options', $license_options)
213  ->with('depreciation_list', $depreciation_list)
214  ->with('supplier_list', $supplier_list)
215  ->with('company_list', $company_list)
216  ->with('maintained_list', $maintained_list);
217  }
218 
219 
230  public function postEdit($licenseId = null)
231  {
232  // Check if the license exists
233  if (is_null($license = License::find($licenseId))) {
234  // Redirect to the blogs management page
235  return Redirect::to('admin/licenses')->with('error', trans('admin/licenses/message.does_not_exist'));
236  } elseif (!Company::isCurrentUserHasAccess($license)) {
237  return Redirect::to('admin/licenses')->with('error', trans('general.insufficient_permissions'));
238  }
239 
240  // Update the license data
241  $license->name = e(Input::get('name'));
242  $license->serial = e(Input::get('serial'));
243  $license->license_email = e(Input::get('license_email'));
244  $license->license_name = e(Input::get('license_name'));
245  $license->notes = e(Input::get('notes'));
246  $license->order_number = e(Input::get('order_number'));
247  $license->depreciation_id = e(Input::get('depreciation_id'));
248  $license->company_id = Company::getIdForCurrentUser(Input::get('company_id'));
249  $license->purchase_order = e(Input::get('purchase_order'));
250  $license->maintained = e(Input::get('maintained'));
251  $license->reassignable = e(Input::get('reassignable'));
252 
253  if (e(Input::get('supplier_id')) == '') {
254  $license->supplier_id = null;
255  } else {
256  $license->supplier_id = e(Input::get('supplier_id'));
257  }
258 
259  // Update the asset data
260  if (e(Input::get('purchase_date')) == '') {
261  $license->purchase_date = null;
262  } else {
263  $license->purchase_date = e(Input::get('purchase_date'));
264  }
265 
266  if (e(Input::get('expiration_date')) == '') {
267  $license->expiration_date = null;
268  } else {
269  $license->expiration_date = e(Input::get('expiration_date'));
270  }
271 
272  if (e(Input::get('termination_date')) == '') {
273  $license->termination_date = null;
274  } else {
275  $license->termination_date = e(Input::get('termination_date'));
276  }
277 
278  if (e(Input::get('purchase_cost')) == '') {
279  $license->purchase_cost = null;
280  } else {
281  $license->purchase_cost = e(Input::get('purchase_cost'));
282  //$license->purchase_cost = e(Input::get('purchase_cost'));
283  }
284 
285  if (e(Input::get('maintained')) == '') {
286  $license->maintained = 0;
287  } else {
288  $license->maintained = e(Input::get('maintained'));
289  }
290 
291  if (e(Input::get('reassignable')) == '') {
292  $license->reassignable = 0;
293  } else {
294  $license->reassignable = e(Input::get('reassignable'));
295  }
296 
297  if (e(Input::get('purchase_order')) == '') {
298  $license->purchase_order = '';
299  } else {
300  $license->purchase_order = e(Input::get('purchase_order'));
301  }
302 
303  //Are we changing the total number of seats?
304  if ($license->seats != e(Input::get('seats'))) {
305  //Determine how many seats we are dealing with
306  $difference = e(Input::get('seats')) - $license->licenseseats()->count();
307 
308  if ($difference < 0) {
309  //Filter out any license which have a user attached;
310  $seats = $license->licenseseats->filter(function ($seat) {
311  return is_null($seat->user);
312  });
313 
314 
315  //If the remaining collection is as large or larger than the number of seats we want to delete
316  if ($seats->count() >= abs($difference)) {
317  for ($i=1; $i <= abs($difference); $i++) {
318  //Delete the appropriate number of seats
319  $seats->pop()->delete();
320  }
321 
322  //Log the deletion of seats to the log
323  $logaction = new Actionlog();
324  $logaction->asset_id = $license->id;
325  $logaction->asset_type = 'software';
326  $logaction->user_id = Auth::user()->id;
327  $logaction->note = abs($difference)." seats";
328  $logaction->checkedout_to = null;
329  $log = $logaction->logaction('delete seats');
330 
331  } else {
332  // Redirect to the license edit page
333  return Redirect::to("admin/licenses/$licenseId/edit")->with('error', trans('admin/licenses/message.assoc_users'));
334  }
335  } else {
336 
337  for ($i=1; $i <= $difference; $i++) {
338  //Create a seat for this license
339  $license_seat = new LicenseSeat();
340  $license_seat->license_id = $license->id;
341  $license_seat->user_id = Auth::user()->id;
342  $license_seat->assigned_to = null;
343  $license_seat->notes = null;
344  $license_seat->save();
345  }
346 
347  //Log the addition of license to the log.
348  $logaction = new Actionlog();
349  $logaction->asset_id = $license->id;
350  $logaction->asset_type = 'software';
351  $logaction->user_id = Auth::user()->id;
352  $logaction->note = abs($difference)." seats";
353  $log = $logaction->logaction('add seats');
354  }
355  $license->seats = e(Input::get('seats'));
356  }
357 
358  // Was the asset created?
359  if ($license->save()) {
360  // Redirect to the new license page
361  return Redirect::to("admin/licenses/$licenseId/view")->with('success', trans('admin/licenses/message.update.success'));
362  }
363 
364 
365  // Redirect to the license edit page
366  return Redirect::to("admin/licenses/$licenseId/edit")->with('error', trans('admin/licenses/message.update.error'));
367 
368  }
369 
379  public function getDelete($licenseId)
380  {
381  // Check if the license exists
382  if (is_null($license = License::find($licenseId))) {
383  // Redirect to the license management page
384  return Redirect::to('admin/licenses')->with('error', trans('admin/licenses/message.not_found'));
385  } elseif (!Company::isCurrentUserHasAccess($license)) {
386  return Redirect::to('admin/licenses')->with('error', trans('general.insufficient_permissions'));
387  }
388 
389  if (($license->assignedcount()) && ($license->assignedcount() > 0)) {
390 
391  // Redirect to the license management page
392  return Redirect::to('admin/licenses')->with('error', trans('admin/licenses/message.assoc_users'));
393 
394  } else {
395 
396  // Delete the license and the associated license seats
397  DB::table('license_seats')
398  ->where('id', $license->id)
399  ->update(array('assigned_to' => null,'asset_id' => null));
400 
401  $licenseseats = $license->licenseseats();
402  $licenseseats->delete();
403  $license->delete();
404 
405 
406 
407 
408  // Redirect to the licenses management page
409  return Redirect::to('admin/licenses')->with('success', trans('admin/licenses/message.delete.success'));
410  }
411 
412 
413  }
414 
415 
427  public function getCheckout($seatId)
428  {
429  // Check if the license seat exists
430  if (is_null($licenseseat = LicenseSeat::find($seatId))) {
431  // Redirect to the asset management page with error
432  return Redirect::to('admin/licenses')->with('error', trans('admin/licenses/message.not_found'));
433  } elseif (!Company::isCurrentUserHasAccess($licenseseat->license)) {
434  return Redirect::to('admin/licenses')->with('error', trans('general.insufficient_permissions'));
435  }
436 
437  // Get the dropdown of users and then pass it to the checkout view
438  $users_list = array('' => 'Select a User') + DB::table('users')->select(DB::raw('concat(last_name,", ",first_name," (",username,")") as full_name, id'))->whereNull('deleted_at')->orderBy('last_name', 'asc')->orderBy('first_name', 'asc')->lists('full_name', 'id');
439 
440 
441  // Left join to get a list of assets and some other helpful info
442  $asset = DB::table('assets')
443  ->leftJoin('users', 'users.id', '=', 'assets.assigned_to')
444  ->leftJoin('models', 'assets.model_id', '=', 'models.id')
445  ->select(
446  'assets.id',
447  'assets.name',
448  'first_name',
449  'last_name',
450  'asset_tag',
451  DB::raw('concat(first_name," ",last_name) as full_name, assets.id as id, models.name as modelname')
452  )
453  ->whereNull('assets.deleted_at')
454  ->get();
455 
456  $asset_array = json_decode(json_encode($asset), true);
457  $asset_element[''] = 'Please select an asset';
458 
459  // Build a list out of the data results
460  for ($x=0; $x<count($asset_array); $x++) {
461 
462  if ($asset_array[$x]['full_name']!='') {
463  $full_name = ' ('.$asset_array[$x]['full_name'].') '.$asset_array[$x]['modelname'];
464  } else {
465  $full_name = ' (Unassigned) '.$asset_array[$x]['modelname'];
466  }
467  $asset_element[$asset_array[$x]['id']] = $asset_array[$x]['asset_tag'].' - '.$asset_array[$x]['name'].$full_name;
468 
469  }
470 
471  return View::make('licenses/checkout', compact('licenseseat'))->with('users_list', $users_list)->with('asset_list', $asset_element);
472 
473  }
474 
475 
476 
486  public function postCheckout($seatId)
487  {
488 
489  $licenseseat = LicenseSeat::find($seatId);
490  $assigned_to = e(Input::get('assigned_to'));
491  $asset_id = e(Input::get('asset_id'));
492  $user = Auth::user();
493 
494  if (!Company::isCurrentUserHasAccess($licenseseat->license)) {
495  return Redirect::to('admin/licenses')->with('error', trans('general.insufficient_permissions'));
496  }
497 
498  // Declare the rules for the form validation
499  $rules = array(
500 
501  'note' => 'string',
502  'asset_id' => 'required_without:assigned_to',
503  );
504 
505  // Create a new validator instance from our validation rules
506  $validator = Validator::make(Input::all(), $rules);
507 
508  // If validation fails, we'll exit the operation now.
509  if ($validator->fails()) {
510  // Ooops.. something went wrong
511  return Redirect::back()->withInput()->withErrors($validator);
512  }
513 
514  if ($assigned_to!='') {
515  // Check if the user exists
516  if (is_null($is_assigned_to = User::find($assigned_to))) {
517  // Redirect to the asset management page with error
518  return Redirect::to('admin/licenses')->with('error', trans('admin/licenses/message.user_does_not_exist'));
519  }
520  }
521 
522  if ($asset_id!='') {
523 
524  if (is_null($is_asset_id = Asset::find($asset_id))) {
525  // Redirect to the asset management page with error
526  return Redirect::to('admin/licenses')->with('error', trans('admin/licenses/message.asset_does_not_exist'));
527  }
528 
529  if (($is_asset_id->assigned_to!=$assigned_to) && ($assigned_to!='')) {
530  //echo 'asset assigned to: '.$is_asset_id->assigned_to.'<br>license assigned to: '.$assigned_to;
531  return Redirect::to('admin/licenses')->with('error', trans('admin/licenses/message.owner_doesnt_match_asset'));
532  }
533 
534  }
535 
536 
537 
538  // Check if the asset exists
539  if (is_null($licenseseat)) {
540  // Redirect to the asset management page with error
541  return Redirect::to('admin/licenses')->with('error', trans('admin/licenses/message.not_found'));
542  }
543 
544  if (Input::get('asset_id') == '') {
545  $licenseseat->asset_id = null;
546  } else {
547  $licenseseat->asset_id = e(Input::get('asset_id'));
548  }
549 
550  // Update the asset data
551  if (e(Input::get('assigned_to')) == '') {
552  $licenseseat->assigned_to = null;
553 
554  } else {
555  $licenseseat->assigned_to = e(Input::get('assigned_to'));
556  }
557 
558  // Was the asset updated?
559  if ($licenseseat->save()) {
560 
561  $logaction = new Actionlog();
562 
563  //$logaction->location_id = $assigned_to->location_id;
564  $logaction->asset_type = 'software';
565  $logaction->user_id = Auth::user()->id;
566  $logaction->note = e(Input::get('note'));
567  $logaction->asset_id = $licenseseat->license_id;
568 
569 
570  $license = License::find($licenseseat->license_id);
571  $settings = Setting::getSettings();
572 
573 
574  // Update the asset data
575  if (e(Input::get('assigned_to')) == '') {
576  $logaction->checkedout_to = null;
577  $slack_msg = strtoupper($logaction->asset_type).' license <'.config('app.url').'/admin/licenses/'.$license->id.'/view'.'|'.$license->name.'> checked out to <'.config('app.url').'/hardware/'.$is_asset_id->id.'/view|'.$is_asset_id->showAssetName().'> by <'.config('app.url').'/admin/users/'.$user->id.'/view'.'|'.$user->fullName().'>.';
578  } else {
579  $logaction->checkedout_to = e(Input::get('assigned_to'));
580  $slack_msg = strtoupper($logaction->asset_type).' license <'.config('app.url').'/admin/licenses/'.$license->id.'/view'.'|'.$license->name.'> checked out to <'.config('app.url').'/admin/users/'.$is_assigned_to->id.'/view|'.$is_assigned_to->fullName().'> by <'.config('app.url').'/admin/users/'.$user->id.'/view'.'|'.$user->fullName().'>.';
581  }
582 
583 
584 
585  if ($settings->slack_endpoint) {
586 
587 
588  $slack_settings = [
589  'username' => $settings->botname,
590  'channel' => $settings->slack_channel,
591  'link_names' => true
592  ];
593 
594  $client = new \Maknz\Slack\Client($settings->slack_endpoint, $slack_settings);
595 
596  try {
597  $client->attach([
598  'color' => 'good',
599  'fields' => [
600  [
601  'title' => 'Checked Out:',
602  'value' => $slack_msg
603  ],
604  [
605  'title' => 'Note:',
606  'value' => e($logaction->note)
607  ],
608 
609 
610 
611  ]
612  ])->send('License Checked Out');
613 
614  } catch (Exception $e) {
615 
616  }
617 
618  }
619 
620  $log = $logaction->logaction('checkout');
621 
622 
623  // Redirect to the new asset page
624  return Redirect::to("admin/licenses")->with('success', trans('admin/licenses/message.checkout.success'));
625  }
626 
627  // Redirect to the asset management page with error
628  return Redirect::to('admin/licenses/$assetId/checkout')->with('error', trans('admin/licenses/message.create.error'))->with('license', new License);
629  }
630 
631 
641  public function getCheckin($seatId = null, $backto = null)
642  {
643  // Check if the asset exists
644  if (is_null($licenseseat = LicenseSeat::find($seatId))) {
645  // Redirect to the asset management page with error
646  return Redirect::to('admin/licenses')->with('error', trans('admin/licenses/message.not_found'));
647  } elseif (!Company::isCurrentUserHasAccess($licenseseat->license)) {
648  return Redirect::to('admin/licenses')->with('error', trans('general.insufficient_permissions'));
649  }
650  return View::make('licenses/checkin', compact('licenseseat'))->with('backto', $backto);
651 
652  }
653 
654 
655 
666  public function postCheckin($seatId = null, $backto = null)
667  {
668  // Check if the asset exists
669  if (is_null($licenseseat = LicenseSeat::find($seatId))) {
670  // Redirect to the asset management page with error
671  return Redirect::to('admin/licenses')->with('error', trans('admin/licenses/message.not_found'));
672  }
673 
674  $license = License::find($licenseseat->license_id);
675 
676  if (!Company::isCurrentUserHasAccess($license)) {
677  return Redirect::to('admin/licenses')->with('error', trans('general.insufficient_permissions'));
678  }
679 
680  if (!$license->reassignable) {
681  // Not allowed to checkin
682  Session::flash('error', 'License not reassignable.');
683  return Redirect::back()->withInput();
684  }
685 
686  // Declare the rules for the form validation
687  $rules = array(
688  'note' => 'string',
689  'notes' => 'string',
690  );
691 
692  // Create a new validator instance from our validation rules
693  $validator = Validator::make(Input::all(), $rules);
694 
695  // If validation fails, we'll exit the operation now.
696  if ($validator->fails()) {
697  // Ooops.. something went wrong
698  return Redirect::back()->withInput()->withErrors($validator);
699  }
700  $return_to = $licenseseat->assigned_to;
701  $logaction = new Actionlog();
702  $logaction->checkedout_to = $licenseseat->assigned_to;
703 
704  // Update the asset data
705  $licenseseat->assigned_to = null;
706  $licenseseat->asset_id = null;
707 
708  $user = Auth::user();
709 
710  // Was the asset updated?
711  if ($licenseseat->save()) {
712  $logaction->asset_id = $licenseseat->license_id;
713  $logaction->location_id = null;
714  $logaction->asset_type = 'software';
715  $logaction->note = e(Input::get('note'));
716  $logaction->user_id = $user->id;
717 
718  $settings = Setting::getSettings();
719 
720  if ($settings->slack_endpoint) {
721 
722 
723  $slack_settings = [
724  'username' => $settings->botname,
725  'channel' => $settings->slack_channel,
726  'link_names' => true
727  ];
728 
729  $client = new \Maknz\Slack\Client($settings->slack_endpoint, $slack_settings);
730 
731  try {
732  $client->attach([
733  'color' => 'good',
734  'fields' => [
735  [
736  'title' => 'Checked In:',
737  'value' => strtoupper($logaction->asset_type).' <'.config('app.url').'/admin/licenses/'.$license->id.'/view'.'|'.$license->name.'> checked in by <'.config('app.url').'/admin/users/'.$user->id.'/view'.'|'.$user->fullName().'>.'
738  ],
739  [
740  'title' => 'Note:',
741  'value' => e($logaction->note)
742  ],
743 
744  ]
745  ])->send('License Checked In');
746 
747  } catch (Exception $e) {
748 
749  }
750 
751  }
752 
753 
754  $log = $logaction->logaction('checkin from');
755 
756 
757 
758  if ($backto=='user') {
759  return Redirect::to("admin/users/".$return_to.'/view')->with('success', trans('admin/licenses/message.checkin.success'));
760  } else {
761  return Redirect::to("admin/licenses/".$licenseseat->license_id."/view")->with('success', trans('admin/licenses/message.checkin.success'));
762  }
763 
764  }
765 
766  // Redirect to the license page with error
767  return Redirect::to("admin/licenses")->with('error', trans('admin/licenses/message.checkin.error'));
768  }
769 
778  public function getView($licenseId = null)
779  {
780 
781  $license = License::find($licenseId);
782 
783  if (isset($license->id)) {
784 
785  if (!Company::isCurrentUserHasAccess($license)) {
786  return Redirect::to('admin/licenses')->with('error', trans('general.insufficient_permissions'));
787  }
788  return View::make('licenses/view', compact('license'));
789 
790  } else {
791  // Prepare the error message
792  $error = trans('admin/licenses/message.does_not_exist', compact('id'));
793 
794  // Redirect to the user management page
795  return Redirect::route('licenses')->with('error', $error);
796  }
797  }
798 
799  public function getClone($licenseId = null)
800  {
801  // Check if the license exists
802  if (is_null($license_to_clone = License::find($licenseId))) {
803  // Redirect to the blogs management page
804  return Redirect::to('admin/licenses')->with('error', trans('admin/licenses/message.does_not_exist'));
805  } elseif (!Company::isCurrentUserHasAccess($license_to_clone)) {
806  return Redirect::to('admin/licenses')->with('error', trans('general.insufficient_permissions'));
807  }
808 
809  // Show the page
810  $license_options = array('0' => 'Top Level') + License::pluck('name', 'id')->toArray();
811  $maintained_list = array('' => 'Maintained', '1' => 'Yes', '0' => 'No');
812  $company_list = Helper::companyList();
813  //clone the orig
814  $license = clone $license_to_clone;
815  $license->id = null;
816  $license->serial = null;
817 
818  // Show the page
819  $depreciation_list = Helper::depreciationList();
820  $supplier_list = Helper::suppliersList();
821  return View::make('licenses/edit')
822  ->with('license_options', $license_options)
823  ->with('depreciation_list', $depreciation_list)
824  ->with('supplier_list', $supplier_list)
825  ->with('license', $license)
826  ->with('maintained_list', $maintained_list)
827  ->with('company_list', $company_list);
828 
829  }
830 
831 
841  public function postUpload($licenseId = null)
842  {
843  $license = License::find($licenseId);
844 
845  // the license is valid
846  $destinationPath = config('app.private_uploads').'/licenses';
847 
848  if (isset($license->id)) {
849 
850 
851  if (!Company::isCurrentUserHasAccess($license)) {
852  return Redirect::to('admin/licenses')->with('error', trans('general.insufficient_permissions'));
853  }
854 
855  if (Input::hasFile('licensefile')) {
856 
857  foreach (Input::file('licensefile') as $file) {
858 
859  $rules = array(
860  'licensefile' => 'required|mimes:png,gif,jpg,jpeg,doc,docx,pdf,txt,zip,rar|max:2000'
861  );
862  $validator = Validator::make(array('licensefile'=> $file), $rules);
863 
864  if ($validator->passes()) {
865 
866  $extension = $file->getClientOriginalExtension();
867  $filename = 'license-'.$license->id.'-'.str_random(8);
868  $filename .= '-'.str_slug($file->getClientOriginalName()).'.'.$extension;
869  $upload_success = $file->move($destinationPath, $filename);
870 
871  //Log the deletion of seats to the log
872  $logaction = new Actionlog();
873  $logaction->asset_id = $license->id;
874  $logaction->asset_type = 'software';
875  $logaction->user_id = Auth::user()->id;
876  $logaction->note = e(Input::get('notes'));
877  $logaction->checkedout_to = null;
878  $logaction->created_at = date("Y-m-d h:i:s");
879  $logaction->filename = $filename;
880  $log = $logaction->logaction('uploaded');
881  } else {
882  return Redirect::back()->with('error', trans('admin/licenses/message.upload.invalidfiles'));
883  }
884 
885 
886  }
887 
888  if ($upload_success) {
889  return Redirect::back()->with('success', trans('admin/licenses/message.upload.success'));
890  } else {
891  return Redirect::back()->with('success', trans('admin/licenses/message.upload.error'));
892  }
893 
894  } else {
895  return Redirect::back()->with('error', trans('admin/licenses/message.upload.nofiles'));
896  }
897 
898 
899  } else {
900  // Prepare the error message
901  $error = trans('admin/licenses/message.does_not_exist', compact('id'));
902 
903  // Redirect to the licence management page
904  return Redirect::route('licenses')->with('error', $error);
905  }
906  }
907 
908 
918  public function getDeleteFile($licenseId = null, $fileId = null)
919  {
920  $license = License::find($licenseId);
921  $destinationPath = config('app.private_uploads').'/licenses';
922 
923  // the license is valid
924  if (isset($license->id)) {
925 
926 
927  if (!Company::isCurrentUserHasAccess($license)) {
928  return Redirect::to('admin/licenses')->with('error', trans('general.insufficient_permissions'));
929  }
930 
931  $log = Actionlog::find($fileId);
932  $full_filename = $destinationPath.'/'.$log->filename;
933  if (file_exists($full_filename)) {
934  unlink($destinationPath.'/'.$log->filename);
935  }
936  $log->delete();
937  return Redirect::back()->with('success', trans('admin/licenses/message.deletefile.success'));
938 
939  } else {
940  // Prepare the error message
941  $error = trans('admin/licenses/message.does_not_exist', compact('id'));
942 
943  // Redirect to the licence management page
944  return Redirect::route('licenses')->with('error', $error);
945  }
946  }
947 
948 
949 
959  public function displayFile($licenseId = null, $fileId = null)
960  {
961 
962  $license = License::find($licenseId);
963 
964  // the license is valid
965  if (isset($license->id)) {
966 
967  if (!Company::isCurrentUserHasAccess($license)) {
968  return Redirect::to('admin/licenses')->with('error', trans('general.insufficient_permissions'));
969  }
970 
971  $log = Actionlog::find($fileId);
972  $file = $log->get_src('licenses');
973  return Response::download($file);
974  } else {
975  // Prepare the error message
976  $error = trans('admin/licenses/message.does_not_exist', compact('id'));
977 
978  // Redirect to the licence management page
979  return Redirect::route('licenses')->with('error', $error);
980  }
981  }
982 
983 
992  public function getDatatable()
993  {
994  $licenses = License::with('company');
995 
996  if (Input::has('search')) {
997  $licenses = $licenses->TextSearch(Input::get('search'));
998  }
999 
1000  $allowed_columns = ['id','name','purchase_cost','expiration_date','purchase_order','order_number','notes','purchase_date','serial'];
1001  $order = Input::get('order') === 'asc' ? 'asc' : 'desc';
1002  $sort = in_array(Input::get('sort'), $allowed_columns) ? e(Input::get('sort')) : 'created_at';
1003 
1004  $licenses = $licenses->orderBy($sort, $order);
1005 
1006  $licenseCount = $licenses->count();
1007  $licenses = $licenses->skip(Input::get('offset'))->take(Input::get('limit'))->get();
1008 
1009  $rows = array();
1010 
1011  foreach ($licenses as $license) {
1012  $actions = '<span style="white-space: nowrap;"><a href="'.route('freecheckout/license', $license->id).'" class="btn btn-primary btn-sm" style="margin-right:5px;" '.(($license->remaincount() > 0) ? '' : 'disabled').'>'.trans('general.checkout').'</a> <a href="'.route('clone/license', $license->id).'" class="btn btn-info btn-sm" style="margin-right:5px;" title="Clone asset"><i class="fa fa-files-o"></i></a><a href="'.route('update/license', $license->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/license', $license->id).'" data-content="'.trans('admin/licenses/message.delete.confirm').'" data-title="'.trans('general.delete').' '.htmlspecialchars($license->name).'?" onClick="return false;"><i class="fa fa-trash icon-white"></i></a></span>';
1013 
1014  $rows[] = array(
1015  'id' => $license->id,
1016  'name' => (string) link_to('/admin/licenses/'.$license->id.'/view', $license->name),
1017  'serial' => (string) link_to('/admin/licenses/'.$license->id.'/view', mb_strimwidth($license->serial, 0, 50, "...")),
1018  'totalSeats' => $license->totalSeatsByLicenseID(),
1019  'remaining' => $license->remaincount(),
1020  'license_name' => e($license->license_name),
1021  'license_email' => e($license->license_email),
1022  'purchase_date' => ($license->purchase_date) ? $license->purchase_date : '',
1023  'expiration_date' => ($license->expiration_date) ? $license->expiration_date : '',
1024  'purchase_cost' => ($license->purchase_cost) ? $license->purchase_cost : '',
1025  'purchase_order' => ($license->purchase_order) ? e($license->purchase_order) : '',
1026  'order_number' => ($license->order_number) ? e($license->order_number) : '',
1027  'notes' => ($license->notes) ? e($license->notes) : '',
1028  'actions' => $actions,
1029  'companyName' => is_null($license->company) ? '' : e($license->company->name)
1030  );
1031  }
1032 
1033  $data = array('total' => $licenseCount, 'rows' => $rows);
1034 
1035  return $data;
1036  }
1037 
1051  public function getFreeLicense($licenseId)
1052  {
1053  // Check if the asset exists
1054  if (is_null($license = License::find($licenseId))) {
1055  // Redirect to the asset management page with error
1056  return Redirect::to('admin/licenses')->with('error', trans('admin/licenses/message.not_found'));
1057  }
1058  $seatId = $license->freeSeat($licenseId);
1059  return Redirect::to('admin/licenses/'.$seatId.'/checkout');
1060  }
1061 }
postUpload($licenseId=null)
Validates and stores files associated with a license.
postCheckout($seatId)
Validates and stores the license checkout action.
Model for the Actionlog (the table that keeps a historical log of checkouts, checkins, and updates).
Definition: Actionlog.php:15
getCreate()
Returns a form view that allows an admin to create a new licence.
static getIdForCurrentUser($unescaped_input)
Definition: Company.php:81
getCheckin($seatId=null, $backto=null)
Makes the form view to check a license seat back into inventory.
static companyList()
Definition: Helper.php:61
getView($licenseId=null)
Makes the license detail page.
displayFile($licenseId=null, $fileId=null)
Allows the selected file to be viewed.
getDelete($licenseId)
Checks to see whether the selected license can be deleted, and if it can, marks it as deleted...
getCheckout($seatId)
Provides the form view for checking out a license to a user.
static isCurrentUserHasAccess($companyable)
Definition: Company.php:96
static getSettings()
Definition: Setting.php:33
getIndex()
Returns a view that invokes the ajax tables which actually contains the content for the licenses list...
getDeleteFile($licenseId=null, $fileId=null)
Deletes the selected license file.
This controller handles all actions related to Licenses for the Snipe-IT Asset Management application...
static suppliersList()
Definition: Helper.php:79
postCreate()
Validates and stores the license form data submitted from the new license form.
getFreeLicense($licenseId)
Generates the next free seat ID for checkout.
static depreciationList()
Definition: Helper.php:125
postCheckin($seatId=null, $backto=null)
Validates and stores the license checkin action.
getEdit($licenseId=null)
Returns a form with existing license data to allow an admin to update license information.
postEdit($licenseId=null)
Validates and stores the license form data submitted from the edit license form.
getDatatable()
Generates a JSON response to populate the licence index datatables.