IF EXISTS (
		SELECT 1
		FROM SYS.VIEWS
		WHERE OBJECT_ID = OBJECT_ID(N'[V_EST_NF_SAI_DUPLICATAS]')
		)
	DROP VIEW [dbo].[V_EST_NF_SAI_DUPLICATAS]
GO

CREATE VIEW [dbo].[V_EST_NF_SAI_DUPLICATAS]
AS
SELECT E.CD_EMP
	,E.CD_FILIAL
	,E.CD_PED
	,E.CD_NF
	,E.NF_NF
	,E.CD_CLI
	,G.CD_GRP_ECON
	,E.DT_EMI_NF
	,E.VLR_NF
FROM dbo.EST_NF_SAI E
INNER JOIN dbo.GLB_MOV_OP M ON E.CD_NT_MOV = M.CD_MOV_OP
INNER JOIN dbo.PRC_GRP_ECON_PRC_FILIAL G ON E.CD_EMP = G.CD_EMP
	AND E.CD_FILIAL = G.CD_FILIAL
LEFT JOIN (
	SELECT DISTINCT P.CD_EMP
		,P.CD_FILIAL
		,P.CD_NF
	FROM RC_VD_CONV C
	INNER JOIN PDV_VD_RC_VD_CONV R ON R.CD_EMP = C.CD_EMP
		AND R.CD_FILIAL = C.CD_FILIAL
		AND R.CD_CTR = C.CD_CTR
	INNER JOIN PDV_VD_EST_NF_SAI P ON P.CD_EMP = R.CD_EMP
		AND P.CD_FILIAL = R.CD_FILIAL
		AND P.CD_VD = R.CD_VD
	) T ON T.CD_EMP = E.CD_EMP
	AND T.CD_FILIAL = E.CD_FILIAL
	AND T.CD_NF = E.CD_NF
WHERE T.CD_NF IS NULL
	AND M.TP_MOV_OP_TRANSA = 1
	AND E.STS_NF = 0
GO


