-- Start of DDL Script for Procedure NOVA.PR_CREATE_EXPORT_IC
-- Generated 16-mars-2022 16:02:22 from NOVA@smart02

CREATE OR REPLACE 
PROCEDURE pr_create_export_ic  (
      idu_p        VARCHAR2,
      prg_p        VARCHAR2,
      dat_p        NUMBER,
      hre_p        NUMBER,
      pas_p        NUMBER
      ) is 
      
      begin
      declare
      
      cursor c_lstp(cod_p varchar2) is
      select *  from  slstp
      where  lstp_dat= dat_p
      and lstp_hre=hre_p
      and lstp_prg= prg_p
      and lstp_idu= idu_p
      and lstp_typ=cod_p
      ;
      r_lstp  c_lstp%rowtype;
      
      cursor c_zjc2 is
     SELECT ssta_lib,srul_lib,"ZJCP"."ZJCP_TST", "ZJCP"."ZJCP_STA", "ZJCP"."ZJCP_LET", "ZJCP"."ZJCP_NUM", "ZJCP"."ZJCP_CRE", "ZJCP"."ZJCP_DEB", sum("ZJCP"."ZJCP_RES") as MNT,
  "ZJCP"."ZJCP_MON", "ZJCP"."ZJCP_MONLIB"
 FROM   "NOVA"."ZJCP" "ZJCP",srul,ssta
 where  srul_let(+)="ZJCP"."ZJCP_LET" and srul_num(+)="ZJCP"."ZJCP_NUM" and srul_pas(+)=pas_p and srul_lan(+)='F' and ssta_pas(+)= pas_p and ssta_tst(+)= "ZJCP"."ZJCP_TST" and ssta_num(+)= "ZJCP"."ZJCP_STA"
group by ssta_lib,srul_lib, zjcp_tst,zjcp_sta,zjcp_let,zjcp_num,zjcp_cre,zjcp_deb,  "ZJCP"."ZJCP_MON", "ZJCP"."ZJCP_MONLIB"
 ORDER BY "ZJCP"."ZJCP_MON", "ZJCP"."ZJCP_STA", "ZJCP"."ZJCP_LET", "ZJCP"."ZJCP_NUM", "ZJCP"."ZJCP_DEB", "ZJCP"."ZJCP_CRE"
;



r_zjc2  c_zjc2%rowtype;
p_sficher varchar2(255);
v_DATA1  varchar2(4000);
ligne number;

v_periode  varchar2(2); 
v_annee  varchar2(4);  
v_date date;   
      begin
      
-------
open c_lstp('PER_DE');
fetch c_lstp into r_lstp;
close c_lstp;

v_periode:=r_lstp.lstp_par;


open c_lstp('ANN_DE');
fetch c_lstp into r_lstp;
close c_lstp;

v_annee:=r_lstp.lstp_par;

v_date:= ADD_MONTHS(to_date(v_annee||lpad(v_periode,'2','0')||'01','yyyyMMdd'),1)-1;
 -------entete
 ligne:=1;
 p_sficher:= 'Export_interface_comptable'||'_'|| dat_p ||'_'||hre_p ||'.CSV'; -- Nom rapport
 v_DATA1:='Rubrique'||';'|| 'Description'||';'|| 'Centre de charge'||';'||'Descritpion Centre de charge'||';'||'Compte Debit'||';'||'Compte Credit'||';'||'Montant'||';'||'Periode';



  INSERT INTO sw_exp_lst (IDU,PRG,DAT,HRE,FICHIER,PAS,MAT,LIG,DATA)
  VALUES(idu_p,prg_p,dat_p,hre_p,p_sficher,pas_p,0,ligne,v_DATA1);
  ligne:=ligne+1;
    
 ------lignes
 
open c_zjc2;
loop
fetch c_zjc2  into r_zjc2;
exit when c_zjc2%notfound;

v_DATA1:=r_zjc2.zjcp_let||r_zjc2.zjcp_num||';'|| r_zjc2.srul_lib||';'|| r_zjc2.zjcp_sta||';'||r_zjc2.ssta_lib||';'||r_zjc2.zjcp_deb||';'||r_zjc2.zjcp_cre||';'||r_zjc2.MNT||';'||to_char(v_date,'MM.yyyy');

 INSERT INTO sw_exp_lst (IDU,PRG,DAT,HRE,FICHIER,PAS,MAT,LIG,DATA)
  VALUES(idu_p,prg_p,dat_p,hre_p,p_sficher,pas_p,0,ligne,v_DATA1);
  ligne:=ligne+1;



  end loop;
  close c_zjc2;
  commit;  
      
  exception when others then
  null;
  
end;
end ;
/



-- End of DDL Script for Procedure NOVA.PR_CREATE_EXPORT_IC

