create or replace PROCEDURE ATUALIZA_SALDO_CAIXA
   (P_CDFILIAL IN FILIAL.CDFILIAL%TYPE,
    P_CDCENTCUST IN CENTCUST.CDCENTCUST%TYPE,
    P_NRSEQLANCDESP IN LANCDESPCAIXA.NRSEQLANCDESP%TYPE,
    P_IDATUALIZA    IN VARCHAR2)
AS

   V_DTMOVIMENTO DATE;
   V_VRLANCDESP FLOAT;
   V_TOTAL_SALDO FLOAT;
   V_SALDOUTIL  FLOAT;
   V_CDTIPODESP  NUMBER;
   V_CDFILIAL   VARCHAR2(04);
   V_CDCENTCUST VARCHAR2(04);
   V_CDEMPRESA  VARCHAR2(02);
   V_NRSEQRELACLANCDESP  VARCHAR2(20);
   V_NRSEQLANCSALD   NUMBER;
  

BEGIN
    -- BUSCA AS   DESPESAS
     BEGIN
       SELECT VRLANCDESP, CDTIPODESP, CDEMPRESA, DTLANCDESP, CDFILIAL, CDCENTCUST
          INTO V_VRLANCDESP, V_CDTIPODESP, V_CDEMPRESA, V_DTMOVIMENTO, V_CDFILIAL, V_CDCENTCUST
          FROM LANCDESPCAIXA
         WHERE CDFILIAL   =  P_CDFILIAL
           AND CDCENTCUST = P_CDCENTCUST
           AND NRSEQLANCDESP = P_NRSEQLANCDESP ;
     EXCEPTION WHEN NO_DATA_FOUND
          THEN
          V_VRLANCDESP  := NULL;
          V_CDTIPODESP  := NULL;
          V_CDEMPRESA   := NULL;
          V_DTMOVIMENTO := NULL;
          V_CDFILIAL    := NULL;
          V_CDCENTCUST  := NULL;
     END;
    
     -- BUSCA O SALDO ANTERIOR
    FOR C_SALDANT IN (SELECT SUM(VRLANCSALDDISP) AS VRLANCSALDDISP, CDCENTCUST, NRSEQLANCSALD, CDEMPRESA, DTLANCSALD, CDFILIAL
                        FROM LANCSALDCAIXA
                       WHERE CDFILIAL = P_CDFILIAL
                         AND VRLANCSALDDISP > 0 OR P_IDATUALIZA =  'S'
                         AND CDCENTCUST = P_CDCENTCUST
                       GROUP BY CDCENTCUST, NRSEQLANCSALD, CDEMPRESA, DTLANCSALD, CDFILIAL
                       ORDER BY DTLANCSALD) LOOP
     V_TOTAL_SALDO := C_SALDANT.VRLANCSALDDISP;
     V_SALDOUTIL   := 0;
    

     IF P_IDATUALIZA  = 'N'  AND V_CDFILIAL IS NOT NULL AND V_VRLANCDESP IS NOT NULL THEN
         IF C_SALDANT.VRLANCSALDDISP <= V_VRLANCDESP THEN
             NOVO_CODIGO_NT('RELACSALDCAIXADESP', V_NRSEQRELACLANCDESP);
             INSERT INTO RELACSALDCAIXADESP
                   (NRSEQRELACLANCDESP, NRSEQLANCDESP, NRSEQLANCSALD,
                    CDEMPRESA, DTRELACSALDDESP, CDFILIAL, CDCENTCUST, VRRELACSALDCAIX, CDTIPODESP)
                   VALUES
                   (TO_NUMBER(V_NRSEQRELACLANCDESP),P_NRSEQLANCDESP, C_SALDANT.NRSEQLANCSALD,
                    V_CDEMPRESA, V_DTMOVIMENTO, V_CDFILIAL, V_CDCENTCUST, C_SALDANT.VRLANCSALDDISP, V_CDTIPODESP);
           UPDATE LANCSALDCAIXA
              SET VRLANCSALDUTIL = nvl(VRLANCSALDUTIL,0)+C_SALDANT.VRLANCSALDDISP,
                  VRLANCSALDDISP = 0
            WHERE NRSEQLANCSALD = C_SALDANT.NRSEQLANCSALD;
              V_VRLANCDESP := V_VRLANCDESP - C_SALDANT.VRLANCSALDDISP;

         ELSE
            IF V_VRLANCDESP > 0 THEN
               BEGIN
                  SELECT MAX(NRSEQLANCSALD)
                    INTO V_NRSEQLANCSALD
                    FROM RELACSALDCAIXADESP
                   WHERE NRSEQLANCSALD = P_NRSEQLANCDESP;
                   
               EXCEPTION  
                    WHEN NO_DATA_FOUND THEN
                         V_NRSEQLANCSALD := NULL;
               END;
               IF V_NRSEQLANCSALD IS NULL THEN
                  NOVO_CODIGO_NT('RELACSALDCAIXADESP', V_NRSEQRELACLANCDESP);
                  INSERT INTO RELACSALDCAIXADESP
                        (NRSEQRELACLANCDESP, NRSEQLANCDESP, NRSEQLANCSALD, 
                         CDEMPRESA, DTRELACSALDDESP, CDFILIAL, CDCENTCUST, VRRELACSALDCAIX, CDTIPODESP)
                        VALUES
                        (TO_NUMBER(V_NRSEQRELACLANCDESP),P_NRSEQLANCDESP, C_SALDANT.NRSEQLANCSALD,
                         V_CDEMPRESA, V_DTMOVIMENTO, V_CDFILIAL, V_CDCENTCUST, nvl(V_VRLANCDESP,0), V_CDTIPODESP);
                    UPDATE LANCSALDCAIXA
                       SET VRLANCSALDUTIL = nvl(VRLANCSALDUTIL,0) + nvl(V_VRLANCDESP,0),
                           VRLANCSALDDISP = nvl(VRLANCSALDDISP,0) - nvl(V_VRLANCDESP,0)
                     WHERE NRSEQLANCSALD = C_SALDANT.NRSEQLANCSALD;
                     V_VRLANCDESP := 0;
               ELSE
                 UPDATE RELACSALDCAIXADESP
                    SET VRRELACSALDCAIX = V_VRLANCDESP
                  WHERE NRSEQLANCDESP = P_NRSEQLANCDESP;

                    UPDATE LANCSALDCAIXA
                       SET VRLANCSALDUTIL = nvl(VRLANCSALDUTIL,0) + nvl(V_VRLANCDESP,0),
                           VRLANCSALDDISP = nvl(VRLANCSALDDISP,0) - nvl(V_VRLANCDESP,0)
                     WHERE NRSEQLANCSALD = C_SALDANT.NRSEQLANCSALD;

                   end if;
               END IF;
         END IF ;
     ELSIF P_IDATUALIZA = 'S' AND V_CDFILIAL IS NOT NULL AND V_VRLANCDESP IS  NOT NULL THEN
         BEGIN
            SELECT SUM(VRRELACSALDCAIX)
              INTO V_SALDOUTIL
              FROM RELACSALDCAIXADESP
             WHERE NRSEQLANCSALD = C_SALDANT.NRSEQLANCSALD
               AND NRSEQLANCDESP = P_NRSEQLANCDESP
               AND CDFILIAL   = V_CDFILIAL
               AND CDCENTCUST = V_CDCENTCUST;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_SALDOUTIL := NULL;
         END;

          UPDATE LANCSALDCAIXA
             SET VRLANCSALDUTIL = NVL(VRLANCSALDUTIL,0) - NVL(V_SALDOUTIL,0),
                 VRLANCSALDDISP = NVL(VRLANCSALDDISP,0) + NVL(V_SALDOUTIL,0)
           WHERE NRSEQLANCSALD = C_SALDANT.NRSEQLANCSALD
             AND CDFILIAL   = V_CDFILIAL
             AND CDCENTCUST = V_CDCENTCUST;

     END IF;

    END LOOP;
END;
/
-- VERSAO --
BEGIN ATUVEROBJ('ATUALIZA_SALDO_CAIXA','6.19.XXX'); END;
/