IF  EXISTS (SELECT * FROM SYS.VIEWS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[V_RC_CLI_CREDIARIO]'))
DROP VIEW [DBO].[V_RC_CLI_CREDIARIO]
GO
--SELECT * FROM V_RC_CLI_CREDIARIO WHERE LIK
CREATE VIEW [DBO].[V_RC_CLI_CREDIARIO] AS 
SELECT          
	DBO.RC_CLI.CD_EMP, 
	DBO.RC_CLI.CD_CLI, 
	DBO.RC_CLI.RZ_CLI, 
	DBO.RC_CLI.CGC_CPF,
	DBO.RC_CLI.END_CLI,
	ISNULL(DBO.RC_CLI_CREDIARIO.VLR_LIM_CRED,0)          AS VLR_LIM_CRED, 
	ISNULL(DBO.RC_CLI_CREDIARIO.VLR_LIM_PARC_CRED,0)     AS VLR_LIM_PARC_CRED, 
	ISNULL(DBO.RC_CLI_CREDIARIO.VLR_LIM_FAT_CRED,0)      AS VLR_LIM_FAT_CRED , 
	ISNULL(DBO.RC_CLI_CREDIARIO.STS_CRED,1)              AS STS_CRED , 
	ISNULL(DBO.RC_CLI_CREDIARIO.VLR_DEB_CRED,0)          AS VLR_DEB_CRED , 
	ISNULL(DBO.RC_CLI_CREDIARIO.VLR_DEB_PARC_CRED,0)     AS VLR_DEB_PARC_CRED, 
	ISNULL(DBO.RC_CLI_CREDIARIO.VLR_DEB_FAT_CRED,0)      AS VLR_DEB_FAT_CRED, 
	ISNULL(DBO.RC_CLI_CREDIARIO.CD_MDL_VD,0)             AS CD_MDL_VD, 
	ISNULL(DBO.RC_MDL_VENDA.QTDE_MAX_PARC_VD,0)          AS QTDE_MAX_PARC_VD, 
	ISNULL(DBO. RC_CLI.NR_CARTAO_ANTIGO,0)               AS NR_CARTAO_ANTIGO, 
	ISNULL(DBO.RC_MDL_VENDA.DS_MDL_VD,'')                AS DS_MDL_VD, 
	ISNULL(DBO.RC_CLI_CREDIARIO.DT_ULT_FECHA,GETDATE())  AS DT_ULT_FECHA , 
	ISNULL(DBO.RC_CLI_CREDIARIO.DT_PROX_FECHA,GETDATE()) AS DT_PROX_FECHA, 
	ISNULL(DBO.RC_CLI_FIDELI_CARTAO.NR_CARTAO, '')       AS NR_CARTAO, 
	ISNULL(DBO.RC_CLI_CREDIARIO.CD_MOT_BLOQ_CLI, 0)      AS CD_MOT_BLOQ_CLI,
	ISNULL(RC_MDL_VENDA.DIA_VENCTO,0)					 AS DIA_VENCTO, 
	ISNULL(RC_MDL_VENDA.DIA_FECHA,0)                     AS DIA_FECHA, 
	ISNULL(RC_MDL_VENDA.TIPO_MDL_VD,0)					 AS TIPO_MDL_VD, 
	ISNULL(RC_MDL_VENDA.TP_FECHA,0) 					 AS TP_FECHA,
	ISNULL(RC_MDL_VENDA.TP_VENCTO,0) 					 AS TP_VENCTO,
	ISNULL(RC_MDL_VENDA.VLR_MIN_PARC,0)					 AS VLR_MIN_PARC
   ,ISNULL(RC_MDL_VENDA.DIAS_BLOQ_ATRASO,0)  			 AS DIAS_BLOQ_ATRASO
   ,ISNULL(RC_MDL_VENDA.NUM_MAX_PARC_EXTRA,0)            AS NUM_MAX_PARC_EXTRA
   ,ISNULL(RC_MDL_VENDA.VLR_ACIMA_PARC_EXTRA,0)          AS VLR_ACIMA_PARC_EXTRA
   ,ISNULL(RC_MDL_VENDA.VLR_MIN_COMPRA,0)                AS VLR_MIN_COMPRA

FROM            
	DBO.RC_CLI 
	LEFT OUTER JOIN      DBO.RC_CLI_CREDIARIO  ON              
	DBO.RC_CLI.CD_EMP = DBO.RC_CLI_CREDIARIO.CD_EMP  AND             
	DBO.RC_CLI.CD_CLI = DBO.RC_CLI_CREDIARIO.CD_CLI 

	LEFT JOIN  DBO.RC_MDL_VENDA  ON              
	DBO.RC_CLI_CREDIARIO.CD_EMP = DBO.RC_MDL_VENDA.CD_EMP  AND             
	DBO.RC_CLI_CREDIARIO.CD_MDL_VD = DBO.RC_MDL_VENDA.CD_MDL_VD 

	LEFT OUTER JOIN DBO.RC_CLI_FIDELI_CARTAO  ON              
	DBO.RC_CLI.CD_EMP = DBO.RC_CLI_FIDELI_CARTAO.CD_EMP  AND             
	DBO.RC_CLI.CD_CLI = DBO.RC_CLI_FIDELI_CARTAO.CD_CLI
