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;
|
|
|
|
use Auth;
|
2017-09-05 17:54:58 -07:00
|
|
|
use Carbon\Carbon;
|
2017-08-22 13:52:06 -07:00
|
|
|
use Illuminate\Http\Request;
|
2017-09-05 17:54:58 -07:00
|
|
|
use Illuminate\Support\Facades\Input;
|
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]
|
2021-06-10 13:15:52 -07:00
|
|
|
* @return string JSON
|
2017-08-22 13:52:06 -07:00
|
|
|
*/
|
|
|
|
public function index(Request $request)
|
|
|
|
{
|
2022-02-14 15:34:06 -08:00
|
|
|
$this->authorize('view', Asset::class);
|
2022-06-14 16:10:48 -07:00
|
|
|
|
2023-04-27 17:13:09 -07:00
|
|
|
$maintenances = AssetMaintenance::select('asset_maintenances.*')->with('asset', 'asset.model', 'asset.location', 'asset.defaultLoc', 'supplier', 'asset.company', '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')) {
|
|
|
|
$maintenances->where('supplier_id', '=', $request->input('supplier_id'));
|
|
|
|
}
|
|
|
|
|
|
|
|
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',
|
2021-06-10 13:15:52 -07:00
|
|
|
'user_id',
|
2023-06-13 11:20:55 -07:00
|
|
|
'supplier',
|
|
|
|
'is_warranty',
|
2018-02-16 13:22:55 -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;
|
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]
|
2021-06-10 13:15:52 -07:00
|
|
|
* @return string JSON
|
2017-08-22 13:52:06 -07:00
|
|
|
*/
|
|
|
|
public function store(Request $request)
|
|
|
|
{
|
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
|
|
|
|
$assetMaintenance = new AssetMaintenance();
|
|
|
|
$assetMaintenance->supplier_id = $request->input('supplier_id');
|
|
|
|
$assetMaintenance->is_warranty = $request->input('is_warranty');
|
2023-03-29 00:56:34 -07:00
|
|
|
$assetMaintenance->cost = $request->input('cost');
|
2017-08-22 13:52:06 -07:00
|
|
|
$assetMaintenance->notes = e($request->input('notes'));
|
|
|
|
$asset = Asset::find(e($request->input('asset_id')));
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if (! Company::isCurrentUserHasAccess($asset)) {
|
2017-08-22 13:52:06 -07:00
|
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, 'You cannot add a maintenance for that asset'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the asset maintenance data
|
2021-06-10 13:15:52 -07:00
|
|
|
$assetMaintenance->asset_id = $request->input('asset_id');
|
2017-08-22 13:52:06 -07:00
|
|
|
$assetMaintenance->asset_maintenance_type = $request->input('asset_maintenance_type');
|
2021-06-10 13:15:52 -07:00
|
|
|
$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')
|
2017-08-22 13:52:06 -07:00
|
|
|
) {
|
2021-06-10 13:15:52 -07:00
|
|
|
$startDate = Carbon::parse($assetMaintenance->start_date);
|
|
|
|
$completionDate = Carbon::parse($assetMaintenance->completion_date);
|
2017-08-22 13:52:06 -07:00
|
|
|
$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 <snipe@snipe.net>
|
|
|
|
* @param int $assetMaintenanceId
|
|
|
|
* @param int $request
|
|
|
|
* @version v1.0
|
|
|
|
* @since [v4.0]
|
2021-06-10 13:15:52 -07:00
|
|
|
* @return string JSON
|
2017-08-22 13:52:06 -07:00
|
|
|
*/
|
|
|
|
public function update(Request $request, $assetMaintenanceId = null)
|
|
|
|
{
|
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 edit a maintenance for that asset'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$assetMaintenance->supplier_id = e($request->input('supplier_id'));
|
|
|
|
$assetMaintenance->is_warranty = e($request->input('is_warranty'));
|
2023-03-29 00:56:34 -07:00
|
|
|
$assetMaintenance->cost = $request->input('cost');
|
2017-08-22 13:52:06 -07:00
|
|
|
$assetMaintenance->notes = e($request->input('notes'));
|
|
|
|
|
|
|
|
$asset = Asset::find(request('asset_id'));
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if (! Company::isCurrentUserHasAccess($asset)) {
|
2017-08-22 13:52:06 -07:00
|
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, 'You cannot edit a maintenance for that asset'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the asset maintenance data
|
2021-06-10 13:15:52 -07:00
|
|
|
$assetMaintenance->asset_id = $request->input('asset_id');
|
2017-08-22 13:52:06 -07:00
|
|
|
$assetMaintenance->asset_maintenance_type = $request->input('asset_maintenance_type');
|
2021-06-10 13:15:52 -07:00
|
|
|
$assetMaintenance->title = $request->input('title');
|
|
|
|
$assetMaintenance->start_date = $request->input('start_date');
|
|
|
|
$assetMaintenance->completion_date = $request->input('completion_date');
|
2017-08-22 13:52:06 -07:00
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if (($assetMaintenance->completion_date == null)
|
2017-08-22 13:52:06 -07:00
|
|
|
) {
|
2021-06-10 13:15:52 -07:00
|
|
|
if (($assetMaintenance->asset_maintenance_time !== 0)
|
|
|
|
|| (! is_null($assetMaintenance->asset_maintenance_time))
|
2017-08-22 13:52:06 -07:00
|
|
|
) {
|
|
|
|
$assetMaintenance->asset_maintenance_time = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if (($assetMaintenance->completion_date !== null)
|
|
|
|
&& ($assetMaintenance->start_date !== '')
|
|
|
|
&& ($assetMaintenance->start_date !== '0000-00-00')
|
2017-08-22 13:52:06 -07:00
|
|
|
) {
|
2021-06-10 13:15:52 -07:00
|
|
|
$startDate = Carbon::parse($assetMaintenance->start_date);
|
|
|
|
$completionDate = Carbon::parse($assetMaintenance->completion_date);
|
2017-08-22 13:52:06 -07:00
|
|
|
$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')));
|
|
|
|
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-08-22 13:52:06 -07:00
|
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, $assetMaintenance->getErrors()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete an asset maintenance
|
|
|
|
*
|
|
|
|
* @author A. Gianotto <snipe@snipe.net>
|
|
|
|
* @param int $assetMaintenanceId
|
|
|
|
* @version v1.0
|
|
|
|
* @since [v4.0]
|
2021-06-10 13:15:52 -07:00
|
|
|
* @return string JSON
|
2017-08-22 13:52:06 -07:00
|
|
|
*/
|
|
|
|
public function destroy($assetMaintenanceId)
|
|
|
|
{
|
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]
|
2021-06-10 13:15:52 -07:00
|
|
|
* @return string JSON
|
2017-08-22 13:52:06 -07:00
|
|
|
*/
|
|
|
|
public function show($assetMaintenanceId)
|
|
|
|
{
|
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
|
|
|
|
|
|
|
}
|
|
|
|
}
|