IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[V_EST_PED_ELTR_FORN]'))
	BEGIN
		DROP VIEW [dbo].[V_EST_PED_ELTR_FORN]
	END
	
GO
--SELECT * FROM V_EST_PED_ELTR_FORN WHERE CD_PED_SOL = 118577 AND STS_PED <> 3 AND CD_FORN = 12719
CREATE VIEW [dbo].[V_EST_PED_ELTR_FORN]
AS
SELECT     
	dbo.EST_PED_ELTR.CD_EMP, 
	dbo.EST_PED_ELTR.CD_PED_SOL, 
	dbo.EST_PED_ELTR.CD_FIL_ENTR, 
	dbo.EST_PED_ELTR.STS_PED, 
    ISNULL(dbo.PG_FORN_CD_FIL_FORN.CD_FORN, 0) AS CD_FORN, 
	dbo.EST_PED_ELTR_IT.CD_PROD, 
	dbo.EST_PED_ELTR_IT.QT_IT_UN, 
	ISNULL(EST_PED_ELTR_IT.QT_IT_UN*EST_PROD.APRES_QT_CP,0) AS QT_SLD_UND,
    dbo.EST_PED_ELTR.DT_PED, 
	dbo.EST_PED_ELTR.DT_ENTR, 
	dbo.EST_PED_ELTR.CD_FIL_FAT, 
	dbo.EST_PED_ELTR.DT_ULT_ATU, 
    dbo.EST_PED_ELTR.CD_USU, 
	dbo.EST_PED_ELTR.DT_CAD, 
	dbo.EST_PED_ELTR_IT.VLR_PROD, 
	dbo.EST_PED_ELTR_IT.QT_IT AS QT_IT_CP, 
    dbo.EST_PED_ELTR.TP_REQ, 
	dbo.EST_PED_ELTR_FORN.STS_FORN,
	dbo.EST_PED_ELTR.FLAG_FECHAR,
	dbo.EST_PED_ELTR.FLAG_PERC_AUMENTO,
	dbo.EST_PED_ELTR.PERC_AUMENTO,
	dbo.EST_PED_ELTR.FLAG_ESTOQUE_ZERADO,
	EST_PED_ELTR.HORA_DEMANDA,
	ISNULL(EST_PED_ELTR_IT.QTDE_MEDIAF,0) AS QTDE_MEDIAF,
	ISNULL(EST_PED_ELTR_IT.COBERTURA,0) AS COBERTURA,
	ISNULL(EST_PED_ELTR_IT.QTDE_BASE_ALCADA,0) AS QTDE_BASE_ALCADA,
	ISNULL(EST_PED_ELTR_IT.COBERTURA_ALCADA,0) AS COBERTURA_ALCADA,
	ISNULL(EST_PED_ELTR_IT.VLR_ALCADA,0) AS VLR_ALCADA,
	ISNULL(EST_PED_ELTR_IT.QTDE_ALCADA,0) AS QTDE_ALCADA,
	ISNULL(EST_PED_ELTR_IT.PERC_VLR_ALCADA,0) AS PERC_VLR_ALCADA,
	ISNULL(EST_PED_ELTR_IT.PERC_COBERTURA_ALCADA,0) AS PERC_COBERTURA_ALCADA
FROM         
	dbo.EST_PED_ELTR INNER JOIN dbo.EST_PED_ELTR_IT ON 
	dbo.EST_PED_ELTR.CD_EMP = dbo.EST_PED_ELTR_IT.CD_EMP AND 
    dbo.EST_PED_ELTR.CD_PED_SOL = dbo.EST_PED_ELTR_IT.CD_PED_SOL 
	LEFT OUTER JOIN PG_FORN_CD_FIL_FORN ON 
	EST_PED_ELTR.CD_EMP = PG_FORN_CD_FIL_FORN.CD_EMP AND
	EST_PED_ELTR.CD_FIL_ENTR  = PG_FORN_CD_FIL_FORN.CD_FILIAL
	LEFT OUTER JOIN dbo.EST_PED_ELTR_FORN ON 
	dbo.EST_PED_ELTR.CD_EMP = dbo.EST_PED_ELTR_FORN.CD_EMP AND 
    dbo.EST_PED_ELTR.CD_PED_SOL = dbo.EST_PED_ELTR_FORN.CD_PED_SOL
    INNER JOIN EST_PROD ON
	EST_PED_ELTR_IT.CD_EMP = EST_PROD.CD_EMP AND
	EST_PED_ELTR_IT.CD_PROD = EST_PROD.CD_PROD
