diff --git a/app/Http/Requests/Vendor/StoreVendorRequest.php b/app/Http/Requests/Vendor/StoreVendorRequest.php index 28f556a58616..c547c4062e7d 100644 --- a/app/Http/Requests/Vendor/StoreVendorRequest.php +++ b/app/Http/Requests/Vendor/StoreVendorRequest.php @@ -25,42 +25,41 @@ class StoreVendorRequest extends Request * Determine if the user is authorized to make this request. * * @return bool + * @method static \Illuminate\Contracts\Auth\Authenticatable|null user() */ public function authorize() : bool { - return auth()->user()->can('create', Vendor::class); + /** @var \App\User|null $user */ + $user = auth()->user(); + + return $user->can('create', Vendor::class); } public function rules() { - - /* Ensure we have a client name, and that all emails are unique*/ - //$rules['name'] = 'required|min:1'; - // $rules['id_number'] = 'unique:vendors,id_number,'.$this->id.',id,company_id,'.auth()->user()->company()->id; - //$rules['settings'] = new ValidVendorGroupSettingsRule(); + /** @var \App\User|null $user */ + $user = auth()->user(); $rules['contacts.*.email'] = 'bail|nullable|distinct|sometimes|email'; - if (isset($this->number)) { - $rules['number'] = Rule::unique('vendors')->where('company_id', auth()->user()->company()->id); - } - - // if (isset($this->id_number)) { - // $rules['id_number'] = Rule::unique('vendors')->where('company_id', auth()->user()->company()->id); - // } - - $rules['currency_id'] = 'bail|required|exists:currencies,id'; + if (isset($this->number)) + $rules['number'] = Rule::unique('vendors')->where('company_id', $user->company()->id); + $rules['currency_id'] = 'bail|required|exists:currencies,id'; + return $rules; } public function prepareForValidation() { + /** @var \App\User|null $user */ + $user = auth()->user(); + $input = $this->all(); if(!array_key_exists('currency_id', $input)){ - $input['currency_id'] = auth()->user()->company()->settings->currency_id; + $input['currency_id'] = $user->company()->settings->currency_id; } $input = $this->decodePrimaryKeys($input); @@ -71,8 +70,6 @@ class StoreVendorRequest extends Request public function messages() { return [ - // 'unique' => ctrans('validation.unique', ['attribute' => 'email']), - //'required' => trans('validation.required', ['attribute' => 'email']), 'contacts.*.email.required' => ctrans('validation.email', ['attribute' => 'email']), ]; } diff --git a/app/Models/User.php b/app/Models/User.php index a0494419a0ed..c018089d7818 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -212,7 +212,7 @@ class User extends Authenticatable implements MustVerifyEmail /** * Returns the current company. * - * @return Collection + * @return App\Models\Company $company */ public function company() {