Skip to content

A free open source IT asset/license management system

Fixed accessories verifying that enough remain to be checked out

snipeffdc0d76f71e
diff --git a/app/Http/Controllers/Accessories/AccessoryCheckoutController.php b/app/Http/Controllers/Accessories/AccessoryCheckoutController.php
index bbfc91b831..1ea036e6ed 100644
--- a/app/Http/Controllers/Accessories/AccessoryCheckoutController.php
+++ b/app/Http/Controllers/Accessories/AccessoryCheckoutController.php
@@ -25,11 +25,16 @@ class AccessoryCheckoutController extends Controller
public function create($accessoryId)
{
// Check if the accessory exists
- if (is_null($accessory = Accessory::find($accessoryId))) {
+ if (is_null($accessory = Accessory::withCount('users as users_count')->find($accessoryId))) {
// Redirect to the accessory management page with error
return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.not_found'));
}
+ // Make sure there is at least one available to checkout
+ if ($accessory->numRemaining() <= 0){
+ return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.checkout.unavailable'));
+ }
+
if ($accessory->category) {
$this->authorize('checkout', $accessory);
@@ -55,17 +60,23 @@ class AccessoryCheckoutController extends Controller
public function store(Request $request, $accessoryId)
{
// Check if the accessory exists
- if (is_null($accessory = Accessory::find($accessoryId))) {
+ if (is_null($accessory = Accessory::withCount('users as users_count')->find($accessoryId))) {
// Redirect to the accessory management page with error
return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.user_not_found'));
}
$this->authorize('checkout', $accessory);
- if (! $user = User::find($request->input('assigned_to'))) {
+ if (!$user = User::find($request->input('assigned_to'))) {
return redirect()->route('accessories.checkout.show', $accessory->id)->with('error', trans('admin/accessories/message.checkout.user_does_not_exist'));
}
+ // Make sure there is at least one available to checkout
+ if ($accessory->numRemaining() <= 0){
+ return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.checkout.unavailable'));
+ }
+
+
// Update the accessory data
$accessory->assigned_to = e($request->input('assigned_to'));
@@ -77,15 +88,6 @@ class AccessoryCheckoutController extends Controller
'note' => $request->input('note'),
]);
- $available= new Accessory();
-
-
-
- if($available->numRemaining()<=0){
-
- return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.checkout.unavailable'));
- }
-
DB::table('accessories_users')->where('assigned_to', '=', $accessory->assigned_to)->where('accessory_id', '=', $accessory->id)->first();
event(new CheckoutableCheckedOut($accessory, $user, Auth::user(), $request->input('note')));
diff --git a/app/Models/Accessory.php b/app/Models/Accessory.php
index 0457cf253f..7576cc644f 100755
--- a/app/Models/Accessory.php
+++ b/app/Models/Accessory.php
@@ -330,7 +330,11 @@ class Accessory extends SnipeModel
/**
- * Check how many items of an accessory remain
+ * Check how many items of an accessory remain.
+ *
+ * In order to use this model method, you MUST call withCount('users as users_count')
+ * on the eloquent query in the controller, otherwise $this->>users_count will be null and
+ * bad things happen.
*
* @author [A. Gianotto] [<[email protected]>]
* @since [v3.0]