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

GO

CREATE VIEW V_EST_PROD_TBL_DESC_CARGA_PDV
AS
SELECT	* 
FROM	EST_PROD_TBL_DESC TBL
WHERE	ISNULL(DT_INI,GETDATE()) <= GETDATE() 
		AND (TBL.DT_INI = TBL.DT_FIM OR TBL.DT_FIM >= convert (date, GETDATE()))

GO 

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

GO

--DROP VIEW V_EST_PROD_TBL_DESC_EST_PROD_CARGA_PDV
CREATE VIEW V_EST_PROD_TBL_DESC_EST_PROD_CARGA_PDV
AS
SELECT
	TBL_FIL.CD_EMP,
	TBL_FIL.CD_FILIAL,
	TBL_FIL.CD_TBL_DESC,
	TBL.CD_TP_DESCONTO,
	TBL_PROD.CD_PROD,
	TBL_PROD.PERC_DESC,
	TBL_PROD.PERC_DESC_MAX,
	TBL_PROD.PERC_DESC_LIMITE,
	TBL_PROD.PERC_AUMENTO
FROM	
	V_EST_PROD_TBL_DESC_CARGA_PDV TBL 
	INNER	JOIN EST_PROD_TBL_DESC_PRC_FILIAL TBL_FIL ON		
	TBL.CD_EMP		= TBL_FIL.CD_EMP AND 
	TBL.CD_TBL_DESC = TBL_FIL.CD_TBL_DESC
	INNER	JOIN EST_PROD_TBL_DESC_EST_PROD TBL_PROD ON		
	TBL_FIL.CD_EMP		= TBL_PROD.CD_EMP AND 
	TBL_FIL.CD_TBL_DESC = TBL_PROD.CD_TBL_DESC
WHERE
	TBL_PROD.CD_EMP =1



