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

This commit is contained in:
Daniel Meltzer 2020-04-29 13:25:04 -04:00
parent 358609720a
commit 68224757f4
No known key found for this signature in database
GPG key ID: 91C5C7B09A5B1CA0
3 changed files with 33 additions and 11 deletions

View file

@ -8,6 +8,7 @@ use App\Models\Component;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input; use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
/** /**
* This class controls all actions related to Components for * This class controls all actions related to Components for
@ -121,6 +122,16 @@ class ComponentsController extends Controller
if (is_null($component = Component::find($componentId))) { if (is_null($component = Component::find($componentId))) {
return redirect()->route('components.index')->with('error', trans('admin/components/message.does_not_exist')); 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); $this->authorize('update', $component);

View file

@ -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(); 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] [<snipe@snipe.net>]
* @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 * Check how many items within a component are remaining
* *
@ -164,17 +181,8 @@ class Component extends SnipeModel
*/ */
public function numRemaining() public function numRemaining()
{ {
$checkedout = 0; return $this->qty - $this->numCheckedOut();
}
foreach ($this->assets as $checkout) {
$checkedout += $checkout->pivot->assigned_qty;
}
$total = $this->qty;
$remaining = $total - $checkedout;
return $remaining;
}
/** /**
* Query builder scope to order on company * Query builder scope to order on company

View file

@ -44,6 +44,9 @@ return array(
'exists' => 'The selected :attribute is invalid.', 'exists' => 'The selected :attribute is invalid.',
'file' => 'The :attribute must be a file.', 'file' => 'The :attribute must be a file.',
'filled' => 'The :attribute field must have a value.', 'filled' => 'The :attribute field must have a value.',
'gt' => [
'numeric' => 'The :attribute field must be greater than :value.'
],
'hashed_pass' => 'Your password is incorrect.', 'hashed_pass' => 'Your password is incorrect.',
'image' => 'The :attribute must be an image.', 'image' => 'The :attribute must be an image.',
'in' => 'The selected :attribute is invalid.', 'in' => 'The selected :attribute is invalid.',