-- =============================================
-- Author:		<Mohammed, Ebid>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE procedure [C6MartPeriodico].[PL_S43ContoCorrenteBF] 
	@Rete char(1), 
	@CodiceFiscale varchar(16)
AS
BEGIN
	SELECT  
	distinct
		dbo.ToShortDateString(ANAG_CC.DATA_APERTURA) AS datadiSottoscrizione, 
		ANAG_CC.COD_CC AS conto, 
		cast(ANAG_CC.SALDOCONTABILE as decimal(15,2)) as Controvalore,
		cast(ANAG_CC.SALDODISPONIBILE as decimal(15,2)) as SALDO_DISPONIBILE,
        null as creditrisk ,
        'n.a.' as creditriskstring,
		0 as var,
		null as varString,
		0 as totaleVar,
		null as totaleVarString,
		 apc.is_APC as isAPC
		-- spb.CODCONF as CodiceContratto
	FROM       
		C6MartPeriodico.ANAG_CC	 
	left join C6StagingPeriodico.SPB_CONTR_SINTESI spb
	on  spb.rete=@rete
		and spb.CODFIS=@CodiceFiscale
		and spb.conto=anag_cc.cod_cc
			--aggiunta apc103
	left join C6MartPeriodico.WSEICC apc
on 
	apc.RETE=@Rete
	and	apc.cod_fiscale=@CodiceFiscale
	and spb.CODCONF = apc.CODCONFC
	--(case when PATRBF.tipo_prodotto='CC' then apc.CODCONFC else apc.CODCONFPR end)
	--fine aggiunta apc103
	WHERE 
		ANAG_CC.rete= @Rete
		AND ANAG_CC.cod_fiscale= @CodiceFiscale
		AND ANAG_CC.SALDOCONTABILE < 0
--	ORDER BY	ANAG_CC.SALDOCONTABILE DESC
END