IF OBJECT_ID('V_EST_PROD_CPL_POS_EST_DROGRASMIL') IS NOT NULL
BEGIN
	DROP VIEW V_EST_PROD_CPL_POS_EST_DROGRASMIL
END

GO

CREATE VIEW V_EST_PROD_CPL_POS_EST_DROGRASMIL
AS
SELECT 
	CPL.CD_FILIAL,
	ISNULL(VPU.CD_PROD_BANDEIRA,CPL.CD_PROD) AS CD_PROD, 
	LINHA.DS_ARV_MERC_LINHA AS GRUPO,  
	CAT.DS_ARV_MERC_CATEG AS CATEGORIA, 
	FABRI.NM_FABRIC AS FABRICANTE, 
	CASE WHEN QT_EST >= 0 THEN '+' ELSE '-' END AS SINAL, 
	QT_EST
FROM EST_PROD_CPL CPL
	INNER JOIN EST_PROD PROD ON PROD.CD_EMP = CPL.CD_EMP AND PROD.CD_PROD = CPL.CD_PROD
	LEFT JOIN EST_PROD_FABRIC FABRI ON PROD.CD_FABRIC = FABRI.CD_FABRIC
	LEFT JOIN EST_PROD_EST_ARV_MERCADOLOGICA ARV_MERC ON ARV_MERC.CD_EMP = PROD.CD_EMP AND ARV_MERC.CD_PROD = PROD.CD_PROD
	LEFT JOIN EST_ARV_MERC_CATEGORIA CAT ON ARV_MERC.CD_EMP = CAT.CD_EMP AND ARV_MERC.CD_ARV_MERC_CATEG = CAT.CD_ARV_MERC_CATEG
	LEFT JOIN EST_ARV_MERC_LINHA LINHA ON ARV_MERC.CD_EMP = LINHA.CD_EMP AND ARV_MERC.CD_ARV_MERC_LINHA = LINHA.CD_ARV_MERC_LINHA
	LEFT JOIN V_CAD_UNICO_DEPARAS_EST_PROD VPU ON
	VPU.CD_PROD=CPL.CD_PROD AND
	VPU.CD_FILIAL=CPL.CD_FILIAL





