2016-03-25 01:18:05 -07:00
|
|
|
<?php
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2016-03-25 15:50:08 -07:00
|
|
|
namespace App\Http\Controllers;
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2019-03-13 20:12:03 -07:00
|
|
|
use App\Helpers\Helper;
|
|
|
|
use App\Models\Asset;
|
2016-03-25 15:50:08 -07:00
|
|
|
use App\Models\AssetMaintenance;
|
|
|
|
use App\Models\Company;
|
2024-05-29 04:38:15 -07:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2019-03-13 20:12:03 -07:00
|
|
|
use Carbon\Carbon;
|
|
|
|
use Illuminate\Http\Request;
|
2016-03-25 15:50:08 -07:00
|
|
|
use Slack;
|
|
|
|
use Str;
|
|
|
|
use TCPDF;
|
|
|
|
use View;
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2016-04-07 13:21:09 -07:00
|
|
|
/**
|
|
|
|
* This controller handles all actions related to Asset Maintenance for
|
|
|
|
* the Snipe-IT Asset Management application.
|
|
|
|
*
|
|
|
|
* @version v2.0
|
|
|
|
*/
|
2016-03-25 01:18:05 -07:00
|
|
|
class AssetMaintenancesController extends Controller
|
|
|
|
{
|
2016-03-25 18:07:12 -07:00
|
|
|
/**
|
|
|
|
* Checks for permissions for this action.
|
|
|
|
*
|
|
|
|
* @todo This should be replaced with middleware and/or policies
|
|
|
|
* @author Vincent Sposato <vincent.sposato@gmail.com>
|
|
|
|
* @version v1.0
|
|
|
|
* @since [v1.8]
|
|
|
|
* @return View
|
|
|
|
*/
|
2016-03-25 01:18:05 -07:00
|
|
|
private static function getInsufficientPermissionsRedirect()
|
|
|
|
{
|
2016-12-15 15:15:11 -08:00
|
|
|
return redirect()->route('maintenances.index')
|
2016-04-07 13:39:35 -07:00
|
|
|
->with('error', trans('general.insufficient_permissions'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2016-03-25 15:50:08 -07:00
|
|
|
/**
|
2016-03-25 18:07:12 -07:00
|
|
|
* Returns a view that invokes the ajax tables which actually contains
|
|
|
|
* the content for the asset maintenances listing, which is generated in getDatatable.
|
2016-03-25 15:50:08 -07:00
|
|
|
*
|
2016-03-25 18:07:12 -07:00
|
|
|
* @todo This should be replaced with middleware and/or policies
|
|
|
|
* @see AssetMaintenancesController::getDatatable() method that generates the JSON response
|
2016-03-25 15:50:08 -07:00
|
|
|
* @author Vincent Sposato <vincent.sposato@gmail.com>
|
|
|
|
* @version v1.0
|
2016-03-25 18:07:12 -07:00
|
|
|
* @since [v1.8]
|
|
|
|
* @return View
|
2016-03-25 15:50:08 -07:00
|
|
|
*/
|
2016-12-15 15:15:11 -08:00
|
|
|
public function index()
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2022-02-14 15:34:06 -08:00
|
|
|
$this->authorize('view', Asset::class);
|
2017-06-09 16:44:03 -07:00
|
|
|
return view('asset_maintenances/index');
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-19 11:04:28 -08:00
|
|
|
* Returns a form view to create a new asset maintenance.
|
|
|
|
*
|
|
|
|
* @see AssetMaintenancesController::postCreate() method that stores the data
|
|
|
|
* @author Vincent Sposato <vincent.sposato@gmail.com>
|
|
|
|
* @version v1.0
|
|
|
|
* @since [v1.8]
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function create()
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2022-03-03 18:47:20 -08:00
|
|
|
$this->authorize('update', Asset::class);
|
2017-11-12 17:22:16 -08:00
|
|
|
$asset = null;
|
|
|
|
|
|
|
|
if ($asset = Asset::find(request('asset_id'))) {
|
|
|
|
// We have to set this so that the correct property is set in the select2 ajax dropdown
|
|
|
|
$asset->asset_id = $asset->id;
|
|
|
|
}
|
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
// Prepare Asset Maintenance Type List
|
|
|
|
$assetMaintenanceType = [
|
|
|
|
'' => 'Select an asset maintenance type',
|
|
|
|
] + AssetMaintenance::getImprovementOptions();
|
|
|
|
// Mark the selected asset, if it came in
|
2017-11-12 17:22:16 -08:00
|
|
|
|
2017-06-09 16:44:03 -07:00
|
|
|
return view('asset_maintenances/edit')
|
2017-11-12 17:22:16 -08:00
|
|
|
->with('asset', $asset)
|
2016-03-25 01:18:05 -07:00
|
|
|
->with('assetMaintenanceType', $assetMaintenanceType)
|
2016-11-29 05:02:48 -08:00
|
|
|
->with('item', new AssetMaintenance);
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-25 18:07:12 -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]
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2016-12-15 15:15:11 -08:00
|
|
|
public function store(Request $request)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2022-03-03 18:47:20 -08:00
|
|
|
$this->authorize('update', Asset::class);
|
2016-03-25 01:18:05 -07:00
|
|
|
// create a new model instance
|
|
|
|
$assetMaintenance = new AssetMaintenance();
|
2016-12-26 15:17:46 -08:00
|
|
|
$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');
|
2020-11-12 23:55:16 -08:00
|
|
|
$assetMaintenance->notes = $request->input('notes');
|
|
|
|
$asset = Asset::find($request->input('asset_id'));
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if ((! Company::isCurrentUserHasAccess($asset)) && ($asset != null)) {
|
2016-03-25 01:18:05 -07:00
|
|
|
return static::getInsufficientPermissionsRedirect();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the asset maintenance data
|
2021-06-10 13:15:52 -07:00
|
|
|
$assetMaintenance->asset_id = $request->input('asset_id');
|
2016-12-19 22:00:50 -08: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')
|
2016-03-25 01:18:05 -07:00
|
|
|
) {
|
2021-06-10 13:15:52 -07:00
|
|
|
$startDate = Carbon::parse($assetMaintenance->start_date);
|
|
|
|
$completionDate = Carbon::parse($assetMaintenance->completion_date);
|
2016-03-25 01:18:05 -07:00
|
|
|
$assetMaintenance->asset_maintenance_time = $completionDate->diffInDays($startDate);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Was the asset maintenance created?
|
|
|
|
if ($assetMaintenance->save()) {
|
|
|
|
// Redirect to the new asset maintenance page
|
2016-12-15 15:15:11 -08:00
|
|
|
return redirect()->route('maintenances.index')
|
2016-04-07 13:39:35 -07:00
|
|
|
->with('success', trans('admin/asset_maintenances/message.create.success'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->back()->withInput()->withErrors($assetMaintenance->getErrors());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-25 18:07:12 -07:00
|
|
|
* Returns a form view to edit a selected asset maintenance.
|
|
|
|
*
|
|
|
|
* @see AssetMaintenancesController::postEdit() method that stores the data
|
|
|
|
* @author Vincent Sposato <vincent.sposato@gmail.com>
|
|
|
|
* @param int $assetMaintenanceId
|
|
|
|
* @version v1.0
|
|
|
|
* @since [v1.8]
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2016-12-15 15:15:11 -08:00
|
|
|
public function edit($assetMaintenanceId = null)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2024-02-14 02:14:25 -08:00
|
|
|
$this->authorize('update', Asset::class);
|
|
|
|
// Check if the asset maintenance exists
|
2022-03-03 18:47:20 -08:00
|
|
|
$this->authorize('update', Asset::class);
|
2016-03-25 01:18:05 -07:00
|
|
|
// Check if the asset maintenance exists
|
|
|
|
if (is_null($assetMaintenance = AssetMaintenance::find($assetMaintenanceId))) {
|
2024-02-14 02:14:25 -08:00
|
|
|
// 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');
|
2021-06-10 13:15:52 -07:00
|
|
|
} elseif (! Company::isCurrentUserHasAccess($assetMaintenance->asset)) {
|
2016-03-25 01:18:05 -07:00
|
|
|
return static::getInsufficientPermissionsRedirect();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Prepare Improvement Type List
|
|
|
|
$assetMaintenanceType = [
|
|
|
|
'' => 'Select an improvement type',
|
|
|
|
] + AssetMaintenance::getImprovementOptions();
|
|
|
|
|
|
|
|
// Get Supplier List
|
|
|
|
// Render the view
|
2017-06-09 16:44:03 -07:00
|
|
|
return view('asset_maintenances/edit')
|
2016-03-25 01:18:05 -07:00
|
|
|
->with('selectedAsset', null)
|
|
|
|
->with('assetMaintenanceType', $assetMaintenanceType)
|
2016-11-29 05:02:48 -08:00
|
|
|
->with('item', $assetMaintenance);
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-19 22:00:50 -08:00
|
|
|
* Validates and stores an update to an asset maintenance
|
|
|
|
*
|
|
|
|
* @see AssetMaintenancesController::postEdit() method that stores the data
|
|
|
|
* @author Vincent Sposato <vincent.sposato@gmail.com>
|
|
|
|
* @param Request $request
|
|
|
|
* @param int $assetMaintenanceId
|
|
|
|
* @return mixed
|
|
|
|
* @version v1.0
|
|
|
|
* @since [v1.8]
|
|
|
|
*/
|
2016-12-15 15:15:11 -08:00
|
|
|
public function update(Request $request, $assetMaintenanceId = null)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2022-03-03 18:47:20 -08:00
|
|
|
$this->authorize('update', Asset::class);
|
2016-03-25 01:18:05 -07:00
|
|
|
// Check if the asset maintenance exists
|
|
|
|
if (is_null($assetMaintenance = AssetMaintenance::find($assetMaintenanceId))) {
|
|
|
|
// Redirect to the asset maintenance management page
|
2024-02-14 02:14:25 -08:00
|
|
|
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');
|
2021-06-10 13:15:52 -07:00
|
|
|
} elseif (! Company::isCurrentUserHasAccess($assetMaintenance->asset)) {
|
2016-03-25 01:18:05 -07:00
|
|
|
return static::getInsufficientPermissionsRedirect();
|
|
|
|
}
|
|
|
|
|
2021-08-31 02:01:20 -07:00
|
|
|
$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');
|
2021-08-31 01:50:57 -07:00
|
|
|
$assetMaintenance->notes = $request->input('notes');
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2016-12-19 22:00:50 -08:00
|
|
|
$asset = Asset::find(request('asset_id'));
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if (! Company::isCurrentUserHasAccess($asset)) {
|
2016-03-25 01:18:05 -07:00
|
|
|
return static::getInsufficientPermissionsRedirect();
|
|
|
|
}
|
|
|
|
|
2016-03-25 18:07:12 -07:00
|
|
|
// Save the asset maintenance data
|
2021-06-10 13:15:52 -07:00
|
|
|
$assetMaintenance->asset_id = $request->input('asset_id');
|
2016-12-19 22:00:50 -08: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');
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if (($assetMaintenance->completion_date == null)
|
2016-03-25 01:18:05 -07:00
|
|
|
) {
|
2021-06-10 13:15:52 -07:00
|
|
|
if (($assetMaintenance->asset_maintenance_time !== 0)
|
|
|
|
|| (! is_null($assetMaintenance->asset_maintenance_time))
|
2016-03-25 01:18:05 -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')
|
2016-03-25 01:18:05 -07:00
|
|
|
) {
|
2021-06-10 13:15:52 -07:00
|
|
|
$startDate = Carbon::parse($assetMaintenance->start_date);
|
|
|
|
$completionDate = Carbon::parse($assetMaintenance->completion_date);
|
2016-03-25 01:18:05 -07:00
|
|
|
$assetMaintenance->asset_maintenance_time = $completionDate->diffInDays($startDate);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Was the asset maintenance created?
|
|
|
|
if ($assetMaintenance->save()) {
|
|
|
|
|
|
|
|
// Redirect to the new asset maintenance page
|
2016-12-15 15:15:11 -08:00
|
|
|
return redirect()->route('maintenances.index')
|
2016-12-26 15:17:46 -08:00
|
|
|
->with('success', trans('admin/asset_maintenances/message.edit.success'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2016-12-15 15:15:11 -08:00
|
|
|
return redirect()->back()->withInput()->withErrors($assetMaintenance->getErrors());
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-25 18:07:12 -07:00
|
|
|
* Delete an asset maintenance
|
|
|
|
*
|
|
|
|
* @author Vincent Sposato <vincent.sposato@gmail.com>
|
|
|
|
* @param int $assetMaintenanceId
|
|
|
|
* @version v1.0
|
|
|
|
* @since [v1.8]
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2016-12-15 15:15:11 -08:00
|
|
|
public function destroy($assetMaintenanceId)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2022-03-03 18:47:20 -08:00
|
|
|
$this->authorize('update', Asset::class);
|
2016-03-25 01:18:05 -07:00
|
|
|
// Check if the asset maintenance exists
|
|
|
|
if (is_null($assetMaintenance = AssetMaintenance::find($assetMaintenanceId))) {
|
|
|
|
// Redirect to the asset maintenance management page
|
2016-12-15 15:15:11 -08:00
|
|
|
return redirect()->route('maintenances.index')
|
2016-04-07 13:39:35 -07:00
|
|
|
->with('error', trans('admin/asset_maintenances/message.not_found'));
|
2021-06-10 13:15:52 -07:00
|
|
|
} elseif (! Company::isCurrentUserHasAccess($assetMaintenance->asset)) {
|
2016-03-25 01:18:05 -07:00
|
|
|
return static::getInsufficientPermissionsRedirect();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the asset maintenance
|
|
|
|
$assetMaintenance->delete();
|
|
|
|
|
|
|
|
// Redirect to the asset_maintenance management page
|
2016-12-15 15:15:11 -08:00
|
|
|
return redirect()->route('maintenances.index')
|
2016-04-07 13:39:35 -07:00
|
|
|
->with('success', trans('admin/asset_maintenances/message.delete.success'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-25 18:07:12 -07:00
|
|
|
* View an asset maintenance
|
|
|
|
*
|
|
|
|
* @author Vincent Sposato <vincent.sposato@gmail.com>
|
|
|
|
* @param int $assetMaintenanceId
|
|
|
|
* @version v1.0
|
|
|
|
* @since [v1.8]
|
|
|
|
* @return View
|
|
|
|
*/
|
2016-12-15 15:15:11 -08:00
|
|
|
public function show($assetMaintenanceId)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2022-02-14 15:34:06 -08:00
|
|
|
$this->authorize('view', Asset::class);
|
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
// Check if the asset maintenance exists
|
|
|
|
if (is_null($assetMaintenance = AssetMaintenance::find($assetMaintenanceId))) {
|
|
|
|
// Redirect to the asset maintenance management page
|
2016-12-15 15:15:11 -08:00
|
|
|
return redirect()->route('maintenances.index')
|
2016-04-07 13:39:35 -07:00
|
|
|
->with('error', trans('admin/asset_maintenances/message.not_found'));
|
2021-06-10 13:15:52 -07:00
|
|
|
} elseif (! Company::isCurrentUserHasAccess($assetMaintenance->asset)) {
|
2016-03-25 01:18:05 -07:00
|
|
|
return static::getInsufficientPermissionsRedirect();
|
|
|
|
}
|
|
|
|
|
2017-06-09 16:44:03 -07:00
|
|
|
return view('asset_maintenances/view')->with('assetMaintenance', $assetMaintenance);
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
}
|