From 4b98711e07db0ac3f3cc31a60598a7ba5cf1669c Mon Sep 17 00:00:00 2001 From: Daniel Meltzer Date: Tue, 17 May 2016 23:31:57 -0500 Subject: [PATCH 1/5] Replace the assorted concat() methods with a full_name method in the User model and adjust queries accordingly --- app/Helpers/Helper.php | 23 ++++++++++----------- app/Http/Controllers/LicensesController.php | 3 +-- app/Models/User.php | 4 ++++ 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/app/Helpers/Helper.php b/app/Helpers/Helper.php index f2f9aba4d0..f8749c407e 100644 --- a/app/Helpers/Helper.php +++ b/app/Helpers/Helper.php @@ -164,11 +164,11 @@ class Helper public static function managerList() { - $manager_list = array('' => '') + User::select(DB::raw('concat(last_name,", ",first_name," (",username,")") as full_name, id')) - ->whereNull('deleted_at', 'and') - ->orderBy('last_name', 'asc') - ->orderBy('first_name', 'asc') - ->pluck('full_name', 'id')->toArray(); + $manager_list = User::where('deleted_at', '=', null) + ->orderBy('last_name', 'asc') + ->orderBy('first_name', 'asc')->get() + ->lists('full_name', 'id'); + return $manager_list; } @@ -187,13 +187,12 @@ class Helper public static function usersList() { - $users_list = array('' => trans('general.select_user')) + DB::table('users') - ->select(DB::raw('concat(last_name,", ",first_name," (",username,")") as full_name, id')) - ->whereNull('deleted_at') - ->where('show_in_list','=',1) - ->orderBy('last_name', 'asc') - ->orderBy('first_name', 'asc') - ->pluck('full_name', 'id'); + $users_list = User::where('deleted_at', '=', null) + ->where('show_in_list','=',1) + ->orderBy('last_name', 'asc') + ->orderBy('first_name', 'asc')->get() + ->lists('full_name', 'id'); + return $users_list; } diff --git a/app/Http/Controllers/LicensesController.php b/app/Http/Controllers/LicensesController.php index 5013317519..ef17637a3b 100755 --- a/app/Http/Controllers/LicensesController.php +++ b/app/Http/Controllers/LicensesController.php @@ -435,8 +435,7 @@ class LicensesController extends Controller } // Get the dropdown of users and then pass it to the checkout view - $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'); - + $users_list = Helper::usersList(); // Left join to get a list of assets and some other helpful info $asset = DB::table('assets') diff --git a/app/Models/User.php b/app/Models/User.php index 53bdcc6401..c24648b23b 100755 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -118,6 +118,10 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon return "{$this->first_name} {$this->last_name}"; } + public function getFullNameAttribute() + { + return $this->first_name . " " . $this->last_name; + } /** * Returns the user Gravatar image url. From 45d93ffa8c4261958fe04a28058528a9dd21e846 Mon Sep 17 00:00:00 2001 From: Daniel Meltzer Date: Tue, 17 May 2016 23:44:27 -0500 Subject: [PATCH 2/5] Use a transaction while creating license seats. Makes adding a license with a large number of seats much more managable. --- app/Http/Controllers/LicensesController.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/Http/Controllers/LicensesController.php b/app/Http/Controllers/LicensesController.php index ef17637a3b..b1db080758 100755 --- a/app/Http/Controllers/LicensesController.php +++ b/app/Http/Controllers/LicensesController.php @@ -156,6 +156,7 @@ class LicensesController extends Controller $insertedId = $license->id; // Save the license seat data + DB::transaction(function() use (&$insertedId,&$license) { for ($x=0; $x<$license->seats; $x++) { $license_seat = new LicenseSeat(); $license_seat->license_id = $insertedId; @@ -164,6 +165,7 @@ class LicensesController extends Controller $license_seat->notes = null; $license_seat->save(); } + }); // Redirect to the new license page From 0fa82743a1c0b179490f111cce64930a88708730 Mon Sep 17 00:00:00 2001 From: Daniel Meltzer Date: Tue, 24 May 2016 20:05:48 -0500 Subject: [PATCH 3/5] Remove remaining DB::Raw(concat) calls to make things more sqlite friendly. This adds one new method to the Asset Model to return the formatted string that was used by the license and asset maintence controller. It also fixes a potential sqlite-only issue where '' and null are different. --- .../AssetMaintenancesController.php | 62 ++----------------- app/Http/Controllers/LicensesController.php | 38 ++---------- app/Http/Controllers/UsersController.php | 8 +-- app/Models/Asset.php | 9 +++ 4 files changed, 20 insertions(+), 97 deletions(-) diff --git a/app/Http/Controllers/AssetMaintenancesController.php b/app/Http/Controllers/AssetMaintenancesController.php index 3c2bb56a9d..a748f3d1d8 100644 --- a/app/Http/Controllers/AssetMaintenancesController.php +++ b/app/Http/Controllers/AssetMaintenancesController.php @@ -155,41 +155,14 @@ class AssetMaintenancesController extends Controller ] + AssetMaintenance::getImprovementOptions(); // Mark the selected asset, if it came in $selectedAsset = $assetId; - // Get the possible assets using a left join to get a list of assets and some other helpful info - $asset = Company::scopeCompanyables(DB::table('assets'), 'assets.company_id') - ->leftJoin('users', 'users.id', '=', 'assets.assigned_to') - ->leftJoin('models', 'assets.model_id', '=', 'models.id') - ->select( - 'assets.id', - 'assets.name', - 'first_name', - 'last_name', - 'asset_tag', - DB::raw('concat(first_name," ",last_name) as full_name, assets.id as id, models.name as modelname') - ) - ->whereNull('assets.deleted_at') - ->get(); - $asset_array = json_decode(json_encode($asset), true); - $asset_element[ '' ] = 'Please select an asset'; - // Build a list out of the data results - for ($x = 0; $x < count($asset_array); $x++) { + $assets = Company::scopeCompanyables(Asset::all(), 'assets.company_id')->lists('detailed_name', 'id'); - if ($asset_array[ $x ][ 'full_name' ] != '') { - $full_name = ' (' . $asset_array[ $x ][ 'full_name' ] . ') ' . $asset_array[ $x ][ 'modelname' ]; - } else { - $full_name = ' (Unassigned) ' . $asset_array[ $x ][ 'modelname' ]; - } - $asset_element[ $asset_array[ $x ][ 'id' ] ] = - $asset_array[ $x ][ 'asset_tag' ] . ' - ' . $asset_array[ $x ][ 'name' ] . $full_name; - } - - // Get Supplier List $supplier_list = Helper::suppliersList(); // Render the view return View::make('asset_maintenances/edit') - ->with('asset_list', $asset_element) + ->with('asset_list', $assets) ->with('selectedAsset', $selectedAsset) ->with('supplier_list', $supplier_list) ->with('assetMaintenanceType', $assetMaintenanceType) @@ -321,40 +294,13 @@ class AssetMaintenancesController extends Controller '' => 'Select an improvement type', ] + AssetMaintenance::getImprovementOptions(); - // Get the possible assets using a left join to get a list of assets and some other helpful info - $asset = Company::scopeCompanyables(DB::table('assets'), 'assets.company_id') - ->leftJoin('users', 'users.id', '=', 'assets.assigned_to') - ->leftJoin('models', 'assets.model_id', '=', 'models.id') - ->select( - 'assets.id', - 'assets.name', - 'first_name', - 'last_name', - 'asset_tag', - DB::raw('concat(first_name," ",last_name) as full_name, assets.id as id, models.name as modelname') - ) - ->whereNull('assets.deleted_at') - ->get(); - $asset_array = json_decode(json_encode($asset), true); - $asset_element[ '' ] = 'Please select an asset'; - - // Build a list out of the data results - for ($x = 0; $x < count($asset_array); $x++) { - - if ($asset_array[ $x ][ 'full_name' ] != '') { - $full_name = ' (' . $asset_array[ $x ][ 'full_name' ] . ') ' . $asset_array[ $x ][ 'modelname' ]; - } else { - $full_name = ' (Unassigned) ' . $asset_array[ $x ][ 'modelname' ]; - } - $asset_element[ $asset_array[ $x ][ 'id' ] ] = - $asset_array[ $x ][ 'asset_tag' ] . ' - ' . $asset_array[ $x ][ 'name' ] . $full_name; - } + $assets = Company::scopeCompanyables(Asset::all(), 'assets.company_id')->lists('detailed_name', 'id'); // Get Supplier List $supplier_list = Helper::suppliersList(); // Render the view return View::make('asset_maintenances/edit') - ->with('asset_list', $asset_element) + ->with('asset_list', $assets) ->with('selectedAsset', null) ->with('supplier_list', $supplier_list) ->with('assetMaintenanceType', $assetMaintenanceType) diff --git a/app/Http/Controllers/LicensesController.php b/app/Http/Controllers/LicensesController.php index b1db080758..217ddac54e 100755 --- a/app/Http/Controllers/LicensesController.php +++ b/app/Http/Controllers/LicensesController.php @@ -439,37 +439,11 @@ class LicensesController extends Controller // Get the dropdown of users and then pass it to the checkout view $users_list = Helper::usersList(); - // Left join to get a list of assets and some other helpful info - $asset = DB::table('assets') - ->leftJoin('users', 'users.id', '=', 'assets.assigned_to') - ->leftJoin('models', 'assets.model_id', '=', 'models.id') - ->select( - 'assets.id', - 'assets.name', - 'first_name', - 'last_name', - 'asset_tag', - DB::raw('concat(first_name," ",last_name) as full_name, assets.id as id, models.name as modelname') - ) - ->whereNull('assets.deleted_at') - ->get(); + $assets = Company::scopeCompanyables(Asset::all(), 'assets.company_id')->lists('detailed_name', 'id'); - $asset_array = json_decode(json_encode($asset), true); - $asset_element[''] = 'Please select an asset'; - - // Build a list out of the data results - for ($x=0; $xwith('users_list', $users_list)->with('asset_list', $asset_element); + return View::make('licenses/checkout', compact('licenseseat')) + ->with('users_list', $users_list) + ->with('asset_list', $assets); } @@ -526,8 +500,8 @@ class LicensesController extends Controller // Redirect to the asset management page with error return redirect()->to('admin/licenses')->with('error', trans('admin/licenses/message.asset_does_not_exist')); } - - if (($is_asset_id->assigned_to!=$assigned_to) && ($assigned_to!='')) { + $was_assigned_to = $is_asset_id->assigned_to; + if (($was_assigned_to!=$assigned_to) && !is_null($was_assigned_to) && ($was_assigned_to != '')) { //echo 'asset assigned to: '.$is_asset_id->assigned_to.'
license assigned to: '.$assigned_to; return redirect()->to('admin/licenses')->with('error', trans('admin/licenses/message.owner_doesnt_match_asset')); } diff --git a/app/Http/Controllers/UsersController.php b/app/Http/Controllers/UsersController.php index 9a7f4f3b37..6c8024a472 100755 --- a/app/Http/Controllers/UsersController.php +++ b/app/Http/Controllers/UsersController.php @@ -657,13 +657,7 @@ class UsersController extends Controller $location_list = Helper::locationsList(); $company_list = Helper::companyList(); - $manager_list = array('' => 'Select a User') + DB::table('users') - ->select(DB::raw('concat(last_name,", ",first_name," (",email,")") as full_name, id')) - ->whereNull('deleted_at') - ->where('id', '!=', $id) - ->orderBy('last_name', 'asc') - ->orderBy('first_name', 'asc') - ->lists('full_name', 'id'); + $manager_list = Helper::managerList(); // Show the page return View::make('users/edit', compact('groups', 'userGroups', 'permissions', 'userPermissions')) diff --git a/app/Models/Asset.php b/app/Models/Asset.php index 4e66640c1b..5f2fa2d622 100644 --- a/app/Models/Asset.php +++ b/app/Models/Asset.php @@ -172,6 +172,15 @@ class Asset extends Depreciable } + public function getDetailedNameAttribute() { + $user = $this->assigneduser; + if($user) { + $user_name = $user->fullName(); + } else { + $user_name = "Unassigned"; + } + return $this->asset_tag . ' - ' . $this->name . ' (' . $user_name . ') ' . $this->model->name; + } public function validationRules($id = '0') { return $this->rules; From 4af5cdd64f5a3cd21a5e8574f1645bcc6ff8a25b Mon Sep 17 00:00:00 2001 From: Daniel Meltzer Date: Wed, 25 May 2016 20:32:45 -0500 Subject: [PATCH 4/5] asset_id is not allowed to be null in asset_log according to db schema. 0 it out here to make sqlite happy. --- app/Http/Controllers/AccessoriesController.php | 1 + app/Http/Controllers/ConsumablesController.php | 1 + 2 files changed, 2 insertions(+) diff --git a/app/Http/Controllers/AccessoriesController.php b/app/Http/Controllers/AccessoriesController.php index 5c29dc7642..736a0db964 100755 --- a/app/Http/Controllers/AccessoriesController.php +++ b/app/Http/Controllers/AccessoriesController.php @@ -313,6 +313,7 @@ class AccessoriesController extends Controller $logaction = new Actionlog(); $logaction->accessory_id = $accessory->id; + $logaction->asset_id = 0; $logaction->checkedout_to = $accessory->assigned_to; $logaction->asset_type = 'accessory'; $logaction->location_id = $user->location_id; diff --git a/app/Http/Controllers/ConsumablesController.php b/app/Http/Controllers/ConsumablesController.php index a3a3899192..7a6f01c72d 100644 --- a/app/Http/Controllers/ConsumablesController.php +++ b/app/Http/Controllers/ConsumablesController.php @@ -319,6 +319,7 @@ class ConsumablesController extends Controller $logaction->consumable_id = $consumable->id; $logaction->checkedout_to = $consumable->assigned_to; $logaction->asset_type = 'consumable'; + $logaction->asset_id = 0; $logaction->location_id = $user->location_id; $logaction->user_id = Auth::user()->id; $logaction->note = e(Input::get('note')); From f2a5337ad24d4671345303c05a3aa044e3164a01 Mon Sep 17 00:00:00 2001 From: Daniel Meltzer Date: Wed, 25 May 2016 23:26:15 -0500 Subject: [PATCH 5/5] Simplify --- app/Models/Asset.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/Models/Asset.php b/app/Models/Asset.php index 5f2fa2d622..3a48b9df6b 100644 --- a/app/Models/Asset.php +++ b/app/Models/Asset.php @@ -173,8 +173,7 @@ class Asset extends Depreciable public function getDetailedNameAttribute() { - $user = $this->assigneduser; - if($user) { + if($this->assigned_user) { $user_name = $user->fullName(); } else { $user_name = "Unassigned";