Skip to content

Commit b09b562

Browse files
committed
Merge remote-tracking branch 'origin/develop'
2 parents 42a60a9 + 42eef7e commit b09b562

File tree

4 files changed

+51
-39
lines changed

4 files changed

+51
-39
lines changed

app/Http/Controllers/Api/ComponentsController.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use App\Events\CheckoutableCheckedIn;
1313
use App\Events\ComponentCheckedIn;
1414
use App\Models\Asset;
15+
use Illuminate\Support\Facades\Validator;
1516

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

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

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

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

237-
if (!$asset = Asset::find($request->input('assigned_to'))) {
238-
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/hardware/message.does_not_exist')));
239-
}
243+
}
244+
245+
// Make sure there is at least one available to checkout
246+
if ($component->numRemaining() <= $request->get('assigned_qty')) {
247+
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/components/message.checkout.unavailable', ['remaining' => $component->numRemaining(), 'requested' => $request->get('assigned_qty')])));
248+
}
249+
250+
if ($component->numRemaining() >= $request->get('assigned_qty')) {
240251

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

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

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

261272
/**

app/Http/Controllers/Components/ComponentCheckoutController.php

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ public function create($componentId)
3333
}
3434
$this->authorize('checkout', $component);
3535

36+
// Make sure there is at least one available to checkout
37+
if ($component->numRemaining() <= 0){
38+
return redirect()->route('components.index')->with('error', trans('admin/components/message.checkout.unavailable'));
39+
}
40+
3641
return view('components/checkout', compact('component'));
3742
}
3843

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

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

6065
$max_to_checkout = $component->numRemaining();
66+
67+
// Make sure there is at least one available to checkout
68+
if ($max_to_checkout <= $request->get('assigned_qty')) {
69+
return redirect()->back()->withInput()->with('error', trans('admin/components/message.checkout.unavailable', ['remaining' => $max_to_checkout, 'requested' => $request->get('assigned_qty')]));
70+
}
71+
6172
$validator = Validator::make($request->all(), [
62-
'asset_id' => 'required',
63-
'assigned_qty' => "required|numeric|between:1,$max_to_checkout",
73+
'asset_id' => 'required|exists:assets,id',
74+
'assigned_qty' => "required|numeric|min:1|digits_between:1,$max_to_checkout",
6475
]);
6576

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

72-
$admin_user = Auth::user();
73-
$asset_id = e($request->input('asset_id'));
74-
7583
// Check if the user exists
76-
if (is_null($asset = Asset::find($asset_id))) {
77-
// Redirect to the component management page with error
78-
return redirect()->route('components.index')->with('error', trans('admin/components/message.asset_does_not_exist'));
79-
}
84+
$asset = Asset::find($request->input('asset_id'));
8085

8186
// Update the component data
82-
$component->asset_id = $asset_id;
83-
87+
$component->asset_id = $request->input('asset_id');
8488
$component->assets()->attach($component->id, [
8589
'component_id' => $component->id,
86-
'user_id' => $admin_user->id,
90+
'user_id' => Auth::user(),
8791
'created_at' => date('Y-m-d H:i:s'),
8892
'assigned_qty' => $request->input('assigned_qty'),
89-
'asset_id' => $asset_id,
93+
'asset_id' => $request->input('asset_id'),
9094
'note' => $request->input('note'),
9195
]);
9296

resources/lang/en/admin/components/message.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
'checkout' => array(
2424
'error' => 'Component was not checked out, please try again',
2525
'success' => 'Component checked out successfully.',
26-
'user_does_not_exist' => 'That user is invalid. Please try again.'
26+
'user_does_not_exist' => 'That user is invalid. Please try again.',
27+
'unavailable' => 'Not enough components remaining: :remaining remaining, :requested requested ',
2728
),
2829

2930
'checkin' => array(

resources/views/components/checkout.blade.php

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
@section('content')
1111

1212
<div class="row">
13-
<div class="col-md-9">
13+
<div class="col-md-8">
1414
<form class="form-horizontal" method="post" action="" autocomplete="off">
1515
<!-- CSRF Token -->
1616
{{ csrf_field() }}
@@ -25,30 +25,26 @@
2525
@endif
2626

2727
<div class="box-body">
28-
@if ($component->name)
29-
<!-- consumable name -->
30-
<div class="form-group">
31-
<label class="col-sm-3 control-label">{{ trans('admin/components/general.component_name') }}</label>
32-
<div class="col-md-6">
33-
<p class="form-control-static">{{ $component->name }}</p>
34-
</div>
35-
</div>
36-
@endif
37-
3828
<!-- Asset -->
3929
@include ('partials.forms.edit.asset-select', ['translated_name' => trans('general.select_asset'), 'fieldname' => 'asset_id'])
4030

4131
<div class="form-group {{ $errors->has('assigned_qty') ? ' has-error' : '' }}">
42-
<label for="assigned_qty" class="col-md-3 control-label">{{ trans('general.qty') }}
43-
<i class='icon-asterisk'></i></label>
44-
<div class="col-md-9">
45-
<input class="form-control" type="text" name="assigned_qty" id="assigned_qty" style="width: 70px;" value="{{ old('assigned_qty') ?? 1 }}" />
46-
{!! $errors->first('assigned_qty', '<br><span class="alert-msg" aria-hidden="true"><i class="fas fa-times" aria-hidden="true"></i> :message</span>') !!}
32+
<label for="assigned_qty" class="col-md-3 control-label">
33+
{{ trans('general.qty') }}
34+
</label>
35+
<div class="col-md-2 col-sm-5 col-xs-5">
36+
<input class="form-control required col-md-12" type="text" name="assigned_qty" id="assigned_qty" value="{{ old('assigned_qty') ?? 1 }}" />
4737
</div>
38+
@if ($errors->first('assigned_qty'))
39+
<div class="col-md-9 col-md-offset-3">
40+
{!! $errors->first('assigned_qty', '<span class="alert-msg" aria-hidden="true"><i class="fas fa-times" aria-hidden="true"></i> :message</span>') !!}
41+
</div>
42+
@endif
4843
</div>
4944

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

0 commit comments

Comments
 (0)