Merge pull request #12802 from snipe/fixes/consumables_checkout_when_0_qty
Check for available quantity on consumables before checkout
This commit is contained in:
commit
1ee1cf1d78
3 changed files with 41 additions and 23 deletions
|
@ -154,7 +154,7 @@ class ConsumablesController extends Controller
|
||||||
public function show($id)
|
public function show($id)
|
||||||
{
|
{
|
||||||
$this->authorize('view', Consumable::class);
|
$this->authorize('view', Consumable::class);
|
||||||
$consumable = Consumable::findOrFail($id);
|
$consumable = Consumable::with('users')->findOrFail($id);
|
||||||
|
|
||||||
return (new ConsumablesTransformer)->transformConsumable($consumable);
|
return (new ConsumablesTransformer)->transformConsumable($consumable);
|
||||||
}
|
}
|
||||||
|
@ -253,33 +253,39 @@ class ConsumablesController extends Controller
|
||||||
public function checkout(Request $request, $id)
|
public function checkout(Request $request, $id)
|
||||||
{
|
{
|
||||||
// Check if the consumable exists
|
// Check if the consumable exists
|
||||||
if (is_null($consumable = Consumable::find($id))) {
|
if (!$consumable = Consumable::with('users')->find($id)) {
|
||||||
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/consumables/message.does_not_exist')));
|
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/consumables/message.does_not_exist')));
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->authorize('checkout', $consumable);
|
$this->authorize('checkout', $consumable);
|
||||||
|
|
||||||
if ($consumable->qty > 0) {
|
// Make sure there is at least one available to checkout
|
||||||
|
if ($consumable->numRemaining() <= 0) {
|
||||||
|
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/consumables/message.checkout.unavailable')));
|
||||||
|
\Log::debug('No enough remaining');
|
||||||
|
}
|
||||||
|
|
||||||
// Check if the user exists
|
// Check if the user exists - @TODO: this should probably be handled via validation, not here??
|
||||||
$assigned_to = $request->input('assigned_to');
|
if (!$user = User::find($request->input('assigned_to'))) {
|
||||||
if (is_null($user = User::find($assigned_to))) {
|
// Return error message
|
||||||
// Return error message
|
return response()->json(Helper::formatStandardApiResponse('error', null, 'No user found'));
|
||||||
return response()->json(Helper::formatStandardApiResponse('error', null, 'No user found'));
|
\Log::debug('No valid user');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the consumable data
|
// Update the consumable data
|
||||||
$consumable->assigned_to = e($assigned_to);
|
$consumable->assigned_to = $request->input('assigned_to');
|
||||||
|
|
||||||
$consumable->users()->attach($consumable->id, [
|
$consumable->users()->attach($consumable->id,
|
||||||
'consumable_id' => $consumable->id,
|
[
|
||||||
'user_id' => $user->id,
|
'consumable_id' => $consumable->id,
|
||||||
'assigned_to' => $assigned_to,
|
'user_id' => $user->id,
|
||||||
'note' => $request->input('note'),
|
'assigned_to' => $request->input('assigned_to'),
|
||||||
]);
|
'note' => $request->input('note'),
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
// Log checkout event
|
// Log checkout event
|
||||||
$logaction = $consumable->logCheckout(e($request->input('note')), $user);
|
$logaction = $consumable->logCheckout($request->input('note'), $user);
|
||||||
$data['log_id'] = $logaction->id;
|
$data['log_id'] = $logaction->id;
|
||||||
$data['eula'] = $consumable->getEula();
|
$data['eula'] = $consumable->getEula();
|
||||||
$data['first_name'] = $user->first_name;
|
$data['first_name'] = $user->first_name;
|
||||||
|
@ -289,9 +295,7 @@ class ConsumablesController extends Controller
|
||||||
$data['require_acceptance'] = $consumable->requireAcceptance();
|
$data['require_acceptance'] = $consumable->requireAcceptance();
|
||||||
|
|
||||||
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/consumables/message.checkout.success')));
|
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/consumables/message.checkout.success')));
|
||||||
}
|
|
||||||
|
|
||||||
return response()->json(Helper::formatStandardApiResponse('error', null, 'No consumables remaining'));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -24,9 +24,16 @@ class ConsumableCheckoutController extends Controller
|
||||||
*/
|
*/
|
||||||
public function create($consumableId)
|
public function create($consumableId)
|
||||||
{
|
{
|
||||||
if (is_null($consumable = Consumable::find($consumableId))) {
|
|
||||||
|
if (is_null($consumable = Consumable::with('users')->find($consumableId))) {
|
||||||
return redirect()->route('consumables.index')->with('error', trans('admin/consumables/message.does_not_exist'));
|
return redirect()->route('consumables.index')->with('error', trans('admin/consumables/message.does_not_exist'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Make sure there is at least one available to checkout
|
||||||
|
if ($consumable->numRemaining() <= 0){
|
||||||
|
return redirect()->route('consumables.index')->with('error', trans('admin/consumables/message.checkout.unavailable'));
|
||||||
|
}
|
||||||
|
|
||||||
$this->authorize('checkout', $consumable);
|
$this->authorize('checkout', $consumable);
|
||||||
|
|
||||||
return view('consumables/checkout', compact('consumable'));
|
return view('consumables/checkout', compact('consumable'));
|
||||||
|
@ -44,12 +51,18 @@ class ConsumableCheckoutController extends Controller
|
||||||
*/
|
*/
|
||||||
public function store(Request $request, $consumableId)
|
public function store(Request $request, $consumableId)
|
||||||
{
|
{
|
||||||
if (is_null($consumable = Consumable::find($consumableId))) {
|
if (is_null($consumable = Consumable::with('users')->find($consumableId))) {
|
||||||
return redirect()->route('consumables.index')->with('error', trans('admin/consumables/message.not_found'));
|
return redirect()->route('consumables.index')->with('error', trans('admin/consumables/message.not_found'));
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->authorize('checkout', $consumable);
|
$this->authorize('checkout', $consumable);
|
||||||
|
|
||||||
|
// Make sure there is at least one available to checkout
|
||||||
|
if ($consumable->numRemaining() <= 0) {
|
||||||
|
return redirect()->route('consumables.index')->with('error', trans('admin/consumables/message.checkout.unavailable'));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
$admin_user = Auth::user();
|
$admin_user = Auth::user();
|
||||||
$assigned_to = e($request->input('assigned_to'));
|
$assigned_to = e($request->input('assigned_to'));
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,8 @@ return array(
|
||||||
'checkout' => array(
|
'checkout' => array(
|
||||||
'error' => 'Consumable was not checked out, please try again',
|
'error' => 'Consumable was not checked out, please try again',
|
||||||
'success' => 'Consumable checked out successfully.',
|
'success' => 'Consumable 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' => 'There are not enough consumables for this checkout. Please check the quantity left. ',
|
||||||
),
|
),
|
||||||
|
|
||||||
'checkin' => array(
|
'checkin' => array(
|
||||||
|
|
Loading…
Add table
Reference in a new issue