From 343e13efeee979ab4c854256f260804c1b469770 Mon Sep 17 00:00:00 2001 From: snipe Date: Sun, 17 Nov 2013 20:36:54 -0500 Subject: [PATCH] Checkout/checkin functionality for assets Need to refactor a bit and add to licenses as well --- app/controllers/admin/AssetsController.php | 72 ++++++++++++++++++--- app/routes.php | 2 + app/views/backend/assets/checkout.blade.php | 3 +- app/views/backend/assets/index.blade.php | 2 + 4 files changed, 67 insertions(+), 12 deletions(-) diff --git a/app/controllers/admin/AssetsController.php b/app/controllers/admin/AssetsController.php index a1ef1e4def..61a3294560 100644 --- a/app/controllers/admin/AssetsController.php +++ b/app/controllers/admin/AssetsController.php @@ -213,15 +213,17 @@ class AssetsController extends AdminController { } // Get the dropdown of users and then pass it to the checkout view - $users = DB::table('users')->select(DB::raw('concat (first_name," ",last_name) as full_name,id'))->lists('full_name', 'id'); - return View::make('backend/assets/checkout', compact('asset'))->with('users',$users); + $users_list = array('' => 'Select a User') + DB::table('users')->select(DB::raw('concat (first_name," ",last_name) as full_name, id'))->lists('full_name', 'id'); + + //print_r($users); + return View::make('backend/assets/checkout', compact('asset'))->with('users_list',$users_list); } /** * Check out the asset to a person **/ - public function postCheckout($assetId,$userId) + public function postCheckout($assetId) { // Check if the asset exists if (is_null($asset = Asset::find($assetId))) @@ -230,19 +232,69 @@ class AssetsController extends AdminController { return Redirect::to('admin')->with('error', Lang::get('admin/assets/message.not_found')); } + $user_id = e(Input::get('user_id')); + // Check if the asset exists - if (is_null($user = User::find($userId))) + if (is_null($user = User::find($user_id))) { // Redirect to the asset management page with error - return Redirect::to('admin')->with('error', Lang::get('admin/assets/message.does_not_exist')); + return Redirect::to('admin')->with('error', Lang::get('admin/assets/message.user_does_not_exist')); } - echo 'bar'; - // Delete the asset - //$asset->delete(); + // Declare the rules for the form validation + $rules = array( + 'user_id' => 'required' + ); - // Redirect to the asset management page - //return Redirect::to('admin')->with('success', Lang::get('admin/assets/message.checkout.success')); + // Create a new validator instance from our validation rules + $validator = Validator::make(Input::all(), $rules); + + // If validation fails, we'll exit the operation now. + if ($validator->fails()) + { + // Ooops.. something went wrong + return Redirect::back()->withInput()->withErrors($validator); + } + + // Update the asset data + $asset->assigned_to = e(Input::get('user_id')); + + + // Was the asset updated? + if($asset->save()) + { + // Redirect to the new asset page + return Redirect::to("admin")->with('success', Lang::get('admin/assets/message.checkout.success')); + } + + // Redirect to the asset management page with error + return Redirect::to("assets/$assetId/checkout")->with('error', Lang::get('admin/assets/message.checkout.error')); + } + + /** + * Check out the asset to a person + **/ + public function postCheckin($assetId) + { + // Check if the asset exists + if (is_null($asset = Asset::find($assetId))) + { + // Redirect to the asset management page with error + return Redirect::to('admin')->with('error', Lang::get('admin/assets/message.not_found')); + } + + // Update the asset data + $asset->assigned_to = ''; + + // Was the asset updated? + if($asset->save()) + { + // Redirect to the new asset page + return Redirect::to("admin")->with('success', Lang::get('admin/assets/message.checkout.success')); + } + + // Redirect to the asset management page with error + return Redirect::to("admin")->with('error', Lang::get('admin/assets/message.checkout.error')); } diff --git a/app/routes.php b/app/routes.php index b4dc9de7b1..0cf68da589 100755 --- a/app/routes.php +++ b/app/routes.php @@ -23,6 +23,8 @@ Route::group(array('prefix' => 'assets'), function() Route::get('{assetId}/delete', array('as' => 'delete/asset', 'uses' => 'Controllers\Admin\AssetsController@getDelete')); Route::get('{assetId}/checkout', array('as' => 'checkout/asset', 'uses' => 'Controllers\Admin\AssetsController@getCheckout')); Route::post('{assetId}/checkout', 'Controllers\Admin\AssetsController@postCheckout'); + Route::get('{assetId}/checkin', array('as' => 'checkin/asset', 'uses' => 'Controllers\Admin\AssetsController@postCheckin')); + diff --git a/app/views/backend/assets/checkout.blade.php b/app/views/backend/assets/checkout.blade.php index 784411a6e5..99339ca3d6 100644 --- a/app/views/backend/assets/checkout.blade.php +++ b/app/views/backend/assets/checkout.blade.php @@ -56,12 +56,11 @@
- {{ Form::select('user_id', $users , Input::old('user_id', $asset->user_id)) }} + {{ Form::select('user_id', $users_list , Input::old('user_id', $asset->assigned_to)) }} {{ $errors->first('user_id', ':message') }}
- diff --git a/app/views/backend/assets/index.blade.php b/app/views/backend/assets/index.blade.php index cbbd79475c..f6a22491f3 100755 --- a/app/views/backend/assets/index.blade.php +++ b/app/views/backend/assets/index.blade.php @@ -44,6 +44,7 @@ Assets :: @if ($asset->assigned_to != 0) {{ $asset->assigned_to }} + Checkin @else Checkout @endif @@ -56,6 +57,7 @@ Assets :: @lang('button.edit') @lang('button.delete') + @endforeach