IF EXISTS( SELECT * FROM SYS.VIEWS WHERE NAME = 'V_RC_RECTO_TERCEIROS')
BEGIN 
	DROP VIEW V_RC_RECTO_TERCEIROS
END 
GO 

CREATE VIEW [DBO].[V_RC_RECTO_TERCEIROS]
AS
SELECT PDV_CUPOM_NAO_FISCAL.CD_FILIAL
	,PDV_CUPOM_NAO_FISCAL.CD_CX
	,PDV_CUPOM_NAO_FISCAL.DT_CUPOM
	,PDV_CUPOM_NAO_FISCAL.HR_CUPOM
	,PDV_CUPOM_NAO_FISCAL.NR_COO
	,PDV_CUPOM_NAO_FISCAL.VLR_TOT_CUPOM
	,EST_RECB_PDV.DS_RECB_PDV
	,EST_RECB_PDV.DS_PDV_RECB_PDV
	,PDV_CUPOM_NAO_FISCAL_EST_RECB_PDV.IDENT_RECB_PDV
	,PDV_CUPOM_NAO_FISCAL_EST_RECB_PDV.DT_VENCTO
	,PDV_CUPOM_NAO_FISCAL.VLR_DINH
	,0 AS VLR_CARTAO
	,0 AS VLR_CHQS
	,0 AS VLR_CHQ
	,'' AS NR_CHQ
	,'' AS SR_CHQ
	,'' AS ANO_CHQ
	,'' AS CD_BC_CHQ
	,'' AS DS_BC
	,'' AS AG_CHQ
	,'' AS NR_CNT_CHQ
	,'' AS CMC7
	,'' AS CGC_CPF
	,'' AS NM_EMI
	,PDV_CUPOM_NAO_FISCAL.CD_USU
FROM PDV_CUPOM_NAO_FISCAL
INNER JOIN PDV_CUPOM_NAO_FISCAL_EST_RECB_PDV ON PDV_CUPOM_NAO_FISCAL.CD_EMP = PDV_CUPOM_NAO_FISCAL_EST_RECB_PDV.CD_EMP
	AND PDV_CUPOM_NAO_FISCAL.CD_FILIAL = PDV_CUPOM_NAO_FISCAL_EST_RECB_PDV.CD_FILIAL
	AND PDV_CUPOM_NAO_FISCAL.CD_CTR = PDV_CUPOM_NAO_FISCAL_EST_RECB_PDV.CD_CTR
INNER JOIN EST_RECB_PDV ON PDV_CUPOM_NAO_FISCAL_EST_RECB_PDV.CD_EMP = EST_RECB_PDV.CD_EMP
	AND PDV_CUPOM_NAO_FISCAL_EST_RECB_PDV.CD_RECB_PDV = EST_RECB_PDV.CD_RECB_PDV
WHERE PDV_CUPOM_NAO_FISCAL.VLR_DINH > 0

UNION ALL

SELECT PDV_CUPOM_NAO_FISCAL.CD_FILIAL
	,PDV_CUPOM_NAO_FISCAL.CD_CX
	,PDV_CUPOM_NAO_FISCAL.DT_CUPOM
	,PDV_CUPOM_NAO_FISCAL.HR_CUPOM
	,PDV_CUPOM_NAO_FISCAL.NR_COO
	,PDV_CUPOM_NAO_FISCAL.VLR_TOT_CUPOM
	,EST_RECB_PDV.DS_RECB_PDV
	,EST_RECB_PDV.DS_PDV_RECB_PDV
	,PDV_CUPOM_NAO_FISCAL_EST_RECB_PDV.IDENT_RECB_PDV
	,PDV_CUPOM_NAO_FISCAL_EST_RECB_PDV.DT_VENCTO
	,0 AS VLR_DINH
	,PDV_CUPOM_NAO_FISCAL.VLR_CARTAO
	,0 AS VLR_CHQS
	,0 AS VLR_CHQ
	,'' AS NR_CHQ
	,'' AS SR_CHQ
	,'' AS ANO_CHQ
	,'' AS CD_BC_CHQ
	,'' AS DS_BC
	,'' AS AG_CHQ
	,'' AS NR_CNT_CHQ
	,'' AS CMC7
	,'' AS CGC_CPF
	,'' AS NM_EMI
	,PDV_CUPOM_NAO_FISCAL.CD_USU
FROM PDV_CUPOM_NAO_FISCAL
INNER JOIN PDV_CUPOM_NAO_FISCAL_EST_RECB_PDV ON PDV_CUPOM_NAO_FISCAL.CD_EMP = PDV_CUPOM_NAO_FISCAL_EST_RECB_PDV.CD_EMP
	AND PDV_CUPOM_NAO_FISCAL.CD_FILIAL = PDV_CUPOM_NAO_FISCAL_EST_RECB_PDV.CD_FILIAL
	AND PDV_CUPOM_NAO_FISCAL.CD_CTR = PDV_CUPOM_NAO_FISCAL_EST_RECB_PDV.CD_CTR
INNER JOIN EST_RECB_PDV ON PDV_CUPOM_NAO_FISCAL_EST_RECB_PDV.CD_E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CUPOM_NAO_FISCAL_EST_RECB_PDV.CD_CTR
INNER JOIN EST_RECB_PDV ON PDV_CUPOM_NAO_FISCAL_EST_RECB_PDV.CD_EMP = EST_RECB_PDV.CD_EMP
	AND PDV_CUPOM_NAO_FISCAL_EST_RECB_PDV.CD_RECB_PDV = EST_RECB_PDV.CD_RECB_PDV
INNER JOIN PDV_CUPOM_NAO_FISCAL_CHQS_CHQ_LANC ON PDV_CUPOM_NAO_FISCAL.CD_EMP = PDV_CUPOM_NAO_FISCAL_CHQS_CHQ_LANC.CD_EMP
	AND PDV_CUPOM_NAO_FISCAL.CD_FILIAL = PDV_CUPOM_NAO_FISCAL_CHQS_CHQ_LANC.CD_FILIAL
	AND PDV_CUPOM_NAO_FISCAL.CD_CTR = PDV_CUPOM_NAO_FISCAL_CHQS_CHQ_LANC.CD_CTR
INNER JOIN CHQ_LANC ON PDV_CUPOM_NAO_FISCAL_CHQS_CHQ_LANC.CD_EMP = CHQ_LANC.CD_EMP
	AND PDV_CUPOM_NAO_FISCAL_CHQS_CHQ_LANC.CD_FILIAL = CHQ_LANC.CD_FILIAL
	AND PDV_CUPOM_NAO_FISCAL_CHQS_CHQ_LANC.CD_CHQ_LANC = CHQ_LANC.CD_CHQ_LANC
INNER JOIN CHQ_LANC_CHQ_LANC_CPL ON CHQ_LANC.CD_EMP = CHQ_LANC_CHQ_LANC_CPL.CD_EMP
	AND CHQ_LANC.ANO = CHQ_LANC_CHQ_LANC_CPL.ANO
	AND CHQ_LANC.CD_CHQ_LANC = CHQ_LANC_CHQ_LANC_CPL.CD_CHQ_LANC
INNER JOIN CHQ_LANC_CPL ON CHQ_LANC_CHQ_LANC_CPL.CD_EMP = CHQ_LANC_CPL.CD_EMP
	AND CHQ_LANC_CHQ_LANC_CPL.CD_CHQ = CHQ_LANC_CPL.CD_CHQ
INNER JOIN CHQ_EMI ON CHQ_LANC_CPL.CD_EMP = CHQ_EMI.CD_EMP
	AND CHQ_LANC_CPL.CD_EMI = CHQ_EMI.CD_EMI
INNER JOIN BC_BANCO ON CHQ_LANC_CPL.CD_EMP = BC_BANCO.CD_EMP
	AND CHQ_LANC_CPL.CD_BC_CHQ = BC_BANCO.CD_BC
WHERE PDV_CUPOM_NAO_FISCAL.VLR_CHQS > 0
