CREATE OR REPLACE PROCEDURE CTB_DFE_FORNECEDOR(
   P_NRLANCTBTEMP LANCCONT.NRLANCCONT%TYPE,
   P_CDFILIAL FILIAL.CDFILIAL%TYPE,
   P_NRLANCTONF NOTAFISCAL.NRLANCTONF%TYPE,
   P_FATOR INT,
   P_VRLANCTO IN OUT LANCCONT.VRLANCAMENT%TYPE,
   P_VRINDUSTRIA OUT LANCCONT.VRLANCAMENT%TYPE)
AS
   V_CDCONTACR CONTCTBL.CDCONTCTBL%TYPE;
   V_CDCONTAP CONTCTBL.CDCONTCTBL%TYPE;
   V_LIXO VARCHAR2(50);
   V_VRLANCAMENTO LANCCONT.VRLANCAMENT%TYPE;
   V_CDHISTPADR LANCCONT.CDHISTPADR%TYPE;
   V_DSHISTLANC LANCCONT.DSHISTLANC%TYPE;
   V_IDCOME VARCHAR2(01);
   V_IDCCUS VARCHAR2(01);
   V_CDCOMERCIAL FORNECEDOR.CDFORNECED%TYPE;
   V_CONTCCUS VARCHAR2(01);
   V_CDCENTCUST CENTCUST.CDCENTCUST%TYPE;
   V_CDFILIAL FILIAL.CDFILIAL%TYPE;
   V_VRINDUSTRIA LANCCONT.VRLANCAMENT%TYPE;

BEGIN
   FOR NOTA IN
      (SELECT NF.IDORIGDESTNF, NF.IDENTRSAIDOP, NF.CDTIPOOPER, NF.CDFILIRECE,
             NF.CDFORNECED, NF.CDCLIENTE, NF.CDFILIEMIT, NF.VRNOTAFISC,
             NF.NRNOTAFISC, NF.DTENTRSAID, NF.CDEMPRESA, T.IDNFRETIND,
             T.IDOPERDEVONF
        FROM NOTAFISCAL NF, TIPOOPERAC T
       WHERE NF.NRLANCTONF = P_NRLANCTONF
         AND NF.CDFILIAL = P_CDFILIAL
         AND NF.IDENTRSAIDOP = T.IDENTRSAIDOP
         AND NF.CDTIPOOPER = T.CDTIPOOPER)LOOP

      BEGIN
         SELECT CDCONTFORCLI, CDCONTPROD
           INTO V_CDCONTACR, V_CDCONTAP
           FROM TIPOOPERAC
          WHERE CDTIPOOPER = NOTA.CDTIPOOPER
            AND IDENTRSAIDOP = NOTA.IDENTRSAIDOP;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_CDCONTACR := NULL;
            V_CDCONTAP := NULL;
      END;

      BEGIN
         SELECT IDCONTGERE
           INTO V_CONTCCUS
           FROM CONTOPECTBL
          WHERE CDEMPRESA = NOTA.CDEMPRESA;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_CONTCCUS := 'N';
      END;

      IF NOTA.IDENTRSAIDOP = 'E' THEN
         V_CDFILIAL := NOTA.CDFILIRECE;
      ELSE
         V_CDFILIAL := NOTA.CDFILIEMIT;
      END IF;
      IF NOTA.IDORIGDESTNF = 'F' THEN /*FORNECEDOR*/
         IF V_CDCONTACR IS NULL THEN
            V_CDCONTACR := CTB_CCFORNECEDOR(NOTA.CDFORNECED, NOTA.CDFILIRECE, NOTA.CDEMPRESA);
         END IF;
      ELSIF NOTA.IDORIGDESTNF = 'C' THEN /*CLIENTE*/
         IF V_CDCONTACR IS NULL THEN
            V_CDCONTACR := CTB_CCCLIENTE(NOTA.CDCLIENTE, NOTA.CDFILIEMIT, NOTA.CDEMPRESA);
         END IF;
      ELSE 
         CONTCTBL_FILIAL(NOTA.CDEMPRESA, NOTA.CDTIPOOPER, NOTA.IDENTRSAIDOP, NOTA.CDFILIRECE, V_CDCONTACR, V_LIXO, V_LIXO, V_LIXO);
      END IF;

      V_VRLANCAMENTO := NOTA.VRNOTAFISC;
      FOR IMPOSTO_RETIDO IN
         (SELECT NVL(ROUND(SUM(VRIMPOSTONF),2), 0) AS VRIMPRETIDO
           FROM IMPOSTONF INF, IMPOSTO I
          WHERE CDFILIAL      = P_CDFILIAL   AND
               NRLANCTONF     = P_NRLANCTONF AND
               INF.CDIMPOSTO  = I.CDIMPOSTO AND
               I.IDTRATIMPO   = 'R'         AND
               I.IDTIPCONTAB  = 'D'         AND
               (((IDIMPORECUP = 'S') AND ((CDCCTBLATIV IS NULL) OR (CDFISCALNF <> '1'))) OR
                (IDIMPORECUP <> 'S')))LOOP
         V_VRLANCAMENTO := (NOTA.VRNOTAFISC - IMPOSTO_RETIDO.VRIMPRETIDO) * P_FATOR;
      END LOOP;
      V_VRINDUSTRIA := 0;
      IF ((NOTA.IDNFRETIND = 'S') AND (NOTA.IDOPERDEVONF = 'S')) THEN
         FOR ITEMINDUSTRIA IN
            (SELECT ROUND(I.VRCONTITEM,2) AS VRCONTITEM
               FROM ITEMPRODUTO I, PRODUTO P
              WHERE (I.CDFILIAL     = P_CDFILIAL)
                AND (I.NRLANCTONF   = P_NRLANCTONF)
                AND (I.VRCONTITEM   > 0)
                AND (I.CDPRODUTO = P.CDPRODUTO)
                AND (I.IDITEMORIGEM = 'S')
              ORDER BY I.NRSEQITPROD) LOOP
            V_VRINDUSTRIA := V_VRINDUSTRIA + ITEMINDUSTRIA.VRCONTITEM;
            V_VRLANCAMENTO := V_VRLANCAMENTO - ITEMINDUSTRIA.VRCONTITEM;
         END LOOP;
         V_VRINDUSTRIA := ROUND(V_VRINDUSTRIA, 2);
      END IF;
      FOR MOD_DOC_FISCAL IN
         (SELECT 1
            FROM NOTAFISCAL N, MODDOCFISC A
           WHERE N.CDMODDOCFISC = A.CDMODDOCFISC
             AND A.CDTPREGFISC  = '70'
             AND N.CDFILIAL     = P_CDFILIAL
             AND N.NRLANCTONF   = P_NRLANCTONF)LOOP
         FOR SUBST_TRIB IN
            (SELECT S.CDIMPOSTRIB, I.VRTOTIMPONF
               FROM IMPOSNFTOT I, (SELECT CDIMPOSTRIB
                                     FROM IMPOSTO
                                    WHERE CDIMPOSTRIB IS NOT NULL) S
              WHERE I.CDFILIAL   = P_CDFILIAL AND
                    I.NRLANCTONF = P_NRLANCTONF and
                    I.CDIMPOSTO  = S.CDIMPOSTRIB)LOOP
            V_VRLANCAMENTO := NOTA.VRNOTAFISC - SUBST_TRIB.VRTOTIMPONF;
         END LOOP;
      END LOOP;

      IF V_VRLANCAMENTO <> 0 THEN
         CTB_DFE_HISTORICO(P_CDFILIAL, P_NRLANCTONF, V_CDHISTPADR, V_DSHISTLANC);

         BUSCA_TPCOMERCIAL(V_CDCONTACR, V_IDCOME, V_IDCCUS);

         BEGIN
            SELECT DECODE(NOTA.IDORIGDESTNF, 'F', NOTA.CDFORNECED, DECODE(NOTA.IDORIGDESTNF, 'C', NOTA.CDCLIENTE, NULL))
              INTO V_CDCOMERCIAL
              FROM DUAL;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_CDCOMERCIAL := NULL;
         END;

         IF NVL(V_IDCOME, 'N') = 'N' THEN
            V_CDCOMERCIAL := NULL;
         END IF;

         INSERT INTO LANCCTBTEMP
            (CDEMPRESA, NRLANCCTBTEMP, CDDOCUTEMP, NRSEQCTBTEMP, CDCONTCTBL, CDHISTPADR,
             DSHISTCTB, DTLANCCTBTEMP, VRLANCCTBTEMP, IDLANCCTBCOME, CDLANCCTBCOME, IDSISTEMA)
         VALUES
            (NOTA.CDEMPRESA, P_NRLANCTBTEMP, NOTA.NRNOTAFISC, '00001', V_CDCONTACR, V_CDHISTPADR,
             V_DSHISTLANC, NOTA.DTENTRSAID, V_VRLANCAMENTO, V_IDCOME, V_CDCOMERCIAL, 'DFE');
         IF V_CONTCCUS <> 'N' THEN
            IF V_IDCCUS <> 'O' THEN
               V_CDCENTCUST := BUSCA_CCUSTO(V_CDFILIAL, NOTA.CDEMPRESA);
               INSERT INTO RATECTBTEMP
                  (CDEMPRESA, NRLANCCTBTEMP, NRSEQCTBTEMP, CDFILIAL, CDCENTCUST, VRRATECTBTEMP, DTCOMPCTBTEMP, NRSEQRATTEMP)
               VALUES
                  (NOTA.CDEMPRESA, P_NRLANCTBTEMP, '00001', NOTA.CDFILIRECE, V_CDCENTCUST, V_VRLANCAMENTO, NOTA.DTENTRSAID, '00001');
            END IF;
         END IF;
      END IF;
   END LOOP;
   P_VRLANCTO := V_VRLANCAMENTO;
   P_VRINDUSTRIA := NVL(V_VRINDUSTRIA, 0);
END;
/


