/****** Object:  View [dbo].[V_EST_NF_SAI_IMPOSTOS_PIS_COFINS]    Script Date: 12/27/2013 10:30:57 ******/
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[V_EST_NF_SAI_IMPOSTOS_PIS_COFINS]'))
DROP VIEW [dbo].[V_EST_NF_SAI_IMPOSTOS_PIS_COFINS]
GO
GO
GO
GO

CREATE VIEW [dbo].[V_EST_NF_SAI_IMPOSTOS_PIS_COFINS] AS   
--Versao 2466
SELECT     
	EST_NF_SAI.CD_EMP, 
	EST_NF_SAI.CD_FILIAL , 
	EST_NF_SAI.CD_NF , 
	EST_NF_SAI.DT_EMI_NF , 
	ISNULL(SUM(VLR_BASE_PIS ),0) BASE_PIS_NF ,
	ISNULL(SUM(VLR_PIS ),0) PIS_NF ,
	ISNULL(SUM(VLR_BASE_COFINS ),0) BASE_COFINS_NF ,
	ISNULL(SUM(VLR_COFINS ),0) COFINS_NF 

FROM 
	EST_NF_SAI_IT INNER JOIN EST_NF_SAI ON  -- SELECT TOP 1 EST_NF_SAI.STS_NF, * FROM EST_NF_SAI
	EST_NF_SAI_IT.CD_EMP  =  EST_NF_SAI.CD_EMP AND 
	EST_NF_SAI_IT.CD_FILIAL  = EST_NF_SAI.CD_FILIAL AND 
	EST_NF_SAI_IT.CD_NF = EST_NF_SAI.CD_NF 
WHERE 
	EST_NF_SAI.STS_NF = 0
GROUP BY  
	EST_NF_SAI.CD_EMP, 
	EST_NF_SAI.CD_FILIAL, 
	EST_NF_SAI.CD_NF,
	EST_NF_SAI.DT_EMI_NF