diff --git a/app/Http/Controllers/Api/AssetMaintenancesController.php b/app/Http/Controllers/Api/AssetMaintenancesController.php index 931e8e51c08c..a26935cc594d 100644 --- a/app/Http/Controllers/Api/AssetMaintenancesController.php +++ b/app/Http/Controllers/Api/AssetMaintenancesController.php @@ -36,7 +36,8 @@ public function index(Request $request) { $this->authorize('view', Asset::class); - $maintenances = AssetMaintenance::select('asset_maintenances.*')->with('asset', 'asset.model', 'asset.location', 'asset.defaultLoc', 'supplier', 'asset.company', 'admin'); + $maintenances = AssetMaintenance::select('asset_maintenances.*') + ->with('asset', 'asset.model', 'asset.location', 'asset.defaultLoc', 'supplier', 'asset.company', 'asset.assetstatus', 'admin'); if ($request->filled('search')) { $maintenances = $maintenances->TextSearch($request->input('search')); @@ -70,10 +71,13 @@ public function index(Request $request) 'notes', 'asset_tag', 'asset_name', + 'serial', 'user_id', 'supplier', 'is_warranty', + 'status_label', ]; + $order = $request->input('order') === 'asc' ? 'asc' : 'desc'; $sort = in_array($request->input('sort'), $allowed_columns) ? e($request->input('sort')) : 'created_at'; @@ -90,6 +94,12 @@ public function index(Request $request) case 'asset_name': $maintenances = $maintenances->OrderByAssetName($order); break; + case 'serial': + $maintenances = $maintenances->OrderByAssetSerial($order); + break; + case 'status_label': + $maintenances = $maintenances->OrderStatusName($order); + break; default: $maintenances = $maintenances->orderBy($sort, $order); break; diff --git a/app/Http/Controllers/AssetMaintenancesController.php b/app/Http/Controllers/AssetMaintenancesController.php index dc6bc8434711..b5a50925ea32 100644 --- a/app/Http/Controllers/AssetMaintenancesController.php +++ b/app/Http/Controllers/AssetMaintenancesController.php @@ -148,30 +148,20 @@ public function store(Request $request) */ public function edit($assetMaintenanceId = null) { + $this->authorize('update', Asset::class); + // Check if the asset maintenance exists $this->authorize('update', Asset::class); // Check if the asset maintenance exists if (is_null($assetMaintenance = AssetMaintenance::find($assetMaintenanceId))) { - // Redirect to the improvement management page - return redirect()->route('maintenances.index') - ->with('error', trans('admin/asset_maintenances/message.not_found')); - } elseif (! $assetMaintenance->asset) { - return redirect()->route('maintenances.index') - ->with('error', 'The asset associated with this maintenance does not exist.'); + // Redirect to the asset maintenance management page + return redirect()->route('maintenances.index')->with('error', trans('admin/asset_maintenances/message.not_found')); + } elseif ((!$assetMaintenance->asset) || ($assetMaintenance->asset->deleted_at!='')) { + // Redirect to the asset maintenance management page + return redirect()->route('maintenances.index')->with('error', 'asset does not exist'); } elseif (! Company::isCurrentUserHasAccess($assetMaintenance->asset)) { return static::getInsufficientPermissionsRedirect(); } - if ($assetMaintenance->completion_date == '0000-00-00') { - $assetMaintenance->completion_date = null; - } - - if ($assetMaintenance->start_date == '0000-00-00') { - $assetMaintenance->start_date = null; - } - - if ($assetMaintenance->cost == '0.00') { - $assetMaintenance->cost = null; - } // Prepare Improvement Type List $assetMaintenanceType = [ @@ -203,8 +193,10 @@ public function update(Request $request, $assetMaintenanceId = null) // Check if the asset maintenance exists if (is_null($assetMaintenance = AssetMaintenance::find($assetMaintenanceId))) { // Redirect to the asset maintenance management page - return redirect()->route('maintenances.index') - ->with('error', trans('admin/asset_maintenances/message.not_found')); + return redirect()->route('maintenances.index')->with('error', trans('admin/asset_maintenances/message.not_found')); + } elseif ((!$assetMaintenance->asset) || ($assetMaintenance->asset->deleted_at!='')) { + // Redirect to the asset maintenance management page + return redirect()->route('maintenances.index')->with('error', 'asset does not exist'); } elseif (! Company::isCurrentUserHasAccess($assetMaintenance->asset)) { return static::getInsufficientPermissionsRedirect(); } diff --git a/app/Http/Transformers/AssetMaintenancesTransformer.php b/app/Http/Transformers/AssetMaintenancesTransformer.php index 01ef2adb87fc..88ac447c25bb 100644 --- a/app/Http/Transformers/AssetMaintenancesTransformer.php +++ b/app/Http/Transformers/AssetMaintenancesTransformer.php @@ -28,12 +28,20 @@ public function transformAssetMaintenance(AssetMaintenance $assetmaintenance) 'id' => (int) $assetmaintenance->asset->id, 'name'=> ($assetmaintenance->asset->name) ? e($assetmaintenance->asset->name) : null, 'asset_tag'=> e($assetmaintenance->asset->asset_tag), - + 'serial'=> e($assetmaintenance->asset->serial), + 'deleted_at'=> e($assetmaintenance->asset->deleted_at), + 'created_at'=> e($assetmaintenance->asset->created_at), ] : null, 'model' => (($assetmaintenance->asset) && ($assetmaintenance->asset->model)) ? [ 'id' => (int) $assetmaintenance->asset->model->id, 'name'=> ($assetmaintenance->asset->model->name) ? e($assetmaintenance->asset->model->name).' '.e($assetmaintenance->asset->model->model_number) : null, ] : null, + 'status_label' => ($assetmaintenance->asset->assetstatus) ? [ + 'id' => (int) $assetmaintenance->asset->assetstatus->id, + 'name'=> e($assetmaintenance->asset->assetstatus->name), + 'status_type'=> e($assetmaintenance->asset->assetstatus->getStatuslabelType()), + 'status_meta' => e($assetmaintenance->asset->present()->statusMeta), + ] : null, 'company' => (($assetmaintenance->asset) && ($assetmaintenance->asset->company)) ? [ 'id' => (int) $assetmaintenance->asset->company->id, 'name'=> ($assetmaintenance->asset->company->name) ? e($assetmaintenance->asset->company->name) : null, @@ -64,7 +72,7 @@ public function transformAssetMaintenance(AssetMaintenance $assetmaintenance) ]; $permissions_array['available_actions'] = [ - 'update' => Gate::allows('update', Asset::class), + 'update' => (Gate::allows('update', Asset::class) && ($assetmaintenance->asset->deleted_at=='')) ? true : false, 'delete' => Gate::allows('delete', Asset::class), ]; diff --git a/app/Models/Asset.php b/app/Models/Asset.php index abb2239dc7d5..c2a2a8d9955d 100644 --- a/app/Models/Asset.php +++ b/app/Models/Asset.php @@ -1560,7 +1560,7 @@ public function scopeByFilter($query, $filter) * * In short, this set of statements tells the query builder to ONLY query against an * actual field that's being passed if it doesn't meet known relational fields. This - * allows us to query custom fields directly in the assetsv table + * allows us to query custom fields directly in the assets table * (regardless of their name) and *skip* any fields that we already know can only be * searched through relational searches that we do earlier in this method. * diff --git a/app/Models/AssetMaintenance.php b/app/Models/AssetMaintenance.php index 760abf1519cf..5f66783cbbe9 100644 --- a/app/Models/AssetMaintenance.php +++ b/app/Models/AssetMaintenance.php @@ -62,7 +62,15 @@ class AssetMaintenance extends Model implements ICompanyableChild * * @var array */ - protected $searchableAttributes = ['title', 'notes', 'asset_maintenance_type', 'cost', 'start_date', 'completion_date']; + protected $searchableAttributes = + [ + 'title', + 'notes', + 'asset_maintenance_type', + 'cost', + 'start_date', + 'completion_date' + ]; /** * The relations and their attributes that should be included when searching the model. @@ -70,9 +78,10 @@ class AssetMaintenance extends Model implements ICompanyableChild * @var array */ protected $searchableRelations = [ - 'asset' => ['name', 'asset_tag'], + 'asset' => ['name', 'asset_tag', 'serial'], 'asset.model' => ['name', 'model_number'], 'asset.supplier' => ['name'], + 'asset.assetstatus' => ['name'], 'supplier' => ['name'], ]; @@ -197,6 +206,7 @@ public function scopeOrderBySupplier($query, $order) ->orderBy('suppliers_maintenances.name', $order); } + /** * Query builder scope to order on admin user * @@ -239,4 +249,33 @@ public function scopeOrderByAssetName($query, $order) return $query->leftJoin('assets', 'asset_maintenances.asset_id', '=', 'assets.id') ->orderBy('assets.name', $order); } + + /** + * Query builder scope to order on serial + * + * @param \Illuminate\Database\Query\Builder $query Query builder instance + * @param string $order Order + * + * @return \Illuminate\Database\Query\Builder Modified query builder + */ + public function scopeOrderByAssetSerial($query, $order) + { + return $query->leftJoin('assets', 'asset_maintenances.asset_id', '=', 'assets.id') + ->orderBy('assets.serial', $order); + } + + /** + * Query builder scope to order on status label name + * + * @param \Illuminate\Database\Query\Builder $query Query builder instance + * @param text $order Order + * + * @return \Illuminate\Database\Query\Builder Modified query builder + */ + public function scopeOrderStatusName($query, $order) + { + return $query->join('assets as maintained_asset', 'asset_maintenances.asset_id', '=', 'maintained_asset.id') + ->leftjoin('status_labels as maintained_asset_status', 'maintained_asset_status.id', '=', 'maintained_asset.status_id') + ->orderBy('maintained_asset_status.name', $order); + } } diff --git a/app/Presenters/AssetMaintenancesPresenter.php b/app/Presenters/AssetMaintenancesPresenter.php index c4446c0b2aae..5f9694b44ca8 100644 --- a/app/Presenters/AssetMaintenancesPresenter.php +++ b/app/Presenters/AssetMaintenancesPresenter.php @@ -41,6 +41,19 @@ public static function dataTableLayout() 'sortable' => true, 'title' => trans('admin/hardware/table.asset_tag'), 'formatter' => 'assetTagLinkFormatter', + ], [ + 'field' => 'serial', + 'searchable' => true, + 'sortable' => true, + 'title' => trans('admin/hardware/table.serial'), + 'formatter' => 'assetSerialLinkFormatter', + ], [ + 'field' => 'status_label', + 'searchable' => true, + 'sortable' => true, + 'title' => trans('admin/hardware/table.status'), + 'visible' => true, + 'formatter' => 'statuslabelsLinkObjFormatter', ], [ 'field' => 'model', 'searchable' => true, diff --git a/resources/lang/en-US/general.php b/resources/lang/en-US/general.php index 0db52859ff11..fe3ada1a2906 100644 --- a/resources/lang/en-US/general.php +++ b/resources/lang/en-US/general.php @@ -459,6 +459,7 @@ 'no_autoassign_licenses_help' => 'Do not include user for bulk-assigning through the license UI or cli tools.', 'modal_confirm_generic' => 'Are you sure?', 'cannot_be_deleted' => 'This item cannot be deleted', + 'cannot_be_edited' => 'This item cannot be edited.', 'undeployable_tooltip' => 'This item cannot be checked out. Check the quantity remaining.', 'serial_number' => 'Serial Number', 'item_notes' => ':item Notes', diff --git a/resources/views/hardware/view.blade.php b/resources/views/hardware/view.blade.php index d856427bd372..2c7ddcf535ab 100755 --- a/resources/views/hardware/view.blade.php +++ b/resources/views/hardware/view.blade.php @@ -872,11 +872,13 @@ @can('update', $asset) + @if ($asset->deleted_at=='')