diff --git a/app/Http/Traits/UniqueSerialTrait.php b/app/Http/Traits/UniqueSerialTrait.php deleted file mode 100644 index b5529d7cf830..000000000000 --- a/app/Http/Traits/UniqueSerialTrait.php +++ /dev/null @@ -1,25 +0,0 @@ -unique_serial == '1') { - return 'unique_undeleted:'.$this->table.','.$this->getKey(); - } - } - } -} diff --git a/app/Models/Asset.php b/app/Models/Asset.php index a7d4d7442a5d..ea436f013820 100644 --- a/app/Models/Asset.php +++ b/app/Models/Asset.php @@ -6,7 +6,6 @@ use App\Events\CheckoutableCheckedOut; use App\Exceptions\CheckoutNotAllowed; use App\Helpers\Helper; -use App\Http\Traits\UniqueSerialTrait; use App\Http\Traits\UniqueUndeletedTrait; use App\Models\Traits\Acceptable; use App\Models\Traits\Searchable; @@ -32,7 +31,7 @@ class Asset extends Depreciable protected $presenter = \App\Presenters\AssetPresenter::class; use CompanyableTrait; - use HasFactory, Loggable, Requestable, Presentable, SoftDeletes, ValidatingTrait, UniqueUndeletedTrait, UniqueSerialTrait; + use HasFactory, Loggable, Requestable, Presentable, SoftDeletes, ValidatingTrait, UniqueUndeletedTrait; public const LOCATION = 'location'; public const ASSET = 'asset'; @@ -100,7 +99,7 @@ public function declinedCheckout(User $declinedBy, $signature) 'expected_checkin' => 'date|nullable', 'location_id' => 'exists:locations,id|nullable', 'rtd_location_id' => 'exists:locations,id|nullable', - 'asset_tag' => 'required|min:1|max:255|unique_undeleted:assets,asset_tag', + 'asset_tag' => 'required|min:1|max:255|unique_undeleted:assets,asset_tag|not_array', 'purchase_date' => 'date|date_format:Y-m-d|nullable', 'serial' => 'unique_serial|nullable', 'purchase_cost' => 'numeric|nullable|gte:0', diff --git a/app/Providers/ValidationServiceProvider.php b/app/Providers/ValidationServiceProvider.php index 30c5a0197ea1..55d8efa3ec90 100644 --- a/app/Providers/ValidationServiceProvider.php +++ b/app/Providers/ValidationServiceProvider.php @@ -3,6 +3,7 @@ namespace App\Providers; use App\Models\Department; +use App\Models\Setting; use DB; use Illuminate\Support\ServiceProvider; use Illuminate\Validation\Rule; @@ -72,6 +73,17 @@ public function boot() } }); + + Validator::extend('unique_serial', function ($attribute, $value, $parameters, $validator) { + if(Setting::getSettings()->unique_serial == '1') { + $count = DB::table('assets')->select('id')->where('serial', '=', $value)->whereNull('deleted_at')->count(); + + return $count < 1; + } else { + return true; + } + }); + // Prevent circular references // // Example usage in Location model where parent_id references another Location: diff --git a/resources/lang/en/validation.php b/resources/lang/en/validation.php index bb35515fdb19..85e844781204 100644 --- a/resources/lang/en/validation.php +++ b/resources/lang/en/validation.php @@ -96,6 +96,7 @@ 'unique_undeleted' => 'The :attribute must be unique.', 'non_circular' => 'The :attribute must not create a circular reference.', 'not_array' => 'The :attribute field cannot be an array.', + 'unique_serial' => 'The :attribute must be unique.', 'disallow_same_pwd_as_user_fields' => 'Password cannot be the same as the username.', 'letters' => 'Password must contain at least one letter.', 'numbers' => 'Password must contain at least one number.',