Dumped on 2009-12-30
begin
if new.Password is not NULL then
execute 'ALTER USER ' || new.Login ||
' WITH PASSWORD ''' || quote_ident(new.Password) || '\'';
end if;
return new;
end;
declare
r1 RECORD;
r2 RECORD;
utente VARCHAR;
gruppopg VARCHAR;
begin
for r1 in SELECT * FROM Anagrafiche.Utenti WHERE IDUtente = old.IDUtente loop
utente := r1.login;
end loop;
for r2 in SELECT * FROM Anagrafiche.GruppiPg WHERE IDGruppoPg = old.IDGruppoPg loop
gruppopg := r2.denominazione;
end loop;
execute 'REVOKE ' || gruppopg || ' FROM ' || utente;
return old;
end;
begin execute 'DROP ROLE ' || old.login; return old; end;
begin
if new.IDAllegato is NULL or new.IDAllegato = 0 then
new.IDAllegato = nextval('Anagrafiche.Gen_IDAllegato');
end if;
new.Protetto = FALSE;
return new;
end;
begin
if new.IDAzione is NULL or new.IDAzione = 0 then
new.IDAzione = nextval('Anagrafiche.Gen_IDAzione');
end if;
return new;
end;
begin
if new.IDComuneCatastale is NULL or new.IDComuneCatastale = 0 then
new.IDComuneCatastale = nextval('Anagrafiche.Gen_IDComuneCatastale');
end if;
return new;
end;
begin
if new.IDCondizione is NULL or new.IDCondizione = 0 then
new.IDCondizione = nextval('Anagrafiche.Gen_IDCondizione');
end if;
return new;
end;
begin
if new.IDFase is NULL or new.IDFase = 0 then
new.IDFase = nextval('Anagrafiche.Gen_IDFase');
end if;
return new;
end;
begin
if new.IDGruppo is NULL or new.IDGruppo = 0 then
new.IDGruppo = nextval('Anagrafiche.Gen_IDGruppo');
end if;
return new;
end;
begin
if new.IDGruppoPg is NULL or new.IDGruppoPg = 0 then
new.IDGruppoPg = nextval('Anagrafiche.Gen_IDGruppoPg');
end if;
return new;
end;
declare
r1 RECORD;
r2 RECORD;
utente VARCHAR;
gruppopg VARCHAR;
begin
if new.IDGruppoPgUtente is NULL or new.IDGruppoPgUtente = 0 then
new.IDGruppoPgUtente = nextval('Anagrafiche.Gen_IDGruppoPgUtente');
end if;
for r1 in SELECT * FROM Anagrafiche.Utenti WHERE IDUtente = new.IDUtente loop
utente := r1.login;
end loop;
for r2 in SELECT * FROM Anagrafiche.GruppiPg WHERE IDGruppoPg = new.IDGruppoPg loop
gruppopg := r2.denominazione;
end loop;
execute 'GRANT ' || gruppopg || ' TO ' || utente;
return new;
end;
begin
if new.IDGruppoSoggetto is NULL or new.IDGruppoSoggetto = 0 then
new.IDGruppoSoggetto = nextval('Anagrafiche.Gen_IDGruppoSoggetto');
end if;
return new;
end;
begin
if new.IDIndirizzoSoggetto is NULL or new.IDIndirizzoSoggetto = 0 then
new.IDIndirizzoSoggetto = nextval('Anagrafiche.Gen_IDIndirizzoSoggetto');
end if;
return new;
end;
begin
if new.IDOperazione is NULL or new.IDOperazione = 0 then
new.IDOperazione = nextval('Anagrafiche.Gen_IDOperazione');
end if;
return new;
end;
begin
if new.IDOperazioneAzione is NULL or new.IDOperazioneAzione = 0 then
new.IDOperazioneAzione = nextval('Anagrafiche.Gen_IDOperazioneAzione');
end if;
return new;
end;
begin
if new.IDRelazioneSoggetto is NULL or new.IDRelazioneSoggetto = 0 then
new.IDRelazioneSoggetto = nextval('Anagrafiche.Gen_IDRelazioneSoggetto');
end if;
return new;
end;
begin
if new.IDRiferimentoSoggetto is NULL or new.IDRiferimentoSoggetto = 0 then
new.IDRiferimentoSoggetto = nextval('Anagrafiche.Gen_IDRiferimentoSoggetto');
end if;
return new;
end;
begin
if new.IDSoggetto is NULL or new.IDSoggetto = 0 then
new.IDSoggetto = nextval('Anagrafiche.Gen_IDSoggetto');
end if;
return new;
end;
begin
if new.IDTipologiaOperazione is NULL or new.IDTipologiaOperazione = 0 then
new.IDTipologiaOperazione = nextval('Anagrafiche.Gen_IDTipologiaOperazione');
end if;
return new;
end;
begin
if new.IDTitolo is NULL or new.IDTitolo = 0 then
new.IDTitolo = nextval('Anagrafiche.Gen_IDTitolo');
end if;
return new;
end;
begin
if new.IDTitoloDiStudio is NULL or new.IDTitoloDiStudio = 0 then
new.IDTitoloDiStudio = nextval('Anagrafiche.Gen_IDTitoloDiStudio');
end if;
return new;
end;
begin
if new.IDTitoloDiStudioSoggetto is NULL or new.IDTitoloDiStudioSoggetto = 0 then
new.IDTitoloDiStudioSoggetto = nextval('Anagrafiche.Gen_IDTitoloDiStudioSoggetto');
end if;
return new;
end;
begin
if new.IDUfficio is NULL or new.IDUfficio = 0 then
new.IDUfficio = nextval('Anagrafiche.Gen_IDUfficio');
end if;
return new;
end;
begin
if new.IDUtente is NULL or new.IDUtente = 0 then
new.IDUtente = nextval('Anagrafiche.Gen_IDUtente');
end if;
if not exists(SELECT * FROM pg_catalog.pg_user WHERE usename=new.Login) then
execute 'CREATE USER ' || quote_ident(new.Login);
end if;
if new.Password is not NULL then
execute 'ALTER USER ' || quote_ident(new.Login) ||
' WITH PASSWORD ''' || new.Password || '''';
end if;
new.Password = '******';
return new;
end;
begin
if new.IDUtenteUfficio is NULL or new.IDUtenteUfficio = 0 then
new.IDUtenteUfficio = nextval('Anagrafiche.Gen_IDUtenteUfficio');
end if;
return new;
end;
begin
if not exists (select * from anagrafiche.websoggetto where idsoggetto=new.idsoggetto) then
insert into anagrafiche.websoggetto (idsoggetto) values (new.idsoggetto);
end if;
return new;
end;
begin
if new.IDApplicazioneEsterna is NULL or new.IDApplicazioneEsterna = 0 then
new.IDApplicazioneEsterna = nextval('Generale.Gen_IDApplicazioneEsterna');
end if;
return new;
end;
begin
if new.IDCostante is NULL or new.IDCostante = 0 then
new.IDCostante = nextval('Generale.Gen_IDCostante');
end if;
return new;
end;
begin
if new.IDModelloooo is NULL or new.IDModelloooo = 0 then
new.IDModelloooo = nextval('Ooops.Gen_IDModelloooo');
end if;
return new;
end;
begin
if new.IDModelloooops is NULL or new.IDModelloooops = 0 then
new.IDModelloooops = nextval('Ooops.Gen_IDModelloooops');
end if;
return new;
end;
begin
if new.IDPasso is NULL or new.IDPasso = 0 then
new.IDPasso = nextval('Ooops.Gen_IDPasso');
end if;
return new;
end;
begin
if new.IDCapitoloUnitaOrganizzativa is NULL or new.IDCapitoloUnitaOrganizzativa = 0 then
new.IDCapitoloUnitaOrganizzativa = nextval('Personale.Gen_IDCapitoloUnitaOrganizzativa');
end if;
return new;
end;
begin
if new.IDInterruzione is NULL or new.IDInterruzione = 0 then
new.IDInterruzione = nextval('Personale.Gen_IDInterruzione');
end if;
return new;
end;
begin
if new.IDLivelloProgressione is NULL or new.IDLivelloProgressione = 0 then
new.IDLivelloProgressione = nextval('Personale.Gen_IDLivelloProgressione');
end if;
return new;
end;
begin
if new.IDProgressione is NULL or new.IDProgressione = 0 then
new.IDProgressione = nextval('Personale.Gen_IDProgressione');
end if;
return new;
end;
declare
aumento integer;
riporto integer;
r record;
begin
if new.IDStraordinario is NULL or new.IDStraordinario = 0 then
new.IDStraordinario = nextval('Personale.Gen_IDStraordinario');
end if;
if new.OreRecupero is NULL then
new.OreRecupero = 0;
end if;
if new.OrePagamento is NULL then
new.OrePagamento = 0;
end if;
new.Anno = date_part('year', new.DataInizio);
return new;
end;
begin
if new.IDUnitaOrganizzativa is NULL or new.IDUnitaOrganizzativa = 0 then
new.IDUnitaOrganizzativa = nextval('Personale.Gen_IDUnitaOrganizzativa');
end if;
if new.DataInizio is NULL then
new.DataInizio = 'now';
end if;
return new;
end;
begin
if new.IDUnitaOrganizzativaSoggetto is NULL or new.IDUnitaOrganizzativaSoggetto = 0 then
new.IDUnitaOrganizzativaSoggetto = nextval('Personale.Gen_IDUnitaOrganizzativaSoggetto');
end if;
return new;
end;
begin
-- La categoria A è unica
if new.idlivelloprogressione is NULL then
new.idlivelloprogressione = '-';
end if;
if new.idcategoriaprogressione = 'A' and not new.idlivelloprogressione = '-' then
raise exception 'Per la categoria A non può essere specificato un livello';
end if;
if new.posizioneorganizzativa = TRUE then
if new.idcategoriaprogressione = 'A' or
new.idcategoriaprogressione = 'B' or
(new.idcategoriaprogressione = 'B' and new.idlivelloprogressione = '-') or
(new.idcategoriaprogressione = 'B' and new.idlivelloprogressione = 'B') then
raise exception 'PO solo per C evoluto o superiore';
end if;
end if;
return new;
end;
begin
if old.DataFine is NULL and new.DataFine is not NULL then
if old.DataInizio > new.DataFine then
raise exception 'La data di fine validità non può antecedere quella di inizio validità';
end if;
if exists (select *
from personale.unitaorganizzativesoggetto uos
where uos.idunitaorganizzativa = new.idunitaorganizzativa
and uos.datafine is NULL) then
raise exception 'L\'unità organizzativa ha ancora almeno un dipendente attivo.';
end if;
if exists (select *
from personale.unitaorganizzative uo
where uo.iduopadre = new.idunitaorganizzativa
and uo.datafine is NULL) then
raise exception 'L\'unità organizzativa ha ancora almeno un altra unità figlia attiva.';
end if;
end if;
return new;
end;
begin
if new.Foglia = true then
-- Verifica che non vi siano delle sottofoglie
if new.CodiceSezione2 is null then
if exists (select * from Pratiche.TipologiePratica
where CodiceSezione1=new.CodiceSezione1
and CodiceSezione2 is not null) then
raise exception 'Presenti delle sottocodifiche di una foglia';
end if;
else
if new.CodiceSezione3 is null then
if exists (select * from Pratiche.TipologiePratica
where CodiceSezione1=new.CodiceSezione1
and CodiceSezione2=new.CodiceSezione2
and CodiceSezione3 is not null) then
raise exception 'Presenti delle sottocodifiche di una foglia';
end if;
end if;
end if;
else
-- Al terzo livello deve essere una foglia
if new.CodiceSezione3 is not null then
raise exception 'Un codice al terzo livello deve essere una foglia';
end if;
end if;
if new.CodiceSezione2 is not null and
exists (select * from Pratiche.TipologiePratica
where CodiceSezione1=new.CodiceSezione1
and CodiceSezione2 is null
and Foglia = true) then
raise exception 'Sottocodifica di una foglia';
end if;
if new.CodiceSezione3 is not null and
exists (select * from Pratiche.TipologiePratica
where CodiceSezione1=new.CodiceSezione1
and CodiceSezione2=new.CodiceSezione2
and CodiceSezione3 is null
and Foglia = true) then
raise exception 'Sottocodifica di una foglia';
end if;
return new;
end;
declare
anno INT;
adesso TIMESTAMP;
begin
if new.IDPKPratica is NULL or new.IDPKPratica = 0 then
new.IDPKPratica = nextval('Pratiche.Gen_IDPKPratica');
end if;
if new.Riservata is NULL then
new.Riservata = FALSE;
end if;
if new.Tipo is NULL then
new.Tipo = 'pratica';
end if;
if new.categoria is NULL or new.classe is NULL or new.fascicolo is NULL then
new.categoria = (select categoria from
pratiche.tipologiepratica where
idtipologiapratica = new.idtipologiapratica);
new.classe = (select classe from
pratiche.tipologiepratica where
idtipologiapratica = new.idtipologiapratica);
new.fascicolo = (select fascicolo from
pratiche.tipologiepratica where
idtipologiapratica = new.idtipologiapratica);
end if;
adesso := 'now';
anno := extract(year from adesso);
if not exists(SELECT * FROM pratiche.MaxIDPratica WHERE IDPratica like anno || '%') then
ALTER SEQUENCE Pratiche.Gen_NrPratica RESTART WITH 1;
end if;
new.IDPratica := '' || anno || substring(to_char(nextval('Pratiche.Gen_NrPratica'), '00000') from 2 for 5);
return new;
end;
begin
if new.IDTipologiaPratica is NULL or new.IDTipologiaPratica = 0 then
new.IDTipologiaPratica = nextval('Pratiche.Gen_IDTipologiaPratica');
end if;
return new;
end;
begin
if not exists (select *
from anagrafiche.utentiufficio uu
join anagrafiche.utenti as ut on ut.idutente = uu.idutente
where ut.login = current_user
and uu.modifica = true
and uu.idufficio = new.idufficio
and (old.riservata = false or uu.riservato = true)) then
raise exception 'Modifica pratica non permessa';
end if;
return new;
end;
begin
if not exists (select *
from anagrafiche.utentiufficio uu
join anagrafiche.utenti as ut on ut.idutente = uu.idutente
where ut.login = current_user
and uu.inserisce = true
and uu.idufficio = new.idufficio
and (new.riservata = false or uu.riservato = true)) then
raise exception 'Inserimento pratica non permesso';
end if;
return new;
end;
declare
iditer smallid_t;
idpratica largeid_t;
esito flag_t;
begin
esito = NULL;
if tg_op = 'INSERT' then
esito = new.Esito;
else
if old.Esito is NULL then
esito = new.Esito;
end if;
end if;
if esito is not NULL then
select IDPratica,IDIterProcedurale
into idpratica, iditer
from Procedimenti.FasiIterProcedurale
where IDFaseIterProcedurale = new.IDFaseIterProcedurale;
perform Procedimenti.EseguiOperazioniAzione(new.idazione, iditer, idpratica, esito);
end if;
return new;
end;
declare
iditer smallid_t;
idazione smallid_t;
esito flag_t;
begin
esito = NULL;
if tg_op = 'INSERT' then
esito = new.Esito;
else
if old.Esito is null then
esito = new.Esito;
end if;
end if;
if esito is not NULL then
select vip.IDIterProcedurale, vip.IDAzione
into iditer, idazione
from Procedimenti.VincoliIterProcedurale vip
where vip.IDVincoloIterProcedurale = new.IDVincoloIterProcedurale;
perform Procedimenti.EseguiOperazioniAzione(idazione, iditer, new.idpratica, esito);
end if;
return new;
end;
declare
execopz text;
p1 text;
p2 text;
idp text;
opz record;
begin
for opz in select o.codice, oa.parametro1, oa.parametro2
from Anagrafiche.OperazioniAzione oa
join Anagrafiche.Operazioni o on o.idoperazione = oa.idoperazione
where oa.idazione = $1
order by oa.progressivo, oa.idoperazione
loop
if opz.parametro1 is NULL then
p1 := 'NULL';
else
p1 := cast(opz.parametro1 as text);
end if;
if opz.parametro2 is NULL then
p2 := 'NULL';
else
p2 := cast(opz.parametro2 as text);
end if;
if $3 is NULL then
idp := 'NULL';
else
idp := $3;
end if;
execopz := 'select procedimenti.operazione_' || opz.codice ||
'(' ||
'cast(' || $1 || ' as smallid_t)' || ',' ||
'cast(' || quote_literal($4) || ' as flag_t)' || ',' ||
'cast(' || p1 || ' as integer_t)' || ',' ||
'cast(' || p2 || ' as integer_t)' || ',' ||
'cast(' || $2 || ' as smallid_t)' || ',' ||
'cast(' || idp || ' as stringid_t)' ||
')';
execute execopz;
end loop;
return;
end
begin
if new.IDAzioneIterProcedurale is NULL or new.IDAzioneIterProcedurale = 0 then
new.IDAzioneIterProcedurale = nextval('Procedimenti.Gen_IDAzioneIterProcedurale');
end if;
return new;
end;
begin
if new.IDEsitoVincoloPratica is NULL or new.IDEsitoVincoloPratica = 0 then
new.IDEsitoVincoloPratica = nextval('Procedimenti.Gen_IDEsitoVincoloPratica');
end if;
return new;
end;
begin
if new.IDFaseIterProcedurale is NULL or new.IDFaseIterProcedurale = 0 then
new.IDFaseIterProcedurale = nextval('Procedimenti.Gen_IDFaseIterProcedurale');
end if;
return new;
end;
begin
if new.IDIterProcedurale is NULL or new.IDIterProcedurale = 0 then
new.IDIterProcedurale = nextval('Procedimenti.Gen_IDIterProcedurale');
end if;
return new;
end;
begin
if new.IDNormaIterProcedurale is NULL or new.IDNormaIterProcedurale = 0 then
new.IDNormaIterProcedurale = nextval('Procedimenti.Gen_IDNormaIterProcedurale');
end if;
return new;
end;
begin
if new.IDUfficioIterProcedurale is NULL or new.IDUfficioIterProcedurale = 0 then
new.IDUfficioIterProcedurale = nextval('Procedimenti.Gen_IDUfficioIterProcedurale');
end if;
return new;
end;
begin
if new.IDUtenteIterProcedurale is NULL or new.IDUtenteIterProcedurale = 0 then
new.IDUtenteIterProcedurale = nextval('Procedimenti.Gen_IDUtenteIterProcedurale');
end if;
return new;
end;
begin
if new.IDVincoloIterProcedurale is NULL or new.IDVincoloIterProcedurale = 0 then
new.IDVincoloIterProcedurale = nextval('Procedimenti.Gen_IDVincoloIterProcedurale');
end if;
return new;
end;
declare
v_idfip largeid_t;
v_idfase smallid_t;
begin
if $2 = 'S' then
v_idfase = cast($4 as smallid_t);
v_idfip = null;
select fip.IDFaseIterProcedurale
into v_idfip
from Procedimenti.FasiIterProcedurale fip
where fip.IDIterProcedurale = $5
and fip.IDPratica = $6
and fip.IDFase = v_idfase;
if v_idfip is null then
v_idfip = nextval('Procedimenti.Gen_IDFaseIterProcedurale');
insert into Procedimenti.FasiIterProcedurale (IDFaseIterProcedurale, IDIterProcedurale, IDPratica, IDFase)
values (v_idfip, $5, $6, v_idfase);
end if;
insert into Procedimenti.AzioniIterProcedurale (IDFaseIterProcedurale, IDAzione)
values (v_idfip, $3);
end if;
return;
end
begin
raise notice 'NOOP(idazione=%,esito=%,parametro1=%,parametro2=%,iterprocedurale=%,idpratica=%)',
$1, $2, $3, $4, $5, $6;
end
begin
if not exists (select *
from anagrafiche.utentiufficio uu
join anagrafiche.utenti as ut on ut.idutente = uu.idutente
join pratiche.pratiche as pr on pr.idpratica = new.idpratica
where ut.login = current_user
and uu.modifica = true
and uu.idufficio = pr.idufficio
and (pr.riservata = false or uu.riservato = true)) then
raise exception 'Modifica esito pratica non permessa';
end if;
return new;
end;
begin
if not exists (select *
from anagrafiche.utentiufficio uu
join anagrafiche.utenti as ut on ut.idutente = uu.idutente
join procedimenti.ufficiiterprocedurale as uip on uip.iditerprocedurale = new.iditerprocedurale
where ut.login = current_user
and uu.modifica = true
and uu.idufficio = uip.idufficio
and uip.principale = true) then
raise exception 'Modifica fase iter procedurale non permessa';
end if;
return new;
end;
begin
if not exists (select *
from anagrafiche.utentiufficio uu
join anagrafiche.utenti as ut on ut.idutente = uu.idutente
join procedimenti.ufficiiterprocedurale as uip on uf.idufficio = uu.idufficio
where ut.login = current_user
and uu.incaricatoprocedimenti = true
and uu.idufficio = new.idufficio
and uip.iditerprocedurale = new.iditerprocedurale) then
raise exception 'Modifica utente non permesso';
end if;
if not exists (select *
from anagrafiche.utentiufficio uu
where uu.idutente = new.idutente
and uu.idufficio = new.idufficio) then
raise exception 'Utente non appartenente a ufficio specificato';
end if;
return new;
end;
begin
if not exists (select *
from anagrafiche.utentiufficio uu
join anagrafiche.utenti as ut on ut.idutente = uu.idutente
join procedimenti.ufficiiterprocedurale as uip on ip.iditerprocedurale = new.iditerprocedurale
where ut.login = current_user
and uu.modifica = true
and uu.idufficio = uip.idufficio
and uip.principale = true) then
raise exception 'Modifica vincolo iter procedurale non permessa';
end if;
return new;
end;
begin
if not exists (select *
from anagrafiche.utentiufficio uu
join anagrafiche.utenti as ut on ut.idutente = uu.idutente
join pratiche.pratiche as pr on pr.idpratica = old.idpratica
where ut.login = current_user
and uu.inserisce = true
and uu.idufficio = pr.idufficio
and (pr.riservata = false or uu.riservato = true)) then
raise exception 'Cancellazione esito pratica non permessa';
end if;
return old;
end;
begin
if not exists (select *
from anagrafiche.utentiufficio uu
join anagrafiche.utenti as ut on ut.idutente = uu.idutente
join procedimenti.ufficiiterprocedurale as uip on uip.iditerprocedurale = old.iditerprocedurale
where ut.login = current_user
and uu.inserisce = true
and uu.idufficio = uip.idufficio
and uip.principale = true) then
raise exception 'Cancellazione fase iter procedurale non permessa';
end if;
return old;
end;
begin
if not exists (select *
from anagrafiche.utentiufficio uu
join anagrafiche.utenti as ut on ut.idutente = uu.idutente
join procedimenti.ufficiiterprocedurale as uip on uip.idufficio = uu.idufficio
where ut.login = current_user
and uu.incaricatoprocedimenti = true
and uu.idufficio = old.idufficio
and uip.iditerprocedurale = old.iditerprocedurale) then
raise exception 'Cancellazione utente non permessa';
end if;
return old;
end;
begin
if not exists (select *
from anagrafiche.utentiufficio uu
join anagrafiche.utenti as ut on ut.idutente = uu.idutente
join procedimenti.ufficiiterprocedurale as uip on uip.iditerprocedurale = old.iditerprocedurale
where ut.login = current_user
and uu.inserisce = true
and uu.idufficio = uip.idufficio
and uip.principale = true) then
raise exception 'Cancellazione vincolo iter procedurale non permessa';
end if;
return old;
end;
begin
if not exists (select *
from anagrafiche.utentiufficio uu
join anagrafiche.utenti as ut on ut.idutente = uu.idutente
join pratiche.pratiche as pr on pr.idpratica = new.idpratica
where ut.login = current_user
and uu.inserisce = true
and uu.idufficio = pr.idufficio
and (pr.riservata = false or uu.riservato = true)) then
raise exception 'Inserimento esito pratica non permesso';
end if;
return new;
end;
begin
if not exists (select *
from anagrafiche.utentiufficio uu
join anagrafiche.utenti as ut on ut.idutente = uu.idutente
join procedimenti.ufficiiterprocedurale as uip on uip.iditerprocedurale = new.iditerprocedurale
where ut.login = current_user
and uu.inserisce = true
and uu.idufficio = uip.idufficio
and uip.principale = true) then
raise exception 'Inserimento fase iter procedurale non permesso';
end if;
return new;
end;
begin
-- Innanzitutto verifica che l'utente corrente sia autorizzato
-- ad aggiornare la tabella, cioè che sia l'incaricato ai
-- procedimenti per l'ufficio in questione
if not exists (select *
from anagrafiche.utentiufficio uu
join anagrafiche.utenti as ut on ut.idutente = uu.idutente
join procedimenti.ufficiiterprocedurale as uip on uip.idufficio = uu.idufficio
where ut.login = current_user
and uu.incaricatoprocedimenti = true
and uu.idufficio = new.idufficio
and uip.iditerprocedurale = new.iditerprocedurale) then
raise exception 'Inserimento utente non permesso';
end if;
-- Dopo di che, verifica che l'utente inserito appartenga
-- all'ufficio specificato
if not exists (select *
from anagrafiche.utentiufficio uu
where uu.idutente = new.idutente
and uu.idufficio = new.idufficio) then
raise exception 'Utente non appartenente a ufficio specificato';
end if;
return new;
end;
begin
if not exists (select *
from anagrafiche.utentiufficio uu
join anagrafiche.utenti as ut on ut.idutente = uu.idutente
join procedimenti.ufficiiterprocedurale as uip on uip.iditerprocedurale = new.iditerprocedurale
where ut.login = current_user
and uu.inserisce = true
and uu.idufficio = uip.idufficio
and uip.principale = true) then
raise exception 'Inserimento vincolo iter procedurale non permesso';
end if;
return new;
end;
begin
if new.principale = true then
if exists (select *
from procedimenti.ufficiiterprocedurale
where principale = true
and iditerprocedurale = new.iditerprocedurale
and idufficioiterprocedurale <> new.idufficioiterprocedurale) then
raise exception 'Vi deve essere solo un ufficio principale';
end if;
end if;
return new;
end;
begin
if new.iddocumento != old.iddocumento then
raise exception 'Impossibile modificare allegati di un protocollo annullato o convalidato';
end if;
return new;
end;
begin
if new.iddocumento != old.iddocumento then
raise exception 'Impossibile rimuovere l''ultima pratica in cui è inserito il protocollo';
end if;
return old;
end;
begin
if new.iddocumento != old.iddocumento then
raise exception 'Impossibile modificare uffici referenti di un protocollo annullato o convalidato';
end if;
return new;
end;
begin
if new.iddocumento != old.iddocumento then
raise exception 'Impossibile modificare uffici di un protocollo annullato o convalidato';
end if;
return new;
end;
begin
if check_protocolloconvalidato(old.IDDocumento) or check_protocolloannullato(old.IDDocumento) then
raise exception 'Impossibile cancellare allegati di un protocollo annullato o convalidato';
end if;
return old;
end;
begin
if not exists (SELECT * FROM protocollo.praticheprotocollo WHERE iddocumento = old.iddocumento
AND idpratica <> old.idpratica) then
raise exception 'Impossibile rimuovere l''ultima pratica in cui è inserito il protocollo';
end if;
return old;
end;
begin
if check_protocolloconvalidato(old.IDDocumento) or check_protocolloannullato(old.IDDocumento) then
raise exception 'Impossibile cancellare uffici referenti di un protocollo annullato o convalidato';
end if;
return old;
end;
begin
if check_protocolloconvalidato(old.IDDocumento) or check_protocolloannullato(old.IDDocumento) then
raise exception 'Impossibile cancellare soggetti di un protocollo annullato o convalidato';
end if;
if old.PrimoInserimento = TRUE then
UPDATE Protocollo.SoggettiProtocolloUtente SET Annullato = TRUE
WHERE IDSoggettoProtocollo = old.IDSoggettoProtocollo;
return NULL;
end if;
return old;
end;
begin
if check_protocolloconvalidato(old.IDDocumento) or check_protocolloannullato(old.IDDocumento) then
raise exception 'Impossibile cancellare uffici di un protocollo annullato o convalidato';
end if;
return old;
end;
begin
if check_protocolloconvalidato(new.IDDocumento) or check_protocolloannullato(new.IDDocumento) then
raise exception 'Impossibile aggiungere allegati ad un protocollo annullato o convalidato';
end if;
if new.IDAllegatoProtocollo is NULL or new.IDAllegatoProtocollo = 0 then
new.IDAllegatoProtocollo = nextval('Protocollo.Gen_IDAllegatoProtocollo');
end if;
return new;
end;
begin
if new.IDAnnullato is NULL or new.IDAnnullato = 0 then
new.IDAnnullato = nextval('Protocollo.Gen_IDAnnullato');
new.IDEsecutoreRichiesta = (select au.idutente from
anagrafiche.utenti au
where au.login = current_user);
new.DataRichiesta = 'now';
end if;
if exists (select *
from anagrafiche.utenti ut
join anagrafiche.utentiufficio as uu
on uu.idutente = ut.idutente
join anagrafiche.uffici as uf
on uf.idufficio = uu.idufficio
where ut.login = current_user
and uf.ufficioprotocollo = TRUE) then
-- Annullamento autorizzato
new.IDEsecutoreAutorizzazione = (select au.idutente from
anagrafiche.utenti au
where au.login = current_user);
new.DataAutorizzazione = 'now';
end if;
return new;
end;
begin
if new.IDMotivazioneAnnullamento is NULL or new.IDMotivazioneAnnullamento = 0 then
new.IDMotivazioneAnnullamento = nextval('Protocollo.Gen_IDMotivazioneAnnullamento');
end if;
return new;
end;
begin
if new.IDOggetto is NULL or new.IDOggetto = 0 then
new.IDOggetto = nextval('Protocollo.Gen_IDOggetto');
end if;
return new;
end;
begin
if new.IDPraticaProtocollo is NULL or new.IDPraticaProtocollo = 0 then
new.IDPraticaProtocollo = nextval('Protocollo.Gen_IDPraticaProtocollo');
end if;
return new;
end;
begin
if new.IDProtocollo is NULL or new.IDProtocollo = 0 then
new.IDProtocollo = nextval('Protocollo.Gen_IDProtocollo');
end if;
new.DataProtocollo := 'now';
new.Anno := extract(year from new.DataProtocollo);
if not exists(SELECT * FROM protocollo.MaxIDDocumento WHERE IDDocumento like new.Anno || '%') then
ALTER SEQUENCE Protocollo.Gen_NrProtocollo RESTART WITH 1;
end if;
new.IDDocumento := '' || new.Anno || substring(to_char(nextval('Protocollo.Gen_NrProtocollo'), '00000000') from 2 for 8);
new.ConvalidaParziale := FALSE;
new.Convalida := FALSE;
-- utente nello sportello
if not check_utenteinufficio(current_user, new.idufficio) then
raise exception 'Inserimento protocollo non permesso';
end if;
-- protocollo dichiarato spedito
if new.Spedito then
if new.IDTipologiaProtocollo = 'U' then
if not check_utenteflaginufficio(current_user, 'consolida', new.idufficio)
and not check_utenteflaginreferente(current_user, 'consolida', new.iddocumento, TRUE) then
raise exception 'Non hai i permessi per dichiarare il protocollo come spedito';
end if;
else
raise exception 'Solo i protocolli in uscita possono essere marcati come spediti';
end if;
end if;
return new;
end;
begin
if check_protocolloconvalidato(new.IDDocumento) or check_protocolloannullato(new.IDDocumento) then
raise exception 'Impossibile aggiungere uffici referenti ad un protocollo annullato o convalidato';
end if;
if exists (select * from protocollo.protocolliutente p where
p.iddocumento = new.iddocumento and
p.convalidaparziale = TRUE) then
raise exception 'Inserimento non permesso: protocollo convalidato parzialmente';
end if;
if new.IDReferenteProtocollo is NULL or new.IDReferenteProtocollo = 0 then
new.IDReferenteProtocollo = nextval('Protocollo.Gen_IDReferenteProtocollo');
end if;
if new.letto is not TRUE then
new.letto = FALSE;
end if;
return new;
end;
begin
if new.IDRiferimentoProtocollo is NULL or new.IDRiferimentoProtocollo = 0 then
new.IDRiferimentoProtocollo = nextval('Protocollo.Gen_IDRiferimentoProtocollo');
end if;
return new;
end;
begin
if check_protocolloconvalidato(new.IDDocumento) or check_protocolloannullato(new.IDDocumento) then
raise exception 'Impossibile aggiungere soggetti ad un protocollo annullato o convalidato';
end if;
if new.IDSoggettoProtocollo is NULL or new.IDSoggettoProtocollo = 0 then
new.IDSoggettoProtocollo = nextval('Protocollo.Gen_IDSoggettoProtocollo');
end if;
new.Riservato = FALSE;
if exists(SELECT * FROM protocollo.protocolliutente WHERE
IDDocumento = new.IDDocumento AND
PrimoInserimentoCompleto = TRUE) then
new.PrimoInserimento = FALSE;
else
new.PrimoInserimento = TRUE;
end if;
if new.InViaPrincipale = TRUE then
UPDATE Protocollo.SoggettiProtocollo SET InViaPrincipale = FALSE WHERE IDDocumento = new.IDDocumento;
end if;
return new;
end;
begin
if check_protocolloconvalidato(new.IDDocumento) or check_protocolloannullato(new.IDDocumento) then
raise exception 'Impossibile aggiungere uffici ad un protocollo annullato o convalidato';
end if;
if new.IDUfficioProtocollo is NULL or new.IDUfficioProtocollo = 0 then
new.IDUfficioProtocollo = nextval('Protocollo.Gen_IDUfficioProtocollo');
end if;
return new;
end;
begin
if new.iddocumento = old.iddocumento then
return new;
else
if exists(SELECT * FROM protocollo.protocolli WHERE
IDDocumento = new.IDDocumento AND
PrimoInserimentoCompleto = TRUE) then
new.PrimoInserimento = FALSE;
else
new.PrimoInserimento = TRUE;
end if;
end if;
return new;
end;
declare
iddocumento alias FOR $1;
iddocs alias FOR $2;
n alias FOR $3;
newiddocs varchar[];
i INT;
r RECORD;
begin
i := n;
newiddocs := iddocs;
for r in SELECT * FROM Protocollo.RiferimentiProtocollo rp
WHERE rp.IDRiferimento = iddocumento loop
i := i + 1;
newiddocs[i] := r.IDDocumento;
newiddocs := Protocollo.ricerca_child(r.IDDocumento, newiddocs, i);
end loop;
return newiddocs;
end;
declare
iddocumento alias FOR $1;
iddocs alias FOR $2;
n alias FOR $3;
newiddocs varchar[];
i INT;
r RECORD;
begin
i := n;
newiddocs := iddocs;
for r in SELECT * FROM Protocollo.RiferimentiProtocollo rp
WHERE rp.IDDocumento = iddocumento loop
i := i + 1;
newiddocs[i] := r.IDRiferimento;
newiddocs := Protocollo.ricerca_parent(r.IDRiferimento, newiddocs, i);
end loop;
return newiddocs;
end;
declare
iddocumento alias FOR $1;
iddocs VARCHAR[];
d VARCHAR;
r RECORD;
i INT;
begin
iddocs[1] := iddocumento;
iddocs := Protocollo.ricerca_parent(iddocumento, iddocs, 1);
i := 0;
while true loop
i := i+1;
d := iddocs[i];
if (d is null) then
exit;
end if;
SELECT * INTO r FROM Protocollo.ProtocolliUtente p
WHERE p.IDDocumento = d;
return next r;
end loop;
return;
end;
declare
iddocumento alias FOR $1;
iddocs VARCHAR[];
d VARCHAR;
r RECORD;
i INT;
begin
iddocs[1] := iddocumento;
iddocs := Protocollo.ricerca_child(iddocumento, iddocs, 1);
i := 0;
while true loop
i := i+1;
d := iddocs[i];
if (d is null) then
exit;
end if;
SELECT * INTO r FROM Protocollo.ProtocolliUtente p
WHERE p.IDDocumento = d;
return next r;
end loop;
return;
end;
begin
-- Il protocollo è già stato annullato
if old.annullato = TRUE then
raise exception 'Modifica del protocollo annullato non permessa';
end if;
-- Il protocollo è già convalidato
if old.convalida = TRUE then
raise exception 'Modifica del protocollo convalidato non permessa';
end if;
-- Non sei nello sportello o in un referente
if not (check_utenteinufficio(current_user, new.idufficio)
or check_utenteinreferente(current_user, new.iddocumento)) then
raise exception 'Modifica protocollo non permessa';
end if;
if new.annullato = TRUE then
if exists (select *
from anagrafiche.utenti ut
join anagrafiche.utentiufficio as uu
on uu.idutente = ut.idutente
join anagrafiche.uffici as uf
on uf.idufficio = uu.idufficio
where ut.login = current_user
and uf.ufficioprotocollo = TRUE) then
-- Annullamento del protocollo permesso
-- Scollego le pratiche e i riferimenti
delete from protocollo.praticheprotocollo where
iddocumento = old.iddocumento;
delete from protocollo.riferimentiprotocollo where
iddocumento = old.iddocumento;
-- Inserisco nella pratica dei protocolli annullati
insert into protocollo.praticheprotocollo (iddocumento, idpratica)
values(new.iddocumento, cast((select valore
from generale.costanti
where nome = 'PRA_PROTO_ANN')
as largeid_t
));
-- Inserisco nella tabella delle richieste di annullamento
insert into protocollo.annullati (iddocumento)
values (new.iddocumento);
return new;
elsif check_utenteinufficio(current_user, new.idufficio) then
-- Richiesta di annullamento
insert into protocollo.annullati (iddocumento)
values (new.iddocumento);
new.Annullato = FALSE;
else
raise exception 'Annullamento del protocollo non permesso';
end if;
end if;
-- Convalide
if old.convalida = FALSE and new.convalida = TRUE then
if (check_utenteflaginufficio(current_user, 'consolida', new.idufficio)
or check_utenteflaginreferente(current_user, 'consolida', new.iddocumento, TRUE)
or check_utenteingruppo(current_user, 'convalidatore')) then
if exists (SELECT * FROM protocollo.praticheprotocollo WHERE iddocumento=new.iddocumento) then
new.dataconvalida := 'now';
new.esecutoreconvalida := current_user;
if not exists (SELECT * FROM protocollo.MaxNrConvalida WHERE Anno = old.Anno) then
ALTER SEQUENCE Protocollo.Gen_NrConvalida RESTART WITH 1;
end if;
new.nrconvalida := '' || old.Anno || substring(to_char(nextval('Protocollo.Gen_NrConvalida'), '00000000') from 2 for 8);
else
raise exception 'Il protocollo deve essere inserito in una pratica per poter essere convalidato';
end if;
else
raise exception 'Non hai i permessi per convalidare il documento';
end if;
end if;
if old.convalidaparziale = FALSE and (new.convalidaparziale = TRUE or
new.convalida = TRUE) then
if check_utenteflaginufficio(current_user, 'consolida', new.idufficio)
or check_utenteflaginreferente(current_user, 'consolida', new.iddocumento, TRUE)
or check_utenteingruppo(current_user, 'convalidatore') then
new.convalidaparziale := TRUE;
new.dataconvalidaparziale := 'now';
new.esecutoreconvalidaparziale := current_user;
else
raise exception 'Non hai i permessi per convalidare gli uffici';
end if;
end if;
-- riservatezza
if old.riservato = TRUE and new.riservato = FALSE then
if not check_utenteingruppo(current_user, 'superutente') then
raise exception 'Protocollo dichiarato riservato';
end if;
end if;
-- protocollo dichiarato spedito
if old.Spedito = FALSE and new.Spedito = TRUE then
if new.IDTipologiaProtocollo = 'U' then
if not check_utenteflaginufficio(current_user, 'consolida', new.idufficio)
and not check_utenteflaginreferente(current_user, 'consolida', new.iddocumento, TRUE) then
raise exception 'Non hai i permessi per dichiarare il protocollo come spedito';
end if;
else
raise exception 'Solo i protocolli in uscita possono essere marcati come spediti';
end if;
end if;
return new;
end;
begin
if old.letto = TRUE and new.letto = FALSE then
raise exception 'Protocollo dato per letto precedentemente';
end if;
if old.letto = FALSE and new.letto = TRUE then
if exists (select *
from anagrafiche.utentiufficio uu
join anagrafiche.utenti as ut on ut.idutente = uu.idutente
where ut.login = current_user
and uu.idufficio = new.idufficio
and uu.daiperletto = TRUE) then
new.utenteletto := current_user;
new.dataletto := 'now';
else
raise exception 'Non puoi dare per letto il protocollo';
end if;
end if;
if new.progressivo != old.progressivo then
if exists (select * from protocollo.protocolliutente p where
p.iddocumento = new.iddocumento and
p.convalidaparziale = TRUE) then
raise exception 'Modifica non permessa: protocollo convalidato parzialmente';
end if;
end if;
return new;
end;
begin
if new.iddocumento != old.iddocumento then
raise exception 'Impossibile modificare soggetti di un protocollo annullato o convalidato';
end if;
if old.riservato = FALSE and new.riservato = TRUE then
if not exists (select * from protocollo.protocolliutente where
iddocumento = new.iddocumento and
riservato = TRUE) then
raise exception 'Impossibile allegare soggetti riservati ad un protocollo non riservato';
end if;
end if;
if new.InViaPrincipale = TRUE and old.InViaPrincipale != TRUE then
UPDATE Protocollo.SoggettiProtocollo SET InViaPrincipale = FALSE WHERE IDDocumento = new.IDDocumento;
end if;
return new;
end;
begin
if exists (select * from protocollo.protocolli p where
p.iddocumento = old.iddocumento and
p.convalidaparziale = TRUE) then
raise exception 'Cancellazione non permessa: protocollo convalidato parzialmente';
end if;
return old;
end;
standard public schema
begin new.rec_modificato := 'now'; new.rec_modificato_da := current_user; return new; end;
declare
iddocumento alias FOR $1;
begin
if exists (select * from protocollo.protocolliutente p
where p.iddocumento = iddocumento
and p.annullato = TRUE) then
return TRUE;
else
return FALSE;
end if;
end;
declare
iddocumento alias FOR $1;
begin
if exists (select * from protocollo.protocolliutente p
where p.iddocumento = iddocumento
and p.convalida = TRUE) then
return TRUE;
else
return FALSE;
end if;
end;
declare
sqlstring varchar;
username alias FOR $1;
flag alias FOR $2;
iddocumento alias FOR $3;
principale alias FOR $4;
r record;
begin
sqlstring := 'select count(*) as ok from protocollo.referentiprotocollo rp'
|| ' join anagrafiche.utentiufficio as uu on rp.idufficio = uu.idufficio'
|| ' join anagrafiche.utenti as ut on ut.idutente = uu.idutente'
|| ' where rp.iddocumento = ''' || iddocumento || '''';
if principale = TRUE then
sqlstring := sqlstring || ' and rp.progressivo = 1';
end if;
sqlstring := sqlstring || ' and uu.' || flag || ' = TRUE'
|| ' and ut.login = ''' || username || '''';
for r in execute sqlstring loop
if r.ok > 0 then
return TRUE;
else
return FALSE;
end if;
end loop;
return FALSE;
end;
declare
username alias FOR $1;
flag alias FOR $2;
idufficio alias FOR $3;
r record;
begin
for r in execute 'select count(*) as ok from anagrafiche.utentiufficio uu'
|| ' join anagrafiche.utenti as ut on ut.idutente = uu.idutente'
|| ' where ut.login = ''' || username || ''''
|| ' and uu.' || flag || ' = TRUE'
|| ' and uu.idufficio = ' || idufficio loop
if r.ok > 0 then
return TRUE;
else
return FALSE;
end if;
end loop;
return FALSE;
end;
declare
username alias FOR $1;
group alias FOR $2;
begin
if exists (select * from pg_roles where rolname = username and
group = ANY(ARRAY(select groname from pg_group where oid = ANY(grolist)))) then
return TRUE;
else
return FALSE;
end if;
end;
declare
username alias FOR $1;
iddocumento alias FOR $2;
r record;
begin
for r in execute 'select count(*) as ok from protocollo.referentiprotocollo rp'
|| ' join anagrafiche.utentiufficio as uu on rp.idufficio = uu.idufficio'
|| ' join anagrafiche.utenti as ut on ut.idutente = uu.idutente'
|| ' where rp.iddocumento = ''' || iddocumento || ''''
|| ' and ut.login = ''' || username || '''' loop
if r.ok > 0 then
return TRUE;
else
return FALSE;
end if;
end loop;
return FALSE;
end;
declare
username alias FOR $1;
idufficio alias FOR $2;
begin
if exists (select * from anagrafiche.utentiufficio uu
join anagrafiche.utenti as ut on ut.idutente = uu.idutente
where ut.login = username
and uu.idufficio = idufficio limit 1) then
return TRUE;
else
return FALSE;
end if;
end;
begin
if new.rec_creato is NULL then
new.rec_creato := 'now';
end if;
if new.rec_creato_da is NULL then
new.rec_creato_da := current_user;
end if;
return new;
end;
begin
if new.IDInterfacciaTabella is NULL or new.IDInterfacciaTabella = 0 then
new.IDInterfacciaTabella = nextval('PyPaPi.Gen_IDInterfacciaTabella');
end if;
return new;
end;
begin
if new.IDIntervento is NULL or new.IDIntervento = 0 then
new.IDIntervento = nextval('Tecnico.Gen_IDIntervento');
end if;
return new;
end;
begin
if new.IDParticella is NULL or new.IDParticella = 0 then
new.IDParticella = nextval('Tecnico.Gen_IDParticella');
end if;
return new;
end;
begin
if new.IDParticellaPratica is NULL or new.IDParticellaPratica = 0 then
new.IDParticellaPratica = nextval('Tecnico.Gen_IDParticellaPratica');
end if;
return new;
end;
begin
if new.IDVia is NULL or new.IDVia = 0 then
new.IDVia = nextval('Tecnico.Gen_IDVia');
end if;
return new;
end;
Generated by PostgreSQL Autodoc