2020-01-20 02:31:58 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-01-20 02:31:58 +01:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-01-20 02:31:58 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-01-20 02:31:58 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Requests\Vendor;
|
|
|
|
|
|
|
|
use App\Http\Requests\Request;
|
|
|
|
use App\Utils\Traits\ChecksEntityStatus;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
2021-03-19 23:51:52 +01:00
|
|
|
use Illuminate\Validation\Rule;
|
2020-01-20 02:31:58 +01:00
|
|
|
|
|
|
|
class UpdateVendorRequest extends Request
|
|
|
|
{
|
|
|
|
use MakesHash;
|
|
|
|
use ChecksEntityStatus;
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-01-20 02:31:58 +01:00
|
|
|
/**
|
|
|
|
* Determine if the user is authorized to make this request.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2024-01-14 05:05:00 +01:00
|
|
|
public function authorize(): bool
|
2020-01-20 02:31:58 +01:00
|
|
|
{
|
2023-08-10 02:04:16 +02:00
|
|
|
/** @var \App\Models\User $user */
|
|
|
|
$user = auth()->user();
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-08-10 02:04:16 +02:00
|
|
|
return $user->can('edit', $this->vendor);
|
2020-01-20 02:31:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function rules()
|
|
|
|
{
|
2023-08-10 02:04:16 +02:00
|
|
|
/** @var \App\Models\User $user */
|
|
|
|
$user = auth()->user();
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2022-12-08 00:38:52 +01:00
|
|
|
$rules['country_id'] = 'integer';
|
2022-06-21 11:57:17 +02:00
|
|
|
|
|
|
|
if ($this->number) {
|
2023-08-10 02:04:16 +02:00
|
|
|
$rules['number'] = Rule::unique('vendors')->where('company_id', $user->company()->id)->ignore($this->vendor->id);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-10-26 12:09:22 +02:00
|
|
|
$rules['contacts'] = 'bail|array';
|
|
|
|
$rules['contacts.*.email'] = 'bail|nullable|distinct|sometimes|email';
|
|
|
|
$rules['contacts.*.password'] = [
|
|
|
|
'bail',
|
|
|
|
'nullable',
|
|
|
|
'sometimes',
|
|
|
|
'string',
|
|
|
|
'min:7', // must be at least 10 characters in length
|
|
|
|
'regex:/[a-z]/', // must contain at least one lowercase letter
|
|
|
|
'regex:/[A-Z]/', // must contain at least one uppercase letter
|
|
|
|
'regex:/[0-9]/', // must contain at least one digit
|
|
|
|
//'regex:/[@$!%*#?&.]/', // must contain a special character
|
|
|
|
];
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2022-12-14 01:09:14 +01:00
|
|
|
$rules['currency_id'] = 'bail|sometimes|exists:currencies,id';
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2023-03-18 08:24:56 +01:00
|
|
|
if ($this->file('documents') && is_array($this->file('documents'))) {
|
2024-03-19 00:46:57 +01:00
|
|
|
$rules['documents.*'] = $this->fileValidation();
|
2023-03-18 08:24:56 +01:00
|
|
|
} elseif ($this->file('documents')) {
|
2024-03-19 00:46:57 +01:00
|
|
|
$rules['documents'] = $this->fileValidation();
|
2024-06-14 09:09:44 +02:00
|
|
|
} else {
|
2024-02-16 19:57:15 +01:00
|
|
|
$rules['documents'] = 'bail|sometimes|array';
|
2023-03-18 08:24:56 +01:00
|
|
|
}
|
2023-02-27 10:12:59 +01:00
|
|
|
|
|
|
|
if ($this->file('file') && is_array($this->file('file'))) {
|
2024-03-19 00:46:57 +01:00
|
|
|
$rules['file.*'] = $this->fileValidation();
|
2023-02-27 10:12:59 +01:00
|
|
|
} elseif ($this->file('file')) {
|
2024-03-19 00:46:57 +01:00
|
|
|
$rules['file'] = $this->fileValidation();
|
2023-02-27 10:12:59 +01:00
|
|
|
}
|
|
|
|
|
2023-08-10 02:04:16 +02:00
|
|
|
$rules['language_id'] = 'bail|nullable|sometimes|exists:languages,id';
|
2024-02-12 10:16:35 +01:00
|
|
|
$rules['classification'] = 'bail|sometimes|nullable|in:individual,business,company,partnership,trust,charity,government,other';
|
2023-08-10 02:04:16 +02:00
|
|
|
|
2020-01-20 02:31:58 +01:00
|
|
|
return $rules;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function messages()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'email' => ctrans('validation.email', ['attribute' => 'email']),
|
|
|
|
'name.required' => ctrans('validation.required', ['attribute' => 'name']),
|
|
|
|
'required' => ctrans('validation.required', ['attribute' => 'email']),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-06-24 03:55:41 +02:00
|
|
|
public function prepareForValidation()
|
2020-01-20 02:31:58 +01:00
|
|
|
{
|
|
|
|
$input = $this->all();
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2024-02-12 04:00:24 +01:00
|
|
|
if (isset($input['name'])) {
|
|
|
|
$input['name'] = strip_tags($input['name']);
|
2020-10-15 23:17:31 +02:00
|
|
|
}
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if (array_key_exists('country_id', $input) && is_null($input['country_id'])) {
|
2022-12-08 00:38:52 +01:00
|
|
|
unset($input['country_id']);
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-12-08 00:38:52 +01:00
|
|
|
|
2022-07-22 06:07:51 +02:00
|
|
|
$input = $this->decodePrimaryKeys($input);
|
|
|
|
|
2020-01-20 02:31:58 +01:00
|
|
|
$this->replace($input);
|
|
|
|
}
|
|
|
|
}
|