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.

This commit is contained in:
Daniel Meltzer 2016-05-24 20:05:48 -05:00
parent 45d93ffa8c
commit 0fa82743a1
4 changed files with 20 additions and 97 deletions

View file

@ -155,41 +155,14 @@ class AssetMaintenancesController extends Controller
] + AssetMaintenance::getImprovementOptions(); ] + AssetMaintenance::getImprovementOptions();
// Mark the selected asset, if it came in // Mark the selected asset, if it came in
$selectedAsset = $assetId; $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 $assets = Company::scopeCompanyables(Asset::all(), 'assets.company_id')->lists('detailed_name', 'id');
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
$supplier_list = Helper::suppliersList(); $supplier_list = Helper::suppliersList();
// Render the view // Render the view
return View::make('asset_maintenances/edit') return View::make('asset_maintenances/edit')
->with('asset_list', $asset_element) ->with('asset_list', $assets)
->with('selectedAsset', $selectedAsset) ->with('selectedAsset', $selectedAsset)
->with('supplier_list', $supplier_list) ->with('supplier_list', $supplier_list)
->with('assetMaintenanceType', $assetMaintenanceType) ->with('assetMaintenanceType', $assetMaintenanceType)
@ -321,40 +294,13 @@ class AssetMaintenancesController extends Controller
'' => 'Select an improvement type', '' => 'Select an improvement type',
] + AssetMaintenance::getImprovementOptions(); ] + AssetMaintenance::getImprovementOptions();
// Get the possible assets using a left join to get a list of assets and some other helpful info $assets = Company::scopeCompanyables(Asset::all(), 'assets.company_id')->lists('detailed_name', 'id');
$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 // Get Supplier List
$supplier_list = Helper::suppliersList(); $supplier_list = Helper::suppliersList();
// Render the view // Render the view
return View::make('asset_maintenances/edit') return View::make('asset_maintenances/edit')
->with('asset_list', $asset_element) ->with('asset_list', $assets)
->with('selectedAsset', null) ->with('selectedAsset', null)
->with('supplier_list', $supplier_list) ->with('supplier_list', $supplier_list)
->with('assetMaintenanceType', $assetMaintenanceType) ->with('assetMaintenanceType', $assetMaintenanceType)

View file

@ -439,37 +439,11 @@ class LicensesController extends Controller
// Get the dropdown of users and then pass it to the checkout view // Get the dropdown of users and then pass it to the checkout view
$users_list = Helper::usersList(); $users_list = Helper::usersList();
// Left join to get a list of assets and some other helpful info $assets = Company::scopeCompanyables(Asset::all(), 'assets.company_id')->lists('detailed_name', 'id');
$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); return View::make('licenses/checkout', compact('licenseseat'))
$asset_element[''] = 'Please select an asset'; ->with('users_list', $users_list)
->with('asset_list', $assets);
// 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;
}
return View::make('licenses/checkout', compact('licenseseat'))->with('users_list', $users_list)->with('asset_list', $asset_element);
} }
@ -526,8 +500,8 @@ class LicensesController extends Controller
// Redirect to the asset management page with error // Redirect to the asset management page with error
return redirect()->to('admin/licenses')->with('error', trans('admin/licenses/message.asset_does_not_exist')); return redirect()->to('admin/licenses')->with('error', trans('admin/licenses/message.asset_does_not_exist'));
} }
$was_assigned_to = $is_asset_id->assigned_to;
if (($is_asset_id->assigned_to!=$assigned_to) && ($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; //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')); return redirect()->to('admin/licenses')->with('error', trans('admin/licenses/message.owner_doesnt_match_asset'));
} }

View file

@ -657,13 +657,7 @@ class UsersController extends Controller
$location_list = Helper::locationsList(); $location_list = Helper::locationsList();
$company_list = Helper::companyList(); $company_list = Helper::companyList();
$manager_list = array('' => 'Select a User') + DB::table('users') $manager_list = Helper::managerList();
->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');
// Show the page // Show the page
return View::make('users/edit', compact('groups', 'userGroups', 'permissions', 'userPermissions')) return View::make('users/edit', compact('groups', 'userGroups', 'permissions', 'userPermissions'))

View file

@ -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') public function validationRules($id = '0')
{ {
return $this->rules; return $this->rules;