mirror of
https://github.com/snipe/snipe-it.git
synced 2024-11-14 01:24:06 -08:00
9aac1cbba4
Signed-off-by: snipe <snipe@snipe.net> # Conflicts: # README.md # app/Console/Commands/MoveUploadsToNewDisk.php # app/Http/Controllers/ActionlogController.php # app/Http/Controllers/Api/LicensesController.php # app/Http/Controllers/Api/StatuslabelsController.php # app/Http/Controllers/Assets/AssetCheckinController.php # app/Http/Controllers/Licenses/LicensesController.php # app/Http/Controllers/Users/BulkUsersController.php # app/Http/Requests/AssetCheckoutRequest.php # app/Importer/LicenseImporter.php # app/Models/Actionlog.php # app/Models/License.php # app/Models/User.php # app/Observers/AssetObserver.php # composer.lock # config/version.php # database/factories/LicenseFactory.php # database/migrations/2015_09_21_235926_create_custom_field_custom_fieldset.php # database/migrations/2018_10_18_191228_add_kits_licenses_table.php # database/migrations/2018_10_19_153910_add_kits_table.php # database/migrations/2018_10_19_154013_add_kits_models_table.php # database/migrations/2019_02_07_185953_add_kits_consumables_table.php # database/migrations/2019_02_07_190030_add_kits_accessories_table.php # package-lock.json # package.json # public/css/dist/all.css # public/css/dist/bootstrap-table.css # public/js/dist/bootstrap-table.js # public/mix-manifest.json # resources/lang/ar/general.php # resources/lang/ar/passwords.php # resources/lang/cs/general.php # resources/lang/cs/passwords.php # resources/lang/de/admin/custom_fields/general.php # resources/lang/de/admin/settings/general.php # resources/lang/de/admin/settings/message.php # resources/lang/fr/admin/custom_fields/general.php # resources/lang/fr/admin/hardware/general.php # resources/lang/fr/admin/locations/table.php # resources/lang/fr/admin/settings/message.php # resources/lang/hu/admin/custom_fields/general.php # resources/lang/hu/admin/settings/general.php # resources/lang/hu/general.php # resources/lang/it/admin/settings/general.php # resources/lang/nl/admin/custom_fields/general.php # resources/lang/nl/admin/settings/general.php # resources/lang/nl/general.php # resources/lang/pl/admin/custom_fields/general.php # resources/lang/sv-SE/passwords.php # resources/lang/tr/general.php # resources/views/hardware/view.blade.php # resources/views/partials/bootstrap-table.blade.php # resources/views/reports/activity.blade.php # resources/views/users/print.blade.php
146 lines
5.5 KiB
PHP
146 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Assets;
|
|
|
|
use App\Events\CheckoutableCheckedIn;
|
|
use App\Helpers\Helper;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\AssetCheckinRequest;
|
|
use App\Models\Asset;
|
|
use App\Models\CheckoutAcceptance;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Redirect;
|
|
use Illuminate\Support\Facades\View;
|
|
|
|
class AssetCheckinController extends Controller
|
|
{
|
|
/**
|
|
* Returns a view that presents a form to check an asset back into inventory.
|
|
*
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
* @param int $assetId
|
|
* @param string $backto
|
|
* @return View
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
* @since [v1.0]
|
|
*/
|
|
public function create($assetId, $backto = null)
|
|
{
|
|
// Check if the asset exists
|
|
if (is_null($asset = Asset::find($assetId))) {
|
|
// Redirect to the asset management page with error
|
|
return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.does_not_exist'));
|
|
}
|
|
|
|
$this->authorize('checkin', $asset);
|
|
|
|
return view('hardware/checkin', compact('asset'))->with('statusLabel_list', Helper::statusLabelList())->with('backto', $backto);
|
|
}
|
|
|
|
/**
|
|
* Validate and process the form data to check an asset back into inventory.
|
|
*
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
* @param AssetCheckinRequest $request
|
|
* @param int $assetId
|
|
* @param null $backto
|
|
* @return Redirect
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
* @since [v1.0]
|
|
*/
|
|
public function store(AssetCheckinRequest $request, $assetId = null, $backto = null)
|
|
{
|
|
// Check if the asset exists
|
|
if (is_null($asset = Asset::find($assetId))) {
|
|
// Redirect to the asset management page with error
|
|
return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.does_not_exist'));
|
|
}
|
|
|
|
if (is_null($target = $asset->assignedTo)) {
|
|
return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.checkin.already_checked_in'));
|
|
}
|
|
$this->authorize('checkin', $asset);
|
|
|
|
if ($asset->assignedType() == Asset::USER) {
|
|
$user = $asset->assignedTo;
|
|
}
|
|
|
|
$asset->expected_checkin = null;
|
|
$asset->last_checkout = null;
|
|
$asset->assigned_to = null;
|
|
$asset->assignedTo()->disassociate($asset);
|
|
$asset->assigned_type = null;
|
|
$asset->accepted = null;
|
|
$asset->name = $request->get('name');
|
|
|
|
if ($request->filled('status_id')) {
|
|
$asset->status_id = e($request->get('status_id'));
|
|
}
|
|
|
|
// This is just meant to correct legacy issues where some user data would have 0
|
|
// as a location ID, which isn't valid. Later versions of Snipe-IT have stricter validation
|
|
// rules, so it's necessary to fix this for long-time users. It's kinda gross, but will help
|
|
// people (and their data) in the long run
|
|
|
|
if ($asset->rtd_location_id == '0') {
|
|
\Log::debug('Manually override the RTD location IDs');
|
|
\Log::debug('Original RTD Location ID: '.$asset->rtd_location_id);
|
|
$asset->rtd_location_id = '';
|
|
\Log::debug('New RTD Location ID: '.$asset->rtd_location_id);
|
|
}
|
|
|
|
if ($asset->location_id == '0') {
|
|
\Log::debug('Manually override the location IDs');
|
|
\Log::debug('Original Location ID: '.$asset->location_id);
|
|
$asset->location_id = '';
|
|
\Log::debug('New RTD Location ID: '.$asset->location_id);
|
|
}
|
|
|
|
$asset->location_id = $asset->rtd_location_id;
|
|
\Log::debug('After Location ID: '.$asset->location_id);
|
|
\Log::debug('After RTD Location ID: '.$asset->rtd_location_id);
|
|
|
|
|
|
if ($request->filled('location_id')) {
|
|
\Log::debug('NEW Location ID: '.$request->get('location_id'));
|
|
$asset->location_id = e($request->get('location_id'));
|
|
}
|
|
|
|
$checkin_at = date('Y-m-d H:i:s');
|
|
if (($request->filled('checkin_at')) && ($request->get('checkin_at') != date('Y-m-d'))) {
|
|
$checkin_at = $request->get('checkin_at');
|
|
}
|
|
|
|
if(!empty($asset->licenseseats->all())){
|
|
foreach ($asset->licenseseats as $seat){
|
|
$seat->assigned_to = null;
|
|
$seat->save();
|
|
}
|
|
}
|
|
|
|
// Get all pending Acceptances for this asset and delete them
|
|
$acceptances = CheckoutAcceptance::pending()->whereHasMorph('checkoutable',
|
|
[Asset::class],
|
|
function (Builder $query) use ($asset) {
|
|
$query->where('id', $asset->id);
|
|
})->get();
|
|
$acceptances->map(function($acceptance) {
|
|
$acceptance->delete();
|
|
});
|
|
|
|
// Was the asset updated?
|
|
if ($asset->save()) {
|
|
event(new CheckoutableCheckedIn($asset, $target, Auth::user(), $request->input('note'), $checkin_at));
|
|
|
|
if ((isset($user)) && ($backto == 'user')) {
|
|
return redirect()->route('users.show', $user->id)->with('success', trans('admin/hardware/message.checkin.success'));
|
|
}
|
|
|
|
return redirect()->route('hardware.index')->with('success', trans('admin/hardware/message.checkin.success'));
|
|
}
|
|
// Redirect to the asset management page with error
|
|
return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.checkin.error').$asset->getErrors());
|
|
}
|
|
}
|