Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
snipe committed Apr 7, 2023
2 parents 42a60a9 + 42eef7e commit b09b562
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 39 deletions.
25 changes: 18 additions & 7 deletions app/Http/Controllers/Api/ComponentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use App\Events\CheckoutableCheckedIn;
use App\Events\ComponentCheckedIn;
use App\Models\Asset;
use Illuminate\Support\Facades\Validator;

class ComponentsController extends Controller
{
Expand Down Expand Up @@ -225,20 +226,30 @@ public function getAssets(Request $request, $id)
public function checkout(Request $request, $componentId)
{
// Check if the component exists
if (is_null($component = Component::find($componentId))) {
if (!$component = Component::find($componentId)) {
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/components/message.does_not_exist')));
}

$this->authorize('checkout', $component);

$validator = Validator::make($request->all(), [
'asset_id' => 'required|exists:assets,id',
'assigned_qty' => "required|numeric|min:1|digits_between:1,".$component->numRemaining(),
]);

if ($component->numRemaining() >= $request->get('assigned_qty')) {
if ($validator->fails()) {
return response()->json(Helper::formatStandardApiResponse('error', $validator->errors()));

if (!$asset = Asset::find($request->input('assigned_to'))) {
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/hardware/message.does_not_exist')));
}
}

// Make sure there is at least one available to checkout
if ($component->numRemaining() <= $request->get('assigned_qty')) {
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/components/message.checkout.unavailable', ['remaining' => $component->numRemaining(), 'requested' => $request->get('assigned_qty')])));
}

if ($component->numRemaining() >= $request->get('assigned_qty')) {

// Update the accessory data
$asset = Asset::find($request->input('assigned_to'));
$component->assigned_to = $request->input('assigned_to');

$component->assets()->attach($component->id, [
Expand All @@ -255,7 +266,7 @@ public function checkout(Request $request, $componentId)
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/components/message.checkout.success')));
}

return response()->json(Helper::formatStandardApiResponse('error', null, 'Not enough components remaining: '.$component->numRemaining().' remaining, '.$request->get('assigned_qty').' requested.'));
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/components/message.checkout.unavailable', ['remaining' => $component->numRemaining(), 'requested' => $request->get('assigned_qty')])));
}

/**
Expand Down
32 changes: 18 additions & 14 deletions app/Http/Controllers/Components/ComponentCheckoutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public function create($componentId)
}
$this->authorize('checkout', $component);

// Make sure there is at least one available to checkout
if ($component->numRemaining() <= 0){
return redirect()->route('components.index')->with('error', trans('admin/components/message.checkout.unavailable'));
}

return view('components/checkout', compact('component'));
}

Expand All @@ -50,17 +55,23 @@ public function create($componentId)
public function store(Request $request, $componentId)
{
// Check if the component exists
if (is_null($component = Component::find($componentId))) {
if (!$component = Component::find($componentId)) {
// Redirect to the component management page with error
return redirect()->route('components.index')->with('error', trans('admin/components/message.not_found'));
}

$this->authorize('checkout', $component);

$max_to_checkout = $component->numRemaining();

// Make sure there is at least one available to checkout
if ($max_to_checkout <= $request->get('assigned_qty')) {
return redirect()->back()->withInput()->with('error', trans('admin/components/message.checkout.unavailable', ['remaining' => $max_to_checkout, 'requested' => $request->get('assigned_qty')]));
}

$validator = Validator::make($request->all(), [
'asset_id' => 'required',
'assigned_qty' => "required|numeric|between:1,$max_to_checkout",
'asset_id' => 'required|exists:assets,id',
'assigned_qty' => "required|numeric|min:1|digits_between:1,$max_to_checkout",
]);

if ($validator->fails()) {
Expand All @@ -69,24 +80,17 @@ public function store(Request $request, $componentId)
->withInput();
}

$admin_user = Auth::user();
$asset_id = e($request->input('asset_id'));

// Check if the user exists
if (is_null($asset = Asset::find($asset_id))) {
// Redirect to the component management page with error
return redirect()->route('components.index')->with('error', trans('admin/components/message.asset_does_not_exist'));
}
$asset = Asset::find($request->input('asset_id'));

// Update the component data
$component->asset_id = $asset_id;

$component->asset_id = $request->input('asset_id');
$component->assets()->attach($component->id, [
'component_id' => $component->id,
'user_id' => $admin_user->id,
'user_id' => Auth::user(),
'created_at' => date('Y-m-d H:i:s'),
'assigned_qty' => $request->input('assigned_qty'),
'asset_id' => $asset_id,
'asset_id' => $request->input('asset_id'),
'note' => $request->input('note'),
]);

Expand Down
3 changes: 2 additions & 1 deletion resources/lang/en/admin/components/message.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
'checkout' => array(
'error' => 'Component was not checked out, please try again',
'success' => 'Component checked out successfully.',
'user_does_not_exist' => 'That user is invalid. Please try again.'
'user_does_not_exist' => 'That user is invalid. Please try again.',
'unavailable' => 'Not enough components remaining: :remaining remaining, :requested requested ',
),

'checkin' => array(
Expand Down
30 changes: 13 additions & 17 deletions resources/views/components/checkout.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@section('content')

<div class="row">
<div class="col-md-9">
<div class="col-md-8">
<form class="form-horizontal" method="post" action="" autocomplete="off">
<!-- CSRF Token -->
{{ csrf_field() }}
Expand All @@ -25,30 +25,26 @@
@endif

<div class="box-body">
@if ($component->name)
<!-- consumable name -->
<div class="form-group">
<label class="col-sm-3 control-label">{{ trans('admin/components/general.component_name') }}</label>
<div class="col-md-6">
<p class="form-control-static">{{ $component->name }}</p>
</div>
</div>
@endif

<!-- Asset -->
@include ('partials.forms.edit.asset-select', ['translated_name' => trans('general.select_asset'), 'fieldname' => 'asset_id'])

<div class="form-group {{ $errors->has('assigned_qty') ? ' has-error' : '' }}">
<label for="assigned_qty" class="col-md-3 control-label">{{ trans('general.qty') }}
<i class='icon-asterisk'></i></label>
<div class="col-md-9">
<input class="form-control" type="text" name="assigned_qty" id="assigned_qty" style="width: 70px;" value="{{ old('assigned_qty') ?? 1 }}" />
{!! $errors->first('assigned_qty', '<br><span class="alert-msg" aria-hidden="true"><i class="fas fa-times" aria-hidden="true"></i> :message</span>') !!}
<label for="assigned_qty" class="col-md-3 control-label">
{{ trans('general.qty') }}
</label>
<div class="col-md-2 col-sm-5 col-xs-5">
<input class="form-control required col-md-12" type="text" name="assigned_qty" id="assigned_qty" value="{{ old('assigned_qty') ?? 1 }}" />
</div>
@if ($errors->first('assigned_qty'))
<div class="col-md-9 col-md-offset-3">
{!! $errors->first('assigned_qty', '<span class="alert-msg" aria-hidden="true"><i class="fas fa-times" aria-hidden="true"></i> :message</span>') !!}
</div>
@endif
</div>


<!-- Note -->
<div class="form-group {{ $errors->has('note') ? 'error' : '' }}">
<div class="form-group{{ $errors->has('note') ? ' error' : '' }}">
<label for="note" class="col-md-3 control-label">{{ trans('admin/hardware/form.notes') }}</label>
<div class="col-md-7">
<textarea class="col-md-6 form-control" id="note" name="note">{{ old('note', $component->note) }}</textarea>
Expand Down

0 comments on commit b09b562

Please sign in to comment.