create or replace PROCEDURE CANCELA_CUSTO_ABSORCAO
   (P_CDFILIAL     IN FILIAL.CDFILIAL%TYPE,
    P_DATACUSTO    IN FILIAL.DTULTATU%TYPE,
    P_ERRO         OUT VARCHAR)
AS
   V_COUNT               INTEGER;
   V_DTINICCONT          DATE;
   V_DTFINACONT          DATE;
   V_NRLANCCONTGGF       VARCHAR2(20);
   V_NRLANCCONTTRANSF    VARCHAR2(20);
   V_CDEMPRESA           VARCHAR2(02);
BEGIN
   P_ERRO := 'N';

   /* 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_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
         BEGIN
            SELECT MAX(CDEMPRESA), MAX(NRLANCCONTGGF), MAX(NRLANCCONTTRANSFGGF)
              INTO V_CDEMPRESA, V_NRLANCCONTGGF, V_NRLANCCONTTRANSF
              FROM CUSTOABSORCAOPR
             WHERE CDFILIAL = P_CDFILIAL
               AND DTCUSTOABSORCAO = LAST_DAY(P_DATACUSTO);
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_CDEMPRESA := NULL;
               V_NRLANCCONTGGF := NULL;
               V_NRLANCCONTTRANSF := NULL;
         END;
         DELETE FROM LANCCONT
          WHERE CDEMPRESA = V_CDEMPRESA
            AND NRLANCCONT = V_NRLANCCONTGGF;
         DELETE FROM LANCCONT
          WHERE CDEMPRESA = V_CDEMPRESA
            AND NRLANCCONT = V_NRLANCCONTTRANSF;

         DELETE FROM CUSTOABSORCAOPR
          WHERE CDFILIAL = P_CDFILIAL
            AND DTCUSTOABSORCAO = LAST_DAY(P_DATACUSTO);

         FOR C_PRODACABADO IN (SELECT P.CDFILIAL, P.NRLANCESTQ, P.NRSEQUITEM, I.CDFILIMOVI, I.DTLANCMOVI
                                 FROM ORDPRODUCAO O, PRODACABADO P, ITLANCTOEST I
                                WHERE O.CDFILIAL = P_CDFILIAL
                                  AND O.IDSITORDPROD = '3'
                                  AND TO_DATE(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 I.CDFILIAL = P.CDFILIAL
                                  AND I.NRLANCESTQ = P.NRLANCESTQ
                                  AND I.NRSEQUITEM = P.NRSEQUITEM
                                  AND I.VRUNILANCTOANT > 0
                                ORDER BY I.DTLANCMOVI)
         LOOP
            UPDATE ITLANCTOEST
               SET VRUNILANCTO  = ROUND(VRUNILANCTOANT, 5),
                   VRLANCTOEST  = ROUND(VRUNILANCTOANT, 5),
                   VRLANCTOBRUT = ROUND(VRUNILANCTOANT, 5)
             WHERE CDFILIAL   = C_PRODACABADO.CDFILIAL
               AND NRLANCESTQ = C_PRODACABADO.NRLANCESTQ
               AND NRSEQUITEM = C_PRODACABADO.NRSEQUITEM;

            UPDATE ITLANCTOEST
               SET VRTOTLANCTO  = ROUND((QTTOTLANCTO * VRUNILANCTO), 5),
                   VRCUSTENTR   = ROUND((QTTOTLANCTO * VRUNILANCTO), 5),
                   VRUNILANCTOANT = 0
             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;
      ELSE
         P_ERRO := 'S';
      END IF;
   ELSE
      P_ERRO := 'S';
   END IF;
END;
/