Skip to content

A free open source IT asset/license management system

Check for available quantity on consumables before checkout

snipe4840046f44e9
diff --git a/app/Http/Controllers/Api/ConsumablesController.php b/app/Http/Controllers/Api/ConsumablesController.php
index fc6620df48..e9a069c379 100644
--- a/app/Http/Controllers/Api/ConsumablesController.php
+++ b/app/Http/Controllers/Api/ConsumablesController.php
@@ -154,7 +154,7 @@ class ConsumablesController extends Controller
public function show($id)
{
$this->authorize('view', Consumable::class);
- $consumable = Consumable::findOrFail($id);
+ $consumable = Consumable::with('users')->findOrFail($id);
return (new ConsumablesTransformer)->transformConsumable($consumable);
}
@@ -253,33 +253,39 @@ class ConsumablesController extends Controller
public function checkout(Request $request, $id)
{
// 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')));
}
$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
- $assigned_to = $request->input('assigned_to');
- if (is_null($user = User::find($assigned_to))) {
- // Return error message
- return response()->json(Helper::formatStandardApiResponse('error', null, 'No user found'));
- }
+ // Check if the user exists - @TODO: this should probably be handled via validation, not here??
+ if (!$user = User::find($request->input('assigned_to'))) {
+ // Return error message
+ return response()->json(Helper::formatStandardApiResponse('error', null, 'No user found'));
+ \Log::debug('No valid user');
+ }
- // Update the consumable data
- $consumable->assigned_to = e($assigned_to);
+ // Update the consumable data
+ $consumable->assigned_to = $request->input('assigned_to');
- $consumable->users()->attach($consumable->id, [
- 'consumable_id' => $consumable->id,
- 'user_id' => $user->id,
- 'assigned_to' => $assigned_to,
- 'note' => $request->input('note'),
- ]);
+ $consumable->users()->attach($consumable->id,
+ [
+ 'consumable_id' => $consumable->id,
+ 'user_id' => $user->id,
+ 'assigned_to' => $request->input('assigned_to'),
+ 'note' => $request->input('note'),
+ ]
+ );
// Log checkout event
- $logaction = $consumable->logCheckout(e($request->input('note')), $user);
+ $logaction = $consumable->logCheckout($request->input('note'), $user);
$data['log_id'] = $logaction->id;
$data['eula'] = $consumable->getEula();
$data['first_name'] = $user->first_name;
@@ -289,9 +295,7 @@ class ConsumablesController extends Controller
$data['require_acceptance'] = $consumable->requireAcceptance();
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/consumables/message.checkout.success')));
- }
- return response()->json(Helper::formatStandardApiResponse('error', null, 'No consumables remaining'));
}
/**
diff --git a/app/Http/Controllers/Consumables/ConsumableCheckoutController.php b/app/Http/Controllers/Consumables/ConsumableCheckoutController.php
index f7f2b6e54d..6585624d82 100644
--- a/app/Http/Controllers/Consumables/ConsumableCheckoutController.php
+++ b/app/Http/Controllers/Consumables/ConsumableCheckoutController.php
@@ -24,9 +24,16 @@ class ConsumableCheckoutController extends Controller
*/
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'));
}
+
+ // 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);
return view('consumables/checkout', compact('consumable'));
@@ -44,12 +51,18 @@ class ConsumableCheckoutController extends Controller
*/
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'));
}
$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();
$assigned_to = e($request->input('assigned_to'));
diff --git a/resources/lang/en/admin/consumables/message.php b/resources/lang/en/admin/consumables/message.php
index 48a3cfbd9f..c0d0aa7f68 100644
--- a/resources/lang/en/admin/consumables/message.php
+++ b/resources/lang/en/admin/consumables/message.php
@@ -23,7 +23,8 @@ return array(
'checkout' => array(
'error' => 'Consumable was not checked out, please try again',
'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(