PROMPT
PROMPT ****************************************
PROMPT ***** Vkladani do penezniho deniku *****
PROMPT ****************************************
PROMPT
PROMPT *********** Vypis kodu ***********
Select ID Cislo_Kodu,Text Kod from kod;

declare
 cv messages.numcol2%type;
 id rowid;

 vloz_dkp exception;
 Pragma Exception_init (vloz_dkp,-20300);
 vloz_rezie exception;
 Pragma Exception_init (vloz_rezie,-20301);

 datum_den exception;
 Pragma Exception_init (datum_den,-1847);
 datum_den2 exception;
 Pragma Exception_init (datum_den,-1839);
 datum_mes exception;
 Pragma Exception_init (datum_mes,-1843);
 datum_format exception;
 Pragma Exception_init (datum_format,-1858);
 castka_VELKA exception;
 Pragma Exception_init (castka_VELKA,-1438);
 err_dph exception;
 Pragma Exception_init (err_dph,-4001);
 err_platba exception;
 Pragma Exception_init (err_platba,-4002);
 err_vp exception;
 Pragma Exception_init (err_vp,-4003);
 err_typ exception;
 Pragma Exception_init (err_typ,-4004);

begin
INSERT INTO DENIK(RADEKD,Datum,Typ,doklad,castka,vp,platba,poplatek,dph)
       VALUES(seq_denik.nextval,TO_DATE(decode(translate('&&Datum','0123456789','xxxxxxxxxxx'),'xx.xx.xxxx','&datum','spatne zadane datum'),'dd.mm.yyyy')
  ,'&Typ_dokladu_FAK_ZFA_PAR_VYP',&Cislo_dokladu,&Castka,'&Vydej_Prijem','&Platba_Hotove_Uctem_Vymenou',&Poplatek,&Sazba_DPH);
Update DENIK set KOD=&&cislo_kodu where RadekD=cv_denik;
Select ROWID Into id From kod Where ID = &cislo_kodu;
Update denik set FK_KOD=&cislo_kodu where RadekD=cv_denik;
EXCEPTION
  When No_Data_Found Then 
    Raise_Application_Error(-20399, 'Chybne cislo kodu.');
  WHEN vloz_dkp THEN
    INSERT INTO DKP(RADEKDKP,NAZEVDKP,FKR_DENIK)
           VALUES(seq_dkp.nextval,'&Nazev_DKP',cv_denik);
    cv:=cv_dkp;
    INSERT INTO POLDKP(ID,Jmeno,PocetJ,FK_JEDN,castka,dph,zustatek,zmena,FK_DKP)
           VALUES(seq_poldkp.nextval,'&jmeno_dkp_polozky',&&pocet_jednotek,&cislo_jednotky,&castka,&sazba_dph_polozky,&pocet_jednotek,To_date('&datum','dd.mm.yyyy'),cv);
    Update DENIK set FK_DKP=cv where RadekD=cv_denik;
    Update denik set FK_KOD=&cislo_kodu where RadekD=cv_denik;

  WHEN vloz_rezie THEN
    INSERT INTO REZIE(RADEKR,NAZEVR,FKR_DENIK)
           VALUES(seq_rezie.nextval,'&Nazev_Rezie',cv_denik);
    cv:=cv_rezie;
    INSERT INTO POLREZIE(ID,JMENO,castka,DPH,FK_REZIE)
           VALUES(seq_polrezie.nextval,'&jmeno_polozky_rezie',&castka,&sazba_dph_polozky,cv);
    Update DENIK set FK_REZIE=cv where RadekD=cv_denik;
    Update denik set FK_KOD=&cislo_kodu where RadekD=cv_denik;
  when datum_den or datum_den2 Then
    raise_application_error(-20399,'Spatne zadan den v datumu.');
  when datum_mes Then
    raise_application_error(-20399,'Spatne zadan mesic v datumu.');
  when datum_format Then
    raise_application_error(-20399,'Spatne zadan format datumu. XX.XX.XXXX');
  when castka_VELKA Then
    raise_application_error(-20399,'Zadana castka je prilis velka. (10,2)');
  when err_dph Then
    raise_application_error(-20399,'Spatne zadana sazba DPH.');
  when err_platba Then
    raise_application_error(-20399,'Spatne zadana platba.');
  when err_vp Then
    raise_application_error(-20399,'Spatne zadano vydej/prijem.');
  when err_typ Then
    raise_application_error(-20399,'Spatne zadan typ dokladu.');
end;
/
undefine datum
undefine cislo_kodu
undefine pocet_jednotek
commit;


