From 570dd09dcd5c7cd8ed4a607485b8e64abe854c25 Mon Sep 17 00:00:00 2001 From: Ivan Nieto Vivanco Date: Tue, 15 Mar 2022 17:52:19 -0600 Subject: [PATCH 1/2] Add validation to Accessories and Consumables to not let the user update the number of items to less than they already have checked out --- .../Accessories/AccessoriesController.php | 12 ++++++++++++ .../Consumables/ConsumablesController.php | 12 ++++++++++++ app/Models/Accessory.php | 15 +++++++++++++++ app/Models/Consumable.php | 15 +++++++++++++++ 4 files changed, 54 insertions(+) diff --git a/app/Http/Controllers/Accessories/AccessoriesController.php b/app/Http/Controllers/Accessories/AccessoriesController.php index aa5c6158c0..e6a1e5876d 100755 --- a/app/Http/Controllers/Accessories/AccessoriesController.php +++ b/app/Http/Controllers/Accessories/AccessoriesController.php @@ -9,6 +9,7 @@ use App\Models\Accessory; use App\Models\Company; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Storage; +use Illuminate\Support\Facades\Validator; use Redirect; /** This controller handles all actions related to Accessories for @@ -130,6 +131,17 @@ class AccessoriesController extends Controller return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.does_not_exist')); } + $min = $accessory->numCHeckedOut(); + $validator = Validator::make($request->all(), [ + "qty" => "required|numeric|min:$min" + ]); + + if ($validator->fails()) { + return redirect()->back() + ->withErrors($validator) + ->withInput(); + } + $this->authorize($accessory); // Update the accessory data diff --git a/app/Http/Controllers/Consumables/ConsumablesController.php b/app/Http/Controllers/Consumables/ConsumablesController.php index ebab91a1d5..2ef123d096 100644 --- a/app/Http/Controllers/Consumables/ConsumablesController.php +++ b/app/Http/Controllers/Consumables/ConsumablesController.php @@ -9,6 +9,7 @@ use App\Models\Company; use App\Models\Consumable; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Input; +use Illuminate\Support\Facades\Validator; /** * This controller handles all actions related to Consumables for @@ -128,6 +129,17 @@ class ConsumablesController extends Controller return redirect()->route('consumables.index')->with('error', trans('admin/consumables/message.does_not_exist')); } + $min = $consumable->numCHeckedOut(); + $validator = Validator::make($request->all(), [ + "qty" => "required|numeric|min:$min" + ]); + + if ($validator->fails()) { + return redirect()->back() + ->withErrors($validator) + ->withInput(); + } + $this->authorize($consumable); $consumable->name = $request->input('name'); diff --git a/app/Models/Accessory.php b/app/Models/Accessory.php index bc7c533832..ae311ed7e8 100755 --- a/app/Models/Accessory.php +++ b/app/Models/Accessory.php @@ -310,6 +310,21 @@ class Accessory extends SnipeModel return null; } + /** + * Check how many items within an accessory are checked out + * + * @author [A. Gianotto] [] + * @since [v5.0] + * @return int + */ + public function numCheckedOut() + { + $checkedout = 0; + $checkedout = $this->users->count(); + + return $checkedout; + } + /** * Check how many items of an accessory remain * diff --git a/app/Models/Consumable.php b/app/Models/Consumable.php index 0c3dc247e7..b22033ba9d 100644 --- a/app/Models/Consumable.php +++ b/app/Models/Consumable.php @@ -276,6 +276,21 @@ class Consumable extends SnipeModel } } + /** + * Check how many items within a consumable are checked out + * + * @author [A. Gianotto] [] + * @since [v5.0] + * @return int + */ + public function numCheckedOut() + { + $checkedout = 0; + $checkedout = $this->users->count(); + + return $checkedout; + } + /** * Checks the number of available consumables * From b5378eff6456abf6dbde1f02b050af02b90fbc75 Mon Sep 17 00:00:00 2001 From: Ivan Nieto Vivanco Date: Tue, 5 Apr 2022 16:12:31 -0500 Subject: [PATCH 2/2] Fix several typos --- app/Http/Controllers/Accessories/AccessoriesController.php | 2 +- app/Http/Controllers/Components/ComponentsController.php | 2 +- app/Http/Controllers/Consumables/ConsumablesController.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/Accessories/AccessoriesController.php b/app/Http/Controllers/Accessories/AccessoriesController.php index e6a1e5876d..d1af79adf1 100755 --- a/app/Http/Controllers/Accessories/AccessoriesController.php +++ b/app/Http/Controllers/Accessories/AccessoriesController.php @@ -131,7 +131,7 @@ class AccessoriesController extends Controller return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.does_not_exist')); } - $min = $accessory->numCHeckedOut(); + $min = $accessory->numCheckedOut(); $validator = Validator::make($request->all(), [ "qty" => "required|numeric|min:$min" ]); diff --git a/app/Http/Controllers/Components/ComponentsController.php b/app/Http/Controllers/Components/ComponentsController.php index 7e606fec26..f943a71a2e 100644 --- a/app/Http/Controllers/Components/ComponentsController.php +++ b/app/Http/Controllers/Components/ComponentsController.php @@ -129,7 +129,7 @@ class ComponentsController extends Controller if (is_null($component = Component::find($componentId))) { return redirect()->route('components.index')->with('error', trans('admin/components/message.does_not_exist')); } - $min = $component->numCHeckedOut(); + $min = $component->numCheckedOut(); $validator = Validator::make($request->all(), [ 'qty' => "required|numeric|min:$min", ]); diff --git a/app/Http/Controllers/Consumables/ConsumablesController.php b/app/Http/Controllers/Consumables/ConsumablesController.php index 2ef123d096..f068e9868d 100644 --- a/app/Http/Controllers/Consumables/ConsumablesController.php +++ b/app/Http/Controllers/Consumables/ConsumablesController.php @@ -129,7 +129,7 @@ class ConsumablesController extends Controller return redirect()->route('consumables.index')->with('error', trans('admin/consumables/message.does_not_exist')); } - $min = $consumable->numCHeckedOut(); + $min = $consumable->numCheckedOut(); $validator = Validator::make($request->all(), [ "qty" => "required|numeric|min:$min" ]);