
/****** Object:  View [dbo].[V_EST_PROD_CPL_MOVIMENTACAO]    Script Date: 04/11/2013 10:05:53 ******/
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[V_EST_PROD_CPL_MOVIMENTACAO]'))
DROP VIEW [dbo].[V_EST_PROD_CPL_MOVIMENTACAO]
GO 

CREATE  VIEW [dbo].[V_EST_PROD_CPL_MOVIMENTACAO] AS
SELECT 
	CD_EMP,
	CD_FILIAL,
	CONVERT(DATETIME,CONVERT(VARCHAR,GETDATE(),112)) AS DT_MOV,
	CD_PROD,
	QT_EST,
	0 AS QT_SAI_NF,
	0 AS QT_SAI_PDV,
	0 AS QT_EST_ENT_NF,
	0 AS QT_EST_IMP,
	QT_EST_DE_TERC,
	QT_EST_EM_TERC,
	QT_PEND_SAI,
	QT_PEND_ENT
FROM 
	V_EST_PROD_CPL_PENDENCIAS
UNION ALL
SELECT     
	CD_EMP, 
	CD_FILIAL, 
	DT_ENT AS DT_MOV, 
	CD_PROD, 
	0, 
	0,
	0,
	ISNULL(SUM(QT_IT),0) AS QT_EST_ENT_NF,
	0,
	0 AS QT_EST_DE_TERC,
	0 AS QT_EST_EM_TERC,
	0 AS QT_PEND_SAI,
	0 AS QT_PEND_ENT
FROM         
	V_EST_NF_ENT
WHERE
	ATU_EST = 1
GROUP BY 
	CD_EMP, 
	CD_FILIAL, 
	DT_ENT, 
	CD_PROD, 
	ATU_EST
UNION ALL
SELECT     
	CD_EMP, 
	CD_FILIAL, 
	DT_EMI_NF, 
	CD_PROD, 
	0 AS QT_EST,
	SUM(QT_IT) AS QT_SAI_NF,
	0 AS QT_SAI_PDV,
	0 AS QT_EST_ENT_NF,
	0 AS QT_EST_IMP,
	0 AS QT_EST_DE_TERC,
	0 AS QT_EST_EM_TERC,
	0 AS QT_PEND_SAI,
	0 AS QT_PEND_ENT
FROM         
	V_EST_NF_SAI
WHERE 
	BX_EST 	= 1
GROUP BY 
	CD_EMP, 
	CD_FILIAL, 
	DT_EMI_NF, 
	CD_PROD
UNION ALL
SELECT     
	CD_EMP, 
	CD_FILIAL, 
	DT_VD, 
	CD_PROD, 
	0 AS QT_EST,
	0 AS QT_SAI_NF,
	SUM(QT_IT) AS QT_SAI_PDV,
	0 AS QT_EST_ENT_NF,
	0 AS QT_EST_IMP,
	0 AS QT_EST_DE_TERC,
	0 AS QT_EST_EM_TERC,
	0 AS QT_PEND_SAI,
	0 AS QT_PEND_ENT
FROM         
	V_PDV_VD_IT
GROUP BY CD_EMP, CD_FILIAL, DT_VD, CD_PROD
UNION ALL
SELECT     
	CD_EMP, 
	CD_FILIAL, 
	DT_CAD, 
	CD_PROD, 
	0 AS QT_EST,
	0 AS QT_SAI_NF,
	0 AS QT_SAI_PDV,
	0 AS QT_EST_ENT_NF,
	SUM(QT_IMP) AS QT_EST_IMP,
	0 AS QT_EST_DE_TERC,
	0 AS QT_EST_EM_TERC,
	0 AS QT_PEND_SAI,
	0 AS QT_PEND_ENT
FROM         
	EST_PROD_IMP
WHERE
	TP_EST=0
GROUP BY 
	CD_EMP, 
	CD_FILIAL, 
	DT_CAD, 
	CD_PROD

GO


