educar_avancar_mod_cad.php
14.6 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
<?php
class clsIndexBase extends clsBase
{
public function Formular()
{
$this->SetTitulo($this->_instituicao . ' i-Educar');
$this->processoAp = '845';
}
}
use App\Models\LegacyInstitution;
use Illuminate\Support\Facades\Session;
class indice extends clsCadastro
{
public $pessoa_logada;
public $data_matricula;
public function Inicializar()
{
$retorno = 'Novo';
$obj_permissao = new clsPermissoes();
$obj_permissao->permissao_cadastra(845, $this->pessoa_logada, 7, 'educar_index.php');
$this->breadcrumb('Rematrícula automática', [
url('intranet/educar_index.php') => 'Escola',
]);
return $retorno;
}
public function Gerar()
{
$anoLetivoHelperOptions = ['situacoes' => ['em_andamento', 'nao_iniciado']];
$this->inputsHelper()->dynamic('ano');
$this->inputsHelper()->dynamic(['instituicao', 'escola', 'curso', 'serie']);
$this->inputsHelper()->dynamic('turma', ['label' => 'Selecione a turma do ano anterior', 'required' => false]);
$this->inputsHelper()->dynamic('anoLetivo', ['label' => 'Ano destino'], $anoLetivoHelperOptions);
$this->inputsHelper()->date('data_matricula', ['label' => 'Data da matricula', 'placeholder' => 'dd/mm/yyyy']);
Portabilis_View_Helper_Application::loadJavascript($this, [
'/modules/Cadastro/Assets/Javascripts/RematriculaAutomatica.js'
]);
}
public function Novo()
{
$anoLetivo = new clsPmieducarEscolaAnoLetivo();
$anoLetivo = $anoLetivo->lista($this->ref_cod_escola, null, null, null, 1);
$this->data_matricula = Portabilis_Date_Utils::brToPgSQL($this->data_matricula);
if (count($anoLetivo) > 1) {
Session::now('notice', 'Nenhum aluno rematriculado. Certifique-se que somente um ano letivo encontra-se em aberto.');
return false;
}
// Valida se a data da matrícula não é menor que a data do início do
// ano letivo.
$obj = new clsPmieducarAnoLetivoModulo();
$obj->setOrderBy('sequencial ASC');
$registros = $obj->lista($anoLetivo[0]['ano'], $this->ref_cod_escola);
$inicioAnoLetivo = $registros[0]['data_inicio'];
/** @var LegacyInstitution $instituicao */
$instituicao = app(LegacyInstitution::class);
if ($this->data_matricula < $inicioAnoLetivo) {
if (!$instituicao->allowRegistrationOutAcademicYear) {
Session::now('notice', 'A data da matrícula deve ser posterior ao dia ' . Portabilis_Date_Utils::pgSQLToBr($inicioAnoLetivo) . '.');
return false;
}
}
$this->db = new clsBanco();
$this->db2 = new clsBanco();
$this->db3 = new clsBanco();
$result = $this->rematricularAlunos(
$this->ref_cod_escola,
$this->ref_cod_curso,
$this->ref_cod_serie,
$this->ref_cod_turma,
$_POST['ano']
);
return $result;
}
public function Editar()
{
return true;
}
protected function rematricularAlunos($escolaId, $cursoId, $serieId, $turmaId, $ano)
{
try {
$result = $this->selectMatriculas($escolaId, $cursoId, $serieId, $turmaId, $this->ano_letivo);
$alunosSemInep = $this->getAlunosSemInep($escolaId, $cursoId, $serieId, $turmaId, $ano);
$alunosComSaidaDaEscola = $this->getAlunosComSaidaDaEscola($escolaId, $cursoId, $serieId, $turmaId, $ano);
$count = 0;
$nomesAlunos = [];
if (count($alunosSemInep) === 0) {
while ($result && $this->db->ProximoRegistro()) {
list($matriculaId, $alunoId, $situacao, $nomeAluno) = $this->db->Tupla();
$this->db2->Consulta(
"
UPDATE pmieducar.matricula
SET ultima_matricula = '0'
WHERE cod_matricula = $matriculaId
"
);
if ($result && $situacao == 1 || $situacao == 12 || $situacao == 13) {
$result = $this->rematricularAlunoAprovado($escolaId, $serieId, $this->ano_letivo, $alunoId);
} elseif ($result && $situacao == 2 || $situacao == 14) {
$result = $this->rematricularAlunoReprovado($escolaId, $cursoId, $serieId, $this->ano_letivo, $alunoId);
}
$nomesAlunos[] = $nomeAluno;
$count += 1;
if (!$result) {
break;
}
}
}
} catch (Exception $e) {
Session::now('error', $e->getMessage());
return false;
}
if ($result) {
if ($count > 0) {
$mensagem = '';
if ($count > 0) {
$mensagem .= "Rematriculado os seguinte(s) $count aluno(s) com sucesso em $this->ano_letivo: </br></br>";
foreach ($nomesAlunos as $nome) {
$mensagem .= "{$nome} </br>";
}
$mensagem .= '</br> As enturmações podem ser realizadas em: Movimentação > Enturmação.';
if (count($alunosComSaidaDaEscola) > 0) {
$mensagem .= '</br></br>O(s) seguinte(s) aluno(s) não foram rematriculados, pois possuem saída na escola: </br></br>';
foreach ($alunosComSaidaDaEscola as $nome) {
$mensagem .= "{$nome} </br>";
}
}
}
Session::now('success', $mensagem);
} elseif (count($alunosSemInep) > 0) {
$mensagem = 'Não foi possível realizar a rematrícula, pois o(s) seguinte(s) aluno(s) não possuem o INEP cadastrado: </br></br>';
foreach ($alunosSemInep as $nome) {
$mensagem .= "{$nome} </br>";
}
$mensagem .= '</br>Por favor, cadastre o INEP do(s) aluno(s) em: Cadastros > Aluno > Alunos > Campo: Código INEP.';
Session::now('error', $mensagem);
} elseif ($this->existeMatriculasAprovadasReprovadas($escolaId, $cursoId, $serieId, $turmaId, $this->ano_letivo)) {
Session::now('error', 'Nenhum aluno rematriculado. Certifique-se que a turma possui alunos aprovados ou reprovados em ' . ($this->ano_letivo - 1) . '.');
} else {
Session::now('notice', 'Os alunos desta série já encontram-se rematriculados, sendo assim, favor verificar se as enturmações já foram efetuadas em Movimentação > Enturmação.');
}
} else {
Session::now('error', 'Ocorreu algum erro inesperado durante as rematrículas, por favor, tente novamente.');
}
return $result;
}
protected function getAlunosSemInep($escolaId, $cursoId, $serieId, $turmaId, $ano)
{
//Pega todas as matriculas
$objMatricula = new clsPmieducarMatriculaTurma();
$objMatricula->setOrderby('nome');
$lstMatricula = $objMatricula->lista4($escolaId, $cursoId, $serieId, $turmaId, $ano);
//Verifica o parametro na série pra exigir inep
$objSerie = new clsPmieducarSerie($serieId);
$serieDet = $objSerie->detalhe();
$exigeInep = $serieDet['exigir_inep'];
//Retorna alunos sem inep
$alunosSemInep = [];
$objAluno = new clsPmieducarAluno();
foreach ($lstMatricula as $matricula) {
$alunoInep = $objAluno->verificaInep($matricula['ref_cod_aluno']);
if (!$alunoInep && $exigeInep) {
$alunosSemInep[] = strtoupper($matricula['nome']);
}
}
return $alunosSemInep;
}
protected function getAlunosComSaidaDaEscola($escolaId, $cursoId, $serieId, $turmaId, $ano)
{
$objMatricula = new clsPmieducarMatriculaTurma();
$objMatricula->setOrderby('nome');
$alunosComSaidaDaEscola = $objMatricula->lista4($escolaId, $cursoId, $serieId, $turmaId, $ano, true);
$alunos = [];
foreach ($alunosComSaidaDaEscola as $a) {
$alunos[] = strtoupper($a['nome']);
}
return $alunos;
}
protected function selectMatriculas($escolaId, $cursoId, $serieId, $turmaId, $ano)
{
$anoAnterior = $this->ano_letivo - 1;
$sql = "
SELECT
cod_matricula,
ref_cod_aluno,
aprovado,
(
SELECT upper(nome)
FROM cadastro.pessoa, pmieducar.aluno
WHERE pessoa.idpes = aluno.ref_idpes
AND aluno.cod_aluno = ref_cod_aluno
) as nome
FROM pmieducar.matricula m, pmieducar.matricula_turma
WHERE aprovado in (1, 2, 12, 13, 14)
AND m.ativo = 1
AND ref_ref_cod_escola = $escolaId
AND ref_ref_cod_serie = $serieId
AND ref_cod_curso = $cursoId
AND cod_matricula = ref_cod_matricula
AND matricula_turma.ativo = 1
AND ano = $anoAnterior
AND m.dependencia = FALSE
AND m.saida_escola = FALSE
AND NOT EXISTS(
SELECT 1
FROM pmieducar.matricula m2
WHERE m2.ref_cod_aluno = m.ref_cod_aluno
AND m2.ano = $this->ano_letivo
AND m2.ativo = 1
AND m2.ref_ref_cod_escola = m.ref_ref_cod_escola
)
AND NOT EXISTS(
SELECT 1
FROM pmieducar.matricula m2
WHERE m2.ref_cod_aluno = m.ref_cod_aluno
AND m2.ano = $this->ano_letivo
AND m2.ativo = 1
AND m2.ref_ref_cod_serie = (
SELECT ref_serie_destino
FROM pmieducar.sequencia_serie
WHERE ref_serie_origem = $serieId
AND ativo = 1
)
)
";
if ($turmaId) {
$sql .= "AND ref_cod_turma = $turmaId ORDER BY nome";
}
try {
$this->db->Consulta($sql);
} catch (Exception $e) {
error_log('Erro ao selecionar matrículas ano anterior, no processo rematrícula automática:' . $e->getMessage());
throw new Exception("Erro ao selecionar matrículas ano anterior: $anoAnterior");
}
return true;
}
protected function existeMatriculasAprovadasReprovadas($escolaId, $cursoId, $serieId, $turmaId, $ano)
{
$objMatricula = new clsPmieducarMatriculaTurma();
$objMatricula->setOrderby('nome');
$anoAnterior = $this->ano_letivo - 1;
$matriculas = $objMatricula->lista4($escolaId, $cursoId, $serieId, $turmaId, $anoAnterior);
$qtdMatriculasAprovadasReprovadas = 0;
foreach ($matriculas as $m) {
if (in_array($m['aprovado'], [1, 2, 12, 13])) {
$qtdMatriculasAprovadasReprovadas++;
}
}
return ($qtdMatriculasAprovadasReprovadas == 0) ? true : false;
}
protected function rematricularAlunoAprovado($escolaId, $serieId, $ano, $alunoId)
{
$nextSerieId = $this->db2->campoUnico(
"
SELECT ref_serie_destino FROM pmieducar.sequencia_serie
WHERE ref_serie_origem = $serieId AND ativo = 1
"
);
if (is_numeric($nextSerieId)) {
$nextCursoId = $this->db2->CampoUnico(
"
SELECT ref_cod_curso FROM pmieducar.serie
WHERE cod_serie = $nextSerieId
"
);
if ($this->escolaSerieConfigurada($escolaId, $nextSerieId)) {
return $this->matricularAluno($escolaId, $nextCursoId, $nextSerieId, $this->ano_letivo, $alunoId);
} else {
throw new Exception('A série de destino não está configurada na escola. Favor efetuar o cadastro em Cadastro > Série > Escola-Série');
}
} else {
throw new Exception('Não foi possível obter a próxima série da sequência de enturmação');
}
return false;
}
protected function rematricularAlunoReprovado($escolaId, $cursoId, $serieId, $ano, $alunoId)
{
return $this->matricularAluno($escolaId, $cursoId, $serieId, $this->ano_letivo, $alunoId);
}
protected function matricularAluno($escolaId, $cursoId, $serieId, $ano, $alunoId)
{
$sql = "
INSERT INTO pmieducar.matricula (ref_ref_cod_escola, ref_ref_cod_serie, ref_usuario_cad, ref_cod_aluno, aprovado, data_cadastro, ano, ref_cod_curso, ultima_matricula, data_matricula)
VALUES ('%d', '%d', '%d', '%d', '3', 'NOW()', '%d', '%d', '1','{$this->data_matricula}')
";
try {
$this->db2->Consulta(sprintf(
$sql,
$escolaId,
$serieId,
$this->pessoa_logada,
$alunoId,
$this->ano_letivo,
$cursoId
));
} catch (Exception $e) {
error_log("Erro durante a matrícula do aluno $alunoId, no processo de rematrícula automática:" . $e->getMessage());
throw new Exception("Erro durante matrícula do aluno: $alunoId");
}
$this->auditarMatriculas($escolaId, $cursoId, $serieId, $ano, $alunoId);
return true;
}
protected function auditarMatriculas($escolaId, $cursoId, $serieId, $ano, $alunoId)
{
$objMatricula = new clsPmieducarMatricula();
$matricula = $objMatricula->lista(null, null, $escolaId, $serieId, null, null, $alunoId, null, null, null, null, null, 1, $ano, null, null, null, null, null, null, null, null, null, null, $cursoId);
$matriculaId = $matricula[0]['cod_matricula'];
$objMatricula->cod_matricula = $matriculaId;
$detalhe = $objMatricula->detalhe();
$auditoria = new clsModulesAuditoriaGeral('matricula', $this->pessoa_logada, $matriculaId);
$auditoria->inclusao($detalhe);
return true;
}
protected function escolaSerieConfigurada($escolaId, $serieId)
{
$escolaSerie = new clsPmieducarEscolaSerie($escolaId, $serieId);
$escolaSerie = $escolaSerie->detalhe();
if (count($escolaSerie) > 0) {
if ($escolaSerie['ativo'] == '1') {
return true;
}
}
return false;
}
}
$pagina = new clsIndexBase();
$miolo = new indice();
$pagina->addForm($miolo);
$pagina->MakeAll();