Refactored checkout screen to redirect if invalid category

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2023-11-23 16:18:28 +00:00
parent 978bbeccc5
commit 81b2273c37
4 changed files with 92 additions and 52 deletions

View file

@ -18,31 +18,36 @@ class AccessoryCheckoutController extends Controller
* Return the form to checkout an Accessory to a user. * Return the form to checkout an Accessory to a user.
* *
* @author [A. Gianotto] [<snipe@snipe.net>] * @author [A. Gianotto] [<snipe@snipe.net>]
* @param int $accessoryId * @param int $id
* @return View * @return View
* @throws \Illuminate\Auth\Access\AuthorizationException * @throws \Illuminate\Auth\Access\AuthorizationException
*/ */
public function create($accessoryId) public function create($id)
{ {
// Check if the accessory exists
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 = Accessory::withCount('users as users_count')->find($id)) {
if ($accessory->numRemaining() <= 0){
return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.checkout.unavailable'));
}
if ($accessory->category) {
$this->authorize('checkout', $accessory); $this->authorize('checkout', $accessory);
// Get the dropdown of users and then pass it to the checkout view if ($accessory->category) {
return view('accessories/checkout', compact('accessory')); // 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'));
}
// Return the checkout view
return view('accessories/checkout', compact('accessory'));
}
// Invalid category
return redirect()->route('accessories.edit', ['accessory' => $accessory->id])
->with('error', trans('general.invalid_item_category_single', ['type' => trans('general.accessory')]));
} }
return redirect()->back()->with('error', 'The category type for this accessory is not valid. Edit the accessory and select a valid accessory category.'); // Not found
return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.not_found'));
} }
/** /**

View file

@ -20,25 +20,38 @@ class ComponentCheckoutController extends Controller
* @author [A. Gianotto] [<snipe@snipe.net>] * @author [A. Gianotto] [<snipe@snipe.net>]
* @see ComponentCheckoutController::store() method that stores the data. * @see ComponentCheckoutController::store() method that stores the data.
* @since [v3.0] * @since [v3.0]
* @param int $componentId * @param int $id
* @return \Illuminate\Contracts\View\View * @return \Illuminate\Contracts\View\View
* @throws \Illuminate\Auth\Access\AuthorizationException * @throws \Illuminate\Auth\Access\AuthorizationException
*/ */
public function create($componentId) public function create($id)
{ {
// Check if the component exists
if (is_null($component = Component::find($componentId))) {
// Redirect to the component management page with error
return redirect()->route('components.index')->with('error', trans('admin/components/message.not_found'));
}
$this->authorize('checkout', $component);
// Make sure there is at least one available to checkout if ($component = Component::find($id)) {
if ($component->numRemaining() <= 0){
return redirect()->route('components.index')->with('error', trans('admin/components/message.checkout.unavailable')); $this->authorize('checkout', $component);
// Make sure the category is valid
if ($component->category) {
// Make sure there is at least one available to checkout
if ($component->numRemaining() <= 0){
return redirect()->route('components.index')
->with('error', trans('admin/components/message.checkout.unavailable'));
}
// Return the checkout view
return view('components/checkout', compact('component'));
}
// Invalid category
return redirect()->route('components.edit', ['component' => $component->id])
->with('error', trans('general.invalid_item_category_single', ['type' => trans('general.component')]));
} }
return view('components/checkout', compact('component')); // Not found
return redirect()->route('components.index')->with('error', trans('admin/components/message.not_found'));
} }
/** /**

View file

@ -4,6 +4,7 @@ namespace App\Http\Controllers\Consumables;
use App\Events\CheckoutableCheckedOut; use App\Events\CheckoutableCheckedOut;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Models\Accessory;
use App\Models\Consumable; use App\Models\Consumable;
use App\Models\User; use App\Models\User;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@ -18,30 +19,38 @@ class ConsumableCheckoutController extends Controller
* @author [A. Gianotto] [<snipe@snipe.net>] * @author [A. Gianotto] [<snipe@snipe.net>]
* @see ConsumableCheckoutController::store() method that stores the data. * @see ConsumableCheckoutController::store() method that stores the data.
* @since [v1.0] * @since [v1.0]
* @param int $consumableId * @param int $id
* @return \Illuminate\Contracts\View\View * @return \Illuminate\Contracts\View\View
* @throws \Illuminate\Auth\Access\AuthorizationException * @throws \Illuminate\Auth\Access\AuthorizationException
*/ */
public function create($consumableId) public function create($id)
{ {
if (is_null($consumable = Consumable::with('users')->find($consumableId))) { if ($consumable = Consumable::with('users')->find($id)) {
return redirect()->route('consumables.index')->with('error', trans('admin/consumables/message.does_not_exist'));
$this->authorize('checkout', $consumable);
// Make sure the category is valid
if ($consumable->category) {
// 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'));
}
// Return the checkout view
return view('consumables/checkout', compact('consumable'));
}
// Invalid category
return redirect()->route('consumables.edit', ['consumable' => $consumable->id])
->with('error', trans('general.invalid_item_category_single', ['type' => trans('general.consumable')]));
} }
// Make sure there is at least one available to checkout // Not found
if ($consumable->numRemaining() <= 0){ 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.checkout.unavailable'));
}
// Make sure there is a valid category
if (!$consumable->category){
return redirect()->route('consumables.edit', ['consumable' => $consumable->id])->with('error', trans('general.invalid_item_category_single', ['type' => trans('general.consumable')]));
}
$this->authorize('checkout', $consumable);
return view('consumables/checkout', compact('consumable'));
} }
/** /**

View file

@ -5,6 +5,7 @@ namespace App\Http\Controllers\Licenses;
use App\Events\CheckoutableCheckedOut; use App\Events\CheckoutableCheckedOut;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Http\Requests\LicenseCheckoutRequest; use App\Http\Requests\LicenseCheckoutRequest;
use App\Models\Accessory;
use App\Models\Asset; use App\Models\Asset;
use App\Models\License; use App\Models\License;
use App\Models\LicenseSeat; use App\Models\LicenseSeat;
@ -21,23 +22,35 @@ class LicenseCheckoutController extends Controller
* *
* @author [A. Gianotto] [<snipe@snipe.net>] * @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v1.0] * @since [v1.0]
* @param $licenseId * @param $id
* @return \Illuminate\Contracts\View\View * @return \Illuminate\Contracts\View\View
* @throws \Illuminate\Auth\Access\AuthorizationException * @throws \Illuminate\Auth\Access\AuthorizationException
*/ */
public function create($licenseId) public function create($id)
{ {
// Check that the license is valid
if ($license = License::find($licenseId)) { if ($license = License::find($id)) {
$this->authorize('checkout', $license); $this->authorize('checkout', $license);
// If the license is valid, check that there is an available seat
if ($license->avail_seats_count < 1) { if ($license->category) {
return redirect()->route('licenses.index')->with('error', 'There are no available seats for this license');
// Make sure there is at least one available to checkout
if ($license->availCount()->count() < 1){
return redirect()->route('licenses.index')->with('error', trans('admin/licenses/message.checkout.not_enough_seats'));
}
// Return the checkout view
return view('licenses/checkout', compact('license'));
} }
return view('licenses/checkout', compact('license'));
// Invalid category
return redirect()->route('licenses.edit', ['license' => $license->id])
->with('error', trans('general.invalid_item_category_single', ['type' => trans('general.license')]));
} }
// Not found
return redirect()->route('licenses.index')->with('error', trans('admin/licenses/message.not_found')); return redirect()->route('licenses.index')->with('error', trans('admin/licenses/message.not_found'));