IF exists (select 1 from sys.procedures where name ='P_V_SPED_EST_NF_ENT_DANFE') 
    BEGIN 
	   DROP PROCEDURE P_V_SPED_EST_NF_ENT_DANFE
    END   --  EXEC P_V_SPED_EST_NF_ENT_DANFE '20220901','20220901','1'
GO

/*
=======================================================================================
'Nível Classificação.: INFORMAÇÃO RESTRITA
'Versão..............: 20230215
'Job.................:  
'Nome................: [P_V_SPED_EST_NF_ENT_DANFE] 
'Objetivo............: Pesquisar Notas Fiscais de Entrada com base nos dados do Danfe
'Tabelas.............:  
'Dt Criação..........: 2023-02-15
'Dt Ult Alteracão....: 
'Alterado por........:  
'Criado por..........: SHIRLENE PRADO
'=======================================================================================
FARMAGOI-34446
*/
CREATE PROCEDURE P_V_SPED_EST_NF_ENT_DANFE (@DT_INI DATETIME,@DT_FIM DATETIME ,@CD_FILIAL VARCHAR (MAX) )
AS 
SET NOCOUNT ON
    CREATE TABLE #V_SPED_NF_ENT(  
     CD_EMP INT ,  
     CD_FILIAL INT ,  
     CD_DOC INT ,  
     IND_OPER INT ,  
     IND_EMIT INT ,  
     CD_ORIG INT NULL,  
     COD_MOD VARCHAR(30) ,  
     TP_SR_NFE INT ,  
     SERIE VARCHAR(100) ,  
     NR_DOC INT ,  
     CHAVE_NFE VARCHAR(150) ,  
     DT_EMI_NF DATETIME NULL,  
     DT_ENT_SAI DATETIME NULL,  
     VLR_NF MONEY ,  
     IND_PAGTO INT ,  
     VLR_DESC MONEY ,  
     VLR_ABAT INT ,  
     VLR_MERC MONEY NULL,  
     TP_FRETE INT ,  
     VLR_FRETE MONEY ,  
     VLR_SEG INT ,  
     VLR_OUTRAS_DESP_CAB MONEY ,  
     VLR_BASE_ICMS_CAB MONEY NULL,  
     VLR_ICMS_CAB MONEY NULL,  
     VLR_BASE_ICMS_SUBS_CAB MONEY ,  
     VLR_ICMS_SUBS_CAB MONEY ,  
     VLR_IPI_CAB MONEY ,  
     VLR_ISENTAS_CAB MONEY ,  
     CGC_CPF VARCHAR(30) ,  
     STS_NF INT ,  
     VLR_SERV_NT MONEY ,  
     VLR_BASE_ISSQN_CAB INT ,  
     VLR_ISSQN_CAB INT ,  
     VLR_BASE_IRRF_CAB INT ,  
     VLR_IRRF_CAB INT ,  
     CD_TRANSP INT ,  
     QT_VOL MONEY ,  
     PESO_BRT MONEY ,  
     PESO_LIQ MONEY ,  
     POSSUI_DUPL INT ,  
     TP_MOV INT ,  
     TP_MOV_OP_TRANSA INT ,  
     DEST_ORIG INT ,  
     TIPO_PART VARCHAR(30) ,  
     NUM_ITEM INT ,  
     COD_ITEM INT ,  
     TIPO_ITEM INT ,  
     DS_PROD VARCHAR(150) NULL,  
     QT_IT MONEY ,  
     VLR_IT MONEY ,  
     VLR_ITEM MONEY NULL,  
     CST_ICMS VARCHAR(80) ,  
     TRIB_FC_ELETRONICO VARCHAR(100) ,  
     CFOP VARCHAR(6) NULL,  
     VLR_BASE_ICMS MONEY ,  
     ALIQ_ICMS MONEY ,  
     VLR_ICMS_IT MONEY ,  
     VLR_BASE_SUBS MONEY ,  
     VLR_ICMS_SUBS MONEY ,  
     VLR_IPI_IT MONEY ,  
     PERC_IPI MONEY ,  
     VLR_ISENTO_IT MONEY ,  
     CST_PIS INT ,  
     CST_COFINS INT ,  
     VLR_BASE_PIS MONEY ,  
     PERC_ALIQ_PIS MONEY ,  
     VLR_PIS MONEY ,  
     VLR_BASE_COFINS MONEY ,  
     PERC_ALIQ_COFINS MONEY ,  
     VLR_COFINS MONEY ,  
     VLR_OUTRAS_DESPESAS_IT MONEY NULL,  
     VLR_BASE_ISSQN INT ,  
     VLR_ISSQN INT ,  
     ALIQ_ISSQN INT ,  
     COD_PART INT NULL,  
     COD_IMP_ALMOX INT ,  
     LISTA_PROD INT NULL,  
     SUBS_TRIB INT ,  
     SUBS_TRIB_ANT INT ,  
     CD_NT_OP INT ,  
     ICMS INT ,  
     ICMS_RD INT ,  
     FLAG_OPERADOR_LOG INT ,  
     QTDE_PROD MONEY ,  
     NR_LOTE NVARCHAR(100) ,  
     DT_VALID DATETIME ,  
     DT_FABR DATETIME ,  
     VLR_TABELA MONEY NULL,  
     PROD_MEDICAM INT ,  
     TIPO_MEDIC INT ,  
     NOME_PART VARCHAR(150) ,  
     ENDERECO VARCHAR(150) ,  
     BAIRRO VARCHAR(150) ,  
     NUMERO VARCHAR(150) ,  
     INSC_EST VARCHAR(50) ,  
     UF VARCHAR(10) ,  
     TEL VARCHAR(50) ,  
     CD_PAIS INT ,  
     COD_IBGE VARCHAR(150) ,  
     CEP VARCHAR(20) ,  
     TP_PROD INT ,  
     COD_BARRA VARCHAR(50) ,  
     COD_NCM VARCHAR(50) ,  
     VLR_DESC_IT MONEY ,  
     PIS_NF MONEY ,  
     COFINS_NF MONEY ,  
     DS_NT_OP VARCHAR(300) NULL,  
     CD_MOV_OP INT ,  
     FLAG_NAO_GERAR_ARQ_SPED INT   NULL,  
     CD_TRIB INT ,  
     CANCEL_FORA_PRAZO INT ,  
     PROTOCOLO_CANCEL VARCHAR(50)   NULL,  
     POSSUI_DARE INT ,  
     NUM_DARE VARCHAR(10)   NULL,  
     VALOR_DARE VARCHAR(10)   NULL,  
     DT_VENCTO_DARE VARCHAR(10)   NULL,  
     DT_RECOLHIMENTO_DARE VARCHAR(10)   NULL,  
     FLAG_MOV_OP_NFE_CPL INT ,  
     NOTA_DENEGADA INT ,  
     VLR_FRETE_CABE_RATEIO FLOAT NULL,  
     CD_NF_SAI INT ,  
     VLR_FECOP MONEY NULL,  
     NR_CEST VARCHAR(20) ,  
     COD_IBGE_MUNICIPIO_DESTINATARIO VARCHAR(50) ,  
     END_DESTINATARIO VARCHAR(100) ,  
     BAIRRO_DESTINATARIO VARCHAR(50) ,  
     CEP_DESTINATARIO VARCHAR(50) ,  
     CIDADE_DESTINATARIO VARCHAR(50) ,  
     CNPJ_DESTINATARIO VARCHAR(50) ,  
     INSC_ESTADUAL_DESTINATARIO VARCHAR(50) ,  
     RZ_DESTINATARIO VARCHAR(60) ,  
     UF_DESTINATARIO VARCHAR(50) ,  
     CD_CID_DESTINATARIO INT ,  
     ALIQ_ICMS_INTERNO MONEY ,  
     VLR_ICMS_DESONERADO MONEY NULL,  
     PART_FLAG_MICRO_EMPRESA INT ,  
     BASE_ICMS_SIMPLES MONEY NULL,  
     ALIQ_SIMPLES MONEY NULL,  
     VLR_ICMS_SIMPLES MONEY NULL,  
     VLR_CUSTO_MD MONEY NULL,  
     VLR_ICMS_DESONERADO_IT MONEY NULL,  
     VLR_FECOP_ST MONEY NULL,  
     VLR_FECOP_ST_RET MONEY NULL,  
     VLR_FECOP_IT MONEY NULL,  
     VLR_FECOP_ST_IT MONEY NULL,  
     VLR_FECOP_ST_RET_IT MONEY NULL,  
	 NR_SERV VARCHAR(10),   
	 FLAG_VLR_ICMS_DESONERADO_DESCONTO INT  NULL,  
	 VLR_ICMS_RET_IT   MONEY NULL                     ,
	 VLR_BASE_ICMS_RET_IT              MONEY NULL    ,
	 VLR_ICMS_OP_IT                    MONEY NULL    ,
	 ALIQ_INTERNA_PROD                 MONEY NULL    ,
	 VLR_BASE_SUBS_CONV_IT             MONEY NULL    ,
	 VLR_ICMS_SUBS_CONV_IT             MONEY NULL    ,
	 APRES_QT_CP                       MONEY NULL    ,
	 FLAG_CST_60_PARA_SPED  INT   NULL                     ,
	 INUTILIZADO   INT   NULL  ) -- TOTAL DE 156 CAMPOS  
    
-------------------------------------------------------------------------------------------------  
CREATE TABLE #FILIAIS (CD_EMP INT ,CD_FILIAL INT,FLAG_OPERADOR_LOG INT )  
    INSERT INTO #FILIAIS  
       SELECT CD_EMP,  
      CD_FILIAL,  
      FLAG_OPERADOR_LOG  
       FROM PRC_FILIAL  
       WHERE CD_FILIAL IN  
    (  
    SELECT ID  
    FROM UFD_PRM_IN_STRING(@CD_FILIAL)  
    );  
-------------------------------------------------------------------------------------------------  
  
INSERT INTO #V_SPED_NF_ENT   
  
   -- NOTAS DE ENTRADA SEM VINCULO COM DANFE   
   -- (NOT EXISTS EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT)  
   -- SEM DARE / CONSULTA 1  
    
  SELECT EST_NF_ENT.CD_EMP,  
   EST_NF_ENT.CD_FILIAL,  
   EST_NF_ENT.CD_NF AS CD_DOC,  
   0 AS IND_OPER,  
   (CASE  
    WHEN EST_NF_ENT_EF_NF_CTR_NF.CD_NF_ENT > 0  
    THEN 0  
    ELSE 1  
    END) AS IND_EMIT,  
   EST_NF_ENT.CD_FORN AS CD_ORIG,  
   LEFT(EST_NF_SERIE.MODELO,2),  
   EST_NF_SERIE.TP_SR_NFE,  
   EST_NF_SERIE.SERIE,  
   EST_NF_ENT.NR_NF AS NR_DOC,  
   ISNULL(V_EST_NF_ENT_NFE_SPED.NR_AUTORIZADOR, '') AS CHAVE_NFE,  
   EST_NF_ENT.DT_EMI_NF,  
   EST_NF_ENT.DT_ENT AS DT_ENT_SAI,  
   EST_NF_ENT.VLR_NF,  
   PGTO_VISTA_PRAZO AS IND_PAGTO,  
   EST_NF_ENT.VLR_DESC,  
   0 AS VLR_ABAT,  
   CASE  
       WHEN ISNULL(EST_NF_ENT.FLAG_NF_PROD_TOTAL_BRUTO, 0) = 0  
       THEN EST_NF_ENT.VLR_TOT_PROD  
       ELSE EST_NF_ENT.VLR_TOT_PROD - EST_NF_ENT.VLR_DESC  
   END AS VLR_MERC,  
   1 AS TP_FRETE,  
   VLR_FRETE,  
   0 AS VLR_SEG,  
   EST_NF_ENT.VLR_OUTROS AS VLR_OUTRAS_DESP_CAB,  
   V_APURA_ICMS.VLR_BASE_ICMS_CAB,  
   V_APURA_ICMS.VLR_ICMS_CAB,  
   EST_NF_ENT.VLR_BASE_ICMS_SUBS AS VLR_BASE_ICMS_SUBS_CAB,  
   EST_NF_ENT.VLR_ICMS_SUBS AS VLR_ICMS_SUBS_CAB,  
   EST_NF_ENT.VLR_IPI AS VLR_IPI_CAB,  
   EST_NF_ENT.VRL_ISENTAS AS VLR_ISENTAS_CAB,  
   '' AS CGC_CPF,  
   EST_NF_ENT.STS_NF,  
   EST_NF_ENT.VLR_OUTROS AS VLR_SERV_NT,  
   0 AS VLR_BASE_ISSQN_CAB,  
   0 VLR_ISSQN_CAB,  
   0 AS VLR_BASE_IRRF_CAB,  
   0 AS VLR_IRRF_CAB,  
   0 AS CD_TRANSP,  
   QT_VOL,  
   PESO_BRT,  
   PESO_LIQ,  
   ISNULL(  
    (  
    SELECT TOP 1 CD_EMP  
    FROM PG_CRED_EST_NF_ENT  
    WHERE EST_NF_ENT.CD_EMP = PG_CRED_EST_NF_ENT.CD_EMP  
      AND EST_NF_ENT.CD_FILIAL = PG_CRED_EST_NF_ENT.CD_FILIAL  
      AND EST_NF_ENT.CD_NF = PG_CRED_EST_NF_ENT.CD_NF  
    ), 0) AS POSSUI_DUPL,  
   GLB_MOV_OP.TP_MOV,  
   GLB_MOV_OP.TP_MOV_OP_TRANSA,  
   GLB_MOV_OP.DEST_ORIG,  
   CASE  
       WHEN GLB_MOV_OP.DEST_ORIG = 0  
       THEN 'CLI'  
       WHEN GLB_MOV_OP.DEST_ORIG = 1  
       THEN 'FOR'  
       ELSE 'FIL'  
   END AS TIPO_PART,  
  
   EST_NF_ENT_IT.SEQ_ITEM AS NUM_ITEM,  
   EST_NF_ENT_IT.CD_PROD AS COD_ITEM,  
   EST_PROD_CPL.TP_PROD AS TIPO_ITEM,  
   EST_PROD.DS_PROD, --   
   EST_NF_ENT_IT.QT_IT,  
   EST_NF_ENT_IT.VLR_IT,  
   CASE  
       WHEN ISNULL(DBO.EST_NF_ENT.FLAG_NF_PROD_TOTAL_BRUTO, 0) = 0  
       THEN DBO.EST_NF_ENT_IT.VLR_TOT_IT  
       ELSE DBO.EST_NF_ENT_IT.VLR_TOT_IT - EST_NF_ENT_IT.VLR_DESC  
   END AS VLR_ITEM,  
     
   EST_TRIB.CD_TRIB_FC AS CST_ICMS,  
   EST_TRIB.TRIB_FC_ELETRONICO,  
  
  
    GLB_NT_OP.CD_NT_OP_FC  AS CFOP,  
   CASE  
       WHEN EST_TRIB.ICMS > 0  
       THEN EST_NF_ENT_IT.VLR_BASE_ICMS  
       ELSE 0  
   END VLR_BASE_ICMS,  
   CASE  
       WHEN EST_TRIB.ICMS > 0  OR ( GLB_MOV_OP.TP_MOV IN ( 27,21) )  
       THEN EST_NF_ENT_IT.ALIQ_ICMS  
       ELSE 0  
   END ALIQ_ICMS,  
   CASE  
       WHEN EST_TRIB.ICMS > 0  
        OR GLB_MOV_OP.TP_MOV IN(58)  
       THEN EST_NF_ENT_IT.VLR_ICMS_IT  
       ELSE 0  
   END VLR_ICMS_IT,  
   EST_NF_ENT_IT.VLR_BASE_SUBS AS VLR_BASE_SUBS,  
   EST_NF_ENT_IT.VLR_ICMS_SUBS AS VLR_ICMS_SUBS,  
   EST_NF_ENT_IT.VLR_IPI AS VLR_IPI_IT,  
   EST_NF_ENT_IT.PERC_IPI,  
   EST_NF_ENT_IT.VLR_ISENTO AS VLR_ISENTO_IT,  
   ISNULL(EST_NF_ENT_IT.CST_PIS, 0) CST_PIS,  
   ISNULL(EST_NF_ENT_IT.CST_COFINS, 0) CST_COFINS,  
   ISNULL(EST_NF_ENT_IT.VLR_BASE_PIS, 0) VLR_BASE_PIS,  
   ISNULL(EST_NF_ENT_IT.PERC_ALIQ_PIS, 0) PERC_ALIQ_PIS,  
   ISNULL(EST_NF_ENT_IT.VLR_PIS, 0) VLR_PIS,  
   ISNULL(EST_NF_ENT_IT.VLR_BASE_COFINS, 0) VLR_BASE_COFINS,  
   ISNULL(EST_NF_ENT_IT.PERC_ALIQ_COFINS, 0) PERC_ALIQ_COFINS,  
   ISNULL(EST_NF_ENT_IT.VLR_COFINS, 0) VLR_COFINS,  
   EST_NF_ENT_IT.VLR_OUTRAS_DESPESAS AS VLR_OUTRAS_DESPESAS_IT,  
   0 AS VLR_BASE_ISSQN,  
   0 AS VLR_ISSQN,  
   0 AS ALIQ_ISSQN,  
   EST_NF_ENT.CD_FORN AS COD_PART,  
   1 AS COD_IMP_ALMOX,  
   EST_NF_ENT_IT.LISTA_PROD,  
   EST_TRIB.SUBS_TRIB,  
   EST_TRIB.SUBS_TRIB_ANT,  
   EST_NF_ENT_IT.CD_NT_OP,  
   EST_TRIB.ICMS,  
   EST_TRIB.ICMS_RD,  
   FLAG_OPERADOR_LOG,  
   ISNULL(  
    (  
    SELECT SUM(EST_NF_ENT_IT_EST_PROD_CPL_LOTE.QTDE_PROD) QTDE_PROD  
    FROM EST_NF_ENT_IT_EST_PROD_CPL_LOTE,  
     EST_PROD_CPL_LOTE  
    WHERE EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_EMP = EST_NF_ENT_IT.CD_EMP  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_FILIAL = EST_NF_ENT_IT.CD_FILIAL  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_NF = EST_NF_ENT_IT.CD_NF  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_PROD = EST_NF_ENT_IT.CD_PROD  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_EMP = EST_PROD_CPL_LOTE.CD_EMP  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_FILIAL = EST_PROD_CPL_LOTE.CD_FILIAL  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_LOTE = EST_PROD_CPL_LOTE.CD_LOTE  
    ), 0) AS QTDE_PROD,  
   ISNULL(  
    (  
    SELECT TOP (1) EST_PROD_CPL_LOTE.NR_LOTE  
    FROM EST_NF_ENT_IT_EST_PROD_CPL_LOTE,  
     EST_PROD_CPL_LOTE  
    WHERE EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_EMP = EST_NF_ENT_IT.CD_EMP  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_FILIAL = EST_NF_ENT_IT.CD_FILIAL  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_NF = EST_NF_ENT_IT.CD_NF  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_PROD = EST_NF_ENT_IT.CD_PROD  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_EMP = EST_PROD_CPL_LOTE.CD_EMP  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_FILIAL = EST_PROD_CPL_LOTE.CD_FILIAL  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_LOTE = EST_PROD_CPL_LOTE.CD_LOTE  
    ORDER BY EST_PROD_CPL_LOTE.CD_LOTE  
    ), 0) AS NR_LOTE,  
   ISNULL(  
    (  
    SELECT TOP (1) EST_PROD_CPL_LOTE.DT_VALID  
    FROM EST_NF_ENT_IT_EST_PROD_CPL_LOTE,  
     EST_PROD_CPL_LOTE  
    WHERE EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_EMP = EST_NF_ENT_IT.CD_EMP  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_FILIAL = EST_NF_ENT_IT.CD_FILIAL  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_NF = EST_NF_ENT_IT.CD_NF  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_PROD = EST_NF_ENT_IT.CD_PROD  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_EMP = EST_PROD_CPL_LOTE.CD_EMP  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_FILIAL = EST_PROD_CPL_LOTE.CD_FILIAL  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_LOTE = EST_PROD_CPL_LOTE.CD_LOTE  
    ), 0) AS DT_VALID,  
   ISNULL(  
    (  
    SELECT TOP (1) EST_PROD_CPL_LOTE.DT_FABR  
    FROM EST_NF_ENT_IT_EST_PROD_CPL_LOTE,  
     EST_PROD_CPL_LOTE  
    WHERE EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_EMP = EST_NF_ENT_IT.CD_EMP  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_FILIAL = EST_NF_ENT_IT.CD_FILIAL  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_NF = EST_NF_ENT_IT.CD_NF  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_PROD = EST_NF_ENT_IT.CD_PROD  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_EMP = EST_PROD_CPL_LOTE.CD_EMP  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_FILIAL = EST_PROD_CPL_LOTE.CD_FILIAL  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_LOTE = EST_PROD_CPL_LOTE.CD_LOTE  
    ), 0) AS DT_FABR,  
   EST_PROD_PRECO.VLR_TABELA,  
   CASE  
       WHEN CTR_PMC = 1  
       THEN 1  
       ELSE 0  
   END AS PROD_MEDICAM,  
   (CASE  
    WHEN DS_SIGLA_LINHA = 'S'  
    THEN 0   --' SIGLAS: E - ETICO,   S - SIMILAR, G - GENERICO |0- SIMILAR; 1- GEN?RICO; 2- ?TICO;  
    WHEN DS_SIGLA_LINHA = 'G'  
    THEN 1  
    WHEN DS_SIGLA_LINHA = 'E'  
    THEN 2  
    ELSE 0  
    END) TIPO_MEDIC,  
   '' AS NOME_PART,  
   '' AS ENDERECO,  
   '' AS BAIRRO,  
   '' AS NUMERO,  
   '' AS INSC_EST,  
   '' AS UF,  
   '' AS TEL,  
   01058 AS CD_PAIS,  
   ISNULL(GLB_MUNICIPIO.COD_MUNICIPIO_SINTEGRA, 0) AS COD_IBGE, -- SELECT TOP 2 CEP,* FROM V_EST_NF_ENT  
   '' AS CEP,  
   EST_PROD_CPL.TP_PROD,  
   ISNULL(  
    (  
    SELECT TOP (1) CD_BARRA  
    FROM EST_PROD_CD_BARRA  
    WHERE CD_EMP = EST_NF_ENT_IT.CD_EMP  
      AND CD_PROD = EST_NF_ENT_IT.CD_PROD  
    ORDER BY CD_BARRA  
    ), '') AS COD_BARRA,  
   EST_PROD.NR_NCM AS COD_NCM,  
   EST_NF_ENT_IT.VLR_DESC AS VLR_DESC_IT,  
   ISNULL(V_EST_NF_ENT_IMPOSTOS_PIS_COFINS.PIS_NF, 0) PIS_NF,  
   ISNULL(V_EST_NF_ENT_IMPOSTOS_PIS_COFINS.COFINS_NF, 0) COFINS_NF,  
   GLB_NT_OP.DS_NT_OP,  
   GLB_MOV_OP.CD_MOV_OP,  
   ISNULL(GLB_MOV_OP.FLAG_NAO_GERAR_ARQ_SPED, 0) AS FLAG_NAO_GERAR_ARQ_SPED,  
   EST_TRIB.CD_TRIB,  
   ISNULL(EST_NF_ENT_CANCEL.CANCEL_FORA_PRAZO, 0) CANCEL_FORA_PRAZO,  
   ISNULL(EST_NF_ENT_NFE_CANCEL.NR_PROTOCOLO, '') AS PROTOCOLO_CANCEL,  
   0 AS POSSUI_DARE,  
   '' AS NUM_DARE,  
   '' AS VALOR_DARE,  
   '' AS DT_VENCTO_DARE,  
   '' AS DT_RECOLHIMENTO_DARE,  
   FLAG_MOV_OP_NFE_CPL, -- SELECT TOP 2 VLR_FRETE_CABE_RATEIO, * FROM V_EST_NF_ENT WHERE CD_FILIAL = 1030 AND NR_NF = 13341  
   ISNULL(EST_NF_ENT_DENEGADA.CD_EMP, 0) AS NOTA_DENEGADA,  
   CASE  
       WHEN EST_NF_ENT.VLR_FRETE = 0  
       THEN 0  
       ELSE CASE  
        WHEN CONVERT(FLOAT, EST_NF_ENT.VLR_NF - EST_NF_ENT.VLR_FRETE) > 0  
        THEN ROUND((ROUND((CONVERT(FLOAT, EST_NF_ENT.VLR_FRETE) / CONVERT(FLOAT, EST_NF_ENT.VLR_NF - EST_NF_ENT.VLR_FRETE)), 9) * (ROUND(((EST_NF_ENT_IT.QT_IT * EST_NF_ENT_IT.VLR_IT) - EST_NF_ENT_IT.VLR_DESC), 2) + EST_NF_ENT_IT.VLR_IPI + EST_NF_ENT_IT.VLR_ICMS_SUBS)), 2)  
        ELSE 0  
        END  
   END AS VLR_FRETE_CABE_RATEIO,  
   ISNULL(EST_NF_SAI_NF_ENT.CD_NF_SAI, 0) CD_NF_SAI,  
   ISNULL(EST_NF_ENT.VLR_FECOP,0 ) AS VLR_FECOP,  
   ISNULL(NR_CEST, '') AS NR_CEST,  
   ISNULL(COD_MUNICIPIO_SINTEGRA, 0) AS COD_IBGE_MUNICIPIO_DESTINATARIO,  
   ISNULL(EST_NF_ENT.END_DESTINATARIO, '') AS END_DESTINATARIO,  
   ISNULL(EST_NF_ENT.BAIRRO_DESTINATARIO, '') AS BAIRRO_DESTINATARIO,  
   ISNULL(EST_NF_ENT.CEP_DESTINATARIO, '') AS CEP_DESTINATARIO,  
   ISNULL(EST_NF_ENT.CIDADE_DESTINATARIO, '') AS CIDADE_DESTINATARIO,  
   ISNULL(EST_NF_ENT.CNPJ_DESTINATARIO, '') AS CNPJ_DESTINATARIO,  
   ISNULL(EST_NF_ENT.INSC_ESTADUAL_DESTINATARIO, '') INSC_ESTADUAL_DESTINATARIO,  
   ISNULL(LEFT(EST_NF_ENT.RZ_DESTINATARIO,60), '') AS RZ_DESTINATARIO,  
   ISNULL(EST_NF_ENT.UF_DESTINATARIO, '') AS UF_DESTINATARIO,  
   ISNULL(EST_NF_ENT.CD_CID_DESTINATARIO, '') CD_CID_DESTINATARIO,  
        
   CASE  
       WHEN GLB_MOV_OP.TP_MOV IN ( 27,21)  --compra de uso e consumo e ativo imobilizado   
       THEN EST_NF_ENT_IT.ALIQ_ICMS  
       ELSE ISNULL(EST_NF_ENT_IT.ALIQ_ICMS_INTERNO, 0)   
   END ALIQ_ICMS_INTERNO,  
   --   ISNULL(EST_NF_ENT_IT.ALIQ_ICMS_INTERNO, 0) AS ALIQ_ICMS_INTERNO,  
   V_APURA_ICMS.VLR_ICMS_DESONERADO,  
   0 AS PART_FLAG_MICRO_EMPRESA,  
   V_APURA_ICMS.BASE_ICMS_SIMPLES,  
   V_APURA_ICMS.ALIQ_SIMPLES,  
   V_APURA_ICMS.VLR_ICMS_SIMPLES,  
   ISNULL(EST_PROD_CPL.VLR_CUSTO_MD,0 )VLR_CUSTO_MD,  
   ISNULL(EST_NF_ENT_IT.VLR_ICMS_DESONERADO, 0) VLR_ICMS_DESONERADO_IT,  
   ISNULL(EST_NF_ENT.VLR_FECOP_ST,0)VLR_FECOP_ST,  
   ISNULL(EST_NF_ENT.VLR_FECOP_ST_RET,0 )VLR_FECOP_ST_RET,  
   ISNULL(EST_NF_ENT_IT.VLR_FECOP,0 )VLR_FECOP_IT,  
   ISNULL(EST_NF_ENT_IT.VLR_FECOP_ST,0 )VLR_FECOP_ST_IT,  
   ISNULL(EST_NF_ENT_IT.VLR_FECOP_RET,0 )VLR_FECOP_ST_RET_IT,  
   ISNULL(EST_PROD.NR_SERV,'') AS NR_SERV,  
   ISNULL(FLAG_VLR_ICMS_DESONERADO_DESCONTO,0) AS FLAG_VLR_ICMS_DESONERADO_DESCONTO,  
   ISNULL(VLR_ICMS_RET,0) AS VLR_ICMS_RET_IT,
   ISNULL(EST_NF_ENT_IT.VLR_BASE_ICMS_RET,0) VLR_BASE_ICMS_RET_IT,
	
	0 AS VLR_ICMS_OP_IT,
    ISNULL(F.PERC_ICMS ,0)  AS ALIQ_INTERNA_PROD,  
    0 AS VLR_BASE_SUBS_CONV_IT,
    0 AS VLR_ICMS_SUBS_CONV_IT,
    0 AS APRES_QT_CP,
	ISNULL(EST_TRIB.FLAG_CST_60_PARA_SPED,0) FLAG_CST_60_PARA_SPED,
	ISNULL(EST_NF_ENT_EST_NF_NFE_INU.CD_EMP,0) INUTILIZADO 
   
    FROM DBO.EST_NF_ENT     
     INNER JOIN #FILIAIS FL ON DBO.EST_NF_ENT.CD_EMP = FL.CD_EMP  
         AND DBO.EST_NF_ENT.CD_FILIAL = FL.CD_FILIAL  
     INNER JOIN DBO.EST_NF_ENT_IT ON DBO.EST_NF_ENT.CD_EMP = DBO.EST_NF_ENT_IT.CD_EMP  
         AND DBO.EST_NF_ENT.CD_FILIAL = DBO.EST_NF_ENT_IT.CD_FILIAL  
         AND DBO.EST_NF_ENT.CD_NF = DBO.EST_NF_ENT_IT.CD_NF  
         AND DBO.EST_NF_ENT.DT_ENT = DBO.EST_NF_ENT_IT.DT_ENT  
  
    INNER JOIN DBO.EST_TRIB   ON DBO.EST_NF_ENT_IT.CD_TRIB = EST_TRIB.CD_TRIB  
    INNER JOIN DBO.GLB_NT_OP ON DBO.EST_NF_ENT_IT.CD_NT_OP = DBO.GLB_NT_OP.CD_NT_OP  
  
     INNER JOIN DBO.EST_PROD ON DBO.EST_PROD.CD_EMP = DBO.EST_NF_ENT_IT.CD_EMP  
        AND DBO.EST_PROD.CD_PROD = DBO.EST_NF_ENT_IT.CD_PROD  
       
     INNER JOIN DBO.GLB_MOV_OP ON DBO.EST_NF_ENT.CD_NT_MOV = DBO.GLB_MOV_OP.CD_MOV_OP  
     INNER JOIN DBO.EST_NF_SERIE ON DBO.EST_NF_ENT.CD_EMP = DBO.EST_NF_SERIE.CD_EMP  
            AND DBO.EST_NF_ENT.CD_NF_SERIE = DBO.EST_NF_SERIE.CD_NF_SERIE  
     LEFT JOIN V_APURA_ICMS ON EST_NF_ENT.CD_EMP = V_APURA_ICMS.CD_EMP  
        AND EST_NF_ENT.CD_FILIAL = V_APURA_ICMS.CD_FILIAL  
        AND EST_NF_ENT.CD_NF = V_APURA_ICMS.CD_NF  
        AND V_APURA_ICMS.TIPO = 0  
     INNER JOIN EST_PROD_CPL ON EST_NF_ENT_IT.CD_EMP = EST_PROD_CPL.CD_EMP  
        AND EST_NF_ENT_IT.CD_FILIAL = EST_PROD_CPL.CD_FILIAL  
        AND EST_NF_ENT_IT.CD_PROD = EST_PROD_CPL.CD_PROD  
     INNER JOIN DBO.EST_PROD_EST_ARV_MERCADOLOGICA AS ARV ON EST_PROD.CD_EMP = ARV.CD_EMP  
                 AND EST_PROD.CD_PROD = ARV.CD_PROD  
     INNER JOIN DBO.EST_ARV_MERC_LINHA AS LINHA ON ARV.CD_EMP = LINHA.CD_EMP  
               AND ARV.CD_ARV_MERC_LINHA = LINHA.CD_ARV_MERC_LINHA
	INNER JOIN GLB_USU ON EST_NF_ENT.CD_USU = GLB_USU.CD_USU    
     LEFT JOIN V_EST_NF_ENT_IMPOSTOS_PIS_COFINS ON V_EST_NF_ENT_IMPOSTOS_PIS_COFINS.CD_EMP = EST_NF_ENT.CD_EMP  
      AND V_EST_NF_ENT_IMPOSTOS_PIS_COFINS.CD_FILIAL = EST_NF_ENT.CD_FILIAL  
      AND V_EST_NF_ENT_IMPOSTOS_PIS_COFINS.CD_NF = EST_NF_ENT.CD_NF  
     LEFT JOIN EST_PROD_PRECO ON EST_PROD_PRECO.CD_EMP = EST_NF_ENT_IT.CD_EMP  
         AND EST_PROD_PRECO.CD_FILIAL = EST_NF_ENT_IT.CD_FILIAL  
         AND EST_PROD_PRECO.CD_PROD = EST_NF_ENT_IT.CD_PROD  
     LEFT JOIN EST_NF_ENT_DAR_IT ON EST_NF_ENT_IT.CD_EMP = EST_NF_ENT_DAR_IT.CD_EMP  
         AND EST_NF_ENT_IT.CD_FILIAL = EST_NF_ENT_DAR_IT.CD_FILIAL  
         AND EST_NF_ENT_IT.CD_NF = EST_NF_ENT_DAR_IT.CD_NF  
         AND EST_NF_ENT_IT.CD_PROD = EST_NF_ENT_DAR_IT.CD_PROD  
  
     LEFT JOIN EST_NF_ENT_DAR ON EST_NF_ENT_DAR.CD_EMP = EST_NF_ENT_DAR_IT.CD_EMP  
          AND EST_NF_ENT_DAR.CD_FILIAL = EST_NF_ENT_DAR_IT.CD_FILIAL  
          AND EST_NF_ENT_DAR.CD_NF = EST_NF_ENT_DAR_IT.CD_NF  
     LEFT JOIN EST_NF_ENT_EF_NF_CTR_NF ON EST_NF_ENT_EF_NF_CTR_NF.CD_EMP = EST_NF_ENT.CD_EMP  
          AND EST_NF_ENT_EF_NF_CTR_NF.CD_FILIAL = EST_NF_ENT.CD_FILIAL  
          AND EST_NF_ENT_EF_NF_CTR_NF.CD_NF_ENT = EST_NF_ENT.CD_NF  
     LEFT JOIN V_EST_NF_ENT_NFE_SPED ON V_EST_NF_ENT_NFE_SPED.CD_EMP = EST_NF_ENT.CD_EMP  
            AND V_EST_NF_ENT_NFE_SPED.CD_FILIAL = EST_NF_ENT.CD_FILIAL  
            AND V_EST_NF_ENT_NFE_SPED.CD_NF = EST_NF_ENT.CD_NF  
     LEFT JOIN GLB_CID ON EST_NF_ENT.CD_CID_DESTINATARIO = GLB_CID.CD_CID  
     LEFT OUTER JOIN GLB_CID_GLB_MUNICIPIO ON GLB_CID.CD_CID = GLB_CID_GLB_MUNICIPIO.CD_CID  
     LEFT OUTER JOIN GLB_MUNICIPIO ON GLB_CID_GLB_MUNICIPIO.CD_MUNICIPIO = GLB_MUNICIPIO.CD_MUNICIPIO  
     LEFT JOIN EST_NF_ENT_CANCEL ON EST_NF_ENT_CANCEL.CD_EMP = EST_NF_ENT.CD_EMP  
            AND EST_NF_ENT_CANCEL.CD_FILIAL = EST_NF_ENT.CD_FILIAL  
            AND EST_NF_ENT_CANCEL.CD_NF = EST_NF_ENT.CD_NF  
     LEFT JOIN EST_NF_ENT_NFE_CANCEL ON EST_NF_ENT_CANCEL.CD_EMP = EST_NF_ENT_NFE_CANCEL.CD_EMP  
            AND EST_NF_ENT_CANCEL.CD_FILIAL = EST_NF_ENT_NFE_CANCEL.CD_FILIAL  
            AND EST_NF_ENT_CANCEL.CD_NF = EST_NF_ENT_NFE_CANCEL.CD_NF  
     LEFT JOIN EST_NF_ENT_DENEGADA ON EST_NF_ENT.CD_EMP = EST_NF_ENT_DENEGADA.CD_EMP  
          AND EST_NF_ENT.CD_FILIAL = EST_NF_ENT_DENEGADA.CD_FILIAL  
          AND EST_NF_ENT.CD_NF = EST_NF_ENT_DENEGADA.CD_NF  
     LEFT JOIN EST_NF_SAI_NF_ENT ON EST_NF_ENT.CD_EMP = EST_NF_SAI_NF_ENT.CD_EMP  
            AND EST_NF_ENT.CD_FILIAL = EST_NF_SAI_NF_ENT.CD_FILIAL  
            AND EST_NF_ENT.CD_NF = EST_NF_SAI_NF_ENT.CD_NF_ENT  
           
     LEFT JOIN EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT DANFE ON  EST_NF_ENT.CD_EMP = DANFE.CD_EMP  
            AND EST_NF_ENT.CD_FILIAL = DANFE.CD_FILIAL  
            AND EST_NF_ENT.CD_NF = DANFE.CD_NF

	 LEFT JOIN EST_NF_ENT_EST_NF_NFE_INU  
		ON     EST_NF_ENT.CD_EMP    = EST_NF_ENT_EST_NF_NFE_INU.CD_EMP
		AND    EST_NF_ENT.CD_FILIAL = EST_NF_ENT_EST_NF_NFE_INU.CD_FILIAL
	    AND    EST_NF_ENT.CD_NF     = EST_NF_ENT_EST_NF_NFE_INU.CD_NF
			
	 OUTER APPLY UFD_TRIB_PROD_ALIQ_INTERNA(EST_NF_ENT_IT.CD_EMP, 
			EST_NF_ENT_IT.CD_PROD,  
			GLB_CID.UF) F		
			  
    WHERE    
    EST_NF_ENT.CD_EMP = 1   
    AND EST_NF_ENT.DT_ENT BETWEEN @DT_INI AND @DT_FIM  
    AND EST_NF_ENT_DAR_IT.CD_EMP IS NULL  
    AND DANFE.CD_EMP IS NULL  
  
   -- NOTAS DE ENTRADA SEM VINCULO COM DANFE   
   -- (NOT EXISTS EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT)  
   -- COM DARE / CONSULTA 2  
    
    UNION ALL  
    SELECT EST_NF_ENT.CD_EMP,  
   EST_NF_ENT.CD_FILIAL,  
   EST_NF_ENT.CD_NF AS CD_DOC,  
   0 AS IND_OPER,  
   (CASE  
    WHEN EST_NF_ENT_EF_NF_CTR_NF.CD_NF_ENT > 0  
    THEN 0  
    ELSE 1  
    END) AS IND_EMIT,  
   EST_NF_ENT.CD_FORN AS CD_ORIG,  
   LEFT(EST_NF_SERIE.MODELO,2),  
   EST_NF_SERIE.TP_SR_NFE,  
   EST_NF_SERIE.SERIE,  
   EST_NF_ENT.NR_NF AS NR_DOC,  
   ISNULL(V_EST_NF_ENT_NFE_SPED.NR_AUTORIZADOR, '') AS CHAVE_NFE,  
   EST_NF_ENT.DT_EMI_NF,  
   EST_NF_ENT.DT_ENT AS DT_ENT_SAI,  
   EST_NF_ENT.VLR_NF,  
   PGTO_VISTA_PRAZO AS IND_PAGTO,  
   EST_NF_ENT.VLR_DESC,  
   0 AS VLR_ABAT,  
   CASE  
       WHEN ISNULL(EST_NF_ENT.FLAG_NF_PROD_TOTAL_BRUTO, 0) = 0  
       THEN EST_NF_ENT.VLR_TOT_PROD  
       ELSE EST_NF_ENT.VLR_TOT_PROD - EST_NF_ENT.VLR_DESC  
   END AS VLR_MERC,  
   1 AS TP_FRETE,  
   VLR_FRETE,  
   0 AS VLR_SEG,  
   EST_NF_ENT.VLR_OUTROS AS VLR_OUTRAS_DESP_CAB,  
   V_APURA_ICMS.VLR_BASE_ICMS_CAB,  
   V_APURA_ICMS.VLR_ICMS_CAB,  
   EST_NF_ENT.VLR_BASE_ICMS_SUBS AS VLR_BASE_ICMS_SUBS_CAB,  
   EST_NF_ENT.VLR_ICMS_SUBS AS VLR_ICMS_SUBS_CAB,  
   EST_NF_ENT.VLR_IPI AS VLR_IPI_CAB,  
   EST_NF_ENT.VRL_ISENTAS AS VLR_ISENTAS_CAB,  
   '' AS CGC_CPF,  
   EST_NF_ENT.STS_NF,  
   EST_NF_ENT.VLR_OUTROS AS VLR_SERV_NT,  
   0 AS VLR_BASE_ISSQN_CAB,  
   0 VLR_ISSQN_CAB,  
   0 AS VLR_BASE_IRRF_CAB,  
   0 AS VLR_IRRF_CAB,  
   0 AS CD_TRANSP,  
   QT_VOL,  
   PESO_BRT,  
   PESO_LIQ,  
   ISNULL(  
    (  
    SELECT TOP 1 CD_EMP  
    FROM PG_CRED_EST_NF_ENT  
    WHERE EST_NF_ENT.CD_EMP = PG_CRED_EST_NF_ENT.CD_EMP  
      AND EST_NF_ENT.CD_FILIAL = PG_CRED_EST_NF_ENT.CD_FILIAL  
      AND EST_NF_ENT.CD_NF = PG_CRED_EST_NF_ENT.CD_NF  
    ), 0) AS POSSUI_DUPL,  
   GLB_MOV_OP.TP_MOV,  
   GLB_MOV_OP.TP_MOV_OP_TRANSA,  
   GLB_MOV_OP.DEST_ORIG,  
   CASE  
       WHEN GLB_MOV_OP.DEST_ORIG = 0  
       THEN 'CLI'  
       WHEN GLB_MOV_OP.DEST_ORIG = 1  
       THEN 'FOR'  
       ELSE 'FIL'  
   END AS TIPO_PART,  
   EST_NF_ENT_IT.SEQ_ITEM AS NUM_ITEM,  
   EST_NF_ENT_IT.CD_PROD AS COD_ITEM,  
   EST_PROD_CPL.TP_PROD AS TIPO_ITEM,  
   EST_PROD.DS_PROD, --   
   EST_NF_ENT_IT.QT_IT,  
   EST_NF_ENT_IT.VLR_IT,  
   CASE  
       WHEN ISNULL(DBO.EST_NF_ENT.FLAG_NF_PROD_TOTAL_BRUTO, 0) = 0  
       THEN DBO.EST_NF_ENT_IT.VLR_TOT_IT  
       ELSE DBO.EST_NF_ENT_IT.VLR_TOT_IT - EST_NF_ENT_IT.VLR_DESC  
   END AS VLR_ITEM,  
   TRIB_DARE.CD_TRIB_FC AS CST_ICMS,  
   TRIB_DARE.TRIB_FC_ELETRONICO,  
   GLB_NT_OP.CD_NT_OP_FC AS CFOP,  
   CASE  
       WHEN TRIB_DARE.ICMS > 0  
       THEN EST_NF_ENT_DAR_IT.VLR_BASE_ICMS  
       ELSE 0  
   END AS VLR_BASE_ICMS,  
   CASE  
       WHEN TRIB_DARE.ICMS > 0  
       THEN EST_NF_ENT_DAR_IT.PERC_ICMS  
       ELSE 0  
   END ALIQ_ICMS,  
   CASE  
       WHEN TRIB_DARE.ICMS > 0  
       THEN EST_NF_ENT_DAR_IT.VLR_ICMS  
       ELSE 0  
   END AS VLR_ICMS_IT,  
   EST_NF_ENT_DAR_IT.VLR_BASE_SUBS AS VLR_BASE_SUBS,  
   EST_NF_ENT_DAR_IT.VLR_ICMS_SUBS AS VLR_ICMS_SUBS,  
   EST_NF_ENT_IT.VLR_IPI AS VLR_IPI_IT,  
   EST_NF_ENT_IT.PERC_IPI,  
   EST_NF_ENT_IT.VLR_ISENTO AS VLR_ISENTO_IT,  
   ISNULL(EST_NF_ENT_IT.CST_PIS, 0) CST_PIS,  
   ISNULL(EST_NF_ENT_IT.CST_COFINS, 0) CST_COFINS,  
   ISNULL(EST_NF_ENT_IT.VLR_BASE_PIS, 0) VLR_BASE_PIS,  
   ISNULL(EST_NF_ENT_IT.PERC_ALIQ_PIS, 0) PERC_ALIQ_PIS,  
   ISNULL(EST_NF_ENT_IT.VLR_PIS, 0) VLR_PIS,  
   ISNULL(EST_NF_ENT_IT.VLR_BASE_COFINS, 0) VLR_BASE_COFINS,  
   ISNULL(EST_NF_ENT_IT.PERC_ALIQ_COFINS, 0) PERC_ALIQ_COFINS,  
   ISNULL(EST_NF_ENT_IT.VLR_COFINS, 0) VLR_COFINS,  
   EST_NF_ENT_IT.VLR_OUTRAS_DESPESAS AS VLR_OUTRAS_DESPESAS_IT,  
   0 AS VLR_BASE_ISSQN,  
   0 AS VLR_ISSQN,  
   0 AS ALIQ_ISSQN,  
   EST_NF_ENT.CD_FORN AS COD_PART,  
   1 AS COD_IMP_ALMOX,  
   EST_NF_ENT_IT.LISTA_PROD,  
   TRIB_DARE.SUBS_TRIB,  
   TRIB_DARE.SUBS_TRIB_ANT,  
   EST_NF_ENT_IT.CD_NT_OP,  
   TRIB_DARE.ICMS,  
   TRIB_DARE.ICMS_RD,  
   FLAG_OPERADOR_LOG,  
   ISNULL(  
    (  
    SELECT SUM(EST_NF_ENT_IT_EST_PROD_CPL_LOTE.QTDE_PROD) QTDE_PROD  
    FROM EST_NF_ENT_IT_EST_PROD_CPL_LOTE,  
     EST_PROD_CPL_LOTE  
    WHERE EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_EMP = EST_NF_ENT_IT.CD_EMP  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_FILIAL = EST_NF_ENT_IT.CD_FILIAL  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_NF = EST_NF_ENT_IT.CD_NF  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_PROD = EST_NF_ENT_IT.CD_PROD  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_EMP = EST_PROD_CPL_LOTE.CD_EMP  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_FILIAL = EST_PROD_CPL_LOTE.CD_FILIAL  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_LOTE = EST_PROD_CPL_LOTE.CD_LOTE  
    ), 0) AS QTDE_PROD,  
   ISNULL(  
    (  
    SELECT TOP (1) EST_PROD_CPL_LOTE.NR_LOTE  
    FROM EST_NF_ENT_IT_EST_PROD_CPL_LOTE,  
     EST_PROD_CPL_LOTE  
    WHERE EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_EMP = EST_NF_ENT_IT.CD_EMP  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_FILIAL = EST_NF_ENT_IT.CD_FILIAL  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_NF = EST_NF_ENT_IT.CD_NF  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_PROD = EST_NF_ENT_IT.CD_PROD  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_EMP = EST_PROD_CPL_LOTE.CD_EMP  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_FILIAL = EST_PROD_CPL_LOTE.CD_FILIAL  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_LOTE = EST_PROD_CPL_LOTE.CD_LOTE  
    ORDER BY EST_PROD_CPL_LOTE.CD_LOTE  
    ), 0) AS NR_LOTE,  
   ISNULL(  
    (  
    SELECT TOP (1) EST_PROD_CPL_LOTE.DT_VALID  
    FROM EST_NF_ENT_IT_EST_PROD_CPL_LOTE,  
     EST_PROD_CPL_LOTE  
    WHERE EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_EMP = EST_NF_ENT_IT.CD_EMP  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_FILIAL = EST_NF_ENT_IT.CD_FILIAL  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_NF = EST_NF_ENT_IT.CD_NF  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_PROD = EST_NF_ENT_IT.CD_PROD  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_EMP = EST_PROD_CPL_LOTE.CD_EMP  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_FILIAL = EST_PROD_CPL_LOTE.CD_FILIAL  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_LOTE = EST_PROD_CPL_LOTE.CD_LOTE  
    ), 0) AS DT_VALID,  
   ISNULL(  
    (  
    SELECT TOP (1) EST_PROD_CPL_LOTE.DT_FABR  
    FROM EST_NF_ENT_IT_EST_PROD_CPL_LOTE,  
     EST_PROD_CPL_LOTE  
    WHERE EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_EMP = EST_NF_ENT_IT.CD_EMP  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_FILIAL = EST_NF_ENT_IT.CD_FILIAL  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_NF = EST_NF_ENT_IT.CD_NF  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_PROD = EST_NF_ENT_IT.CD_PROD  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_EMP = EST_PROD_CPL_LOTE.CD_EMP  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_FILIAL = EST_PROD_CPL_LOTE.CD_FILIAL  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_LOTE = EST_PROD_CPL_LOTE.CD_LOTE  
    ), 0) AS DT_FABR,  
   EST_PROD_PRECO.VLR_TABELA,  
   CASE  
       WHEN CTR_PMC = 1  
       THEN 1  
       ELSE 0  
   END AS PROD_MEDICAM,  
   (CASE  
    WHEN DS_SIGLA_LINHA = 'S'  
    THEN 0   --' SIGLAS: E - ETICO,   S - SIMILAR, G - GENERICO |0- SIMILAR; 1- GEN?RICO; 2- ?TICO;  
    WHEN DS_SIGLA_LINHA = 'G'  
    THEN 1  
    WHEN DS_SIGLA_LINHA = 'E'  
    THEN 2  
    ELSE 0  
    END) TIPO_MEDIC,  
   '' AS NOME_PART,  
   '' AS ENDERECO,  
   '' AS BAIRRO,  
   '' AS NUMERO,  
   '' AS INSC_EST,  
   '' AS UF,  
   '' AS TEL,  
   01058 AS CD_PAIS,  
   ISNULL(GLB_MUNICIPIO.COD_MUNICIPIO_SINTEGRA, 0) AS COD_IBGE, -- SELECT TOP 2 CEP,* FROM V_EST_NF_ENT  
   '' AS CEP,  
   EST_PROD_CPL.TP_PROD,  
   ISNULL(  
    (  
    SELECT TOP (1) CD_BARRA  
    FROM EST_PROD_CD_BARRA  
    WHERE CD_EMP = EST_NF_ENT_IT.CD_EMP  
      AND CD_PROD = EST_NF_ENT_IT.CD_PROD  
    ORDER BY CD_BARRA  
    ), '') AS COD_BARRA,  
   EST_PROD.NR_NCM AS COD_NCM,  
   EST_NF_ENT_IT.VLR_DESC AS VLR_DESC_IT,  
   ISNULL(V_EST_NF_ENT_IMPOSTOS_PIS_COFINS.PIS_NF, 0) PIS_NF,  
   ISNULL(V_EST_NF_ENT_IMPOSTOS_PIS_COFINS.COFINS_NF, 0) COFINS_NF,  
   GLB_NT_OP.DS_NT_OP,  
   GLB_MOV_OP.CD_MOV_OP,  
   ISNULL(GLB_MOV_OP.FLAG_NAO_GERAR_ARQ_SPED, 0) AS FLAG_NAO_GERAR_ARQ_SPED,  
   EST_NF_ENT_DAR_IT.CD_TRIB,  
   ISNULL(EST_NF_ENT_CANCEL.CANCEL_FORA_PRAZO, 0) CANCEL_FORA_PRAZO,  
   ISNULL(EST_NF_ENT_NFE_CANCEL.NR_PROTOCOLO, '') AS PROTOCOLO_CANCEL,  
   1 AS POSSUI_DARE,  
   EST_NF_ENT_DAR.NR_DAR AS NUM_DARE,  
   EST_NF_ENT_DAR.VLR_DAR AS VALOR_DARE,  
   EST_NF_ENT_DAR.DT_VENCTO AS DT_VENCTO_DARE,  
   EST_NF_ENT_DAR.DT_RECOLHIMENTO AS DT_RECOLHIMENTO_DARE,  
   FLAG_MOV_OP_NFE_CPL, -- SELECT TOP 2 VLR_FRETE_CABE_RATEIO, * FROM V_EST_NF_ENT WHERE CD_FILIAL = 1030 AND NR_NF = 13341  
   ISNULL(EST_NF_ENT_DENEGADA.CD_EMP, 0) AS NOTA_DENEGADA,  
   CASE  
       WHEN EST_NF_ENT.VLR_FRETE = 0  
       THEN 0  
       ELSE CASE  
        WHEN CONVERT(FLOAT, EST_NF_ENT.VLR_NF - EST_NF_ENT.VLR_FRETE) > 0  
        THEN ROUND((ROUND((CONVERT(FLOAT, EST_NF_ENT.VLR_FRETE) / CONVERT(FLOAT, EST_NF_ENT.VLR_NF - EST_NF_ENT.VLR_FRETE)), 9) * (ROUND(((EST_NF_ENT_IT.QT_IT * EST_NF_ENT_IT.VLR_IT) - EST_NF_ENT_IT.VLR_DESC), 2) + EST_NF_ENT_IT.VLR_IPI + EST_NF_ENT_IT.VLR_ICMS_SUBS)), 2)  
        ELSE 0  
        END  
   END AS VLR_FRETE_CABE_RATEIO,  
   ISNULL(EST_NF_SAI_NF_ENT.CD_NF_SAI, 0) CD_NF_SAI,  
   ISNULL(EST_NF_ENT.VLR_FECOP,0)VLR_FECOP,  
   ISNULL(NR_CEST, '') AS NR_CEST,  
   ISNULL(COD_MUNICIPIO_SINTEGRA, 0) AS COD_IBGE_MUNICIPIO_DESTINATARIO,  
   ISNULL(EST_NF_ENT.END_DESTINATARIO, '') AS END_DESTINATARIO,  
   ISNULL(EST_NF_ENT.BAIRRO_DESTINATARIO, '') AS BAIRRO_DESTINATARIO,  
   ISNULL(EST_NF_ENT.CEP_DESTINATARIO, '') AS CEP_DESTINATARIO,  
   ISNULL(EST_NF_ENT.CIDADE_DESTINATARIO, '') AS CIDADE_DESTINATARIO,  
   ISNULL(EST_NF_ENT.CNPJ_DESTINATARIO, '') AS CNPJ_DESTINATARIO,  
   ISNULL(EST_NF_ENT.INSC_ESTADUAL_DESTINATARIO, '') INSC_ESTADUAL_DESTINATARIO,  
   ISNULL(EST_NF_ENT.RZ_DESTINATARIO, '') AS RZ_DESTINATARIO,  
   ISNULL(EST_NF_ENT.UF_DESTINATARIO, '') AS UF_DESTINATARIO,  
   ISNULL(EST_NF_ENT.CD_CID_DESTINATARIO, '') CD_CID_DESTINATARIO,  
   ISNULL(EST_NF_ENT_IT.ALIQ_ICMS_INTERNO, 0) AS ALIQ_ICMS_INTERNO,  
   V_APURA_ICMS.VLR_ICMS_DESONERADO,  
   0 AS PART_FLAG_MICRO_EMPRESA,  
   V_APURA_ICMS.BASE_ICMS_SIMPLES,  
   V_APURA_ICMS.ALIQ_SIMPLES,  
   V_APURA_ICMS.VLR_ICMS_SIMPLES,  
   ISNULL(EST_PROD_CPL.VLR_CUSTO_MD,0 )VLR_CUSTO_MD,  
   ISNULL(EST_NF_ENT_IT.VLR_ICMS_DESONERADO, 0) VLR_ICMS_DESONERADO_IT,  
   ISNULL(EST_NF_ENT.VLR_FECOP_ST,0)VLR_FECOP_ST,  
   ISNULL(EST_NF_ENT.VLR_FECOP_ST_RET,0 )VLR_FECOP_ST_RET,  
   ISNULL(EST_NF_ENT_IT.VLR_FECOP,0 )VLR_FECOP_IT,  
   ISNULL(EST_NF_ENT_IT.VLR_FECOP_ST,0 )VLR_FECOP_ST_IT,  
   ISNULL(EST_NF_ENT_IT.VLR_FECOP_RET,0 )VLR_FECOP_ST_RET_IT,  
   ISNULL(EST_PROD.NR_SERV,'') AS NR_SERV,  
   ISNULL(FLAG_VLR_ICMS_DESONERADO_DESCONTO,0) AS FLAG_VLR_ICMS_DESONERADO_DESCONTO,  
   ISNULL(VLR_ICMS_RET,0) AS VLR_ICMS_RET_IT,  
   ISNULL(EST_NF_ENT_IT.VLR_BASE_ICMS_RET,0)   AS   VLR_BASE_ICMS_RET_IT,
   0 AS VLR_ICMS_OP_IT,	
   ISNULL(F.PERC_ICMS ,0)  AS ALIQ_INTERNA_PROD,  	   
   0  AS VLR_BASE_SUBS_CONV_IT ,
   0  AS VLR_ICMS_SUBS_CONV_IT,
   0 AS APRES_QT_CP,  
   ISNULL(TRIB_DARE.FLAG_CST_60_PARA_SPED,0) FLAG_CST_60_PARA_SPED,
   ISNULL(EST_NF_ENT_EST_NF_NFE_INU.CD_EMP,0) INUTILIZADO

    FROM DBO.EST_NF_ENT    
     INNER JOIN #FILIAIS FL ON DBO.EST_NF_ENT.CD_EMP = FL.CD_EMP  
         AND DBO.EST_NF_ENT.CD_FILIAL = FL.CD_FILIAL  
     INNER JOIN DBO.EST_NF_ENT_IT ON DBO.EST_NF_ENT.CD_EMP = DBO.EST_NF_ENT_IT.CD_EMP  
         AND DBO.EST_NF_ENT.CD_FILIAL = DBO.EST_NF_ENT_IT.CD_FILIAL  
         AND DBO.EST_NF_ENT.CD_NF = DBO.EST_NF_ENT_IT.CD_NF  
         AND DBO.EST_NF_ENT.DT_ENT = DBO.EST_NF_ENT_IT.DT_ENT   
     INNER JOIN DBO.EST_PROD ON DBO.EST_PROD.CD_EMP = DBO.EST_NF_ENT_IT.CD_EMP  
        AND DBO.EST_PROD.CD_PROD = DBO.EST_NF_ENT_IT.CD_PROD   
     INNER JOIN DBO.GLB_MOV_OP ON DBO.EST_NF_ENT.CD_NT_MOV = DBO.GLB_MOV_OP.CD_MOV_OP  
     INNER JOIN DBO.EST_NF_SERIE ON DBO.EST_NF_ENT.CD_EMP = DBO.EST_NF_SERIE.CD_EMP  
            AND DBO.EST_NF_ENT.CD_NF_SERIE = DBO.EST_NF_SERIE.CD_NF_SERIE  
     LEFT JOIN V_APURA_ICMS ON EST_NF_ENT.CD_EMP = V_APURA_ICMS.CD_EMP  
        AND EST_NF_ENT.CD_FILIAL = V_APURA_ICMS.CD_FILIAL  
        AND EST_NF_ENT.CD_NF = V_APURA_ICMS.CD_NF  
        AND V_APURA_ICMS.TIPO = 0  
     INNER JOIN EST_PROD_CPL ON EST_NF_ENT_IT.CD_EMP = EST_PROD_CPL.CD_EMP  
        AND EST_NF_ENT_IT.CD_FILIAL = EST_PROD_CPL.CD_FILIAL  
        AND EST_NF_ENT_IT.CD_PROD = EST_PROD_CPL.CD_PROD  
     INNER JOIN DBO.EST_PROD_EST_ARV_MERCADOLOGICA AS ARV ON EST_PROD.CD_EMP = ARV.CD_EMP  
                 AND EST_PROD.CD_PROD = ARV.CD_PROD  
     INNER JOIN DBO.EST_ARV_MERC_LINHA AS LINHA ON ARV.CD_EMP = LINHA.CD_EMP  
               AND ARV.CD_ARV_MERC_LINHA = LINHA.CD_ARV_MERC_LINHA  
     LEFT JOIN V_EST_NF_ENT_IMPOSTOS_PIS_COFINS ON V_EST_NF_ENT_IMPOSTOS_PIS_COFINS.CD_EMP = EST_NF_ENT.CD_EMP  
      AND V_EST_NF_ENT_IMPOSTOS_PIS_COFINS.CD_FILIAL = EST_NF_ENT.CD_FILIAL  
      AND V_EST_NF_ENT_IMPOSTOS_PIS_COFINS.CD_NF = EST_NF_ENT.CD_NF  
     LEFT JOIN EST_PROD_PRECO ON EST_PROD_PRECO.CD_EMP = EST_NF_ENT_IT.CD_EMP  
         AND EST_PROD_PRECO.CD_FILIAL = EST_NF_ENT_IT.CD_FILIAL  
         AND EST_PROD_PRECO.CD_PROD = EST_NF_ENT_IT.CD_PROD  
     INNER JOIN EST_NF_ENT_DAR_IT ON EST_NF_ENT_IT.CD_EMP = EST_NF_ENT_DAR_IT.CD_EMP  
         AND EST_NF_ENT_IT.CD_FILIAL = EST_NF_ENT_DAR_IT.CD_FILIAL  
         AND EST_NF_ENT_IT.CD_NF = EST_NF_ENT_DAR_IT.CD_NF  
         AND EST_NF_ENT_IT.CD_PROD = EST_NF_ENT_DAR_IT.CD_PROD  
     INNER JOIN DBO.EST_TRIB TRIB_DARE ON DBO.EST_NF_ENT_DAR_IT.CD_TRIB = TRIB_DARE.CD_TRIB  
     INNER JOIN DBO.GLB_NT_OP ON DBO.EST_NF_ENT_DAR_IT.CD_NT_OP = DBO.GLB_NT_OP.CD_NT_OP       
     INNER JOIN EST_NF_ENT_DAR ON EST_NF_ENT_DAR.CD_EMP = EST_NF_ENT_DAR_IT.CD_EMP  
          AND EST_NF_ENT_DAR.CD_FILIAL = EST_NF_ENT_DAR_IT.CD_FILIAL  
          AND EST_NF_ENT_DAR.CD_NF = EST_NF_ENT_DAR_IT.CD_NF  
     LEFT JOIN EST_NF_ENT_EF_NF_CTR_NF ON EST_NF_ENT_EF_NF_CTR_NF.CD_EMP = EST_NF_ENT.CD_EMP  
          AND EST_NF_ENT_EF_NF_CTR_NF.CD_FILIAL = EST_NF_ENT.CD_FILIAL  
          AND EST_NF_ENT_EF_NF_CTR_NF.CD_NF_ENT = EST_NF_ENT.CD_NF  
     LEFT JOIN V_EST_NF_ENT_NFE_SPED ON V_EST_NF_ENT_NFE_SPED.CD_EMP = EST_NF_ENT.CD_EMP  
            AND V_EST_NF_ENT_NFE_SPED.CD_FILIAL = EST_NF_ENT.CD_FILIAL  
            AND V_EST_NF_ENT_NFE_SPED.CD_NF = EST_NF_ENT.CD_NF  
     INNER JOIN GLB_USU ON EST_NF_ENT.CD_USU = GLB_USU.CD_USU  
     LEFT JOIN GLB_CID ON EST_NF_ENT.CD_CID_DESTINATARIO = GLB_CID.CD_CID  
     LEFT OUTER JOIN GLB_CID_GLB_MUNICIPIO ON GLB_CID.CD_CID = GLB_CID_GLB_MUNICIPIO.CD_CID  
     LEFT OUTER JOIN GLB_MUNICIPIO ON GLB_CID_GLB_MUNICIPIO.CD_MUNICIPIO = GLB_MUNICIPIO.CD_MUNICIPIO  
     LEFT JOIN EST_NF_ENT_CANCEL ON EST_NF_ENT_CANCEL.CD_EMP = EST_NF_ENT.CD_EMP  
            AND EST_NF_ENT_CANCEL.CD_FILIAL = EST_NF_ENT.CD_FILIAL  
            AND EST_NF_ENT_CANCEL.CD_NF = EST_NF_ENT.CD_NF  
     LEFT JOIN EST_NF_ENT_NFE_CANCEL ON EST_NF_ENT_CANCEL.CD_EMP = EST_NF_ENT_NFE_CANCEL.CD_EMP  
            AND EST_NF_ENT_CANCEL.CD_FILIAL = EST_NF_ENT_NFE_CANCEL.CD_FILIAL  
            AND EST_NF_ENT_CANCEL.CD_NF = EST_NF_ENT_NFE_CANCEL.CD_NF  
     LEFT JOIN EST_NF_ENT_DENEGADA ON EST_NF_ENT.CD_EMP = EST_NF_ENT_DENEGADA.CD_EMP  
          AND EST_NF_ENT.CD_FILIAL = EST_NF_ENT_DENEGADA.CD_FILIAL  
          AND EST_NF_ENT.CD_NF = EST_NF_ENT_DENEGADA.CD_NF  
     LEFT JOIN EST_NF_SAI_NF_ENT ON EST_NF_ENT.CD_EMP = EST_NF_SAI_NF_ENT.CD_EMP  
            AND EST_NF_ENT.CD_FILIAL = EST_NF_SAI_NF_ENT.CD_FILIAL  
          AND EST_NF_ENT.CD_NF = EST_NF_SAI_NF_ENT.CD_NF_ENT  
     LEFT JOIN EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT DANFE ON  EST_NF_ENT.CD_EMP = DANFE.CD_EMP  
            AND EST_NF_ENT.CD_FILIAL = DANFE.CD_FILIAL  
            AND EST_NF_ENT.CD_NF = DANFE.CD_NF
	LEFT JOIN EST_NF_ENT_EST_NF_NFE_INU  
		ON     EST_NF_ENT.CD_EMP    = EST_NF_ENT_EST_NF_NFE_INU.CD_EMP
		AND    EST_NF_ENT.CD_FILIAL = EST_NF_ENT_EST_NF_NFE_INU.CD_FILIAL
		AND    EST_NF_ENT.CD_NF     = EST_NF_ENT_EST_NF_NFE_INU.CD_NF
     OUTER APPLY UFD_TRIB_PROD_ALIQ_INTERNA(EST_NF_ENT_IT.CD_EMP, 
			EST_NF_ENT_IT.CD_PROD,  
			GLB_CID.UF) F   
 
    WHERE   
   EST_NF_ENT.CD_EMP = 1   
      AND EST_NF_ENT.DT_ENT BETWEEN @DT_INI AND @DT_FIM  
   AND DANFE.CD_EMP IS NULL  
  
 UNION ALL   
  
  --NOTAS DE ENTRADA VINCULO COM DANFE   
  --(INNER JOIN EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT)  
  --SEM DARE / CONSULTA 3  
  
   SELECT EST_NF_ENT.CD_EMP,  
   EST_NF_ENT.CD_FILIAL,  
   EST_NF_ENT.CD_NF AS CD_DOC,  
   0 AS IND_OPER,  
   (CASE  
    WHEN EST_NF_ENT_EF_NF_CTR_NF.CD_NF_ENT > 0  
    THEN 0  
    ELSE 1  
    END) AS IND_EMIT,  
   EST_NF_ENT.CD_FORN AS CD_ORIG,  
   LEFT(EST_NF_SERIE.MODELO,2),  
   EST_NF_SERIE.TP_SR_NFE,  
   EST_NF_SERIE.SERIE,  
   EST_NF_ENT.NR_NF AS NR_DOC,  
   ISNULL(V_EST_NF_ENT_NFE_SPED.NR_AUTORIZADOR, '') AS CHAVE_NFE,  
   EST_NF_ENT.DT_EMI_NF,  
   EST_NF_ENT.DT_ENT AS DT_ENT_SAI,  
   EST_NF_ENT.VLR_NF,  
   PGTO_VISTA_PRAZO AS IND_PAGTO,  
   EST_NF_ENT.VLR_DESC,  
   0 AS VLR_ABAT,  
   CASE  
       WHEN ISNULL(EST_NF_ENT.FLAG_NF_PROD_TOTAL_BRUTO, 0) = 0  
       THEN EST_NF_ENT.VLR_TOT_PROD  
       ELSE EST_NF_ENT.VLR_TOT_PROD - EST_NF_ENT.VLR_DESC  
   END AS VLR_MERC,  
   1 AS TP_FRETE,  
   VLR_FRETE,  
   0 AS VLR_SEG,  
   EST_NF_ENT.VLR_OUTROS AS VLR_OUTRAS_DESP_CAB,  
   V_APURA_ICMS.VLR_BASE_ICMS_CAB,  
   V_APURA_ICMS.VLR_ICMS_CAB,  
   EST_NF_ENT.VLR_BASE_ICMS_SUBS AS VLR_BASE_ICMS_SUBS_CAB,  
   EST_NF_ENT.VLR_ICMS_SUBS AS VLR_ICMS_SUBS_CAB,  
   EST_NF_ENT.VLR_IPI AS VLR_IPI_CAB,  
   EST_NF_ENT.VRL_ISENTAS AS VLR_ISENTAS_CAB,  
   '' AS CGC_CPF,  
   EST_NF_ENT.STS_NF,  
   EST_NF_ENT.VLR_OUTROS AS VLR_SERV_NT,  
   0 AS VLR_BASE_ISSQN_CAB,  
   0 VLR_ISSQN_CAB,  
   0 AS VLR_BASE_IRRF_CAB,  
   0 AS VLR_IRRF_CAB,  
   0 AS CD_TRANSP,  
   QT_VOL,  
   PESO_BRT,  
   PESO_LIQ,  
   ISNULL(  
    (  
    SELECT TOP 1 CD_EMP  
    FROM PG_CRED_EST_NF_ENT  
    WHERE EST_NF_ENT.CD_EMP = PG_CRED_EST_NF_ENT.CD_EMP  
      AND EST_NF_ENT.CD_FILIAL = PG_CRED_EST_NF_ENT.CD_FILIAL  
      AND EST_NF_ENT.CD_NF = PG_CRED_EST_NF_ENT.CD_NF  
    ), 0) AS POSSUI_DUPL,  
   GLB_MOV_OP.TP_MOV,  
   GLB_MOV_OP.TP_MOV_OP_TRANSA,  
   GLB_MOV_OP.DEST_ORIG,  
   CASE  
       WHEN GLB_MOV_OP.DEST_ORIG = 0  
       THEN 'CLI'  
       WHEN GLB_MOV_OP.DEST_ORIG = 1  
       THEN 'FOR'  
       ELSE 'FIL'  
   END AS TIPO_PART,  
  
   -- ITENS INICIO -------------------------------------  
     
   DANFE_ITENS.CD_CTR_IT AS NUM_ITEM,  
   DANFE_ITENS.CD_PROD AS COD_ITEM,  
   EST_PROD_CPL.TP_PROD AS TIPO_ITEM,  
   EST_PROD.DS_PROD,    
   DANFE_ITENS.QTDE_PROD AS QT_IT,  
   DANFE_ITENS.VLR_PROD_UNIT AS VLR_IT,  
   DANFE_ITENS.VLR_PROD_TOTAL AS VLR_ITEM,  
   EST_TRIB.CD_TRIB_FC AS CST_ICMS,  
   EST_TRIB.TRIB_FC_ELETRONICO,  
     
   --CASE WHEN DANFE_ITENS.CFOP = '5405' THEN  
   --      '1403'  
   --WHEN DANFE_ITENS.CFOP = '6109' OR DANFE_ITENS.CFOP = '6110' THEN  
   --     '2102'  
   --   ELSE  
   --   CASE WHEN LEFT(DANFE_ITENS.CFOP,1) = 5 THEN  
   --        '1' +  RIGHT(DANFE_ITENS.CFOP,3)  
   --   ELSE  
   --        '2' +  RIGHT(DANFE_ITENS.CFOP,3)  
   --   END   
   --END AS CFOP, 
   
   C.CD_NT_OP_FC AS  CFOP, 
     
   CASE  
       WHEN EST_TRIB.ICMS > 0  
       THEN DANFE_ITENS.VLR_PROD_BASE_ICMS  
       ELSE 0  
   END VLR_BASE_ICMS,  
   CASE   
       WHEN EST_TRIB.ICMS > 0  OR ( GLB_MOV_OP.TP_MOV IN ( 27,21) )  
       THEN DANFE_ITENS.PERC_PROD_ICMS  
       ELSE 0  
   END ALIQ_ICMS,  
   CASE  
       WHEN EST_TRIB.ICMS > 0  
        OR GLB_MOV_OP.TP_MOV IN(58)  
       THEN DANFE_ITENS.VLR_PROD_ICMS  
       ELSE 0  
   END VLR_ICMS_IT,  
   DANFE_ITENS.VLR_PROD_BASE_ST AS VLR_BASE_SUBS,  
   DANFE_ITENS.VLR_PROD_ICMS_ST AS VLR_ICMS_SUBS,  
   DANFE_ITENS.VLR_PROD_IPI AS VLR_IPI_IT,  
   DANFE_ITENS.PERC_PROD_IPI AS PERC_IPI,  
   0 AS VLR_ISENTO_IT,     
   ISNULL(DANFE_ITENS.CST_PIS, 0) CST_PIS,  
   ISNULL(DANFE_ITENS.CST_COFINS, 0) CST_COFINS,  
     ISNULL(DANFE_ITENS.VLR_BASE_PIS, 0) VLR_BASE_PIS,  
   ISNULL(DANFE_ITENS.PERC_ALIQ_PIS, 0) PERC_ALIQ_PIS,  
   ISNULL(DANFE_ITENS.VLR_PIS, 0) VLR_PIS,  
   ISNULL(DANFE_ITENS.VLR_BASE_COFINS, 0) VLR_BASE_COFINS,  
   ISNULL(DANFE_ITENS.PERC_ALIQ_COFINS, 0) PERC_ALIQ_COFINS,  
   ISNULL(DANFE_ITENS.VLR_COFINS, 0) VLR_COFINS,  
   DANFE_ITENS.VLR_OUTRAS_DESPESAS AS VLR_OUTRAS_DESPESAS_IT,  
   0 AS VLR_BASE_ISSQN,  
   0 AS VLR_ISSQN,  
   0 AS ALIQ_ISSQN,  
   EST_NF_ENT.CD_FORN AS COD_PART,  
   1 AS COD_IMP_ALMOX,  
   0 as LISTA_PROD,  
   EST_TRIB.SUBS_TRIB,  
   EST_TRIB.SUBS_TRIB_ANT,  
   ISNULL(C.CD_NT_OP,0) CD_NT_OP,  
   EST_TRIB.ICMS,  
   EST_TRIB.ICMS_RD,  
   FLAG_OPERADOR_LOG,  
   ISNULL(  
    (  
    SELECT SUM(EST_NF_ENT_IT_EST_PROD_CPL_LOTE.QTDE_PROD) QTDE_PROD  
    FROM EST_NF_ENT_IT_EST_PROD_CPL_LOTE,  
     EST_PROD_CPL_LOTE  
    WHERE EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_EMP = DANFE_ITENS.CD_EMP  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_FILIAL = DANFE_ITENS.CD_FILIAL_DANFE  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_NF = EST_NF_ENT.CD_NF  --- VERIFICAR   
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_PROD = DANFE_ITENS.CD_PROD  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_EMP = EST_PROD_CPL_LOTE.CD_EMP  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_FILIAL = EST_PROD_CPL_LOTE.CD_FILIAL  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_LOTE = EST_PROD_CPL_LOTE.CD_LOTE  
    ), 0) AS QTDE_PROD,  
  
   ISNULL(  
    (  
    SELECT TOP (1) EST_PROD_CPL_LOTE.NR_LOTE  
    FROM EST_NF_ENT_IT_EST_PROD_CPL_LOTE,  
     EST_PROD_CPL_LOTE  
    WHERE EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_EMP = DANFE_ITENS.CD_EMP  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_FILIAL = DANFE_ITENS.CD_FILIAL_DANFE  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_NF = EST_NF_ENT.CD_NF  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_PROD = DANFE_ITENS.CD_PROD  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_EMP = EST_PROD_CPL_LOTE.CD_EMP  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_FILIAL = EST_PROD_CPL_LOTE.CD_FILIAL  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_LOTE = EST_PROD_CPL_LOTE.CD_LOTE  
    ORDER BY EST_PROD_CPL_LOTE.CD_LOTE  
    ), 0) AS NR_LOTE,  
   ISNULL(  
    (  
    SELECT TOP (1) EST_PROD_CPL_LOTE.DT_VALID  
    FROM EST_NF_ENT_IT_EST_PROD_CPL_LOTE,  
     EST_PROD_CPL_LOTE  
    WHERE EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_EMP = DANFE_ITENS.CD_EMP  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_FILIAL = DANFE_ITENS.CD_FILIAL_DANFE  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_NF = EST_NF_ENT.CD_NF  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_PROD = DANFE_ITENS.CD_PROD  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_EMP = EST_PROD_CPL_LOTE.CD_EMP  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_FILIAL = EST_PROD_CPL_LOTE.CD_FILIAL  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_LOTE = EST_PROD_CPL_LOTE.CD_LOTE  
    ), 0) AS DT_VALID,  
   ISNULL(  
    (  
    SELECT TOP (1) EST_PROD_CPL_LOTE.DT_FABR  
    FROM EST_NF_ENT_IT_EST_PROD_CPL_LOTE,  
     EST_PROD_CPL_LOTE  
    WHERE EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_EMP = DANFE_ITENS.CD_EMP  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_FILIAL = DANFE_ITENS.CD_FILIAL_DANFE  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_NF = EST_NF_ENT.CD_NF  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_PROD = DANFE_ITENS.CD_PROD  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_EMP = EST_PROD_CPL_LOTE.CD_EMP  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_FILIAL = EST_PROD_CPL_LOTE.CD_FILIAL  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_LOTE = EST_PROD_CPL_LOTE.CD_LOTE  
    ), 0) AS DT_FABR,  
   EST_PROD_PRECO.VLR_TABELA,  
   CASE  
       WHEN CTR_PMC = 1  
       THEN 1  
       ELSE 0  
   END AS PROD_MEDICAM,  
   (CASE  
    WHEN DS_SIGLA_LINHA = 'S'  
    THEN 0   --' SIGLAS: E - ETICO,   S - SIMILAR, G - GENERICO |0- SIMILAR; 1- GEN?RICO; 2- ?TICO;  
    WHEN DS_SIGLA_LINHA = 'G'  
    THEN 1  
    WHEN DS_SIGLA_LINHA = 'E'  
    THEN 2  
    ELSE 0  
    END) TIPO_MEDIC,  
   '' AS NOME_PART,  
   '' AS ENDERECO,  
   '' AS BAIRRO,  
   '' AS NUMERO,  
   '' AS INSC_EST,  
   '' AS UF,  
   '' AS TEL,  
   01058 AS CD_PAIS,  
   ISNULL(GLB_MUNICIPIO.COD_MUNICIPIO_SINTEGRA, 0) AS COD_IBGE, -- SELECT TOP 2 CEP,* FROM V_EST_NF_ENT  
   '' AS CEP,  
   EST_PROD_CPL.TP_PROD,  
   DANFE_ITENS.CD_BARRA AS  COD_BARRA,  
   EST_PROD.NR_NCM AS COD_NCM,  
   DANFE_ITENS.VLR_PROD_DESC AS VLR_DESC_IT,  
   ISNULL(V_EST_NF_ENT_IMPOSTOS_PIS_COFINS.PIS_NF, 0) PIS_NF,  
   ISNULL(V_EST_NF_ENT_IMPOSTOS_PIS_COFINS.COFINS_NF, 0) COFINS_NF,  
   ISNULL(C.DS_NT_OP,'CFOP NÃO CADASTRADO') AS DS_NT_OP,  
   GLB_MOV_OP.CD_MOV_OP,  
   ISNULL(GLB_MOV_OP.FLAG_NAO_GERAR_ARQ_SPED, 0) AS FLAG_NAO_GERAR_ARQ_SPED,  
   EST_TRIB.CD_TRIB,  
   ISNULL(EST_NF_ENT_CANCEL.CANCEL_FORA_PRAZO, 0) CANCEL_FORA_PRAZO,  
   ISNULL(EST_NF_ENT_NFE_CANCEL.NR_PROTOCOLO, '') AS PROTOCOLO_CANCEL,  
   0 AS POSSUI_DARE,  
   '' AS NUM_DARE,  
   '' AS VALOR_DARE,  
   '' AS DT_VENCTO_DARE,  
   '' AS DT_RECOLHIMENTO_DARE,  
   FLAG_MOV_OP_NFE_CPL, -- SELECT TOP 2 VLR_FRETE_CABE_RATEIO, * FROM V_EST_NF_ENT WHERE CD_FILIAL = 1030 AND NR_NF = 13341  
   ISNULL(EST_NF_ENT_DENEGADA.CD_EMP, 0) AS NOTA_DENEGADA,  
   CASE  
       WHEN EST_NF_ENT.VLR_FRETE = 0  
       THEN 0  
       ELSE CASE  
        WHEN CONVERT(FLOAT, EST_NF_ENT.VLR_NF - EST_NF_ENT.VLR_FRETE) > 0  
        THEN ROUND((ROUND((CONVERT(FLOAT, EST_NF_ENT.VLR_FRETE) / CONVERT(FLOAT, EST_NF_ENT.VLR_NF - EST_NF_ENT.VLR_FRETE)), 9) * (ROUND(((DANFE_ITENS.QTDE_PROD * DANFE_ITENS.VLR_PROD_UNIT) - DANFE_ITENS.VLR_PROD_DESC), 2) + DANFE_ITENS.VLR_PROD_IPI + DANFE_ITENS.VLR_PROD_ICMS_ST)), 2)  
        ELSE 0  
        END  
   END AS VLR_FRETE_CABE_RATEIO,  
  
   ISNULL(EST_NF_SAI_NF_ENT.CD_NF_SAI, 0) CD_NF_SAI,  
   ISNULL(EST_NF_ENT.VLR_FECOP,0 ) AS VLR_FECOP,  
   ISNULL(NR_CEST, '') AS NR_CEST,  
   ISNULL(COD_MUNICIPIO_SINTEGRA, 0) AS COD_IBGE_MUNICIPIO_DESTINATARIO,  
   ISNULL(EST_NF_ENT.END_DESTINATARIO, '') AS END_DESTINATARIO,  
   ISNULL(EST_NF_ENT.BAIRRO_DESTINATARIO, '') AS BAIRRO_DESTINATARIO,  
   ISNULL(EST_NF_ENT.CEP_DESTINATARIO, '') AS CEP_DESTINATARIO,  
   ISNULL(EST_NF_ENT.CIDADE_DESTINATARIO, '') AS CIDADE_DESTINATARIO,  
   ISNULL(EST_NF_ENT.CNPJ_DESTINATARIO, '') AS CNPJ_DESTINATARIO,  
   ISNULL(EST_NF_ENT.INSC_ESTADUAL_DESTINATARIO, '') INSC_ESTADUAL_DESTINATARIO,  
   ISNULL(LEFT(EST_NF_ENT.RZ_DESTINATARIO,60), '') AS RZ_DESTINATARIO,  
   ISNULL(EST_NF_ENT.UF_DESTINATARIO, '') AS UF_DESTINATARIO,  
   ISNULL(EST_NF_ENT.CD_CID_DESTINATARIO, '') CD_CID_DESTINATARIO,  
      
   CASE  
       WHEN GLB_MOV_OP.TP_MOV IN ( 27,21)  --compra de uso e consumo e ativo imobilizado   
       THEN DANFE_ITENS.PERC_PROD_ICMS  
       ELSE ISNULL(DANFE_ITENS.PERC_PROD_ICMS_ST, 0)   
   END ALIQ_ICMS_INTERNO,  
      
   V_APURA_ICMS.VLR_ICMS_DESONERADO,  
   0 AS PART_FLAG_MICRO_EMPRESA,  
   V_APURA_ICMS.BASE_ICMS_SIMPLES,  
   V_APURA_ICMS.ALIQ_SIMPLES,  
   V_APURA_ICMS.VLR_ICMS_SIMPLES,  
   ISNULL(EST_PROD_CPL.VLR_CUSTO_MD,0 )VLR_CUSTO_MD,  
   ISNULL(DANFE_ITENS.VLR_ICMS_DESONERADO, 0) VLR_ICMS_DESONERADO_IT,  
   ISNULL(DANFE_ITENS.VLR_FECOP_ST,0)VLR_FECOP_ST,  
   ISNULL(DANFE_ITENS.VLR_FECOP_ST_RET,0 )VLR_FECOP_ST_RET,  
   ISNULL(DANFE_ITENS.VLR_FECOP,0 )VLR_FECOP_IT,  
   ISNULL(DANFE_ITENS.VLR_FECOP_ST,0 )VLR_FECOP_ST_IT,  
   ISNULL(DANFE_ITENS.VLR_FECOP_ST_RET,0 )VLR_FECOP_ST_RET_IT,  
   ISNULL(EST_PROD.NR_SERV,'') AS NR_SERV,  
   ISNULL(FLAG_VLR_ICMS_DESONERADO_DESCONTO,0) AS FLAG_VLR_ICMS_DESONERADO_DESCONTO,  
   ISNULL(DANFE_ITENS.VLR_ICMS_RET,0) AS VLR_ICMS_RET_IT, 
   ISNULL(DANFE_ITENS.VLR_BASE_ICMS_RET,0) AS VLR_BASE_ICMS_RET_IT,   
   0 AS VLR_ICMS_OP_IT,
   ISNULL(F.PERC_ICMS,0 ) AS ALIQ_INTERNA_PROD,
   0 AS VLR_BASE_SUBS_CONV_IT ,
   0 AS VLR_ICMS_SUBS_CONV_IT ,
   DANFE_ITENS.APRES_QT_CP,
   ISNULL(EST_TRIB.FLAG_CST_60_PARA_SPED,0) FLAG_CST_60_PARA_SPED ,
   ISNULL(EST_NF_ENT_EST_NF_NFE_INU.CD_EMP,0) INUTILIZADO 
   
    FROM DBO.EST_NF_ENT  
     
     INNER JOIN #FILIAIS FL ON DBO.EST_NF_ENT.CD_EMP = FL.CD_EMP  
                AND DBO.EST_NF_ENT.CD_FILIAL = FL.CD_FILIAL  
     INNER JOIN   
     EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT VINC_DANFE ON    
  EST_NF_ENT.CD_EMP = VINC_DANFE.CD_EMP  
  AND EST_NF_ENT.CD_FILIAL = VINC_DANFE.CD_FILIAL  
  AND EST_NF_ENT.CD_NF = VINC_DANFE.CD_NF  
       
  INNER JOIN EST_NF_ENT_DANFE_PROVISORIO_ITENS DANFE_ITENS   ON    
                          VINC_DANFE.CD_EMP = DANFE_ITENS.CD_EMP  
        AND VINC_DANFE.CD_CTR = DANFE_ITENS.CD_CTR  
    
   -- LEFT JOIN DBO.GLB_NT_OP ON    
   --  CASE WHEN DANFE_ITENS.CFOP = '5405' THEN  
   --      '1403'  
   --    WHEN DANFE_ITENS.CFOP = '6109' OR DANFE_ITENS.CFOP = '6110' THEN  
   --     '2102'  
   --ELSE  
  
   --   CASE WHEN LEFT(DANFE_ITENS.CFOP,1) = 5 THEN  
   --        '1' +  RIGHT(DANFE_ITENS.CFOP,3)  
   --   ELSE  
   --        '2' +  RIGHT(DANFE_ITENS.CFOP,3)  
   --   END   
   --END     
     
   -- = REPLACE(GLB_NT_OP.CD_NT_OP_FC,'.','')  
  
     INNER JOIN DBO.EST_PROD ON DBO.EST_PROD.CD_EMP =  DANFE_ITENS.CD_EMP  
        AND DBO.EST_PROD.CD_PROD =  DANFE_ITENS.CD_PROD  
       
     INNER JOIN DBO.GLB_MOV_OP ON DBO.EST_NF_ENT.CD_NT_MOV = DBO.GLB_MOV_OP.CD_MOV_OP  
     INNER JOIN DBO.EST_NF_SERIE ON DBO.EST_NF_ENT.CD_EMP = DBO.EST_NF_SERIE.CD_EMP  
             AND DBO.EST_NF_ENT.CD_NF_SERIE = DBO.EST_NF_SERIE.CD_NF_SERIE  
     LEFT JOIN V_APURA_ICMS ON EST_NF_ENT.CD_EMP = V_APURA_ICMS.CD_EMP  
        AND EST_NF_ENT.CD_FILIAL = V_APURA_ICMS.CD_FILIAL  
        AND EST_NF_ENT.CD_NF = V_APURA_ICMS.CD_NF  
        AND V_APURA_ICMS.TIPO = 0  
   
     INNER JOIN DBO.EST_PROD_EST_ARV_MERCADOLOGICA AS ARV ON EST_PROD.CD_EMP = ARV.CD_EMP  
                 AND EST_PROD.CD_PROD = ARV.CD_PROD  
     INNER JOIN DBO.EST_ARV_MERC_LINHA AS LINHA ON ARV.CD_EMP = LINHA.CD_EMP  
               AND ARV.CD_ARV_MERC_LINHA = LINHA.CD_ARV_MERC_LINHA  
  
     INNER JOIN GLB_USU ON EST_NF_ENT.CD_USU = GLB_USU.CD_USU  
     LEFT JOIN V_EST_NF_ENT_IMPOSTOS_PIS_COFINS ON V_EST_NF_ENT_IMPOSTOS_PIS_COFINS.CD_EMP = EST_NF_ENT.CD_EMP  
      AND V_EST_NF_ENT_IMPOSTOS_PIS_COFINS.CD_FILIAL = EST_NF_ENT.CD_FILIAL  
      AND V_EST_NF_ENT_IMPOSTOS_PIS_COFINS.CD_NF = EST_NF_ENT.CD_NF  
  
   LEFT JOIN DBO.EST_TRIB   ON   
  
    CASE WHEN CD_SIT_OPERA_SIMPLES_NACIO = 0 THEN  
       CONVERT(VARCHAR,DANFE_ITENS.ORIGEM ) + CONVERT(VARCHAR,REPLICATE('0', 2 - LEN(DANFE_ITENS.CD_TRIB)) + RTrim(DANFE_ITENS.CD_TRIB))  
    ELSE  DANFE_ITENS.CD_SIT_OPERA_SIMPLES_NACIO END  
  
   = EST_TRIB.CD_TRIB_FC  
  
   AND EST_TRIB.TIPO_REGIME_EMPRESA  =  CASE WHEN  CD_SIT_OPERA_SIMPLES_NACIO = 0 THEN 0 ELSE 1 END  
  
    LEFT JOIN EST_NF_ENT_EF_NF_CTR_NF ON EST_NF_ENT_EF_NF_CTR_NF.CD_EMP = EST_NF_ENT.CD_EMP  
         AND EST_NF_ENT_EF_NF_CTR_NF.CD_FILIAL = EST_NF_ENT.CD_FILIAL  
         AND EST_NF_ENT_EF_NF_CTR_NF.CD_NF_ENT = EST_NF_ENT.CD_NF  
  
   LEFT JOIN V_EST_NF_ENT_NFE_SPED ON V_EST_NF_ENT_NFE_SPED.CD_EMP = EST_NF_ENT.CD_EMP  
       AND V_EST_NF_ENT_NFE_SPED.CD_FILIAL = EST_NF_ENT.CD_FILIAL  
       AND V_EST_NF_ENT_NFE_SPED.CD_NF = EST_NF_ENT.CD_NF  
  
        LEFT JOIN GLB_CID ON EST_NF_ENT.CD_CID_DESTINATARIO = GLB_CID.CD_CID  
     LEFT OUTER JOIN GLB_CID_GLB_MUNICIPIO ON GLB_CID.CD_CID = GLB_CID_GLB_MUNICIPIO.CD_CID  
     LEFT OUTER JOIN GLB_MUNICIPIO ON GLB_CID_GLB_MUNICIPIO.CD_MUNICIPIO = GLB_MUNICIPIO.CD_MUNICIPIO  
     LEFT JOIN EST_NF_ENT_CANCEL ON EST_NF_ENT_CANCEL.CD_EMP = EST_NF_ENT.CD_EMP  
            AND EST_NF_ENT_CANCEL.CD_FILIAL = EST_NF_ENT.CD_FILIAL  
            AND EST_NF_ENT_CANCEL.CD_NF = EST_NF_ENT.CD_NF  
     LEFT JOIN EST_NF_ENT_NFE_CANCEL ON EST_NF_ENT_CANCEL.CD_EMP = EST_NF_ENT_NFE_CANCEL.CD_EMP  
            AND EST_NF_ENT_CANCEL.CD_FILIAL = EST_NF_ENT_NFE_CANCEL.CD_FILIAL  
            AND EST_NF_ENT_CANCEL.CD_NF = EST_NF_ENT_NFE_CANCEL.CD_NF  
     LEFT JOIN EST_NF_ENT_DENEGADA ON EST_NF_ENT.CD_EMP = EST_NF_ENT_DENEGADA.CD_EMP  
          AND EST_NF_ENT.CD_FILIAL = EST_NF_ENT_DENEGADA.CD_FILIAL  
          AND EST_NF_ENT.CD_NF = EST_NF_ENT_DENEGADA.CD_NF  
     LEFT JOIN EST_NF_SAI_NF_ENT ON EST_NF_ENT.CD_EMP = EST_NF_SAI_NF_ENT.CD_EMP  
            AND EST_NF_ENT.CD_FILIAL = EST_NF_SAI_NF_ENT.CD_FILIAL  
            AND EST_NF_ENT.CD_NF = EST_NF_SAI_NF_ENT.CD_NF_ENT  
    
     INNER JOIN EST_PROD_CPL ON DANFE_ITENS.CD_EMP = EST_PROD_CPL.CD_EMP  
        AND DANFE_ITENS.CD_PROD = EST_PROD_CPL.CD_PROD  
        AND VINC_DANFE.CD_FILIAL = EST_PROD_CPL.CD_FILIAL
		
	 OUTER APPLY UFD_MONTAR_CFOP (EST_NF_ENT.CD_EMP,EST_NF_ENT.CD_FILIAL,EST_NF_ENT.CD_NT_MOV,EST_TRIB.CD_TRIB,EST_NF_ENT.CD_FORN,0,GLB_MOV_OP.TP_MOV,0) C  
  
    LEFT JOIN EST_PROD_PRECO ON EST_PROD_PRECO.CD_EMP = DANFE_ITENS.CD_EMP  
     AND EST_PROD_PRECO.CD_FILIAL = DANFE_ITENS.CD_FILIAL_DANFE  
                    AND EST_PROD_PRECO.CD_PROD = DANFE_ITENS.CD_PROD  
  
   LEFT JOIN EST_NF_ENT_DAR ON EST_NF_ENT_DAR.CD_EMP = EST_NF_ENT.CD_EMP  
     AND EST_NF_ENT_DAR.CD_FILIAL = EST_NF_ENT.CD_FILIAL  
     AND EST_NF_ENT_DAR.CD_NF = EST_NF_ENT.CD_NF  
        
     LEFT JOIN EST_NF_ENT_DAR_IT ON EST_NF_ENT_DAR.CD_EMP = EST_NF_ENT_DAR_IT.CD_EMP  
         AND EST_NF_ENT_DAR.CD_FILIAL = EST_NF_ENT_DAR_IT.CD_FILIAL  
         AND EST_NF_ENT_DAR.CD_NF = EST_NF_ENT_DAR_IT.CD_NF  
         AND DANFE_ITENS.CD_PROD = EST_NF_ENT_DAR_IT.CD_PROD
	LEFT JOIN EST_NF_ENT_EST_NF_NFE_INU  
		ON     EST_NF_ENT.CD_EMP    = EST_NF_ENT_EST_NF_NFE_INU.CD_EMP
		AND    EST_NF_ENT.CD_FILIAL = EST_NF_ENT_EST_NF_NFE_INU.CD_FILIAL
		AND    EST_NF_ENT.CD_NF     = EST_NF_ENT_EST_NF_NFE_INU.CD_NF
   
    OUTER APPLY UFD_TRIB_PROD_ALIQ_INTERNA(EST_NF_ENT.CD_EMP, 
				DANFE_ITENS.CD_PROD,  
				GLB_CID.UF) F 
		   
    WHERE    
    EST_NF_ENT.CD_EMP = 1   
    AND EST_NF_ENT.DT_ENT BETWEEN @DT_INI AND @DT_FIM  
    AND EST_NF_ENT_DAR_IT.CD_EMP IS NULL  
    
    UNION ALL  
   
---- NOTAS DE ENTRADA VINCULO COM DANFE   
---- (INNER JOIN EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT)  
---- COM DARE / CONSULTA 4  
    
 SELECT EST_NF_ENT.CD_EMP,  
   EST_NF_ENT.CD_FILIAL,  
   EST_NF_ENT.CD_NF AS CD_DOC,  
   0 AS IND_OPER,  
   (CASE  
    WHEN EST_NF_ENT_EF_NF_CTR_NF.CD_NF_ENT > 0  
    THEN 0  
    ELSE 1  
    END) AS IND_EMIT,  
   EST_NF_ENT.CD_FORN AS CD_ORIG,  
   LEFT(EST_NF_SERIE.MODELO,2),  
   EST_NF_SERIE.TP_SR_NFE,  
   EST_NF_SERIE.SERIE,  
   EST_NF_ENT.NR_NF AS NR_DOC,  
   ISNULL(V_EST_NF_ENT_NFE_SPED.NR_AUTORIZADOR, '') AS CHAVE_NFE,  
   EST_NF_ENT.DT_EMI_NF,  
   EST_NF_ENT.DT_ENT AS DT_ENT_SAI,  
   EST_NF_ENT.VLR_NF,  
   PGTO_VISTA_PRAZO AS IND_PAGTO,  
   EST_NF_ENT.VLR_DESC,  
   0 AS VLR_ABAT,  
   CASE  
       WHEN ISNULL(EST_NF_ENT.FLAG_NF_PROD_TOTAL_BRUTO, 0) = 0  
       THEN EST_NF_ENT.VLR_TOT_PROD  
       ELSE EST_NF_ENT.VLR_TOT_PROD - EST_NF_ENT.VLR_DESC  
   END AS VLR_MERC,  
   1 AS TP_FRETE,  
   VLR_FRETE,  
   0 AS VLR_SEG,  
   EST_NF_ENT.VLR_OUTROS AS VLR_OUTRAS_DESP_CAB,  
   V_APURA_ICMS.VLR_BASE_ICMS_CAB,  
   V_APURA_ICMS.VLR_ICMS_CAB,  
   EST_NF_ENT.VLR_BASE_ICMS_SUBS AS VLR_BASE_ICMS_SUBS_CAB,  
   EST_NF_ENT.VLR_ICMS_SUBS AS VLR_ICMS_SUBS_CAB,  
   EST_NF_ENT.VLR_IPI AS VLR_IPI_CAB,  
   EST_NF_ENT.VRL_ISENTAS AS VLR_ISENTAS_CAB,  
   '' AS CGC_CPF,  
   EST_NF_ENT.STS_NF,  
   EST_NF_ENT.VLR_OUTROS AS VLR_SERV_NT,  
   0 AS VLR_BASE_ISSQN_CAB,  
   0 VLR_ISSQN_CAB,  
   0 AS VLR_BASE_IRRF_CAB,  
   0 AS VLR_IRRF_CAB,  
   0 AS CD_TRANSP,  
   QT_VOL,  
   PESO_BRT,  
   PESO_LIQ,  
   ISNULL(  
    (  
    SELECT TOP 1 CD_EMP  
    FROM PG_CRED_EST_NF_ENT  
    WHERE EST_NF_ENT.CD_EMP = PG_CRED_EST_NF_ENT.CD_EMP  
      AND EST_NF_ENT.CD_FILIAL = PG_CRED_EST_NF_ENT.CD_FILIAL  
      AND EST_NF_ENT.CD_NF = PG_CRED_EST_NF_ENT.CD_NF  
    ), 0) AS POSSUI_DUPL,  
   GLB_MOV_OP.TP_MOV,  
   GLB_MOV_OP.TP_MOV_OP_TRANSA,  
   GLB_MOV_OP.DEST_ORIG,  
   CASE  
       WHEN GLB_MOV_OP.DEST_ORIG = 0  
       THEN 'CLI'  
       WHEN GLB_MOV_OP.DEST_ORIG = 1  
       THEN 'FOR'  
       ELSE 'FIL'  
   END AS TIPO_PART,  
   DANFE_ITENS.CD_CTR_IT AS NUM_ITEM,  
   DANFE_ITENS.CD_PROD AS COD_ITEM,  
   EST_PROD_CPL.TP_PROD AS TIPO_ITEM,  
   EST_PROD.DS_PROD, --   
            DANFE_ITENS.QTDE_PROD AS QT_IT,  
   DANFE_ITENS.VLR_PROD_UNIT AS VLR_IT,  
   DANFE_ITENS.VLR_PROD_TOTAL AS VLR_ITEM,  
   TRIB_DARE.CD_TRIB_FC AS CST_ICMS,  
   TRIB_DARE.TRIB_FC_ELETRONICO,  
     
   --CASE WHEN DANFE_ITENS.CFOP = '5405' THEN  
   --      '1403'  
   --              WHEN DANFE_ITENS.CFOP = '6109' OR DANFE_ITENS.CFOP = '6110' THEN  
   --      '2102'  
   --ELSE  
   --   CASE WHEN LEFT(DANFE_ITENS.CFOP,1) = 5 THEN  
   --        '1' +  RIGHT(DANFE_ITENS.CFOP,3)  
   --   ELSE  
   --        '2' +  RIGHT(DANFE_ITENS.CFOP,3)  
   --   END   
   --END AS CFOP,
   
   C.CD_NT_OP_FC AS  CFOP,
     
   CASE  
       WHEN TRIB_DARE.ICMS > 0  
       THEN EST_NF_ENT_DAR_IT.VLR_BASE_ICMS  
       ELSE 0  
   END AS VLR_BASE_ICMS,  
   CASE  
       WHEN TRIB_DARE.ICMS > 0  
       THEN EST_NF_ENT_DAR_IT.PERC_ICMS  
       ELSE 0  
   END ALIQ_ICMS,  
   CASE  
       WHEN TRIB_DARE.ICMS > 0  
       THEN EST_NF_ENT_DAR_IT.VLR_ICMS  
       ELSE 0  
   END AS VLR_ICMS_IT,  
   EST_NF_ENT_DAR_IT.VLR_BASE_SUBS AS VLR_BASE_SUBS,  
   EST_NF_ENT_DAR_IT.VLR_ICMS_SUBS AS VLR_ICMS_SUBS,  
   DANFE_ITENS.VLR_PROD_IPI AS VLR_IPI_IT,  
   DANFE_ITENS.PERC_PROD_IPI AS PERC_IPI,  
   0 AS VLR_ISENTO_IT,  
   ISNULL(DANFE_ITENS.CST_PIS, 0) CST_PIS,  
   ISNULL(DANFE_ITENS.CST_COFINS, 0) CST_COFINS,  
   ISNULL(DANFE_ITENS.VLR_BASE_PIS, 0) VLR_BASE_PIS,  
   ISNULL(DANFE_ITENS.PERC_ALIQ_PIS, 0) PERC_ALIQ_PIS,  
   ISNULL(DANFE_ITENS.VLR_PIS, 0) VLR_PIS,  
   ISNULL(DANFE_ITENS.VLR_BASE_COFINS, 0) VLR_BASE_COFINS,  
   ISNULL(DANFE_ITENS.PERC_ALIQ_COFINS, 0) PERC_ALIQ_COFINS,  
   ISNULL(DANFE_ITENS.VLR_COFINS, 0) VLR_COFINS,  
   DANFE_ITENS.VLR_OUTRAS_DESPESAS AS VLR_OUTRAS_DESPESAS_IT,  
   0 AS VLR_BASE_ISSQN,  
   0 AS VLR_ISSQN,  
   0 AS ALIQ_ISSQN,  
   EST_NF_ENT.CD_FORN AS COD_PART,  
   1 AS COD_IMP_ALMOX,  
   0 as  LISTA_PROD,  
   TRIB_DARE.SUBS_TRIB,  
   TRIB_DARE.SUBS_TRIB_ANT,  
   ISNULL(C.CD_NT_OP,0) CD_NT_OP, 
   TRIB_DARE.ICMS,  
   TRIB_DARE.ICMS_RD,  
   FLAG_OPERADOR_LOG,  
   ISNULL(  
    (  
    SELECT SUM(EST_NF_ENT_IT_EST_PROD_CPL_LOTE.QTDE_PROD) QTDE_PROD  
    FROM EST_NF_ENT_IT_EST_PROD_CPL_LOTE,  
     EST_PROD_CPL_LOTE  
    WHERE EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_EMP = DANFE_ITENS.CD_EMP  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_FILIAL = DANFE_ITENS.CD_FILIAL_DANFE  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_NF = EST_NF_ENT.CD_NF  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_PROD = DANFE_ITENS.CD_PROD  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_EMP = EST_PROD_CPL_LOTE.CD_EMP  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_FILIAL = EST_PROD_CPL_LOTE.CD_FILIAL  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_LOTE = EST_PROD_CPL_LOTE.CD_LOTE  
    ), 0) AS QTDE_PROD,  
   ISNULL(  
    (  
    SELECT TOP (1) EST_PROD_CPL_LOTE.NR_LOTE  
    FROM EST_NF_ENT_IT_EST_PROD_CPL_LOTE,  
     EST_PROD_CPL_LOTE  
    WHERE EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_EMP = DANFE_ITENS.CD_EMP  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_FILIAL = DANFE_ITENS.CD_FILIAL_DANFE  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_NF = EST_NF_ENT.CD_NF  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_PROD = DANFE_ITENS.CD_PROD  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_EMP = EST_PROD_CPL_LOTE.CD_EMP  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_FILIAL = EST_PROD_CPL_LOTE.CD_FILIAL  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_LOTE = EST_PROD_CPL_LOTE.CD_LOTE  
    ORDER BY EST_PROD_CPL_LOTE.CD_LOTE  
    ), 0) AS NR_LOTE,  
   ISNULL(  
    (  
    SELECT TOP (1) EST_PROD_CPL_LOTE.DT_VALID  
    FROM EST_NF_ENT_IT_EST_PROD_CPL_LOTE,  
     EST_PROD_CPL_LOTE  
    WHERE EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_EMP = DANFE_ITENS.CD_EMP  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_FILIAL = DANFE_ITENS.CD_FILIAL_DANFE  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_NF = EST_NF_ENT.CD_NF  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_PROD = DANFE_ITENS.CD_PROD  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_EMP = EST_PROD_CPL_LOTE.CD_EMP  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_FILIAL = EST_PROD_CPL_LOTE.CD_FILIAL  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_LOTE = EST_PROD_CPL_LOTE.CD_LOTE  
    ), 0) AS DT_VALID,  
   ISNULL(  
    (  
    SELECT TOP (1) EST_PROD_CPL_LOTE.DT_FABR  
    FROM EST_NF_ENT_IT_EST_PROD_CPL_LOTE,  
     EST_PROD_CPL_LOTE  
    WHERE EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_EMP = DANFE_ITENS.CD_EMP  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_FILIAL = DANFE_ITENS.CD_FILIAL_DANFE  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_NF = EST_NF_ENT.CD_NF  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_PROD = DANFE_ITENS.CD_PROD  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_EMP = EST_PROD_CPL_LOTE.CD_EMP  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_FILIAL = EST_PROD_CPL_LOTE.CD_FILIAL  
      AND EST_NF_ENT_IT_EST_PROD_CPL_LOTE.CD_LOTE = EST_PROD_CPL_LOTE.CD_LOTE  
    ), 0) AS DT_FABR,  
   EST_PROD_PRECO.VLR_TABELA,  
   CASE  
       WHEN CTR_PMC = 1  
       THEN 1  
       ELSE 0  
   END AS PROD_MEDICAM,  
   (CASE  
    WHEN DS_SIGLA_LINHA = 'S'  
    THEN 0   --' SIGLAS: E - ETICO,   S - SIMILAR, G - GENERICO |0- SIMILAR; 1- GEN?RICO; 2- ?TICO;  
    WHEN DS_SIGLA_LINHA = 'G'  
    THEN 1  
    WHEN DS_SIGLA_LINHA = 'E'  
    THEN 2  
    ELSE 0  
    END) TIPO_MEDIC,  
   '' AS NOME_PART,  
   '' AS ENDERECO,  
   '' AS BAIRRO,  
   '' AS NUMERO,  
   '' AS INSC_EST,  
   '' AS UF,  
   '' AS TEL,  
   01058 AS CD_PAIS,  
   ISNULL(GLB_MUNICIPIO.COD_MUNICIPIO_SINTEGRA, 0) AS COD_IBGE, -- SELECT TOP 2 CEP,* FROM V_EST_NF_ENT  
   '' AS CEP,  
   EST_PROD_CPL.TP_PROD,  
   ISNULL(  
    (  
    SELECT TOP (1) CD_BARRA  
    FROM EST_PROD_CD_BARRA  
    WHERE CD_EMP = DANFE_ITENS.CD_EMP  
      AND CD_PROD = DANFE_ITENS.CD_PROD  
    ORDER BY CD_BARRA  
    ), '') AS COD_BARRA,  
   EST_PROD.NR_NCM AS COD_NCM,  
   DANFE_ITENS.VLR_PROD_DESC AS VLR_DESC_IT,  
   ISNULL(V_EST_NF_ENT_IMPOSTOS_PIS_COFINS.PIS_NF, 0) PIS_NF,  
   ISNULL(V_EST_NF_ENT_IMPOSTOS_PIS_COFINS.COFINS_NF, 0) COFINS_NF,  
   ISNULL(C.DS_NT_OP,'CFOP NÃO CADASTRADO') AS DS_NT_OP, 
   GLB_MOV_OP.CD_MOV_OP,  
   ISNULL(GLB_MOV_OP.FLAG_NAO_GERAR_ARQ_SPED, 0) AS FLAG_NAO_GERAR_ARQ_SPED,  
   EST_NF_ENT_DAR_IT.CD_TRIB,  
   ISNULL(EST_NF_ENT_CANCEL.CANCEL_FORA_PRAZO, 0) CANCEL_FORA_PRAZO,  
   ISNULL(EST_NF_ENT_NFE_CANCEL.NR_PROTOCOLO, '') AS PROTOCOLO_CANCEL,  
   1 AS POSSUI_DARE,  
   EST_NF_ENT_DAR.NR_DAR AS NUM_DARE,  
   EST_NF_ENT_DAR.VLR_DAR AS VALOR_DARE,  
   EST_NF_ENT_DAR.DT_VENCTO AS DT_VENCTO_DARE,  
   EST_NF_ENT_DAR.DT_RECOLHIMENTO AS DT_RECOLHIMENTO_DARE,  
   FLAG_MOV_OP_NFE_CPL, -- SELECT TOP 2 VLR_FRETE_CABE_RATEIO, * FROM V_EST_NF_ENT WHERE CD_FILIAL = 1030 AND NR_NF = 13341  
   ISNULL(EST_NF_ENT_DENEGADA.CD_EMP, 0) AS NOTA_DENEGADA,  
   CASE  
       WHEN EST_NF_ENT.VLR_FRETE = 0  
       THEN 0  
       ELSE CASE  
        WHEN CONVERT(FLOAT, EST_NF_ENT.VLR_NF - EST_NF_ENT.VLR_FRETE) > 0  
        THEN ROUND((ROUND((CONVERT(FLOAT, EST_NF_ENT.VLR_FRETE) / CONVERT(FLOAT, EST_NF_ENT.VLR_NF - EST_NF_ENT.VLR_FRETE)), 9) * (ROUND(((DANFE_ITENS.QTDE_PROD * DANFE_ITENS.VLR_PROD_UNIT) - DANFE_ITENS.VLR_PROD_DESC), 2) + DANFE_ITENS.VLR_PROD_IPI + DANFE_ITENS.VLR_PROD_ICMS_ST)), 2)  
        ELSE 0  
        END  
   END AS VLR_FRETE_CABE_RATEIO,  
   ISNULL(EST_NF_SAI_NF_ENT.CD_NF_SAI, 0) CD_NF_SAI,  
   ISNULL(EST_NF_ENT.VLR_FECOP,0)VLR_FECOP,  
   ISNULL(NR_CEST, '') AS NR_CEST,  
   ISNULL(COD_MUNICIPIO_SINTEGRA, 0) AS COD_IBGE_MUNICIPIO_DESTINATARIO,  
   ISNULL(EST_NF_ENT.END_DESTINATARIO, '') AS END_DESTINATARIO,  
   ISNULL(EST_NF_ENT.BAIRRO_DESTINATARIO, '') AS BAIRRO_DESTINATARIO,  
   ISNULL(EST_NF_ENT.CEP_DESTINATARIO, '') AS CEP_DESTINATARIO,  
   ISNULL(EST_NF_ENT.CIDADE_DESTINATARIO, '') AS CIDADE_DESTINATARIO,  
   ISNULL(EST_NF_ENT.CNPJ_DESTINATARIO, '') AS CNPJ_DESTINATARIO,  
   ISNULL(EST_NF_ENT.INSC_ESTADUAL_DESTINATARIO, '') INSC_ESTADUAL_DESTINATARIO,  
   ISNULL(EST_NF_ENT.RZ_DESTINATARIO, '') AS RZ_DESTINATARIO,  
   ISNULL(EST_NF_ENT.UF_DESTINATARIO, '') AS UF_DESTINATARIO,  
   ISNULL(EST_NF_ENT.CD_CID_DESTINATARIO, '') CD_CID_DESTINATARIO,  
   ISNULL(DANFE_ITENS.PERC_PROD_ICMS_ST, 0) AS ALIQ_ICMS_INTERNO,  
   V_APURA_ICMS.VLR_ICMS_DESONERADO,  
   0 AS PART_FLAG_MICRO_EMPRESA,  
   V_APURA_ICMS.BASE_ICMS_SIMPLES,  
   V_APURA_ICMS.ALIQ_SIMPLES,  
   V_APURA_ICMS.VLR_ICMS_SIMPLES,  
   ISNULL(EST_PROD_CPL.VLR_CUSTO_MD,0 )VLR_CUSTO_MD,  
      ISNULL(DANFE_ITENS.VLR_ICMS_DESONERADO, 0) VLR_ICMS_DESONERADO_IT,  
   ISNULL(DANFE_ITENS.VLR_FECOP_ST,0)VLR_FECOP_ST,  
   ISNULL(DANFE_ITENS.VLR_FECOP_ST_RET,0 )VLR_FECOP_ST_RET,  
   ISNULL(DANFE_ITENS.VLR_FECOP,0 )VLR_FECOP_IT,  
   ISNULL(DANFE_ITENS.VLR_FECOP_ST,0 )VLR_FECOP_ST_IT,  
   ISNULL(DANFE_ITENS.VLR_FECOP_ST_RET,0 )VLR_FECOP_ST_RET_IT,  
   ISNULL(EST_PROD.NR_SERV,'') AS NR_SERV,  
   ISNULL(FLAG_VLR_ICMS_DESONERADO_DESCONTO,0) FLAG_VLR_ICMS_DESONERADO_DESCONTO,
   ISNULL(VLR_ICMS_RET,0) AS VLR_ICMS_RET_IT,
   ISNULL(DANFE_ITENS.VLR_BASE_ICMS_RET,0) AS VLR_BASE_ICMS_RET_IT,
   0 AS VLR_ICMS_OP_IT,
   ISNULL(F.PERC_ICMS,0) AS ALIQ_INTERNA_PROD,
   0 AS VLR_BASE_SUBS_CONV_IT ,
   0 AS  VLR_ICMS_SUBS_CONV_IT, 
   DANFE_ITENS.APRES_QT_CP,
   ISNULL(TRIB_DARE.FLAG_CST_60_PARA_SPED,0) FLAG_CST_60_PARA_SPED,
   ISNULL(EST_NF_ENT_EST_NF_NFE_INU.CD_EMP,0) INUTILIZADO  
      
    FROM DBO.EST_NF_ENT    
     INNER JOIN #FILIAIS FL ON DBO.EST_NF_ENT.CD_EMP = FL.CD_EMP  
         AND DBO.EST_NF_ENT.CD_FILIAL = FL.CD_FILIAL  
      INNER JOIN   
     EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT VINC_DANFE ON    
  EST_NF_ENT.CD_EMP = VINC_DANFE.CD_EMP  
  AND EST_NF_ENT.CD_FILIAL = VINC_DANFE.CD_FILIAL  
  AND EST_NF_ENT.CD_NF = VINC_DANFE.CD_NF  
    
  INNER JOIN EST_NF_ENT_DANFE_PROVISORIO_ITENS DANFE_ITENS   ON    
                          VINC_DANFE.CD_EMP = DANFE_ITENS.CD_EMP  
        AND VINC_DANFE.CD_CTR = DANFE_ITENS.CD_CTR   
  
     INNER JOIN DBO.EST_PROD ON DBO.EST_PROD.CD_EMP = DANFE_ITENS.CD_EMP  
        AND DBO.EST_PROD.CD_PROD =  DANFE_ITENS.CD_PROD   
     INNER JOIN DBO.GLB_MOV_OP ON DBO.EST_NF_ENT.CD_NT_MOV = DBO.GLB_MOV_OP.CD_MOV_OP  
     INNER JOIN DBO.EST_NF_SERIE ON DBO.EST_NF_ENT.CD_EMP = DBO.EST_NF_SERIE.CD_EMP  
            AND DBO.EST_NF_ENT.CD_NF_SERIE = DBO.EST_NF_SERIE.CD_NF_SERIE  
     LEFT JOIN V_APURA_ICMS ON EST_NF_ENT.CD_EMP = V_APURA_ICMS.CD_EMP  
        AND EST_NF_ENT.CD_FILIAL = V_APURA_ICMS.CD_FILIAL  
        AND EST_NF_ENT.CD_NF = V_APURA_ICMS.CD_NF  
        AND V_APURA_ICMS.TIPO = 0  
     INNER JOIN EST_PROD_CPL ON DANFE_ITENS.CD_EMP = EST_PROD_CPL.CD_EMP  
        AND DANFE_ITENS.CD_FILIAL_DANFE = EST_PROD_CPL.CD_FILIAL  
        AND DANFE_ITENS.CD_PROD = EST_PROD_CPL.CD_PROD  
     INNER JOIN DBO.EST_PROD_EST_ARV_MERCADOLOGICA AS ARV ON EST_PROD.CD_EMP = ARV.CD_EMP  
                 AND EST_PROD.CD_PROD = ARV.CD_PROD  
     INNER JOIN DBO.EST_ARV_MERC_LINHA AS LINHA ON ARV.CD_EMP = LINHA.CD_EMP  
               AND ARV.CD_ARV_MERC_LINHA = LINHA.CD_ARV_MERC_LINHA  
     LEFT JOIN V_EST_NF_ENT_IMPOSTOS_PIS_COFINS ON V_EST_NF_ENT_IMPOSTOS_PIS_COFINS.CD_EMP = EST_NF_ENT.CD_EMP  
      AND V_EST_NF_ENT_IMPOSTOS_PIS_COFINS.CD_FILIAL = EST_NF_ENT.CD_FILIAL  
      AND V_EST_NF_ENT_IMPOSTOS_PIS_COFINS.CD_NF = EST_NF_ENT.CD_NF  
     LEFT JOIN EST_PROD_PRECO ON EST_PROD_PRECO.CD_EMP = DANFE_ITENS.CD_EMP  
         AND EST_PROD_PRECO.CD_FILIAL = DANFE_ITENS.CD_FILIAL_DANFE  
         AND EST_PROD_PRECO.CD_PROD = DANFE_ITENS.CD_PROD  
  
   INNER JOIN EST_NF_ENT_DAR ON EST_NF_ENT_DAR.CD_EMP = EST_NF_ENT.CD_EMP  
   AND EST_NF_ENT_DAR.CD_FILIAL = EST_NF_ENT.CD_FILIAL  
   AND EST_NF_ENT_DAR.CD_NF = EST_NF_ENT.CD_NF  
      
     INNER JOIN EST_NF_ENT_DAR_IT ON EST_NF_ENT_DAR.CD_EMP = EST_NF_ENT_DAR_IT.CD_EMP  
         AND EST_NF_ENT_DAR.CD_FILIAL = EST_NF_ENT_DAR_IT.CD_FILIAL  
         AND EST_NF_ENT_DAR.CD_NF = EST_NF_ENT_DAR_IT.CD_NF  
         AND DANFE_ITENS.CD_PROD = EST_NF_ENT_DAR_IT.CD_PROD  

   INNER JOIN DBO.EST_TRIB TRIB_DARE  ON   
  
    CASE WHEN CD_SIT_OPERA_SIMPLES_NACIO = 0 THEN  
       CONVERT(VARCHAR,DANFE_ITENS.ORIGEM ) + CONVERT(VARCHAR,REPLICATE('0', 2 - LEN(DANFE_ITENS.CD_TRIB)) + RTrim(DANFE_ITENS.CD_TRIB))  
    ELSE  DANFE_ITENS.CD_SIT_OPERA_SIMPLES_NACIO END  
       
   = TRIB_DARE.CD_TRIB_FC  
  
   AND TRIB_DARE.TIPO_REGIME_EMPRESA  =  CASE WHEN  CD_SIT_OPERA_SIMPLES_NACIO = 0 THEN 0 ELSE 1 END  
    
   -- INNER JOIN DBO.GLB_NT_OP ON  -- SELECT * FROM EST_TRIB  
      
   --  CASE WHEN DANFE_ITENS.CFOP = '5405' THEN  
   --      '1403'  
   --    WHEN DANFE_ITENS.CFOP = '6109' OR DANFE_ITENS.CFOP = '6110' THEN  
   --     '2102'  
   --ELSE  
   --   CASE WHEN LEFT(DANFE_ITENS.CFOP,1) = 5 THEN  
   --        '1' +  RIGHT(DANFE_ITENS.CFOP,3)  
   --   ELSE  
   --        '2' +  RIGHT(DANFE_ITENS.CFOP,3)  
   --   END   
   --END     
     
   -- = REPLACE(GLB_NT_OP.CD_NT_OP_FC,'.','')       
       
     LEFT JOIN EST_NF_ENT_EF_NF_CTR_NF ON EST_NF_ENT_EF_NF_CTR_NF.CD_EMP = EST_NF_ENT.CD_EMP  
          AND EST_NF_ENT_EF_NF_CTR_NF.CD_FILIAL = EST_NF_ENT.CD_FILIAL  
          AND EST_NF_ENT_EF_NF_CTR_NF.CD_NF_ENT = EST_NF_ENT.CD_NF  
     LEFT JOIN V_EST_NF_ENT_NFE_SPED ON V_EST_NF_ENT_NFE_SPED.CD_EMP = EST_NF_ENT.CD_EMP  
            AND V_EST_NF_ENT_NFE_SPED.CD_FILIAL = EST_NF_ENT.CD_FILIAL  
            AND V_EST_NF_ENT_NFE_SPED.CD_NF = EST_NF_ENT.CD_NF  
     INNER JOIN GLB_USU ON EST_NF_ENT.CD_USU = GLB_USU.CD_USU 
	 
	 OUTER APPLY UFD_MONTAR_CFOP (EST_NF_ENT.CD_EMP,EST_NF_ENT.CD_FILIAL,EST_NF_ENT.CD_NT_MOV,TRIB_DARE.CD_TRIB,EST_NF_ENT.CD_FORN,0,GLB_MOV_OP.TP_MOV,0)C  
        
     LEFT JOIN GLB_CID ON EST_NF_ENT.CD_CID_DESTINATARIO = GLB_CID.CD_CID  
     LEFT OUTER JOIN GLB_CID_GLB_MUNICIPIO ON GLB_CID.CD_CID = GLB_CID_GLB_MUNICIPIO.CD_CID  
     LEFT OUTER JOIN GLB_MUNICIPIO ON GLB_CID_GLB_MUNICIPIO.CD_MUNICIPIO = GLB_MUNICIPIO.CD_MUNICIPIO  
     LEFT JOIN EST_NF_ENT_CANCEL ON EST_NF_ENT_CANCEL.CD_EMP = EST_NF_ENT.CD_EMP  
            AND EST_NF_ENT_CANCEL.CD_FILIAL = EST_NF_ENT.CD_FILIAL  
            AND EST_NF_ENT_CANCEL.CD_NF = EST_NF_ENT.CD_NF  
     LEFT JOIN EST_NF_ENT_NFE_CANCEL ON EST_NF_ENT_CANCEL.CD_EMP = EST_NF_ENT_NFE_CANCEL.CD_EMP  
            AND EST_NF_ENT_CANCEL.CD_FILIAL = EST_NF_ENT_NFE_CANCEL.CD_FILIAL  
            AND EST_NF_ENT_CANCEL.CD_NF = EST_NF_ENT_NFE_CANCEL.CD_NF  
     LEFT JOIN EST_NF_ENT_DENEGADA ON EST_NF_ENT.CD_EMP = EST_NF_ENT_DENEGADA.CD_EMP  
          AND EST_NF_ENT.CD_FILIAL = EST_NF_ENT_DENEGADA.CD_FILIAL  
          AND EST_NF_ENT.CD_NF = EST_NF_ENT_DENEGADA.CD_NF  
     LEFT JOIN EST_NF_SAI_NF_ENT ON EST_NF_ENT.CD_EMP = EST_NF_SAI_NF_ENT.CD_EMP  
            AND EST_NF_ENT.CD_FILIAL = EST_NF_SAI_NF_ENT.CD_FILIAL  
            AND EST_NF_ENT.CD_NF = EST_NF_SAI_NF_ENT.CD_NF_ENT
			
	LEFT JOIN EST_NF_ENT_EST_NF_NFE_INU  
		ON     EST_NF_ENT.CD_EMP    = EST_NF_ENT_EST_NF_NFE_INU.CD_EMP
		AND    EST_NF_ENT.CD_FILIAL = EST_NF_ENT_EST_NF_NFE_INU.CD_FILIAL
		AND    EST_NF_ENT.CD_NF     = EST_NF_ENT_EST_NF_NFE_INU.CD_NF
	
    OUTER APPLY UFD_TRIB_PROD_ALIQ_INTERNA(EST_NF_ENT.CD_EMP, 
				DANFE_ITENS.CD_PROD,  
				GLB_CID.UF) F	
			    
    WHERE   
   EST_NF_ENT.CD_EMP = 1   
      AND EST_NF_ENT.DT_ENT BETWEEN @DT_INI AND @DT_FIM  
  
       order by CD_FILIAL,CD_DOC,NUM_ITEM  
  
------------------------------------------------------------------------------------------------------------  
    UPDATE SPED SET   
   CGC_CPF=V_GLB_PESSOA.CGC_CPF,  
   NOME_PART = ISNULL(V_GLB_PESSOA.RAZAO_SOCIAL_PESSOA, ''),  
   ENDERECO=ISNULL(V_GLB_PESSOA.ENDERECO_PESSOA, '') ,  
   BAIRRO= ISNULL(V_GLB_PESSOA.BAIRRO_ENDERECO_PESSOA, ''),  
   NUMERO= ISNULL(V_GLB_PESSOA.NUMERO_ENDERECO_PESSOA, ''),  
   INSC_EST = ISNULL(V_GLB_PESSOA.INSC_EST, '') ,  
   UF=ISNULL(V_GLB_PESSOA.UF, '') ,  
   TEL= ISNULL(V_GLB_PESSOA.TELEFONE_PESSOA, '') ,  
   CEP = ISNULL(V_GLB_PESSOA.CEP_ENDERECO_PESSOA, '') ,  
   PART_FLAG_MICRO_EMPRESA =ISNULL(V_GLB_PESSOA.FLAG_SIMPLES_NACIONAL, 0),  
   COD_IBGE = ISNULL(V_GLB_PESSOA.COD_IBGE_MUNICIPIO,'')  
   --FLAG_NAO_ABATER_ICMS_DESONERADO = ISNULL(V_GLB_PESSOA.FLAG_NAO_ABATER_ICMS_DESONERADO,0 )      
    FROM #V_SPED_NF_ENT SPED  
      JOIN V_GLB_PESSOA ON SPED.CD_EMP = V_GLB_PESSOA.CD_EMP  
  AND SPED.CD_ORIG = V_GLB_PESSOA.CD_PESSOA  
  AND SPED.DEST_ORIG = V_GLB_PESSOA.DEST_ORIG  
------------------------------------------------------------------------------------------------------------  
   
  SELECT * FROM #V_SPED_NF_ENT  
  