Merge remote-tracking branch 'origin/develop'

This commit is contained in:
snipe 2023-04-06 18:42:22 -07:00
commit d120fd2a16
2 changed files with 19 additions and 13 deletions

View file

@ -25,11 +25,16 @@ class AccessoryCheckoutController extends Controller
public function create($accessoryId) public function create($accessoryId)
{ {
// Check if the accessory exists // 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 // Redirect to the accessory management page with error
return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.not_found')); 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) { if ($accessory->category) {
$this->authorize('checkout', $accessory); $this->authorize('checkout', $accessory);
@ -55,17 +60,23 @@ class AccessoryCheckoutController extends Controller
public function store(Request $request, $accessoryId) public function store(Request $request, $accessoryId)
{ {
// Check if the accessory exists // 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 // Redirect to the accessory management page with error
return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.user_not_found')); return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.user_not_found'));
} }
$this->authorize('checkout', $accessory); $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')); 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 // Update the accessory data
$accessory->assigned_to = e($request->input('assigned_to')); $accessory->assigned_to = e($request->input('assigned_to'));
@ -77,15 +88,6 @@ class AccessoryCheckoutController extends Controller
'note' => $request->input('note'), '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(); 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'))); event(new CheckoutableCheckedOut($accessory, $user, Auth::user(), $request->input('note')));

View file

@ -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] [<snipe@snipe.net>] * @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v3.0] * @since [v3.0]