Skip to content

A free open source IT asset/license management system

Refactored checkout screen to redirect if invalid category

snipe81b2273c371e
diff --git a/app/Http/Controllers/Accessories/AccessoryCheckoutController.php b/app/Http/Controllers/Accessories/AccessoryCheckoutController.php
index 1ea036e6ed..1cafe319d1 100644
--- a/app/Http/Controllers/Accessories/AccessoryCheckoutController.php
+++ b/app/Http/Controllers/Accessories/AccessoryCheckoutController.php
@@ -18,31 +18,36 @@ class AccessoryCheckoutController extends Controller
* Return the form to checkout an Accessory to a user.
*
* @author [A. Gianotto] [<[email protected]>]
- * @param int $accessoryId
+ * @param int $id
* @return View
* @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->numRemaining() <= 0){
- return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.checkout.unavailable'));
- }
-
- if ($accessory->category) {
+ if ($accessory = Accessory::withCount('users as users_count')->find($id)) {
+
$this->authorize('checkout', $accessory);
- // Get the dropdown of users and then pass it to the checkout view
- return view('accessories/checkout', compact('accessory'));
+ if ($accessory->category) {
+ // 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'));
+
}
/**
diff --git a/app/Http/Controllers/Components/ComponentCheckoutController.php b/app/Http/Controllers/Components/ComponentCheckoutController.php
index 412d9dde62..79ff57e7df 100644
--- a/app/Http/Controllers/Components/ComponentCheckoutController.php
+++ b/app/Http/Controllers/Components/ComponentCheckoutController.php
@@ -20,25 +20,38 @@ class ComponentCheckoutController extends Controller
* @author [A. Gianotto] [<[email protected]>]
* @see ComponentCheckoutController::store() method that stores the data.
* @since [v3.0]
- * @param int $componentId
+ * @param int $id
* @return \Illuminate\Contracts\View\View
* @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->numRemaining() <= 0){
- return redirect()->route('components.index')->with('error', trans('admin/components/message.checkout.unavailable'));
+ if ($component = Component::find($id)) {
+
+ $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'));
+
}
/**
diff --git a/app/Http/Controllers/Consumables/ConsumableCheckoutController.php b/app/Http/Controllers/Consumables/ConsumableCheckoutController.php
index 5723c2c7ed..0cac973415 100644
--- a/app/Http/Controllers/Consumables/ConsumableCheckoutController.php
+++ b/app/Http/Controllers/Consumables/ConsumableCheckoutController.php
@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Consumables;
use App\Events\CheckoutableCheckedOut;
use App\Http\Controllers\Controller;
+use App\Models\Accessory;
use App\Models\Consumable;
use App\Models\User;
use Illuminate\Http\Request;
@@ -18,30 +19,38 @@ class ConsumableCheckoutController extends Controller
* @author [A. Gianotto] [<[email protected]>]
* @see ConsumableCheckoutController::store() method that stores the data.
* @since [v1.0]
- * @param int $consumableId
+ * @param int $id
* @return \Illuminate\Contracts\View\View
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
- public function create($consumableId)
+ public function create($id)
{
- if (is_null($consumable = Consumable::with('users')->find($consumableId))) {
- return redirect()->route('consumables.index')->with('error', trans('admin/consumables/message.does_not_exist'));
- }
+ if ($consumable = Consumable::with('users')->find($id)) {
- // 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);
+
+ // 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'));
+ }
- // 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')]));
+ // 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')]));
}
- $this->authorize('checkout', $consumable);
+ // Not found
+ return redirect()->route('consumables.index')->with('error', trans('admin/consumables/message.does_not_exist'));
- return view('consumables/checkout', compact('consumable'));
}
/**
diff --git a/app/Http/Controllers/Licenses/LicenseCheckoutController.php b/app/Http/Controllers/Licenses/LicenseCheckoutController.php
index 07ca8bbd58..d61a1ad819 100644
--- a/app/Http/Controllers/Licenses/LicenseCheckoutController.php
+++ b/app/Http/Controllers/Licenses/LicenseCheckoutController.php
@@ -5,6 +5,7 @@ namespace App\Http\Controllers\Licenses;
use App\Events\CheckoutableCheckedOut;
use App\Http\Controllers\Controller;
use App\Http\Requests\LicenseCheckoutRequest;
+use App\Models\Accessory;
use App\Models\Asset;
use App\Models\License;
use App\Models\LicenseSeat;
@@ -21,23 +22,35 @@ class LicenseCheckoutController extends Controller
*
* @author [A. Gianotto] [<[email protected]>]
* @since [v1.0]
- * @param $licenseId
+ * @param $id
* @return \Illuminate\Contracts\View\View
* @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);
- // If the license is valid, check that there is an available seat
- if ($license->avail_seats_count < 1) {
- return redirect()->route('licenses.index')->with('error', 'There are no available seats for this license');
+
+ if ($license->category) {
+
+ // 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'));