# EVENTI_RICALCOLA_ACCONTI ## Codice Sorgente ```sql procedure EVENTI_RICALCOLA_ACCONTI(p_event_id number) as v_cnt number; v_calc_only_saldo number := 0; v_totale_tipi number; v_totale_degus number; v_totale_ris number; v_totale_ospiti number; v_totale_evento number; v_totale_altricosti number; v_primo_acconto number := 0; v_secondo_acconto number := 0; v_terzo_acconto number := 0; v_prima_perc number := 0.3; v_seconda_perc number := 0.5; v_terza_perc number := 0.2; begin select count(*) into v_cnt from eventi_acconti where id_evento = p_event_id and "DATA" is not null; /* if v_cnt > 0 then raise_application_error(-20001, 'Impossibile ricalcolare gli acconti per un evento già saldato o parzialmente saldato'); end if; */ select count(*) into v_cnt from eventi_acconti where id_evento = p_event_id and (ORDINE = 10 OR ORDINE = 20) -- primo acconto (o anche secondo) dato quindi evento confermato and "DATA" is not null; if v_cnt > 0 then v_calc_only_saldo := 1; end if; select sum(costo_ivato) into v_totale_tipi from get_costo_tipi_evt where id_evento = p_event_id; select sum(costo) into v_totale_degus from get_costo_degus_evt where id_evento = p_event_id; select sum(costo) into v_totale_ris from get_costo_ris_evt where id_evento = p_event_id; select sum(costo+costo*0.10) into v_totale_ospiti from get_costo_ospiti_evt where id_evento = p_event_id; select sum((costo * quantity)+(case when costo > 0 then costo * quantity * 0.10 else 0 end)) into v_totale_altricosti from eventi_altricosti where id_evento = p_event_id; v_totale_evento := nvl(v_totale_tipi, 0) - nvl(v_totale_degus, 0) + nvl(v_totale_ris, 0) + nvl(v_totale_ospiti, 0) + nvl(v_totale_altricosti, 0); if v_calc_only_saldo = 0 then -- Se nessun acconto è stato pagato allora ricalcola tutti gli acconti delete from eventi_acconti where id_evento = p_event_id and ordine in (10, 20, 30); insert into eventi_acconti (DESCRIZIONE, ACCONTO, ID_EVENTO, A_CONFERMA, ORDINE) values ('PRIMA CAPARRA (art.7 punto A del contratto) a conferma evento nella cifra di euro:', v_totale_evento * v_prima_perc, p_event_id, 1, 10); insert into eventi_acconti (DESCRIZIONE, ACCONTO, ID_EVENTO, A_CONFERMA, ORDINE) values ('SECONDA CAPARRA (art. 7 punto B - circa 60 giorni prima dell''evento) nella cifra di euro:', v_totale_evento * v_seconda_perc, p_event_id, 0, 20); insert into eventi_acconti (DESCRIZIONE, ACCONTO, ID_EVENTO, A_CONFERMA, ORDINE) values ('SALDO A RICEVIMENTO CONSUNTIVO (art.7 punto c del contratto) 5 giorni prima dell''evento', v_totale_evento * v_terza_perc, p_event_id, 0, 30); else -- Controllo se gli acconti sono stato pagati e in caso ricalcolo soltanto i saldi begin select acconto into v_primo_acconto from eventi_acconti where id_evento = p_event_id and ORDINE = 10 and data is not null; -- Il primo acconto è stato pagato exception when no_data_found then v_primo_acconto := 0; end; begin select acconto into v_secondo_acconto from eventi_acconti where id_evento = p_event_id and ORDINE = 20 and data is not null; -- Il secondo acconto è stato pagato exception when no_data_found then v_secondo_acconto := 0; end; begin select acconto into v_terzo_acconto from eventi_acconti where id_evento = p_event_id and ORDINE = 30 and data is not null; -- Il terzo acconto è stato pagato exception when no_data_found then v_terzo_acconto := 0; end; -- Se non hanno pagato il primo acconto lo calcolo in automatico if v_primo_acconto = 0 then v_primo_acconto := v_totale_evento * v_prima_perc; end if; -- Ricalcolo la percentuale del secondo acconto in base al primo se non è stato pagato if v_secondo_acconto = 0 then v_secondo_acconto := (v_totale_evento - v_primo_acconto) * (v_seconda_perc/(v_seconda_perc + v_terza_perc)); end if; -- Calcolo il terzo acconto come la rimanenza tra il totale e il primo + secondo se non è stato pagato if v_terzo_acconto = 0 then v_terzo_acconto := v_totale_evento - (v_primo_acconto + v_secondo_acconto); end if; -- Controllo se i totali acconti superano il totale dell'evento if v_primo_acconto > v_totale_evento then raise_application_error(-20001, 'Attenzione! Il primo acconto supera il costo totale del''evento'); end if; if v_primo_acconto + v_secondo_acconto > v_totale_evento then raise_application_error(-20001, 'Attenzione! Il primo e il secondo acconto superano il costo totale del''evento'); end if; if v_primo_acconto + v_secondo_acconto + v_terzo_acconto > v_totale_evento then raise_application_error(-20001, 'Attenzione! Gli acconti superano il costo totale del''evento'); end if; -- Se gli acconti successivi sono validi allora li aggiorno, se l'acconto precendente salda tutto li elimino if v_secondo_acconto > 0 then update eventi_acconti set ACCONTO = v_secondo_acconto where id_evento = p_event_id and ordine = 20; if SQL%ROWCOUNT = 0 then insert into eventi_acconti (DESCRIZIONE, ACCONTO, ID_EVENTO, A_CONFERMA, ORDINE) values ('SECONDA CAPARRA (art. 7 punto B - circa 60 giorni prima dell''evento) nella cifra di euro:', v_secondo_acconto, p_event_id, 0, 20); end if; else delete from eventi_acconti where id_evento = p_event_id and ordine = 20; end if; if v_terzo_acconto > 0 then update eventi_acconti set ACCONTO = v_terzo_acconto where id_evento = p_event_id and ordine = 30; if SQL%ROWCOUNT = 0 then insert into eventi_acconti (DESCRIZIONE, ACCONTO, ID_EVENTO, A_CONFERMA, ORDINE) values ('SALDO A RICEVIMENTO CONSUNTIVO (art.7 punto c del contratto) ', v_terzo_acconto, p_event_id, 0, 30); end if; else delete from eventi_acconti where id_evento = p_event_id and ordine = 30; end if; end if; end;```