This commit is contained in:
126
api/app/Http/Requests/Parametrizacao/KitExame/ItemRequest.php
Normal file
126
api/app/Http/Requests/Parametrizacao/KitExame/ItemRequest.php
Normal file
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Parametrizacao\KitExame;
|
||||
|
||||
use App\Http\Requests\FormRequest;
|
||||
use App\Models\Parametrizacao\Exame\Exame;
|
||||
use App\Models\Parametrizacao\KitExame\KitExame;
|
||||
|
||||
class ItemRequest extends FormRequest
|
||||
{
|
||||
public function authorize()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function rules()
|
||||
{
|
||||
switch ($this->method()) {
|
||||
|
||||
case 'POST': {
|
||||
|
||||
return $this->getPostRules();
|
||||
}
|
||||
case 'PUT': {
|
||||
|
||||
return $this->getPutRules();
|
||||
}
|
||||
default: {
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function getPostRules()
|
||||
{
|
||||
return [
|
||||
'nome' => [
|
||||
'required',
|
||||
'unique:\App\Models\Parametrizacao\KitExame\KitExame,nome,NULL,id,deleted_at,NULL',
|
||||
],
|
||||
'tipo_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
],
|
||||
'itens' => [
|
||||
'bail',
|
||||
'required',
|
||||
'array',
|
||||
function ($attribute, $value, $fail) {
|
||||
|
||||
foreach ($value as $item) {
|
||||
|
||||
if (Exame::findOrFail($item['exame_id'])->tipo_id != $this->tipo_id) {
|
||||
|
||||
$fail("Os exames devem ser do mesmo tipo do kit.");
|
||||
}
|
||||
}
|
||||
},
|
||||
function ($attribute, $value, $fail) {
|
||||
|
||||
$this->validateDuplicatedKitItems($attribute, $value, $fail);
|
||||
},
|
||||
],
|
||||
'itens.*.exame_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
'distinct',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
private function getPutRules()
|
||||
{
|
||||
return [
|
||||
'nome' => [
|
||||
'required',
|
||||
"unique:\App\Models\Parametrizacao\KitExame\KitExame,nome,{$this->kit},id,deleted_at,NULL",
|
||||
],
|
||||
'itens' => [
|
||||
'bail',
|
||||
'required',
|
||||
'array',
|
||||
function ($attribute, $value, $fail) {
|
||||
|
||||
$this->validateDuplicatedKitItems($attribute, $value, $fail);
|
||||
}
|
||||
],
|
||||
'itens.*.exame_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
'distinct',
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
private function validateDuplicatedKitItems($attribute, $value, $fail)
|
||||
{
|
||||
$exames = collect($value)->pluck('exame_id');
|
||||
$examesCount = $exames->count();
|
||||
|
||||
foreach (KitExame::where('id', '<>', $this->kit)->get() as $kit) {
|
||||
|
||||
$currentExames = $kit->itens->pluck('exame_id');
|
||||
|
||||
if ($currentExames->count() != $examesCount) {
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($currentExames->diff($exames)->isEmpty()) {
|
||||
|
||||
$fail("O kit {$kit->nome} já possui estes mesmos exames cadastrados. Utilize ou atualize ele.");
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function messages()
|
||||
{
|
||||
return [
|
||||
'nome.unique' => 'Já existe um kit com este nome cadastrado.'
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user