2018-07-24 19:35:26 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Components;
|
|
|
|
|
2018-09-10 07:40:26 -07:00
|
|
|
use App\Events\CheckoutableCheckedIn;
|
2018-07-25 01:02:06 -07:00
|
|
|
use App\Events\ComponentCheckedIn;
|
2018-07-24 19:35:26 -07:00
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Models\Asset;
|
|
|
|
use App\Models\Component;
|
|
|
|
use Illuminate\Http\Request;
|
2020-04-29 09:50:09 -07:00
|
|
|
use Illuminate\Support\Carbon;
|
2018-07-27 15:23:58 -07:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2018-07-24 19:35:26 -07:00
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
|
|
|
|
class ComponentCheckinController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Returns a view that allows the checkin of a component from an asset.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @see ComponentCheckinController::store() method that stores the data.
|
|
|
|
* @since [v4.1.4]
|
|
|
|
* @param $component_asset_id
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
|
|
|
public function create($component_asset_id)
|
|
|
|
{
|
|
|
|
|
|
|
|
// This could probably be done more cleanly but I am very tired. - @snipe
|
|
|
|
if ($component_assets = DB::table('components_assets')->find($component_asset_id)) {
|
|
|
|
if (is_null($component = Component::find($component_assets->component_id))) {
|
|
|
|
return redirect()->route('components.index')->with('error', trans('admin/components/messages.not_found'));
|
|
|
|
}
|
|
|
|
if (is_null($asset = Asset::find($component_assets->asset_id))) {
|
|
|
|
return redirect()->route('components.index')->with('error',
|
|
|
|
trans('admin/components/message.not_found'));
|
|
|
|
}
|
|
|
|
$this->authorize('checkin', $component);
|
2021-06-10 13:15:52 -07:00
|
|
|
|
|
|
|
return view('components/checkin', compact('component_assets', 'component', 'asset'));
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|
|
|
|
|
2023-10-03 12:31:58 -07:00
|
|
|
return redirect()->route('components.index')->with('error', trans('admin/components/messages.not_found'));
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate and store checkin data.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @see ComponentCheckinController::create() method that returns the form.
|
|
|
|
* @since [v4.1.4]
|
|
|
|
* @param Request $request
|
|
|
|
* @param $component_asset_id
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
2023-10-03 12:30:08 -07:00
|
|
|
public function store(Request $request, $component_asset_id, $backto = null)
|
2018-07-24 19:35:26 -07:00
|
|
|
{
|
|
|
|
if ($component_assets = DB::table('components_assets')->find($component_asset_id)) {
|
|
|
|
if (is_null($component = Component::find($component_assets->component_id))) {
|
2023-10-03 12:30:08 -07:00
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
return redirect()->route('components.index')->with('error',
|
|
|
|
trans('admin/components/message.not_found'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->authorize('checkin', $component);
|
|
|
|
|
|
|
|
$max_to_checkin = $component_assets->assigned_qty;
|
|
|
|
$validator = Validator::make($request->all(), [
|
2021-06-10 13:15:52 -07:00
|
|
|
'checkin_qty' => "required|numeric|between:1,$max_to_checkin",
|
2018-07-24 19:35:26 -07:00
|
|
|
]);
|
|
|
|
|
|
|
|
if ($validator->fails()) {
|
|
|
|
return redirect()->back()
|
|
|
|
->withErrors($validator)
|
|
|
|
->withInput();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validation passed, so let's figure out what we have to do here.
|
2021-06-10 13:15:52 -07:00
|
|
|
$qty_remaining_in_checkout = ($component_assets->assigned_qty - (int) $request->input('checkin_qty'));
|
2018-07-24 19:35:26 -07:00
|
|
|
|
|
|
|
// We have to modify the record to reflect the new qty that's
|
|
|
|
// actually checked out.
|
|
|
|
$component_assets->assigned_qty = $qty_remaining_in_checkout;
|
|
|
|
DB::table('components_assets')->where('id',
|
|
|
|
$component_asset_id)->update(['assigned_qty' => $qty_remaining_in_checkout]);
|
|
|
|
|
|
|
|
// If the checked-in qty is exactly the same as the assigned_qty,
|
|
|
|
// we can simply delete the associated components_assets record
|
|
|
|
if ($qty_remaining_in_checkout == 0) {
|
|
|
|
DB::table('components_assets')->where('id', '=', $component_asset_id)->delete();
|
|
|
|
}
|
|
|
|
|
2018-09-10 07:40:26 -07:00
|
|
|
$asset = Asset::find($component_assets->asset_id);
|
|
|
|
|
2020-04-29 09:50:09 -07:00
|
|
|
event(new CheckoutableCheckedIn($component, $asset, Auth::user(), $request->input('note'), Carbon::now()));
|
2023-11-08 06:40:38 -08:00
|
|
|
if ($backto == 'asset'){
|
|
|
|
return redirect()->route('hardware.show', $asset->id)->with('success',
|
2023-10-03 12:30:08 -07:00
|
|
|
trans('admin/components/message.checkin.success'));
|
|
|
|
}
|
2018-07-25 01:02:06 -07:00
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
return redirect()->route('components.index')->with('success',
|
2020-04-29 09:50:09 -07:00
|
|
|
trans('admin/components/message.checkin.success'));
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2020-04-29 09:50:09 -07:00
|
|
|
return redirect()->route('components.index')->with('error', trans('admin/components/message.does_not_exist'));
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|
|
|
|
}
|