diff --git a/app/Http/Controllers/Api/AssetMaintenancesController.php b/app/Http/Controllers/Api/AssetMaintenancesController.php new file mode 100644 index 0000000000..eb07aa6814 --- /dev/null +++ b/app/Http/Controllers/Api/AssetMaintenancesController.php @@ -0,0 +1,226 @@ + + * @version v1.0 + * @since [v1.8] + * @return String JSON + */ + public function index(Request $request) + { + $maintenances = AssetMaintenance::with('asset', 'supplier', 'asset.company', 'admin'); + + if (Input::has('search')) { + $maintenances = $maintenances->TextSearch(e($request->input('search'))); + } + + $offset = request('offset', 0); + $limit = request('limit', 50); + + $allowed_columns = ['id','title','asset_maintenance_time','asset_maintenance_type','cost','start_date','completion_date','notes','user_id']; + $order = Input::get('order') === 'asc' ? 'asc' : 'desc'; + $sort = in_array(Input::get('sort'), $allowed_columns) ? e($request->input('sort')) : 'created_at'; + + switch ($sort) { + case 'user_id': + $maintenances = $maintenances->OrderAdmin($order); + break; + default: + $maintenances = $maintenances->orderBy($sort, $order); + break; + } + + + $maintenances = $maintenances->skip($offset)->take($limit)->get(); + + + return (new AssetMaintenancesTransformer())->transformAssetMaintenances($maintenances, $maintenances->count()); + + + } + + + /** + * Validates and stores the new asset maintenance + * + * @see AssetMaintenancesController::getCreate() method for the form + * @author Vincent Sposato + * @version v1.0 + * @since [v1.8] + * @return String JSON + */ + public function store(Request $request) + { + // create a new model instance + $assetMaintenance = new AssetMaintenance(); + $assetMaintenance->supplier_id = $request->input('supplier_id'); + $assetMaintenance->is_warranty = $request->input('is_warranty'); + $assetMaintenance->cost = e($request->input('cost')); + $assetMaintenance->notes = e($request->input('notes')); + $asset = Asset::find(e($request->input('asset_id'))); + + if (!Company::isCurrentUserHasAccess($asset)) { + return response()->json(Helper::formatStandardApiResponse('error', null, 'You cannot add a maintenance for that asset')); + } + + // Save the asset maintenance data + $assetMaintenance->asset_id = $request->input('asset_id'); + $assetMaintenance->asset_maintenance_type = $request->input('asset_maintenance_type'); + $assetMaintenance->title = $request->input('title'); + $assetMaintenance->start_date = $request->input('start_date'); + $assetMaintenance->completion_date = $request->input('completion_date'); + $assetMaintenance->user_id = Auth::id(); + + if (( $assetMaintenance->completion_date !== null ) + && ( $assetMaintenance->start_date !== "" ) + && ( $assetMaintenance->start_date !== "0000-00-00" ) + ) { + $startDate = Carbon::parse($assetMaintenance->start_date); + $completionDate = Carbon::parse($assetMaintenance->completion_date); + $assetMaintenance->asset_maintenance_time = $completionDate->diffInDays($startDate); + } + + // Was the asset maintenance created? + if ($assetMaintenance->save()) { + return response()->json(Helper::formatStandardApiResponse('success', $assetMaintenance, trans('admin/asset_maintenances/message.create.success'))); + + } + + return response()->json(Helper::formatStandardApiResponse('error', null, $assetMaintenance->getErrors())); + + } + + + /** + * Validates and stores an update to an asset maintenance + * + * @author A. Gianotto + * @param int $assetMaintenanceId + * @param int $request + * @version v1.0 + * @since [v4.0] + * @return String JSON + */ + public function update(Request $request, $assetMaintenanceId = null) + { + // Check if the asset maintenance exists + $assetMaintenance = AssetMaintenance::findOrFail($assetMaintenanceId); + + if (!Company::isCurrentUserHasAccess($assetMaintenance->asset)) { + return response()->json(Helper::formatStandardApiResponse('error', null, 'You cannot edit a maintenance for that asset')); + } + + $assetMaintenance->supplier_id = e($request->input('supplier_id')); + $assetMaintenance->is_warranty = e($request->input('is_warranty')); + $assetMaintenance->cost = Helper::ParseFloat(e($request->input('cost'))); + $assetMaintenance->notes = e($request->input('notes')); + + $asset = Asset::find(request('asset_id')); + + if (!Company::isCurrentUserHasAccess($asset)) { + return response()->json(Helper::formatStandardApiResponse('error', null, 'You cannot edit a maintenance for that asset')); + } + + // Save the asset maintenance data + $assetMaintenance->asset_id = $request->input('asset_id'); + $assetMaintenance->asset_maintenance_type = $request->input('asset_maintenance_type'); + $assetMaintenance->title = $request->input('title'); + $assetMaintenance->start_date = $request->input('start_date'); + $assetMaintenance->completion_date = $request->input('completion_date'); + + if (( $assetMaintenance->completion_date == null ) + ) { + if (( $assetMaintenance->asset_maintenance_time !== 0 ) + || ( !is_null($assetMaintenance->asset_maintenance_time) ) + ) { + $assetMaintenance->asset_maintenance_time = null; + } + } + + if (( $assetMaintenance->completion_date !== null ) + && ( $assetMaintenance->start_date !== "" ) + && ( $assetMaintenance->start_date !== "0000-00-00" ) + ) { + $startDate = Carbon::parse($assetMaintenance->start_date); + $completionDate = Carbon::parse($assetMaintenance->completion_date); + $assetMaintenance->asset_maintenance_time = $completionDate->diffInDays($startDate); + } + + // Was the asset maintenance created? + if ($assetMaintenance->save()) { + + return response()->json(Helper::formatStandardApiResponse('success', $assetMaintenance, trans('admin/asset_maintenances/message.edit.success'))); + + } + return response()->json(Helper::formatStandardApiResponse('error', null, $assetMaintenance->getErrors())); + } + + /** + * Delete an asset maintenance + * + * @author A. Gianotto + * @param int $assetMaintenanceId + * @version v1.0 + * @since [v4.0] + * @return String JSON + */ + public function destroy($assetMaintenanceId) + { + // Check if the asset maintenance exists + $assetMaintenance = AssetMaintenance::findOrFail($assetMaintenanceId); + + if (!Company::isCurrentUserHasAccess($assetMaintenance->asset)) { + return response()->json(Helper::formatStandardApiResponse('error', null, 'You cannot delete a maintenance for that asset')); + } + + $assetMaintenance->delete(); + + return response()->json(Helper::formatStandardApiResponse('success', $assetMaintenance, trans('admin/asset_maintenances/message.delete.success'))); + + + } + + /** + * View an asset maintenance + * + * @author A. Gianotto + * @param int $assetMaintenanceId + * @version v1.0 + * @since [v4.0] + * @return String JSON + */ + public function show($assetMaintenanceId) + { + $assetMaintenance = AssetMaintenance::findOrFail($assetMaintenanceId); + if (!Company::isCurrentUserHasAccess($assetMaintenance->asset)) { + return response()->json(Helper::formatStandardApiResponse('error', null, 'You cannot view a maintenance for that asset')); + } + return (new AssetMaintenancesTransformer())->transformAssetMaintenance($assetMaintenance); + + } +} diff --git a/app/Http/Transformers/AssetMaintenancesTransformer.php b/app/Http/Transformers/AssetMaintenancesTransformer.php new file mode 100644 index 0000000000..f27f022c36 --- /dev/null +++ b/app/Http/Transformers/AssetMaintenancesTransformer.php @@ -0,0 +1,53 @@ +transformDatatables($array, $total); + } + + public function transformAssetMaintenance (AssetMaintenance $assetmaintenance) + { + $array = [ + 'id' => (int) $assetmaintenance->id, + 'asset_name' => ($assetmaintenance->asset) ? ['id' => $assetmaintenance->asset->id,'name'=> e($assetmaintenance->asset->name)] : null, + 'title' => ($assetmaintenance->title) ? e($assetmaintenance->title) : null, + 'notes' => ($assetmaintenance->notes) ? e($assetmaintenance->notes) : null, + 'supplier' => ($assetmaintenance->supplier) ? ['id' => $assetmaintenance->supplier->id,'name'=> e($assetmaintenance->supplier->name)] : null, + 'cost' => Helper::formatCurrencyOutput($assetmaintenance->cost), + 'asset_maintenance_type' => e($assetmaintenance->asset_maintenance_type), + 'start_date' => Helper::getFormattedDateObject($assetmaintenance->start_date, 'datetime'), + 'asset_maintenance_time' => $assetmaintenance->asset_maintenance_time, + 'completion_date' => Helper::getFormattedDateObject($assetmaintenance->completion_date, 'datetime'), + 'user_id' => ($assetmaintenance->admin) ? ['id' => $assetmaintenance->admin->id,'name'=> e($assetmaintenance->admin->getFullNameAttribute())] : null, + 'created_at' => Helper::getFormattedDateObject($assetmaintenance->created_at, 'datetime'), + 'updated_at' => Helper::getFormattedDateObject($assetmaintenance->updated_at, 'datetime'), + + ]; + + $permissions_array['available_actions'] = [ + 'delete' => (bool) Gate::allows('delete', Asset::class), + ]; + + $array += $permissions_array; + + return $array; + } + + + + + +} diff --git a/resources/views/asset_maintenances/edit.blade.php b/resources/views/asset_maintenances/edit.blade.php index 013e211873..df7737c0fd 100644 --- a/resources/views/asset_maintenances/edit.blade.php +++ b/resources/views/asset_maintenances/edit.blade.php @@ -72,24 +72,28 @@
- -
- - + + +
+
+ + +
{!! $errors->first('start_date', ' :message') !!}
+ +
- -
- - + + +
+
+ + +
{!! $errors->first('completion_date', ' :message') !!}
diff --git a/resources/views/asset_maintenances/index.blade.php b/resources/views/asset_maintenances/index.blade.php index ac25fa4894..5a3aa18ef8 100644 --- a/resources/views/asset_maintenances/index.blade.php +++ b/resources/views/asset_maintenances/index.blade.php @@ -24,7 +24,7 @@ name="maintenances" id="table" class="table table-striped snipe-table" - data-url="{{route('api.asset_maintenances.list') }}" + data-url="{{route('api.maintenances.index') }}" data-cookie="true" data-click-to-select="true" data-cookie-id-table="maintenancesTable-{{ config('version.hash_version') }}" @@ -33,18 +33,18 @@ {{ trans('admin/companies/table.title') }} {{ trans('general.id') }} - {{ trans('admin/asset_maintenances/table.asset_name') }} - {{ trans('general.supplier') }} + {{ trans('admin/asset_maintenances/table.asset_name') }} + {{ trans('general.supplier') }} {{ trans('admin/asset_maintenances/form.asset_maintenance_type') }} {{ trans('admin/asset_maintenances/form.title') }} - {{ trans('admin/asset_maintenances/form.start_date') }} - {{ trans('admin/asset_maintenances/form.completion_date') }} + {{ trans('admin/asset_maintenances/form.start_date') }} + {{ trans('admin/asset_maintenances/form.completion_date') }} {{ trans('admin/asset_maintenances/form.asset_maintenance_time') }} {{ trans('admin/asset_maintenances/form.cost') }} - {{ trans('general.admin') }} + {{ trans('general.admin') }} {{ trans('admin/asset_maintenances/form.notes') }} @can('update', \App\Models\Asset::class) - {{ trans('table.actions') }} + {{ trans('table.actions') }} @endcan @@ -58,4 +58,18 @@ @section('moar_scripts') @include ('partials.bootstrap-table', ['exportFile' => 'maintenances-export', 'search' => true]) + @stop diff --git a/routes/api.php b/routes/api.php index f79dfa0277..d9051f8851 100644 --- a/routes/api.php +++ b/routes/api.php @@ -230,6 +230,22 @@ Route::group(['prefix' => 'v1','namespace' => 'Api'], function () { }); + /*--- Asset Maintenances API ---*/ + Route::resource('maintenances', 'AssetMaintenancesController', + [ + 'names' => + [ + 'index' => 'api.maintenances.index', + 'show' => 'api.maintenances.show', + 'store' => 'api.maintenances.store', + 'update' => 'api.maintenances.update', + 'destroy' => 'api.maintenances.destroy' + ], + 'except' => ['create', 'edit'], + 'parameters' => ['maintenance' => 'maintenance_id'] + ] + ); // Consumables resource + Route::resource('hardware', 'AssetsController', [