/****** Object:  View [dbo].[V_EST_NF_ENT_DAR_DIFERENCIAL_ALIQUOTA]    Script Date: 05/30/2013 15:29:32 ******/
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[V_EST_NF_ENT_DAR_DIFERENCIAL_ALIQUOTA]'))
DROP VIEW [dbo].[V_EST_NF_ENT_DAR_DIFERENCIAL_ALIQUOTA]
GO
GO
GO
GO

/*SELECT TOP 10 * FROM V_EST_NF_ENT_DAR_DIFERENCIAL_ALIQUOTA WHERE CD_EMP = 1 AND CD_FILIAL = 99 AND DT_ENT BETWEEN  '2006-03-01' AND '2006-03-28'*/  
CREATE VIEW [dbo].[V_EST_NF_ENT_DAR_DIFERENCIAL_ALIQUOTA]  
AS  
SELECT     
	EST_NF_ENT.CD_EMP, 
	EST_NF_ENT.CD_FILIAL, 
	EST_NF_ENT.CD_FORN, 
	PG_FORN.RZ_FORN, 
	EST_NF_ENT.DT_ENT,   
    EST_NF_ENT_IT.CD_PROD, 
    EST_NF_ENT_IT.DS_PROD, 
    EST_NF_ENT_IT.QT_IT, 
    EST_NF_ENT_IT.VLR_IT,   
    EST_NF_ENT_IT.VLR_DESC, 
    EST_NF_ENT_IT.PERC_DESC, 
    EST_NF_ENT.CD_NF, 
    EST_NF_ENT.NR_NF, 
    EST_NF_ENT.VLR_NF,   
    EST_NF_ENT.CD_NT_MOV, 
    EST_NF_ENT_IT.ALIQ_ICMS AS PERC_ICMS, 
    EST_NF_ENT_IT.PERC_IPI, 
    GLB_CID.UF,   
    EST_PROD_CPL_TRIB_INTER_DAR.VLR_PAUTA_IVA, 
    EST_PROD_CPL_TRIB_INTER_DAR.CD_TRIB, 
    GLB_MOV_OP.TP_MOV_OP_TRANSA,   
    GLB_MOV_OP.TP_MOV, 
    ISNULL(GLB_MOV_OP.FLAG_REALIZA_DIF_ALIQUOTA,0) AS FLAG_REALIZA_DIF_ALIQUOTA,
    ISNULL(EST_PROD_PRECO_TBL.VLR_VD, 0) AS VLR_VD, 
    PG_FORN.PRECO_BASE_SUBS,   
    EST_NF_ENT.DT_ENTRADA_EST, 
    EST_PROD_CPL_TRIB_INTER_DAR.UF AS UF_TRIB, 
    EST_NF_ENT.CALC_SUBS_PMC,   
    EST_NF_ENT.PERC_REDU_SUBS_PMC,
    ISNULL(EST_NF_ENT.FLAG_N_GERAR_DAR,0) AS FLAG_N_GERAR_DAR,
    ISNULL(EST_NF_ENT_IT.FLAG_N_GERAR_DAR,0) AS FLAG_N_GERAR_DAR_IT
FROM           
    EST_NF_ENT INNER JOIN EST_NF_ENT_IT ON   
    EST_NF_ENT.CD_EMP = EST_NF_ENT_IT.CD_EMP AND   
    EST_NF_ENT.CD_FILIAL = EST_NF_ENT_IT.CD_FILIAL AND   
    EST_NF_ENT.CD_NF = EST_NF_ENT_IT.CD_NF   
    INNER JOIN EST_PROD_CPL_TRIB_INTER_DAR ON   
    EST_NF_ENT_IT.CD_EMP = EST_PROD_CPL_TRIB_INTER_DAR.CD_EMP AND   
    EST_NF_ENT_IT.CD_FILIAL = EST_PROD_CPL_TRIB_INTER_DAR.CD_FILIAL AND   
    EST_NF_ENT_IT.CD_PROD = EST_PROD_CPL_TRIB_INTER_DAR.CD_PROD   
    INNER JOIN EST_TRIB ON   
    EST_PROD_CPL_TRIB_INTER_DAR.CD_TRIB = EST_TRIB.CD_TRIB   
    INNER JOIN PG_FORN ON   
    EST_NF_ENT.CD_EMP = PG_FORN.CD_EMP AND   
    EST_NF_ENT.CD_FORN = PG_FORN.CD_FORN   
    INNER JOIN GLB_CID ON   
    PG_FORN.CD_CID = GLB_CID.CD_CID   
    INNER JOIN V_EST_PROD_CPL_TRIB_INTER_DAR_UF ON   
    GLB_CID.UF COLLATE SQL_Latin1_General_CP1_CI_AS = V_EST_PROD_CPL_TRIB_INTER_DAR_UF.UF COLLATE SQL_Latin1_General_CP1_CI_AS   
    INNER JOIN GLB_MOV_OP ON   
    EST_NF_ENT.CD_NT_MOV = GLB_MOV_OP.CD_MOV_OP   
    LEFT OUTER JOIN V_EST_PROD_PRECO_TBL EST_PROD_PRECO_TBL ON   
    EST_NF_ENT_IT.CD_EMP = EST_PROD_PRECO_TBL.CD_EMP AND   
    EST_NF_ENT_IT.CD_FILIAL = EST_PROD_PRECO_TBL.CD_FILIAL AND  
    EST_NF_ENT_IT.CD_PROD = EST_PROD_PRECO_TBL.CD_PROD  
    
    LEFT OUTER JOIN EST_NF_ENT_DAR  ON
	EST_NF_ENT.CD_EMP = EST_NF_ENT_DAR.CD_EMP AND 
	EST_NF_ENT.CD_FILIAL = EST_NF_ENT_DAR.CD_FILIAL AND 
	EST_NF_ENT.CD_NF = EST_NF_ENT_DAR.CD_NF 
WHERE       
 GLB_MOV_OP.DEST_ORIG = 1  
 AND EST_PROD_CPL_TRIB_INTER_DAR.TP_MOV = GLB_MOV_OP.TP_MOV
 AND EST_NF_ENT_DAR.CD_NF IS NULL
 AND GLB_MOV_OP.TP_MOV_OP_TRANSA = 0 
 AND GLB_MOV_OP.FLAG_REALIZA_DIF_ALIQUOTA =1 
