create or replace procedure tahsilat_yaslandirma(psirket_kod number,pmust_kod varchar2,pbas_tar date) is

cursor c1 is
select A.SIRKET_KOD,A.MUST_KOD,A.ISLEM_TAR,null bordro_no,A.ISLEM_REF,A.TUTAR,A.ISLEM_TAR VADE_TAR from hareket_table a
where a.sirket_kod=psirket_kod
and a.must_kod=pmust_kod
and a.ba='A'
and a.islem_tar>=pbas_tar
and substr(islem_ref,1,2) in ('TH','BA')
UNION ALL
select b.SIRKET_KOD,b.MUST_KOD,b.ISLEM_TAR,b.bordro_no,b.ISLEM_REF,b.TUTAR,b.vade_tarihi from cek_senet_detay_table b
where b.sirket_kod=psirket_kod
and b.must_kod=pmust_kod
and b.islem_tar>=pbas_tar
UNION ALL
select b.SIRKET_KOD,b.MUST_KOD,b.iade_tarihi,b.bordro_no,b.iade_ref,-1*b.TUTAR,b.iade_tarihi vade_tar from cek_senet_detay_table b
where b.sirket_kod=1
and b.must_kod=pmust_kod
and b.sonuc='M'
and b.iade_tarihi>=pbas_tar
UNION ALL
select c.SIRKET_KOD,c.MUST_KOD,c.islem_tar,null bordro_no,c.islem_ref,-1*(c.toplam_tutar-nvl(c.tevkifat,0)),c.islem_tar vade_tar from fatura_giris_table c
where c.sirket_kod=psirket_kod
and c.must_kod=pmust_kod
and c.tip=0
and c.islem_tar>=pbas_tar
ORDER BY 3,6;


kayit c1%rowtype;
xdagitilacak_tutar number(18,2);
xdagitilan_tutar   number(18,2);
xkalan_tutar   number(18,2);
xesleme_tutar  number(18,2);
xbordro_no varchar2(200);

cursor c2 is
select b.islem_tar,b.islem_ref,b.toplam_tutar-nvl(b.tevkifat,0) tutar,(b.toplam_tutar-nvl(b.tevkifat,0) )-
(select nvl(sum(t.esleme_fat_tutar),0) from tahsilat_esleme_table t
                where t.sirket_kod=b.sirket_kod
                and t.fat_islem_tar=b.islem_tar
                and t.fat_islem_ref=b.islem_ref)

 bakiye,b.fatura_no from fatura_giris_table b
where b.sirket_kod=psirket_kod
and b.must_kod=pmust_kod
and b.islem_tar<=kayit.islem_tar
and b.tip=1
and (b.toplam_tutar-nvl(b.tevkifat,0) )> (select nvl(sum(t.esleme_fat_tutar),0) from tahsilat_esleme_table t
                where t.sirket_kod=b.sirket_kod
                and t.fat_islem_tar=b.islem_tar
                and t.fat_islem_ref=b.islem_ref
                and t.esleme_fat_tutar>0)
union                 
select b.islem_tar,b.islem_ref,B.TUTAR, b.tutar-
(select nvl(sum(t.esleme_fat_tutar),0) from tahsilat_esleme_table t
                where t.sirket_kod=b.sirket_kod
                and t.fat_islem_tar=b.islem_tar
                and t.fat_islem_ref=b.islem_ref)
 bakiye,substr(b.aciklama,1,20) from hareket_table b
where b.sirket_kod=psirket_kod
and b.must_kod=pmust_kod
and b.islem_tar<=kayit.islem_tar
and b.ba='B'
and substr(islem_ref,1,2) in ('TH','BA')
and b.tutar> (select nvl(sum(t.esleme_fat_tutar),0) from tahsilat_esleme_table t
                where t.sirket_kod=b.sirket_kod
                and t.fat_islem_tar=b.islem_tar
                and t.fat_islem_ref=b.islem_ref
                and t.esleme_fat_tutar>0);
                
kayit2 c2%rowtype;

cursor c3 is
select b.islem_tar,b.islem_ref,(b.toplam_tutar-nvl(b.tevkifat,0) ) tutar,
(select nvl(sum(t.esleme_fat_tutar),0) from tahsilat_esleme_table t
                where t.sirket_kod=b.sirket_kod
                and t.fat_islem_tar=b.islem_tar
                and t.fat_islem_ref=b.islem_ref)

 bakiye,b.fatura_no from fatura_giris_table b
where b.sirket_kod=psirket_kod
and b.must_kod=pmust_kod
and b.islem_tar<=kayit.islem_tar
and b.tip=1
and exists (select 1 from  tahsilat_esleme_table t
                where t.sirket_kod=b.sirket_kod
                and t.fat_islem_tar=b.islem_tar
                and t.fat_islem_ref=b.islem_ref
                and t.esleme_fat_tutar>0)
union                 
select b.islem_tar,b.islem_ref,b.tutar,
(select nvl(sum(t.esleme_fat_tutar),0) from tahsilat_esleme_table t
                where t.sirket_kod=b.sirket_kod
                and t.fat_islem_tar=b.islem_tar
                and t.fat_islem_ref=b.islem_ref)

 bakiye,b.aciklama from hareket_table b
where b.sirket_kod=psirket_kod
and b.must_kod=pmust_kod
and b.islem_tar<=kayit.islem_tar
and b.ba='B'
and substr(islem_ref,1,2) in ('TH','BA')
and exists (select 1 from  tahsilat_esleme_table t
                where t.sirket_kod=b.sirket_kod
                and t.fat_islem_tar=b.islem_tar
                and t.fat_islem_ref=b.islem_ref
                and t.esleme_fat_tutar>0)
ORDER BY 1 DESC                
;
                
kayit3 c3%rowtype;
xislem_ref varchar2(10);

begin
  delete tahsilat_esleme_table a
  where a.sirket_kod=psirket_kod
  and a.MUST_KOD=PMUST_KOD
  AND A.ISLEM_TAR>=PBAS_TAR;
  commit;
  open c1;
  loop
      fetch c1 into kayit;
      exit when (c1%notfound);
         xdagitilacak_tutar:=kayit.tutar;
         xislem_ref:=kayit.islem_ref;
         xbordro_no:=kayit.bordro_no;
         select nvl(sum(t.esleme_fat_tutar),0) into xdagitilan_tutar from tahsilat_esleme_table t
                where t.sirket_kod=psirket_kod
                and t.islem_tar=kayit.islem_tar
                and t.islem_ref=xislem_ref
                and t.must_kod=pmust_kod
                and t.esleme_fat_tutar>0
               and nvl(t.bordro_no,'X')=nvl(kayit.bordro_no,'X');
         
      if xdagitilacak_tutar>0 then   
         xkalan_tutar:=xdagitilacak_tutar-xdagitilan_tutar;   
                      
      open c2;
      loop
          fetch c2 into kayit2;
          exit when (c2%notfound);
          if kayit2.bakiye<xkalan_tutar then
             xesleme_tutar:=kayit2.bakiye;
          else
             xesleme_tutar:=xkalan_tutar;
          end if;
          xkalan_tutar:=xkalan_tutar-xesleme_tutar;
          insert into tahsilat_esleme_table(sirket_kod,
                                            sube_kod,
                                            islem_tar,
                                            islem_ref,
                                            bordro_no,
                                            vade_tarihi,
                                            odeme_doviz,
                                            odeme_tutar,
                                            fat_islem_tar,
                                            fat_islem_ref,
                                            fat_doviz,
                                            fat_tutar,
                                            esleme_odeme_tutar,
                                            esleme_fat_tutar,
                                            must_kod,Fatura_No)
          values
(psirket_kod,
                                            1,
                                            kayit.islem_tar,
                                            kayit.islem_ref,
                                            kayit.bordro_no,
                                            kayit.vade_tar,
                                            null,
                                            kayit.tutar,
                                            kayit2.islem_tar,
                                            kayit2.islem_ref,
                                            null,
                                            kayit2.tutar,
                                            xesleme_tutar,
                                            xesleme_tutar,
                                            pmust_kod,kayit2.fatura_no);  
                                            commit;                                          

          exit when (xkalan_tutar=0);
      end loop;
      close c2;  
     else
       if xbordro_no is not null then
          insert into tahsilat_esleme_table(sirket_kod,
                                            sube_kod,
                                            islem_tar,
                                            islem_ref,
                                            bordro_no,
                                            vade_tarihi,
                                            odeme_doviz,
                                            odeme_tutar,
                                            fat_islem_tar,
                                            fat_islem_ref,
                                            fat_doviz,
                                            fat_tutar,
                                            esleme_odeme_tutar,
                                            esleme_fat_tutar,
                                            must_kod,fatura_no)
         select
psirket_kod,
                                            1,
                                            kayit.islem_tar,
                                            kayit.islem_ref,
                                            kayit.bordro_no,
                                            kayit.vade_tar,
                                            null,
                                            kayit.tutar,
                                            a.islem_tar,
                                            a.islem_ref,
                                            null,
                                            a.fat_tutar,
                                            -1*a.esleme_odeme_tutar,
                                            -1*a.esleme_fat_tutar,
                                            a.must_kod,
                                            a.fatura_no
          from tahsilat_esleme_table a
          where a.sirket_kod=psirket_kod
          and a.must_kod=pmust_kod
          and a.bordro_no=xbordro_no;                                  
                                            commit;                                            
         
       
       
       else
         select nvl(sum(-1*t.esleme_fat_tutar),0) into xdagitilan_tutar from tahsilat_esleme_table t
                where t.sirket_kod=psirket_kod
                and t.islem_tar=kayit.islem_tar
                and t.islem_ref=xislem_ref
                and t.must_kod=pmust_kod
                and t.esleme_fat_tutar<0
               and nvl(t.bordro_no,'X')=nvl(kayit.bordro_no,'X');
       
         xkalan_tutar:=(-1*xdagitilacak_tutar)-xdagitilan_tutar;   
                      
      open c3;
      loop
          fetch c3 into kayit3;
          exit when (c3%notfound);
          if (kayit3.bakiye)<xkalan_tutar then
             xesleme_tutar:=kayit3.bakiye;
          else
             xesleme_tutar:=xkalan_tutar;
          end if;
          xkalan_tutar:=xkalan_tutar-xesleme_tutar;
          insert into tahsilat_esleme_table(sirket_kod,
                                            sube_kod,
                                            islem_tar,
                                            islem_ref,
                                            bordro_no,
                                            vade_tarihi,
                                            odeme_doviz,
                                            odeme_tutar,
                                            fat_islem_tar,
                                            fat_islem_ref,
                                            fat_doviz,
                                            fat_tutar,
                                            esleme_odeme_tutar,
                                            esleme_fat_tutar,
                                            must_kod,fatura_no)
          values
(psirket_kod,
                                            1,
                                            kayit.islem_tar,
                                            kayit.islem_ref,
                                            kayit.bordro_no,
                                            kayit.vade_tar,
                                            null,
                                            kayit.tutar,
                                            kayit3.islem_tar,
                                            kayit3.islem_ref,
                                            null,
                                            kayit3.tutar,
                                            -1*xesleme_tutar,
                                            -1*xesleme_tutar,
                                            pmust_kod,kayit3.fatura_no);
                                            commit;                                            

          exit when (xkalan_tutar=0);
      end loop;
      close c3;  
     end if;
      
     end if;
     
  end loop;
  close c1;
end;
