1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 17:01:33 +02:00
invoiceninja/app/Http/Requests/Vendor/StoreVendorRequest.php

79 lines
2.1 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Http\Requests\Vendor;
use App\Http\Requests\Request;
use App\Models\Vendor;
use App\Utils\Traits\MakesHash;
2021-05-21 02:23:37 +02:00
use Illuminate\Validation\Rule;
class StoreVendorRequest extends Request
{
use MakesHash;
/**
* Determine if the user is authorized to make this request.
*
*/
public function authorize() : bool
{
2023-03-09 10:40:49 +01:00
return auth()->user()->can('create', Vendor::class);
}
public function rules()
{
2023-03-09 10:40:49 +01:00
$rules = [];
2022-03-10 10:48:14 +01:00
$rules['contacts.*.email'] = 'bail|nullable|distinct|sometimes|email';
2023-02-16 02:36:09 +01:00
if (isset($this->number)) {
2023-03-09 10:40:49 +01:00
$rules['number'] = Rule::unique('vendors')->where('company_id', auth()->user()->company()->id);
2023-02-16 02:36:09 +01:00
}
2022-12-14 01:09:14 +01:00
2022-12-14 01:26:07 +01:00
$rules['currency_id'] = 'bail|required|exists:currencies,id';
2023-03-18 08:24:56 +01:00
if ($this->file('documents') && is_array($this->file('documents'))) {
2023-02-27 10:12:59 +01:00
$rules['documents.*'] = $this->file_validation;
2023-03-18 08:24:56 +01:00
} elseif ($this->file('documents')) {
2023-02-27 10:12:59 +01:00
$rules['documents'] = $this->file_validation;
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'))) {
$rules['file.*'] = $this->file_validation;
} elseif ($this->file('file')) {
$rules['file'] = $this->file_validation;
}
2022-12-14 01:09:14 +01:00
return $rules;
}
2022-06-24 03:55:41 +02:00
public function prepareForValidation()
{
$input = $this->all();
2023-02-16 02:36:09 +01:00
if (!array_key_exists('currency_id', $input) || empty($input['currency_id'])) {
2023-03-09 10:40:49 +01:00
$input['currency_id'] = auth()->user()->company()->settings->currency_id;
2022-12-14 01:09:14 +01:00
}
$input = $this->decodePrimaryKeys($input);
$this->replace($input);
}
public function messages()
{
return [
'contacts.*.email.required' => ctrans('validation.email', ['attribute' => 'email']),
];
}
}