mirror of
https://github.com/snipe/snipe-it.git
synced 2024-12-27 06:29:47 -08:00
987536930c
* Fix some n+1 problems * Use route in notification dropdown to make sure we link to correct page * Work on better UI support for checkout to non-user. Fix links on index bootstrap table, work towards eliminating assignedUser * Remove Asset::assigneduser() relationship. Instead add a checkedOutToUser() method and/or port to using assignedTo() * Adjust string to fit new reality * Fix #3780. Move the consumables getDataView method to the ApiController. Not entirely RESTful, but it's a weird method that probably doesn't need its own controller and the functionality would be strange to stack on the userscontroller... * Fix file uploads to assets and restore the delete route. * Add asset maintence edit action to index. * Suppliers asset list should link to the related asset, not to the supplier with same ID. * Asset models page should use polymorphic formatter on assigned to to better handle assorted item types. * Comment out more assigneduser fallacy until we figure out the query builder approach to searching for location text.
228 lines
8.6 KiB
PHP
228 lines
8.6 KiB
PHP
<?php
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Helpers\Helper;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Transformers\AssetMaintenancesTransformer;
|
|
use App\Models\Asset;
|
|
use App\Models\AssetMaintenance;
|
|
use App\Models\Company;
|
|
use Auth;
|
|
use Carbon\Carbon;
|
|
use Gate;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Input;
|
|
|
|
/**
|
|
* 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]
|
|
* @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 <vincent.sposato@gmail.com>
|
|
* @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 <snipe@snipe.net>
|
|
* @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 <snipe@snipe.net>
|
|
* @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 <snipe@snipe.net>
|
|
* @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);
|
|
|
|
}
|
|
}
|