snipe-it/app/Http/Controllers/AssetMaintenancesController.php

473 lines
18 KiB
PHP
Raw Normal View History

2016-03-25 01:18:05 -07:00
<?php
namespace App\Http\Controllers;
2016-03-25 01:18:05 -07:00
use App\Models\AssetMaintenance;
use Carbon\Carbon;
use App\Models\Company;
use DB;
use Input;
use Lang;
use Log;
use Mail;
use Redirect;
use Response;
use Slack;
use Str;
use App\Models\Supplier;
use TCPDF;
use Validator;
use View;
use App\Models\Setting;
use App\Models\Asset;
use App\Helpers\Helper;
2016-06-22 17:04:47 -07:00
use Auth;
use Gate;
2016-12-15 15:15:11 -08:00
use Illuminate\Http\Request;
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')
->with('error', trans('general.insufficient_permissions'));
2016-03-25 01:18:05 -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 18:07:12 -07:00
* @todo This should be replaced with middleware and/or policies
* @see AssetMaintenancesController::getDatatable() method that generates the JSON response
* @author Vincent Sposato <vincent.sposato@gmail.com>
* @version v1.0
2016-03-25 18:07:12 -07:00
* @since [v1.8]
* @return View
*/
2016-12-15 15:15:11 -08:00
public function index()
2016-03-25 01:18:05 -07:00
{
return View::make('asset_maintenances/index');
}
2016-03-25 18:07:12 -07:00
/**
* 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
*/
2016-12-15 15:15:11 -08:00
public function getDatatable(Request $request)
2016-03-25 01:18:05 -07:00
{
$maintenances = AssetMaintenance::with('asset', 'supplier', 'asset.company','admin');
2016-03-25 01:18:05 -07:00
if (Input::has('search')) {
2016-12-15 15:15:11 -08:00
$maintenances = $maintenances->TextSearch(e($request->input('search')));
2016-03-25 01:18:05 -07:00
}
2016-06-22 17:04:47 -07:00
2016-12-15 15:15:11 -08:00
if ($request->has('offset')) {
$offset = e($request->input('offset'));
2016-03-25 01:18:05 -07:00
} else {
$offset = 0;
}
if (Input::has('limit')) {
2016-12-15 15:15:11 -08:00
$limit = e($request->input('limit'));
2016-03-25 01:18:05 -07:00
} else {
$limit = 50;
}
2016-06-22 17:04:47 -07:00
$allowed_columns = ['id','title','asset_maintenance_time','asset_maintenance_type','cost','start_date','completion_date','notes','user_id'];
2016-03-25 01:18:05 -07:00
$order = Input::get('order') === 'asc' ? 'asc' : 'desc';
2016-12-15 15:15:11 -08:00
$sort = in_array(Input::get('sort'), $allowed_columns) ? e($request->input('sort')) : 'created_at';
2016-03-25 01:18:05 -07:00
2016-06-22 17:04:47 -07:00
switch ($sort) {
case 'user_id':
$maintenances = $maintenances->OrderAdmin($order);
break;
default:
$maintenances = $maintenances->orderBy($sort, $order);
break;
}
2016-03-25 01:18:05 -07:00
$maintenancesCount = $maintenances->count();
$maintenances = $maintenances->skip($offset)->take($limit)->get();
$rows = array();
$settings = Setting::getSettings();
foreach ($maintenances as $maintenance) {
$actions = '';
Discussion: Moving to policies for controller based authorization (#3080) * Make delete routes work. We put a little form in the modal that spoofs the delete field. * Fix route on creating a user. * Fix redundant id parameter. * Port acceptance tests to new urls. * Initial work on migrating to model based policies instead of global gates. Will allow for much more detailed permissions bits in the future. * This needs to stay for the dashboard checks. * Add user states for permissions to build tests. * Build up unit tests for gates/permissions. Move accessories/consumables/assets to policies instead of in authserviceprovider * Migrate various locations to new syntax. Update test to be more specific * Fix functional tests. Add an artisan command for installing a settings setup on travis-ci * Try a different id... Need to come up with a better way of passing the id for tests that need an existing one. * Try to fix travis * Update urls to use routes and not hardcode old paths. Also fix some migration errors found along the way.: * Add a environment for travis functional tests. * Adjust config file to make travis use it. * Use redirect()->route instead of redirect()-to * Dump all failures in the output directory if travis fails. * Cleanups and minor fixes. * Adjust the supplier modelfactory to comply with new validation restrictions. * Some test fixes. * Locales can be longer than 5 characters according to faker... fex gez_ET. Increase lenght in mysql and add a validation * Update test database dump to latest migrations.
2016-12-19 11:04:28 -08:00
if (Gate::allows('update', Asset::class)) {
2016-12-15 15:15:11 -08:00
$actions .= '<nobr><a href="' . route('maintenances.edit',
$maintenance->id) . '" class="btn btn-warning btn-sm" style="margin-right:5px;"><i class="fa fa-pencil icon-white"></i></a><a data-html="false" class="btn delete-asset btn-danger btn-sm" data-toggle="modal" href="' . route('maintenances.destroy',
$maintenance->id) . '" data-content="' . trans('admin/asset_maintenances/message.delete.confirm') . '" data-title="' . trans('general.delete') . ' ' . htmlspecialchars($maintenance->title) . '?" onClick="return false;"><i class="fa fa-trash icon-white"></i></a></nobr>';
}
2016-03-25 01:18:05 -07:00
if (($maintenance->cost) && (isset($maintenance->asset)) && ($maintenance->asset->assetloc) && ($maintenance->asset->assetloc->currency!='')) {
2016-03-25 01:18:05 -07:00
$maintenance_cost = $maintenance->asset->assetloc->currency.$maintenance->cost;
} else {
$maintenance_cost = $settings->default_currency.$maintenance->cost;
}
2016-12-15 15:15:11 -08:00
2016-03-25 01:18:05 -07:00
$rows[] = array(
'id' => $maintenance->id,
2016-12-15 15:15:11 -08:00
'asset_name' => ($maintenance->asset) ? (string)link_to_route('maintenances.show', $maintenance->asset->showAssetName(), ['maintenance' => $maintenance->asset->id]) : 'Deleted Asset' ,
2016-03-25 01:18:05 -07:00
'title' => $maintenance->title,
'notes' => $maintenance->notes,
Discussion: Moving to policies for controller based authorization (#3080) * Make delete routes work. We put a little form in the modal that spoofs the delete field. * Fix route on creating a user. * Fix redundant id parameter. * Port acceptance tests to new urls. * Initial work on migrating to model based policies instead of global gates. Will allow for much more detailed permissions bits in the future. * This needs to stay for the dashboard checks. * Add user states for permissions to build tests. * Build up unit tests for gates/permissions. Move accessories/consumables/assets to policies instead of in authserviceprovider * Migrate various locations to new syntax. Update test to be more specific * Fix functional tests. Add an artisan command for installing a settings setup on travis-ci * Try a different id... Need to come up with a better way of passing the id for tests that need an existing one. * Try to fix travis * Update urls to use routes and not hardcode old paths. Also fix some migration errors found along the way.: * Add a environment for travis functional tests. * Adjust config file to make travis use it. * Use redirect()->route instead of redirect()-to * Dump all failures in the output directory if travis fails. * Cleanups and minor fixes. * Adjust the supplier modelfactory to comply with new validation restrictions. * Some test fixes. * Locales can be longer than 5 characters according to faker... fex gez_ET. Increase lenght in mysql and add a validation * Update test database dump to latest migrations.
2016-12-19 11:04:28 -08:00
'supplier' => ($maintenance->supplier) ? (string)link_to_route('suppliers.show', $maintenance->supplier->name, ['maintenance'=>$maintenance->supplier->id]) : 'Deleted Supplier',
2016-03-25 01:18:05 -07:00
'cost' => $maintenance_cost,
'asset_maintenance_type' => e($maintenance->asset_maintenance_type),
'start_date' => $maintenance->start_date,
'asset_maintenance_time' => $maintenance->asset_maintenance_time,
'completion_date' => $maintenance->completion_date,
Discussion: Moving to policies for controller based authorization (#3080) * Make delete routes work. We put a little form in the modal that spoofs the delete field. * Fix route on creating a user. * Fix redundant id parameter. * Port acceptance tests to new urls. * Initial work on migrating to model based policies instead of global gates. Will allow for much more detailed permissions bits in the future. * This needs to stay for the dashboard checks. * Add user states for permissions to build tests. * Build up unit tests for gates/permissions. Move accessories/consumables/assets to policies instead of in authserviceprovider * Migrate various locations to new syntax. Update test to be more specific * Fix functional tests. Add an artisan command for installing a settings setup on travis-ci * Try a different id... Need to come up with a better way of passing the id for tests that need an existing one. * Try to fix travis * Update urls to use routes and not hardcode old paths. Also fix some migration errors found along the way.: * Add a environment for travis functional tests. * Adjust config file to make travis use it. * Use redirect()->route instead of redirect()-to * Dump all failures in the output directory if travis fails. * Cleanups and minor fixes. * Adjust the supplier modelfactory to comply with new validation restrictions. * Some test fixes. * Locales can be longer than 5 characters according to faker... fex gez_ET. Increase lenght in mysql and add a validation * Update test database dump to latest migrations.
2016-12-19 11:04:28 -08:00
'user_id' => ($maintenance->admin) ? (string)link_to_route('users.show', $maintenance->admin->fullName(), ['user'=>$maintenance->admin->id]) : '',
2016-03-25 01:18:05 -07:00
'actions' => $actions,
2016-09-27 19:07:45 -07:00
'companyName' => ($maintenance->asset->company) ? $maintenance->asset->company->name : ''
2016-03-25 01:18:05 -07:00
);
}
$data = array('total' => $maintenancesCount, 'rows' => $rows);
return $data;
}
/**
Discussion: Moving to policies for controller based authorization (#3080) * Make delete routes work. We put a little form in the modal that spoofs the delete field. * Fix route on creating a user. * Fix redundant id parameter. * Port acceptance tests to new urls. * Initial work on migrating to model based policies instead of global gates. Will allow for much more detailed permissions bits in the future. * This needs to stay for the dashboard checks. * Add user states for permissions to build tests. * Build up unit tests for gates/permissions. Move accessories/consumables/assets to policies instead of in authserviceprovider * Migrate various locations to new syntax. Update test to be more specific * Fix functional tests. Add an artisan command for installing a settings setup on travis-ci * Try a different id... Need to come up with a better way of passing the id for tests that need an existing one. * Try to fix travis * Update urls to use routes and not hardcode old paths. Also fix some migration errors found along the way.: * Add a environment for travis functional tests. * Adjust config file to make travis use it. * Use redirect()->route instead of redirect()-to * Dump all failures in the output directory if travis fails. * Cleanups and minor fixes. * Adjust the supplier modelfactory to comply with new validation restrictions. * Some test fixes. * Locales can be longer than 5 characters according to faker... fex gez_ET. Increase lenght in mysql and add a validation * Update test database dump to latest migrations.
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
{
// Prepare Asset Maintenance Type List
$assetMaintenanceType = [
'' => 'Select an asset maintenance type',
] + AssetMaintenance::getImprovementOptions();
// Mark the selected asset, if it came in
Discussion: Moving to policies for controller based authorization (#3080) * Make delete routes work. We put a little form in the modal that spoofs the delete field. * Fix route on creating a user. * Fix redundant id parameter. * Port acceptance tests to new urls. * Initial work on migrating to model based policies instead of global gates. Will allow for much more detailed permissions bits in the future. * This needs to stay for the dashboard checks. * Add user states for permissions to build tests. * Build up unit tests for gates/permissions. Move accessories/consumables/assets to policies instead of in authserviceprovider * Migrate various locations to new syntax. Update test to be more specific * Fix functional tests. Add an artisan command for installing a settings setup on travis-ci * Try a different id... Need to come up with a better way of passing the id for tests that need an existing one. * Try to fix travis * Update urls to use routes and not hardcode old paths. Also fix some migration errors found along the way.: * Add a environment for travis functional tests. * Adjust config file to make travis use it. * Use redirect()->route instead of redirect()-to * Dump all failures in the output directory if travis fails. * Cleanups and minor fixes. * Adjust the supplier modelfactory to comply with new validation restrictions. * Some test fixes. * Locales can be longer than 5 characters according to faker... fex gez_ET. Increase lenght in mysql and add a validation * Update test database dump to latest migrations.
2016-12-19 11:04:28 -08:00
$selectedAsset = request('asset_id');
2016-03-25 18:07:12 -07:00
2016-06-22 16:58:36 -07:00
$assets = Helper::detailedAssetList();
2016-03-25 01:18:05 -07:00
$supplier_list = Helper::suppliersList();
// Render the view
return View::make('asset_maintenances/edit')
->with('asset_list', $assets)
2016-03-25 01:18:05 -07:00
->with('selectedAsset', $selectedAsset)
->with('supplier_list', $supplier_list)
->with('assetMaintenanceType', $assetMaintenanceType)
->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
{
// get the POST data
2016-12-15 15:15:11 -08:00
$new = $request->all();
Discussion: Moving to policies for controller based authorization (#3080) * Make delete routes work. We put a little form in the modal that spoofs the delete field. * Fix route on creating a user. * Fix redundant id parameter. * Port acceptance tests to new urls. * Initial work on migrating to model based policies instead of global gates. Will allow for much more detailed permissions bits in the future. * This needs to stay for the dashboard checks. * Add user states for permissions to build tests. * Build up unit tests for gates/permissions. Move accessories/consumables/assets to policies instead of in authserviceprovider * Migrate various locations to new syntax. Update test to be more specific * Fix functional tests. Add an artisan command for installing a settings setup on travis-ci * Try a different id... Need to come up with a better way of passing the id for tests that need an existing one. * Try to fix travis * Update urls to use routes and not hardcode old paths. Also fix some migration errors found along the way.: * Add a environment for travis functional tests. * Adjust config file to make travis use it. * Use redirect()->route instead of redirect()-to * Dump all failures in the output directory if travis fails. * Cleanups and minor fixes. * Adjust the supplier modelfactory to comply with new validation restrictions. * Some test fixes. * Locales can be longer than 5 characters according to faker... fex gez_ET. Increase lenght in mysql and add a validation * Update test database dump to latest migrations.
2016-12-19 11:04:28 -08:00
// dd($new);
2016-03-25 01:18:05 -07:00
// create a new model instance
$assetMaintenance = new AssetMaintenance();
if (e(Input::get('supplier_id')) == '') {
$assetMaintenance->supplier_id = null;
} else {
2016-12-15 15:15:11 -08:00
$assetMaintenance->supplier_id = e($request->input('supplier_id'));
2016-03-25 01:18:05 -07:00
}
if (e(Input::get('is_warranty')) == '') {
$assetMaintenance->is_warranty = 0;
} else {
2016-12-15 15:15:11 -08:00
$assetMaintenance->is_warranty = e($request->input('is_warranty'));
2016-03-25 01:18:05 -07:00
}
if (e(Input::get('cost')) == '') {
$assetMaintenance->cost = '';
} else {
2016-12-15 15:15:11 -08:00
$assetMaintenance->cost = Helper::ParseFloat(e($request->input('cost')));
2016-03-25 01:18:05 -07:00
}
if (e(Input::get('notes')) == '') {
$assetMaintenance->notes = null;
} else {
2016-12-15 15:15:11 -08:00
$assetMaintenance->notes = e($request->input('notes'));
2016-03-25 01:18:05 -07:00
}
2016-12-15 15:15:11 -08:00
$asset = Asset::find(e($request->input('asset_id')));
2016-03-25 01:18:05 -07:00
if (!Company::isCurrentUserHasAccess($asset)) {
return static::getInsufficientPermissionsRedirect();
}
// Save the asset maintenance data
2016-12-15 15:15:11 -08:00
$assetMaintenance->asset_id = e($request->input('asset_id'));
$assetMaintenance->asset_maintenance_type = e($request->input('asset_maintenance_type'));
$assetMaintenance->title = e($request->input('title'));
$assetMaintenance->start_date = e($request->input('start_date'));
$assetMaintenance->completion_date = e($request->input('completion_date'));
2016-06-22 17:04:47 -07:00
$assetMaintenance->user_id = Auth::user()->id;
2016-03-25 01:18:05 -07:00
if (( $assetMaintenance->completion_date == "" )
|| ( $assetMaintenance->completion_date == "0000-00-00" )
) {
$assetMaintenance->completion_date = null;
}
if (( $assetMaintenance->completion_date !== "" )
&& ( $assetMaintenance->completion_date !== "0000-00-00" )
&& ( $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()) {
// Redirect to the new asset maintenance page
2016-12-15 15:15:11 -08:00
return redirect()->route('maintenances.index')
->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
{
// Check if the asset maintenance exists
if (is_null($assetMaintenance = AssetMaintenance::find($assetMaintenanceId))) {
// Redirect to the improvement management page
2016-12-15 15:15:11 -08:00
return redirect()->route('maintenances.index')
->with('error', trans('admin/asset_maintenances/message.not_found'));
2016-03-25 01:18:05 -07:00
} elseif (!Company::isCurrentUserHasAccess($assetMaintenance->asset)) {
return static::getInsufficientPermissionsRedirect();
}
if ($assetMaintenance->completion_date == '0000-00-00') {
$assetMaintenance->completion_date = null;
}
if ($assetMaintenance->start_date == '0000-00-00') {
$assetMaintenance->start_date = null;
}
if ($assetMaintenance->cost == '0.00') {
$assetMaintenance->cost = null;
}
// Prepare Improvement Type List
$assetMaintenanceType = [
'' => 'Select an improvement type',
] + AssetMaintenance::getImprovementOptions();
Discussion: Moving to policies for controller based authorization (#3080) * Make delete routes work. We put a little form in the modal that spoofs the delete field. * Fix route on creating a user. * Fix redundant id parameter. * Port acceptance tests to new urls. * Initial work on migrating to model based policies instead of global gates. Will allow for much more detailed permissions bits in the future. * This needs to stay for the dashboard checks. * Add user states for permissions to build tests. * Build up unit tests for gates/permissions. Move accessories/consumables/assets to policies instead of in authserviceprovider * Migrate various locations to new syntax. Update test to be more specific * Fix functional tests. Add an artisan command for installing a settings setup on travis-ci * Try a different id... Need to come up with a better way of passing the id for tests that need an existing one. * Try to fix travis * Update urls to use routes and not hardcode old paths. Also fix some migration errors found along the way.: * Add a environment for travis functional tests. * Adjust config file to make travis use it. * Use redirect()->route instead of redirect()-to * Dump all failures in the output directory if travis fails. * Cleanups and minor fixes. * Adjust the supplier modelfactory to comply with new validation restrictions. * Some test fixes. * Locales can be longer than 5 characters according to faker... fex gez_ET. Increase lenght in mysql and add a validation * Update test database dump to latest migrations.
2016-12-19 11:04:28 -08:00
$assets = Helper::detailedAssetList();
2016-03-25 01:18:05 -07:00
// Get Supplier List
$supplier_list = Helper::suppliersList();
// Render the view
return View::make('asset_maintenances/edit')
->with('asset_list', $assets)
2016-03-25 01:18:05 -07:00
->with('selectedAsset', null)
->with('supplier_list', $supplier_list)
->with('assetMaintenanceType', $assetMaintenanceType)
->with('item', $assetMaintenance);
2016-03-25 01:18:05 -07:00
}
/**
2016-03-25 18:07:12 -07: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 int $assetMaintenanceId
* @version v1.0
* @since [v1.8]
* @return mixed
*/
2016-12-15 15:15:11 -08:00
public function update(Request $request, $assetMaintenanceId = null)
2016-03-25 01:18:05 -07:00
{
// get the POST data
2016-12-15 15:15:11 -08:00
$new = $request->all();
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')
->with('error', trans('admin/asset_maintenances/message.not_found'));
2016-03-25 01:18:05 -07:00
} elseif (!Company::isCurrentUserHasAccess($assetMaintenance->asset)) {
return static::getInsufficientPermissionsRedirect();
}
if (e(Input::get('supplier_id')) == '') {
$assetMaintenance->supplier_id = null;
} else {
2016-12-15 15:15:11 -08:00
$assetMaintenance->supplier_id = e($request->input('supplier_id'));
2016-03-25 01:18:05 -07:00
}
if (e(Input::get('is_warranty')) == '') {
$assetMaintenance->is_warranty = 0;
} else {
2016-12-15 15:15:11 -08:00
$assetMaintenance->is_warranty = e($request->input('is_warranty'));
2016-03-25 01:18:05 -07:00
}
if (e(Input::get('cost')) == '') {
$assetMaintenance->cost = '';
} else {
2016-12-15 15:15:11 -08:00
$assetMaintenance->cost = Helper::ParseFloat(e($request->input('cost')));
2016-03-25 01:18:05 -07:00
}
if (e(Input::get('notes')) == '') {
$assetMaintenance->notes = null;
} else {
2016-12-15 15:15:11 -08:00
$assetMaintenance->notes = e($request->input('notes'));
2016-03-25 01:18:05 -07:00
}
$asset = Asset::find(e(Input::get('asset_id')));
if (!Company::isCurrentUserHasAccess($asset)) {
return static::getInsufficientPermissionsRedirect();
}
2016-03-25 18:07:12 -07:00
// Save the asset maintenance data
2016-12-15 15:15:11 -08:00
$assetMaintenance->asset_id = e($request->input('asset_id'));
$assetMaintenance->asset_maintenance_type = e($request->input('asset_maintenance_type'));
$assetMaintenance->title = e($request->input('title'));
$assetMaintenance->start_date = e($request->input('start_date'));
$assetMaintenance->completion_date = e($request->input('completion_date'));
2016-03-25 01:18:05 -07:00
if (( $assetMaintenance->completion_date == "" )
|| ( $assetMaintenance->completion_date == "0000-00-00" )
) {
$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 !== "" )
&& ( $assetMaintenance->completion_date !== "0000-00-00" )
&& ( $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()) {
// Redirect to the new asset maintenance page
2016-12-15 15:15:11 -08:00
return redirect()->route('maintenances.index')
->with('success', trans('admin/asset_maintenances/message.create.success'));
2016-03-25 01:18:05 -07:00
}
2016-12-15 15:15:11 -08:00
return redirect()->back()->withInput()->withErrors($assetMaintenance->getErrors());
2016-03-25 15:24:12 -07:00
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
{
// 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')
->with('error', trans('admin/asset_maintenances/message.not_found'));
2016-03-25 01:18:05 -07:00
} elseif (!Company::isCurrentUserHasAccess($assetMaintenance->asset)) {
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')
->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
{
// 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')
->with('error', trans('admin/asset_maintenances/message.not_found'));
2016-03-25 01:18:05 -07:00
} elseif (!Company::isCurrentUserHasAccess($assetMaintenance->asset)) {
return static::getInsufficientPermissionsRedirect();
}
return View::make('asset_maintenances/view')->with('assetMaintenance', $assetMaintenance);
}
}