|
1
|
do $$
|
|
2
|
declare
|
|
3
|
vinculoAExcluir record;
|
|
4
|
declare qtdPessoasParaODiscente numeric;
|
|
5
|
declare pessoaResponsavel record;
|
|
6
|
begin
|
|
7
|
|
|
8
|
alter table medio.discente_medio drop constraint discente_infantil_id_responsavel_ufrn_fkey;
|
|
9
|
alter table medio.discente_medio drop constraint discente_infantil_id_outro_responsavel_fkey;
|
|
10
|
|
|
11
|
for vinculoAExcluir in
|
|
12
|
(
|
|
13
|
select d.id_discente,d.matricula,p.id_pessoa, p.nome from
|
|
14
|
medio.discente_medio dm
|
|
15
|
|
|
16
|
inner join public.discente d on dm.id_discente=d.id_discente
|
|
17
|
inner join comum.pessoa p on d.id_pessoa = p.id_pessoa
|
|
18
|
where dm.id_discente not in
|
|
19
|
(
|
|
20
|
|
|
21
|
select mds.id_discente from
|
|
22
|
medio.matricula_discente_serie mds inner join medio.turma_serie ts on mds.id_turma_serie=ts.id_turma_serie
|
|
23
|
inner join medio.situacao_matricula_serie sms on mds.id_situacao_matricula_serie=sms.id_situacao_matricula_serie
|
|
24
|
where ts.id_turma_serie in
|
|
25
|
(
|
|
26
|
|
|
27
|
select distinct ts.id_turma_serie
|
|
28
|
from
|
|
29
|
medio.turma_serie ts
|
|
30
|
inner join medio.turma_serie_ano tsa on ts.id_turma_serie=tsa.id_turma_serie
|
|
31
|
inner join ensino.turma t on tsa.id_turma=t.id_turma
|
|
32
|
inner join ensino.situacao_turma st on t.id_situacao_turma=st.id_situacao_turma
|
|
33
|
where ts.ativo=true and tsa.ativo=true and st.descricao in ('ABERTA', 'A DEFINIR DOCENTE','CONSOLIDADA')
|
|
34
|
and ts.ano=2019
|
|
35
|
)
|
|
36
|
and sms.descricao in ('MATRICULADO', 'APROVADO', 'TRANCADO','APROVADO EM DEPENDÊNCIA')
|
|
37
|
)
|
|
38
|
and trim(p.nome) in
|
|
39
|
(
|
|
40
|
select nome from
|
|
41
|
(
|
|
42
|
select
|
|
43
|
trim(p.nome) as nome,
|
|
44
|
count(*) as qtd
|
|
45
|
from
|
|
46
|
comum.pessoa p
|
|
47
|
inner join public.discente d on
|
|
48
|
p.id_pessoa = d.id_pessoa
|
|
49
|
group by
|
|
50
|
trim(p.nome)
|
|
51
|
having
|
|
52
|
count(*)>1
|
|
53
|
order by
|
|
54
|
trim(p.nome) asc
|
|
55
|
) as nome
|
|
56
|
)
|
|
57
|
|
|
58
|
order by p.nome asc
|
|
59
|
)
|
|
60
|
loop
|
|
61
|
raise notice 'Analisando % % %', vinculoAExcluir.id_discente, vinculoAExcluir.matricula, vinculoAExcluir.nome;
|
|
62
|
|
|
63
|
delete from medio.matricula_componente_serie where id_matricula_discente_serie in
|
|
64
|
(select id_matricula_discente_serie from medio.matricula_discente_serie mds where mds.id_discente=vinculoAExcluir.id_discente);
|
|
65
|
raise notice ' apagados registros de medio.matricula_componente_serie';
|
|
66
|
|
|
67
|
delete from ensino.matricula_componente where id_discente=vinculoAExcluir.id_discente;
|
|
68
|
raise notice ' apagados registros de ensino.matricula_componente';
|
|
69
|
|
|
70
|
delete from medio.matricula_discente_serie where id_discente = vinculoAExcluir.id_discente;
|
|
71
|
raise notice ' apagados registros de medio.matricula_discente_serie';
|
|
72
|
|
|
73
|
delete from medio.registro_emissao_historico where id_discente= vinculoAExcluir.id_discente;
|
|
74
|
raise notice ' apagados registros de medio.registro_emissao_historico';
|
|
75
|
raise notice ' responsáveis:';
|
|
76
|
|
|
77
|
for pessoaResponsavel in (select rd.id_responsavel_discente,rd.id_pessoa, rd.id_discente from infantil.responsavel_discente rd where rd.id_discente=vinculoAExcluir.id_discente)
|
|
78
|
loop
|
|
79
|
|
|
80
|
|
|
81
|
if (select count(*) from comum.pessoa p
|
|
82
|
inner join infantil.responsavel_discente rd on p.id_pessoa = rd.id_pessoa
|
|
83
|
where rd.id_discente != vinculoAExcluir.id_discente
|
|
84
|
and rd.id_pessoa= pessoaResponsavel.id_pessoa)=0
|
|
85
|
then
|
|
86
|
delete from infantil.responsavel_discente where id_responsavel_discente=pessoaResponsavel.id_responsavel_discente;
|
|
87
|
raise notice ' excluído responsavel %', pessoaResponsavel.id_responsavel_discente;
|
|
88
|
delete from comum.alteracao_pessoa where id_pessoa=pessoaResponsavel.id_pessoa;
|
|
89
|
delete from comum.pessoa where id_pessoa=pessoaResponsavel.id_pessoa;
|
|
90
|
raise notice ' excluída pessoa %', pessoaResponsavel.id_pessoa;
|
|
91
|
else
|
|
92
|
delete from infantil.responsavel_discente where id_responsavel_discente=pessoaResponsavel.id_responsavel_discente;
|
|
93
|
raise notice ' excluído responsavel %', pessoaResponsavel.id_responsavel_discente;
|
|
94
|
raise notice ' não foi excluída a pessoa porque tinha associação com outro responsável de outro vínculo.';
|
|
95
|
|
|
96
|
end if;
|
|
97
|
end loop;
|
|
98
|
|
|
99
|
|
|
100
|
delete from medio.discente_medio where id_discente = vinculoAExcluir.id_discente;
|
|
101
|
raise notice ' apagados registros de medio.discente_medio';
|
|
102
|
|
|
103
|
raise notice ' apagando observações associadas ao vínculo duplicado';
|
|
104
|
delete from ensino.observacao_discente where id_discente=vinculoAExcluir.id_discente;
|
|
105
|
|
|
106
|
|
|
107
|
delete from public.discente where id_discente = vinculoAExcluir.id_discente;
|
|
108
|
raise notice ' apagados registros de public.discente';
|
|
109
|
|
|
110
|
if (select count(*) from public.discente where id_pessoa=vinculoAExcluir.id_pessoa)>0 then
|
|
111
|
raise notice ' não foi possível apagar a pessoa porque havia outro vínculo associado, aguardando próximas iterações para averiguar estes vínculos.';
|
|
112
|
else
|
|
113
|
raise notice ' apagando registros de comum.alteracao_pessoa';
|
|
114
|
delete from comum.alteracao_pessoa where id_pessoa=vinculoAExcluir.id_pessoa;
|
|
115
|
delete from comum.pessoa where id_pessoa=vinculoAExcluir.id_pessoa;
|
|
116
|
raise notice ' Registro de pessoa duplicado de % excluído.', vinculoAExcluir.nome;
|
|
117
|
end if;
|
|
118
|
|
|
119
|
end loop;
|
|
120
|
|
|
121
|
ALTER TABLE medio.discente_medio ADD CONSTRAINT discente_infantil_id_responsavel_ufrn_fkey FOREIGN KEY (id_responsavel_ufrn) REFERENCES infantil.responsavel_discente(id_responsavel_discente);
|
|
122
|
ALTER TABLE medio.discente_medio ADD CONSTRAINT discente_infantil_id_outro_responsavel_fkey FOREIGN KEY (id_outro_responsavel) REFERENCES infantil.responsavel_discente(id_responsavel_discente);
|
|
123
|
|
|
124
|
end;
|
|
125
|
$$ language plpgsql;
|