CREATE OR REPLACE PROCEDURE ATUALIZA_CUSTO_ABSORCAO
   (P_CDFILIAL     IN FILIAL.CDFILIAL%TYPE,
    P_DATACUSTO    IN FILIAL.DTULTATU%TYPE,
    P_CDPRODINI    IN PRODUTO.CDARVPROD%TYPE,
    P_CDPRODFIN    IN PRODUTO.CDARVPROD%TYPE,
    P_ERRO         OUT VARCHAR)

AS
   V_VRCUSTOCONT   FLOAT;
   V_QTTEMPOAPO    FLOAT;
   V_COUNT         INTEGER;
   V_QTHRTOTAL     FLOAT;
   V_DTINICCONT    DATE;
   V_DTFINACONT    DATE;
   V_NRSEQLANC     NUMBER;
   V_NRLANCCONT    VARCHAR2(20);
   V_CDEMPRESA     VARCHAR2(02);
   V_VRTOTRATEADO  FLOAT;
   V_VRDIFERENCA   FLOAT;
   V_CDHISTPADR    VARCHAR2(04);
   V_DSHISTLANC    VARCHAR2(200);
   V_IDCONTCCUS    VARCHAR2(01);
   V_CDCENTCUST    VARCHAR2(04);
   V_IDCUSTFIL     VARCHAR2(01);
BEGIN
   P_ERRO := 'N';
   V_IDCUSTFIL := 'N';
   /* Verifica os rateios das contas contabeis associadas para calculo do custo */
   BEGIN
      SELECT ABS(SUM(S.VRLANCCUST))
        INTO V_VRCUSTOCONT
        FROM ATUCUSTPROD P, FILIAL F, LANCCCUSTO S
       WHERE P.CDFILIAL   = P_CDFILIAL
         AND F.CDFILIAL   = P.CDFILIAL
         AND S.CDEMPRESA  = F.CDEMPRESA
         AND S.CDCONTCTBL = P.CDCONTCTBL
         AND S.CDFILIAL   = P_CDFILIAL
         AND S.DTCOMPCUSTO BETWEEN P_DATACUSTO AND LAST_DAY(P_DATACUSTO);
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_VRCUSTOCONT := 0;
   END;
   IF NVL(V_VRCUSTOCONT,0) = 0 THEN
      /* Verifica contas contabeis associadas para calculo do custo NA LANCCONT caso nao exista rateio para as contas*/
      BEGIN
         SELECT ABS(SUM(S.VRLANCAMENT))
           INTO V_VRCUSTOCONT
           FROM ATUCUSTPROD P, FILIAL F, LANCCONT S
          WHERE P.CDFILIAL   = P_CDFILIAL
            AND F.CDFILIAL   = P.CDFILIAL
            AND S.CDEMPRESA  = F.CDEMPRESA
            AND S.CDCONTCTBL = P.CDCONTCTBL
            AND S.dtlanccont BETWEEN P_DATACUSTO AND LAST_DAY(P_DATACUSTO);
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_VRCUSTOCONT := 0;
      END;
   ELSE
      V_IDCUSTFIL := 'S';
   END IF;

   BEGIN
      SELECT CDEMPRESA
        INTO V_CDEMPRESA
        FROM FILIAL
       WHERE CDFILIAL = P_CDFILIAL;
   EXCEPTION
     WHEN NO_DATA_FOUND THEN
       V_CDEMPRESA := NULL;
   END;
   /* Verifica se foi realizado o calculo do custo de absorcao */
   BEGIN
      SELECT COUNT(*)
        INTO V_COUNT
        FROM CUSTOABSORCAOPR
       WHERE CDFILIAL = P_CDFILIAL
         AND DTCUSTOABSORCAO = LAST_DAY(P_DATACUSTO);
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_COUNT := 0;
   END;

   IF (V_VRCUSTOCONT > 0) AND (V_COUNT = 0) THEN

      /* Verifica se o periodo contabil esta aberto */
      BEGIN
         SELECT C.DTINICCONT, C.DTFINACONT
           INTO V_DTINICCONT, V_DTFINACONT
           FROM FILIAL F, EMPRESA E, CONTOPECTBL C
          WHERE F.CDFILIAL = P_CDFILIAL
            AND E.CDEMPRESA = F.CDEMPRESA
            AND C.CDEMPRESA = E.CDEMPRESA;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_DTINICCONT := SYSDATE; V_DTFINACONT := SYSDATE;
      END;

      IF (P_DATACUSTO >= V_DTINICCONT) AND (P_DATACUSTO <= V_DTFINACONT) THEN
         /* Tempo apontado referente as tarefas por habilidade (homem/hora) por produto em todas as
            ordens de producao (OP) conforme mes informado (tempo de producao) */
         FOR C_PROD IN (SELECT P.CDSUBPRODETA AS CDPRODUTO, SUM(I.QTTOTLANCTO) AS QTPRODUZIDA, NVL(SUM(QTHSPRODUZIDOPR),0) AS QTHSPRODUZIDOPR
                          FROM ORDPRODUCAO O, PRODACABADO P, ITLANCTOEST I, PRODUTO A,
                               (SELECT O.CDFILIAL, O.NRORDPROD, O.NRSUBORDPROD, (R.DTFINUTRECOP - R.DTINIUTRECOP) * 24 AS QTHSPRODUZIDOPR
                                  FROM ORDPRODUCAO O, AUDITORIA A, RECURETAPAOP R, PRODUTO P
                                 WHERE P.CDARVPROD BETWEEN P_CDPRODINI AND P_CDPRODFIN
                                   AND O.CDPRODUTO = P.CDPRODUTO
                                   AND O.CDFILIAL = P_CDFILIAL
                                   AND A.CDFILIAL = O.CDFILIAL
                                   AND A.NRORDPROD = O.NRORDPROD
                                   AND A.NRSUBORDPROD = O.NRSUBORDPROD
                                   AND A.IDORIGAUDIT = 'TAR'
                                   AND R.CDFILIAL = A.CDFILIAL
                                   AND R.NRAUDITORIA = A.NRAUDITORIA
                                   AND R.IDRECURETAOP = 'H'
                                   AND TO_CHAR(P_DATACUSTO, 'MM/YYYY') = TO_CHAR(R.DTINIUTRECOP, 'MM/YYYY')
                                   AND NOT R.DTFINUTRECOP IS NULL) C
                         WHERE A.CDARVPROD BETWEEN P_CDPRODINI AND P_CDPRODFIN
                           AND O.CDPRODUTO = A.CDPRODUTO
                           AND O.CDFILIAL = P_CDFILIAL
                           AND O.IDSITORDPROD = '3'
                           AND TRUNC(O.DTHRFIMREAOP) BETWEEN P_DATACUSTO AND LAST_DAY(P_DATACUSTO)
                           AND P.CDFILIAL = O.CDFILIAL
                           AND P.NRORDPROD = O.NRORDPROD
                           AND P.NRSUBORDPROD = O.NRSUBORDPROD
                           AND O.CDFILIAL = C.CDFILIAL (+)
                           AND O.NRORDPROD = C.NRORDPROD (+)
                           AND O.NRSUBORDPROD = C.NRSUBORDPROD (+)
                           AND I.CDFILIAL = P.CDFILIAL
                           AND I.NRLANCESTQ = P.NRLANCESTQ
                           AND I.NRSEQUITEM = P.NRSEQUITEM
                         GROUP BY P.CDSUBPRODETA)
         LOOP
            INSERT INTO CUSTOABSORCAOPR
               (CDFILIAL, DTCUSTOABSORCAO, VRTOTRATEADO, CDPRODUTO, QTPRODUZIDA, QTHSPRODUZIDOPR, VRCUSTOABSORCAO)
            VALUES
               (P_CDFILIAL, LAST_DAY(P_DATACUSTO), V_VRCUSTOCONT, C_PROD.CDPRODUTO, C_PROD.QTPRODUZIDA, C_PROD.QTHSPRODUZIDOPR, 0);
         END LOOP;

         SELECT SUM(QTHSPRODUZIDOPR)
           INTO V_QTHRTOTAL
           FROM CUSTOABSORCAOPR
          WHERE CDFILIAL = P_CDFILIAL
            AND DTCUSTOABSORCAO = LAST_DAY(P_DATACUSTO);

         IF V_QTHRTOTAL > 0 THEN
         /* (Total de horas de producao do produto / total de horas * Valor total contabil) / qtde produzida do produto no periodo */
            UPDATE CUSTOABSORCAOPR
               SET VRCUSTOABSORCAO = ROUND(((QTHSPRODUZIDOPR / V_QTHRTOTAL) * VRTOTRATEADO) / QTPRODUZIDA, 5)
             WHERE CDFILIAL = P_CDFILIAL
               AND DTCUSTOABSORCAO = LAST_DAY(P_DATACUSTO);

            FOR C_CUSTOABSORCAO IN (SELECT CDFILIAL, CDPRODUTO, VRCUSTOABSORCAO
                                      FROM CUSTOABSORCAOPR
                                     WHERE CDFILIAL = P_CDFILIAL
                                       AND DTCUSTOABSORCAO = LAST_DAY(P_DATACUSTO)
                                     ORDER BY CDPRODUTO)
            LOOP
               FOR C_PRODACABADO IN (SELECT P.CDFILIAL, P.NRLANCESTQ, P.NRSEQUITEM, I.CDFILIMOVI, I.DTLANCMOVI
                                       FROM ORDPRODUCAO O, PRODACABADO P, ITLANCTOEST I, PRODUTO A
                                      WHERE A.CDARVPROD BETWEEN P_CDPRODINI AND P_CDPRODFIN
                                        AND O.CDPRODUTO = A.CDPRODUTO
                                        AND O.CDFILIAL = C_CUSTOABSORCAO.CDFILIAL
                                        AND O.IDSITORDPROD = '3'
                                        AND TRUNC(O.DTHRFIMREAOP) BETWEEN P_DATACUSTO AND LAST_DAY(P_DATACUSTO)
                                        AND P.CDFILIAL = O.CDFILIAL
                                        AND P.NRORDPROD = O.NRORDPROD
                                        AND P.NRSUBORDPROD = O.NRSUBORDPROD
                                        AND P.CDSUBPRODETA = C_CUSTOABSORCAO.CDPRODUTO
                                        AND I.CDFILIAL = P.CDFILIAL
                                        AND I.NRLANCESTQ = P.NRLANCESTQ
                                        AND I.NRSEQUITEM = P.NRSEQUITEM
                                      ORDER BY I.DTLANCMOVI)
               LOOP
                  UPDATE ITLANCTOEST
                     SET VRUNILANCTO  = VRUNILANCTO + C_CUSTOABSORCAO.VRCUSTOABSORCAO,
                         VRTOTLANCTO  = VRTOTLANCTO + (QTTOTLANCTO * C_CUSTOABSORCAO.VRCUSTOABSORCAO),
                         VRLANCTOEST  = VRLANCTOEST + C_CUSTOABSORCAO.VRCUSTOABSORCAO,
                         VRLANCTOBRUT = VRLANCTOBRUT + C_CUSTOABSORCAO.VRCUSTOABSORCAO,
                         VRCUSTENTR   = VRCUSTENTR + (QTTOTLANCTO * C_CUSTOABSORCAO.VRCUSTOABSORCAO),
                         VRUNILANCTOANT = VRUNILANCTO
                   WHERE CDFILIAL   = C_PRODACABADO.CDFILIAL
                     AND NRLANCESTQ = C_PRODACABADO.NRLANCESTQ
                     AND NRSEQUITEM = C_PRODACABADO.NRSEQUITEM;

                  ATUALIZA_ESTOQUE(C_PRODACABADO.CDFILIMOVI, C_PRODACABADO.DTLANCMOVI, TRUNC(SYSDATE), C_PRODACABADO.CDFILIAL, C_PRODACABADO.NRLANCESTQ);
               END LOOP;
            END LOOP;
         END IF;
      ELSE
         P_ERRO := 'S';
      END IF;
   ELSE
      P_ERRO := 'S';
   END IF;

   NOVO_CODIGO_NT('LANCCONTABIL' || V_CDEMPRESA || TO_CHAR(TRUNC(LAST_DAY(P_DATACUSTO)),'YYYYMM'), V_NRLANCCONT);
                  V_NRLANCCONT := TO_CHAR(TRUNC(LAST_DAY(P_DATACUSTO)),'YYYYMM')||SUBSTR(V_NRLANCCONT, 15, 6);
   V_COUNT := 0;
   /*REALIZA A CONTABILIZACAO DO GGF*/
   FOR C_LANC IN (SELECT (SUM(S.VRLANCAMENT)) AS VRLANCAMENT, S.CDCONTCTBL, S.CDEMPRESA, P.CDCONTCTBLGGF,
                        COUNT(*) OVER(PARTITION BY S.CDEMPRESA) AS CONT
                    FROM ATUCUSTPROD P, FILIAL F, LANCCONT S
                   WHERE P.CDFILIAL   = P_CDFILIAL
                     AND F.CDFILIAL   = P.CDFILIAL
                     AND S.CDEMPRESA  = F.CDEMPRESA
                     AND S.CDCONTCTBL = P.CDCONTCTBL
                     AND S.dtlanccont BETWEEN P_DATACUSTO AND LAST_DAY(P_DATACUSTO)
                     AND V_IDCUSTFIL  = 'N'
                   GROUP BY S.CDCONTCTBL, S.CDEMPRESA, P.CDCONTCTBLGGF
                   UNION
                  SELECT (SUM(S.VRLANCCUST)) AS VRLANCAMENT, S.CDCONTCTBL, S.CDEMPRESA, P.CDCONTCTBLGGF,
                        COUNT(*) OVER(PARTITION BY S.CDEMPRESA) AS CONT
                    FROM ATUCUSTPROD P, FILIAL F, LANCCCUSTO S
                   WHERE P.CDFILIAL   = P_CDFILIAL
                     AND F.CDFILIAL   = P.CDFILIAL
                     AND S.CDEMPRESA  = F.CDEMPRESA
                     AND S.CDCONTCTBL = P.CDCONTCTBL
                     AND S.CDFILIAL   = P_CDFILIAL
                     AND S.DTCOMPCUSTO BETWEEN P_DATACUSTO AND LAST_DAY(P_DATACUSTO)
                     AND V_IDCUSTFIL  = 'S'
                   GROUP BY S.CDCONTCTBL, S.CDEMPRESA, P.CDCONTCTBLGGF)LOOP

       V_NRSEQLANC :=   NVL(V_NRSEQLANC,0)+ 1;
       V_COUNT := V_COUNT + 1;
       V_VRTOTRATEADO := NVL(V_VRTOTRATEADO,0) + C_LANC.VRLANCAMENT;
       BEGIN
          SELECT CDHISTPADRGGF
            INTO V_CDHISTPADR
            FROM PARAMESTOQUE;
       EXCEPTION
            WHEN NO_DATA_FOUND THEN
            V_CDHISTPADR := NULL;
       END;
       BEGIN
          SELECT DSHISTPADR
            INTO V_DSHISTLANC
            FROM HISTPADR
           WHERE CDHISTPADR = V_CDHISTPADR ;
       EXCEPTION
            WHEN NO_DATA_FOUND THEN
            V_DSHISTLANC := NULL;
       END;

       MONTA_HISTORICO(C_LANC.CDEMPRESA, P_CDFILIAL, V_CDHISTPADR, TO_CHAR(TRUNC(LAST_DAY(P_DATACUSTO)),'DDMMYY'), TRUNC(LAST_DAY(P_DATACUSTO)),'14', '',V_DSHISTLANC);

       INSERT INTO LANCCONT
            (CDEMPRESA, NRLANCCONT, NRSEQLANC, DTLANCCONT, CDCONTCTBL,
             CDHISTPADR, DSHISTLANC, IDTIPOLANC, VRLANCAMENT,
             IDLANCSALDO, IDLANCINTE, DTULTATULAN, IDLANCCOME, CDCONTCOME, CDDOCUCONT, CDFILLANC)
         VALUES
            (C_LANC.CDEMPRESA, V_NRLANCCONT, LPAD(TO_CHAR(V_NRSEQLANC),5,'0'), TRUNC(LAST_DAY(P_DATACUSTO)), C_LANC.CDCONTCTBL,
             V_CDHISTPADR, V_DSHISTLANC, DECODE(C_LANC.VRLANCAMENT,ABS(C_LANC.VRLANCAMENT),'D','C'), C_LANC.VRLANCAMENT*-1,
             'N', 'I', TRUNC(SYSDATE), NULL, NULL, TO_CHAR(TRUNC(LAST_DAY(P_DATACUSTO)),'DDMMYY'),P_CDFILIAL);

       BEGIN
          SELECT NVL(F.CDCENTCUST, E.CDCENTCUST) AS CDCENTCUST
            INTO V_CDCENTCUST
            FROM FILIAL F, EMPRESA E
           WHERE F.CDFILIAL  = P_CDFILIAL
             AND E.CDEMPRESA = C_LANC.CDEMPRESA;
       EXCEPTION
            WHEN NO_DATA_FOUND THEN
             V_CDCENTCUST := NULL;
       END;

       BEGIN
          SELECT IDCONTCCUS
            INTO V_IDCONTCCUS
            FROM CONTCTBL
           WHERE CDCONTCTBL = C_LANC.CDCONTCTBL;
       EXCEPTION
           WHEN NO_DATA_FOUND THEN
             V_IDCONTCCUS := NULL;
       END;

       IF V_CDCENTCUST IS NOT NULL THEN
          IF V_IDCONTCCUS  <> 'O' THEN
             INSERT INTO LANCCCUSTO
                  (CDEMPRESA, NRLANCCONT, NRSEQLANC, CDFILIAL,
                   CDCENTCUST, VRLANCCUST, DTCOMPCUSTO)
                VALUES
                  (C_LANC.CDEMPRESA, V_NRLANCCONT, LPAD(TO_CHAR(V_NRSEQLANC),5,'0'),  P_CDFILIAL,
                   V_CDCENTCUST, C_LANC.VRLANCAMENT*-1, TRUNC(LAST_DAY(P_DATACUSTO)));
          END IF;
       END IF;
       V_NRSEQLANC :=   NVL(V_NRSEQLANC,0)+ 1;
       INSERT INTO LANCCONT
            (CDEMPRESA, NRLANCCONT, NRSEQLANC, DTLANCCONT, CDCONTCTBL,
             CDHISTPADR, DSHISTLANC, IDTIPOLANC, VRLANCAMENT,
             IDLANCSALDO, IDLANCINTE, DTULTATULAN, IDLANCCOME, CDCONTCOME, CDDOCUCONT, CDFILLANC)
         VALUES
            (C_LANC.CDEMPRESA, V_NRLANCCONT, LPAD(TO_CHAR(V_NRSEQLANC),5,'0'), TRUNC(LAST_DAY(P_DATACUSTO)), C_LANC.CDCONTCTBLGGF,
             V_CDHISTPADR, V_DSHISTLANC, DECODE(C_LANC.VRLANCAMENT,ABS(C_LANC.VRLANCAMENT),'C','D'), C_LANC.VRLANCAMENT,
             'N', 'I', TRUNC(SYSDATE), NULL, NULL, TO_CHAR(TRUNC(LAST_DAY(P_DATACUSTO)),'DDMMYY'),P_CDFILIAL);

      BEGIN
         SELECT IDCONTCCUS
           INTO V_IDCONTCCUS
           FROM CONTCTBL
          WHERE CDCONTCTBL = C_LANC.CDCONTCTBLGGF;
      EXCEPTION
          WHEN NO_DATA_FOUND THEN
            V_IDCONTCCUS := NULL;
      END;

      IF V_CDCENTCUST IS NOT NULL THEN
         IF V_IDCONTCCUS  <> 'O' THEN
            INSERT INTO LANCCCUSTO
                 (CDEMPRESA, NRLANCCONT, NRSEQLANC, CDFILIAL,
                  CDCENTCUST, VRLANCCUST, DTCOMPCUSTO)
               VALUES
                 (C_LANC.CDEMPRESA, V_NRLANCCONT, LPAD(TO_CHAR(V_NRSEQLANC),5,'0'),  P_CDFILIAL,
                  V_CDCENTCUST, C_LANC.VRLANCAMENT, TRUNC(LAST_DAY(P_DATACUSTO)));
         END IF;
      END IF;



   END LOOP;
   UPDATE CUSTOABSORCAOPR
      SET CDEMPRESA = V_CDEMPRESA,
          NRLANCCONTGGF = V_NRLANCCONT
    WHERE CDFILIAL = P_CDFILIAL
      AND DTCUSTOABSORCAO = LAST_DAY(P_DATACUSTO);
   V_COUNT := 0;   
   IF V_IDCUSTFIL = 'N' THEN
      V_COUNT := 1;
      V_NRSEQLANC := 0;
      V_VRTOTRATEADO := 0;
      V_VRDIFERENCA := 0;
      NOVO_CODIGO_NT('LANCCONTABIL' || V_CDEMPRESA || TO_CHAR(TRUNC(LAST_DAY(P_DATACUSTO)),'YYYYMM'), V_NRLANCCONT);
                     V_NRLANCCONT := TO_CHAR(TRUNC(LAST_DAY(P_DATACUSTO)),'YYYYMM')||SUBSTR(V_NRLANCCONT, 15, 6);
      FOR C_TRANSF IN (SELECT F.CDEMPRESA,
                              CONTCTBL_PRODUTO(P.CDARVPROD, 'C', F.CDFILIAL) CDCTBLPRODPR,
                              CTBL_PRODUTO(P.CDPRODUTO, 'E') AS CDCTBLENTRPR,
                              ROUND(sum(I.QTTOTLANCTO * I.VRUNILANCTO) - sum(I.QTTOTLANCTO * I.VRUNILANCTOANT),2) AS VRDIFERENCA,
                              COUNT(*) OVER(PARTITION BY F.CDEMPRESA) AS CONT
                         FROM ITLANCTOEST I, PRODACABADO A, PRODUTO P, CUSTOABSORCAOPR C, FILIAL F
                        WHERE I.CDFILIMOVI = P_CDFILIAL
                          AND I.DTLANCMOVI BETWEEN P_DATACUSTO AND LAST_DAY(P_DATACUSTO)
                          AND I.IDTIPOMOVI = '2'
                          AND I.CDFILIAL = A.CDFILIAL
                          AND I.NRLANCESTQ = A.NRLANCESTQ
                          AND I.NRSEQUITEM = A.NRSEQUITEM
                          AND P.CDPRODUTO = I.CDPRODMOVI
                          AND C.CDFILIAL = I.CDFILIAL
                          AND C.CDPRODUTO = I.CDPRODUTO
                          AND TO_CHAR(C.DTCUSTOABSORCAO, 'MM/YYYY') = TO_CHAR (I.DTLANCMOVI, 'MM/YYYY')
                          AND F.CDFILIAL = I.CDFILIMOVI
                        GROUP BY F.CDEMPRESA,
                                 CONTCTBL_PRODUTO(P.CDARVPROD, 'C', F.CDFILIAL),
                                 CTBL_PRODUTO(P.CDPRODUTO, 'E') )LOOP


          V_COUNT := V_COUNT + 1;
          V_VRTOTRATEADO := NVL(V_VRTOTRATEADO,0) + C_TRANSF.VRDIFERENCA;
          V_NRSEQLANC :=   NVL(V_NRSEQLANC,0)+ 1;
          BEGIN
             SELECT CDHISTPADRTRANSGGF
               INTO V_CDHISTPADR
               FROM PARAMESTOQUE;
          EXCEPTION
               WHEN NO_DATA_FOUND THEN
               V_CDHISTPADR := NULL;
          END;
          BEGIN
             SELECT DSHISTPADR
               INTO V_DSHISTLANC
               FROM HISTPADR
              WHERE CDHISTPADR = V_CDHISTPADR ;
          EXCEPTION
               WHEN NO_DATA_FOUND THEN
               V_DSHISTLANC := NULL;
          END;

          MONTA_HISTORICO(C_TRANSF.CDEMPRESA, P_CDFILIAL, V_CDHISTPADR, TO_CHAR(TRUNC(LAST_DAY(P_DATACUSTO)),'DDMMYY'), TRUNC(LAST_DAY(P_DATACUSTO)),'14', '',V_DSHISTLANC);

          INSERT INTO LANCCONT
               (CDEMPRESA, NRLANCCONT, NRSEQLANC, DTLANCCONT, CDCONTCTBL,
                CDHISTPADR, DSHISTLANC, IDTIPOLANC, VRLANCAMENT,
                IDLANCSALDO, IDLANCINTE, DTULTATULAN, IDLANCCOME, CDCONTCOME, CDDOCUCONT, CDFILLANC)
            VALUES
               (C_TRANSF.CDEMPRESA, V_NRLANCCONT, LPAD(TO_CHAR(V_NRSEQLANC),5,'0'), TRUNC(LAST_DAY(P_DATACUSTO)), C_TRANSF.CDCTBLENTRPR,
                V_CDHISTPADR, V_DSHISTLANC, 'C', C_TRANSF.VRDIFERENCA*-1,
                'N', 'I', TRUNC(SYSDATE), NULL, NULL, TO_CHAR(TRUNC(LAST_DAY(P_DATACUSTO)),'DDMMYY'),P_CDFILIAL);
          BEGIN
             SELECT NVL(F.CDCENTCUST, E.CDCENTCUST) AS CDCENTCUST
               INTO V_CDCENTCUST
               FROM FILIAL F, EMPRESA E
              WHERE F.CDFILIAL  = P_CDFILIAL
                AND E.CDEMPRESA = C_TRANSF.CDEMPRESA;
          EXCEPTION
               WHEN NO_DATA_FOUND THEN
                V_CDCENTCUST := NULL;
          END;

          BEGIN
             SELECT IDCONTCCUS
               INTO V_IDCONTCCUS
               FROM CONTCTBL
              WHERE CDCONTCTBL = C_TRANSF.CDCTBLENTRPR;
          EXCEPTION
              WHEN NO_DATA_FOUND THEN
                V_IDCONTCCUS := NULL;
          END;

          IF V_CDCENTCUST IS NOT NULL THEN
             IF V_IDCONTCCUS  <> 'O' THEN
                INSERT INTO LANCCCUSTO
                     (CDEMPRESA, NRLANCCONT, NRSEQLANC, CDFILIAL,
                      CDCENTCUST, VRLANCCUST, DTCOMPCUSTO)
                   VALUES
                     (C_TRANSF.CDEMPRESA, V_NRLANCCONT, LPAD(TO_CHAR(V_NRSEQLANC),5,'0'),  P_CDFILIAL,
                      V_CDCENTCUST, C_TRANSF.VRDIFERENCA*-1, TRUNC(LAST_DAY(P_DATACUSTO)));
             END IF;
          END IF;

          V_NRSEQLANC :=   NVL(V_NRSEQLANC,0)+ 1;
          INSERT INTO LANCCONT
               (CDEMPRESA, NRLANCCONT, NRSEQLANC, DTLANCCONT, CDCONTCTBL,
                CDHISTPADR, DSHISTLANC, IDTIPOLANC, VRLANCAMENT,
                IDLANCSALDO, IDLANCINTE, DTULTATULAN, IDLANCCOME, CDCONTCOME, CDDOCUCONT, CDFILLANC)
            VALUES
               (C_TRANSF.CDEMPRESA, V_NRLANCCONT, LPAD(TO_CHAR(V_NRSEQLANC),5,'0'), TRUNC(LAST_DAY(P_DATACUSTO)), C_TRANSF.CDCTBLPRODPR,
                V_CDHISTPADR, V_DSHISTLANC, 'D', C_TRANSF.VRDIFERENCA,
                'N', 'I', TRUNC(SYSDATE), NULL, NULL, TO_CHAR(TRUNC(LAST_DAY(P_DATACUSTO)),'DDMMYY'),P_CDFILIAL);

          BEGIN
             SELECT IDCONTCCUS
               INTO V_IDCONTCCUS
               FROM CONTCTBL
              WHERE CDCONTCTBL = C_TRANSF.CDCTBLPRODPR;
          EXCEPTION
              WHEN NO_DATA_FOUND THEN
                V_IDCONTCCUS := NULL;
          END;

          IF V_CDCENTCUST IS NOT NULL THEN
             IF V_IDCONTCCUS  <> 'O' THEN
                INSERT INTO LANCCCUSTO
                     (CDEMPRESA, NRLANCCONT, NRSEQLANC, CDFILIAL,
                      CDCENTCUST, VRLANCCUST, DTCOMPCUSTO)
                   VALUES
                     (C_TRANSF.CDEMPRESA, V_NRLANCCONT, LPAD(TO_CHAR(V_NRSEQLANC),5,'0'),  P_CDFILIAL,
                      V_CDCENTCUST, C_TRANSF.VRDIFERENCA, TRUNC(LAST_DAY(P_DATACUSTO)));
             END IF;
          END IF;


          IF V_COUNT = C_TRANSF.CONT THEN
             IF ROUND(NVL(V_VRTOTRATEADO,0),2) <> ROUND(NVL(V_VRCUSTOCONT,0),2) THEN
                V_VRDIFERENCA := NVL(V_VRCUSTOCONT,0)- NVL(V_VRTOTRATEADO,0);
                UPDATE LANCCONT
                   SET VRLANCAMENT =  VRLANCAMENT + DECODE(IDTIPOLANC,'D',(ROUND(V_VRDIFERENCA,2)),ABS(ROUND(V_VRDIFERENCA,2)))
                 WHERE CDEMPRESA   = C_TRANSF.CDEMPRESA
                   AND NRLANCCONT  = V_NRLANCCONT
                   AND NRSEQLANC   IN (LPAD(TO_CHAR(V_NRSEQLANC),5,'0'),LPAD(TO_CHAR(V_NRSEQLANC-1),5,'0'));
                UPDATE LANCCCUSTO
                   SET VRLANCCUST =  VRLANCCUST + (CASE WHEN VRLANCCUST < 0 THEN (ROUND(V_VRDIFERENCA,2)) ELSE ABS(ROUND(V_VRDIFERENCA,2))END)
                 WHERE CDEMPRESA   = C_TRANSF.CDEMPRESA
                   AND NRLANCCONT  = V_NRLANCCONT
                   AND NRSEQLANC   IN (LPAD(TO_CHAR(V_NRSEQLANC),5,'0'),LPAD(TO_CHAR(V_NRSEQLANC-1),5,'0'));

             END IF;
          END IF;
      END LOOP;
      UPDATE CUSTOABSORCAOPR
         SET CDEMPRESA = V_CDEMPRESA,
             NRLANCCONTTRANSFGGF = V_NRLANCCONT
       WHERE CDFILIAL = P_CDFILIAL
         AND DTCUSTOABSORCAO = LAST_DAY(P_DATACUSTO);
   END IF;
END;
/