Skip to content

A free open source IT asset/license management system

Validate when editing the quantity of a component that the new quantity is > the amount checked out

Daniel Meltzer68224757f4b6
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
@@ -156,25 +156,33 @@ class Component extends SnipeModel
}
/**
- * Check how many items within a component are remaining
+ * Check how many items within a component are checked out
*
* @author [A. Gianotto] [<[email protected]>]
- * @since [v3.0]
+ * @since [v5.0]
* @return int
*/
- public function numRemaining()
+ public function numCheckedOut()
{
$checkedout = 0;
-
foreach ($this->assets as $checkout) {
$checkedout += $checkout->pivot->assigned_qty;
}
+ return $checkedout;
+ }
- $total = $this->qty;
- $remaining = $total - $checkedout;
- return $remaining;
- }
+ /**
+ * Check how many items within a component are remaining
+ *
+ * @author [A. Gianotto] [<[email protected]>]
+ * @since [v3.0]
+ * @return int
+ */
+ public function numRemaining()
+ {
+ 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.',