Merge pull request #13887 from spencerrlongg/bug/sc-24024

Fix for Unique Serial
This commit is contained in:
snipe 2023-11-16 15:27:55 +00:00 committed by GitHub
commit 81583207a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 28 deletions

View file

@ -1,25 +0,0 @@
<?php
namespace App\Http\Traits;
use App\Models\Setting;
trait UniqueSerialTrait
{
/**
* Prepare a unique_ids rule, adding a model identifier if required.
*
* @param array $parameters
* @param string $field
*
* @return string
*/
protected function prepareUniqueSerialRule($parameters, $field)
{
if ($settings = Setting::getSettings()) {
if ($settings->unique_serial == '1') {
return 'unique_undeleted:'.$this->table.','.$this->getKey();
}
}
}
}

View file

@ -6,7 +6,6 @@ use App\Events\AssetCheckedOut;
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 @@ class Asset extends Depreciable
'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',

View file

@ -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 @@ class ValidationServiceProvider extends ServiceProvider
}
});
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:

View file

@ -96,6 +96,7 @@ return [
'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.',