This commit is contained in:
42
api/app/Rules/Raas/ExportacaoContainsFinanciamento.php
Normal file
42
api/app/Rules/Raas/ExportacaoContainsFinanciamento.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Rules\Raas;
|
||||
|
||||
use App\Models\Raas\Exportacao;
|
||||
use Illuminate\Contracts\Validation\Rule;
|
||||
|
||||
class ExportacaoContainsFinanciamento implements Rule
|
||||
{
|
||||
private $unidadeId;
|
||||
private $competenciaId;
|
||||
|
||||
public function __construct($unidadeId, $competenciaId)
|
||||
{
|
||||
$this->unidadeId = $unidadeId;
|
||||
$this->competenciaId = $competenciaId;
|
||||
}
|
||||
|
||||
public function passes($attribute, $value)
|
||||
{
|
||||
$response = true;
|
||||
|
||||
Exportacao::where('unidade_id', $this->unidadeId)
|
||||
->where('competencia_id', $this->competenciaId)
|
||||
->whereHas('financiamentos', function($query) use ($value) {
|
||||
$query->from('sigtap.financiamento')->whereIn('id', $value);
|
||||
})
|
||||
->get()
|
||||
->map(function($exportacao) use ($value, &$response) {
|
||||
if ($exportacao->financiamentos->count() === count($value)) {
|
||||
$response = false;
|
||||
}
|
||||
});
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function message()
|
||||
{
|
||||
return 'Exportação já existente para os financiamentos selecionados.';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user