Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
286 kB
2
Indexable
Never
USE [bsi]
GO

/****** Object:  StoredProcedure [mrms].[lcr_TempTable_Upload_Daily_Create]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_TempTable_Upload_Daily_Create]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_TCPPUMKM_EOM]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Get_Equation_TCPPUMKM_EOM]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_TCPPUMKM_Daily]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Get_Equation_TCPPUMKM_Daily]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_TCPP_EOM]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Get_Equation_TCPP_EOM]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_TCPP_Daily]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Get_Equation_TCPP_Daily]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_TCPLK_EOM]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Get_Equation_TCPLK_EOM]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_TCPLK_Daily]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Get_Equation_TCPLK_Daily]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_TCPK_EOM]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Get_Equation_TCPK_EOM]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_TCPK_Daily]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Get_Equation_TCPK_Daily]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_TCPBI_EOM]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Get_Equation_TCPBI_EOM]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_TCPBI_Daily]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Get_Equation_TCPBI_Daily]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_NasabahUMKMTidakStabil_EOM]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Get_Equation_NasabahUMKMTidakStabil_EOM]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_NasabahUMKMTidakStabil_Daily]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Get_Equation_NasabahUMKMTidakStabil_Daily]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_NasabahUMKMStabil_EOM]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Get_Equation_NasabahUMKMStabil_EOM]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_NasabahUMKMStabil_Daily]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Get_Equation_NasabahUMKMStabil_Daily]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_NasabahPeroranganTidakStabil_EOM]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Get_Equation_NasabahPeroranganTidakStabil_EOM]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_NasabahPeroranganTidakStabil_Daily]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Get_Equation_NasabahPeroranganTidakStabil_Daily]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_NasabahPeroranganStabil_EOM]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Get_Equation_NasabahPeroranganStabil_EOM]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_NasabahPeroranganStabil_Daily]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Get_Equation_NasabahPeroranganStabil_Daily]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_KorporasiOp_EOM]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Get_Equation_KorporasiOp_EOM]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_KorporasiOp_Daily]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Get_Equation_KorporasiOp_Daily]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_KorporasiNonOp_EOM]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Get_Equation_KorporasiNonOp_EOM]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_KorporasiNonOp_Daily]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Get_Equation_KorporasiNonOp_Daily]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_TradeFinance_EOM]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_TradeFinance_EOM]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_TradeFinance_Daily]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_TradeFinance_Daily]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_TCPPUMKM_EOM]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_TCPPUMKM_EOM]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_TCPPUMKM_Daily]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_TCPPUMKM_Daily]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_TCPP_EOM]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_TCPP_EOM]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_TCPP_Daily]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_TCPP_Daily]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_TCPLK_EOM]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_TCPLK_EOM]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_TCPLK_Daily]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_TCPLK_Daily]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_TCPK_EOM]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_TCPK_EOM]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_TCPK_Daily]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_TCPK_Daily]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_TCPBI_EOM]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_TCPBI_EOM]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_TCPBI_Daily]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_TCPBI_Daily]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_Simulasi]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_Simulasi]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_SetaraKas_EOM]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_SetaraKas_EOM]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_SetaraKas_Daily]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_SetaraKas_Daily]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_SBD]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_SBD]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_ReverseRepo]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_ReverseRepo]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_Repo]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_Repo]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_PlacementBI_EOM]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_PlacementBI_EOM]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_PlacementBI_Daily]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_PlacementBI_Daily]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_NonOperasional_EOM]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_NonOperasional_EOM]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_NonOperasional_Daily]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_NonOperasional_Daily]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_NonHQLA]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_NonHQLA]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_NasabahUMKMTidakStabil_EOM]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_NasabahUMKMTidakStabil_EOM]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_NasabahUMKMTidakStabil_Daily]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_NasabahUMKMTidakStabil_Daily]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_NasabahUMKMStabil_EOM]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_NasabahUMKMStabil_EOM]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_NasabahUMKMStabil_Daily]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_NasabahUMKMStabil_Daily]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_NasabahPerorangranStabil_EOM]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_NasabahPerorangranStabil_EOM]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_NasabahPeroranganTidakStabil_EOM]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_NasabahPeroranganTidakStabil_EOM]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_NasabahPeroranganTidakStabil_Daily]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_NasabahPeroranganTidakStabil_Daily]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_NasabahPeroranganStabil_EOM]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_NasabahPeroranganStabil_EOM]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_NasabahPeroranganStabil_Daily]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_NasabahPeroranganStabil_Daily]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_KorporasiOp_EOM]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_KorporasiOp_EOM]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_KorporasiOp_Daily]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_KorporasiOp_Daily]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_KorporasiNonOp_EOM]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_KorporasiNonOp_EOM]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_KorporasiNonOp_Daily]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_KorporasiNonOp_Daily]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_Komitmen_EOM]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_Komitmen_EOM]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_Komitmen_Daily]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_Komitmen_Daily]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_DHSB]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Equation_DHSB]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Checking_Datawarehouse_Exist]    Script Date: 1/12/2024 10:57:07 AM ******/
DROP PROCEDURE [mrms].[lcr_Checking_Datawarehouse_Exist]
GO

/****** Object:  StoredProcedure [mrms].[lcr_Checking_Datawarehouse_Exist]    Script Date: 1/12/2024 10:57:07 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO






-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 22 Jun 2023
-- Description:	count datawarehouse table for date report
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Checking_Datawarehouse_Exist] 
	@reportDate varchar (12)
AS
--declare @reportDate  varchar(12) = '20230531';
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	declare @EndOfMonth date = EOMONTH(cast (@reportDate as date))

	if ( DATENAME(WEEKDAY, @EndOfMonth) = 'Saturday')
		begin
			set @EndOfMonth = DATEADD(day,-1,@EndOfMonth)
		end
		if ( DATENAME(WEEKDAY, @EndOfMonth) = 'Sunday')
		begin
			set @EndOfMonth = DATEADD(day,-2,@EndOfMonth)
		end

	if (@EndOfMonth = cast (@reportDate as date))
	begin 
		Declare @listTableLCR1 varchar (max) = 
		'select case when sum(jumlah) =13 then 1 else 0 end from(select count(distinct TABLE_NAME) jumlah from  INFORMATION_SCHEMA.TABLES
		 where TABLE_SCHEMA =''cfa'' 
		 and TABLE_NAME in(
		 ''KompilasiCOA_'+@reportDate+''',
		 ''Master_01B_Neraca_'+@reportDate +''',''T_MasterForm01_MappingCoa_'+@reportDate +''')
		 union all
		 select count(distinct TABLE_NAME) from  INFORMATION_SCHEMA.TABLES
		 where TABLE_SCHEMA =''ddm'' 
		 and TABLE_NAME in(''masterDPK_BSI_'+@reportDate +''',''masterPembiayaan_BSI_'+@reportDate +''')
		 union all
		 select count(distinct TABLE_NAME) from  INFORMATION_SCHEMA.TABLES
		 where TABLE_SCHEMA =''mis'' 
		 and TABLE_NAME in(''EDW.ACCOUNT_MITRA_REMITTANCE_'+@reportDate +''',''EDW.CIF_DPK_DIVISI_'+@reportDate 
		+''',''EDW.DS_AC_CASH_POOL_'+@reportDate +''',''EDW.DS_CUSTOMER_MASTER_'+@reportDate +''',''BSM_EXCHG_RATE_'+@reportDate  
		+''',''EDW.DS_SALARY_DTL_'+@reportDate 
		+''',''EDW.OP_TRX_CMS_'+@reportDate +''',''TRX.STMT_FT_'+@reportDate +''')) jumlah'

			
			exec (@listTableLCR1)
			print (@listTableLCR1)
	end
	else
	begin
		Declare @listTableLCR2 varchar (max) = 
	'select case when sum(jumlah) =15 then 1 else 0 end from(select count(distinct TABLE_NAME) jumlah from  INFORMATION_SCHEMA.TABLES
	 where TABLE_SCHEMA =''cfa'' 
	 and TABLE_NAME in(''GL_LSMKHarian_Data_'+@reportDate +''',
	 --''KompilasiCOA_'+@reportDate+''',
	 ''Master_01B_Neraca_'+@reportDate +''',''T_MasterForm01_MappingCoa_'+@reportDate +''')
	 --union all
	 --select count(distinct TABLE_NAME) from  INFORMATION_SCHEMA.TABLES
	 --where TABLE_SCHEMA =''ddm'' 
	 --and TABLE_NAME in(''masterDPK_BSI_'+@reportDate +''',''masterPembiayaan_BSI_'+@reportDate +''')
	 union all
	 select count(distinct TABLE_NAME) from  INFORMATION_SCHEMA.TABLES
	 where TABLE_SCHEMA =''mis'' 
	 and TABLE_NAME in(''DS_LD_SCHEDULE_GAB_'+@reportDate +''',''DS_REAL_PROJ_SCHEDULE_N_'+@reportDate
	+''',''EDW.ACCOUNT_MITRA_REMITTANCE_'+@reportDate +''',''EDW.DEPOSITS_'+@reportDate +''',''EDW.CIF_DPK_DIVISI_'+@reportDate 
	+''',''EDW.DS_AC_CASH_POOL_'+@reportDate +''',''EDW.DS_CUSTOMER_MASTER_'+@reportDate +''',''BSM_EXCHG_RATE_'+@reportDate  
	+''',''EDW.LOAN_'+@reportDate +''',''EDW.DS_SALARY_DTL_'+@reportDate 
	+''',''EDW.OP_TRX_CMS_'+@reportDate +''',''TRX.STMT_FT_'+@reportDate +''')) jumlah'

			exec (@listTableLCR2)
			print (@listTableLCR2)
	end
	


END
/*
exec [mrms].[lcr_Checking_Datawarehouse_Exist]'20230428'
go
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_DHSB]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO










-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 22 Jun 2023
-- Description: Data harian surat berharga equation
-- Modified: Febrian Alfandi 04 Jul 2023 ; Reverse Rating Value
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_DHSB]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max),
	 @randomString varchar(10) = ''
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	declare @sql varchar (max) 

	declare @First varchar (20) = '[ratingR]'
	declare @Second varchar (50) = '['
	declare @filterRatingBefore varchar (max) =''
	declare @filterRatingAfter varchar (max)=''
	
	if CHARINDEX('[ratingR]',@filterDinamic) > 0
	begin
		
		declare @countRating int = (len(@filterDinamic) - len(replace(@filterDinamic,'ratingR','')))/7		
		DECLARE @CounterRating INT 
		SET @CounterRating=1
		WHILE ( @CounterRating <= @countRating)
		BEGIN
			SELECT @filterRatingBefore = SUBSTRING(@filterDinamic,CHARINDEX(@First,@filterDinamic)+9, 
			CHARINDEX(@Second,@filterDinamic,CHARINDEX(@Second,@filterDinamic)+1) -CHARINDEX(@Second,@filterDinamic))

			set @filterRatingAfter = @filterRatingBefore;
			declare @Counter INT 
			SET @Counter=1
			declare @maxCounter int =(select count(*) from mrms.[lcr_Rating_View] )
			WHILE ( @Counter <= @maxCounter)
			BEGIN
				declare @code varchar (20),  @value varchar (20)
				select @code = ''''+Code+'''', @value = Value from [mrms].[lcr_Rating_View]  where ID = @Counter
				set @filterRatingAfter =  REPLACE(@filterRatingAfter,@code,''''+@value+'''' )
				SET @Counter  = @Counter  + 1
			END
			set @CounterRating = @CounterRating + 1
			set @filterDinamic = replace (@filterDinamic, @filterRatingBefore, @filterRatingAfter)
			set @filterDinamic =STUFF(@filterDinamic,CHARINDEX('[dataHarianSuratBerharga].[ratingR]',@filterDinamic),len('[dataHarianSuratBerharga].[ratingR]'),'b.value')
		END
	end
	

	set @filterDinamic = replace (@filterDinamic,'inputLPS','currency')
	set @filterDinamic = replace (@filterDinamic,'[dataHarianSuratBerharga]','a')


	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end

	set @sql = '	
			declare @IDR decimal (32,2)=0,@USD decimal (32,2)=0,@Total decimal (32,2)=0
			select @IDR = sum(IDR),
					@USD = sum(USD),
					@total = sum(total)
					from (
						select  IDR = case when a.Currency = ''IDR''  then 
											case when [type] in(''AFS'',''Trading'') then FAIRVALUE  else BOOKVALUE end
										else 0 end * isnull(rate.[KURS_TENGAH],1),
								USD =  case when a.Currency = ''USD''  then 
											case when [type] in(''AFS'',''Trading'') then FAIRVALUE  else BOOKVALUE end
										else 0 end * isnull(rate.[KURS_TENGAH],1),
								Total = case when [type] in(''AFS'',''Trading'') then FAIRVALUE  else BOOKVALUE end  * isnull(rate.[KURS_TENGAH],1)
					from ##DataHarianSuratBerharga a
					--from [dbo].[DataHarianSuratBerharga] a
					inner join [mrms].[lcr_Rating_View] b
					on a.ratingR =b.code
					left join [mis].[BSM_EXCHG_RATE] rate
							  on a.Currency =rate.CCY
						where (1=1) {parent} {children}
					)a'

	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )
	set @sql = replace(@sql,'BSM_EXCHG_RATE','BSM_EXCHG_RATE_'+@reportDate)
	set @sql = replace(@sql,'[mrms].[dataHarianSuratBerharga','[mrms].[dataHarianSuratBerharga_'+@reportDate)
	set @sql = replace(@sql,'##DataHarianSuratBerharga','##DataHarianSuratBerharga'+@randomString)

	set @sql = @sql +' select isnull(@IDR,0) union all select isnull(@USD,0) union all select isnull(@total,0)  '
	exec (@sql)
	--print (@sql)
END
/*
declare @reportDate varchar (10) = '20230619'
declare @filterDinamic varchar (max) = '([dataHarianSuratBerharga].[KategoriPortfolio] = ''Tagihan kepada Pemerintah Negara Lain'' 
AND [dataHarianSuratBerharga].[bobotAtmr] = 0 AND [dataHarianSuratBerharga].[INTRATE] * 4 < case when inputLPS = ''IDR'' then 100 else 100 end  )'
exec [mrms].[lcr_Equation_DHSB] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_Komitmen_Daily]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO












-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 25 May 2023
-- Description:	Persamaan Komitmen
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_Komitmen_Daily]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	set @filterDinamic = replace (@filterDinamic,'[GL_LSMKHarian_Data]','c');
	set @filterDinamic = replace (@filterDinamic,'inputLPS','c.ccy_code')


	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end

    -- Insert statements for procedure here
	declare @sql varchar (max) 
	set @sql = 'declare @IDR decimal (32,2),@USD decimal (32,2),@Total decimal (32,2)
				select @IDR = sum(IDR),@USD = sum(USD), @Total  = sum(total) from (
				select case when c.ccy_code =''IDR'' then c.saldo_akhir_lcy else 0 end IDR,
				case when c.ccy_code =''USD'' then c.saldo_akhir_lcy else 0 end USD,
				c.saldo_akhir_lcy Total  FROM [cfa].[GL_LSMKHarian_Data] c
				  where c.coa in(
				''0-05-2-111'', ''0-05-2-121'', ''0-05-2-122'', ''0-05-2-222'', ''0-05-3-120'')
				 {parent}
				 {children} 
				)a
				'
	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )

	set @sql = replace(@sql,'GL_LSMKHarian_Data','GL_LSMKHarian_Data_'+@reportDate)
	set @sql = @sql +' select abs(isnull(@IDR,0)) union all select abs(isnull(@USD,0)) union all select abs(isnull(@Total,0)) '
	exec (@sql)
END
/*
declare @reportDate varchar (10) = '20230619'
declare @filterDinamic varchar (max) = '[GL_LSMKHarian_Data].[COA_Desc] = ''Kas Besar''
AND [GL_LSMKHarian_Data].[saldo_akhir_acy] * 4 > case when inputLPS = ''IDR'' then 100 else 100 end'
exec [lcr_Equation_Komitmen_Daily] @reportDate,@filterDinamic
*/

GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_Komitmen_EOM]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO












-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 25 May 2023
-- Description:	Persamaan Komitmen
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_Komitmen_EOM]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	set @filterDinamic = replace (@filterDinamic,'[KompilasiCOA]','c');
	set @filterDinamic = replace (@filterDinamic,'inputLPS','c.ccy_code')


	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end

    -- Insert statements for procedure here
	declare @sql varchar (max) 
	set @sql = 'declare @IDR decimal (32,2),@USD decimal (32,2),@Total decimal (32,2)
				select @IDR = sum(IDR),@USD = sum(USD), @Total  = sum(total) from (
				select case when c.ccy_code = ''IDR'' then c.saldo_akhir_lcy else 0 end IDR,
				case when c.ccy_code =''USD'' then c.saldo_akhir_lcy else 0 end USD,
				c.saldo_akhir_lcy Total  FROM [cfa].[KompilasiCOA] c
				  where c.coa in(
				''0-05-2-111'', ''0-05-2-121'', ''0-05-2-122'', ''0-05-2-222'', ''0-05-3-120'')
				 {parent}
				 {children} 
				)a
				'
	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )

	set @sql = replace(@sql,'KompilasiCOA','KompilasiCOA_'+@reportDate)
	set @sql = @sql +' select abs(isnull(@IDR,0)) union all select abs(isnull(@USD,0)) union all select abs(isnull(@Total,0)) '
	exec (@sql)
END
/*
declare @reportDate varchar (10) = '20230630'
declare @filterDinamic varchar (max) = '[KompilasiCOA].[COA_Desc] = ''Kas Besar''
AND [KompilasiCOA].[saldo_akhir_acy] * 4 > case when inputLPS = ''IDR'' then 100 else 100 end'
exec [mrms].[lcr_Equation_Komitmen_EOM] @reportDate,@filterDinamic
*/

GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_KorporasiNonOp_Daily]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO









-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 25 May 2023
-- Description:	Persamaan korporasi not Operasional
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_KorporasiNonOp_Daily]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    declare @sqlResult varchar (max) =''
	set @filterDinamic = replace (@filterDinamic,'[EDW.LOAN].[POKOK_AWAL]','isnull(c.[POKOK_AWAL],0)')
	set @filterDinamic = replace (@filterDinamic,'[EDW.LOAN].[SUM_POKOK_AWAL]','isnull([sumPokokAwal].[pokok_awal],0)')

	set @filterDinamic = replace (@filterDinamic,'[EDW.DS_CUSTOMER_MASTER]','a');
	set @filterDinamic = replace (@filterDinamic,'[EDW.DEPOSITS]','b');
	set @filterDinamic = replace (@filterDinamic,'[EDW.CIF_DPK_DIVISI]','c');
	set @filterDinamic = replace (@filterDinamic,'[EDW.LOAN]','d');
	set @filterDinamic = replace (@filterDinamic,'inputLPS','b.CCY')


	declare @UMKMParentFilter varchar (max) =''
	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =3)
	begin 

		with V as (select value v, Row_Number() over (order by (select 0)) n from String_Split(@filterDinamic, ';'))
		 select
        @UMKMParentFilter = (select v from V where n = 1),
        @parentFilter = (select v from V where n = 2) ,
        @Filter = (select v from V where n = 3) 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end

	declare @summaryPokokAwal varchar (max)='create table #SummaryPokokAwal (CIF_NO varchar (100),pokok_awal decimal (32,2))
	insert into #SummaryPokokAwal
	select CIF_NO,sum(POKOK_AWAL) pokok_awal from [mis].[EDW.LOAN]
	group by CIF_NO '

	declare @checkingTable varchar (max)='	declare @CMS table (CIF_NO varchar(100))
	insert into @CMS
	select distinct CIF_NO from [mis].[EDW.OP_TRX_CMS] b with (nolock)
	declare @CashPool table (recid varchar(100))
	insert into @CashPool
	select distinct recid from  [mis].[EDW.DS_AC_CASH_POOL] b with (nolock)
	declare @Remitance table (account_no varchar(100))
	insert into @Remitance
	select distinct account_no from [mis].[EDW.ACCOUNT_MITRA_REMITTANCE] b with (nolock)
	declare @STMT table (DEBIT_ACCT_NO varchar(100))
	insert into @STMT
	select distinct DEBIT_ACCT_NO from [mis].[TRX.STMT_FT]  b with (nolock)
	where [AS_OF_DT] >= DATEADD(day,-30,	cast('''+@reportDate+''' as date)) 

	'

	declare @cifDivisi varchar (max)='declare @cifdivisi table (CIF_NO varchar(100),DIVISI_CD varchar(100))
	insert into @cifdivisi
	select distinct CIF_NO,DIVISI_CD from [mis].[EDW.CIF_DPK_DIVISI] b with (nolock)'


	declare @checkingTableDeposit varchar (max)='	
	create table #KorporasiAll (ID bigint)
	insert into #KorporasiAll
	select distinct b.ID 
	from [mis].[EDW.DEPOSITS] b with (nolock) 
	left join @CMS e  on e.cif_no =b.cif_no
	left join @CashPool f on f.recid =b.account_no
	left join @Remitance g on g.account_no = b.account_no
	left join @STMT h on h.DEBIT_ACCT_NO =b.account_no
	where (e.CIF_NO is null and f.recid is null and g.account_no is null and h.DEBIT_ACCT_NO is null)


	create table #UMKM (ID bigint)
	insert into #UMKM
	select distinct b.ID 
	from [mis].[EDW.DEPOSITS] b with (nolock) 
	inner join #KorporasiAll x on x.ID = b.id
	left join #SummaryPokokAwal d with (nolock) on b.CIF_NO =d.CIF_NO
	where d.CIF_NO is null

	'


	declare @rawQuery varchar (max) ='
	SELECT B.id,b.CIF_NO,b.ccy,max(b.SALDO_AKHIR) total
	from [mis].[EDW.DS_CUSTOMER_MASTER] a with (nolock)
	inner join [mis].[EDW.DEPOSITS] b with (nolock) on a.CUSTOMER_CODE = b.CIF_NO
	inner join @cifdivisi c on b.CIF_NO =c.CIF_NO
	inner join #KorporasiAll kor on kor.id =b.id
	left join #SummaryPokokAwal sumPokokAwal on b.CIF_NO =sumPokokAwal.CIF_NO
    left join [mis].[EDW.LOAN] d with (nolock) on b.CIF_NO =d.CIF_NO
	where 
	a.cust_type = ''C'' and b.saldo_akhir >=0
	and left(c.divisi_CD,2) in(''IB'', ''CM'', ''CB'',''BB'', ''SM'', ''RD'',''MB'',''FH'')
	and case when left(c.divisi_CD,2) in(''BB'', ''SM'', ''RD'',''MB'',''FH'') then isnull([sumPokokAwal].[pokok_awal],0) else 600000000 end >500000000 
		{parent}
  	and upper(a.NAME_1) not like ''%VENTURA%'' and upper(a.NAME_1) not like ''%KJKS%''
	and upper(a.NAME_1) not like ''%KSP%'' and upper(a.NAME_1) not like ''%BMT%'' 
	and upper(a.NAME_1) not like ''%GADAI%'' and upper(a.NAME_1) not like ''%DANA PENSIUN%''
	and upper(a.NAME_1) not like ''%DAPEN%'' and upper(a.NAME_1) not like ''%ASURANSI%'' 
	and upper(a.NAME_1) not like ''%REKSADANA%'' and upper(a.NAME_1) not like ''%FINANCE%'' 
	and upper(a.NAME_1) not like ''%SEKURITAS%'' and upper(a.NAME_1) not like ''%BANK%''
	and upper(a.NAME_1) not like ''%BPR%'' and upper(a.NAME_1) not like ''%BPD%''
	{parent}
	and b.Product_CD not like ''2%''
		{children} 
	group by b.id,b.CCY,b.CIF_NO
	union
	SELECT B.id,b.CIF_NO,b.CCY,max(b.SALDO_AKHIR) total
	FROM [mis].[EDW.DS_CUSTOMER_MASTER] a with (nolock)
	inner join [mis].[EDW.DEPOSITS] b with (nolock) on a.CUSTOMER_CODE = b.CIF_NO
	inner join @cifdivisi c on b.CIF_NO =c.CIF_NO
	inner join #UMKM kor on kor.id =b.id
	left join #SummaryPokokAwal sumPokokAwal on b.CIF_NO =sumPokokAwal.CIF_NO
	left join [mis].[EDW.LOAN] d with (nolock) on b.CIF_NO =d.CIF_NO
	where a.CUST_TYPE = ''c'' and b.PRODUCT_CD not like ''2%'' and d.CIF_NO is null and b.saldo_akhir >=0 and left (c.[DIVISI_CD] ,2) in(''RD'',''FH'')
  	and upper(a.NAME_1) not like ''%VENTURA%'' and upper(a.NAME_1) not like ''%KJKS%''
	and upper(a.NAME_1) not like ''%KSP%'' and upper(a.NAME_1) not like ''%BMT%'' 
	and upper(a.NAME_1) not like ''%GADAI%'' and upper(a.NAME_1) not like ''%DANA PENSIUN%''
	and upper(a.NAME_1) not like ''%DAPEN%'' and upper(a.NAME_1) not like ''%ASURANSI%'' 
	and upper(a.NAME_1) not like ''%REKSADANA%'' and upper(a.NAME_1) not like ''%FINANCE%'' 
	and upper(a.NAME_1) not like ''%SEKURITAS%'' and upper(a.NAME_1) not like ''%BANK%''
	and upper(a.NAME_1) not like ''%BPR%'' and upper(a.NAME_1) not like ''%BPD%'' {UMKMparentFilter} {parent}
	and b.Product_CD not like ''2%'' {children}
	group by b.id,b.CCY,b.CIF_NO
	
	'
	set @rawQuery = replace(@rawQuery,'{UMKMparentFilter}',case when @UMKMParentFilter <> '' then 'and '+ @UMKMParentFilter else '' end )
	set @rawQuery = replace(@rawQuery,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )

	declare @mainQuery varchar (max)=' select @IDR  += case when CCY =''IDR'' then sum(total) else 0 end,@USD  += case when CCY = ''USD'' then sum(total) else 0 end,
				@Total  += sum(total) from ('+@rawQuery+')b group by b.CCY'


	if @Filter like '%SUM_END_SALDO_AKHIR%'
	begin
			set @sqlResult += [dbo].[summarySaldoFilter] (@Filter ,@mainQuery  ,@rawQuery,'SUM_END_SALDO_AKHIR','wholesaleNonOperational'  )
	end
	else
	begin

		set @mainQuery = replace(@mainQuery,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )
		set @sqlResult += @mainQuery

	end

	declare @variable varchar(100) ='declare @IDR decimal (18,2) =0,@USD decimal (18,2) =0,@Total decimal (18,2) =0'

	set @sqlResult =@cifDivisi + @summaryPokokAwal + @checkingTable + @checkingTableDeposit + @variable  + @sqlResult 

	set @sqlResult = replace(@sqlResult,'EDW.DS_CUSTOMER_MASTER','EDW.DS_CUSTOMER_MASTER_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'EDW.DEPOSITS','EDW.DEPOSITS_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'EDW.CIF_DPK_DIVISI','EDW.CIF_DPK_DIVISI_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'EDW.LOAN','EDW.LOAN_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'EDW.OP_TRX_CMS','EDW.OP_TRX_CMS_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'EDW.DS_AC_CASH_POOL','EDW.DS_AC_CASH_POOL_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'EDW.ACCOUNT_MITRA_REMITTANCE','EDW.ACCOUNT_MITRA_REMITTANCE_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'RX.STMT_FT','RX.STMT_FT_'+@reportDate)

	set @sqlResult += '  select isnull(@IDR,0) union all select isnull(@USD,0) union all select isnull(@TOTAL,0) '

	print (@sqlResult)
	exec (@sqlResult)
END
/*
declare @reportDate varchar (10) = '20230618'
declare @filterDinamic varchar (max) = ''
exec [mrms].[lcr_Equation_KorporasiNonOp_Daily] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_KorporasiNonOp_EOM]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO













-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 25 May 2023
-- Description:	Persamaan UMKM Operasional
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_KorporasiNonOp_EOM]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
    -- Insert statements for procedure here
	declare @sqlResult varchar (max) =''
    -- Insert statements for procedure here
	set @filterDinamic = replace (@filterDinamic,'[MASTERPEMBIAYAAN_BSI].[pencairanPokok','isnull([MASTERPEMBIAYAAN_BSI].[pencairanPokok,0)')
	set @filterDinamic = replace (@filterDinamic,'[MASTERPEMBIAYAAN_BSI].[SUM_PENCAIRANPOKOK]','isnull([spp].[sum_pencairanPokok],0)')
	set @filterDinamic = replace (@filterDinamic,'[masterDPK_BSI]','a');
	set @filterDinamic = replace (@filterDinamic,'[masterPembiayaan_BSI]','b');
		set @filterDinamic = replace (@filterDinamic,'[EDW.CIF_DPK_DIVISI]','Y');
	set @filterDinamic = replace (@filterDinamic,'inputLPS','a.jenisvaluta')

	declare @UMKMParentFilter varchar (max) =''
	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =3)
	begin 

		with V as (select value v, Row_Number() over (order by (select 0)) n from String_Split(@filterDinamic, ';'))
		 select
        @UMKMParentFilter = (select v from V where n = 1),
        @parentFilter = (select v from V where n = 2) ,
        @Filter = (select v from V where n = 3) 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end

	declare @summaryPencairanPokok varchar (max)='create table #SummaryPencairanPokok (CIF_NO varchar (100),sum_pencairanPokok decimal (32,2))
	insert into #SummaryPencairanPokok
	select noCif,sum([pencairanPokok]) from [ddm].[masterPembiayaan_BSI]
	group by noCif '

	declare @checkingTable varchar (max)='	create table #CMS  (CIF_NO varchar(100))
	insert into #CMS
	select distinct CIF_NO from [mis].[EDW.OP_TRX_CMS] b with (nolock)
	create table #CashPool (recid varchar(100))
	insert into #CashPool
	select distinct recid from  [mis].[EDW.DS_AC_CASH_POOL] b with (nolock)
	create table #Remitance (account_no varchar(100))
	insert into #Remitance
	select distinct account_no from [mis].[EDW.ACCOUNT_MITRA_REMITTANCE] b with (nolock)
	create table #STMT (DEBIT_ACCT_NO varchar(100))
	insert into #STMT
	select distinct DEBIT_ACCT_NO from [mis].[TRX.STMT_FT]  b with (nolock)
	where [AS_OF_DT] >= DATEADD(day,-30,	cast('''+@reportDate+''' as date)) 

	'
	declare @cifDivisi varchar (max)='declare @cifdivisi table (CIF_NO varchar(100),DIVISI_CD varchar(100))
	insert into @cifdivisi
	select distinct CIF_NO,DIVISI_CD from [mis].[EDW.CIF_DPK_DIVISI] b with (nolock)'

	declare @checkingTableDPK varchar (max)='	
	create table #KorporasiAll (ID bigint)
	insert into #KorporasiAll
	select distinct b.ID 
	from  [ddm].[masterDPK_BSI] b with (nolock) 
	left join #CMS e  on e.cif_no =b.nocif
	left join #CashPool f on f.recid =b.norekening
	left join #Remitance g on g.account_no = b.norekening
	left join #STMT h on h.DEBIT_ACCT_NO =b.norekening
	where (e.CIF_NO is null and f.recid is null and g.account_no is null and h.DEBIT_ACCT_NO is null)

	create table #UMKM (ID bigint)
	insert into #UMKM
	select distinct b.ID 
	from  [ddm].[masterDPK_BSI] b with (nolock) 
	inner join #KorporasiAll x on x.ID = b.id
	left join #SummaryPencairanPokok d
	on b.nocif = d.CIF_NO
	where d.CIF_NO is null

	'

	declare @rawQuery varchar (max) ='
	SELECT a.id,a.nocif CIF_NO ,a.jenisvaluta CCY,max(a.Saldolsmk) total
	from [ddm].[masterDPK_BSI] a
	left join [ddm].[masterPembiayaan_BSI] b
	on a.nocif =b.nocif
	inner join @cifdivisi y on a.nocif =y.CIF_NO
	inner join #KorporasiAll kor on kor.id =a.id
	left join #SummaryPencairanPokok spp
	on a.nocif = spp.CIF_NO
	where 
	a.individu = ''C''   and a.Saldolsmk >=0
	and upper(a.namaRekening) not like ''%VENTURA%'' and upper(a.namaRekening) not like ''%KJKS%''
	and upper(a.namaRekening) not like ''%KSP%'' and upper(a.namaRekening) not like ''%BMT%'' 
	and upper(a.namaRekening) not like ''%GADAI%'' and upper(a.namaRekening) not like ''%DANA PENSIUN%''
	and upper(a.namaRekening) not like ''%DAPEN%'' and upper(a.namaRekening) not like ''%ASURANSI%'' 
	and upper(a.namaRekening) not like ''%REKSADANA%'' and upper(a.namaRekening) not like ''%FINANCE%'' 
	and upper(a.namaRekening) not like ''%SEKURITAS%'' and upper(a.namaRekening) not like ''%BANK%''
	and upper(a.namaRekening) not like ''%BPR%'' and upper(a.namaRekening) not like ''%BPD%''
	and left(y.[DIVISI_CD],2) in(''IB'', ''CM'', ''CB'',''BB'', ''SM'', ''RD'', ''MB'', ''FH'')
	and case when left(y.[DIVISI_CD],2) in (''BB'', ''SM'', ''RD'', ''MB'', ''FH'') 
	then isnull([spp].[sum_pencairanPokok],0) else 600000000 end >500000000 {parent}
		{children}
	group by a.id,a.jenisvaluta,a.nocif
	union 
	SELECT a.id,a.nocif CIF_NO ,a.jenisvaluta  CCY,max(a.Saldolsmk) total
	from [ddm].[masterDPK_BSI] a with (nolock)
	left join [ddm].[masterPembiayaan_BSI] b with (nolock)
	on a.nocif =b.nocif
	inner join #UMKM kor on kor.id =a.id
	inner join @cifdivisi y on a.nocif =y.CIF_NO
	left join #SummaryPencairanPokok spp
	on a.nocif = spp.CIF_NO
	where a.individu = ''c'' and a.kodeProduk not like ''2%'' and a.Saldolsmk >=0 and left (y.[DIVISI_CD] ,2) in(''RD'',''FH'')
  	and upper(a.namarekening) not like ''%VENTURA%'' and upper(a.namarekening) not like ''%KJKS%''
	and upper(a.namarekening) not like ''%KSP%'' and upper(a.namarekening) not like ''%BMT%'' 
	and upper(a.namarekening) not like ''%GADAI%'' and upper(a.namarekening) not like ''%DANA PENSIUN%''
	and upper(a.namarekening) not like ''%DAPEN%'' and upper(a.namarekening) not like ''%ASURANSI%'' 
	and upper(a.namarekening) not like ''%REKSADANA%'' and upper(a.namarekening) not like ''%FINANCE%'' 
	and upper(a.namarekening) not like ''%SEKURITAS%'' and upper(a.namarekening) not like ''%BANK%''
	and upper(a.namarekening) not like ''%BPR%''  and upper(a.namaRekening) not like ''%BPD%'' {UMKMparentFilter} {parent} 
	 {children}
	group by a.id,a.nocif,a.jenisvaluta '

	set @rawQuery = replace(@rawQuery,'{UMKMparentFilter}',case when @UMKMParentFilter <> '' then 'and '+ @UMKMParentFilter else '' end )
	set @rawQuery = replace(@rawQuery,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )

	declare @mainQuery varchar (max)=' select @IDR  += case when CCY =''IDR'' then sum(total) else 0 end,@USD  += case when CCY = ''USD'' then sum(total) else 0 end,
				@Total  += sum(total) from ('+@rawQuery+')b group by b.CCY'
		

	set @rawQuery = replace(@rawQuery,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )


	if @Filter like '%SUM_END_SALDO_LSMK%'
	begin
			set @sqlResult += [dbo].[summarySaldoFilter] (@Filter ,@mainQuery  ,@rawQuery,'SUM_END_SALDO_LSMK','wholesale'  )
	end
	else
	begin

		set @mainQuery = replace(@mainQuery,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )
		set @sqlResult += @mainQuery

	end

	declare @variable varchar(100) ='declare @IDR decimal (18,2) =0,@USD decimal (18,2) =0,@Total decimal (18,2) =0'

	set @sqlResult = @summaryPencairanPokok + @cifDivisi +  @checkingTable + @variable +@checkingTableDPK + @sqlResult

	set @sqlResult = replace(@sqlResult,'masterDPK_BSI','masterDPK_BSI_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'masterPembiayaan_BSI','masterPembiayaan_BSI_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'EDW.OP_TRX_CMS','EDW.OP_TRX_CMS_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'EDW.DS_AC_CASH_POOL','EDW.DS_AC_CASH_POOL_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'EDW.ACCOUNT_MITRA_REMITTANCE','EDW.ACCOUNT_MITRA_REMITTANCE_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'RX.STMT_FT','RX.STMT_FT_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'EDW.CIF_DPK_DIVISI','EDW.CIF_DPK_DIVISI_'+@reportDate)
	
	set @sqlResult += '  select isnull(@IDR,0) union all select isnull(@USD,0) union all select isnull(@TOTAL,0) '

	print (@sqlResult)
	exec (@sqlResult)
END
/*
declare @reportDate varchar (10) = '20230618'
declare @filterDinamic varchar (max) = 'inputLPS = ''IDR'''
exec [mrms].[lcr_Equation_KorporasiNonOp_EOM] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_KorporasiOp_Daily]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO














-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 25 May 2023
-- Description:	Persamaan wholesale Operasional
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_KorporasiOp_Daily]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	declare @sqlResult varchar (max) =''
    -- Insert statements for procedure here
	set @filterDinamic = replace (@filterDinamic,'[EDW.LOAN].[POKOK_AWAL]','isnull(c.[POKOK_AWAL],0)')
	set @filterDinamic = replace (@filterDinamic,'[EDW.LOAN].[SUM_POKOK_AWAL]','isnull([sumPokokAwal].[pokok_awal],0)')


	set @filterDinamic = replace (@filterDinamic,'[EDW.DS_CUSTOMER_MASTER]','a');
	set @filterDinamic = replace (@filterDinamic,'[EDW.DEPOSITS]','b');
	set @filterDinamic = replace (@filterDinamic,'[EDW.CIF_DPK_DIVISI]','c');
	set @filterDinamic = replace (@filterDinamic,'[EDW.LOAN]','d');
	set @filterDinamic = replace (@filterDinamic,'inputLPS','b.CCY')

	declare @UMKMParentFilter varchar (max) =''
	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =3)
	begin 

		with V as (select value v, Row_Number() over (order by (select 0)) n from String_Split(@filterDinamic, ';'))
		 select
        @UMKMParentFilter = (select v from V where n = 1),
        @parentFilter = (select v from V where n = 2) ,
        @Filter = (select v from V where n = 3) 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end

	declare @summaryPokokAwal varchar (max)='create table #SummaryPokokAwal (CIF_NO varchar (100),pokok_awal decimal (32,2))
	insert into #SummaryPokokAwal
	select CIF_NO,sum(POKOK_AWAL) pokok_awal from [mis].[EDW.LOAN]
	group by CIF_NO '

	declare @checkingTable varchar (max)='	declare @CMS table (CIF_NO varchar(100))
	insert into @CMS
	select distinct CIF_NO from [mis].[EDW.OP_TRX_CMS] b with (nolock)
	declare @CashPool table (recid varchar(100))
	insert into @CashPool
	select distinct recid from  [mis].[EDW.DS_AC_CASH_POOL] b with (nolock)
	declare @Remitance table (account_no varchar(100))
	insert into @Remitance
	select distinct account_no from [mis].[EDW.ACCOUNT_MITRA_REMITTANCE] b with (nolock)
	declare @STMT table (DEBIT_ACCT_NO varchar(100))
	insert into @STMT
	select distinct DEBIT_ACCT_NO from [mis].[TRX.STMT_FT]  b with (nolock)
	where [AS_OF_DT] >= DATEADD(day,-30,	cast('''+@reportDate+''' as date)) 

	'

	declare @cifDivisi varchar (max)='declare @cifdivisi table (CIF_NO varchar(100),DIVISI_CD varchar(100))
	insert into @cifdivisi
	select distinct CIF_NO,DIVISI_CD from [mis].[EDW.CIF_DPK_DIVISI] b with (nolock)'

	declare @checkingTableDeposit varchar (max)='	
	create table #KorporasiAll (ID bigint)
	insert into #KorporasiAll
	select distinct b.ID 
	from [mis].[EDW.DEPOSITS] b with (nolock) 
	left join @CMS e  on e.cif_no =b.cif_no
	left join @CashPool f on f.recid =b.account_no
	left join @Remitance g on g.account_no = b.account_no
	left join @STMT h on h.DEBIT_ACCT_NO =b.account_no
	where (e.CIF_NO is not null or f.recid is not null or g.account_no is not null or h.DEBIT_ACCT_NO is not null)


	create table #UMKM (ID bigint)
	insert into #UMKM
	select distinct b.ID 
	from [mis].[EDW.DEPOSITS] b with (nolock) 
	inner join #KorporasiAll x on x.ID = b.id
	left join #SummaryPokokAwal d with (nolock) on b.CIF_NO =d.CIF_NO
	where d.CIF_NO is null

	'

	declare @rawQuery varchar (max) ='

	SELECT B.id,b.CIF_NO,b.CCY ,max(b.SALDO_AKHIR) total
	from [mis].[EDW.DS_CUSTOMER_MASTER] a with (nolock)
	inner join [mis].[EDW.DEPOSITS] b with (nolock) on a.CUSTOMER_CODE = b.CIF_NO
	inner join @cifdivisi c on b.CIF_NO =c.CIF_NO
	inner join #KorporasiAll kor on kor.id =b.id
	left join #SummaryPokokAwal sumPokokAwal on b.CIF_NO =sumPokokAwal.CIF_NO
	left join [mis].[EDW.LOAN] d with (nolock) on b.CIF_NO =d.CIF_NO
	where a.cust_type = ''C'' and b.CIF_NO <> ''81758472'' and  b.SALDO_AKHIR >=0
	and left(c.divisi_CD,2) in(''IB'', ''CM'', ''CB'',''BB'', ''SM'', ''RD'',''MB'',''FH'')
	and case when left(c.divisi_CD,2) in(''BB'', ''SM'', ''RD'',''MB'',''FH'') then isnull([sumPokokAwal].[pokok_awal],0) else 600000000 end >500000000 {parent}
	{children}
	group by b.id,b.CCY,b.CIF_NO
	union
	SELECT B.id,b.CIF_NO,b.CCY,max(b.SALDO_AKHIR) total
	FROM [mis].[EDW.DS_CUSTOMER_MASTER] a with (nolock)
	inner join [mis].[EDW.DEPOSITS] b with (nolock) on a.CUSTOMER_CODE = b.CIF_NO
	inner join @cifdivisi c on b.CIF_NO =c.CIF_NO
	inner join #UMKM kor on kor.id =b.id
	left join #SummaryPokokAwal sumPokokAwal on b.CIF_NO =sumPokokAwal.CIF_NO
	left join [mis].[EDW.LOAN] d with (nolock) on b.CIF_NO =d.CIF_NO
	where a.CUST_TYPE = ''c'' and b.PRODUCT_CD not like ''2%'' and d.CIF_NO is null and b.saldo_akhir >=0 and left (c.[DIVISI_CD] ,2) in(''RD'',''FH'')
	and b.CIF_NO <> ''81758472''
  	and upper(a.NAME_1) not like ''%VENTURA%'' and upper(a.NAME_1) not like ''%KJKS%''
	and upper(a.NAME_1) not like ''%KSP%'' and upper(a.NAME_1) not like ''%BMT%'' 
	and upper(a.NAME_1) not like ''%GADAI%'' and upper(a.NAME_1) not like ''%DANA PENSIUN%''
	and upper(a.NAME_1) not like ''%DAPEN%'' and upper(a.NAME_1) not like ''%ASURANSI%'' 
	and upper(a.NAME_1) not like ''%REKSADANA%'' and upper(a.NAME_1) not like ''%FINANCE%'' 
	and upper(a.NAME_1) not like ''%SEKURITAS%'' and upper(a.NAME_1) not like ''%BANK%''
	and upper(a.NAME_1) not like ''%BPR%'' {UMKMparentFilter} {parent}
	{children}
	group by b.id,b.CCY,b.CIF_NO
	
	'
	set @rawQuery = replace(@rawQuery,'{UMKMparentFilter}',case when @UMKMParentFilter <> '' then 'and '+ @UMKMParentFilter else '' end )
	set @rawQuery = replace(@rawQuery,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )

	declare @mainQuery varchar (max)=' select @IDR  += case when CCY =''IDR'' then sum(total) else 0 end,@USD  += case when CCY = ''USD'' then sum(total) else 0 end,
				@Total  += sum(total) from ('+@rawQuery+')b group by b.CCY'

	if @Filter like '%SUM_END_SALDO_AKHIR%'
	begin
			set @sqlResult += [dbo].[summarySaldoFilter] (@Filter ,@mainQuery  ,@rawQuery,'SUM_END_SALDO_AKHIR','wholesaleOperational' )
	end
	else
	begin

		set @mainQuery = replace(@mainQuery,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )
		set @sqlResult += @mainQuery
	end

	declare @variable varchar(100) ='declare @IDR decimal (18,2) =0,@USD decimal (18,2) =0,@Total decimal (18,2) =0'

	set @sqlResult =@cifDivisi + @summaryPokokAwal + @checkingTable + @checkingTableDeposit + @variable  + @sqlResult 
		
	set @sqlResult = replace(@sqlResult,'EDW.DS_CUSTOMER_MASTER','EDW.DS_CUSTOMER_MASTER_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'EDW.DEPOSITS','EDW.DEPOSITS_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'EDW.CIF_DPK_DIVISI','EDW.CIF_DPK_DIVISI_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'EDW.LOAN','EDW.LOAN_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'EDW.OP_TRX_CMS','EDW.OP_TRX_CMS_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'EDW.DS_AC_CASH_POOL','EDW.DS_AC_CASH_POOL_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'EDW.ACCOUNT_MITRA_REMITTANCE','EDW.ACCOUNT_MITRA_REMITTANCE_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'RX.STMT_FT','RX.STMT_FT_'+@reportDate)
	
	set @sqlResult += '  select isnull(@IDR,0) union all select isnull(@USD,0) union all select isnull(@TOTAL,0) '

	print (@sqlResult)
	exec (@sqlResult)
	
END
/*
exec sp_executesql N'[mrms].[lcr_Equation_KorporasiOp_Daily_update] @p0,@p1 
',N'@p0 nvarchar(4000),@p1 nvarchar(4000)',@p0=N'20230619',@p1=N'([EDW.LOAN].[SUM_POKOK_AWAL] <= 500000000 AND [EDW.CIF_DPK_DIVISI].[DIVISI_CD] in (''BBG'',''BB1G'',''BB2G'',''MBG'',''BBG_B_TO_B'',''BBG_B_TO_C'',''RDG'',''RDP'',''FHG'') );;(LOG_SUM_END_SALDO_AKHIR_START[EDW.DEPOSITS].[NISBAH_AKHIR] * 4 <= case when inputLPS = ''IDR'' then 5 else 6 end AND LOG_SUM_END_SALDO_AKHIR_END SUM_SUM_END_SALDO_AKHIR_START <= 2000000000SUM_SUM_END_SALDO_AKHIR_END  )'
go
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_KorporasiOp_EOM]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO





-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 25 May 2023
-- Description:	Persamaan UMKM Operasional
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_KorporasiOp_EOM]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	set @filterDinamic = replace (@filterDinamic,'[MASTERPEMBIAYAAN_BSI].[pencairanPokok','isnull([MASTERPEMBIAYAAN_BSI].[pencairanPokok,0)')
	set @filterDinamic = replace (@filterDinamic,'[MASTERPEMBIAYAAN_BSI].[SUM_PENCAIRANPOKOK]','isnull([spp].[sum_pencairanPokok],0)')
	
	set @filterDinamic = replace (@filterDinamic,'[masterDPK_BSI]','a');
	set @filterDinamic = replace (@filterDinamic,'[masterPembiayaan_BSI]','b');
	set @filterDinamic = replace (@filterDinamic,'[EDW.CIF_DPK_DIVISI]','Y');
	set @filterDinamic = replace (@filterDinamic,'inputLPS','a.jenisvaluta')

	declare @UMKMParentFilter varchar (max) =''
	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =3)
	begin 

		with V as (select value v, Row_Number() over (order by (select 0)) n from String_Split(@filterDinamic, ';'))
		 select
        @UMKMParentFilter = (select v from V where n = 1),
        @parentFilter = (select v from V where n = 2) ,
        @Filter = (select v from V where n = 3) 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end

	declare @summaryPencairanPokok varchar (max)='create table #SummaryPencairanPokok (CIF_NO varchar (100),sum_pencairanPokok decimal (32,2))
	insert into #SummaryPencairanPokok
	select noCif,sum([pencairanPokok]) from [ddm].[masterPembiayaan_BSI]
	group by noCif '

	declare @checkingTable varchar (max)='	declare @CMS table (CIF_NO varchar(100))
	insert into @CMS
	select distinct CIF_NO from [mis].[EDW.OP_TRX_CMS] b with (nolock)
	declare @CashPool table (recid varchar(100))
	insert into @CashPool
	select distinct recid from  [mis].[EDW.DS_AC_CASH_POOL] b with (nolock)
	declare @Remitance table (account_no varchar(100))
	insert into @Remitance
	select distinct account_no from [mis].[EDW.ACCOUNT_MITRA_REMITTANCE] b with (nolock)
	declare @STMT table (DEBIT_ACCT_NO varchar(100))
	insert into @STMT
	select distinct DEBIT_ACCT_NO from [mis].[TRX.STMT_FT]  b with (nolock)
	where [AS_OF_DT] >= DATEADD(day,-30,	cast('''+@reportDate+''' as date)) 

	'
	declare @cifDivisi varchar (max)='declare @cifdivisi table (CIF_NO varchar(100),DIVISI_CD varchar(100))
	insert into @cifdivisi
	select distinct CIF_NO,DIVISI_CD from [mis].[EDW.CIF_DPK_DIVISI] b with (nolock)'

	declare @checkingTableDPK varchar (max)='	
	create table #KorporasiOp (ID bigint)
	insert into #KorporasiOp
	select distinct b.ID 
	from  [ddm].[masterDPK_BSI] b with (nolock) 
	left join @CMS e  on e.cif_no =b.nocif
	left join @CashPool f on f.recid =b.norekening
	left join @Remitance g on g.account_no = b.norekening
	left join @STMT h on h.DEBIT_ACCT_NO =b.norekening
	where (e.CIF_NO is not null or f.recid is not null or g.account_no is not null or h.DEBIT_ACCT_NO is not null)

	create table #UMKM (ID bigint)
	insert into #UMKM
	select distinct b.ID 
	from  [ddm].[masterDPK_BSI] b with (nolock) 
	inner join #KorporasiOp x on x.ID = b.id
	left join #SummaryPencairanPokok d
	on b.nocif = d.CIF_NO
	where d.CIF_NO is null

	'


	declare @rawQuery varchar (max) ='
	SELECT a.id,a.nocif CIF_NO ,a.jenisvaluta CCY,max(a.Saldolsmk) total
	from [ddm].[masterDPK_BSI] a with (nolock)
	left join [ddm].[masterPembiayaan_BSI] b with (nolock) on a.nocif =b.nocif
	inner join @cifdivisi y on a.nocif =y.CIF_NO
	inner join #KorporasiOp kor on kor.id =a.id
	left join #SummaryPencairanPokok spp
	on a.nocif = spp.CIF_NO
	where a.individu = ''C'' and a.nocif <> ''81758472'' and a.Saldolsmk >=0
	and left(y.[DIVISI_CD],2) in(''IB'', ''CM'', ''CB'',''BB'', ''SM'', ''RD'', ''MB'', ''FH'')
	and case when left(y.[DIVISI_CD],2) in (''BB'', ''SM'', ''RD'', ''MB'', ''FH'') 
	then isnull([spp].[sum_pencairanPokok],0) else 600000000 end >500000000 {parent}
		{children}
	group by a.id,a.jenisvaluta,a.nocif
	union 
	SELECT a.id,a.nocif CIF_NO ,a.jenisvaluta  CCY,max(a.Saldolsmk) total
	from [ddm].[masterDPK_BSI] a with (nolock)
	left join [ddm].[masterPembiayaan_BSI] b with (nolock)
	on a.nocif =b.nocif
	inner join #UMKM kor on kor.id =a.id
	inner join @cifdivisi y on a.nocif =y.CIF_NO
	left join #SummaryPencairanPokok spp
	on a.nocif = spp.CIF_NO
	where a.individu = ''c'' and a.kodeProduk not like ''2%'' and a.Saldolsmk >=0 and a.nocif <> ''81758472'' and left (y.[DIVISI_CD] ,2) in(''RD'',''FH'')
  	and upper(a.namarekening) not like ''%VENTURA%''
	and upper(a.namarekening) not like ''%KJKS%''
	and upper(a.namarekening) not like ''%KSP%'' 
	and upper(a.namarekening) not like ''%BMT%'' 
	and upper(a.namarekening) not like ''%GADAI%'' 
	and upper(a.namarekening) not like ''%DANA PENSIUN%''
	and upper(a.namarekening) not like ''%DAPEN%'' 
	and upper(a.namarekening) not like ''%ASURANSI%'' 
	and upper(a.namarekening) not like ''%REKSADANA%''
	and upper(a.namarekening) not like ''%FINANCE%'' 
	and upper(a.namarekening) not like ''%SEKURITAS%''
	and upper(a.namarekening) not like ''%BANK%''
	and upper(a.namarekening) not like ''%BPR%'' {UMKMparentFilter} {parent}
	{children}
	group by a.id,a.nocif,a.jenisvaluta '
	
	set @rawQuery = replace(@rawQuery,'{UMKMparentFilter}',case when @UMKMParentFilter <> '' then 'and '+ @UMKMParentFilter else '' end )
	set @rawQuery = replace(@rawQuery,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )

	
	declare @mainQuery varchar (max)=' select @IDR  += case when CCY =''IDR'' then sum(total) else 0 end,@USD  += case when CCY = ''USD'' then sum(total) else 0 end,
				@Total  += sum(total) from ('+@rawQuery+')b group by b.CCY'
		
	declare @sqlResult varchar (max) =''
	if @Filter like '%SUM_END_SALDO_LSMK%'
	begin
			set @sqlResult += [dbo].[summarySaldoFilter] (@Filter ,@mainQuery  ,@rawQuery,'SUM_END_SALDO_LSMK','wholesale'  )
	end
	else
	begin

		set @mainQuery = replace(@mainQuery,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )
		set @sqlResult += @mainQuery

	end
	
	declare @variable varchar(100) ='declare @IDR decimal (18,2) =0,@USD decimal (18,2) =0,@Total decimal (18,2) =0'
	
	set @sqlResult = @summaryPencairanPokok + @cifDivisi +  @checkingTable + @variable +@checkingTableDPK + @sqlResult

	set @sqlResult = replace(@sqlResult,'masterDPK_BSI','masterDPK_BSI_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'masterPembiayaan_BSI','masterPembiayaan_BSI_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'EDW.OP_TRX_CMS','EDW.OP_TRX_CMS_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'EDW.DS_AC_CASH_POOL','EDW.DS_AC_CASH_POOL_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'EDW.ACCOUNT_MITRA_REMITTANCE','EDW.ACCOUNT_MITRA_REMITTANCE_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'RX.STMT_FT','RX.STMT_FT_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'EDW.CIF_DPK_DIVISI','EDW.CIF_DPK_DIVISI_'+@reportDate)
	
	set @sqlResult += '  select isnull(@IDR,0) union all select isnull(@USD,0) union all select isnull(@TOTAL,0) '

	print (@sqlResult)
	exec (@sqlResult)
END
/*
declare @reportDate varchar (10) = '20230630'
declare @filterDinamic varchar (max) = ''
exec  [mrms].[lcr_Equation_KorporasiOp_EOM] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_NasabahPeroranganStabil_Daily]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO










-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 25 May 2023
-- Description:	Persamaan nasabah perorangan stabil
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_NasabahPeroranganStabil_Daily]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	declare @sqlResult varchar (max) =''

	set @filterDinamic = replace (@filterDinamic,'[EDW.DS_CUSTOMER_MASTER]','a');
	set @filterDinamic = replace (@filterDinamic,'[EDW.DEPOSITS]','b')
	set @filterDinamic = replace (@filterDinamic,'inputLPS','b.CCY')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end

	declare @rawQuery varchar (max) ='
		SELECT B.id,b.CIF_NO,b.CCY,max(b.SALDO_AKHIR) total
		FROM [mis].[EDW.DS_CUSTOMER_MASTER] a with (nolock)
		inner join [mis].[EDW.DEPOSITS] b with (nolock)
		on a.CUSTOMER_CODE = b.CIF_NO
		left join [mis].[EDW.LOAN] C with (nolock)
		on b.CIF_NO =C.CIF_NO
		left join [mis].[EDW.DS_SALARY_DTL] d with (nolock)
		on b.ACCOUNT_NO =d.ACCOUNT_NUMBER
		where (a.CUST_TYPE = ''R''  and b.Product_CD not like ''2%'' {parent} )
		and (c.CIF_NO is not null or d.account_number is not null) and b.SALDO_AKHIR >=0
		{children}
		group by B.id,b.CIF_NO,b.CCY'

	set @rawQuery = replace(@rawQuery,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )

	declare @mainQuery varchar (max)=' select @IDR  += case when CCY =''IDR'' then sum(total) else 0 end,@USD  += case when CCY = ''USD'' then sum(total) else 0 end,
				@Total  += sum(total) from ('+@rawQuery+')b group by b.CCY'

	if @Filter like '%SUM_END_SALDO_AKHIR%'
	begin
			set @sqlResult += [dbo].[summarySaldoFilter] (@Filter ,@mainQuery  ,@rawQuery,'SUM_END_SALDO_AKHIR','Perorangan'  )
	end
	else
	begin

		set @mainQuery = replace(@mainQuery,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )
		set @sqlResult += @mainQuery

	end

	declare @variable varchar(100) ='declare @IDR decimal (32,2) =0,@USD decimal (32,2) =0,@Total decimal (32,2) =0'

	set @sqlResult = @variable + @sqlResult

	set @sqlResult = replace(@sqlResult,'EDW.DS_CUSTOMER_MASTER','EDW.DS_CUSTOMER_MASTER_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'EDW.DEPOSITS','EDW.DEPOSITS_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'EDW.LOAN','EDW.LOAN_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'EDW.DS_SALARY_DTL','EDW.DS_SALARY_DTL_'+@reportDate)

	set @sqlResult += '  select isnull(@IDR,0) union all select isnull(@USD,0) union all select isnull(@TOTAL,0) '
	
	print (@sqlResult)
	exec (@sqlResult)

	


END
/*
declare @reportDate varchar (10) = '20230619'
declare @filterDinamic varchar (max) =
			'( [EDW.DS_CUSTOMER_MASTER].[CUST_TYPE] = ''R''
				and [EDW.DEPOSITS].[SALDO_AKHIR] < 2000000000 
				OR [EDW.DEPOSITS].[NISBAH_AKHIR] * 4 < case when inputLPS = ''IDR'' then 5 else 6 end )'
exec[mrms].[lcr_Equation_NasabahPeroranganStabil_Daily] @reportDate,@filterDinamic


exec sp_executesql N'[mrms].[lcr_Equation_NasabahPeroranganStabil_Daily_testing] @p0,@p1 
',N'@p0 nvarchar(4000),@p1 nvarchar(4000)',@p0=N'20230619',@p1=N'(LOG_SUM_END_SALDO_AKHIR_START[EDW.DEPOSITS].[NISBAH_AKHIR] * 4 < case when inputLPS = ''IDR'' then 5 else 6 end AND LOG_SUM_END_SALDO_AKHIR_END SUM_SUM_END_SALDO_AKHIR_START <= 2000000000SUM_SUM_END_SALDO_AKHIR_END  )'
go

exec sp_executesql N'[mrms].[lcr_Equation_NasabahPeroranganStabil_Daily_testing] @p0,@p1 
',N'@p0 nvarchar(4000),@p1 nvarchar(4000)',@p0=N'20230619',@p1=N'(( LOG_SUM_END_SALDO_AKHIR_START [EDW.DEPOSITS].[NISBAH_AKHIR] * 4 < case when inputLPS = ''IDR'' then 5 else 6 end AND LOG_SUM_END_SALDO_AKHIR_END SUM_SUM_END_SALDO_AKHIR_START > 2000000000SUM_SUM_END_SALDO_AKHIR_END  ) OR [EDW.DEPOSITS].[NISBAH_AKHIR] * 4 >= case when inputLPS = ''IDR'' then 5 else 6 end )'
go

*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_NasabahPeroranganStabil_EOM]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO







-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 25 May 2023
-- Description:	Persamaan nasabah perorangan stabil
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_NasabahPeroranganStabil_EOM]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    declare @sqlResult varchar (max) =''
	set @filterDinamic = replace (@filterDinamic,'[MASTERPEMBIAYAAN_BSI].[pencairanPokok','isnull([MASTERPEMBIAYAAN_BSI].[pencairanPokok,0)')
	set @filterDinamic = replace (@filterDinamic,'[MASTERPEMBIAYAAN_BSI].[SUM_PENCAIRANPOKOK]','isnull([spp].[sum_pencairanPokok],0)')
	set @filterDinamic = replace (@filterDinamic,'[masterDPK_BSI]','a');
	set @filterDinamic = replace (@filterDinamic,'[masterPembiayaan_BSI]','b');
	set @filterDinamic = replace (@filterDinamic,'inputLPS','a.jenisvaluta')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end

	declare @summaryPencairanPokok varchar (max)='create table #SummaryPencairanPokok (CIF_NO varchar (100),sum_pencairanPokok decimal (32,2))
	insert into #SummaryPencairanPokok
	select noCif,sum([pencairanPokok]) from [ddm].[masterPembiayaan_BSI]
	group by noCif '



	declare @rawQuery varchar (max) ='
		SELECT a.id,a.nocif CIF_NO ,a.jenisvaluta CCY,max (a.Saldolsmk) total
		from [ddm].[masterDPK_BSI] a
		left join [ddm].[masterPembiayaan_BSI] b
		on a.nocif =b.nocif
		left join #SummaryPencairanPokok spp
		on a.nocif = spp.CIF_NO
		left join [mis].[EDW.DS_SALARY_DTL] d with (nolock)
		on a.norekening =d.ACCOUNT_NUMBER
		where (a.individu = ''R''  and a.kodeProduk not like ''2%'' {parent} )
		and (b.nocif is not null or d.account_number is not null) and a.Saldolsmk >=0
		{children}
		group by a.id,a.nocif,a.jenisvaluta'

	set @rawQuery = replace(@rawQuery,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )

	declare @mainQuery varchar (max)=' select @IDR  += case when CCY =''IDR'' then sum(total) else 0 end,@USD  += case when CCY = ''USD'' then sum(total) else 0 end,
				@Total  += sum(total) from ('+@rawQuery+')b group by b.CCY'

	if @Filter like '%SUM_END_SALDO_LSMK%'
	begin
			set @sqlResult += [dbo].[summarySaldoFilter] (@Filter ,@mainQuery  ,@rawQuery,'SUM_END_SALDO_LSMK','peorangan'  )
	end
	else
	begin
		set @mainQuery = replace(@mainQuery,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )
		set @sqlResult += @mainQuery
	end
	
	declare @variable varchar(100) ='declare @IDR decimal (18,2) =0,@USD decimal (18,2) =0,@Total decimal (18,2) =0'

	set @sqlResult = @variable + @summaryPencairanPokok + @sqlResult

	set @sqlResult = replace(@sqlResult,'masterDPK_BSI','masterDPK_BSI_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'masterPembiayaan_BSI','masterPembiayaan_BSI_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'EDW.DS_SALARY_DTL','EDW.DS_SALARY_DTL_'+@reportDate)

	set @sqlResult += '  select isnull(@IDR,0) union all select isnull(@USD,0) union all select isnull(@TOTAL,0) '
	print(@sqlResult)
	exec (@sqlResult)
	
END
/*
declare @reportDate varchar (10) = '20230630'
declare @filterDinamic varchar (max) = 'inputLPS =''IDR'''
exec [mrms].[lcr_Equation_NasabahPeroranganStabil_EOM] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_NasabahPeroranganTidakStabil_Daily]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO











-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 25 May 2023
-- Description:	Persamaan nasabah perorangan stabil
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_NasabahPeroranganTidakStabil_Daily]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	declare @sqlResult varchar (max) =''

	set @filterDinamic = replace (@filterDinamic,'[EDW.DS_CUSTOMER_MASTER]','a');
	set @filterDinamic = replace (@filterDinamic,'[EDW.DEPOSITS]','b')
	set @filterDinamic = replace (@filterDinamic,'inputLPS','b.CCY')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end

	declare @NonStable varchar(max) = '
	declare @Loan table (CIF_NO varchar(100))
	insert into @Loan
	select distinct CIF_NO from [mis].[EDW.LOAN] b with (nolock)

	declare @salary table (ACCOUNT_NUMBER varchar(100))
	insert into @salary
	select distinct ACCOUNT_NUMBER from [mis].[EDW.DS_SALARY_DTL] b with (nolock)

	create table #tabelTidakStabil  (ID bigint,CIF_NO varchar(100),ACCOUNT_NO varchar(100) )
	insert into #tabelTidakStabil
	SELECT  b.id,max(c.CIF_NO) CIF_NO, max(d.ACCOUNT_NUMBER) ACCOUNT_NO
	FROM [mis].[EDW.DS_CUSTOMER_MASTER] a with (nolock)
	inner join [mis].[EDW.DEPOSITS] b with (nolock)
	on a.CUSTOMER_CODE = b.CIF_NO
	left join @Loan C
	on b.CIF_NO =C.CIF_NO
	left join @salary d
	on b.ACCOUNT_NO =d.ACCOUNT_NUMBER
	where (a.CUST_TYPE = ''R''  and b.Product_CD not like ''2%'' {parent} ) and b.saldo_Akhir>=0
	group by b.id 

	'
	declare @rawQueryNoAcount varchar (max) ='
	select distinct   B.id,b.CIF_NO,b.CCY,(b.SALDO_AKHIR) total
	from #tabelTidakStabil a
	inner join [mis].[EDW.DEPOSITS] b with (nolock)
	on a.id = b.ID
	where  (a.CIF_NO is null and a.ACCOUNT_NO is null)'


	declare @rawQuery varchar (max) ='
	SELECT B.id,b.CIF_NO,b.CCY,max(b.SALDO_AKHIR) total
		FROM [mis].[EDW.DS_CUSTOMER_MASTER] a with (nolock)
		inner join [mis].[EDW.DEPOSITS] b with (nolock)
		on a.CUSTOMER_CODE = b.CIF_NO
		left join @Loan C
		on b.CIF_NO =C.CIF_NO
		left join @salary d
		on b.ACCOUNT_NO =d.ACCOUNT_NUMBER
		where (a.CUST_TYPE = ''R''  and b.Product_CD not like ''2%'' {parent} )
		and (c.CIF_NO is not null or d.account_number is not null) and b.saldo_Akhir>=0
			{children}
		group by B.id,b.CIF_NO,b.CCY'

	set @rawQuery = replace(@rawQuery,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @NonStable = replace(@NonStable,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )

	declare @mainQuery varchar (max)=' select @IDR  += case when CCY =''IDR'' then sum(total) else 0 end,@USD  += case when CCY = ''USD'' then sum(total) else 0 end,
				@Total  += sum(total) from ('+@rawQuery+')b group by b.CCY'

	declare @mainQueryNoAccount varchar (max)=' select @IDR  += case when CCY =''IDR'' then sum(total) else 0 end,@USD  += case when CCY = ''USD'' then sum(total) else 0 end,
				@Total  += sum(total) from  ('+@rawQueryNoAcount+')b group by b.CCY'

	if @Filter like '%SUM_END_SALDO_AKHIR%'
	begin
			set @sqlResult += [dbo].[summarySaldoFilter] (@Filter ,@mainQuery  ,@rawQuery,'SUM_END_SALDO_AKHIR','perorangan'  )
	end
	else
	begin

		set @mainQuery = replace(@mainQuery,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )
		set @sqlResult += @mainQuery

	end

	declare @variable varchar(100) ='declare @IDR decimal (18,2) =0,@USD decimal (18,2) =0,@Total decimal (18,2) =0'

	set @sqlResult =@NonStable +  @variable + @sqlResult + @mainQueryNoAccount

	set @sqlResult = replace(@sqlResult,'EDW.DS_CUSTOMER_MASTER','EDW.DS_CUSTOMER_MASTER_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'EDW.DEPOSITS','EDW.DEPOSITS_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'EDW.LOAN','EDW.LOAN_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'EDW.DS_SALARY_DTL','EDW.DS_SALARY_DTL_'+@reportDate)

	set @sqlResult += '  select isnull(@IDR,0) union all select isnull(@USD,0) union all select isnull(@Total,0) '
	
	print (@sqlResult)
	exec (@sqlResult)

	


END
/*
declare @reportDate varchar (10) = '20230618'
declare @filterDinamic varchar (max) =
			'( [EDW.DS_CUSTOMER_MASTER].[CUST_TYPE] = ''R''
				and [EDW.DEPOSITS].[SALDO_AKHIR] < 2000000000 
				OR [EDW.DEPOSITS].[NISBAH_AKHIR] * 4 < case when inputLPS = ''IDR'' then 5 else 6 end )'
exec[mrms].[lcr_Equation_NasabahPeroranganStabil_Daily] @reportDate,@filterDinamic


exec sp_executesql N'[mrms].[lcr_Equation_NasabahPeroranganStabil_Daily_testing] @p0,@p1 
',N'@p0 nvarchar(4000),@p1 nvarchar(4000)',@p0=N'20230619',@p1=N'(LOG_SUM_END_SALDO_AKHIR_START[EDW.DEPOSITS].[NISBAH_AKHIR] * 4 < case when inputLPS = ''IDR'' then 5 else 6 end AND LOG_SUM_END_SALDO_AKHIR_END SUM_SUM_END_SALDO_AKHIR_START <= 2000000000SUM_SUM_END_SALDO_AKHIR_END  )'
go

exec sp_executesql N'[mrms].[lcr_Equation_NasabahPeroranganStabil_Daily_testing] @p0,@p1 
',N'@p0 nvarchar(4000),@p1 nvarchar(4000)',@p0=N'20230619',@p1=N'(( LOG_SUM_END_SALDO_AKHIR_START [EDW.DEPOSITS].[NISBAH_AKHIR] * 4 < case when inputLPS = ''IDR'' then 5 else 6 end AND LOG_SUM_END_SALDO_AKHIR_END SUM_SUM_END_SALDO_AKHIR_START > 2000000000SUM_SUM_END_SALDO_AKHIR_END  ) OR [EDW.DEPOSITS].[NISBAH_AKHIR] * 4 >= case when inputLPS = ''IDR'' then 5 else 6 end )'
go

*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_NasabahPeroranganTidakStabil_EOM]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO














-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 25 May 2023
-- Description:	Persamaan nasabah perorangan stabil
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_NasabahPeroranganTidakStabil_EOM]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    declare @sqlResult varchar (max) =''
	set @filterDinamic = replace (@filterDinamic,'[MASTERPEMBIAYAAN_BSI].[pencairanPokok','isnull([MASTERPEMBIAYAAN_BSI].[pencairanPokok,0)')
	set @filterDinamic = replace (@filterDinamic,'[MASTERPEMBIAYAAN_BSI].[SUM_PENCAIRANPOKOK]','isnull([spp].[sum_pencairanPokok],0)')
	
	set @filterDinamic = replace (@filterDinamic,'[masterDPK_BSI]','a');
	set @filterDinamic = replace (@filterDinamic,'[masterPembiayaan_BSI]','b');
	set @filterDinamic = replace (@filterDinamic,'inputLPS','a.jenisvaluta')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end

	declare @summaryPencairanPokok varchar (max)='create table #SummaryPencairanPokok (CIF_NO varchar (100),sum_pencairanPokok decimal (32,2))
	insert into #SummaryPencairanPokok
	select noCif,sum([pencairanPokok]) from [ddm].[masterPembiayaan_BSI]
	group by noCif '

	declare @salaryPembiayaan varchar (max)='
	declare @pembiayaan table (nocif varchar(100))
	insert into @pembiayaan
	select distinct nocif from [ddm].[masterPembiayaan_BSI] b

	declare @salary table (ACCOUNT_NUMBER varchar(100))
	insert into @salary
	select distinct ACCOUNT_NUMBER from [mis].[EDW.DS_SALARY_DTL] b

	'

	declare @rawQueryNoAcount varchar (max) ='
		SELECT a.id,a.nocif CIF_NO ,a.jenisvaluta CCY,max (a.Saldolsmk) total
		from [ddm].[masterDPK_BSI] a
		left join [ddm].[masterPembiayaan_BSI] b
		on a.nocif =b.nocif
		left join #SummaryPencairanPokok spp
		on a.nocif = spp.CIF_NO
		left join @salary d
		on a.norekening =d.ACCOUNT_NUMBER
		left join @pembiayaan x
		on a.nocif =x.nocif
		where (a.individu = ''R''  and a.kodeProduk not like ''2%'' {parent} )
		and (x.nocif is null and d.account_number is null) and a.Saldolsmk>0
	 group by a.id,a.jenisvaluta,a.nocif'


	declare @rawQuery varchar (max) ='
		SELECT a.id,a.nocif CIF_NO ,a.jenisvaluta CCY,max (a.Saldolsmk) total
		from [ddm].[masterDPK_BSI] a
		left join [ddm].[masterPembiayaan_BSI] b
		on a.nocif =b.nocif
		left join #SummaryPencairanPokok spp
		on b.nocif = spp.CIF_NO
		left join @salary d
		on a.norekening =d.ACCOUNT_NUMBER
		left join @pembiayaan x
		on a.nocif =x.nocif
		where (a.individu = ''R''  and a.kodeProduk not like ''2%'' {parent} )
		and (x.nocif is not null or d.account_number is not null) and a.Saldolsmk>0
		{children}
	group by a.id,a.jenisvaluta,a.nocif'

	set @rawQuery = replace(@rawQuery,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @rawQueryNoAcount = replace(@rawQueryNoAcount,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )


declare @mainQuery varchar (max)=' select @IDR  += case when CCY =''IDR'' then sum(total) else 0 end,@USD  += case when CCY = ''USD'' then sum(total) else 0 end,
				@Total  += sum(total) from ('+@rawQuery+')b group by b.CCY'

	declare @mainQueryNoAccount varchar (max)=' select @IDR  += case when CCY =''IDR'' then sum(total) else 0 end,@USD  += case when CCY = ''USD'' then sum(total) else 0 end,
				@Total  += sum(total) from  ('+@rawQueryNoAcount+')b group by b.CCY'

	if @Filter like '%SUM_END_SALDO_LSMK%'
	begin
			set @sqlResult += [dbo].[summarySaldoFilter] (@Filter ,@mainQuery  ,@rawQuery,'SUM_END_SALDO_LSMK','perorangan'  )
	end
	else
	begin
		set @mainQuery = replace(@mainQuery,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )
		set @sqlResult += @mainQuery
	end

	declare @variable varchar(100) ='declare @IDR decimal (18,2) =0,@USD decimal (18,2) =0,@Total decimal (18,2) =0'

	set @sqlResult = @salaryPembiayaan+@variable + @summaryPencairanPokok + @sqlResult + @mainQueryNoAccount

	set @sqlResult = replace(@sqlResult,'masterDPK_BSI','masterDPK_BSI_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'masterPembiayaan_BSI','masterPembiayaan_BSI_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'EDW.DS_SALARY_DTL','EDW.DS_SALARY_DTL_'+@reportDate)

	set @sqlResult += '  select isnull(@IDR,0) union all select isnull(@USD,0) union all select isnull(@Total,0) '
	exec (@sqlResult)
	print(@sqlResult)
END
/*
declare @reportDate varchar (10) = '20230630'
declare @filterDinamic varchar (max) = 'inputLPS =''IDR'''
exec [mrms].[lcr_Equation_NasabahPeroranganStabil_EOM] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_NasabahPerorangranStabil_EOM]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO







-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 25 May 2023
-- Description:	Persamaan nasabah perorangan stabil
-- =============================================
create PROCEDURE [mrms].[lcr_Equation_NasabahPerorangranStabil_EOM]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    declare @sqlResult varchar (max) =''
	set @filterDinamic = replace (@filterDinamic,'[MASTERPEMBIAYAAN_BSI].[pencairanPokok','isnull([MASTERPEMBIAYAAN_BSI].[pencairanPokok,0)')
	set @filterDinamic = replace (@filterDinamic,'[MASTERPEMBIAYAAN_BSI].[SUM_PENCAIRANPOKOK]','isnull([spp].[sum_pencairanPokok],0)')
	set @filterDinamic = replace (@filterDinamic,'[masterDPK_BSI]','a');
	set @filterDinamic = replace (@filterDinamic,'[masterPembiayaan_BSI]','b');
	set @filterDinamic = replace (@filterDinamic,'inputLPS','a.jenisvaluta')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end

	declare @summaryPencairanPokok varchar (max)='create table #SummaryPencairanPokok (CIF_NO varchar (100),sum_pencairanPokok decimal (32,2))
	insert into #SummaryPencairanPokok
	select noCif,sum([pencairanPokok]) from [ddm].[masterPembiayaan_BSI]
	group by noCif '



	declare @rawQuery varchar (max) ='
		SELECT a.id,a.nocif CIF_NO ,a.jenisvaluta CCY,max (a.Saldolsmk) total
		from [ddm].[masterDPK_BSI] a
		left join [ddm].[masterPembiayaan_BSI] b
		on a.nocif =b.nocif
		left join #SummaryPencairanPokok spp
		on b.nocif = spp.CIF_NO
		left join [mis].[EDW.DS_SALARY_DTL] d with (nolock)
		on a.norekening =d.ACCOUNT_NUMBER
		where (a.individu = ''R''  and a.kodeProduk not like ''2%'' {parent} )
		and (b.nocif is not null or d.account_number is not null) and a.Saldolsmk >=0
		{children}
		group by a.id,a.nocif,a.jenisvaluta'

	set @rawQuery = replace(@rawQuery,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )

	declare @mainQuery varchar (max)=' select @IDR  += case when CCY =''IDR'' then sum(total) else 0 end,@USD  += case when CCY = ''USD'' then sum(total) else 0 end,
				@Total  += sum(total) from ('+@rawQuery+')b group by b.CCY'

	if @Filter like '%SUM_END_SALDO_LSMK%'
	begin
			set @sqlResult += [dbo].[summarySaldoFilter] (@Filter ,@mainQuery  ,@rawQuery,'SUM_END_SALDO_LSMK'  )
	end
	else
	begin
		set @mainQuery = replace(@mainQuery,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )
		set @sqlResult += @mainQuery
	end
	
	declare @variable varchar(100) ='declare @IDR decimal (18,2) =0,@USD decimal (18,2) =0,@Total decimal (18,2) =0'

	set @sqlResult = @variable + @summaryPencairanPokok + @sqlResult

	set @sqlResult = replace(@sqlResult,'masterDPK_BSI','masterDPK_BSI_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'masterPembiayaan_BSI','masterPembiayaan_BSI_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'EDW.DS_SALARY_DTL','EDW.DS_SALARY_DTL_'+@reportDate)

	set @sqlResult += '  select isnull(@IDR,0) union all select isnull(@USD,0) union all select isnull(@TOTAL,0) '
	print(@sqlResult)
	exec (@sqlResult)
	
END
/*
declare @reportDate varchar (10) = '20230630'
declare @filterDinamic varchar (max) = 'inputLPS =''IDR'''
exec [mrms].[lcr_Equation_NasabahPeroranganStabil_EOM] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_NasabahUMKMStabil_Daily]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO










-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 25 May 2023
-- Description:	Persamaan nasabah UMKM
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_NasabahUMKMStabil_Daily]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	declare @sqlResult varchar (max) =''
	--#region filter update
	set @filterDinamic = replace (@filterDinamic,'[EDW.LOAN].[POKOK_AWAL]','isnull(c.[POKOK_AWAL],0)')
	set @filterDinamic = replace (@filterDinamic,'[EDW.LOAN].[SUM_POKOK_AWAL]','isnull([src].[sum_pencairanPokok],0)')

	set @filterDinamic = replace (@filterDinamic,'[EDW.LOAN].[SUM_POKOK_AWAL]','a')
	set @filterDinamic = replace (@filterDinamic,'[EDW.DS_CUSTOMER_MASTER]','a')
	set @filterDinamic = replace (@filterDinamic,'[EDW.DEPOSITS]','b')
	set @filterDinamic = replace (@filterDinamic,'[EDW.LOAN]','c')
	set @filterDinamic = replace (@filterDinamic,'[EDW.CIF_DPK_DIVISI]','y')
	set @filterDinamic = replace (@filterDinamic,'inputLPS','b.CCY')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end
	--#endregion

	declare @SupportTable varchar (max)='
	create table #cifdivisi (CIF_NO varchar(100),DIVISI_CD varchar(100))
	insert into #cifdivisi
	select distinct CIF_NO,DIVISI_CD from [mis].[EDW.CIF_DPK_DIVISI] b with (nolock)
	create table #CIFKodeBU (CIF_NO varchar (100),kodeBUCount int)
	insert into #CIFKodeBU
	select CIF_NO,count(distinct DIVISI_CD) kodeBUCount from #cifdivisi group by CIF_NO 
	create table #PencairanPokok (CIF_NO varchar (100),pencairan decimal (32,2))
	insert into #PencairanPokok
	select CIF_NO,sum([POKOK_AWAL]) pencairan from [mis].[EDW.LOAN] with (nolock) group by CIF_NO
	create table #CIPembayaranDivisi (CIF_NO varchar (100),sum_pencairanPokok decimal (32,2))
	insert into #CIPembayaranDivisi
	select distinct cust.CUSTOMER_CODE,isnull(b.pencairan,0) 
	from [mis].[EDW.DS_CUSTOMER_MASTER] cust with (nolock)
    inner join [mis].[EDW.DEPOSITS] a with (nolock)
	on cust.CUSTOMER_CODE = a.CIF_NO and cust.CUST_TYPE = ''c'' and a.SALDO_AKHIR >=0
	inner join #cifdivisi y
	on cust.CUSTOMER_CODE =y.CIF_NO
	left join #PencairanPokok b on cust.CUSTOMER_CODE =b.CIF_NO
	'
	
	--#region main query

	declare @rawCIF varchar (max) ='
	create table #CIFUMKM_Pembiayaan (CIF_NO varchar(100),CountDivisi int,idPembiayaan bigint)
	insert into #CIFUMKM_Pembiayaan
	select src.CIF_NO,count(distinct y.DIVISI_CD),min(c.id) from #CIPembayaranDivisi src with (nolock)
	inner join [mis].[EDW.DEPOSITS] a with (nolock)
	on src.CIF_NO = a.CIF_NO and a.PRODUCT_CD not like ''2%'' and a.SALDO_AKHIR >=0
	inner join [mis].[EDW.DS_CUSTOMER_MASTER] cust with (nolock)
	on cust.CUSTOMER_CODE = a.CIF_NO and cust.CUST_TYPE = ''c''  
  	and upper(cust.NAME_1) not like ''%VENTURA%''
	and upper(cust.NAME_1) not like ''%KJKS%''
	and upper(cust.NAME_1) not like ''%KSP%'' 
	and upper(cust.NAME_1) not like ''%BMT%'' 
	and upper(cust.NAME_1) not like ''%GADAI%'' 
	and upper(cust.NAME_1) not like ''%DANA PENSIUN%''
	and upper(cust.NAME_1) not like ''%DAPEN%'' 
	and upper(cust.NAME_1) not like ''%ASURANSI%'' 
	and upper(cust.NAME_1) not like ''%REKSADANA%''
	and upper(cust.NAME_1) not like ''%FINANCE%'' 
	and upper(cust.NAME_1) not like ''%SEKURITAS%''
	and upper(cust.NAME_1) not like ''%BANK%''
	and upper(cust.NAME_1) not like ''%BPR%''
	inner join #cifdivisi y
	on a.CIF_NO =y.CIF_NO
	left join [mis].[EDW.LOAN] C with (nolock)on src.CIF_NO =C.CIF_NO
	where (1=1) {parent}
	group by src.CIF_NO

	create table #CIFUMKM_Divisi (CIF_NO varchar(100))
	insert into #CIFUMKM_Divisi
	select a.CIF_NO from #CIFUMKM_Pembiayaan  a
	inner join #CIFKodeBU b
	on a.cif_no = b.CIF_NO and a.CountDivisi =b.kodeBUCount
	where idPembiayaan is not null

	'

	set @rawCIF = replace(@rawCIF,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )

	declare @rawQuery varchar (max) ='
	SELECT B.id,b.CIF_NO,b.CCY,max(b.SALDO_AKHIR) total
	from #CIPembayaranDivisi src with (nolock)
	inner join #CIFUMKM_Divisi srcUMKM with (nolock) on src.CIF_NO = srcUMKM.CIF_NO
	inner join [mis].[EDW.DEPOSITS] b with (nolock)
	on src.cif_no = b.CIF_NO
	inner join [mis].[EDW.DS_CUSTOMER_MASTER] a with (nolock)
	on a.CUSTOMER_CODE = b.CIF_NO
	inner join #cifdivisi y
	on b.CIF_NO =y.CIF_NO
	left join [mis].[EDW.LOAN] C with (nolock)
	on b.CIF_NO =C.CIF_NO
	where a.CUST_TYPE = ''c'' and b.PRODUCT_CD not like ''2%''
	and c.CIF_NO is not null and b.SALDO_AKHIR>=0
  	and upper(a.NAME_1) not like ''%VENTURA%''
	and upper(a.NAME_1) not like ''%KJKS%''
	and upper(a.NAME_1) not like ''%KSP%'' 
	and upper(a.NAME_1) not like ''%BMT%'' 
	and upper(a.NAME_1) not like ''%GADAI%'' 
	and upper(a.NAME_1) not like ''%DANA PENSIUN%''
	and upper(a.NAME_1) not like ''%DAPEN%'' 
	and upper(a.NAME_1) not like ''%ASURANSI%'' 
	and upper(a.NAME_1) not like ''%REKSADANA%''
	and upper(a.NAME_1) not like ''%FINANCE%'' 
	and upper(a.NAME_1) not like ''%SEKURITAS%''
	and upper(a.NAME_1) not like ''%BANK%''
	and upper(a.NAME_1) not like ''%BPR%'' {parent} 
	{children}
	group by B.id,b.CIF_NO,b.CCY'

	set @rawQuery = replace(@rawQuery,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )

	declare @mainQuery varchar (max)=' select @IDR  += case when CCY =''IDR'' then sum(total) else 0 end,@USD  += case when CCY = ''USD'' then sum(total) else 0 end,
				@Total  += sum(total) from ('+@rawQuery+')b group by b.CCY'

	
	--#endregion

	if @Filter like '%SUM_END_SALDO_AKHIR%'
	begin
			set @sqlResult += [dbo].[summarySaldoFilter] (@Filter ,@mainQuery  ,@rawQuery,'SUM_END_SALDO_AKHIR','umkm'  )
	end
	else
	begin

		set @mainQuery = replace(@mainQuery,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )
		set @sqlResult += @mainQuery

	end

	declare @variable varchar(100) ='declare @IDR decimal (18,2) =0,@USD decimal (18,2) =0,@Total decimal (18,2) =0'

	set @sqlResult =@SupportTable + @rawCIF + @sqlResult
	set @sqlResult = @variable  + @sqlResult
	set @sqlResult = replace(@sqlResult,'EDW.DS_CUSTOMER_MASTER','EDW.DS_CUSTOMER_MASTER_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'EDW.DEPOSITS','EDW.DEPOSITS_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'EDW.LOAN','EDW.LOAN_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'EDW.CIF_DPK_DIVISI','EDW.CIF_DPK_DIVISI_'+@reportDate)

	set @sqlResult += '  select isnull(@IDR,0) union all select isnull(@USD,0) union all select isnull(@TOTAL,0) '

	print (@sqlResult)
	exec (@sqlResult)
END
/*
declare @reportDate varchar (10) = '20230529'
declare @filterDinamic varchar (max) = ''
exec [mrms].[lcr_Equation_NasabahUMKMStabil_Daily_TESTING] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_NasabahUMKMStabil_EOM]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO














-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 25 May 2023
-- Description:	Persamaan nasabah UMKM
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_NasabahUMKMStabil_EOM]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	declare @sqlResult varchar (max) =''

	set @filterDinamic = replace (@filterDinamic,'[MASTERPEMBIAYAAN_BSI].[pencairanPokok','isnull([MASTERPEMBIAYAAN_BSI].[pencairanPokok,0)')
	set @filterDinamic = replace (@filterDinamic,'[MASTERPEMBIAYAAN_BSI].[SUM_PENCAIRANPOKOK]','isnull([src].[sum_pencairanPokok],0)')
	
	set @filterDinamic = replace (@filterDinamic,'[masterDPK_BSI]','a');
	set @filterDinamic = replace (@filterDinamic,'[masterPembiayaan_BSI]','b');
	set @filterDinamic = replace (@filterDinamic,'[EDW.CIF_DPK_DIVISI]','Y');
	set @filterDinamic = replace (@filterDinamic,'inputLPS','a.jenisvaluta')



	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end


	declare @SupportTable varchar (max)='
	create table #cifdivisi (CIF_NO varchar(100),DIVISI_CD varchar(100))
	insert into #cifdivisi
	select distinct CIF_NO,DIVISI_CD from [mis].[EDW.CIF_DPK_DIVISI] b with (nolock)
	create table #CIFKodeBU (CIF_NO varchar (100),kodeBUCount decimal (32,2))
	insert into #CIFKodeBU
	select CIF_NO,count(distinct DIVISI_CD) kodeBUCount from #cifdivisi group by CIF_NO 
	create table #CIPembayaranDivisi (CIF_NO varchar (100),sum_pencairanPokok decimal (32,2))
	insert into #CIPembayaranDivisi
	select distinct a.noCIF,isnull(b.pencairan,0) from [ddm].[masterDPK_BSI] a
	inner join #cifdivisi y
	on a.nocif =y.CIF_NO and a.individu = ''C'' 
	left join (
			select noCif,sum([pencairanPokok]) pencairan 
			from [ddm].[masterPembiayaan_BSI]
	group by noCif) b on a.noCIF =b.noCif
	'

	declare @rawCIF varchar (max) ='
	create table #CIFUMKM_Pembiayaan (CIF_NO varchar(100),CountDivisi int,idPembiayaan bigint)
	insert into #CIFUMKM_Pembiayaan
	select src.CIF_NO,count(distinct y.DIVISI_CD),min(b.id) from #CIPembayaranDivisi src with (nolock)
	inner join [ddm].[masterDPK_BSI] a with (nolock)
	on a.noCIF =src.CIF_NO and a.individu = ''c'' and a.kodeProduk not like ''2%'' and a.Saldolsmk >=0
  			and upper(a.namarekening) not like ''%VENTURA%''
			and upper(a.namarekening) not like ''%KJKS%''
			and upper(a.namarekening) not like ''%KSP%'' 
			and upper(a.namarekening) not like ''%BMT%'' 
			and upper(a.namarekening) not like ''%GADAI%'' 
			and upper(a.namarekening) not like ''%DANA PENSIUN%''
			and upper(a.namarekening) not like ''%DAPEN%'' 
			and upper(a.namarekening) not like ''%ASURANSI%'' 
			and upper(a.namarekening) not like ''%REKSADANA%''
			and upper(a.namarekening) not like ''%FINANCE%'' 
			and upper(a.namarekening) not like ''%SEKURITAS%''
			and upper(a.namarekening) not like ''%BANK%''
			and upper(a.namarekening) not like ''%BPR%'' 
	inner join #cifdivisi y
	on a.nocif =y.CIF_NO
	left join [ddm].[masterPembiayaan_BSI] b on src.CIF_NO =b.nocif
	where (1=1) {parent}
	group by src.CIF_NO

	create table #CIFUMKM_Divisi (CIF_NO varchar(100))
	insert into #CIFUMKM_Divisi
	select a.CIF_NO from #CIFUMKM_Pembiayaan  a
	inner join #CIFKodeBU b
	on a.cif_no = b.CIF_NO and a.CountDivisi =b.kodeBUCount
	where idPembiayaan is not null

	'

	set @rawCIF = replace(@rawCIF,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )


	declare @rawQuery varchar (max) ='
	SELECT a.id,a.nocif CIF_NO ,a.jenisvaluta  CCY,max(a.Saldolsmk) total
	from #CIPembayaranDivisi src with (nolock)
	inner join #CIFUMKM_Divisi srcUMKM with (nolock) on src.CIF_NO = srcUMKM.CIF_NO
	inner join [ddm].[masterDPK_BSI] a with (nolock)
	on a.noCIF =src.CIF_NO and a.individu = ''c'' and a.kodeProduk not like ''2%'' and a.Saldolsmk >=0
  	and upper(a.namarekening) not like ''%VENTURA%''
	and upper(a.namarekening) not like ''%KJKS%''
	and upper(a.namarekening) not like ''%KSP%'' 
	and upper(a.namarekening) not like ''%BMT%'' 
	and upper(a.namarekening) not like ''%GADAI%'' 
	and upper(a.namarekening) not like ''%DANA PENSIUN%''
	and upper(a.namarekening) not like ''%DAPEN%'' 
	and upper(a.namarekening) not like ''%ASURANSI%'' 
	and upper(a.namarekening) not like ''%REKSADANA%''
	and upper(a.namarekening) not like ''%FINANCE%'' 
	and upper(a.namarekening) not like ''%SEKURITAS%''
	and upper(a.namarekening) not like ''%BANK%''
	and upper(a.namarekening) not like ''%BPR%'' 
	inner join #cifdivisi y
	on a.nocif =y.CIF_NO
	left join [ddm].[masterPembiayaan_BSI] b on src.CIF_NO =b.nocif
	where (1=1) {parent} {children}
	group by a.id,a.nocif,a.jenisvaluta '

	set @rawQuery = replace(@rawQuery,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )

	declare @mainQuery varchar (max)=' select @IDR  += case when CCY =''IDR'' then sum(total) else 0 end,@USD  += case when CCY = ''USD'' then sum(total) else 0 end,
				@Total  += sum(total) from ('+@rawQuery+')b group by b.CCY'

	--#endregion

	if @Filter like '%SUM_END_SALDO_LSMK%'
	begin
			set @sqlResult += [dbo].[summarySaldoFilter] (@Filter ,@mainQuery  ,@rawQuery,'SUM_END_SALDO_LSMK','umkm'  )
	end
	else
	begin

		set @mainQuery = replace(@mainQuery,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )
		set @sqlResult += @mainQuery

	end

	declare @variable varchar(100) ='declare @IDR decimal (18,2) =0,@USD decimal (18,2) =0,@Total decimal (18,2) =0'

	set @sqlResult =@SupportTable + @rawCIF + @sqlResult
	set @sqlResult = @variable  + @sqlResult
	set @sqlResult = replace(@sqlResult,'masterDPK_BSI','masterDPK_BSI_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'masterPembiayaan_BSI','masterPembiayaan_BSI_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'EDW.CIF_DPK_DIVISI','EDW.CIF_DPK_DIVISI_'+@reportDate)
	
	set @sqlResult += '  select isnull(@IDR,0) union all select isnull(@USD,0) union all select isnull(@TOTAL,0) '

	print (@sqlResult)
	exec (@sqlResult)
END
/*
declare @reportDate varchar (10) = '20230630'
declare @filterDinamic varchar (max) = 'inputLPS =''IDR'''
exec [mrms].[lcr_Equation_NasabahUMKMStabil_EOM] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_NasabahUMKMTidakStabil_Daily]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO










-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 25 May 2023
-- Description:	Persamaan nasabah UMKM
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_NasabahUMKMTidakStabil_Daily]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	declare @sqlResult varchar (max) =''
	--#region filter update
	set @filterDinamic = replace (@filterDinamic,'[EDW.LOAN].[POKOK_AWAL]','isnull(c.[POKOK_AWAL],0)')
	set @filterDinamic = replace (@filterDinamic,'[EDW.LOAN].[SUM_POKOK_AWAL]','isnull([src].[sum_pencairanPokok],0)')

	set @filterDinamic = replace (@filterDinamic,'[EDW.LOAN].[SUM_POKOK_AWAL]','a')
	set @filterDinamic = replace (@filterDinamic,'[EDW.DS_CUSTOMER_MASTER]','a')
	set @filterDinamic = replace (@filterDinamic,'[EDW.DEPOSITS]','b')
	set @filterDinamic = replace (@filterDinamic,'[EDW.LOAN]','c')
	set @filterDinamic = replace (@filterDinamic,'[EDW.CIF_DPK_DIVISI]','y')
	set @filterDinamic = replace (@filterDinamic,'inputLPS','b.CCY')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end
	--#endregion
	

	--#region main query
	declare @SupportTable varchar (max)='
	create table #cifdivisi (CIF_NO varchar(100),DIVISI_CD varchar(100))
	insert into #cifdivisi
	select distinct CIF_NO,DIVISI_CD from [mis].[EDW.CIF_DPK_DIVISI] b with (nolock)
	create table #CIFKodeBU (CIF_NO varchar (100),kodeBUCount int)
	insert into #CIFKodeBU
	select CIF_NO,count(distinct DIVISI_CD) kodeBUCount from #cifdivisi group by CIF_NO 
	create table #PencairanPokok (CIF_NO varchar (100),pencairan decimal (32,2))
	insert into #PencairanPokok
	select CIF_NO,sum([POKOK_AWAL]) pencairan from [mis].[EDW.LOAN] with (nolock) group by CIF_NO
	create table #CIPembayaranDivisi (CIF_NO varchar (100),sum_pencairanPokok decimal (32,2))
	insert into #CIPembayaranDivisi
	select distinct cust.CUSTOMER_CODE,isnull(b.pencairan,0) 
	from [mis].[EDW.DS_CUSTOMER_MASTER] cust with (nolock)
    inner join [mis].[EDW.DEPOSITS] a with (nolock)
	on cust.CUSTOMER_CODE = a.CIF_NO and cust.CUST_TYPE = ''c'' and a.SALDO_AKHIR >=0
	inner join #cifdivisi y
	on cust.CUSTOMER_CODE =y.CIF_NO
	left join #PencairanPokok b on cust.CUSTOMER_CODE =b.CIF_NO
	'

	declare @rawCIF varchar (max) ='
	create table #CIFUMKM_Pembiayaan (CIF_NO varchar(100),CountDivisi int,idPembiayaan bigint)
	insert into #CIFUMKM_Pembiayaan
	select src.CIF_NO,count(distinct y.DIVISI_CD),min(c.id) from #CIPembayaranDivisi src with (nolock)
	inner join [mis].[EDW.DEPOSITS] a with (nolock)
	on src.CIF_NO = a.CIF_NO and a.PRODUCT_CD not like ''2%'' and a.SALDO_AKHIR >=0
	inner join [mis].[EDW.DS_CUSTOMER_MASTER] cust with (nolock)
	on cust.CUSTOMER_CODE = a.CIF_NO and cust.CUST_TYPE = ''c''  
  	and upper(cust.NAME_1) not like ''%VENTURA%''
	and upper(cust.NAME_1) not like ''%KJKS%''
	and upper(cust.NAME_1) not like ''%KSP%'' 
	and upper(cust.NAME_1) not like ''%BMT%'' 
	and upper(cust.NAME_1) not like ''%GADAI%'' 
	and upper(cust.NAME_1) not like ''%DANA PENSIUN%''
	and upper(cust.NAME_1) not like ''%DAPEN%'' 
	and upper(cust.NAME_1) not like ''%ASURANSI%'' 
	and upper(cust.NAME_1) not like ''%REKSADANA%''
	and upper(cust.NAME_1) not like ''%FINANCE%'' 
	and upper(cust.NAME_1) not like ''%SEKURITAS%''
	and upper(cust.NAME_1) not like ''%BANK%''
	and upper(cust.NAME_1) not like ''%BPR%''
	inner join #cifdivisi y
	on a.CIF_NO =y.CIF_NO
	left join [mis].[EDW.LOAN] C with (nolock)on src.CIF_NO =C.CIF_NO
	where (1=1) {parent}
	group by src.CIF_NO

	create table #CIFUMKM_Divisi (CIF_NO varchar(100))
	insert into #CIFUMKM_Divisi
	select a.CIF_NO from #CIFUMKM_Pembiayaan  a
	inner join #CIFKodeBU b
	on a.cif_no = b.CIF_NO and a.CountDivisi =b.kodeBUCount
	where idPembiayaan is not null

	'

	set @rawCIF = replace(@rawCIF,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )

	declare @CIFNoAkun varchar (max) ='
	delete from #CIFUMKM_Divisi 
	insert into #CIFUMKM_Divisi
	select distinct a.CIF_NO from #CIFUMKM_Pembiayaan  a
	inner join #CIFKodeBU c
	on a.cif_no = c.CIF_NO and a.CountDivisi =c.kodeBUCount
	left join (
	select a.CIF_NO from #CIFUMKM_Pembiayaan  a
	inner join #CIFKodeBU b
	on a.cif_no = b.CIF_NO 
	inner join #cifdivisi c on c.CIF_NO =a.CIF_NO
	where idPembiayaan is  null and left (c.DIVISI_CD,2) in(''RD'',''FH'')
	) b
	on a.CIF_NO =b.CIF_NO
	where idPembiayaan is null and b.CIF_NO is null '


	declare @rawQueryNoAcount varchar (max) ='
	SELECT B.id,b.CIF_NO,b.CCY,max(b.SALDO_AKHIR) total
	from #CIPembayaranDivisi src with (nolock)
	inner join #CIFUMKM_Divisi srcUMKM with (nolock) on src.CIF_NO = srcUMKM.CIF_NO
	inner join [mis].[EDW.DEPOSITS] b with (nolock)
	on src.cif_no = b.CIF_NO
	inner join [mis].[EDW.DS_CUSTOMER_MASTER] a with (nolock)
	on a.CUSTOMER_CODE = b.CIF_NO
	inner join #cifdivisi y
	on b.CIF_NO =y.CIF_NO
	left join [mis].[EDW.LOAN] C with (nolock)
	on b.CIF_NO =C.CIF_NO
	where a.CUST_TYPE = ''c'' and b.PRODUCT_CD not like ''2%''
	and c.CIF_NO is null and b.saldo_akhir >=0 and left(y.[DIVISI_CD],2) not in (''RD'',''FH'')
  	and upper(a.NAME_1) not like ''%VENTURA%''
	and upper(a.NAME_1) not like ''%KJKS%''
	and upper(a.NAME_1) not like ''%KSP%'' 
	and upper(a.NAME_1) not like ''%BMT%'' 
	and upper(a.NAME_1) not like ''%GADAI%'' 
	and upper(a.NAME_1) not like ''%DANA PENSIUN%''
	and upper(a.NAME_1) not like ''%DAPEN%'' 
	and upper(a.NAME_1) not like ''%ASURANSI%'' 
	and upper(a.NAME_1) not like ''%REKSADANA%''
	and upper(a.NAME_1) not like ''%FINANCE%'' 
	and upper(a.NAME_1) not like ''%SEKURITAS%''
	and upper(a.NAME_1) not like ''%BANK%''
	and upper(a.NAME_1) not like ''%BPR%'' {parent} 
	group by b.id,b.CCY,b.CIF_NO'

	declare @rawQuery varchar (max) ='
	SELECT B.id,b.CIF_NO,b.CCY,max(b.SALDO_AKHIR) total
	from #CIPembayaranDivisi src with (nolock)
	inner join #CIFUMKM_Divisi srcUMKM with (nolock) on src.CIF_NO = srcUMKM.CIF_NO
	inner join [mis].[EDW.DEPOSITS] b with (nolock)
	on src.cif_no = b.CIF_NO
	inner join [mis].[EDW.DS_CUSTOMER_MASTER] a with (nolock)
	on a.CUSTOMER_CODE = b.CIF_NO
	inner join #cifdivisi y
	on b.CIF_NO =y.CIF_NO
	left join [mis].[EDW.LOAN] C with (nolock)
	on b.CIF_NO =C.CIF_NO
	where a.CUST_TYPE = ''c'' and b.PRODUCT_CD not like ''2%''
	and c.CIF_NO is not null and b.SALDO_AKHIR>=0
  	and upper(a.NAME_1) not like ''%VENTURA%''
	and upper(a.NAME_1) not like ''%KJKS%''
	and upper(a.NAME_1) not like ''%KSP%'' 
	and upper(a.NAME_1) not like ''%BMT%'' 
	and upper(a.NAME_1) not like ''%GADAI%'' 
	and upper(a.NAME_1) not like ''%DANA PENSIUN%''
	and upper(a.NAME_1) not like ''%DAPEN%'' 
	and upper(a.NAME_1) not like ''%ASURANSI%'' 
	and upper(a.NAME_1) not like ''%REKSADANA%''
	and upper(a.NAME_1) not like ''%FINANCE%'' 
	and upper(a.NAME_1) not like ''%SEKURITAS%''
	and upper(a.NAME_1) not like ''%BANK%''
	and upper(a.NAME_1) not like ''%BPR%'' {parent} 
	{children}
	group by B.id,b.CIF_NO,b.CCY'

	set @rawQuery = replace(@rawQuery,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @rawQueryNoAcount = replace(@rawQueryNoAcount,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )

	declare @mainQuery varchar (max)=' select @IDR  += case when CCY =''IDR'' then sum(total) else 0 end,@USD  += case when CCY = ''USD'' then sum(total) else 0 end,
				@Total  += sum(total) from ('+@rawQuery+')b group by b.CCY'

	declare @mainQueryNoLoanAccount varchar (max)=' select @IDR  += case when CCY =''IDR'' then sum(total) else 0 end,@USD  += case when CCY = ''USD'' then sum(total) else 0 end,
				@Total  += sum(total) from ('+@rawQueryNoAcount+')b group by b.CCY'


	--#endregion

	if @Filter like '%SUM_END_SALDO_AKHIR%'
	begin
			set @sqlResult += [dbo].[summarySaldoFilter] (@Filter ,@mainQuery  ,@rawQuery,'SUM_END_SALDO_AKHIR','umkm'  )
	end
	else
	begin
		set @mainQuery = replace(@mainQuery,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )
		set @sqlResult += @mainQuery 

	end

	declare @variable varchar(100) ='declare @IDR decimal (18,2) =0,@USD decimal (18,2) =0,@Total decimal (18,2) =0'

	set @sqlResult =@SupportTable + @rawCIF + @sqlResult
	set @sqlResult = @variable  + @sqlResult + @CIFNoAkun + @mainQueryNoLoanAccount

	set @sqlResult = replace(@sqlResult,'EDW.DS_CUSTOMER_MASTER','EDW.DS_CUSTOMER_MASTER_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'EDW.DEPOSITS','EDW.DEPOSITS_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'EDW.LOAN','EDW.LOAN_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'EDW.CIF_DPK_DIVISI','EDW.CIF_DPK_DIVISI_'+@reportDate)

	set @sqlResult += '  select isnull(@IDR,0) union all select isnull(@USD,0) union all select isnull(@TOTAL,0) '

	print (@sqlResult)
	exec (@sqlResult)
END
/*
declare @reportDate varchar (10) = '20230529'
declare @filterDinamic varchar (max) = ''
exec [mrms].[lcr_Equation_NasabahUMKMTidakStabil_Daily] @reportDate,@filterDinamic

exec sp_executesql N'[mrms].[lcr_Equation_NasabahUMKMTidakStabil_Daily] @p0,@p1 
',N'@p0 nvarchar(4000),@p1 nvarchar(4000)',@p0=N'20230529',@p1=N'([EDW.LOAN].[SUM_POKOK_AWAL] <= 500000000 AND [EDW.CIF_DPK_DIVISI].[DIVISI_CD] in (''BBG'',''BB1G'',''BB2G'',''MBG'',''BBG B TO B'',''BBG B TO C'') );(( LOG_SUM_END_SALDO_AKHIR_START [EDW.DEPOSITS].[NISBAH_AKHIR] * 4 < case when inputLPS = ''IDR'' then 5 else 6 end AND LOG_SUM_END_SALDO_AKHIR_END SUM_SUM_END_SALDO_AKHIR_START > 200000000000SUM_SUM_END_SALDO_AKHIR_END  ) OR [EDW.DEPOSITS].[NISBAH_AKHIR] * 4 >= case when inputLPS = ''IDR'' then 5 else 6 end )'
go


*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_NasabahUMKMTidakStabil_EOM]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO






-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 25 May 2023
-- Description:	Persamaan nasabah UMKM
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_NasabahUMKMTidakStabil_EOM]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	declare @sqlResult varchar (max) =''

	set @filterDinamic = replace (@filterDinamic,'[MASTERPEMBIAYAAN_BSI].[pencairanPokok','isnull([MASTERPEMBIAYAAN_BSI].[pencairanPokok,0)')
	set @filterDinamic = replace (@filterDinamic,'[MASTERPEMBIAYAAN_BSI].[SUM_PENCAIRANPOKOK]','isnull([src].[sum_pencairanPokok],0)')
	
	set @filterDinamic = replace (@filterDinamic,'[masterDPK_BSI]','a');
	set @filterDinamic = replace (@filterDinamic,'[masterPembiayaan_BSI]','b');
	set @filterDinamic = replace (@filterDinamic,'[EDW.CIF_DPK_DIVISI]','y');
	
	set @filterDinamic = replace (@filterDinamic,'inputLPS','a.jenisvaluta')



	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end

	declare @SupportTable varchar (max)='
	create table #cifdivisi (CIF_NO varchar(100),DIVISI_CD varchar(100))
	insert into #cifdivisi
	select distinct CIF_NO,DIVISI_CD from [mis].[EDW.CIF_DPK_DIVISI] b with (nolock)
	create table #CIFKodeBU (CIF_NO varchar (100),kodeBUCount decimal (32,2))
	insert into #CIFKodeBU
	select CIF_NO,count(distinct DIVISI_CD) kodeBUCount from #cifdivisi group by CIF_NO 
	create table #CIPembayaranDivisi (CIF_NO varchar (100),sum_pencairanPokok decimal (32,2))
	insert into #CIPembayaranDivisi
	select distinct a.noCIF,isnull(b.pencairan,0) from [ddm].[masterDPK_BSI] a
	inner join #cifdivisi y
	on a.nocif =y.CIF_NO and a.individu = ''C'' 
	left join (
			select noCif,sum([pencairanPokok]) pencairan 
			from [ddm].[masterPembiayaan_BSI]
	group by noCif) b on a.noCIF =b.noCif
	'

	declare @rawCIF varchar (max) ='
	create table #CIFUMKM_Pembiayaan (CIF_NO varchar(100),CountDivisi int,idPembiayaan bigint)
	insert into #CIFUMKM_Pembiayaan
	select src.CIF_NO,count(distinct y.DIVISI_CD),min(b.id) from #CIPembayaranDivisi src with (nolock)
	inner join [ddm].[masterDPK_BSI] a with (nolock)
	on a.noCIF =src.CIF_NO and a.individu = ''c'' and a.kodeProduk not like ''2%'' and a.Saldolsmk >=0
  			and upper(a.namarekening) not like ''%VENTURA%''
			and upper(a.namarekening) not like ''%KJKS%''
			and upper(a.namarekening) not like ''%KSP%'' 
			and upper(a.namarekening) not like ''%BMT%'' 
			and upper(a.namarekening) not like ''%GADAI%'' 
			and upper(a.namarekening) not like ''%DANA PENSIUN%''
			and upper(a.namarekening) not like ''%DAPEN%'' 
			and upper(a.namarekening) not like ''%ASURANSI%'' 
			and upper(a.namarekening) not like ''%REKSADANA%''
			and upper(a.namarekening) not like ''%FINANCE%'' 
			and upper(a.namarekening) not like ''%SEKURITAS%''
			and upper(a.namarekening) not like ''%BANK%''
			and upper(a.namarekening) not like ''%BPR%'' 
	inner join #cifdivisi y
	on a.nocif =y.CIF_NO
	left join [ddm].[masterPembiayaan_BSI] b on src.CIF_NO =b.nocif
	where (1=1) {parent}
	group by src.CIF_NO

	create table #CIFUMKM_Divisi (CIF_NO varchar(100))
	insert into #CIFUMKM_Divisi
	select a.CIF_NO from #CIFUMKM_Pembiayaan  a
	inner join #CIFKodeBU b
	on a.cif_no = b.CIF_NO and a.CountDivisi =b.kodeBUCount
	where idPembiayaan is not null

	'
	set @rawCIF = replace(@rawCIF,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )

	declare @CIFNoAkun varchar (max) ='
	delete from #CIFUMKM_Divisi 
	insert into #CIFUMKM_Divisi
	select distinct a.CIF_NO from #CIFUMKM_Pembiayaan  a
	inner join #CIFKodeBU c
	on a.cif_no = c.CIF_NO and a.CountDivisi =c.kodeBUCount
	left join (
	select a.CIF_NO from #CIFUMKM_Pembiayaan  a
	inner join #CIFKodeBU b
	on a.cif_no = b.CIF_NO 
	inner join #cifdivisi c on c.CIF_NO =a.CIF_NO
	where idPembiayaan is  null and left (c.DIVISI_CD,2) in(''RD'',''FH'')
	) b
	on a.CIF_NO =b.CIF_NO
	where idPembiayaan is null and b.CIF_NO is null '

	declare @rawQueryNoAcount varchar (max) ='
	SELECT a.id,a.nocif CIF_NO ,a.jenisvaluta CCY,max(a.Saldolsmk) total
	from #CIPembayaranDivisi src with (nolock)
	inner join #CIFUMKM_Divisi srcUMKM with (nolock) on src.CIF_NO = srcUMKM.CIF_NO
	inner join [ddm].[masterDPK_BSI] a with (nolock)
	on a.noCIF =src.CIF_NO and a.individu = ''c'' and a.kodeProduk not like ''2%'' and a.Saldolsmk >=0
  	and upper(a.namarekening) not like ''%VENTURA%''
	and upper(a.namarekening) not like ''%KJKS%''
	and upper(a.namarekening) not like ''%KSP%'' 
	and upper(a.namarekening) not like ''%BMT%'' 
	and upper(a.namarekening) not like ''%GADAI%'' 
	and upper(a.namarekening) not like ''%DANA PENSIUN%''
	and upper(a.namarekening) not like ''%DAPEN%'' 
	and upper(a.namarekening) not like ''%ASURANSI%'' 
	and upper(a.namarekening) not like ''%REKSADANA%''
	and upper(a.namarekening) not like ''%FINANCE%'' 
	and upper(a.namarekening) not like ''%SEKURITAS%''
	and upper(a.namarekening) not like ''%BANK%''
	and upper(a.namarekening) not like ''%BPR%'' 
	inner join #cifdivisi y
	on a.nocif =y.CIF_NO
	left join [ddm].[masterPembiayaan_BSI] b on src.CIF_NO =b.nocif
	where (1=1) {parent}
	group by a.id,a.nocif,a.jenisvaluta '


	declare @rawQuery varchar (max) ='
	SELECT a.id,a.nocif CIF_NO ,a.jenisvaluta  CCY,max(a.Saldolsmk) total
	from #CIPembayaranDivisi src with (nolock)
	inner join #CIFUMKM_Divisi srcUMKM with (nolock) on src.CIF_NO = srcUMKM.CIF_NO
	inner join [ddm].[masterDPK_BSI] a with (nolock)
	on a.noCIF =src.CIF_NO and a.individu = ''c'' and a.kodeProduk not like ''2%'' and a.Saldolsmk >=0
  	and upper(a.namarekening) not like ''%VENTURA%''
	and upper(a.namarekening) not like ''%KJKS%''
	and upper(a.namarekening) not like ''%KSP%'' 
	and upper(a.namarekening) not like ''%BMT%'' 
	and upper(a.namarekening) not like ''%GADAI%'' 
	and upper(a.namarekening) not like ''%DANA PENSIUN%''
	and upper(a.namarekening) not like ''%DAPEN%'' 
	and upper(a.namarekening) not like ''%ASURANSI%'' 
	and upper(a.namarekening) not like ''%REKSADANA%''
	and upper(a.namarekening) not like ''%FINANCE%'' 
	and upper(a.namarekening) not like ''%SEKURITAS%''
	and upper(a.namarekening) not like ''%BANK%''
	and upper(a.namarekening) not like ''%BPR%'' 
	inner join #cifdivisi y
	on a.nocif =y.CIF_NO
	left join [ddm].[masterPembiayaan_BSI] b on src.CIF_NO =b.nocif
	where (1=1) {parent} {children}
	group by a.id,a.nocif,a.jenisvaluta '

	set @rawQuery = replace(@rawQuery,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @rawQueryNoAcount = replace(@rawQueryNoAcount,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )

	declare @mainQuery varchar (max)=' select @IDR  += case when CCY =''IDR'' then sum(total) else 0 end,@USD  += case when CCY = ''USD'' then sum(total) else 0 end,
				@Total  += sum(total) from ('+@rawQuery+')b group by b.CCY'

	declare @mainQueryNoLoanAccount varchar (max)=' select @IDR  += case when CCY =''IDR'' then sum(total) else 0 end,@USD  += case when CCY = ''USD'' then sum(total) else 0 end,
				@Total  += sum(total) from ('+@rawQueryNoAcount+')b group by b.CCY'
	--#endregion

	if @Filter like '%SUM_END_SALDO_LSMK%'
	begin
			set @sqlResult += [dbo].[summarySaldoFilter] (@Filter ,@mainQuery  ,@rawQuery,'SUM_END_SALDO_LSMK','umkm'  )
	end
	else
	begin

		set @mainQuery = replace(@mainQuery,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )
		set @sqlResult += @mainQuery

	end

	declare @variable varchar(100) ='declare @IDR decimal (18,2) =0,@USD decimal (18,2) =0,@Total decimal (18,2) =0'

	set @sqlResult =@SupportTable + @rawCIF + @sqlResult
	set @sqlResult = @variable + @sqlResult + @CIFNoAkun + @mainQueryNoLoanAccount

	set @sqlResult = replace(@sqlResult,'masterDPK_BSI','masterDPK_BSI_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'masterPembiayaan_BSI','masterPembiayaan_BSI_'+@reportDate)
	set @sqlResult = replace(@sqlResult,'EDW.CIF_DPK_DIVISI','EDW.CIF_DPK_DIVISI_'+@reportDate)
	
	set @sqlResult += '  select isnull(@IDR,0) union all select isnull(@USD,0) union all select isnull(@TOTAL,0) '

	print (@sqlResult)
	exec (@sqlResult)
END
/*
declare @reportDate varchar (10) = '20230630'
declare @filterDinamic varchar (max) = 'inputLPS =''IDR'''
exec [mrms].[lcr_Equation_NasabahUMKMTidakStabil_EOM] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_NonHQLA]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO









-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 22 Jun 2023
-- Description: Data harian surat berharga - non hqla equation 
-- Modified: Febrian Alfandi 04 Jul 2023 ; Reverse Rating Value, maturityDate < 30 hari
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_NonHQLA]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) ,
	 @randomString varchar(10) = ''
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	declare @sql varchar (max) 

	declare @First varchar (20) = '[ratingR]'
	declare @Second varchar (50) = '['
	declare @filterRatingBefore varchar (max) =''
	declare @filterRatingAfter varchar (max)=''
	
	if CHARINDEX('[ratingR]',@filterDinamic) > 0
	begin
		
		declare @countRating int = (len(@filterDinamic) - len(replace(@filterDinamic,'ratingR','')))/7		
		DECLARE @CounterRating INT 
		SET @CounterRating=1
		WHILE ( @CounterRating <= @countRating)
		BEGIN
			SELECT @filterRatingBefore = SUBSTRING(@filterDinamic,CHARINDEX(@First,@filterDinamic)+9, 
			CHARINDEX(@Second,@filterDinamic,CHARINDEX(@Second,@filterDinamic)+1) -CHARINDEX(@Second,@filterDinamic))

			set @filterRatingAfter = @filterRatingBefore;
			declare @Counter INT 
			SET @Counter=1
			declare @maxCounter int =(select count(*) from [mrms].[lcr_Rating_View] )
			WHILE ( @Counter <= @maxCounter)
			BEGIN
				declare @code varchar (20),  @value varchar (20)
				select @code = ''''+Code+'''', @value = Value from [mrms].[lcr_Rating_View]  where ID = @Counter
				set @filterRatingAfter =  REPLACE(@filterRatingAfter,@code,''''+@value+'''' )
				SET @Counter  = @Counter  + 1
			END
			set @CounterRating = @CounterRating + 1
			set @filterDinamic = replace (@filterDinamic, @filterRatingBefore, @filterRatingAfter)
			set @filterDinamic =STUFF(@filterDinamic,CHARINDEX('[dataHarianSuratBerharga].[ratingR]',@filterDinamic),len('[dataHarianSuratBerharga].[ratingR]'),'b.value')
		END
	end
	

	set @filterDinamic = replace (@filterDinamic,'inputLPS','currency')
	set @filterDinamic = replace (@filterDinamic,'[dataHarianSuratBerharga]','a')


	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end

	set @sql = '	
			declare @IDR decimal (32,2)=0,@USD decimal (32,2)=0,@Total decimal (32,2)=0
			select @IDR = sum(IDR),
					@USD = sum(USD),
					@total = sum(total)
					from (
						select  IDR = case when a.Currency = ''IDR''  then 
											case when [type] in(''AFS'',''Trading'') then FAIRVALUE  else BOOKVALUE end
										else 0 end * isnull(rate.[KURS_TENGAH],1),
								USD =  case when a.Currency = ''USD''  then 
											case when [type] in(''AFS'',''Trading'') then FAIRVALUE  else BOOKVALUE end
										else 0 end * isnull(rate.[KURS_TENGAH],1),
								Total = case when [type] in(''AFS'',''Trading'') then FAIRVALUE  else BOOKVALUE end  * isnull(rate.[KURS_TENGAH],1)
					 from ##DataHarianSuratBerharga a
					--from [dbo].[DataHarianSuratBerharga_Replika] a
					inner join [mrms].[lcr_Rating_View] b
					on a.ratingR =b.code
					left join [mis].[BSM_EXCHG_RATE] rate
					on a.Currency =rate.CCY
						where a.[MATURITYDATE] between DATEADD(day,1,  CAST('''+ @reportDate +''' as date)) and DATEADD(day,30,  CAST('''+ @reportDate +''' as date)) {parent} {children}
					)a'

	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )
	set @sql = replace(@sql,'BSM_EXCHG_RATE','BSM_EXCHG_RATE_'+@reportDate)
	set @sql = replace(@sql,'[mrms].[dataHarianSuratBerharga','[mrms].[dataHarianSuratBerharga_'+@reportDate)
	set @sql = replace(@sql,'##DataHarianSuratBerharga','##DataHarianSuratBerharga'+@randomString)
	set @sql = @sql +' select isnull(@IDR,0) union all select isnull(@USD,0) union all select isnull(@total,0)  '
	exec (@sql)
END
/*
declare @reportDate varchar (10) = '20230619'
declare @filterDinamic varchar (max) = '([dataHarianSuratBerharga].[KategoriPortfolio] = ''Tagihan kepada Pemerintah Negara Lain'' 
AND [dataHarianSuratBerharga].[bobotAtmr] = 0 AND [dataHarianSuratBerharga].[INTRATE] * 4 < case when inputLPS = ''IDR'' then 100 else 100 end  )'
exec [mrms].[lcr_Equation_NonHQLA] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_NonOperasional_Daily]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO












-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 25 May 2023
-- Description:	Persamaan non operasional
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_NonOperasional_Daily]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	set @filterDinamic = replace (@filterDinamic,'[Master_01B_Neraca]','a')
	set @filterDinamic = replace (@filterDinamic,'[T_MasterForm01_MappingCoa]','b')
	set @filterDinamic = replace (@filterDinamic,'[GL_LSMKHarian_Data]','c')
	set @filterDinamic = replace (@filterDinamic,'inputLPS','c.ccy_code')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end

    -- Insert statements for procedure here
	declare @sql varchar (max) 
	set @sql = 'declare @IDR decimal (32,2),@USD decimal (32,2),@Total decimal (32,2)
				select @IDR = sum(IDR),
						@USD = sum(USD), 
						@Total  = sum(total) 
				from (
				select case when c.ccy_code =''IDR'' then c.saldo_akhir_lcy else 0 end IDR,
						case when c.ccy_code =''USD'' then c.saldo_akhir_lcy else 0 end USD,
						c.saldo_akhir_lcy Total 
				FROM [cfa].[Master_01B_Neraca] a
				inner join [cfa].[T_MasterForm01_MappingCoa] b
				on cast (a.SandiAntasena as varchar)  = b.SandiLBUt
				inner join [cfa].[GL_LSMKHarian_Data] c
				on b.COA = c.COA
				where cast (SandiAntasena as varchar) 
				in(''02.01.02.01.00.00'',''02.01.02.02.00.00'',''02.02.02.01.00.00'',''02.02.02.02.00.00'',''02.03.02.01.00.00'')
				{parent}
				{children}
				)a

				'
	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'or '+ @Filter else '' end )

	set @sql = replace(@sql,'Master_01B_Neraca','Master_01B_Neraca_'+@reportDate)
	set @sql = replace(@sql,'T_MasterForm01_MappingCoa','T_MasterForm01_MappingCoa_'+@reportDate)
	set @sql = replace(@sql,'GL_LSMKHarian_Data','GL_LSMKHarian_Data_'+@reportDate)
	set @sql = @sql +' select abs(isnull(@IDR,0)) union all select  abs(isnull(@USD,0)) union all select abs(isnull(@Total,0))'
	exec (@sql)
	print (@sql)
END
/*
declare @reportDate varchar (10) = '20230619'
declare @filterDinamic varchar (max) = ''
exec [mrms].[lcr_Equation_NonOperasional_Daily] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_NonOperasional_EOM]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO












-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 25 May 2023
-- Description:	Persamaan non operasional
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_NonOperasional_EOM]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	set @filterDinamic = replace (@filterDinamic,'[Master_01B_Neraca]','a')
	set @filterDinamic = replace (@filterDinamic,'[T_MasterForm01_MappingCoa]','b')
	set @filterDinamic = replace (@filterDinamic,'[KompilasiCOA]','c')
	set @filterDinamic = replace (@filterDinamic,'inputLPS','c.ccy_code')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end

    -- Insert statements for procedure here
	declare @sql varchar (max) 
	set @sql = 'declare @IDR decimal (32,2),@USD decimal (32,2),@Total decimal (32,2)
				select @IDR = sum(IDR),
						@USD = sum(USD), 
						@Total  = sum(total) 
				from (
				select case when c.ccy_code =''IDR'' then c.saldo_akhir_lcy else 0 end IDR,
						case when c.ccy_code =''USD'' then c.saldo_akhir_lcy else 0 end USD,
						c.saldo_akhir_lcy Total 
				FROM [cfa].[Master_01B_Neraca] a
				inner join [cfa].[T_MasterForm01_MappingCoa] b
				on cast (a.SandiAntasena as varchar)  = b.SandiLBUt
				inner join [cfa].[KompilasiCOA] c
				on b.COA = c.COA
				where cast (SandiAntasena as varchar) 
				in(''02.01.02.01.00.00'',''02.01.02.02.00.00'',''02.02.02.01.00.00'',''02.02.02.02.00.00'',''02.03.02.01.00.00'')
				{parent}
				{children}
				)a

				'
	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'or '+ @Filter else '' end )

	set @sql = replace(@sql,'Master_01B_Neraca','Master_01B_Neraca_'+@reportDate)
	set @sql = replace(@sql,'T_MasterForm01_MappingCoa','T_MasterForm01_MappingCoa_'+@reportDate)
	set @sql = replace(@sql,'KompilasiCOA','KompilasiCOA_'+@reportDate)
	set @sql = @sql +' select abs(isnull(@IDR,0)) union all select abs(isnull(@USD,0)) union all select abs(isnull(@Total,0)) '
	exec (@sql)
END
/*
declare @reportDate varchar (10) = '20230630'
declare @filterDinamic varchar (max) = ''
exec [mrms].[lcr_Equation_NonOperasional_EOM] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_PlacementBI_Daily]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO












-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 22 Jun 2023
-- Description: Placement BI equation
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_PlacementBI_Daily]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10) ,
	 @filterDinamic varchar (max)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	
	set @filterDinamic = replace (@filterDinamic,'inputLPS','ccy_code')
	set @filterDinamic = replace (@filterDinamic,'[GL_LSMKHarian_Data]','a')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end

	declare @sql varchar (max) 
	set @sql = 'declare @IDR decimal (32,2),@USD decimal (32,2),@Total decimal (32,2)
				select @IDR = sum(IDR),@USD = sum(USD), @Total  = sum(total) from (
				select 
				case when ccy_code =''IDR'' then saldo_akhir_lcy else 0 end IDR,
				case when ccy_code =''USD'' then saldo_akhir_lcy else 0 end USD,
				saldo_akhir_lcy Total
				FROM [cfa].[GL_LSMKHarian_Data] a 
				where coa in(''1-02-1-100'',''1-02-1-200'', ''1-02-1-411'')  {parent} {children} )a'

	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )

	set @sql = replace(@sql,'GL_LSMKHarian_Data','GL_LSMKHarian_Data_'+@reportDate)
	set @sql = @sql +' select abs(isnull(@IDR,0)) union all select abs(isnull(@USD,0)) union all select abs(isnull(@Total,0)) '
	exec (@sql)
	print (@sql)


END
/*
declare @reportDate varchar (10) = '20230619'
declare @filterDinamic varchar (max) = 
				'[GL_LSMKHarian_Data].COA_Desc =''Giro Wadiah pada BI''  or [GL_LSMKHarian_Data].COA_Desc =''Term Deposit Valas Syariah BI''
				and [GL_LSMKHarian_Data].[saldo_akhir_lcy] * 4 > case when inputLPS = ''IDR'' then 5 else 6 end '
exec [mrms].[lcr_Equation_PlacementBI_Daily] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_PlacementBI_EOM]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO












-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 25 may 2023
-- Description:	Persamaan placement pada BI
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_PlacementBI_EOM]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10) ,
	 @filterDinamic varchar (max)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	
	set @filterDinamic = replace (@filterDinamic,'inputLPS','ccy_code')
	set @filterDinamic = replace (@filterDinamic,'[KompilasiCOA]','a')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end

	declare @sql varchar (max) 
	set @sql = 'declare @IDR decimal (32,2),@USD decimal (32,2),@Total decimal (32,2)
				select @IDR = sum(IDR),@USD = sum(USD), @Total  = sum(total) from (
				select 
				case when ccy_code =''IDR'' then saldo_akhir_lcy else 0 end IDR,
				case when ccy_code =''USD'' then saldo_akhir_lcy else 0 end USD,
				saldo_akhir_lcy Total
				FROM [cfa].[KompilasiCOA] a 
				where coa in(''1-02-1-100'',''1-02-1-200'', ''1-02-1-411'')  {parent} {children} )a'

	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )

	set @sql = replace(@sql,'KompilasiCOA','KompilasiCOA_'+@reportDate)
	set @sql = @sql +' select abs(isnull(@IDR,0)) union all select abs(isnull(@USD,0)) union all select abs(isnull(@Total,0)) '
	exec (@sql)


END
/*
declare @reportDate varchar (10) = '20230630'
declare @filterDinamic varchar (max) = 
				'[KompilasiCOA].COA_Desc =''Giro Wadiah pada BI''  or [KompilasiCOA].COA_Desc =''Term Deposit Valas Syariah BI''
				and [KompilasiCOA].[saldo_akhir_lcy] * 4 > case when inputLPS = ''IDR'' then 5 else 6 end '
exec [mrms].[lcr_Equation_PlacementBI_EOM] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_Repo]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO












-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 25 May 2023
-- Description:	Persamaan REPO
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_Repo]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max),
	 @randomString varchar(10) = ''
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	set @filterDinamic = replace (@filterDinamic,'inputLPS','a.Currency')
	set @filterDinamic = replace (@filterDinamic,'[REPO]','a')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end

    -- Insert statements for procedure here
	declare @sql varchar (max) 
	set @sql = 'declare @IDR decimal (32,2)=0,@USD decimal (32,2)=0,@Total decimal (32,2)=0
				select @IDR = sum(IDR),
						@USD = sum(USD),
						@Total =sum(total)
						from (	
								SELECT case when a.Currency = ''IDR'' then a.nominal else 0 end * isnull(rate.[KURS_TENGAH],1) IDR,
								case when a.Currency = ''USD'' then a.nominal else 0 end * isnull(rate.[KURS_TENGAH],1) USD,
								 a.nominal * isnull(rate.[KURS_TENGAH],1) total
								  from ##REPO a
								   left join [mis].[BSM_EXCHG_RATE] rate
									on a.Currency =rate.CCY
								  where a.[jatuhtempo] between DATEADD(day,1,  CAST('''+ @reportDate +''' as date)) and DATEADD(day,30,  CAST('''+ @reportDate +''' as date))
							  		{parent}
									{children}
							  )a  '

	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )
	set @sql = replace(@sql,'BSM_EXCHG_RATE','BSM_EXCHG_RATE_'+@reportDate)
	set @sql = replace(@sql,'##REPO','##REPO'+@randomString)
	set @sql = @sql +' select isnull(@IDR,0) union all select isnull(@USD,0) union all select isnull(@Total,0) '
	
	exec (@sql)
END
/*
declare @reportDate varchar (10) = '20230618'
declare @filterDinamic varchar (max) = 'inputLPS = ''IDR'''
exec [mrms].[lcr_Equation_Repo] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_ReverseRepo]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO












-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 25 May 2023
-- Description:	Persamaan Reverse REPO
-- Modified :  Febrian Alfandi 25 may 2023 ; replace MaturityDate <= to <
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_ReverseRepo]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max),
	 @randomString varchar(10) = '' 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	set @filterDinamic = replace (@filterDinamic,'inputLPS','a.Currency')
	set @filterDinamic = replace (@filterDinamic,'[REVERSEREPO]','a')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end

    -- Insert statements for procedure here
	declare @sql varchar (max) 
	set @sql = 'declare @IDR decimal (32,2)=0,@USD decimal (32,2)=0,@Total decimal (32,2)=0
				select @IDR = sum(IDR),
						@USD = sum(USD),
						@Total =sum(total)
						from (	
								SELECT case when a.Currency = ''IDR'' then a.nominal else 0 end * isnull(rate.[KURS_TENGAH],1) IDR,
								case when a.Currency = ''USD'' then a.nominal else 0 end * isnull(rate.[KURS_TENGAH],1)  USD,
								 a.nominal * isnull(rate.[KURS_TENGAH],1) total
								  from ##REVERSEREPO a
								left join [mis].[BSM_EXCHG_RATE] rate
								on a.Currency =rate.CCY
								  where a.[jatuhtempo] between DATEADD(day,1,  CAST('''+ @reportDate +''' as date)) and DATEADD(day,30,  CAST('''+ @reportDate +''' as date))
							  		{parent}
									{children}
							  )a  '

	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )
	set @sql = replace(@sql,'BSM_EXCHG_RATE','BSM_EXCHG_RATE_'+@reportDate)
	set @sql = replace(@sql,'##REVERSEREPO','##REVERSEREPO'+@randomString)
	set @sql = @sql +' select isnull(@IDR,0) union all select isnull(@USD,0) union all select isnull(@Total,0) '
	exec (@sql)
END
/*
declare @reportDate varchar (10) = '20230618'
declare @filterDinamic varchar (max) = 'inputLPS = ''IDR'''
exec [mrms].[lcr_Equation_Repo] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_SBD]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO













-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 25 May 2023
-- Description:	Persamaan Surat berharga diterbikan
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_SBD]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) ,
	 @randomString varchar(10) = ''
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	declare @First varchar (20) = '[peringkat]'
	declare @Second varchar (50) = '['
	declare @filterRatingBefore varchar (max) =''
	declare @filterRatingAfter varchar (max)=''
		
	if CHARINDEX('[peringkat]',@filterDinamic) > 0
	begin
		
		declare @countRating int = (len(@filterDinamic) - len(replace(@filterDinamic,'peringkat','')))/9	

		DECLARE @CounterRating INT 
		SET @CounterRating=1
		WHILE ( @CounterRating <= @countRating)
		BEGIN
			SELECT @filterRatingBefore = SUBSTRING(@filterDinamic,CHARINDEX(@First,@filterDinamic)+9, 
			CHARINDEX(@Second,@filterDinamic,CHARINDEX(@Second,@filterDinamic)+1) -CHARINDEX(@Second,@filterDinamic))
				
			set @filterRatingAfter = @filterRatingBefore;
			declare @Counter INT 
			SET @Counter=1
			declare @maxCounter int =(select count(*) from [mrms].[lcr_Rating_View] )
			WHILE ( @Counter <= @maxCounter)
			BEGIN
				
				declare @code varchar (20),  @value varchar (20)
				select @code = ''''+Code+'''', @value = Value from [mrms].[lcr_Rating_View]  where ID = @Counter
				set @filterRatingAfter =  REPLACE(@filterRatingAfter,@code,''''+@value+'''' )
				SET @Counter  = @Counter  + 1
			END
			set @CounterRating = @CounterRating + 1

			set @filterDinamic = replace (@filterDinamic, @filterRatingBefore, @filterRatingAfter)
			set @filterDinamic =STUFF(@filterDinamic,CHARINDEX('[SuratBerhargaDiterbitkan].[peringkat]',@filterDinamic),len('[SuratBerhargaDiterbitkan].[peringkat]'),'b.value')
			
		END
	end


	set @filterDinamic = replace (@filterDinamic,'[SuratBerhargaDiterbitkan]','a')
	set @filterDinamic = replace (@filterDinamic,'inputLPS','a.Currency')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end


    -- Insert statements for procedure here
	declare @sql varchar (max) 
	set @sql = 'declare @IDR decimal (32,2)=0,@USD decimal (32,2)=0,@Total decimal (32,2)=0
				select @IDR = sum(IDR),
						@USD = sum(USD),
						@Total =sum(total)
						from (	
								SELECT case when a.Currency = ''IDR'' then a.nominal else 0 end * isnull(rate.[KURS_TENGAH],1) IDR,
								case when a.Currency = ''USD'' then a.nominal else 0 end * isnull(rate.[KURS_TENGAH],1)  USD,
								 a.nominal * isnull(rate.[KURS_TENGAH],1) total
							  --from SuratBerhargaDiterbitkan_replika a
							  from ##SuratBerhargaDiterbitkan a
							  inner join [mrms].[lcr_Rating_View] b
							  on a.peringkat =b.code
							  left join [mis].[BSM_EXCHG_RATE] rate
							  on a.Currency =rate.CCY
							  where a.[jatuhtempo] between DATEADD(day,1,  CAST('''+ @reportDate +''' as date)) and  DATEADD(day,30,  CAST('''+ @reportDate +''' as date))
							  	{parent}
							    {children}
							  )a  '

	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )
	set @sql = replace(@sql,'BSM_EXCHG_RATE','BSM_EXCHG_RATE_'+@reportDate)
	set @sql = replace(@sql,'##SuratBerhargaDiterbitkan','##SuratBerhargaDiterbitkan'+@randomString)
	set @sql = @sql +' select isnull(@IDR,0) union all select isnull(@USD,0) union all select isnull(@Total,0) '
	exec (@sql)
END
/*
declare @reportDate varchar (10) = '20230619'
declare @filterDinamic varchar (max) = 'inputLPS = ''IDR'''
exec [mrms].[lcr_Equation_SBD] @reportDate,@filterDinamic
*/

GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_SetaraKas_Daily]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO












-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 25 May 2023
-- Description:	Persamaan setara kas
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_SetaraKas_Daily]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	declare @sql varchar (max) 
	set @filterDinamic = replace (@filterDinamic,'inputLPS','c.ccy_code')
	set @filterDinamic = replace (@filterDinamic,'[Master_01B_Neraca]','a')
	set @filterDinamic = replace (@filterDinamic,'[T_MasterForm01_MappingCoa]','b')
	set @filterDinamic = replace (@filterDinamic,'[GL_LSMKHarian_Data]','c')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end

	set @sql = 'declare @IDR decimal (32,2),@USD decimal (32,2),@Total decimal (32,2)
				select @IDR = sum(IDR),@USD = sum(USD), @Total  = sum(total) from (
				
				select case when c.ccy_code =''IDR'' then c.saldo_akhir_lcy else 0 end IDR,
				case when c.ccy_code =''USD'' then c.saldo_akhir_lcy else 0 end USD,
				c.saldo_akhir_lcy Total FROM [cfa].[Master_01B_Neraca] a
				inner join [cfa].T_MasterForm01_MappingCoa b on CONVERT(VARCHAR, a.SandiAntasena) = b.SandiLBUT
				inner join [cfa].[GL_LSMKHarian_Data] c on c.coa =b.COA
				where CONVERT(VARCHAR, SandiAntasena) =''01.01.00.00.00.00'' {parent} {children}
				)a
				'
	
	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )

	set @sql = replace(@sql,'Master_01B_Neraca','Master_01B_Neraca_'+@reportDate)
	set @sql = replace(@sql,'T_MasterForm01_MappingCoa','T_MasterForm01_MappingCoa_'+@reportDate)
	set @sql = replace(@sql,'GL_LSMKHarian_Data','GL_LSMKHarian_Data_'+@reportDate)

	set @sql = @sql +' select abs(isnull(@IDR,0)) union all select abs(isnull(@USD,0)) union all select abs(isnull(@Total,0))  '
	exec (@sql)
	print (@sql)
END
/*
declare @reportDate varchar (10) = '20230619'
declare @filterDinamic varchar (max) = 
				'[GL_LSMKHarian_Data].COA_Desc =''Kas Dalam Perjalanan'' 
				and [T_MasterForm01_MappingCoa].[SandiLBUS2013] = ''100''
				and [GL_LSMKHarian_Data].[saldo_akhir_lcy] * 4 > case when inputLPS = ''IDR'' then 5 else 6 end 
				and [Master_01B_Neraca].[SandiAPOLO] =''10101010000000000'''
exec [mrms].[lcr_Equation_SetaraKas_Daily] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_SetaraKas_EOM]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO












-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 25 May 2023
-- Description:	Persamaan setara kas akhir bulan
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_SetaraKas_EOM]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	declare @sql varchar (max) 
	set @filterDinamic = replace (@filterDinamic,'inputLPS','c.ccy_code')
	set @filterDinamic = replace (@filterDinamic,'[Master_01B_Neraca]','a')
	set @filterDinamic = replace (@filterDinamic,'[T_MasterForm01_MappingCoa]','b')
	set @filterDinamic = replace (@filterDinamic,'[KompilasiCOA]','c')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end
	set @sql = 'declare @IDR decimal (32,2),@USD decimal (32,2),@Total decimal (32,2)
				select @IDR = sum(IDR),@USD = sum(USD), @Total  = sum(total) from (
				
				select case when c.ccy_code =''IDR'' then c.saldo_akhir_lcy else 0 end IDR,
				case when c.ccy_code =''USD'' then c.saldo_akhir_lcy else 0 end USD,
				c.saldo_akhir_lcy Total FROM [cfa].[Master_01B_Neraca] a
				inner join [cfa].T_MasterForm01_MappingCoa b on CONVERT(VARCHAR, a.SandiAntasena) = b.SandiLBUT
				inner join [cfa].[KompilasiCOA] c on c.coa =b.COA
				where CONVERT(VARCHAR, SandiAntasena) =''01.01.00.00.00.00'' {parent} {children}
				)a
				'
	
		set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )

	set @sql = replace(@sql,'Master_01B_Neraca','Master_01B_Neraca_'+@reportDate)
	set @sql = replace(@sql,'T_MasterForm01_MappingCoa','T_MasterForm01_MappingCoa_'+@reportDate)
	set @sql = replace(@sql,'KompilasiCOA','KompilasiCOA_'+@reportDate)

	set @sql = @sql +' select abs(isnull(@IDR,0)) union all select abs(isnull(@USD,0)) union all select abs(isnull(@Total,0)) '
	exec (@sql)
END
/*
declare @reportDate varchar (10) = '20230630'
declare @filterDinamic varchar (max) = 
				'[KompilasiCOA].[COA_Desc] =''Kas Besar'' 
				and [T_MasterForm01_MappingCoa].[SandiLBUS2013] = ''100''
				and [KompilasiCOA].[saldo_akhir_lcy] * 4 > case when inputLPS = ''IDR'' then 5 else 6 end 
				and [Master_01B_Neraca].[SandiAPOLO] =''10101010000000000'''
exec [mrms].[lcr_Equation_SetaraKas_EOM] @reportDate,@filterDinamic
*/


GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_Simulasi]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO












-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 25 May 2023
-- Description:	Persamaan simulasi
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_Simulasi]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max),
	 @randomString varchar(10) = '' 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	declare @First varchar (20) = '[peringkat]'
	declare @Second varchar (50) = '['
	declare @filterRatingBefore varchar (max) =''
	declare @filterRatingAfter varchar (max)=''
		
	if CHARINDEX('[peringkat]',@filterDinamic) > 0
	begin
		
		declare @countRating int = (len(@filterDinamic) - len(replace(@filterDinamic,'peringkat','')))/9	

		DECLARE @CounterRating INT 
		SET @CounterRating=1
		WHILE ( @CounterRating <= @countRating)
		BEGIN
			SELECT @filterRatingBefore = SUBSTRING(@filterDinamic,CHARINDEX(@First,@filterDinamic)+9, 
			CHARINDEX(@Second,@filterDinamic,CHARINDEX(@Second,@filterDinamic)+1) -CHARINDEX(@Second,@filterDinamic))
					
			set @filterRatingAfter = @filterRatingBefore;
			declare @Counter INT 
			SET @Counter=1
			declare @maxCounter int =(select count(*) from [mrms].[lcr_Rating_View] )
			WHILE ( @Counter <= @maxCounter)
			BEGIN
				declare @code varchar (20),  @value varchar (20)
				select @code = ''''+Code+'''', @value = Value from [mrms].[lcr_Rating_View]  where ID = @Counter
				set @filterRatingAfter =  REPLACE(@filterRatingAfter,@code,''''+@value+'''' )
				SET @Counter  = @Counter  + 1
				
			END
			set @CounterRating = @CounterRating + 1
		
			set @filterDinamic = replace (@filterDinamic, @filterRatingBefore, @filterRatingAfter)
			set @filterDinamic =STUFF(@filterDinamic,CHARINDEX('[simulasi].[peringkat]',@filterDinamic),len('[simulasi].[peringkat]'),'b.value')
			
		END
	end
	set @filterDinamic = replace (@filterDinamic,'inputLPS','a.Currency')
	set @filterDinamic = replace (@filterDinamic,'[Simulasi]','a')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end

    -- Insert statements for procedure here
	declare @sql varchar (max) 
	set @sql = 'declare @IDR decimal (32,2)=0,@USD decimal (32,2)=0,@Total decimal (32,2)=0
				select @IDR = sum(IDR),
						@USD = sum(USD),
						@Total =sum(total)
						from (	
								SELECT case when a.Currency = ''IDR'' then a.nominal else 0 end * isnull(rate.[KURS_TENGAH],1) IDR,
								case when a.Currency = ''USD'' then a.nominal else 0 end * isnull(rate.[KURS_TENGAH],1)  USD,
								 a.nominal * isnull(rate.[KURS_TENGAH],1) total
								  from ##Simulasi a
								  -- from Simulasi_replika a
								   inner join [mrms].[lcr_Rating_View] b
									on a.peringkat =b.code
									left join [mis].[BSM_EXCHG_RATE] rate
									on a.Currency =rate.CCY
								where a.[jatuhtempo] between DATEADD(day,1,  CAST('''+ @reportDate +''' as date)) and  DATEADD(day,30,  CAST('''+ @reportDate +''' as date))
							  		{parent}
									{children}
							  )a  '

	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )
	set @sql = replace(@sql,'BSM_EXCHG_RATE','BSM_EXCHG_RATE_'+@reportDate)
	set @sql = replace(@sql,'##Simulasi','##Simulasi'+@randomString)
	set @sql = @sql +' select isnull(@IDR,0) union all select isnull(@USD,0) union all select isnull(@Total,0) '
	exec (@sql)
END
/*
declare @reportDate varchar (10) = '20230618'
declare @filterDinamic varchar (max) = 'inputLPS = ''IDR'''
exec [mrms].[lcr_Equation_Simulasi] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_TCPBI_Daily]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO












-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 25 May 2023
-- Description:	Persamaan counter party UMKM
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_TCPBI_Daily]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	declare @sql varchar (max) 
	set @filterDinamic = replace (@filterDinamic,'[DS_LD_SCHEDULE_GAB]','a');
	set @filterDinamic = replace (@filterDinamic,'[DS_REAL_PROJ_SCHEDULE_N]','b')
	set @filterDinamic = replace (@filterDinamic,'[EDW.LOAN]','c');
	set @filterDinamic = replace (@filterDinamic,'[EDW.DS_CUSTOMER_MASTER]','d')
	set @filterDinamic = replace (@filterDinamic,'[EDW.CIF_DPK_DIVISI]','e')
	set @filterDinamic = replace (@filterDinamic,'inputLPS','c.CCY')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end


	set @sql = 'declare @IDR decimal (32,2),@USD decimal (32,2),@Total decimal (32,2)

				select @IDR = sum(case when CCY = ''IDR'' then Jumlah else 0 end), 
						@USD =sum(case when CCY = ''USD'' then Jumlah else 0 end) ,
						@Total =sum(Jumlah)
				from (
					select sum(ANGS_POKOK + (case when isnull(ANGS_MARGIN,0) =0 then isnull(b.PROYEKSI,0) else isnull(ANGS_MARGIN,0)  end)) Jumlah
							,c.ccy 
					from [mis].[DS_LD_SCHEDULE_GAB] a with (nolock)
					left join [mis].[DS_REAL_PROJ_SCHEDULE_N] b with (nolock)
					on a.LOAN_NO = b.ID
					inner join [mis].[EDW.LOAN] c with (nolock)
					on a.LOAN_NO =c.loan_NO
					inner join [mis].[EDW.DS_CUSTOMER_MASTER] d with (nolock)
					on d.CUSTOMER_CODE = c.CIF_NO
					where a.SCHD_DT between DATEADD(day,1,  CAST('''+ @reportDate +''' as date)) and  DATEADD(day,30,  CAST('''+ @reportDate +''' as date)) and (d.CUST_TYPE = ''C'' and c.KOLEKT_CIF =1 
					and upper(d.name_1)  like ''%BANK INDONESIA%''
					{parent}  )
					{children}
					group by c.ccy 
				)a  '

	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )

	set @sql = replace(@sql,'DS_LD_SCHEDULE_GAB','DS_LD_SCHEDULE_GAB_'+@reportDate)
	set @sql = replace(@sql,'DS_REAL_PROJ_SCHEDULE_N','DS_REAL_PROJ_SCHEDULE_N_'+@reportDate)
	set @sql = replace(@sql,'EDW.LOAN','EDW.LOAN_'+@reportDate)
	set @sql = replace(@sql,'EDW.DS_CUSTOMER_MASTER','EDW.DS_CUSTOMER_MASTER_'+@reportDate)
	set @sql = replace(@sql,'EDW.CIF_DPK_DIVISI','EDW.CIF_DPK_DIVISI_'+@reportDate)

	set @sql = @sql +' select isnull(@IDR,0) union all select isnull(@USD,0) union all select isnull(@Total,0) '
	exec (@sql)

END
/*
declare @reportDate varchar (10) = '20230618'
declare @filterDinamic varchar (max) =
			'( [DS_LD_SCHEDULE_GAB].[ANGS_KE] = 77
				and [EDW.LOAN].[POKOK_AWAL] < 2000000000 
				OR [EDW.LOAN].[POKOK_AWAL] * 4 < case when inputLPS = ''IDR'' then 5 else 6 end )'
exec [mrms].[lcr_Equation_TCPBI_Daily] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_TCPBI_EOM]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO















-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 25 May 2023
-- Description:	Persamaan counter party bank indonesia
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_TCPBI_EOM]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	declare @sql varchar (max) 
	set @filterDinamic = replace (@filterDinamic,'[masterPembiayaan_BSI]','a');
	set @filterDinamic = replace (@filterDinamic,'[EDW.DS_CUSTOMER_MASTER]','b')
	set @filterDinamic = replace (@filterDinamic,'inputLPS','a.jenisValuta')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end

	set @sql = '

			declare @IDR decimal (32,2)=0,@USD decimal (32,2)=0,@Total decimal (32,2)=0
				select @IDR =  sum(case when CCY = ''IDR'' then Jumlah else 0 end), 
						@USD = sum(case when CCY = ''USD'' then Jumlah else 0 end)  ,
						@Total =sum(Jumlah)
				from (
						select sum(case when a.[periodeAngsuranPokokBerikutnya] between DATEADD(day,1,  CAST('''+ @reportDate +''' as date)) and  DATEADD(day,30,  CAST('''+ @reportDate +''' as date))
								then isnull(a.angsuranPokokBerikutnya,0)
								else 0 end + 
								case when a.[periodeAngsuranMarginBerikutnya] between DATEADD(day,1,  CAST('''+ @reportDate +''' as date)) and  DATEADD(day,30,  CAST('''+ @reportDate +''' as date))
								then isnull(angsuranMarginBerikutnya,0) 
								else 0 end + 
								case when a.[periodeAngsuranMarginBerikutnya] between DATEADD(day,1,  CAST('''+ @reportDate +''' as date)) and  DATEADD(day,30,  CAST('''+ @reportDate +''' as date))
								then isnull(proyeksiBagiHasil,0)
								else 0 end 
								) Jumlah
							,a.jenisValuta  ccy
					from [ddm].[masterPembiayaan_BSI] a
					inner join [mis].[EDW.DS_CUSTOMER_MASTER] b
					on a.noCif =b.CUSTOMER_CODE
					where ((b.CUST_TYPE = ''C'' and a.kolektibilitas =1 
					and upper(b.name_1)  like ''%BANK INDONESIA%'' )
					{parent} )
					{children}
					group by a.jenisValuta 
				)a  '

	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )

	set @sql = replace(@sql,'masterPembiayaan_BSI','masterPembiayaan_BSI_'+@reportDate)
	set @sql = replace(@sql,'EDW.DS_CUSTOMER_MASTER','EDW.DS_CUSTOMER_MASTER_'+@reportDate)

	set @sql = @sql +' select isnull(@IDR,0) union all select isnull(@USD,0) union all select isnull(@Total,0) '
	print (@sql)
	exec (@sql)

END
/*
declare @reportDate varchar (10) = '20230618'
declare @filterDinamic varchar (max) =
			'( [masterPembiayaan_BSI].[pencairanPokok] <= 500000000
				OR [masterPembiayaan_BSI].[jenisPenggunaan] * 4 < case when inputLPS = ''IDR'' then 5 else 6 end )'
exec  [mrms].[lcr_Equation_TCPBI_EOM] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_TCPK_Daily]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO












-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 25 May 2023
-- Description:	Persamaan counter party lembaga keuangan 
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_TCPK_Daily]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	declare @sql varchar (max) 
	set @filterDinamic = replace (@filterDinamic,'[DS_LD_SCHEDULE_GAB]','a');
	set @filterDinamic = replace (@filterDinamic,'[DS_REAL_PROJ_SCHEDULE_N]','b')
	set @filterDinamic = replace (@filterDinamic,'[EDW.LOAN]','c');
	set @filterDinamic = replace (@filterDinamic,'[EDW.DS_CUSTOMER_MASTER]','d')
	set @filterDinamic = replace (@filterDinamic,'[EDW.CIF_DPK_DIVISI]','e')
	set @filterDinamic = replace (@filterDinamic,'inputLPS','c.CCY')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end


	set @sql = 'declare @IDR decimal (32,2),@USD decimal (32,2),@Total decimal (32,2)

				select @IDR = sum(case when CCY = ''IDR'' then Jumlah else 0 end), 
						@USD =sum(case when CCY = ''USD'' then Jumlah else 0 end) ,
						@Total = sum(Jumlah)
				from (
					select sum(ANGS_POKOK + (case when isnull(ANGS_MARGIN,0) =0 then isnull(b.PROYEKSI,0) else isnull(ANGS_MARGIN,0)  end)) Jumlah
							,c.ccy 
					from [mis].[DS_LD_SCHEDULE_GAB] a with (nolock)
					left join [mis].[DS_REAL_PROJ_SCHEDULE_N] b with (nolock)
					on a.LOAN_NO = b.ID
					inner join [mis].[EDW.LOAN] c with (nolock)
					on a.LOAN_NO =c.loan_NO
					inner join [mis].[EDW.DS_CUSTOMER_MASTER] d with (nolock)
					on d.CUSTOMER_CODE = c.CIF_NO
					where (d.CUST_TYPE = ''C'' and c.KOLEKT_CIF =1  and c.CIF_NO <> ''81758474''
					and a.SCHD_DT between DATEADD(day,1,  CAST('''+ @reportDate +''' as date)) and  DATEADD(day,30,  CAST('''+ @reportDate +''' as date)) and
					(LBU_CUST_TYPE in(4516,8139,8449,870,871,872,7273, 7379, 8480, 8670, 885,889, 8900,893, 894, 895, 9100, 9300, 9400, 9501, 9502, 9519, 9611, 9612, 9613 ) or
					LBU_CUST_TYPE between 8111 and 8118 or 
					LBU_CUST_TYPE between 8411 and 8418 or
					LBU_CUST_TYPE between 8611 and 8619 or
					BUSINESS_TYPE in(101, 103, 104, 105, 9629,9690, 896, 897, 899) or
					LBU_CUST_TYPE in(0010, 0020, 0030, 0040, 0050, 0060, 0070, 0080, 0999, 2020, 4111, 801, 802, 803, 804, 805, 806, 807, 808, 809, 822, 891) or
					LBU_CUST_TYPE in(892, 104, 9200, 8670) or
					LBU_CUST_TYPE in(069, 793 ) or
					LBU_CUST_TYPE in(1010, 1020, 1030, 2010, 2090, 4112, 4113, 4114, 4115, 4116, 4119, 4120, 4130, 4140, 4151, 4152, 4153, 4154, 4155, 
									4159, 4171, 4172, 4179, 4501, 4502, 4503, 4504, 4505, 4506, 4507, 4508, 4509, 4510, 4511, 4513, 4514, 4515, 4599, 
									5110, 5120, 5130, 5140, 5151, 5152, 5153, 5154, 5159, 5199, 5501, 5502, 5599, 810, 816, 821, 827, 828, 829, 830, 
									831, 832, 833, 834, 835, 836, 837, 838, 839, 840, 842, 849) or
					LBU_CUST_TYPE between 9611 and 9613 ) 
					{parent} )
					{children}
					group by c.ccy 
				)a  '

	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )

	set @sql = replace(@sql,'DS_LD_SCHEDULE_GAB','DS_LD_SCHEDULE_GAB_'+@reportDate)
	set @sql = replace(@sql,'DS_REAL_PROJ_SCHEDULE_N','DS_REAL_PROJ_SCHEDULE_N_'+@reportDate)
	set @sql = replace(@sql,'EDW.LOAN','EDW.LOAN_'+@reportDate)
	set @sql = replace(@sql,'EDW.DS_CUSTOMER_MASTER','EDW.DS_CUSTOMER_MASTER_'+@reportDate)
	set @sql = replace(@sql,'EDW.CIF_DPK_DIVISI','EDW.CIF_DPK_DIVISI_'+@reportDate)

	set @sql = @sql +' select isnull(@IDR,0) union all select isnull(@USD,0) union all select isnull(@Total,0) '
	exec (@sql)

END
/*
declare @reportDate varchar (10) = '20230618'
declare @filterDinamic varchar (max) =
			'( [DS_LD_SCHEDULE_GAB].[ANGS_KE] = 77
				and [EDW.LOAN].[POKOK_AWAL] < 2000000000 
				OR [EDW.LOAN].[POKOK_AWAL] * 4 < case when inputLPS = ''IDR'' then 5 else 6 end )'
exec [mrms].[lcr_Equation_TCPK_Daily] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_TCPK_EOM]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO














-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 25 May 2023
-- Description:	Persamaan counter party keuangan
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_TCPK_EOM]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	declare @sql varchar (max) 
	set @filterDinamic = replace (@filterDinamic,'[masterPembiayaan_BSI]','a');
	set @filterDinamic = replace (@filterDinamic,'[EDW.DS_CUSTOMER_MASTER]','b')
	set @filterDinamic = replace (@filterDinamic,'inputLPS','a.jenisValuta')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end


	set @sql = 'declare @IDR decimal (32,2)=0,@USD decimal (32,2)=0,@Total decimal (32,2)=0
				select @IDR =sum(case when CCY = ''IDR'' then Jumlah else 0 end), 
						@USD = sum(case when CCY = ''USD'' then Jumlah else 0 end)  ,
						@Total =sum(Jumlah)
				from (
					select sum(case when a.[periodeAngsuranPokokBerikutnya] between DATEADD(day,1,  CAST('''+ @reportDate +''' as date)) and  DATEADD(day,30,  CAST('''+ @reportDate +''' as date))
								then isnull(a.angsuranPokokBerikutnya,0)
								else 0 end + 
								case when a.[periodeAngsuranMarginBerikutnya] between DATEADD(day,1,  CAST('''+ @reportDate +''' as date)) and  DATEADD(day,30,  CAST('''+ @reportDate +''' as date))
								then isnull(angsuranMarginBerikutnya,0) 
								else 0 end + 
								case when a.[periodeAngsuranMarginBerikutnya] between DATEADD(day,1,  CAST('''+ @reportDate +''' as date)) and  DATEADD(day,30,  CAST('''+ @reportDate +''' as date))
								then isnull(proyeksiBagiHasil,0) 
								else 0 end) Jumlah
							,a.jenisValuta  ccy
					from [ddm].[masterPembiayaan_BSI] a
					inner join [mis].[EDW.DS_CUSTOMER_MASTER] b
					on a.noCif =b.CUSTOMER_CODE
					where (b.CUST_TYPE = ''C'' and a.kolektibilitas =1 {parent} ) and
					(LBU_CUST_TYPE in(4516,8139,8449,870,871,872,7273, 7379, 8480, 8670, 885,889, 8900,893, 894, 895, 9100, 9300, 9400, 9501, 9502, 9519, 9611, 9612, 9613 ) or
					LBU_CUST_TYPE between 8111 and 8118 or 
					LBU_CUST_TYPE between 8411 and 8418 or
					LBU_CUST_TYPE between 8611 and 8619 or
					BUSINESS_TYPE in(101, 103, 104, 105, 9629,9690, 896, 897, 899) or
					LBU_CUST_TYPE in(0010, 0020, 0030, 0040, 0050, 0060, 0070, 0080, 0999, 2020, 4111, 801, 802, 803, 804, 805, 806, 807, 808, 809, 822, 891) or
					LBU_CUST_TYPE in(892, 104, 9200, 8670) or
					LBU_CUST_TYPE in(069, 793 ) or
					LBU_CUST_TYPE in(1010, 1020, 1030, 2010, 2090, 4112, 4113, 4114, 4115, 4116, 4119, 4120, 4130, 4140, 4151, 4152, 4153, 4154, 4155, 
									4159, 4171, 4172, 4179, 4501, 4502, 4503, 4504, 4505, 4506, 4507, 4508, 4509, 4510, 4511, 4513, 4514, 4515, 4599, 
									5110, 5120, 5130, 5140, 5151, 5152, 5153, 5154, 5159, 5199, 5501, 5502, 5599, 810, 816, 821, 827, 828, 829, 830, 
									831, 832, 833, 834, 835, 836, 837, 838, 839, 840, 842, 849) or
					LBU_CUST_TYPE between 9611 and 9613 ) 
					{children}
					group by a.jenisValuta 
				)a  '

	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )

	set @sql = replace(@sql,'masterPembiayaan_BSI','masterPembiayaan_BSI_'+@reportDate)
	set @sql = replace(@sql,'EDW.DS_CUSTOMER_MASTER','EDW.DS_CUSTOMER_MASTER_'+@reportDate)

	set @sql = @sql +' select isnull(@IDR,0) union all select isnull(@USD,0) union all select isnull(@Total,0) '
	exec (@sql)
	print (@sql)

END
/*
declare @reportDate varchar (10) = '20230619'
declare @filterDinamic varchar (max) =
			'( [masterPembiayaan_BSI].[namaNasabah] = ''EVELYN LUCIANA SOEYAPTO'' 
				OR [masterPembiayaan_BSI].[jenisPenggunaan] * 4 < case when inputLPS = ''IDR'' then 5 else 6 end )'
exec [mrms].[lcr_Equation_TCPP_EOM] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_TCPLK_Daily]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO












-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 25 May 2023
-- Description:	Persamaan counter party lembaga keuangan
-- Modified: Febrian Alfandi 04 Jul 2023 ; Reverse Rating Value and maturityDate replace <= with <
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_TCPLK_Daily]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max),
	 @randomString varchar(10) = ''
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	declare @sql varchar (max) 
	set @filterDinamic = replace (@filterDinamic,'[DS_LD_SCHEDULE_GAB]','a');
	set @filterDinamic = replace (@filterDinamic,'[DS_REAL_PROJ_SCHEDULE_N]','b')
	set @filterDinamic = replace (@filterDinamic,'[EDW.LOAN]','c');
	set @filterDinamic = replace (@filterDinamic,'[EDW.DS_CUSTOMER_MASTER]','d')	
	set @filterDinamic = replace (@filterDinamic,'[EDW.CIF_DPK_DIVISI]','e')
	set @filterDinamic = replace (@filterDinamic,'inputLPS','c.CCY')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end


	set @sql = '
				
			declare @IDR decimal (32,2)=0,@USD decimal (32,2)=0,@Total decimal (32,2)=0
			select @IDR = sum(IDR),
					@USD = sum(USD),
					@total = sum(total)
					from (
						select  IDR = case when a.Currency = ''IDR''  then 
											case when [type] in(''AFS'',''Trading'') then FAIRVALUE  else BOOKVALUE end
										else 0 end * isnull(rate.[KURS_TENGAH],1),
								USD =  case when a.Currency = ''USD''  then 
											case when [type] in(''AFS'',''Trading'') then FAIRVALUE  else BOOKVALUE end
										else 0 end * isnull(rate.[KURS_TENGAH],1),
								Total = case when [type] in(''AFS'',''Trading'') then FAIRVALUE  else BOOKVALUE end  * isnull(rate.[KURS_TENGAH],1)
					from ##DataHarianSuratBerharga a
					--from [dbo].[DataHarianSuratBerharga_Replika] a
					 inner join [mrms].[lcr_Rating_View] b
							  on a.ratingR =b.code
					left join [mis].[BSM_EXCHG_RATE] rate
					on a.Currency =rate.CCY
						 where ( (LembagaKeuangan = ''Lembaga Keuangan'' and DESCR <> ''EBA'')
								or LembagaKeuangan = ''Lembaga Keuangan'' and DESCR = ''EBA'' and b.value < ''Zp''
						 )
						 and (( a.[MATURITYDATE] between DATEADD(day,1,  CAST('''+ @reportDate +''' as date)) 
								and DATEADD(day,30,  CAST('''+ @reportDate +''' as date))) or a.[MATURITYDATE] is null)
					)a

				select @IDR =  @IDR  + isnull(sum(case when CCY = ''IDR'' then Jumlah else 0 end),0), 
						@USD = @USD + isnull(sum(case when CCY = ''USD'' then Jumlah else 0 end) ,0) ,
						@Total = @Total + isnull(sum(jumlah),0)
				from (
					select sum(ANGS_POKOK + (case when isnull(ANGS_MARGIN,0) =0 then isnull(b.PROYEKSI,0) else isnull(ANGS_MARGIN,0)  end)) Jumlah
							,c.ccy 
					from [mis].[DS_LD_SCHEDULE_GAB] a with (nolock)
					left join [mis].[DS_REAL_PROJ_SCHEDULE_N] b with (nolock)
					on a.LOAN_NO = b.ID
					inner join [mis].[EDW.LOAN] c with (nolock)
					on a.LOAN_NO =c.loan_NO
					inner join [mis].[EDW.DS_CUSTOMER_MASTER] d with (nolock)
					on d.CUSTOMER_CODE = c.CIF_NO
					where (d.CUST_TYPE = ''C'' and c.KOLEKT_CIF =1 
					and a.SCHD_DT between DATEADD(day,1,  CAST('''+ @reportDate +''' as date)) and  DATEADD(day,30,  CAST('''+ @reportDate +''' as date)) and
					(upper(d.name_1)  like ''%VENTURA%''
					or upper(d.name_1)  like ''%KJKS%''
					or upper(d.name_1) like ''%KSP%'' 
					or upper(d.name_1) like ''%BMT%'' 
					or upper(d.name_1) like ''%GADAI%'' 
					or upper(d.name_1) like ''%DANA PENSIUN%''
					or upper(d.name_1) like ''%DAPEN%'' 
					or upper(d.name_1) like ''%ASURANSI%'' 
					or upper(d.name_1) like ''%REKSADANA%''
					or upper(d.name_1) like ''%FINANCE%'' 
					or upper(d.name_1) like ''%SEKURITAS%''
					or upper(d.name_1) like ''%BANK%''
					or upper(d.name_1) like ''%BPR%''
					or upper(d.name_1) like ''%BPD%''
					)
					{parent}  )
					{children}
					group by c.ccy 
				)a  '

	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )

	set @sql = replace(@sql,'DS_LD_SCHEDULE_GAB','DS_LD_SCHEDULE_GAB_'+@reportDate)
	set @sql = replace(@sql,'DS_REAL_PROJ_SCHEDULE_N','DS_REAL_PROJ_SCHEDULE_N_'+@reportDate)
	set @sql = replace(@sql,'EDW.LOAN','EDW.LOAN_'+@reportDate)
	set @sql = replace(@sql,'EDW.DS_CUSTOMER_MASTER','EDW.DS_CUSTOMER_MASTER_'+@reportDate)
	set @sql = replace(@sql,'EDW.CIF_DPK_DIVISI','EDW.CIF_DPK_DIVISI_'+@reportDate)
	set @sql = replace(@sql,'BSM_EXCHG_RATE','BSM_EXCHG_RATE_'+@reportDate)
	set @sql = replace(@sql,'##DataHarianSuratBerharga','##DataHarianSuratBerharga'+@randomString)
	set @sql = @sql +' select isnull(@IDR,0) union all select isnull(@USD,0) union all select isnull(@Total,0) '
	exec (@sql)


END
/*
declare @reportDate varchar (10) = '20230619'
declare @filterDinamic varchar (max) =
			'([EDW.CIF_DPK_DIVISI].[DIVISI_CD] in (''CB1G'',''CB2G'',''CB3G'',''CMG'') )'
exec [mrms].[lcr_Equation_TCPLK_Daily] @reportDate,@filterDinamic, 'Random'
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_TCPLK_EOM]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO














-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 25 May 2023
-- Description:	Persamaan counter party lembaga keuangan
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_TCPLK_EOM]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max),
	 @randomString varchar(10) = ''
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	declare @sql varchar (max) 
	set @filterDinamic = replace (@filterDinamic,'[masterPembiayaan_BSI]','a');
	set @filterDinamic = replace (@filterDinamic,'[EDW.DS_CUSTOMER_MASTER]','b')
	set @filterDinamic = replace (@filterDinamic,'inputLPS','a.jenisValuta')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end


	set @sql = '
				
			declare @IDR decimal (32,2)=0,@USD decimal (32,2)=0,@Total decimal (32,2)=0
			select @IDR = sum(IDR),
					@USD = sum(USD),
					@total = sum(total)
					from (
						select  IDR = case when a.Currency = ''IDR''  then 
											case when [type] in(''AFS'',''Trading'') then FAIRVALUE  else BOOKVALUE end
										else 0 end * isnull(rate.[KURS_TENGAH],1),
								USD =  case when a.Currency = ''USD''  then 
											case when [type] in(''AFS'',''Trading'') then FAIRVALUE  else BOOKVALUE end
										else 0 end * isnull(rate.[KURS_TENGAH],1),
								Total = case when [type] in(''AFS'',''Trading'') then FAIRVALUE  else BOOKVALUE end  * isnull(rate.[KURS_TENGAH],1)
					from ##DataHarianSuratBerharga a
					--from [dbo].[DataHarianSuratBerharga] a
					 inner join [mrms].[lcr_Rating_View] b
							  on a.ratingR =b.code
					left join [mis].[BSM_EXCHG_RATE] rate
					on a.Currency =rate.CCY
						 where ((LembagaKeuangan = ''Lembaga Keuangan'' and DESCR <> ''EBA'')
						 or (LembagaKeuangan = ''Lembaga Keuangan'' and DESCR = ''EBA'' and b.value < ''Zp''))

						 and (( a.[MATURITYDATE] between DATEADD(day,1,  CAST('''+ @reportDate +''' as date)) 
								and DATEADD(day,30,  CAST('''+ @reportDate +''' as date))) or a.[MATURITYDATE] is null)
					)a



				select @IDR =  @IDR  + isnull(sum(case when CCY = ''IDR'' then Jumlah else 0 end),0), 
						@USD = @USD + isnull(sum(case when CCY = ''USD'' then Jumlah else 0 end) ,0) ,
						@Total = @Total + isnull(sum(jumlah),0)
				from (
					select sum(case when a.[periodeAngsuranPokokBerikutnya] between DATEADD(day,-30,  CAST('''+ @reportDate +''' as date)) and CAST('''+ @reportDate +''' as date) 
								then isnull(a.angsuranPokokBerikutnya,0)
								else 0 end + 
								case when a.[periodeAngsuranMarginBerikutnya] between DATEADD(day,-30,  CAST('''+ @reportDate +''' as date)) and CAST('''+ @reportDate +''' as date) 
								then isnull(angsuranMarginBerikutnya,0) 
								else 0 end + 
									case when a.[periodeAngsuranMarginBerikutnya] between DATEADD(day,1,  CAST('''+ @reportDate +''' as date)) and  DATEADD(day,30,  CAST('''+ @reportDate +''' as date))
								then isnull(proyeksiBagiHasil,0) 
								else 0 end) Jumlah
							,a.jenisValuta  ccy
					from [ddm].[masterPembiayaan_BSI] a
					inner join [mis].[EDW.DS_CUSTOMER_MASTER] b
					on a.noCif =b.CUSTOMER_CODE
					where (b.CUST_TYPE = ''C'' and a.kolektibilitas =1 
					and 
					(upper(b.name_1)  like ''%BPRS%''
					or upper(b.name_1)  like ''%BANK%''
					or upper(b.name_1) like ''%BPR%'' 
					or upper(b.name_1) like ''%VENTURA%'' 
					or upper(b.name_1) like ''%KJKS%'' 
					or upper(b.name_1) like ''%KSP%''
					or upper(b.name_1) like ''%BMT%'' 
					or upper(b.name_1) like ''%GADAI%'' 
					or upper(b.name_1) like ''%DANA PENSIUN%''
					or upper(b.name_1) like ''%DAPEN%'' 
					or upper(b.name_1) like ''%ASURANSI%''
					or upper(b.name_1) like ''%REKSADANA%''
					or upper(b.name_1) like ''%FINANCE%''
					or upper(b.name_1) like ''%SEKURITAS%'' )
					{parent} )
					{children}
					group by a.jenisValuta 
				)a  '

	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )

	set @sql = replace(@sql,'masterPembiayaan_BSI','masterPembiayaan_BSI_'+@reportDate)
	set @sql = replace(@sql,'EDW.DS_CUSTOMER_MASTER','EDW.DS_CUSTOMER_MASTER_'+@reportDate)
	set @sql = replace(@sql,'BSM_EXCHG_RATE','BSM_EXCHG_RATE_'+@reportDate)
	set @sql = replace(@sql,'##DataHarianSuratBerharga','##DataHarianSuratBerharga'+@randomString)
	set @sql = @sql +' select isnull(@IDR,0) union all select isnull(@USD,0) union all select isnull(@Total,0)'
	print (@sql)
	exec (@sql)

END
/*
declare @reportDate varchar (10) = '20230619'
declare @filterDinamic varchar (max) =
			'( [masterPembiayaan_BSI].[pencairanPokok] <= 500000000
				OR [masterPembiayaan_BSI].[jenisPenggunaan] * 4 < case when inputLPS = ''IDR'' then 5 else 6 end )'
exec  [mrms].[lcr_Equation_TCPLK_EOM] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_TCPP_Daily]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO












-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 25 May 2023
-- Description:	Persamaan counter party nasabah perorangan 
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_TCPP_Daily]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	declare @sql varchar (max) 
	set @filterDinamic = replace (@filterDinamic,'[DS_LD_SCHEDULE_GAB]','a');
	set @filterDinamic = replace (@filterDinamic,'[DS_REAL_PROJ_SCHEDULE_N]','b')
	set @filterDinamic = replace (@filterDinamic,'[EDW.LOAN]','c');
	set @filterDinamic = replace (@filterDinamic,'[EDW.DS_CUSTOMER_MASTER]','d')
	set @filterDinamic = replace (@filterDinamic,'inputLPS','c.CCY')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end


	set @sql = 'declare @IDR decimal (32,2),@USD decimal (32,2),@Total decimal (32,2)

				select @IDR = sum(case when CCY = ''IDR'' then Jumlah else 0 end), 
						@USD =sum(case when CCY = ''USD'' then Jumlah else 0 end)  ,
						@Total = sum(Jumlah)
				from (
					select sum(ANGS_POKOK + (case when isnull(ANGS_MARGIN,0) =0 then isnull(b.PROYEKSI,0) else isnull(ANGS_MARGIN,0)  end)) Jumlah
							,c.ccy 
					from [mis].[DS_LD_SCHEDULE_GAB] a with (nolock)
					left join [mis].[DS_REAL_PROJ_SCHEDULE_N] b with (nolock)
					on a.LOAN_NO = b.ID
					inner join [mis].[EDW.LOAN] c with (nolock)
					on a.LOAN_NO =c.loan_NO
					inner join [mis].[EDW.DS_CUSTOMER_MASTER] d with (nolock)
					on d.CUSTOMER_CODE = c.CIF_NO 
					where a.SCHD_DT between DATEADD(day,1,  CAST('''+ @reportDate +''' as date)) and  DATEADD(day,30,  CAST('''+ @reportDate +''' as date)) and (d.CUST_TYPE = ''R'' and c.KOLEKT_CIF =1  {parent} )
					{children}
					group by c.ccy 
				)a  '

	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )

	set @sql = replace(@sql,'DS_LD_SCHEDULE_GAB','DS_LD_SCHEDULE_GAB_'+@reportDate)
	set @sql = replace(@sql,'DS_REAL_PROJ_SCHEDULE_N','DS_REAL_PROJ_SCHEDULE_N_'+@reportDate)
	set @sql = replace(@sql,'EDW.LOAN','EDW.LOAN_'+@reportDate)
	set @sql = replace(@sql,'EDW.DS_CUSTOMER_MASTER','EDW.DS_CUSTOMER_MASTER_'+@reportDate)

	set @sql = @sql +' select isnull(@IDR,0) union all select isnull(@USD,0) union all select isnull(@Total,0) '
	exec (@sql)

END
/*
declare @reportDate varchar (10) = '20230618'
declare @filterDinamic varchar (max) =
			'( [DS_LD_SCHEDULE_GAB].[ANGS_KE] = 77
				and [EDW.LOAN].[POKOK_AWAL] < 2000000000 
				OR [EDW.LOAN].[POKOK_AWAL] * 4 < case when inputLPS = ''IDR'' then 5 else 6 end )'
exec [mrms].[lcr_Equation_TCPP_Daily] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_TCPP_EOM]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 25 May 2023
-- Description:	Persamaan counter party nasabah perorangan 
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_TCPP_EOM]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	declare @sql varchar (max) 
	set @filterDinamic = replace (@filterDinamic,'[masterPembiayaan_BSI]','a');
	set @filterDinamic = replace (@filterDinamic,'[EDW.DS_CUSTOMER_MASTER]','b')
	set @filterDinamic = replace (@filterDinamic,'inputLPS','a.jenisValuta')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end


	set @sql = 'declare @IDR decimal (32,2),@USD decimal (32,2),@Total decimal (32,2)

				select @IDR = sum(case when CCY = ''IDR'' then Jumlah else 0 end), 
						@USD =sum(case when CCY = ''USD'' then Jumlah else 0 end)  ,
						@Total =sum(Jumlah)
				from (
					select sum(case when a.[periodeAngsuranPokokBerikutnya] between DATEADD(day,1,  CAST('''+ @reportDate +''' as date)) and  DATEADD(day,30,  CAST('''+ @reportDate +''' as date))
								then isnull(a.angsuranPokokBerikutnya,0)
								else 0 end + 
								case when a.[periodeAngsuranMarginBerikutnya] between DATEADD(day,1,  CAST('''+ @reportDate +''' as date)) and  DATEADD(day,30,  CAST('''+ @reportDate +''' as date))
								then isnull(angsuranMarginBerikutnya,0) 
								else 0 end + 
								case when a.[periodeAngsuranMarginBerikutnya] between DATEADD(day,1,  CAST('''+ @reportDate +''' as date)) and  DATEADD(day,30,  CAST('''+ @reportDate +''' as date))
								then isnull(proyeksiBagiHasil,0) 
								else 0 end ) Jumlah
							,a.jenisValuta  ccy
					from [ddm].[masterPembiayaan_BSI] a
					inner join [mis].[EDW.DS_CUSTOMER_MASTER] b
					on a.noCif =b.CUSTOMER_CODE
					where (b.CUST_TYPE = ''R'' and a.kolektibilitas =1 {parent} )
					{children}
					group by a.jenisValuta 
				)a  '

	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )

	set @sql = replace(@sql,'masterPembiayaan_BSI','masterPembiayaan_BSI_'+@reportDate)
	set @sql = replace(@sql,'EDW.DS_CUSTOMER_MASTER','EDW.DS_CUSTOMER_MASTER_'+@reportDate)

	set @sql = @sql +' select isnull(@IDR,0) union all select isnull(@USD,0) union all select isnull(@Total,0) '
	print (@sql)
	exec (@sql)

END
/*
declare @reportDate varchar (10) = '20230619'
declare @filterDinamic varchar (max) =
			'( [masterPembiayaan_BSI].[namaNasabah] = ''EVELYN LUCIANA SOEYAPTO'' 
				OR [masterPembiayaan_BSI].[jenisPenggunaan] * 4 < case when inputLPS = ''IDR'' then 5 else 6 end )'
exec [mrms].[lcr_Equation_TCPP_EOM] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_TCPPUMKM_Daily]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO












-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 25 May 2023
-- Description:	Persamaan counter party UMKM
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_TCPPUMKM_Daily]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	declare @sql varchar (max) 
	set @filterDinamic = replace (@filterDinamic,'[DS_LD_SCHEDULE_GAB]','a');
	set @filterDinamic = replace (@filterDinamic,'[DS_REAL_PROJ_SCHEDULE_N]','b')
	set @filterDinamic = replace (@filterDinamic,'[EDW.LOAN]','c');
	set @filterDinamic = replace (@filterDinamic,'[EDW.DS_CUSTOMER_MASTER]','d')
	set @filterDinamic = replace (@filterDinamic,'[EDW.CIF_DPK_DIVISI]','e')
	set @filterDinamic = replace (@filterDinamic,'inputLPS','c.CCY')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end


	set @sql = 'declare @IDR decimal (32,2),@USD decimal (32,2),@Total decimal (32,2)
				select @IDR = sum(case when CCY = ''IDR'' then Jumlah else 0 end), 
						@USD =sum(case when CCY = ''USD'' then Jumlah else 0 end)  ,
						@Total = sum(Jumlah)
				from (
					select sum(ANGS_POKOK + (case when isnull(ANGS_MARGIN,0) =0 then isnull(b.PROYEKSI,0) else isnull(ANGS_MARGIN,0)  end)) Jumlah
							,c.ccy 
					from [mis].[DS_LD_SCHEDULE_GAB] a with (nolock)
					left join [mis].[DS_REAL_PROJ_SCHEDULE_N] b with (nolock)
					on a.LOAN_NO = b.ID
					inner join [mis].[EDW.LOAN] c with (nolock)
					on a.LOAN_NO =c.loan_NO
					inner join [mis].[EDW.DS_CUSTOMER_MASTER] d with (nolock)
					on d.CUSTOMER_CODE = c.CIF_NO
					where 
					a.SCHD_DT between DATEADD(day,1,  CAST('''+ @reportDate +''' as date)) and  DATEADD(day,30,  CAST('''+ @reportDate +''' as date)) and
					(d.CUST_TYPE = ''C'' and c.KOLEKT_CIF =1 
					and upper(d.name_1) not like ''%VENTURA%''
					and upper(d.name_1) not like ''%KJKS%''
					and upper(d.name_1) not like ''%KSP%'' 
					and upper(d.name_1) not like ''%BMT%'' 
					and upper(d.name_1) not like ''%GADAI%'' 
					and upper(d.name_1) not like ''%DANA PENSIUN%''
					and upper(d.name_1) not like ''%DAPEN%'' 
					and upper(d.name_1) not like ''%ASURANSI%'' 
					and upper(d.name_1) not like ''%REKSADANA%''
					and upper(d.name_1) not like ''%FINANCE%'' 
					and upper(d.name_1) not like ''%SEKURITAS%''
					and upper(d.name_1) not like ''%BANK%''
					and upper(d.name_1) not like ''%BPR%''
					{parent}  )
					{children}
					group by c.ccy 
				)a  '

	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )

	set @sql = replace(@sql,'DS_LD_SCHEDULE_GAB','DS_LD_SCHEDULE_GAB_'+@reportDate)
	set @sql = replace(@sql,'DS_REAL_PROJ_SCHEDULE_N','DS_REAL_PROJ_SCHEDULE_N_'+@reportDate)
	set @sql = replace(@sql,'EDW.LOAN','EDW.LOAN_'+@reportDate)
	set @sql = replace(@sql,'EDW.DS_CUSTOMER_MASTER','EDW.DS_CUSTOMER_MASTER_'+@reportDate)
	set @sql = replace(@sql,'EDW.CIF_DPK_DIVISI','EDW.CIF_DPK_DIVISI_'+@reportDate)

	set @sql = @sql +' select isnull(@IDR,0) union all select isnull(@USD,0) union all select isnull(@Total,0) '
	exec (@sql)

END
/*
declare @reportDate varchar (10) = '20230618'
declare @filterDinamic varchar (max) =
			'( [DS_LD_SCHEDULE_GAB].[ANGS_KE] = 77
				and [EDW.LOAN].[POKOK_AWAL] < 2000000000 
				OR [EDW.LOAN].[POKOK_AWAL] * 4 < case when inputLPS = ''IDR'' then 5 else 6 end )'
exec [mrms].[lcr_Equation_TCPPUMKM_Daily] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_TCPPUMKM_EOM]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO














-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 25 May 2023
-- Description:	Persamaan counter party UMKM
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_TCPPUMKM_EOM]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	declare @sql varchar (max) 
	set @filterDinamic = replace (@filterDinamic,'[masterPembiayaan_BSI]','a');
	set @filterDinamic = replace (@filterDinamic,'[EDW.DS_CUSTOMER_MASTER]','b')
	set @filterDinamic = replace (@filterDinamic,'inputLPS','a.jenisValuta')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end


	set @sql = 'declare @IDR decimal (32,2),@USD decimal (32,2),@Total decimal (32,2)

				select @IDR = sum(case when CCY = ''IDR'' then Jumlah else 0 end), 
						@USD =sum(case when CCY = ''USD'' then Jumlah else 0 end) ,
						@Total = sum(Jumlah)
				from (
						select sum(case when a.[periodeAngsuranPokokBerikutnya] between DATEADD(day,1,  CAST('''+ @reportDate +''' as date)) and  DATEADD(day,30,  CAST('''+ @reportDate +''' as date))
								then isnull(a.angsuranPokokBerikutnya,0)
								else 0 end + 
								case when a.[periodeAngsuranMarginBerikutnya] between DATEADD(day,1,  CAST('''+ @reportDate +''' as date)) and  DATEADD(day,30,  CAST('''+ @reportDate +''' as date))
								then isnull(angsuranMarginBerikutnya,0) 
								else 0 end + 
								case when a.[periodeAngsuranMarginBerikutnya] between DATEADD(day,1,  CAST('''+ @reportDate +''' as date)) and  DATEADD(day,30,  CAST('''+ @reportDate +''' as date))
								then isnull(proyeksiBagiHasil,0)
								else 0 end ) Jumlah
							,a.jenisValuta  ccy
					from [ddm].[masterPembiayaan_BSI] a
					inner join [mis].[EDW.DS_CUSTOMER_MASTER] b
					on a.noCif =b.CUSTOMER_CODE
					where (b.CUST_TYPE = ''C'' and a.kolektibilitas =1 {parent} )
					and upper(b.name_1) not like ''%VENTURA%''
					and upper(b.name_1) not like ''%KJKS%''
					and upper(b.name_1) not like ''%KSP%'' 
					and upper(b.name_1) not like ''%BMT%'' 
					and upper(b.name_1) not like ''%GADAI%'' 
					and upper(b.name_1) not like ''%DANA PENSIUN%''
					and upper(b.name_1) not like ''%DAPEN%'' 
					and upper(b.name_1) not like ''%ASURANSI%'' 
					and upper(b.name_1) not like ''%REKSADANA%''
					and upper(b.name_1) not like ''%FINANCE%'' 
					and upper(b.name_1) not like ''%SEKURITAS%''
					and upper(b.name_1) not like ''%BANK%''
					and upper(b.name_1) not like ''%BPR%''
					{children}
					group by a.jenisValuta 
				)a  '

	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )

	set @sql = replace(@sql,'masterPembiayaan_BSI','masterPembiayaan_BSI_'+@reportDate)
	set @sql = replace(@sql,'EDW.DS_CUSTOMER_MASTER','EDW.DS_CUSTOMER_MASTER_'+@reportDate)

	set @sql = @sql +' select isnull(@IDR,0) union all select isnull(@USD,0) union all select isnull(@Total,0) '
	print (@sql)
	exec (@sql)

END
/*
declare @reportDate varchar (10) = '20230618'
declare @filterDinamic varchar (max) =
			'( [masterPembiayaan_BSI].[pencairanPokok] <= 500000000
				OR [masterPembiayaan_BSI].[jenisPenggunaan] * 4 < case when inputLPS = ''IDR'' then 5 else 6 end )'
exec  [mrms].[lcr_Equation_TCPPUMKM_EOM]@reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_TradeFinance_Daily]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO












-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 25 May 2023
-- Description:	Persamaan trade finance
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_TradeFinance_Daily]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	set @filterDinamic = replace (@filterDinamic,'[GL_LSMKHarian_Data]','c')
	set @filterDinamic = replace (@filterDinamic,'inputLPS','c.ccy_code')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end

    -- Insert statements for procedure here
	declare @sql varchar (max) 
	set @sql = 'declare @IDR decimal (32,2),@USD decimal (32,2),@Total decimal (32,2)
				select @IDR = sum(IDR),@USD = sum(USD), @Total  = sum(total) from (
				select case when c.ccy_code =''IDR'' then c.saldo_akhir_lcy else 0 end IDR,
				case when c.ccy_code =''USD'' then c.saldo_akhir_lcy else 0 end USD,
				c.saldo_akhir_lcy Total  FROM [cfa].[GL_LSMKHarian_Data] c
				  where c.coa in(
				''1-10-1-112'', ''1-10-1-211'', ''1-10-1-212'', ''1-14-1-210'', ''1-99-9-898'', ''1-99-9-899'', ''0-05-4-110'', ''0-05-4-120'', 
				''0-05-4-210'', ''0-05-4-220'', ''0-05-4-221'', ''0-15-1-100'', ''0-15-1-101'', ''0-15-1-102'', ''0-15-1-105'', ''0-15-1-106'', 
				''0-15-1-107'', ''0-15-1-199'', ''0-99-9-977'', ''0-99-9-978'', ''0-99-9-979'', ''0-99-9-980'', ''0-99-9-981'', ''0-99-9-984'', 
				''0-99-9-985'', ''0-99-9-986'', ''0-99-9-987'', ''0-99-9-989'')
				{parent}
				{children}

				)a
				'

	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )

	set @sql = replace(@sql,'GL_LSMKHarian_Data','GL_LSMKHarian_Data_'+@reportDate)
	set @sql = @sql +' select abs(isnull(@IDR,0)) union all select abs(isnull(@USD,0)) union all select abs(isnull(@Total,0))  '
	exec (@sql)
	print (@sql)
END
/*
declare @reportDate varchar (10) = '20230619'
declare @filterDinamic varchar (max) = 'inputLPS = ''IDR'''
exec [mrms].[lcr_Equation_TradeFinance_Daily] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Equation_TradeFinance_EOM]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO












-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 25 May 2023
-- Description:	Persamaan trade finance
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Equation_TradeFinance_EOM]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	set @filterDinamic = replace (@filterDinamic,'[KompilasiCOA]','c')
	set @filterDinamic = replace (@filterDinamic,'inputLPS','c.ccy_code')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end

    -- Insert statements for procedure here
	declare @sql varchar (max) 
	set @sql = 'declare @IDR decimal (32,2),@USD decimal (18,2),@Total decimal (32,2)
				select @IDR = sum(IDR),@USD = sum(USD), @Total  = sum(total) from (
				select case when c.ccy_code =''IDR'' then c.saldo_akhir_lcy else 0 end IDR,
				case when c.ccy_code =''USD'' then c.saldo_akhir_lcy else 0 end USD,
				c.saldo_akhir_lcy Total  FROM [cfa].[KompilasiCOA] c
				  where c.coa in(
				''1-10-1-112'', ''1-10-1-211'', ''1-10-1-212'', ''1-14-1-210'', ''1-99-9-898'', ''1-99-9-899'', ''0-05-4-110'', ''0-05-4-120'', 
				''0-05-4-210'', ''0-05-4-220'', ''0-05-4-221'', ''0-15-1-100'', ''0-15-1-101'', ''0-15-1-102'', ''0-15-1-105'', ''0-15-1-106'', 
				''0-15-1-107'', ''0-15-1-199'', ''0-99-9-977'', ''0-99-9-978'', ''0-99-9-979'', ''0-99-9-980'', ''0-99-9-981'', ''0-99-9-984'', 
				''0-99-9-985'', ''0-99-9-986'', ''0-99-9-987'', ''0-99-9-989'')
				{parent}
				{children}

				)a
				'

	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )

	set @sql = replace(@sql,'KompilasiCOA','KompilasiCOA_'+@reportDate)
	set @sql = @sql +' select abs(isnull(@IDR,0)) union all select abs(isnull(@USD,0)) union all select abs(isnull(@Total,0)) '
	exec (@sql)
END
/*
declare @reportDate varchar (10) = '20230630'
declare @filterDinamic varchar (max) = 'inputLPS = ''IDR'''
exec  [mrms].[lcr_Equation_TradeFinance_EOM] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_KorporasiNonOp_Daily]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO













-- =============================================
-- Author:		Willy Anggara
-- Create date: 27 June 2023
-- Description:	Get Equation Korporasi Non Op Daily
-- Modified : Febrian Alfandi 07 Jul 2023 -->add new parameter OrderLPS
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Get_Equation_KorporasiNonOp_Daily]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) ,
	 @OrderLPS int
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here

	set @filterDinamic = replace (@filterDinamic,'SUM_POKOK_AWAL','POKOK_AWAL')
	set @filterDinamic = replace (@filterDinamic,'SUM_PENCAIRANPOKOK','PENCAIRANPOKOK')
	set @filterDinamic = replace (@filterDinamic,'SUM_END_SALDO_AKHIR','SALDO_AKHIR')
	set @filterDinamic = replace (@filterDinamic,'SUM_END_SALDO_LSMK','SALDOLSMK')
 
	set @filterDinamic = replace (@filterDinamic,'[EDW.DS_CUSTOMER_MASTER]','a');
	set @filterDinamic = replace (@filterDinamic,'[EDW.DEPOSITS]','b');
	set @filterDinamic = replace (@filterDinamic,'[EDW.CIF_DPK_DIVISI]','c');
	set @filterDinamic = replace (@filterDinamic,'[EDW.LOAN]','d');
	set @filterDinamic = replace (@filterDinamic,'inputLPS','b.CCY')


	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end

	declare @sql varchar (max) 
	
		set @sql = 'select distinct b.id, b.ACCOUNT_NO, a.NAME_1, b.CCY, b.SALDO_AKHIR, isnull(b.NISBAH_AKHIR,0) NISBAH_AKHIR, 
				CASE WHEN e.cif_no IS NULL THEN ''Tidak Ada'' ELSE ''Ada'' END CMS,
				CASE WHEN g.account_no IS NULL THEN ''Tidak ada'' ELSE ''Ada'' END REMITTANCE,
				CASE WHEN f.recid IS NULL THEN ''Tidak ada'' ELSE ''Ada'' END [CASH POOL],
				''Simpanan Non Operasional'' [Komponen 1],
				case when '''+ cast (@OrderLPS as varchar)+'''  = ''0'' then ''Dijamin LPS'' else ''Tidak Dijamin LPS'' end [Komponen 2]
				from
			[mis].[EDW.DS_CUSTOMER_MASTER] a with (nolock)
			inner join [mis].[EDW.DEPOSITS] b with (nolock)
			on a.CUSTOMER_CODE = b.CIF_NO
			inner join [mis].[EDW.CIF_DPK_DIVISI] C with (nolock)
			on b.CIF_NO =C.CIF_NO
			left join [mis].[EDW.LOAN] d with (nolock)
			on b.CIF_NO =d.CIF_NO
			left join [mis].[EDW.OP_TRX_CMS] e with (nolock)
			on e.cif_no =b.cif_no
			left join [mis].[EDW.DS_AC_CASH_POOL] f with (nolock)
			on f.recid =b.account_no
			left join [mis].[EDW.ACCOUNT_MITRA_REMITTANCE] g with (nolock)
			on g.account_no = b.account_no
			left join [mis].[TRX.STMT_FT] h 
			on h.[AS_OF_DT] >= DATEADD(month,-1,getdate()) and h.DEBIT_ACCT_NO =b.account_no
			where 
			a.cust_type = ''C'' and b.CIF_NO <> ''81758472'' 
			and c.divisi_CD in(	''IB2'', ''IB2 KP'', ''IB1 KP'', ''RDG'', ''CMG'', ''IB1'',''RDP'', ''CB2G'', ''IBG KP'', ''CB1G'', ''IBG'',''IB'',''CBG'',''CB3G'', ''BBG'', ''BB1G'', ''BB2G'')
			and case when c.divisi_CD in( ''BBG'', ''BB1G'', ''BB2G'') then d.pokok_awal else 600000000 end >500000000 
  			and upper(a.NAME_1) not like ''%VENTURA%''
			and upper(a.NAME_1) not like ''%KJKS%''
			and upper(a.NAME_1) not like ''%KSP%'' 
			and upper(a.NAME_1) not like ''%BMT%'' 
			and upper(a.NAME_1) not like ''%GADAI%'' 
			and upper(a.NAME_1) not like ''%DANA PENSIUN%''
			and upper(a.NAME_1) not like ''%DAPEN%'' 
			and upper(a.NAME_1) not like ''%ASURANSI%'' 
			and upper(a.NAME_1) not like ''%REKSADANA%''
			and upper(a.NAME_1) not like ''%FINANCE%'' 
			and upper(a.NAME_1) not like ''%SEKURITAS%''
			and upper(a.NAME_1) not like ''%BANK%''
			and upper(a.NAME_1) not like ''%BPR%'' 
			and upper(a.NAME_1) not like ''%BPD%''
			{parent}
			and (e.CIF_NO is  null and f.recid is  null and g.account_no is null and h.DEBIT_ACCT_NO is  null and b.Product_CD not like ''2%'')
				{children}'

	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )

	set @sql = replace(@sql,'EDW.DS_CUSTOMER_MASTER','EDW.DS_CUSTOMER_MASTER_'+@reportDate)
	set @sql = replace(@sql,'EDW.DEPOSITS','EDW.DEPOSITS_'+@reportDate)
	set @sql = replace(@sql,'EDW.CIF_DPK_DIVISI','EDW.CIF_DPK_DIVISI_'+@reportDate)
	set @sql = replace(@sql,'EDW.LOAN','EDW.LOAN_'+@reportDate)
	set @sql = replace(@sql,'EDW.OP_TRX_CMS','EDW.OP_TRX_CMS_'+@reportDate)
	set @sql = replace(@sql,'EDW.DS_AC_CASH_POOL','EDW.DS_AC_CASH_POOL_'+@reportDate)
	set @sql = replace(@sql,'EDW.ACCOUNT_MITRA_REMITTANCE','EDW.ACCOUNT_MITRA_REMITTANCE_'+@reportDate)
	set @sql = replace(@sql,'RX.STMT_FT','RX.STMT_FT_'+@reportDate)
	exec (@sql + ' Order by id')
END
/*
declare @reportDate varchar (10) = '20230630'
declare @filterDinamic varchar (max) = ''
exec [mrms].[lcr_Get_Equation_KorporasiNonOp_Daily] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_KorporasiNonOp_EOM]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO













-- =============================================
-- Author:		Willy Anggara
-- Create date: 27 June 2023
-- Description:	Get Equation Korporasi Non Op EOM
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Get_Equation_KorporasiNonOp_EOM]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) ,
	 @OrderLPS int
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	DECLARE @Names VARCHAR(8000) 

	set @filterDinamic = replace (@filterDinamic,'SUM_POKOK_AWAL','POKOK_AWAL')
	set @filterDinamic = replace (@filterDinamic,'SUM_PENCAIRANPOKOK','PENCAIRANPOKOK')
	set @filterDinamic = replace (@filterDinamic,'SUM_END_SALDO_AKHIR','SALDO_AKHIR')
	set @filterDinamic = replace (@filterDinamic,'SUM_END_SALDO_LSMK','SALDOLSMK')


	set @filterDinamic = replace (@filterDinamic,'[masterDPK_BSI]','a');
	set @filterDinamic = replace (@filterDinamic,'[masterPembiayaan_BSI]','b');
	set @filterDinamic = replace (@filterDinamic,'inputLPS','a.jenisvaluta')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end

	declare @sql varchar (max) 
		
	set @sql = 'SELECT a.id, a.noRekening, a.namaRekening, a.jenisValuta, a.saldoLSMK, a.imbalan,
				CASE WHEN e.cif_no IS NULL THEN ''tidak ada'' ELSE ''ada'' END CMS,
				CASE WHEN g.account_no IS NULL THEN ''tidak ada'' ELSE ''ada'' END REMITTANCE,
				CASE WHEN f.recid IS NULL THEN ''tidak ada'' ELSE ''ada'' END [CASH POOL],
				''Simpanan Non Operasional'' [Komponen 1],
				case when '''+ cast (@OrderLPS as varchar)+'''  = ''0'' then ''Dijamin LPS'' else ''Tidak Dijamin LPS'' end [Komponen 2]
			from [ddm].[masterDPK_BSI] a
			inner join [ddm].[masterPembiayaan_BSI] b
			on a.nocif =b.nocif
			left join [mis].[EDW.OP_TRX_CMS] e with (nolock)
			on e.cif_no =b.nocif
			left join [mis].[EDW.DS_AC_CASH_POOL] f with (nolock)
			on f.recid =a.norekening
			left join [mis].[EDW.ACCOUNT_MITRA_REMITTANCE] g with (nolock)
			on g.account_no = a.norekening
			left join [mis].[TRX.STMT_FT] h 
			on h.[AS_OF_DT] >= DATEADD(month,-1,getdate()) and h.DEBIT_ACCT_NO =a.norekening
			where 
			a.individu = ''C'' and a.nocif <> ''81758472'' 
			and a.kodebu in(	''IB2'', ''IB2 KP'', ''IB1 KP'', ''RDG'', ''CMG'', ''IB1'',''RDP'', ''CB2G'', ''IBG KP'', ''CB1G'', ''IBG'',''IB'',''CBG'',''CB3G'', ''BBG'', ''BB1G'', ''BB2G'')
			and b.pencairanpokok >500000000
			and upper(a.namaRekening) not like ''%VENTURA%''
				and upper(a.namaRekening) not like ''%KJKS%''
				and upper(a.namaRekening) not like ''%KSP%'' 
				and upper(a.namaRekening) not like ''%BMT%'' 
				and upper(a.namaRekening) not like ''%GADAI%'' 
				and upper(a.namaRekening) not like ''%DANA PENSIUN%''
				and upper(a.namaRekening) not like ''%DAPEN%'' 
				and upper(a.namaRekening) not like ''%ASURANSI%'' 
				and upper(a.namaRekening) not like ''%REKSADANA%''
				and upper(a.namaRekening) not like ''%FINANCE%'' 
				and upper(a.namaRekening) not like ''%SEKURITAS%''
				and upper(a.namaRekening) not like ''%BANK%''
				and upper(a.namaRekening) not like ''%BPR%'' 
				and upper(a.namaRekening) not like ''%BPD%'' 

			 {parent}
			 and (e.CIF_NO is  null and f.recid is  null and g.account_no is null or h.DEBIT_ACCT_NO is  null and a.kodeProduk not like ''2%'')
			 {children} '
	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )

	set @sql = replace(@sql,'masterDPK_BSI','masterDPK_BSI_'+@reportDate)
	set @sql = replace(@sql,'masterPembiayaan_BSI','masterPembiayaan_BSI_'+@reportDate)
	set @sql = replace(@sql,'EDW.OP_TRX_CMS','EDW.OP_TRX_CMS_'+@reportDate)
	set @sql = replace(@sql,'EDW.DS_AC_CASH_POOL','EDW.DS_AC_CASH_POOL_'+@reportDate)
	set @sql = replace(@sql,'EDW.ACCOUNT_MITRA_REMITTANCE','EDW.ACCOUNT_MITRA_REMITTANCE_'+@reportDate)
	set @sql = replace(@sql,'RX.STMT_FT','RX.STMT_FT_'+@reportDate)
	exec (@sql + ' Order by id')
END
/*
declare @reportDate varchar (10) = '20230630'
declare @filterDinamic varchar (max) = 'inputLPS = ''IDR'''
exec [mrms].[lcr_Get_Equation_KorporasiNonOp_EOM] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_KorporasiOp_Daily]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO













-- =============================================
-- Author:		Willy Anggara
-- Create date: 27 June 2023
-- Description:	Get Equation Korporasi Op Daily
-- Modified : Febrian Alfandi 07 Jul 2023 -->add new parameter OrderLPS
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Get_Equation_KorporasiOp_Daily]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max),
	 @OrderLPS int
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	DECLARE @Names VARCHAR(8000) 

		set @filterDinamic = replace (@filterDinamic,'SUM_POKOK_AWAL','POKOK_AWAL')
	set @filterDinamic = replace (@filterDinamic,'SUM_PENCAIRANPOKOK','PENCAIRANPOKOK')
	set @filterDinamic = replace (@filterDinamic,'SUM_END_SALDO_AKHIR','SALDO_AKHIR')
	set @filterDinamic = replace (@filterDinamic,'SUM_END_SALDO_LSMK','SALDOLSMK')

	set @filterDinamic = replace (@filterDinamic,'[EDW.DS_CUSTOMER_MASTER]','a');
	set @filterDinamic = replace (@filterDinamic,'[EDW.DEPOSITS]','b');
	set @filterDinamic = replace (@filterDinamic,'[EDW.CIF_DPK_DIVISI]','c');
	set @filterDinamic = replace (@filterDinamic,'[EDW.LOAN]','d');
	set @filterDinamic = replace (@filterDinamic,'inputLPS','b.CCY')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end

	declare @sql varchar (max) 
		
	set @sql = 'select distinct b.id, b.ACCOUNT_NO, a.NAME_1, b.CCY, b.SALDO_AKHIR, isnull(b.NISBAH_AKHIR,0) NISBAH_AKHIR, 
				CASE WHEN e.cif_no IS NULL THEN ''Tidak Ada'' ELSE ''Ada'' END CMS,
				CASE WHEN g.account_no IS NULL THEN ''Tidak ada'' ELSE ''Ada'' END REMITTANCE,
				CASE WHEN f.recid IS NULL THEN ''Tidak ada'' ELSE ''Ada'' END [CASH POOL],
				''Simpanan Operasional'' [Komponen 1],
				case when '''+ cast (@OrderLPS as varchar)+'''  = ''0'' then ''Dijamin LPS'' else ''Tidak Dijamin LPS'' end [Komponen 2]
				from
			[mis].[EDW.DS_CUSTOMER_MASTER] a with (nolock)
			inner join [mis].[EDW.DEPOSITS] b with (nolock)
			on a.CUSTOMER_CODE = b.CIF_NO
			inner join [mis].[EDW.CIF_DPK_DIVISI] C with (nolock)
			on b.CIF_NO =C.CIF_NO
			left join [mis].[EDW.LOAN] d with (nolock)
			on b.CIF_NO =d.CIF_NO
			left join [mis].[EDW.OP_TRX_CMS] e with (nolock)
			on e.cif_no =b.cif_no
			left join [mis].[EDW.DS_AC_CASH_POOL] f with (nolock)
			on f.recid =b.account_no
			left join [mis].[EDW.ACCOUNT_MITRA_REMITTANCE] g with (nolock)
			on g.account_no = b.account_no
			left join [mis].[TRX.STMT_FT] h 
			on h.[AS_OF_DT] >= DATEADD(month,-1,getdate()) and h.DEBIT_ACCT_NO =b.account_no
			where 
			a.cust_type = ''C'' and b.CIF_NO <> ''81758472'' 
			and c.divisi_CD in(	''IB2'', ''IB2 KP'', ''IB1 KP'', ''RDG'', ''CMG'', ''IB1'',''RDP'', ''CB2G'', ''IBG KP'', ''CB1G'', ''IBG'',''IB'',''CBG'',''CB3G'', ''BBG'', ''BB1G'', ''BB2G'')
			and case when c.divisi_CD in( ''BBG'', ''BB1G'', ''BB2G'') then d.pokok_awal else 600000000 end >500000000 
			--and d.pokok_awal  > 500000000
			 {parent}
			and (e.CIF_NO is not null or f.recid is not null or g.account_no is not null or h.DEBIT_ACCT_NO is not null)
			 {children} '
	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )

	set @sql = replace(@sql,'EDW.DS_CUSTOMER_MASTER','EDW.DS_CUSTOMER_MASTER_'+@reportDate)
	set @sql = replace(@sql,'EDW.DEPOSITS','EDW.DEPOSITS_'+@reportDate)
	set @sql = replace(@sql,'EDW.CIF_DPK_DIVISI','EDW.CIF_DPK_DIVISI_'+@reportDate)
	set @sql = replace(@sql,'EDW.LOAN','EDW.LOAN_'+@reportDate)
	set @sql = replace(@sql,'EDW.OP_TRX_CMS','EDW.OP_TRX_CMS_'+@reportDate)
	set @sql = replace(@sql,'EDW.DS_AC_CASH_POOL','EDW.DS_AC_CASH_POOL_'+@reportDate)
	set @sql = replace(@sql,'EDW.ACCOUNT_MITRA_REMITTANCE','EDW.ACCOUNT_MITRA_REMITTANCE_'+@reportDate)
	set @sql = replace(@sql,'RX.STMT_FT','RX.STMT_FT_'+@reportDate)
	exec (@sql + ' Order by id')
	
END
/*
declare @reportDate varchar (10) = '20230630'
declare @filterDinamic varchar (max) = ''
exec [mrms].[lcr_Get_Equation_KorporasiOp_Daily] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_KorporasiOp_EOM]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO













-- =============================================
-- Author:		Willy Anggara
-- Create date: 27 June 2023
-- Description:	Get Equation KorporasiOp EOM
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Get_Equation_KorporasiOp_EOM]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) ,
	 @OrderLPS int
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	DECLARE @Names VARCHAR(8000) 

		set @filterDinamic = replace (@filterDinamic,'SUM_POKOK_AWAL','POKOK_AWAL')
	set @filterDinamic = replace (@filterDinamic,'SUM_PENCAIRANPOKOK','PENCAIRANPOKOK')
	set @filterDinamic = replace (@filterDinamic,'SUM_END_SALDO_AKHIR','SALDO_AKHIR')
	set @filterDinamic = replace (@filterDinamic,'SUM_END_SALDO_LSMK','SALDOLSMK')

	set @filterDinamic = replace (@filterDinamic,'[masterDPK_BSI]','a');
	set @filterDinamic = replace (@filterDinamic,'[masterPembiayaan_BSI]','b');
	set @filterDinamic = replace (@filterDinamic,'inputLPS','a.jenisvaluta')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end

	declare @sql varchar (max) 
		
	set @sql = 'SELECT a.id, a.noRekening, a.namaRekening, a.jenisValuta, a.saldoLSMK, a.imbalan,
				CASE WHEN e.cif_no IS NULL THEN ''tidak ada'' ELSE ''ada'' END CMS,
				CASE WHEN g.account_no IS NULL THEN ''tidak ada'' ELSE ''ada'' END REMITTANCE,
				CASE WHEN f.recid IS NULL THEN ''tidak ada'' ELSE ''ada'' END [CASH POOL],
				''Simpanan Operasional'' [Komponen 1],
				case when '''+ cast (@OrderLPS as varchar)+'''  = ''0'' then ''Dijamin LPS'' else ''Tidak Dijamin LPS'' end [Komponen 2]
			from [ddm].[masterDPK_BSI] a
			inner join [ddm].[masterPembiayaan_BSI] b
			on a.nocif =b.nocif
			left join [mis].[EDW.OP_TRX_CMS] e with (nolock)
			on e.cif_no =b.nocif
			left join [mis].[EDW.DS_AC_CASH_POOL] f with (nolock)
			on f.recid =a.norekening
			left join [mis].[EDW.ACCOUNT_MITRA_REMITTANCE] g with (nolock)
			on g.account_no = a.norekening
			left join [mis].[TRX.STMT_FT] h 
			on h.[AS_OF_DT] >= DATEADD(month,-1,getdate()) and h.DEBIT_ACCT_NO =a.norekening
			where 
			a.individu = ''C'' and a.nocif <> ''81758472'' 
			and a.kodebu in(	''IB2'', ''IB2 KP'', ''IB1 KP'', ''RDG'', ''CMG'', ''IB1'',''RDP'', ''CB2G'', ''IBG KP'', ''CB1G'', ''IBG'',''IB'',''CBG'',''CB3G'', ''BBG'', ''BB1G'', ''BB2G'')
			and b.pencairanpokok >500000000 
			 {parent}
			and (e.CIF_NO is not null or f.recid is not null or g.account_no is not null or h.DEBIT_ACCT_NO is not null)
			 {children} '
	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )

	set @sql = replace(@sql,'masterDPK_BSI','masterDPK_BSI_'+@reportDate)
	set @sql = replace(@sql,'masterPembiayaan_BSI','masterPembiayaan_BSI_'+@reportDate)
	set @sql = replace(@sql,'EDW.OP_TRX_CMS','EDW.OP_TRX_CMS_'+@reportDate)
	set @sql = replace(@sql,'EDW.DS_AC_CASH_POOL','EDW.DS_AC_CASH_POOL_'+@reportDate)
	set @sql = replace(@sql,'EDW.ACCOUNT_MITRA_REMITTANCE','EDW.ACCOUNT_MITRA_REMITTANCE_'+@reportDate)
	set @sql = replace(@sql,'RX.STMT_FT','RX.STMT_FT_'+@reportDate)
	
	exec (@sql + ' Order by id')
END
/*
declare @reportDate varchar (10) = '20230630'
declare @filterDinamic varchar (max) = ''
exec  [mrms].[lcr_Get_Equation_KorporasiOp_EOM] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_NasabahPeroranganStabil_Daily]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO











-- =============================================
-- Author:		Willy Anggara
-- Create date: 27 June 2023
-- Description:	Persamaan nasabah perorangan stabil
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Get_Equation_NasabahPeroranganStabil_Daily]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	declare @sql varchar (max) 
	set @filterDinamic = replace (@filterDinamic,'SUM_POKOK_AWAL','POKOK_AWAL')
	set @filterDinamic = replace (@filterDinamic,'SUM_PENCAIRANPOKOK','PENCAIRANPOKOK')
	set @filterDinamic = replace (@filterDinamic,'SUM_END_SALDO_AKHIR','SALDO_AKHIR')
	set @filterDinamic = replace (@filterDinamic,'SUM_END_SALDO_LSMK','SALDOLSMK')

	set @filterDinamic = replace (@filterDinamic,'[EDW.DS_CUSTOMER_MASTER]','a');
	set @filterDinamic = replace (@filterDinamic,'[EDW.DEPOSITS]','b')
	set @filterDinamic = replace (@filterDinamic,'inputLPS','b.CCY')
	--set @filterDinamic = replace (@filterDinamic,'inputLPS','b.CCY')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end


	set @sql = 'SELECT B.id, a.NAME_1, b.CIF_NO, b.ACCOUNT_NO, a.CUST_TYPE, b.CCY, b.SALDO_AKHIR, b.NISBAH_AKHIR
					FROM [mis].[EDW.DS_CUSTOMER_MASTER] a with (nolock)
					inner join [mis].[EDW.DEPOSITS] b with (nolock)
					on a.CUSTOMER_CODE = b.CIF_NO
					left join [mis].[EDW.LOAN] C with (nolock)
					on b.CIF_NO =C.CIF_NO
					left join [mis].[EDW.DS_SALARY_DTL] d with (nolock)
					on b.ACCOUNT_NO =d.ACCOUNT_NUMBER
					where 
					(a.CUST_TYPE = ''R''  and b.Product_CD not like ''2%'' {parent} )
					and (c.CIF_NO is not null or d.account_number is not null {children}) '

	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'or '+ @Filter else '' end )

	set @sql = replace(@sql,'EDW.DS_CUSTOMER_MASTER','EDW.DS_CUSTOMER_MASTER_'+@reportDate)
	set @sql = replace(@sql,'EDW.DEPOSITS','EDW.DEPOSITS_'+@reportDate)
	set @sql = replace(@sql,'EDW.LOAN','EDW.LOAN_'+@reportDate)
	set @sql = replace(@sql,'EDW.DS_SALARY_DTL','EDW.DS_SALARY_DTL_'+@reportDate)

	exec (@sql + ' Order by id ')

END
/*
declare @reportDate varchar (10) = '20230630'
declare @filterDinamic varchar (max) = 'inputLPS =''IDR'''
exec [mrms].[lcr_Get_Equation_NasabahPeroranganStabil_Daily] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_NasabahPeroranganStabil_EOM]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO











-- =============================================
-- Author:		Willy Anggara
-- Create date: 27 June 2023
-- Description:	Get Nasabah Peorangan Stabil EOM Equation
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Get_Equation_NasabahPeroranganStabil_EOM]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	declare @sql varchar (max) 

	set @filterDinamic = replace (@filterDinamic,'SUM_POKOK_AWAL','POKOK_AWAL')
	set @filterDinamic = replace (@filterDinamic,'SUM_PENCAIRANPOKOK','PENCAIRANPOKOK')
	set @filterDinamic = replace (@filterDinamic,'SUM_END_SALDO_AKHIR','SALDO_AKHIR')
	set @filterDinamic = replace (@filterDinamic,'SUM_END_SALDO_LSMK','SALDOLSMK')

	set @filterDinamic = replace (@filterDinamic,'[masterDPK_BSI]','a');
	set @filterDinamic = replace (@filterDinamic,'[masterPembiayaan_BSI]','b');
	set @filterDinamic = replace (@filterDinamic,'inputLPS','a.jenisvaluta')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end


	set @sql = 'SELECT a.id, a.noRekening, a.namaRekening, a.individu, a.jenisvaluta, a.SaldoLSMK, a.imbalan
					from [ddm].[masterDPK_BSI] a
					inner join [ddm].[masterPembiayaan_BSI] b
					on a.nocif =b.nocif
					left join [mis].[EDW.LOAN] C with (nolock)
					on b.nocif =C.CIF_NO
					left join [mis].[EDW.DS_SALARY_DTL] d with (nolock)
					on a.norekening =d.ACCOUNT_NUMBER
					where 
					(a.individu = ''R''  and a.kodeProduk not like ''2%'' {parent} )
					and (c.CIF_NO is not null or d.account_number is not null {children}) '

	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'or '+ @Filter else '' end )

	set @sql = replace(@sql,'masterDPK_BSI','masterDPK_BSI_'+@reportDate)
	set @sql = replace(@sql,'masterPembiayaan_BSI','masterPembiayaan_BSI_'+@reportDate)
	set @sql = replace(@sql,'EDW.LOAN','EDW.LOAN_'+@reportDate)
	set @sql = replace(@sql,'EDW.DS_SALARY_DTL','EDW.DS_SALARY_DTL_'+@reportDate)

	exec (@sql + ' Order by id ')

END
/*
declare @reportDate varchar (10) = '20230630'
declare @filterDinamic varchar (max) = 'inputLPS =''IDR'''
exec [mrms].[lcr_Get_Equation_NasabahPeroranganStabil_EOM] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_NasabahPeroranganTidakStabil_Daily]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO











-- =============================================
-- Author:		Willy Anggara
-- Create date: 27 June 2023
-- Description:	Get Nasabah Peorangan Tidak Stabil Daily Equation
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Get_Equation_NasabahPeroranganTidakStabil_Daily]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
    -- Insert statements for procedure here
	declare @sql varchar (max) 
	set @filterDinamic = replace (@filterDinamic,'SUM_POKOK_AWAL','POKOK_AWAL')
	set @filterDinamic = replace (@filterDinamic,'SUM_PENCAIRANPOKOK','PENCAIRANPOKOK')
	set @filterDinamic = replace (@filterDinamic,'SUM_END_SALDO_AKHIR','SALDO_AKHIR')
	set @filterDinamic = replace (@filterDinamic,'SUM_END_SALDO_LSMK','SALDOLSMK')

	set @filterDinamic = replace (@filterDinamic,'[EDW.DS_CUSTOMER_MASTER]','a');
	set @filterDinamic = replace (@filterDinamic,'[EDW.DEPOSITS]','b')
	set @filterDinamic = replace (@filterDinamic,'inputLPS','b.CCY')
	--set @filterDinamic = replace (@filterDinamic,'inputLPS','b.CCY')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end


	set @sql = 'SELECT B.id, a.NAME_1, b.CIF_NO, b.ACCOUNT_NO, a.CUST_TYPE, b.CCY, b.SALDO_AKHIR, b.NISBAH_AKHIR
					FROM [mis].[EDW.DS_CUSTOMER_MASTER] a with (nolock)
					inner join [mis].[EDW.DEPOSITS] b with (nolock)
					on a.CUSTOMER_CODE = b.CIF_NO
					left join [mis].[EDW.LOAN] C with (nolock)
					on b.CIF_NO =C.CIF_NO
					left join [mis].[EDW.DS_SALARY_DTL] d with (nolock)
					on b.ACCOUNT_NO =d.ACCOUNT_NUMBER
					where 
					(a.CUST_TYPE = ''R''  and b.Product_CD not like ''2%'' {parent} )
					and (c.CIF_NO is null and d.account_number is  null {children}) '

	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )

	set @sql = replace(@sql,'EDW.DS_CUSTOMER_MASTER','EDW.DS_CUSTOMER_MASTER_'+@reportDate)
	set @sql = replace(@sql,'EDW.DEPOSITS','EDW.DEPOSITS_'+@reportDate)
	set @sql = replace(@sql,'EDW.LOAN','EDW.LOAN_'+@reportDate)
	set @sql = replace(@sql,'EDW.DS_SALARY_DTL','EDW.DS_SALARY_DTL_'+@reportDate)

	exec (@sql + ' Order by id ')
END
/*
declare @reportDate varchar (10) = '20230630'
declare @filterDinamic varchar (max) = 'inputLPS =''IDR'''
exec [mrms].[lcr_Get_Equation_NasabahPeroranganTidakStabil_Daily] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_NasabahPeroranganTidakStabil_EOM]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO











-- =============================================
-- Author:		Willy Anggara
-- Create date: 27 June 2023
-- Description:	Get Nasabah Peorangan Tidak Stabil EOM Equation
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Get_Equation_NasabahPeroranganTidakStabil_EOM]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
    -- Insert statements for procedure here
	declare @sql varchar (max) 

	set @filterDinamic = replace (@filterDinamic,'SUM_POKOK_AWAL','POKOK_AWAL')
	set @filterDinamic = replace (@filterDinamic,'SUM_PENCAIRANPOKOK','PENCAIRANPOKOK')
	set @filterDinamic = replace (@filterDinamic,'SUM_END_SALDO_AKHIR','SALDO_AKHIR')
	set @filterDinamic = replace (@filterDinamic,'SUM_END_SALDO_LSMK','SALDOLSMK')

	set @filterDinamic = replace (@filterDinamic,'[masterDPK_BSI]','a');
	set @filterDinamic = replace (@filterDinamic,'[masterPembiayaan_BSI]','b');
	set @filterDinamic = replace (@filterDinamic,'inputLPS','a.jenisvaluta')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end


	set @sql = 'SELECT a.id, a.noRekening, a.namaRekening, a.individu, a.jenisvaluta, a.SaldoLSMK, a.imbalan
					from [ddm].[masterDPK_BSI] a
					inner join [ddm].[masterPembiayaan_BSI] b
					on a.nocif =b.nocif
					left join [mis].[EDW.LOAN] C with (nolock)
					on b.nocif =C.CIF_NO
					left join [mis].[EDW.DS_SALARY_DTL] d with (nolock)
					on a.norekening =d.ACCOUNT_NUMBER
					where 
					(a.individu = ''R''  and b.kodeProduk not like ''2%'' {parent} )
					and (c.CIF_NO is null and d.account_number is null {children}) '

	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )

	set @sql = replace(@sql,'masterDPK_BSI','masterDPK_BSI_'+@reportDate)
	set @sql = replace(@sql,'masterPembiayaan_BSI','masterPembiayaan_BSI_'+@reportDate)
	set @sql = replace(@sql,'EDW.LOAN','EDW.LOAN_'+@reportDate)
	set @sql = replace(@sql,'EDW.DS_SALARY_DTL','EDW.DS_SALARY_DTL_'+@reportDate)


	exec (@sql + ' Order by id ')
END
/*
declare @reportDate varchar (10) = '20230630'
declare @filterDinamic varchar (max) = 'inputLPS =''IDR'''
exec [mrms].[lcr_Get_Equation_NasabahPeroranganTidakStabil_EOM] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_NasabahUMKMStabil_Daily]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO











-- =============================================
-- Author:		Willy Anggara
-- Create date: 27 June 2023
-- Description:	Get Equation Nasabah UMKM Stabil Daily
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Get_Equation_NasabahUMKMStabil_Daily]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	declare @sql varchar (max) 

	set @filterDinamic = replace (@filterDinamic,'SUM_POKOK_AWAL','POKOK_AWAL')
	set @filterDinamic = replace (@filterDinamic,'SUM_PENCAIRANPOKOK','PENCAIRANPOKOK')
	set @filterDinamic = replace (@filterDinamic,'SUM_END_SALDO_AKHIR','SALDO_AKHIR')
	set @filterDinamic = replace (@filterDinamic,'SUM_END_SALDO_LSMK','SALDOLSMK')

	set @filterDinamic = replace (@filterDinamic,'[EDW.DS_CUSTOMER_MASTER]','a')
	set @filterDinamic = replace (@filterDinamic,'[EDW.DEPOSITS]','b')
	set @filterDinamic = replace (@filterDinamic,'[EDW.LOAN]','c')
	set @filterDinamic = replace (@filterDinamic,'[EDW.CIF_DPK_DIVISI]','y')
	set @filterDinamic = replace (@filterDinamic,'inputLPS','b.CCY')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end

	set @sql = 'SELECT B.id, b.CIF_NO, b.ACCOUNT_NO, a.NAME_1, b.CCY, b.SALDO_AKHIR, b.NISBAH_AKHIR, c.POKOK_AWAL
				FROM [mis].[EDW.DS_CUSTOMER_MASTER] a with (nolock)
				inner join [mis].[EDW.DEPOSITS] b with (nolock)
				on a.CUSTOMER_CODE = b.CIF_NO
				inner join [mis].[EDW.CIF_DPK_DIVISI] y with (nolock)
				on b.CIF_NO =y.CIF_NO
				left join [mis].[EDW.LOAN] C with (nolock)
				on b.CIF_NO =C.CIF_NO
				left join [mis].[EDW.DS_SALARY_DTL] d with (nolock)
				on b.ACCOUNT_NO =d.ACCOUNT_NUMBER
			    where a.CUST_TYPE = ''c'' and b.PRODUCT_CD not like ''2%''
  				and upper(a.NAME_1) not like ''%VENTURA%''
				and upper(a.NAME_1) not like ''%KJKS%''
				and upper(a.NAME_1) not like ''%KSP%'' 
				and upper(a.NAME_1) not like ''%BMT%'' 
				and upper(a.NAME_1) not like ''%GADAI%'' 
				and upper(a.NAME_1) not like ''%DANA PENSIUN%''
				and upper(a.NAME_1) not like ''%DAPEN%'' 
				and upper(a.NAME_1) not like ''%ASURANSI%'' 
				and upper(a.NAME_1) not like ''%REKSADANA%''
				and upper(a.NAME_1) not like ''%FINANCE%'' 
				and upper(a.NAME_1) not like ''%SEKURITAS%''
				and upper(a.NAME_1) not like ''%BANK%''
				and upper(a.NAME_1) not like ''%BPR%'' {parent}
				
				and (c.CIF_NO is not null or d.account_number is not null {children}) '
	
	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'or '+ @Filter else '' end )

	set @sql = replace(@sql,'EDW.DS_CUSTOMER_MASTER','EDW.DS_CUSTOMER_MASTER_'+@reportDate)
	set @sql = replace(@sql,'EDW.DEPOSITS','EDW.DEPOSITS_'+@reportDate)
	set @sql = replace(@sql,'EDW.LOAN','EDW.LOAN_'+@reportDate)
	set @sql = replace(@sql,'EDW.DS_SALARY_DTL','EDW.DS_SALARY_DTL_'+@reportDate)
	set @sql = replace(@sql,'EDW.CIF_DPK_DIVISI','EDW.CIF_DPK_DIVISI_'+@reportDate)
	exec (@sql + ' Order by id ')
END
/*
declare @reportDate varchar (10) = '20230630'
declare @filterDinamic varchar (max) = ''
exec [mrms].[lcr_Get_Equation_NasabahUMKMStabil_Daily] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_NasabahUMKMStabil_EOM]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO











-- =============================================
-- Author:		Willy Anggara
-- Create date: 26 June 2023
-- Description:	Persamaan nasabah UMKM
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Get_Equation_NasabahUMKMStabil_EOM]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	declare @sql varchar (max) 
	set @filterDinamic = replace (@filterDinamic,'[masterDPK_BSI]','a');
	set @filterDinamic = replace (@filterDinamic,'[masterPembiayaan_BSI]','b');
	set @filterDinamic = replace (@filterDinamic,'inputLPS','a.jenisvaluta')

	set @filterDinamic = replace (@filterDinamic,'SUM_POKOK_AWAL','POKOK_AWAL')
	set @filterDinamic = replace (@filterDinamic,'SUM_PENCAIRANPOKOK','PENCAIRANPOKOK')
	set @filterDinamic = replace (@filterDinamic,'SUM_END_SALDO_AKHIR','SALDO_AKHIR')
	set @filterDinamic = replace (@filterDinamic,'SUM_END_SALDO_LSMK','SALDOLSMK')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end

	set @sql = 'SELECT a.id, a.noCIF, a.namaRekening, a.jenisValuta, a.saldoLSMK, b.pencairanPokok, a.imbalan
				from [ddm].[masterDPK_BSI] a
				inner join [ddm].[masterPembiayaan_BSI] b
				on a.nocif =b.nocif
				left join [mis].[EDW.LOAN] C with (nolock)
				on b.nocif =C.CIF_NO
				left join [mis].[EDW.DS_SALARY_DTL] d with (nolock)
				on a.norekening =d.ACCOUNT_NUMBER
			    where a.individu = ''c'' and a.kodeProduk not like ''2%''
  				and upper(a.namarekening) not like ''%VENTURA%''
				and upper(a.namarekening) not like ''%KJKS%''
				and upper(a.namarekening) not like ''%KSP%'' 
				and upper(a.namarekening) not like ''%BMT%'' 
				and upper(a.namarekening) not like ''%GADAI%'' 
				and upper(a.namarekening) not like ''%DANA PENSIUN%''
				and upper(a.namarekening) not like ''%DAPEN%'' 
				and upper(a.namarekening) not like ''%ASURANSI%'' 
				and upper(a.namarekening) not like ''%REKSADANA%''
				and upper(a.namarekening) not like ''%FINANCE%'' 
				and upper(a.namarekening) not like ''%SEKURITAS%''
				and upper(a.namarekening) not like ''%BANK%''
				and upper(a.namarekening) not like ''%BPR%'' {parent}
				and (c.CIF_NO is not null or d.account_number is not null {children}) '
	
	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'or '+ @Filter else '' end )

	set @sql = replace(@sql,'masterDPK_BSI','masterDPK_BSI_'+@reportDate)
	set @sql = replace(@sql,'masterPembiayaan_BSI','masterPembiayaan_BSI_'+@reportDate)
	set @sql = replace(@sql,'EDW.LOAN','EDW.LOAN_'+@reportDate)
	set @sql = replace(@sql,'EDW.DS_SALARY_DTL','EDW.DS_SALARY_DTL_'+@reportDate)
	exec (@sql + ' Order by id ')
END
/*
declare @reportDate varchar (10) = '20230630'
declare @filterDinamic varchar (max) = 'inputLPS =''IDR'''
exec [mrms].[lcr_Get_Equation_NasabahUMKMStabil_EOM] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_NasabahUMKMTidakStabil_Daily]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO











-- =============================================
-- Author:		Willy Anggara
-- Create date: 27 June 2023
-- Description:	Get Equation Nasabah UMKM Tidak Stabil Daily
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Get_Equation_NasabahUMKMTidakStabil_Daily]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	declare @sql varchar (max) 

	set @filterDinamic = replace (@filterDinamic,'[EDW.DS_CUSTOMER_MASTER]','a')
	set @filterDinamic = replace (@filterDinamic,'[EDW.DEPOSITS]','b')
	set @filterDinamic = replace (@filterDinamic,'[EDW.LOAN]','c')
	set @filterDinamic = replace (@filterDinamic,'[EDW.CIF_DPK_DIVISI]','y')
	set @filterDinamic = replace (@filterDinamic,'inputLPS','b.CCY')

	set @filterDinamic = replace (@filterDinamic,'SUM_POKOK_AWAL','POKOK_AWAL')
	set @filterDinamic = replace (@filterDinamic,'SUM_PENCAIRANPOKOK','PENCAIRANPOKOK')
	set @filterDinamic = replace (@filterDinamic,'SUM_END_SALDO_AKHIR','SALDO_AKHIR')
	set @filterDinamic = replace (@filterDinamic,'SUM_END_SALDO_LSMK','SALDOLSMK')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end

	set @sql = 'SELECT B.id, b.CIF_NO, b.ACCOUNT_NO, a.NAME_1, b.CCY, b.SALDO_AKHIR, b.NISBAH_AKHIR, c.POKOK_AWAL
				FROM [mis].[EDW.DS_CUSTOMER_MASTER] a with (nolock)
				inner join [mis].[EDW.DEPOSITS] b with (nolock)
				on a.CUSTOMER_CODE = b.CIF_NO
				inner join [mis].[EDW.CIF_DPK_DIVISI] y with (nolock)
				on b.CIF_NO =y.CIF_NO
				left join [mis].[EDW.LOAN] C with (nolock)
				on b.CIF_NO =C.CIF_NO
				left join [mis].[EDW.DS_SALARY_DTL] d with (nolock)
				on b.ACCOUNT_NO =d.ACCOUNT_NUMBER
			    where a.CUST_TYPE = ''c'' and b.PRODUCT_CD not like ''2%''
  				and upper(a.NAME_1) not like ''%VENTURA%''
				and upper(a.NAME_1) not like ''%KJKS%''
				and upper(a.NAME_1) not like ''%KSP%'' 
				and upper(a.NAME_1) not like ''%BMT%'' 
				and upper(a.NAME_1) not like ''%GADAI%'' 
				and upper(a.NAME_1) not like ''%DANA PENSIUN%''
				and upper(a.NAME_1) not like ''%DAPEN%'' 
				and upper(a.NAME_1) not like ''%ASURANSI%'' 
				and upper(a.NAME_1) not like ''%REKSADANA%''
				and upper(a.NAME_1) not like ''%FINANCE%'' 
				and upper(a.NAME_1) not like ''%SEKURITAS%''
				and upper(a.NAME_1) not like ''%BANK%''
				and upper(a.NAME_1) not like ''%BPR%'' {parent}
				
				and (c.CIF_NO is null and d.account_number is null {children}) '
	
	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )

	set @sql = replace(@sql,'EDW.DS_CUSTOMER_MASTER','EDW.DS_CUSTOMER_MASTER_'+@reportDate)
	set @sql = replace(@sql,'EDW.DEPOSITS','EDW.DEPOSITS_'+@reportDate)
	set @sql = replace(@sql,'EDW.LOAN','EDW.LOAN_'+@reportDate)
	set @sql = replace(@sql,'EDW.DS_SALARY_DTL','EDW.DS_SALARY_DTL_'+@reportDate)
	set @sql = replace(@sql,'EDW.CIF_DPK_DIVISI','EDW.CIF_DPK_DIVISI_'+@reportDate)
	exec (@sql + ' Order by id ')
END
/*
declare @reportDate varchar (10) = '20230630'
declare @filterDinamic varchar (max) = ''
exec [mrms].[lcr_Get_Equation_NasabahUMKMTidakStabil_Daily] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_NasabahUMKMTidakStabil_EOM]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO











-- =============================================
-- Author:		Willy Anggara
-- Create date: 26 June 2023
-- Description:	Persamaan nasabah UMKM
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Get_Equation_NasabahUMKMTidakStabil_EOM]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	declare @sql varchar (max) 
	set @filterDinamic = replace (@filterDinamic,'[masterDPK_BSI]','a');
	set @filterDinamic = replace (@filterDinamic,'[masterPembiayaan_BSI]','b');
	set @filterDinamic = replace (@filterDinamic,'inputLPS','a.jenisvaluta')

	set @filterDinamic = replace (@filterDinamic,'SUM_POKOK_AWAL','POKOK_AWAL')
	set @filterDinamic = replace (@filterDinamic,'SUM_PENCAIRANPOKOK','PENCAIRANPOKOK')
	set @filterDinamic = replace (@filterDinamic,'SUM_END_SALDO_AKHIR','SALDO_AKHIR')
	set @filterDinamic = replace (@filterDinamic,'SUM_END_SALDO_LSMK','SALDOLSMK')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end

	set @sql = 'SELECT a.id, a.noCIF, a.namaRekening, a.jenisValuta, a.saldoLSMK, b.pencairanPokok, a.imbalan
				from [ddm].[masterDPK_BSI] a
				inner join [ddm].[masterPembiayaan_BSI] b
				on a.nocif =b.nocif
				left join [mis].[EDW.LOAN] C with (nolock)
				on b.nocif =C.CIF_NO
				left join [mis].[EDW.DS_SALARY_DTL] d with (nolock)
				on a.norekening =d.ACCOUNT_NUMBER
			    where a.individu = ''c'' and a.kodeProduk not like ''2%''
  				and upper(a.namarekening) not like ''%VENTURA%''
				and upper(a.namarekening) not like ''%KJKS%''
				and upper(a.namarekening) not like ''%KSP%'' 
				and upper(a.namarekening) not like ''%BMT%'' 
				and upper(a.namarekening) not like ''%GADAI%'' 
				and upper(a.namarekening) not like ''%DANA PENSIUN%''
				and upper(a.namarekening) not like ''%DAPEN%'' 
				and upper(a.namarekening) not like ''%ASURANSI%'' 
				and upper(a.namarekening) not like ''%REKSADANA%''
				and upper(a.namarekening) not like ''%FINANCE%'' 
				and upper(a.namarekening) not like ''%SEKURITAS%''
				and upper(a.namarekening) not like ''%BANK%''
				and upper(a.namarekening) not like ''%BPR%'' {parent}
				and (c.CIF_NO is null or d.account_number is null {children}) '
	
	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )

	set @sql = replace(@sql,'masterDPK_BSI','masterDPK_BSI_'+@reportDate)
	set @sql = replace(@sql,'masterPembiayaan_BSI','masterPembiayaan_BSI_'+@reportDate)
	set @sql = replace(@sql,'EDW.LOAN','EDW.LOAN_'+@reportDate)
	set @sql = replace(@sql,'EDW.DS_SALARY_DTL','EDW.DS_SALARY_DTL_'+@reportDate)
	exec (@sql + ' Order by id ')
END
/*
declare @reportDate varchar (10) = '20230630'
declare @filterDinamic varchar (max) = 'inputLPS =''IDR'''
exec [mrms].[lcr_Get_Equation_NasabahUMKMTidakStabil_EOM] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_TCPBI_Daily]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO













-- =============================================
-- Author:		Willy Anggara
-- Create date: 27 May 2023
-- Description:	Get Equation TCPBI Daily
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Get_Equation_TCPBI_Daily]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	declare @sql varchar (max) 
	set @filterDinamic = replace (@filterDinamic,'[DS_LD_SCHEDULE_GAB]','a');
	set @filterDinamic = replace (@filterDinamic,'[DS_REAL_PROJ_SCHEDULE_N]','b')
	set @filterDinamic = replace (@filterDinamic,'[EDW.LOAN]','c');
	set @filterDinamic = replace (@filterDinamic,'[EDW.DS_CUSTOMER_MASTER]','d')
	set @filterDinamic = replace (@filterDinamic,'[EDW.CIF_DPK_DIVISI]','e')
	set @filterDinamic = replace (@filterDinamic,'inputLPS','c.CCY')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end


	set @sql = 'select c.id, c.LOAN_NO, d.NAME_1, c.KOLEKT_CIF, c.CCY, a.ANGS_POKOK, a.ANGS_MARGIN, isnull(b.PROYEKSI,0) PROYEKSI
					from [mis].[DS_LD_SCHEDULE_GAB] a with (nolock)
					left join [mis].[DS_REAL_PROJ_SCHEDULE_N] b with (nolock)
					on a.LOAN_NO = b.ID
					inner join [mis].[EDW.LOAN] c with (nolock)
					on a.LOAN_NO =c.loan_NO
					inner join [mis].[EDW.DS_CUSTOMER_MASTER] d with (nolock)
					on d.CUSTOMER_CODE = c.CIF_NO
					inner join [mis].[EDW.CIF_DPK_DIVISI] e with (nolock)
					on e.CIF_NO = d.CUSTOMER_CODE
					where (d.CUST_TYPE = ''C'' and c.KOLEKT_CIF =1 
					and upper(d.name_1)  like ''%BANK INDONESIA%''
					{parent}  )
					{children} '

	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )

	set @sql = replace(@sql,'DS_LD_SCHEDULE_GAB','DS_LD_SCHEDULE_GAB_'+@reportDate)
	set @sql = replace(@sql,'DS_REAL_PROJ_SCHEDULE_N','DS_REAL_PROJ_SCHEDULE_N_'+@reportDate)
	set @sql = replace(@sql,'EDW.LOAN','EDW.LOAN_'+@reportDate)
	set @sql = replace(@sql,'EDW.DS_CUSTOMER_MASTER','EDW.DS_CUSTOMER_MASTER_'+@reportDate)
	set @sql = replace(@sql,'EDW.CIF_DPK_DIVISI','EDW.CIF_DPK_DIVISI_'+@reportDate)

	exec (@sql + ' Order by LOAN_NO ')

END
/*
declare @reportDate varchar (10) = '20230630'
declare @filterDinamic varchar (max) =
			''
exec [mrms].[lcr_Get_Equation_TCPBI_Daily] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_TCPBI_EOM]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO













-- =============================================
-- Author:		Willy Anggara
-- Create date: 27 May 2023
-- Description:	Get Equation TCPBI EOM
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Get_Equation_TCPBI_EOM]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	declare @sql varchar (max) 
	set @filterDinamic = replace (@filterDinamic,'[masterPembiayaan_BSI]','a');
	set @filterDinamic = replace (@filterDinamic,'[EDW.DS_CUSTOMER_MASTER]','b')
	set @filterDinamic = replace (@filterDinamic,'inputLPS','a.jenisValuta')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end

	set @sql = 'select a.id, a.noRekening, a.namaNasabah, a.kolektibilitas, a.jenisValuta, 
					a.angsuranPokokBerikutnya,a.periodeAngsuranMarginBerikutnya, a.proyeksiBagiHasil
					from [ddm].[masterPembiayaan_BSI] a
					inner join [mis].[EDW.DS_CUSTOMER_MASTER] b
					on a.noCif =b.CUSTOMER_CODE
					where (b.CUST_TYPE = ''C'' and a.kolektibilitas =1 
					and upper(b.name_1)  like ''%BANK INDONESIA%''
					{parent} )
					{children} '

	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )

	set @sql = replace(@sql,'masterPembiayaan_BSI','masterPembiayaan_BSI_'+@reportDate)
	set @sql = replace(@sql,'EDW.DS_CUSTOMER_MASTER','EDW.DS_CUSTOMER_MASTER_'+@reportDate)

	exec (@sql + ' Order by id ')

END
/*
declare @reportDate varchar (10) = '20230630'
declare @filterDinamic varchar (max) =
			''
exec  [mrms].[lcr_Get_Equation_TCPBI_EOM] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_TCPK_Daily]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO













-- =============================================
-- Author:		Willy Anggara
-- Create date: 27 May 2023
-- Description:	Get Equation TCPK Daily 
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Get_Equation_TCPK_Daily]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	declare @sql varchar (max) 
	set @filterDinamic = replace (@filterDinamic,'[DS_LD_SCHEDULE_GAB]','a');
	set @filterDinamic = replace (@filterDinamic,'[DS_REAL_PROJ_SCHEDULE_N]','b')
	set @filterDinamic = replace (@filterDinamic,'[EDW.LOAN]','c');
	set @filterDinamic = replace (@filterDinamic,'[EDW.DS_CUSTOMER_MASTER]','d')
	set @filterDinamic = replace (@filterDinamic,'[EDW.CIF_DPK_DIVISI]','e')
	set @filterDinamic = replace (@filterDinamic,'inputLPS','c.CCY')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end


	set @sql = 'select c.id, c.LOAN_NO, d.NAME_1, c.KOLEKT_CIF, c.CCY, a.ANGS_POKOK, a.ANGS_MARGIN, b.PROYEKSI
					from [mis].[DS_LD_SCHEDULE_GAB] a with (nolock)
					left join [mis].[DS_REAL_PROJ_SCHEDULE_N] b with (nolock)
					on a.LOAN_NO = b.ID
					inner join [mis].[EDW.LOAN] c with (nolock)
					on a.LOAN_NO =c.loan_NO
					inner join [mis].[EDW.DS_CUSTOMER_MASTER] d with (nolock)
					on d.CUSTOMER_CODE = c.CIF_NO
					inner join [mis].[EDW.CIF_DPK_DIVISI] e with (nolock)
					on e.CIF_NO = d.CUSTOMER_CODE
					where (d.CUST_TYPE = ''C'' and c.KOLEKT_CIF =1 and c.CIF_NO <> ''81758472''   {parent} )
					{children} '

	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )

	set @sql = replace(@sql,'DS_LD_SCHEDULE_GAB','DS_LD_SCHEDULE_GAB_'+@reportDate)
	set @sql = replace(@sql,'DS_REAL_PROJ_SCHEDULE_N','DS_REAL_PROJ_SCHEDULE_N_'+@reportDate)
	set @sql = replace(@sql,'EDW.LOAN','EDW.LOAN_'+@reportDate)
	set @sql = replace(@sql,'EDW.DS_CUSTOMER_MASTER','EDW.DS_CUSTOMER_MASTER_'+@reportDate)
	set @sql = replace(@sql,'EDW.CIF_DPK_DIVISI','EDW.CIF_DPK_DIVISI_'+@reportDate)

	exec (@sql + ' Order by LOAN_NO ')

END
/*
declare @reportDate varchar (10) = '20230630'
declare @filterDinamic varchar (max) =
			''
exec [mrms].[lcr_Get_Equation_TCPK_Daily] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_TCPK_EOM]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO













-- =============================================
-- Author:		Willy Anggara
-- Create date: 27 May 2023
-- Description:	Get Equation TCPK EOM
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Get_Equation_TCPK_EOM]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	declare @sql varchar (max) 
	set @filterDinamic = replace (@filterDinamic,'[masterPembiayaan_BSI]','a');
	set @filterDinamic = replace (@filterDinamic,'[EDW.DS_CUSTOMER_MASTER]','b')
	set @filterDinamic = replace (@filterDinamic,'inputLPS','a.jenisValuta')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end


	set @sql = 'select a.id, a.noRekening, a.namaNasabah, a.kolektibilitas, a.jenisValuta, 
					a.angsuranPokokBerikutnya,a.periodeAngsuranMarginBerikutnya, a.proyeksiBagiHasil
					from [ddm].[masterPembiayaan_BSI] a
					inner join [mis].[EDW.DS_CUSTOMER_MASTER] b
					on a.noCif =b.CUSTOMER_CODE
					where (b.CUST_TYPE = ''R'' and a.kolektibilitas =1 {parent} )
					{children} '

	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )

	set @sql = replace(@sql,'masterPembiayaan_BSI','masterPembiayaan_BSI_'+@reportDate)
	set @sql = replace(@sql,'EDW.DS_CUSTOMER_MASTER','EDW.DS_CUSTOMER_MASTER_'+@reportDate)

	exec (@sql + ' Order by id ')

END
/*
declare @reportDate varchar (10) = '20230630'
declare @filterDinamic varchar (max) =
			''
exec [mrms].[lcr_Get_Equation_TCPK_EOM] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_TCPLK_Daily]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO













-- =============================================
-- Author:		Willy Anggara
-- Create date: 27 May 2023
-- Description:	Get Equation TCPLK Daily
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Get_Equation_TCPLK_Daily]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	declare @sql varchar (max) 
	set @filterDinamic = replace (@filterDinamic,'[DS_LD_SCHEDULE_GAB]','a');
	set @filterDinamic = replace (@filterDinamic,'[DS_REAL_PROJ_SCHEDULE_N]','b')
	set @filterDinamic = replace (@filterDinamic,'[EDW.LOAN]','c');
	set @filterDinamic = replace (@filterDinamic,'[EDW.DS_CUSTOMER_MASTER]','d')
	set @filterDinamic = replace (@filterDinamic,'[EDW.CIF_DPK_DIVISI]','e')
	set @filterDinamic = replace (@filterDinamic,'inputLPS','c.CCY')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end


	set @sql = 'select c.id,c.LOAN_NO, d.NAME_1, c.KOLEKT_CIF, c.CCY, a.ANGS_POKOK, a.ANGS_MARGIN, b.PROYEKSI
					from [mis].[DS_LD_SCHEDULE_GAB] a with (nolock)
					left join [mis].[DS_REAL_PROJ_SCHEDULE_N] b with (nolock)
					on a.LOAN_NO = b.ID
					inner join [mis].[EDW.LOAN] c with (nolock)
					on a.LOAN_NO =c.loan_NO
					inner join [mis].[EDW.DS_CUSTOMER_MASTER] d with (nolock)
					on d.CUSTOMER_CODE = c.CIF_NO
					inner join [mis].[EDW.CIF_DPK_DIVISI] e with (nolock)
					on e.CIF_NO = d.CUSTOMER_CODE
					where (d.CUST_TYPE = ''C'' and c.KOLEKT_CIF =1 
					and 
					(upper(d.name_1)  like ''%VENTURA%''
					or upper(d.name_1)  like ''%KJKS%''
					or upper(d.name_1) like ''%KSP%'' 
					or upper(d.name_1) like ''%BMT%'' 
					or upper(d.name_1) like ''%GADAI%'' 
					or upper(d.name_1) like ''%DANA PENSIUN%''
					or upper(d.name_1) like ''%DAPEN%'' 
					or upper(d.name_1) like ''%ASURANSI%'' 
					or upper(d.name_1) like ''%REKSADANA%''
					or upper(d.name_1) like ''%FINANCE%'' 
					or upper(d.name_1) like ''%SEKURITAS%''
					or upper(d.name_1) like ''%BANK%''
					or upper(d.name_1) like ''%BPR%'' )
					{parent}  )
					{children} '

	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )

	set @sql = replace(@sql,'DS_LD_SCHEDULE_GAB','DS_LD_SCHEDULE_GAB_'+@reportDate)
	set @sql = replace(@sql,'DS_REAL_PROJ_SCHEDULE_N','DS_REAL_PROJ_SCHEDULE_N_'+@reportDate)
	set @sql = replace(@sql,'EDW.LOAN','EDW.LOAN_'+@reportDate)
	set @sql = replace(@sql,'EDW.DS_CUSTOMER_MASTER','EDW.DS_CUSTOMER_MASTER_'+@reportDate)
	set @sql = replace(@sql,'EDW.CIF_DPK_DIVISI','EDW.CIF_DPK_DIVISI_'+@reportDate)

	exec (@sql + ' Order by LOAN_NO ')

END
/*
declare @reportDate varchar (10) = '20230630'
declare @filterDinamic varchar (max) =
			''
exec [mrms].[lcr_Get_Equation_TCPLK_Daily] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_TCPLK_EOM]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO













-- =============================================
-- Author:		Willy Anggara
-- Create date: 27 May 2023
-- Description:	Get Equation TCPLK EOM
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Get_Equation_TCPLK_EOM]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	declare @sql varchar (max) 
	set @filterDinamic = replace (@filterDinamic,'[masterPembiayaan_BSI]','a');
	set @filterDinamic = replace (@filterDinamic,'[EDW.DS_CUSTOMER_MASTER]','b')
	set @filterDinamic = replace (@filterDinamic,'inputLPS','a.jenisValuta')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end


	set @sql = 'select a.id, a.noRekening, a.namaNasabah, a.kolektibilitas, a.jenisValuta, 
					a.angsuranPokokBerikutnya, a.periodeAngsuranMarginBerikutnya,a.proyeksiBagiHasil
					from [ddm].[masterPembiayaan_BSI] a
					inner join [mis].[EDW.DS_CUSTOMER_MASTER] b
					on a.noCif =b.CUSTOMER_CODE
					where (b.CUST_TYPE = ''C'' and a.kolektibilitas =1 
					and 
					(upper(b.name_1)  like ''%VENTURA%''
					or upper(b.name_1)  like ''%KJKS%''
					or upper(b.name_1) like ''%KSP%'' 
					or upper(b.name_1) like ''%BMT%'' 
					or upper(b.name_1) like ''%GADAI%'' 
					or upper(b.name_1) like ''%DANA PENSIUN%''
					or upper(b.name_1) like ''%DAPEN%'' 
					or upper(b.name_1) like ''%ASURANSI%'' 
					or upper(b.name_1) like ''%REKSADANA%''
					or upper(b.name_1) like ''%FINANCE%'' 
					or upper(b.name_1) like ''%SEKURITAS%''
					or upper(b.name_1) like ''%BANK%''
					or upper(b.name_1) like ''%BPR%'' )
					{parent} )
					{children} '

	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )

	set @sql = replace(@sql,'masterPembiayaan_BSI','masterPembiayaan_BSI_'+@reportDate)
	set @sql = replace(@sql,'EDW.DS_CUSTOMER_MASTER','EDW.DS_CUSTOMER_MASTER_'+@reportDate)

	exec (@sql + ' Order by id ')

END
/*
declare @reportDate varchar (10) = '20230630'
declare @filterDinamic varchar (max) =
			''
exec  [mrms].[lcr_Get_Equation_TCPLK_EOM] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_TCPP_Daily]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO













-- =============================================
-- Author:		Willy Anggara
-- Create date: 27 May 2023
-- Description:	Get Equation TCPP Daily
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Get_Equation_TCPP_Daily]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	declare @sql varchar (max) 
	set @filterDinamic = replace (@filterDinamic,'[DS_LD_SCHEDULE_GAB]','a');
	set @filterDinamic = replace (@filterDinamic,'[DS_REAL_PROJ_SCHEDULE_N]','b')
	set @filterDinamic = replace (@filterDinamic,'[EDW.LOAN]','c');
	set @filterDinamic = replace (@filterDinamic,'[EDW.DS_CUSTOMER_MASTER]','d')
	set @filterDinamic = replace (@filterDinamic,'inputLPS','c.CCY')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end


	set @sql = 'select c.id,c.LOAN_NO, d.NAME_1, c.KOLEKT_CIF, c.CCY, a.ANGS_POKOK, a.ANGS_MARGIN, b.PROYEKSI
					from [mis].[DS_LD_SCHEDULE_GAB] a with (nolock)
					left join [mis].[DS_REAL_PROJ_SCHEDULE_N] b with (nolock)
					on a.LOAN_NO = b.ID
					inner join [mis].[EDW.LOAN] c with (nolock)
					on a.LOAN_NO =c.loan_NO
					inner join [mis].[EDW.DS_CUSTOMER_MASTER] d with (nolock)
					on d.CUSTOMER_CODE = c.CIF_NO
					where (d.CUST_TYPE = ''R'' and c.KOLEKT_CIF =1  {parent} )
					{children} '

	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )

	set @sql = replace(@sql,'DS_LD_SCHEDULE_GAB','DS_LD_SCHEDULE_GAB_'+@reportDate)
	set @sql = replace(@sql,'DS_REAL_PROJ_SCHEDULE_N','DS_REAL_PROJ_SCHEDULE_N_'+@reportDate)
	set @sql = replace(@sql,'EDW.LOAN','EDW.LOAN_'+@reportDate)
	set @sql = replace(@sql,'EDW.DS_CUSTOMER_MASTER','EDW.DS_CUSTOMER_MASTER_'+@reportDate)

	exec (@sql + ' Order by LOAN_NO ')

END
/*
declare @reportDate varchar (10) = '20230630'
declare @filterDinamic varchar (max) =
			''
exec [mrms].[lcr_Get_Equation_TCPP_Daily] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_TCPP_EOM]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO













-- =============================================
-- Author:		Willy Anggara
-- Create date: 27 May 2023
-- Description:	Get Equation TCPP EOM
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Get_Equation_TCPP_EOM]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	declare @sql varchar (max) 
	set @filterDinamic = replace (@filterDinamic,'[masterPembiayaan_BSI]','a');
	set @filterDinamic = replace (@filterDinamic,'[EDW.DS_CUSTOMER_MASTER]','b')
	set @filterDinamic = replace (@filterDinamic,'inputLPS','a.jenisValuta')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end


	set @sql = 'select a.id, a.noRekening, a.namaNasabah, a.kolektibilitas, a.jenisValuta, 
					a.angsuranPokokBerikutnya, a.periodeAngsuranMarginBerikutnya,a.proyeksiBagiHasil
					from [ddm].[masterPembiayaan_BSI] a
					inner join [mis].[EDW.DS_CUSTOMER_MASTER] b
					on a.noCif =b.CUSTOMER_CODE
					where (b.CUST_TYPE = ''R'' and a.kolektibilitas =1 {parent} )
					{children} '

	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )

	set @sql = replace(@sql,'masterPembiayaan_BSI','masterPembiayaan_BSI_'+@reportDate)
	set @sql = replace(@sql,'EDW.DS_CUSTOMER_MASTER','EDW.DS_CUSTOMER_MASTER_'+@reportDate)

	exec (@sql + ' Order by id ')

END
/*
declare @reportDate varchar (10) = '20230630'
declare @filterDinamic varchar (max) =
			''
exec [mrms].[lcr_Get_Equation_TCPP_EOM] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_TCPPUMKM_Daily]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO













-- =============================================
-- Author:		Willy Anggara
-- Create date: 27 May 2023
-- Description:	Get Equation TCPPUMKM Daily
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Get_Equation_TCPPUMKM_Daily]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	declare @sql varchar (max) 
	set @filterDinamic = replace (@filterDinamic,'[DS_LD_SCHEDULE_GAB]','a');
	set @filterDinamic = replace (@filterDinamic,'[DS_REAL_PROJ_SCHEDULE_N]','b')
	set @filterDinamic = replace (@filterDinamic,'[EDW.LOAN]','c');
	set @filterDinamic = replace (@filterDinamic,'[EDW.DS_CUSTOMER_MASTER]','d')
	set @filterDinamic = replace (@filterDinamic,'[EDW.CIF_DPK_DIVISI]','e')
	set @filterDinamic = replace (@filterDinamic,'inputLPS','c.CCY')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end


	set @sql = 'select c.id,c.LOAN_NO, d.NAME_1, c.KOLEKT_CIF, c.CCY, a.ANGS_POKOK, a.ANGS_MARGIN, b.PROYEKSI
					from [mis].[DS_LD_SCHEDULE_GAB] a with (nolock)
					left join [mis].[DS_REAL_PROJ_SCHEDULE_N] b with (nolock)
					on a.LOAN_NO = b.ID
					inner join [mis].[EDW.LOAN] c with (nolock)
					on a.LOAN_NO =c.loan_NO
					inner join [mis].[EDW.DS_CUSTOMER_MASTER] d with (nolock)
					on d.CUSTOMER_CODE = c.CIF_NO
					inner join [mis].[EDW.CIF_DPK_DIVISI] e with (nolock)
					on e.CIF_NO = d.CUSTOMER_CODE
					where (d.CUST_TYPE = ''C'' and c.KOLEKT_CIF =1 
					and upper(d.name_1) not like ''%VENTURA%''
					and upper(d.name_1) not like ''%KJKS%''
					and upper(d.name_1) not like ''%KSP%'' 
					and upper(d.name_1) not like ''%BMT%'' 
					and upper(d.name_1) not like ''%GADAI%'' 
					and upper(d.name_1) not like ''%DANA PENSIUN%''
					and upper(d.name_1) not like ''%DAPEN%'' 
					and upper(d.name_1) not like ''%ASURANSI%'' 
					and upper(d.name_1) not like ''%REKSADANA%''
					and upper(d.name_1) not like ''%FINANCE%'' 
					and upper(d.name_1) not like ''%SEKURITAS%''
					and upper(d.name_1) not like ''%BANK%''
					and upper(d.name_1) not like ''%BPR%''
					{parent}  )
					{children} '

	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )

	set @sql = replace(@sql,'DS_LD_SCHEDULE_GAB','DS_LD_SCHEDULE_GAB_'+@reportDate)
	set @sql = replace(@sql,'DS_REAL_PROJ_SCHEDULE_N','DS_REAL_PROJ_SCHEDULE_N_'+@reportDate)
	set @sql = replace(@sql,'EDW.LOAN','EDW.LOAN_'+@reportDate)
	set @sql = replace(@sql,'EDW.DS_CUSTOMER_MASTER','EDW.DS_CUSTOMER_MASTER_'+@reportDate)
	set @sql = replace(@sql,'EDW.CIF_DPK_DIVISI','EDW.CIF_DPK_DIVISI_'+@reportDate)

	exec (@sql + ' Order by LOAN_NO ')

END
/*
declare @reportDate varchar (10) = '20230630'
declare @filterDinamic varchar (max) =
			''
exec [mrms].[lcr_Get_Equation_TCPPUMKM_Daily] @reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_Get_Equation_TCPPUMKM_EOM]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO













-- =============================================
-- Author:		Willy Anggara
-- Create date: 27 May 2023
-- Description:	Get Equation TCPPUMKM EOM
-- =============================================
CREATE PROCEDURE [mrms].[lcr_Get_Equation_TCPPUMKM_EOM]
	-- Add the parameters for the stored procedure here
	 @reportDate varchar (10),
	 @filterDinamic varchar (max) 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	declare @sql varchar (max) 
	set @filterDinamic = replace (@filterDinamic,'[masterPembiayaan_BSI]','a');
	set @filterDinamic = replace (@filterDinamic,'[EDW.DS_CUSTOMER_MASTER]','b')
	set @filterDinamic = replace (@filterDinamic,'inputLPS','a.jenisValuta')

	declare @parentFilter varchar (max) =''
	declare @Filter varchar (max)=''

	if ((select count(*) FROM STRING_SPLIT(@filterDinamic, ';')) =2)
	begin 
		select top 1 @parentFilter = value FROM STRING_SPLIT(@filterDinamic, ';')
		set @Filter =  replace(replace(@filterDinamic,@parentFilter,''),';','') 
	end
	else
	begin
		select @Filter = value FROM STRING_SPLIT(@filterDinamic, ';')
	end


	set @sql = 'select a.id, a.noRekening, a.namaNasabah, a.kolektibilitas, a.jenisValuta, 
					a.angsuranPokokBerikutnya, a.periodeAngsuranMarginBerikutnya, a.proyeksiBagiHasil
					from [ddm].[masterPembiayaan_BSI] a
					inner join [mis].[EDW.DS_CUSTOMER_MASTER] b
					on a.noCif =b.CUSTOMER_CODE
					where (b.CUST_TYPE = ''C'' and a.kolektibilitas =1 {parent} )
					{children} '

	set @sql = replace(@sql,'{parent}',case when @parentFilter <> '' then 'and '+ @parentFilter else '' end )
	set @sql = replace(@sql,'{children}',case when @Filter <> '' then 'and '+ @Filter else '' end )

	set @sql = replace(@sql,'masterPembiayaan_BSI','masterPembiayaan_BSI_'+@reportDate)
	set @sql = replace(@sql,'EDW.DS_CUSTOMER_MASTER','EDW.DS_CUSTOMER_MASTER_'+@reportDate)

	exec (@sql + ' Order by id ')

END
/*
declare @reportDate varchar (10) = '20230630'
declare @filterDinamic varchar (max) =
			''
exec  [mrms].[lcr_Get_Equation_TCPPUMKM_EOM]@reportDate,@filterDinamic
*/
GO

/****** Object:  StoredProcedure [mrms].[lcr_TempTable_Upload_Daily_Create]    Script Date: 1/12/2024 10:57:10 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO









-- =============================================
-- Author:		Febrian Alfandi
-- Create date: 22 Jun 2023
-- Description:	Create temp table for daily upload
-- modified : Febrian Alfandi 03 Jun 2023 --> add 2 new column in DataHarianSuratBerharga
-- =============================================
CREATE PROCEDURE [mrms].[lcr_TempTable_Upload_Daily_Create]
	@typeExcel varchar (100),
	@RandomString varchar(10)=''
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	declare @sql varchar (max) =''
	if (@typeExcel = 'DataHarianSuratBerharga')
	begin
		set @sql = '
		CREATE TABLE ##DataHarianSuratBerharga'+@RandomString+'(
			[DESCR] [nvarchar](max) NULL,
			[SECID] [nvarchar](max) NULL,
			[ISSUER] [nvarchar](max) NULL,
			[TYPE] [nvarchar](max) NULL,
			[RELATED] [nvarchar](max) NULL,
			[CURRENCY] [nvarchar](max) NULL,
			[INTRATE] [decimal](10, 2) NOT NULL,
			[ISSUEDATE] [datetime2](7) NOT NULL,
			[MATURITYDATE] [datetime2](7) NULL,
			[POSITION] [decimal](18, 2) NOT NULL,
			[PARVALUE] [decimal](10, 2) NOT NULL,
			[BOOKCOST] [decimal](18, 2) NOT NULL,
			[UNREALIZED] [decimal](18, 2) NOT NULL,
			[REALIZED] [decimal](18, 2) NOT NULL,
			[AccrueInt] [decimal](18, 2) NOT NULL,
			[UNAMORTIZE] [decimal](18, 2) NOT NULL,
			[MARKET] [decimal](18, 2) NOT NULL,
			[BOOKVALUE] [decimal](18, 2) NOT NULL,
			[NEXTINTEREST] [datetime2](7) NOT NULL,
			[RATINGR] [nvarchar](max) NULL,
			[LEMBAGARATING] [nvarchar](max) NULL,
			[INDUSTRY] [nvarchar](max) NULL,
			[FairValue] [decimal](18, 2) NOT NULL,
			[LembagaKeuangan] [nvarchar](max) NULL,
			[GovCorp] [nvarchar](max) NULL,
			[BobotATMR] [decimal](10, 2) NOT NULL,
			[Yield] [decimal](10, 2) NOT NULL,
			[KategoriPortfolio] [nvarchar](max) NULL,
			[DiperdagangkanSecaraAktif] [nvarchar](max) NULL,
			[CouponRate] [decimal](10, 2)  NULL,
			[CouponPaymentperYear] [decimal](10, 2) NULL
		)
		'
	end
	else if (@typeExcel = 'SuratBerhargaDiterbitkan')
	begin
		set @sql = '
		CREATE TABLE ##SuratBerhargaDiterbitkan'+@RandomString+'(
			[Number] [int] NOT NULL,
			[Sukuk] [nvarchar](max) NULL,
			[Currency] [nvarchar](max) NULL,
			[Nominal] [decimal](18, 2) NOT NULL,
			[IndikasiRate] [decimal](10, 2) NOT NULL,
			[JatuhTempo] [datetime2](7) NOT NULL,
			[Peringkat] [nvarchar](max) NULL,
		)'
	end
	else if (@typeExcel = 'Repo')
	begin
		set @sql = '
		CREATE TABLE ##Repo'+@RandomString+'(
		[Number] [int] NOT NULL,
		[CounterParty] [nvarchar](max) NULL,
		[Currency] [nvarchar](max) NULL,
		[Individu] [nvarchar](max) NULL,
		[Nominal] [decimal](18, 2) NOT NULL,
		[Indikasi Rate] [decimal](10, 2) NOT NULL,
		[JatuhTempo] [datetime2](7) NOT NULL,
		[HQLALevel] [nvarchar](max) NULL,
		)'
	end
	else if (@typeExcel = 'ReverseRepo')
	begin
		set @sql = '
			CREATE TABLE ##ReverseRepo'+@RandomString+'(
			[Number] [int] NOT NULL,
			[CounterParty] [nvarchar](max) NULL,
			[Currency] [nvarchar](max) NULL,
			[Individu] [nvarchar](max) NULL,
			[Nominal] [decimal](18, 2) NOT NULL,
			[Indikasi Rate] [decimal](10, 2) NOT NULL,
			[JatuhTempo] [datetime2](7) NOT NULL,
			[HQLALevel] [nvarchar](max) NULL,
			)'

	end
	else if (@typeExcel = 'Simulasi')
	begin
		set @sql = '
		CREATE TABLE ##Simulasi'+@RandomString+'(
		[Number] [int] NOT NULL,
		[Jenis] [nvarchar](100) NULL,
		[Currency] [nvarchar](max) NULL,
		[Individu] [nvarchar](max) NULL,
		[Nominal] [decimal](18, 2) NOT NULL,
		[ImbahHasil] [decimal](10, 2) NOT NULL,
		[JatuhTempo] [datetime2](7) NOT NULL,
		[Peringkat] [nvarchar](max) NULL,
		)'
	end
	exec (@sql)
END
/*
exec [mrms].[TempTable_Upload_Daily_Create] 'DataHarianSuratBerharga'
go
*/
GO


Leave a Comment