2017-08-22 13:52:06 -07:00
|
|
|
<?php
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-08-22 13:52:06 -07:00
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
|
2017-09-05 17:54:58 -07:00
|
|
|
use App\Helpers\Helper;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Http\Transformers\AssetMaintenancesTransformer;
|
|
|
|
use App\Models\Asset;
|
2017-08-22 13:52:06 -07:00
|
|
|
use App\Models\AssetMaintenance;
|
|
|
|
use App\Models\Company;
|
2024-05-29 04:38:15 -07:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2017-08-22 13:52:06 -07:00
|
|
|
use Illuminate\Http\Request;
|
2024-07-04 23:07:20 -07:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2017-08-22 13:52:06 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This controller handles all actions related to Asset Maintenance for
|
|
|
|
* the Snipe-IT Asset Management application.
|
|
|
|
*
|
|
|
|
* @version v2.0
|
|
|
|
*/
|
|
|
|
class AssetMaintenancesController extends Controller
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates the JSON response for asset maintenances listing view.
|
|
|
|
*
|
|
|
|
* @see AssetMaintenancesController::getIndex() method that generates view
|
|
|
|
* @author Vincent Sposato <vincent.sposato@gmail.com>
|
|
|
|
* @version v1.0
|
|
|
|
* @since [v1.8]
|
|
|
|
*/
|
2024-07-04 23:07:20 -07:00
|
|
|
public function index(Request $request) : JsonResponse | array
|
2017-08-22 13:52:06 -07:00
|
|
|
{
|
2022-02-14 15:34:06 -08:00
|
|
|
$this->authorize('view', Asset::class);
|
2022-06-14 16:10:48 -07:00
|
|
|
|
2024-02-14 01:47:13 -08:00
|
|
|
$maintenances = AssetMaintenance::select('asset_maintenances.*')
|
2024-02-14 01:58:39 -08:00
|
|
|
->with('asset', 'asset.model', 'asset.location', 'asset.defaultLoc', 'supplier', 'asset.company', 'asset.assetstatus', 'admin');
|
2017-08-22 13:52:06 -07:00
|
|
|
|
2018-08-01 18:24:52 -07:00
|
|
|
if ($request->filled('search')) {
|
|
|
|
$maintenances = $maintenances->TextSearch($request->input('search'));
|
2017-08-22 13:52:06 -07:00
|
|
|
}
|
|
|
|
|
2019-05-23 17:39:50 -07:00
|
|
|
if ($request->filled('asset_id')) {
|
2018-02-16 13:22:55 -08:00
|
|
|
$maintenances->where('asset_id', '=', $request->input('asset_id'));
|
|
|
|
}
|
|
|
|
|
2022-06-14 16:10:48 -07:00
|
|
|
if ($request->filled('supplier_id')) {
|
2024-02-15 01:51:14 -08:00
|
|
|
$maintenances->where('asset_maintenances.supplier_id', '=', $request->input('supplier_id'));
|
2022-06-14 16:10:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($request->filled('asset_maintenance_type')) {
|
|
|
|
$maintenances->where('asset_maintenance_type', '=', $request->input('asset_maintenance_type'));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-12 11:28:46 -07:00
|
|
|
// Make sure the offset and limit are actually integers and do not exceed system limits
|
2023-04-15 17:28:25 -07:00
|
|
|
$offset = ($request->input('offset') > $maintenances->count()) ? $maintenances->count() : abs($request->input('offset'));
|
2023-04-16 08:46:39 -07:00
|
|
|
$limit = app('api_limit_value');
|
2019-09-03 14:29:58 -07:00
|
|
|
|
2018-02-16 13:22:55 -08:00
|
|
|
$allowed_columns = [
|
|
|
|
'id',
|
|
|
|
'title',
|
|
|
|
'asset_maintenance_time',
|
|
|
|
'asset_maintenance_type',
|
|
|
|
'cost',
|
|
|
|
'start_date',
|
|
|
|
'completion_date',
|
|
|
|
'notes',
|
2018-03-05 16:26:40 -08:00
|
|
|
'asset_tag',
|
|
|
|
'asset_name',
|
2024-02-14 01:47:13 -08:00
|
|
|
'serial',
|
2021-06-10 13:15:52 -07:00
|
|
|
'user_id',
|
2023-06-13 11:20:55 -07:00
|
|
|
'supplier',
|
|
|
|
'is_warranty',
|
2024-02-14 01:58:39 -08:00
|
|
|
'status_label',
|
2018-02-16 13:22:55 -08:00
|
|
|
];
|
2024-02-14 01:58:39 -08:00
|
|
|
|
2019-12-11 11:09:54 -08:00
|
|
|
$order = $request->input('order') === 'asc' ? 'asc' : 'desc';
|
|
|
|
$sort = in_array($request->input('sort'), $allowed_columns) ? e($request->input('sort')) : 'created_at';
|
2017-08-22 13:52:06 -07:00
|
|
|
|
|
|
|
switch ($sort) {
|
|
|
|
case 'user_id':
|
|
|
|
$maintenances = $maintenances->OrderAdmin($order);
|
|
|
|
break;
|
2022-06-14 16:10:48 -07:00
|
|
|
case 'supplier':
|
|
|
|
$maintenances = $maintenances->OrderBySupplier($order);
|
|
|
|
break;
|
2018-03-05 16:26:40 -08:00
|
|
|
case 'asset_tag':
|
|
|
|
$maintenances = $maintenances->OrderByTag($order);
|
|
|
|
break;
|
|
|
|
case 'asset_name':
|
|
|
|
$maintenances = $maintenances->OrderByAssetName($order);
|
|
|
|
break;
|
2024-02-14 01:47:13 -08:00
|
|
|
case 'serial':
|
|
|
|
$maintenances = $maintenances->OrderByAssetSerial($order);
|
|
|
|
break;
|
2024-02-14 01:58:39 -08:00
|
|
|
case 'status_label':
|
|
|
|
$maintenances = $maintenances->OrderStatusName($order);
|
|
|
|
break;
|
2017-08-22 13:52:06 -07:00
|
|
|
default:
|
|
|
|
$maintenances = $maintenances->orderBy($sort, $order);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-11-14 01:32:25 -08:00
|
|
|
$total = $maintenances->count();
|
2017-08-22 13:52:06 -07:00
|
|
|
$maintenances = $maintenances->skip($offset)->take($limit)->get();
|
2021-06-10 13:15:52 -07:00
|
|
|
return (new AssetMaintenancesTransformer())->transformAssetMaintenances($maintenances, $total);
|
2017-08-22 13:52:06 -07:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates and stores the new asset maintenance
|
|
|
|
*
|
|
|
|
* @see AssetMaintenancesController::getCreate() method for the form
|
|
|
|
* @author Vincent Sposato <vincent.sposato@gmail.com>
|
|
|
|
* @version v1.0
|
|
|
|
* @since [v1.8]
|
|
|
|
*/
|
2024-07-04 23:07:20 -07:00
|
|
|
public function store(Request $request) : JsonResponse
|
2017-08-22 13:52:06 -07:00
|
|
|
{
|
2022-03-03 18:49:54 -08:00
|
|
|
$this->authorize('update', Asset::class);
|
2017-08-22 13:52:06 -07:00
|
|
|
// create a new model instance
|
2023-12-07 11:36:12 -08:00
|
|
|
$maintenance = new AssetMaintenance();
|
|
|
|
$maintenance->fill($request->all());
|
|
|
|
$maintenance->user_id = Auth::id();
|
2017-08-22 13:52:06 -07:00
|
|
|
|
|
|
|
// Was the asset maintenance created?
|
2023-12-07 11:36:12 -08:00
|
|
|
if ($maintenance->save()) {
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('success', $maintenance, trans('admin/asset_maintenances/message.create.success')));
|
2017-08-22 13:52:06 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-12-07 11:36:12 -08:00
|
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, $maintenance->getErrors()));
|
2017-08-22 13:52:06 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates and stores an update to an asset maintenance
|
|
|
|
*
|
|
|
|
* @author A. Gianotto <snipe@snipe.net>
|
2023-12-07 11:36:12 -08:00
|
|
|
* @param int $id
|
2017-08-22 13:52:06 -07:00
|
|
|
* @param int $request
|
|
|
|
* @version v1.0
|
|
|
|
* @since [v4.0]
|
|
|
|
*/
|
2024-07-04 23:07:20 -07:00
|
|
|
public function update(Request $request, $id) : JsonResponse
|
2017-08-22 13:52:06 -07:00
|
|
|
{
|
2022-03-03 18:49:54 -08:00
|
|
|
$this->authorize('update', Asset::class);
|
2017-08-22 13:52:06 -07:00
|
|
|
|
2023-12-07 11:36:12 -08:00
|
|
|
if ($maintenance = AssetMaintenance::with('asset')->find($id)) {
|
2017-08-22 13:52:06 -07:00
|
|
|
|
2023-12-07 11:36:12 -08:00
|
|
|
// Can this user manage this asset?
|
|
|
|
if (! Company::isCurrentUserHasAccess($maintenance->asset)) {
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, trans('general.action_permission_denied', ['item_type' => trans('admin/asset_maintenances/general.maintenance'), 'id' => $id, 'action' => trans('general.edit')])));
|
|
|
|
}
|
2017-08-22 13:52:06 -07:00
|
|
|
|
2023-12-07 11:36:12 -08:00
|
|
|
// The asset this miantenance is attached to is not valid or has been deleted
|
|
|
|
if (!$maintenance->asset) {
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, trans('general.item_not_found', ['item_type' => trans('general.asset'), 'id' => $id])));
|
|
|
|
}
|
2017-08-22 13:52:06 -07:00
|
|
|
|
2023-12-07 11:36:12 -08:00
|
|
|
$maintenance->fill($request->all());
|
2017-08-22 13:52:06 -07:00
|
|
|
|
2023-12-07 11:36:12 -08:00
|
|
|
if ($maintenance->save()) {
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('success', $maintenance, trans('admin/asset_maintenances/message.edit.success')));
|
2017-08-22 13:52:06 -07:00
|
|
|
}
|
|
|
|
|
2023-12-07 11:36:12 -08:00
|
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, $maintenance->getErrors()));
|
2017-08-22 13:52:06 -07:00
|
|
|
}
|
|
|
|
|
2023-12-07 11:36:12 -08:00
|
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, trans('general.item_not_found', ['item_type' => trans('admin/asset_maintenances/general.maintenance'), 'id' => $id])));
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-08-22 13:52:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete an asset maintenance
|
|
|
|
*
|
|
|
|
* @author A. Gianotto <snipe@snipe.net>
|
|
|
|
* @param int $assetMaintenanceId
|
|
|
|
* @version v1.0
|
|
|
|
* @since [v4.0]
|
|
|
|
*/
|
2024-07-04 23:07:20 -07:00
|
|
|
public function destroy($assetMaintenanceId) : JsonResponse
|
2017-08-22 13:52:06 -07:00
|
|
|
{
|
2022-03-03 18:49:54 -08:00
|
|
|
$this->authorize('update', Asset::class);
|
2017-08-22 13:52:06 -07:00
|
|
|
// Check if the asset maintenance exists
|
|
|
|
$assetMaintenance = AssetMaintenance::findOrFail($assetMaintenanceId);
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if (! Company::isCurrentUserHasAccess($assetMaintenance->asset)) {
|
2017-08-22 13:52:06 -07:00
|
|
|
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 <snipe@snipe.net>
|
|
|
|
* @param int $assetMaintenanceId
|
|
|
|
* @version v1.0
|
|
|
|
* @since [v4.0]
|
|
|
|
*/
|
2024-07-04 23:07:20 -07:00
|
|
|
public function show($assetMaintenanceId) : JsonResponse
|
2017-08-22 13:52:06 -07:00
|
|
|
{
|
2022-02-14 15:34:06 -08:00
|
|
|
$this->authorize('view', Asset::class);
|
2017-08-22 13:52:06 -07:00
|
|
|
$assetMaintenance = AssetMaintenance::findOrFail($assetMaintenanceId);
|
2021-06-10 13:15:52 -07:00
|
|
|
if (! Company::isCurrentUserHasAccess($assetMaintenance->asset)) {
|
2017-08-22 13:52:06 -07:00
|
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, 'You cannot view a maintenance for that asset'));
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
return (new AssetMaintenancesTransformer())->transformAssetMaintenance($assetMaintenance);
|
2017-08-22 13:52:06 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|