CREATE OR REPLACE PROCEDURE APURAC_FECHM(
   P_FILIAL    IN FILIAL.CDFILIAL%TYPE,
   P_FILIPROC  IN NOTAFISCAL.CDFILIAL%TYPE,
   P_LANCTONF  IN NOTAFISCAL.NRLANCTONF%TYPE,
   P_CDCIA     IN COMPANHIA.CDCIA%TYPE,
   P_DTFECHAME IN DATE,
   P_DTINIAPUR IN DATE,
   P_DTFIMAPUR IN DATE)
AS
   V_VRNOTA          NOTAFISCAL.VRNOTAFISC%TYPE;
   V_DSDELIVNF       VARCHAR2(120);
   V_DSDELIVNFLOCAL  VARCHAR2(120);
   V_NRDELEMISS2      VARCHAR2(20);
   V_NRDELEMISS      NUMBER;
   V_NRDADOADIC      NUMBER;
   V_TESTE           NUMBER;
   V_CDPRODEMIS      ITEMPRODUTO.CDPRODEMIS%TYPE;
   V_IDNACCIA        COMPANHIA.IDNACCIA%TYPE;
   V_CDTIPEMNF       CIAFILI.CDTIPEMNF%TYPE;
   V_NRVOOAP         TRAB_APURACM.NRVOOAP%TYPE;
   V_QUANTIDADE      ITEMPRODUTO.QTITEMPROD%TYPE;
   V_VRPESOBRUTO     ITEMPRODUTO.QTITEMPROD%TYPE;
   V_VRLIQUIDO       ITEMPRODUTO.QTITEMPROD%TYPE;
   V_VRENMOEDESTR    ITEMENCARGO.VRENMOEDESTR%TYPE;
   V_VRITMOEDESTR    ITEMPRODUTO.VRITMOEDESTR%TYPE;
   V_VRACUMRECE      CONTARECEBER.VRORIGREC%TYPE;
   V_VRRECMOEDEST    CONTARECEBER.VRRECMOEDEST%TYPE;
   V_NRMAXSEQREC     CONTARECEBER.NRSEQVENREC%TYPE;
   V_SGEQUIP         EQUIPAMENTO.SGEQUIP%TYPE;
   V_IDUTSEQNUMRE    PARAMCOMIS.IDUTSEQNUMRE%TYPE;
   V_CDMODDOCFISC    VARCHAR2(02);
BEGIN
   V_CDMODDOCFISC := NULL;
   
   SELECT NVL(MAX(F.IDTIPEMNF),SUBSTR(MAX(C.CDTIPEMNF),3,4)), NVL(MAX(CO.IDNACCIA),'1')
   INTO V_CDTIPEMNF, V_IDNACCIA
   FROM CIAFILI C, COMPANHIA CO, FORMEMISNF F
   WHERE (C.CDFILIAL  = P_FILIAL) AND
         (C.CDCIA     = P_CDCIA)  AND
         (CO.CDCIA    = P_CDCIA)  AND
         (C.CDTIPEMNF = F.CDTIPEMNF (+));
   SELECT NVL(IDUTSEQNUMRE, 'N')
   INTO V_IDUTSEQNUMRE
   FROM PARAMCOMIS
   WHERE
      (CDFILIAL = P_FILIAL);
   INSERT INTO ITEMENCARGO
   (CDFILIAL, NRLANCTONF, CDENCARGO, VRITEMENCA)
   (SELECT P_FILIPROC, P_LANCTONF, CDENCARGO, 0 AS VRITEMENCA
    FROM ENCARGO
    MINUS
    SELECT CDFILIAL, NRLANCTONF, CDENCARGO, 0 AS VRITEMENCA
    FROM ITEMENCARGO
    WHERE CDFILIAL   = P_FILIPROC AND
          NRLANCTONF = P_LANCTONF);
   FOR CURS_APNF IN (SELECT * FROM NOTAFISCAL
                     WHERE CDFILIAL   = P_FILIPROC AND
                           NRLANCTONF = P_LANCTONF) LOOP
-- =============================================================================
-- CONSULTA OS IMPOSTOS DOS ITENS DA NOTA.
-- =============================================================================
      IMPOSTO_SAIDA(CURS_APNF.CDEMPRESA, CURS_APNF.CDINSCESTA,
                    CURS_APNF.CDTIPOOPER, CURS_APNF.CDCLIENTE,
                    CURS_APNF.CDFILIAL, CURS_APNF.NRLANCTONF,
                    CURS_APNF.DTENTRSAID, V_VRNOTA, NULL);


      /*------------------------------------------------------ 
         Busca o Modelo da Nota Fiscal
         O modelo selecionado sera o Modelo de menor codigo
         cadastrado para o tipo de operacao da NF, na tela
         "Modelo de Documento Fiscal", no sistema "Fiscal"
      ------------------------------------------------------*/              
      SELECT MIN(CDMODDOCFISC) CDMODDOCFISC
      INTO V_CDMODDOCFISC
      FROM MODDOCFISCTPOP
      WHERE CDTIPOOPER   = CURS_APNF.CDTIPOOPER
      AND IDENTRSAIDOP = 'S';   
      
      --cadastra o modelo buscado na NF 
      UPDATE NOTAFISCAL
      SET CDMODDOCFISC = V_CDMODDOCFISC
      WHERE CDFILIAL   = P_FILIPROC AND
      NRLANCTONF       = P_LANCTONF;
      ---------------------------------------------------------
   
      SELECT NVL(SUM(VRENMOEDESTR),0)
      INTO V_VRENMOEDESTR
      FROM ITEMENCARGO
      WHERE CDFILIAL   = P_FILIPROC AND
            NRLANCTONF = P_LANCTONF AND
            CDENCARGO BETWEEN '06' AND '15';
      SELECT NVL(SUM(VRITMOEDESTR),0)
      INTO V_VRITMOEDESTR
      FROM ITEMPRODUTO
      WHERE CDFILIAL   = P_FILIPROC AND
            NRLANCTONF = P_LANCTONF;
      IF (V_VRITMOEDESTR - V_VRENMOEDESTR) > 0 THEN
         UPDATE NOTAFISCAL
         SET VRTTMOEDESTR = (V_VRITMOEDESTR - V_VRENMOEDESTR)
         WHERE CDFILIAL   = P_FILIPROC AND
               NRLANCTONF = P_LANCTONF;
      END IF;
      SELECT SUBSTR(MAX(CDPRODEMIS),1,1)
      INTO V_CDPRODEMIS
      FROM ITEMPRODUTO
      WHERE CDFILIAL   = P_FILIPROC AND
            NRLANCTONF = P_LANCTONF;

/*      IF (V_IDNACCIA = '2') AND (V_CDPRODEMIS = '1') THEN
         UPDATE ITEMPRODUTO
         SET CDFISOPEITEM = '5' || SUBSTR(CDFISOPEITEM,2,2)
         WHERE CDFILIAL   = P_FILIPROC AND
               NRLANCTONF = P_LANCTONF;
         UPDATE ITEMPROIMPOS
         SET CDFISCOPERIT = '5' || SUBSTR(CDFISCOPERIT,2,2)
         WHERE CDFILIAL   = P_FILIPROC AND
               NRLANCTONF = P_LANCTONF;
         UPDATE IMPOSTONF
         SET CDFISCOPERNF = '5' || SUBSTR(CDFISCOPERNF,2,2)
         WHERE CDFILIAL   = P_FILIPROC AND
               NRLANCTONF = P_LANCTONF;
         UPDATE NOTAFISCAL
         SET CDFISOPENTF = '5' || SUBSTR(CDFISOPENTF,2,2)
         WHERE CDFILIAL   = P_FILIPROC AND
               NRLANCTONF = P_LANCTONF;
      END IF;*/

      UPDATE ITEMPRODUTO
      SET CDPRODEMIS   = SUBSTR(CDPRODEMIS,2,19)
      WHERE CDFILIAL   = P_FILIPROC AND
            NRLANCTONF = P_LANCTONF;
-- =============================================================================
-- VENCIMENTO DA NOTA FISCAL
-- =============================================================================
      APURAC_VENC(CURS_APNF.CDFILIAL, CURS_APNF.CDCLIENTE, CURS_APNF.NRLANCTONF);
      V_VRACUMRECE := 0;
      SELECT MAX(NRSEQVENREC)
      INTO V_NRMAXSEQREC
      FROM CONTARECEBER C
      WHERE C.CDFILIAL   = P_FILIPROC   AND
            C.NRLANCTONF = P_LANCTONF;
      FOR C_CNTREC IN (SELECT NVL(N.VRNOTAFISC,0) AS VRNOTAFISC, N.VRTTMOEDESTR, C.NRSEQVENREC, C.VRORIGREC
                       FROM NOTAFISCAL N, CONTARECEBER C
                       WHERE N.CDFILIAL   = P_FILIPROC AND
                             N.NRLANCTONF = P_LANCTONF AND
                             C.CDFILIAL   = P_FILIPROC AND
                             C.NRLANCTONF = P_LANCTONF
                       ORDER BY NRSEQVENREC) LOOP
         IF C_CNTREC.VRNOTAFISC <> 0 THEN
            V_VRRECMOEDEST := ROUND((C_CNTREC.VRORIGREC/C_CNTREC.VRNOTAFISC) * C_CNTREC.VRTTMOEDESTR,2);
            V_VRACUMRECE   := V_VRACUMRECE + V_VRRECMOEDEST;
            IF C_CNTREC.NRSEQVENREC = V_NRMAXSEQREC THEN
               IF ROUND(V_VRACUMRECE,2) <> C_CNTREC.VRTTMOEDESTR THEN
                  V_VRRECMOEDEST := V_VRRECMOEDEST + (C_CNTREC.VRTTMOEDESTR - ROUND(V_VRACUMRECE,2));
               END IF;
            END IF;
         ELSE
            V_VRRECMOEDEST := 0;
         END IF;
         UPDATE CONTARECEBER
         SET VRRECMOEDEST  = V_VRRECMOEDEST
         WHERE CDFILIAL    = P_FILIPROC AND
               NRLANCTONF  = P_LANCTONF AND
               NRSEQVENREC = C_CNTREC.NRSEQVENREC;
      END LOOP;
-- =============================================================================
-- DADOS ADICIONAIS - DELIVERYS DAS NOTAS FISCAIS
-- =============================================================================
      SELECT MAX(NRDADOADIC) + 1 INTO V_NRDADOADIC
      FROM DADOSADIC
      WHERE (CDFILIAL   = CURS_APNF.CDFILIAL) AND
            (NRLANCTONF = CURS_APNF.NRLANCTONF);
      IF (V_NRDADOADIC IS NULL) THEN
         V_NRDADOADIC := 1;
      END IF;
      V_DSDELIVNF := 'DELIV.: ';
      FOR CURS_DELIVNF IN
            (SELECT DISTINCT NRDELIVERYAP, CDLOCALDELIVAP, LPAD(NRDELEMISSAP, 7, '0') NRDELEMISSAP
             FROM TRAB_APURACM
             WHERE (CDCIAAP      = P_CDCIA)    AND
                   (CDFILIALAP   = P_FILIPROC) AND
                   (NRLANCTONFAP = P_LANCTONF)) LOOP
         IF LENGTH(V_DSDELIVNF) > 111 THEN
            /* 111 representa 120 (tamanho mximo do campo) - 7 (tamanho do
               proximo numero de delivery) - 2 (', ') */
            INSERT INTO DADOSADIC (CDFILIAL, NRDADOADIC, CDMENSAGEM,
                                   DSDADOADIC, NRLANCTONF)
            VALUES (CURS_APNF.CDFILIAL, LPAD(TO_CHAR(V_NRDADOADIC),3,'0'), NULL,
                    V_DSDELIVNF, CURS_APNF.NRLANCTONF);
            V_DSDELIVNF := 'DELIV.: ';
            V_NRDADOADIC := V_NRDADOADIC + 1;
         END IF;         
         IF V_IDUTSEQNUMRE = 'S' THEN
            V_DSDELIVNFLOCAL  :=  V_DSDELIVNF || CURS_DELIVNF.CDLOCALDELIVAP; 
            V_NRDELEMISS  := CURS_DELIVNF.NRDELEMISSAP;
            V_NRDELEMISS2 :=  TO_CHAR(V_NRDELEMISS );
            V_DSDELIVNF := V_DSDELIVNFLOCAL||' - '||V_NRDELEMISS2 || ', ';
         ELSE
            V_DSDELIVNF := V_DSDELIVNF || TO_CHAR(CURS_DELIVNF.NRDELIVERYAP) || ', ';
         END IF;
      END LOOP;
      INSERT INTO DADOSADIC (CDFILIAL, NRDADOADIC, CDMENSAGEM,
                             DSDADOADIC, NRLANCTONF)
      VALUES (CURS_APNF.CDFILIAL, LPAD(TO_CHAR(V_NRDADOADIC),3,'0'), NULL,
              V_DSDELIVNF, CURS_APNF.NRLANCTONF);
      V_NRDADOADIC := V_NRDADOADIC + 1;
      V_DSDELIVNF := 'DATA DELIV.: ';
      FOR CURS_DELIVNF IN
            (SELECT DISTINCT DTEMISDELAP FROM TRAB_APURACM
             WHERE (CDCIAAP      = P_CDCIA)    AND
                   (CDFILIALAP   = P_FILIPROC) AND
                   (NRLANCTONFAP = P_LANCTONF) AND
                   (DTEMISDELAP IS NOT NULL)) LOOP
         IF LENGTH(V_DSDELIVNF) > 108 THEN
            /* 108 representa 120 (tamanho mximo do campo) - 10 (tamanho do
               proximo numero de data) - 2 (', ') */
            INSERT INTO DADOSADIC (CDFILIAL, NRDADOADIC, CDMENSAGEM,
                                   DSDADOADIC, NRLANCTONF)
            VALUES (CURS_APNF.CDFILIAL, LPAD(TO_CHAR(V_NRDADOADIC),3,'0'), NULL,
                    V_DSDELIVNF, CURS_APNF.NRLANCTONF);
            V_DSDELIVNF := 'DATA DELIV.: ';
            V_NRDADOADIC := V_NRDADOADIC + 1;
         END IF;
         V_DSDELIVNF := V_DSDELIVNF || TO_CHAR(CURS_DELIVNF.DTEMISDELAP,'DD/MM/YYYY') || ', ';
      END LOOP;
      INSERT INTO DADOSADIC (CDFILIAL, NRDADOADIC, CDMENSAGEM,
                             DSDADOADIC, NRLANCTONF)
      VALUES (CURS_APNF.CDFILIAL, LPAD(TO_CHAR(V_NRDADOADIC),3,'0'), NULL,
              V_DSDELIVNF, CURS_APNF.NRLANCTONF);
      V_NRDADOADIC := V_NRDADOADIC + 1;
      V_DSDELIVNF := 'VOO:';
      FOR CURS_DELIVNF IN
            (SELECT DISTINCT NRVOOAP FROM TRAB_APURACM
             WHERE (CDCIAAP      = P_CDCIA)    AND
                   (CDFILIALAP   = P_FILIPROC) AND
                   (NRLANCTONFAP = P_LANCTONF)) LOOP
         IF LENGTH(V_DSDELIVNF) > 113 THEN
            /* 111 representa 120 (tamanho mximo do campo) - 5 (tamanho do
               proximo numero de voo) - 2 (', ') */
            INSERT INTO DADOSADIC (CDFILIAL, NRDADOADIC, CDMENSAGEM,
                                   DSDADOADIC, NRLANCTONF)
            VALUES (CURS_APNF.CDFILIAL, LPAD(TO_CHAR(V_NRDADOADIC),3,'0'), NULL,
                    V_DSDELIVNF, CURS_APNF.NRLANCTONF);
            V_DSDELIVNF := 'VOO:';
            V_NRDADOADIC := V_NRDADOADIC + 1;
         END IF;
         V_DSDELIVNF := V_DSDELIVNF || CURS_DELIVNF.NRVOOAP || ', ';
      END LOOP;
      INSERT INTO DADOSADIC (CDFILIAL, NRDADOADIC, CDMENSAGEM,
                             DSDADOADIC, NRLANCTONF)
      VALUES (CURS_APNF.CDFILIAL, LPAD(TO_CHAR(V_NRDADOADIC),3,'0'), NULL,
              V_DSDELIVNF, CURS_APNF.NRLANCTONF);
      IF (V_CDTIPEMNF = '07') OR (V_CDTIPEMNF = '08') OR (V_CDTIPEMNF = '09') THEN
         V_NRDADOADIC := V_NRDADOADIC + 1;
         V_DSDELIVNF := 'EQUIP.: ';
         FOR CURS_DELIVNF IN
               (SELECT DISTINCT CDEQUIPAP
                FROM TRAB_APURACM
                WHERE (CDCIAAP      = P_CDCIA)    AND
                      (CDFILIALAP   = P_FILIPROC) AND
                      (NRLANCTONFAP = P_LANCTONF)) LOOP
            IF LENGTH(V_DSDELIVNF) > 108 THEN
               /* 108 representa 120 (tamanho mximo do campo) - 10 (tamanho do
                  proximo numero de delivery) - 2 (', ') */
               INSERT INTO DADOSADIC (CDFILIAL, NRDADOADIC, CDMENSAGEM,
                                      DSDADOADIC, NRLANCTONF)
               VALUES (CURS_APNF.CDFILIAL, LPAD(TO_CHAR(V_NRDADOADIC),3,'0'), NULL,
                       V_DSDELIVNF, CURS_APNF.NRLANCTONF);
               V_DSDELIVNF := 'EQUIP.: ';
               V_NRDADOADIC := V_NRDADOADIC + 1;
            END IF;
            SELECT MAX(SGEQUIP)
            INTO V_SGEQUIP
            FROM EQUIPAMENTO
            WHERE CDEQUIP = CURS_DELIVNF.CDEQUIPAP;
            V_DSDELIVNF := V_DSDELIVNF || V_SGEQUIP || ', ';
         END LOOP;
         INSERT INTO DADOSADIC (CDFILIAL, NRDADOADIC, CDMENSAGEM,
                                DSDADOADIC, NRLANCTONF)
         VALUES (CURS_APNF.CDFILIAL, LPAD(TO_CHAR(V_NRDADOADIC),3,'0'), NULL,
                 V_DSDELIVNF, CURS_APNF.NRLANCTONF);
      END IF;
   END LOOP;
END;
/