forked from portabilis/i-educar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCanCreateSchoolClass.php
73 lines (64 loc) · 2.39 KB
/
CanCreateSchoolClass.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
namespace App\Rules;
use App\Models\LegacySchoolClass;
use App\Models\LegacySchoolGrade;
use Illuminate\Contracts\Validation\Rule;
class CanCreateSchoolClass implements Rule
{
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
*
* @return bool
*/
public function passes($attribute, $value)
{
$escolaId = $value->ref_ref_cod_escola;
$serieId = $value->ref_ref_cod_serie;
$turnoId = $value->turma_turno_id;
$anoLetivo = $value->ano;
$isCreate = empty($value->cod_turma);
$schoolGrade = LegacySchoolGrade::query()
->where('ref_cod_escola', $escolaId)
->where('ref_cod_serie', $serieId)
->first();
if ($isCreate && $schoolGrade && $schoolGrade->bloquear_cadastro_turma_para_serie_com_vagas == 1) {
$schoolClasses = LegacySchoolClass::query()
->where('ref_ref_cod_serie', $serieId)
->where('ref_ref_cod_escola', $escolaId)
->where('ativo', 1)
->where('turma_turno_id', $turnoId)
->where('ano', $anoLetivo)
->whereExists(function ($builder) use ($anoLetivo, $escolaId) {
$builder->from('pmieducar.escola_ano_letivo')
->where('andamento', 1)
->where('ativo', 1)
->where('ref_cod_escola', $escolaId)
->where('ano', $anoLetivo)
->get();
})
->get();
foreach ($schoolClasses as $schoolClass) {
$countMatriculas = $schoolClass->getTotalEnrolled();
$maxAlunos = $schoolClass->max_aluno;
if (($maxAlunos - $countMatriculas) > 0) {
$vagas = $schoolClass->max_aluno - $countMatriculas;
$this->message = "Não é possivel cadastrar turmas, pois ainda existem {$vagas} vagas em aberto na turma '{$schoolClass->nm_turma}' desta serie e turno. Tal limitação ocorre devido definição feita para estae scola e série.";
return false;
}
}
}
return true;
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return $this->message;
}
}