declare 
  cursor c_mov is
  select a.cod_proveedor,sum(decode(b.tip_saldo,'C',mon_saldo,-mon_saldo)) saldomov
  from cxp_enc_movimiento a,cxp_documento b
  where a.cod_cia ='001'
  and   a.tip_doc = b.tip_doc
  and  a.est_movimiento='A'
  and  a.cod_moneda='CL'
  group by a.cod_proveedor;
  vsaldo number:=0;
begin
  for a in c_mov
  loop
   begin
      select saldo into vsaldo
      from cxp_credito
      where cod_cia ='001'
      and cod_moneda='CL'
      and cod_proveedor=a.cod_proveedor;
    exception
      when no_data_found then
          insert into cxp_credito(COD_CIA,COD_PROVEEDOR,          
          COD_MONEDA,DIAS_CREDITO,LIMITE_CREDITO,INTERES,SALDO)
          VALUES('001',a.cod_proveedor,'CL',30,9999999,0,0);                  
         vsaldo:=0;
    end;
    if vsaldo<>a.saldomov then
       --update cxp_credito
       --set saldo=a.saldomov
       --where cod_cia='001'
       --and  cod_proveedor=a.cod_proveedor;
       insert into cxp_credito_tmp
       values(a.cod_proveedor,a.saldomov,vsaldo);
    end if;
  end loop;
end;
---- Verificar saldo a partir de movimientos 
declare 
  cursor c_mov is
  select a.cod_proveedor,sum(decode(b.tip_saldo,'C',mon_docto,-mon_docto)) saldomov
  from cxp_enc_movimiento a,cxp_documento b
  where a.cod_cia ='001'
  and   a.tip_doc = b.tip_doc
  and   a.est_movimiento='A'
  group by a.cod_proveedor
  union all
  select a.cod_proveedor,sum(decode(b.tip_saldo,'C',mon_pago,-mon_pago)) saldomov
  from cxp_enc_pago_nota a,cxp_documento b
  where a.cod_cia ='001'
  and   a.tip_doc = b.tip_doc
  and   a.ind_estado='A'
  group by a.cod_proveedor;
  vsaldo number:=0;
begin
  for a in c_mov
  loop
   begin
      select saldo into vsaldo
      from cxp_credito
      where cod_cia ='001'
      and cod_proveedor=a.cod_proveedor;
    exception
      when no_data_found then
         vsaldo:=0;
    end;
    if vsaldo<>a.saldomov then
       insert into cxp_credito_tmp
       values(a.cod_proveedor,a.saldomov,vsaldo);
    end if;
  end loop;
end;

---------------------------------

declare 
  cursor c_mov is
  select a.cod_proveedor,a.sucursal,a.tip_doc,a.num_doc,a.mon_docto,a.mon_saldo
  from cxp_enc_movimiento a,cxp_documento b
  where a.cod_cia ='001'
  and   a.tip_doc = b.tip_doc
  and   b.clase='FC'
  and   a.est_movimiento='A'
  and   a.cod_moneda='CL';

  vsaldo number:=0;
  vsaldo1 number:=0;
begin
  for mov in c_mov
  loop
    begin
        select sum(decode(b.tip_saldo,'C',a.mon_pago,-a.mon_pago)) into vsaldo
        from cxp_det_pago_nota a,cxp_documento b,cxc_enc_pago_nota c
        where a.cod_cia ='001'
        and   c.cod_cia    = '001'
        and   a.tip_doc    = b.tip_doc
        and   c.tip_doc    = b.tip_doc
        and   a.sucursal   = c.sucursal
        and   a.tip_doc    = c.tip_doc
        and   a.num_doc    = c.num_doc
        and   a.sucursal_ref = mov.sucursal
        and   a.tip_doc_ref  = mov.tip_doc
        and   a.num_doc_ref  = mov.num_doc
        and   c.ind_estado   = 'C';    
       exception
        when no_data_found then
            vsaldo:=0;
      end; 
      begin
        select sum(decode(b.tip_saldo,'C',a.mon_pago,-a.mon_pago)) into vsaldo1
        from cxp_canmov a,cxp_documento b
        where cod_cia ='001'
        and   a.tip_doc    = b.tip_doc
        and   sucursal_ref = mov.sucursal
        and   tip_doc_ref  = mov.tip_doc
        and   num_doc_ref  = mov.num_doc;    
       exception
        when no_data_found then
            vsaldo1:=0;
      end;
      if mov.mon_docto+vsaldo+vsaldo1<>mov.mon_saldo then
        insert into cxp_credito_tmp (COD_PROVEEDOR,SALDODOC,SALDO,TIP_DOC,NUM_DOC)
        values(mov.cod_proveedor,vsaldo+vsaldo1,mov.mon_saldo,mov.tip_doc,mov.num_doc);          
      end if;
  end loop;
end;