IF EXISTS (
		SELECT 1
		FROM SYS.VIEWS
		WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[V_EST_NF_ENT_IBS_CBS]')
		)
	DROP VIEW [DBO].[V_EST_NF_ENT_IBS_CBS];
GO


CREATE VIEW [DBO].[V_EST_NF_ENT_IBS_CBS]
	WITH ENCRYPTION
AS
SELECT CD_EMP
,CD_FILIAL
,CD_NF
,CD_PED
,NR_NF
,CD_NF_SERIE
,SERIE
,MODELO
,CD_ORIG
,NM_ORIG
,NM_ORIG_FANT
,CGC_CPF
,INSC_EST
,TP_SUBS_TRIB
,SUBS_TRIB_FORN
,ENDE
,NUMERO
,BAIRRO
,CEP
,CD_CID
,DS_CID
,UF
,TEL
,COD_IBGE
,FLAG_ICMS_SUBST_PAGO_ANTERIOR
,FLAG_VALIDA_NFE
,TIPO_FORNECEDOR
,FLAG_MICRO_EMPRESA
,CD_TP_FORN
,QTDE_CASAS_DECIMAIS_NOTA
,CONTRIBUINTE
,DT_ENT
,DT_EMI_NF
,HR_EMI_NF
,VLR_NF
,VLR_TOT_PROD
,VLR_BASE_ICMS
,VLR_ICMS
,VLR_BASE_ICMS_SUBS
,VLR_ICMS_SUBS
,VLR_FRETE
,VLR_FRETE_CABE_RATEIO
,VLR_DESC_CAB
,VLR_DESC_CAB_ARRED
,VLR_PERC_IPI_FRETE
,PERC_IPI_FRETE
,TP_FRETE
,VLR_IPI
,VLR_OUTROS
,VRL_ISENTAS
,VLR_ENCG_FIN
,PERC_ICMS_ENCG_FIN
,OBS1
,OBS2
,DT_ENTRADA_EST
,VLR_OUTRAS_DESP
,OBS3
,OBS4
,OBS5
,OBS6
,OBS7
,NM_COMPRADOR
,INF_DESCONTO
,DT_CAD
,CD_USU
,STS_NF
,DS_TRANSP
,PLACA_VEIC
,PLACA_UF
,ENCG_TRIB
,CGC_CPF_TRANSP
,ENDE_TRANSP
,DS_CID_TRANSP
,UF_TRANSP
,INSC_EST_TRANSP
,EMAIL_TRANSP
,QT_VOL
,ES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PMC
,CD_SIT_OPERA_SIMPLES_NACIO
,MODALI_BASE_ICMS_SUBST
,MODALI_BASE_ICMS
,VLR_CRED_BASE_SN
,VLR_CRED_ICMS_SN
,VLR_IMPOSTO_REA
,FLAG_NF_PROD_TOTAL_BRUTO
,ENVIAR_XML_NFE
,EMAIL
,ESPC
,IMP_NF
,CD_USU_LIBEROU_CUSTO_VARIACAO
,CD_USU_LIBEROU_DESACORDO
,CST_PIS
,CST_COFINS
,CD_MT_DEV_VD
,FLAG_NAO_TRIBUTAR_PIS_COFINS
,CD_TRIB_DEVOLUCAO
,FLAG_VLR_SUBST_TOTAL_PROD
,PERC_DESC_FINAN
,VLR_DESC_FINAN_PROD
,VLR_DESC_FINAN_PROD_UNIT
,FRETE_INCIDE_BASE_ICMS
,ALIQ_ICMS_INTERNO
,TP_SR_NFE
,HR_ENT
,QT_EMB
,FLAG_N_GERAR_DAR
,FLAG_N_GERAR_DAR_IT
,APLICATIVO
,VERSAO_APLICATIVO
,PIS_COFINS_RECALCULADO
,TIPO_TRIBUTACAO_PIS_COFINS
,DS_NT_OP
,CTR_PMC
,FLAG_OPERADOR_LOG
,FLAG_NAO_GERAR_ARQ_SPED
,NR_AUTORIZADOR
,NR_PROTOCOLO
,VS_PROTOCOLO
,DT_RECBTO
,DG_VALIDADOR
,TP_AMB
,VS_APLIC
,REG_CONTG
,NR_PROTOCOLO_CANCEL
,JUSTIFICA_CANCEL
,VLR_DESC_FINAN
,TIPO_REGIME_EMPRESA
,VLR_ICMS_SUBS_DARE
,VLR_ICMS_DIFERENCIAL_ALIQUOTA
,ORIGEM_PRODUTO
,FLAG_CUSTO_ZERO_ENT
,VLR_CUSTO_GERENCIAL_TRANSF
,VLR_BASE_ICMS_OP
,VLR_ICMS_OP
,VLR_CUSTO_ANT_RECALC
,FLAG_FATURAMENTO_AUTOMATICO_HABILITADO
,FLAG_NOTA_FISCAL_ENTRADA_AUTOMATICA
,NFE_DENEGADA
,FLAG_NOTA_EMISSAO_PROPRIA
,FLAG_OUTROS
,PERC_ICMS_OP
,PERC_IVA_RET
,PERC_ICMS_RET
,VLR_IT_TOT_NF
,VLR_RESSARCIMENTO_ICMS
,GERA_MOV_ICMS_SUBS
,GERA_MOV_ICMS
,VLR_ICMS_DESONERADO
,VLR_ICMS_DESONERADO_CAB
,TP_PRECO
,ICMS_DESONERADO_MOTIVO
,CALC_CUSTO_MEDIO
,NR_CEST
,NM_USU
,ICMS_DIFERIDO
,PERC_DIFERIMENTO
,VLR_ICMS_DIFERIMENTO
,VLR_FRETE_IT
,DT_HR_CAD
,VLR_BASE_ICMS_LUCRO_REAL
,PERC_ICMS_LUCRO_REAL
,VLR_ICMS_LUCRO_REAL
,REGIME_TRIBUTARIO_FILIAL
,COD_IBGE_MUNICIPIO_DESTINATARIO
,END_DESTINATARIO
,BAIRRO_DESTINATARIO
,CEP_DESTINATARIO
,CIDADE_DESTINATARIO
,CNPJ_DESTINATARIO
,INSC_ESTADUAL_DESTINATARIO
,RZ_DESTINATARIO
,UF_DESTINATARIO
,CD_CID_DESTINATARIO
,TP_REGIME_APURAC_PIS_COFINS
,END_EMITENTE
,BAIRRO_EMITENTE
,CEP_EMITENTE
,CIDADE_EMITENTE
,CNPJ_EMITENTE
,INSC_ESTADUAL_EMITENTE
,RZ_EMITENTE
,UF_EMITENTE
,VLR_BASE_SUBS_DARE
,PERC_IVA_DARE
,PERC_ICMS_DESTINO_DARE
,VLR_FECOP_ST
,VLR_BASE_FECOP_ST_IT
,VLR_FECOP_ST_IT
,PERC_FECOP_ST
,VLR_FECOP
,VLR_BASE_FECOP_IT
,VLR_FECOP_IT
,PERC_FECOP
,VLR_FECOP_ST_RET
,VLR_BASE_FECOP_RET_IT
,VLR_FECOP_RET_IT
,PERC_FECOP_RET
,COMPLEMENTO_RESIDENCIAL
,CD_BENEF
,UUID
,CD_BENEF_IT
,FLAG_VLR_FECOP_ST
,VLR_PMC_DAR_IT
,VLR_PMPF_DAR_IT
,TP_SUBS_DAR_IT
,FLAG_VLR_ICMS_DESONERADO_DESCONTO
,OBS_CONTRIBUINTE
,FLAG_ACAO_3_DEV_OUTRA_UF
,INSC_SUFRAMA
,CD_TRIB_CST_DEST
,VLR_ICMS_SUBSTITUTO
,FLAG_VLR_ICMS_DESONERADO_DESCONTO_ITEM
,CD_BENEF_RED_IT
,IND_TOTAL
,ISNULL(CASE 
			WHEN ISNULL(V.CD_TRIB_IBS_CBS,0) <> 0
				THEN V.CD_TRIB_IBS_CBS
			ELSE IBS_CBS.CD_TRIB_IBS_CBS
			END, 0) AS CD_TRIB_IBS_CBS
	,ISNULL(CASE 
			WHEN ISNULL(V.CD_TRIB_IBS_CBS,0) <> 0
				THEN V.CST_IBS_CBS
			ELSE IBS_CBS.CST_IBS_CBS
			END, '') AS CST_IBS_CBS
	,ISNULL(CASE 
			WHEN ISNULL(V.CD_TRIB_IBS_CBS,0) <> 0
				THEN V.CLASS_TRIB_IBS_CBS
			ELSE IBS_CBS.CLASS_TRIB_IBS_CBS
			END, '') AS CLASS_TRIB_IBS_CBS
	,ISNULL(CASE 
			WHEN ISNULL(V.CD_TRIB_IBS_CBS,0) <> 0
				THEN V.VLR_BC_IBS_CBS
			ELSE IBS_CBS.VLR_BC_IBS_CBS
			END, 0) AS VLR_BC_IBS_CBS
	,ISNULL(CASE 
			WHEN ISNULL(V.CD_TRIB_IBS_CBS,0) <> 0
				THEN V.PERC_IBS_UF
			ELSE IBS_CBS.PERC_IBS_UF
			END, 0) AS PERC_IBS_UF
	,ISNULL(CASE 
			WHEN ISNULL(V.CD_TRIB_IBS_CBS,0) <> 0
				THEN V.VLR_IBS_UF
			ELSE IBS_CBS.VLR_IBS_UF
			END, 0) AS VLR_IBS_UF
	,ISNULL(CASE 
			WHEN ISNULL(V.CD_TRIB_IBS_CBS,0) <> 0
				THEN V.VLR_IBS
			ELSE IBS_CBS.VLR_IBS
			END, 0) AS VLR_IBS
	,ISNULL(CASE 
			WHEN ISNULL(V.CD_TRIB_IBS_CBS,0) <> 0
				THEN V.PERC_CBS
			ELSE IBS_CBS.PERC_CBS
			END, 0) AS PERC_CBS
	,ISNULL(CASE 
			WHEN ISNULL(V.CD_TRIB_IBS_CBS,0) <> 0
				THEN V.VLR_CBS
			ELSE IBS_CBS.VLR_CBS
			END, 0) AS VLR_CBS
	,ISNULL(CASE 
			WHEN ISNULL(V.CD_TRIB_IBS_CBS,0) <> 0
				THEN V.VLR_IBS_UF_DIFERIMENTO
			ELSE IBS_CBS.VLR_IBS_UF_DIFERIMENTO
			END, 0) AS VLR_IBS_UF_DIFERIMENTO
	,ISNULL(CASE 
			WHEN ISNULL(V.CD_TRIB_IBS_CBS,0) <> 0
				THEN V.VLR_IBS_UF_DEV_TRIB
			ELSE IBS_CBS.VLR_IBS_UF_DEV_TRIB
			END, 0) AS VLR_IBS_UF_DEV_TRIB
	,ISNULL(CASE 
			WHEN ISNULL(V.CD_TRIB_IBS_CBS,0) <> 0
				THEN V.PERC_IBS_UF_RED
			ELSE IBS_CBS.PERC_IBS_UF_RED
			END, 0) AS PERC_IBS_UF_RED
	,ISNULL(CASE 
			WHEN ISNULL(V.CD_TRIB_IBS_CBS,0) <> 0
				THEN V.PERC_IBS_UF_RED_EFET
			ELSE IBS_CBS.PERC_IBS_UF_RED_EFET
			END, 0) AS PERC_IBS_UF_RED_EFET
	,ISNULL(CASE 
			WHEN ISNULL(V.CD_TRIB_IBS_CBS,0) <> 0
				THEN V.PERC_IBS_MUN
			ELSE IBS_CBS.PERC_IBS_MUN
			END, 0) AS PERC_IBS_MUN
	,ISNULL(CASE 
			WHEN ISNULL(V.CD_TRIB_IBS_CBS,0) <> 0
				THEN V.PERC_IBS_MUN_DIFERIMENTO
			ELSE IBS_CBS.PERC_IBS_MUN_DIFERIMENTO
			END, 0) AS PERC_IBS_MUN_DIFERIMENTO
	,ISNULL(CASE 
			WHEN ISNULL(V.CD_TRIB_IBS_CBS,0) <> 0
				THEN V.VLR_IBS_MUN_DIFERIMENTO
			ELSE IBS_CBS.VLR_IBS_MUN_DIFERIMENTO
			END, 0) AS VLR_IBS_MUN_DIFERIMENTO
	,ISNULL(CASE 
			WHEN ISNULL(V.CD_TRIB_IBS_CBS,0) <> 0
				THEN V.VLR_IBS_MUN_DEV_TRIB
			ELSE IBS_CBS.VLR_IBS_MUN_DEV_TRIB
			END, 0) AS VLR_IBS_MUN_DEV_TRIB
	,ISNULL(CASE 
			WHEN ISNULL(V.CD_TRIB_IBS_CBS,0) <> 0
				THEN V.PERC_IBS_MUN_RED
			ELSE IBS_CBS.PERC_IBS_MUN_RED
			END, 0) AS PERC_IBS_MUN_RED
	,ISNULL(CASE 
			WHEN ISNULL(V.CD_TRIB_IBS_CBS,0) <> 0
				THEN V.PERC_IBS_MUN_RED_EFET
			ELSE IBS_CBS.PERC_IBS_MUN_RED_EFET
			END, 0) AS PERC_IBS_MUN_RED_EFET
	,ISNULL(CASE 
			WHEN ISNULL(V.CD_TRIB_IBS_CBS,0) <> 0
				THEN V.VLR_IBS_MUN
			ELSE IBS_CBS.VLR_IBS_MUN
			END, 0) AS VLR_IBS_MUN
	,ISNULL(CASE 
			WHEN ISNULL(V.CD_TRIB_IBS_CBS,0) <> 0
				THEN V.PERC_CBS_DIFERIMENTO
			ELSE IBS_CBS.PERC_CBS_DIFERIMENTO
			END, 0) AS PERC_CBS_DIFERIMENTO
	,ISNULL(CASE 
			WHEN ISNULL(V.CD_TRIB_IBS_CBS,0) <> 0
				THEN V.VLR_CBS_DIFERIMENTO
			ELSE IBS_CBS.VLR_CBS_DIFERIMENTO
			END, 0) AS VLR_CBS_DIFERIMENTO
	,ISNULL(CASE 
			WHEN ISNULL(V.CD_TRIB_IBS_CBS,0) <> 0
				THEN V.VLR_CBS_DEV_TRIB
			ELSE IBS_CBS.VLR_CBS_DEV_TRIB
			END, 0) AS VLR_CBS_DEV_TRIB
	,ISNULL(CASE 
			WHEN ISNULL(V.CD_TRIB_IBS_CBS,0) <> 0
				THEN V.PERC_CBS_RED
			ELSE IBS_CBS.PERC_CBS_RED
			END, 0) AS PERC_CBS_RED
	,ISNULL(CASE 
			WHEN ISNULL(V.CD_TRIB_IBS_CBS,0) <> 0
				THEN V.PERC_CBS_RED_EFET
			ELSE IBS_CBS.PERC_CBS_RED_EFET
			END, 0) AS PERC_CBS_RED_EFET
	,ISNULL(CASE 
			WHEN ISNULL(V.CD_TRIB_IBS_CBS,0) <> 0
				THEN V.PERC_IBS_UF_DIFERIMENTO
			ELSE IBS_CBS.PERC_IBS_UF_DIFERIMENTO
			END, 0) AS PERC_IBS_UF_DIFERIMENTO
	--
	,ISNULL(V.CD_TRIB_IBS_CBS_REG,0) AS CD_TRIB_IBS_CBS_REG 
	,V.CST_REG
	,V.CLASS_TRIB_REG
	,V.PERC_EFET_REG_IBS_UF
	,V.VLR_TRIB_REG_IBS_UF
	,V.PERC_EFET_REG_IBS_MUN
	,V.VLR_TRIB_REG_IBS_MUN
	,V.PERC_EFET_REG_CBS
	,V.VLR_TRIB_REG_CBS
	,TP_NF_CREDITO
	,CH_ACESSO_DFE_REFERENCIADO
	,NUM_ITEM_DFE_REFERENCIADO

	,V.TIPO_ENTE_GOV 
	,V.PERC_RED_COMPRA_GOV 
	,V.TIPO_OPER_ENTE_GOV 

	,V.PERC_IBS_UF_COMPRA_GOV 
	,V.VLR_TRIB_IBS_UF_COMPRA_GOV 
	,V.PERC_IBS_MUN_COMPRA_GOV 
	,V.VLR_TRIB_IBS_MUN_COMPRA_GOV 
	,V.PERC_CBS_COMPRA_GOV 
	,V.VLR_TRIB_CBS_COMPRA_GOV 

FROM V_EST_NF_ENT V
OUTER APPLY UFD_EST_PROD_TRIB_IBS_CBS(V.CD_EMP, V.CD_FILIAL, V.CD_MOV_OP, V.CD_FORN, V.CD_PROD, V.QT_IT, V.VLR_IT, V.PERC_DESC, V.VLR_FRETE, V.VLR_OUTROS, V.VLR_BASE_PIS) IBS_CBS