From 68224757f4b60576cb6224553f1514b544a30b97 Mon Sep 17 00:00:00 2001 From: Daniel Meltzer Date: Wed, 29 Apr 2020 13:25:04 -0400 Subject: [PATCH] Validate when editing the quantity of a component that the new quantity is > the amount checked out --- .../Components/ComponentsController.php | 11 +++++++ app/Models/Component.php | 30 ++++++++++++------- resources/lang/en/validation.php | 3 ++ 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/app/Http/Controllers/Components/ComponentsController.php b/app/Http/Controllers/Components/ComponentsController.php index 2118b37b42..9fde48df41 100644 --- a/app/Http/Controllers/Components/ComponentsController.php +++ b/app/Http/Controllers/Components/ComponentsController.php @@ -8,6 +8,7 @@ use App\Models\Component; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Input; use Illuminate\Support\Facades\Storage; +use Illuminate\Support\Facades\Validator; /** * This class controls all actions related to Components for @@ -121,6 +122,16 @@ 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(); + $validator = Validator::make($request->all(), [ + "qty" => "required|numeric|gt:$min" + ]); + + if ($validator->fails()) { + return redirect()->back() + ->withErrors($validator) + ->withInput(); + } $this->authorize('update', $component); diff --git a/app/Models/Component.php b/app/Models/Component.php index e880234ba7..f24f3250e7 100644 --- a/app/Models/Component.php +++ b/app/Models/Component.php @@ -155,6 +155,23 @@ class Component extends SnipeModel return $this->hasMany('\App\Models\Actionlog', 'item_id')->where('item_type', Component::class)->orderBy('created_at', 'desc')->withTrashed(); } + /** + * Check how many items within a component are checked out + * + * @author [A. Gianotto] [] + * @since [v5.0] + * @return int + */ + public function numCheckedOut() + { + $checkedout = 0; + foreach ($this->assets as $checkout) { + $checkedout += $checkout->pivot->assigned_qty; + } + + return $checkedout; + } + /** * Check how many items within a component are remaining * @@ -164,17 +181,8 @@ class Component extends SnipeModel */ public function numRemaining() { - $checkedout = 0; - - foreach ($this->assets as $checkout) { - $checkedout += $checkout->pivot->assigned_qty; - } - - - $total = $this->qty; - $remaining = $total - $checkedout; - return $remaining; - } + return $this->qty - $this->numCheckedOut(); + } /** * Query builder scope to order on company diff --git a/resources/lang/en/validation.php b/resources/lang/en/validation.php index 47f4377ad0..e399504d08 100644 --- a/resources/lang/en/validation.php +++ b/resources/lang/en/validation.php @@ -44,6 +44,9 @@ return array( 'exists' => 'The selected :attribute is invalid.', 'file' => 'The :attribute must be a file.', 'filled' => 'The :attribute field must have a value.', + 'gt' => [ + 'numeric' => 'The :attribute field must be greater than :value.' + ], 'hashed_pass' => 'Your password is incorrect.', 'image' => 'The :attribute must be an image.', 'in' => 'The selected :attribute is invalid.',