Skip to content

A free open source IT asset/license management system

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.

Daniel Meltzer0fa82743a1c0
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++) {
-
- 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
+ $assets = Company::scopeCompanyables(Asset::all(), 'assets.company_id')->lists('detailed_name', 'id');
+
$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();
-
- $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');
- }
-
- return View::make('licenses/checkout', compact('licenseseat'))->with('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.'<br>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;