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

GO
--SELECT TOP 1 * FROM V_PDV_VD_DOCUMENTOS_RELACIONADOS
CREATE VIEW V_PDV_VD_DOCUMENTOS_RELACIONADOS
AS
SELECT 
	CD_EMP,
	CD_FILIAL,
	CD_VD,
	NR_NF,
	DT_ENT AS DT_DOC,
	CD_IMP,
	DT_CAD,
	HORA,
	CD_NF,
	VLR_DEVO AS VLR_DOC,
	CD_CTR_NCC,
	NM_USU,
	STS_NCC,
	'DEVOLUCAO' AS ORIGEM_DOC,
	0 AS CD_PED,
	0 AS CD_FILIAL_PED
FROM 
	V_PDV_VD_EST_NF_ENT 
UNION ALL
SELECT 
	CD_EMP,
	CD_FILIAL,
	CD_VD,
	NF_NF,
	DT_EMI_NF,
	0 AS CD_IMP,
	DT_EMI_NF,
	HR_EMI_NF,
	CD_NF,
	VLR_NF,
	0 AS CD_CTR_NCC,
	NM_USU,
	'' AS STS_NCC,
	CASE WHEN CD_NF =0 THEN
		'PRE-FATURA SEM FATURAR'
	ELSE 
		'NOTA FISCAL SAIDA' 
	END AS ORIGEM_DOC,
	CD_PED,
	CD_FILIAL_PED 
FROM 
	V_PDV_VD_EST_NF_SAI