SET ANSI_NULLS ON
SET ANSI_PADDING ON;
SET QUOTED_IDENTIFIER ON
go


--drop functions
if object_id('[FAF].[report_activity]', 'FN') is not NULL
	drop function [FAF].[report_activity]; 
go
if object_id('[FAF].[report_client_trans]', 'FN') is not NULL
	drop function [FAF].[report_client_trans]; 
go
if object_id('[FAF].[historical_report]', 'FN') is not NULL
	drop function [FAF].[historical_report]; 
go
if object_id('[FAF].[booking_report]', 'FN') is not NULL
	drop function [FAF].[booking_report]; 
go
if object_id('[FAF].[set_of_payments]', 'FN') is not NULL
	drop function [FAF].[set_of_payments]; 
go
if object_id('[FAF].[one_in_payment]', 'FN') is not NULL
	drop function [FAF].[one_in_payment]; 
go
if object_id('[FAF].[one_out_payment]', 'FN') is not NULL
	drop function [FAF].[one_out_payment]; 
go
if object_id('[FAF].[one_payment]', 'FN') is not NULL
	drop function [FAF].[one_payment]; 
go
if object_id('[FAF].[trans_node_from_payments]', 'FN') is not NULL
	drop function [FAF].[trans_node_from_payments]; 
go
if object_id('[FAF].[node_deposit_withdrawal]', 'FN') is not NULL
	drop function [FAF].[node_deposit_withdrawal]; 
go
if object_id('[FAF].[trans_node_from_ea]', 'FN') is not NULL
	drop function [FAF].[trans_node_from_ea]; 
go
if object_id('[FAF].[head_part_of_report]', 'FN') is not NULL
	drop function [FAF].[head_part_of_report]; 
go
if object_id('[FAF].[transaction_node]', 'FN') is not NULL
	drop function [FAF].[transaction_node]; 
go
if object_id('[FAF].[historical_Tr_number]', 'FN') is not NULL
	drop function [FAF].[historical_Tr_number]; 
go

--drop types
if exists (select 1 from sys.types where is_table_type = 1 and name ='FAF_MROS_IdRsn_t') 
	drop type FAF.FAF_MROS_IdRsn_t;
go
if exists (select 1 from sys.types where is_table_type = 1 and name ='FAF_MROS_charID_t') 
	drop type FAF.FAF_MROS_charID_t;
go
if exists (select 1 from sys.types where is_table_type = 1 and name ='FAF_MROS_numID_t') 
	drop type FAF.FAF_MROS_numID_t;
go
if exists (select 1 from sys.types where is_table_type = 1 and name ='FAF_MROS_techn_t') 
	drop type FAF.FAF_MROS_techn_t;
go
if exists (select 1 from sys.types where is_table_type = 1 and name ='FAF_MROS_NumNum_t') 
	drop type FAF.FAF_MROS_NumNum_t;
go
if exists (select 1 from sys.types where is_table_type = 1 and name ='FAF_MROS_CharChar_t') 
	drop type FAF.FAF_MROS_CharChar_t;
go
if exists (select 1 from sys.types where is_table_type = 1 and name ='FAF_MROS_PmntTpl_t') 
	drop type FAF.FAF_MROS_PmntTpl_t;
go
if exists (select 1 from sys.types where is_table_type = 1 and name ='FAF_MROS_TrTpl_t') 
	drop type FAF.FAF_MROS_TrTpl_t;
go
if exists (select 1 from sys.types where is_table_type = 1 and name ='FAF_MROS_ClntTpl_t') 
	drop type FAF.FAF_MROS_ClntTpl_t;
go
if exists (select 1 from sys.types where is_table_type = 1 and name ='FAF_MROS_AcntTpl_t') 
	drop type FAF.FAF_MROS_AcntTpl_t;
go
if exists (select 1 from sys.types where is_table_type = 1 and name ='FAF_MROS_KdKdTpl_t') 
	drop type FAF.FAF_MROS_KdKdTpl_t;
go
if exists (select 1 from sys.types where is_table_type = 1 and name ='FAF_MROS_KdKdTpl_ext_t') 
	drop type FAF.FAF_MROS_KdKdTpl_ext_t;
go
if exists (select 1 from sys.types where is_table_type = 1 and name ='FAF_MROS_NumChar_t') 
	drop type FAF.FAF_MROS_NumChar_t;
go


--create types
create type FAF.FAF_MROS_IdRsn_t as table (chID varchar(50),partner_flag decimal(1,0),rsn varchar(4000));
create type FAF.FAF_MROS_charID_t as table (chID varchar(50));
create type FAF.FAF_MROS_numID_t as table (id int identity(1,1),nID decimal(24,0));
create type FAF.FAF_MROS_techn_t as table (nID decimal(24,0),significance int,comments varchar(4000),lvl int);
create type FAF.FAF_MROS_NumNum_t as table (id int identity(1,1),nID1 decimal(24,0),nID2 decimal(24,0));
create type FAF.FAF_MROS_CharChar_t as table (id int identity(1,1),chID1 varchar(80),chID2 varchar(80));
create type FAF.FAF_MROS_PmntTpl_t as table (id int identity(1,1),pm_id decimal(24,0),a1_kt decimal(24,0),a2_kt decimal(24,0),adr_nr decimal(24,0),au_nr decimal(24,0),om_nr decimal(24,0),tp int,esr varchar(27),ap_flag int,sh_cd int,kt_orig varchar(35),key_20 int,key_30 int,key_40 int,key_50 int,key_60 int,key_70 int,key_80 int,key_90 int,key_100 int);
create type FAF.FAF_MROS_TrTpl_t as table (id int identity(1,1),tr_id decimal(24,0),ac_id decimal(24,0),tp int,key_510 int,key_520 int,key_530 int,key_540 int,key_550 int,key_560 int,key_570 int);
create type FAF.FAF_MROS_AcntTpl_t as table (nID decimal(24,0),xml1 xml,xml2 xml,clid decimal(24,0),flg int,cltp int);
create type FAF.FAF_MROS_KdKdTpl_t as table (id int identity(1,1),nID decimal(24,0),rl varchar(80),cmm varchar(4000),ptn varchar(4000),kdtp int,kdnr varchar(80),kdnm varchar(255),dt1 date,dt2 date,e_rl varchar(80),e_cmm varchar(4000));
create type FAF.FAF_MROS_KdKdTpl_ext_t as table (id int identity(1,1),nID decimal(24,0),rl varchar(80),cmm varchar(4000),ptn varchar(4000),kdtp int,kdnr varchar(80),kdnm varchar(255),srt int,flg decimal(1,0));
create type FAF.FAF_MROS_NumChar_t as table (id int identity(1,1),nID decimal(24,0),chID varchar(4000));
go



---------------------------------------------------------------------------------------------------



--Transaction number in historical request
if object_id('[FAF].[historical_Tr_number]', 'FN') is not NULL
	drop function [FAF].[historical_Tr_number]; 
go
create function FAF.historical_Tr_number (
		@F_BANK_ID int,
		@SET_OF_CL_NR FAF.FAF_MROS_charID_t readonly,
		@SET_OF_AC_NR FAF.FAF_MROS_charID_t readonly,
		@SET_OF_BUCH_CAT FAF.FAF_MROS_CharChar_t readonly,
		@DATE_FROM int,
		@DATE_TO int,
		@LOW_AMOUNT decimal(20,2),
		@DEBIT tinyint,
		@CREDIT tinyint)
returns int
as
begin
	declare @nn int;
	declare @mm int;
	declare @kk int;
	declare @am_lim decimal(20,2);
	declare @d1 int;
	declare @d2 int;
	declare @dd1 date;
	declare @dd2 date;
	declare @n_ct int;
	declare @SET_OF_AC_ID FAF.FAF_MROS_NumID_t;
	declare @SET_OF_BUCH_CAT1 FAF.FAF_MROS_CharChar_t;

	--Debit/Credit checking
	if (coalesce(@DEBIT,1)=0 and coalesce(@CREDIT,1)=0) or (@DATE_FROM>@DATE_TO or @DATE_FROM>cast(convert(varchar,sysdatetime(),112) as int))
		set @kk = 0;

	--if there is no error then start the main request
	if @kk is NULL
	begin
		--input dates checking
		set @d1 = @DATE_FROM;
		if @d1 is NULL or @d1<cast(FAF.GoAML_get_paramS(@F_BANK_ID,115,1) as integer) or @d1>cast(convert(varchar,sysdatetime(),112) as int) 
			set @d1 = cast(FAF.GoAML_get_paramS(@F_BANK_ID,115,1) as integer);
		set @d2 = @DATE_TO;
		if @d2 is NULL or @d1>@d2
			set @d2 = cast(FAF.GoAML_get_paramS(@F_BANK_ID,115,2) as integer);
		set @dd1 = convert(date,cast(@d1 as varchar),112);
		set @dd2 = convert(date,cast(@d2 as varchar),112);

		--amount limit checking
		set @am_lim = @LOW_AMOUNT;
		if @am_lim is NULL or @am_lim<0.01
			set @am_lim = 0.01;

		set @n_ct = (select count(*) from @SET_OF_BUCH_CAT);
		if @n_ct>0
		begin
			insert into @SET_OF_BUCH_CAT1
			select chID1,
				case
					when tst>=3 then 'all'
					when tst=2 then 'credit'
					when tst=1 then 'debit'
				end as chID2
			from 
				(select chID1,sum(tst) as tst from
					(select distinct chID1,
						case 
							when lower(chID2)='debit' then 1
							when lower(chID2)='credit' then 2
							when lower(chID2)='all' then 4
							else 0
						end as tst
					from @SET_OF_BUCH_CAT) s0
				group by chID1) s1
			where tst>0;
		end;
		set @n_ct = (select count(*) from @SET_OF_BUCH_CAT1);

		--list of relevant account_id
		set @kk = (select count(*) from @SET_OF_AC_NR);
		if @kk>0
			begin
				insert into @SET_OF_AC_ID
				select kt_lnr as nID
				from 
					FAF.FA_MROS_KT_STAMM kt
				where kt.edit_kt_nr in (select chID from @SET_OF_AC_NR)
					and kt.userbk_nr=@F_BANK_ID;
			end;
		else
			begin
				set @kk = (select count(*) from @SET_OF_CL_NR);
				if @kk>0
					begin
						insert into @SET_OF_AC_ID
						select kt_lnr as nID
						from 
							FAF.FA_MROS_KD_STAMM kd
						join
							FAF.FA_MROS_KT_STAMM kt
						on kt.userbk_nr=kd.userbk_nr and kt.kd_lnr=kd.kd_lnr
						where kd.edit_kd_nr in (select chID from @SET_OF_CL_NR)
							and kd.userbk_nr=@F_BANK_ID;
					end;
			end;

		set @kk = (select count(*) from @SET_OF_AC_ID);
		if @kk>0
			begin
				if @n_ct>0
					begin
						set @nn = 
							(select count(*) from
								(select pm.zv_zlg_sys_lnr
								from
									FAF.FA_MROS_ZV_ZLG_T pm
								--acceptable transaction types only
								join
									FAF.FA_MROS_REFERENCE_TABLE tr_tp
								on pm.buchtyp_cd=tr_tp.Finnova_value and tr_tp.userbk_nr=pm.userbk_nr and tr_tp.ref_type=10 and tr_tp.activ_MROS=1
								--acceptable trak_ref types only
								join
									FAF.FA_MROS_REFERENCE_TABLE ref_tp
								on pm.trak_ref_typ=ref_tp.Finnova_value and ref_tp.userbk_nr=pm.userbk_nr and ref_tp.ref_type=11 and ref_tp.activ_MROS=1
								left join
									FAF.FA_MROS_TRX_CATEGORIES_DET cat
								on cat.userbk_nr=pm.userbk_nr and cat.buchtyp_cd=pm.buchtyp_cd and cat.trak_ref_typ=pm.trak_ref_typ and cat.gf_nr=pm.gf_nr 
								left join
									@SET_OF_BUCH_CAT1 sc
								on sc.chID1=cat.category_group
								left join
									@SET_OF_BUCH_CAT1 sc1
								on sc1.chID1=tr_tp.category_group
								where pm.userbk_nr=@F_BANK_ID
									and pm.kt_lnr in (select nID from @SET_OF_AC_ID)
									and pm.ausf_dt_ist>=@dd1
									and pm.ausf_dt_ist<=@dd2
									and abs(pm.betrag_bwrg)>=@am_lim
									and (sc.chID1 is not NULL 
										and (pm.sh_cd=1 and lower(sc.chID2) in ('credit','all') and isnull(@CREDIT,1)=1
											or pm.sh_cd=2 and lower(sc.chID2) in ('debit','all') and isnull(@DEBIT,1)=1)
									or sc1.chID1 is not NULL 
										and (pm.sh_cd=1 and lower(sc1.chID2) in ('credit','all') and isnull(@CREDIT,1)=1
											or pm.sh_cd=2 and lower(sc1.chID2) in ('debit','all') and isnull(@DEBIT,1)=1))
								union
								select pm.zv_zlg_sys_lnr
								from
									FAF.FA_MROS_ZV_ZLG_T pm
								--acceptable transaction types only
								join
									FAF.FA_MROS_REFERENCE_TABLE tr_tp
								on pm.buchtyp_cd=tr_tp.Finnova_value and tr_tp.userbk_nr=pm.userbk_nr and tr_tp.ref_type=10 and tr_tp.activ_MROS=1
								--acceptable trak_ref types only
								join
									FAF.FA_MROS_REFERENCE_TABLE ref_tp
								on pm.trak_ref_typ=ref_tp.Finnova_value and ref_tp.userbk_nr=pm.userbk_nr and ref_tp.ref_type=11 and ref_tp.activ_MROS=1
								left join
									FAF.FA_MROS_TRX_CATEGORIES_DET cat
								on cat.userbk_nr=pm.userbk_nr and cat.buchtyp_cd=pm.buchtyp_cd and cat.trak_ref_typ=pm.trak_ref_typ and cat.gf_nr=pm.gf_nr
								left join
									@SET_OF_BUCH_CAT1 sc
								on sc.chID1=cat.category_group
								left join
									@SET_OF_BUCH_CAT1 sc1
								on sc1.chID1=tr_tp.category_group
								where pm.userbk_nr=@F_BANK_ID
									and pm.kt_lnr_b in (select nID from @SET_OF_AC_ID)
									and pm.ausf_dt_ist>=@dd1
									and pm.ausf_dt_ist<=@dd2
									and abs(pm.betrag_bwrg)>=@am_lim
									and (sc.chID1 is not NULL 
										and (pm.sh_cd=2 and lower(sc.chID2) in ('credit','all') and isnull(@CREDIT,1)=1
										or pm.sh_cd=1 and lower(sc.chID2) in ('debit','all') and isnull(@DEBIT,1)=1)
									or sc1.chID1 is not NULL 
										and (pm.sh_cd=2 and lower(sc1.chID2) in ('credit','all') and isnull(@CREDIT,1)=1
										or pm.sh_cd=1 and lower(sc1.chID2) in ('debit','all') and isnull(@DEBIT,1)=1))
								) s
							);
					end;
				else
					begin
						set @nn = 
							(select count(*) from
								(select pm.zv_zlg_sys_lnr
								from
									FAF.FA_MROS_ZV_ZLG_T pm
								--acceptable transaction types only
								join
									FAF.FA_MROS_REFERENCE_TABLE tr_tp
								on pm.buchtyp_cd=tr_tp.Finnova_value and tr_tp.userbk_nr=pm.userbk_nr and tr_tp.ref_type=10 and tr_tp.activ_MROS=1
								--acceptable trak_ref types only
								join
									FAF.FA_MROS_REFERENCE_TABLE ref_tp
								on pm.trak_ref_typ=ref_tp.Finnova_value and ref_tp.userbk_nr=pm.userbk_nr and ref_tp.ref_type=11 and ref_tp.activ_MROS=1
								where pm.userbk_nr=@F_BANK_ID
									and pm.kt_lnr in (select nID from @SET_OF_AC_ID)
									and pm.ausf_dt_ist>=@dd1
									and pm.ausf_dt_ist<=@dd2
									and abs(pm.betrag_bwrg)>=@am_lim
									and (pm.sh_cd=1 and isnull(@CREDIT,1)=1 or pm.sh_cd=2 and isnull(@DEBIT,1)=1)
								union
								select pm.zv_zlg_sys_lnr
								from
									FAF.FA_MROS_ZV_ZLG_T pm
								--acceptable transaction types only
								join
									FAF.FA_MROS_REFERENCE_TABLE tr_tp
								on pm.buchtyp_cd=tr_tp.Finnova_value and tr_tp.userbk_nr=pm.userbk_nr and tr_tp.ref_type=10 and tr_tp.activ_MROS=1
								--acceptable trak_ref types only
								join
									FAF.FA_MROS_REFERENCE_TABLE ref_tp
								on pm.trak_ref_typ=ref_tp.Finnova_value and ref_tp.userbk_nr=pm.userbk_nr and ref_tp.ref_type=11 and ref_tp.activ_MROS=1
								where pm.userbk_nr=@F_BANK_ID
									and pm.kt_lnr_b in (select nID from @SET_OF_AC_ID)
									and pm.ausf_dt_ist>=@dd1
									and pm.ausf_dt_ist<=@dd2
									and abs(pm.betrag_bwrg)>=@am_lim
									and (pm.sh_cd=2 and isnull(@CREDIT,1)=1 or pm.sh_cd=1 and isnull(@DEBIT,1)=1)
								) s
							);
					end;

				--non-payments
				if @n_ct>0
					begin
						set @mm = 
							(select count(*) from
								FAF.FA_MROS_KT_BUCH_EA tr
							--acceptable transaction types only
							join
								FAF.FA_MROS_REFERENCE_TABLE tr_tp
							on tr.buchtyp_cd=tr_tp.Finnova_value and tr_tp.userbk_nr=tr.userbk_nr and tr_tp.ref_type=10 and tr_tp.activ_MROS=1
							--acceptable trak_ref types only
							join
								FAF.FA_MROS_REFERENCE_TABLE ref_tp
							on tr.trak_ref_typ=ref_tp.Finnova_value and ref_tp.userbk_nr=tr.userbk_nr and ref_tp.ref_type=11 and ref_tp.activ_MROS=1
							left join
								FAF.FA_MROS_TRX_CATEGORIES_DET cat
							on cat.userbk_nr=tr.userbk_nr and cat.buchtyp_cd=tr.buchtyp_cd and cat.trak_ref_typ=tr.trak_ref_typ and cat.gf_nr is NULL
							left join
								@SET_OF_BUCH_CAT1 sc
							on sc.chID1=cat.category_group
							left join
								@SET_OF_BUCH_CAT1 sc1
							on sc1.chID1=tr_tp.category_group
							where tr.kt_lnr in (select nID from @SET_OF_AC_ID)
								and tr.userbk_nr=@F_BANK_ID
								and tr.buch_dat_a_1>=@d1
								and tr.buch_dat_a_1<=@d2
								and abs(tr.betrag_bwrg)>=@am_lim
								and (sc.chID1 is not NULL
									and (tr.sh_cd='D' and lower(sc.chID2) in ('debit','all') and isnull(@DEBIT,1)=1
									or tr.sh_cd='C' and lower(sc.chID2) in ('credit','all') and isnull(@CREDIT,1)=1)
								or sc1.chID1 is not NULL
									and (tr.sh_cd='D' and lower(sc1.chID2) in ('debit','all') and isnull(@DEBIT,1)=1
									or tr.sh_cd='C' and lower(sc1.chID2) in ('credit','all') and isnull(@CREDIT,1)=1)));
					end;
				else
					begin
						set @mm = 
							(select count(*) from
								FAF.FA_MROS_KT_BUCH_EA tr
							--acceptable transaction types only
							join
								FAF.FA_MROS_REFERENCE_TABLE tr_tp
							on tr.buchtyp_cd=tr_tp.Finnova_value and tr_tp.userbk_nr=tr.userbk_nr and tr_tp.ref_type=10 and tr_tp.activ_MROS=1
							--acceptable trak_ref types only
							join
							  FAF.FA_MROS_REFERENCE_TABLE ref_tp
							on tr.trak_ref_typ=ref_tp.Finnova_value and ref_tp.userbk_nr=tr.userbk_nr and ref_tp.ref_type=11 and ref_tp.activ_MROS=1
							where tr.kt_lnr in (select nID from @SET_OF_AC_ID)
								and tr.userbk_nr=@F_BANK_ID
								and tr.buch_dat_a_1>=@d1
								and tr.buch_dat_a_1<=@d2
								and abs(tr.betrag_bwrg)>=@am_lim
								and (tr.SH_CD = 'D' and isnull(@DEBIT,1)=1 or tr.SH_CD = 'C' and isnull(@CREDIT,1)=1));
					end;
			end;
	end;
	
	set @kk = isnull(@nn,0)+isnull(@mm,0);
	return(@kk);
end;
go



---------------------------------------------------------------------------------------------------



--transaction node for all types of functions (technical function)
if object_id('[FAF].[transaction_node]', 'FN') is not NULL
	drop function [FAF].[transaction_node]; 
go
create function FAF.transaction_node (
	@F_BANK_ID int,
	@pmnt_id FAF.FAF_MROS_PmntTpl_t readonly, 
	@trans_id FAF.FAF_MROS_TrTpl_t readonly,
	@acnt_id FAF.FAF_MROS_NumNum_t readonly) 
returns xml
as
begin
	declare @xml_r xml,
		@xml_t xml,
		@xml_p xml,
		@xml_wd xml,
		@xml_cl xml,
		@xml_clt xml,
		@xml_cl1 xml,
		@xml_cl0 xml,
		@xml_cl5 xml,
		@xml_cl5t xml,
		@xml_kd xml,
		@xml_kd5 xml,
		@xml_ac xml,
		@xml_a2 xml,
		@xml_dp xml,
		@xml_de xml,
		@xml_da xml,
		@xml_da0 xml,
		@xml_in xml,
		@xml_out xml,
		@xml_bk xml,
		@xml_ac_fnd xml;
	declare @ac_fnd int;
	declare @curr varchar(50);
	declare @kt_nr varchar(80);
	declare @am_lim decimal(20,2);
	declare @d1 int;
	declare @d2 int;
	declare @dd1 date;
	declare @dd2 date;
	declare @mm int;
	declare @mm0 int;
	declare @mmm int;
	declare @mmm0 int;
	declare @nn int;
	declare @ii int;
	declare @jj int;
	declare @ll int;
	declare @kk int;
	declare @bb int;
	declare @nn_p int;
	declare @nn_t int;
	declare @n_id decimal(24,0);
	declare @kt_id decimal(24,0);
	declare @cl_id decimal(24,0);
	declare @ac1_id decimal(24,0);
	declare @ac2_id decimal(24,0);
	declare @adr_nr decimal(24,0);
	declare @au_nr decimal(24,0);
	declare @om_nr decimal(24,0);
	declare @pmnt_tp int;
	declare @trans_tp int;
	declare @acnts FAF.FAF_MROS_AcntTpl_t;
	declare @KD_KD0 FAF.FAF_MROS_numID_t;
	declare @KD_KD FAF.FAF_MROS_KdKdTpl_t;
	declare @dt_fr date;
	declare @dt_fr0 date;
	declare @dt_to date;
	declare @dt_fr_e date;
	declare @dt_to_e date;
	declare @cl_tp int;
	declare @kd_tp int;
	declare @kt_orig varchar(35);
	declare @cl_nm  varchar(4000);
	declare @kd_nm  varchar(4000);
	declare @cl_nr varchar(80);
	declare @kd_nr varchar(80);
	declare @tecom varchar(4000);
	declare @tpcom varchar(4000);
	declare @auf_dt0 date;
	declare @rol varchar(80);
	declare @comm varchar(4000);
	declare @e_rol varchar(80);
	declare @e_comm varchar(4000);
	declare @clnt_txt varchar(4000);
	declare @adr_txt varchar(4000);
	declare @esr_txt varchar(4000);
	declare @om_txt varchar(4000);
	declare @com_txt varchar(4000);
	declare @com1_txt varchar(4000);
	declare @trd_txt varchar(4000);
	declare @iban_txt varchar(4000);
	declare @tmp_txt varchar(4000);
	declare @D_AdrTp varchar(4000);
	declare @D_Dept varchar(4000);
	declare @D_SubDept varchar(4000);
	declare @D_BldgNm varchar(4000);
	declare @D_Flr varchar(4000);
	declare @D_PstBx varchar(4000);
	declare @D_Room varchar(4000);
	declare @D_TwnLctnNm varchar(4000);
	declare @D_DstrctNm varchar(4000);
	declare @D_CtrySubDvsn varchar(4000);
	declare @D_name varchar(4000);
	declare @D_StrtNm varchar(4000);
	declare @D_BldgNb varchar(4000);
	declare @D_AdrLine varchar(4000);
	declare @D_AdrLine_tmp varchar(4000);
	declare @D_TwnNm varchar(4000);
	declare @D_PstCd varchar(4000);
	declare @D_Ctry varchar(4000);
	declare @D_xml xml;
	declare @C_AdrTp varchar(4000);
	declare @C_Dept varchar(4000);
	declare @C_SubDept varchar(4000);
	declare @C_BldgNm varchar(4000);
	declare @C_Flr varchar(4000);
	declare @C_PstBx varchar(4000);
	declare @C_Room varchar(4000);
	declare @C_TwnLctnNm varchar(4000);
	declare @C_DstrctNm varchar(4000);
	declare @C_CtrySubDvsn varchar(4000);
	declare @C_name varchar(4000);
	declare @C_StrtNm varchar(4000);
	declare @C_BldgNb varchar(4000);
	declare @C_AdrLine varchar(4000);
	declare @C_AdrLine_tmp varchar(4000);
	declare @C_TwnNm varchar(4000);
	declare @C_PstCd varchar(4000);
	declare @C_Ctry varchar(4000);
	declare @C_xml xml;
	declare @partners varchar(4000);
	declare @st1 varchar(4000);
	declare @st2 varchar(4000);
	declare @st3 varchar(4000);
	declare @st4 varchar(4000);
	declare @esr varchar(60);
	declare @cntr varchar(3);
	declare @swif varchar(11);
	declare @inst_name varchar(255);
	declare @df_esr_txt varchar(10);
	declare @flag int;
	declare @flag0 int;
	declare @flag1 int;
	declare @flag2 int;
	declare @tr_dscr varchar(50);
	declare @d_txt varchar(50);
	declare @c_txt varchar(50);
	declare @shcd int;
	declare @keycd int;
	declare @key_20 int;
	declare @key_30 int;
	declare @key_40 int;
	declare @key_50 int;
	declare @key_60 int;
	declare @key_70 int;
	declare @key_80 int;
	declare @key_90 int;
	declare @key_100 int;
	declare @key_510 int;
	declare @key_520 int;
	declare @key_530 int;
	declare @key_540 int;
	declare @key_550 int;
	declare @key_560 int;
	declare @key_570 int;
	declare @cur_date date;
	set @cur_date = cast(sysdatetime() as date);

	set @nn_t = (select count(*) from @trans_id);
	set @nn_p = (select count(*) from @pmnt_id);
	set @nn = (select count(*) from @acnt_id);

	if @nn>0 and (@nn_p>0 or @nn_t>0)
	begin
		--Bank name and swift code from the technical table
		set @inst_name = (select top 1 institution_name from FAF.FA_MROS_INT_PERS_CHARGE where userbk_nr_home=@F_BANK_ID);
		set @swif = (select top 1 swift from FAF.FA_MROS_INT_PERS_CHARGE where userbk_nr_home=@F_BANK_ID);
		set @cntr = substring(@swif,5,2);
		if (select count(*) from FAF.FA_MROS_INT_LAND where userbk_nr=@F_BANK_ID and iso_land_cd=@cntr) = 0
			set @cntr = FAF.GoAML_get_paramV(@F_BANK_ID,'tfmc__from_country');

		--default person
		set @xml_dp = 
			(select 
				FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__gender') as "gender",
				FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name') as "first_name",
				case 
					when FAF.GoAML_get_paramS(@F_BANK_ID,44,1)='ok'
					then upper(FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__last_name'))
					else FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__last_name')
				end as "last_name",
				FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__birthdate') as "birthdate",
				FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__id_number') as "id_number",
				FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__nationality1') as "nationality1",
				case
					when FAF.GoAML_get_paramS(@F_BANK_ID,47,1)='ok'
					then
						(select
							FAF.GoAML_get_paramS(@F_BANK_ID,8,2) as "address_type",
							FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address') as "address",
							FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__city') as "city",
							FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__country_code') as "country_code"
						FOR XML PATH ('address'),type, ELEMENTS)
					else NULL
				end as "addresses",
				case
					when FAF.GoAML_get_paramS(@F_BANK_ID,48,1)='ok'
					then
						(select
							FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__type') as "type",
							FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__number') as "number",
							FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__issue_country') as "issue_country",
							FAF.GoAML_char_process(
								case
									when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
									then
										case
											when FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__type')=FAF.GoAML_get_paramS(@F_BANK_ID,6,1)
											then FAF.GoAML_get_paramS(@F_BANK_ID,6,2)
											else NULL
										end
									else NULL
								end
							,FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpi__comments')) as "comments"
						FOR XML PATH ('identification'),type, ELEMENTS)
					else NULL
				end as "identifications"
			FOR XML PATH (''),type, ELEMENTS);

		--default entity
		set @xml_de = 
			(select
				FAF.GoAML_get_paramV(@F_BANK_ID,'temc__name') as "name",
				FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_legal_form') as "incorporation_legal_form",
				case
					when FAF.GoAML_get_paramS(@F_BANK_ID,49,1)='ok'
					then
						(select
							FAF.GoAML_get_paramS(@F_BANK_ID,7,2) as "address_type",
							FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address') as "address",
							FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__city') as "city",
							FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__country_code') as "country_code"
						FOR XML PATH ('address'),type, ELEMENTS)
					else NULL
				end as "addresses",
				FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_country_code') as "incorporation_country_code",
				FAF.GoAML_get_paramV(@F_BANK_ID,'temc__tax_reg_number') as "tax_reg_number",
				case
					when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
					then
						case
							when FAF.GoAML_get_paramS(@F_BANK_ID,32,1) = FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_legal_form')
							then FAF.GoAML_get_paramS(@F_BANK_ID,32,2)
							else NULL
						end
					else NULL
				end as "comments"
			FOR XML PATH (''),type, ELEMENTS);

		--default account
		set @xml_da = 
			(select
				FAF.GoAML_char_process(@inst_name,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__institution_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__institution_name')) as "institution_name",
				FAF.GoAML_char_process(@swif,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__swift'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__swift')) as "swift",
				FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__branch') as "branch",
				case 
					when FAF.GoAML_get_paramS(@F_BANK_ID,89,1)='ok'
					then FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__account_category')
					else NULL
				end as "account_category",
				FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__account') as "account",
				FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__currency_code') as "currency_code",
				FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__iban') as "iban",
				FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__client_number') as "client_number",
				FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__account_type') as "account_type",
				(select
					@xml_dp as "t_person",
					case
						when FAF.GoAML_get_paramS(@F_BANK_ID,60,1)='ok'
						then FAF.GoAML_get_paramS(@F_BANK_ID,60,2)
						else FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__role')
					end as "role",
					case
						when FAF.GoAML_get_paramS(@F_BANK_ID,105,1)='ok'
						then FAF.GoAML_get_paramV(@F_BANK_ID,'rdr_valid_from')
						else NULL
					end as "relation_date_range/valid_from",
					case
						when FAF.GoAML_get_paramS(@F_BANK_ID,105,1)='ok'
						then FAF.GoAML_get_paramS(@F_BANK_ID,105,2)
						else NULL
					end as "relation_date_range/is_approx_from_date"
				FOR XML PATH ('account_related_person'),type, ELEMENTS) as "related_persons",
				FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__opened') as "opened",
				FAF.GoAML_get_paramS(@F_BANK_ID,16,2) as "balance",
				convert(varchar,@cur_date,23) + 'T00:00:00' as "date_balance",
				FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__status_code') as "status_code",
				FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__beneficiary_comment') as "beneficiary_comment"
			FOR XML PATH (''),type, ELEMENTS);
		set @xml_da0 = 
			(select
				FAF.GoAML_char_process(@inst_name,FAF.GoAML_get_paramV(@F_BANK_ID,'ta__institution_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'ta__institution_name')) as "institution_name",
				FAF.GoAML_char_process(@swif,FAF.GoAML_get_paramV(@F_BANK_ID,'ta__swift'),FAF.GoAML_get_paramL(@F_BANK_ID,'ta__swift')) as "swift",
				case
					when FAF.GoAML_get_paramS(@F_BANK_ID,127,1)='ok'
					then NULL
					else
						case 
							when FAF.GoAML_get_paramS(@F_BANK_ID,89,2)='ok'
							then FAF.GoAML_get_paramV(@F_BANK_ID,'ta__account_category')
							else NULL
						end
				end as "account_category",
				FAF.GoAML_get_paramV(@F_BANK_ID,'ta__account') as "account",
				case
					when FAF.GoAML_get_paramS(@F_BANK_ID,127,1)='ok' or FAF.GoAML_get_paramS(@F_BANK_ID,136,1)='ok'
					then NULL
					else
						case
							when FAF.GoAML_get_paramS(@F_BANK_ID,106,1)='ok'
							then FAF.GoAML_get_paramS(@F_BANK_ID,106,2)
							else NULL
						end
				end as "t_entity/name",
				FAF.GoAML_get_paramV(@F_BANK_ID,'ta__comments') as "comments"
			FOR XML PATH (''),type, ELEMENTS);
		
		set @ii = 1;
		while @ii<=@nn
		begin
			set @flag = NULL;
			set @flag1 = NULL;
			set @n_id = NULL;
			set @kt_id = NULL;
			set @cl_id = NULL;
			set @cl_tp = NULL;
			set @cl_nr = NULL;
			set @cl_nm = NULL;
			set @kd_tp = NULL;
			set @kd_nm = NULL;
			set @kd_nr = NULL;
			set @auf_dt0 = NULL;
			set @xml_cl = NULL;
			set @xml_cl1 = NULL;
			set @xml_cl0 = NULL;
			set @xml_kd = NULL;
			set @xml_kd5 = NULL;
			set @xml_ac = NULL;
			set @clnt_txt = NULL;
			set @adr_txt = NULL;
			set @partners = NULL;
			set @xml_ac_fnd = NULL;
			set @curr = NULL;
			set @ac_fnd = NULL;
			set @kt_nr = NULL;
			set @dt_fr0 = NULL;
			set @mm = NULL;
			set @mm0 = NULL;
			set @mmm = (select count(*) from @KD_KD);
			set @mmm0 = (select count(*) from @KD_KD0);

			select 
				@kt_id = ac.nID1, 
				@flag = case when ac.nID2 in (2,3) then 1 else 0 end, 
				@flag1 = case when ac.nID2 in (1,3) then 1 else 0 end
			from @acnt_id ac where ac.id=@ii;
			select @cl_id = kt.kd_lnr,
				@auf_dt0 = kt.aufheb_dat,
				@dt_fr0 = kt.eroeff_dat
			from FAF.FA_MROS_KT_STAMM kt where kt.kt_lnr=@kt_id and kt.userbk_nr=@F_BANK_ID;

			if FAF.GoAML_get_paramS(@F_BANK_ID,63,1) = 'ok'
				set @flag = 1;

			if @cl_id is not NULL
			begin
				if @flag>0
					begin
						select
							@cl_tp = se.kd_htyp_cd,
							@cl_nm = se.kd_bez,
							@cl_nr = se.edit_kd_nr
						from 
							FAF.FA_MROS_KD_STAMM se
						where se.kd_lnr=@cl_id and se.userbk_nr=@F_BANK_ID and se.partner_flag=0;

						if @cl_tp=2
						begin
							select 
								@st1 = FAF.GoAML_char_process(vorname,'',4000),
								@st2 = FAF.GoAML_char_process(name_1,'',4000)
							from 
								FAF.FA_MROS_KD_STAMM
							where kd_lnr=@cl_id and userbk_nr=@F_BANK_ID and partner_flag=0;
							select 
								@st3 = FAF.GoAML_char_process(vorname,'',4000),
								@st4 = FAF.GoAML_char_process(name_1,'',4000)
							from 
								FAF.FA_MROS_KD_STAMM
							where kd_lnr=@cl_id and userbk_nr=@F_BANK_ID and partner_flag=1;
									
							if len(@st3+@st4)>0
								begin
									if @st2=@st4 
										set @cl_nm = @st1 + case when len(@st1)>0 then ' and ' else '' end + @st3 + ' ' + @st4;
									else
										set @cl_nm = @st1 + ' ' + @st2 + case when len(@st1+@st2)>0 then ' and ' else '' end + @st3 + ' ' + @st4;
								end;
							else
								set @cl_nm = @st1 + ' ' + @st2;
						end;

						--entity
						if @cl_tp = 3
						begin
							set @xml_cl1 =
								(select
									FAF.GoAML_char_process(se.kd_bez,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__name'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__name')) as "name",
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,70,1)='ok'
										then FAF.GoAML_char_process(se.kd_bez,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__commercial_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__commercial_name'))
										else NULL
									end as "commercial_name",
									FAF.GoAML_char_process(lf.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_legal_form'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_legal_form')) as "incorporation_legal_form",
									FAF.GoAML_char_process(
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,71,1)='ok'
											then vsb.kd_ausw_nr
											else se.uid_nr
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_number')) as "incorporation_number",
									FAF.GoAML_char_process(se.noga_bez,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__business'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__business')) as "business",
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,43,1)='ok'
										then spp.phones
										else NULL
									end as "phones",
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,49,1)='ok'
										then
											coalesce(sea.addresses,
												(select
													FAF.GoAML_get_paramS(@F_BANK_ID,7,2) as "address_type",
													FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address') as "address",
													FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__city') as "city",
													FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__country_code') as "country_code"
												FOR XML PATH ('address'),type, ELEMENTS)
											)
										else sea.addresses
									end as "addresses",
									(select FAF.GoAML_char_process(lower(eml.email_adr),FAF.GoAML_get_paramV(@F_BANK_ID,'temc__email'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__email')) as "email"
									from FAF.FA_MROS_KD_EMAIL eml
									join FAF.FA_MROS_REFERENCE_TABLE rt
									on eml.userbk_nr=rt.userbk_nr and cast(eml.mail_art_cd as varchar)=rt.finnova_value and rt.ref_type=7 and rt.MROS_value=FAF.GoAML_get_paramS(@F_BANK_ID,121,2)
									where eml.kd_lnr=se.kd_lnr and eml.userbk_nr=se.userbk_nr and left(eml.email_adr,1)<>'@'
									FOR XML PATH (''),type, ELEMENTS) as "emails",
									(select FAF.GoAML_char_process(ema.email_adr,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__url'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__url')) as "url"
									from FAF.FA_MROS_KD_EMAIL ema 
									where ema.kd_lnr=se.kd_lnr and ema.userbk_nr=se.userbk_nr and ema.mail_art_cd=FAF.GoAML_get_paramS(@F_BANK_ID,122,2)
									FOR XML PATH (''),type, ELEMENTS) as "urls",
									FAF.GoAML_char_process(se.geburt_ort,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_state'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_state')) as "incorporation_state",
									FAF.GoAML_char_process(cntr1.iso_land_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_country_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_country_code')) as "incorporation_country_code",
									FAF.GoAML_char_process(
										case 
											when se.geburt_dat is NULL or se.geburt_dat < convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or cast(se.geburt_dat as date)>@cur_date
											then 
												case 
													when se.geburt_jahr is NULL or se.geburt_jahr<cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) or se.geburt_jahr>year(@cur_date)
													then NULL
													else cast(se.geburt_jahr as varchar) + '-01-01T00:00:00' 
												end 
											else left(convert(varchar,cast(se.geburt_dat as datetime),126),19)
										end 
									,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_date'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_date')) as "incorporation_date",
									FAF.GoAML_char_process(
										case 
											when FAF.GoAML_get_paramS(@F_BANK_ID,28,1)='ok' 
												and ((se.tod_dat >= convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) and cast(se.tod_dat as date)<=@cur_date) or (se.tod_jahr>=cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) and se.tod_jahr<=year(@cur_date)))
											then FAF.GoAML_get_paramS(@F_BANK_ID,28,2) 
											else NULL
										end 
									,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__business_closed'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__business_closed')) as "business_closed",
									FAF.GoAML_char_process(
										case 
											when se.tod_dat is NULL or se.tod_dat < convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or cast(se.tod_dat as date)>@cur_date
											then 
												case 
													when se.tod_jahr is NULL or se.tod_jahr<cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) or se.tod_jahr>year(@cur_date)
													then NULL
													else cast(se.tod_jahr as varchar) + '-01-01T00:00:00' 
												end 
											else left(convert(varchar,cast(se.tod_dat as datetime),126),19)
										end 
									,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__date_business_closed'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__date_business_closed')) as "date_business_closed",
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,67,1)='ok' and len(se.uid_nr)>1
										then FAF.GoAML_char_process(se.uid_nr,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__tax_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__tax_number'))
										else NULL
									end as "tax_number",
									FAF.GoAML_char_process(
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,104,1)='ok'
											then NULL
											else tax.MROS_value
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__tax_reg_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__tax_reg_number')) as "tax_reg_number",
									FAF.GoAML_char_process_WR(
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
											then
												'Client number: ' + se.edit_kd_nr + 
												case
													when cntr1.iso_land_cd is NULL and FAF.GoAML_char_process(se.land_lnr_nat_bez,NULL,0) is not NULL and FAF.GoAML_char_process(se.iso_land_code_nat,NULL,0) is not NULL
													then '; ' + char(13) + char(10) + 'Actual country is ' + FAF.GoAML_char_process(se.land_lnr_nat_bez,'',0) + ' (' + FAF.GoAML_char_process(se.iso_land_code_nat,'',0) + ')'
													else ''
												end + 
												case 
													when FAF.GoAML_get_paramS(@F_BANK_ID,32,1)=FAF.GoAML_char_process(lf.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_legal_form'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_legal_form'))
													then '; ' + char(13) + char(10) + coalesce(FAF.GoAML_char_process(lf.comments,FAF.GoAML_get_paramS(@F_BANK_ID,32,2),0),'')
													else ''
												end
											else NULL
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__comments')) as "comments"
								from 
									FAF.FA_MROS_KD_STAMM se
								left join 
									FAF.FA_MROS_KD_ADR_XML sea
								on sea.kd_lnr=se.kd_lnr and sea.userbk_nr=se.userbk_nr
								left join
									FAF.FA_MROS_KD_EMAIL_XML spp
								on spp.kd_lnr=se.kd_lnr and spp.userbk_nr=se.userbk_nr
								left join
									FAF.FA_MROS_INT_LAND cntr1
								on cntr1.iso_land_cd=se.iso_land_code_nat and cntr1.userbk_nr=se.userbk_nr
								left join
									FAF.FA_MROS_REFERENCE_TABLE lf
								on se.userbk_nr=lf.userbk_nr and cast(se.kd_rstat_cd as varchar)=lf.finnova_value and lf.ref_type=8
								left join
									FAF.FA_MROS_REFERENCE_TABLE tax
								on se.userbk_nr=tax.userbk_nr and se.KD_SITZ_GSCH_CD=tax.finnova_value and tax.ref_type=3
								left join
									(select userbk_nr,kd_lnr,kd_ausw_nr,
										row_number() over (partition by userbk_nr,kd_lnr order by ausstell_dat desc) as rn
									from FAF.FA_MROS_KD_VSB where kd_ausw_nr is not NULL and cast(iddoc_cd as varchar)=FAF.GoAML_get_paramS(@F_BANK_ID,71,2)) vsb
								on se.userbk_nr=vsb.userbk_nr and se.kd_lnr=vsb.kd_lnr and vsb.rn=1
								where se.kd_lnr=@cl_id and se.userbk_nr=@F_BANK_ID and se.partner_flag=0
								FOR XML PATH (''),type, ELEMENTS);
							
							if FAF.GoAML_get_paramS(@F_BANK_ID,136,1)='ok' and @xml_cl1 is not NULL
								begin
									set @xml_kd5 =
										(select
											FAF.GoAML_get_paramS(@F_BANK_ID,136,2) as "account_entity_relation",
											@xml_cl1 as "entity",
											FAF.GoAML_char_process(
												convert(varchar,cast(@dt_fr0 as datetime),126)
											,FAF.GoAML_get_paramV(@F_BANK_ID,'rdr_valid_from'),FAF.GoAML_get_paramL(@F_BANK_ID,'rdr_valid_from')) as "relation_date_range/valid_from",
											FAF.GoAML_char_process(
												convert(varchar,cast(@auf_dt0 as datetime),126)
											,FAF.GoAML_get_paramV(@F_BANK_ID,'rdr_valid_to'),FAF.GoAML_get_paramL(@F_BANK_ID,'rdr_valid_to')) as "relation_date_range/valid_to"
										FOR XML PATH ('account_related_entity'),type, ELEMENTS);
								end;	
							else
								begin
									if @xml_cl1 is NULL
										set @xml_cl0 = (select @xml_de as "t_entity" FOR XML PATH (''),type, ELEMENTS);
									else
										set @xml_cl0 = (select @xml_cl1 as "t_entity" FOR XML PATH (''),type, ELEMENTS);
								end;
						end;

						insert into @KD_KD0 select @cl_id as nID;

						if @cl_tp = 1
						begin
							insert into @KD_KD 
							select @cl_id as nID,
								case 
									when FAF.GoAML_get_paramS(@F_BANK_ID,26,1)='ok' 
									then FAF.GoAML_get_paramS(@F_BANK_ID,26,2) 
									else FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__role') 
								end as rl,
								NULL as cmm,NULL as ptn,@cl_tp as kdtp,@cl_nr as kdnr,@cl_nm as kdnm,NULL as dt1,NULL as dt2,NULL as e_rl,NULL as e_cmm;
						end;

						--doppelpersons of initial client
						if @cl_tp = 2
						begin
							insert into @KD_KD
							select kdkd.kd_lnr_zuw as nID,
								case 
									when FAF.GoAML_get_paramS(@F_BANK_ID,26,1)='ok' 
									then FAF.GoAML_get_paramS(@F_BANK_ID,26,2) 
									else FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__role') 
								end as rl,
								NULL as cmm,@cl_nr as ptn,kd.kd_htyp_cd as kdtp,kd.edit_kd_nr as kdnr,
								case
									when kd.kd_htyp_cd=3 then kd.kd_bez
									when kd.kd_htyp_cd=2 and kd.name_1=kd1.name_1 
									then isnull(kd.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' and ' + isnull(kd1.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd.name_1,'')
									when kd.kd_htyp_cd=2 and kd1.userbk_nr is not NULL
									then isnull(kd.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd.name_1,'') + ' and ' + isnull(kd1.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd1.name_1,'')
									else isnull(kd.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd.name_1,'')
								end as kdnm,
								NULL as dt1,NULL as dt2,NULL as e_rl,NULL as e_cmm
							from
								(select kdkd1.kd_lnr_zuw 
								from 
									FAF.FA_MROS_KD_KD kdkd1
								join
									FAF.FA_MROS_REFERENCE_TABLE rt_1
								on cast(kdkd1.kd_kd_verb_cd as varchar)=rt_1.Finnova_value and rt_1.userbk_nr=kdkd1.userbk_nr and rt_1.ref_type=14 and rt_1.activ_MROS=1 and rt_1.special_mark='DOPPELPERSON'
								where kdkd1.userbk_nr=@F_BANK_ID and kdkd1.kd_lnr=@cl_id and kdkd1.kd_lnr_zuw<>@cl_id
									and (kdkd1.valid_bis_dat is NULL or kdkd1.valid_bis_dat>=isnull(@auf_dt0,@cur_date))
									and (kdkd1.valid_von_dat is NULL or kdkd1.valid_von_dat<=isnull(@auf_dt0,@cur_date))
								union
								select kdkd2.kd_lnr as kd_lnr_zuw
								from 
									FAF.FA_MROS_KD_KD kdkd2
								join
									FAF.FA_MROS_REFERENCE_TABLE rt_2
								on cast(kdkd2.kd_kd_verb_cd as varchar)=rt_2.Finnova_value and rt_2.userbk_nr=kdkd2.userbk_nr and rt_2.ref_type=15 and rt_2.activ_MROS=1 and rt_2.special_mark='DOPPELPERSON'
								where kdkd2.userbk_nr=@F_BANK_ID and kdkd2.kd_lnr_zuw=@cl_id and kdkd2.kd_lnr<>@cl_id
									and (kdkd2.valid_bis_dat is NULL or kdkd2.valid_bis_dat>=isnull(@auf_dt0,@cur_date))
									and (kdkd2.valid_von_dat is NULL or kdkd2.valid_von_dat<=isnull(@auf_dt0,@cur_date))) kdkd
							join
								FAF.FA_MROS_KD_STAMM kd
							on kd.userbk_nr=@F_BANK_ID and kd.kd_lnr=kdkd.kd_lnr_zuw and kd.partner_flag=0
							left join
								FAF.FA_MROS_KD_STAMM kd1
							on kd1.userbk_nr=@F_BANK_ID and kd1.kd_lnr=kdkd.kd_lnr_zuw and kd1.partner_flag=1;

							set @mm = (select count(*) from @KD_KD) - isnull(@mmm,0);
							if @mm>0
							begin
								insert into @KD_KD0 select kdkd.nID from @KD_KD kdkd where kdkd.id>isnull(@mmm,0);

								set @partners = 
									(select STUFF(
										(select ', ' + coalesce(kdkd.kdnr,'') from @KD_KD kdkd where kdkd.id>isnull(@mmm,0) FOR XML PATH(''))
									, 1, LEN(', '), ''));
							end;

							set @xml_cl1 =
								(select
									FAF.GoAML_char_process(@cl_nm,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__name'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__name')) as "name",
									FAF.GoAML_char_process(
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,78,1) = 'ok'
											then lf.MROS_value
											else FAF.GoAML_get_paramS(@F_BANK_ID,54,1)
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_legal_form'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_legal_form')) as "incorporation_legal_form",
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,43,1)='ok'
										then spp.phones
										else NULL
									end as "phones",
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,49,1)='ok'
										then
											coalesce(sea.addresses,
												(select
													FAF.GoAML_get_paramS(@F_BANK_ID,9,2) as "address_type",
													FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address') as "address",
													FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__city') as "city",
													FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__country_code') as "country_code"
												FOR XML PATH ('address'),type, ELEMENTS)
											)
										else sea.addresses
									end as "addresses",
									FAF.GoAML_char_process(cntr1.iso_land_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_country_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_country_code')) as "incorporation_country_code",
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,67,1)='ok' and len(se.uid_nr)>1
										then FAF.GoAML_char_process(se.uid_nr,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__tax_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__tax_number'))
										else NULL
									end as "tax_number",
									FAF.GoAML_char_process(FAF.GoAML_get_paramS(@F_BANK_ID,55,1),FAF.GoAML_get_paramV(@F_BANK_ID,'temc__tax_reg_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__tax_reg_number')) as "tax_reg_number",
									FAF.GoAML_char_process_WR(
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
											then
												'Client number: ' + se.edit_kd_nr + 
												case 
													when @partners is not NULL 
													then char(13) + char(10) + 'It is the partnership of the clients: ' + ltrim(@partners)
													else ''
												end +
												case 
													when FAF.GoAML_get_paramS(@F_BANK_ID,78,1) = 'ok' and FAF.GoAML_get_paramS(@F_BANK_ID,32,1) = FAF.GoAML_char_process(lf.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_legal_form'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_legal_form'))
													then char(13) + char(10) + coalesce(FAF.GoAML_char_process(lf.comments,FAF.GoAML_get_paramS(@F_BANK_ID,32,2),0),'')
													else ''
												end + 
												case
													when cntr1.iso_land_cd is NULL and FAF.GoAML_char_process(se.land_lnr_nat_bez,NULL,0) is not NULL and FAF.GoAML_char_process(se.iso_land_code_nat,NULL,0) is not NULL
													then char(13) + char(10) + 'Actual country is ' + FAF.GoAML_char_process(se.land_lnr_nat_bez,'',0) + ' (' + FAF.GoAML_char_process(se.iso_land_code_nat,'',0) + ')'
													else ''
												end
											else NULL
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__comments')) as "comments"
								from 
									FAF.FA_MROS_KD_STAMM se
								left join
									FAF.FA_MROS_KD_EMAIL_XML spp
								on spp.kd_lnr=se.kd_lnr and spp.userbk_nr=se.userbk_nr
								left join 
									FAF.FA_MROS_KD_ADR_XML sea
								on sea.kd_lnr=se.kd_lnr and sea.userbk_nr=se.userbk_nr
								left join
									FAF.FA_MROS_INT_LAND cntr1
								on cntr1.iso_land_cd=se.iso_land_code_nat and cntr1.userbk_nr=se.userbk_nr
								left join
									FAF.FA_MROS_REFERENCE_TABLE lf
								on se.userbk_nr=lf.userbk_nr and cast(se.kd_rstat_cd as varchar)=lf.finnova_value and lf.ref_type=8
								where se.kd_lnr=@cl_id and se.userbk_nr=@F_BANK_ID and se.partner_flag=0
								FOR XML PATH (''),type, ELEMENTS);

							if FAF.GoAML_get_paramS(@F_BANK_ID,136,1)='ok' and @xml_cl1 is not NULL
								begin
									set @xml_kd5 =
										(select
											FAF.GoAML_get_paramS(@F_BANK_ID,136,2) as "account_entity_relation",
											@xml_cl1 as "entity",
											FAF.GoAML_char_process(
												convert(varchar,cast(@dt_fr0 as datetime),126)
											,FAF.GoAML_get_paramV(@F_BANK_ID,'rdr_valid_from'),FAF.GoAML_get_paramL(@F_BANK_ID,'rdr_valid_from')) as "relation_date_range/valid_from",
											FAF.GoAML_char_process(
												convert(varchar,cast(@auf_dt0 as datetime),126)
											,FAF.GoAML_get_paramV(@F_BANK_ID,'rdr_valid_to'),FAF.GoAML_get_paramL(@F_BANK_ID,'rdr_valid_to')) as "relation_date_range/valid_to"
										FOR XML PATH ('account_related_entity'),type, ELEMENTS);
								end;
							else
								begin
									if @xml_cl1 is NULL
										set @xml_cl0 = (select @xml_de as "t_entity" FOR XML PATH (''),type, ELEMENTS);
									else
										set @xml_cl0 = (select @xml_cl1 as "t_entity" FOR XML PATH (''),type, ELEMENTS);
								end;
						end;

						set @mm0 = (select count(*) from @KD_KD) - isnull(@mmm,0);
						set @mm = (select count(*) from @KD_KD0) - isnull(@mmm0,0);
						set @jj = 1;
						while @jj<=@mm
						begin
							set @n_id = (select nID from @KD_KD0 where id=@jj + isnull(@mmm0,0));
							if @n_id is not NULL
							begin
								if FAF.GoAML_get_paramS(@F_BANK_ID,77,1) = 'ok'
									begin
										insert into @KD_KD
										select 
											kdkd.kd_lnr_zuw as nID,kdkd.mros_value as rl,kdkd.comments as cmm,NULL as ptn,kd.kd_htyp_cd as kdtp,kd.edit_kd_nr as kdnr,
											case
												when kd.kd_htyp_cd=3 then kd.kd_bez
												when kd.kd_htyp_cd=2 and kd.name_1=kd1.name_1 
												then isnull(kd.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' and ' + isnull(kd1.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd.name_1,'')
												when kd.kd_htyp_cd=2 and kd1.userbk_nr is not NULL
												then isnull(kd.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd.name_1,'') + ' and ' + isnull(kd1.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd1.name_1,'')
												else isnull(kd.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd.name_1,'')
											end as kdnm,
											kdkd.dt1 as dt1,kdkd.dt2 as dt2,kdkd.e_rl,kdkd.e_cmm
										from
											(select kdkd1.kd_lnr_zuw,rt1.mros_value,rt1_e.mros_value as e_rl,
												case when FAF.GoAML_char_process(rt1.special_mark,NULL,4000) is NULL then rt1.comments else rt1.special_mark end as comments,
												case when FAF.GoAML_char_process(rt1_e.special_mark,NULL,4000) is NULL then rt1_e.comments else rt1_e.special_mark end as e_cmm,
												kdkd1.valid_von_dat as dt1,kdkd1.valid_bis_dat as dt2,
												isnull(rt1.activ_MROS,1) as p_flag,isnull(rt1_e.activ_MROS,1) as e_flag
											from 
												FAF.FA_MROS_KD_KD kdkd1
											left join
												FAF.FA_MROS_REFERENCE_TABLE rt1
											on cast(kdkd1.kd_kd_verb_cd as varchar)=rt1.Finnova_value and rt1.userbk_nr=kdkd1.userbk_nr and rt1.ref_type=14
											left join
												FAF.FA_MROS_REFERENCE_TABLE rt1_e
											on cast(kdkd1.kd_kd_verb_cd as varchar)=rt1_e.Finnova_value and rt1_e.userbk_nr=kdkd1.userbk_nr and rt1_e.ref_type=27
											where kdkd1.userbk_nr=@F_BANK_ID and kdkd1.kd_lnr=@n_id and kdkd1.kd_lnr_zuw<>@n_id
												and (kdkd1.valid_bis_dat is NULL or kdkd1.valid_bis_dat>=isnull(@auf_dt0,@cur_date))
												and (kdkd1.valid_von_dat is NULL or kdkd1.valid_von_dat<=isnull(@auf_dt0,@cur_date))
											union
											select kdkd2.kd_lnr as kd_lnr_zuw,rt2.mros_value,rt2_e.mros_value as e_rl,
												case when FAF.GoAML_char_process(rt2.special_mark,NULL,4000) is NULL then rt2.comments else rt2.special_mark end as comments,
												case when FAF.GoAML_char_process(rt2_e.special_mark,NULL,4000) is NULL then rt2_e.comments else rt2_e.special_mark end as e_cmm,
												kdkd2.valid_von_dat as dt1,kdkd2.valid_bis_dat as dt2,
												isnull(rt2.activ_MROS,1) as p_flag,isnull(rt2_e.activ_MROS,1) as e_flag
											from 
												FAF.FA_MROS_KD_KD kdkd2
											left join
												FAF.FA_MROS_REFERENCE_TABLE rt2
											on cast(kdkd2.kd_kd_verb_cd as varchar)=rt2.Finnova_value and rt2.userbk_nr=kdkd2.userbk_nr and rt2.ref_type=15
											left join
												FAF.FA_MROS_REFERENCE_TABLE rt2_e
											on cast(kdkd2.kd_kd_verb_cd as varchar)=rt2_e.Finnova_value and rt2_e.userbk_nr=kdkd2.userbk_nr and rt2_e.ref_type=28
											where kdkd2.userbk_nr=@F_BANK_ID and kdkd2.kd_lnr_zuw=@n_id and kdkd2.kd_lnr<>@n_id
												and (kdkd2.valid_bis_dat is NULL or kdkd2.valid_bis_dat>=isnull(@auf_dt0,@cur_date))
												and (kdkd2.valid_von_dat is NULL or kdkd2.valid_von_dat<=isnull(@auf_dt0,@cur_date))
											union
											select vtz.kd_lnr as kd_lnr_zuw,rt3.mros_value,rt3_e.mros_value as e_rl,
												case when FAF.GoAML_char_process(rt3.special_mark,NULL,4000) is NULL then rt3.comments else rt3.special_mark end as comments,
												case when FAF.GoAML_char_process(rt3_e.special_mark,NULL,4000) is NULL then rt3_e.comments else rt3_e.special_mark end as e_cmm,
												case when vtz.beg_dat>=vts.beginn_dat or vts.beginn_dat is NULL then vtz.beg_dat else vts.beginn_dat end as dt1,
												case when vtz.aufheb_dat<=vts.ende_dat or vts.ende_dat is NULL then vtz.aufheb_dat else vts.ende_dat end as dt2,
												isnull(rt3.activ_MROS,1) as p_flag,isnull(rt3_e.activ_MROS,1) as e_flag
											from 
												FAF.FA_MROS_VT_STAMM vts
											join
												FAF.FA_MROS_VT_ZUORD vtz
											on vts.userbk_nr=vtz.userbk_nr and vts.vt_lnr=vtz.vt_lnr
											left join
												FAF.FA_MROS_REFERENCE_TABLE rt3
											on cast(vts.vt_typ_cd as varchar)+'_'+cast(vtz.sb_sign_recht_cd as varchar)=rt3.Finnova_value and rt3.userbk_nr=vts.userbk_nr and rt3.ref_type=18
											left join
												FAF.FA_MROS_REFERENCE_TABLE rt3_e
											on cast(vts.vt_typ_cd as varchar)+'_'+cast(vtz.sb_sign_recht_cd as varchar)=rt3_e.Finnova_value and rt3_e.userbk_nr=vts.userbk_nr and rt3_e.ref_type=29
											where vts.userbk_nr=@F_BANK_ID and vts.kd_lnr=@n_id and vtz.kd_lnr<>@n_id
												and (vts.kt_lnr is NULL or @jj=1 and vts.kt_lnr=@kt_id)
												and (vts.ende_dat is NULL or vts.ende_dat>=isnull(@auf_dt0,@cur_date))
												and (vts.beginn_dat is NULL or vts.beginn_dat<=isnull(@auf_dt0,@cur_date))
												and (vtz.beg_dat is NULL or vtz.beg_dat<=isnull(@auf_dt0,@cur_date))
												and (vtz.aufheb_dat is NULL or vtz.aufheb_dat>=isnull(@auf_dt0,@cur_date))
											union
											select vts.kd_lnr as kd_lnr_zuw,rt4.mros_value,rt4_e.mros_value as e_rl,
												case when FAF.GoAML_char_process(rt4.special_mark,NULL,4000) is NULL then rt4.comments else rt4.special_mark end as comments,
												case when FAF.GoAML_char_process(rt4_e.special_mark,NULL,4000) is NULL then rt4_e.comments else rt4_e.special_mark end as e_cmm,
												case when vtz.beg_dat>=vts.beginn_dat or vts.beginn_dat is NULL then vtz.beg_dat else vts.beginn_dat end as dt1,
												case when vtz.aufheb_dat<=vts.ende_dat or vts.ende_dat is NULL then vtz.aufheb_dat else vts.ende_dat end as dt2,
												isnull(rt4.activ_MROS,1) as p_flag,isnull(rt4_e.activ_MROS,1) as e_flag
											from 
												FAF.FA_MROS_VT_STAMM vts
											join
												FAF.FA_MROS_VT_ZUORD vtz
											on vts.userbk_nr=vtz.userbk_nr and vts.vt_lnr=vtz.vt_lnr
											left join
												FAF.FA_MROS_REFERENCE_TABLE rt4
											on cast(vts.vt_typ_cd as varchar)+'_'+cast(vtz.sb_sign_recht_cd as varchar)=rt4.Finnova_value and rt4.userbk_nr=vts.userbk_nr and rt4.ref_type=19
											left join
												FAF.FA_MROS_REFERENCE_TABLE rt4_e
											on cast(vts.vt_typ_cd as varchar)+'_'+cast(vtz.sb_sign_recht_cd as varchar)=rt4_e.Finnova_value and rt4_e.userbk_nr=vts.userbk_nr and rt4_e.ref_type=30
											where vtz.userbk_nr=@F_BANK_ID and vtz.kd_lnr=@n_id and vts.kd_lnr<>@n_id
												and (vts.ende_dat is NULL or vts.ende_dat>=isnull(@auf_dt0,@cur_date))
												and (vts.beginn_dat is NULL or vts.beginn_dat<=isnull(@auf_dt0,@cur_date))
												and (vtz.beg_dat is NULL or vtz.beg_dat<=isnull(@auf_dt0,@cur_date))
												and (vtz.aufheb_dat is NULL or vtz.aufheb_dat>=isnull(@auf_dt0,@cur_date))
											) kdkd
										join
											FAF.FA_MROS_KD_STAMM kd
										on kd.userbk_nr=@F_BANK_ID and kd.kd_lnr=kdkd.kd_lnr_zuw and kd.partner_flag=0
										left join
											FAF.FA_MROS_KD_STAMM kd1
										on kd1.userbk_nr=@F_BANK_ID and kd1.kd_lnr=kdkd.kd_lnr_zuw and kd1.partner_flag=1
										where kdkd.kd_lnr_zuw not in (select kd0.nID from @KD_KD0 kd0 where kd0.id>isnull(@mmm0,0))
											and (kd.kd_htyp_cd=1 and kdkd.p_flag=1 or kd.kd_htyp_cd>1 and kdkd.e_flag=1);
									end;
								else
									begin
										insert into @KD_KD
										select 
											kdkd.kd_lnr_zuw as nID,kdkd.mros_value as rl,kdkd.comments as cmm,NULL as ptn,kd.kd_htyp_cd as kdtp,kd.edit_kd_nr as kdnr,
											case
												when kd.kd_htyp_cd=3 then kd.kd_bez
												when kd.kd_htyp_cd=2 and kd.name_1=kd1.name_1 
												then isnull(kd.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' and ' + isnull(kd1.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd.name_1,'')
												when kd.kd_htyp_cd=2 and kd1.userbk_nr is not NULL
												then isnull(kd.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd.name_1,'') + ' and ' + isnull(kd1.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd1.name_1,'')
												else isnull(kd.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd.name_1,'')
											end as kdnm,
											kdkd.dt1 as dt1,kdkd.dt2 as dt2,kdkd.e_rl,kdkd.e_cmm
										from
											(select kdkd1.kd_lnr_zuw,rt1.mros_value,rt1_e.mros_value as e_rl,
												case when FAF.GoAML_char_process(rt1.special_mark,NULL,4000) is NULL then rt1.comments else rt1.special_mark end as comments,
												case when FAF.GoAML_char_process(rt1_e.special_mark,NULL,4000) is NULL then rt1_e.comments else rt1_e.special_mark end as e_cmm,
												kdkd1.valid_von_dat as dt1,kdkd1.valid_bis_dat as dt2,
												isnull(rt1.activ_MROS,1) as p_flag,isnull(rt1_e.activ_MROS,1) as e_flag
											from 
												FAF.FA_MROS_KD_KD kdkd1
											left join
												FAF.FA_MROS_REFERENCE_TABLE rt1
											on cast(kdkd1.kd_kd_verb_cd as varchar)=rt1.Finnova_value and rt1.userbk_nr=kdkd1.userbk_nr and rt1.ref_type=14
											left join
												FAF.FA_MROS_REFERENCE_TABLE rt1_e
											on cast(kdkd1.kd_kd_verb_cd as varchar)=rt1_e.Finnova_value and rt1_e.userbk_nr=kdkd1.userbk_nr and rt1_e.ref_type=27
											where kdkd1.userbk_nr=@F_BANK_ID and kdkd1.kd_lnr=@n_id and kdkd1.kd_lnr_zuw<>@n_id
												and (kdkd1.valid_bis_dat is NULL or kdkd1.valid_bis_dat>=isnull(@auf_dt0,@cur_date))
												and (kdkd1.valid_von_dat is NULL or kdkd1.valid_von_dat<=isnull(@auf_dt0,@cur_date))
											union
											select kdkd2.kd_lnr as kd_lnr_zuw,rt2.mros_value,rt2_e.mros_value as e_rl,
												case when FAF.GoAML_char_process(rt2.special_mark,NULL,4000) is NULL then rt2.comments else rt2.special_mark end as comments,
												case when FAF.GoAML_char_process(rt2_e.special_mark,NULL,4000) is NULL then rt2_e.comments else rt2_e.special_mark end as e_cmm,
												kdkd2.valid_von_dat as dt1,kdkd2.valid_bis_dat as dt2,
												isnull(rt2.activ_MROS,1) as p_flag,isnull(rt2_e.activ_MROS,1) as e_flag
											from 
												FAF.FA_MROS_KD_KD kdkd2
											left join
												FAF.FA_MROS_REFERENCE_TABLE rt2
											on cast(kdkd2.kd_kd_verb_cd as varchar)=rt2.Finnova_value and rt2.userbk_nr=kdkd2.userbk_nr and rt2.ref_type=15
											left join
												FAF.FA_MROS_REFERENCE_TABLE rt2_e
											on cast(kdkd2.kd_kd_verb_cd as varchar)=rt2_e.Finnova_value and rt2_e.userbk_nr=kdkd2.userbk_nr and rt2_e.ref_type=28
											where kdkd2.userbk_nr=@F_BANK_ID and kdkd2.kd_lnr_zuw=@n_id and kdkd2.kd_lnr<>@n_id
												and (kdkd2.valid_bis_dat is NULL or kdkd2.valid_bis_dat>=isnull(@auf_dt0,@cur_date))
												and (kdkd2.valid_von_dat is NULL or kdkd2.valid_von_dat<=isnull(@auf_dt0,@cur_date))
											union
											select vtz.kd_lnr as kd_lnr_zuw,rt3.mros_value,rt3_e.mros_value as e_rl,
												case when FAF.GoAML_char_process(rt3.special_mark,NULL,4000) is NULL then rt3.comments else rt3.special_mark end as comments,
												case when FAF.GoAML_char_process(rt3_e.special_mark,NULL,4000) is NULL then rt3_e.comments else rt3_e.special_mark end as e_cmm,
												case when vtz.beg_dat>=vts.beginn_dat or vts.beginn_dat is NULL then vtz.beg_dat else vts.beginn_dat end as dt1,
												case when vtz.aufheb_dat<=vts.ende_dat or vts.ende_dat is NULL then vtz.aufheb_dat else vts.ende_dat end as dt2,
												isnull(rt3.activ_MROS,1) as p_flag,isnull(rt3_e.activ_MROS,1) as e_flag
											from 
												FAF.FA_MROS_VT_STAMM vts
											join
												FAF.FA_MROS_VT_ZUORD vtz
											on vts.userbk_nr=vtz.userbk_nr and vts.vt_lnr=vtz.vt_lnr
											left join
												FAF.FA_MROS_REFERENCE_TABLE rt3
											on cast(vts.vt_typ_cd as varchar)=rt3.Finnova_value and rt3.userbk_nr=vts.userbk_nr and rt3.ref_type=18
											left join
												FAF.FA_MROS_REFERENCE_TABLE rt3_e
											on cast(vts.vt_typ_cd as varchar)=rt3_e.Finnova_value and rt3_e.userbk_nr=vts.userbk_nr and rt3_e.ref_type=29
											where vts.userbk_nr=@F_BANK_ID and vts.kd_lnr=@n_id and vtz.kd_lnr<>@n_id
												and (vts.kt_lnr is NULL or @jj=1 and vts.kt_lnr=@kt_id)
												and (vts.ende_dat is NULL or vts.ende_dat>=isnull(@auf_dt0,@cur_date))
												and (vts.beginn_dat is NULL or vts.beginn_dat<=isnull(@auf_dt0,@cur_date))
												and (vtz.beg_dat is NULL or vtz.beg_dat<=isnull(@auf_dt0,@cur_date))
												and (vtz.aufheb_dat is NULL or vtz.aufheb_dat>=isnull(@auf_dt0,@cur_date))
											union
											select vts.kd_lnr as kd_lnr_zuw,rt4.mros_value,rt4_e.mros_value as e_rl,
												case when FAF.GoAML_char_process(rt4.special_mark,NULL,4000) is NULL then rt4.comments else rt4.special_mark end as comments,
												case when FAF.GoAML_char_process(rt4_e.special_mark,NULL,4000) is NULL then rt4_e.comments else rt4_e.special_mark end as e_cmm,
												case when vtz.beg_dat>=vts.beginn_dat or vts.beginn_dat is NULL then vtz.beg_dat else vts.beginn_dat end as dt1,
												case when vtz.aufheb_dat<=vts.ende_dat or vts.ende_dat is NULL then vtz.aufheb_dat else vts.ende_dat end as dt2,
												isnull(rt4.activ_MROS,1) as p_flag,isnull(rt4_e.activ_MROS,1) as e_flag
											from 
												FAF.FA_MROS_VT_STAMM vts
											join
												FAF.FA_MROS_VT_ZUORD vtz
											on vts.userbk_nr=vtz.userbk_nr and vts.vt_lnr=vtz.vt_lnr
											left join
												FAF.FA_MROS_REFERENCE_TABLE rt4
											on cast(vts.vt_typ_cd as varchar)=rt4.Finnova_value and rt4.userbk_nr=vts.userbk_nr and rt4.ref_type=19
											left join
												FAF.FA_MROS_REFERENCE_TABLE rt4_e
											on cast(vts.vt_typ_cd as varchar)=rt4_e.Finnova_value and rt4_e.userbk_nr=vts.userbk_nr and rt4_e.ref_type=30
											where vtz.userbk_nr=@F_BANK_ID and vtz.kd_lnr=@n_id and vts.kd_lnr<>@n_id
												and (vts.ende_dat is NULL or vts.ende_dat>=isnull(@auf_dt0,@cur_date))
												and (vts.beginn_dat is NULL or vts.beginn_dat<=isnull(@auf_dt0,@cur_date))
												and (vtz.beg_dat is NULL or vtz.beg_dat<=isnull(@auf_dt0,@cur_date))
												and (vtz.aufheb_dat is NULL or vtz.aufheb_dat>=isnull(@auf_dt0,@cur_date))
											) kdkd
										join
											FAF.FA_MROS_KD_STAMM kd
										on kd.userbk_nr=@F_BANK_ID and kd.kd_lnr=kdkd.kd_lnr_zuw and kd.partner_flag=0
										left join
											FAF.FA_MROS_KD_STAMM kd1
										on kd1.userbk_nr=@F_BANK_ID and kd1.kd_lnr=kdkd.kd_lnr_zuw and kd1.partner_flag=1
										where kdkd.kd_lnr_zuw not in (select kd0.nID from @KD_KD0 kd0 where kd0.id>isnull(@mmm0,0))
											and (kd.kd_htyp_cd=1 and kdkd.p_flag=1 or kd.kd_htyp_cd>1 and kdkd.e_flag=1);
									end;
							end;

							set @jj = @jj + 1;
						end;
						
						set @mm = (select count(*) from @KD_KD) - isnull(@mmm,0);
						if @mm>@mm0
						begin
							set @jj = @mm0 + 1;
							while @jj<=@mm
							begin
								set @n_id = NULL;
								set @rol = NULL;
								set @comm = NULL;
								set @kd_nr = NULL;
								set @kd_tp = NULL;
								set @partners = NULL;
								set @dt_fr = NULL;
								set @dt_to = NULL;
								set @e_rol = NULL;
								set @e_comm = NULL;

								select @n_id = nID,@rol=rl,@comm=cmm,@kd_nr=kdnr,@kd_tp=kdtp,@dt_fr=dt1,@dt_to=dt2,@e_rol=e_rl,@e_comm=e_cmm from @KD_KD where id=@jj + isnull(@mmm,0);
								if @kd_tp=2
								begin
									--doppelpersons of clients linked to the initial client
									set @mm0 = (select count(*) from @KD_KD) - isnull(@mmm,0);
									insert into @KD_KD
									select 
										kdkd.kd_lnr_zuw as nID,@rol as rl,@comm as cmm,@kd_nr as ptn,kd.kd_htyp_cd as kdtp,kd.edit_kd_nr as kdnr,
										case
											when kd.kd_htyp_cd=3 then kd.kd_bez
											when kd.kd_htyp_cd=2 and kd.name_1=kd1.name_1 
											then isnull(kd.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' and ' + isnull(kd1.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd.name_1,'')
											when kd.kd_htyp_cd=2 and kd1.userbk_nr is not NULL
											then isnull(kd.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd.name_1,'') + ' and ' + isnull(kd1.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd1.name_1,'')
											else isnull(kd.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd.name_1,'')
										end as kdnm,
										@dt_fr as dt1,@dt_to as dt2,@e_rol as e_rl,@e_comm as e_cmm
									from
										(select kdkd1.kd_lnr_zuw 
										from 
											FAF.FA_MROS_KD_KD kdkd1
										join
											FAF.FA_MROS_REFERENCE_TABLE rt_1
										on cast(kdkd1.kd_kd_verb_cd as varchar)=rt_1.Finnova_value and rt_1.userbk_nr=kdkd1.userbk_nr and rt_1.ref_type=14 and rt_1.activ_MROS=1 and rt_1.special_mark='DOPPELPERSON'
										where kdkd1.userbk_nr=@F_BANK_ID and kdkd1.kd_lnr=@n_id and kdkd1.kd_lnr_zuw<>@n_id
											and (kdkd1.valid_bis_dat is NULL or kdkd1.valid_bis_dat>=isnull(@auf_dt0,@cur_date))
											and (kdkd1.valid_von_dat is NULL or kdkd1.valid_von_dat<=isnull(@auf_dt0,@cur_date))
										union
										select kdkd2.kd_lnr as kd_lnr_zuw
										from 
											FAF.FA_MROS_KD_KD kdkd2
										join
											FAF.FA_MROS_REFERENCE_TABLE rt_2
										on cast(kdkd2.kd_kd_verb_cd as varchar)=rt_2.Finnova_value and rt_2.userbk_nr=kdkd2.userbk_nr and rt_2.ref_type=15 and rt_2.activ_MROS=1 and rt_2.special_mark='DOPPELPERSON'
										where kdkd2.userbk_nr=@F_BANK_ID and kdkd2.kd_lnr_zuw=@n_id and kdkd2.kd_lnr<>@n_id
											and (kdkd2.valid_bis_dat is NULL or kdkd2.valid_bis_dat>=isnull(@auf_dt0,@cur_date))
											and (kdkd2.valid_von_dat is NULL or kdkd2.valid_von_dat<=isnull(@auf_dt0,@cur_date))) kdkd
									join
										FAF.FA_MROS_KD_STAMM kd
									on kd.userbk_nr=@F_BANK_ID and kd.kd_lnr=kdkd.kd_lnr_zuw and kd.partner_flag=0
									left join
										FAF.FA_MROS_KD_STAMM kd1
									on kd1.userbk_nr=@F_BANK_ID and kd1.kd_lnr=kdkd.kd_lnr_zuw and kd1.partner_flag=1;

									if (select count(*) from @KD_KD)>@mm0 + isnull(@mmm,0)
									begin
										set @partners = 
											(select STUFF(
												(select ', ' + coalesce(kdnr,'') from @KD_KD where id>@mm0 + isnull(@mmm,0) FOR XML PATH(''))
											, 1, LEN(', '), ''));

										if @partners is not NULL
											insert into @KD_KD select @n_id as nID,NULL as rl,NULL as cmm,@partners as ptn,NULL as kdtp,NULL as kdnr,NULL as kdnm,NULL as dt1,NULL as dt2,NULL as e_rl,NULL as e_cmm;
									end;
								end;

								set @jj = @jj + 1;
							end;
						end;
						
						set @mm = (select count(*) from @KD_KD) - isnull(@mmm,0);
						--prepare client xml
						set @jj = 1;
						while @jj<=@mm
						begin
							set @n_id = NULL;
							set @rol = NULL;
							set @comm = NULL;
							set @e_rol = NULL;
							set @e_comm = NULL;
							set @kd_nr = NULL;
							set @kd_nm = NULL;
							set @kd_tp = NULL;
							set @partners = NULL;
							set @xml_cl = NULL;
							set @xml_clt = NULL;
							set @xml_cl5 = NULL;
							set @xml_cl5t = NULL;
							set @bb = NULL;
							set @dt_fr = NULL;
							set @dt_to = NULL;
							set @dt_fr_e = NULL;
							set @dt_to_e = NULL;
							set @tecom = NULL;
							set @tpcom = NULL;
							select @n_id = nID,@rol=rl,@comm=cmm,@partners=ptn,@kd_nr=kdnr,@kd_tp=kdtp,@kd_nm=kdnm,@dt_fr=dt1,@dt_to=dt2,@e_rol=e_rl,@e_comm=e_cmm from @KD_KD where id=@jj + isnull(@mmm,0);
							set @bb = (select count(*) from @KD_KD0 where id>isnull(@mmm0,0) and nID=@n_id);
							if FAF.GoAML_get_paramS(@F_BANK_ID,60,1) = 'ok' and @mm = 1 and @cl_tp=1
								set @rol = FAF.GoAML_get_paramS(@F_BANK_ID,60,2);
							set @dt_fr_e = @dt_fr;
							set @dt_to_e = @dt_to;

							--for withdrawal/deposits
							if @kd_tp=1 and @jj=1 and @flag1>0 and @cl_tp=1
							begin
								set @xml_cl1 =
									(select
										FAF.GoAML_char_process(gd.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__gender'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__gender')) as "gender",
										FAF.GoAML_char_process(se.titel,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__title'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__title')) as "title",
										FAF.GoAML_char_process(se.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__first_name')) as "first_name",
										case 
											when FAF.GoAML_get_paramS(@F_BANK_ID,44,1)='ok'
											then upper(FAF.GoAML_char_process(se.name_1,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__last_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__last_name')))
											else FAF.GoAML_char_process(se.name_1,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__last_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__last_name'))
										end as "last_name",
										FAF.GoAML_char_process(
											case 
												when se.geburt_dat is NULL or se.geburt_dat < convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or cast(se.geburt_dat as date)>@cur_date
												then 
													case 
														when se.geburt_jahr is NULL or se.geburt_jahr<cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) or se.geburt_jahr>year(@cur_date)
														then NULL
														else cast(se.geburt_jahr as varchar) + '-01-01T00:00:00' 
													end 
												else left(convert(varchar,cast(se.geburt_dat as datetime),126),19)
											end
										,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__birthdate'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__birthdate')) as "birthdate",
										FAF.GoAML_char_process(se.geburt_ort,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__birth_place'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__birth_place')) as "birth_place",
										FAF.GoAML_char_process(
											case
												when FAF.GoAML_get_paramS(@F_BANK_ID,42,1)='ok'
												then
													case
														when FAF.GoAML_get_paramS(@F_BANK_ID,98,1)='ok'
														then
															case
																when FAF.GoAML_get_paramS(@F_BANK_ID,64,1)='ok'
																then
																	case 
																		when isnull(se.kd_pep_cd,se.kd_pep_cd_auto) is NULL then 'UNBEKANNT'
																		when isnull(se.kd_pep_cd,se.kd_pep_cd_auto) = '0' then 'NEIN'
																		else 'JA'
																	end
																else
																	case 
																		when isnull(se.kd_pep_cd,se.kd_pep_cd_auto) is NULL then 'unbekannt'
																		when isnull(se.kd_pep_cd,se.kd_pep_cd_auto) = '0' then 'nein'
																		else 'ja'
																	end
															end
														else
															case
																when FAF.GoAML_get_paramS(@F_BANK_ID,64,1)='ok'
																then
																	case 
																		when se.kd_pep_cd is NULL then 'UNBEKANNT'
																		when se.kd_pep_cd = '0' then 'NEIN'
																		else 'JA'
																	end
																else
																	case 
																		when se.kd_pep_cd is NULL then 'unbekannt'
																		when se.kd_pep_cd = '0' then 'nein'
																		else 'ja'
																	end
															end
													end
												else NULL
											end
										,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__id_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__id_number')) as "id_number",
										FAF.GoAML_char_process(cntr1.iso_land_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__nationality1'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__nationality1')) as "nationality1",
										FAF.GoAML_char_process(cntr2.iso_land_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__nationality2'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__nationality2')) as "nationality2",
										spp.phones as "phones",
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,47,1)='ok'
											then
												coalesce(sea.addresses,
													(select
														FAF.GoAML_get_paramS(@F_BANK_ID,8,2) as "address_type",
														FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address') as "address",
														FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__city') as "city",
														FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__country_code') as "country_code"
													FOR XML PATH ('address'),type, ELEMENTS)
												)
											else sea.addresses
										end as "addresses",
										(select FAF.GoAML_char_process(lower(eml.email_adr),FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__email'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__email')) as "email"
										from FAF.FA_MROS_KD_EMAIL eml
										join FAF.FA_MROS_REFERENCE_TABLE rt
										on eml.userbk_nr=rt.userbk_nr and cast(eml.mail_art_cd as varchar)=rt.finnova_value and rt.ref_type=7 and rt.MROS_value=FAF.GoAML_get_paramS(@F_BANK_ID,121,2)
										where eml.kd_lnr=se.kd_lnr and eml.userbk_nr=se.userbk_nr and left(eml.email_adr,1)<>'@'
										FOR XML PATH (''),type, ELEMENTS) as "emails",
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,48,1)='ok'
											then
												coalesce(spi.identification, 
													(select
														FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__type') as "type",
														FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__number') as "number",
														FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__issue_country') as "issue_country",
														FAF.GoAML_char_process(
															case
																when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
																then
																	case
																		when FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__type')=FAF.GoAML_get_paramS(@F_BANK_ID,6,1)
																		then FAF.GoAML_get_paramS(@F_BANK_ID,6,2)
																		else NULL
																	end
																else NULL
															end
														,FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpi__comments')) as "comments"
													FOR XML PATH ('identification'),type, ELEMENTS)
												)
											else spi.identification
										end as "identifications",
										FAF.GoAML_char_process(
											case 
												when FAF.GoAML_get_paramS(@F_BANK_ID,34,1)='ok' and ((se.tod_dat >= convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) and cast(se.tod_dat as date)<=@cur_date)
													or (se.tod_jahr>=cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) and se.tod_jahr<=year(@cur_date)))
												then FAF.GoAML_get_paramS(@F_BANK_ID,34,2)
												else NULL
											end
										,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__deceased'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__deceased')) as "deceased",
										FAF.GoAML_char_process(
											case 
												when se.tod_dat is NULL or se.tod_dat < convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or cast(se.tod_dat as date)>@cur_date
												then 
													case 
														when se.tod_jahr is NULL or se.tod_jahr<cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) or se.tod_jahr>year(@cur_date)
														then NULL
														else cast(se.tod_jahr as varchar) + '-01-01T00:00:00' 
													end 
												else left(convert(varchar,cast(se.tod_dat as datetime),126),19)
											end
										,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__date_deceased'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__date_deceased')) as "date_deceased",
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,68,1)='ok' and len(se.uid_nr)>1
											then FAF.GoAML_char_process(se.uid_nr,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__tax_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__tax_number'))
											else NULL
										end as "tax_number",
										FAF.GoAML_char_process(
											case
												when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
												then
													case
														when cntr1.iso_land_cd is NULL and FAF.GoAML_char_process(se.land_lnr_nat_bez,NULL,0) is not NULL and FAF.GoAML_char_process(se.iso_land_code_nat,NULL,0) is not NULL
														then 'Actual country is ' + FAF.GoAML_char_process(se.land_lnr_nat_bez,'',0) + ' (' + FAF.GoAML_char_process(se.iso_land_code_nat,'',0) + ')'
														else ''
													end
												else NULL
											end
										,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__comments')) as "comments"
									from 
										FAF.FA_MROS_KD_STAMM se
									left join 
										FAF.FA_MROS_KD_ADR_XML sea
									on sea.kd_lnr=se.kd_lnr and sea.userbk_nr=se.userbk_nr
									left join
										FAF.FA_MROS_KD_EMAIL_XML spp
									on spp.kd_lnr=se.kd_lnr and spp.userbk_nr=se.userbk_nr
									left join
										FAF.FA_MROS_KD_VSB_XML spi
									on spi.kd_lnr=se.kd_lnr and spi.userbk_nr=se.userbk_nr
									left join
										FAF.FA_MROS_INT_LAND cntr1
									on cntr1.iso_land_cd=se.iso_land_code_nat and cntr1.userbk_nr=se.userbk_nr
									left join
										FAF.FA_MROS_INT_LAND cntr2
									on cntr2.iso_land_cd=se.iso_land_code_nat_2 and cntr2.userbk_nr=se.userbk_nr
									left join
										FAF.FA_MROS_REFERENCE_TABLE gd
									on se.userbk_nr=gd.userbk_nr and cast(se.GESCHLECHT_CD as varchar)=gd.finnova_value and gd.ref_type=4
									where se.kd_lnr=@n_id and se.userbk_nr=@F_BANK_ID and se.partner_flag=0
									FOR XML PATH (''),type, ELEMENTS);
							end;

							if FAF.GoAML_get_paramS(@F_BANK_ID,92,1)='ok' 
								begin
									if (select count(*) from @KD_KD where id<@jj + isnull(@mmm,0) and id>isnull(@mmm,0) and nID=@n_id) = 0
									begin
										--person
										if @kd_tp=1
										begin
											select
												@xml_clt = 
													(select
														FAF.GoAML_char_process(gd.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__gender'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__gender')) as "gender",
														FAF.GoAML_char_process(se.titel,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__title'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__title')) as "title",
														FAF.GoAML_char_process(se.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__first_name')) as "first_name",
														case 
															when FAF.GoAML_get_paramS(@F_BANK_ID,44,1)='ok'
															then upper(FAF.GoAML_char_process(se.name_1,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__last_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__last_name')))
															else FAF.GoAML_char_process(se.name_1,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__last_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__last_name'))
														end as "last_name",
														FAF.GoAML_char_process(
															case 
																when se.geburt_dat is NULL or se.geburt_dat < convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or cast(se.geburt_dat as date)>@cur_date
																then 
																	case 
																		when se.geburt_jahr is NULL or se.geburt_jahr<cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) or se.geburt_jahr>year(@cur_date)
																		then NULL
																		else cast(se.geburt_jahr as varchar) + '-01-01T00:00:00' 
																	end 
																else left(convert(varchar,cast(se.geburt_dat as datetime),126),19)
															end
														,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__birthdate'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__birthdate')) as "birthdate",
														FAF.GoAML_char_process(se.geburt_ort,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__birth_place'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__birth_place')) as "birth_place",
														FAF.GoAML_char_process(
															case
																when FAF.GoAML_get_paramS(@F_BANK_ID,42,1)='ok'
																then
																	case 
																		when FAF.GoAML_get_paramS(@F_BANK_ID,98,1)='ok'
																		then
																			case 
																				when FAF.GoAML_get_paramS(@F_BANK_ID,64,1)='ok'
																				then
																					case 
																						when isnull(se.kd_pep_cd,se.kd_pep_cd_auto) is NULL then 'UNBEKANNT'
																						when isnull(se.kd_pep_cd,se.kd_pep_cd_auto) = '0' then 'NEIN'
																						else 'JA'
																					end
																				else
																					case 
																						when isnull(se.kd_pep_cd,se.kd_pep_cd_auto) is NULL then 'unbekannt'
																						when isnull(se.kd_pep_cd,se.kd_pep_cd_auto) = '0' then 'nein'
																						else 'ja'
																					end
																			end
																		else
																			case 
																				when FAF.GoAML_get_paramS(@F_BANK_ID,64,1)='ok'
																				then
																					case 
																						when se.kd_pep_cd is NULL then 'UNBEKANNT'
																						when se.kd_pep_cd = '0' then 'NEIN'
																						else 'JA'
																					end
																				else
																					case 
																						when se.kd_pep_cd is NULL then 'unbekannt'
																						when se.kd_pep_cd = '0' then 'nein'
																						else 'ja'
																					end
																			end
																	end
																else NULL
															end
														,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__id_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__id_number')) as "id_number",
														FAF.GoAML_char_process(cntr1.iso_land_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__nationality1'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__nationality1')) as "nationality1",
														FAF.GoAML_char_process(cntr2.iso_land_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__nationality2'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__nationality2')) as "nationality2",
														spp.phones as "phones",
														case
															when FAF.GoAML_get_paramS(@F_BANK_ID,47,1)='ok'
															then
																coalesce(sea.addresses,
																	(select
																		FAF.GoAML_get_paramS(@F_BANK_ID,8,2) as "address_type",
																		FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address') as "address",
																		FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__city') as "city",
																		FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__country_code') as "country_code"
																	FOR XML PATH ('address'),type, ELEMENTS)
																)
															else sea.addresses
														end as "addresses",
														(select FAF.GoAML_char_process(lower(eml.email_adr),FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__email'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__email')) as "email"
														from FAF.FA_MROS_KD_EMAIL eml
														join FAF.FA_MROS_REFERENCE_TABLE rt
														on eml.userbk_nr=rt.userbk_nr and cast(eml.mail_art_cd as varchar)=rt.finnova_value and rt.ref_type=7 and rt.MROS_value=FAF.GoAML_get_paramS(@F_BANK_ID,121,2)
														where eml.kd_lnr=se.kd_lnr and eml.userbk_nr=se.userbk_nr and left(eml.email_adr,1)<>'@'
														FOR XML PATH (''),type, ELEMENTS) as "emails",
														case
															when FAF.GoAML_get_paramS(@F_BANK_ID,48,1)='ok'
															then
																coalesce(spi.identification, 
																	(select
																		FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__type') as "type",
																		FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__number') as "number",
																		FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__issue_country') as "issue_country",
																		FAF.GoAML_char_process(
																			case
																				when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
																				then
																					case
																						when FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__type')=FAF.GoAML_get_paramS(@F_BANK_ID,6,1)
																						then FAF.GoAML_get_paramS(@F_BANK_ID,6,2)
																						else NULL
																					end
																				else NULL
																			end
																		,FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpi__comments')) as "comments"
																	FOR XML PATH ('identification'),type, ELEMENTS)
																)
															else spi.identification
														end as "identifications",
														FAF.GoAML_char_process(
															case 
																when FAF.GoAML_get_paramS(@F_BANK_ID,34,1)='ok' and ((se.tod_dat >= convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) and cast(se.tod_dat as date)<=@cur_date)
																	or (se.tod_jahr>=cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) and se.tod_jahr<=year(@cur_date)))
																then FAF.GoAML_get_paramS(@F_BANK_ID,34,2)
																else NULL
															end
														,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__deceased'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__deceased')) as "deceased",
														FAF.GoAML_char_process(
															case 
																when se.tod_dat is NULL or se.tod_dat < convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or cast(se.tod_dat as date)>@cur_date
																then 
																	case 
																		when se.tod_jahr is NULL or se.tod_jahr<cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) or se.tod_jahr>year(@cur_date)
																		then NULL
																		else cast(se.tod_jahr as varchar) + '-01-01T00:00:00' 
																	end 
																else left(convert(varchar,cast(se.tod_dat as datetime),126),19)
															end
														,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__date_deceased'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__date_deceased')) as "date_deceased",
														case
															when FAF.GoAML_get_paramS(@F_BANK_ID,68,1)='ok' and len(se.uid_nr)>1
															then FAF.GoAML_char_process(se.uid_nr,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__tax_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__tax_number'))
															else NULL
														end as "tax_number"
													FOR XML PATH (''),type, ELEMENTS),
												@tpcom = 
													case
														when cntr1.iso_land_cd is NULL and FAF.GoAML_char_process(se.land_lnr_nat_bez,NULL,0) is not NULL and FAF.GoAML_char_process(se.iso_land_code_nat,NULL,0) is not NULL
														then '; ' + char(13) + char(10) + 'Actual country is ' + FAF.GoAML_char_process(se.land_lnr_nat_bez,'',0) + ' (' + FAF.GoAML_char_process(se.iso_land_code_nat,'',0) + ')'
														else ''
													end
											from 
												FAF.FA_MROS_KD_STAMM se
											left join 
												FAF.FA_MROS_KD_ADR_XML sea
											on sea.kd_lnr=se.kd_lnr and sea.userbk_nr=se.userbk_nr
											left join
												FAF.FA_MROS_KD_EMAIL_XML spp
											on spp.kd_lnr=se.kd_lnr and spp.userbk_nr=se.userbk_nr
											left join
												FAF.FA_MROS_KD_VSB_XML spi
											on spi.kd_lnr=se.kd_lnr and spi.userbk_nr=se.userbk_nr
											left join
												FAF.FA_MROS_INT_LAND cntr1
											on cntr1.iso_land_cd=se.iso_land_code_nat and cntr1.userbk_nr=se.userbk_nr
											left join
												FAF.FA_MROS_INT_LAND cntr2
											on cntr2.iso_land_cd=se.iso_land_code_nat_2 and cntr2.userbk_nr=se.userbk_nr
											left join
												FAF.FA_MROS_REFERENCE_TABLE gd
											on se.userbk_nr=gd.userbk_nr and cast(se.GESCHLECHT_CD as varchar)=gd.finnova_value and gd.ref_type=4
											where se.kd_lnr=@n_id and se.userbk_nr=@F_BANK_ID and se.partner_flag=0;

											if @bb>0
												begin
													set @xml_cl =
														(select
															FAF.GoAML_char_process(
																case 
																	when @cl_tp=1 and @jj=1 and FAF.GoAML_get_paramS(@F_BANK_ID,30,1)='0'
																	then FAF.GoAML_get_paramS(@F_BANK_ID,30,2) 
																	else NULL 
																end
															,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__is_primary'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__is_primary')) as "is_primary",
															(select
																@xml_clt,
																(select
																	FAF.GoAML_char_process_WR(
																		case
																			when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
																			then
																				'Client number: ' + @kd_nr + 
																				case 
																					when len(partners)>0
																					then ' (partner in the partnership ' + partners + ')' 
																					else ''
																				end + @tpcom
																			else NULL
																		end
																	,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__comments')) as "comments"
																FOR XML PATH (''),type, ELEMENTS)
															FOR XML PATH (''),type, ELEMENTS) as "t_person",
															FAF.GoAML_char_process(@rol,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__role'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__role')) as "role",
															case
																when FAF.GoAML_get_paramS(@F_BANK_ID,105,1)='ok' or @dt_fr>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or 
																	@dt_to<=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112)
																then 
																	(select
																		FAF.GoAML_char_process(
																			convert(varchar,cast((case when @dt_fr>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) then @dt_fr else @dt_fr0 end) as datetime),126)
																		,FAF.GoAML_get_paramV(@F_BANK_ID,'rdr_valid_from'),FAF.GoAML_get_paramL(@F_BANK_ID,'rdr_valid_from')) as "valid_from",
																		case 
																			when @dt_fr>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)
																			then NULL
																			else FAF.GoAML_get_paramS(@F_BANK_ID,105,2)
																		end as "is_approx_from_date",
																		FAF.GoAML_char_process(
																			case 
																				when @dt_to<=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112)
																				then convert(varchar,cast(@dt_to as datetime),126)
																				else NULL
																			end
																		,FAF.GoAML_get_paramV(@F_BANK_ID,'rdr_valid_to'),FAF.GoAML_get_paramL(@F_BANK_ID,'rdr_valid_to')) as "valid_to"
																	FOR XML PATH (''),type, ELEMENTS)
																else NULL
															end as "relation_date_range"
														from
															(select max(ptn) as partners from @KD_KD where id>isnull(@mmm,0) and nID=@n_id) s
														FOR XML PATH ('account_related_person'),type, ELEMENTS);
												end;
											else
												begin
													set @xml_cl =
														(select
															FAF.GoAML_char_process(
																case 
																	when @cl_tp=1 and @jj=1 and FAF.GoAML_get_paramS(@F_BANK_ID,30,1)='0'
																	then FAF.GoAML_get_paramS(@F_BANK_ID,30,2) 
																	else NULL 
																end
															,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__is_primary'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__is_primary')) as "is_primary",
															(select
																@xml_clt,
																(select
																	FAF.GoAML_char_process_WR(
																		case
																			when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
																			then
																				'Client number: ' + @kd_nr + 
																				case 
																					when len(partners)>0
																					then ' (partner in the partnership ' + partners + ')' 
																					else ''
																				end + @tpcom
																			else NULL
																		end
																	,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__comments')) as "comments"
																FOR XML PATH (''),type, ELEMENTS)
															FOR XML PATH (''),type, ELEMENTS) as "t_person",
															FAF.GoAML_char_process(rl,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__role'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__role')) as "role",
															case
																when FAF.GoAML_get_paramS(@F_BANK_ID,105,1)='ok' or dt_fr>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or 
																	dt_to<=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112)
																then 
																	(select
																		FAF.GoAML_char_process(
																			convert(varchar,cast((case when dt_fr>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) then dt_fr else @dt_fr0 end) as datetime),126)
																		,FAF.GoAML_get_paramV(@F_BANK_ID,'rdr_valid_from'),FAF.GoAML_get_paramL(@F_BANK_ID,'rdr_valid_from')) as "valid_from",
																		case 
																			when dt_fr>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)
																			then NULL
																			else FAF.GoAML_get_paramS(@F_BANK_ID,105,2)
																		end as "is_approx_from_date",
																		FAF.GoAML_char_process(
																			case 
																				when dt_to<=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112)
																				then convert(varchar,cast(dt_to as datetime),126)
																				else NULL
																			end
																		,FAF.GoAML_get_paramV(@F_BANK_ID,'rdr_valid_to'),FAF.GoAML_get_paramL(@F_BANK_ID,'rdr_valid_to')) as "valid_to"
																	FOR XML PATH (''),type, ELEMENTS)
																else NULL
															end as "relation_date_range"
														from
															(select rl,
																case when len(partners)>0 then partners else NULL end as partners,
																case 
																	when dt_fr = convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or dt_fr = convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112) 
																	then NULL 
																	else dt_fr 
																end as dt_fr,
																case 
																	when dt_to = convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112) or dt_to = convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) 
																	then NULL 
																	else dt_to 
																end as dt_to
															from
																(select rl,
																	max(case when kdnr is not NULL and kdtp is not NULL then 1 else 0 end) as tst,
																	max(isnull(ptn,'')) as partners,
																	min(
																		case 
																			when dt1 is NULL and kdnr is not NULL and kdtp is not NULL 
																			then convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)
																			when dt1 is NULL  
																			then convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112) 
																			else dt1 
																		end
																	) as dt_fr,
																	max(
																		case 
																			when dt2 is NULL and kdnr is not NULL and kdtp is not NULL 
																			then convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112) 
																			when dt2 is NULL
																			then convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)
																			else dt2 
																		end
																	) as dt_to
																from @KD_KD where id>isnull(@mmm,0) and nID=@n_id
																group by rl) s
															where rl is not NULL or tst>0) ss
														FOR XML PATH ('account_related_person'),type, ELEMENTS);
												end;
										end;

										--partnership
										if @kd_tp=2
										begin
											select
												@xml_cl5t = 
													(select
														FAF.GoAML_char_process(@kd_nm,FAF.GoAML_get_paramV(@F_BANK_ID,'te__name'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__name')) as "name",
														FAF.GoAML_char_process(
															case
																when FAF.GoAML_get_paramS(@F_BANK_ID,78,1) = 'ok'
																then lf.MROS_value
																else FAF.GoAML_get_paramS(@F_BANK_ID,54,1)
															end
														,FAF.GoAML_get_paramV(@F_BANK_ID,'te__incorporation_legal_form'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__incorporation_legal_form')) as "incorporation_legal_form",
														case
															when FAF.GoAML_get_paramS(@F_BANK_ID,43,1)='ok'
															then spp.phones
															else NULL
														end as "phones",
														case
															when FAF.GoAML_get_paramS(@F_BANK_ID,49,1)='ok'
															then
																coalesce(sea.addresses,
																	(select
																		FAF.GoAML_get_paramS(@F_BANK_ID,9,2) as "address_type",
																		FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address') as "address",
																		FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__city') as "city",
																		FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__country_code') as "country_code"
																	FOR XML PATH ('address'),type, ELEMENTS)
																)
															else sea.addresses
														end as "addresses",
														FAF.GoAML_char_process(cntr1.iso_land_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'te__incorporation_country_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__incorporation_country_code')) as "incorporation_country_code",
														case
															when FAF.GoAML_get_paramS(@F_BANK_ID,67,1)='ok' and len(se.uid_nr)>1
															then FAF.GoAML_char_process(se.uid_nr,FAF.GoAML_get_paramV(@F_BANK_ID,'te__tax_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__tax_number'))
															else NULL
														end as "tax_number",
														FAF.GoAML_char_process(FAF.GoAML_get_paramS(@F_BANK_ID,55,1),FAF.GoAML_get_paramV(@F_BANK_ID,'te__tax_reg_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__tax_reg_number')) as "tax_reg_number"
													FOR XML PATH (''),type, ELEMENTS),
												@tecom = 
													case 
														when FAF.GoAML_get_paramS(@F_BANK_ID,78,1) = 'ok' and FAF.GoAML_get_paramS(@F_BANK_ID,32,1)=FAF.GoAML_char_process(lf.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'te__incorporation_legal_form'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__incorporation_legal_form'))
														then char(13) + char(10) + coalesce(FAF.GoAML_char_process(lf.comments,FAF.GoAML_get_paramS(@F_BANK_ID,32,2),0),'')
														else ''
													end + 
													case
														when cntr1.iso_land_cd is NULL and FAF.GoAML_char_process(se.land_lnr_nat_bez,NULL,0) is not NULL and FAF.GoAML_char_process(se.iso_land_code_nat,NULL,0) is not NULL
														then char(13) + char(10) + 'Actual country is ' + FAF.GoAML_char_process(se.land_lnr_nat_bez,'',0) + ' (' + FAF.GoAML_char_process(se.iso_land_code_nat,'',0) + ')'
														else ''
													end
											from 
												FAF.FA_MROS_KD_STAMM se
											left join
												FAF.FA_MROS_KD_EMAIL_XML spp
											on spp.kd_lnr=se.kd_lnr and spp.userbk_nr=se.userbk_nr
											left join 
												FAF.FA_MROS_KD_ADR_XML sea
											on sea.kd_lnr=se.kd_lnr and sea.userbk_nr=se.userbk_nr
											left join
												FAF.FA_MROS_INT_LAND cntr1
											on cntr1.iso_land_cd=se.iso_land_code_nat and cntr1.userbk_nr=se.userbk_nr
											left join
												FAF.FA_MROS_REFERENCE_TABLE lf
											on se.userbk_nr=lf.userbk_nr and cast(se.kd_rstat_cd as varchar)=lf.finnova_value and lf.ref_type=8
											where se.kd_lnr=@n_id and se.userbk_nr=@F_BANK_ID and se.partner_flag=0;

											set @xml_cl5 =
												(select
													FAF.GoAML_char_process(e_rl,FAF.GoAML_get_paramV(@F_BANK_ID,'are__account_entity_relation'),FAF.GoAML_get_paramL(@F_BANK_ID,'are__account_entity_relation')) as "account_entity_relation",
													(select
														@xml_cl5t,
														(select
															FAF.GoAML_char_process_WR(
																case
																	when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
																	then
																		'Client number: ' + @kd_nr + 
																		case 
																			when partners is not NULL 
																			then char(13) + char(10) + 'It is the partnership of the clients: ' + ltrim(partners)
																			else ''
																		end + @tecom
																	else NULL
																end
															,FAF.GoAML_get_paramV(@F_BANK_ID,'te__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__comments')) as "comments"
														FOR XML PATH (''),type, ELEMENTS)
													FOR XML PATH (''),type, ELEMENTS) as "entity",
													case
														when dt_fr_e>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or dt_to_e<=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112)
														then 
															(select
																FAF.GoAML_char_process(
																	convert(varchar,cast((case when dt_fr_e>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) then dt_fr_e else @dt_fr0 end) as datetime),126)
																,FAF.GoAML_get_paramV(@F_BANK_ID,'rdr_valid_from'),FAF.GoAML_get_paramL(@F_BANK_ID,'rdr_valid_from')) as "valid_from",
																case 
																	when dt_fr_e>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)
																	then NULL
																	else FAF.GoAML_get_paramS(@F_BANK_ID,105,2)
																end as "is_approx_from_date",
																case 
																	when dt_to_e<=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112)
																	then left(convert(varchar,cast(dt_to_e as datetime),126),19)
																	else NULL
																end as "valid_to"
															FOR XML PATH (''),type, ELEMENTS)
														else NULL
													end as "relation_date_range"
												from
													(select e_rl,
														case when len(partners)>0 then partners else NULL end as partners,
														case 
															when dt_fr_e = convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or dt_fr_e = convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112) 
															then NULL 
															else dt_fr_e
														end as dt_fr_e,
														case 
															when dt_to_e = convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112) or dt_to_e = convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) 
															then NULL 
															else dt_to_e 
														end as dt_to_e
													from
														(select e_rl,
															max(isnull(ptn,'')) as partners,
															min(
																case 
																	when dt1 is NULL and kdnr is not NULL and kdtp is not NULL 
																	then convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)
																	when dt1 is NULL  
																	then convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112) 
																	else dt1 
																end
															) as dt_fr_e,
															max(
																case 
																	when dt2 is NULL and kdnr is not NULL and kdtp is not NULL 
																	then convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112) 
																	when dt2 is NULL
																	then convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)
																	else dt2 
																end
															) as dt_to_e
														from @KD_KD where id>isnull(@mmm,0) and nID=@n_id
														group by e_rl) s
													) ss
												FOR XML PATH ('account_related_entity'),type, ELEMENTS);
										end;

										--entity
										if @kd_tp=3
										begin
											set @xml_cl5t =
												(select
													FAF.GoAML_char_process(se.kd_bez,FAF.GoAML_get_paramV(@F_BANK_ID,'te__name'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__name')) as "name",
													case
														when FAF.GoAML_get_paramS(@F_BANK_ID,70,1)='ok'
														then FAF.GoAML_char_process(se.kd_bez,FAF.GoAML_get_paramV(@F_BANK_ID,'te__commercial_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__commercial_name'))
														else NULL
													end as "commercial_name",
													FAF.GoAML_char_process(lf.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'te__incorporation_legal_form'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__incorporation_legal_form')) as "incorporation_legal_form",
													FAF.GoAML_char_process(
														case
															when FAF.GoAML_get_paramS(@F_BANK_ID,71,1)='ok'
															then vsb.kd_ausw_nr
															else se.uid_nr
														end
													,FAF.GoAML_get_paramV(@F_BANK_ID,'te__incorporation_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__incorporation_number')) as "incorporation_number",
													FAF.GoAML_char_process(se.noga_bez,FAF.GoAML_get_paramV(@F_BANK_ID,'te__business'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__business')) as "business",
													case
														when FAF.GoAML_get_paramS(@F_BANK_ID,43,1)='ok'
														then spp.phones
														else NULL
													end as "phones",
													case
														when FAF.GoAML_get_paramS(@F_BANK_ID,49,1)='ok'
														then
															coalesce(sea.addresses,
																(select
																	FAF.GoAML_get_paramS(@F_BANK_ID,7,2) as "address_type",
																	FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address') as "address",
																	FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__city') as "city",
																	FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__country_code') as "country_code"
																FOR XML PATH ('address'),type, ELEMENTS)
															)
														else sea.addresses
													end as "addresses",
													(select FAF.GoAML_char_process(lower(eml.email_adr),FAF.GoAML_get_paramV(@F_BANK_ID,'te__email'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__email')) as "email"
													from FAF.FA_MROS_KD_EMAIL eml
													join FAF.FA_MROS_REFERENCE_TABLE rt
													on eml.userbk_nr=rt.userbk_nr and cast(eml.mail_art_cd as varchar)=rt.finnova_value and rt.ref_type=7 and rt.MROS_value=FAF.GoAML_get_paramS(@F_BANK_ID,121,2)
													where eml.kd_lnr=se.kd_lnr and eml.userbk_nr=se.userbk_nr and left(eml.email_adr,1)<>'@'
													FOR XML PATH (''),type, ELEMENTS) as "emails",
													(select FAF.GoAML_char_process(ema.email_adr,FAF.GoAML_get_paramV(@F_BANK_ID,'te__url'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__url')) as "url"
													from FAF.FA_MROS_KD_EMAIL ema 
													where ema.kd_lnr=se.kd_lnr and ema.userbk_nr=se.userbk_nr and ema.mail_art_cd=FAF.GoAML_get_paramS(@F_BANK_ID,122,2)
													FOR XML PATH (''),type, ELEMENTS) as "urls",
													FAF.GoAML_char_process(se.geburt_ort,FAF.GoAML_get_paramV(@F_BANK_ID,'te__incorporation_state'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__incorporation_state')) as "incorporation_state",
													FAF.GoAML_char_process(cntr1.iso_land_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'te__incorporation_country_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__incorporation_country_code')) as "incorporation_country_code",
													FAF.GoAML_char_process(
														case 
															when se.geburt_dat is NULL or se.geburt_dat < convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or cast(se.geburt_dat as date)>@cur_date
															then 
																case 
																	when se.geburt_jahr is NULL or se.geburt_jahr<cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) or se.geburt_jahr>year(@cur_date)
																	then NULL
																	else cast(se.geburt_jahr as varchar) + '-01-01T00:00:00' 
																end 
															else left(convert(varchar,cast(se.geburt_dat as datetime),126),19)
														end 
													,FAF.GoAML_get_paramV(@F_BANK_ID,'te__incorporation_date'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__incorporation_date')) as "incorporation_date",
													FAF.GoAML_char_process(
														case 
															when FAF.GoAML_get_paramS(@F_BANK_ID,28,1)='ok' 
																and ((se.tod_dat >= convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) and cast(se.tod_dat as date)<=@cur_date) or (se.tod_jahr>=cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) and se.tod_jahr<=year(@cur_date)))
															then FAF.GoAML_get_paramS(@F_BANK_ID,28,2) 
															else NULL
														end 
													,FAF.GoAML_get_paramV(@F_BANK_ID,'te__business_closed'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__business_closed')) as "business_closed",
													FAF.GoAML_char_process(
														case 
															when se.tod_dat is NULL or se.tod_dat < convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or cast(se.tod_dat as date)>@cur_date
															then 
																case 
																	when se.tod_jahr is NULL or se.tod_jahr<cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) or se.tod_jahr>year(@cur_date)
																	then NULL
																	else cast(se.tod_jahr as varchar) + '-01-01T00:00:00' 
																end 
															else left(convert(varchar,cast(se.tod_dat as datetime),126),19)
														end 
													,FAF.GoAML_get_paramV(@F_BANK_ID,'te__date_business_closed'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__date_business_closed')) as "date_business_closed",
													case
														when FAF.GoAML_get_paramS(@F_BANK_ID,67,1)='ok' and len(se.uid_nr)>1
														then FAF.GoAML_char_process(se.uid_nr,FAF.GoAML_get_paramV(@F_BANK_ID,'te__tax_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__tax_number'))
														else NULL
													end as "tax_number",
													FAF.GoAML_char_process(
														case
															when FAF.GoAML_get_paramS(@F_BANK_ID,104,1)='ok'
															then NULL
															else tax.MROS_value
														end
													,FAF.GoAML_get_paramV(@F_BANK_ID,'te__tax_reg_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__tax_reg_number')) as "tax_reg_number",
													FAF.GoAML_char_process_WR(
														case
															when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
															then
																'Client number: ' + se.edit_kd_nr + 
																case
																	when cntr1.iso_land_cd is NULL and FAF.GoAML_char_process(se.land_lnr_nat_bez,NULL,0) is not NULL and FAF.GoAML_char_process(se.iso_land_code_nat,NULL,0) is not NULL
																	then '; ' + char(13) + char(10) + 'Actual country is ' + FAF.GoAML_char_process(se.land_lnr_nat_bez,'',0) + ' (' + FAF.GoAML_char_process(se.iso_land_code_nat,'',0) + ')'
																	else ''
																end + 
																case 
																	when FAF.GoAML_get_paramS(@F_BANK_ID,32,1)=FAF.GoAML_char_process(lf.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'te__incorporation_legal_form'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__incorporation_legal_form'))
																	then '; ' + char(13) + char(10) + coalesce(FAF.GoAML_char_process(lf.comments,FAF.GoAML_get_paramS(@F_BANK_ID,32,2),0),'')
																	else ''
																end
															else NULL
														end
													,FAF.GoAML_get_paramV(@F_BANK_ID,'te__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__comments')) as "comments"
												from 
													FAF.FA_MROS_KD_STAMM se
												left join 
													FAF.FA_MROS_KD_ADR_XML sea
												on sea.kd_lnr=se.kd_lnr and sea.userbk_nr=se.userbk_nr
												left join
													FAF.FA_MROS_KD_EMAIL_XML spp
												on spp.kd_lnr=se.kd_lnr and spp.userbk_nr=se.userbk_nr
												left join
													FAF.FA_MROS_INT_LAND cntr1
												on cntr1.iso_land_cd=se.iso_land_code_nat and cntr1.userbk_nr=se.userbk_nr
												left join
													FAF.FA_MROS_REFERENCE_TABLE lf
												on se.userbk_nr=lf.userbk_nr and cast(se.kd_rstat_cd as varchar)=lf.finnova_value and lf.ref_type=8
												left join
													FAF.FA_MROS_REFERENCE_TABLE tax
												on se.userbk_nr=tax.userbk_nr and se.KD_SITZ_GSCH_CD=tax.finnova_value and tax.ref_type=3
												left join
													(select userbk_nr,kd_lnr,kd_ausw_nr,
														row_number() over (partition by userbk_nr,kd_lnr order by ausstell_dat desc) as rn
													from FAF.FA_MROS_KD_VSB where kd_ausw_nr is not NULL and cast(iddoc_cd as varchar)=FAF.GoAML_get_paramS(@F_BANK_ID,71,2)) vsb
												on se.userbk_nr=vsb.userbk_nr and se.kd_lnr=vsb.kd_lnr and vsb.rn=1
												where se.kd_lnr=@n_id and se.userbk_nr=@F_BANK_ID and se.partner_flag=0
												FOR XML PATH (''),type, ELEMENTS);

											set @xml_cl5 =
												(select
													FAF.GoAML_char_process(e_rl,FAF.GoAML_get_paramV(@F_BANK_ID,'are__account_entity_relation'),FAF.GoAML_get_paramL(@F_BANK_ID,'are__account_entity_relation')) as "account_entity_relation",
													@xml_cl5t as "entity",
													case
														when dt_fr_e>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or dt_to_e<=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112)
														then 
															(select
																FAF.GoAML_char_process(
																	convert(varchar,cast((case when dt_fr_e>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) then dt_fr_e else @dt_fr0 end) as datetime),126)
																,FAF.GoAML_get_paramV(@F_BANK_ID,'rdr_valid_from'),FAF.GoAML_get_paramL(@F_BANK_ID,'rdr_valid_from')) as "valid_from",
																case 
																	when dt_fr_e>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)
																	then NULL
																	else FAF.GoAML_get_paramS(@F_BANK_ID,105,2)
																end as "is_approx_from_date",
																case 
																	when dt_to_e<=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112)
																	then left(convert(varchar,cast(dt_to_e as datetime),126),19)
																	else NULL
																end as "valid_to"
															FOR XML PATH (''),type, ELEMENTS)
														else NULL
													end as "relation_date_range"
												from
													(select e_rl,
														case 
															when dt_fr_e = convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or dt_fr_e = convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112) 
															then NULL 
															else dt_fr_e
														end as dt_fr_e,
														case 
															when dt_to_e = convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112) or dt_to_e = convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) 
															then NULL 
															else dt_to_e 
														end as dt_to_e
													from
														(select e_rl,
															min(
																case 
																	when dt1 is NULL and kdnr is not NULL and kdtp is not NULL 
																	then convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)
																	when dt1 is NULL  
																	then convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112) 
																	else dt1 
																end
															) as dt_fr_e,
															max(
																case 
																	when dt2 is NULL and kdnr is not NULL and kdtp is not NULL 
																	then convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112) 
																	when dt2 is NULL
																	then convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)
																	else dt2 
																end
															) as dt_to_e
														from @KD_KD where id>isnull(@mmm,0) and nID=@n_id
														group by e_rl) s
													) ss
												FOR XML PATH ('account_related_entity'),type, ELEMENTS);
										end;
									end;
								end;
							else
								begin
									--union all records of the client in the array if it is first appearance
									if (select count(*) from @KD_KD where id<@jj + isnull(@mmm,0) and id>isnull(@mmm,0) and nID=@n_id) = 0
									begin
										set @kk = @jj + 1;
										while @kk<=@mm
										begin
											select @rol=rl,@comm=cmm,@partners=ptn,@dt_fr=dt1,@dt_to=dt2,@dt_fr_e=dt1_e,@dt_to_e=dt2_e,@e_rol=e_rl,@e_comm=e_cmm from 
												(select
													case 
														when @bb=0 and len(rl)>0 and rl<>isnull(@rol,'')
														then 
															case
																when len(@rol)>0
																then
																	case
																		when FAF.GoAML_get_paramS(@F_BANK_ID,41,1)='ok'
																		then
																			case
																				when @rol+','+rl in ('13,14', '14,13', '19,13', '13,19', '19,14', '14,19') 
																				then '19'
																				when @rol+','+rl in ('14,7', '7,14', '20,7', '7,20', '14,20', '20,14')
																				then '20'
																				when @rol+','+rl in ('14,15', '15,14', '14,21', '21,14', '21,15', '15,21') 
																				then '21'
																				when @rol+','+rl in ('13,15', '15,13', '22,15', '15,22', '13,22', '22,13') 
																				then '22'
																				when @rol+','+rl in ('15,7', '7,15', '15,23', '23,15', '23,7', '7,23') 
																				then '23'
																				when @rol+','+rl in ('15,19', '19,15', '13,21', '21,13', '14,22', '22,14','21,22', '22,21', '21,19', '19,21', '19,22', '22,19','21,24', '24,21', '24,22', '22,24', '24,19', '19,24') 
																				then '24'
																				when @rol+','+rl in ('15,20', '20,15', '14,23', '23,14', '7,21', '21,7', '20,23', '23,20', '21,23', '23,21', '20,21', '21,20', '20,25', '25,20', '25,23', '23,25', '25,21', '21,25') 
																				then '25'
																				else '6'
																			end
																		when @rol=FAF.GoAML_get_paramS(@F_BANK_ID,75,1) then rl
																		when rl=FAF.GoAML_get_paramS(@F_BANK_ID,75,1) then @rol
																		when @rol=FAF.GoAML_get_paramS(@F_BANK_ID,76,1) then @rol
																		when rl=FAF.GoAML_get_paramS(@F_BANK_ID,76,1) then rl
																		else FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__role')
																	end
																else rl
															end
														else @rol
													end as rl,
													case 
														when @bb=0 and len(e_rl)>0 and e_rl<>isnull(@e_rol,'')
														then 
															case
																when len(@e_rol)>0
																then
																	case
																		when @e_rol=FAF.GoAML_get_paramS(@F_BANK_ID,93,1) then e_rl
																		when e_rl=FAF.GoAML_get_paramS(@F_BANK_ID,93,1) then @e_rol
																		else FAF.GoAML_get_paramV(@F_BANK_ID,'are__account_entity_relation')
																	end
																else e_rl
															end
														else @e_rol
													end as e_rl,
													case
														when @bb=0 and len(cmm)>0 and (isnull(len(@comm),0)=0 or charindex(cmm,@comm)=0)
														then isnull(@comm,'') + case when len(@comm)>0 then ', ' + char(13) + char(10) + '   ' else '' end + cmm
														else @comm
													end as cmm,
													case
														when @bb=0 and len(e_cmm)>0 and (isnull(len(@e_comm),0)=0 or charindex(e_cmm,@e_comm)=0)
														then isnull(@e_comm,'') + case when len(@e_comm)>0 then ', ' + char(13) + char(10) + '   ' else '' end + e_cmm
														else @e_comm
													end as e_cmm,
													case
														when len(ptn)>0
														then ptn
														else @partners
													end as ptn,
													case 
														when @bb=0 and len(rl)>0 and rl<>isnull(@rol,'')
														then 
															case
																when len(@rol)>0
																then
																	case
																		when FAF.GoAML_get_paramS(@F_BANK_ID,41,1)='ok'
																		then
																			case
																				when dt1 is NULL then @dt_fr
																				when @dt_fr is NULL then dt1
																				when dt1>@dt_fr then dt1
																				else @dt_fr
																			end
																		when @rol=FAF.GoAML_get_paramS(@F_BANK_ID,75,1) then dt1
																		when rl=FAF.GoAML_get_paramS(@F_BANK_ID,75,1) then @dt_fr
																		when @rol=FAF.GoAML_get_paramS(@F_BANK_ID,76,1) then @dt_fr
																		when rl=FAF.GoAML_get_paramS(@F_BANK_ID,76,1) then dt1
																		when dt1 is NULL then @dt_fr
																		when @dt_fr is NULL then dt1
																		when dt1>@dt_fr then dt1
																		else @dt_fr
																	end
																else dt1
															end
														else @dt_fr
													end as dt1,
													case 
														when @bb=0 and len(rl)>0 and rl<>isnull(@rol,'')
														then 
															case
																when len(@rol)>0
																then
																	case
																		when FAF.GoAML_get_paramS(@F_BANK_ID,41,1)='ok'
																		then
																			case
																				when dt2 is NULL then @dt_to
																				when @dt_to is NULL then dt2
																				when dt2>@dt_to then dt2
																				else @dt_to
																			end
																		when @rol=FAF.GoAML_get_paramS(@F_BANK_ID,75,1) then dt2
																		when rl=FAF.GoAML_get_paramS(@F_BANK_ID,75,1) then @dt_to
																		when @rol=FAF.GoAML_get_paramS(@F_BANK_ID,76,1) then @dt_to
																		when rl=FAF.GoAML_get_paramS(@F_BANK_ID,76,1) then dt2
																		when dt2 is NULL then @dt_to
																		when @dt_to is NULL then dt2
																		when dt2>@dt_to then dt2
																		else @dt_to
																	end
																else dt2
															end
														else @dt_to
													end as dt2,
													case 
														when @bb=0 and len(e_rl)>0 and e_rl<>isnull(@e_rol,'')
														then 
															case
																when len(@e_rol)>0
																then
																	case
																		when @e_rol=FAF.GoAML_get_paramS(@F_BANK_ID,93,1) then dt1
																		when e_rl=FAF.GoAML_get_paramS(@F_BANK_ID,93,1) then @dt_fr_e
																		when dt1 is NULL then @dt_fr_e
																		when @dt_fr_e is NULL then dt1
																		when dt1>@dt_fr_e then dt1
																		else @dt_fr_e
																	end
																else dt1
															end
														else @dt_fr_e
													end as dt1_e,
													case 
														when @bb=0 and len(e_rl)>0 and e_rl<>isnull(@e_rol,'')
														then 
															case
																when len(@e_rol)>0
																then
																	case
																		when @e_rol=FAF.GoAML_get_paramS(@F_BANK_ID,93,1) then dt2
																		when e_rl=FAF.GoAML_get_paramS(@F_BANK_ID,93,1) then @dt_to_e
																		when dt2 is NULL then @dt_to_e
																		when @dt_to_e is NULL then dt2
																		when dt2>@dt_to_e then dt2
																		else @dt_to_e
																	end
																else dt2
															end
														else @dt_to_e
													end as dt2_e
												from @KD_KD where id=@kk + isnull(@mmm,0) and nID=@n_id) s;

											set @kk = @kk + 1;
										end;

										--person
										if @kd_tp=1
										begin
											set @xml_cl =
												(select
													FAF.GoAML_char_process(
														case 
															when @cl_tp=1 and @jj=1 and FAF.GoAML_get_paramS(@F_BANK_ID,30,1)='0'
															then FAF.GoAML_get_paramS(@F_BANK_ID,30,2) 
															else NULL 
														end
													,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__is_primary'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__is_primary')) as "is_primary",
													(select
														FAF.GoAML_char_process(gd.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__gender'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__gender')) as "gender",
														FAF.GoAML_char_process(se.titel,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__title'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__title')) as "title",
														FAF.GoAML_char_process(se.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__first_name')) as "first_name",
														case 
															when FAF.GoAML_get_paramS(@F_BANK_ID,44,1)='ok'
															then upper(FAF.GoAML_char_process(se.name_1,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__last_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__last_name')))
															else FAF.GoAML_char_process(se.name_1,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__last_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__last_name'))
														end as "last_name",
														FAF.GoAML_char_process(
															case 
																when se.geburt_dat is NULL or se.geburt_dat < convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or cast(se.geburt_dat as date)>@cur_date
																then 
																	case 
																		when se.geburt_jahr is NULL or se.geburt_jahr<cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) or se.geburt_jahr>year(@cur_date)
																		then NULL
																		else cast(se.geburt_jahr as varchar) + '-01-01T00:00:00' 
																	end 
																else left(convert(varchar,cast(se.geburt_dat as datetime),126),19)
															end
														,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__birthdate'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__birthdate')) as "birthdate",
														FAF.GoAML_char_process(se.geburt_ort,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__birth_place'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__birth_place')) as "birth_place",
														FAF.GoAML_char_process(
															case
																when FAF.GoAML_get_paramS(@F_BANK_ID,42,1)='ok'
																then
																	case 
																		when FAF.GoAML_get_paramS(@F_BANK_ID,98,1)='ok'
																		then
																			case 
																				when FAF.GoAML_get_paramS(@F_BANK_ID,64,1)='ok'
																				then
																					case 
																						when isnull(se.kd_pep_cd,se.kd_pep_cd_auto) is NULL then 'UNBEKANNT'
																						when isnull(se.kd_pep_cd,se.kd_pep_cd_auto) = '0' then 'NEIN'
																						else 'JA'
																					end
																				else
																					case 
																						when isnull(se.kd_pep_cd,se.kd_pep_cd_auto) is NULL then 'unbekannt'
																						when isnull(se.kd_pep_cd,se.kd_pep_cd_auto) = '0' then 'nein'
																						else 'ja'
																					end
																			end
																		else
																			case 
																				when FAF.GoAML_get_paramS(@F_BANK_ID,64,1)='ok'
																				then
																					case 
																						when se.kd_pep_cd is NULL then 'UNBEKANNT'
																						when se.kd_pep_cd = '0' then 'NEIN'
																						else 'JA'
																					end
																				else
																					case 
																						when se.kd_pep_cd is NULL then 'unbekannt'
																						when se.kd_pep_cd = '0' then 'nein'
																						else 'ja'
																					end
																			end
																	end
																else NULL
															end
														,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__id_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__id_number')) as "id_number",
														FAF.GoAML_char_process(cntr1.iso_land_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__nationality1'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__nationality1')) as "nationality1",
														FAF.GoAML_char_process(cntr2.iso_land_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__nationality2'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__nationality2')) as "nationality2",
														spp.phones as "phones",
														case
															when FAF.GoAML_get_paramS(@F_BANK_ID,47,1)='ok'
															then
																coalesce(sea.addresses,
																	(select
																		FAF.GoAML_get_paramS(@F_BANK_ID,8,2) as "address_type",
																		FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address') as "address",
																		FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__city') as "city",
																		FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__country_code') as "country_code"
																	FOR XML PATH ('address'),type, ELEMENTS)
																)
															else sea.addresses
														end as "addresses",
														(select FAF.GoAML_char_process(lower(eml.email_adr),FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__email'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__email')) as "email"
														from FAF.FA_MROS_KD_EMAIL eml
														join FAF.FA_MROS_REFERENCE_TABLE rt
														on eml.userbk_nr=rt.userbk_nr and cast(eml.mail_art_cd as varchar)=rt.finnova_value and rt.ref_type=7 and rt.MROS_value=FAF.GoAML_get_paramS(@F_BANK_ID,121,2)
														where eml.kd_lnr=se.kd_lnr and eml.userbk_nr=se.userbk_nr and left(eml.email_adr,1)<>'@'
														FOR XML PATH (''),type, ELEMENTS) as "emails",
														case
															when FAF.GoAML_get_paramS(@F_BANK_ID,48,1)='ok'
															then
																coalesce(spi.identification, 
																	(select
																		FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__type') as "type",
																		FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__number') as "number",
																		FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__issue_country') as "issue_country",
																		FAF.GoAML_char_process(
																			case
																				when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
																				then
																					case
																						when FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__type')=FAF.GoAML_get_paramS(@F_BANK_ID,6,1)
																						then FAF.GoAML_get_paramS(@F_BANK_ID,6,2)
																						else NULL
																					end
																				else NULL
																			end
																		,FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpi__comments')) as "comments"
																	FOR XML PATH ('identification'),type, ELEMENTS)
																)
															else spi.identification
														end as "identifications",
														FAF.GoAML_char_process(
															case 
																when FAF.GoAML_get_paramS(@F_BANK_ID,34,1)='ok' and ((se.tod_dat >= convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) and cast(se.tod_dat as date)<=@cur_date)
																	or (se.tod_jahr>=cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) and se.tod_jahr<=year(@cur_date)))
																then FAF.GoAML_get_paramS(@F_BANK_ID,34,2)
																else NULL
															end
														,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__deceased'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__deceased')) as "deceased",
														FAF.GoAML_char_process(
															case 
																when se.tod_dat is NULL or se.tod_dat < convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or cast(se.tod_dat as date)>@cur_date
																then 
																	case 
																		when se.tod_jahr is NULL or se.tod_jahr<cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) or se.tod_jahr>year(@cur_date)
																		then NULL
																		else cast(se.tod_jahr as varchar) + '-01-01T00:00:00' 
																	end 
																else left(convert(varchar,cast(se.tod_dat as datetime),126),19)
															end
														,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__date_deceased'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__date_deceased')) as "date_deceased",
														case
															when FAF.GoAML_get_paramS(@F_BANK_ID,68,1)='ok' and len(se.uid_nr)>1
															then FAF.GoAML_char_process(se.uid_nr,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__tax_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__tax_number'))
															else NULL
														end as "tax_number",
														FAF.GoAML_char_process_WR(
															case
																when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
																then
																	'Client number: ' + @kd_nr + 
																	case 
																		when len(@partners)>0
																		then ' (partner in the partnership ' + @partners + ')' 
																		else ''
																	end + 
																	case
																		when cntr1.iso_land_cd is NULL and FAF.GoAML_char_process(se.land_lnr_nat_bez,NULL,0) is not NULL and FAF.GoAML_char_process(se.iso_land_code_nat,NULL,0) is not NULL
																		then '; ' + char(13) + char(10) + 'Actual country is ' + FAF.GoAML_char_process(se.land_lnr_nat_bez,'',0) + ' (' + FAF.GoAML_char_process(se.iso_land_code_nat,'',0) + ')'
																		else ''
																	end
																else NULL
															end
														,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__comments')) as "comments"
													FOR XML PATH (''),type, ELEMENTS) as "t_person",
													FAF.GoAML_char_process(@rol,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__role'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__role')) as "role",
													case
														when FAF.GoAML_get_paramS(@F_BANK_ID,105,1)='ok' or @dt_fr>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or 
															@dt_to<=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112)
														then 
															(select
																FAF.GoAML_char_process(
																	convert(varchar,cast((case when @dt_fr>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) then @dt_fr else @dt_fr0 end) as datetime),126)
																,FAF.GoAML_get_paramV(@F_BANK_ID,'rdr_valid_from'),FAF.GoAML_get_paramL(@F_BANK_ID,'rdr_valid_from')) as "valid_from",
																case 
																	when @dt_fr>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)
																	then NULL
																	else FAF.GoAML_get_paramS(@F_BANK_ID,105,2)
																end as "is_approx_from_date",
																FAF.GoAML_char_process(
																	case 
																		when @dt_to<=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112)
																		then convert(varchar,cast(@dt_to as datetime),126)
																		else NULL
																	end
																,FAF.GoAML_get_paramV(@F_BANK_ID,'rdr_valid_to'),FAF.GoAML_get_paramL(@F_BANK_ID,'rdr_valid_to')) as "valid_to"
															FOR XML PATH (''),type, ELEMENTS)
														else NULL
													end as "relation_date_range",
													case
														when FAF.GoAML_char_process(@rol,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__role'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__role')) = FAF.GoAML_get_paramS(@F_BANK_ID,20,1)
														then 'Role(s) of the client: ' + char(13) + char(10) + '   ' + isnull(@comm,FAF.GoAML_get_paramS(@F_BANK_ID,20,2))
														else NULL
													end as "comments"
												from 
													FAF.FA_MROS_KD_STAMM se
												left join 
													FAF.FA_MROS_KD_ADR_XML sea
												on sea.kd_lnr=se.kd_lnr and sea.userbk_nr=se.userbk_nr
												left join
													FAF.FA_MROS_KD_EMAIL_XML spp
												on spp.kd_lnr=se.kd_lnr and spp.userbk_nr=se.userbk_nr
												left join
													FAF.FA_MROS_KD_VSB_XML spi
												on spi.kd_lnr=se.kd_lnr and spi.userbk_nr=se.userbk_nr
												left join
													FAF.FA_MROS_INT_LAND cntr1
												on cntr1.iso_land_cd=se.iso_land_code_nat and cntr1.userbk_nr=se.userbk_nr
												left join
													FAF.FA_MROS_INT_LAND cntr2
												on cntr2.iso_land_cd=se.iso_land_code_nat_2 and cntr2.userbk_nr=se.userbk_nr
												left join
													FAF.FA_MROS_REFERENCE_TABLE gd
												on se.userbk_nr=gd.userbk_nr and cast(se.GESCHLECHT_CD as varchar)=gd.finnova_value and gd.ref_type=4
												where se.kd_lnr=@n_id and se.userbk_nr=@F_BANK_ID and se.partner_flag=0															
												FOR XML PATH ('account_related_person'),type, ELEMENTS);

										end;

										--partnership
										if @kd_tp=2
										begin
											set @xml_cl5 =
												(select
													FAF.GoAML_char_process(@e_rol,FAF.GoAML_get_paramV(@F_BANK_ID,'are__account_entity_relation'),FAF.GoAML_get_paramL(@F_BANK_ID,'are__account_entity_relation')) as "account_entity_relation",
													(select
														FAF.GoAML_char_process(@kd_nm,FAF.GoAML_get_paramV(@F_BANK_ID,'te__name'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__name')) as "name",
														FAF.GoAML_char_process(
															case
																when FAF.GoAML_get_paramS(@F_BANK_ID,78,1) = 'ok'
																then lf.MROS_value
																else FAF.GoAML_get_paramS(@F_BANK_ID,54,1)
															end
														,FAF.GoAML_get_paramV(@F_BANK_ID,'te__incorporation_legal_form'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__incorporation_legal_form')) as "incorporation_legal_form",
														case
															when FAF.GoAML_get_paramS(@F_BANK_ID,43,1)='ok'
															then spp.phones
															else NULL
														end as "phones",
														case
															when FAF.GoAML_get_paramS(@F_BANK_ID,49,1)='ok'
															then
																coalesce(sea.addresses,
																	(select
																		FAF.GoAML_get_paramS(@F_BANK_ID,9,2) as "address_type",
																		FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address') as "address",
																		FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__city') as "city",
																		FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__country_code') as "country_code"
																	FOR XML PATH ('address'),type, ELEMENTS)
																)
															else sea.addresses
														end as "addresses",
														FAF.GoAML_char_process(cntr1.iso_land_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'te__incorporation_country_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__incorporation_country_code')) as "incorporation_country_code",
														case
															when FAF.GoAML_get_paramS(@F_BANK_ID,67,1)='ok' and len(se.uid_nr)>1
															then FAF.GoAML_char_process(se.uid_nr,FAF.GoAML_get_paramV(@F_BANK_ID,'te__tax_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__tax_number'))
															else NULL
														end as "tax_number",
														FAF.GoAML_char_process(FAF.GoAML_get_paramS(@F_BANK_ID,55,1),FAF.GoAML_get_paramV(@F_BANK_ID,'te__tax_reg_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__tax_reg_number')) as "tax_reg_number",
														FAF.GoAML_char_process_WR(
															case
																when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
																then
																	'Client number: ' + @kd_nr + 
																	case 
																		when @partners is not NULL 
																		then char(13) + char(10) + 'It is the partnership of the clients: ' + ltrim(@partners)
																		else ''
																	end + 
																	case 
																		when FAF.GoAML_get_paramS(@F_BANK_ID,78,1) = 'ok' and FAF.GoAML_get_paramS(@F_BANK_ID,32,1)=FAF.GoAML_char_process(lf.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'te__incorporation_legal_form'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__incorporation_legal_form'))
																		then char(13) + char(10) + coalesce(FAF.GoAML_char_process(lf.comments,FAF.GoAML_get_paramS(@F_BANK_ID,32,2),0),'')
																		else ''
																	end + 
																	case
																		when cntr1.iso_land_cd is NULL and FAF.GoAML_char_process(se.land_lnr_nat_bez,NULL,0) is not NULL and FAF.GoAML_char_process(se.iso_land_code_nat,NULL,0) is not NULL
																		then char(13) + char(10) + 'Actual country is ' + FAF.GoAML_char_process(se.land_lnr_nat_bez,'',0) + ' (' + FAF.GoAML_char_process(se.iso_land_code_nat,'',0) + ')'
																		else ''
																	end
																else NULL
															end
														,FAF.GoAML_get_paramV(@F_BANK_ID,'te__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__comments')) as "comments"
													FOR XML PATH (''),type, ELEMENTS) as "entity",
													case
														when @dt_fr_e>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or @dt_to_e<=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112)
														then 
															(select
																FAF.GoAML_char_process(
																	convert(varchar,cast((case when @dt_fr_e>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) then @dt_fr_e else @dt_fr0 end) as datetime),126)
																,FAF.GoAML_get_paramV(@F_BANK_ID,'rdr_valid_from'),FAF.GoAML_get_paramL(@F_BANK_ID,'rdr_valid_from')) as "valid_from",
																case 
																	when @dt_fr_e>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)
																	then NULL
																	else FAF.GoAML_get_paramS(@F_BANK_ID,105,2)
																end as "is_approx_from_date",
																case 
																	when @dt_to_e<=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112)
																	then left(convert(varchar,cast(@dt_to_e as datetime),126),19)
																	else NULL
																end as "valid_to"
															FOR XML PATH (''),type, ELEMENTS)
														else NULL
													end as "relation_date_range",
													case
														when @e_rol = FAF.GoAML_get_paramS(@F_BANK_ID,21,1) or @e_rol is NULL
														then 'Role(s) of the client: ' + char(13) + char(10) + '   ' + isnull(@e_comm,FAF.GoAML_get_paramS(@F_BANK_ID,21,2))
														else NULL
													end as "comments"
												from 
													FAF.FA_MROS_KD_STAMM se
												left join
													FAF.FA_MROS_KD_EMAIL_XML spp
												on spp.kd_lnr=se.kd_lnr and spp.userbk_nr=se.userbk_nr
												left join 
													FAF.FA_MROS_KD_ADR_XML sea
												on sea.kd_lnr=se.kd_lnr and sea.userbk_nr=se.userbk_nr
												left join
													FAF.FA_MROS_INT_LAND cntr1
												on cntr1.iso_land_cd=se.iso_land_code_nat and cntr1.userbk_nr=se.userbk_nr
												left join
													FAF.FA_MROS_REFERENCE_TABLE lf
												on se.userbk_nr=lf.userbk_nr and cast(se.kd_rstat_cd as varchar)=lf.finnova_value and lf.ref_type=8
												where se.kd_lnr=@n_id and se.userbk_nr=@F_BANK_ID and se.partner_flag=0
												FOR XML PATH ('account_related_entity'),type, ELEMENTS);
										end;

										--entity
										if @kd_tp=3
										begin
											set @xml_cl5 =
												(select
													FAF.GoAML_char_process(@e_rol,FAF.GoAML_get_paramV(@F_BANK_ID,'are__account_entity_relation'),FAF.GoAML_get_paramL(@F_BANK_ID,'are__account_entity_relation')) as "account_entity_relation",
													(select
														FAF.GoAML_char_process(se.kd_bez,FAF.GoAML_get_paramV(@F_BANK_ID,'te__name'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__name')) as "name",
														case
															when FAF.GoAML_get_paramS(@F_BANK_ID,70,1)='ok'
															then FAF.GoAML_char_process(se.kd_bez,FAF.GoAML_get_paramV(@F_BANK_ID,'te__commercial_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__commercial_name'))
															else NULL
														end as "commercial_name",
														FAF.GoAML_char_process(lf.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'te__incorporation_legal_form'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__incorporation_legal_form')) as "incorporation_legal_form",
														FAF.GoAML_char_process(
															case
																when FAF.GoAML_get_paramS(@F_BANK_ID,71,1)='ok'
																then vsb.kd_ausw_nr
																else se.uid_nr
															end
														,FAF.GoAML_get_paramV(@F_BANK_ID,'te__incorporation_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__incorporation_number')) as "incorporation_number",
														FAF.GoAML_char_process(se.noga_bez,FAF.GoAML_get_paramV(@F_BANK_ID,'te__business'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__business')) as "business",
														case
															when FAF.GoAML_get_paramS(@F_BANK_ID,43,1)='ok'
															then spp.phones
															else NULL
														end as "phones",
														case
															when FAF.GoAML_get_paramS(@F_BANK_ID,49,1)='ok'
															then
																coalesce(sea.addresses,
																	(select
																		FAF.GoAML_get_paramS(@F_BANK_ID,7,2) as "address_type",
																		FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address') as "address",
																		FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__city') as "city",
																		FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__country_code') as "country_code"
																	FOR XML PATH ('address'),type, ELEMENTS)
																)
															else sea.addresses
														end as "addresses",
														(select FAF.GoAML_char_process(lower(eml.email_adr),FAF.GoAML_get_paramV(@F_BANK_ID,'te__email'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__email')) as "email"
														from FAF.FA_MROS_KD_EMAIL eml
														join FAF.FA_MROS_REFERENCE_TABLE rt
														on eml.userbk_nr=rt.userbk_nr and cast(eml.mail_art_cd as varchar)=rt.finnova_value and rt.ref_type=7 and rt.MROS_value=FAF.GoAML_get_paramS(@F_BANK_ID,121,2)
														where eml.kd_lnr=se.kd_lnr and eml.userbk_nr=se.userbk_nr and left(eml.email_adr,1)<>'@'
														FOR XML PATH (''),type, ELEMENTS) as "emails",
														(select FAF.GoAML_char_process(ema.email_adr,FAF.GoAML_get_paramV(@F_BANK_ID,'te__url'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__url')) as "url"
														from FAF.FA_MROS_KD_EMAIL ema 
														where ema.kd_lnr=se.kd_lnr and ema.userbk_nr=se.userbk_nr and ema.mail_art_cd=FAF.GoAML_get_paramS(@F_BANK_ID,122,2)
														FOR XML PATH (''),type, ELEMENTS) as "urls",
														FAF.GoAML_char_process(se.geburt_ort,FAF.GoAML_get_paramV(@F_BANK_ID,'te__incorporation_state'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__incorporation_state')) as "incorporation_state",
														FAF.GoAML_char_process(cntr1.iso_land_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'te__incorporation_country_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__incorporation_country_code')) as "incorporation_country_code",
														FAF.GoAML_char_process(
															case 
																when se.geburt_dat is NULL or se.geburt_dat < convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or cast(se.geburt_dat as date)>@cur_date
																then 
																	case 
																		when se.geburt_jahr is NULL or se.geburt_jahr<cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) or se.geburt_jahr>year(@cur_date)
																		then NULL
																		else cast(se.geburt_jahr as varchar) + '-01-01T00:00:00' 
																	end 
																else left(convert(varchar,cast(se.geburt_dat as datetime),126),19)
															end 
														,FAF.GoAML_get_paramV(@F_BANK_ID,'te__incorporation_date'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__incorporation_date')) as "incorporation_date",
														FAF.GoAML_char_process(
															case 
																when FAF.GoAML_get_paramS(@F_BANK_ID,28,1)='ok' 
																	and ((se.tod_dat >= convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) and cast(se.tod_dat as date)<=@cur_date) or (se.tod_jahr>=cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) and se.tod_jahr<=year(@cur_date)))
																then FAF.GoAML_get_paramS(@F_BANK_ID,28,2) 
																else NULL
															end 
														,FAF.GoAML_get_paramV(@F_BANK_ID,'te__business_closed'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__business_closed')) as "business_closed",
														FAF.GoAML_char_process(
															case 
																when se.tod_dat is NULL or se.tod_dat < convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or cast(se.tod_dat as date)>@cur_date
																then 
																	case 
																		when se.tod_jahr is NULL or se.tod_jahr<cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) or se.tod_jahr>year(@cur_date)
																		then NULL
																		else cast(se.tod_jahr as varchar) + '-01-01T00:00:00' 
																	end 
																else left(convert(varchar,cast(se.tod_dat as datetime),126),19)
															end 
														,FAF.GoAML_get_paramV(@F_BANK_ID,'te__date_business_closed'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__date_business_closed')) as "date_business_closed",
														case
															when FAF.GoAML_get_paramS(@F_BANK_ID,67,1)='ok' and len(se.uid_nr)>1
															then FAF.GoAML_char_process(se.uid_nr,FAF.GoAML_get_paramV(@F_BANK_ID,'te__tax_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__tax_number'))
															else NULL
														end as "tax_number",
														FAF.GoAML_char_process(
															case
																when FAF.GoAML_get_paramS(@F_BANK_ID,104,1)='ok'
																then NULL
																else tax.MROS_value
															end
														,FAF.GoAML_get_paramV(@F_BANK_ID,'te__tax_reg_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__tax_reg_number')) as "tax_reg_number",
														FAF.GoAML_char_process_WR(
															case
																when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
																then
																	'Client number: ' + se.edit_kd_nr + 
																	case
																		when cntr1.iso_land_cd is NULL and FAF.GoAML_char_process(se.land_lnr_nat_bez,NULL,0) is not NULL and FAF.GoAML_char_process(se.iso_land_code_nat,NULL,0) is not NULL
																		then '; ' + char(13) + char(10) + 'Actual country is ' + FAF.GoAML_char_process(se.land_lnr_nat_bez,'',0) + ' (' + FAF.GoAML_char_process(se.iso_land_code_nat,'',0) + ')'
																		else ''
																	end + 
																	case 
																		when FAF.GoAML_get_paramS(@F_BANK_ID,32,1)=FAF.GoAML_char_process(lf.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'te__incorporation_legal_form'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__incorporation_legal_form'))
																		then '; ' + char(13) + char(10) + coalesce(FAF.GoAML_char_process(lf.comments,FAF.GoAML_get_paramS(@F_BANK_ID,32,2),0),'')
																		else ''
																	end
																else NULL
															end
														,FAF.GoAML_get_paramV(@F_BANK_ID,'te__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'te__comments')) as "comments"
													FOR XML PATH (''),type, ELEMENTS) as "entity",
													case
														when @dt_fr_e>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or @dt_to_e<=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112)
														then 
															(select
																FAF.GoAML_char_process(
																	convert(varchar,cast((case when @dt_fr_e>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) then @dt_fr_e else @dt_fr0 end) as datetime),126)
																,FAF.GoAML_get_paramV(@F_BANK_ID,'rdr_valid_from'),FAF.GoAML_get_paramL(@F_BANK_ID,'rdr_valid_from')) as "valid_from",
																case 
																	when @dt_fr_e>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)
																	then NULL
																	else FAF.GoAML_get_paramS(@F_BANK_ID,105,2)
																end as "is_approx_from_date",
																case 
																	when @dt_to_e<=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112)
																	then left(convert(varchar,cast(@dt_to_e as datetime),126),19)
																	else NULL
																end as "valid_to"
															FOR XML PATH (''),type, ELEMENTS)
														else NULL
													end as "relation_date_range",
													case
														when @e_rol = FAF.GoAML_get_paramS(@F_BANK_ID,21,1) or @e_rol is NULL
														then 'Role(s) of the client: ' + char(13) + char(10) + '   ' + isnull(@e_comm,FAF.GoAML_get_paramS(@F_BANK_ID,21,2))
														else NULL
													end as "comments"
												from 
													FAF.FA_MROS_KD_STAMM se
												left join 
													FAF.FA_MROS_KD_ADR_XML sea
												on sea.kd_lnr=se.kd_lnr and sea.userbk_nr=se.userbk_nr
												left join
													FAF.FA_MROS_KD_EMAIL_XML spp
												on spp.kd_lnr=se.kd_lnr and spp.userbk_nr=se.userbk_nr
												left join
													FAF.FA_MROS_INT_LAND cntr1
												on cntr1.iso_land_cd=se.iso_land_code_nat and cntr1.userbk_nr=se.userbk_nr
												left join
													FAF.FA_MROS_REFERENCE_TABLE lf
												on se.userbk_nr=lf.userbk_nr and cast(se.kd_rstat_cd as varchar)=lf.finnova_value and lf.ref_type=8
												left join
													FAF.FA_MROS_REFERENCE_TABLE tax
												on se.userbk_nr=tax.userbk_nr and se.KD_SITZ_GSCH_CD=tax.finnova_value and tax.ref_type=3
												left join
													(select userbk_nr,kd_lnr,kd_ausw_nr,
														row_number() over (partition by userbk_nr,kd_lnr order by ausstell_dat desc) as rn
													from FAF.FA_MROS_KD_VSB where kd_ausw_nr is not NULL and cast(iddoc_cd as varchar)=FAF.GoAML_get_paramS(@F_BANK_ID,71,2)) vsb
												on se.userbk_nr=vsb.userbk_nr and se.kd_lnr=vsb.kd_lnr and vsb.rn=1
												where se.kd_lnr=@n_id and se.userbk_nr=@F_BANK_ID and se.partner_flag=0
												FOR XML PATH ('account_related_entity'),type, ELEMENTS);
										end;
									end;
								end;
							
							if @xml_cl is not NULL
								set @xml_kd = (select @xml_kd,@xml_cl FOR XML PATH (''),type, ELEMENTS);
							if @xml_cl5 is not NULL
								set @xml_kd5 = (select @xml_kd5,@xml_cl5 FOR XML PATH (''),type, ELEMENTS);
							set @jj = @jj + 1;
						end;

						set @xml_cl = 
							(select 
								@xml_cl0,
								@xml_kd5 as "related_entities",
								case 
									when FAF.GoAML_get_paramS(@F_BANK_ID,50,1)='ok' and @xml_kd is NULL 
									then '' 
									else @xml_kd 
								end as "related_persons" 
							FOR XML PATH (''));

						--if @xml_cl is NULL
						--begin
						--	set @xml_cl = 
						--		(select
						--			@xml_dp as "account_related_person/t_person",
						--			case
						--				when FAF.GoAML_get_paramS(@F_BANK_ID,60,1)='ok'
						--				then FAF.GoAML_get_paramS(@F_BANK_ID,60,2)
						--				else FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__role')
						--			end as "account_related_person/role",
						--			case
						--				when FAF.GoAML_get_paramS(@F_BANK_ID,105,1)='ok'
						--				then FAF.GoAML_get_paramV(@F_BANK_ID,'rdr_valid_from')
						--				else NULL
						--			end as "account_related_person/relation_date_range/valid_from",
						--			case
						--				when FAF.GoAML_get_paramS(@F_BANK_ID,105,1)='ok'
						--				then FAF.GoAML_get_paramS(@F_BANK_ID,105,2)
						--				else NULL
						--			end as "account_related_person/relation_date_range/is_approx_from_date"
						--		FOR XML PATH ('related_persons'),type, ELEMENTS);
						--end;
					end;
				else
					begin
						--short version when client is not requested
						set @adr_txt = 
							(select top 1
								case
									when len(FAF.GoAML_char_process(adr.strasse,NULL,0))>0
									then char(13) + char(10) + FAF.GoAML_char_process(adr.strasse,'',100)
									else ''
								end +
								case
									when len(FAF.GoAML_char_process(coalesce(adr.adr_plz,adr.ort_plz),NULL,0))>0
										or len(FAF.GoAML_char_process(coalesce(adr.ort_ort_bez,adr.adr_ort_bez),NULL,0)) > 0
									then char(13) + char(10)
									else ''
								end +
								case
									when len(FAF.GoAML_char_process(coalesce(adr.adr_plz,adr.ort_plz),NULL,0))>0
									then FAF.GoAML_char_process(coalesce(adr.adr_plz,adr.ort_plz),'',10) + ' '
									else ''
								end +
								case
									when len(FAF.GoAML_char_process(coalesce(adr.ort_ort_bez,adr.adr_ort_bez),NULL,0)) >0
									then FAF.GoAML_char_process(coalesce(adr.ort_ort_bez,adr.adr_ort_bez),'',255)
									else ''
								end +
								case
									when adr.land_bez is not NULL
									then char(13) + char(10) + adr.land_bez
									else ''
								end 
							from
								FAF.FA_MROS_KD_ADR adr
							where adr.KD_LNR=@cl_id and adr.userbk_nr=@F_BANK_ID);

						set @clnt_txt = 
							coalesce(
								(select top 1
									char(13) + char(10) + FAF.GoAML_char_process(sb1.kd_bez,'n/a',0) 
								from
									FAF.FA_MROS_KD_STAMM sb1
								where sb1.KD_LNR=@cl_id and sb1.userbk_nr=@F_BANK_ID and sb1.partner_flag=0)
								,'')
							+ isnull(@adr_txt,'');
					end;

				if FAF.GoAML_get_paramS(@F_BANK_ID,86,1)='ok'
				begin
					select @kt_nr = edit_kt_nr,@curr=iso_wrg_cd from FAF.FA_MROS_KT_STAMM where kt_lnr=@kt_id and userbk_nr=@F_BANK_ID;

					set @ac_fnd = (select count(*) from FAF.FA_MROS_KT_STAMM where edit_kt_nr=@kt_nr and kd_lnr=@cl_id and userbk_nr=@F_BANK_ID);
					if @ac_fnd>1
					begin
						set @xml_ac_fnd = 
							(select 
								FAF.GoAML_char_process(wrg.iso_wrg_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'af__currency_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'af__currency_code')) as "currency_code",
								FAF.GoAML_char_process(
									cast(cast(sld.saldo_kwrg as decimal(15,2)) as varchar)
								,FAF.GoAML_get_paramV(@F_BANK_ID,'af__currency_balance'),FAF.GoAML_get_paramL(@F_BANK_ID,'af__currency_balance')) as "currency_balance",
								FAF.GoAML_char_process(convert(varchar,@cur_date,23) + 'T00:00:00',FAF.GoAML_get_paramV(@F_BANK_ID,'af__currency_balance_date'),FAF.GoAML_get_paramL(@F_BANK_ID,'af__currency_balance_date')) as "currency_balance_date"
							from 
								FAF.FA_MROS_KT_STAMM kt 
							join
								FAF.FA_MROS_KT_SALDO sld 
							on kt.kt_lnr=sld.kt_lnr and kt.userbk_nr=sld.userbk_nr
							left join
								FAF.FA_MROS_INT_WRG wrg
							on kt.iso_wrg_cd=wrg.iso_wrg_cd and kt.userbk_nr=wrg.userbk_nr
							where kt.edit_kt_nr=@kt_nr and kt.kd_lnr=@cl_id and kt.userbk_nr=@F_BANK_ID
							FOR XML PATH ('account_fund'),type, ELEMENTS);
					end;

					if FAF.GoAML_get_paramS(@F_BANK_ID,117,1)='ok' and @curr<>FAF.GoAML_get_paramS(@F_BANK_ID,117,2)
					begin
						set @xml_ac_fnd = 
							(select 
								FAF.GoAML_char_process(FAF.GoAML_get_paramS(@F_BANK_ID,117,2),FAF.GoAML_get_paramV(@F_BANK_ID,'af__currency_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'af__currency_code')) as "currency_code",
								FAF.GoAML_char_process(
									cast(cast(isnull(sld.saldo_bwrg,0) as decimal(15,2)) as varchar)
								,FAF.GoAML_get_paramV(@F_BANK_ID,'af__currency_balance'),FAF.GoAML_get_paramL(@F_BANK_ID,'af__currency_balance')) as "currency_balance",
								FAF.GoAML_char_process(convert(varchar,@cur_date,23) + 'T00:00:00',FAF.GoAML_get_paramV(@F_BANK_ID,'af__currency_balance_date'),FAF.GoAML_get_paramL(@F_BANK_ID,'af__currency_balance_date')) as "currency_balance_date"
							from 
								FAF.FA_MROS_KT_STAMM kt 
							left join
								FAF.FA_MROS_KT_SALDO sld 
							on kt.kt_lnr=sld.kt_lnr and kt.userbk_nr=sld.userbk_nr
							where kt.kt_lnr=@kt_id and kt.userbk_nr=@F_BANK_ID
							FOR XML PATH ('account_fund'),type, ELEMENTS);
					end;
				end;

				set @xml_ac = 
					(select 
						FAF.GoAML_char_process(@inst_name,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__institution_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__institution_name')) as "institution_name",
						FAF.GoAML_char_process(@swif,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__swift'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__swift')) as "swift",
						case
							when @flag>0 and FAF.GoAML_get_paramS(@F_BANK_ID,85,1)='ok'
							then FAF.GoAML_char_process(@cntr,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__institution_country'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__institution_country'))
							else NULL
						end as "institution_country",
						case
							when @flag>0 
							then
								FAF.GoAML_char_process(
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,37,1)='ok'
										then stz.MROS_value
										else NULL
									end
								,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__branch'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__branch'))
							else NULL
						end as "branch",
						case
							when FAF.GoAML_get_paramS(@F_BANK_ID,127,1)='ok' and @flag=0
							then NULL
							else
								case 
									when FAF.GoAML_get_paramS(@F_BANK_ID,89,1)='ok'
									then 
										FAF.GoAML_char_process(
											case
												when FAF.GoAML_get_paramS(@F_BANK_ID,38,1)='ok'
												then 
													case 
														when len(s5.iban_id)<15 
														then FAF.GoAML_get_paramS(@F_BANK_ID,91,1)
														when substring(s5.iban_id,3,2) not like '[0-9][0-9]' 
														then FAF.GoAML_get_paramS(@F_BANK_ID,91,1)
														when (select count(*) from FAF.FA_MROS_INT_LAND where userbk_nr=@F_BANK_ID and iso_land_cd=substring(s5.iban_id,1,2))=0 
														then FAF.GoAML_get_paramS(@F_BANK_ID,91,1)
														else FAF.GoAML_get_paramS(@F_BANK_ID,90,1)
													end
												else FAF.GoAML_get_paramS(@F_BANK_ID,90,2)
											end
										,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__account_category'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__account_category'))
									else NULL
								end
						end as "account_category",
						FAF.GoAML_char_process(
							case
								when FAF.GoAML_get_paramS(@F_BANK_ID,38,1)='ok'
								then s5.iban_id
								else s5.edit_kt_nr
							end
						,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__account'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__account')) as "account",
						case 
							when @flag>0 
							then FAF.GoAML_char_process(wrg.iso_wrg_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__currency_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__currency_code'))
							else NULL 
						end as "currency_code",
						case
							when FAF.GoAML_get_paramS(@F_BANK_ID,127,1)='ok' and @flag=0
							then NULL
							else
								case
									when FAF.GoAML_get_paramS(@F_BANK_ID,86,1)='ok'
									then @xml_ac_fnd
									else NULL
								end
						end as "account_funds",
						case 
							when @flag>0 
							then FAF.GoAML_char_process(coalesce(s5.rubrik,s5.kt_art_nr_bez),FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__account_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__account_name'))
							else NULL 
						end as "account_name",
						case
							when FAF.GoAML_get_paramS(@F_BANK_ID,127,1)='ok' and @flag=0
							then NULL
							else
								FAF.GoAML_char_process(
									case 
										when len(s5.iban_id)<15 then NULL
										when substring(s5.iban_id,3,2) not like '[0-9][0-9]' then NULL
										when (select count(*) from FAF.FA_MROS_INT_LAND where userbk_nr=@F_BANK_ID and iso_land_cd=substring(s5.iban_id,1,2))=0 then NULL
										else s5.iban_id
									end
								,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__iban'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__iban'))
						end as "iban",
						case 
							when @flag>0 
							then FAF.GoAML_char_process(@cl_nr,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__client_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__client_number'))
							else NULL 
						end as "client_number",
						case 
							when @flag>0 
							then FAF.GoAML_char_process(isnull(ac_tp.MROS_value,ac_tp1.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__account_type'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__account_type'))
							else NULL 
						end as "account_type",
						--account owner
						case
							when FAF.GoAML_get_paramS(@F_BANK_ID,127,1)='ok' and @flag=0
							then NULL
							else
								case 
									when @flag>0 or FAF.GoAML_get_paramS(@F_BANK_ID,106,1)='ok'
									then @xml_cl
									else NULL
								end
						end,
						case 
							when @flag>0 
							then 
								FAF.GoAML_char_process(
									case 
										when s5.eroeff_dat>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) and cast(s5.eroeff_dat as date)<=@cur_date
										then left(convert(varchar,cast(s5.eroeff_dat as datetime),126),19) 
										else NULL
									end
								,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__opened'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__opened'))
							else NULL 
						end as "opened",
						case 
							when @flag>0 
							then 
								FAF.GoAML_char_process(
									case 
										when s5.aufheb_dat>=coalesce(s5.eroeff_dat,convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)) and cast(s5.aufheb_dat as date)<=@cur_date
										then left(convert(varchar,cast(s5.aufheb_dat as datetime),126),19)
										else NULL
									end
								,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__closed'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__closed'))
							else NULL 
						end as "closed",
						case 
							when @flag>0 
							then 
								FAF.GoAML_char_process(
									case 
										when FAF.GoAML_get_paramS(@F_BANK_ID,73,1)='ok' and s5.aufheb_dat is not NULL and cast(s5.aufheb_dat as date)<=@cur_date and s5.aufheb_dat>=coalesce(s5.eroeff_dat,convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112))
										then FAF.GoAML_get_paramS(@F_BANK_ID,73,2)
										when FAF.GoAML_get_paramS(@F_BANK_ID,16,1)='ok' 
											and (s5.aufheb_dat is NULL or s5.aufheb_dat<coalesce(s5.eroeff_dat,convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)) or cast(s5.aufheb_dat as date)>@cur_date)
										then 
											coalesce(cast(cast(
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,111,1)='ok'
													then sld.saldo_kwrg
													else sld.saldo_bwrg
												end
											as decimal(15,2)) as varchar),FAF.GoAML_get_paramS(@F_BANK_ID,16,2))
										else NULL
									end
								,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__balance'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__balance'))
							else NULL 
						end as "balance",
						case 
							when @flag>0 
							then 
								FAF.GoAML_char_process(
									case 
										when FAF.GoAML_get_paramS(@F_BANK_ID,73,1)='ok' and s5.aufheb_dat is not NULL and cast(s5.aufheb_dat as date)<=@cur_date and s5.aufheb_dat>=coalesce(s5.eroeff_dat,convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112))
										then convert(varchar,s5.aufheb_dat,23) + 'T00:00:00'
										when FAF.GoAML_get_paramS(@F_BANK_ID,16,1)='ok' 
											and (s5.aufheb_dat is NULL or s5.aufheb_dat<coalesce(s5.eroeff_dat,convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)) or cast(s5.aufheb_dat as date)>@cur_date)
										then convert(varchar,@cur_date,23) + 'T00:00:00'
										else NULL
									end
								,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__date_balance'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__date_balance'))
							else NULL 
						end as "date_balance",
						case 
							when @flag>0 
							then 
								FAF.GoAML_char_process(
									case 
										when FAF.GoAML_get_paramS(@F_BANK_ID,66,1)='ok'
										then 
											case 
												when s5.aufheb_cd is NULL 
												then FAF.GoAML_get_paramS(@F_BANK_ID,18,1)
												else stcd.MROS_value
											end
										else
											case 
												when (s5.aufheb_dat>=coalesce(s5.eroeff_dat,convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)) and cast(s5.aufheb_dat as date)<=@cur_date) --or s5.aufheb_cd='ZZ'
												then FAF.GoAML_get_paramS(@F_BANK_ID,18,2)
												else FAF.GoAML_get_paramS(@F_BANK_ID,18,1)
											end
									end
								,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__status_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__status_code'))
							else NULL 
						end as "status_code",
						case
							when FAF.GoAML_get_paramS(@F_BANK_ID,127,1)='ok' and @flag=0
							then NULL
							else
								case 
									when FAF.GoAML_get_paramS(@F_BANK_ID,95,1)='ok' 
									then 
										FAF.GoAML_char_process(
											case
												when FAF.GoAML_get_paramS(@F_BANK_ID,108,1)='ok'
												then convert(varchar,@cur_date,23)+'T00:00:00' 
												else convert(varchar,sysdatetime(),126)
											end
										,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__status_date'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__status_date'))
									else NULL 
								end
						end as "status_date",
						case 
							when @flag>0 
							then 
								FAF.GoAML_char_process(
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,39,1)='ok'
										then 
											case 
												when FAF.GoAML_get_paramS(@F_BANK_ID,22,1)=FAF.GoAML_char_process(wrg.iso_wrg_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__currency_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__currency_code')) 
													or (s5.aufheb_dat>=coalesce(s5.eroeff_dat,convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)) and cast(s5.aufheb_dat as date)<=@cur_date)
													or FAF.GoAML_get_paramS(@F_BANK_ID,111,1)='ok'
												then NULL
												else coalesce(cast(cast(sld.saldo_kwrg as decimal(15,2)) as varchar),FAF.GoAML_get_paramS(@F_BANK_ID,22,2))
											end
										else NULL
									end
								,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__beneficiary'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__beneficiary'))
							else NULL 
						end as "beneficiary",
						case 
							when @flag>0 
							then 
								FAF.GoAML_char_process(
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,40,1)='ok'
										then s5.sitz_nr_bez
										else NULL
									end
								,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__beneficiary_comment'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__beneficiary_comment'))
							else NULL 
						end as "beneficiary_comment",
						FAF.GoAML_char_process_WR(
							case
								when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
								then
									case 
										when @flag>0 
										then 
											stuff(
												case 
													when wrg.iso_wrg_cd is NULL and FAF.GoAML_char_process(s5.wrg_bez,NULL,0) is not NULL and FAF.GoAML_char_process(s5.iso_wrg_cd,NULL,0) is not NULL
													then '. ' + char(13) + char(10) + 'Actual currency is ' + FAF.GoAML_char_process(s5.wrg_bez,'',0) + ' (' + FAF.GoAML_char_process(s5.iso_wrg_cd,'',0) + ')'
													else ''
												end + 
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,24,1)=FAF.GoAML_char_process(isnull(ac_tp.MROS_value,ac_tp1.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__account_type'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__account_type'))
													then '. ' + char(13) + char(10) + coalesce(FAF.GoAML_char_process(case when ac_tp.MROS_value is not NULL then ac_tp.comments else ac_tp1.comments end,FAF.GoAML_get_paramS(@F_BANK_ID,24,2),0),'')
													else ''
												end
											, 1, LEN('. ' + char(13) + char(10)), '')
										else
											case 
												when FAF.GoAML_char_process(s5.iban_id,NULL,0) is not NULL
												then FAF.GoAML_char_process(s5.iban_id,FAF.GoAML_get_paramV(@F_BANK_ID,'ta__iban'),FAF.GoAML_get_paramL(@F_BANK_ID,'ta__iban')) + isnull(@clnt_txt,'')
												else 
													case
														when len(@clnt_txt)>2
														then right(@clnt_txt,len(@clnt_txt)-2)
														else NULL
													end 
											end
									end
								else NULL
							end
						,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__comments')) as "comments"
					from
						FAF.FA_MROS_KT_STAMM s5
					left join
						FAF.FA_MROS_KT_SALDO sld
					on s5.kt_lnr=sld.kt_lnr and s5.userbk_nr=sld.userbk_nr
					left join
						FAF.FA_MROS_INT_WRG wrg
					on s5.iso_wrg_cd=wrg.iso_wrg_cd and s5.userbk_nr=wrg.userbk_nr
					left join
						FAF.FA_MROS_REFERENCE_TABLE stz
					on s5.userbk_nr=stz.userbk_nr and cast(s5.sitz_nr as varchar)=stz.finnova_value and stz.ref_type=1
					left join
						FAF.FA_MROS_REFERENCE_TABLE ac_tp
					on s5.userbk_nr=ac_tp.userbk_nr and cast(s5.kt_art_ber_cd as varchar)=ac_tp.finnova_value and ac_tp.ref_type=9
					left join
						FAF.FA_MROS_REFERENCE_TABLE ac_tp1
					on s5.userbk_nr=ac_tp1.userbk_nr and cast(s5.arbgeb_nr as varchar)=ac_tp1.finnova_value and ac_tp1.ref_type=31
					left join
						FAF.FA_MROS_REFERENCE_TABLE stcd
					on s5.userbk_nr=stcd.userbk_nr and cast(s5.aufheb_cd as varchar)=stcd.finnova_value and stcd.ref_type=25
					where s5.kt_lnr=@kt_id and s5.userbk_nr=@F_BANK_ID
					FOR XML PATH (''),type, ELEMENTS);

				insert into @acnts select @kt_id as nID,@xml_ac as xml1,@xml_cl1 as xml2,@cl_id as clid,@flag as flg,@cl_tp as cltp;
			end;

			set @ii = @ii + 1;
		end;

		--payments
		if @nn_p>0
		begin
			set @ii = 1;
			while @ii <=@nn_p
			begin
				set @n_id = NULL;
				set @ac1_id = NULL;
				set @ac2_id = NULL;
				set @adr_nr = NULL;
				set @au_nr = NULL;
				set @om_nr = NULL;
				set @pmnt_tp = NULL;
				set @esr = NULL;
				set @flag0 = NULL;
				set @tr_dscr = NULL;
				set @d_txt = NULL;
				set @c_txt = NULL;
				set @keycd = 10;
				set @key_20 = NULL;
				set @key_30 = NULL;
				set @key_40 = NULL;
				set @key_50 = NULL;
				set @key_60 = NULL;
				set @key_70 = NULL;
				set @key_80 = NULL;
				set @key_90 = NULL;
				set @key_100 = NULL;

				select
					@n_id = pm_id,
					@ac1_id = a1_kt,
					@ac2_id = a2_kt,
					@adr_nr = adr_nr,
					@au_nr = au_nr,
					@om_nr = om_nr,
					@pmnt_tp = tp,
					@esr = esr,
					@flag0 = ap_flag,
					@shcd = sh_cd,
					@kt_orig = kt_orig,
					@key_20 = key_20,
					@key_30 = key_30,
					@key_40 = key_40,
					@key_50 = key_50,
					@key_60 = key_60,
					@key_70 = key_70,
					@key_80 = key_80,
					@key_90 = key_90,
					@key_100 = key_100
				from @pmnt_id where id=@ii;

				if @n_id is not NULL and @pmnt_tp is not NULL
				begin
					set @esr_txt = NULL;
					set @df_esr_txt = NULL;
					set @om_txt = NULL;
					set @com_txt = NULL;
					set @com1_txt = NULL;
					set @trd_txt = NULL;
					set @iban_txt = NULL;
					set @tmp_txt = NULL;
					set @D_AdrTp = NULL;
					set @D_Dept = NULL;
					set @D_SubDept = NULL;
					set @D_BldgNm = NULL;
					set @D_Flr = NULL;
					set @D_PstBx = NULL;
					set @D_Room = NULL;
					set @D_TwnLctnNm = NULL;
					set @D_DstrctNm = NULL;
					set @D_CtrySubDvsn = NULL;
					set @D_name = NULL;
					set @D_StrtNm = NULL;
					set @D_BldgNb = NULL;
					set @D_AdrLine = NULL;
					set @D_AdrLine_tmp = NULL;
					set @D_TwnNm = NULL;
					set @D_PstCd = NULL;
					set @D_Ctry = NULL;
					set @D_xml = NULL;
					set @C_AdrTp = NULL;
					set @C_Dept = NULL;
					set @C_SubDept = NULL;
					set @C_BldgNm = NULL;
					set @C_Flr = NULL;
					set @C_PstBx = NULL;
					set @C_Room = NULL;
					set @C_TwnLctnNm = NULL;
					set @C_DstrctNm = NULL;
					set @C_CtrySubDvsn = NULL;
					set @C_name = NULL;
					set @C_StrtNm = NULL;
					set @C_BldgNb = NULL;
					set @C_AdrLine = NULL;
					set @C_AdrLine_tmp = NULL;
					set @C_TwnNm = NULL;
					set @C_PstCd = NULL;
					set @C_Ctry = NULL;
					set @C_xml = NULL;
					set @xml_ac = NULL;
					set @xml_a2 = NULL;
					set @xml_cl = NULL;
					set @xml_t = NULL;
					set @xml_in = NULL;
					set @xml_out = NULL;
					set @flag1 = 0;
					set @flag2 = 0;
					set @cl_id = NULL;
					set @cl_tp = NULL;

					if @flag0>0
					begin
						set @flag1 = 
							(select 
								case when kd1.bank_flag>0 then 1 else 0 end
							from 
								FAF.FA_MROS_KT_STAMM kt1
							left join
								FAF.FA_MROS_KD_STAMM kd1
							on kd1.userbk_nr=kt1.userbk_nr and kd1.kd_lnr=kt1.kd_lnr and kd1.partner_flag=0
							where kt1.userbk_nr=@F_BANK_ID and kt1.kt_lnr=@ac1_id);

						set @flag2 = 
							(select 
								case when kd2.bank_flag>0 then 1 else 0 end
							from
								FAF.FA_MROS_KT_STAMM kt2
							left join
								FAF.FA_MROS_KD_STAMM kd2
							on kd2.userbk_nr=kt2.userbk_nr and kd2.kd_lnr=kt2.kd_lnr and kd2.partner_flag=0
							where kt2.userbk_nr=@F_BANK_ID and kt2.kt_lnr=@ac2_id);
					end;

					if @flag1+@flag2=1 
						set @flag0 = 1;
					else
						set @flag0 = 0;

					select 
						@tr_dscr = transaction_description,
						@d_txt = debit_text,
						@c_txt = credit_text,
						@keycd = key_cd
					from
						(select d.*,row_number() over (partition by d.userbk_nr order by d.priority_cd desc) as rn
						from
							(select @F_BANK_ID as userbk_nr,10 as key_cd
							union all
							select @F_BANK_ID as userbk_nr,20*@key_20 as key_cd
							union all
							select @F_BANK_ID as userbk_nr,30*@key_30 as key_cd
							union all
							select @F_BANK_ID as userbk_nr,40*@key_40*@flag0 as key_cd
							union all
							select @F_BANK_ID as userbk_nr,50*@key_50*@flag0 as key_cd
							union all
							select @F_BANK_ID as userbk_nr,60*@key_60 as key_cd
							union all
							select @F_BANK_ID as userbk_nr,70*@key_70 as key_cd
							union all
							select @F_BANK_ID as userbk_nr,80*@key_80 as key_cd
							union all
							select @F_BANK_ID as userbk_nr,90*@key_90*@flag0 as key_cd
							union all
							select @F_BANK_ID as userbk_nr,100*@key_100*@flag0 as key_cd) t
						join
							FAF.FA_MROS_TRANSACTION_DESCR d
						on d.userbk_nr=t.userbk_nr and d.key_cd=t.key_cd) s
					where rn=1;

					if @esr is not NULL
					begin
						if @esr = 'Post'
							set @df_esr_txt = 'Post';
						else
							begin
								set @df_esr_txt = 'PostESR';
								set @esr = right('                          ' + @esr,27);
								set @esr = ltrim(substring(@esr,1,2) + ' ' + substring(@esr,3,5) + ' ' + substring(@esr,8,5) + ' ' + substring(@esr,13,5) + ' ' + substring(@esr,18,5) + ' ' + substring(@esr,23,5));
							end;
					end;

					set @om_txt = (select top 1 FAF.GoAML_char_process_WR(text_rec,NULL,4000) from FAF.FA_MROS_OM_TEXT where om_text_lnr=@om_nr and userbk_nr=@F_BANK_ID);
					
					--seek iban in om_text.text_rec
					if @om_txt is not NULL
					begin
						if charindex('(90): ',@om_txt)>0
							set @iban_txt = substring(@om_txt,charindex('(90): ',@om_txt)+len('(90): '),len(@om_txt));
						else
							begin
								if charindex(' 90: ',@om_txt)>0
									set @iban_txt = substring(@om_txt,charindex(' 90: ',@om_txt)+len(' 90: '),len(@om_txt));
								else
									begin
										if charindex('(1090): ',@om_txt)>0
											begin
												set @iban_txt = substring(@om_txt,charindex('(1090): ',@om_txt)+len('(1090): '),len(@om_txt));
												if charindex('<IBAN>',@iban_txt)>0
												begin
													if charindex('</IBAN>',@iban_txt,charindex('<IBAN>',@iban_txt)+len('<IBAN>'))>0
														set @iban_txt = substring(@iban_txt,charindex('<IBAN>',@iban_txt)+len('<IBAN>'),charindex('</IBAN>',@iban_txt,charindex('<IBAN>',@iban_txt)+len('<IBAN>'))-charindex('<IBAN>',@iban_txt)-len('<IBAN>'));
													else
														set @iban_txt = substring(@iban_txt,charindex('<IBAN>',@iban_txt)+len('<IBAN>'),len(@iban_txt));
												end;
											end;
										else
											begin
												if charindex(' 1090: ',@om_txt)>0
													begin
														set @iban_txt = substring(@om_txt,charindex(' 1090: ',@om_txt)+len(' 1090: '),len(@om_txt));
														if charindex('<IBAN>',@iban_txt)>0
														begin
															if charindex('</IBAN>',@iban_txt,charindex('<IBAN>',@iban_txt)+len('<IBAN>'))>0
																set @iban_txt = substring(@iban_txt,charindex('<IBAN>',@iban_txt)+len('<IBAN>'),charindex('</IBAN>',@iban_txt,charindex('<IBAN>',@iban_txt)+len('<IBAN>'))-charindex('<IBAN>',@iban_txt)-len('<IBAN>'));
															else
																set @iban_txt = substring(@iban_txt,charindex('<IBAN>',@iban_txt)+len('<IBAN>'),len(@iban_txt));
														end;
													end;
												else
													begin
														if charindex(char(10) + '6/',@om_txt)>0
															set @iban_txt = substring(@om_txt,charindex(char(10) + '6/',@om_txt)+len(char(10) + '6/'),len(@om_txt));
														else
															begin
																if charindex('(1): ',@om_txt)>0
																	set @iban_txt = substring(@om_txt,charindex('(1): ',@om_txt)+len('(1): '),len(@om_txt));
																else
																	begin
																		if charindex(' 1: ',@om_txt)>0
																			set @iban_txt = substring(@om_txt,charindex(' 1: ',@om_txt)+len(' 1: '),len(@om_txt));
																		else
																			set @iban_txt = NULL;
																	end;
															end;
													end;
											end;
									end;
							end;
						if charindex(char(10),@iban_txt)>0
							set @iban_txt = substring(@iban_txt,1,charindex(char(10),@iban_txt)-1);
						set @iban_txt = replace(replace(@iban_txt,'/C/',''),'/','');
						if patindex('%[0-9a-Z][^0-9a-Z]%',@iban_txt)>=15
							set @iban_txt = substring(@iban_txt,1,patindex('%[0-9a-Z][^0-9a-Z]%',@iban_txt));
						set @iban_txt = FAF.GoAML_char_process(replace(@iban_txt,' ',''),NULL,34);
						if FAF.GoAML_get_paramS(@F_BANK_ID,137,1)='ok'
							and (len(@iban_txt)<15 
								or (select count(*) from FAF.FA_MROS_INT_LAND where userbk_nr=@F_BANK_ID and iso_land_cd=substring(@iban_txt,1,2))=0
								or substring(@iban_txt,3,2) not like '[0-9][0-9]')
						begin
							set @iban_txt = NULL;
						end;
					end;					

					--om_text_typ_cd = 1, 90 we take into comments, other between 1 and 90 into transaction_description
					if charindex('(1): ',@om_txt)>0
						begin
							set @tmp_txt = substring(@om_txt,1,charindex('; ',@om_txt+'; ',charindex('(1): ',@om_txt))-1);
							set @tmp_txt = reverse(@tmp_txt);
							set @tmp_txt = reverse(substring(@tmp_txt,1,charindex(' ;',@tmp_txt+' ;')-1));
							if left(@tmp_txt,1) in (' ',char(10),char(13))
								set @tmp_txt = right(@tmp_txt,len(@tmp_txt)-1);
							if left(@tmp_txt,1) in (' ',char(10),char(13))
								set @tmp_txt = right(@tmp_txt,len(@tmp_txt)-1);
							if left(@tmp_txt,1) in (' ',char(10),char(13))
								set @tmp_txt = right(@tmp_txt,len(@tmp_txt)-1);
							set @com_txt = case when @com_txt is NULL then '' else @com_txt + '; ' + char(13) + char(10) end + @tmp_txt;
						end;
					else
						begin
							if charindex(' 1: ',@om_txt)>0
							begin
								set @tmp_txt = substring(@om_txt,1,charindex('; ',@om_txt+'; ',charindex(' 1: ',@om_txt))-1);
								set @tmp_txt = reverse(@tmp_txt);
								set @tmp_txt = reverse(substring(@tmp_txt,1,charindex(' ;',@tmp_txt+' ;')-1));
								if left(@tmp_txt,1) in (' ',char(10),char(13))
									set @tmp_txt = right(@tmp_txt,len(@tmp_txt)-1);
								if left(@tmp_txt,1) in (' ',char(10),char(13))
									set @tmp_txt = right(@tmp_txt,len(@tmp_txt)-1);
								if left(@tmp_txt,1) in (' ',char(10),char(13))
									set @tmp_txt = right(@tmp_txt,len(@tmp_txt)-1);
								set @com_txt = case when @com_txt is NULL then '' else @com_txt + '; ' + char(13) + char(10) end + @tmp_txt;
							end;
						end;
					if charindex('(90): ',@om_txt)>0
						begin
							set @tmp_txt = substring(@om_txt,1,charindex('; ',@om_txt+'; ',charindex('(90): ',@om_txt))-1);
							set @tmp_txt = reverse(@tmp_txt);
							set @tmp_txt = reverse(substring(@tmp_txt,1,charindex(' ;',@tmp_txt+' ;')-1));
							if left(@tmp_txt,1) in (' ',char(10),char(13))
								set @tmp_txt = right(@tmp_txt,len(@tmp_txt)-1);
							if left(@tmp_txt,1) in (' ',char(10),char(13))
								set @tmp_txt = right(@tmp_txt,len(@tmp_txt)-1);
							if left(@tmp_txt,1) in (' ',char(10),char(13))
								set @tmp_txt = right(@tmp_txt,len(@tmp_txt)-1);
							set @com_txt = case when @com_txt is NULL then '' else @com_txt + '; ' + char(13) + char(10) end + @tmp_txt;
						end;
					else
						begin
							if charindex(' 90: ',@om_txt)>0
							begin
								set @tmp_txt = substring(@om_txt,1,charindex('; ',@om_txt+'; ',charindex(' 90: ',@om_txt))-1);
								set @tmp_txt = reverse(@tmp_txt);
								set @tmp_txt = reverse(substring(@tmp_txt,1,charindex(' ;',@tmp_txt+' ;')-1));
								if left(@tmp_txt,1) in (' ',char(10),char(13))
									set @tmp_txt = right(@tmp_txt,len(@tmp_txt)-1);
								if left(@tmp_txt,1) in (' ',char(10),char(13))
									set @tmp_txt = right(@tmp_txt,len(@tmp_txt)-1);
								if left(@tmp_txt,1) in (' ',char(10),char(13))
									set @tmp_txt = right(@tmp_txt,len(@tmp_txt)-1);
								set @com_txt = case when @com_txt is NULL then '' else @com_txt + '; ' + char(13) + char(10) end + @tmp_txt;
							end;
						end;
					--om_text_typ_cd = 3, 9, 21 we take into transaction_description
					--om_text_typ_cd = 8 goes into transaction_description too, but not for LI
					if charindex('(3): ',@om_txt)>0
						begin
							set @tmp_txt = substring(@om_txt,1,charindex('; ',@om_txt+'; ',charindex('(3): ',@om_txt))-1);
							set @tmp_txt = reverse(@tmp_txt);
							set @tmp_txt = reverse(substring(@tmp_txt,1,charindex(' ;',@tmp_txt+' ;')-1));
							if left(@tmp_txt,1) in (' ',char(10),char(13))
								set @tmp_txt = right(@tmp_txt,len(@tmp_txt)-1);
							if left(@tmp_txt,1) in (' ',char(10),char(13))
								set @tmp_txt = right(@tmp_txt,len(@tmp_txt)-1);
							if left(@tmp_txt,1) in (' ',char(10),char(13))
								set @tmp_txt = right(@tmp_txt,len(@tmp_txt)-1);
							set @trd_txt = case when @trd_txt is NULL then '' else @trd_txt + '; ' + char(13) + char(10) end + @tmp_txt;
						end;
					else
						begin
							if charindex(' 3: ',@om_txt)>0
							begin
								set @tmp_txt = substring(@om_txt,1,charindex('; ',@om_txt+'; ',charindex(' 3: ',@om_txt))-1);
								set @tmp_txt = reverse(@tmp_txt);
								set @tmp_txt = reverse(substring(@tmp_txt,1,charindex(' ;',@tmp_txt+' ;')-1));
								if left(@tmp_txt,1) in (' ',char(10),char(13))
									set @tmp_txt = right(@tmp_txt,len(@tmp_txt)-1);
								if left(@tmp_txt,1) in (' ',char(10),char(13))
									set @tmp_txt = right(@tmp_txt,len(@tmp_txt)-1);
								if left(@tmp_txt,1) in (' ',char(10),char(13))
									set @tmp_txt = right(@tmp_txt,len(@tmp_txt)-1);
								set @trd_txt = case when @trd_txt is NULL then '' else @trd_txt + '; ' + char(13) + char(10) end + @tmp_txt;
							end;
						end;
					if charindex('(8): ',@om_txt)>0
						begin
							set @tmp_txt = substring(@om_txt,1,charindex('; ',@om_txt+'; ',charindex('(8): ',@om_txt))-1);
							set @tmp_txt = reverse(@tmp_txt);
							set @tmp_txt = reverse(substring(@tmp_txt,1,charindex(' ;',@tmp_txt+' ;')-1));
							if left(@tmp_txt,1) in (' ',char(10),char(13))
								set @tmp_txt = right(@tmp_txt,len(@tmp_txt)-1);
							if left(@tmp_txt,1) in (' ',char(10),char(13))
								set @tmp_txt = right(@tmp_txt,len(@tmp_txt)-1);
							if left(@tmp_txt,1) in (' ',char(10),char(13))
								set @tmp_txt = right(@tmp_txt,len(@tmp_txt)-1);
							set @com1_txt = @tmp_txt;
						end;
					else
						begin
							if charindex(' 8: ',@om_txt)>0
							begin
								set @tmp_txt = substring(@om_txt,1,charindex('; ',@om_txt+'; ',charindex(' 8: ',@om_txt))-1);
								set @tmp_txt = reverse(@tmp_txt);
								set @tmp_txt = reverse(substring(@tmp_txt,1,charindex(' ;',@tmp_txt+' ;')-1));
								if left(@tmp_txt,1) in (' ',char(10),char(13))
									set @tmp_txt = right(@tmp_txt,len(@tmp_txt)-1);
								if left(@tmp_txt,1) in (' ',char(10),char(13))
									set @tmp_txt = right(@tmp_txt,len(@tmp_txt)-1);
								if left(@tmp_txt,1) in (' ',char(10),char(13))
									set @tmp_txt = right(@tmp_txt,len(@tmp_txt)-1);
								set @com1_txt = @tmp_txt;
							end;
						end;
					if @com1_txt is not NULL
					begin
						if FAF.GoAML_get_paramS(@F_BANK_ID,134,1)='ok'
							begin
								if charindex('(8): ',@com1_txt)>0
									set @com1_txt = substring(@com1_txt,charindex('(8): ',@com1_txt)+len('(8): '),len(@com1_txt));
								else
									set @com1_txt = substring(@com1_txt,charindex('8: ',@com1_txt)+len('8: '),len(@com1_txt));
							end;
						else
							set @trd_txt = case when @trd_txt is NULL then '' else @trd_txt + '; ' + char(13) + char(10) end + @com1_txt;
					end;
					if charindex('(9): ',@om_txt)>0
						begin
							set @tmp_txt = substring(@om_txt,1,charindex('; ',@om_txt+'; ',charindex('(9): ',@om_txt))-1);
							set @tmp_txt = reverse(@tmp_txt);
							set @tmp_txt = reverse(substring(@tmp_txt,1,charindex(' ;',@tmp_txt+' ;')-1));
							if left(@tmp_txt,1) in (' ',char(10),char(13))
								set @tmp_txt = right(@tmp_txt,len(@tmp_txt)-1);
							if left(@tmp_txt,1) in (' ',char(10),char(13))
								set @tmp_txt = right(@tmp_txt,len(@tmp_txt)-1);
							if left(@tmp_txt,1) in (' ',char(10),char(13))
								set @tmp_txt = right(@tmp_txt,len(@tmp_txt)-1);
							set @trd_txt = case when @trd_txt is NULL then '' else @trd_txt + '; ' + char(13) + char(10) end + @tmp_txt;
						end;
					else
						begin
							if charindex(' 9: ',@om_txt)>0
							begin
								set @tmp_txt = substring(@om_txt,1,charindex('; ',@om_txt+'; ',charindex(' 9: ',@om_txt))-1);
								set @tmp_txt = reverse(@tmp_txt);
								set @tmp_txt = reverse(substring(@tmp_txt,1,charindex(' ;',@tmp_txt+' ;')-1));
								if left(@tmp_txt,1) in (' ',char(10),char(13))
									set @tmp_txt = right(@tmp_txt,len(@tmp_txt)-1);
								if left(@tmp_txt,1) in (' ',char(10),char(13))
									set @tmp_txt = right(@tmp_txt,len(@tmp_txt)-1);
								if left(@tmp_txt,1) in (' ',char(10),char(13))
									set @tmp_txt = right(@tmp_txt,len(@tmp_txt)-1);
								set @trd_txt = case when @trd_txt is NULL then '' else @trd_txt + '; ' + char(13) + char(10) end + @tmp_txt;
							end;
						end;
					if charindex('(21): ',@om_txt)>0
						begin
							set @tmp_txt = substring(@om_txt,1,charindex('; ',@om_txt+'; ',charindex('(21): ',@om_txt))-1);
							set @tmp_txt = reverse(@tmp_txt);
							set @tmp_txt = reverse(substring(@tmp_txt,1,charindex(' ;',@tmp_txt+' ;')-1));
							if left(@tmp_txt,1) in (' ',char(10),char(13))
								set @tmp_txt = right(@tmp_txt,len(@tmp_txt)-1);
							if left(@tmp_txt,1) in (' ',char(10),char(13))
								set @tmp_txt = right(@tmp_txt,len(@tmp_txt)-1);
							if left(@tmp_txt,1) in (' ',char(10),char(13))
								set @tmp_txt = right(@tmp_txt,len(@tmp_txt)-1);
							set @trd_txt = case when @trd_txt is NULL then '' else @trd_txt + '; ' + char(13) + char(10) end + @tmp_txt;
						end;
					else
						begin
							if charindex(' 21: ',@om_txt)>0
							begin
								set @tmp_txt = substring(@om_txt,1,charindex('; ',@om_txt+'; ',charindex(' 21: ',@om_txt))-1);
								set @tmp_txt = reverse(@tmp_txt);
								set @tmp_txt = reverse(substring(@tmp_txt,1,charindex(' ;',@tmp_txt+' ;')-1));
								if left(@tmp_txt,1) in (' ',char(10),char(13))
									set @tmp_txt = right(@tmp_txt,len(@tmp_txt)-1);
								if left(@tmp_txt,1) in (' ',char(10),char(13))
									set @tmp_txt = right(@tmp_txt,len(@tmp_txt)-1);
								if left(@tmp_txt,1) in (' ',char(10),char(13))
									set @tmp_txt = right(@tmp_txt,len(@tmp_txt)-1);
								set @trd_txt = case when @trd_txt is NULL then '' else @trd_txt + '; ' + char(13) + char(10) end + @tmp_txt;
							end;
						end;

					if FAF.GoAML_get_paramS(@F_BANK_ID,106,1)='ok'
					begin
						--om_text_typ_cd = 1376 we take into t_entity for incoming payments
						set @tmp_txt = NULL;
						if charindex('(1376): ',@om_txt)>0
							set @tmp_txt = substring(@om_txt,charindex('(1376): ',@om_txt)+len('(1376): '),len(@om_txt));
						else
							begin
								if charindex('1376: ',@om_txt)>0
									set @tmp_txt = substring(@om_txt,charindex('1376: ',@om_txt)+len('1376: '),len(@om_txt));
							end;
						if len(@tmp_txt)>0
						begin
							if charindex('; ',@tmp_txt)>0
								set @tmp_txt = substring(@tmp_txt,1,charindex('; ',@tmp_txt)-1);
							if charindex('<Nm>',@tmp_txt)>0
							begin
								set @D_name=substring(@tmp_txt,charindex('<Nm>',@tmp_txt)+len('<Nm>'),len(@tmp_txt));
								if charindex('</Nm>',@D_name)>0
									set @D_name=substring(@D_name,1,charindex('</Nm>',@D_name)-1);
								set @D_name = FAF.GoAML_char_process(@D_name,NULL,NULL)
							end;
							if charindex('<PstlAdr>',@tmp_txt)>0
							begin
								set @tmp_txt=substring(@tmp_txt,charindex('<PstlAdr>',@tmp_txt)+len('<PstlAdr>'),len(@tmp_txt));
								if charindex('</PstlAdr>',@tmp_txt)>0
									set @tmp_txt=substring(@tmp_txt,1,charindex('</PstlAdr>',@tmp_txt)-1);
								if charindex('<StrtNm>',@tmp_txt)>0
								begin
									set @D_StrtNm=substring(@tmp_txt,charindex('<StrtNm>',@tmp_txt)+len('<StrtNm>'),len(@tmp_txt));
									if charindex('</StrtNm>',@D_StrtNm)>0
										set @D_StrtNm=substring(@D_StrtNm,1,charindex('</StrtNm>',@D_StrtNm)-1);
									set @D_StrtNm = FAF.GoAML_char_process(@D_StrtNm,NULL,NULL)
								end;
								if charindex('<BldgNb>',@tmp_txt)>0
								begin
									set @D_BldgNb=substring(@tmp_txt,charindex('<BldgNb>',@tmp_txt)+len('<BldgNb>'),len(@tmp_txt));
									if charindex('</BldgNb>',@D_BldgNb)>0
										set @D_BldgNb=substring(@D_BldgNb,1,charindex('</BldgNb>',@D_BldgNb)-1);
									set @D_BldgNb = FAF.GoAML_char_process(@D_BldgNb,NULL,NULL)
								end;
								if charindex('<PstCd>',@tmp_txt)>0
								begin
									set @D_PstCd=substring(@tmp_txt,charindex('<PstCd>',@tmp_txt)+len('<PstCd>'),len(@tmp_txt));
									if charindex('</PstCd>',@D_PstCd)>0
										set @D_PstCd=substring(@D_PstCd,1,charindex('</PstCd>',@D_PstCd)-1);
									set @D_PstCd = FAF.GoAML_char_process(@D_PstCd,NULL,NULL)
								end;
								if charindex('<TwnNm>',@tmp_txt)>0
								begin
									set @D_TwnNm=substring(@tmp_txt,charindex('<TwnNm>',@tmp_txt)+len('<TwnNm>'),len(@tmp_txt));
									if charindex('</TwnNm>',@D_TwnNm)>0
										set @D_TwnNm=substring(@D_TwnNm,1,charindex('</TwnNm>',@D_TwnNm)-1);
									set @D_TwnNm = FAF.GoAML_char_process(@D_TwnNm,NULL,NULL)
								end;
								if charindex('<Ctry>',@tmp_txt)>0
								begin
									set @D_Ctry=substring(@tmp_txt,charindex('<Ctry>',@tmp_txt)+len('<Ctry>'),len(@tmp_txt));
									if charindex('</Ctry>',@D_Ctry)>0
										set @D_Ctry=substring(@D_Ctry,1,charindex('</Ctry>',@D_Ctry)-1);
									set @D_Ctry = FAF.GoAML_char_process(@D_Ctry,NULL,NULL)
								end;
								if charindex('<AdrTp>',@tmp_txt)>0
								begin
									set @D_AdrTp=substring(@tmp_txt,charindex('<AdrTp>',@tmp_txt)+len('<AdrTp>'),len(@tmp_txt));
									if charindex('</AdrTp>',@D_AdrTp)>0
										set @D_AdrTp=substring(@D_AdrTp,1,charindex('</AdrTp>',@D_AdrTp)-1);
									set @D_AdrTp = FAF.GoAML_char_process(@D_AdrTp,NULL,NULL)
								end;
								if charindex('<Dept>',@tmp_txt)>0
								begin
									set @D_Dept=substring(@tmp_txt,charindex('<Dept>',@tmp_txt)+len('<Dept>'),len(@tmp_txt));
									if charindex('</Dept>',@D_Dept)>0
										set @D_Dept=substring(@D_Dept,1,charindex('</Dept>',@D_Dept)-1);
									set @D_Dept = FAF.GoAML_char_process(@D_Dept,NULL,NULL)
								end;
								if charindex('<SubDept>',@tmp_txt)>0
								begin
									set @D_SubDept=substring(@tmp_txt,charindex('<SubDept>',@tmp_txt)+len('<SubDept>'),len(@tmp_txt));
									if charindex('</SubDept>',@D_SubDept)>0
										set @D_SubDept=substring(@D_SubDept,1,charindex('</SubDept>',@D_SubDept)-1);
									set @D_SubDept = FAF.GoAML_char_process(@D_SubDept,NULL,NULL)
								end;
								if charindex('<BldgNm>',@tmp_txt)>0
								begin
									set @D_BldgNm=substring(@tmp_txt,charindex('<BldgNm>',@tmp_txt)+len('<BldgNm>'),len(@tmp_txt));
									if charindex('</BldgNm>',@D_BldgNm)>0
										set @D_BldgNm=substring(@D_BldgNm,1,charindex('</BldgNm>',@D_BldgNm)-1);
									set @D_BldgNm = FAF.GoAML_char_process(@D_BldgNm,NULL,NULL)
								end;
								if charindex('<Flr>',@tmp_txt)>0
								begin
									set @D_Flr=substring(@tmp_txt,charindex('<Flr>',@tmp_txt)+len('<Flr>'),len(@tmp_txt));
									if charindex('</Flr>',@D_Flr)>0
										set @D_Flr=substring(@D_Flr,1,charindex('</Flr>',@D_Flr)-1);
									set @D_Flr = FAF.GoAML_char_process(@D_Flr,NULL,NULL)
								end;
								if charindex('<PstBx>',@tmp_txt)>0
								begin
									set @D_PstBx=substring(@tmp_txt,charindex('<PstBx>',@tmp_txt)+len('<PstBx>'),len(@tmp_txt));
									if charindex('</PstBx>',@D_PstBx)>0
										set @D_PstBx=substring(@D_PstBx,1,charindex('</PstBx>',@D_PstBx)-1);
									set @D_PstBx = FAF.GoAML_char_process(@D_PstBx,NULL,NULL)
								end;
								if charindex('<Room>',@tmp_txt)>0
								begin
									set @D_Room=substring(@tmp_txt,charindex('<Room>',@tmp_txt)+len('<Room>'),len(@tmp_txt));
									if charindex('</Room>',@D_Room)>0
										set @D_Room=substring(@D_Room,1,charindex('</Room>',@D_Room)-1);
									set @D_Room = FAF.GoAML_char_process(@D_Room,NULL,NULL)
								end;
								if charindex('<TwnLctnNm>',@tmp_txt)>0
								begin
									set @D_TwnLctnNm=substring(@tmp_txt,charindex('<TwnLctnNm>',@tmp_txt)+len('<TwnLctnNm>'),len(@tmp_txt));
									if charindex('</TwnLctnNm>',@D_TwnLctnNm)>0
										set @D_TwnLctnNm=substring(@D_TwnLctnNm,1,charindex('</TwnLctnNm>',@D_TwnLctnNm)-1);
									set @D_TwnLctnNm = FAF.GoAML_char_process(@D_TwnLctnNm,NULL,NULL)
								end;
								if charindex('<DstrctNm>',@tmp_txt)>0
								begin
									set @D_DstrctNm=substring(@tmp_txt,charindex('<DstrctNm>',@tmp_txt)+len('<DstrctNm>'),len(@tmp_txt));
									if charindex('</DstrctNm>',@D_DstrctNm)>0
										set @D_DstrctNm=substring(@D_DstrctNm,1,charindex('</DstrctNm>',@D_DstrctNm)-1);
									set @D_DstrctNm = FAF.GoAML_char_process(@D_DstrctNm,NULL,NULL)
								end;
								if charindex('<CtrySubDvsn>',@tmp_txt)>0
								begin
									set @D_CtrySubDvsn=substring(@tmp_txt,charindex('<CtrySubDvsn>',@tmp_txt)+len('<CtrySubDvsn>'),len(@tmp_txt));
									if charindex('</CtrySubDvsn>',@D_CtrySubDvsn)>0
										set @D_CtrySubDvsn=substring(@D_CtrySubDvsn,1,charindex('</CtrySubDvsn>',@D_CtrySubDvsn)-1);
									set @D_CtrySubDvsn = FAF.GoAML_char_process(@D_CtrySubDvsn,NULL,NULL)
								end;
								set @ll = charindex('<AdrLine>',@tmp_txt);
								while @ll>0
								begin
									set @D_AdrLine_tmp=substring(@tmp_txt,@ll+len('<AdrLine>'),len(@tmp_txt));
									if charindex('</AdrLine>',@D_AdrLine_tmp)>0
										set @D_AdrLine_tmp=substring(@D_AdrLine_tmp,1,charindex('</AdrLine>',@D_AdrLine_tmp)-1);
									set @D_AdrLine = case when len(@D_AdrLine)>0 then @D_AdrLine + ', ' else '' end + FAF.GoAML_char_process(@D_AdrLine_tmp,NULL,NULL);
									set @ll = charindex('<AdrLine>',@tmp_txt,@ll+len('<AdrLine>'));
								end;

								if len(@D_StrtNm)>0
								begin
									set @D_AdrLine = @D_StrtNm + 
										case when len(@D_BldgNb)>0 then ' ' + @D_BldgNb else '' end +
										case when len(@D_BldgNm)>0 then ' ' + @D_BldgNm else '' end + 
										case when len(@D_Flr)>0 then ', floor ' + @D_Flr else '' end + 
										case when len(@D_Room)>0 then ', room ' + @D_Room else '' end + 
										case when len(@D_AdrLine)>0 then ', ' + @D_AdrLine else '' end;
								end;

								if len(@D_DstrctNm)>0 and len(@D_CtrySubDvsn)>0 
									set @D_DstrctNm = @D_DstrctNm + ', ' + @D_CtrySubDvsn;
								else
									set @D_DstrctNm = isnull(@D_DstrctNm,'') + isnull(@D_CtrySubDvsn,'');
								
								if len(@D_Dept)>0 and len(@D_SubDept)>0
									set @D_Dept = 'Departjment: ' + @D_Dept + ', Sub Department: ' + @D_SubDept;
								else
									begin
										set @D_Dept = 
											case when len(@D_Dept)>0 then 'Departjment: ' + @D_Dept else '' end + 
											case when len(@D_SubDept)>0 then 'Sub Department: ' + @D_SubDept else '' end;
									end;

								if len(@D_Dept)>0 and len(@D_PstBx)>0
									set @D_Dept = @D_Dept + char(13) + char(10) + 'Post Box: ' + @D_PstBx;
								else
									set @D_Dept = isnull(@D_Dept,'') + case when len(@D_PstBx)>0 then 'Post Box: ' + @D_PstBx else '' end;

								if FAF.GoAML_get_paramS(@F_BANK_ID,135,2) = 'ok'
								begin
									if len(@D_Dept)>0 and len(@D_AdrTp)>0
										begin
											set @D_Dept = @D_Dept +  
												case @D_AdrTp
													when 'ADDR' then char(13) + char(10) + 'Address is the complete postal address'
													when 'PBOX' then char(13) + char(10) + 'Address is a postal office (PO) box'
													when 'MLTO' then char(13) + char(10) + 'Address is the address to which mail is sent'
													when 'DLVY' then char(13) + char(10) + 'Address is the address to which delivery is to take place'
													else ''
												end;
										end;
									else
										begin
											set @D_Dept = isnull(@D_Dept,'') +  
												case @D_AdrTp
													when 'ADDR' then 'Address is the complete postal address'
													when 'PBOX' then 'Address is a postal office (PO) box'
													when 'MLTO' then 'Address is the address to which mail is sent'
													when 'DLVY' then 'Address is the address to which delivery is to take place'
													else ''
												end;
										end;
								end;
							end;
							if len(@D_name)>0
							begin
								if FAF.GoAML_get_paramS(@F_BANK_ID,136,1)='ok'
									begin
										set @D_xml = 
											(select
												FAF.GoAML_get_paramS(@F_BANK_ID,136,2) as "account_entity_relation",
												(select
													@D_name as "name",
													case
														when len(@D_AdrLine)>0
														then
															(select
																FAF.GoAML_char_process(
																	case
																		when @D_AdrTp='HOME' then FAF.GoAML_get_paramS(@F_BANK_ID,8,2)
																		when @D_AdrTp='BIZZ' then FAF.GoAML_get_paramS(@F_BANK_ID,7,2)
																		when @D_AdrTp in ('ADDR','PBOX','MLTO','DLVY') then FAF.GoAML_get_paramS(@F_BANK_ID,135,1)
																		else NULL
																	end
																,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address_type'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__address_type')) as "address_type",
																FAF.GoAML_char_process_WR(@D_AdrLine,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__address')) as "address",
																FAF.GoAML_char_process(@D_TwnLctnNm,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__town'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__town')) as "town",
																FAF.GoAML_char_process(@D_TwnNm,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__city'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__city')) as "city",
																FAF.GoAML_char_process(@D_PstCd,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__zip'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__zip')) as "zip",
																FAF.GoAML_char_process(@D_Ctry,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__country_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__country_code')) as "country_code",
																FAF.GoAML_char_process(@D_DstrctNm,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__state'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__state')) as "state",
																FAF.GoAML_char_process_WR(@D_Dept,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__comments')) as "comments"
															FOR XML PATH ('address'),type, ELEMENTS)
														else NULL
													end as "addresses"
												FOR XML PATH (''),type, ELEMENTS) as "entity"
											FOR XML PATH ('account_related_entity'),type, ELEMENTS);
									end;
								else
									begin
										set @D_xml = 
											(select
												@D_name as "name",
												case
													when len(@D_AdrLine)>0
													then
														(select
															FAF.GoAML_char_process(
																case
																	when @D_AdrTp='HOME' then FAF.GoAML_get_paramS(@F_BANK_ID,8,2)
																	when @D_AdrTp='BIZZ' then FAF.GoAML_get_paramS(@F_BANK_ID,7,2)
																	when @D_AdrTp in ('ADDR','PBOX','MLTO','DLVY') then FAF.GoAML_get_paramS(@F_BANK_ID,135,1)
																	else NULL
																end
															,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address_type'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__address_type')) as "address_type",
															FAF.GoAML_char_process_WR(@D_AdrLine,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__address')) as "address",
															FAF.GoAML_char_process(@D_TwnLctnNm,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__town'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__town')) as "town",
															FAF.GoAML_char_process(@D_TwnNm,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__city'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__city')) as "city",
															FAF.GoAML_char_process(@D_PstCd,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__zip'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__zip')) as "zip",
															FAF.GoAML_char_process(@D_Ctry,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__country_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__country_code')) as "country_code",
															FAF.GoAML_char_process(@D_DstrctNm,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__state'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__state')) as "state",
															FAF.GoAML_char_process_WR(@D_Dept,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__comments')) as "comments"
														FOR XML PATH ('address'),type, ELEMENTS)
													else NULL
												end as "addresses"
											FOR XML PATH ('t_entity'),type, ELEMENTS);
									end;
							end;
						end;
						if @D_xml is not NULL
						begin
							--om_text_typ_cd = 1337 we take into related_entities and only if 1376 exists
							set @tmp_txt = NULL;
							set @D_AdrTp = NULL;
							set @D_Dept = NULL;
							set @D_SubDept = NULL;
							set @D_BldgNm = NULL;
							set @D_Flr = NULL;
							set @D_PstBx = NULL;
							set @D_Room = NULL;
							set @D_TwnLctnNm = NULL;
							set @D_DstrctNm = NULL;
							set @D_CtrySubDvsn = NULL;
							set @D_name = NULL;
							set @D_StrtNm = NULL;
							set @D_BldgNb = NULL;
							set @D_AdrLine = NULL;
							set @D_AdrLine_tmp = NULL;
							set @D_TwnNm = NULL;
							set @D_PstCd = NULL;
							set @D_Ctry = NULL;

							if charindex('(1337): ',@om_txt)>0
								set @tmp_txt = substring(@om_txt,charindex('(1337): ',@om_txt)+len('(1337): '),len(@om_txt));
							else
								begin
									if charindex('1337: ',@om_txt)>0
										set @tmp_txt = substring(@om_txt,charindex('1337: ',@om_txt)+len('1337: '),len(@om_txt));
								end;
							if len(@tmp_txt)>0
							begin
								if charindex('; ',@tmp_txt)>0
									set @tmp_txt = substring(@tmp_txt,1,charindex('; ',@tmp_txt)-1);
								if charindex('<Nm>',@tmp_txt)>0
								begin
									set @D_name=substring(@tmp_txt,charindex('<Nm>',@tmp_txt)+len('<Nm>'),len(@tmp_txt));
									if charindex('</Nm>',@D_name)>0
										set @D_name=substring(@D_name,1,charindex('</Nm>',@D_name)-1);
									set @D_name = FAF.GoAML_char_process(@D_name,NULL,NULL)
								end;
								if charindex('<PstlAdr>',@tmp_txt)>0
								begin
									set @tmp_txt=substring(@tmp_txt,charindex('<PstlAdr>',@tmp_txt)+len('<PstlAdr>'),len(@tmp_txt));
									if charindex('</PstlAdr>',@tmp_txt)>0
										set @tmp_txt=substring(@tmp_txt,1,charindex('</PstlAdr>',@tmp_txt)-1);
									if charindex('<StrtNm>',@tmp_txt)>0
									begin
										set @D_StrtNm=substring(@tmp_txt,charindex('<StrtNm>',@tmp_txt)+len('<StrtNm>'),len(@tmp_txt));
										if charindex('</StrtNm>',@D_StrtNm)>0
											set @D_StrtNm=substring(@D_StrtNm,1,charindex('</StrtNm>',@D_StrtNm)-1);
										set @D_StrtNm = FAF.GoAML_char_process(@D_StrtNm,NULL,NULL)
									end;
									if charindex('<BldgNb>',@tmp_txt)>0
									begin
										set @D_BldgNb=substring(@tmp_txt,charindex('<BldgNb>',@tmp_txt)+len('<BldgNb>'),len(@tmp_txt));
										if charindex('</BldgNb>',@D_BldgNb)>0
											set @D_BldgNb=substring(@D_BldgNb,1,charindex('</BldgNb>',@D_BldgNb)-1);
										set @D_BldgNb = FAF.GoAML_char_process(@D_BldgNb,NULL,NULL)
									end;
									if charindex('<PstCd>',@tmp_txt)>0
									begin
										set @D_PstCd=substring(@tmp_txt,charindex('<PstCd>',@tmp_txt)+len('<PstCd>'),len(@tmp_txt));
										if charindex('</PstCd>',@D_PstCd)>0
											set @D_PstCd=substring(@D_PstCd,1,charindex('</PstCd>',@D_PstCd)-1);
										set @D_PstCd = FAF.GoAML_char_process(@D_PstCd,NULL,NULL)
									end;
									if charindex('<TwnNm>',@tmp_txt)>0
									begin
										set @D_TwnNm=substring(@tmp_txt,charindex('<TwnNm>',@tmp_txt)+len('<TwnNm>'),len(@tmp_txt));
										if charindex('</TwnNm>',@D_TwnNm)>0
											set @D_TwnNm=substring(@D_TwnNm,1,charindex('</TwnNm>',@D_TwnNm)-1);
										set @D_TwnNm = FAF.GoAML_char_process(@D_TwnNm,NULL,NULL)
									end;
									if charindex('<Ctry>',@tmp_txt)>0
									begin
										set @D_Ctry=substring(@tmp_txt,charindex('<Ctry>',@tmp_txt)+len('<Ctry>'),len(@tmp_txt));
										if charindex('</Ctry>',@D_Ctry)>0
											set @D_Ctry=substring(@D_Ctry,1,charindex('</Ctry>',@D_Ctry)-1);
										set @D_Ctry = FAF.GoAML_char_process(@D_Ctry,NULL,NULL)
									end;
									if charindex('<AdrTp>',@tmp_txt)>0
									begin
										set @D_AdrTp=substring(@tmp_txt,charindex('<AdrTp>',@tmp_txt)+len('<AdrTp>'),len(@tmp_txt));
										if charindex('</AdrTp>',@D_AdrTp)>0
											set @D_AdrTp=substring(@D_AdrTp,1,charindex('</AdrTp>',@D_AdrTp)-1);
										set @D_AdrTp = FAF.GoAML_char_process(@D_AdrTp,NULL,NULL)
									end;
									if charindex('<Dept>',@tmp_txt)>0
									begin
										set @D_Dept=substring(@tmp_txt,charindex('<Dept>',@tmp_txt)+len('<Dept>'),len(@tmp_txt));
										if charindex('</Dept>',@D_Dept)>0
											set @D_Dept=substring(@D_Dept,1,charindex('</Dept>',@D_Dept)-1);
										set @D_Dept = FAF.GoAML_char_process(@D_Dept,NULL,NULL)
									end;
									if charindex('<SubDept>',@tmp_txt)>0
									begin
										set @D_SubDept=substring(@tmp_txt,charindex('<SubDept>',@tmp_txt)+len('<SubDept>'),len(@tmp_txt));
										if charindex('</SubDept>',@D_SubDept)>0
											set @D_SubDept=substring(@D_SubDept,1,charindex('</SubDept>',@D_SubDept)-1);
										set @D_SubDept = FAF.GoAML_char_process(@D_SubDept,NULL,NULL)
									end;
									if charindex('<BldgNm>',@tmp_txt)>0
									begin
										set @D_BldgNm=substring(@tmp_txt,charindex('<BldgNm>',@tmp_txt)+len('<BldgNm>'),len(@tmp_txt));
										if charindex('</BldgNm>',@D_BldgNm)>0
											set @D_BldgNm=substring(@D_BldgNm,1,charindex('</BldgNm>',@D_BldgNm)-1);
										set @D_BldgNm = FAF.GoAML_char_process(@D_BldgNm,NULL,NULL)
									end;
									if charindex('<Flr>',@tmp_txt)>0
									begin
										set @D_Flr=substring(@tmp_txt,charindex('<Flr>',@tmp_txt)+len('<Flr>'),len(@tmp_txt));
										if charindex('</Flr>',@D_Flr)>0
											set @D_Flr=substring(@D_Flr,1,charindex('</Flr>',@D_Flr)-1);
										set @D_Flr = FAF.GoAML_char_process(@D_Flr,NULL,NULL)
									end;
									if charindex('<PstBx>',@tmp_txt)>0
									begin
										set @D_PstBx=substring(@tmp_txt,charindex('<PstBx>',@tmp_txt)+len('<PstBx>'),len(@tmp_txt));
										if charindex('</PstBx>',@D_PstBx)>0
											set @D_PstBx=substring(@D_PstBx,1,charindex('</PstBx>',@D_PstBx)-1);
										set @D_PstBx = FAF.GoAML_char_process(@D_PstBx,NULL,NULL)
									end;
									if charindex('<Room>',@tmp_txt)>0
									begin
										set @D_Room=substring(@tmp_txt,charindex('<Room>',@tmp_txt)+len('<Room>'),len(@tmp_txt));
										if charindex('</Room>',@D_Room)>0
											set @D_Room=substring(@D_Room,1,charindex('</Room>',@D_Room)-1);
										set @D_Room = FAF.GoAML_char_process(@D_Room,NULL,NULL)
									end;
									if charindex('<TwnLctnNm>',@tmp_txt)>0
									begin
										set @D_TwnLctnNm=substring(@tmp_txt,charindex('<TwnLctnNm>',@tmp_txt)+len('<TwnLctnNm>'),len(@tmp_txt));
										if charindex('</TwnLctnNm>',@D_TwnLctnNm)>0
											set @D_TwnLctnNm=substring(@D_TwnLctnNm,1,charindex('</TwnLctnNm>',@D_TwnLctnNm)-1);
										set @D_TwnLctnNm = FAF.GoAML_char_process(@D_TwnLctnNm,NULL,NULL)
									end;
									if charindex('<DstrctNm>',@tmp_txt)>0
									begin
										set @D_DstrctNm=substring(@tmp_txt,charindex('<DstrctNm>',@tmp_txt)+len('<DstrctNm>'),len(@tmp_txt));
										if charindex('</DstrctNm>',@D_DstrctNm)>0
											set @D_DstrctNm=substring(@D_DstrctNm,1,charindex('</DstrctNm>',@D_DstrctNm)-1);
										set @D_DstrctNm = FAF.GoAML_char_process(@D_DstrctNm,NULL,NULL)
									end;
									if charindex('<CtrySubDvsn>',@tmp_txt)>0
									begin
										set @D_CtrySubDvsn=substring(@tmp_txt,charindex('<CtrySubDvsn>',@tmp_txt)+len('<CtrySubDvsn>'),len(@tmp_txt));
										if charindex('</CtrySubDvsn>',@D_CtrySubDvsn)>0
											set @D_CtrySubDvsn=substring(@D_CtrySubDvsn,1,charindex('</CtrySubDvsn>',@D_CtrySubDvsn)-1);
										set @D_CtrySubDvsn = FAF.GoAML_char_process(@D_CtrySubDvsn,NULL,NULL)
									end;
									set @ll = charindex('<AdrLine>',@tmp_txt);
									while @ll>0
									begin
										set @D_AdrLine_tmp=substring(@tmp_txt,@ll+len('<AdrLine>'),len(@tmp_txt));
										if charindex('</AdrLine>',@D_AdrLine_tmp)>0
											set @D_AdrLine_tmp=substring(@D_AdrLine_tmp,1,charindex('</AdrLine>',@D_AdrLine_tmp)-1);
										set @D_AdrLine = case when len(@D_AdrLine)>0 then @D_AdrLine + ', ' else '' end + FAF.GoAML_char_process(@D_AdrLine_tmp,NULL,NULL);
										set @ll = charindex('<AdrLine>',@tmp_txt,@ll+len('<AdrLine>'));
									end;
									
									if len(@D_StrtNm)>0
									begin
										set @D_AdrLine = @D_StrtNm + 
											case when len(@D_BldgNb)>0 then ' ' + @D_BldgNb else '' end +
											case when len(@D_BldgNm)>0 then ' ' + @D_BldgNm else '' end + 
											case when len(@D_Flr)>0 then ', floor ' + @D_Flr else '' end + 
											case when len(@D_Room)>0 then ', room ' + @D_Room else '' end + 
											case when len(@D_AdrLine)>0 then ', ' + @D_AdrLine else '' end;
									end;

									if len(@D_DstrctNm)>0 and len(@D_CtrySubDvsn)>0 
										set @D_DstrctNm = @D_DstrctNm + ', ' + @D_CtrySubDvsn;
									else
										set @D_DstrctNm = isnull(@D_DstrctNm,'') + isnull(@D_CtrySubDvsn,'');
									
									if len(@D_Dept)>0 and len(@D_SubDept)>0
										set @D_Dept = 'Departjment: ' + @D_Dept + ', Sub Department: ' + @D_SubDept;
									else
										begin
											set @D_Dept = 
												case when len(@D_Dept)>0 then 'Departjment: ' + @D_Dept else '' end + 
												case when len(@D_SubDept)>0 then 'Sub Department: ' + @D_SubDept else '' end;
										end;

									if len(@D_Dept)>0 and len(@D_PstBx)>0
										set @D_Dept = @D_Dept + char(13) + char(10) + 'Post Box: ' + @D_PstBx;
									else
										set @D_Dept = isnull(@D_Dept,'') + case when len(@D_PstBx)>0 then 'Post Box: ' + @D_PstBx else '' end;

									if FAF.GoAML_get_paramS(@F_BANK_ID,135,2) = 'ok'
									begin
										if len(@D_Dept)>0 and len(@D_AdrTp)>0
											begin
												set @D_Dept = @D_Dept +  
													case @D_AdrTp
														when 'ADDR' then char(13) + char(10) + 'Address is the complete postal address'
														when 'PBOX' then char(13) + char(10) + 'Address is a postal office (PO) box'
														when 'MLTO' then char(13) + char(10) + 'Address is the address to which mail is sent'
														when 'DLVY' then char(13) + char(10) + 'Address is the address to which delivery is to take place'
														else ''
													end;
											end;
										else
											begin
												set @D_Dept = isnull(@D_Dept,'') +  
													case @D_AdrTp
														when 'ADDR' then 'Address is the complete postal address'
														when 'PBOX' then 'Address is a postal office (PO) box'
														when 'MLTO' then 'Address is the address to which mail is sent'
														when 'DLVY' then 'Address is the address to which delivery is to take place'
														else ''
													end;
											end;
									end;
								end;
								if len(@D_name)>0
								begin
									if FAF.GoAML_get_paramS(@F_BANK_ID,136,1)='ok'
										begin
											set @D_xml = 
												(select
													@D_xml,
													(select
														FAF.GoAML_get_paramV(@F_BANK_ID,'are__account_entity_relation') as "account_entity_relation",
														(select
															@D_name as "name",
															case
																when len(@D_AdrLine)>0
																then
																	(select
																		FAF.GoAML_char_process(
																			case
																				when @D_AdrTp='HOME' then FAF.GoAML_get_paramS(@F_BANK_ID,8,2)
																				when @D_AdrTp='BIZZ' then FAF.GoAML_get_paramS(@F_BANK_ID,7,2)
																				when @D_AdrTp in ('ADDR','PBOX','MLTO','DLVY') then FAF.GoAML_get_paramS(@F_BANK_ID,135,1)
																				else NULL
																			end
																		,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address_type'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__address_type')) as "address_type",
																		FAF.GoAML_char_process_WR(@D_AdrLine,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__address')) as "address",
																		FAF.GoAML_char_process(@D_TwnLctnNm,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__town'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__town')) as "town",
																		FAF.GoAML_char_process(@D_TwnNm,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__city'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__city')) as "city",
																		FAF.GoAML_char_process(@D_PstCd,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__zip'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__zip')) as "zip",
																		FAF.GoAML_char_process(@D_Ctry,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__country_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__country_code')) as "country_code",
																		FAF.GoAML_char_process(@D_DstrctNm,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__state'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__state')) as "state",
																		FAF.GoAML_char_process_WR(@D_Dept,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__comments')) as "comments"
																	FOR XML PATH ('address'),type, ELEMENTS)
																else NULL
															end as "addresses"
														FOR XML PATH (''),type, ELEMENTS) as "entity"
													FOR XML PATH ('account_related_entity'),type, ELEMENTS)
												FOR XML PATH ('related_entities'),type, ELEMENTS);
										end;
									else
										begin
											set @D_xml = 
												(select
													@D_xml,
													(select
														FAF.GoAML_get_paramV(@F_BANK_ID,'are__account_entity_relation') as "account_entity_relation",
														(select
															@D_name as "name",
															case
																when len(@D_AdrLine)>0
																then
																	(select
																		FAF.GoAML_char_process(
																			case
																				when @D_AdrTp='HOME' then FAF.GoAML_get_paramS(@F_BANK_ID,8,2)
																				when @D_AdrTp='BIZZ' then FAF.GoAML_get_paramS(@F_BANK_ID,7,2)
																				when @D_AdrTp in ('ADDR','PBOX','MLTO','DLVY') then FAF.GoAML_get_paramS(@F_BANK_ID,135,1)
																				else NULL
																			end
																		,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address_type'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__address_type')) as "address_type",
																		FAF.GoAML_char_process_WR(@D_AdrLine,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__address')) as "address",
																		FAF.GoAML_char_process(@D_TwnLctnNm,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__town'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__town')) as "town",
																		FAF.GoAML_char_process(@D_TwnNm,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__city'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__city')) as "city",
																		FAF.GoAML_char_process(@D_PstCd,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__zip'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__zip')) as "zip",
																		FAF.GoAML_char_process(@D_Ctry,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__country_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__country_code')) as "country_code",
																		FAF.GoAML_char_process(@D_DstrctNm,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__state'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__state')) as "state",
																		FAF.GoAML_char_process_WR(@D_Dept,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__comments')) as "comments"
																	FOR XML PATH ('address'),type, ELEMENTS)
																else NULL
															end as "addresses"
														FOR XML PATH ('entity'),type, ELEMENTS)
													FOR XML PATH ('account_related_entity'),type, ELEMENTS) as "related_entities"
												FOR XML PATH (''),type, ELEMENTS);
										end;
								end;
							end;
						end;

						--om_text_typ_cd = 1379 we take into t_entity for outgoing payments
						set @tmp_txt = NULL;
						if charindex('(1379): ',@om_txt)>0
							set @tmp_txt = substring(@om_txt,charindex('(1379): ',@om_txt)+len('(1379): '),len(@om_txt));
						else
							begin
								if charindex('1379: ',@om_txt)>0
									set @tmp_txt = substring(@om_txt,charindex('1379: ',@om_txt)+len('1379: '),len(@om_txt));
							end;
						if len(@tmp_txt)>0
						begin
							if charindex('; ',@tmp_txt)>0
								set @tmp_txt = substring(@tmp_txt,1,charindex('; ',@tmp_txt)-1);
							if charindex('<Nm>',@tmp_txt)>0
							begin
								set @C_name=substring(@tmp_txt,charindex('<Nm>',@tmp_txt)+len('<Nm>'),len(@tmp_txt));
								if charindex('</Nm>',@C_name)>0
									set @C_name=substring(@C_name,1,charindex('</Nm>',@C_name)-1);
								set @C_name = FAF.GoAML_char_process(@C_name,NULL,NULL)
							end;
							if charindex('<PstlAdr>',@tmp_txt)>0
							begin
								set @tmp_txt=substring(@tmp_txt,charindex('<PstlAdr>',@tmp_txt)+len('<PstlAdr>'),len(@tmp_txt));
								if charindex('</PstlAdr>',@tmp_txt)>0
									set @tmp_txt=substring(@tmp_txt,1,charindex('</PstlAdr>',@tmp_txt)-1);
								if charindex('<StrtNm>',@tmp_txt)>0
								begin
									set @C_StrtNm=substring(@tmp_txt,charindex('<StrtNm>',@tmp_txt)+len('<StrtNm>'),len(@tmp_txt));
									if charindex('</StrtNm>',@C_StrtNm)>0
										set @C_StrtNm=substring(@C_StrtNm,1,charindex('</StrtNm>',@C_StrtNm)-1);
									set @C_StrtNm = FAF.GoAML_char_process(@C_StrtNm,NULL,NULL)
								end;
								if charindex('<BldgNb>',@tmp_txt)>0
								begin
									set @C_BldgNb=substring(@tmp_txt,charindex('<BldgNb>',@tmp_txt)+len('<BldgNb>'),len(@tmp_txt));
									if charindex('</BldgNb>',@C_BldgNb)>0
										set @C_BldgNb=substring(@C_BldgNb,1,charindex('</BldgNb>',@C_BldgNb)-1);
									set @C_BldgNb = FAF.GoAML_char_process(@C_BldgNb,NULL,NULL)
								end;
								if charindex('<PstCd>',@tmp_txt)>0
								begin
									set @C_PstCd=substring(@tmp_txt,charindex('<PstCd>',@tmp_txt)+len('<PstCd>'),len(@tmp_txt));
									if charindex('</PstCd>',@C_PstCd)>0
										set @C_PstCd=substring(@C_PstCd,1,charindex('</PstCd>',@C_PstCd)-1);
									set @C_PstCd = FAF.GoAML_char_process(@C_PstCd,NULL,NULL)
								end;
								if charindex('<TwnNm>',@tmp_txt)>0
								begin
									set @C_TwnNm=substring(@tmp_txt,charindex('<TwnNm>',@tmp_txt)+len('<TwnNm>'),len(@tmp_txt));
									if charindex('</TwnNm>',@C_TwnNm)>0
										set @C_TwnNm=substring(@C_TwnNm,1,charindex('</TwnNm>',@C_TwnNm)-1);
									set @C_TwnNm = FAF.GoAML_char_process(@C_TwnNm,NULL,NULL)
								end;
								if charindex('<Ctry>',@tmp_txt)>0
								begin
									set @C_Ctry=substring(@tmp_txt,charindex('<Ctry>',@tmp_txt)+len('<Ctry>'),len(@tmp_txt));
									if charindex('</Ctry>',@C_Ctry)>0
										set @C_Ctry=substring(@C_Ctry,1,charindex('</Ctry>',@C_Ctry)-1);
									set @C_Ctry = FAF.GoAML_char_process(@C_Ctry,NULL,NULL)
								end;
								if charindex('<AdrTp>',@tmp_txt)>0
								begin
									set @C_AdrTp=substring(@tmp_txt,charindex('<AdrTp>',@tmp_txt)+len('<AdrTp>'),len(@tmp_txt));
									if charindex('</AdrTp>',@C_AdrTp)>0
										set @C_AdrTp=substring(@C_AdrTp,1,charindex('</AdrTp>',@C_AdrTp)-1);
									set @C_AdrTp = FAF.GoAML_char_process(@C_AdrTp,NULL,NULL)
								end;
								if charindex('<Dept>',@tmp_txt)>0
								begin
									set @C_Dept=substring(@tmp_txt,charindex('<Dept>',@tmp_txt)+len('<Dept>'),len(@tmp_txt));
									if charindex('</Dept>',@C_Dept)>0
										set @C_Dept=substring(@C_Dept,1,charindex('</Dept>',@C_Dept)-1);
									set @C_Dept = FAF.GoAML_char_process(@C_Dept,NULL,NULL)
								end;
								if charindex('<SubDept>',@tmp_txt)>0
								begin
									set @C_SubDept=substring(@tmp_txt,charindex('<SubDept>',@tmp_txt)+len('<SubDept>'),len(@tmp_txt));
									if charindex('</SubDept>',@C_SubDept)>0
										set @C_SubDept=substring(@C_SubDept,1,charindex('</SubDept>',@C_SubDept)-1);
									set @C_SubDept = FAF.GoAML_char_process(@C_SubDept,NULL,NULL)
								end;
								if charindex('<BldgNm>',@tmp_txt)>0
								begin
									set @C_BldgNm=substring(@tmp_txt,charindex('<BldgNm>',@tmp_txt)+len('<BldgNm>'),len(@tmp_txt));
									if charindex('</BldgNm>',@C_BldgNm)>0
										set @C_BldgNm=substring(@C_BldgNm,1,charindex('</BldgNm>',@C_BldgNm)-1);
									set @C_BldgNm = FAF.GoAML_char_process(@C_BldgNm,NULL,NULL)
								end;
								if charindex('<Flr>',@tmp_txt)>0
								begin
									set @C_Flr=substring(@tmp_txt,charindex('<Flr>',@tmp_txt)+len('<Flr>'),len(@tmp_txt));
									if charindex('</Flr>',@C_Flr)>0
										set @C_Flr=substring(@C_Flr,1,charindex('</Flr>',@C_Flr)-1);
									set @C_Flr = FAF.GoAML_char_process(@C_Flr,NULL,NULL)
								end;
								if charindex('<PstBx>',@tmp_txt)>0
								begin
									set @C_PstBx=substring(@tmp_txt,charindex('<PstBx>',@tmp_txt)+len('<PstBx>'),len(@tmp_txt));
									if charindex('</PstBx>',@C_PstBx)>0
										set @C_PstBx=substring(@C_PstBx,1,charindex('</PstBx>',@C_PstBx)-1);
									set @C_PstBx = FAF.GoAML_char_process(@C_PstBx,NULL,NULL)
								end;
								if charindex('<Room>',@tmp_txt)>0
								begin
									set @C_Room=substring(@tmp_txt,charindex('<Room>',@tmp_txt)+len('<Room>'),len(@tmp_txt));
									if charindex('</Room>',@C_Room)>0
										set @C_Room=substring(@C_Room,1,charindex('</Room>',@C_Room)-1);
									set @C_Room = FAF.GoAML_char_process(@C_Room,NULL,NULL)
								end;
								if charindex('<TwnLctnNm>',@tmp_txt)>0
								begin
									set @C_TwnLctnNm=substring(@tmp_txt,charindex('<TwnLctnNm>',@tmp_txt)+len('<TwnLctnNm>'),len(@tmp_txt));
									if charindex('</TwnLctnNm>',@C_TwnLctnNm)>0
										set @C_TwnLctnNm=substring(@C_TwnLctnNm,1,charindex('</TwnLctnNm>',@C_TwnLctnNm)-1);
									set @C_TwnLctnNm = FAF.GoAML_char_process(@C_TwnLctnNm,NULL,NULL)
								end;
								if charindex('<DstrctNm>',@tmp_txt)>0
								begin
									set @C_DstrctNm=substring(@tmp_txt,charindex('<DstrctNm>',@tmp_txt)+len('<DstrctNm>'),len(@tmp_txt));
									if charindex('</DstrctNm>',@C_DstrctNm)>0
										set @C_DstrctNm=substring(@C_DstrctNm,1,charindex('</DstrctNm>',@C_DstrctNm)-1);
									set @C_DstrctNm = FAF.GoAML_char_process(@C_DstrctNm,NULL,NULL)
								end;
								if charindex('<CtrySubDvsn>',@tmp_txt)>0
								begin
									set @C_CtrySubDvsn=substring(@tmp_txt,charindex('<CtrySubDvsn>',@tmp_txt)+len('<CtrySubDvsn>'),len(@tmp_txt));
									if charindex('</CtrySubDvsn>',@C_CtrySubDvsn)>0
										set @C_CtrySubDvsn=substring(@C_CtrySubDvsn,1,charindex('</CtrySubDvsn>',@C_CtrySubDvsn)-1);
									set @C_CtrySubDvsn = FAF.GoAML_char_process(@C_CtrySubDvsn,NULL,NULL)
								end;
								set @ll = charindex('<AdrLine>',@tmp_txt);
								while @ll>0
								begin
									set @C_AdrLine_tmp=substring(@tmp_txt,@ll+len('<AdrLine>'),len(@tmp_txt));
									if charindex('</AdrLine>',@C_AdrLine_tmp)>0
										set @C_AdrLine_tmp=substring(@C_AdrLine_tmp,1,charindex('</AdrLine>',@C_AdrLine_tmp)-1);
									set @C_AdrLine = case when len(@C_AdrLine)>0 then @C_AdrLine + ', ' else '' end + FAF.GoAML_char_process(@C_AdrLine_tmp,NULL,NULL);
									set @ll = charindex('<AdrLine>',@tmp_txt,@ll+len('<AdrLine>'));
								end;
								
								if len(@C_StrtNm)>0
								begin
									set @C_AdrLine = @C_StrtNm + 
										case when len(@C_BldgNb)>0 then ' ' + @C_BldgNb else '' end +
										case when len(@C_BldgNm)>0 then ' ' + @C_BldgNm else '' end + 
										case when len(@C_Flr)>0 then ', floor ' + @C_Flr else '' end + 
										case when len(@C_Room)>0 then ', room ' + @C_Room else '' end + 
										case when len(@C_AdrLine)>0 then ', ' + @C_AdrLine else '' end;
								end;

								if len(@C_DstrctNm)>0 and len(@C_CtrySubDvsn)>0 
									set @C_DstrctNm = @C_DstrctNm + ', ' + @C_CtrySubDvsn;
								else
									set @C_DstrctNm = isnull(@C_DstrctNm,'') + isnull(@C_CtrySubDvsn,'');
								
								if len(@C_Dept)>0 and len(@C_SubDept)>0
									set @C_Dept = 'Departjment: ' + @C_Dept + ', Sub Department: ' + @C_SubDept;
								else
									begin
										set @C_Dept = 
											case when len(@C_Dept)>0 then 'Departjment: ' + @C_Dept else '' end + 
											case when len(@C_SubDept)>0 then 'Sub Department: ' + @C_SubDept else '' end;
									end;

								if len(@C_Dept)>0 and len(@C_PstBx)>0
									set @C_Dept = @C_Dept + char(13) + char(10) + 'Post Box: ' + @C_PstBx;
								else
									set @C_Dept = isnull(@C_Dept,'') + case when len(@C_PstBx)>0 then 'Post Box: ' + @C_PstBx else '' end;

								if FAF.GoAML_get_paramS(@F_BANK_ID,135,2) = 'ok'
								begin
									if len(@C_Dept)>0 and len(@C_AdrTp)>0
										begin
											set @C_Dept = @C_Dept +  
												case @C_AdrTp
													when 'ADDR' then char(13) + char(10) + 'Address is the complete postal address'
													when 'PBOX' then char(13) + char(10) + 'Address is a postal office (PO) box'
													when 'MLTO' then char(13) + char(10) + 'Address is the address to which mail is sent'
													when 'DLVY' then char(13) + char(10) + 'Address is the address to which delivery is to take place'
													else ''
												end;
										end;
									else
										begin
											set @C_Dept = isnull(@C_Dept,'') +  
												case @C_AdrTp
													when 'ADDR' then 'Address is the complete postal address'
													when 'PBOX' then 'Address is a postal office (PO) box'
													when 'MLTO' then 'Address is the address to which mail is sent'
													when 'DLVY' then 'Address is the address to which delivery is to take place'
													else ''
												end;
										end;
								end;
							end;
							if len(@C_name)>0
							begin
								if FAF.GoAML_get_paramS(@F_BANK_ID,136,1)='ok'
									begin
										set @C_xml = 
											(select
												FAF.GoAML_get_paramS(@F_BANK_ID,136,2) as "account_entity_relation",
												(select
													@C_name as "name",
													case
														when len(@C_AdrLine)>0
														then
															(select
																FAF.GoAML_char_process(
																	case
																		when @C_AdrTp='HOME' then FAF.GoAML_get_paramS(@F_BANK_ID,8,2)
																		when @C_AdrTp='BIZZ' then FAF.GoAML_get_paramS(@F_BANK_ID,7,2)
																		when @C_AdrTp in ('ADDR','PBOX','MLTO','DLVY') then FAF.GoAML_get_paramS(@F_BANK_ID,135,1)
																		else NULL
																	end
																,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address_type'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__address_type')) as "address_type",
																FAF.GoAML_char_process_WR(@C_AdrLine,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__address')) as "address",
																FAF.GoAML_char_process(@C_TwnLctnNm,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__town'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__town')) as "town",
																FAF.GoAML_char_process(@C_TwnNm,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__city'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__city')) as "city",
																FAF.GoAML_char_process(@C_PstCd,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__zip'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__zip')) as "zip",
																FAF.GoAML_char_process(@C_Ctry,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__country_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__country_code')) as "country_code",
																FAF.GoAML_char_process(@C_DstrctNm,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__state'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__state')) as "state",
																FAF.GoAML_char_process_WR(@C_Dept,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__comments')) as "comments"
															FOR XML PATH ('address'),type, ELEMENTS)
														else NULL
													end as "addresses"
												FOR XML PATH (''),type, ELEMENTS) as "entity"
											FOR XML PATH ('account_related_entity'),type, ELEMENTS);
									end;
								else
									begin
										set @C_xml = 
											(select
												@C_name as "name",
												case
													when len(@C_AdrLine)>0
													then
														(select
															FAF.GoAML_char_process(
																case
																	when @C_AdrTp='HOME' then FAF.GoAML_get_paramS(@F_BANK_ID,8,2)
																	when @C_AdrTp='BIZZ' then FAF.GoAML_get_paramS(@F_BANK_ID,7,2)
																	when @C_AdrTp in ('ADDR','PBOX','MLTO','DLVY') then FAF.GoAML_get_paramS(@F_BANK_ID,135,1)
																	else NULL
																end
															,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address_type'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__address_type')) as "address_type",
															FAF.GoAML_char_process_WR(@C_AdrLine,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__address')) as "address",
															FAF.GoAML_char_process(@C_TwnLctnNm,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__town'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__town')) as "town",
															FAF.GoAML_char_process(@C_TwnNm,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__city'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__city')) as "city",
															FAF.GoAML_char_process(@C_PstCd,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__zip'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__zip')) as "zip",
															FAF.GoAML_char_process(@C_Ctry,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__country_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__country_code')) as "country_code",
															FAF.GoAML_char_process(@C_DstrctNm,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__state'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__state')) as "state",
															FAF.GoAML_char_process_WR(@C_Dept,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__comments')) as "comments"
														FOR XML PATH ('address'),type, ELEMENTS)
													else NULL
												end as "addresses"
											FOR XML PATH ('t_entity'),type, ELEMENTS);
									end;
							end;
						end;
						if @C_xml is not NULL
						begin
							--om_text_typ_cd = 1378 we take into related_entities and only if 1379 exists
							set @tmp_txt = NULL;
							set @C_AdrTp = NULL;
							set @C_Dept = NULL;
							set @C_SubDept = NULL;
							set @C_BldgNm = NULL;
							set @C_Flr = NULL;
							set @C_PstBx = NULL;
							set @C_Room = NULL;
							set @C_TwnLctnNm = NULL;
							set @C_DstrctNm = NULL;
							set @C_CtrySubDvsn = NULL;
							set @C_name = NULL;
							set @C_StrtNm = NULL;
							set @C_BldgNb = NULL;
							set @C_AdrLine = NULL;
							set @C_AdrLine_tmp = NULL;
							set @C_TwnNm = NULL;
							set @C_PstCd = NULL;
							set @C_Ctry = NULL;
							if charindex('(1378): ',@om_txt)>0
								set @tmp_txt = substring(@om_txt,charindex('(1378): ',@om_txt)+len('(1378): '),len(@om_txt));
							else
								begin
									if charindex('1378: ',@om_txt)>0
										set @tmp_txt = substring(@om_txt,charindex('1378: ',@om_txt)+len('1378: '),len(@om_txt));
								end;
							if len(@tmp_txt)>0
							begin
								if charindex('; ',@tmp_txt)>0
									set @tmp_txt = substring(@tmp_txt,1,charindex('; ',@tmp_txt)-1);
								if charindex('<Nm>',@tmp_txt)>0
								begin
									set @C_name=substring(@tmp_txt,charindex('<Nm>',@tmp_txt)+len('<Nm>'),len(@tmp_txt));
									if charindex('</Nm>',@C_name)>0
										set @C_name=substring(@C_name,1,charindex('</Nm>',@C_name)-1);
									set @C_name = FAF.GoAML_char_process(@C_name,NULL,NULL)
								end;
								if charindex('<PstlAdr>',@tmp_txt)>0
								begin
									set @tmp_txt=substring(@tmp_txt,charindex('<PstlAdr>',@tmp_txt)+len('<PstlAdr>'),len(@tmp_txt));
									if charindex('</PstlAdr>',@tmp_txt)>0
										set @tmp_txt=substring(@tmp_txt,1,charindex('</PstlAdr>',@tmp_txt)-1);
									if charindex('<StrtNm>',@tmp_txt)>0
									begin
										set @C_StrtNm=substring(@tmp_txt,charindex('<StrtNm>',@tmp_txt)+len('<StrtNm>'),len(@tmp_txt));
										if charindex('</StrtNm>',@C_StrtNm)>0
											set @C_StrtNm=substring(@C_StrtNm,1,charindex('</StrtNm>',@C_StrtNm)-1);
										set @C_StrtNm = FAF.GoAML_char_process(@C_StrtNm,NULL,NULL)
									end;
									if charindex('<BldgNb>',@tmp_txt)>0
									begin
										set @C_BldgNb=substring(@tmp_txt,charindex('<BldgNb>',@tmp_txt)+len('<BldgNb>'),len(@tmp_txt));
										if charindex('</BldgNb>',@C_BldgNb)>0
											set @C_BldgNb=substring(@C_BldgNb,1,charindex('</BldgNb>',@C_BldgNb)-1);
										set @C_BldgNb = FAF.GoAML_char_process(@C_BldgNb,NULL,NULL)
									end;
									if charindex('<PstCd>',@tmp_txt)>0
									begin
										set @C_PstCd=substring(@tmp_txt,charindex('<PstCd>',@tmp_txt)+len('<PstCd>'),len(@tmp_txt));
										if charindex('</PstCd>',@C_PstCd)>0
											set @C_PstCd=substring(@C_PstCd,1,charindex('</PstCd>',@C_PstCd)-1);
										set @C_PstCd = FAF.GoAML_char_process(@C_PstCd,NULL,NULL)
									end;
									if charindex('<TwnNm>',@tmp_txt)>0
									begin
										set @C_TwnNm=substring(@tmp_txt,charindex('<TwnNm>',@tmp_txt)+len('<TwnNm>'),len(@tmp_txt));
										if charindex('</TwnNm>',@C_TwnNm)>0
											set @C_TwnNm=substring(@C_TwnNm,1,charindex('</TwnNm>',@C_TwnNm)-1);
										set @C_TwnNm = FAF.GoAML_char_process(@C_TwnNm,NULL,NULL)
									end;
									if charindex('<Ctry>',@tmp_txt)>0
									begin
										set @C_Ctry=substring(@tmp_txt,charindex('<Ctry>',@tmp_txt)+len('<Ctry>'),len(@tmp_txt));
										if charindex('</Ctry>',@C_Ctry)>0
											set @C_Ctry=substring(@C_Ctry,1,charindex('</Ctry>',@C_Ctry)-1);
										set @C_Ctry = FAF.GoAML_char_process(@C_Ctry,NULL,NULL)
									end;
									if charindex('<AdrTp>',@tmp_txt)>0
									begin
										set @C_AdrTp=substring(@tmp_txt,charindex('<AdrTp>',@tmp_txt)+len('<AdrTp>'),len(@tmp_txt));
										if charindex('</AdrTp>',@C_AdrTp)>0
											set @C_AdrTp=substring(@C_AdrTp,1,charindex('</AdrTp>',@C_AdrTp)-1);
										set @C_AdrTp = FAF.GoAML_char_process(@C_AdrTp,NULL,NULL)
									end;
									if charindex('<Dept>',@tmp_txt)>0
									begin
										set @C_Dept=substring(@tmp_txt,charindex('<Dept>',@tmp_txt)+len('<Dept>'),len(@tmp_txt));
										if charindex('</Dept>',@C_Dept)>0
											set @C_Dept=substring(@C_Dept,1,charindex('</Dept>',@C_Dept)-1);
										set @C_Dept = FAF.GoAML_char_process(@C_Dept,NULL,NULL)
									end;
									if charindex('<SubDept>',@tmp_txt)>0
									begin
										set @C_SubDept=substring(@tmp_txt,charindex('<SubDept>',@tmp_txt)+len('<SubDept>'),len(@tmp_txt));
										if charindex('</SubDept>',@C_SubDept)>0
											set @C_SubDept=substring(@C_SubDept,1,charindex('</SubDept>',@C_SubDept)-1);
										set @C_SubDept = FAF.GoAML_char_process(@C_SubDept,NULL,NULL)
									end;
									if charindex('<BldgNm>',@tmp_txt)>0
									begin
										set @C_BldgNm=substring(@tmp_txt,charindex('<BldgNm>',@tmp_txt)+len('<BldgNm>'),len(@tmp_txt));
										if charindex('</BldgNm>',@C_BldgNm)>0
											set @C_BldgNm=substring(@C_BldgNm,1,charindex('</BldgNm>',@C_BldgNm)-1);
										set @C_BldgNm = FAF.GoAML_char_process(@C_BldgNm,NULL,NULL)
									end;
									if charindex('<Flr>',@tmp_txt)>0
									begin
										set @C_Flr=substring(@tmp_txt,charindex('<Flr>',@tmp_txt)+len('<Flr>'),len(@tmp_txt));
										if charindex('</Flr>',@C_Flr)>0
											set @C_Flr=substring(@C_Flr,1,charindex('</Flr>',@C_Flr)-1);
										set @C_Flr = FAF.GoAML_char_process(@C_Flr,NULL,NULL)
									end;
									if charindex('<PstBx>',@tmp_txt)>0
									begin
										set @C_PstBx=substring(@tmp_txt,charindex('<PstBx>',@tmp_txt)+len('<PstBx>'),len(@tmp_txt));
										if charindex('</PstBx>',@C_PstBx)>0
											set @C_PstBx=substring(@C_PstBx,1,charindex('</PstBx>',@C_PstBx)-1);
										set @C_PstBx = FAF.GoAML_char_process(@C_PstBx,NULL,NULL)
									end;
									if charindex('<Room>',@tmp_txt)>0
									begin
										set @C_Room=substring(@tmp_txt,charindex('<Room>',@tmp_txt)+len('<Room>'),len(@tmp_txt));
										if charindex('</Room>',@C_Room)>0
											set @C_Room=substring(@C_Room,1,charindex('</Room>',@C_Room)-1);
										set @C_Room = FAF.GoAML_char_process(@C_Room,NULL,NULL)
									end;
									if charindex('<TwnLctnNm>',@tmp_txt)>0
									begin
										set @C_TwnLctnNm=substring(@tmp_txt,charindex('<TwnLctnNm>',@tmp_txt)+len('<TwnLctnNm>'),len(@tmp_txt));
										if charindex('</TwnLctnNm>',@C_TwnLctnNm)>0
											set @C_TwnLctnNm=substring(@C_TwnLctnNm,1,charindex('</TwnLctnNm>',@C_TwnLctnNm)-1);
										set @C_TwnLctnNm = FAF.GoAML_char_process(@C_TwnLctnNm,NULL,NULL)
									end;
									if charindex('<DstrctNm>',@tmp_txt)>0
									begin
										set @C_DstrctNm=substring(@tmp_txt,charindex('<DstrctNm>',@tmp_txt)+len('<DstrctNm>'),len(@tmp_txt));
										if charindex('</DstrctNm>',@C_DstrctNm)>0
											set @C_DstrctNm=substring(@C_DstrctNm,1,charindex('</DstrctNm>',@C_DstrctNm)-1);
										set @C_DstrctNm = FAF.GoAML_char_process(@C_DstrctNm,NULL,NULL)
									end;
									if charindex('<CtrySubDvsn>',@tmp_txt)>0
									begin
										set @C_CtrySubDvsn=substring(@tmp_txt,charindex('<CtrySubDvsn>',@tmp_txt)+len('<CtrySubDvsn>'),len(@tmp_txt));
										if charindex('</CtrySubDvsn>',@C_CtrySubDvsn)>0
											set @C_CtrySubDvsn=substring(@C_CtrySubDvsn,1,charindex('</CtrySubDvsn>',@C_CtrySubDvsn)-1);
										set @C_CtrySubDvsn = FAF.GoAML_char_process(@C_CtrySubDvsn,NULL,NULL)
									end;
									set @ll = charindex('<AdrLine>',@tmp_txt);
									while @ll>0
									begin
										set @C_AdrLine_tmp=substring(@tmp_txt,@ll+len('<AdrLine>'),len(@tmp_txt));
										if charindex('</AdrLine>',@C_AdrLine_tmp)>0
											set @C_AdrLine_tmp=substring(@C_AdrLine_tmp,1,charindex('</AdrLine>',@C_AdrLine_tmp)-1);
										set @C_AdrLine = case when len(@C_AdrLine)>0 then @C_AdrLine + ', ' else '' end + FAF.GoAML_char_process(@C_AdrLine_tmp,NULL,NULL);
										set @ll = charindex('<AdrLine>',@tmp_txt,@ll+len('<AdrLine>'));
									end;
									
									if len(@C_StrtNm)>0
									begin
										set @C_AdrLine = @C_StrtNm + 
											case when len(@C_BldgNb)>0 then ' ' + @C_BldgNb else '' end +
											case when len(@C_BldgNm)>0 then ' ' + @C_BldgNm else '' end + 
											case when len(@C_Flr)>0 then ', floor ' + @C_Flr else '' end + 
											case when len(@C_Room)>0 then ', room ' + @C_Room else '' end + 
											case when len(@C_AdrLine)>0 then ', ' + @C_AdrLine else '' end;
									end;

									if len(@C_DstrctNm)>0 and len(@C_CtrySubDvsn)>0 
										set @C_DstrctNm = @C_DstrctNm + ', ' + @C_CtrySubDvsn;
									else
										set @C_DstrctNm = isnull(@C_DstrctNm,'') + isnull(@C_CtrySubDvsn,'');
									
									if len(@C_Dept)>0 and len(@C_SubDept)>0
										set @C_Dept = 'Departjment: ' + @C_Dept + ', Sub Department: ' + @C_SubDept;
									else
										begin
											set @C_Dept = 
												case when len(@C_Dept)>0 then 'Departjment: ' + @C_Dept else '' end + 
												case when len(@C_SubDept)>0 then 'Sub Department: ' + @C_SubDept else '' end;
										end;

									if len(@C_Dept)>0 and len(@C_PstBx)>0
										set @C_Dept = @C_Dept + char(13) + char(10) + 'Post Box: ' + @C_PstBx;
									else
										set @C_Dept = isnull(@C_Dept,'') + case when len(@C_PstBx)>0 then 'Post Box: ' + @C_PstBx else '' end;

									if FAF.GoAML_get_paramS(@F_BANK_ID,135,2) = 'ok'
									begin
										if len(@C_Dept)>0 and len(@C_AdrTp)>0
											begin
												set @C_Dept = @C_Dept +  
													case @C_AdrTp
														when 'ADDR' then char(13) + char(10) + 'Address is the complete postal address'
														when 'PBOX' then char(13) + char(10) + 'Address is a postal office (PO) box'
														when 'MLTO' then char(13) + char(10) + 'Address is the address to which mail is sent'
														when 'DLVY' then char(13) + char(10) + 'Address is the address to which delivery is to take place'
														else ''
													end;
											end;
										else
											begin
												set @C_Dept = isnull(@C_Dept,'') +  
													case @C_AdrTp
														when 'ADDR' then 'Address is the complete postal address'
														when 'PBOX' then 'Address is a postal office (PO) box'
														when 'MLTO' then 'Address is the address to which mail is sent'
														when 'DLVY' then 'Address is the address to which delivery is to take place'
														else ''
													end;
											end;
									end;
								end;
								if len(@C_name)>0
								begin
									if FAF.GoAML_get_paramS(@F_BANK_ID,136,1)='ok'
										begin
											set @C_xml = 
												(select
													@C_xml,
													(select
														FAF.GoAML_get_paramV(@F_BANK_ID,'are__account_entity_relation') as "account_entity_relation",
														(select
															@C_name as "name",
															case
																when len(@C_AdrLine)>0
																then
																	(select
																		FAF.GoAML_char_process(
																			case
																				when @C_AdrTp='HOME' then FAF.GoAML_get_paramS(@F_BANK_ID,8,2)
																				when @C_AdrTp='BIZZ' then FAF.GoAML_get_paramS(@F_BANK_ID,7,2)
																				when @C_AdrTp in ('ADDR','PBOX','MLTO','DLVY') then FAF.GoAML_get_paramS(@F_BANK_ID,135,1)
																				else NULL
																			end
																		,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address_type'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__address_type')) as "address_type",
																		FAF.GoAML_char_process_WR(@C_AdrLine,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__address')) as "address",
																		FAF.GoAML_char_process(@C_TwnLctnNm,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__town'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__town')) as "town",
																		FAF.GoAML_char_process(@C_TwnNm,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__city'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__city')) as "city",
																		FAF.GoAML_char_process(@C_PstCd,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__zip'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__zip')) as "zip",
																		FAF.GoAML_char_process(@C_Ctry,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__country_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__country_code')) as "country_code",
																		FAF.GoAML_char_process(@C_DstrctNm,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__state'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__state')) as "state",
																		FAF.GoAML_char_process_WR(@C_Dept,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__comments')) as "comments"
																	FOR XML PATH ('address'),type, ELEMENTS)
																else NULL
															end as "addresses"
														FOR XML PATH (''),type, ELEMENTS) as "entity"
													FOR XML PATH ('account_related_entity'),type, ELEMENTS)
												FOR XML PATH ('related_entities'),type, ELEMENTS);
										end;
									else
										begin
											set @C_xml = 
												(select
													@C_xml,
													(select
														FAF.GoAML_get_paramV(@F_BANK_ID,'are__account_entity_relation') as "account_entity_relation",
														(select
															@C_name as "name",
															case
																when len(@C_AdrLine)>0
																then
																	(select
																		FAF.GoAML_char_process(
																			case
																				when @C_AdrTp='HOME' then FAF.GoAML_get_paramS(@F_BANK_ID,8,2)
																				when @C_AdrTp='BIZZ' then FAF.GoAML_get_paramS(@F_BANK_ID,7,2)
																				when @C_AdrTp in ('ADDR','PBOX','MLTO','DLVY') then FAF.GoAML_get_paramS(@F_BANK_ID,135,1)
																				else NULL
																			end
																		,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address_type'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__address_type')) as "address_type",
																		FAF.GoAML_char_process_WR(@C_AdrLine,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__address')) as "address",
																		FAF.GoAML_char_process(@C_TwnLctnNm,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__town'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__town')) as "town",
																		FAF.GoAML_char_process(@C_TwnNm,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__city'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__city')) as "city",
																		FAF.GoAML_char_process(@C_PstCd,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__zip'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__zip')) as "zip",
																		FAF.GoAML_char_process(@C_Ctry,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__country_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__country_code')) as "country_code",
																		FAF.GoAML_char_process(@C_DstrctNm,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__state'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__state')) as "state",
																		FAF.GoAML_char_process_WR(@C_Dept,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__comments')) as "comments"
																	FOR XML PATH ('address'),type, ELEMENTS)
																else NULL
															end as "addresses"
														FOR XML PATH ('entity'),type, ELEMENTS)
													FOR XML PATH ('account_related_entity'),type, ELEMENTS) as "related_entities"
												FOR XML PATH (''),type, ELEMENTS);
										end;
								end;
							end;
						end;
					end;

					--TWINT
					if @keycd=70
					begin
						select @xml_ac=s.xml1 from @acnts s where s.nID=@ac1_id;

						set @xml_t = 
							(select
								FAF.GoAML_char_process(cast(s3.zv_zlg_sys_lnr as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transactionnumber'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transactionnumber')) as "transactionnumber",
								FAF.GoAML_char_process(s3.auf_id,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__internal_ref_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__internal_ref_number')) as "internal_ref_number",
								FAF.GoAML_char_process(
									case 
										when s3.sitz_nr_bez is not NULL
										then 
											case
												when FAF.GoAML_get_paramS(@F_BANK_ID,97,1)='ok'
												then s3.sitz_nr_bez
												else 'Branch '+ cast(s3.sitz_nr_ausf as varchar) + ' (' + coalesce(s3.sitz_nr_bez,'') + ')'
											end
										else
											case 
												when FAF.GoAML_get_paramS(@F_BANK_ID,1,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code')) then FAF.GoAML_get_paramS(@F_BANK_ID,1,2)
												when FAF.GoAML_get_paramS(@F_BANK_ID,2,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code')) then FAF.GoAML_get_paramS(@F_BANK_ID,2,2)
												else NULL 
											end
									end
								,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_location'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_location')) as "transaction_location",
								FAF.GoAML_char_process_WR(
									@tr_dscr + ' ' + @d_txt +
									case 
										when len(@trd_txt)>0
										then '; ' + char(13) + char(10) + @trd_txt
										else ''
									end
								,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_description'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_description')) as "transaction_description",
								FAF.GoAML_char_process(
									case 
										when s3.ausf_dt_ist is not NULL
										then left(convert(varchar,cast(s3.ausf_dt_ist as datetime),126),19)
										when s3.valuta is not NULL
										then left(convert(varchar,cast(s3.valuta as datetime),126),19)
										else left(convert(varchar,cast(convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) as datetime),126),19)
									end
								,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__date_transaction'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__date_transaction')) as "date_transaction",
								FAF.GoAML_char_process(
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,130,1)='ok'
										then FAF.GoAML_get_paramS(@F_BANK_ID,132,1)
										else
											case 
												when s3.betrag_bwrg<0 
												then FAF.GoAML_get_paramS(@F_BANK_ID,94,2)
												else NULL
											end
									end
								,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_type_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_type_code')) as "transaction_type_code",
								case
									when FAF.GoAML_get_paramS(@F_BANK_ID,124,1)='ok'
									then NULL
									else FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code'))
								end as "transmode_code", 
								case
									when FAF.GoAML_get_paramS(@F_BANK_ID,124,2)='ok'
									then NULL
									else 
										FAF.GoAML_char_process(
											case
												when 
													FAF.GoAML_char_process(
														case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end,
														case
															when FAF.GoAML_get_paramS(@F_BANK_ID,61,1)='ok'
															then FAF.GoAML_get_paramS(@F_BANK_ID,61,2)
															else NULL
														end
													,0) is not NULL
												then '[' + 
													FAF.GoAML_char_process(
														case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end,
														case
															when FAF.GoAML_get_paramS(@F_BANK_ID,61,1)='ok'
															then FAF.GoAML_get_paramS(@F_BANK_ID,61,2)
															else NULL
														end
													,0) + ']: '
												else ''
											end + @tr_dscr
										,case 
											when FAF.GoAML_get_paramS(@F_BANK_ID,3,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code'))
											then FAF.GoAML_get_paramS(@F_BANK_ID,3,2)
											else FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_comment')
										end
										,FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_comment'))
								end as "transmode_comment",
								cast(
									FAF.GoAML_char_process(
										case 
											when FAF.GoAML_get_paramS(@F_BANK_ID,4,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code')) 
											then FAF.GoAML_get_paramS(@F_BANK_ID,4,2)
											else 
												case
													when abs(s3.betrag_bwrg)<0.01 
													then NULL
													else cast(cast(abs(s3.betrag_bwrg) as decimal(15,2)) as varchar)
												end 
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__amount_local'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__amount_local'))
								 as decimal(15,2)) as "amount_local",
								FAF.GoAML_char_process(
									case 
										when s3.betrag_bwrg<0 
										then FAF.GoAML_get_paramS(@F_BANK_ID,84,2)
										else FAF.GoAML_get_paramS(@F_BANK_ID,84,1)
									end
								,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_status'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_status')) as "transaction_status",
								
								--OM_AUF.kt_lnr as sender
								(select
									FAF.GoAML_char_process(f_fnd.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'tfmc__from_funds_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfmc__from_funds_code')) as "from_funds_code",
									FAF.GoAML_char_process(
										case 
											when FAF.GoAML_get_paramS(@F_BANK_ID,51,1)='ok'
											then FAF.GoAML_char_process(f_fnd.comments,NULL,0)
											else NULL
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'tfmc__from_funds_comment'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfmc__from_funds_comment')) as "from_funds_comment",
									FAF.GoAML_char_process(cur.iso_wrg_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_currency_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_currency_code')) as "from_foreign_currency/foreign_currency_code",
									FAF.GoAML_char_process(cast(cast(abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg)) as decimal(15,2)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_amount'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_amount')) as "from_foreign_currency/foreign_amount",
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,99,1)='ok'
										then 
											FAF.GoAML_char_process(cast(cast(abs(
												case
													when abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg))>0
													then s3.betrag_bwrg/coalesce(s3.betrag_zwrg,s3.betrag_bwrg)
													else NULL
												end
											) as decimal(26,13)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_exchange_rate'))
										else FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate')
									end as "from_foreign_currency/foreign_exchange_rate",
									isnull(@xml_ac, @xml_da) as "from_account",
									@cntr as "from_country"
								FOR XML PATH ('t_from_my_client'),type, ELEMENTS),
								
								--default recipient
								(select
									FAF.GoAML_char_process(t_fnd.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'tt__to_funds_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tt__to_funds_code')) as "to_funds_code",
									--FAF.GoAML_char_process(
									--	case 
									--		when FAF.GoAML_get_paramS(@F_BANK_ID,51,2)='ok'
									--		then FAF.GoAML_char_process(t_fnd.comments,NULL,0)
									--		else NULL
									--	end
									--,FAF.GoAML_get_paramV(@F_BANK_ID,'tt__to_funds_comment'),FAF.GoAML_get_paramL(@F_BANK_ID,'tt__to_funds_comment')) as "to_funds_comment",
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,113,2)='ok'
										then FAF.GoAML_char_process(cur.iso_wrg_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_currency_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_currency_code'))
										else NULL
									end as "to_foreign_currency/foreign_currency_code",
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,113,2)='ok'
										then FAF.GoAML_char_process(cast(cast(abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg)) as decimal(15,2)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_amount'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_amount'))
										else NULL
									end as "to_foreign_currency/foreign_amount",
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,113,2)='ok'
										then
											case
												when FAF.GoAML_get_paramS(@F_BANK_ID,99,1)='ok'
												then 
													FAF.GoAML_char_process(cast(cast(abs(
														case
															when abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg))>0
															then s3.betrag_bwrg/coalesce(s3.betrag_zwrg,s3.betrag_bwrg)
															else NULL
														end
													) as decimal(26,13)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_exchange_rate'))
												else FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate')
											end
										else NULL
									end as "to_foreign_currency/foreign_exchange_rate",
									(select
										FAF.GoAML_get_paramS(@F_BANK_ID,69,1) as "institution_name",
										FAF.GoAML_get_paramS(@F_BANK_ID,69,2) as "swift",
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,127,1)='ok'
											then NULL
											else
												case 
													when FAF.GoAML_get_paramS(@F_BANK_ID,89,2)='ok'
													then 
														FAF.GoAML_char_process(
															case
																when FAF.GoAML_get_paramS(@F_BANK_ID,103,2)='ok'
																then FAF.GoAML_get_paramS(@F_BANK_ID,91,2)
																when len(s3.ext_ref)<15 
																then FAF.GoAML_get_paramS(@F_BANK_ID,91,1)
																when substring(s3.ext_ref,3,2) not like '[0-9][0-9]' 
																then FAF.GoAML_get_paramS(@F_BANK_ID,91,1)
																when (select count(*) from FAF.FA_MROS_INT_LAND where userbk_nr=@F_BANK_ID and iso_land_cd=substring(s3.ext_ref,1,2))=0 
																then FAF.GoAML_get_paramS(@F_BANK_ID,91,1)
																else FAF.GoAML_get_paramS(@F_BANK_ID,90,1)
															end
														,FAF.GoAML_get_paramV(@F_BANK_ID,'ta__account_category'),FAF.GoAML_get_paramL(@F_BANK_ID,'ta__account_category'))
													else NULL
												end
										end as "account_category",
										FAF.GoAML_char_process(s3.ext_ref,FAF.GoAML_get_paramV(@F_BANK_ID,'ta__account'),FAF.GoAML_get_paramL(@F_BANK_ID,'ta__account')) as "account",
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,127,1)='ok'
											then NULL
											else
												FAF.GoAML_char_process(
													case 
														when FAF.GoAML_get_paramS(@F_BANK_ID,103,2)='ok'
														then s3.ext_ref
														else
															case 
																when len(s3.ext_ref)<15 then NULL
																when substring(s3.ext_ref,3,2) not like '[0-9][0-9]' then NULL
																when (select count(*) from FAF.FA_MROS_INT_LAND where userbk_nr=@F_BANK_ID and iso_land_cd=substring(s3.ext_ref,1,2))=0 then NULL
																else s3.ext_ref
															end
													end
												,FAF.GoAML_get_paramV(@F_BANK_ID,'ta__iban'),FAF.GoAML_get_paramL(@F_BANK_ID,'ta__iban'))
										end as "iban",
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,127,1)='ok' or FAF.GoAML_get_paramS(@F_BANK_ID,136,1)='ok'
											then NULL
											else
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,106,1)='ok'
													then FAF.GoAML_get_paramS(@F_BANK_ID,106,2)
													else NULL
												end
										end as "t_entity/name",
										FAF.GoAML_get_paramV(@F_BANK_ID,'ta__comments') as "comments"
									FOR XML PATH (''),type, ELEMENTS) as "to_account",
									@cntr as "to_country"
								FOR XML PATH (''),type, ELEMENTS) as "t_to",

								FAF.GoAML_char_process_WR(
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
										then
											stuff(
												case
													when FAF.GoAML_char_process(isnull(cat.category_group,tr_tp.category_group),NULL,0) is not NULL
													then char(13) + char(10) + '[' + FAF.GoAML_char_process(isnull(cat.category_group,tr_tp.category_group),'',0) + ']'
													else ''
												end +
												case 
													when s3.betrag_bwrg<0 and FAF.GoAML_get_paramS(@F_BANK_ID,5,1)='ok'
													then char(13) + char(10) + FAF.GoAML_get_paramS(@F_BANK_ID,5,2) 
													else ''
												end
											, 1, LEN(char(13) + char(10)), '')
										else ''
									end +
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,134,1)='ok' and @com1_txt is not NULL
										then @com1_txt
										else ''
									end
								,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__comments')) as "comments"
							from 
								FAF.FA_MROS_ZV_ZLG_T s3 
							left join
								FAF.FA_MROS_INT_WRG cur
							on s3.iso_wrg_cd=cur.iso_wrg_cd and s3.userbk_nr=cur.userbk_nr
							left join
								FAF.FA_MROS_REFERENCE_TABLE tr_tp
							on s3.userbk_nr=tr_tp.userbk_nr and cast(s3.buchtyp_cd as varchar)=tr_tp.Finnova_value and tr_tp.ref_type=10
							left join
								FAF.FA_MROS_TRX_CATEGORIES_DET cat
							on cat.userbk_nr=s3.userbk_nr and cat.buchtyp_cd=s3.buchtyp_cd and cat.trak_ref_typ=s3.trak_ref_typ and cat.gf_nr=s3.gf_nr
							left join
								FAF.FA_MROS_REFERENCE_TABLE ref_tp
							on s3.userbk_nr=ref_tp.userbk_nr and cast(s3.trak_ref_typ as varchar)=ref_tp.Finnova_value and ref_tp.ref_type=11
							left join
								FAF.FA_MROS_REFERENCE_TABLE f_fnd
							on s3.userbk_nr=f_fnd.userbk_nr and cast(s3.buchtyp_cd as varchar)=f_fnd.Finnova_value and f_fnd.ref_type=20
							left join
								FAF.FA_MROS_REFERENCE_TABLE t_fnd
							on s3.userbk_nr=t_fnd.userbk_nr and cast(s3.buchtyp_cd as varchar)=t_fnd.Finnova_value and t_fnd.ref_type=21
							where s3.userbk_nr=@F_BANK_ID and s3.zv_zlg_sys_lnr=@n_id
							FOR XML PATH ('transaction'),type, ELEMENTS);
					end;

					if @keycd=60
					begin
						select @xml_ac=s.xml1 from @acnts s where s.nID=@ac2_id;

						set @xml_t = 
							(select
								FAF.GoAML_char_process(cast(s3.zv_zlg_sys_lnr as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transactionnumber'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transactionnumber')) as "transactionnumber",
								FAF.GoAML_char_process(s3.auf_id,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__internal_ref_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__internal_ref_number')) as "internal_ref_number",
								FAF.GoAML_char_process(
									case 
										when s3.sitz_nr_bez is not NULL
										then 
											case
												when FAF.GoAML_get_paramS(@F_BANK_ID,97,1)='ok'
												then s3.sitz_nr_bez
												else 'Branch '+ cast(s3.sitz_nr_ausf as varchar) + ' (' + coalesce(s3.sitz_nr_bez,'') + ')'
											end
										else
											case 
												when FAF.GoAML_get_paramS(@F_BANK_ID,1,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code')) then FAF.GoAML_get_paramS(@F_BANK_ID,1,2)
												when FAF.GoAML_get_paramS(@F_BANK_ID,2,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code')) then FAF.GoAML_get_paramS(@F_BANK_ID,2,2)
												else NULL 
											end
									end
								,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_location'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_location')) as "transaction_location",
								FAF.GoAML_char_process_WR(
									@tr_dscr + ' ' + @c_txt +
									case 
										when len(@trd_txt)>0
										then '; ' + char(13) + char(10) + @trd_txt
										else ''
									end
								,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_description'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_description')) as "transaction_description",
								FAF.GoAML_char_process(
									case 
										when s3.ausf_dt_ist is not NULL
										then left(convert(varchar,cast(s3.ausf_dt_ist as datetime),126),19)
										when s3.valuta is not NULL
										then left(convert(varchar,cast(s3.valuta as datetime),126),19)
										else left(convert(varchar,cast(convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) as datetime),126),19)
									end
								,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__date_transaction'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__date_transaction')) as "date_transaction",
								FAF.GoAML_char_process(
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,130,1)='ok'
										then FAF.GoAML_get_paramS(@F_BANK_ID,132,1)
										else
											case 
												when s3.betrag_bwrg<0 
												then FAF.GoAML_get_paramS(@F_BANK_ID,94,2)
												else NULL
											end
									end
								,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_type_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_type_code')) as "transaction_type_code",
								case
									when FAF.GoAML_get_paramS(@F_BANK_ID,124,1)='ok'
									then NULL
									else FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code'))
								end as "transmode_code", 
								case
									when FAF.GoAML_get_paramS(@F_BANK_ID,124,2)='ok'
									then NULL
									else 
										FAF.GoAML_char_process(
											case
												when 
													FAF.GoAML_char_process(
														case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end,
														case
															when FAF.GoAML_get_paramS(@F_BANK_ID,61,1)='ok'
															then FAF.GoAML_get_paramS(@F_BANK_ID,61,2)
															else NULL
														end
													,0) is not NULL
												then '[' + 
													FAF.GoAML_char_process(
														case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end,
														case
															when FAF.GoAML_get_paramS(@F_BANK_ID,61,1)='ok'
															then FAF.GoAML_get_paramS(@F_BANK_ID,61,2)
															else NULL
														end
													,0) + ']: '
												else ''
											end + @tr_dscr
										,case 
											when FAF.GoAML_get_paramS(@F_BANK_ID,3,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code'))
											then FAF.GoAML_get_paramS(@F_BANK_ID,3,2)
											else FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_comment')
										end
										,FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_comment'))
								end as "transmode_comment",
								cast(
									FAF.GoAML_char_process(
										case 
											when FAF.GoAML_get_paramS(@F_BANK_ID,4,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code')) 
											then FAF.GoAML_get_paramS(@F_BANK_ID,4,2)
											else 
												case
													when abs(s3.betrag_bwrg)<0.01 
													then NULL
													else cast(cast(abs(s3.betrag_bwrg) as decimal(15,2)) as varchar)
												end 
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__amount_local'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__amount_local'))
								 as decimal(15,2)) as "amount_local",
								FAF.GoAML_char_process(
									case 
										when s3.betrag_bwrg<0 
										then FAF.GoAML_get_paramS(@F_BANK_ID,84,2)
										else FAF.GoAML_get_paramS(@F_BANK_ID,84,1)
									end
								,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_status'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_status')) as "transaction_status",
								
								--default sender
								(select
									FAF.GoAML_char_process(f_fnd.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'tf__from_funds_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tf__from_funds_code')) as "from_funds_code",
									--FAF.GoAML_char_process(
									--	case 
									--		when FAF.GoAML_get_paramS(@F_BANK_ID,51,1)='ok'
									--		then FAF.GoAML_char_process(f_fnd.comments,NULL,0)
									--		else NULL
									--	end
									--,FAF.GoAML_get_paramV(@F_BANK_ID,'tf__from_funds_comment'),FAF.GoAML_get_paramL(@F_BANK_ID,'tf__from_funds_comment')) as "from_funds_comment",
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,113,1)='ok'
										then FAF.GoAML_char_process(cur.iso_wrg_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_currency_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_currency_code'))
										else NULL
									end as "from_foreign_currency/foreign_currency_code",
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,113,1)='ok'
										then FAF.GoAML_char_process(cast(cast(abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg)) as decimal(15,2)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_amount'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_amount'))
										else NULL
									end as "from_foreign_currency/foreign_amount",
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,113,1)='ok'
										then 
											case
												when FAF.GoAML_get_paramS(@F_BANK_ID,99,1)='ok'
												then 
													FAF.GoAML_char_process(cast(cast(abs(
														case
															when abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg))>0
															then s3.betrag_bwrg/coalesce(s3.betrag_zwrg,s3.betrag_bwrg)
															else NULL
														end
													) as decimal(26,13)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_exchange_rate'))
												else FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate')
											end
										else NULL
									end as "from_foreign_currency/foreign_exchange_rate",
									(select
										FAF.GoAML_get_paramS(@F_BANK_ID,69,1) as "institution_name",
										FAF.GoAML_get_paramS(@F_BANK_ID,69,2) as "swift",
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,127,1)='ok'
											then NULL
											else
												case 
													when FAF.GoAML_get_paramS(@F_BANK_ID,89,2)='ok'
													then 
														FAF.GoAML_char_process(
															case
																when FAF.GoAML_get_paramS(@F_BANK_ID,103,2)='ok'
																then FAF.GoAML_get_paramS(@F_BANK_ID,91,2)
																when len(s3.ext_ref)<15 
																then FAF.GoAML_get_paramS(@F_BANK_ID,91,1)
																when substring(s3.ext_ref,3,2) not like '[0-9][0-9]' 
																then FAF.GoAML_get_paramS(@F_BANK_ID,91,1)
																when (select count(*) from FAF.FA_MROS_INT_LAND where userbk_nr=@F_BANK_ID and iso_land_cd=substring(s3.ext_ref,1,2))=0 
																then FAF.GoAML_get_paramS(@F_BANK_ID,91,1)
																else FAF.GoAML_get_paramS(@F_BANK_ID,90,1)
															end
														,FAF.GoAML_get_paramV(@F_BANK_ID,'ta__account_category'),FAF.GoAML_get_paramL(@F_BANK_ID,'ta__account_category'))
													else NULL
												end
										end as "account_category",
										FAF.GoAML_char_process(s3.ext_ref,FAF.GoAML_get_paramV(@F_BANK_ID,'ta__account'),FAF.GoAML_get_paramL(@F_BANK_ID,'ta__account')) as "account",
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,127,1)='ok'
											then NULL
											else
												FAF.GoAML_char_process(
													case 
														when FAF.GoAML_get_paramS(@F_BANK_ID,103,2)='ok'
														then s3.ext_ref
														else
															case 
																when len(s3.ext_ref)<15 then NULL
																when substring(s3.ext_ref,3,2) not like '[0-9][0-9]' then NULL
																when (select count(*) from FAF.FA_MROS_INT_LAND where userbk_nr=@F_BANK_ID and iso_land_cd=substring(s3.ext_ref,1,2))=0 then NULL
																else s3.ext_ref
															end
													end
												,FAF.GoAML_get_paramV(@F_BANK_ID,'ta__iban'),FAF.GoAML_get_paramL(@F_BANK_ID,'ta__iban'))
										end as "iban",
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,127,1)='ok' or FAF.GoAML_get_paramS(@F_BANK_ID,136,1)='ok'
											then NULL
											else
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,106,1)='ok'
													then FAF.GoAML_get_paramS(@F_BANK_ID,106,2)
													else NULL
												end
										end as "t_entity/name",
										FAF.GoAML_get_paramV(@F_BANK_ID,'ta__comments') as "comments"
									FOR XML PATH (''),type, ELEMENTS) as "from_account",
									@cntr as "from_country"
								FOR XML PATH ('t_from'),type, ELEMENTS),
								
								--ZV_ZLG_T.kt_lnr_b as recipient
								(select
									FAF.GoAML_char_process(t_fnd.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'ttmc__to_funds_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'ttmc__to_funds_code')) as "to_funds_code",
									FAF.GoAML_char_process(
										case 
											when FAF.GoAML_get_paramS(@F_BANK_ID,51,2)='ok'
											then FAF.GoAML_char_process(t_fnd.comments,NULL,0)
											else NULL
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'ttmc__to_funds_comment'),FAF.GoAML_get_paramL(@F_BANK_ID,'ttmc__to_funds_comment')) as "to_funds_comment",
									FAF.GoAML_char_process(cur.iso_wrg_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_currency_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_currency_code')) as "to_foreign_currency/foreign_currency_code",
									FAF.GoAML_char_process(cast(cast(abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg)) as decimal(15,2)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_amount'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_amount')) as "to_foreign_currency/foreign_amount",
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,99,1)='ok'
										then 
											FAF.GoAML_char_process(cast(cast(abs(
												case
													when abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg))>0
													then s3.betrag_bwrg/coalesce(s3.betrag_zwrg,s3.betrag_bwrg)
													else NULL
												end
											) as decimal(26,13)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_exchange_rate'))
										else FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate')
									end  as "to_foreign_currency/foreign_exchange_rate",
									isnull(@xml_ac, @xml_da) as "to_account",
									@cntr as "to_country"
								FOR XML PATH (''),type, ELEMENTS) as "t_to_my_client",

								FAF.GoAML_char_process_WR(
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
										then
											stuff(
												case
													when FAF.GoAML_char_process(isnull(cat.category_group,tr_tp.category_group),NULL,0) is not NULL
													then char(13) + char(10) + '[' + FAF.GoAML_char_process(isnull(cat.category_group,tr_tp.category_group),'',0) + ']'
													else ''
												end +
												case 
													when s3.betrag_bwrg<0 and FAF.GoAML_get_paramS(@F_BANK_ID,5,1)='ok'
													then char(13) + char(10) + FAF.GoAML_get_paramS(@F_BANK_ID,5,2) 
													else ''
												end
											, 1, LEN(char(13) + char(10)), '')
										else ''
									end +
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,134,1)='ok' and @com1_txt is not NULL
										then @com1_txt
										else ''
									end
								,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__comments')) as "comments"
							from 
								FAF.FA_MROS_ZV_ZLG_T s3 
							left join
								FAF.FA_MROS_INT_WRG cur
							on s3.iso_wrg_cd=cur.iso_wrg_cd and s3.userbk_nr=cur.userbk_nr
							left join
								FAF.FA_MROS_REFERENCE_TABLE tr_tp
							on s3.userbk_nr=tr_tp.userbk_nr and cast(s3.buchtyp_cd as varchar)=tr_tp.Finnova_value and tr_tp.ref_type=10
							left join
								FAF.FA_MROS_TRX_CATEGORIES_DET cat
							on cat.userbk_nr=s3.userbk_nr and cat.buchtyp_cd=s3.buchtyp_cd and cat.trak_ref_typ=s3.trak_ref_typ and cat.gf_nr=s3.gf_nr
							left join
								FAF.FA_MROS_REFERENCE_TABLE ref_tp
							on s3.userbk_nr=ref_tp.userbk_nr and cast(s3.trak_ref_typ as varchar)=ref_tp.Finnova_value and ref_tp.ref_type=11
							left join
								FAF.FA_MROS_REFERENCE_TABLE f_fnd
							on s3.userbk_nr=f_fnd.userbk_nr and cast(s3.buchtyp_cd as varchar)=f_fnd.Finnova_value and f_fnd.ref_type=20
							left join
								FAF.FA_MROS_REFERENCE_TABLE t_fnd
							on s3.userbk_nr=t_fnd.userbk_nr and cast(s3.buchtyp_cd as varchar)=t_fnd.Finnova_value and t_fnd.ref_type=21
							where s3.userbk_nr=@F_BANK_ID and s3.zv_zlg_sys_lnr=@n_id
							FOR XML PATH ('transaction'),type, ELEMENTS);
					end;

					if @keycd in (40,50,90,100)
					begin
						if @flag2=1
							select @xml_ac=s.xml1,@xml_cl=s.xml2,@cl_id=s.clid,@cl_tp=s.cltp from @acnts s where s.nID=@ac1_id;
						else
							select @xml_ac=s.xml1,@xml_cl=s.xml2,@cl_id=s.clid,@cl_tp=s.cltp from @acnts s where s.nID=@ac2_id;

						--1 from account to client
						--(kt_lnr and credit) or (kt_lnr_b and debit)
						if (@flag2=1 and @shcd=2) or (@flag1=1 and @shcd=1)
							begin
								set @xml_t = 
									(select
										FAF.GoAML_char_process(cast(s3.zv_zlg_sys_lnr as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transactionnumber'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transactionnumber')) as "transactionnumber",
										FAF.GoAML_char_process(s3.auf_id,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__internal_ref_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__internal_ref_number')) as "internal_ref_number",
										FAF.GoAML_char_process(
											case 
												when s3.sitz_nr_bez is not NULL
												then 
													case
														when FAF.GoAML_get_paramS(@F_BANK_ID,97,1)='ok'
														then s3.sitz_nr_bez
														else 'Branch '+ cast(s3.sitz_nr_ausf as varchar) + ' (' + coalesce(s3.sitz_nr_bez,'') + ')'
													end
												else
													case 
														when FAF.GoAML_get_paramS(@F_BANK_ID,1,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code')) then FAF.GoAML_get_paramS(@F_BANK_ID,1,2)
														when FAF.GoAML_get_paramS(@F_BANK_ID,2,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code')) then FAF.GoAML_get_paramS(@F_BANK_ID,2,2)
														else NULL 
													end
											end
										,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_location'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_location')) as "transaction_location",
										FAF.GoAML_char_process_WR(
											@tr_dscr +' ' + @d_txt +
											case 
												when len(@trd_txt)>0
												then '; ' + char(13) + char(10) + @trd_txt
												else ''
											end
										,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_description'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_description')) as "transaction_description",
										FAF.GoAML_char_process(
											case 
												when s3.ausf_dt_ist is not NULL
												then left(convert(varchar,cast(s3.ausf_dt_ist as datetime),126),19)
												when s3.valuta is not NULL
												then left(convert(varchar,cast(s3.valuta as datetime),126),19)
												else left(convert(varchar,cast(convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) as datetime),126),19)
											end
										,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__date_transaction'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__date_transaction')) as "date_transaction",
										FAF.GoAML_char_process(
											case
												when FAF.GoAML_get_paramS(@F_BANK_ID,130,1)='ok'
												then 
													case 
														when @keycd=40
														then FAF.GoAML_get_paramS(@F_BANK_ID,131,2)
														when @keycd=90 and lower(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end) = 'card_operation'
														then FAF.GoAML_get_paramS(@F_BANK_ID,83,1)
														else FAF.GoAML_get_paramS(@F_BANK_ID,132,2)
													end
												else
													case 
														when s3.betrag_bwrg<0 
														then FAF.GoAML_get_paramS(@F_BANK_ID,94,2)
														else NULL
													end
											end
										,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_type_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_type_code')) as "transaction_type_code",
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,124,1)='ok'
											then NULL
											else FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code'))
										end as "transmode_code",
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,124,2)='ok'
											then NULL
											else 
												FAF.GoAML_char_process(
													case
														when 
															FAF.GoAML_char_process(
																case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end,
																case
																	when FAF.GoAML_get_paramS(@F_BANK_ID,61,1)='ok'
																	then FAF.GoAML_get_paramS(@F_BANK_ID,61,2)
																	else NULL
																end
															,0) is not NULL
														then '[' + 
															FAF.GoAML_char_process(
																case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end,
																case
																	when FAF.GoAML_get_paramS(@F_BANK_ID,61,1)='ok'
																	then FAF.GoAML_get_paramS(@F_BANK_ID,61,2)
																	else NULL
																end
															,0) + ']: ' 
														else '' 
													end + @tr_dscr
												,case 
													when FAF.GoAML_get_paramS(@F_BANK_ID,3,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code'))
													then FAF.GoAML_get_paramS(@F_BANK_ID,3,2)
													else FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_comment')
												end
												,FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_comment'))
										end as "transmode_comment",
										cast(
											FAF.GoAML_char_process(
												case 
													when FAF.GoAML_get_paramS(@F_BANK_ID,4,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code')) 
													then FAF.GoAML_get_paramS(@F_BANK_ID,4,2)
													else 
														case
															when abs(s3.betrag_bwrg)<0.01 
															then NULL
															else cast(cast(abs(s3.betrag_bwrg) as decimal(15,2)) as varchar)
														end 
												end
											,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__amount_local'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__amount_local'))
										 as decimal(15,2)) as "amount_local",
										FAF.GoAML_char_process(
											case 
												when s3.betrag_bwrg<0 
												then FAF.GoAML_get_paramS(@F_BANK_ID,84,2)
												else FAF.GoAML_get_paramS(@F_BANK_ID,84,1)
											end
										,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_status'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_status')) as "transaction_status",

										--from account
										(select 
											FAF.GoAML_char_process(f_fnd.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'tfmc__from_funds_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfmc__from_funds_code')) as "from_funds_code",
											FAF.GoAML_char_process(
												case 
													when FAF.GoAML_get_paramS(@F_BANK_ID,51,1)='ok'
													then FAF.GoAML_char_process(f_fnd.comments,NULL,0)
													else NULL
												end
											,FAF.GoAML_get_paramV(@F_BANK_ID,'tfmc__from_funds_comment'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfmc__from_funds_comment')) as "from_funds_comment",
											FAF.GoAML_char_process(cur.iso_wrg_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_currency_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_currency_code')) as "from_foreign_currency/foreign_currency_code",
											FAF.GoAML_char_process(cast(cast(abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg)) as decimal(15,2)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_amount'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_amount')) as "from_foreign_currency/foreign_amount",
											case
												when FAF.GoAML_get_paramS(@F_BANK_ID,99,1)='ok'
												then 
													FAF.GoAML_char_process(cast(cast(abs(
														case
															when abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg))>0
															then s3.betrag_bwrg/coalesce(s3.betrag_zwrg,s3.betrag_bwrg)
															else NULL
														end
													) as decimal(26,13)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_exchange_rate'))
												else FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate')
											end as "from_foreign_currency/foreign_exchange_rate",
											isnull(@xml_ac, @xml_da) as "from_account",
											@cntr as "from_country"
										FOR XML PATH ('t_from_my_client'),type, ELEMENTS),
										--to client
										(select 
											FAF.GoAML_char_process(
												isnull(t_fnd.MROS_value,case when FAF.GoAML_get_paramS(@F_BANK_ID,129,1)='ok' then FAF.GoAML_get_paramS(@F_BANK_ID,129,2) else NULL end)
											,FAF.GoAML_get_paramV(@F_BANK_ID,'ttmc__to_funds_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'ttmc__to_funds_code')) as "to_funds_code",
											FAF.GoAML_char_process(
												case 
													when FAF.GoAML_get_paramS(@F_BANK_ID,51,2)='ok'
													then FAF.GoAML_char_process(t_fnd.comments,NULL,0)
													else NULL
												end
											,FAF.GoAML_get_paramV(@F_BANK_ID,'ttmc__to_funds_comment'),FAF.GoAML_get_paramL(@F_BANK_ID,'ttmc__to_funds_comment')) as "to_funds_comment",
											FAF.GoAML_char_process(cur.iso_wrg_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_currency_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_currency_code')) as "to_foreign_currency/foreign_currency_code",
											FAF.GoAML_char_process(cast(cast(abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg)) as decimal(15,2)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_amount'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_amount')) as "to_foreign_currency/foreign_amount",
											case
												when FAF.GoAML_get_paramS(@F_BANK_ID,99,1)='ok'
												then 
													FAF.GoAML_char_process(cast(cast(abs(
														case
															when abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg))>0
															then s3.betrag_bwrg/coalesce(s3.betrag_zwrg,s3.betrag_bwrg)
															else NULL
														end
													) as decimal(26,13)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_exchange_rate'))
												else FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate')
											end as "to_foreign_currency/foreign_exchange_rate",
											case
												when @cl_tp=1 
												then isnull(@xml_cl, @xml_dp)
												else NULL
											end as "to_person",
											case
												when @cl_tp>1 
												then isnull(@xml_cl, @xml_de)
												else NULL
											end as "to_entity",
											@cntr as "to_country"
										FOR XML PATH ('t_to_my_client'),type, ELEMENTS),
										FAF.GoAML_char_process_WR(
											case
												when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
												then
													stuff(
														case
															when FAF.GoAML_char_process(isnull(cat.category_group,tr_tp.category_group),NULL,0) is not NULL
															then char(13) + char(10) + '[' + FAF.GoAML_char_process(isnull(cat.category_group,tr_tp.category_group),'',0) + ']'
															else ''
														end +
														case 
															when s3.betrag_bwrg<0 and FAF.GoAML_get_paramS(@F_BANK_ID,5,1)='ok'
															then char(13) + char(10) + FAF.GoAML_get_paramS(@F_BANK_ID,5,2) 
															else ''
														end
													, 1, LEN(char(13) + char(10)), '')
												else ''
											end +
											case
												when FAF.GoAML_get_paramS(@F_BANK_ID,134,1)='ok' and @com1_txt is not NULL
												then @com1_txt
												else ''
											end
										,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__comments')) as "comments"
									from 
										FAF.FA_MROS_ZV_ZLG_T s3 
									left join
										FAF.FA_MROS_INT_WRG cur
									on s3.iso_wrg_cd=cur.iso_wrg_cd and s3.userbk_nr=cur.userbk_nr
									left join
										FAF.FA_MROS_REFERENCE_TABLE tr_tp
									on s3.userbk_nr=tr_tp.userbk_nr and cast(s3.buchtyp_cd as varchar)=tr_tp.Finnova_value and tr_tp.ref_type=10
									left join
										FAF.FA_MROS_TRX_CATEGORIES_DET cat
									on cat.userbk_nr=s3.userbk_nr and cat.buchtyp_cd=s3.buchtyp_cd and cat.trak_ref_typ=s3.trak_ref_typ and cat.gf_nr=s3.gf_nr
									left join
										FAF.FA_MROS_REFERENCE_TABLE ref_tp
									on s3.userbk_nr=ref_tp.userbk_nr and cast(s3.trak_ref_typ as varchar)=ref_tp.Finnova_value and ref_tp.ref_type=11
									left join
										FAF.FA_MROS_REFERENCE_TABLE f_fnd
									on s3.userbk_nr=f_fnd.userbk_nr and cast(s3.buchtyp_cd as varchar)=f_fnd.Finnova_value and f_fnd.ref_type=20
									left join
										FAF.FA_MROS_REFERENCE_TABLE t_fnd
									on s3.userbk_nr=t_fnd.userbk_nr and cast(s3.buchtyp_cd as varchar)=t_fnd.Finnova_value and t_fnd.ref_type=21
									where s3.userbk_nr=@F_BANK_ID and s3.zv_zlg_sys_lnr=@n_id
									FOR XML PATH ('transaction'),type, ELEMENTS);
							end;
						else
							--2 from client to account
							begin
								set @xml_t = 
									(select
										FAF.GoAML_char_process(cast(s3.zv_zlg_sys_lnr as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transactionnumber'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transactionnumber')) as "transactionnumber",
										FAF.GoAML_char_process(s3.auf_id,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__internal_ref_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__internal_ref_number')) as "internal_ref_number",
										FAF.GoAML_char_process(
											case 
												when s3.sitz_nr_bez is not NULL
												then 
													case
														when FAF.GoAML_get_paramS(@F_BANK_ID,97,1)='ok'
														then s3.sitz_nr_bez
														else 'Branch '+ cast(s3.sitz_nr_ausf as varchar) + ' (' + coalesce(s3.sitz_nr_bez,'') + ')'
													end
												else
													case 
														when FAF.GoAML_get_paramS(@F_BANK_ID,1,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code')) then FAF.GoAML_get_paramS(@F_BANK_ID,1,2)
														when FAF.GoAML_get_paramS(@F_BANK_ID,2,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code')) then FAF.GoAML_get_paramS(@F_BANK_ID,2,2)
														else NULL 
													end
											end
										,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_location'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_location')) as "transaction_location",
										FAF.GoAML_char_process_WR(
											@tr_dscr + ' ' + @c_txt +
											case 
												when len(@trd_txt)>0
												then '; ' + char(13) + char(10) + @trd_txt
												else ''
											end
										,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_description'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_description')) as "transaction_description",
										FAF.GoAML_char_process(
											case 
												when s3.ausf_dt_ist is not NULL
												then left(convert(varchar,cast(s3.ausf_dt_ist as datetime),126),19)
												when s3.valuta is not NULL
												then left(convert(varchar,cast(s3.valuta as datetime),126),19)
												else left(convert(varchar,cast(convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) as datetime),126),19)
											end
										,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__date_transaction'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__date_transaction')) as "date_transaction",
										FAF.GoAML_char_process(
											case
												when FAF.GoAML_get_paramS(@F_BANK_ID,130,1)='ok'
												then
													case 
														when @keycd=40
														then FAF.GoAML_get_paramS(@F_BANK_ID,131,2)
														when @keycd=90 and lower(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end) = 'card_operation'
														then FAF.GoAML_get_paramS(@F_BANK_ID,83,2)
														else FAF.GoAML_get_paramS(@F_BANK_ID,132,2)
													end
												else
													case 
														when s3.betrag_bwrg<0 
														then FAF.GoAML_get_paramS(@F_BANK_ID,94,2)
														else NULL
													end
											end
										,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_type_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_type_code')) as "transaction_type_code",
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,124,1)='ok'
											then NULL
											else FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code'))
										end as "transmode_code",
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,124,2)='ok'
											then NULL
											else 
												FAF.GoAML_char_process(
													case
														when 
															FAF.GoAML_char_process(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end,
																case
																	when FAF.GoAML_get_paramS(@F_BANK_ID,61,1)='ok'
																	then FAF.GoAML_get_paramS(@F_BANK_ID,61,2)
																	else NULL
																end
															,0) is not NULL
														then '[' + 
															FAF.GoAML_char_process(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end,
																case
																	when FAF.GoAML_get_paramS(@F_BANK_ID,61,1)='ok'
																	then FAF.GoAML_get_paramS(@F_BANK_ID,61,2)
																	else NULL
																end
															,0) + ']: ' 
														else ''
													end + @tr_dscr
												,case 
													when FAF.GoAML_get_paramS(@F_BANK_ID,3,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code'))
													then FAF.GoAML_get_paramS(@F_BANK_ID,3,2)
													else FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_comment')
												end
												,FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_comment'))
										end as "transmode_comment",
										cast(
											FAF.GoAML_char_process(
												case 
													when FAF.GoAML_get_paramS(@F_BANK_ID,4,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code')) 
													then FAF.GoAML_get_paramS(@F_BANK_ID,4,2)
													else 
														case
															when abs(s3.betrag_bwrg)<0.01 
															then NULL
															else cast(cast(abs(s3.betrag_bwrg) as decimal(15,2)) as varchar)
														end 
												end
											,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__amount_local'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__amount_local'))
										 as decimal(15,2)) as "amount_local",
										FAF.GoAML_char_process(
											case 
												when s3.betrag_bwrg<0 
												then FAF.GoAML_get_paramS(@F_BANK_ID,84,2)
												else FAF.GoAML_get_paramS(@F_BANK_ID,84,1)
											end
										,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_status'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_status')) as "transaction_status",

										--from account
										(select 
											FAF.GoAML_char_process(
												isnull(f_fnd.MROS_value,case when FAF.GoAML_get_paramS(@F_BANK_ID,129,1)='ok' then FAF.GoAML_get_paramS(@F_BANK_ID,129,2) else NULL end)
											,FAF.GoAML_get_paramV(@F_BANK_ID,'tfmc__from_funds_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfmc__from_funds_code')) as "from_funds_code",
											FAF.GoAML_char_process(
												case 
													when FAF.GoAML_get_paramS(@F_BANK_ID,51,1)='ok'
													then FAF.GoAML_char_process(f_fnd.comments,NULL,0)
													else NULL
												end
											,FAF.GoAML_get_paramV(@F_BANK_ID,'tfmc__from_funds_comment'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfmc__from_funds_comment')) as "from_funds_comment",
											FAF.GoAML_char_process(cur.iso_wrg_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_currency_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_currency_code')) as "from_foreign_currency/foreign_currency_code",
											FAF.GoAML_char_process(cast(cast(abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg)) as decimal(15,2)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_amount'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_amount')) as "from_foreign_currency/foreign_amount",
											case
												when FAF.GoAML_get_paramS(@F_BANK_ID,99,1)='ok'
												then 
													FAF.GoAML_char_process(cast(cast(abs(
														case
															when abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg))>0
															then s3.betrag_bwrg/coalesce(s3.betrag_zwrg,s3.betrag_bwrg)
															else NULL
														end
													) as decimal(26,13)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_exchange_rate'))
												else FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate')
											end as "from_foreign_currency/foreign_exchange_rate",
											case
												when @cl_tp=1 
												then isnull(@xml_cl, @xml_dp)
												else NULL
											end as "from_person",
											case
												when @cl_tp>1 
												then isnull(@xml_cl, @xml_de)
												else NULL
											end as "from_entity",
											@cntr as "from_country"
										FOR XML PATH ('t_from_my_client'),type, ELEMENTS),
										--to client
										(select 
											FAF.GoAML_char_process(t_fnd.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'ttmc__to_funds_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'ttmc__to_funds_code')) as "to_funds_code",
											FAF.GoAML_char_process(
												case 
													when FAF.GoAML_get_paramS(@F_BANK_ID,51,2)='ok'
													then FAF.GoAML_char_process(t_fnd.comments,NULL,0)
													else NULL
												end
											,FAF.GoAML_get_paramV(@F_BANK_ID,'ttmc__to_funds_comment'),FAF.GoAML_get_paramL(@F_BANK_ID,'ttmc__to_funds_comment')) as "to_funds_comment",
											FAF.GoAML_char_process(cur.iso_wrg_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_currency_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_currency_code')) as "to_foreign_currency/foreign_currency_code",
											FAF.GoAML_char_process(cast(cast(abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg)) as decimal(15,2)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_amount'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_amount')) as "to_foreign_currency/foreign_amount",
											case
												when FAF.GoAML_get_paramS(@F_BANK_ID,99,1)='ok'
												then 
													FAF.GoAML_char_process(cast(cast(abs(
														case
															when abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg))>0
															then s3.betrag_bwrg/coalesce(s3.betrag_zwrg,s3.betrag_bwrg)
															else NULL
														end
													) as decimal(26,13)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_exchange_rate'))
												else FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate')
											end as "to_foreign_currency/foreign_exchange_rate",
											isnull(@xml_ac, @xml_da) as "to_account",
											@cntr as "to_country"
										FOR XML PATH ('t_to_my_client'),type, ELEMENTS),
										FAF.GoAML_char_process_WR(
											case
												when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
												then
													stuff(
														case
															when FAF.GoAML_char_process(isnull(cat.category_group,tr_tp.category_group),NULL,0) is not NULL
															then char(13) + char(10) + '[' + FAF.GoAML_char_process(isnull(cat.category_group,tr_tp.category_group),'',0) + ']'
															else ''
														end +
														case 
															when s3.betrag_bwrg<0 and FAF.GoAML_get_paramS(@F_BANK_ID,5,1)='ok'
															then char(13) + char(10) + FAF.GoAML_get_paramS(@F_BANK_ID,5,2) 
															else ''
														end
													, 1, LEN(char(13) + char(10)), '')
												else ''
											end +
											case
												when FAF.GoAML_get_paramS(@F_BANK_ID,134,1)='ok' and @com1_txt is not NULL
												then @com1_txt
												else ''
											end
										,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__comments')) as "comments"
									from 
										FAF.FA_MROS_ZV_ZLG_T s3 
									left join
										FAF.FA_MROS_INT_WRG cur
									on s3.iso_wrg_cd=cur.iso_wrg_cd and s3.userbk_nr=cur.userbk_nr
									left join
										FAF.FA_MROS_REFERENCE_TABLE tr_tp
									on s3.userbk_nr=tr_tp.userbk_nr and cast(s3.buchtyp_cd as varchar)=tr_tp.Finnova_value and tr_tp.ref_type=10
									left join
										FAF.FA_MROS_TRX_CATEGORIES_DET cat
									on cat.userbk_nr=s3.userbk_nr and cat.buchtyp_cd=s3.buchtyp_cd and cat.trak_ref_typ=s3.trak_ref_typ and cat.gf_nr=s3.gf_nr
									left join
										FAF.FA_MROS_REFERENCE_TABLE ref_tp
									on s3.userbk_nr=ref_tp.userbk_nr and cast(s3.trak_ref_typ as varchar)=ref_tp.Finnova_value and ref_tp.ref_type=11
									left join
										FAF.FA_MROS_REFERENCE_TABLE f_fnd
									on s3.userbk_nr=f_fnd.userbk_nr and cast(s3.buchtyp_cd as varchar)=f_fnd.Finnova_value and f_fnd.ref_type=20
									left join
										FAF.FA_MROS_REFERENCE_TABLE t_fnd
									on s3.userbk_nr=t_fnd.userbk_nr and cast(s3.buchtyp_cd as varchar)=t_fnd.Finnova_value and t_fnd.ref_type=21
									where s3.userbk_nr=@F_BANK_ID and s3.zv_zlg_sys_lnr=@n_id
									FOR XML PATH ('transaction'),type, ELEMENTS);
							end;
					end;
					
					if @keycd in (10,20,30,80)
					begin
						set @flag1 = NULL;
						set @flag2 = NULL;
						select @xml_ac = xml1, @flag1=flg from @acnts where nID = @ac1_id;

						--1 Input transaction (the recipient is OM_AUF.kt_lnr)
						if @pmnt_tp = 1
						begin
							select 
								@xml_in = 
									(select 
										(select
											FAF.GoAML_char_process(
												FAF.GoAML_char_process(
													case 
														when len(s25.bk_name)>1
														then s25.bk_name
														when patindex('%' + char(10) + '%',s25.adr_zeilen)>0 
														then substring(s25.adr_zeilen,1,patindex('%' + char(10) + '%',s25.adr_zeilen)-1) 
														when len(s25.adr_zeilen)>1 
														then s25.adr_zeilen
														else NULL
													end
												,@df_esr_txt,0)
											,FAF.GoAML_get_paramV(@F_BANK_ID,'ta__institution_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'ta__institution_name')) as "institution_name",
											FAF.GoAML_char_process(
												FAF.GoAML_char_process(
													case
														when len(s25.bic_cp)>1 then s25.bic_cp
														when len(s25.bic)>1 then s25.bic
														when len(s25.kt_id)>1 then s25.kt_id
														else NULL
													end
												,@df_esr_txt,0)
											,FAF.GoAML_get_paramV(@F_BANK_ID,'ta__swift'),FAF.GoAML_get_paramL(@F_BANK_ID,'ta__swift')) as "swift",
											case
												when FAF.GoAML_get_paramS(@F_BANK_ID,127,1)='ok'
												then NULL
												else
													case
														when FAF.GoAML_get_paramS(@F_BANK_ID,85,2)='ok'
														then FAF.GoAML_char_process(from_lnd.iso_land_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'ta__institution_country'),FAF.GoAML_get_paramL(@F_BANK_ID,'ta__institution_country'))
														else NULL
													end
											end as "institution_country",
											case
												when FAF.GoAML_get_paramS(@F_BANK_ID,127,1)='ok'
												then NULL
												else
													case 
														when FAF.GoAML_get_paramS(@F_BANK_ID,89,2)='ok'
														then 
															FAF.GoAML_char_process(
																case
																	when len(coalesce(s25.kt_nr, s25.iban_id, s25.qr_iban_id,@iban_txt,@kt_orig))<15 
																	then FAF.GoAML_get_paramS(@F_BANK_ID,91,1)
																	when substring(coalesce(s25.kt_nr, s25.iban_id, s25.qr_iban_id,@iban_txt,@kt_orig),3,2) not like '[0-9][0-9]' 
																	then FAF.GoAML_get_paramS(@F_BANK_ID,91,1)
																	when (select count(*) from FAF.FA_MROS_INT_LAND where userbk_nr=@F_BANK_ID and iso_land_cd=substring(coalesce(s25.kt_nr, s25.iban_id, s25.qr_iban_id,@iban_txt,@kt_orig),1,2))=0 
																	then FAF.GoAML_get_paramS(@F_BANK_ID,91,1)
																	else FAF.GoAML_get_paramS(@F_BANK_ID,90,1)
																end
															,FAF.GoAML_get_paramV(@F_BANK_ID,'ta__account_category'),FAF.GoAML_get_paramL(@F_BANK_ID,'ta__account_category'))
														else NULL
													end
											end as "account_category",
											FAF.GoAML_char_process(
												coalesce(s25.kt_nr, s25.iban_id, s25.qr_iban_id,@iban_txt,@kt_orig)
											,FAF.GoAML_get_paramV(@F_BANK_ID,'ta__account'),FAF.GoAML_get_paramL(@F_BANK_ID,'ta__account')) as "account",
											case
												when FAF.GoAML_get_paramS(@F_BANK_ID,127,1)='ok'
												then NULL
												else
													FAF.GoAML_char_process(
														case
															when len(coalesce(s25.kt_nr, s25.iban_id, s25.qr_iban_id,@iban_txt,@kt_orig))<15 then NULL
															when substring(coalesce(s25.kt_nr, s25.iban_id, s25.qr_iban_id,@iban_txt,@kt_orig),3,2) not like '[0-9][0-9]' then NULL
															when (select count(*) from FAF.FA_MROS_INT_LAND where userbk_nr=@F_BANK_ID and iso_land_cd=substring(coalesce(s25.kt_nr, s25.iban_id, s25.qr_iban_id,@iban_txt,@kt_orig),1,2))=0 then NULL
															else coalesce(s25.kt_nr, s25.iban_id, s25.qr_iban_id,@iban_txt,@kt_orig)
														end
													,FAF.GoAML_get_paramV(@F_BANK_ID,'ta__iban'),FAF.GoAML_get_paramL(@F_BANK_ID,'ta__iban'))
											end as "iban",
											@D_xml,
											case
												when FAF.GoAML_get_paramS(@F_BANK_ID,127,1)='ok' or FAF.GoAML_get_paramS(@F_BANK_ID,136,1)='ok'
												then NULL
												when FAF.GoAML_get_paramS(@F_BANK_ID,106,1)='ok' and @D_xml is NULL
												then 
													FAF.GoAML_char_process(
														case
															when charindex('(1): ',coalesce(s25.counterparty_details,@com_txt))>0
															then
																case
																	when charindex(char(10),substring(coalesce(s25.counterparty_details,@com_txt),charindex('(1): ',coalesce(s25.counterparty_details,@com_txt))+len('(1): '),len(coalesce(s25.counterparty_details,@com_txt))))>0
																	then 
																		case 
																			when patindex('%[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]%',replace(replace(replace(replace(substring(coalesce(s25.counterparty_details,@com_txt),charindex('(1): ',coalesce(s25.counterparty_details,@com_txt)),charindex(char(10),substring(coalesce(s25.counterparty_details,@com_txt),charindex('(1): ',coalesce(s25.counterparty_details,@com_txt))+len('(1): '),len(coalesce(s25.counterparty_details,@com_txt))))),' ',''),'-',''),'/',''),'.',''))>0
																			then 
																				case 
																					when charindex(char(10),substring(coalesce(s25.counterparty_details,@com_txt),charindex(char(10),coalesce(s25.counterparty_details,@com_txt),charindex('(1): ',coalesce(s25.counterparty_details,@com_txt)))+1,len(coalesce(s25.counterparty_details,@com_txt))))>0
																					then substring(coalesce(s25.counterparty_details,@com_txt),charindex(char(10),coalesce(s25.counterparty_details,@com_txt),charindex('(1): ',coalesce(s25.counterparty_details,@com_txt)))+1,charindex(char(10),substring(coalesce(s25.counterparty_details,@com_txt),charindex(char(10),coalesce(s25.counterparty_details,@com_txt),charindex('(1): ',coalesce(s25.counterparty_details,@com_txt)))+1,len(coalesce(s25.counterparty_details,@com_txt))))-1)
																					else substring(coalesce(s25.counterparty_details,@com_txt),charindex(char(10),coalesce(s25.counterparty_details,@com_txt),charindex('(1): ',coalesce(s25.counterparty_details,@com_txt)))+1,len(coalesce(s25.counterparty_details,@com_txt)))
																				end
																			else 
																				substring(coalesce(s25.counterparty_details,@com_txt),charindex('(1): ',coalesce(s25.counterparty_details,@com_txt))+len('(1): '),charindex(char(10),substring(coalesce(s25.counterparty_details,@com_txt),charindex('(1): ',coalesce(s25.counterparty_details,@com_txt))+len('(1): '),len(coalesce(s25.counterparty_details,@com_txt))))-1)
																		end
																	when patindex('%[0-9]%',substring(coalesce(s25.counterparty_details,@com_txt),charindex('(1): ',coalesce(s25.counterparty_details,@com_txt))+len('(1): '),len(coalesce(s25.counterparty_details,@com_txt))))=0
																	then substring(coalesce(s25.counterparty_details,@com_txt),charindex('(1): ',coalesce(s25.counterparty_details,@com_txt))+len('(1): '),len(coalesce(s25.counterparty_details,@com_txt)))
																	else NULL
																end
															when charindex('1: ',coalesce(s25.counterparty_details,@com_txt))>0
															then
																case
																	when charindex(char(10),substring(coalesce(s25.counterparty_details,@com_txt),charindex('1: ',coalesce(s25.counterparty_details,@com_txt))+len('1: '),len(coalesce(s25.counterparty_details,@com_txt))))>0
																	then 
																		case 
																			when patindex('%[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]%',replace(replace(replace(replace(substring(coalesce(s25.counterparty_details,@com_txt),charindex('1: ',coalesce(s25.counterparty_details,@com_txt)),charindex(char(10),substring(coalesce(s25.counterparty_details,@com_txt),charindex('1: ',coalesce(s25.counterparty_details,@com_txt))+len('1: '),len(coalesce(s25.counterparty_details,@com_txt))))),' ',''),'-',''),'/',''),'.',''))>0
																			then 
																				case 
																					when charindex(char(10),substring(coalesce(s25.counterparty_details,@com_txt),charindex(char(10),coalesce(s25.counterparty_details,@com_txt),charindex('1: ',coalesce(s25.counterparty_details,@com_txt)))+1,len(coalesce(s25.counterparty_details,@com_txt))))>0
																					then substring(coalesce(s25.counterparty_details,@com_txt),charindex(char(10),coalesce(s25.counterparty_details,@com_txt),charindex('1: ',coalesce(s25.counterparty_details,@com_txt)))+1,charindex(char(10),substring(coalesce(s25.counterparty_details,@com_txt),charindex(char(10),coalesce(s25.counterparty_details,@com_txt),charindex('1: ',coalesce(s25.counterparty_details,@com_txt)))+1,len(coalesce(s25.counterparty_details,@com_txt))))-1)
																					else substring(coalesce(s25.counterparty_details,@com_txt),charindex(char(10),coalesce(s25.counterparty_details,@com_txt),charindex('1: ',coalesce(s25.counterparty_details,@com_txt)))+1,len(coalesce(s25.counterparty_details,@com_txt)))
																				end
																			else 
																				substring(coalesce(s25.counterparty_details,@com_txt),charindex('1: ',coalesce(s25.counterparty_details,@com_txt))+len('1: '),charindex(char(10),substring(coalesce(s25.counterparty_details,@com_txt),charindex('1: ',coalesce(s25.counterparty_details,@com_txt))+len('1: '),len(coalesce(s25.counterparty_details,@com_txt))))-1)
																		end
																	when patindex('%[0-9]%',substring(coalesce(s25.counterparty_details,@com_txt),charindex('1: ',coalesce(s25.counterparty_details,@com_txt))+len('1: '),len(coalesce(s25.counterparty_details,@com_txt))))=0
																	then substring(coalesce(s25.counterparty_details,@com_txt),charindex('1: ',coalesce(s25.counterparty_details,@com_txt))+len('1: '),len(coalesce(s25.counterparty_details,@com_txt)))
																	else NULL
																end
														end
													,FAF.GoAML_get_paramS(@F_BANK_ID,106,2),FAF.GoAML_get_paramL(@F_BANK_ID,'te__name'))
												else NULL
											end as "t_entity/name",
											case
												when FAF.GoAML_get_paramS(@F_BANK_ID,127,1)='ok'
												then NULL
												when FAF.GoAML_get_paramS(@F_BANK_ID,136,1)='ok' and FAF.GoAML_get_paramS(@F_BANK_ID,106,1)='ok' and @D_xml is NULL
												then
													(select
														FAF.GoAML_get_paramS(@F_BANK_ID,136,2) as "account_entity_relation",
														FAF.GoAML_char_process(
															case
																when charindex('(1): ',coalesce(s25.counterparty_details,@com_txt))>0
																then
																	case
																		when charindex(char(10),substring(coalesce(s25.counterparty_details,@com_txt),charindex('(1): ',coalesce(s25.counterparty_details,@com_txt))+len('(1): '),len(coalesce(s25.counterparty_details,@com_txt))))>0
																		then 
																			case 
																				when patindex('%[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]%',replace(replace(replace(replace(substring(coalesce(s25.counterparty_details,@com_txt),charindex('(1): ',coalesce(s25.counterparty_details,@com_txt)),charindex(char(10),substring(coalesce(s25.counterparty_details,@com_txt),charindex('(1): ',coalesce(s25.counterparty_details,@com_txt))+len('(1): '),len(coalesce(s25.counterparty_details,@com_txt))))),' ',''),'-',''),'/',''),'.',''))>0
																				then 
																					case 
																						when charindex(char(10),substring(coalesce(s25.counterparty_details,@com_txt),charindex(char(10),coalesce(s25.counterparty_details,@com_txt),charindex('(1): ',coalesce(s25.counterparty_details,@com_txt)))+1,len(coalesce(s25.counterparty_details,@com_txt))))>0
																						then substring(coalesce(s25.counterparty_details,@com_txt),charindex(char(10),coalesce(s25.counterparty_details,@com_txt),charindex('(1): ',coalesce(s25.counterparty_details,@com_txt)))+1,charindex(char(10),substring(coalesce(s25.counterparty_details,@com_txt),charindex(char(10),coalesce(s25.counterparty_details,@com_txt),charindex('(1): ',coalesce(s25.counterparty_details,@com_txt)))+1,len(coalesce(s25.counterparty_details,@com_txt))))-1)
																						else substring(coalesce(s25.counterparty_details,@com_txt),charindex(char(10),coalesce(s25.counterparty_details,@com_txt),charindex('(1): ',coalesce(s25.counterparty_details,@com_txt)))+1,len(coalesce(s25.counterparty_details,@com_txt)))
																					end
																				else 
																					substring(coalesce(s25.counterparty_details,@com_txt),charindex('(1): ',coalesce(s25.counterparty_details,@com_txt))+len('(1): '),charindex(char(10),substring(coalesce(s25.counterparty_details,@com_txt),charindex('(1): ',coalesce(s25.counterparty_details,@com_txt))+len('(1): '),len(coalesce(s25.counterparty_details,@com_txt))))-1)
																			end
																		when patindex('%[0-9]%',substring(coalesce(s25.counterparty_details,@com_txt),charindex('(1): ',coalesce(s25.counterparty_details,@com_txt))+len('(1): '),len(coalesce(s25.counterparty_details,@com_txt))))=0
																		then substring(coalesce(s25.counterparty_details,@com_txt),charindex('(1): ',coalesce(s25.counterparty_details,@com_txt))+len('(1): '),len(coalesce(s25.counterparty_details,@com_txt)))
																		else NULL
																	end
																when charindex('1: ',coalesce(s25.counterparty_details,@com_txt))>0
																then
																	case
																		when charindex(char(10),substring(coalesce(s25.counterparty_details,@com_txt),charindex('1: ',coalesce(s25.counterparty_details,@com_txt))+len('1: '),len(coalesce(s25.counterparty_details,@com_txt))))>0
																		then 
																			case 
																				when patindex('%[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]%',replace(replace(replace(replace(substring(coalesce(s25.counterparty_details,@com_txt),charindex('1: ',coalesce(s25.counterparty_details,@com_txt)),charindex(char(10),substring(coalesce(s25.counterparty_details,@com_txt),charindex('1: ',coalesce(s25.counterparty_details,@com_txt))+len('1: '),len(coalesce(s25.counterparty_details,@com_txt))))),' ',''),'-',''),'/',''),'.',''))>0
																				then 
																					case 
																						when charindex(char(10),substring(coalesce(s25.counterparty_details,@com_txt),charindex(char(10),coalesce(s25.counterparty_details,@com_txt),charindex('1: ',coalesce(s25.counterparty_details,@com_txt)))+1,len(coalesce(s25.counterparty_details,@com_txt))))>0
																						then substring(coalesce(s25.counterparty_details,@com_txt),charindex(char(10),coalesce(s25.counterparty_details,@com_txt),charindex('1: ',coalesce(s25.counterparty_details,@com_txt)))+1,charindex(char(10),substring(coalesce(s25.counterparty_details,@com_txt),charindex(char(10),coalesce(s25.counterparty_details,@com_txt),charindex('1: ',coalesce(s25.counterparty_details,@com_txt)))+1,len(coalesce(s25.counterparty_details,@com_txt))))-1)
																						else substring(coalesce(s25.counterparty_details,@com_txt),charindex(char(10),coalesce(s25.counterparty_details,@com_txt),charindex('1: ',coalesce(s25.counterparty_details,@com_txt)))+1,len(coalesce(s25.counterparty_details,@com_txt)))
																					end
																				else 
																					substring(coalesce(s25.counterparty_details,@com_txt),charindex('1: ',coalesce(s25.counterparty_details,@com_txt))+len('1: '),charindex(char(10),substring(coalesce(s25.counterparty_details,@com_txt),charindex('1: ',coalesce(s25.counterparty_details,@com_txt))+len('1: '),len(coalesce(s25.counterparty_details,@com_txt))))-1)
																			end
																		when patindex('%[0-9]%',substring(coalesce(s25.counterparty_details,@com_txt),charindex('1: ',coalesce(s25.counterparty_details,@com_txt))+len('1: '),len(coalesce(s25.counterparty_details,@com_txt))))=0
																		then substring(coalesce(s25.counterparty_details,@com_txt),charindex('1: ',coalesce(s25.counterparty_details,@com_txt))+len('1: '),len(coalesce(s25.counterparty_details,@com_txt)))
																		else NULL
																	end
															end
														,FAF.GoAML_get_paramS(@F_BANK_ID,106,2),FAF.GoAML_get_paramL(@F_BANK_ID,'te__name')) as "entity/name"
													FOR XML PATH ('account_related_entity'),type, ELEMENTS)
												else NULL
											end as "related_entities",
											case 
												when @D_xml is NULL 
												then FAF.GoAML_char_process_WR(coalesce(s25.counterparty_details,@com_txt),FAF.GoAML_get_paramV(@F_BANK_ID,'ta__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'ta__comments'))
												else NULL 
											end as "comments"
										FOR XML PATH (''),type, ELEMENTS) as "from_account",
										FAF.GoAML_char_process(isnull(from_lnd.iso_land_cd,from_lnd1.iso_land_cd),FAF.GoAML_get_paramV(@F_BANK_ID,'tf__from_country'),FAF.GoAML_get_paramL(@F_BANK_ID,'tf__from_country')) as "from_country"
									FOR XML PATH (''),type, ELEMENTS),
								@esr_txt = 
									case
										when @esr='Post'
										then 
											rtrim(left('Post ' + 
												isnull(
													FAF.GoAML_char_process(
														coalesce(s25.kt_nr,s25.iban_id,s25.qr_iban_id,@iban_txt,@kt_orig)
													,NULL,0)
												,'')
											,4000))
										when @esr is not NULL
										then 
											rtrim(left('ESR ' + 
												isnull(
													FAF.GoAML_char_process(
														coalesce(s25.kt_nr,s25.iban_id,s25.qr_iban_id,@iban_txt,@kt_orig)
													,NULL,0)
												,'')
												 + '; Referenz nummer ' + @esr
											,4000))
										else NULL
									end
							from
								(select *,
									upper(substring(
										case
											when len(bic_cp)>1 then bic_cp
											when len(bic)>1 then bic
											when len(kt_id)>1 then kt_id
											else NULL
										end
									,5,2)) as from_cntr,
									upper(substring(
										FAF.GoAML_char_process(
											coalesce(kt_nr,iban_id,qr_iban_id,@iban_txt,@kt_orig)
										,NULL,0)
									,1,2)) as from_cntr1
								from FAF.FA_MROS_ZV_ADR_EFF where userbk_nr=@F_BANK_ID and zv_adr_eff_lnr=@au_nr) s25 
							left join
								FAF.FA_MROS_INT_LAND from_lnd
							on s25.from_cntr=from_lnd.iso_land_cd and s25.userbk_nr=from_lnd.userbk_nr
							left join
								FAF.FA_MROS_INT_LAND from_lnd1
							on s25.from_cntr1=from_lnd1.iso_land_cd and s25.userbk_nr=from_lnd1.userbk_nr;

							if @xml_in is NULL
							begin
								set @xml_in = 
									(select 
										FAF.GoAML_get_paramV(@F_BANK_ID,'ta__institution_name') as "from_account/institution_name",
										FAF.GoAML_get_paramV(@F_BANK_ID,'ta__swift') as "from_account/swift",
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,127,1)='ok'
											then NULL
											else
												case 
													when FAF.GoAML_get_paramS(@F_BANK_ID,89,2)='ok'
													then FAF.GoAML_get_paramV(@F_BANK_ID,'ta__account_category')
													else NULL
												end
										end as "from_account/account_category",
										FAF.GoAML_get_paramV(@F_BANK_ID,'ta__account') as "from_account/account",
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,127,1)='ok' or FAF.GoAML_get_paramS(@F_BANK_ID,136,1)='ok'
											then NULL
											else
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,106,1)='ok'
													then FAF.GoAML_get_paramS(@F_BANK_ID,106,2)
													else NULL
												end
										end as "from_account/t_entity/name",
										FAF.GoAML_char_process_WR(@com_txt,FAF.GoAML_get_paramV(@F_BANK_ID,'ta__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'ta__comments')) as "from_account/comments",
										FAF.GoAML_get_paramV(@F_BANK_ID,'tf__from_country') as "from_country"
									FOR XML PATH (''),type, ELEMENTS);
							end;

							set @xml_t = 
								(select
									FAF.GoAML_char_process(cast(s3.zv_zlg_sys_lnr as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transactionnumber'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transactionnumber')) as "transactionnumber",
									FAF.GoAML_char_process(s3.auf_id,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__internal_ref_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__internal_ref_number')) as "internal_ref_number",
									FAF.GoAML_char_process(
										case 
											when s3.sitz_nr_bez is not NULL
											then 
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,97,1)='ok'
													then s3.sitz_nr_bez
													else 'Branch '+ cast(s3.sitz_nr_ausf as varchar) + ' (' + coalesce(s3.sitz_nr_bez,'') + ')'
												end
											else
												case 
													when FAF.GoAML_get_paramS(@F_BANK_ID,1,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code')) then FAF.GoAML_get_paramS(@F_BANK_ID,1,2)
													when FAF.GoAML_get_paramS(@F_BANK_ID,2,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code')) then FAF.GoAML_get_paramS(@F_BANK_ID,2,2)
													else NULL 
												end
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_location'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_location')) as "transaction_location",
									FAF.GoAML_char_process_WR(
										case
											when len(@esr_txt)>0
											then @esr_txt + '; ' + char(13) + char(10) 
											else ''
										end +
										@tr_dscr + ' ' + @c_txt +
										case 
											when len(@trd_txt)>0
											then '; ' + char(13) + char(10) + @trd_txt
											else ''
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_description'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_description')) as "transaction_description",
									FAF.GoAML_char_process(
										case 
											when s3.ausf_dt_ist is not NULL
											then left(convert(varchar,cast(s3.ausf_dt_ist as datetime),126),19)
											when s3.valuta is not NULL
											then left(convert(varchar,cast(s3.valuta as datetime),126),19)
											else left(convert(varchar,cast(convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) as datetime),126),19)
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__date_transaction'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__date_transaction')) as "date_transaction",
									FAF.GoAML_char_process(
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,130,1)='ok'
											then FAF.GoAML_get_paramS(@F_BANK_ID,131,1)
											else
												case 
													when s3.betrag_bwrg<0 
													then FAF.GoAML_get_paramS(@F_BANK_ID,94,2)
													else 
														case
															when @keycd in (10,20)
															then FAF.GoAML_get_paramS(@F_BANK_ID,94,1)
															else NULL
														end
												end
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_type_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_type_code')) as "transaction_type_code",
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,124,1)='ok'
										then NULL
										else FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code'))
									end as "transmode_code",
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,124,2)='ok'
										then NULL
										else 
											FAF.GoAML_char_process(
												case
													when lower(FAF.GoAML_char_process(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end,'withdrawal',0)) not in ('withdrawal','deposit','card_operation','card_purchase','cash_operation')
													then '[' + FAF.GoAML_char_process(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end,NULL,0) + ']: '
													else ''
												end + @tr_dscr
											,case 
												when FAF.GoAML_get_paramS(@F_BANK_ID,3,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code'))
												then FAF.GoAML_get_paramS(@F_BANK_ID,3,2)
												else FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_comment')
											end
											,FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_comment'))
									end as "transmode_comment",
									cast(
										FAF.GoAML_char_process(
											case 
												when FAF.GoAML_get_paramS(@F_BANK_ID,4,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code')) 
												then FAF.GoAML_get_paramS(@F_BANK_ID,4,2)
												else 
													case
														when abs(s3.betrag_bwrg)<0.01 
														then NULL
														else cast(cast(abs(s3.betrag_bwrg) as decimal(15,2)) as varchar)
													end 
											end
										,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__amount_local'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__amount_local'))
									 as decimal(15,2)) as "amount_local",
									FAF.GoAML_char_process(
										case 
											when s3.betrag_bwrg<0 
											then FAF.GoAML_get_paramS(@F_BANK_ID,84,2)
											else FAF.GoAML_get_paramS(@F_BANK_ID,84,1)
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_status'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_status')) as "transaction_status",
									
									--OM_TEXT.TEXT_REC as sender
									(select
										FAF.GoAML_char_process(f_fnd.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'tf__from_funds_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tf__from_funds_code')) as "from_funds_code",
										--FAF.GoAML_char_process(
										--	case 
										--		when FAF.GoAML_get_paramS(@F_BANK_ID,51,1)='ok'
										--		then FAF.GoAML_char_process(f_fnd.comments,NULL,0)
										--		else NULL
										--	end
										--,FAF.GoAML_get_paramV(@F_BANK_ID,'tf__from_funds_comment'),FAF.GoAML_get_paramL(@F_BANK_ID,'tf__from_funds_comment')) as "from_funds_comment",
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,113,1)='ok'
											then FAF.GoAML_char_process(cur.iso_wrg_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_currency_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_currency_code'))
											else NULL
										end as "from_foreign_currency/foreign_currency_code",
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,113,1)='ok'
											then FAF.GoAML_char_process(cast(cast(abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg)) as decimal(15,2)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_amount'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_amount'))
											else NULL
										end as "from_foreign_currency/foreign_amount",
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,113,1)='ok' 
											then
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,99,1)='ok'
													then 
														FAF.GoAML_char_process(cast(cast(abs(
															case
																when abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg))>0
																then s3.betrag_bwrg/coalesce(s3.betrag_zwrg,s3.betrag_bwrg)
																else NULL
															end
														) as decimal(26,13)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_exchange_rate'))
													else FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate')
												end
											else NULL
										end as "from_foreign_currency/foreign_exchange_rate",
										@xml_in
									FOR XML PATH (''),type, ELEMENTS) as "t_from",
									
									--OM_AUF.kt_lnr as recipient
									(select 
										FAF.GoAML_char_process(t_fnd.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'ttmc__to_funds_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'ttmc__to_funds_code')) as "to_funds_code",
										FAF.GoAML_char_process(
											case 
												when FAF.GoAML_get_paramS(@F_BANK_ID,51,2)='ok'
												then FAF.GoAML_char_process(t_fnd.comments,NULL,0)
												else NULL
											end
										,FAF.GoAML_get_paramV(@F_BANK_ID,'ttmc__to_funds_comment'),FAF.GoAML_get_paramL(@F_BANK_ID,'ttmc__to_funds_comment')) as "to_funds_comment",
										FAF.GoAML_char_process(cur.iso_wrg_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_currency_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_currency_code')) as "to_foreign_currency/foreign_currency_code",
										FAF.GoAML_char_process(cast(cast(abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg)) as decimal(15,2)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_amount'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_amount')) as "to_foreign_currency/foreign_amount",
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,99,1)='ok'
											then 
												FAF.GoAML_char_process(cast(cast(abs(
													case
														when abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg))>0
														then s3.betrag_bwrg/coalesce(s3.betrag_zwrg,s3.betrag_bwrg)
														else NULL
													end
												) as decimal(26,13)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_exchange_rate'))
											else FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate')
										end as "to_foreign_currency/foreign_exchange_rate",
										isnull(@xml_ac, @xml_da) as "to_account",
										@cntr as "to_country"
									FOR XML PATH ('t_to_my_client'),type, ELEMENTS),
									FAF.GoAML_char_process_WR(
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
											then
												stuff(
													case
														when FAF.GoAML_char_process(isnull(cat.category_group,tr_tp.category_group),NULL,0) is not NULL
														then char(13) + char(10) + '[' + FAF.GoAML_char_process(isnull(cat.category_group,tr_tp.category_group),'',0) + ']'
														else ''
													end +
													case 
														when s3.betrag_bwrg<0 and FAF.GoAML_get_paramS(@F_BANK_ID,5,1)='ok'
														then char(13) + char(10) + FAF.GoAML_get_paramS(@F_BANK_ID,5,2) 
														else ''
													end
												, 1, LEN(char(13) + char(10)), '')
											else ''
										end +
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,134,1)='ok' and @com1_txt is not NULL
											then @com1_txt
											else ''
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__comments')) as "comments"
								from 
									FAF.FA_MROS_ZV_ZLG_T s3 
								left join
									FAF.FA_MROS_INT_WRG cur
								on s3.iso_wrg_cd=cur.iso_wrg_cd and s3.userbk_nr=cur.userbk_nr
								left join
									FAF.FA_MROS_REFERENCE_TABLE tr_tp
								on s3.userbk_nr=tr_tp.userbk_nr and cast(s3.buchtyp_cd as varchar)=tr_tp.Finnova_value and tr_tp.ref_type=10
								left join
									FAF.FA_MROS_TRX_CATEGORIES_DET cat
								on cat.userbk_nr=s3.userbk_nr and cat.buchtyp_cd=s3.buchtyp_cd and cat.trak_ref_typ=s3.trak_ref_typ and cat.gf_nr=s3.gf_nr
								left join
									FAF.FA_MROS_REFERENCE_TABLE ref_tp
								on s3.userbk_nr=ref_tp.userbk_nr and cast(s3.trak_ref_typ as varchar)=ref_tp.Finnova_value and ref_tp.ref_type=11
								left join
									FAF.FA_MROS_REFERENCE_TABLE f_fnd
								on s3.userbk_nr=f_fnd.userbk_nr and cast(s3.buchtyp_cd as varchar)=f_fnd.Finnova_value and f_fnd.ref_type=20
								left join
									FAF.FA_MROS_REFERENCE_TABLE t_fnd
								on s3.userbk_nr=t_fnd.userbk_nr and cast(s3.buchtyp_cd as varchar)=t_fnd.Finnova_value and t_fnd.ref_type=21
								where s3.userbk_nr=@F_BANK_ID and s3.zv_zlg_sys_lnr=@n_id
								FOR XML PATH ('transaction'),type, ELEMENTS);
						end;

						--2 Internal transaction (the recipient is OM_AUF.kt_lnr)
						if @pmnt_tp = 2
						begin
							select @xml_a2 = xml1, @flag2=flg from @acnts where nID = @ac2_id;

							set @xml_t = 
								(select
									FAF.GoAML_char_process(cast(s3.zv_zlg_sys_lnr as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transactionnumber'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transactionnumber')) as "transactionnumber",
									FAF.GoAML_char_process(s3.auf_id,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__internal_ref_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__internal_ref_number')) as "internal_ref_number",
									FAF.GoAML_char_process(
										case 
											when s3.sitz_nr_bez is not NULL
											then 
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,97,1)='ok'
													then s3.sitz_nr_bez
													else 'Branch '+ cast(s3.sitz_nr_ausf as varchar) + ' (' + coalesce(s3.sitz_nr_bez,'') + ')'
												end
											else
												case 
													when FAF.GoAML_get_paramS(@F_BANK_ID,1,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code')) then FAF.GoAML_get_paramS(@F_BANK_ID,1,2)
													when FAF.GoAML_get_paramS(@F_BANK_ID,2,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code')) then FAF.GoAML_get_paramS(@F_BANK_ID,2,2)
													else NULL 
												end
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_location'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_location')) as "transaction_location",
									FAF.GoAML_char_process_WR(
										@tr_dscr + ' ' + @d_txt +
										case 
											when len(@trd_txt)>0
											then '; ' + char(13) + char(10) + @trd_txt
											else ''
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_description'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_description')) as "transaction_description",
									FAF.GoAML_char_process(
										case 
											when s3.ausf_dt_ist is not NULL
											then left(convert(varchar,cast(s3.ausf_dt_ist as datetime),126),19)
											when s3.valuta is not NULL
											then left(convert(varchar,cast(s3.valuta as datetime),126),19)
											else left(convert(varchar,cast(convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) as datetime),126),19)
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__date_transaction'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__date_transaction')) as "date_transaction",
									FAF.GoAML_char_process(
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,130,1)='ok'
											then FAF.GoAML_get_paramS(@F_BANK_ID,131,1)
											else
												case 
													when s3.betrag_bwrg<0 
													then FAF.GoAML_get_paramS(@F_BANK_ID,94,2)
													else 
														case
															when @keycd in (10,20)
															then FAF.GoAML_get_paramS(@F_BANK_ID,94,1)
															else NULL
														end
												end
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_type_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_type_code')) as "transaction_type_code",
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,124,1)='ok'
										then NULL
										else FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code'))
									end as "transmode_code",
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,124,2)='ok'
										then NULL
										else 
											FAF.GoAML_char_process(
												case
													when lower(FAF.GoAML_char_process(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end,'withdrawal',0)) not in ('withdrawal','deposit','card_operation','card_purchase','cash_operation')
													then '[' + FAF.GoAML_char_process(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end,NULL,0) + ']: '
													else ''
												end + @tr_dscr
											,case 
												when FAF.GoAML_get_paramS(@F_BANK_ID,3,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code'))
												then FAF.GoAML_get_paramS(@F_BANK_ID,3,2)
												else FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_comment')
											end
											,FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_comment'))
									end as "transmode_comment",
									cast(
										FAF.GoAML_char_process(
											case 
												when FAF.GoAML_get_paramS(@F_BANK_ID,4,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code')) 
												then FAF.GoAML_get_paramS(@F_BANK_ID,4,2)
												else 
													case
														when abs(s3.betrag_bwrg)<0.01 
														then NULL
														else cast(cast(abs(s3.betrag_bwrg) as decimal(15,2)) as varchar)
													end 
											end
										,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__amount_local'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__amount_local'))
									 as decimal(15,2)) as "amount_local",
									FAF.GoAML_char_process(
										case 
											when s3.betrag_bwrg<0 
											then FAF.GoAML_get_paramS(@F_BANK_ID,84,2)
											else FAF.GoAML_get_paramS(@F_BANK_ID,84,1)
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_status'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_status')) as "transaction_status",
									
									--ZV_ZLG_T.kt_lnr_b as sender
									(select 
										case
											when @flag2>0
											then
												(select 
													FAF.GoAML_char_process(f_fnd.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'tfmc__from_funds_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfmc__from_funds_code')) as "from_funds_code",
													FAF.GoAML_char_process(
														case 
															when FAF.GoAML_get_paramS(@F_BANK_ID,51,1)='ok'
															then FAF.GoAML_char_process(f_fnd.comments,NULL,0)
															else NULL
														end
													,FAF.GoAML_get_paramV(@F_BANK_ID,'tfmc__from_funds_comment'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfmc__from_funds_comment')) as "from_funds_comment",
													FAF.GoAML_char_process(cur.iso_wrg_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_currency_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_currency_code')) as "from_foreign_currency/foreign_currency_code",
													FAF.GoAML_char_process(cast(cast(abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg)) as decimal(15,2)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_amount'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_amount')) as "from_foreign_currency/foreign_amount",
													case
														when FAF.GoAML_get_paramS(@F_BANK_ID,99,1)='ok'
														then 
															FAF.GoAML_char_process(cast(cast(abs(
																case
																	when abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg))>0
																	then s3.betrag_bwrg/coalesce(s3.betrag_zwrg,s3.betrag_bwrg)
																	else NULL
																end
															) as decimal(26,13)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_exchange_rate'))
														else FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate')
													end as "from_foreign_currency/foreign_exchange_rate",
													isnull(@xml_a2, @xml_da) as "from_account",
													@cntr as "from_country"
												FOR XML PATH ('t_from_my_client'),type, ELEMENTS)
											else
												(select
													FAF.GoAML_char_process(f_fnd.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'tf__from_funds_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tf__from_funds_code')) as "from_funds_code",
													--FAF.GoAML_char_process(
													--	case 
													--		when FAF.GoAML_get_paramS(@F_BANK_ID,51,1)='ok'
													--		then FAF.GoAML_char_process(f_fnd.comments,NULL,0)
													--		else NULL
													--	end
													--,FAF.GoAML_get_paramV(@F_BANK_ID,'tf__from_funds_comment'),FAF.GoAML_get_paramL(@F_BANK_ID,'tf__from_funds_comment')) as "from_funds_comment",
													case
														when FAF.GoAML_get_paramS(@F_BANK_ID,113,1)='ok'
														then FAF.GoAML_char_process(cur.iso_wrg_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_currency_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_currency_code'))
														else NULL
													end as "from_foreign_currency/foreign_currency_code",
													case
														when FAF.GoAML_get_paramS(@F_BANK_ID,113,1)='ok'
														then FAF.GoAML_char_process(cast(cast(abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg)) as decimal(15,2)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_amount'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_amount'))
														else NULL
													end as "from_foreign_currency/foreign_amount",
													case
														when FAF.GoAML_get_paramS(@F_BANK_ID,113,1)='ok'
														then
															case
																when FAF.GoAML_get_paramS(@F_BANK_ID,99,1)='ok'
																then 
																	FAF.GoAML_char_process(cast(cast(abs(
																		case
																			when abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg))>0
																			then s3.betrag_bwrg/coalesce(s3.betrag_zwrg,s3.betrag_bwrg)
																			else NULL
																		end
																	) as decimal(26,13)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_exchange_rate'))
																else FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate')
															end
														else NULL
													end as "from_foreign_currency/foreign_exchange_rate",
													isnull(@xml_a2, @xml_da0) as "from_account",
													@cntr as "from_country"
												FOR XML PATH ('t_from'),type, ELEMENTS)
										end
									FOR XML PATH (''),type, ELEMENTS),
									
									--OM_AUF.kt_lnr as recipient
									(select 
										case
											when @flag1>0
											then
												(select 
													FAF.GoAML_char_process(t_fnd.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'ttmc__to_funds_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'ttmc__to_funds_code')) as "to_funds_code",
													FAF.GoAML_char_process(
														case 
															when FAF.GoAML_get_paramS(@F_BANK_ID,51,2)='ok'
															then FAF.GoAML_char_process(t_fnd.comments,NULL,0)
															else NULL
														end
													,FAF.GoAML_get_paramV(@F_BANK_ID,'ttmc__to_funds_comment'),FAF.GoAML_get_paramL(@F_BANK_ID,'ttmc__to_funds_comment')) as "to_funds_comment",
													FAF.GoAML_char_process(cur.iso_wrg_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_currency_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_currency_code')) as "to_foreign_currency/foreign_currency_code",
													FAF.GoAML_char_process(cast(cast(abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg)) as decimal(15,2)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_amount'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_amount')) as "to_foreign_currency/foreign_amount",
													case
														when FAF.GoAML_get_paramS(@F_BANK_ID,99,1)='ok'
														then 
															FAF.GoAML_char_process(cast(cast(abs(
																case
																	when abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg))>0
																	then s3.betrag_bwrg/coalesce(s3.betrag_zwrg,s3.betrag_bwrg)
																	else NULL
																end
															) as decimal(26,13)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_exchange_rate'))
														else FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate')
													end as "to_foreign_currency/foreign_exchange_rate",
													isnull(@xml_ac, @xml_da) as "to_account",
													@cntr as "to_country"
												FOR XML PATH ('t_to_my_client'),type, ELEMENTS)
											else
												(select
													FAF.GoAML_char_process(t_fnd.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'tt__to_funds_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tt__to_funds_code')) as "to_funds_code",
													--FAF.GoAML_char_process(
													--	case 
													--		when FAF.GoAML_get_paramS(@F_BANK_ID,51,2)='ok'
													--		then FAF.GoAML_char_process(t_fnd.comments,NULL,0)
													--		else NULL
													--	end
													--,FAF.GoAML_get_paramV(@F_BANK_ID,'tt__to_funds_comment'),FAF.GoAML_get_paramL(@F_BANK_ID,'tt__to_funds_comment')) as "to_funds_comment",
													case
														when FAF.GoAML_get_paramS(@F_BANK_ID,113,2)='ok'
														then FAF.GoAML_char_process(cur.iso_wrg_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_currency_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_currency_code'))
														else NULL
													end as "to_foreign_currency/foreign_currency_code",
													case
														when FAF.GoAML_get_paramS(@F_BANK_ID,113,2)='ok'
														then FAF.GoAML_char_process(cast(cast(abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg)) as decimal(15,2)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_amount'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_amount'))
														else NULL
													end as "to_foreign_currency/foreign_amount",
													case
														when FAF.GoAML_get_paramS(@F_BANK_ID,113,2)='ok'
														then 
															case
																when FAF.GoAML_get_paramS(@F_BANK_ID,99,1)='ok'
																then 
																	FAF.GoAML_char_process(cast(cast(abs(
																		case
																			when abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg))>0
																			then s3.betrag_bwrg/coalesce(s3.betrag_zwrg,s3.betrag_bwrg)
																			else NULL
																		end
																	) as decimal(26,13)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_exchange_rate'))
																else FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate')
															end
														else NULL
													end as "to_foreign_currency/foreign_exchange_rate",
													isnull(@xml_ac, @xml_da0) as "to_account",
													@cntr as "to_country"
												FOR XML PATH ('t_to'),type, ELEMENTS)
										end
									FOR XML PATH (''),type, ELEMENTS),
									
									FAF.GoAML_char_process_WR(
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
											then
												stuff(
													case
														when FAF.GoAML_char_process(isnull(cat.category_group,tr_tp.category_group),NULL,0) is not NULL
														then char(13) + char(10) + '[' + FAF.GoAML_char_process(isnull(cat.category_group,tr_tp.category_group),'',0) + ']'
														else ''
													end +
													case 
														when s3.betrag_bwrg<0 and FAF.GoAML_get_paramS(@F_BANK_ID,5,1)='ok'
														then char(13) + char(10) + FAF.GoAML_get_paramS(@F_BANK_ID,5,2) 
														else ''
													end
												, 1, LEN(char(13) + char(10)), '')
											else ''
										end +
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,134,1)='ok' and @com1_txt is not NULL
											then @com1_txt
											else ''
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__comments')) as "comments"
								from 
									FAF.FA_MROS_ZV_ZLG_T s3 
								left join
									FAF.FA_MROS_INT_WRG cur
								on s3.iso_wrg_cd=cur.iso_wrg_cd and s3.userbk_nr=cur.userbk_nr
								left join
									FAF.FA_MROS_REFERENCE_TABLE tr_tp
								on s3.userbk_nr=tr_tp.userbk_nr and cast(s3.buchtyp_cd as varchar)=tr_tp.Finnova_value and tr_tp.ref_type=10
								left join
									FAF.FA_MROS_TRX_CATEGORIES_DET cat
								on cat.userbk_nr=s3.userbk_nr and cat.buchtyp_cd=s3.buchtyp_cd and cat.trak_ref_typ=s3.trak_ref_typ and cat.gf_nr=s3.gf_nr
								left join
									FAF.FA_MROS_REFERENCE_TABLE ref_tp
								on s3.userbk_nr=ref_tp.userbk_nr and cast(s3.trak_ref_typ as varchar)=ref_tp.Finnova_value and ref_tp.ref_type=11
								left join
									FAF.FA_MROS_REFERENCE_TABLE f_fnd
								on s3.userbk_nr=f_fnd.userbk_nr and cast(s3.buchtyp_cd as varchar)=f_fnd.Finnova_value and f_fnd.ref_type=20
								left join
									FAF.FA_MROS_REFERENCE_TABLE t_fnd
								on s3.userbk_nr=t_fnd.userbk_nr and cast(s3.buchtyp_cd as varchar)=t_fnd.Finnova_value and t_fnd.ref_type=21
								where s3.userbk_nr=@F_BANK_ID and s3.zv_zlg_sys_lnr=@n_id
								FOR XML PATH ('transaction'),type, ELEMENTS);
						end;

						--3 Internal transaction (the sender is OM_AUF.kt_lnr)
						if @pmnt_tp = 3
						begin
							select @xml_a2 = xml1, @flag2=flg from @acnts where nID = @ac2_id;

							set @xml_t = 
								(select
									FAF.GoAML_char_process(cast(s3.zv_zlg_sys_lnr as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transactionnumber'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transactionnumber')) as "transactionnumber",
									FAF.GoAML_char_process(s3.auf_id,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__internal_ref_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__internal_ref_number')) as "internal_ref_number",
									FAF.GoAML_char_process(
										case 
											when s3.sitz_nr_bez is not NULL
											then 
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,97,1)='ok'
													then s3.sitz_nr_bez
													else 'Branch '+ cast(s3.sitz_nr_ausf as varchar) + ' (' + coalesce(s3.sitz_nr_bez,'') + ')'
												end
											else
												case 
													when FAF.GoAML_get_paramS(@F_BANK_ID,1,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code')) then FAF.GoAML_get_paramS(@F_BANK_ID,1,2)
													when FAF.GoAML_get_paramS(@F_BANK_ID,2,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code')) then FAF.GoAML_get_paramS(@F_BANK_ID,2,2)
													else NULL 
												end
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_location'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_location')) as "transaction_location",
									FAF.GoAML_char_process_WR(
										@tr_dscr + ' ' + @c_txt +
										case 
											when len(@trd_txt)>0
											then '; ' + char(13) + char(10) + @trd_txt
											else ''
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_description'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_description')) as "transaction_description",
									FAF.GoAML_char_process(
										case 
											when s3.ausf_dt_ist is not NULL
											then left(convert(varchar,cast(s3.ausf_dt_ist as datetime),126),19)
											when s3.valuta is not NULL
											then left(convert(varchar,cast(s3.valuta as datetime),126),19)
											else left(convert(varchar,cast(convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) as datetime),126),19)
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__date_transaction'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__date_transaction')) as "date_transaction",
									FAF.GoAML_char_process(
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,130,1)='ok'
											then FAF.GoAML_get_paramS(@F_BANK_ID,131,1)
											else
												case 
													when s3.betrag_bwrg<0 
													then FAF.GoAML_get_paramS(@F_BANK_ID,94,2)
													else 
														case
															when @keycd in (10,20)
															then FAF.GoAML_get_paramS(@F_BANK_ID,94,1)
															else NULL
														end
												end
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_type_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_type_code')) as "transaction_type_code",
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,124,1)='ok'
										then NULL
										else FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code'))
									end as "transmode_code",
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,124,2)='ok'
										then NULL
										else 
											FAF.GoAML_char_process(
												case
													when lower(FAF.GoAML_char_process(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end,'withdrawal',0)) not in ('withdrawal','deposit','card_operation','card_purchase','cash_operation')
													then '[' + FAF.GoAML_char_process(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end,NULL,0) + ']: '
													else ''
												end + @tr_dscr
											,case 
												when FAF.GoAML_get_paramS(@F_BANK_ID,3,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code'))
												then FAF.GoAML_get_paramS(@F_BANK_ID,3,2)
												else FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_comment')
											end
											,FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_comment'))
									end as "transmode_comment",
									cast(
										FAF.GoAML_char_process(
											case 
												when FAF.GoAML_get_paramS(@F_BANK_ID,4,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code')) 
												then FAF.GoAML_get_paramS(@F_BANK_ID,4,2)
												else 
													case
														when abs(s3.betrag_bwrg)<0.01 
														then NULL
														else cast(cast(abs(s3.betrag_bwrg) as decimal(15,2)) as varchar)
													end 
											end
										,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__amount_local'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__amount_local'))
									 as decimal(15,2)) as "amount_local",
									FAF.GoAML_char_process(
										case 
											when s3.betrag_bwrg<0 
											then FAF.GoAML_get_paramS(@F_BANK_ID,84,2)
											else FAF.GoAML_get_paramS(@F_BANK_ID,84,1)
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_status'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_status')) as "transaction_status",
									
									--OM_AUF.kt_lnr as sender
									(select
										case
											when @flag1>0
											then
												(select
													FAF.GoAML_char_process(f_fnd.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'tfmc__from_funds_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfmc__from_funds_code')) as "from_funds_code",
													FAF.GoAML_char_process(
														case 
															when FAF.GoAML_get_paramS(@F_BANK_ID,51,1)='ok'
															then FAF.GoAML_char_process(f_fnd.comments,NULL,0)
															else NULL
														end
													,FAF.GoAML_get_paramV(@F_BANK_ID,'tfmc__from_funds_comment'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfmc__from_funds_comment')) as "from_funds_comment",
													FAF.GoAML_char_process(cur.iso_wrg_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_currency_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_currency_code')) as "from_foreign_currency/foreign_currency_code",
													FAF.GoAML_char_process(cast(cast(abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg)) as decimal(15,2)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_amount'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_amount')) as "from_foreign_currency/foreign_amount",
													case
														when FAF.GoAML_get_paramS(@F_BANK_ID,99,1)='ok'
														then 
															FAF.GoAML_char_process(cast(cast(abs(
																case
																	when abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg))>0
																	then s3.betrag_bwrg/coalesce(s3.betrag_zwrg,s3.betrag_bwrg)
																	else NULL
																end
															) as decimal(26,13)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_exchange_rate'))
														else FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate')
													end as "from_foreign_currency/foreign_exchange_rate",
													isnull(@xml_ac, @xml_da) as "from_account",
													@cntr as "from_country"
												FOR XML PATH ('t_from_my_client'),type, ELEMENTS)
											else
												(select
													FAF.GoAML_char_process(f_fnd.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'tf__from_funds_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tf__from_funds_code')) as "from_funds_code",
													--FAF.GoAML_char_process(
													--	case 
													--		when FAF.GoAML_get_paramS(@F_BANK_ID,51,1)='ok'
													--		then FAF.GoAML_char_process(f_fnd.comments,NULL,0)
													--		else NULL
													--	end
													--,FAF.GoAML_get_paramV(@F_BANK_ID,'tf__from_funds_comment'),FAF.GoAML_get_paramL(@F_BANK_ID,'tf__from_funds_comment')) as "from_funds_comment",
													case
														when FAF.GoAML_get_paramS(@F_BANK_ID,113,1)='ok'
														then FAF.GoAML_char_process(cur.iso_wrg_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_currency_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_currency_code'))
														else NULL
													end as "from_foreign_currency/foreign_currency_code",
													case
														when FAF.GoAML_get_paramS(@F_BANK_ID,113,1)='ok'
														then FAF.GoAML_char_process(cast(cast(abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg)) as decimal(15,2)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_amount'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_amount'))
														else NULL
													end as "from_foreign_currency/foreign_amount",
													case
														when FAF.GoAML_get_paramS(@F_BANK_ID,113,1)='ok'
														then
															case
																when FAF.GoAML_get_paramS(@F_BANK_ID,99,1)='ok'
																then 
																	FAF.GoAML_char_process(cast(cast(abs(
																		case
																			when abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg))>0
																			then s3.betrag_bwrg/coalesce(s3.betrag_zwrg,s3.betrag_bwrg)
																			else NULL
																		end
																	) as decimal(26,13)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_exchange_rate'))
																else FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate')
															end
														else NULL
													end as "from_foreign_currency/foreign_exchange_rate",
													isnull(@xml_ac, @xml_da0) as "from_account",
													@cntr as "from_country"
												FOR XML PATH ('t_from'),type, ELEMENTS)
										end
									FOR XML PATH (''),type, ELEMENTS),

									--ZV_ZLG_T.kt_lnr_b as recipient
									(select
										case
											when @flag2>0
											then
												(select 
													FAF.GoAML_char_process(t_fnd.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'ttmc__to_funds_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'ttmc__to_funds_code')) as "to_funds_code",
													FAF.GoAML_char_process(
														case 
															when FAF.GoAML_get_paramS(@F_BANK_ID,51,2)='ok'
															then FAF.GoAML_char_process(t_fnd.comments,NULL,0)
															else NULL
														end
													,FAF.GoAML_get_paramV(@F_BANK_ID,'ttmc__to_funds_comment'),FAF.GoAML_get_paramL(@F_BANK_ID,'ttmc__to_funds_comment')) as "to_funds_comment",
													FAF.GoAML_char_process(cur.iso_wrg_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_currency_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_currency_code')) as "to_foreign_currency/foreign_currency_code",
													FAF.GoAML_char_process(cast(cast(abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg)) as decimal(15,2)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_amount'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_amount')) as "to_foreign_currency/foreign_amount",
													case
														when FAF.GoAML_get_paramS(@F_BANK_ID,99,1)='ok'
														then 
															FAF.GoAML_char_process(cast(cast(abs(
																case
																	when abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg))>0
																	then s3.betrag_bwrg/coalesce(s3.betrag_zwrg,s3.betrag_bwrg)
																	else NULL
																end
															) as decimal(26,13)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_exchange_rate'))
														else FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate')
													end as "to_foreign_currency/foreign_exchange_rate",
													isnull(@xml_a2, @xml_da) as "to_account",
													@cntr as "to_country"
												FOR XML PATH ('t_to_my_client'),type, ELEMENTS)
											else
												(select
													FAF.GoAML_char_process(t_fnd.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'tt__to_funds_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tt__to_funds_code')) as "to_funds_code",
													--FAF.GoAML_char_process(
													--	case 
													--		when FAF.GoAML_get_paramS(@F_BANK_ID,51,2)='ok'
													--		then FAF.GoAML_char_process(t_fnd.comments,NULL,0)
													--		else NULL
													--	end
													--,FAF.GoAML_get_paramV(@F_BANK_ID,'tt__to_funds_comment'),FAF.GoAML_get_paramL(@F_BANK_ID,'tt__to_funds_comment')) as "to_funds_comment",
													case
														when FAF.GoAML_get_paramS(@F_BANK_ID,113,2)='ok'
														then FAF.GoAML_char_process(cur.iso_wrg_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_currency_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_currency_code'))
														else NULL
													end as "to_foreign_currency/foreign_currency_code",
													case
														when FAF.GoAML_get_paramS(@F_BANK_ID,113,2)='ok'
														then FAF.GoAML_char_process(cast(cast(abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg)) as decimal(15,2)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_amount'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_amount'))
														else NULL
													end as "to_foreign_currency/foreign_amount",
													case
														when FAF.GoAML_get_paramS(@F_BANK_ID,113,2)='ok'
														then 
															case
																when FAF.GoAML_get_paramS(@F_BANK_ID,99,1)='ok'
																then 
																	FAF.GoAML_char_process(cast(cast(abs(
																		case
																			when abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg))>0
																			then s3.betrag_bwrg/coalesce(s3.betrag_zwrg,s3.betrag_bwrg)
																			else NULL
																		end
																	) as decimal(26,13)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_exchange_rate'))
																else FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate')
															end
														else NULL
													end as "to_foreign_currency/foreign_exchange_rate",
													isnull(@xml_a2, @xml_da0) as "to_account",
													@cntr as "to_country"
												FOR XML PATH ('t_to'),type, ELEMENTS)
										end
									FOR XML PATH (''),type, ELEMENTS),

									FAF.GoAML_char_process_WR(
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
											then
												stuff(
													case
														when FAF.GoAML_char_process(isnull(cat.category_group,tr_tp.category_group),NULL,0) is not NULL
														then char(13) + char(10) + '[' + FAF.GoAML_char_process(isnull(cat.category_group,tr_tp.category_group),'',0) + ']'
														else ''
													end +
													case 
														when s3.betrag_bwrg<0 and FAF.GoAML_get_paramS(@F_BANK_ID,5,1)='ok'
														then char(13) + char(10) + FAF.GoAML_get_paramS(@F_BANK_ID,5,2) 
														else ''
													end
												, 1, LEN(char(13) + char(10)), '')
											else ''
										end +
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,134,1)='ok' and @com1_txt is not NULL
											then @com1_txt
											else ''
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__comments')) as "comments"
								from 
									FAF.FA_MROS_ZV_ZLG_T s3 
								left join
									FAF.FA_MROS_INT_WRG cur
								on s3.iso_wrg_cd=cur.iso_wrg_cd and s3.userbk_nr=cur.userbk_nr
								left join
									FAF.FA_MROS_REFERENCE_TABLE tr_tp
								on s3.userbk_nr=tr_tp.userbk_nr and cast(s3.buchtyp_cd as varchar)=tr_tp.Finnova_value and tr_tp.ref_type=10
								left join
									FAF.FA_MROS_TRX_CATEGORIES_DET cat
								on cat.userbk_nr=s3.userbk_nr and cat.buchtyp_cd=s3.buchtyp_cd and cat.trak_ref_typ=s3.trak_ref_typ and cat.gf_nr=s3.gf_nr
								left join
									FAF.FA_MROS_REFERENCE_TABLE ref_tp
								on s3.userbk_nr=ref_tp.userbk_nr and cast(s3.trak_ref_typ as varchar)=ref_tp.Finnova_value and ref_tp.ref_type=11
								left join
									FAF.FA_MROS_REFERENCE_TABLE f_fnd
								on s3.userbk_nr=f_fnd.userbk_nr and cast(s3.buchtyp_cd as varchar)=f_fnd.Finnova_value and f_fnd.ref_type=20
								left join
									FAF.FA_MROS_REFERENCE_TABLE t_fnd
								on s3.userbk_nr=t_fnd.userbk_nr and cast(s3.buchtyp_cd as varchar)=t_fnd.Finnova_value and t_fnd.ref_type=21
								where s3.userbk_nr=@F_BANK_ID and s3.zv_zlg_sys_lnr=@n_id
								FOR XML PATH ('transaction'),type, ELEMENTS);
						end;

						--4 Outgoing transaction (the sender is OM_AUF.kt_lnr)
						if @pmnt_tp = 4
						begin
							select 
								@xml_out = 
									(select 
										(select
											FAF.GoAML_char_process(
												FAF.GoAML_char_process(
													case
														when len(ss1.bk_name)>1 then ss1.bk_name
														when ss1.EMPF_ART_CD=1 then case when patindex('%' + char(10) + '%',ss1.auf_adr_zeilen)>0 then substring(ss1.auf_adr_zeilen,1,patindex('%' + char(10) + '%',ss1.auf_adr_zeilen)-1) else ss1.auf_adr_zeilen end
														when ss1.EMPF_ART_CD=2 then case when patindex('%' + char(10) + '%',ss1.adr_zeilen)>0 then substring(ss1.adr_zeilen,1,patindex('%' + char(10) + '%',ss1.adr_zeilen)-1) else ss1.adr_zeilen end
														else NULL
													end
												,@df_esr_txt,0)
											,FAF.GoAML_get_paramV(@F_BANK_ID,'ta__institution_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'ta__institution_name'))  as "institution_name",
											FAF.GoAML_char_process(
												FAF.GoAML_char_process(
													case
														when len(ss1.bic_cp)>1 then ss1.bic_cp
														when ss1.EMPF_ART_CD=1 
														then 
															case 
																when len(ss1.auf_bic)>1 then ss1.auf_bic
																when len(ss1.auf_kt_id)>1 then ss1.auf_kt_id
																when len(ss1.bic)>1 then ss1.bic
																else NULL
															end
														when ss1.EMPF_ART_CD=2 
														then 
															case 
																when len(ss1.bic)>1 then ss1.bic
																when len(ss1.kt_id)>1 then ss1.kt_id
																else NULL
															end
														else NULL
													end
												,@df_esr_txt,0)
											,FAF.GoAML_get_paramV(@F_BANK_ID,'ta__swift'),FAF.GoAML_get_paramL(@F_BANK_ID,'ta__swift')) as "swift",
											case
												when FAF.GoAML_get_paramS(@F_BANK_ID,127,1)='ok'
												then NULL
												else
													case
														when FAF.GoAML_get_paramS(@F_BANK_ID,85,2)='ok'
														then FAF.GoAML_char_process(to_lnd.iso_land_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'ta__institution_country'),FAF.GoAML_get_paramL(@F_BANK_ID,'ta__institution_country'))
														else NULL
													end
											end as "institution_country",
											case
												when FAF.GoAML_get_paramS(@F_BANK_ID,127,1)='ok'
												then NULL
												else
													case 
														when FAF.GoAML_get_paramS(@F_BANK_ID,89,2)='ok'
														then 
															FAF.GoAML_char_process(
																case
																	when 
																		len(coalesce(ss1.kt_nr, ss1.iban_id, ss1.qr_iban_id,
																			case
																				when len(ss1.ext_kt_nr)>1 then ss1.ext_kt_nr
																				when @esr is not NULL and ss1.kt_id_cd in (1,3) and len(ss1.kt_id)>1 then ss1.kt_id
																				when len(ss1.auf_ext_kt_nr)>1 then ss1.auf_ext_kt_nr
																				when @esr is not NULL and ss1.auf_kt_id_cd in (1,3) and len(ss1.auf_kt_id)>1 then ss1.auf_kt_id
																				else NULL
																			end
																		,@kt_orig))<15
																	then FAF.GoAML_get_paramS(@F_BANK_ID,91,1)
																	when
																		substring(coalesce(ss1.kt_nr, ss1.iban_id, ss1.qr_iban_id,
																			case
																				when len(ss1.ext_kt_nr)>1 then ss1.ext_kt_nr
																				when @esr is not NULL and ss1.kt_id_cd in (1,3) and len(ss1.kt_id)>1 then ss1.kt_id
																				when len(ss1.auf_ext_kt_nr)>1 then ss1.auf_ext_kt_nr
																				when @esr is not NULL and ss1.auf_kt_id_cd in (1,3) and len(ss1.auf_kt_id)>1 then ss1.auf_kt_id
																				else NULL
																			end
																		,@kt_orig),3,2) not like '[0-9][0-9]'
																	then FAF.GoAML_get_paramS(@F_BANK_ID,91,1)
																	when
																		(select count(*) from FAF.FA_MROS_INT_LAND where userbk_nr=@F_BANK_ID and iso_land_cd=substring(coalesce(ss1.kt_nr, ss1.iban_id, ss1.qr_iban_id,
																			case
																				when len(ss1.ext_kt_nr)>1 then ss1.ext_kt_nr
																				when @esr is not NULL and ss1.kt_id_cd in (1,3) and len(ss1.kt_id)>1 then ss1.kt_id
																				when len(ss1.auf_ext_kt_nr)>1 then ss1.auf_ext_kt_nr
																				when @esr is not NULL and ss1.auf_kt_id_cd in (1,3) and len(ss1.auf_kt_id)>1 then ss1.auf_kt_id
																				else NULL
																			end
																		,@kt_orig),1,2))=0
																	then FAF.GoAML_get_paramS(@F_BANK_ID,91,1)
																	else FAF.GoAML_get_paramS(@F_BANK_ID,90,1)
																end
															,FAF.GoAML_get_paramV(@F_BANK_ID,'ta__account_category'),FAF.GoAML_get_paramL(@F_BANK_ID,'ta__account_category'))
														else NULL
													end
											end as "account_category",
											FAF.GoAML_char_process(
												coalesce(ss1.kt_nr, ss1.iban_id, ss1.qr_iban_id,
													case
														when len(ss1.ext_kt_nr)>1 then ss1.ext_kt_nr
														when @esr is not NULL and ss1.kt_id_cd in (1,3) and len(ss1.kt_id)>1 then ss1.kt_id
														when len(ss1.auf_ext_kt_nr)>1 then ss1.auf_ext_kt_nr
														when @esr is not NULL and ss1.auf_kt_id_cd in (1,3) and len(ss1.auf_kt_id)>1 then ss1.auf_kt_id
														else NULL
													end
												,@kt_orig)
											,FAF.GoAML_get_paramV(@F_BANK_ID,'ta__account'),FAF.GoAML_get_paramL(@F_BANK_ID,'ta__account')) as "account",
											case
												when FAF.GoAML_get_paramS(@F_BANK_ID,127,1)='ok'
												then NULL
												else
													FAF.GoAML_char_process(
														case
															when 
																len(coalesce(ss1.kt_nr, ss1.iban_id, ss1.qr_iban_id,
																	case
																		when len(ss1.ext_kt_nr)>1 then ss1.ext_kt_nr
																		when @esr is not NULL and ss1.kt_id_cd in (1,3) and len(ss1.kt_id)>1 then ss1.kt_id
																		when len(ss1.auf_ext_kt_nr)>1 then ss1.auf_ext_kt_nr
																		when @esr is not NULL and ss1.auf_kt_id_cd in (1,3) and len(ss1.auf_kt_id)>1 then ss1.auf_kt_id
																		else NULL
																	end
																,@kt_orig))<15
															then NULL
															when
																substring(coalesce(ss1.kt_nr, ss1.iban_id, ss1.qr_iban_id,
																	case
																		when len(ss1.ext_kt_nr)>1 then ss1.ext_kt_nr
																		when @esr is not NULL and ss1.kt_id_cd in (1,3) and len(ss1.kt_id)>1 then ss1.kt_id
																		when len(ss1.auf_ext_kt_nr)>1 then ss1.auf_ext_kt_nr
																		when @esr is not NULL and ss1.auf_kt_id_cd in (1,3) and len(ss1.auf_kt_id)>1 then ss1.auf_kt_id
																		else NULL
																	end
																,@kt_orig),3,2) not like '[0-9][0-9]'
															then NULL
															when
																(select count(*) from FAF.FA_MROS_INT_LAND where userbk_nr=@F_BANK_ID and iso_land_cd=substring(coalesce(ss1.kt_nr, ss1.iban_id, ss1.qr_iban_id,
																	case
																		when len(ss1.ext_kt_nr)>1 then ss1.ext_kt_nr
																		when @esr is not NULL and ss1.kt_id_cd in (1,3) and len(ss1.kt_id)>1 then ss1.kt_id
																		when len(ss1.auf_ext_kt_nr)>1 then ss1.auf_ext_kt_nr
																		when @esr is not NULL and ss1.auf_kt_id_cd in (1,3) and len(ss1.auf_kt_id)>1 then ss1.auf_kt_id
																		else NULL
																	end
																,@kt_orig),1,2))=0
															then NULL
															else
																coalesce(ss1.kt_nr, ss1.iban_id, ss1.qr_iban_id,
																	case
																		when len(ss1.ext_kt_nr)>1 then ss1.ext_kt_nr
																		when @esr is not NULL and ss1.kt_id_cd in (1,3) and len(ss1.kt_id)>1 then ss1.kt_id
																		when len(ss1.auf_ext_kt_nr)>1 then ss1.auf_ext_kt_nr
																		when @esr is not NULL and ss1.auf_kt_id_cd in (1,3) and len(ss1.auf_kt_id)>1 then ss1.auf_kt_id
																		else NULL
																	end
																,@kt_orig)
														end
													,FAF.GoAML_get_paramV(@F_BANK_ID,'ta__iban'),FAF.GoAML_get_paramL(@F_BANK_ID,'ta__iban'))
											end as "iban",
											@C_xml,
											case
												when FAF.GoAML_get_paramS(@F_BANK_ID,127,1)='ok' or FAF.GoAML_get_paramS(@F_BANK_ID,136,1)='ok'
												then NULL
												when FAF.GoAML_get_paramS(@F_BANK_ID,106,1)='ok' and @C_xml is NULL
												then 
													FAF.GoAML_char_process(
														case
															when charindex(char(10),coalesce(ss1.counterparty_details,ss1.adr_zeilen))>0
															then substring(coalesce(ss1.counterparty_details,ss1.adr_zeilen),1,charindex(char(10),coalesce(ss1.counterparty_details,ss1.adr_zeilen))-1)
															else coalesce(ss1.counterparty_details,ss1.adr_zeilen)
														end
													,FAF.GoAML_get_paramS(@F_BANK_ID,106,2),FAF.GoAML_get_paramL(@F_BANK_ID,'te__name'))
												else NULL
											end as "t_entity/name",
											case
												when FAF.GoAML_get_paramS(@F_BANK_ID,127,1)='ok'
												then NULL
												when FAF.GoAML_get_paramS(@F_BANK_ID,136,1)='ok' and FAF.GoAML_get_paramS(@F_BANK_ID,106,1)='ok' and @C_xml is NULL
												then 
													(select
														FAF.GoAML_get_paramS(@F_BANK_ID,136,2) as "account_entity_relation",
														FAF.GoAML_char_process(
															case
																when charindex(char(10),coalesce(ss1.counterparty_details,ss1.adr_zeilen))>0
																then substring(coalesce(ss1.counterparty_details,ss1.adr_zeilen),1,charindex(char(10),coalesce(ss1.counterparty_details,ss1.adr_zeilen))-1)
																else coalesce(ss1.counterparty_details,ss1.adr_zeilen)
															end
														,FAF.GoAML_get_paramS(@F_BANK_ID,106,2),FAF.GoAML_get_paramL(@F_BANK_ID,'te__name')) as "entity/name"
													FOR XML PATH ('account_related_entity'),type, ELEMENTS)
												else NULL
											end as "related_entities",
											case 
												when @C_xml is NULL 
												then FAF.GoAML_char_process_WR(coalesce(ss1.counterparty_details,ss1.adr_zeilen),FAF.GoAML_get_paramV(@F_BANK_ID,'ta__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'ta__comments'))
												else NULL 
											end as "comments"
										FOR XML PATH (''),type, ELEMENTS) as "to_account",
										FAF.GoAML_char_process(isnull(to_lnd.iso_land_cd,to_lnd1.iso_land_cd),FAF.GoAML_get_paramV(@F_BANK_ID,'tt__to_country'),FAF.GoAML_get_paramL(@F_BANK_ID,'tt__to_country')) as "to_country"
									FOR XML PATH (''),type, ELEMENTS),
								@esr_txt = 
									case
										when @esr = 'Post'
										then 
											rtrim(left('Post ' +
												FAF.GoAML_char_process(
													coalesce(ss1.kt_nr, ss1.iban_id, ss1.qr_iban_id,
														case
															when len(ss1.ext_kt_nr)>1 then ss1.ext_kt_nr
															when ss1.kt_id_cd in (1,3) and len(ss1.kt_id)>1 then ss1.kt_id
															when len(ss1.auf_ext_kt_nr)>1 then ss1.auf_ext_kt_nr
															when ss1.auf_kt_id_cd in (1,3) and len(ss1.auf_kt_id)>1 then ss1.auf_kt_id
															else NULL
														end
													,@kt_orig)
												,'',0)
											,4000))
										when @esr is not NULL
										then 
											rtrim(left('ESR ' +
												FAF.GoAML_char_process(
													coalesce(ss1.kt_nr, ss1.iban_id, ss1.qr_iban_id,
														case
															when len(ss1.ext_kt_nr)>1 then ss1.ext_kt_nr
															when ss1.kt_id_cd in (1,3) and len(ss1.kt_id)>1 then ss1.kt_id
															when len(ss1.auf_ext_kt_nr)>1 then ss1.auf_ext_kt_nr
															when ss1.auf_kt_id_cd in (1,3) and len(ss1.auf_kt_id)>1 then ss1.auf_kt_id
															else NULL
														end
													,@kt_orig)
												,'',0)
												 + '; Referenz nummer ' + @esr
											,4000))
										else NULL
									end
							from
								(select *,
									upper(substring(
										case
											when len(bic_cp)>1 then bic_cp
											else
												case
													when EMPF_ART_CD=1 
													then 
														case 
															when len(auf_bic)>1 then auf_bic
															when len(auf_kt_id)>1 then auf_kt_id
															when len(bic)>1 then bic
															else NULL
														end
													when EMPF_ART_CD=2 
													then 
														case 
															when len(bic)>1 then bic
															when len(kt_id)>1 then kt_id
															else NULL
														end
													else NULL
												end
										end
									,5,2)) as to_cntr,
									upper(substring(
										FAF.GoAML_char_process(
											coalesce(kt_nr, iban_id, qr_iban_id,
												case
													when len(ext_kt_nr)>1 then ext_kt_nr
													when @esr is not NULL and kt_id_cd in (1,3) and len(kt_id)>1 then kt_id
													when len(auf_ext_kt_nr)>1 then auf_ext_kt_nr
													when @esr is not NULL and auf_kt_id_cd in (1,3) and len(auf_kt_id)>1 then auf_kt_id
													else NULL
												end
											,@kt_orig)
										,NULL,0)
									,1,2)) as to_cntr1
								from FAF.FA_MROS_ZV_ADR_EFF where userbk_nr=@F_BANK_ID and zv_adr_eff_lnr=@adr_nr) ss1 
							left join
								FAF.FA_MROS_INT_LAND to_lnd
							on ss1.to_cntr=to_lnd.iso_land_cd and ss1.userbk_nr=to_lnd.userbk_nr
							left join
								FAF.FA_MROS_INT_LAND to_lnd1
							on ss1.to_cntr1=to_lnd1.iso_land_cd and ss1.userbk_nr=to_lnd1.userbk_nr;

							if @xml_out is NULL
							begin
								set @xml_out = 
									(select 
										FAF.GoAML_get_paramV(@F_BANK_ID,'ta__institution_name')  as "to_account/institution_name",
										FAF.GoAML_get_paramV(@F_BANK_ID,'ta__swift') as "to_account/swift",
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,127,1)='ok'
											then NULL
											else
												case 
													when FAF.GoAML_get_paramS(@F_BANK_ID,89,2)='ok'
													then FAF.GoAML_get_paramV(@F_BANK_ID,'ta__account_category')
													else NULL
												end
										end as "to_account/account_category",
										FAF.GoAML_get_paramV(@F_BANK_ID,'ta__account') as "to_account/account",
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,127,1)='ok' or FAF.GoAML_get_paramS(@F_BANK_ID,136,1)='ok'
											then NULL
											else
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,106,1)='ok'
													then FAF.GoAML_get_paramS(@F_BANK_ID,106,2)
													else NULL
												end
										end as "to_account/t_entity/name",
										FAF.GoAML_get_paramV(@F_BANK_ID,'ta__comments') as "to_account/comments",
										FAF.GoAML_get_paramV(@F_BANK_ID,'tt__to_country') as "to_country"
									FOR XML PATH (''),type, ELEMENTS);
							end;

							set @xml_t = 
								(select
									FAF.GoAML_char_process(cast(s3.zv_zlg_sys_lnr as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transactionnumber'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transactionnumber')) as "transactionnumber",
									FAF.GoAML_char_process(s3.auf_id,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__internal_ref_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__internal_ref_number')) as "internal_ref_number",
									FAF.GoAML_char_process(
										case 
											when s3.sitz_nr_bez is not NULL
											then 
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,97,1)='ok'
													then s3.sitz_nr_bez
													else 'Branch '+ cast(s3.sitz_nr_ausf as varchar) + ' (' + coalesce(s3.sitz_nr_bez,'') + ')'
												end
											else
												case 
													when FAF.GoAML_get_paramS(@F_BANK_ID,1,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code')) then FAF.GoAML_get_paramS(@F_BANK_ID,1,2)
													when FAF.GoAML_get_paramS(@F_BANK_ID,2,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code')) then FAF.GoAML_get_paramS(@F_BANK_ID,2,2)
													else NULL 
												end
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_location'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_location')) as "transaction_location",
									FAF.GoAML_char_process_WR(
										case
											when len(@esr_txt)>0
											then @esr_txt + '; ' + char(13) + char(10) 
											else ''
										end +
										@tr_dscr + ' ' + @d_txt +
										case 
											when len(@trd_txt)>0
											then '; ' + char(13) + char(10) + @trd_txt
											else ''
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_description'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_description')) as "transaction_description",
									FAF.GoAML_char_process(
										case 
											when s3.ausf_dt_ist is not NULL
											then left(convert(varchar,cast(s3.ausf_dt_ist as datetime),126),19)
											when s3.valuta is not NULL
											then left(convert(varchar,cast(s3.valuta as datetime),126),19)
											else left(convert(varchar,cast(convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) as datetime),126),19)
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__date_transaction'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__date_transaction')) as "date_transaction",
									FAF.GoAML_char_process(
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,130,1)='ok'
											then FAF.GoAML_get_paramS(@F_BANK_ID,131,1)
											else
												case 
													when s3.betrag_bwrg<0 
													then FAF.GoAML_get_paramS(@F_BANK_ID,94,2)
													else 
														case
															when @keycd in (10,20)
															then FAF.GoAML_get_paramS(@F_BANK_ID,94,1)
															else NULL
														end
												end
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_type_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_type_code')) as "transaction_type_code",
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,124,1)='ok'
										then NULL
										else FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code'))
									end as "transmode_code",
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,124,2)='ok'
										then NULL
										else 
											FAF.GoAML_char_process(
												case
													when lower(FAF.GoAML_char_process(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end,'withdrawal',0)) not in ('withdrawal','deposit','card_operation','card_purchase','cash_operation')
													then '[' + FAF.GoAML_char_process(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end,NULL,0) + ']: '
													else ''
												end + @tr_dscr
											,case 
												when FAF.GoAML_get_paramS(@F_BANK_ID,3,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code'))
												then FAF.GoAML_get_paramS(@F_BANK_ID,3,2)
												else FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_comment')
											end
											,FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_comment'))
									end as "transmode_comment",
									cast(
										FAF.GoAML_char_process(
											case 
												when FAF.GoAML_get_paramS(@F_BANK_ID,4,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code')) 
												then FAF.GoAML_get_paramS(@F_BANK_ID,4,2)
												else 
													case
														when abs(s3.betrag_bwrg)<0.01 
														then NULL
														else cast(cast(abs(s3.betrag_bwrg) as decimal(15,2)) as varchar)
													end 
											end
										,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__amount_local'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__amount_local'))
									 as decimal(15,2)) as "amount_local",
									FAF.GoAML_char_process(
										case 
											when s3.betrag_bwrg<0 
											then FAF.GoAML_get_paramS(@F_BANK_ID,84,2)
											else FAF.GoAML_get_paramS(@F_BANK_ID,84,1)
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_status'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_status')) as "transaction_status",
									
									--OM_AUF.kt_lnr as sender
									(select
										FAF.GoAML_char_process(f_fnd.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'tfmc__from_funds_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfmc__from_funds_code')) as "from_funds_code",
										FAF.GoAML_char_process(
											case 
												when FAF.GoAML_get_paramS(@F_BANK_ID,51,1)='ok'
												then FAF.GoAML_char_process(f_fnd.comments,NULL,0)
												else NULL
											end
										,FAF.GoAML_get_paramV(@F_BANK_ID,'tfmc__from_funds_comment'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfmc__from_funds_comment')) as "from_funds_comment",
										FAF.GoAML_char_process(cur.iso_wrg_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_currency_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_currency_code')) as "from_foreign_currency/foreign_currency_code",
										FAF.GoAML_char_process(cast(cast(abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg)) as decimal(15,2)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_amount'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_amount')) as "from_foreign_currency/foreign_amount",
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,99,1)='ok'
											then 
												FAF.GoAML_char_process(cast(cast(abs(
													case
														when abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg))>0
														then s3.betrag_bwrg/coalesce(s3.betrag_zwrg,s3.betrag_bwrg)
														else NULL
													end
												) as decimal(26,13)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_exchange_rate'))
											else FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate')
										end as "from_foreign_currency/foreign_exchange_rate",
										isnull(@xml_ac, @xml_da) as "from_account",
										@cntr as "from_country"
									FOR XML PATH ('t_from_my_client'),type, ELEMENTS),
									
									--ZV_ADR_EFF.adr_zeilen as recipient
									(select
										FAF.GoAML_char_process(t_fnd.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'tt__to_funds_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tt__to_funds_code')) as "to_funds_code",
										--FAF.GoAML_char_process(
										--	case 
										--		when FAF.GoAML_get_paramS(@F_BANK_ID,51,2)='ok'
										--		then FAF.GoAML_char_process(t_fnd.comments,NULL,0)
										--		else NULL
										--	end
										--,FAF.GoAML_get_paramV(@F_BANK_ID,'tt__to_funds_comment'),FAF.GoAML_get_paramL(@F_BANK_ID,'tt__to_funds_comment')) as "to_funds_comment",
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,113,2)='ok'
											then FAF.GoAML_char_process(cur.iso_wrg_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_currency_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_currency_code'))
											else NULL
										end as "to_foreign_currency/foreign_currency_code",
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,113,2)='ok'
											then FAF.GoAML_char_process(cast(cast(abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg)) as decimal(15,2)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_amount'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_amount'))
											else NULL
										end as "to_foreign_currency/foreign_amount",
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,113,2)='ok' 
											then 
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,99,1)='ok'
													then 
														FAF.GoAML_char_process(cast(cast(abs(
															case
																when abs(coalesce(s3.betrag_zwrg,s3.betrag_bwrg))>0
																then s3.betrag_bwrg/coalesce(s3.betrag_zwrg,s3.betrag_bwrg)
																else NULL
															end
														) as decimal(26,13)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_exchange_rate'))
													else FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate')
												end
											else NULL
										end as "to_foreign_currency/foreign_exchange_rate",
										@xml_out 
									FOR XML PATH (''),type, ELEMENTS) as "t_to",

									FAF.GoAML_char_process_WR(
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
											then
												stuff(
													case
														when FAF.GoAML_char_process(isnull(cat.category_group,tr_tp.category_group),NULL,0) is not NULL
														then char(13) + char(10) + '[' + FAF.GoAML_char_process(isnull(cat.category_group,tr_tp.category_group),'',0) + ']'
														else ''
													end +
													case 
														when s3.betrag_bwrg<0 and FAF.GoAML_get_paramS(@F_BANK_ID,5,1)='ok'
														then char(13) + char(10) + FAF.GoAML_get_paramS(@F_BANK_ID,5,2) 
														else ''
													end
												, 1, LEN(char(13) + char(10)), '')
											else ''
										end +
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,134,1)='ok' and @com1_txt is not NULL
											then @com1_txt
											else ''
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__comments')) as "comments"
								from 
									FAF.FA_MROS_ZV_ZLG_T s3 
								left join
									FAF.FA_MROS_INT_WRG cur
								on s3.iso_wrg_cd=cur.iso_wrg_cd and s3.userbk_nr=cur.userbk_nr
								left join
									FAF.FA_MROS_REFERENCE_TABLE tr_tp
								on s3.userbk_nr=tr_tp.userbk_nr and cast(s3.buchtyp_cd as varchar)=tr_tp.Finnova_value and tr_tp.ref_type=10
								left join
									FAF.FA_MROS_TRX_CATEGORIES_DET cat
								on cat.userbk_nr=s3.userbk_nr and cat.buchtyp_cd=s3.buchtyp_cd and cat.trak_ref_typ=s3.trak_ref_typ and cat.gf_nr=s3.gf_nr
								left join
									FAF.FA_MROS_REFERENCE_TABLE ref_tp
								on s3.userbk_nr=ref_tp.userbk_nr and cast(s3.trak_ref_typ as varchar)=ref_tp.Finnova_value and ref_tp.ref_type=11
								left join
									FAF.FA_MROS_REFERENCE_TABLE f_fnd
								on s3.userbk_nr=f_fnd.userbk_nr and cast(s3.buchtyp_cd as varchar)=f_fnd.Finnova_value and f_fnd.ref_type=20
								left join
									FAF.FA_MROS_REFERENCE_TABLE t_fnd
								on s3.userbk_nr=t_fnd.userbk_nr and cast(s3.buchtyp_cd as varchar)=t_fnd.Finnova_value and t_fnd.ref_type=21
								where s3.userbk_nr=@F_BANK_ID and s3.zv_zlg_sys_lnr=@n_id
								FOR XML PATH ('transaction'),type, ELEMENTS);
						end;
					end;
				end;

				set @xml_p = (select @xml_p,@xml_t FOR XML PATH (''));
				set @ii = @ii + 1;
			end;
		end;

		--withdrawal/deposits and non-payments
		if @nn_t>0
		begin
			--Bank node for non-payments
			set @xml_bk =
				(select top 1
					FAF.GoAML_char_process(pc.institution_name,'n/a',255) as "name",
					FAF.GoAML_get_paramS(@F_BANK_ID,114,1) as "addresses/address/address_type",
					FAF.GoAML_char_process(pc.[address],FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__address')) as "addresses/address/address",
					FAF.GoAML_char_process(pc.city,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__city'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__city')) as "addresses/address/city",
					FAF.GoAML_char_process(pc.zip,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__zip'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__zip')) as "addresses/address/zip",
					FAF.GoAML_get_paramS(@F_BANK_ID,0,1) as "addresses/address/country_code",
					case 
						when FAF.GoAML_get_paramS(@F_BANK_ID,96,1)='ok'
						then FAF.GoAML_get_paramS(@F_BANK_ID,96,2) 
						else NULL
					end as "incorporation_country_code"
				from FAF.FA_MROS_INT_PERS_CHARGE pc where userbk_nr_home=@F_BANK_ID
				FOR XML PATH (''),type, ELEMENTS);

			set @ii = 1;
			while @ii <= @nn_t
			begin
				set @n_id = NULL;
				set @ac1_id = NULL;
				set @trans_tp = NULL;
				set @key_510 = NULL;
				set @key_520 = NULL;
				set @key_530 = NULL;
				set @key_540 = NULL;
				set @key_550 = NULL;
				set @key_560 = NULL;
				set @key_570 = NULL;
				set @keycd = NULL

				select
					@n_id = tr_id,
					@ac1_id = ac_id,
					@trans_tp = tp,
					@key_510 = key_510,
					@key_520 = key_520,
					@key_530 = key_530,
					@key_540 = key_540,
					@key_550 = key_550,
					@key_560 = key_560,
					@key_570 = key_570
				from @trans_id where id=@ii;

				if @n_id is not NULL and @trans_tp is not NULL
				begin
					set @xml_ac = NULL;
					set @xml_cl1 = NULL;
					set @xml_t = NULL;
					set @cl_id = NULL;

					select 
						@tr_dscr = transaction_description,
						@d_txt = debit_text,
						@c_txt = credit_text,
						@keycd = key_cd
					from
						(select d.*,row_number() over (partition by d.userbk_nr order by d.priority_cd desc) as rn
						from
							(select @F_BANK_ID as userbk_nr,500 as key_cd
							union all
							select @F_BANK_ID as userbk_nr,510*@key_510 as key_cd
							union all
							select @F_BANK_ID as userbk_nr,520*@key_520 as key_cd
							union all
							select @F_BANK_ID as userbk_nr,530*@key_530 as key_cd
							union all
							select @F_BANK_ID as userbk_nr,540*@key_540 as key_cd
							union all
							select @F_BANK_ID as userbk_nr,550*@key_550 as key_cd
							union all
							select @F_BANK_ID as userbk_nr,560*@key_560 as key_cd
							union all
							select @F_BANK_ID as userbk_nr,570*@key_570 as key_cd) t
						join
							FAF.FA_MROS_TRANSACTION_DESCR d
						on d.userbk_nr=t.userbk_nr and d.key_cd=t.key_cd) s
					where rn=1;

					select @xml_ac = xml1, @xml_cl1 = xml2, @cl_id=clid, @kd_tp=cltp from @acnts where nID = @ac1_id;
					
					--withdrawal/deposits
					if @trans_tp >= 100
						begin
							set @xml_t = 
								(select 
									FAF.GoAML_char_process(cast(s3.kt_buch_lnr as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transactionnumber'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transactionnumber')) as "transactionnumber",
									FAF.GoAML_char_process(
										case 
											when s3.sitz_nr_bez is not NULL
											then
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,97,1)='ok'
													then s3.sitz_nr_bez
													else 'Branch '+ cast(s3.sitz_nr as varchar) + ' (' + coalesce(s3.sitz_nr_bez,'') + ')'
												end
											else
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,1,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code')) then FAF.GoAML_get_paramS(@F_BANK_ID,1,2)
													when FAF.GoAML_get_paramS(@F_BANK_ID,2,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code')) then FAF.GoAML_get_paramS(@F_BANK_ID,2,2)
													else NULL 
												end
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_location'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_location')) as "transaction_location",
									FAF.GoAML_char_process_WR(
										case
											when @tr_dscr = '<comments>' 
											then FAF.GoAML_char_process(case when cat.MROS_value is not NULL then cat.comments else tr_tp.comments end,NULL,0)
											when @tr_dscr = '<special_mark>'
											then FAF.GoAML_char_process(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end,NULL,0)
											else @tr_dscr
										end + ' ' + 
										case
											when @trans_tp < 110
											then @d_txt
											else @c_txt
										end +
										case
											when FAF.GoAML_char_process(s3.text_ext,NULL,0) is not NULL 
											then '; ' + char(13) + char(10) + 'Text: ' + FAF.GoAML_char_process_WR(s3.text_ext,'',0)
											else ''
										end +
										case
											when FAF.GoAML_char_process(s3.text_zus,NULL,0) is not NULL 
											then '; ' + char(13) + char(10) + 
												case
													when FAF.GoAML_char_process(s3.text_ext,NULL,0) is not NULL 
													then ''
													else 'Text: '
												end + FAF.GoAML_char_process_WR(s3.text_zus,'',0)
											else ''
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_description'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_description')) as "transaction_description",
									FAF.GoAML_char_process(convert(varchar,s3.buch_dat_a,126),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__date_transaction'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__date_transaction')) as "date_transaction",
									FAF.GoAML_char_process(
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,130,1)='ok'
											then 
												case
													when @keycd = 550
													then FAF.GoAML_get_paramS(@F_BANK_ID,132,2)
													when @keycd in (530,560)
													then FAF.GoAML_get_paramS(@F_BANK_ID,83,2)
													when @keycd = 540
													then FAF.GoAML_get_paramS(@F_BANK_ID,83,1)
													when @keycd in (510,520) and @trans_tp < 110
													then FAF.GoAML_get_paramS(@F_BANK_ID,83,1)
													when @keycd in (510,520) and @trans_tp >= 110
													then FAF.GoAML_get_paramS(@F_BANK_ID,83,2)
													else FAF.GoAML_get_paramS(@F_BANK_ID,131,1)
												end
											else
												case 
													when s3.betrag_bwrg<0 
													then FAF.GoAML_get_paramS(@F_BANK_ID,94,2)
													when @trans_tp < 110
													then FAF.GoAML_get_paramS(@F_BANK_ID,83,1)
													when @trans_tp >= 110
													then FAF.GoAML_get_paramS(@F_BANK_ID,83,2)
													else NULL
												end
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_type_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_type_code')) as "transaction_type_code",
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,124,1)='ok'
										then NULL
										else FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code'))
									end as "transmode_code",
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,124,2)='ok'
										then NULL
										else 
											FAF.GoAML_char_process(
												case
													when FAF.GoAML_char_process(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end,NULL,0) is not NULL
													then '[' + FAF.GoAML_char_process(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end,NULL,0) + ']: '
													else ''
												end + 
												case
													when @tr_dscr = '<comments>' 
													then FAF.GoAML_char_process(case when cat.MROS_value is not NULL then cat.comments else tr_tp.comments end,NULL,0)
													when @tr_dscr = '<special_mark>'
													then FAF.GoAML_char_process(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end,NULL,0)
													else @tr_dscr
												end
											,case 
												when FAF.GoAML_get_paramS(@F_BANK_ID,3,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code'))
												then FAF.GoAML_get_paramS(@F_BANK_ID,3,2)
												else FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_comment')
											end
											,FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_comment'))
									end as "transmode_comment",
									cast(
										FAF.GoAML_char_process(
											case 
												when FAF.GoAML_get_paramS(@F_BANK_ID,4,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code')) then FAF.GoAML_get_paramS(@F_BANK_ID,4,2)
												else 
													case
														when abs(s3.betrag_bwrg)<0.01 
														then NULL
														else cast(cast(abs(s3.betrag_bwrg) as decimal(15,2)) as varchar) 
													end
											end
										,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__amount_local'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__amount_local'))
									 as decimal(15,2)) as "amount_local",
									FAF.GoAML_char_process(
										case 
											when s3.betrag_bwrg<0 
											then FAF.GoAML_get_paramS(@F_BANK_ID,84,2)
											else FAF.GoAML_get_paramS(@F_BANK_ID,84,1)
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_status'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_status')) as "transaction_status",
									case 
										when @trans_tp < 110
										then
											(select
												FAF.GoAML_char_process(f_fnd.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'tfmc__from_funds_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfmc__from_funds_code')) as "from_funds_code",
												FAF.GoAML_char_process(
													case 
														when FAF.GoAML_get_paramS(@F_BANK_ID,51,1)='ok'
														then FAF.GoAML_char_process(f_fnd.comments,NULL,0)
														else NULL
													end
												,FAF.GoAML_get_paramV(@F_BANK_ID,'tfmc__from_funds_comment'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfmc__from_funds_comment')) as "from_funds_comment",
												FAF.GoAML_char_process(cur.iso_wrg_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_currency_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_currency_code')) as "from_foreign_currency/foreign_currency_code",
												FAF.GoAML_char_process(cast(cast(abs(coalesce(s3.betrag_kwrg,s3.betrag_bwrg)) as decimal(15,2)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_amount'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_amount')) as "from_foreign_currency/foreign_amount",
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,99,1)='ok'
													then 
														FAF.GoAML_char_process(cast(cast(abs(
															case
																when abs(coalesce(s3.betrag_kwrg,s3.betrag_bwrg))>0
																then s3.betrag_bwrg/coalesce(s3.betrag_kwrg,s3.betrag_bwrg)
																else NULL
															end
														) as decimal(26,13)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_exchange_rate'))
													else FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate')
												end as "from_foreign_currency/foreign_exchange_rate",
												isnull(@xml_ac,@xml_da) as "from_account",
												@cntr as "from_country"
											FOR XML PATH (''),type, ELEMENTS)
										when @trans_tp = 110
										then
											(select
												FAF.GoAML_char_process(
													isnull(f_fnd.MROS_value,case when FAF.GoAML_get_paramS(@F_BANK_ID,129,1)='ok' then FAF.GoAML_get_paramS(@F_BANK_ID,129,2) else NULL end)
												,FAF.GoAML_get_paramV(@F_BANK_ID,'tfmc__from_funds_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfmc__from_funds_code')) as "from_funds_code",
												FAF.GoAML_char_process(
													case 
														when FAF.GoAML_get_paramS(@F_BANK_ID,51,1)='ok'
														then FAF.GoAML_char_process(f_fnd.comments,NULL,0)
														else NULL
													end
												,FAF.GoAML_get_paramV(@F_BANK_ID,'tfmc__from_funds_comment'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfmc__from_funds_comment')) as "from_funds_comment",
												FAF.GoAML_char_process(cur.iso_wrg_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_currency_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_currency_code')) as "from_foreign_currency/foreign_currency_code",
												FAF.GoAML_char_process(cast(cast(abs(coalesce(s3.betrag_kwrg,s3.betrag_bwrg)) as decimal(15,2)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_amount'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_amount')) as "from_foreign_currency/foreign_amount",
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,99,1)='ok'
													then 
														FAF.GoAML_char_process(cast(cast(abs(
															case
																when abs(coalesce(s3.betrag_kwrg,s3.betrag_bwrg))>0
																then s3.betrag_bwrg/coalesce(s3.betrag_kwrg,s3.betrag_bwrg)
																else NULL
															end
														) as decimal(26,13)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_exchange_rate'))
													else FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate')
												end as "from_foreign_currency/foreign_exchange_rate",
												--account owner
												case
													when @xml_cl1 is not NULL
													then
														(select
															case when @kd_tp>1 then @xml_cl1 else NULL end as "from_entity",
															case when @kd_tp=1 then @xml_cl1 else NULL end as "from_person"
														FOR XML PATH (''),type, ELEMENTS)
													else (select @xml_dp as "from_person" FOR XML PATH (''),type, ELEMENTS)
												end,
												@cntr as "from_country"
											FOR XML PATH (''),type, ELEMENTS)
										else NULL
									end as "t_from_my_client",
									case 
										when @trans_tp = 111
										then 
											(select
												FAF.GoAML_char_process(
													isnull(f_fnd.MROS_value,case when FAF.GoAML_get_paramS(@F_BANK_ID,129,1)='ok' then FAF.GoAML_get_paramS(@F_BANK_ID,129,2) else NULL end)
												,FAF.GoAML_get_paramV(@F_BANK_ID,'tf__from_funds_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tf__from_funds_code')) as "from_funds_code",
												--FAF.GoAML_char_process(
												--	case 
												--		when FAF.GoAML_get_paramS(@F_BANK_ID,51,1)='ok'
												--		then FAF.GoAML_char_process(f_fnd.comments,NULL,0)
												--		else NULL
												--	end
												--,FAF.GoAML_get_paramV(@F_BANK_ID,'tf__from_funds_comment'),FAF.GoAML_get_paramL(@F_BANK_ID,'tf__from_funds_comment')) as "from_funds_comment",
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,113,1)='ok'
													then FAF.GoAML_char_process(cur.iso_wrg_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_currency_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_currency_code'))
													else NULL
												end as "from_foreign_currency/foreign_currency_code",
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,113,1)='ok'
													then FAF.GoAML_char_process(cast(cast(abs(coalesce(s3.betrag_kwrg,s3.betrag_bwrg)) as decimal(15,2)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_amount'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_amount'))
													else NULL
												end as "from_foreign_currency/foreign_amount",
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,113,1)='ok'
													then
														case
															when FAF.GoAML_get_paramS(@F_BANK_ID,99,1)='ok'
															then 
																FAF.GoAML_char_process(cast(cast(abs(
																	case
																		when abs(coalesce(s3.betrag_kwrg,s3.betrag_bwrg))>0
																		then s3.betrag_bwrg/coalesce(s3.betrag_kwrg,s3.betrag_bwrg)
																		else NULL
																	end
																) as decimal(26,13)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_exchange_rate'))
															else FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate')
														end
													else NULL
												end as "from_foreign_currency/foreign_exchange_rate",
												FAF.GoAML_char_process(
													case
														when patindex('%[a-Z][^a-Z]%',FAF.GoAML_char_process(s3.adr_zeile_1_ez,NULL,0))>0
														then left(FAF.GoAML_char_process(s3.adr_zeile_1_ez,NULL,0),patindex('%[a-Z][^a-Z]%',FAF.GoAML_char_process(s3.adr_zeile_1_ez,NULL,0)))
														else FAF.GoAML_get_paramV(@F_BANK_ID,'tp__first_name')
													end
												,FAF.GoAML_get_paramV(@F_BANK_ID,'tp__first_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'tp__first_name')) as  "from_person/first_name",
												case 
													when FAF.GoAML_get_paramS(@F_BANK_ID,44,1)='ok'
													then upper(
														FAF.GoAML_char_process(
															case
																when patindex('%[a-Z][^a-Z]%',FAF.GoAML_char_process(s3.adr_zeile_1_ez,NULL,0))>0
																then right(FAF.GoAML_char_process(s3.adr_zeile_1_ez,NULL,0),len(FAF.GoAML_char_process(s3.adr_zeile_1_ez,NULL,0))-patindex('%[a-Z][^a-Z]%',FAF.GoAML_char_process(s3.adr_zeile_1_ez,NULL,0)))
																else s3.adr_zeile_1_ez
															end 
														,FAF.GoAML_get_paramV(@F_BANK_ID,'tp__last_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'tp__last_name')))
													else 
														FAF.GoAML_char_process(
															case
																when patindex('%[a-Z][^a-Z]%',FAF.GoAML_char_process(s3.adr_zeile_1_ez,NULL,0))>0
																then right(FAF.GoAML_char_process(s3.adr_zeile_1_ez,NULL,0),len(FAF.GoAML_char_process(s3.adr_zeile_1_ez,NULL,0))-patindex('%[a-Z][^a-Z]%',FAF.GoAML_char_process(s3.adr_zeile_1_ez,NULL,0)))
																else s3.adr_zeile_1_ez
															end 
														,FAF.GoAML_get_paramV(@F_BANK_ID,'tp__last_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'tp__last_name'))
												end as "from_person/last_name",
												case 
													when FAF.GoAML_get_paramS(@F_BANK_ID,42,2)='ok'
													then FAF.GoAML_get_paramV(@F_BANK_ID,'tp__id_number')
													else NULL
												end as "from_person/id_number",
												FAF.GoAML_char_process(
													FAF.GoAML_char_process(s3.adr_zeile_1_ez,'',0) +
													case 
														when FAF.GoAML_char_process(s3.adr_zeile_2_ez,NULL,0) is not NULL
														then '; ' + FAF.GoAML_char_process(s3.adr_zeile_2_ez,'',0)
														else ''
													end +
													case 
														when FAF.GoAML_char_process(s3.adr_zeile_3_ez,NULL,0) is not NULL
														then '; ' + FAF.GoAML_char_process(s3.adr_zeile_3_ez,'',0)
														else ''
													end +
													case 
														when FAF.GoAML_char_process(s3.adr_zeile_4_ez,NULL,0) is not NULL
														then '; ' + FAF.GoAML_char_process(s3.adr_zeile_4_ez,'',0)
														else ''
													end +
													case 
														when FAF.GoAML_char_process(s3.adr_zeile_5_ez,NULL,0) is not NULL
														then '; ' + FAF.GoAML_char_process(s3.adr_zeile_5_ez,'',0)
														else ''
													end
												,FAF.GoAML_get_paramV(@F_BANK_ID,'tp__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'tp__comments')) as "from_person/comments",
												@cntr as "from_country"
											FOR XML PATH (''),type, ELEMENTS)
										else NULL
									end as "t_from",
									case 
										when @trans_tp < 110
										then 
											(select
												FAF.GoAML_char_process(
													isnull(t_fnd.MROS_value,case when FAF.GoAML_get_paramS(@F_BANK_ID,129,1)='ok' then FAF.GoAML_get_paramS(@F_BANK_ID,129,2) else NULL end)
												,FAF.GoAML_get_paramV(@F_BANK_ID,'ttmc__to_funds_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'ttmc__to_funds_code')) as "to_funds_code",
												FAF.GoAML_char_process(
													case 
														when FAF.GoAML_get_paramS(@F_BANK_ID,51,2)='ok'
														then FAF.GoAML_char_process(t_fnd.comments,NULL,0)
														else NULL
													end
												,FAF.GoAML_get_paramV(@F_BANK_ID,'ttmc__to_funds_comment'),FAF.GoAML_get_paramL(@F_BANK_ID,'ttmc__to_funds_comment')) as "to_funds_comment",
												FAF.GoAML_char_process(cur.iso_wrg_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_currency_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_currency_code')) as "to_foreign_currency/foreign_currency_code",
												FAF.GoAML_char_process(cast(cast(abs(coalesce(s3.betrag_kwrg,s3.betrag_bwrg)) as decimal(15,2)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_amount'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_amount')) as "to_foreign_currency/foreign_amount",
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,99,1)='ok'
													then 
														FAF.GoAML_char_process(cast(cast(abs(
															case
																when abs(coalesce(s3.betrag_kwrg,s3.betrag_bwrg))>0
																then s3.betrag_bwrg/coalesce(s3.betrag_kwrg,s3.betrag_bwrg)
																else NULL
															end
														) as decimal(26,13)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_exchange_rate'))
													else FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate')
												end as "to_foreign_currency/foreign_exchange_rate",
												--account owner
												case
													when @xml_cl1 is not NULL
													then
														(select
															case when @kd_tp>1 then @xml_cl1 else NULL end as "to_entity",
															case when @kd_tp=1 then @xml_cl1 else NULL end as "to_person"
														FOR XML PATH (''),type, ELEMENTS)
													else (select @xml_dp as "to_person" FOR XML PATH (''),type, ELEMENTS)
												end,
												@cntr as "to_country"
											FOR XML PATH (''),type, ELEMENTS)
										when @trans_tp >= 110
										then 
											(select
												FAF.GoAML_char_process(t_fnd.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'ttmc__to_funds_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'ttmc__to_funds_code')) as "to_funds_code",
												FAF.GoAML_char_process(
													case 
														when FAF.GoAML_get_paramS(@F_BANK_ID,51,2)='ok'
														then FAF.GoAML_char_process(t_fnd.comments,NULL,0)
														else NULL
													end
												,FAF.GoAML_get_paramV(@F_BANK_ID,'ttmc__to_funds_comment'),FAF.GoAML_get_paramL(@F_BANK_ID,'ttmc__to_funds_comment')) as "to_funds_comment",
												FAF.GoAML_char_process(cur.iso_wrg_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_currency_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_currency_code')) as "to_foreign_currency/foreign_currency_code",
												FAF.GoAML_char_process(cast(cast(abs(coalesce(s3.betrag_kwrg,s3.betrag_bwrg)) as decimal(15,2)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_amount'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_amount')) as "to_foreign_currency/foreign_amount",
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,99,1)='ok'
													then 
														FAF.GoAML_char_process(cast(cast(abs(
															case
																when abs(coalesce(s3.betrag_kwrg,s3.betrag_bwrg))>0
																then s3.betrag_bwrg/coalesce(s3.betrag_kwrg,s3.betrag_bwrg)
																else NULL
															end
														) as decimal(26,13)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_exchange_rate'))
													else FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate')
												end as "to_foreign_currency/foreign_exchange_rate",
												isnull(@xml_ac, @xml_da) as "to_account",
												@cntr as "to_country"
											FOR XML PATH (''),type, ELEMENTS)
										else NULL
									end as "t_to_my_client",
									FAF.GoAML_char_process_WR(
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
											then
												stuff(
													case
														when FAF.GoAML_char_process(isnull(cat.category_group,tr_tp.category_group),NULL,0) is not NULL
														then char(13) + char(10) + '[' + FAF.GoAML_char_process(isnull(cat.category_group,tr_tp.category_group),'',0) + ']'
														else ''
													end +
													case 
														when s3.betrag_bwrg<0 and FAF.GoAML_get_paramS(@F_BANK_ID,5,1)='ok'
														then char(13) + char(10) + FAF.GoAML_get_paramS(@F_BANK_ID,5,2) 
														else ''
													end
												, 1, LEN(char(13) + char(10)), '')
											else ''
										end +
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,134,1)='ok' and @com1_txt is not NULL
											then @com1_txt
											else ''
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__comments')) as "comments"
								from 
									FAF.FA_MROS_KT_BUCH_EA s3 
								left join
									FAF.FA_MROS_REFERENCE_TABLE tr_tp
								on s3.userbk_nr=tr_tp.userbk_nr and cast(s3.buchtyp_cd as varchar)=tr_tp.Finnova_value and tr_tp.ref_type=10
								left join
									FAF.FA_MROS_TRX_CATEGORIES_DET cat
								on cat.userbk_nr=s3.userbk_nr and cat.buchtyp_cd=s3.buchtyp_cd and cat.trak_ref_typ=s3.trak_ref_typ and cat.gf_nr is NULL
								left join
									FAF.FA_MROS_REFERENCE_TABLE ref_tp
								on s3.userbk_nr=ref_tp.userbk_nr and cast(s3.trak_ref_typ as varchar)=ref_tp.Finnova_value and ref_tp.ref_type=11
								left join
									FAF.FA_MROS_REFERENCE_TABLE f_fnd
								on s3.userbk_nr=f_fnd.userbk_nr and cast(s3.buchtyp_cd as varchar)=f_fnd.Finnova_value and f_fnd.ref_type=20
								left join
									FAF.FA_MROS_REFERENCE_TABLE t_fnd
								on s3.userbk_nr=t_fnd.userbk_nr and cast(s3.buchtyp_cd as varchar)=t_fnd.Finnova_value and t_fnd.ref_type=21
								left join
									FAF.FA_MROS_INT_WRG cur
								on s3.iso_wrg_cd=cur.iso_wrg_cd and s3.userbk_nr=cur.userbk_nr
								where s3.userbk_nr=@F_BANK_ID and s3.kt_buch_lnr = @n_id
								FOR XML PATH ('transaction'),type, ELEMENTS);
						end;
					else
						begin
							--non payments
							set @xml_t = 
								(select 
									FAF.GoAML_char_process(cast(s3.kt_buch_lnr as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transactionnumber'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transactionnumber')) as "transactionnumber",
									FAF.GoAML_char_process(
										case 
											when s3.sitz_nr_bez is not NULL
											then
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,97,1)='ok'
													then s3.sitz_nr_bez
													else 'Branch '+ cast(s3.sitz_nr as varchar) + ' (' + coalesce(s3.sitz_nr_bez,'') + ')'
												end
											else
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,1,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code')) then FAF.GoAML_get_paramS(@F_BANK_ID,1,2)
													when FAF.GoAML_get_paramS(@F_BANK_ID,2,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code')) then FAF.GoAML_get_paramS(@F_BANK_ID,2,2)
													else NULL 
												end
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_location'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_location')) as "transaction_location",
									FAF.GoAML_char_process_WR(
										case
											when @tr_dscr = '<comments>' 
											then FAF.GoAML_char_process(case when cat.MROS_value is not NULL then cat.comments else tr_tp.comments end,NULL,0)
											when @tr_dscr = '<special_mark>'
											then FAF.GoAML_char_process(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end,NULL,0)
											else @tr_dscr
										end + ' ' + 
										case
											when @trans_tp < 10
											then @d_txt
											else @c_txt
										end +
										case
											when FAF.GoAML_char_process(s3.text_ext,NULL,0) is not NULL 
											then '; ' + char(13) + char(10) + 'Text: ' + FAF.GoAML_char_process_WR(s3.text_ext,'',0)
											else ''
										end +
										case
											when FAF.GoAML_char_process(s3.text_zus,NULL,0) is not NULL 
											then '; ' + char(13) + char(10) + 
												case
													when FAF.GoAML_char_process(s3.text_ext,NULL,0) is not NULL 
													then ''
													else 'Text: '
												end + FAF.GoAML_char_process_WR(s3.text_zus,'',0)
											else ''
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_description'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_description')) as "transaction_description",
									FAF.GoAML_char_process(convert(varchar,s3.buch_dat_a,126),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__date_transaction'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__date_transaction')) as "date_transaction",
									FAF.GoAML_char_process(
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,130,1)='ok'
											then FAF.GoAML_get_paramS(@F_BANK_ID,131,1)
											else
												case 
													when s3.betrag_bwrg<0 
													then FAF.GoAML_get_paramS(@F_BANK_ID,94,2)
													else NULL
												end
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_type_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_type_code')) as "transaction_type_code",
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,124,1)='ok'
										then NULL
										else FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code'))
									end as "transmode_code",
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,124,2)='ok'
										then NULL
										else 
											FAF.GoAML_char_process(
												case
													when FAF.GoAML_char_process(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end,NULL,0) is not NULL
													then '[' + FAF.GoAML_char_process(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end,NULL,0) + ']: '
													else ''
												end + 
												case
													when @tr_dscr = '<comments>' 
													then FAF.GoAML_char_process(case when cat.MROS_value is not NULL then cat.comments else tr_tp.comments end,NULL,0)
													when @tr_dscr = '<special_mark>'
													then FAF.GoAML_char_process(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end,NULL,0)
													else @tr_dscr
												end
											,case 
												when FAF.GoAML_get_paramS(@F_BANK_ID,3,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code'))
												then FAF.GoAML_get_paramS(@F_BANK_ID,3,2)
												else FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_comment')
											end
											,FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_comment'))
									end as "transmode_comment",
									cast(
										FAF.GoAML_char_process(
											case 
												when FAF.GoAML_get_paramS(@F_BANK_ID,4,1)=FAF.GoAML_char_process(isnull(cat.MROS_value,tr_tp.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transmode_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transmode_code')) then FAF.GoAML_get_paramS(@F_BANK_ID,4,2)
												else 
													case
														when abs(s3.betrag_bwrg)<0.01 
														then NULL
														else cast(cast(abs(s3.betrag_bwrg) as decimal(15,2)) as varchar) 
													end
											end
										,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__amount_local'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__amount_local'))
									 as decimal(15,2)) as "amount_local",
									FAF.GoAML_char_process(
										case 
											when s3.betrag_bwrg<0 
											then FAF.GoAML_get_paramS(@F_BANK_ID,84,2)
											else FAF.GoAML_get_paramS(@F_BANK_ID,84,1)
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__transaction_status'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__transaction_status')) as "transaction_status",
									case 
										when s3.sh_cd='D'
										then 
											(select
												FAF.GoAML_char_process(f_fnd.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'tfmc__from_funds_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfmc__from_funds_code')) as "from_funds_code",
												FAF.GoAML_char_process(
													case 
														when FAF.GoAML_get_paramS(@F_BANK_ID,51,1)='ok'
														then FAF.GoAML_char_process(f_fnd.comments,NULL,0)
														else NULL
													end
												,FAF.GoAML_get_paramV(@F_BANK_ID,'tfmc__from_funds_comment'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfmc__from_funds_comment')) as "from_funds_comment",
												FAF.GoAML_char_process(cur.iso_wrg_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_currency_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_currency_code')) as "from_foreign_currency/foreign_currency_code",
												FAF.GoAML_char_process(cast(cast(abs(coalesce(s3.betrag_kwrg,s3.betrag_bwrg)) as decimal(15,2)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_amount'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_amount')) as "from_foreign_currency/foreign_amount",
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,99,1)='ok'
													then 
														FAF.GoAML_char_process(cast(cast(abs(
															case
																when abs(coalesce(s3.betrag_kwrg,s3.betrag_bwrg))>0
																then s3.betrag_bwrg/coalesce(s3.betrag_kwrg,s3.betrag_bwrg)
																else NULL
															end
														) as decimal(26,13)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_exchange_rate'))
													else FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate')
												end as "from_foreign_currency/foreign_exchange_rate",
												isnull(@xml_ac, @xml_da) as "from_account",
												@cntr as "from_country"
											FOR XML PATH (''),type, ELEMENTS)
										else NULL
									end as "t_from_my_client",
									case 
										when s3.sh_cd='C'
										then 
											(select
												FAF.GoAML_char_process(f_fnd.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'tf__from_funds_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tf__from_funds_code')) as "from_funds_code",
												--FAF.GoAML_char_process(
												--	case 
												--		when FAF.GoAML_get_paramS(@F_BANK_ID,51,1)='ok'
												--		then FAF.GoAML_char_process(f_fnd.comments,NULL,0)
												--		else NULL
												--	end
												--,FAF.GoAML_get_paramV(@F_BANK_ID,'tf__from_funds_comment'),FAF.GoAML_get_paramL(@F_BANK_ID,'tf__from_funds_comment')) as "from_funds_comment",
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,113,1)='ok'
													then FAF.GoAML_char_process(cur.iso_wrg_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_currency_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_currency_code'))
													else NULL
												end as "from_foreign_currency/foreign_currency_code",
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,113,1)='ok'
													then FAF.GoAML_char_process(cast(cast(abs(coalesce(s3.betrag_kwrg,s3.betrag_bwrg)) as decimal(15,2)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_amount'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_amount'))
													else NULL
												end as "from_foreign_currency/foreign_amount",
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,113,1)='ok'
													then
														case
															when FAF.GoAML_get_paramS(@F_BANK_ID,99,1)='ok'
															then 
																FAF.GoAML_char_process(cast(cast(abs(
																	case
																		when abs(coalesce(s3.betrag_kwrg,s3.betrag_bwrg))>0
																		then s3.betrag_bwrg/coalesce(s3.betrag_kwrg,s3.betrag_bwrg)
																		else NULL
																	end
																) as decimal(26,13)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_exchange_rate'))
															else FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate')
														end
													else NULL
												end as "from_foreign_currency/foreign_exchange_rate",
												@xml_bk as "from_entity",
												@cntr as "from_country"
											FOR XML PATH (''),type, ELEMENTS)
										else NULL
									end as "t_from",
									case 
										when s3.sh_cd='D'
										then 
											(select
												FAF.GoAML_char_process(t_fnd.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'tt__to_funds_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tt__to_funds_code')) as "to_funds_code",
												--FAF.GoAML_char_process(
												--	case 
												--		when FAF.GoAML_get_paramS(@F_BANK_ID,51,2)='ok'
												--		then FAF.GoAML_char_process(t_fnd.comments,NULL,0)
												--		else NULL
												--	end
												--,FAF.GoAML_get_paramV(@F_BANK_ID,'tt__to_funds_comment'),FAF.GoAML_get_paramL(@F_BANK_ID,'tt__to_funds_comment')) as "to_funds_comment",
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,113,2)='ok'
													then FAF.GoAML_char_process(cur.iso_wrg_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_currency_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_currency_code'))
													else NULL
												end as "to_foreign_currency/foreign_currency_code",
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,113,2)='ok'
													then FAF.GoAML_char_process(cast(cast(abs(coalesce(s3.betrag_kwrg,s3.betrag_bwrg)) as decimal(15,2)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_amount'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_amount'))
													else NULL
												end as "to_foreign_currency/foreign_amount",
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,113,2)='ok'
													then 
														case
															when FAF.GoAML_get_paramS(@F_BANK_ID,99,1)='ok'
															then 
																FAF.GoAML_char_process(cast(cast(abs(
																	case
																		when abs(coalesce(s3.betrag_kwrg,s3.betrag_bwrg))>0
																		then s3.betrag_bwrg/coalesce(s3.betrag_kwrg,s3.betrag_bwrg)
																		else NULL
																	end
																) as decimal(26,13)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_exchange_rate'))
															else FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate')
														end
													else NULL
												end as "to_foreign_currency/foreign_exchange_rate",
												@xml_bk as "to_entity",
												@cntr as "to_country"
											FOR XML PATH (''),type, ELEMENTS)
										else NULL
									end as "t_to",
									case 
										when s3.sh_cd='C'
										then 
											(select
												FAF.GoAML_char_process(t_fnd.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'ttmc__to_funds_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'ttmc__to_funds_code')) as "to_funds_code",
												FAF.GoAML_char_process(
													case 
														when FAF.GoAML_get_paramS(@F_BANK_ID,51,2)='ok'
														then FAF.GoAML_char_process(t_fnd.comments,NULL,0)
														else NULL
													end
												,FAF.GoAML_get_paramV(@F_BANK_ID,'ttmc__to_funds_comment'),FAF.GoAML_get_paramL(@F_BANK_ID,'ttmc__to_funds_comment')) as "to_funds_comment",
												FAF.GoAML_char_process(cur.iso_wrg_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_currency_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_currency_code')) as "to_foreign_currency/foreign_currency_code",
												FAF.GoAML_char_process(cast(cast(abs(coalesce(s3.betrag_kwrg,s3.betrag_bwrg)) as decimal(15,2)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_amount'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_amount')) as "to_foreign_currency/foreign_amount",
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,99,1)='ok'
													then 
														FAF.GoAML_char_process(cast(cast(abs(
															case
																when abs(coalesce(s3.betrag_kwrg,s3.betrag_bwrg))>0
																then s3.betrag_bwrg/coalesce(s3.betrag_kwrg,s3.betrag_bwrg)
																else NULL
															end
														) as decimal(26,13)) as varchar),FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate'),FAF.GoAML_get_paramL(@F_BANK_ID,'tfc__foreign_exchange_rate'))
													else FAF.GoAML_get_paramV(@F_BANK_ID,'tfc__foreign_exchange_rate')
												end as "to_foreign_currency/foreign_exchange_rate",
												isnull(@xml_ac, @xml_da) as "to_account",
												@cntr as "to_country"
											FOR XML PATH (''),type, ELEMENTS)
										else NULL
									end as "t_to_my_client",
									FAF.GoAML_char_process_WR(
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
											then
												stuff(
													case
														when FAF.GoAML_char_process(isnull(cat.category_group,tr_tp.category_group),NULL,0) is not NULL
														then char(13) + char(10) + '[' + FAF.GoAML_char_process(isnull(cat.category_group,tr_tp.category_group),'',0) + ']'
														else ''
													end +
													case 
														when s3.betrag_bwrg<0 and FAF.GoAML_get_paramS(@F_BANK_ID,5,1)='ok'
														then char(13) + char(10) + FAF.GoAML_get_paramS(@F_BANK_ID,5,2) 
														else ''
													end
												, 1, LEN(char(13) + char(10)), '')
											else ''
										end +
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,134,1)='ok' and @com1_txt is not NULL
											then @com1_txt
											else ''
										end
									,FAF.GoAML_get_paramV(@F_BANK_ID,'tr__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'tr__comments')) as "comments"
								from 
									FAF.FA_MROS_KT_BUCH_EA s3 
								left join
									FAF.FA_MROS_REFERENCE_TABLE tr_tp
								on s3.userbk_nr=tr_tp.userbk_nr and cast(s3.buchtyp_cd as varchar)=tr_tp.Finnova_value and tr_tp.ref_type=10
								left join
									FAF.FA_MROS_TRX_CATEGORIES_DET cat
								on cat.userbk_nr=s3.userbk_nr and cat.buchtyp_cd=s3.buchtyp_cd and cat.trak_ref_typ=s3.trak_ref_typ and cat.gf_nr is NULL
								left join
									FAF.FA_MROS_REFERENCE_TABLE ref_tp
								on s3.userbk_nr=ref_tp.userbk_nr and cast(s3.trak_ref_typ as varchar)=ref_tp.Finnova_value and ref_tp.ref_type=11
								left join
									FAF.FA_MROS_REFERENCE_TABLE f_fnd
								on s3.userbk_nr=f_fnd.userbk_nr and cast(s3.buchtyp_cd as varchar)=f_fnd.Finnova_value and f_fnd.ref_type=20
								left join
									FAF.FA_MROS_REFERENCE_TABLE t_fnd
								on s3.userbk_nr=t_fnd.userbk_nr and cast(s3.buchtyp_cd as varchar)=t_fnd.Finnova_value and t_fnd.ref_type=21
								left join
									FAF.FA_MROS_INT_WRG cur
								on s3.iso_wrg_cd=cur.iso_wrg_cd and s3.userbk_nr=cur.userbk_nr
								left join
									FAF.FA_MROS_KT_STAMM s5
								on s5.kt_lnr=s3.kt_lnr and s5.userbk_nr=s3.userbk_nr
								left join
									FAF.FA_MROS_KT_SALDO sld
								on s5.kt_lnr=sld.kt_lnr and s5.userbk_nr=sld.userbk_nr
								left join
									FAF.FA_MROS_INT_WRG wrg
								on s5.iso_wrg_cd=wrg.iso_wrg_cd and s5.userbk_nr=wrg.userbk_nr
								left join
									FAF.FA_MROS_REFERENCE_TABLE stz
								on s5.userbk_nr=stz.userbk_nr and cast(s5.sitz_nr as varchar)=stz.finnova_value and stz.ref_type=1
								left join
									FAF.FA_MROS_KD_STAMM cl
								on s5.kd_lnr=cl.kd_lnr and s5.userbk_nr=cl.userbk_nr and cl.partner_flag=0
								where s3.userbk_nr=@F_BANK_ID and s3.kt_buch_lnr = @n_id
								FOR XML PATH ('transaction'),type, ELEMENTS);
						end;

					set @xml_wd = (select @xml_wd,@xml_t FOR XML PATH (''));
				end;
				set @ii = @ii + 1;
			end;
		end;
	
		set @xml_r = (select @xml_p,@xml_wd FOR XML PATH (''),type, ELEMENTS);
	end;

	return(@xml_r);
end;
go




---------------------------------------------------------------------------------------------------



--caption part of report (technical function)
if object_id('[FAF].[head_part_of_report]', 'FN') is not NULL
	drop function [FAF].[head_part_of_report]; 
go
create function FAF.head_part_of_report (
	@F_BANK_ID int,
	@RP_NAME varchar(100),
	@RP_SURNAME varchar(100),
	@RP_PHONE varchar(50),
	@RP_EMAIL varchar(50),
	@RP_OCCUPATION varchar(255),
	@RP_ADDRESS varchar(100),
	@RP_CITY varchar(255),
	@FIU_REF_NUMBER varchar(255),
	@ENTITY_REFERENCE varchar(255),
	@REPORT_REASON varchar(4000),
	@R_ACTION varchar(4000),
	@REPORT_INDICATORS FAF.FAF_MROS_charID_t readonly,
	@report xml,
	@STR_flag tinyint,
	@REPORT_CODE varchar(255),
	@N_BR int) 
returns xml
as
begin
	declare @xml_r xml;

	if @STR_flag=1
	begin
		--check if there is a transaction here
		if coalesce(@report.exist('transaction'),0)=0
			set @xml_r= (select 'There are no transactions that meet the conditions; ' FOR XML PATH ('mros_report_error'),type, ELEMENTS);
	end;
	else
	begin
		--check if there is a report_party here
		if coalesce(@report.exist('activity/report_parties/report_party'),0)=0
			set @xml_r= (select 'There are no records that meet the conditions; ' FOR XML PATH ('mros_report_error'),type, ELEMENTS);
	end;

	if @xml_r is NULL
	begin
		set @xml_r = 
			(select 
				case 
					when FAF.GoAML_get_paramS(@F_BANK_ID,80,1)='ok' 
					then FAF.GoAML_char_process(FAF.GoAML_get_paramS(@F_BANK_ID,80,2),FAF.GoAML_get_paramV(@F_BANK_ID,'rp__schema_version'),FAF.GoAML_get_paramL(@F_BANK_ID,'rp__schema_version'))
					else NULL 
				end as "schema_version",
				FAF.GoAML_char_process(s4.rentity_id,FAF.GoAML_get_paramV(@F_BANK_ID,'rp__rentity_id'),FAF.GoAML_get_paramL(@F_BANK_ID,'rp__rentity_id')) as "rentity_id",
				FAF.GoAML_get_paramV(@F_BANK_ID,'rp__submission_code') as "submission_code",
				case
					when @REPORT_CODE is not NULL then @REPORT_CODE
					when @STR_flag=1 then 'STR'
					else 'SAR'
				end as "report_code",
				FAF.GoAML_char_process_WR(@ENTITY_REFERENCE,FAF.GoAML_get_paramV(@F_BANK_ID,'rp__entity_reference'),FAF.GoAML_get_paramL(@F_BANK_ID,'rp__entity_reference')) as "entity_reference",
				FAF.GoAML_char_process_WR(
					case
						when @FIU_REF_NUMBER is not NULL
						then @FIU_REF_NUMBER
						--when @REPORT_CODE=FAF.GoAML_get_paramS(@F_BANK_ID,125,1)
						--then FAF.GoAML_get_paramS(@F_BANK_ID,125,2)
						--when @REPORT_CODE=FAF.GoAML_get_paramS(@F_BANK_ID,126,1)
						--then FAF.GoAML_get_paramS(@F_BANK_ID,126,2)
						else NULL
					end
				,FAF.GoAML_get_paramV(@F_BANK_ID,'rp__fiu_ref_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'rp__fiu_ref_number')) as "fiu_ref_number",
				FAF.GoAML_char_process(
					case
						when FAF.GoAML_get_paramS(@F_BANK_ID,108,2)='ok'
						then convert(varchar,sysdatetime(),23)+'T00:00:00'
						else convert(varchar,sysdatetime(),126)
					end
				,FAF.GoAML_get_paramV(@F_BANK_ID,'rp__report_date'),FAF.GoAML_get_paramL(@F_BANK_ID,'rp__report_date')) as "report_date",
				FAF.GoAML_get_paramV(@F_BANK_ID,'rp__currency_code_local') as "currency_code_local",
				case
					when FAF.GoAML_get_paramS(@F_BANK_ID,107,1)='ok'
					then s4.reporting_user_code
					else NULL
				end as "reporting_user_code",
				case
					when FAF.GoAML_get_paramS(@F_BANK_ID,107,2)='ok'
					then NULL
					else
						FAF.GoAML_char_process(
							case
								when FAF.GoAML_char_process(@RP_NAME,NULL,0) is not NULL and FAF.GoAML_char_process(@RP_SURNAME,NULL,0) is not NULL
								then @RP_NAME
								else s4.first_name
							end
						,FAF.GoAML_get_paramV(@F_BANK_ID,'prir__first_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'prir__first_name'))
				end as "reporting_person/first_name",
				case
					when FAF.GoAML_get_paramS(@F_BANK_ID,107,2)='ok'
					then NULL
					else
						case 
							when FAF.GoAML_get_paramS(@F_BANK_ID,44,1)='ok'
							then upper(
								FAF.GoAML_char_process(
									case
										when FAF.GoAML_char_process(@RP_NAME,NULL,0) is not NULL and FAF.GoAML_char_process(@RP_SURNAME,NULL,0) is not NULL
										then @RP_SURNAME
										else s4.last_name
									end 
								,FAF.GoAML_get_paramV(@F_BANK_ID,'prir__last_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'prir__last_name')))
							else 
								FAF.GoAML_char_process(
									case
										when FAF.GoAML_char_process(@RP_NAME,NULL,0) is not NULL and FAF.GoAML_char_process(@RP_SURNAME,NULL,0) is not NULL
										then @RP_SURNAME
										else s4.last_name
									end 
								,FAF.GoAML_get_paramV(@F_BANK_ID,'prir__last_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'prir__last_name'))
						end
				end as "reporting_person/last_name",
				case
					when FAF.GoAML_get_paramS(@F_BANK_ID,107,2)='ok'
					then NULL
					else
						(select
							case 
								when FAF.GoAML_char_process(@RP_PHONE,NULL,0) is not NULL
								then 
									(select 
										FAF.GoAML_get_paramS(@F_BANK_ID,11,2) as "tph_contact_type",
										FAF.GoAML_get_paramS(@F_BANK_ID,13,2) as "tph_communication_type",
										FAF.GoAML_char_process_WR(@RP_PHONE,FAF.GoAML_get_paramV(@F_BANK_ID,'tph__tph_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'tph__tph_number')) as "tph_number"
									FOR XML PATH ('phone'),type, ELEMENTS)
								else NULL
							end,
							case 
								when FAF.GoAML_char_process(@RP_EMAIL,NULL,0) is not NULL
								then 
									(select 
										FAF.GoAML_get_paramS(@F_BANK_ID,11,2) as "tph_contact_type",
										FAF.GoAML_get_paramS(@F_BANK_ID,14,2) as "tph_communication_type",
										FAF.GoAML_char_process_WR(@RP_EMAIL,FAF.GoAML_get_paramV(@F_BANK_ID,'tph__tph_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'tph__tph_number')) as "tph_number"
									FOR XML PATH ('phone'),type, ELEMENTS)
								else NULL
							end,
							case 
								when FAF.GoAML_char_process(@RP_PHONE,NULL,0) is NULL and FAF.GoAML_char_process(@RP_EMAIL,NULL,0) is NULL
								then 
									(select 
										FAF.GoAML_get_paramS(@F_BANK_ID,11,2) as "tph_contact_type",
										FAF.GoAML_char_process(s4.tph_communication_type,FAF.GoAML_get_paramV(@F_BANK_ID,'tph__tph_communication_type'),FAF.GoAML_get_paramL(@F_BANK_ID,'tph__tph_communication_type')) as "tph_communication_type",
										FAF.GoAML_char_process(s4.tph_number,FAF.GoAML_get_paramV(@F_BANK_ID,'tph__tph_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'tph__tph_number')) as "tph_number"
									FOR XML PATH ('phone'),type, ELEMENTS)
								else NULL
							end
						FOR XML PATH (''),type, ELEMENTS)
				end as "reporting_person/phones",
				case
					when FAF.GoAML_get_paramS(@F_BANK_ID,107,2)='ok'
					then NULL
					else
						case 
							when FAF.GoAML_char_process(@RP_ADDRESS,NULL,0) is not NULL and FAF.GoAML_char_process(@RP_CITY,NULL,0) is not NULL
							then 
								(select 
									FAF.GoAML_get_paramS(@F_BANK_ID,114,1) as "address_type",
									FAF.GoAML_char_process_WR(@RP_ADDRESS,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__address')) as "address",
									FAF.GoAML_char_process_WR(@RP_CITY,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__city'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__city')) as "city",
									FAF.GoAML_get_paramS(@F_BANK_ID,15,2) as "country_code"
								FOR XML PATH ('address'),type, ELEMENTS)
							else NULL
						end
				end as "reporting_person/addresses",
				case
					when FAF.GoAML_get_paramS(@F_BANK_ID,107,2)='ok'
					then NULL
					else FAF.GoAML_char_process_WR(@RP_OCCUPATION,FAF.GoAML_get_paramV(@F_BANK_ID,'prir__occupation'),FAF.GoAML_get_paramL(@F_BANK_ID,'prir__occupation'))
				end as "reporting_person/occupation",
				FAF.GoAML_get_paramS(@F_BANK_ID,114,1) as "location/address_type",
				case
					when FAF.GoAML_get_paramS(@F_BANK_ID,119,1)='ok'
					then FAF.GoAML_get_paramS(@F_BANK_ID,119,2)
					else FAF.GoAML_char_process(s4.[address],FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__address'))
				end as "location/address",
				case
					when FAF.GoAML_get_paramS(@F_BANK_ID,119,1)='ok'
					then FAF.GoAML_get_paramS(@F_BANK_ID,119,2)
					else FAF.GoAML_char_process(s4.city,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__city'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__city'))
				end as "location/city",
				case
					when FAF.GoAML_get_paramS(@F_BANK_ID,119,1)='ok'
					then NULL
					else FAF.GoAML_char_process(s4.zip,FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__zip'),FAF.GoAML_get_paramL(@F_BANK_ID,'tadr__zip'))
				end as "location/zip",
				FAF.GoAML_get_paramS(@F_BANK_ID,15,2) as "location/country_code",
				FAF.GoAML_char_process_WR(@REPORT_REASON,FAF.GoAML_get_paramV(@F_BANK_ID,'rp__reason'),FAF.GoAML_get_paramL(@F_BANK_ID,'rp__reason')) as "reason",
				FAF.GoAML_char_process_WR(@R_ACTION,FAF.GoAML_get_paramV(@F_BANK_ID,'rp__action'),FAF.GoAML_get_paramL(@F_BANK_ID,'rp__action')) as "action",
				@report,
				(select chID as "indicator" from @REPORT_INDICATORS FOR XML PATH (''),type, ELEMENTS) as "report_indicators",
				case 
					when FAF.GoAML_get_paramS(@F_BANK_ID,133,1)='ok'
					then 
						(select
							FAF.GoAML_get_paramS(@F_BANK_ID,133,2) as "info_type",
							@N_BR as "info_numeric"
						FOR XML PATH ('additional_info'),type, ELEMENTS)
					else NULL
				end as "additional_information"
			from 
				FAF.FA_MROS_INT_PERS_CHARGE s4
			where s4.userbk_nr_home=@F_BANK_ID
			FOR XML PATH ('report'),type, ELEMENTS);
	end;
		
	return(@xml_r);
end;
go	


---------------------------------------------------------------------------------------------------

--transactions report from ktwbuch_det (transaction number collection)
if object_id('[FAF].[booking_report]', 'FN') is not NULL
	drop function [FAF].[booking_report]; 
go
create function FAF.booking_report (
	@F_BANK_ID int,
	@RP_NAME varchar(100),
	@RP_SURNAME varchar(100),
	@RP_PHONE varchar(50),
	@RP_EMAIL varchar(50),
	@RP_OCCUPATION varchar(255),
	@RP_ADDRESS varchar(100),
	@RP_CITY varchar(255),
	@FIU_REF_NUMBER varchar(255),
	@ENTITY_REFERENCE varchar(255),
	@REPORT_REASON varchar(4000),
	@R_ACTION varchar(4000),
	@REPORT_INDICATORS FAF.FAF_MROS_charID_t readonly,
	@SET_OF_TR_ID FAF.FAF_MROS_numID_t readonly,
	@AC_NR varchar(50),
	@REPORT_CODE varchar(255)) 
returns xml
as
begin
	declare @xml_r xml,
			@xml_t xml;
	--declare @ind FAF.FAF_MROS_charID_t;
	declare @pmnt_id FAF.FAF_MROS_PmntTpl_t; 
	declare @trans_id FAF.FAF_MROS_TrTpl_t;
	declare @acnt_id FAF.FAF_MROS_NumNum_t;
	declare @cl_id decimal(24,0);
	declare @str varchar(4000);
	declare @nn_p int;
	declare @nn_t int;
	declare @nn int;
	declare @kk int;
	declare @max_items int;
	declare @cur_date date;
	set @max_items = 1000;
	set @cur_date = cast(sysdatetime() as date);
	set @str = '';
	--indicators checking
	--insert into @ind
	--	select indicator from FAF.FA_MROS_INT_INDIZ where userbk_nr=@F_BANK_ID and indicator in (select ltrim(rtrim(chID)) from @REPORT_INDICATORS);
	
	--if FAF.GoAML_get_paramS(@F_BANK_ID,45,1)='ok'
	--begin
	--	if (select count(*) from @ind where right(chID,1)='M')<>1 
	--		set @str=@str + 'the report must include only one indicator with type Report_Type (M)';
	--	if (select count(*) from @ind where right(chID,1)='V')<1
	--		set @str = case when len(@str)=0 then '' else @str+', ' end + 'the report must include at least one indicator with type Predicate_Offenses (V)';
	--	if (select count(*) from @ind where right(chID,1)='G')<1
	--		set @str = case when len(@str)=0 then '' else @str+', ' end + 'the report must include at least one indicator with type Reason (G)';
	--	if len(@str)>0
	--		set @str = 'Indicators error: ' + @str + '; ';
	--end;

	--table FA_MROS_INT_PERS_CHARGE checking
	if (select count(*) from FAF.FA_MROS_INT_PERS_CHARGE where userbk_nr_home=@F_BANK_ID)=0
		set @str = @str + 'The table FAF_MROS_persons_in_charge does not have the record with userbk_nr_home = ' + cast(@F_BANK_ID as varchar) + '; ';

	--account ID checking
	if (select count(*) from FAF.FA_MROS_KT_STAMM where userbk_nr=@F_BANK_ID and EDIT_kt_nr=@AC_NR)=0
		set @str = @str + 'Account number not found; ';

	--transactions set checking
	if (select count(*) from @SET_OF_TR_ID)=0
		set @str = @str + 'At least one transaction number should be provided; ';

	if upper(@REPORT_CODE)=FAF.GoAML_get_paramS(@F_BANK_ID,116,1)
	begin
		set @kk = 
			(select count(*) from FAF.FA_MROS_KT_STAMM kt
			where kt.edit_kt_nr=@AC_NR
				and kt.userbk_nr=@F_BANK_ID
				and (kt.aufheb_dat is null or kt.aufheb_dat<coalesce(kt.eroeff_dat,convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)) or cast(kt.aufheb_dat as date)>@cur_date));
		if @kk>0
			set @str = @str + 'In a ' + FAF.GoAML_get_paramS(@F_BANK_ID,116,1) + ' report should be only closed accounts, but accont ' + @AC_NR + ' is open; ';
		set @kk = NULL;
	end;

	--Error processing
	if len(@str)>0
		set @xml_r= (select @str as "mros_report_error" FOR XML PATH (''),type, ELEMENTS);
	
	--if there is no error then start the main request
	if @xml_r is NULL
	begin
		--list of relevant payments
		if @AC_NR is not NULL
			begin
				insert into @pmnt_id
				select top (@max_items)
					zv_zlg_sys_lnr
					,kt_lnr
					,kt_lnr_b
					,zv_adr_eff_lnr
					,auf_zv_adr_eff_lnr
					,om_text_lnr
					,case
						when zv_eing_ausg_cd='E' and auf_zv_adr_eff_lnr is not NULL then 1
						when zv_eing_ausg_cd='A' and zv_adr_eff_lnr is not NULL then 4
						when (zv_eing_ausg_cd='E' and auf_zv_adr_eff_lnr is NULL and sh_cd=1) or (zv_eing_ausg_cd='A' and zv_adr_eff_lnr is NULL and sh_cd=1) then 2
						when (zv_eing_ausg_cd='E' and auf_zv_adr_eff_lnr is NULL and sh_cd=2) or (zv_eing_ausg_cd='A' and zv_adr_eff_lnr is NULL and sh_cd=2) then 3
					end,
					case 
						when FAF.GoAML_char_process(esr_ref_nr,NULL,0) is not NULL
						then FAF.GoAML_char_process(replace(esr_ref_nr,' ',''),NULL,27)
						when gf_nr=211 
						then 'Post'
						else NULL
					end,
					acc_psn_flag,
					sh_cd,
					kt_id_orig,
					key_20,key_30,key_40,key_50,key_60,key_70,key_80,key_90,key_100
				from
					(select
						pm.zv_zlg_sys_lnr,pm.ausf_dt_ist,pm.kt_lnr,pm.kt_lnr_b,pm.zv_adr_eff_lnr,
						pm.auf_zv_adr_eff_lnr,pm.om_text_lnr,pm.zv_eing_ausg_cd,pm.sh_cd,pm.esr_ref_nr,
						pm.gf_nr,pm.kt_id_orig,
						-- client <--> account condition
						case
							when cd_nr.userbk_nr is not NULL then 1
							when pm.trak_cd_e like 'DTB%' then 1
							when pm.trak_cd_e like 'KTB%' then 1
							when lower(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end) in ('card_operation','card_purchase') then 1
							when pm.om_auf_empf_cd in ('PE','PG') then 1
							else 0
						end as acc_psn_flag,
						case when (pm.zv_eing_ausg_cd='E' and pm.auf_zv_adr_eff_lnr is NULL) or (pm.zv_eing_ausg_cd='A' and pm.zv_adr_eff_lnr is NULL) then 1 else 0 end as key_20,
						case when pm.gf_nr=211 then 1 else 0 end as key_30,
						case when pm.om_auf_empf_cd in ('PE','PG') then 1 else 0 end as key_40,
						case when pm.trak_cd_e like 'DTB%' or pm.trak_cd_e like 'KTB%' then 1 else 0 end as key_50,
						case when pm.om_auf_empf_cd = 'TG' then 1 else 0 end as key_60,
						case when pm.om_auf_empf_cd = 'TB' then 1 else 0 end as key_70,
						case when FAF.GoAML_char_process(pm.esr_ref_nr,NULL,0) is not NULL then 1 else 0 end as key_80,
						case when lower(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end) in ('card_operation','card_purchase') then 1 else 0 end as key_90,
						case when cd_nr.userbk_nr is not NULL then 1 else 0 end as key_100
					from 
						FAF.FA_MROS_KT_BUCH tr
					join
						FAF.FA_MROS_ZV_ZLG_T pm
					on tr.userbk_nr=pm.userbk_nr and tr.auf_lnr=pm.auf_lnr and tr.kt_lnr=pm.kt_lnr
					--acceptable transaction types only
					join
						FAF.FA_MROS_REFERENCE_TABLE tr_tp
					on pm.buchtyp_cd=tr_tp.Finnova_value and tr_tp.userbk_nr=pm.userbk_nr and tr_tp.ref_type=10 and tr_tp.activ_MROS=1
					--acceptable trak_ref types only
					join
						FAF.FA_MROS_REFERENCE_TABLE ref_tp
					on pm.trak_ref_typ=ref_tp.Finnova_value and ref_tp.userbk_nr=pm.userbk_nr and ref_tp.ref_type=11 and ref_tp.activ_MROS=1
					--account - person payments
					left join
						FAF.FA_MROS_REFERENCE_TABLE cd_nr
					on pm.card_art_nr=cd_nr.Finnova_value and cd_nr.userbk_nr=pm.userbk_nr and cd_nr.ref_type=22 and cd_nr.special_mark='BANKCARD'
					left join
						FAF.FA_MROS_TRX_CATEGORIES_DET cat
					on cat.userbk_nr=pm.userbk_nr and cat.buchtyp_cd=pm.buchtyp_cd and cat.trak_ref_typ=pm.trak_ref_typ and cat.gf_nr=pm.gf_nr
					where tr.userbk_nr=@F_BANK_ID 
						and tr.kt_buch_lnr in (select nID from @SET_OF_TR_ID)
						and tr.edit_kt_nr=@AC_NR
					union
					select
						pm.zv_zlg_sys_lnr,pm.ausf_dt_ist,pm.kt_lnr,pm.kt_lnr_b,pm.zv_adr_eff_lnr,
						pm.auf_zv_adr_eff_lnr,pm.om_text_lnr,pm.zv_eing_ausg_cd,pm.sh_cd,pm.esr_ref_nr,
						pm.gf_nr,pm.kt_id_orig,
						-- client <--> account condition
						case
							when cd_nr.userbk_nr is not NULL then 1
							when pm.trak_cd_e like 'DTB%' then 1
							when pm.trak_cd_e like 'KTB%' then 1
							when lower(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end) in ('card_operation','card_purchase') then 1
							when pm.om_auf_empf_cd in ('PE','PG') then 1
							else 0
						end as acc_psn_flag,
						case when (pm.zv_eing_ausg_cd='E' and pm.auf_zv_adr_eff_lnr is NULL) or (pm.zv_eing_ausg_cd='A' and pm.zv_adr_eff_lnr is NULL) then 1 else 0 end as key_20,
						case when pm.gf_nr=211 then 1 else 0 end as key_30,
						case when pm.om_auf_empf_cd in ('PE','PG') then 1 else 0 end as key_40,
						case when pm.trak_cd_e like 'DTB%' or pm.trak_cd_e like 'KTB%' then 1 else 0 end as key_50,
						case when pm.om_auf_empf_cd = 'TG' then 1 else 0 end as key_60,
						case when pm.om_auf_empf_cd = 'TB' then 1 else 0 end as key_70,
						case when FAF.GoAML_char_process(pm.esr_ref_nr,NULL,0) is not NULL then 1 else 0 end as key_80,
						case when lower(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end) in ('card_operation','card_purchase') then 1 else 0 end as key_90,
						case when cd_nr.userbk_nr is not NULL then 1 else 0 end as key_100
					from 
						FAF.FA_MROS_KT_BUCH tr
					join
						FAF.FA_MROS_ZV_ZLG_T pm
					on tr.userbk_nr=pm.userbk_nr and tr.auf_lnr=pm.auf_lnr and tr.kt_lnr=pm.kt_lnr_b
					--acceptable transaction types only
					join
						FAF.FA_MROS_REFERENCE_TABLE tr_tp
					on pm.buchtyp_cd=tr_tp.Finnova_value and tr_tp.userbk_nr=pm.userbk_nr and tr_tp.ref_type=10 and tr_tp.activ_MROS=1
					--acceptable trak_ref types only
					join
						FAF.FA_MROS_REFERENCE_TABLE ref_tp
					on pm.trak_ref_typ=ref_tp.Finnova_value and ref_tp.userbk_nr=pm.userbk_nr and ref_tp.ref_type=11 and ref_tp.activ_MROS=1
					--account - person payments
					left join
						FAF.FA_MROS_REFERENCE_TABLE cd_nr
					on pm.card_art_nr=cd_nr.Finnova_value and cd_nr.userbk_nr=pm.userbk_nr and cd_nr.ref_type=22 and cd_nr.special_mark='BANKCARD'
					left join
						FAF.FA_MROS_TRX_CATEGORIES_DET cat
					on cat.userbk_nr=pm.userbk_nr and cat.buchtyp_cd=pm.buchtyp_cd and cat.trak_ref_typ=pm.trak_ref_typ and cat.gf_nr=pm.gf_nr
					where tr.userbk_nr=@F_BANK_ID 
						and tr.kt_buch_lnr in (select nID from @SET_OF_TR_ID)
						and tr.edit_kt_nr=@AC_NR
					) s
				order by s.ausf_dt_ist desc;
			end;
		else
			begin
				insert into @pmnt_id
				select top (@max_items)
					zv_zlg_sys_lnr
					,kt_lnr
					,kt_lnr_b
					,zv_adr_eff_lnr
					,auf_zv_adr_eff_lnr
					,om_text_lnr
					,case
						when zv_eing_ausg_cd='E' and auf_zv_adr_eff_lnr is not NULL then 1
						when zv_eing_ausg_cd='A' and zv_adr_eff_lnr is not NULL then 4
						when (zv_eing_ausg_cd='E' and auf_zv_adr_eff_lnr is NULL and sh_cd=1) or (zv_eing_ausg_cd='A' and zv_adr_eff_lnr is NULL and sh_cd=1) then 2
						when (zv_eing_ausg_cd='E' and auf_zv_adr_eff_lnr is NULL and sh_cd=2) or (zv_eing_ausg_cd='A' and zv_adr_eff_lnr is NULL and sh_cd=2) then 3
					end,
					case 
						when FAF.GoAML_char_process(esr_ref_nr,NULL,0) is not NULL
						then FAF.GoAML_char_process(replace(esr_ref_nr,' ',''),NULL,27)
						when gf_nr=211 
						then 'Post'
						else NULL
					end,
					acc_psn_flag,
					sh_cd,
					kt_id_orig,
					key_20,key_30,key_40,key_50,key_60,key_70,key_80,key_90,key_100
				from
					(select
						pm.zv_zlg_sys_lnr,pm.ausf_dt_ist,pm.kt_lnr,pm.kt_lnr_b,pm.zv_adr_eff_lnr,
						pm.auf_zv_adr_eff_lnr,pm.om_text_lnr,pm.zv_eing_ausg_cd,pm.sh_cd,pm.esr_ref_nr,
						pm.gf_nr,pm.kt_id_orig,
						-- client <--> account condition
						case
							when cd_nr.userbk_nr is not NULL then 1
							when pm.trak_cd_e like 'DTB%' then 1
							when pm.trak_cd_e like 'KTB%' then 1
							when lower(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end) in ('card_operation','card_purchase') then 1
							when pm.om_auf_empf_cd in ('PE','PG') then 1
							else 0
						end as acc_psn_flag,
						case when (pm.zv_eing_ausg_cd='E' and pm.auf_zv_adr_eff_lnr is NULL) or (pm.zv_eing_ausg_cd='A' and pm.zv_adr_eff_lnr is NULL) then 1 else 0 end as key_20,
						case when pm.gf_nr=211 then 1 else 0 end as key_30,
						case when pm.om_auf_empf_cd in ('PE','PG') then 1 else 0 end as key_40,
						case when pm.trak_cd_e like 'DTB%' or pm.trak_cd_e like 'KTB%' then 1 else 0 end as key_50,
						case when pm.om_auf_empf_cd = 'TG' then 1 else 0 end as key_60,
						case when pm.om_auf_empf_cd = 'TB' then 1 else 0 end as key_70,
						case when FAF.GoAML_char_process(pm.esr_ref_nr,NULL,0) is not NULL then 1 else 0 end as key_80,
						case when lower(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end) in ('card_operation','card_purchase') then 1 else 0 end as key_90,
						case when cd_nr.userbk_nr is not NULL then 1 else 0 end as key_100
					from 
						FAF.FA_MROS_KT_BUCH tr
					join
						FAF.FA_MROS_ZV_ZLG_T pm
					on tr.userbk_nr=pm.userbk_nr and tr.auf_lnr=pm.auf_lnr and tr.kt_lnr=pm.kt_lnr
					--acceptable transaction types only
					join
						FAF.FA_MROS_REFERENCE_TABLE tr_tp
					on pm.buchtyp_cd=tr_tp.Finnova_value and tr_tp.userbk_nr=pm.userbk_nr and tr_tp.ref_type=10 and tr_tp.activ_MROS=1
					--acceptable trak_ref types only
					join
						FAF.FA_MROS_REFERENCE_TABLE ref_tp
					on pm.trak_ref_typ=ref_tp.Finnova_value and ref_tp.userbk_nr=pm.userbk_nr and ref_tp.ref_type=11 and ref_tp.activ_MROS=1
					--account - person payments
					left join
						FAF.FA_MROS_REFERENCE_TABLE cd_nr
					on pm.card_art_nr=cd_nr.Finnova_value and cd_nr.userbk_nr=pm.userbk_nr and cd_nr.ref_type=22 and cd_nr.special_mark='BANKCARD'
					left join
						FAF.FA_MROS_TRX_CATEGORIES_DET cat
					on cat.userbk_nr=pm.userbk_nr and cat.buchtyp_cd=pm.buchtyp_cd and cat.trak_ref_typ=pm.trak_ref_typ and cat.gf_nr=pm.gf_nr
					where tr.userbk_nr=@F_BANK_ID and tr.kt_buch_lnr in (select nID from @SET_OF_TR_ID)
					union
					select
						pm.zv_zlg_sys_lnr,pm.ausf_dt_ist,pm.kt_lnr,pm.kt_lnr_b,pm.zv_adr_eff_lnr,
						pm.auf_zv_adr_eff_lnr,pm.om_text_lnr,pm.zv_eing_ausg_cd,pm.sh_cd,pm.esr_ref_nr,
						pm.gf_nr,pm.kt_id_orig,
						-- client <--> account condition
						case
							when cd_nr.userbk_nr is not NULL then 1
							when pm.trak_cd_e like 'DTB%' then 1
							when pm.trak_cd_e like 'KTB%' then 1
							when lower(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end) in ('card_operation','card_purchase') then 1
							when pm.om_auf_empf_cd in ('PE','PG') then 1
							else 0
						end as acc_psn_flag,
						case when (pm.zv_eing_ausg_cd='E' and pm.auf_zv_adr_eff_lnr is NULL) or (pm.zv_eing_ausg_cd='A' and pm.zv_adr_eff_lnr is NULL) then 1 else 0 end as key_20,
						case when pm.gf_nr=211 then 1 else 0 end as key_30,
						case when pm.om_auf_empf_cd in ('PE','PG') then 1 else 0 end as key_40,
						case when pm.trak_cd_e like 'DTB%' or pm.trak_cd_e like 'KTB%' then 1 else 0 end as key_50,
						case when pm.om_auf_empf_cd = 'TG' then 1 else 0 end as key_60,
						case when pm.om_auf_empf_cd = 'TB' then 1 else 0 end as key_70,
						case when FAF.GoAML_char_process(pm.esr_ref_nr,NULL,0) is not NULL then 1 else 0 end as key_80,
						case when lower(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end) in ('card_operation','card_purchase') then 1 else 0 end as key_90,
						case when cd_nr.userbk_nr is not NULL then 1 else 0 end as key_100
					from 
						FAF.FA_MROS_KT_BUCH tr
					join
						FAF.FA_MROS_ZV_ZLG_T pm
					on tr.userbk_nr=pm.userbk_nr and tr.auf_lnr=pm.auf_lnr and tr.kt_lnr=pm.kt_lnr_b
					--acceptable transaction types only
					join
						FAF.FA_MROS_REFERENCE_TABLE tr_tp
					on pm.buchtyp_cd=tr_tp.Finnova_value and tr_tp.userbk_nr=pm.userbk_nr and tr_tp.ref_type=10 and tr_tp.activ_MROS=1
					--acceptable trak_ref types only
					join
						FAF.FA_MROS_REFERENCE_TABLE ref_tp
					on pm.trak_ref_typ=ref_tp.Finnova_value and ref_tp.userbk_nr=pm.userbk_nr and ref_tp.ref_type=11 and ref_tp.activ_MROS=1
					--account - person payments
					left join
						FAF.FA_MROS_REFERENCE_TABLE cd_nr
					on pm.card_art_nr=cd_nr.Finnova_value and cd_nr.userbk_nr=pm.userbk_nr and cd_nr.ref_type=22 and cd_nr.special_mark='BANKCARD'
					left join
						FAF.FA_MROS_TRX_CATEGORIES_DET cat
					on cat.userbk_nr=pm.userbk_nr and cat.buchtyp_cd=pm.buchtyp_cd and cat.trak_ref_typ=pm.trak_ref_typ and cat.gf_nr=pm.gf_nr
					where tr.userbk_nr=@F_BANK_ID and tr.kt_buch_lnr in (select nID from @SET_OF_TR_ID)
					) s
				order by s.ausf_dt_ist desc;
			end;
		set @nn_p = (select count(*) from @pmnt_id);

		--list of relevant transactions
		if (@max_items>@nn_p)
		begin
			if @AC_NR is not NULL
				begin
					insert into @trans_id
					select top (@max_items-@nn_p)
						KT_BUCH_LNR as tr_id,
						kt_lnr as ac_id,
						-- client <--> account condition
						case
							when lower(special_mark) in ('withdrawal','deposit','card_operation','card_purchase','cash_operation')
							then 100
							else 0
						end +
						case
							when sh_cd = 'C' then 10
							when sh_cd = 'D' then 0
						end +
						case
							when adr_zeile_1_ez is not NULL
							then 1
							else 0
						end as tp,
						case when lower(special_mark) = 'cash_operation' then 1 else 0 end as key_510,
						case when lower(special_mark) = 'card_operation' then 1 else 0 end as key_520,
						case when lower(special_mark) = 'deposit' then 1 else 0 end as key_530,
						case when lower(special_mark) = 'withdrawal' then 1 else 0 end as key_540,
						case when lower(special_mark) = 'card_purchase' then 1 else 0 end as key_550,
						case when adr_zeile_1_ez is not null then 1 else 0 end as key_560,
						case when special_mark is not NULL and lower(special_mark) not in ('withdrawal','deposit','card_operation','card_purchase','cash_operation') then 1 else 0 end as key_570
					from
						(select
							tr.KT_BUCH_LNR,tr.kt_lnr,
							case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end as special_mark,
							tr.buch_dat_a_1,tr.sh_cd,tr.adr_zeile_1_ez
						from
							FAF.FA_MROS_KT_BUCH_EA tr
						--acceptable transaction types only
						join
							FAF.FA_MROS_REFERENCE_TABLE tr_tp
						on tr.buchtyp_cd=tr_tp.Finnova_value and tr_tp.userbk_nr=tr.userbk_nr and tr_tp.ref_type=10 and tr_tp.activ_MROS=1
						--acceptable trak_ref types only
						join
							FAF.FA_MROS_REFERENCE_TABLE ref_tp
						on tr.trak_ref_typ=ref_tp.Finnova_value and ref_tp.userbk_nr=tr.userbk_nr and ref_tp.ref_type=11 and ref_tp.activ_MROS=1
						left join
							FAF.FA_MROS_TRX_CATEGORIES_DET cat
						on cat.userbk_nr=tr.userbk_nr and cat.buchtyp_cd=tr.buchtyp_cd and cat.trak_ref_typ=tr.trak_ref_typ and cat.gf_nr is NULL
						where tr.userbk_nr=@F_BANK_ID 
							and tr.kt_buch_lnr in (select nID from @SET_OF_TR_ID)
							and tr.edit_kt_nr=@AC_NR
						) s
					order by s.buch_dat_a_1 desc;
				end;
			else
				begin
					insert into @trans_id
					select top (@max_items-@nn_p)
						KT_BUCH_LNR as tr_id,
						kt_lnr as ac_id,
						-- client <--> account condition
						case
							when lower(special_mark) in ('withdrawal','deposit','card_operation','card_purchase','cash_operation')
							then 100
							else 0
						end +
						case
							when sh_cd = 'C' then 10
							when sh_cd = 'D' then 0
						end +
						case
							when adr_zeile_1_ez is not NULL
							then 1
							else 0
						end as tp,
						case when lower(special_mark) = 'cash_operation' then 1 else 0 end as key_510,
						case when lower(special_mark) = 'card_operation' then 1 else 0 end as key_520,
						case when lower(special_mark) = 'deposit' then 1 else 0 end as key_530,
						case when lower(special_mark) = 'withdrawal' then 1 else 0 end as key_540,
						case when lower(special_mark) = 'card_purchase' then 1 else 0 end as key_550,
						case when adr_zeile_1_ez is not null then 1 else 0 end as key_560,
						case when special_mark is not NULL and lower(special_mark) not in ('withdrawal','deposit','card_operation','card_purchase','cash_operation') then 1 else 0 end as key_570
					from
						(select
							tr.KT_BUCH_LNR,tr.kt_lnr,
							case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end as special_mark,
							tr.buch_dat_a_1,tr.sh_cd,tr.adr_zeile_1_ez
						from
							FAF.FA_MROS_KT_BUCH_EA tr
						--acceptable transaction types only
						join
							FAF.FA_MROS_REFERENCE_TABLE tr_tp
						on tr.buchtyp_cd=tr_tp.Finnova_value and tr_tp.userbk_nr=tr.userbk_nr and tr_tp.ref_type=10 and tr_tp.activ_MROS=1
						--acceptable trak_ref types only
						join
							FAF.FA_MROS_REFERENCE_TABLE ref_tp
						on tr.trak_ref_typ=ref_tp.Finnova_value and ref_tp.userbk_nr=tr.userbk_nr and ref_tp.ref_type=11 and ref_tp.activ_MROS=1
						left join
							FAF.FA_MROS_TRX_CATEGORIES_DET cat
						on cat.userbk_nr=tr.userbk_nr and cat.buchtyp_cd=tr.buchtyp_cd and cat.trak_ref_typ=tr.trak_ref_typ and cat.gf_nr is NULL
						where tr.userbk_nr=@F_BANK_ID and tr.kt_buch_lnr in (select nID from @SET_OF_TR_ID)
						) s
					order by s.buch_dat_a_1 desc;
				end;
		end;
		set @nn_t = (select count(*) from @trans_id);
		
		--list of accounts
		if @nn_p>0 or @nn_t>0
		begin
			if @AC_NR is not NULL
				begin
					set @cl_id = (select kd_lnr from FAF.FA_MROS_KT_STAMM where userbk_nr=@F_BANK_ID and edit_kt_nr=@AC_NR);

					with 
						pm0 as 
							(select pm.a1_kt,pm.a2_kt,pm.tp,
								case 
									when pm.ap_flag>0
									then
										case
											when kd1.bank_flag=1 and kd2.bank_flag=0 then 1
											when kd1.bank_flag=0 and kd2.bank_flag=1 then 2
											else 0
										end
									else 0
								end as ap_flag 
							from 
								@pmnt_id pm
							left join
								FAF.FA_MROS_KT_STAMM kt1
							on kt1.userbk_nr=@F_BANK_ID and kt1.kt_lnr=pm.a1_kt
							left join
								FAF.FA_MROS_KD_STAMM kd1
							on kd1.userbk_nr=@F_BANK_ID and kd1.kd_lnr=kt1.kd_lnr and kd1.partner_flag=0
							left join
								FAF.FA_MROS_KT_STAMM kt2
							on kt2.userbk_nr=@F_BANK_ID and kt2.kt_lnr=pm.a2_kt
							left join
								FAF.FA_MROS_KD_STAMM kd2
							on kd2.userbk_nr=@F_BANK_ID and kd2.kd_lnr=kt2.kd_lnr and kd2.partner_flag=0)
					insert into @acnt_id
					select t.nID1,t.nID2+(case when kt.userbk_nr is not NULL then 1 else 0 end)*2 as nID2
					from
						(select nID1,max(nID2) as nID2 from
							(select a1_kt as nID1,
								case when ap_flag>0 then 1 else 0 end as nID2 
							from pm0 where ap_flag<>1
							union all
							select a2_kt as nID1,
								case when ap_flag>0 then 1 else 0 end as nID2 
							from pm0 where (tp<4 and tp>1 and ap_flag=0) or ap_flag=1
							union all
							select ac_id as nID1, case when tp>=100 then 1 else 0 end as nID2 from @trans_id) s
						group by nID1) t
					left join
						FAF.FA_MROS_KT_STAMM kt
					on kt.userbk_nr=@F_BANK_ID and kt.kt_lnr=t.nID1 and kt.kd_lnr = @cl_id;
				end;
			else
				begin
					with 
						pm0 as 
							(select pm.a1_kt,pm.a2_kt,pm.tp,
								case 
									when pm.ap_flag>0
									then
										case
											when kd1.bank_flag=1 and kd2.bank_flag=0 then 1
											when kd1.bank_flag=0 and kd2.bank_flag=1 then 2
											else 0
										end
									else 0
								end as ap_flag 
							from 
								@pmnt_id pm
							left join
								FAF.FA_MROS_KT_STAMM kt1
							on kt1.userbk_nr=@F_BANK_ID and kt1.kt_lnr=pm.a1_kt
							left join
								FAF.FA_MROS_KD_STAMM kd1
							on kd1.userbk_nr=@F_BANK_ID and kd1.kd_lnr=kt1.kd_lnr and kd1.partner_flag=0
							left join
								FAF.FA_MROS_KT_STAMM kt2
							on kt2.userbk_nr=@F_BANK_ID and kt2.kt_lnr=pm.a2_kt
							left join
								FAF.FA_MROS_KD_STAMM kd2
							on kd2.userbk_nr=@F_BANK_ID and kd2.kd_lnr=kt2.kd_lnr and kd2.partner_flag=0)
					insert into @acnt_id
					select nID1,max(nID2)+2 as nID2 from
						(select a1_kt as nID1,
							case when ap_flag>0 then 1 else 0 end as nID2 
						from pm0 where ap_flag<>1
						union all
						select a2_kt as nID1,
							case when ap_flag>0 then 1 else 0 end as nID2 
						from pm0 where (tp<4 and tp>1 and ap_flag=0) or ap_flag=1
						union all
						select ac_id as nID1, case when tp>=100 then 1 else 0 end as nID2 from @trans_id) s
					group by nID1;
				end;
			set @nn = (select count(*) from @acnt_id);

			if @nn>0
				set @xml_t = FAF.transaction_node(@F_BANK_ID,@pmnt_id,@trans_id,@acnt_id);
		end;
	
		--remain part of the report
		set @xml_r = FAF.head_part_of_report (@F_BANK_ID,@RP_NAME,@RP_SURNAME,@RP_PHONE,@RP_EMAIL,@RP_OCCUPATION,@RP_ADDRESS,@RP_CITY,@FIU_REF_NUMBER,@ENTITY_REFERENCE,@REPORT_REASON,@R_ACTION,@REPORT_INDICATORS,@xml_t,1,@REPORT_CODE,1);
	end;

	return(@xml_r);
end;
go	


---------------------------------------------------------------------------------------------------


--transactions report from ktwbuch_det (clients/accounts + filter)
if object_id('[FAF].[historical_report]', 'FN') is not NULL
	drop function [FAF].[historical_report]; 
go
create function FAF.historical_report (
	@F_BANK_ID int,
	@RP_NAME varchar(100),
	@RP_SURNAME varchar(100),
	@RP_PHONE varchar(50),
	@RP_EMAIL varchar(50),
	@RP_OCCUPATION varchar(255),
	@RP_ADDRESS varchar(100),
	@RP_CITY varchar(255),
	@FIU_REF_NUMBER varchar(255),
	@ENTITY_REFERENCE varchar(255),
	@REPORT_REASON varchar(4000),
	@R_ACTION varchar(4000),
	@REPORT_INDICATORS FAF.FAF_MROS_charID_t readonly,
	@SET_OF_CL_NR FAF.FAF_MROS_charID_t readonly,
	@SET_OF_AC_NR FAF.FAF_MROS_charID_t readonly,
	@SET_OF_BUCH_CAT FAF.FAF_MROS_CharChar_t readonly,
	@DATE_FROM int,
	@DATE_TO int,
	@LOW_AMOUNT decimal(20,2),
	@DEBIT tinyint,
	@CREDIT tinyint,
	@N_TRANS_LIMIT int,
	@REPORT_CODE varchar(255)) 
returns xml
as
begin
	declare @xml_r xml,
		@xml_t xml;
	declare @am_lim decimal(20,2);
	declare @d1 int;
	declare @d2 int;
	declare @dd1 date;
	declare @dd2 date;
	declare @nn int;
	declare @kk int;
	declare @n_ct int;
	declare @nn_a int;
	declare @nn_c int;
	declare @nn_p int;
	declare @nn_t int;
	declare @pmnt_id FAF.FAF_MROS_PmntTpl_t; 
	declare @trans_id FAF.FAF_MROS_TrTpl_t;
	declare @acnt_id FAF.FAF_MROS_NumNum_t;
	declare @SET_OF_AC_ID FAF.FAF_MROS_NumID_t;
	declare @SET_OF_BUCH_CAT1 FAF.FAF_MROS_CharChar_t;
	--declare @ind FAF.FAF_MROS_charID_t;
	declare @str varchar(4000);
	declare @st1 varchar(4000);
	declare @Ntrans int;
	declare @max_items int;
	declare @cur_date date;
	declare @N_BR int;
	set @max_items = 1000;
	set @cur_date = cast(sysdatetime() as date);
	set @str = '';

	--indicators checking
	--insert into @ind
	--	select indicator from FAF.FA_MROS_INT_INDIZ where userbk_nr=@F_BANK_ID and indicator in (select ltrim(rtrim(chID)) from @REPORT_INDICATORS);
	--if FAF.GoAML_get_paramS(@F_BANK_ID,45,1)='ok'
	--begin
	--	if (select count(*) from @ind where right(chID,1)='M')<>1 
	--		set @str=@str + 'the report must include only one indicator with type Report_Type (M)';
	--	if (select count(*) from @ind where right(chID,1)='V')<1
	--		set @str = case when len(@str)=0 then '' else @str+', ' end + 'the report must include at least one indicator with type Predicate_Offenses (V)';
	--	if (select count(*) from @ind where right(chID,1)='G')<1
	--		set @str = case when len(@str)=0 then '' else @str+', ' end + 'the report must include at least one indicator with type Reason (G)';
	--	if len(@str)>0
	--		set @str = 'Indicators error: ' + @str + '; ';
	--end;

	--table FA_MROS_INT_PERS_CHARGE checking
	if (select count(*) from FAF.FA_MROS_INT_PERS_CHARGE where userbk_nr_home=@F_BANK_ID)=0
		set @str = @str + 'The table FAF_MROS_persons_in_charge does not have the record with userbk_nr_home = ' + cast(@F_BANK_ID as varchar) + '; ';

	--debit/credit checking
	if coalesce(@DEBIT,1)=0 and coalesce(@CREDIT,1)=0
		set @str = @str + 'At least one from Debit/Credit should be nonzero; ';

	if upper(@REPORT_CODE)=FAF.GoAML_get_paramS(@F_BANK_ID,116,1)
	begin
		set @kk = 
			(select count(*)
			from 
				FAF.FA_MROS_KT_STAMM kt
			where kt.edit_kt_nr in (select s1.chID from @SET_OF_AC_NR s1)
				and kt.userbk_nr=@F_BANK_ID
				and (kt.aufheb_dat is null or kt.aufheb_dat<coalesce(kt.eroeff_dat,convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)) or cast(kt.aufheb_dat as date)>@cur_date));
		if @kk>0
		begin
			with s as
				(select kt.userbk_nr,kt.edit_kt_nr from 
					FAF.FA_MROS_KT_STAMM kt
				where kt.edit_kt_nr in (select s1.chID from @SET_OF_AC_NR s1)
					and kt.userbk_nr=@F_BANK_ID
					and (kt.aufheb_dat is null or kt.aufheb_dat<coalesce(kt.eroeff_dat,convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)) or cast(kt.aufheb_dat as date)>@cur_date))
			select 
				@st1 = cast(STUFF((SELECT ', ' + s1.edit_kt_nr FROM s s1 WHERE s1.userbk_nr = s0.userbk_nr FOR XML PATH('')), 1, LEN(', '), '') as varchar(4000))
			from 
				s s0
			group by s0.userbk_nr;
			set @str = @str + 'In a ' + FAF.GoAML_get_paramS(@F_BANK_ID,116,1) + ' report should be only closed accounts, but acconts ' + @st1 + ' are open; ';
		end;
		set @kk = NULL;
	end;

	--Error processing
	if len(@str)>0
		set @xml_r= (select @str as "mros_report_error" FOR XML PATH (''),type, ELEMENTS);
	else
	begin
		--input dates checking
		set @d1 = @DATE_FROM;
		if @d1 is NULL or @d1<cast(FAF.GoAML_get_paramS(@F_BANK_ID,115,1) as integer) or @d1>cast(convert(varchar,@cur_date,112) as int) 
			set @d1 = cast(FAF.GoAML_get_paramS(@F_BANK_ID,115,1) as integer);
		set @d2 = @DATE_TO;
		if @d2 is NULL or @d1>@d2
			set @d2 = cast(FAF.GoAML_get_paramS(@F_BANK_ID,115,2) as integer);
		set @dd1 = convert(date,cast(@d1 as varchar),112);
		set @dd2 = convert(date,cast(@d2 as varchar),112);

		--N_TRANS_LIMIT checking
		set @Ntrans = @max_items;
		if @N_TRANS_LIMIT>0 and @N_TRANS_LIMIT<@Ntrans
			set @Ntrans = @N_TRANS_LIMIT;

		--amount limit checking
		set @am_lim = @LOW_AMOUNT;
		if @am_lim is NULL or @am_lim<0.01
			set @am_lim = 0.01;

		set @n_ct = (select count(*) from @SET_OF_BUCH_CAT);
		if @n_ct>0
		begin
			insert into @SET_OF_BUCH_CAT1
			select chID1,
				case
					when tst>=3 then 'all'
					when tst=2 then 'credit'
					when tst=1 then 'debit'
				end as chID2
			from 
				(select chID1,sum(tst) as tst from
					(select distinct chID1,
						case 
							when lower(chID2)='debit' then 1
							when lower(chID2)='credit' then 2
							when lower(chID2)='all' then 4
							else 0
						end as tst
					from @SET_OF_BUCH_CAT) s0
				group by chID1) s1
			where tst>0;
		end;
		set @n_ct = (select count(*) from @SET_OF_BUCH_CAT1);

		--list of account_id
		set @nn_a = (select count(*) from @SET_OF_AC_NR);
		if @nn_a>0
			begin
				insert into @SET_OF_AC_ID
				select kt_lnr as nID
				from 
					FAF.FA_MROS_KT_STAMM kt
				where kt.edit_kt_nr in (select chID from @SET_OF_AC_NR)
					and kt.userbk_nr=@F_BANK_ID;
				if FAF.GoAML_get_paramS(@F_BANK_ID,133,1)='ok'
				begin
					select @N_BR = count(*) from
						(select distinct kd_lnr from FAF.FA_MROS_KT_STAMM kt
						where kt.edit_kt_nr in (select chID from @SET_OF_AC_NR) and kt.userbk_nr=@F_BANK_ID) s;
				end;
			end;
		else
			begin
				set @nn_c = (select count(*) from @SET_OF_CL_NR);
				if @nn_c>0
					begin
						insert into @SET_OF_AC_ID
						select kt_lnr as nID
						from 
							FAF.FA_MROS_KD_STAMM kd
						join
							FAF.FA_MROS_KT_STAMM kt
						on kt.userbk_nr=kd.userbk_nr and kt.kd_lnr=kd.kd_lnr
						where kd.edit_kd_nr in (select chID from @SET_OF_CL_NR) and kd.userbk_nr=@F_BANK_ID;

						if FAF.GoAML_get_paramS(@F_BANK_ID,133,1)='ok'
						begin
							select @N_BR = count(*) from
								(select distinct kd_lnr from FAF.FA_MROS_KD_STAMM kd
								where kd.edit_kd_nr in (select chID from @SET_OF_CL_NR) and kd.userbk_nr=@F_BANK_ID) s;
						end;
					end;
			end;

		set @nn = (select count(*) from @SET_OF_AC_ID);
		if @nn>0
		begin
			--payments
			if @n_ct>0
				begin
					insert into @pmnt_id
					select top (@Ntrans)
						zv_zlg_sys_lnr
						,kt_lnr
						,kt_lnr_b
						,zv_adr_eff_lnr
						,auf_zv_adr_eff_lnr
						,om_text_lnr
						,case
							when zv_eing_ausg_cd='E' and auf_zv_adr_eff_lnr is not NULL then 1
							when zv_eing_ausg_cd='A' and zv_adr_eff_lnr is not NULL then 4
							when (zv_eing_ausg_cd='E' and auf_zv_adr_eff_lnr is NULL and sh_cd=1) or (zv_eing_ausg_cd='A' and zv_adr_eff_lnr is NULL and sh_cd=1) then 2
							when (zv_eing_ausg_cd='E' and auf_zv_adr_eff_lnr is NULL and sh_cd=2) or (zv_eing_ausg_cd='A' and zv_adr_eff_lnr is NULL and sh_cd=2) then 3
						end,
						case 
							when FAF.GoAML_char_process(esr_ref_nr,NULL,0) is not NULL
							then FAF.GoAML_char_process(replace(esr_ref_nr,' ',''),NULL,27)
							when gf_nr=211 
							then 'Post'
							else NULL
						end,
						acc_psn_flag,
						sh_cd,
						kt_id_orig,
						key_20,key_30,key_40,key_50,key_60,key_70,key_80,key_90,key_100
					from
						(select 
							pm.zv_zlg_sys_lnr,pm.ausf_dt_ist,pm.kt_lnr,pm.kt_lnr_b,pm.zv_adr_eff_lnr,
							pm.auf_zv_adr_eff_lnr,pm.om_text_lnr,pm.zv_eing_ausg_cd,pm.sh_cd,pm.esr_ref_nr,
							pm.gf_nr,pm.kt_id_orig,pm.betrag_bwrg,
							-- client <--> account condition
							case
								when cd_nr.userbk_nr is not NULL then 1
								when pm.trak_cd_e like 'DTB%' then 1
								when pm.trak_cd_e like 'KTB%' then 1
								when lower(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end) in ('card_operation','card_purchase') then 1
								when pm.om_auf_empf_cd in ('PE','PG') then 1
								else 0
							end as acc_psn_flag,
							case when (pm.zv_eing_ausg_cd='E' and pm.auf_zv_adr_eff_lnr is NULL) or (pm.zv_eing_ausg_cd='A' and pm.zv_adr_eff_lnr is NULL) then 1 else 0 end as key_20,
							case when pm.gf_nr=211 then 1 else 0 end as key_30,
							case when pm.om_auf_empf_cd in ('PE','PG') then 1 else 0 end as key_40,
							case when pm.trak_cd_e like 'DTB%' or pm.trak_cd_e like 'KTB%' then 1 else 0 end as key_50,
							case when pm.om_auf_empf_cd = 'TG' then 1 else 0 end as key_60,
							case when pm.om_auf_empf_cd = 'TB' then 1 else 0 end as key_70,
							case when FAF.GoAML_char_process(pm.esr_ref_nr,NULL,0) is not NULL then 1 else 0 end as key_80,
							case when lower(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end) in ('card_operation','card_purchase') then 1 else 0 end as key_90,
							case when cd_nr.userbk_nr is not NULL then 1 else 0 end as key_100
						from
							FAF.FA_MROS_ZV_ZLG_T pm
						--acceptable transaction types only
						join
							FAF.FA_MROS_REFERENCE_TABLE tr_tp
						on pm.buchtyp_cd=tr_tp.Finnova_value and tr_tp.userbk_nr=pm.userbk_nr and tr_tp.ref_type=10 and tr_tp.activ_MROS=1
						--acceptable trak_ref types only
						join
							FAF.FA_MROS_REFERENCE_TABLE ref_tp
						on pm.trak_ref_typ=ref_tp.Finnova_value and ref_tp.userbk_nr=pm.userbk_nr and ref_tp.ref_type=11 and ref_tp.activ_MROS=1
						--account - person payments
						left join
							FAF.FA_MROS_REFERENCE_TABLE cd_nr
						on pm.card_art_nr=cd_nr.Finnova_value and cd_nr.userbk_nr=pm.userbk_nr and cd_nr.ref_type=22 and cd_nr.special_mark='BANKCARD'
						left join
							FAF.FA_MROS_TRX_CATEGORIES_DET cat
						on cat.userbk_nr=pm.userbk_nr and cat.buchtyp_cd=pm.buchtyp_cd and cat.trak_ref_typ=pm.trak_ref_typ and cat.gf_nr=pm.gf_nr
						left join
							@SET_OF_BUCH_CAT1 sc
						on sc.chID1=cat.category_group
						left join
							@SET_OF_BUCH_CAT1 sc1
						on sc1.chID1=tr_tp.category_group
						where pm.userbk_nr=@F_BANK_ID
							and pm.kt_lnr in (select nID from @SET_OF_AC_ID)
							and pm.ausf_dt_ist>=@dd1
							and pm.ausf_dt_ist<=@dd2
							and abs(pm.betrag_bwrg)>=@am_lim
							and (sc.chID1 is not NULL 
								and (pm.sh_cd=1 and lower(sc.chID2) in ('credit','all') and isnull(@CREDIT,1)=1
									or pm.sh_cd=2 and lower(sc.chID2) in ('debit','all') and isnull(@DEBIT,1)=1)
							or sc1.chID1 is not NULL 
								and (pm.sh_cd=1 and lower(sc1.chID2) in ('credit','all') and isnull(@CREDIT,1)=1
									or pm.sh_cd=2 and lower(sc1.chID2) in ('debit','all') and isnull(@DEBIT,1)=1))
						union
						select 
							pm.zv_zlg_sys_lnr,pm.ausf_dt_ist,pm.kt_lnr,pm.kt_lnr_b,pm.zv_adr_eff_lnr,
							pm.auf_zv_adr_eff_lnr,pm.om_text_lnr,pm.zv_eing_ausg_cd,pm.sh_cd,pm.esr_ref_nr,
							pm.gf_nr,pm.kt_id_orig,pm.betrag_bwrg,
							-- client <--> account condition
							case
								when cd_nr.userbk_nr is not NULL then 1
								when pm.trak_cd_e like 'DTB%' then 1
								when pm.trak_cd_e like 'KTB%' then 1
								when lower(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end) in ('card_operation','card_purchase') then 1
								when pm.om_auf_empf_cd in ('PE','PG') then 1
								else 0
							end as acc_psn_flag,
							case when (pm.zv_eing_ausg_cd='E' and pm.auf_zv_adr_eff_lnr is NULL) or (pm.zv_eing_ausg_cd='A' and pm.zv_adr_eff_lnr is NULL) then 1 else 0 end as key_20,
							case when pm.gf_nr=211 then 1 else 0 end as key_30,
							case when pm.om_auf_empf_cd in ('PE','PG') then 1 else 0 end as key_40,
							case when pm.trak_cd_e like 'DTB%' or pm.trak_cd_e like 'KTB%' then 1 else 0 end as key_50,
							case when pm.om_auf_empf_cd = 'TG' then 1 else 0 end as key_60,
							case when pm.om_auf_empf_cd = 'TB' then 1 else 0 end as key_70,
							case when FAF.GoAML_char_process(pm.esr_ref_nr,NULL,0) is not NULL then 1 else 0 end as key_80,
							case when lower(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end) in ('card_operation','card_purchase') then 1 else 0 end as key_90,
							case when cd_nr.userbk_nr is not NULL then 1 else 0 end as key_100
						from
							FAF.FA_MROS_ZV_ZLG_T pm
						--acceptable transaction types only
						join
							FAF.FA_MROS_REFERENCE_TABLE tr_tp
						on pm.buchtyp_cd=tr_tp.Finnova_value and tr_tp.userbk_nr=pm.userbk_nr and tr_tp.ref_type=10 and tr_tp.activ_MROS=1
						--acceptable trak_ref types only
						join
							FAF.FA_MROS_REFERENCE_TABLE ref_tp
						on pm.trak_ref_typ=ref_tp.Finnova_value and ref_tp.userbk_nr=pm.userbk_nr and ref_tp.ref_type=11 and ref_tp.activ_MROS=1
						--account - person payments
						left join
							FAF.FA_MROS_REFERENCE_TABLE cd_nr
						on pm.card_art_nr=cd_nr.Finnova_value and cd_nr.userbk_nr=pm.userbk_nr and cd_nr.ref_type=22 and cd_nr.special_mark='BANKCARD'
						left join
							FAF.FA_MROS_TRX_CATEGORIES_DET cat
						on cat.userbk_nr=pm.userbk_nr and cat.buchtyp_cd=pm.buchtyp_cd and cat.trak_ref_typ=pm.trak_ref_typ and cat.gf_nr=pm.gf_nr
						left join
							@SET_OF_BUCH_CAT1 sc
						on sc.chID1=cat.category_group
						left join
							@SET_OF_BUCH_CAT1 sc1
						on sc1.chID1=tr_tp.category_group
						where pm.userbk_nr=@F_BANK_ID
							and pm.kt_lnr_b in (select nID from @SET_OF_AC_ID)
							and pm.ausf_dt_ist>=@dd1
							and pm.ausf_dt_ist<=@dd2
							and abs(pm.betrag_bwrg)>=@am_lim
							and (sc.chID1 is not NULL 
								and (pm.sh_cd=2 and lower(sc.chID2) in ('credit','all') and isnull(@CREDIT,1)=1
								or pm.sh_cd=1 and lower(sc.chID2) in ('debit','all') and isnull(@DEBIT,1)=1)
							or sc1.chID1 is not NULL 
								and (pm.sh_cd=2 and lower(sc1.chID2) in ('credit','all') and isnull(@CREDIT,1)=1
								or pm.sh_cd=1 and lower(sc1.chID2) in ('debit','all') and isnull(@DEBIT,1)=1))
						) s
					order by s.betrag_bwrg desc,s.ausf_dt_ist desc,s.zv_zlg_sys_lnr desc;
				end;
			else
				begin
					insert into @pmnt_id
					select top (@Ntrans)
						zv_zlg_sys_lnr
						,kt_lnr
						,kt_lnr_b
						,zv_adr_eff_lnr
						,auf_zv_adr_eff_lnr
						,om_text_lnr
						,case
							when zv_eing_ausg_cd='E' and auf_zv_adr_eff_lnr is not NULL then 1
							when zv_eing_ausg_cd='A' and zv_adr_eff_lnr is not NULL then 4
							when (zv_eing_ausg_cd='E' and auf_zv_adr_eff_lnr is NULL and sh_cd=1) or (zv_eing_ausg_cd='A' and zv_adr_eff_lnr is NULL and sh_cd=1) then 2
							when (zv_eing_ausg_cd='E' and auf_zv_adr_eff_lnr is NULL and sh_cd=2) or (zv_eing_ausg_cd='A' and zv_adr_eff_lnr is NULL and sh_cd=2) then 3
						end,
						case 
							when FAF.GoAML_char_process(esr_ref_nr,NULL,0) is not NULL
							then FAF.GoAML_char_process(replace(esr_ref_nr,' ',''),NULL,27)
							when gf_nr=211 
							then 'Post'
							else NULL
						end,
						acc_psn_flag,
						sh_cd,
						kt_id_orig,
						key_20,key_30,key_40,key_50,key_60,key_70,key_80,key_90,key_100
					from
						(select
							pm.zv_zlg_sys_lnr,pm.ausf_dt_ist,pm.kt_lnr,pm.kt_lnr_b,pm.zv_adr_eff_lnr,
							pm.auf_zv_adr_eff_lnr,pm.om_text_lnr,pm.zv_eing_ausg_cd,pm.sh_cd,pm.esr_ref_nr,
							pm.gf_nr,pm.kt_id_orig,pm.betrag_bwrg,
							-- client <--> account condition
							case
								when cd_nr.userbk_nr is not NULL then 1
								when pm.trak_cd_e like 'DTB%' then 1
								when pm.trak_cd_e like 'KTB%' then 1
								when lower(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end) in ('card_operation','card_purchase') then 1
								when pm.om_auf_empf_cd in ('PE','PG') then 1
								else 0
							end as acc_psn_flag,
							case when (pm.zv_eing_ausg_cd='E' and pm.auf_zv_adr_eff_lnr is NULL) or (pm.zv_eing_ausg_cd='A' and pm.zv_adr_eff_lnr is NULL) then 1 else 0 end as key_20,
							case when pm.gf_nr=211 then 1 else 0 end as key_30,
							case when pm.om_auf_empf_cd in ('PE','PG') then 1 else 0 end as key_40,
							case when pm.trak_cd_e like 'DTB%' or pm.trak_cd_e like 'KTB%' then 1 else 0 end as key_50,
							case when pm.om_auf_empf_cd = 'TG' then 1 else 0 end as key_60,
							case when pm.om_auf_empf_cd = 'TB' then 1 else 0 end as key_70,
							case when FAF.GoAML_char_process(pm.esr_ref_nr,NULL,0) is not NULL then 1 else 0 end as key_80,
							case when lower(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end) in ('card_operation','card_purchase') then 1 else 0 end as key_90,
							case when cd_nr.userbk_nr is not NULL then 1 else 0 end as key_100
						from
							FAF.FA_MROS_ZV_ZLG_T pm
						--acceptable transaction types only
						join
							FAF.FA_MROS_REFERENCE_TABLE tr_tp
						on pm.buchtyp_cd=tr_tp.Finnova_value and tr_tp.userbk_nr=pm.userbk_nr and tr_tp.ref_type=10 and tr_tp.activ_MROS=1
						--acceptable trak_ref types only
						join
							FAF.FA_MROS_REFERENCE_TABLE ref_tp
						on pm.trak_ref_typ=ref_tp.Finnova_value and ref_tp.userbk_nr=pm.userbk_nr and ref_tp.ref_type=11 and ref_tp.activ_MROS=1
						--account - person payments
						left join
							FAF.FA_MROS_REFERENCE_TABLE cd_nr
						on pm.card_art_nr=cd_nr.Finnova_value and cd_nr.userbk_nr=pm.userbk_nr and cd_nr.ref_type=22 and cd_nr.special_mark='BANKCARD'
						left join
							FAF.FA_MROS_TRX_CATEGORIES_DET cat
						on cat.userbk_nr=pm.userbk_nr and cat.buchtyp_cd=pm.buchtyp_cd and cat.trak_ref_typ=pm.trak_ref_typ and cat.gf_nr=pm.gf_nr
						where pm.userbk_nr=@F_BANK_ID
							and pm.kt_lnr in (select nID from @SET_OF_AC_ID)
							and pm.ausf_dt_ist>=@dd1
							and pm.ausf_dt_ist<=@dd2
							and abs(pm.betrag_bwrg)>=@am_lim
							and (pm.sh_cd=1 and isnull(@CREDIT,1)=1 or pm.sh_cd=2 and isnull(@DEBIT,1)=1)
						union
						select
							pm.zv_zlg_sys_lnr,pm.ausf_dt_ist,pm.kt_lnr,pm.kt_lnr_b,pm.zv_adr_eff_lnr,
							pm.auf_zv_adr_eff_lnr,pm.om_text_lnr,pm.zv_eing_ausg_cd,pm.sh_cd,pm.esr_ref_nr,
							pm.gf_nr,pm.kt_id_orig,pm.betrag_bwrg,
							-- client <--> account condition
							case
								when cd_nr.userbk_nr is not NULL then 1
								when pm.trak_cd_e like 'DTB%' then 1
								when pm.trak_cd_e like 'KTB%' then 1
								when lower(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end) in ('card_operation','card_purchase') then 1
								when pm.om_auf_empf_cd in ('PE','PG') then 1
								else 0
							end as acc_psn_flag,
							case when (pm.zv_eing_ausg_cd='E' and pm.auf_zv_adr_eff_lnr is NULL) or (pm.zv_eing_ausg_cd='A' and pm.zv_adr_eff_lnr is NULL) then 1 else 0 end as key_20,
							case when pm.gf_nr=211 then 1 else 0 end as key_30,
							case when pm.om_auf_empf_cd in ('PE','PG') then 1 else 0 end as key_40,
							case when pm.trak_cd_e like 'DTB%' or pm.trak_cd_e like 'KTB%' then 1 else 0 end as key_50,
							case when pm.om_auf_empf_cd = 'TG' then 1 else 0 end as key_60,
							case when pm.om_auf_empf_cd = 'TB' then 1 else 0 end as key_70,
							case when FAF.GoAML_char_process(pm.esr_ref_nr,NULL,0) is not NULL then 1 else 0 end as key_80,
							case when lower(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end) in ('card_operation','card_purchase') then 1 else 0 end as key_90,
							case when cd_nr.userbk_nr is not NULL then 1 else 0 end as key_100
						from
							FAF.FA_MROS_ZV_ZLG_T pm
						--acceptable transaction types only
						join
							FAF.FA_MROS_REFERENCE_TABLE tr_tp
						on pm.buchtyp_cd=tr_tp.Finnova_value and tr_tp.userbk_nr=pm.userbk_nr and tr_tp.ref_type=10 and tr_tp.activ_MROS=1
						--acceptable trak_ref types only
						join
							FAF.FA_MROS_REFERENCE_TABLE ref_tp
						on pm.trak_ref_typ=ref_tp.Finnova_value and ref_tp.userbk_nr=pm.userbk_nr and ref_tp.ref_type=11 and ref_tp.activ_MROS=1
						--account - person payments
						left join
							FAF.FA_MROS_REFERENCE_TABLE cd_nr
						on pm.card_art_nr=cd_nr.Finnova_value and cd_nr.userbk_nr=pm.userbk_nr and cd_nr.ref_type=22 and cd_nr.special_mark='BANKCARD'
						left join
							FAF.FA_MROS_TRX_CATEGORIES_DET cat
						on cat.userbk_nr=pm.userbk_nr and cat.buchtyp_cd=pm.buchtyp_cd and cat.trak_ref_typ=pm.trak_ref_typ and cat.gf_nr=pm.gf_nr
						where pm.userbk_nr=@F_BANK_ID
							and pm.kt_lnr_b in (select nID from @SET_OF_AC_ID)
							and pm.ausf_dt_ist>=@dd1
							and pm.ausf_dt_ist<=@dd2
							and abs(pm.betrag_bwrg)>=@am_lim
							and (pm.sh_cd=2 and isnull(@CREDIT,1)=1 or pm.sh_cd=1 and isnull(@DEBIT,1)=1)
						) s
					order by s.betrag_bwrg desc,s.ausf_dt_ist desc,s.zv_zlg_sys_lnr desc;
				end;
			set @nn_p = (select count(*) from @pmnt_id);

			if @nn_p<@Ntrans
			begin
				--non-payments
				if @n_ct>0
					begin
						insert into @trans_id
						select top (@Ntrans-@nn_p)
							KT_BUCH_LNR as tr_id,
							kt_lnr as ac_id,
							-- client <--> account condition
							case
								when lower(special_mark) in ('withdrawal','deposit','card_operation','card_purchase','cash_operation')
								then 100
								else 0
							end +
							case
								when sh_cd = 'C' then 10
								when sh_cd = 'D' then 0
							end +
							case
								when adr_zeile_1_ez is not NULL
								then 1
								else 0
							end as tp,
							case when lower(special_mark) = 'cash_operation' then 1 else 0 end as key_510,
							case when lower(special_mark) = 'card_operation' then 1 else 0 end as key_520,
							case when lower(special_mark) = 'deposit' then 1 else 0 end as key_530,
							case when lower(special_mark) = 'withdrawal' then 1 else 0 end as key_540,
							case when lower(special_mark) = 'card_purchase' then 1 else 0 end as key_550,
							case when adr_zeile_1_ez is not null then 1 else 0 end as key_560,
							case when special_mark is not NULL and lower(special_mark) not in ('withdrawal','deposit','card_operation','card_purchase','cash_operation') then 1 else 0 end as key_570
						from
							(select
								tr.kt_buch_lnr,tr.kt_lnr,tr.betrag_bwrg,
								case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end as special_mark,
								tr.buch_dat_a_1,tr.sh_cd,tr.adr_zeile_1_ez
							from
								FAF.FA_MROS_KT_BUCH_EA tr
							--acceptable transaction types only
							join
								FAF.FA_MROS_REFERENCE_TABLE tr_tp
							on tr.buchtyp_cd=tr_tp.Finnova_value and tr_tp.userbk_nr=tr.userbk_nr and tr_tp.ref_type=10 and tr_tp.activ_MROS=1
							--acceptable trak_ref types only
							join
								FAF.FA_MROS_REFERENCE_TABLE ref_tp
							on tr.trak_ref_typ=ref_tp.Finnova_value and ref_tp.userbk_nr=tr.userbk_nr and ref_tp.ref_type=11 and ref_tp.activ_MROS=1
							left join
								FAF.FA_MROS_TRX_CATEGORIES_DET cat
							on cat.userbk_nr=tr.userbk_nr and cat.buchtyp_cd=tr.buchtyp_cd and cat.trak_ref_typ=tr.trak_ref_typ and cat.gf_nr is NULL
							left join
								@SET_OF_BUCH_CAT1 sc
							on sc.chID1=cat.category_group
							left join
								@SET_OF_BUCH_CAT1 sc1
							on sc1.chID1=tr_tp.category_group
							where tr.kt_lnr in (select nID from @SET_OF_AC_ID)
								and tr.userbk_nr=@F_BANK_ID
								and tr.buch_dat_a_1>=@d1
								and tr.buch_dat_a_1<=@d2
								and abs(tr.BETRAG_BWRG)>=@am_lim
								and (sc.chID1 is not NULL
									and (tr.sh_cd='D' and lower(sc.chID2) in ('debit','all') and isnull(@DEBIT,1)=1
									or tr.sh_cd='C' and lower(sc.chID2) in ('credit','all') and isnull(@CREDIT,1)=1)
								or sc1.chID1 is not NULL
									and (tr.sh_cd='D' and lower(sc1.chID2) in ('debit','all') and isnull(@DEBIT,1)=1
									or tr.sh_cd='C' and lower(sc1.chID2) in ('credit','all') and isnull(@CREDIT,1)=1))
							) s
						order by s.betrag_bwrg desc,s.buch_dat_a_1 desc,s.kt_buch_lnr desc;
					end;
				else
					begin
						insert into @trans_id
						select top (@Ntrans-@nn_p)
							KT_BUCH_LNR as tr_id,
							kt_lnr as ac_id,
							-- client <--> account condition
							case
								when lower(special_mark) in ('withdrawal','deposit','card_operation','card_purchase','cash_operation')
								then 100
								else 0
							end +
							case
								when sh_cd = 'C' then 10
								when sh_cd = 'D' then 0
							end +
							case
								when adr_zeile_1_ez is not NULL
								then 1
								else 0
							end as tp,
							case when lower(special_mark) = 'cash_operation' then 1 else 0 end as key_510,
							case when lower(special_mark) = 'card_operation' then 1 else 0 end as key_520,
							case when lower(special_mark) = 'deposit' then 1 else 0 end as key_530,
							case when lower(special_mark) = 'withdrawal' then 1 else 0 end as key_540,
							case when lower(special_mark) = 'card_purchase' then 1 else 0 end as key_550,
							case when adr_zeile_1_ez is not null then 1 else 0 end as key_560,
							case when special_mark is not NULL and lower(special_mark) not in ('withdrawal','deposit','card_operation','card_purchase','cash_operation') then 1 else 0 end as key_570
						from
							(select
								tr.kt_buch_lnr,tr.kt_lnr,tr.betrag_bwrg,
								case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end as special_mark,
								tr.buch_dat_a_1,tr.sh_cd,tr.adr_zeile_1_ez
							from
								FAF.FA_MROS_KT_BUCH_EA tr
							--acceptable transaction types only
							join
								FAF.FA_MROS_REFERENCE_TABLE tr_tp
							on tr.buchtyp_cd=tr_tp.Finnova_value and tr_tp.userbk_nr=tr.userbk_nr and tr_tp.ref_type=10 and tr_tp.activ_MROS=1
							--acceptable trak_ref types only
							join
								FAF.FA_MROS_REFERENCE_TABLE ref_tp
							on tr.trak_ref_typ=ref_tp.Finnova_value and ref_tp.userbk_nr=tr.userbk_nr and ref_tp.ref_type=11 and ref_tp.activ_MROS=1
							left join
								FAF.FA_MROS_TRX_CATEGORIES_DET cat
							on cat.userbk_nr=tr.userbk_nr and cat.buchtyp_cd=tr.buchtyp_cd and cat.trak_ref_typ=tr.trak_ref_typ and cat.gf_nr is NULL
							where tr.kt_lnr in (select nID from @SET_OF_AC_ID)
								and tr.userbk_nr=@F_BANK_ID
								and tr.buch_dat_a_1>=@d1
								and tr.buch_dat_a_1<=@d2
								and abs(tr.BETRAG_BWRG)>=@am_lim
								and (tr.SH_CD = 'D' and isnull(@DEBIT,1)=1 or tr.SH_CD = 'C' and isnull(@CREDIT,1)=1)
							) s
						order by s.betrag_bwrg desc,s.buch_dat_a_1 desc,s.kt_buch_lnr desc;
					end;
			end;
			set @nn_t = (select count(*) from @trans_id);
		
			if @nn_p>0 or @nn_t>0
			begin
				--list of accounts
				if @nn_a>0
					begin
						with 
							pm0 as 
								(select pm.a1_kt,pm.a2_kt,pm.tp,
									case 
										when pm.ap_flag>0
										then
											case
												when kd1.bank_flag=1 and kd2.bank_flag=0 then 1
												when kd1.bank_flag=0 and kd2.bank_flag=1 then 2
												else 0
											end
										else 0
									end as ap_flag 
								from 
									@pmnt_id pm
								left join
									FAF.FA_MROS_KT_STAMM kt1
								on kt1.userbk_nr=@F_BANK_ID and kt1.kt_lnr=pm.a1_kt
								left join
									FAF.FA_MROS_KD_STAMM kd1
								on kd1.userbk_nr=@F_BANK_ID and kd1.kd_lnr=kt1.kd_lnr and kd1.partner_flag=0
								left join
									FAF.FA_MROS_KT_STAMM kt2
								on kt2.userbk_nr=@F_BANK_ID and kt2.kt_lnr=pm.a2_kt
								left join
									FAF.FA_MROS_KD_STAMM kd2
								on kd2.userbk_nr=@F_BANK_ID and kd2.kd_lnr=kt2.kd_lnr and kd2.partner_flag=0)
						insert into @acnt_id
						select t.nID1,t.nID2+(case when kt.userbk_nr is not NULL then 1 else 0 end)*2 as nID2
						from
							(select nID1,max(nID2) as nID2 from
								(select a1_kt as nID1,
									case when ap_flag>0 then 1 else 0 end as nID2 
								from pm0 where ap_flag<>1
								union all
								select a2_kt as nID1,
									case when ap_flag>0 then 1 else 0 end as nID2 
								from pm0 where (tp<4 and tp>1 and ap_flag=0) or ap_flag=1
								union all
								select ac_id as nID1, case when tp>=100 then 1 else 0 end as nID2 from @trans_id) s
							group by nID1) t
						left join
							FAF.FA_MROS_KT_STAMM kt
						on kt.userbk_nr=@F_BANK_ID and kt.kt_lnr=t.nID1 and kt.edit_kt_nr in (select chID from @SET_OF_AC_NR);
					end;
				else
					begin
						with 
							pm0 as 
								(select pm.a1_kt,pm.a2_kt,pm.tp,
									case 
										when pm.ap_flag>0
										then
											case
												when kd1.bank_flag=1 and kd2.bank_flag=0 then 1
												when kd1.bank_flag=0 and kd2.bank_flag=1 then 2
												else 0
											end
										else 0
									end as ap_flag 
								from 
									@pmnt_id pm
								left join
									FAF.FA_MROS_KT_STAMM kt1
								on kt1.userbk_nr=@F_BANK_ID and kt1.kt_lnr=pm.a1_kt
								left join
									FAF.FA_MROS_KD_STAMM kd1
								on kd1.userbk_nr=@F_BANK_ID and kd1.kd_lnr=kt1.kd_lnr and kd1.partner_flag=0
								left join
									FAF.FA_MROS_KT_STAMM kt2
								on kt2.userbk_nr=@F_BANK_ID and kt2.kt_lnr=pm.a2_kt
								left join
									FAF.FA_MROS_KD_STAMM kd2
								on kd2.userbk_nr=@F_BANK_ID and kd2.kd_lnr=kt2.kd_lnr and kd2.partner_flag=0)
						insert into @acnt_id
						select t.nID1,t.nID2+(case when kd.userbk_nr is not NULL then 1 else 0 end)*2 as nID2
						from
							(select nID1,max(nID2) as nID2 from
								(select a1_kt as nID1,
									case when ap_flag>0 then 1 else 0 end as nID2 
								from pm0 where ap_flag<>1
								union all
								select a2_kt as nID1,
									case when ap_flag>0 then 1 else 0 end as nID2 
								from pm0 where (tp<4 and tp>1 and ap_flag=0) or ap_flag=1
								union all
								select ac_id as nID1, case when tp>=100 then 1 else 0 end as nID2 from @trans_id) s
							group by nID1) t
						left join
							FAF.FA_MROS_KT_STAMM kt
						on kt.userbk_nr=@F_BANK_ID and kt.kt_lnr=t.nID1
						left join
							FAF.FA_MROS_KD_STAMM kd
						on kd.userbk_nr=kt.userbk_nr and kd.kd_lnr=kt.kd_lnr and kd.partner_flag=0 and kd.edit_kd_nr in (select chID from @SET_OF_CL_NR);
					end;
				set @nn = (select count(*) from @acnt_id);

				if @nn>0
					set @xml_t = FAF.transaction_node(@F_BANK_ID,@pmnt_id,@trans_id,@acnt_id);
			end;
		end;
	
		--remain part of the report
		set @xml_r =FAF.head_part_of_report (@F_BANK_ID,@RP_NAME,@RP_SURNAME,@RP_PHONE,@RP_EMAIL,@RP_OCCUPATION,@RP_ADDRESS,@RP_CITY,@FIU_REF_NUMBER,@ENTITY_REFERENCE,@REPORT_REASON,@R_ACTION,@REPORT_INDICATORS,@xml_t,1,@REPORT_CODE,@N_BR);
	end;

	return(@xml_r);
end;
go



---------------------------------------------------------------------------------------------------



--report with several arbitrary payments
if object_id('[FAF].[set_of_payments]', 'FN') is not NULL
	drop function [FAF].[set_of_payments]; 
go
create function FAF.set_of_payments (
	@F_BANK_ID int,
	@RP_NAME varchar(100),
	@RP_SURNAME varchar(100),
	@RP_PHONE varchar(50),
	@RP_EMAIL varchar(50),
	@RP_OCCUPATION varchar(255),
	@RP_ADDRESS varchar(100),
	@RP_CITY varchar(255),
	@FIU_REF_NUMBER varchar(255),
	@ENTITY_REFERENCE varchar(255),
	@REPORT_REASON varchar(4000),
	@R_ACTION varchar(4000),
	@REPORT_INDICATORS FAF.FAF_MROS_charID_t readonly,
	@SET_OF_PAYMENT_ID FAF.FAF_MROS_numID_t readonly,
	@SET_OF_ORDER_NR FAF.FAF_MROS_charID_t readonly,
	@DF_PAYMENT_ID numeric(22,0),
	@DF_ORDER_NR varchar(50),
	@DF_DESCRIPTION varchar(4000),
	@DF_DATE datetime,
	@DF_CURRENCY varchar(4),
	@DF_AMOUNT numeric(24,4),
	@DF_DEBIT_CREDIT varchar(1),
	@DF_ACCOUNT_ID numeric(22,0),
	@DF_CUSTOMER_ID numeric(22,0),
	@DF_CUSTOMER_NR varchar(50),
	@DF_BANK_NAME varchar(255),
	@DF_SWIFT varchar(11),
	@DF_ACCOUNT varchar(50),
	@DF_COMMENTS varchar(4000),
	@REPORT_CODE varchar(255))
returns xml
as
begin
	declare @xml_t xml,
			@xml_r xml,
			@xml_cl xml,
			@xml_ac xml;
	--declare @ind FAF.FAF_MROS_charID_t;
	declare @pmnt_id FAF.FAF_MROS_PmntTpl_t; 
	declare @trans_id FAF.FAF_MROS_TrTpl_t;
	declare @acnt_id FAF.FAF_MROS_NumNum_t;
	declare @nn_p int;
	declare @nn int;
	declare @str varchar(4000);
	declare @inst_name varchar(255);
	declare @swif varchar(11);
	declare @cntr varchar(3);
	declare @DF_cntr varchar(3);
	declare @N_BR int;
	declare @cur varchar(4);
	declare @max_items int;
	declare @cur_date date;
	set @max_items = 1000;
	set @cur_date = cast(sysdatetime() as date);
	set @str = '';
	
	--indicators checking
	--insert into @ind
	--	select indicator from FAF.FA_MROS_INT_INDIZ where userbk_nr=@F_BANK_ID and indicator in (select ltrim(rtrim(chID)) from @REPORT_INDICATORS);
	--if FAF.GoAML_get_paramS(@F_BANK_ID,45,1)='ok'
	--begin
	--	if (select count(*) from @ind where right(chID,1)='M')<>1 
	--		set @str=@str + 'the report must include only one indicator with type Report_Type (M)';
	--	if (select count(*) from @ind where right(chID,1)='V')<1
	--		set @str = case when len(@str)=0 then '' else @str+', ' end + 'the report must include at least one indicator with type Predicate_Offenses (V)';
	--	if (select count(*) from @ind where right(chID,1)='G')<1
	--		set @str = case when len(@str)=0 then '' else @str+', ' end + 'the report must include at least one indicator with type Reason (G)';
	--	if len(@str)>0
	--		set @str = 'Indicators error: ' + @str + '; ';
	--end;

	--table FA_MROS_INT_PERS_CHARGE checking
	if (select count(*) from FAF.FA_MROS_INT_PERS_CHARGE where userbk_nr_home=@F_BANK_ID)=0
		set @str = @str + 'The table FAF_MROS_persons_in_charge does not have the record with userbk_nr_home = ' + cast(@F_BANK_ID as varchar) + '; ';

	--Error processing
	if len(@str)>0
		set @xml_r= (select @str as "mros_report_error" FOR XML PATH (''),type, ELEMENTS);

	--if there is no error then start the main request
	if @xml_r is NULL
	begin
		--we have the hierarchy now: check set of payments, and only if it is empty, check set of orders
		insert into @pmnt_id
		select top (@max_items)
			pm.zv_zlg_sys_lnr
			,pm.kt_lnr
			,pm.kt_lnr_b
			,pm.zv_adr_eff_lnr
			,pm.auf_zv_adr_eff_lnr
			,pm.om_text_lnr
			,case
				when pm.zv_eing_ausg_cd='E' and pm.auf_zv_adr_eff_lnr is not NULL then 1
				when pm.zv_eing_ausg_cd='A' and pm.zv_adr_eff_lnr is not NULL then 4
				when (pm.zv_eing_ausg_cd='E' and pm.auf_zv_adr_eff_lnr is NULL and pm.sh_cd=1) or (pm.zv_eing_ausg_cd='A' and pm.zv_adr_eff_lnr is NULL and pm.sh_cd=1) then 2
				when (pm.zv_eing_ausg_cd='E' and pm.auf_zv_adr_eff_lnr is NULL and pm.sh_cd=2) or (pm.zv_eing_ausg_cd='A' and pm.zv_adr_eff_lnr is NULL and pm.sh_cd=2) then 3
			end,
			case 
				when FAF.GoAML_char_process(pm.esr_ref_nr,NULL,0) is not NULL
				then FAF.GoAML_char_process(replace(pm.esr_ref_nr,' ',''),NULL,27)
				when pm.gf_nr=211 
				then 'Post'
				else NULL
			end,
			-- client <--> account condition
			case
				when cd_nr.userbk_nr is not NULL then 1
				when pm.trak_cd_e like 'DTB%' then 1
				when pm.trak_cd_e like 'KTB%' then 1
				when lower(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end) in ('card_operation','card_purchase') then 1
				when pm.om_auf_empf_cd in ('PE','PG') then 1
				else 0
			end,
			pm.sh_cd,
			pm.kt_id_orig,
			case when (pm.zv_eing_ausg_cd='E' and pm.auf_zv_adr_eff_lnr is NULL) or (pm.zv_eing_ausg_cd='A' and pm.zv_adr_eff_lnr is NULL) then 1 else 0 end as key_20,
			case when pm.gf_nr=211 then 1 else 0 end as key_30,
			case when pm.om_auf_empf_cd in ('PE','PG') then 1 else 0 end as key_40,
			case when pm.trak_cd_e like 'DTB%' or pm.trak_cd_e like 'KTB%' then 1 else 0 end as key_50,
			case when pm.om_auf_empf_cd = 'TG' then 1 else 0 end as key_60,
			case when pm.om_auf_empf_cd = 'TB' then 1 else 0 end as key_70,
			case when FAF.GoAML_char_process(pm.esr_ref_nr,NULL,0) is not NULL then 1 else 0 end as key_80,
			case when lower(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end) in ('card_operation','card_purchase') then 1 else 0 end as key_90,
			case when cd_nr.userbk_nr is not NULL then 1 else 0 end as key_100
		from
			FAF.FA_MROS_ZV_ZLG_T pm
		--acceptable transaction types only
		join
			FAF.FA_MROS_REFERENCE_TABLE tr_tp
		on pm.buchtyp_cd=tr_tp.Finnova_value and tr_tp.userbk_nr=pm.userbk_nr and tr_tp.ref_type=10 and tr_tp.activ_MROS=1
		--acceptable trak_ref types only
		join
			FAF.FA_MROS_REFERENCE_TABLE ref_tp
		on pm.trak_ref_typ=ref_tp.Finnova_value and ref_tp.userbk_nr=pm.userbk_nr and ref_tp.ref_type=11 and ref_tp.activ_MROS=1
		--account - person payments
		left join
			FAF.FA_MROS_REFERENCE_TABLE cd_nr
		on pm.card_art_nr=cd_nr.Finnova_value and cd_nr.userbk_nr=pm.userbk_nr and cd_nr.ref_type=22 and cd_nr.special_mark='BANKCARD'
		left join
			FAF.FA_MROS_TRX_CATEGORIES_DET cat
		on cat.userbk_nr=pm.userbk_nr and cat.buchtyp_cd=pm.buchtyp_cd and cat.trak_ref_typ=pm.trak_ref_typ and cat.gf_nr=pm.gf_nr
		where pm.userbk_nr=@F_BANK_ID and pm.zv_zlg_sys_lnr in (select nid from @SET_OF_PAYMENT_ID)
		order by pm.ausf_dt_ist desc;
		set @nn_p = (select count(*) from @pmnt_id);

		if FAF.GoAML_get_paramS(@F_BANK_ID,133,1)='ok' and @nn_p > 0
		begin
			select @N_BR = count(*) from
				(select kd.kd_lnr
				from 
					FAF.FA_MROS_ZV_ZLG_T zv
				join
					FAF.FA_MROS_KT_STAMM kt
				on zv.userbk_nr=kt.userbk_nr and zv.kt_lnr=kt.kt_lnr
				join
					FAF.FA_MROS_KD_STAMM kd
				on kd.userbk_nr=kt.userbk_nr and kd.kd_lnr=kt.kd_lnr and kd.bank_flag=0
				where zv.zv_zlg_sys_lnr in (select nid from @SET_OF_PAYMENT_ID)
					and zv.userbk_nr=@F_BANK_ID
				union
				select kd.kd_lnr
				from 
					FAF.FA_MROS_ZV_ZLG_T zv
				join
					FAF.FA_MROS_KT_STAMM kt
				on zv.userbk_nr=kt.userbk_nr and zv.kt_lnr_b=kt.kt_lnr
				join
					FAF.FA_MROS_KD_STAMM kd
				on kd.userbk_nr=kt.userbk_nr and kd.kd_lnr=kt.kd_lnr and kd.bank_flag=0
				where zv.zv_zlg_sys_lnr in (select nid from @SET_OF_PAYMENT_ID)
					and zv.userbk_nr=@F_BANK_ID
				) s;
		end;

		if @nn_p=0
		begin
			insert into @pmnt_id
			select top (@max_items)
				pm.zv_zlg_sys_lnr
				,pm.kt_lnr
				,pm.kt_lnr_b
				,pm.zv_adr_eff_lnr
				,pm.auf_zv_adr_eff_lnr
				,pm.om_text_lnr
				,case
					when pm.zv_eing_ausg_cd='E' and pm.auf_zv_adr_eff_lnr is not NULL then 1
					when pm.zv_eing_ausg_cd='A' and pm.zv_adr_eff_lnr is not NULL then 4
					when (pm.zv_eing_ausg_cd='E' and pm.auf_zv_adr_eff_lnr is NULL and pm.sh_cd=1) or (pm.zv_eing_ausg_cd='A' and pm.zv_adr_eff_lnr is NULL and pm.sh_cd=1) then 2
					when (pm.zv_eing_ausg_cd='E' and pm.auf_zv_adr_eff_lnr is NULL and pm.sh_cd=2) or (pm.zv_eing_ausg_cd='A' and pm.zv_adr_eff_lnr is NULL and pm.sh_cd=2) then 3
				end,
				case 
					when FAF.GoAML_char_process(pm.esr_ref_nr,NULL,0) is not NULL
					then FAF.GoAML_char_process(replace(pm.esr_ref_nr,' ',''),NULL,27)
					when pm.gf_nr=211 
					then 'Post'
					else NULL
				end,
				-- client <--> account condition
				case
					when cd_nr.userbk_nr is not NULL then 1
					when pm.trak_cd_e like 'DTB%' then 1
					when pm.trak_cd_e like 'KTB%' then 1
					when lower(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end) in ('card_operation','card_purchase') then 1
					when pm.om_auf_empf_cd in ('PE','PG') then 1
					else 0
				end,
				pm.sh_cd,
				pm.kt_id_orig,
				case when (pm.zv_eing_ausg_cd='E' and pm.auf_zv_adr_eff_lnr is NULL) or (pm.zv_eing_ausg_cd='A' and pm.zv_adr_eff_lnr is NULL) then 1 else 0 end as key_20,
				case when pm.gf_nr=211 then 1 else 0 end as key_30,
				case when pm.om_auf_empf_cd in ('PE','PG') then 1 else 0 end as key_40,
				case when pm.trak_cd_e like 'DTB%' or pm.trak_cd_e like 'KTB%' then 1 else 0 end as key_50,
				case when pm.om_auf_empf_cd = 'TG' then 1 else 0 end as key_60,
				case when pm.om_auf_empf_cd = 'TB' then 1 else 0 end as key_70,
				case when FAF.GoAML_char_process(pm.esr_ref_nr,NULL,0) is not NULL then 1 else 0 end as key_80,
				case when lower(case when cat.MROS_value is not NULL then cat.special_mark else tr_tp.special_mark end) in ('card_operation','card_purchase') then 1 else 0 end as key_90,
				case when cd_nr.userbk_nr is not NULL then 1 else 0 end as key_100
			from
				FAF.FA_MROS_ZV_ZLG_T pm
			--acceptable transaction types only
			join
				FAF.FA_MROS_REFERENCE_TABLE tr_tp
			on pm.buchtyp_cd=tr_tp.Finnova_value and tr_tp.userbk_nr=pm.userbk_nr and tr_tp.ref_type=10 and tr_tp.activ_MROS=1
			--acceptable trak_ref types only
			join
				FAF.FA_MROS_REFERENCE_TABLE ref_tp
			on pm.trak_ref_typ=ref_tp.Finnova_value and ref_tp.userbk_nr=pm.userbk_nr and ref_tp.ref_type=11 and ref_tp.activ_MROS=1
			--account - person payments
			left join
				FAF.FA_MROS_REFERENCE_TABLE cd_nr
			on pm.card_art_nr=cd_nr.Finnova_value and cd_nr.userbk_nr=pm.userbk_nr and cd_nr.ref_type=22 and cd_nr.special_mark='BANKCARD'
			left join
				FAF.FA_MROS_TRX_CATEGORIES_DET cat
			on cat.userbk_nr=pm.userbk_nr and cat.buchtyp_cd=pm.buchtyp_cd and cat.trak_ref_typ=pm.trak_ref_typ and cat.gf_nr=pm.gf_nr
			where pm.userbk_nr=@F_BANK_ID and pm.auf_id in (select chid from @SET_OF_ORDER_NR)
			order by pm.ausf_dt_ist desc;
			
			set @nn_p = (select count(*) from @pmnt_id);

			if FAF.GoAML_get_paramS(@F_BANK_ID,133,1)='ok' and @nn_p > 0
			begin
				select @N_BR = count(*) from
					(select kd.kd_lnr
					from 
						FAF.FA_MROS_ZV_ZLG_T zv
					join
						FAF.FA_MROS_KT_STAMM kt
					on zv.userbk_nr=kt.userbk_nr and zv.kt_lnr=kt.kt_lnr
					join
						FAF.FA_MROS_KD_STAMM kd
					on kd.userbk_nr=kt.userbk_nr and kd.kd_lnr=kt.kd_lnr and kd.bank_flag=0
					where zv.auf_id in (select chid from @SET_OF_ORDER_NR)
						and zv.userbk_nr=@F_BANK_ID
					union
					select kd.kd_lnr
					from 
						FAF.FA_MROS_ZV_ZLG_T zv
					join
						FAF.FA_MROS_KT_STAMM kt
					on zv.userbk_nr=kt.userbk_nr and zv.kt_lnr_b=kt.kt_lnr
					join
						FAF.FA_MROS_KD_STAMM kd
					on kd.userbk_nr=kt.userbk_nr and kd.kd_lnr=kt.kd_lnr and kd.bank_flag=0
					where zv.auf_id in (select chid from @SET_OF_ORDER_NR)
						and zv.userbk_nr=@F_BANK_ID
					) s;
			end;
		end;

		if @nn_p>0
		begin
			--list of accounts
			with 
				pm0 as 
					(select pm.a1_kt,pm.a2_kt,pm.tp,
						case 
							when pm.ap_flag>0
							then
								case
									when kd1.bank_flag=1 and kd2.bank_flag=0 then 1
									when kd1.bank_flag=0 and kd2.bank_flag=1 then 2
									else 0
								end
							else 0
						end as ap_flag 
					from 
						@pmnt_id pm
					left join
						FAF.FA_MROS_KT_STAMM kt1
					on kt1.userbk_nr=@F_BANK_ID and kt1.kt_lnr=pm.a1_kt
					left join
						FAF.FA_MROS_KD_STAMM kd1
					on kd1.userbk_nr=@F_BANK_ID and kd1.kd_lnr=kt1.kd_lnr and kd1.partner_flag=0
					left join
						FAF.FA_MROS_KT_STAMM kt2
					on kt2.userbk_nr=@F_BANK_ID and kt2.kt_lnr=pm.a2_kt
					left join
						FAF.FA_MROS_KD_STAMM kd2
					on kd2.userbk_nr=@F_BANK_ID and kd2.kd_lnr=kt2.kd_lnr and kd2.partner_flag=0)
			insert into @acnt_id
			select nID1,max(nID2)+2 as nID2 from
				(select a1_kt as nID1,
					case when ap_flag>0 then 1 else 0 end as nID2 
				from pm0 where ap_flag<>1
				union all
				select a2_kt as nID1,
					case when ap_flag>0 then 1 else 0 end as nID2 
				from pm0 where (tp<4 and tp>1 and ap_flag=0) or ap_flag=1) s
			group by nID1;
			set @nn = (select count(*) from @acnt_id);

			if @nn>0
				set @xml_t = FAF.transaction_node(@F_BANK_ID,@pmnt_id,@trans_id,@acnt_id);
		end;

		/*
		--Direct forwarding is not supported
		*/

		--remain part of the report
		set @xml_r =FAF.head_part_of_report (@F_BANK_ID,@RP_NAME,@RP_SURNAME,@RP_PHONE,@RP_EMAIL,@RP_OCCUPATION,@RP_ADDRESS,@RP_CITY,@FIU_REF_NUMBER,@ENTITY_REFERENCE,@REPORT_REASON,@R_ACTION,@REPORT_INDICATORS,@xml_t,1,@REPORT_CODE,@N_BR);
	end;

	return(@xml_r);
end;
go


---------------------------------------------------------------------------------------------------


--SAR report (collection of accounts and clients)
if object_id('[FAF].[report_activity]', 'FN') is not NULL
	drop function [FAF].[report_activity]; 
go
create function FAF.report_activity (
	@F_BANK_ID int,
	@RP_NAME varchar(100),
	@RP_SURNAME varchar(100),
	@RP_PHONE varchar(50),
	@RP_EMAIL varchar(50),
	@RP_OCCUPATION varchar(255),
	@RP_ADDRESS varchar(100),
	@RP_CITY varchar(255),
	@FIU_REF_NUMBER varchar(255),
	@ENTITY_REFERENCE varchar(255),
	@REPORT_REASON varchar(4000),
	@R_ACTION varchar(4000),
	@REPORT_INDICATORS FAF.FAF_MROS_charID_t readonly,
	@SET_OF_ACCOUNTS FAF.FAF_MROS_IdRsn_t readonly,
	@SET_OF_CLIENTS FAF.FAF_MROS_IdRsn_t readonly,
	@REPORT_CODE varchar(255)) 
returns xml
as
begin
	declare @xml_r xml,
		@xml_cl xml,
		@xml_clt xml,
		@xml_cl1 xml,
		@xml_cl0 xml,
		@xml_cl5 xml,
		@xml_cl5t xml,
		@xml_kd xml,
		@xml_kd5 xml,
		@xml_ac xml,
		@xml_dp xml,
		@xml_de xml,
		@xml_ac_fnd xml;
	declare @ac_fnd int;
	declare @curr varchar(50);
	declare @kt_nr varchar(80);
	declare @mm int;
	declare @mm0 int;
	declare @mmm int;
	declare @mmm0 int;
	declare @nn int;
	declare @nn0 int;
	declare @ii int;
	declare @jj int;
	declare @kk int;
	declare @kkk int;
	declare @bb int;
	declare @n_id decimal(24,0);
	declare @kt_id decimal(24,0);
	declare @cl_id decimal(24,0);
	declare @auf_dt0 date;
	declare @auf_dt date;
	declare @stat int;
	declare @ACNTS FAF.FAF_MROS_NumChar_t;
	declare @CLNTS FAF.FAF_MROS_NumChar_t;
	declare @KD_KD0 FAF.FAF_MROS_numID_t;
	declare @KD_KD FAF.FAF_MROS_KdKdTpl_t;
	declare @KD_KD_ext FAF.FAF_MROS_KdKdTpl_ext_t;
	declare @KD_KD_ext1 FAF.FAF_MROS_KdKdTpl_ext_t;
	declare @dt_fr date;
	declare @dt_fr0 date;
	declare @dt_to date;
	declare @dt_fr_e date;
	declare @dt_to_e date;
	declare @tecom varchar(4000);
	declare @tpcom varchar(4000);
	declare @cl_tp int;
	declare @kd_tp int;
	declare @cl_nm  varchar(4000);
	declare @kd_nm  varchar(4000);
	declare @cl_nr varchar(80);
	declare @kd_nr varchar(80);
	declare @rol varchar(80);
	declare @comm varchar(4000);
	declare @e_rol varchar(80);
	declare @e_comm varchar(4000);
	declare @adr_txt varchar(4000);
	declare @partners varchar(4000);
	declare @st1 varchar(4000);
	declare @st2 varchar(4000);
	declare @st3 varchar(4000);
	declare @st4 varchar(4000);
	--declare @ind FAF.FAF_MROS_charID_t;
	declare @str varchar(4000);
	declare @cntr varchar(3);
	declare @swif varchar(11);
	declare @inst_name varchar(255);
	declare @s_reason varchar(4000);
	declare @N_BR int;
	declare @itms int = 0;
	declare @max_items int;
	declare @cur_date date;
	set @cur_date = cast(sysdatetime() as date);
	set @max_items = 1000;
	set @str = '';

	--indicators checking
	--insert into @ind
	--	select indicator from FAF.FA_MROS_INT_INDIZ where userbk_nr=@F_BANK_ID and indicator in (select ltrim(rtrim(chID)) from @REPORT_INDICATORS);
	--if FAF.GoAML_get_paramS(@F_BANK_ID,45,1)='ok'
	--begin
	--	if (select count(*) from @ind where right(chID,1)='M')<>1 
	--		set @str=@str + 'the report must include only one indicator with type Report_Type (M)';
	--	if (select count(*) from @ind where right(chID,1)='V')<1
	--		set @str = case when len(@str)=0 then '' else @str+', ' end + 'the report must include at least one indicator with type Predicate_Offenses (V)';
	--	if (select count(*) from @ind where right(chID,1)='G')<1
	--		set @str = case when len(@str)=0 then '' else @str+', ' end + 'the report must include at least one indicator with type Reason (G)';
	--	if len(@str)>0
	--		set @str = 'Indicators error: ' + @str + '; ';
	--end;

	--table FA_MROS_INT_PERS_CHARGE checking
	if (select count(*) from FAF.FA_MROS_INT_PERS_CHARGE where userbk_nr_home=@F_BANK_ID)=0
		set @str = @str + 'The table FAF_MROS_persons_in_charge does not have the record with userbk_nr_home = ' + cast(@F_BANK_ID as varchar) + '; ';
	
	if (select count(*) from @SET_OF_ACCOUNTS) = 0
		begin
			insert into @ACNTS
			select distinct ac.kt_lnr as nID, s.rsn
			from 
				@SET_OF_CLIENTS s
			join
				FAF.FA_MROS_KD_STAMM cl
			on cl.edit_kd_nr=s.chID and cl.userbk_nr=@F_BANK_ID and cl.partner_flag=0
			join
				FAF.FA_MROS_KT_STAMM ac
			on ac.userbk_nr=cl.userbk_nr and ac.kd_lnr=cl.kd_lnr;

			if FAF.GoAML_get_paramS(@F_BANK_ID,133,1)='ok'
			begin
				select @N_BR = count(*) from
					(select distinct kd_lnr from FAF.FA_MROS_KD_STAMM kd
					where kd.edit_kd_nr in (select chID from @SET_OF_CLIENTS) and kd.userbk_nr=@F_BANK_ID) s;
			end;
		end;
	else
		begin
			insert into @ACNTS 
			select distinct ac.kt_lnr as nID, s.rsn 
			from 
				@SET_OF_ACCOUNTS s 
			join 
				FAF.FA_MROS_KT_STAMM ac 
			on ac.userbk_nr=@F_BANK_ID and ac.edit_kt_nr=s.chID;

			if FAF.GoAML_get_paramS(@F_BANK_ID,133,1)='ok'
			begin
				select @N_BR = count(*) from
					(select distinct kd_lnr from FAF.FA_MROS_KT_STAMM kt
					where kt.edit_kt_nr in (select chID from @SET_OF_ACCOUNTS) and kt.userbk_nr=@F_BANK_ID) s;
			end;
		end;

	insert into @CLNTS 
	select distinct cl.kd_lnr as nID,s.rsn 
	from 
		@SET_OF_CLIENTS s 
	join
		FAF.FA_MROS_KD_STAMM cl 
	on cl.userbk_nr=@F_BANK_ID and cl.edit_kd_nr=s.chID and cl.partner_flag=0;

	--input sets checking
	set @nn = (select count(*) from @ACNTS);
	set @nn0 = (select count(*) from @CLNTS);
	if @nn0+@nn=0
		set @str = @str + 'At least one valid account/client number should be provided; ';

	if upper(@REPORT_CODE)=FAF.GoAML_get_paramS(@F_BANK_ID,116,2)
	begin
		set @kk = 
			(select count(*)
			from 
				FAF.FA_MROS_KT_STAMM kt
			where kt.edit_kt_nr in (select s1.chID from @SET_OF_ACCOUNTS s1)
				and kt.userbk_nr=@F_BANK_ID
				and (kt.aufheb_dat is null or kt.aufheb_dat<coalesce(kt.eroeff_dat,convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)) or cast(kt.aufheb_dat as date)>@cur_date));
		if @kk>0
		begin
			with s as
				(select kt.userbk_nr,kt.edit_kt_nr from 
					FAF.FA_MROS_KT_STAMM kt
				where kt.edit_kt_nr in (select s1.chID from @SET_OF_ACCOUNTS s1)
					and kt.userbk_nr=@F_BANK_ID
					and (kt.aufheb_dat is null or kt.aufheb_dat<coalesce(kt.eroeff_dat,convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)) or cast(kt.aufheb_dat as date)>@cur_date))
			select 
				@st1 = cast(STUFF((SELECT ', ' + s1.edit_kt_nr FROM s s1 WHERE s1.userbk_nr = s0.userbk_nr FOR XML PATH('')), 1, LEN(', '), '') as varchar(4000))
			from 
				s s0
			group by s0.userbk_nr;
			set @str = @str + 'In a ' + FAF.GoAML_get_paramS(@F_BANK_ID,116,2) + ' report should be only closed accounts, but acconts ' + @st1 + ' are open; ';
		end;
		set @kk = NULL;
	end;

	--Error processing
	if len(@str)>0
		set @xml_r= (select @str as "mros_report_error" FOR XML PATH (''),type, ELEMENTS);

	--if there is no error then start the main request
	if @xml_r is NULL
	begin
		--Bank name and swift code from the technical table
		set @inst_name = (select top 1 institution_name from FAF.FA_MROS_INT_PERS_CHARGE where userbk_nr_home=@F_BANK_ID);
		set @swif = (select top 1 swift from FAF.FA_MROS_INT_PERS_CHARGE where userbk_nr_home=@F_BANK_ID);
		set @cntr = substring(@swif,5,2);
		if (select count(*) from FAF.FA_MROS_INT_LAND where userbk_nr=@F_BANK_ID and iso_land_cd=@cntr) = 0
			set @cntr = FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__institution_country');

		--default person
		set @xml_dp = 
			(select 
				FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__gender') as "gender",
				FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name') as "first_name",
				case 
					when FAF.GoAML_get_paramS(@F_BANK_ID,44,1)='ok'
					then upper(FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__last_name'))
					else FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__last_name')
				end as "last_name",
				FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__birthdate') as "birthdate",
				FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__id_number') as "id_number",
				FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__nationality1') as "nationality1",
				case
					when FAF.GoAML_get_paramS(@F_BANK_ID,47,1)='ok'
					then
						(select
							FAF.GoAML_get_paramS(@F_BANK_ID,8,2) as "address_type",
							FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address') as "address",
							FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__city') as "city",
							FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__country_code') as "country_code"
						FOR XML PATH ('address'),type, ELEMENTS)
					else NULL
				end as "addresses",
				case
					when FAF.GoAML_get_paramS(@F_BANK_ID,48,1)='ok'
					then
						(select
							FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__type') as "type",
							FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__number') as "number",
							FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__issue_country') as "issue_country",
							FAF.GoAML_char_process(
								case
									when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
									then
										case
											when FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__type')=FAF.GoAML_get_paramS(@F_BANK_ID,6,1)
											then FAF.GoAML_get_paramS(@F_BANK_ID,6,2)
											else NULL
										end
									else NULL
								end
							,FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpi__comments')) as "comments"
						FOR XML PATH (''),type, ELEMENTS)
					else NULL
				end as "identifications/identification"
			FOR XML PATH (''),type, ELEMENTS);

		--default entity
		set @xml_de = 
			(select
				FAF.GoAML_get_paramV(@F_BANK_ID,'temc__name') as "name",
				FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_legal_form') as "incorporation_legal_form",
				case
					when FAF.GoAML_get_paramS(@F_BANK_ID,49,1)='ok'
					then
						(select
							FAF.GoAML_get_paramS(@F_BANK_ID,7,2) as "address_type",
							FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address') as "address",
							FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__city') as "city",
							FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__country_code') as "country_code"
						FOR XML PATH ('address'),type, ELEMENTS)
					else NULL
				end as "addresses",
				FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_country_code') as "incorporation_country_code",
				FAF.GoAML_get_paramV(@F_BANK_ID,'temc__tax_reg_number') as "tax_reg_number",
				case
					when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
					then
						case
							when FAF.GoAML_get_paramS(@F_BANK_ID,32,1) = FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_legal_form')
							then FAF.GoAML_get_paramS(@F_BANK_ID,32,2)
							else NULL
						end
					else NULL
				end as "comments"
			FOR XML PATH (''),type, ELEMENTS);
		
		--accounts
		set @ii = 1;
		while @ii<=@nn
		begin
			set @n_id = NULL;
			set @kt_id = NULL;
			set @cl_id = NULL;
			set @auf_dt0 = NULL;
			set @cl_tp = NULL;
			set @cl_nr = NULL;
			set @cl_nm = NULL;
			set @kd_tp = NULL;
			set @kd_nm = NULL;
			set @kd_nr = NULL;
			set @xml_cl = NULL;
			set @xml_cl1 = NULL;
			set @xml_cl0 = NULL;
			set @xml_kd = NULL;
			set @xml_kd5 = NULL;
			set @xml_ac = NULL;
			set @adr_txt = NULL;
			set @partners = NULL;
			set @xml_ac_fnd = NULL;
			set @curr = NULL;
			set @ac_fnd = NULL;
			set @kt_nr = NULL;
			set @dt_fr0 = NULL;
			set @mm = NULL;
			set @mm0 = NULL;
			set @mmm = (select count(*) from @KD_KD);
			set @mmm0 = (select count(*) from @KD_KD0);

			select 
				@kt_id = ac.nID, 
				@s_reason = ac.chID
			from @ACNTS ac where ac.id=@ii;
			select @cl_id = kt.kd_lnr,
				@auf_dt0 = kt.aufheb_dat,
				@dt_fr0 = kt.eroeff_dat
			from FAF.FA_MROS_KT_STAMM kt where kt.kt_lnr=@kt_id and kt.userbk_nr=@F_BANK_ID;

			if @cl_id is not NULL
			begin
				select
					@cl_tp = se.kd_htyp_cd,
					@cl_nm = se.kd_bez,
					@cl_nr = se.edit_kd_nr
				from 
					FAF.FA_MROS_KD_STAMM se
				where se.kd_lnr=@cl_id and se.userbk_nr=@F_BANK_ID and se.partner_flag=0;

				if @cl_tp=2
				begin
					select 
						@st1 = FAF.GoAML_char_process(vorname,'',4000),
						@st2 = FAF.GoAML_char_process(name_1,'',4000)
					from 
						FAF.FA_MROS_KD_STAMM
					where kd_lnr=@cl_id and userbk_nr=@F_BANK_ID and partner_flag=0;
					select 
						@st3 = FAF.GoAML_char_process(vorname,'',4000),
						@st4 = FAF.GoAML_char_process(name_1,'',4000)
					from 
						FAF.FA_MROS_KD_STAMM
					where kd_lnr=@cl_id and userbk_nr=@F_BANK_ID and partner_flag=1;
							
					if len(@st3+@st4)>0
						begin
							if @st2=@st4 
								set @cl_nm = @st1 + case when len(@st1)>0 then ' and ' else '' end + @st3 + ' ' + @st4;
							else
								set @cl_nm = @st1 + ' ' + @st2 + case when len(@st1+@st2)>0 then ' and ' else '' end + @st3 + ' ' + @st4;
						end;
					else
						set @cl_nm = @st1 + ' ' + @st2;
				end;

				--entity
				if @cl_tp = 3
				begin
					set @xml_cl1 =
						(select
							FAF.GoAML_char_process(se.kd_bez,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__name'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__name')) as "name",
							case
								when FAF.GoAML_get_paramS(@F_BANK_ID,70,1)='ok'
								then FAF.GoAML_char_process(se.kd_bez,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__commercial_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__commercial_name'))
								else NULL
							end as "commercial_name",
							FAF.GoAML_char_process(lf.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_legal_form'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_legal_form')) as "incorporation_legal_form",
							FAF.GoAML_char_process(
								case
									when FAF.GoAML_get_paramS(@F_BANK_ID,71,1)='ok'
									then vsb.kd_ausw_nr
									else se.uid_nr
								end
							,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_number')) as "incorporation_number",
							FAF.GoAML_char_process(se.noga_bez,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__business'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__business')) as "business",
							case
								when FAF.GoAML_get_paramS(@F_BANK_ID,43,1)='ok'
								then spp.phones
								else NULL
							end as "phones",
							case
								when FAF.GoAML_get_paramS(@F_BANK_ID,49,1)='ok'
								then
									coalesce(sea.addresses,
										(select
											FAF.GoAML_get_paramS(@F_BANK_ID,7,2) as "address_type",
											FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address') as "address",
											FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__city') as "city",
											FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__country_code') as "country_code"
										FOR XML PATH ('address'),type, ELEMENTS)
									)
								else sea.addresses
							end as "addresses",
							(select FAF.GoAML_char_process(lower(eml.email_adr),FAF.GoAML_get_paramV(@F_BANK_ID,'temc__email'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__email')) as "email"
							from FAF.FA_MROS_KD_EMAIL eml
							join FAF.FA_MROS_REFERENCE_TABLE rt
							on eml.userbk_nr=rt.userbk_nr and cast(eml.mail_art_cd as varchar)=rt.finnova_value and rt.ref_type=7 and rt.MROS_value=FAF.GoAML_get_paramS(@F_BANK_ID,121,2)
							where eml.kd_lnr=se.kd_lnr and eml.userbk_nr=se.userbk_nr and left(eml.email_adr,1)<>'@'
							FOR XML PATH (''),type, ELEMENTS) as "emails",
							(select FAF.GoAML_char_process(ema.email_adr,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__url'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__url')) as "url"
							from FAF.FA_MROS_KD_EMAIL ema 
							where ema.kd_lnr=se.kd_lnr and ema.userbk_nr=se.userbk_nr and ema.mail_art_cd=FAF.GoAML_get_paramS(@F_BANK_ID,122,2)
							FOR XML PATH (''),type, ELEMENTS) as "urls",
							FAF.GoAML_char_process(se.geburt_ort,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_state'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_state')) as "incorporation_state",
							FAF.GoAML_char_process(cntr1.iso_land_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_country_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_country_code')) as "incorporation_country_code",
							FAF.GoAML_char_process(
								case 
									when se.geburt_dat is NULL or se.geburt_dat < convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or cast(se.geburt_dat as date)>@cur_date
									then 
										case 
											when se.geburt_jahr is NULL or se.geburt_jahr<cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) or se.geburt_jahr>year(@cur_date)
											then NULL
											else cast(se.geburt_jahr as varchar) + '-01-01T00:00:00' 
										end 
									else left(convert(varchar,cast(se.geburt_dat as datetime),126),19)
								end 
							,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_date'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_date')) as "incorporation_date",
							FAF.GoAML_char_process(
								case 
									when FAF.GoAML_get_paramS(@F_BANK_ID,28,1)='ok' 
										and ((se.tod_dat >= convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) and cast(se.tod_dat as date)<=@cur_date) or (se.tod_jahr>=cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) and se.tod_jahr<=year(@cur_date)))
									then FAF.GoAML_get_paramS(@F_BANK_ID,28,2) 
									else NULL
								end 
							,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__business_closed'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__business_closed')) as "business_closed",
							FAF.GoAML_char_process(
								case 
									when se.tod_dat is NULL or se.tod_dat < convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or cast(se.tod_dat as date)>@cur_date
									then 
										case 
											when se.tod_jahr is NULL or se.tod_jahr<cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) or se.tod_jahr>year(@cur_date)
											then NULL
											else cast(se.tod_jahr as varchar) + '-01-01T00:00:00' 
										end 
									else left(convert(varchar,cast(se.tod_dat as datetime),126),19)
								end 
							,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__date_business_closed'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__date_business_closed')) as "date_business_closed",
							case
								when FAF.GoAML_get_paramS(@F_BANK_ID,67,1)='ok' and len(se.uid_nr)>1
								then FAF.GoAML_char_process(se.uid_nr,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__tax_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__tax_number'))
								else NULL
							end as "tax_number",
							FAF.GoAML_char_process(
								case
									when FAF.GoAML_get_paramS(@F_BANK_ID,104,1)='ok'
									then NULL
									else tax.MROS_value
								end
							,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__tax_reg_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__tax_reg_number')) as "tax_reg_number",
							FAF.GoAML_char_process_WR(
								case
									when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
									then
										'Client number: ' + se.edit_kd_nr + 
										case
											when cntr1.iso_land_cd is NULL and FAF.GoAML_char_process(se.land_lnr_nat_bez,NULL,0) is not NULL and FAF.GoAML_char_process(se.iso_land_code_nat,NULL,0) is not NULL
											then '; ' + char(13) + char(10) + 'Actual country is ' + FAF.GoAML_char_process(se.land_lnr_nat_bez,'',0) + ' (' + FAF.GoAML_char_process(se.iso_land_code_nat,'',0) + ')'
											else ''
										end + 
										case 
											when FAF.GoAML_get_paramS(@F_BANK_ID,32,1)=FAF.GoAML_char_process(lf.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_legal_form'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_legal_form'))
											then '; ' + char(13) + char(10) + coalesce(FAF.GoAML_char_process(lf.comments,FAF.GoAML_get_paramS(@F_BANK_ID,32,2),0),'')
											else ''
										end
									else NULL
								end
							,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__comments')) as "comments"
						from 
							FAF.FA_MROS_KD_STAMM se
						left join 
							FAF.FA_MROS_KD_ADR_XML sea
						on sea.kd_lnr=se.kd_lnr and sea.userbk_nr=se.userbk_nr
						left join
							FAF.FA_MROS_KD_EMAIL_XML spp
						on spp.kd_lnr=se.kd_lnr and spp.userbk_nr=se.userbk_nr
						left join
							FAF.FA_MROS_INT_LAND cntr1
						on cntr1.iso_land_cd=se.iso_land_code_nat and cntr1.userbk_nr=se.userbk_nr
						left join
							FAF.FA_MROS_REFERENCE_TABLE lf
						on se.userbk_nr=lf.userbk_nr and cast(se.kd_rstat_cd as varchar)=lf.finnova_value and lf.ref_type=8
						left join
							FAF.FA_MROS_REFERENCE_TABLE tax
						on se.userbk_nr=tax.userbk_nr and se.KD_SITZ_GSCH_CD=tax.finnova_value and tax.ref_type=3
						left join
							(select userbk_nr,kd_lnr,kd_ausw_nr,
								row_number() over (partition by userbk_nr,kd_lnr order by ausstell_dat desc) as rn
							from FAF.FA_MROS_KD_VSB where kd_ausw_nr is not NULL and cast(iddoc_cd as varchar)=FAF.GoAML_get_paramS(@F_BANK_ID,71,2)) vsb
						on se.userbk_nr=vsb.userbk_nr and se.kd_lnr=vsb.kd_lnr and vsb.rn=1
						where se.kd_lnr=@cl_id and se.userbk_nr=@F_BANK_ID and se.partner_flag=0
						FOR XML PATH (''),type, ELEMENTS);
					
					if FAF.GoAML_get_paramS(@F_BANK_ID,136,1)='ok' and @xml_cl1 is not NULL
						begin
							set @xml_kd5 =
								(select
									FAF.GoAML_get_paramS(@F_BANK_ID,136,2) as "account_entity_relation",
									@xml_cl1 as "entity",
									FAF.GoAML_char_process(
										convert(varchar,cast(@dt_fr0 as datetime),126)
									,FAF.GoAML_get_paramV(@F_BANK_ID,'rdr_valid_from'),FAF.GoAML_get_paramL(@F_BANK_ID,'rdr_valid_from')) as "relation_date_range/valid_from",
									FAF.GoAML_char_process(
										convert(varchar,cast(@auf_dt0 as datetime),126)
									,FAF.GoAML_get_paramV(@F_BANK_ID,'rdr_valid_to'),FAF.GoAML_get_paramL(@F_BANK_ID,'rdr_valid_to')) as "relation_date_range/valid_to"
								FOR XML PATH ('account_related_entity'),type, ELEMENTS);
						end;
					else
						begin
							if @xml_cl1 is NULL
								set @xml_cl0 = (select @xml_de as "t_entity" FOR XML PATH (''),type, ELEMENTS);
							else
								set @xml_cl0 = (select @xml_cl1 as "t_entity" FOR XML PATH (''),type, ELEMENTS);
						end;
				end;

				insert into @KD_KD0 select @cl_id as nID;

				if @cl_tp = 1
				begin
					insert into @KD_KD 
					select @cl_id as nID,
						case 
							when FAF.GoAML_get_paramS(@F_BANK_ID,26,1)='ok' 
							then FAF.GoAML_get_paramS(@F_BANK_ID,26,2) 
							else FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__role') 
						end as rl,
						NULL as cmm,NULL as ptn,@cl_tp as kdtp,@cl_nr as kdnr,@cl_nm as kdnm,NULL as dt1,NULL as dt2,NULL as e_rl,NULL as e_cmm;
				end;

				--doppelpersons of initial client
				if @cl_tp = 2
				begin
					insert into @KD_KD
					select kdkd.kd_lnr_zuw as nID,
						case 
							when FAF.GoAML_get_paramS(@F_BANK_ID,26,1)='ok' 
							then FAF.GoAML_get_paramS(@F_BANK_ID,26,2) 
							else FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__role') 
						end as rl,
						NULL as cmm,@cl_nr as ptn,kd.kd_htyp_cd as kdtp,kd.edit_kd_nr as kdnr,
						case
							when kd.kd_htyp_cd=3 then kd.kd_bez
							when kd.kd_htyp_cd=2 and kd.name_1=kd1.name_1 
							then isnull(kd.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' and ' + isnull(kd1.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd.name_1,'')
							when kd.kd_htyp_cd=2 and kd1.userbk_nr is not NULL
							then isnull(kd.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd.name_1,'') + ' and ' + isnull(kd1.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd1.name_1,'')
							else isnull(kd.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd.name_1,'')
						end as kdnm,NULL as dt1,NULL as dt2,NULL as e_rl,NULL as e_cmm
					from
						(select kdkd1.kd_lnr_zuw 
						from 
							FAF.FA_MROS_KD_KD kdkd1
						join
							FAF.FA_MROS_REFERENCE_TABLE rt_1
						on cast(kdkd1.kd_kd_verb_cd as varchar)=rt_1.Finnova_value and rt_1.userbk_nr=kdkd1.userbk_nr and rt_1.ref_type=14 and rt_1.activ_MROS=1 and rt_1.special_mark='DOPPELPERSON'
						where kdkd1.userbk_nr=@F_BANK_ID and kdkd1.kd_lnr=@cl_id and kdkd1.kd_lnr_zuw<>@cl_id
							and (kdkd1.valid_bis_dat is NULL or kdkd1.valid_bis_dat>=isnull(@auf_dt0,@cur_date))
							and (kdkd1.valid_von_dat is NULL or kdkd1.valid_von_dat<=isnull(@auf_dt0,@cur_date))
						union
						select kdkd2.kd_lnr as kd_lnr_zuw
						from 
							FAF.FA_MROS_KD_KD kdkd2
						join
							FAF.FA_MROS_REFERENCE_TABLE rt_2
						on cast(kdkd2.kd_kd_verb_cd as varchar)=rt_2.Finnova_value and rt_2.userbk_nr=kdkd2.userbk_nr and rt_2.ref_type=15 and rt_2.activ_MROS=1 and rt_2.special_mark='DOPPELPERSON'
						where kdkd2.userbk_nr=@F_BANK_ID and kdkd2.kd_lnr_zuw=@cl_id and kdkd2.kd_lnr<>@cl_id
							and (kdkd2.valid_bis_dat is NULL or kdkd2.valid_bis_dat>=isnull(@auf_dt0,@cur_date))
							and (kdkd2.valid_von_dat is NULL or kdkd2.valid_von_dat<=isnull(@auf_dt0,@cur_date))
						) kdkd
					join
						FAF.FA_MROS_KD_STAMM kd
					on kd.userbk_nr=@F_BANK_ID and kd.kd_lnr=kdkd.kd_lnr_zuw and kd.partner_flag=0
					left join
						FAF.FA_MROS_KD_STAMM kd1
					on kd1.userbk_nr=@F_BANK_ID and kd1.kd_lnr=kdkd.kd_lnr_zuw and kd1.partner_flag=1;

					set @mm = (select count(*) from @KD_KD) - isnull(@mmm,0);
					if @mm>0
					begin
						insert into @KD_KD0 select kdkd.nID from @KD_KD kdkd where kdkd.id>isnull(@mmm,0);

						set @partners = 
							(select STUFF(
								(select ', ' + coalesce(kdkd.kdnr,'') from @KD_KD kdkd where kdkd.id>isnull(@mmm,0) FOR XML PATH(''))
							, 1, LEN(', '), ''));
					end;

					set @xml_cl1 =
						(select
							FAF.GoAML_char_process(@cl_nm,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__name'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__name')) as "name",
							FAF.GoAML_char_process(
								case
									when FAF.GoAML_get_paramS(@F_BANK_ID,78,2) = 'ok'
									then lf.MROS_value
									else FAF.GoAML_get_paramS(@F_BANK_ID,54,1)
								end
							,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_legal_form'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_legal_form')) as "incorporation_legal_form",
							case
								when FAF.GoAML_get_paramS(@F_BANK_ID,43,1)='ok'
								then spp.phones
								else NULL
							end as "phones",
							case
								when FAF.GoAML_get_paramS(@F_BANK_ID,49,1)='ok'
								then
									coalesce(sea.addresses,
										(select
											FAF.GoAML_get_paramS(@F_BANK_ID,9,2) as "address_type",
											FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address') as "address",
											FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__city') as "city",
											FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__country_code') as "country_code"
										FOR XML PATH ('address'),type, ELEMENTS)
									)
								else sea.addresses
							end as "addresses",
							FAF.GoAML_char_process(cntr1.iso_land_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_country_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_country_code')) as "incorporation_country_code",
							case
								when FAF.GoAML_get_paramS(@F_BANK_ID,67,1)='ok' and len(se.uid_nr)>1
								then FAF.GoAML_char_process(se.uid_nr,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__tax_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__tax_number'))
								else NULL
							end as "tax_number",
							FAF.GoAML_char_process(FAF.GoAML_get_paramS(@F_BANK_ID,55,1),FAF.GoAML_get_paramV(@F_BANK_ID,'temc__tax_reg_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__tax_reg_number')) as "tax_reg_number",
							FAF.GoAML_char_process_WR(
								case
									when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
									then
										'Client number: ' + se.edit_kd_nr + 
										case 
											when @partners is not NULL 
											then char(13) + char(10) + 'It is the partnership of the clients: ' + ltrim(@partners)
											else ''
										end +
										case 
											when FAF.GoAML_get_paramS(@F_BANK_ID,78,2) = 'ok' and FAF.GoAML_get_paramS(@F_BANK_ID,32,1)=FAF.GoAML_char_process(lf.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_legal_form'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_legal_form'))
											then char(13) + char(10) + coalesce(FAF.GoAML_char_process(lf.comments,FAF.GoAML_get_paramS(@F_BANK_ID,32,2),0),'')
											else ''
										end + 
										case
											when cntr1.iso_land_cd is NULL and FAF.GoAML_char_process(se.land_lnr_nat_bez,NULL,0) is not NULL and FAF.GoAML_char_process(se.iso_land_code_nat,NULL,0) is not NULL
											then char(13) + char(10) + 'Actual country is ' + FAF.GoAML_char_process(se.land_lnr_nat_bez,'',0) + ' (' + FAF.GoAML_char_process(se.iso_land_code_nat,'',0) + ')'
											else ''
										end
									else NULL
								end
							,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__comments')) as "comments"
						from 
							FAF.FA_MROS_KD_STAMM se
						left join 
							FAF.FA_MROS_KD_ADR_XML sea
						on sea.kd_lnr=se.kd_lnr and sea.userbk_nr=se.userbk_nr
						left join
							FAF.FA_MROS_KD_EMAIL_XML spp
						on spp.kd_lnr=se.kd_lnr and spp.userbk_nr=se.userbk_nr
						left join
							FAF.FA_MROS_INT_LAND cntr1
						on cntr1.iso_land_cd=se.iso_land_code_nat and cntr1.userbk_nr=se.userbk_nr
						left join
							FAF.FA_MROS_REFERENCE_TABLE lf
						on se.userbk_nr=lf.userbk_nr and cast(se.kd_rstat_cd as varchar)=lf.finnova_value and lf.ref_type=8
						where se.kd_lnr=@cl_id and se.userbk_nr=@F_BANK_ID and se.partner_flag=0
						FOR XML PATH (''),type, ELEMENTS);

					if FAF.GoAML_get_paramS(@F_BANK_ID,136,1)='ok' and @xml_cl1 is not NULL
						begin
							set @xml_kd5 =
								(select
									FAF.GoAML_get_paramS(@F_BANK_ID,136,2) as "account_entity_relation",
									@xml_cl1 as "entity",
									FAF.GoAML_char_process(
										convert(varchar,cast(@dt_fr0 as datetime),126)
									,FAF.GoAML_get_paramV(@F_BANK_ID,'rdr_valid_from'),FAF.GoAML_get_paramL(@F_BANK_ID,'rdr_valid_from')) as "relation_date_range/valid_from",
									FAF.GoAML_char_process(
										convert(varchar,cast(@auf_dt0 as datetime),126)
									,FAF.GoAML_get_paramV(@F_BANK_ID,'rdr_valid_to'),FAF.GoAML_get_paramL(@F_BANK_ID,'rdr_valid_to')) as "relation_date_range/valid_to"
								FOR XML PATH ('account_related_entity'),type, ELEMENTS);
						end;	
					else
						begin
							if @xml_cl1 is NULL
								set @xml_cl0 = (select @xml_de as "t_entity" FOR XML PATH (''),type, ELEMENTS);
							else
								set @xml_cl0 = (select @xml_cl1 as "t_entity" FOR XML PATH (''),type, ELEMENTS);
						end;
				end;

				set @mm0 = (select count(*) from @KD_KD) - isnull(@mmm,0);
				set @mm = (select count(*) from @KD_KD0) - isnull(@mmm0,0);
				set @jj = 1;
				while @jj<=@mm
				begin
					set @n_id = (select nID from @KD_KD0 where id=@jj + isnull(@mmm0,0));
					if @n_id is not NULL
					begin
						if FAF.GoAML_get_paramS(@F_BANK_ID,77,1) = 'ok'
							begin
								insert into @KD_KD
								select 
									kdkd.kd_lnr_zuw as nID,kdkd.mros_value as rl,kdkd.comments as cmm,NULL as ptn,kd.kd_htyp_cd as kdtp,kd.edit_kd_nr as kdnr,
									case
										when kd.kd_htyp_cd=3 then kd.kd_bez
										when kd.kd_htyp_cd=2 and kd.name_1=kd1.name_1 
										then isnull(kd.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' and ' + isnull(kd1.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd.name_1,'')
										when kd.kd_htyp_cd=2 and kd1.userbk_nr is not NULL
										then isnull(kd.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd.name_1,'') + ' and ' + isnull(kd1.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd1.name_1,'')
										else isnull(kd.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd.name_1,'')
									end as kdnm,
									kdkd.dt1 as dt1,kdkd.dt2 as dt2,kdkd.e_rl,kdkd.e_cmm
								from
									(select kdkd1.kd_lnr_zuw,rt1.mros_value,rt1_e.mros_value as e_rl,
										case when FAF.GoAML_char_process(rt1.special_mark,NULL,4000) is NULL then rt1.comments else rt1.special_mark end as comments,
										case when FAF.GoAML_char_process(rt1_e.special_mark,NULL,4000) is NULL then rt1_e.comments else rt1_e.special_mark end as e_cmm,
										kdkd1.valid_von_dat as dt1,kdkd1.valid_bis_dat as dt2,
										isnull(rt1.activ_MROS,1) as p_flag,isnull(rt1_e.activ_MROS,1) as e_flag
									from 
										FAF.FA_MROS_KD_KD kdkd1
									left join
										FAF.FA_MROS_REFERENCE_TABLE rt1
									on cast(kdkd1.kd_kd_verb_cd as varchar)=rt1.Finnova_value and rt1.userbk_nr=kdkd1.userbk_nr and rt1.ref_type=14
									left join
										FAF.FA_MROS_REFERENCE_TABLE rt1_e
									on cast(kdkd1.kd_kd_verb_cd as varchar)=rt1_e.Finnova_value and rt1_e.userbk_nr=kdkd1.userbk_nr and rt1_e.ref_type=27
									where kdkd1.userbk_nr=@F_BANK_ID and kdkd1.kd_lnr=@n_id and kdkd1.kd_lnr_zuw<>@n_id
										and (kdkd1.valid_bis_dat is NULL or kdkd1.valid_bis_dat>=isnull(@auf_dt0,@cur_date))
										and (kdkd1.valid_von_dat is NULL or kdkd1.valid_von_dat<=isnull(@auf_dt0,@cur_date))
									union
									select kdkd2.kd_lnr as kd_lnr_zuw,rt2.mros_value,rt2_e.mros_value as e_rl,
										case when FAF.GoAML_char_process(rt2.special_mark,NULL,4000) is NULL then rt2.comments else rt2.special_mark end as comments,
										case when FAF.GoAML_char_process(rt2_e.special_mark,NULL,4000) is NULL then rt2_e.comments else rt2_e.special_mark end as e_cmm,
										kdkd2.valid_von_dat as dt1,kdkd2.valid_bis_dat as dt2,
										isnull(rt2.activ_MROS,1) as p_flag,isnull(rt2_e.activ_MROS,1) as e_flag
									from 
										FAF.FA_MROS_KD_KD kdkd2
									left join
										FAF.FA_MROS_REFERENCE_TABLE rt2
									on cast(kdkd2.kd_kd_verb_cd as varchar)=rt2.Finnova_value and rt2.userbk_nr=kdkd2.userbk_nr and rt2.ref_type=15
									left join
										FAF.FA_MROS_REFERENCE_TABLE rt2_e
									on cast(kdkd2.kd_kd_verb_cd as varchar)=rt2_e.Finnova_value and rt2_e.userbk_nr=kdkd2.userbk_nr and rt2_e.ref_type=28
									where kdkd2.userbk_nr=@F_BANK_ID and kdkd2.kd_lnr_zuw=@n_id and kdkd2.kd_lnr<>@n_id
										and (kdkd2.valid_bis_dat is NULL or kdkd2.valid_bis_dat>=isnull(@auf_dt0,@cur_date))
										and (kdkd2.valid_von_dat is NULL or kdkd2.valid_von_dat<=isnull(@auf_dt0,@cur_date))
									union
									select vtz.kd_lnr as kd_lnr_zuw,rt3.mros_value,rt3_e.mros_value as e_rl,
										case when FAF.GoAML_char_process(rt3.special_mark,NULL,4000) is NULL then rt3.comments else rt3.special_mark end as comments,
										case when FAF.GoAML_char_process(rt3_e.special_mark,NULL,4000) is NULL then rt3_e.comments else rt3_e.special_mark end as e_cmm,
										case when vtz.beg_dat>=vts.beginn_dat or vts.beginn_dat is NULL then vtz.beg_dat else vts.beginn_dat end as dt1,
										case when vtz.aufheb_dat<=vts.ende_dat or vts.ende_dat is NULL then vtz.aufheb_dat else vts.ende_dat end as dt2,
										isnull(rt3.activ_MROS,1) as p_flag,isnull(rt3_e.activ_MROS,1) as e_flag
									from 
										FAF.FA_MROS_VT_STAMM vts
									join
										FAF.FA_MROS_VT_ZUORD vtz
									on vts.userbk_nr=vtz.userbk_nr and vts.vt_lnr=vtz.vt_lnr
									left join
										FAF.FA_MROS_REFERENCE_TABLE rt3
									on cast(vts.vt_typ_cd as varchar)+'_'+cast(vtz.sb_sign_recht_cd as varchar)=rt3.Finnova_value and rt3.userbk_nr=vts.userbk_nr and rt3.ref_type=18
									left join
										FAF.FA_MROS_REFERENCE_TABLE rt3_e
									on cast(vts.vt_typ_cd as varchar)+'_'+cast(vtz.sb_sign_recht_cd as varchar)=rt3_e.Finnova_value and rt3_e.userbk_nr=vts.userbk_nr and rt3_e.ref_type=29
									where vts.userbk_nr=@F_BANK_ID and vts.kd_lnr=@n_id and vtz.kd_lnr<>@n_id
										and (vts.kt_lnr is NULL or @jj=1 and vts.kt_lnr=@kt_id)
										and (vts.ende_dat is NULL or vts.ende_dat>=isnull(@auf_dt0,@cur_date))
										and (vts.beginn_dat is NULL or vts.beginn_dat<=isnull(@auf_dt0,@cur_date))
										and (vtz.beg_dat is NULL or vtz.beg_dat<=isnull(@auf_dt0,@cur_date))
										and (vtz.aufheb_dat is NULL or vtz.aufheb_dat>=isnull(@auf_dt0,@cur_date))
									union
									select vts.kd_lnr as kd_lnr_zuw,rt4.mros_value,rt4_e.mros_value as e_rl,
										case when FAF.GoAML_char_process(rt4.special_mark,NULL,4000) is NULL then rt4.comments else rt4.special_mark end as comments,
										case when FAF.GoAML_char_process(rt4_e.special_mark,NULL,4000) is NULL then rt4_e.comments else rt4_e.special_mark end as e_cmm,
										case when vtz.beg_dat>=vts.beginn_dat or vts.beginn_dat is NULL then vtz.beg_dat else vts.beginn_dat end as dt1,
										case when vtz.aufheb_dat<=vts.ende_dat or vts.ende_dat is NULL then vtz.aufheb_dat else vts.ende_dat end as dt2,
										isnull(rt4.activ_MROS,1) as p_flag,isnull(rt4_e.activ_MROS,1) as e_flag
									from 
										FAF.FA_MROS_VT_STAMM vts
									join
										FAF.FA_MROS_VT_ZUORD vtz
									on vts.userbk_nr=vtz.userbk_nr and vts.vt_lnr=vtz.vt_lnr
									left join
										FAF.FA_MROS_REFERENCE_TABLE rt4
									on cast(vts.vt_typ_cd as varchar)+'_'+cast(vtz.sb_sign_recht_cd as varchar)=rt4.Finnova_value and rt4.userbk_nr=vts.userbk_nr and rt4.ref_type=19
									left join
										FAF.FA_MROS_REFERENCE_TABLE rt4_e
									on cast(vts.vt_typ_cd as varchar)+'_'+cast(vtz.sb_sign_recht_cd as varchar)=rt4_e.Finnova_value and rt4_e.userbk_nr=vts.userbk_nr and rt4_e.ref_type=30
									where vtz.userbk_nr=@F_BANK_ID and vtz.kd_lnr=@n_id and vts.kd_lnr<>@n_id
										and (vts.ende_dat is NULL or vts.ende_dat>=isnull(@auf_dt0,@cur_date))
										and (vts.beginn_dat is NULL or vts.beginn_dat<=isnull(@auf_dt0,@cur_date))
										and (vtz.beg_dat is NULL or vtz.beg_dat<=isnull(@auf_dt0,@cur_date))
										and (vtz.aufheb_dat is NULL or vtz.aufheb_dat>=isnull(@auf_dt0,@cur_date))
									) kdkd
								join
									FAF.FA_MROS_KD_STAMM kd
								on kd.userbk_nr=@F_BANK_ID and kd.kd_lnr=kdkd.kd_lnr_zuw and kd.partner_flag=0
								left join
									FAF.FA_MROS_KD_STAMM kd1
								on kd1.userbk_nr=@F_BANK_ID and kd1.kd_lnr=kdkd.kd_lnr_zuw and kd1.partner_flag=1
								where kdkd.kd_lnr_zuw not in (select kd0.nID from @KD_KD0 kd0 where kd0.id>isnull(@mmm0,0))
									and (kd.kd_htyp_cd=1 and kdkd.p_flag=1 or kd.kd_htyp_cd>1 and kdkd.e_flag=1);
							end;
						else
							begin
								insert into @KD_KD
								select 
									kdkd.kd_lnr_zuw as nID,kdkd.mros_value as rl,kdkd.comments as cmm,NULL as ptn,kd.kd_htyp_cd as kdtp,kd.edit_kd_nr as kdnr,
									case
										when kd.kd_htyp_cd=3 then kd.kd_bez
										when kd.kd_htyp_cd=2 and kd.name_1=kd1.name_1 
										then isnull(kd.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' and ' + isnull(kd1.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd.name_1,'')
										when kd.kd_htyp_cd=2 and kd1.userbk_nr is not NULL
										then isnull(kd.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd.name_1,'') + ' and ' + isnull(kd1.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd1.name_1,'')
										else isnull(kd.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd.name_1,'')
									end as kdnm,
									kdkd.dt1 as dt1,kdkd.dt2 as dt2,kdkd.e_rl,kdkd.e_cmm
								from
									(select kdkd1.kd_lnr_zuw,rt1.mros_value,rt1_e.mros_value as e_rl,
										case when FAF.GoAML_char_process(rt1.special_mark,NULL,4000) is NULL then rt1.comments else rt1.special_mark end as comments,
										case when FAF.GoAML_char_process(rt1_e.special_mark,NULL,4000) is NULL then rt1_e.comments else rt1_e.special_mark end as e_cmm,
										kdkd1.valid_von_dat as dt1,kdkd1.valid_bis_dat as dt2,
										isnull(rt1.activ_MROS,1) as p_flag,isnull(rt1_e.activ_MROS,1) as e_flag
									from 
										FAF.FA_MROS_KD_KD kdkd1
									left join
										FAF.FA_MROS_REFERENCE_TABLE rt1
									on cast(kdkd1.kd_kd_verb_cd as varchar)=rt1.Finnova_value and rt1.userbk_nr=kdkd1.userbk_nr and rt1.ref_type=14
									left join
										FAF.FA_MROS_REFERENCE_TABLE rt1_e
									on cast(kdkd1.kd_kd_verb_cd as varchar)=rt1_e.Finnova_value and rt1_e.userbk_nr=kdkd1.userbk_nr and rt1_e.ref_type=27
									where kdkd1.userbk_nr=@F_BANK_ID and kdkd1.kd_lnr=@n_id and kdkd1.kd_lnr_zuw<>@n_id
										and (kdkd1.valid_bis_dat is NULL or kdkd1.valid_bis_dat>=isnull(@auf_dt0,@cur_date))
										and (kdkd1.valid_von_dat is NULL or kdkd1.valid_von_dat<=isnull(@auf_dt0,@cur_date))
									union
									select kdkd2.kd_lnr as kd_lnr_zuw,rt2.mros_value,rt2_e.mros_value as e_rl,
										case when FAF.GoAML_char_process(rt2.special_mark,NULL,4000) is NULL then rt2.comments else rt2.special_mark end as comments,
										case when FAF.GoAML_char_process(rt2_e.special_mark,NULL,4000) is NULL then rt2_e.comments else rt2_e.special_mark end as e_cmm,
										kdkd2.valid_von_dat as dt1,kdkd2.valid_bis_dat as dt2,
										isnull(rt2.activ_MROS,1) as p_flag,isnull(rt2_e.activ_MROS,1) as e_flag
									from 
										FAF.FA_MROS_KD_KD kdkd2
									left join
										FAF.FA_MROS_REFERENCE_TABLE rt2
									on cast(kdkd2.kd_kd_verb_cd as varchar)=rt2.Finnova_value and rt2.userbk_nr=kdkd2.userbk_nr and rt2.ref_type=15
									left join
										FAF.FA_MROS_REFERENCE_TABLE rt2_e
									on cast(kdkd2.kd_kd_verb_cd as varchar)=rt2_e.Finnova_value and rt2_e.userbk_nr=kdkd2.userbk_nr and rt2_e.ref_type=28
									where kdkd2.userbk_nr=@F_BANK_ID and kdkd2.kd_lnr_zuw=@n_id and kdkd2.kd_lnr<>@n_id
										and (kdkd2.valid_bis_dat is NULL or kdkd2.valid_bis_dat>=isnull(@auf_dt0,@cur_date))
										and (kdkd2.valid_von_dat is NULL or kdkd2.valid_von_dat<=isnull(@auf_dt0,@cur_date))
									union
									select vtz.kd_lnr as kd_lnr_zuw,rt3.mros_value,rt3_e.mros_value as e_rl,
										case when FAF.GoAML_char_process(rt3.special_mark,NULL,4000) is NULL then rt3.comments else rt3.special_mark end as comments,
										case when FAF.GoAML_char_process(rt3_e.special_mark,NULL,4000) is NULL then rt3_e.comments else rt3_e.special_mark end as e_cmm,
										case when vtz.beg_dat>=vts.beginn_dat or vts.beginn_dat is NULL then vtz.beg_dat else vts.beginn_dat end as dt1,
										case when vtz.aufheb_dat<=vts.ende_dat or vts.ende_dat is NULL then vtz.aufheb_dat else vts.ende_dat end as dt2,
										isnull(rt3.activ_MROS,1) as p_flag,isnull(rt3_e.activ_MROS,1) as e_flag
									from 
										FAF.FA_MROS_VT_STAMM vts
									join
										FAF.FA_MROS_VT_ZUORD vtz
									on vts.userbk_nr=vtz.userbk_nr and vts.vt_lnr=vtz.vt_lnr
									left join
										FAF.FA_MROS_REFERENCE_TABLE rt3
									on cast(vts.vt_typ_cd as varchar)=rt3.Finnova_value and rt3.userbk_nr=vts.userbk_nr and rt3.ref_type=18
									left join
										FAF.FA_MROS_REFERENCE_TABLE rt3_e
									on cast(vts.vt_typ_cd as varchar)=rt3_e.Finnova_value and rt3_e.userbk_nr=vts.userbk_nr and rt3_e.ref_type=29
									where vts.userbk_nr=@F_BANK_ID and vts.kd_lnr=@n_id and vtz.kd_lnr<>@n_id
										and (vts.kt_lnr is NULL or @jj=1 and vts.kt_lnr=@kt_id)
										and (vts.ende_dat is NULL or vts.ende_dat>=isnull(@auf_dt0,@cur_date))
										and (vts.beginn_dat is NULL or vts.beginn_dat<=isnull(@auf_dt0,@cur_date))
										and (vtz.beg_dat is NULL or vtz.beg_dat<=isnull(@auf_dt0,@cur_date))
										and (vtz.aufheb_dat is NULL or vtz.aufheb_dat>=isnull(@auf_dt0,@cur_date))
									union
									select vts.kd_lnr as kd_lnr_zuw,rt4.mros_value,rt4_e.mros_value as e_rl,
										case when FAF.GoAML_char_process(rt4.special_mark,NULL,4000) is NULL then rt4.comments else rt4.special_mark end as comments,
										case when FAF.GoAML_char_process(rt4_e.special_mark,NULL,4000) is NULL then rt4_e.comments else rt4_e.special_mark end as e_cmm,
										case when vtz.beg_dat>=vts.beginn_dat or vts.beginn_dat is NULL then vtz.beg_dat else vts.beginn_dat end as dt1,
										case when vtz.aufheb_dat<=vts.ende_dat or vts.ende_dat is NULL then vtz.aufheb_dat else vts.ende_dat end as dt2,
										isnull(rt4.activ_MROS,1) as p_flag,isnull(rt4_e.activ_MROS,1) as e_flag
									from 
										FAF.FA_MROS_VT_STAMM vts
									join
										FAF.FA_MROS_VT_ZUORD vtz
									on vts.userbk_nr=vtz.userbk_nr and vts.vt_lnr=vtz.vt_lnr
									left join
										FAF.FA_MROS_REFERENCE_TABLE rt4
									on cast(vts.vt_typ_cd as varchar)=rt4.Finnova_value and rt4.userbk_nr=vts.userbk_nr and rt4.ref_type=19
									left join
										FAF.FA_MROS_REFERENCE_TABLE rt4_e
									on cast(vts.vt_typ_cd as varchar)=rt4_e.Finnova_value and rt4_e.userbk_nr=vts.userbk_nr and rt4_e.ref_type=30
									where vtz.userbk_nr=@F_BANK_ID and vtz.kd_lnr=@n_id and vts.kd_lnr<>@n_id
										and (vts.ende_dat is NULL or vts.ende_dat>=isnull(@auf_dt0,@cur_date))
										and (vts.beginn_dat is NULL or vts.beginn_dat<=isnull(@auf_dt0,@cur_date))
										and (vtz.beg_dat is NULL or vtz.beg_dat<=isnull(@auf_dt0,@cur_date))
										and (vtz.aufheb_dat is NULL or vtz.aufheb_dat>=isnull(@auf_dt0,@cur_date))
									) kdkd
								join
									FAF.FA_MROS_KD_STAMM kd
								on kd.userbk_nr=@F_BANK_ID and kd.kd_lnr=kdkd.kd_lnr_zuw and kd.partner_flag=0
								left join
									FAF.FA_MROS_KD_STAMM kd1
								on kd1.userbk_nr=@F_BANK_ID and kd1.kd_lnr=kdkd.kd_lnr_zuw and kd1.partner_flag=1
								where kdkd.kd_lnr_zuw not in (select kd0.nID from @KD_KD0 kd0 where kd0.id>isnull(@mmm0,0))
									and (kd.kd_htyp_cd=1 and kdkd.p_flag=1 or kd.kd_htyp_cd>1 and kdkd.e_flag=1);
							end;
					end;

					set @jj = @jj + 1;
				end;
				
				set @mm = (select count(*) from @KD_KD) - isnull(@mmm,0);
				if @mm>@mm0
				begin
					set @jj = @mm0 + 1;
					while @jj<=@mm
					begin
						set @n_id = NULL;
						set @rol = NULL;
						set @comm = NULL;
						set @kd_nr = NULL;
						set @kd_tp = NULL;
						set @partners = NULL;
						set @dt_fr = NULL;
						set @dt_to = NULL;
						set @e_rol = NULL;
						set @e_comm = NULL;

						select @n_id = nID,@rol=rl,@comm=cmm,@kd_nr=kdnr,@kd_tp=kdtp,@dt_fr=dt1,@dt_to=dt2,@e_rol=e_rl,@e_comm=e_cmm from @KD_KD where id=@jj + isnull(@mmm,0);
						if @kd_tp=2
						begin
							--doppelpersons of clients linked to the initial client
							set @mm0 = (select count(*) from @KD_KD) - isnull(@mmm,0);
							insert into @KD_KD
							select 
								kdkd.kd_lnr_zuw as nID,@rol as rl,@comm as cmm,@kd_nr as ptn,kd.kd_htyp_cd as kdtp,kd.edit_kd_nr as kdnr,
								case
									when kd.kd_htyp_cd=3 then kd.kd_bez
									when kd.kd_htyp_cd=2 and kd.name_1=kd1.name_1 
									then isnull(kd.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' and ' + isnull(kd1.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd.name_1,'')
									when kd.kd_htyp_cd=2 and kd1.userbk_nr is not NULL
									then isnull(kd.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd.name_1,'') + ' and ' + isnull(kd1.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd1.name_1,'')
									else isnull(kd.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd.name_1,'')
								end as kdnm,
								@dt_fr as dt1,@dt_to as dt2,@e_rol as e_rl,@e_comm as e_cmm
							from
								(select kdkd1.kd_lnr_zuw 
								from 
									FAF.FA_MROS_KD_KD kdkd1
								join
									FAF.FA_MROS_REFERENCE_TABLE rt_1
								on cast(kdkd1.kd_kd_verb_cd as varchar)=rt_1.Finnova_value and rt_1.userbk_nr=kdkd1.userbk_nr and rt_1.ref_type=14 and rt_1.activ_MROS=1 and rt_1.special_mark='DOPPELPERSON'
								where kdkd1.userbk_nr=@F_BANK_ID and kdkd1.kd_lnr=@n_id and kdkd1.kd_lnr_zuw<>@n_id
									and (kdkd1.valid_bis_dat is NULL or kdkd1.valid_bis_dat>=isnull(@auf_dt0,@cur_date))
									and (kdkd1.valid_von_dat is NULL or kdkd1.valid_von_dat<=isnull(@auf_dt0,@cur_date))
								union
								select kdkd2.kd_lnr as kd_lnr_zuw
								from 
									FAF.FA_MROS_KD_KD kdkd2
								join
									FAF.FA_MROS_REFERENCE_TABLE rt_2
								on cast(kdkd2.kd_kd_verb_cd as varchar)=rt_2.Finnova_value and rt_2.userbk_nr=kdkd2.userbk_nr and rt_2.ref_type=15 and rt_2.activ_MROS=1 and rt_2.special_mark='DOPPELPERSON'
								where kdkd2.userbk_nr=@F_BANK_ID and kdkd2.kd_lnr_zuw=@n_id and kdkd2.kd_lnr<>@n_id
									and (kdkd2.valid_bis_dat is NULL or kdkd2.valid_bis_dat>=isnull(@auf_dt0,@cur_date))
									and (kdkd2.valid_von_dat is NULL or kdkd2.valid_von_dat<=isnull(@auf_dt0,@cur_date))
								) kdkd
							join
								FAF.FA_MROS_KD_STAMM kd
							on kd.userbk_nr=@F_BANK_ID and kd.kd_lnr=kdkd.kd_lnr_zuw and kd.partner_flag=0
							left join
								FAF.FA_MROS_KD_STAMM kd1
							on kd1.userbk_nr=@F_BANK_ID and kd1.kd_lnr=kdkd.kd_lnr_zuw and kd1.partner_flag=1;

							if (select count(*) from @KD_KD)>@mm0 + isnull(@mmm,0)
							begin
								set @partners = 
									(select STUFF(
										(select ', ' + coalesce(kdnr,'') from @KD_KD where id>@mm0 + isnull(@mmm,0) FOR XML PATH(''))
									, 1, LEN(', '), ''));

								if @partners is not NULL
									insert into @KD_KD select @n_id as nID,NULL as rl,NULL as cmm,@partners as ptn,NULL as kdtp,NULL as kdnr,NULL as kdnm,NULL as dt1,NULL as dt2,NULL as e_rl,NULL as e_cmm;
							end;
						end;

						set @jj = @jj + 1;
					end;
				end;
				
				set @mm = (select count(*) from @KD_KD) - isnull(@mmm,0);
				--prepare client xml
				set @jj = 1;
				while @jj<=@mm
				begin
					set @n_id = NULL;
					set @rol = NULL;
					set @comm = NULL;
					set @e_rol = NULL;
					set @e_comm = NULL;
					set @kd_nr = NULL;
					set @kd_nm = NULL;
					set @kd_tp = NULL;
					set @partners = NULL;
					set @xml_cl = NULL;
					set @xml_clt = NULL;
					set @xml_cl5 = NULL;
					set @xml_cl5t = NULL;
					set @bb = NULL;
					set @dt_fr = NULL;
					set @dt_to = NULL;
					set @dt_fr_e = NULL;
					set @dt_to_e = NULL;
					set @tecom = NULL;
					set @tpcom = NULL;
					select @n_id = nID,@rol=rl,@comm=cmm,@partners=ptn,@kd_nr=kdnr,@kd_tp=kdtp,@kd_nm=kdnm,@dt_fr=dt1,@dt_to=dt2,@e_rol=e_rl,@e_comm=e_cmm from @KD_KD where id=@jj + isnull(@mmm,0);
					set @bb = (select count(*) from @KD_KD0 where id>isnull(@mmm0,0) and nID=@n_id);
					if FAF.GoAML_get_paramS(@F_BANK_ID,60,1) = 'ok' and @mm = 1 and @cl_tp=1
						set @rol = FAF.GoAML_get_paramS(@F_BANK_ID,60,2);
					set @dt_fr_e = @dt_fr;
					set @dt_to_e = @dt_to;

					if FAF.GoAML_get_paramS(@F_BANK_ID,92,1)='ok' 
						begin
							if (select count(*) from @KD_KD where id<@jj + isnull(@mmm,0) and id>isnull(@mmm,0) and nID=@n_id) = 0
							begin
								--person
								if @kd_tp=1
								begin
									select
										@xml_clt = 
											(select
												FAF.GoAML_char_process(gd.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__gender'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__gender')) as "gender",
												FAF.GoAML_char_process(se.titel,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__title'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__title')) as "title",
												FAF.GoAML_char_process(se.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__first_name')) as "first_name",
												case 
													when FAF.GoAML_get_paramS(@F_BANK_ID,44,1)='ok'
													then upper(FAF.GoAML_char_process(se.name_1,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__last_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__last_name')))
													else FAF.GoAML_char_process(se.name_1,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__last_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__last_name'))
												end as "last_name",
												FAF.GoAML_char_process(
													case 
														when se.geburt_dat is NULL or se.geburt_dat < convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or cast(se.geburt_dat as date)>@cur_date
														then 
															case 
																when se.geburt_jahr is NULL or se.geburt_jahr<cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) or se.geburt_jahr>year(@cur_date)
																then NULL
																else cast(se.geburt_jahr as varchar) + '-01-01T00:00:00' 
															end 
														else left(convert(varchar,cast(se.geburt_dat as datetime),126),19)
													end
												,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__birthdate'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__birthdate')) as "birthdate",
												FAF.GoAML_char_process(se.geburt_ort,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__birth_place'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__birth_place')) as "birth_place",
												FAF.GoAML_char_process(
													case
														when FAF.GoAML_get_paramS(@F_BANK_ID,42,1)='ok'
														then
															case 
																when FAF.GoAML_get_paramS(@F_BANK_ID,98,1)='ok'
																then
																	case 
																		when FAF.GoAML_get_paramS(@F_BANK_ID,64,1)='ok'
																		then
																			case 
																				when isnull(se.kd_pep_cd,se.kd_pep_cd_auto) is NULL then 'UNBEKANNT'
																				when isnull(se.kd_pep_cd,se.kd_pep_cd_auto) = '0' then 'NEIN'
																				else 'JA'
																			end
																		else
																			case 
																				when isnull(se.kd_pep_cd,se.kd_pep_cd_auto) is NULL then 'unbekannt'
																				when isnull(se.kd_pep_cd,se.kd_pep_cd_auto) = '0' then 'nein'
																				else 'ja'
																			end
																	end
																else
																	case 
																		when FAF.GoAML_get_paramS(@F_BANK_ID,64,1)='ok'
																		then
																			case 
																				when se.kd_pep_cd is NULL then 'UNBEKANNT'
																				when se.kd_pep_cd = '0' then 'NEIN'
																				else 'JA'
																			end
																		else
																			case 
																				when se.kd_pep_cd is NULL then 'unbekannt'
																				when se.kd_pep_cd = '0' then 'nein'
																				else 'ja'
																			end
																	end
															end
														else NULL
													end
												,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__id_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__id_number')) as "id_number",
												FAF.GoAML_char_process(cntr1.iso_land_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__nationality1'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__nationality1')) as "nationality1",
												FAF.GoAML_char_process(cntr2.iso_land_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__nationality2'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__nationality2')) as "nationality2",
												spp.phones as "phones",
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,47,1)='ok'
													then
														coalesce(sea.addresses,
															(select
																FAF.GoAML_get_paramS(@F_BANK_ID,8,2) as "address_type",
																FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address') as "address",
																FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__city') as "city",
																FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__country_code') as "country_code"
															FOR XML PATH ('address'),type, ELEMENTS)
														)
													else sea.addresses
												end as "addresses",
												(select FAF.GoAML_char_process(lower(eml.email_adr),FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__email'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__email')) as "email"
												from FAF.FA_MROS_KD_EMAIL eml
												join FAF.FA_MROS_REFERENCE_TABLE rt
												on eml.userbk_nr=rt.userbk_nr and cast(eml.mail_art_cd as varchar)=rt.finnova_value and rt.ref_type=7 and rt.MROS_value=FAF.GoAML_get_paramS(@F_BANK_ID,121,2)
												where eml.kd_lnr=se.kd_lnr and eml.userbk_nr=se.userbk_nr and left(eml.email_adr,1)<>'@'
												FOR XML PATH (''),type, ELEMENTS) as "emails",
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,48,1)='ok'
													then
														coalesce(spi.identification, 
															(select
																FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__type') as "type",
																FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__number') as "number",
																FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__issue_country') as "issue_country",
																FAF.GoAML_char_process(
																	case
																		when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
																		then
																			case
																				when FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__type')=FAF.GoAML_get_paramS(@F_BANK_ID,6,1)
																				then FAF.GoAML_get_paramS(@F_BANK_ID,6,2)
																				else NULL
																			end
																		else NULL
																	end
																,FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpi__comments')) as "comments"
															FOR XML PATH ('identification'),type, ELEMENTS)
														)
													else spi.identification
												end as "identifications",
												FAF.GoAML_char_process(
													case 
														when FAF.GoAML_get_paramS(@F_BANK_ID,34,1)='ok' and ((se.tod_dat >= convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) and cast(se.tod_dat as date)<=@cur_date)
															or (se.tod_jahr>=cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) and se.tod_jahr<=year(@cur_date)))
														then FAF.GoAML_get_paramS(@F_BANK_ID,34,2)
														else NULL
													end
												,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__deceased'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__deceased')) as "deceased",
												FAF.GoAML_char_process(
													case 
														when se.tod_dat is NULL or se.tod_dat < convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or cast(se.tod_dat as date)>@cur_date
														then 
															case 
																when se.tod_jahr is NULL or se.tod_jahr<cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) or se.tod_jahr>year(@cur_date)
																then NULL
																else cast(se.tod_jahr as varchar) + '-01-01T00:00:00' 
															end 
														else left(convert(varchar,cast(se.tod_dat as datetime),126),19)
													end
												,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__date_deceased'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__date_deceased')) as "date_deceased",
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,68,1)='ok' and len(se.uid_nr)>1
													then FAF.GoAML_char_process(se.uid_nr,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__tax_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__tax_number'))
													else NULL
												end as "tax_number"
											FOR XML PATH (''),type, ELEMENTS),
										@tpcom = 
											case
												when cntr1.iso_land_cd is NULL and FAF.GoAML_char_process(se.land_lnr_nat_bez,NULL,0) is not NULL and FAF.GoAML_char_process(se.iso_land_code_nat,NULL,0) is not NULL
												then '; ' + char(13) + char(10) + 'Actual country is ' + FAF.GoAML_char_process(se.land_lnr_nat_bez,'',0) + ' (' + FAF.GoAML_char_process(se.iso_land_code_nat,'',0) + ')'
												else ''
											end
									from 
										FAF.FA_MROS_KD_STAMM se
									left join 
										FAF.FA_MROS_KD_ADR_XML sea
									on sea.kd_lnr=se.kd_lnr and sea.userbk_nr=se.userbk_nr
									left join
										FAF.FA_MROS_KD_EMAIL_XML spp
									on spp.kd_lnr=se.kd_lnr and spp.userbk_nr=se.userbk_nr
									left join
										FAF.FA_MROS_KD_VSB_XML spi
									on spi.kd_lnr=se.kd_lnr and spi.userbk_nr=se.userbk_nr
									left join
										FAF.FA_MROS_INT_LAND cntr1
									on cntr1.iso_land_cd=se.iso_land_code_nat and cntr1.userbk_nr=se.userbk_nr
									left join
										FAF.FA_MROS_INT_LAND cntr2
									on cntr2.iso_land_cd=se.iso_land_code_nat_2 and cntr2.userbk_nr=se.userbk_nr
									left join
										FAF.FA_MROS_REFERENCE_TABLE gd
									on se.userbk_nr=gd.userbk_nr and cast(se.GESCHLECHT_CD as varchar)=gd.finnova_value and gd.ref_type=4
									where se.kd_lnr=@n_id and se.userbk_nr=@F_BANK_ID and se.partner_flag=0;

									if @bb>0
										begin
											set @xml_cl =
												(select
													FAF.GoAML_char_process(
														case 
															when @cl_tp=1 and @jj=1 and FAF.GoAML_get_paramS(@F_BANK_ID,30,1)='0'
															then FAF.GoAML_get_paramS(@F_BANK_ID,30,2) 
															else NULL 
														end
													,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__is_primary'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__is_primary')) as "is_primary",
													(select
														@xml_clt,
														(select
															FAF.GoAML_char_process_WR(
																case
																	when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
																	then
																		'Client number: ' + @kd_nr + 
																		case 
																			when len(partners)>0
																			then ' (partner in the partnership ' + partners + ')' 
																			else ''
																		end + @tpcom
																	else NULL
																end
															,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__comments')) as "comments"
														FOR XML PATH (''),type, ELEMENTS)
													FOR XML PATH (''),type, ELEMENTS) as "t_person",
													FAF.GoAML_char_process(@rol,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__role'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__role')) as "role",
													case
														when FAF.GoAML_get_paramS(@F_BANK_ID,105,1)='ok' or @dt_fr>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or 
															@dt_to<=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112)
														then 
															(select
																FAF.GoAML_char_process(
																	convert(varchar,cast((case when @dt_fr>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) then @dt_fr else @dt_fr0 end) as datetime),126)
																,FAF.GoAML_get_paramV(@F_BANK_ID,'rdr_valid_from'),FAF.GoAML_get_paramL(@F_BANK_ID,'rdr_valid_from')) as "valid_from",
																case 
																	when @dt_fr>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)
																	then NULL
																	else FAF.GoAML_get_paramS(@F_BANK_ID,105,2)
																end as "is_approx_from_date",
																FAF.GoAML_char_process(
																	case 
																		when @dt_to<=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112)
																		then convert(varchar,cast(@dt_to as datetime),126)
																		else NULL
																	end
																,FAF.GoAML_get_paramV(@F_BANK_ID,'rdr_valid_to'),FAF.GoAML_get_paramL(@F_BANK_ID,'rdr_valid_to')) as "valid_to"
															FOR XML PATH (''),type, ELEMENTS)
														else NULL
													end as "relation_date_range"
												from
													(select max(ptn) as partners from @KD_KD where id>isnull(@mmm,0) and nID=@n_id) s
												FOR XML PATH ('account_related_person'),type, ELEMENTS);
										end;
									else
										begin
											set @xml_cl =
												(select
													FAF.GoAML_char_process(
														case 
															when @cl_tp=1 and @jj=1 and FAF.GoAML_get_paramS(@F_BANK_ID,30,1)='0'
															then FAF.GoAML_get_paramS(@F_BANK_ID,30,2) 
															else NULL 
														end
													,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__is_primary'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__is_primary')) as "is_primary",
													(select
														@xml_clt,
														(select
															FAF.GoAML_char_process_WR(
																case
																	when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
																	then
																		'Client number: ' + @kd_nr + 
																		case 
																			when len(partners)>0
																			then ' (partner in the partnership ' + partners + ')' 
																			else ''
																		end + @tpcom
																	else NULL
																end
															,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__comments')) as "comments"
														FOR XML PATH (''),type, ELEMENTS)
													FOR XML PATH (''),type, ELEMENTS) as "t_person",
													FAF.GoAML_char_process(rl,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__role'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__role')) as "role",
													case
														when FAF.GoAML_get_paramS(@F_BANK_ID,105,1)='ok' or dt_fr>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or 
															dt_to<=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112)
														then 
															(select
																FAF.GoAML_char_process(
																	convert(varchar,cast((case when dt_fr>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) then dt_fr else @dt_fr0 end) as datetime),126)
																,FAF.GoAML_get_paramV(@F_BANK_ID,'rdr_valid_from'),FAF.GoAML_get_paramL(@F_BANK_ID,'rdr_valid_from')) as "valid_from",
																case 
																	when dt_fr>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)
																	then NULL
																	else FAF.GoAML_get_paramS(@F_BANK_ID,105,2)
																end as "is_approx_from_date",
																FAF.GoAML_char_process(
																	case 
																		when dt_to<=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112)
																		then convert(varchar,cast(dt_to as datetime),126)
																		else NULL
																	end
																,FAF.GoAML_get_paramV(@F_BANK_ID,'rdr_valid_to'),FAF.GoAML_get_paramL(@F_BANK_ID,'rdr_valid_to')) as "valid_to"
															FOR XML PATH (''),type, ELEMENTS)
														else NULL
													end as "relation_date_range"
												from
													(select rl,
														case when len(partners)>0 then partners else NULL end as partners,
														case 
															when dt_fr = convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or dt_fr = convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112) 
															then NULL 
															else dt_fr 
														end as dt_fr,
														case 
															when dt_to = convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112) or dt_to = convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) 
															then NULL 
															else dt_to 
														end as dt_to
													from
														(select rl,
															max(case when kdnr is not NULL and kdtp is not NULL then 1 else 0 end) as tst,
															max(isnull(ptn,'')) as partners,
															min(
																case 
																	when dt1 is NULL and kdnr is not NULL and kdtp is not NULL 
																	then convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)
																	when dt1 is NULL  
																	then convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112) 
																	else dt1 
																end
															) as dt_fr,
															max(
																case 
																	when dt2 is NULL and kdnr is not NULL and kdtp is not NULL 
																	then convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112) 
																	when dt2 is NULL
																	then convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)
																	else dt2 
																end
															) as dt_to
														from @KD_KD where id>isnull(@mmm,0) and nID=@n_id
														group by rl) s
													where rl is not NULL or tst>0) ss
												FOR XML PATH ('account_related_person'),type, ELEMENTS);
										end;
								end;

								--partnership
								if @kd_tp=2
								begin
									select
										@xml_cl5t = 
											(select
												FAF.GoAML_char_process(@kd_nm,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__name'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__name')) as "name",
												FAF.GoAML_char_process(
													case
														when FAF.GoAML_get_paramS(@F_BANK_ID,78,2) = 'ok'
														then lf.MROS_value
														else FAF.GoAML_get_paramS(@F_BANK_ID,54,2)
													end
												,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_legal_form'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_legal_form')) as "incorporation_legal_form",
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,43,1)='ok'
													then spp.phones
													else NULL
												end as "phones",
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,49,1)='ok'
													then
														coalesce(sea.addresses,
															(select
																FAF.GoAML_get_paramS(@F_BANK_ID,9,2) as "address_type",
																FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address') as "address",
																FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__city') as "city",
																FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__country_code') as "country_code"
															FOR XML PATH ('address'),type, ELEMENTS)
														)
													else sea.addresses
												end as "addresses",
												FAF.GoAML_char_process(cntr1.iso_land_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_country_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_country_code')) as "incorporation_country_code",
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,67,1)='ok' and len(se.uid_nr)>1
													then FAF.GoAML_char_process(se.uid_nr,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__tax_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__tax_number'))
													else NULL
												end as "tax_number",
												FAF.GoAML_char_process(FAF.GoAML_get_paramS(@F_BANK_ID,55,2),FAF.GoAML_get_paramV(@F_BANK_ID,'temc__tax_reg_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__tax_reg_number')) as "tax_reg_number"
											FOR XML PATH (''),type, ELEMENTS),
										@tecom = 
											case 
												when FAF.GoAML_get_paramS(@F_BANK_ID,78,2) = 'ok' and FAF.GoAML_get_paramS(@F_BANK_ID,32,1)=FAF.GoAML_char_process(lf.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_legal_form'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_legal_form'))
												then char(13) + char(10) + coalesce(FAF.GoAML_char_process(lf.comments,FAF.GoAML_get_paramS(@F_BANK_ID,32,2),0),'')
												else ''
											end + 
											case
												when cntr1.iso_land_cd is NULL and FAF.GoAML_char_process(se.land_lnr_nat_bez,NULL,0) is not NULL and FAF.GoAML_char_process(se.iso_land_code_nat,NULL,0) is not NULL
												then char(13) + char(10) + 'Actual country is ' + FAF.GoAML_char_process(se.land_lnr_nat_bez,'',0) + ' (' + FAF.GoAML_char_process(se.iso_land_code_nat,'',0) + ')'
												else ''
											end
									from 
										FAF.FA_MROS_KD_STAMM se
									left join
										FAF.FA_MROS_KD_EMAIL_XML spp
									on spp.kd_lnr=se.kd_lnr and spp.userbk_nr=se.userbk_nr
									left join 
										FAF.FA_MROS_KD_ADR_XML sea
									on sea.kd_lnr=se.kd_lnr and sea.userbk_nr=se.userbk_nr
									left join
										FAF.FA_MROS_INT_LAND cntr1
									on cntr1.iso_land_cd=se.iso_land_code_nat and cntr1.userbk_nr=se.userbk_nr
									left join
										FAF.FA_MROS_REFERENCE_TABLE lf
									on se.userbk_nr=lf.userbk_nr and cast(se.kd_rstat_cd as varchar)=lf.finnova_value and lf.ref_type=8
									where se.kd_lnr=@n_id and se.userbk_nr=@F_BANK_ID and se.partner_flag=0;

									set @xml_cl5 =
										(select
											FAF.GoAML_char_process(e_rl,FAF.GoAML_get_paramV(@F_BANK_ID,'are__account_entity_relation'),FAF.GoAML_get_paramL(@F_BANK_ID,'are__account_entity_relation')) as "account_entity_relation",
											(select
												@xml_cl5t,
												(select
													FAF.GoAML_char_process_WR(
														case
															when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
															then
																'Client number: ' + @kd_nr + 
																case 
																	when partners is not NULL 
																	then char(13) + char(10) + 'It is the partnership of the clients: ' + ltrim(partners)
																	else ''
																end + @tecom
															else NULL
														end
													,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__comments')) as "comments"
												FOR XML PATH (''),type, ELEMENTS)
											FOR XML PATH (''),type, ELEMENTS) as "entity",
											case
												when dt_fr_e>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or dt_to_e<=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112)
												then 
													(select
														FAF.GoAML_char_process(
															convert(varchar,cast((case when dt_fr_e>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) then dt_fr_e else @dt_fr0 end) as datetime),126)
														,FAF.GoAML_get_paramV(@F_BANK_ID,'rdr_valid_from'),FAF.GoAML_get_paramL(@F_BANK_ID,'rdr_valid_from')) as "valid_from",
														case 
															when dt_fr_e>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)
															then NULL
															else FAF.GoAML_get_paramS(@F_BANK_ID,105,2)
														end as "is_approx_from_date",
														case 
															when dt_to_e<=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112)
															then left(convert(varchar,cast(dt_to_e as datetime),126),19)
															else NULL
														end as "valid_to"
													FOR XML PATH (''),type, ELEMENTS)
												else NULL
											end as "relation_date_range"
										from
											(select e_rl,
												case when len(partners)>0 then partners else NULL end as partners,
												case 
													when dt_fr_e = convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or dt_fr_e = convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112) 
													then NULL 
													else dt_fr_e
												end as dt_fr_e,
												case 
													when dt_to_e = convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112) or dt_to_e = convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) 
													then NULL 
													else dt_to_e 
												end as dt_to_e
											from
												(select e_rl,
													max(isnull(ptn,'')) as partners,
													min(
														case 
															when dt1 is NULL and kdnr is not NULL and kdtp is not NULL 
															then convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)
															when dt1 is NULL  
															then convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112) 
															else dt1 
														end
													) as dt_fr_e,
													max(
														case 
															when dt2 is NULL and kdnr is not NULL and kdtp is not NULL 
															then convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112) 
															when dt2 is NULL
															then convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)
															else dt2 
														end
													) as dt_to_e
												from @KD_KD where id>isnull(@mmm,0) and nID=@n_id
												group by e_rl) s
											) ss
										FOR XML PATH ('account_related_entity'),type, ELEMENTS);
								end;

								--entity
								if @kd_tp=3
								begin
									set @xml_cl5t =
										(select
											FAF.GoAML_char_process(se.kd_bez,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__name'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__name')) as "name",
											case
												when FAF.GoAML_get_paramS(@F_BANK_ID,70,1)='ok'
												then FAF.GoAML_char_process(se.kd_bez,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__commercial_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__commercial_name'))
												else NULL
											end as "commercial_name",
											FAF.GoAML_char_process(lf.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_legal_form'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_legal_form')) as "incorporation_legal_form",
											FAF.GoAML_char_process(
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,71,1)='ok'
													then vsb.kd_ausw_nr
													else se.uid_nr
												end
											,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_number')) as "incorporation_number",
											FAF.GoAML_char_process(se.noga_bez,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__business'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__business')) as "business",
											case
												when FAF.GoAML_get_paramS(@F_BANK_ID,43,1)='ok'
												then spp.phones
												else NULL
											end as "phones",
											case
												when FAF.GoAML_get_paramS(@F_BANK_ID,49,1)='ok'
												then
													coalesce(sea.addresses,
														(select
															FAF.GoAML_get_paramS(@F_BANK_ID,7,2) as "address_type",
															FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address') as "address",
															FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__city') as "city",
															FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__country_code') as "country_code"
														FOR XML PATH ('address'),type, ELEMENTS)
													)
												else sea.addresses
											end as "addresses",
											(select FAF.GoAML_char_process(lower(eml.email_adr),FAF.GoAML_get_paramV(@F_BANK_ID,'temc__email'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__email')) as "email"
											from FAF.FA_MROS_KD_EMAIL eml
											join FAF.FA_MROS_REFERENCE_TABLE rt
											on eml.userbk_nr=rt.userbk_nr and cast(eml.mail_art_cd as varchar)=rt.finnova_value and rt.ref_type=7 and rt.MROS_value=FAF.GoAML_get_paramS(@F_BANK_ID,121,2)
											where eml.kd_lnr=se.kd_lnr and eml.userbk_nr=se.userbk_nr and left(eml.email_adr,1)<>'@'
											FOR XML PATH (''),type, ELEMENTS) as "emails",
											(select FAF.GoAML_char_process(ema.email_adr,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__url'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__url')) as "url"
											from FAF.FA_MROS_KD_EMAIL ema 
											where ema.kd_lnr=se.kd_lnr and ema.userbk_nr=se.userbk_nr and ema.mail_art_cd=FAF.GoAML_get_paramS(@F_BANK_ID,122,2)
											FOR XML PATH (''),type, ELEMENTS) as "urls",
											FAF.GoAML_char_process(se.geburt_ort,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_state'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_state')) as "incorporation_state",
											FAF.GoAML_char_process(cntr1.iso_land_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_country_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_country_code')) as "incorporation_country_code",
											FAF.GoAML_char_process(
												case 
													when se.geburt_dat is NULL or se.geburt_dat < convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or cast(se.geburt_dat as date)>@cur_date
													then 
														case 
															when se.geburt_jahr is NULL or se.geburt_jahr<cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) or se.geburt_jahr>year(@cur_date)
															then NULL
															else cast(se.geburt_jahr as varchar) + '-01-01T00:00:00' 
														end 
													else left(convert(varchar,cast(se.geburt_dat as datetime),126),19)
												end 
											,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_date'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_date')) as "incorporation_date",
											FAF.GoAML_char_process(
												case 
													when FAF.GoAML_get_paramS(@F_BANK_ID,28,1)='ok' 
														and ((se.tod_dat >= convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) and cast(se.tod_dat as date)<=@cur_date) or (se.tod_jahr>=cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) and se.tod_jahr<=year(@cur_date)))
													then FAF.GoAML_get_paramS(@F_BANK_ID,28,2) 
													else NULL
												end 
											,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__business_closed'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__business_closed')) as "business_closed",
											FAF.GoAML_char_process(
												case 
													when se.tod_dat is NULL or se.tod_dat < convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or cast(se.tod_dat as date)>@cur_date
													then 
														case 
															when se.tod_jahr is NULL or se.tod_jahr<cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) or se.tod_jahr>year(@cur_date)
															then NULL
															else cast(se.tod_jahr as varchar) + '-01-01T00:00:00' 
														end 
													else left(convert(varchar,cast(se.tod_dat as datetime),126),19)
												end 
											,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__date_business_closed'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__date_business_closed')) as "date_business_closed",
											case
												when FAF.GoAML_get_paramS(@F_BANK_ID,67,1)='ok' and len(se.uid_nr)>1
												then FAF.GoAML_char_process(se.uid_nr,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__tax_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__tax_number'))
												else NULL
											end as "tax_number",
											FAF.GoAML_char_process(
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,104,1)='ok'
													then NULL
													else tax.MROS_value
												end
											,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__tax_reg_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__tax_reg_number')) as "tax_reg_number",
											FAF.GoAML_char_process_WR(
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
													then
														'Client number: ' + se.edit_kd_nr + 
														case
															when cntr1.iso_land_cd is NULL and FAF.GoAML_char_process(se.land_lnr_nat_bez,NULL,0) is not NULL and FAF.GoAML_char_process(se.iso_land_code_nat,NULL,0) is not NULL
															then '; ' + char(13) + char(10) + 'Actual country is ' + FAF.GoAML_char_process(se.land_lnr_nat_bez,'',0) + ' (' + FAF.GoAML_char_process(se.iso_land_code_nat,'',0) + ')'
															else ''
														end + 
														case 
															when FAF.GoAML_get_paramS(@F_BANK_ID,32,1)=FAF.GoAML_char_process(lf.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_legal_form'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_legal_form'))
															then '; ' + char(13) + char(10) + coalesce(FAF.GoAML_char_process(lf.comments,FAF.GoAML_get_paramS(@F_BANK_ID,32,2),0),'')
															else ''
														end
													else NULL
												end
											,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__comments')) as "comments"
										from 
											FAF.FA_MROS_KD_STAMM se
										left join 
											FAF.FA_MROS_KD_ADR_XML sea
										on sea.kd_lnr=se.kd_lnr and sea.userbk_nr=se.userbk_nr
										left join
											FAF.FA_MROS_KD_EMAIL_XML spp
										on spp.kd_lnr=se.kd_lnr and spp.userbk_nr=se.userbk_nr
										left join
											FAF.FA_MROS_INT_LAND cntr1
										on cntr1.iso_land_cd=se.iso_land_code_nat and cntr1.userbk_nr=se.userbk_nr
										left join
											FAF.FA_MROS_REFERENCE_TABLE lf
										on se.userbk_nr=lf.userbk_nr and cast(se.kd_rstat_cd as varchar)=lf.finnova_value and lf.ref_type=8
										left join
											FAF.FA_MROS_REFERENCE_TABLE tax
										on se.userbk_nr=tax.userbk_nr and se.KD_SITZ_GSCH_CD=tax.finnova_value and tax.ref_type=3
										left join
											(select userbk_nr,kd_lnr,kd_ausw_nr,
												row_number() over (partition by userbk_nr,kd_lnr order by ausstell_dat desc) as rn
											from FAF.FA_MROS_KD_VSB where kd_ausw_nr is not NULL and cast(iddoc_cd as varchar)=FAF.GoAML_get_paramS(@F_BANK_ID,71,2)) vsb
										on se.userbk_nr=vsb.userbk_nr and se.kd_lnr=vsb.kd_lnr and vsb.rn=1
										where se.kd_lnr=@n_id and se.userbk_nr=@F_BANK_ID and se.partner_flag=0
										FOR XML PATH (''),type, ELEMENTS);

									set @xml_cl5 =
										(select
											FAF.GoAML_char_process(e_rl,FAF.GoAML_get_paramV(@F_BANK_ID,'are__account_entity_relation'),FAF.GoAML_get_paramL(@F_BANK_ID,'are__account_entity_relation')) as "account_entity_relation",
											@xml_cl5t as "entity",
											case
												when dt_fr_e>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or dt_to_e<=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112)
												then 
													(select
														FAF.GoAML_char_process(
															convert(varchar,cast((case when dt_fr_e>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) then dt_fr_e else @dt_fr0 end) as datetime),126)
														,FAF.GoAML_get_paramV(@F_BANK_ID,'rdr_valid_from'),FAF.GoAML_get_paramL(@F_BANK_ID,'rdr_valid_from')) as "valid_from",
														case 
															when dt_fr_e>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)
															then NULL
															else FAF.GoAML_get_paramS(@F_BANK_ID,105,2)
														end as "is_approx_from_date",
														case 
															when dt_to_e<=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112)
															then left(convert(varchar,cast(dt_to_e as datetime),126),19)
															else NULL
														end as "valid_to"
													FOR XML PATH (''),type, ELEMENTS)
												else NULL
											end as "relation_date_range"
										from
											(select e_rl,
												case 
													when dt_fr_e = convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or dt_fr_e = convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112) 
													then NULL 
													else dt_fr_e
												end as dt_fr_e,
												case 
													when dt_to_e = convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112) or dt_to_e = convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) 
													then NULL 
													else dt_to_e 
												end as dt_to_e
											from
												(select e_rl,
													min(
														case 
															when dt1 is NULL and kdnr is not NULL and kdtp is not NULL 
															then convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)
															when dt1 is NULL  
															then convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112) 
															else dt1 
														end
													) as dt_fr_e,
													max(
														case 
															when dt2 is NULL and kdnr is not NULL and kdtp is not NULL 
															then convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112) 
															when dt2 is NULL
															then convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)
															else dt2 
														end
													) as dt_to_e
												from @KD_KD where id>isnull(@mmm,0) and nID=@n_id
												group by e_rl) s
											) ss
										FOR XML PATH ('account_related_entity'),type, ELEMENTS);
								end;
							end;
						end;
					else
						begin
							--union all records of the client in the array if it is first appearance
							if (select count(*) from @KD_KD where id<@jj + isnull(@mmm,0) and id>isnull(@mmm,0) and nID=@n_id) = 0
							begin
								set @kk = @jj + 1;
								while @kk<=@mm
								begin
									select @rol=rl,@comm=cmm,@partners=ptn,@dt_fr=dt1,@dt_to=dt2,@dt_fr_e=dt1_e,@dt_to_e=dt2_e,@e_rol=e_rl,@e_comm=e_cmm from 
										(select
											case 
												when @bb=0 and len(rl)>0 and rl<>isnull(@rol,'')
												then 
													case
														when len(@rol)>0
														then
															case
																when FAF.GoAML_get_paramS(@F_BANK_ID,41,1)='ok'
																then
																	case
																		when @rol+','+rl in ('13,14', '14,13', '19,13', '13,19', '19,14', '14,19') 
																		then '19'
																		when @rol+','+rl in ('14,7', '7,14', '20,7', '7,20', '14,20', '20,14')
																		then '20'
																		when @rol+','+rl in ('14,15', '15,14', '14,21', '21,14', '21,15', '15,21') 
																		then '21'
																		when @rol+','+rl in ('13,15', '15,13', '22,15', '15,22', '13,22', '22,13') 
																		then '22'
																		when @rol+','+rl in ('15,7', '7,15', '15,23', '23,15', '23,7', '7,23') 
																		then '23'
																		when @rol+','+rl in ('15,19', '19,15', '13,21', '21,13', '14,22', '22,14','21,22', '22,21', '21,19', '19,21', '19,22', '22,19','21,24', '24,21', '24,22', '22,24', '24,19', '19,24') 
																		then '24'
																		when @rol+','+rl in ('15,20', '20,15', '14,23', '23,14', '7,21', '21,7', '20,23', '23,20', '21,23', '23,21', '20,21', '21,20', '20,25', '25,20', '25,23', '23,25', '25,21', '21,25') 
																		then '25'
																		else '6'
																	end
																when @rol=FAF.GoAML_get_paramS(@F_BANK_ID,75,1) then rl
																when rl=FAF.GoAML_get_paramS(@F_BANK_ID,75,1) then @rol
																when @rol=FAF.GoAML_get_paramS(@F_BANK_ID,76,1) then @rol
																when rl=FAF.GoAML_get_paramS(@F_BANK_ID,76,1) then rl
																else FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__role')
															end
														else rl
													end
												else @rol
											end as rl,
											case 
												when @bb=0 and len(e_rl)>0 and e_rl<>isnull(@e_rol,'')
												then 
													case
														when len(@e_rol)>0
														then
															case
																when @e_rol=FAF.GoAML_get_paramS(@F_BANK_ID,93,1) then e_rl
																when e_rl=FAF.GoAML_get_paramS(@F_BANK_ID,93,1) then @e_rol
																else FAF.GoAML_get_paramV(@F_BANK_ID,'are__account_entity_relation')
															end
														else e_rl
													end
												else @e_rol
											end as e_rl,
											case
												when @bb=0 and len(cmm)>0 and (isnull(len(@comm),0)=0 or charindex(cmm,@comm)=0)
												then isnull(@comm,'') + case when len(@comm)>0 then ', ' + char(13) + char(10) + '   ' else '' end + cmm
												else @comm
											end as cmm,
											case
												when @bb=0 and len(e_cmm)>0 and (isnull(len(@e_comm),0)=0 or charindex(e_cmm,@e_comm)=0)
												then isnull(@e_comm,'') + case when len(@e_comm)>0 then ', ' + char(13) + char(10) + '   ' else '' end + e_cmm
												else @e_comm
											end as e_cmm,
											case
												when len(ptn)>0
												then ptn
												else @partners
											end as ptn,
											case 
												when @bb=0 and len(rl)>0 and rl<>isnull(@rol,'')
												then 
													case
														when len(@rol)>0
														then
															case
																when FAF.GoAML_get_paramS(@F_BANK_ID,41,1)='ok'
																then
																	case
																		when dt1 is NULL then @dt_fr
																		when @dt_fr is NULL then dt1
																		when dt1>@dt_fr then dt1
																		else @dt_fr
																	end
																when @rol=FAF.GoAML_get_paramS(@F_BANK_ID,75,1) then dt1
																when rl=FAF.GoAML_get_paramS(@F_BANK_ID,75,1) then @dt_fr
																when @rol=FAF.GoAML_get_paramS(@F_BANK_ID,76,1) then @dt_fr
																when rl=FAF.GoAML_get_paramS(@F_BANK_ID,76,1) then dt1
																when dt1 is NULL then @dt_fr
																when @dt_fr is NULL then dt1
																when dt1>@dt_fr then dt1
																else @dt_fr
															end
														else dt1
													end
												else @dt_fr
											end as dt1,
											case 
												when @bb=0 and len(rl)>0 and rl<>isnull(@rol,'')
												then 
													case
														when len(@rol)>0
														then
															case
																when FAF.GoAML_get_paramS(@F_BANK_ID,41,1)='ok'
																then
																	case
																		when dt2 is NULL then @dt_to
																		when @dt_to is NULL then dt2
																		when dt2>@dt_to then dt2
																		else @dt_to
																	end
																when @rol=FAF.GoAML_get_paramS(@F_BANK_ID,75,1) then dt2
																when rl=FAF.GoAML_get_paramS(@F_BANK_ID,75,1) then @dt_to
																when @rol=FAF.GoAML_get_paramS(@F_BANK_ID,76,1) then @dt_to
																when rl=FAF.GoAML_get_paramS(@F_BANK_ID,76,1) then dt2
																when dt2 is NULL then @dt_to
																when @dt_to is NULL then dt2
																when dt2>@dt_to then dt2
																else @dt_to
															end
														else dt2
													end
												else @dt_to
											end as dt2,
											case 
												when @bb=0 and len(e_rl)>0 and e_rl<>isnull(@e_rol,'')
												then 
													case
														when len(@e_rol)>0
														then
															case
																when @e_rol=FAF.GoAML_get_paramS(@F_BANK_ID,93,1) then dt1
																when e_rl=FAF.GoAML_get_paramS(@F_BANK_ID,93,1) then @dt_fr_e
																when dt1 is NULL then @dt_fr_e
																when @dt_fr_e is NULL then dt1
																when dt1>@dt_fr_e then dt1
																else @dt_fr_e
															end
														else dt1
													end
												else @dt_fr_e
											end as dt1_e,
											case 
												when @bb=0 and len(e_rl)>0 and e_rl<>isnull(@e_rol,'')
												then 
													case
														when len(@e_rol)>0
														then
															case
																when @e_rol=FAF.GoAML_get_paramS(@F_BANK_ID,93,1) then dt2
																when e_rl=FAF.GoAML_get_paramS(@F_BANK_ID,93,1) then @dt_to_e
																when dt2 is NULL then @dt_to_e
																when @dt_to_e is NULL then dt2
																when dt2>@dt_to_e then dt2
																else @dt_to_e
															end
														else dt2
													end
												else @dt_to_e
											end as dt2_e
										from @KD_KD where id=@kk + isnull(@mmm,0) and nID=@n_id) s;

									set @kk = @kk + 1;
								end;

								--person
								if @kd_tp=1
								begin
									set @xml_cl =
										(select
											FAF.GoAML_char_process(
												case 
													when @cl_tp=1 and @jj=1 and FAF.GoAML_get_paramS(@F_BANK_ID,30,1)='0'
													then FAF.GoAML_get_paramS(@F_BANK_ID,30,2) 
													else NULL 
												end
											,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__is_primary'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__is_primary')) as "is_primary",
											(select
												FAF.GoAML_char_process(gd.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__gender'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__gender')) as "gender",
												FAF.GoAML_char_process(se.titel,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__title'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__title')) as "title",
												FAF.GoAML_char_process(se.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__first_name')) as "first_name",
												case 
													when FAF.GoAML_get_paramS(@F_BANK_ID,44,1)='ok'
													then upper(FAF.GoAML_char_process(se.name_1,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__last_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__last_name')))
													else FAF.GoAML_char_process(se.name_1,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__last_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__last_name'))
												end as "last_name",
												FAF.GoAML_char_process(
													case 
														when se.geburt_dat is NULL or se.geburt_dat < convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or cast(se.geburt_dat as date)>@cur_date
														then 
															case 
																when se.geburt_jahr is NULL or se.geburt_jahr<cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) or se.geburt_jahr>year(@cur_date)
																then NULL
																else cast(se.geburt_jahr as varchar) + '-01-01T00:00:00' 
															end 
														else left(convert(varchar,cast(se.geburt_dat as datetime),126),19)
													end
												,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__birthdate'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__birthdate')) as "birthdate",
												FAF.GoAML_char_process(se.geburt_ort,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__birth_place'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__birth_place')) as "birth_place",
												FAF.GoAML_char_process(
													case
														when FAF.GoAML_get_paramS(@F_BANK_ID,42,1)='ok'
														then
															case 
																when FAF.GoAML_get_paramS(@F_BANK_ID,98,1)='ok'
																then
																	case 
																		when FAF.GoAML_get_paramS(@F_BANK_ID,64,1)='ok'
																		then
																			case 
																				when isnull(se.kd_pep_cd,se.kd_pep_cd_auto) is NULL then 'UNBEKANNT'
																				when isnull(se.kd_pep_cd,se.kd_pep_cd_auto) = '0' then 'NEIN'
																				else 'JA'
																			end
																		else
																			case 
																				when isnull(se.kd_pep_cd,se.kd_pep_cd_auto) is NULL then 'unbekannt'
																				when isnull(se.kd_pep_cd,se.kd_pep_cd_auto) = '0' then 'nein'
																				else 'ja'
																			end
																	end
																else
																	case 
																		when FAF.GoAML_get_paramS(@F_BANK_ID,64,1)='ok'
																		then
																			case 
																				when se.kd_pep_cd is NULL then 'UNBEKANNT'
																				when se.kd_pep_cd = '0' then 'NEIN'
																				else 'JA'
																			end
																		else
																			case 
																				when se.kd_pep_cd is NULL then 'unbekannt'
																				when se.kd_pep_cd = '0' then 'nein'
																				else 'ja'
																			end
																	end
															end
														else NULL
													end
												,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__id_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__id_number')) as "id_number",
												FAF.GoAML_char_process(cntr1.iso_land_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__nationality1'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__nationality1')) as "nationality1",
												FAF.GoAML_char_process(cntr2.iso_land_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__nationality2'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__nationality2')) as "nationality2",
												spp.phones as "phones",
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,47,1)='ok'
													then
														coalesce(sea.addresses,
															(select
																FAF.GoAML_get_paramS(@F_BANK_ID,8,2) as "address_type",
																FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address') as "address",
																FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__city') as "city",
																FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__country_code') as "country_code"
															FOR XML PATH ('address'),type, ELEMENTS)
														)
													else sea.addresses
												end as "addresses",
												(select FAF.GoAML_char_process(lower(eml.email_adr),FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__email'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__email')) as "email"
												from FAF.FA_MROS_KD_EMAIL eml
												join FAF.FA_MROS_REFERENCE_TABLE rt
												on eml.userbk_nr=rt.userbk_nr and cast(eml.mail_art_cd as varchar)=rt.finnova_value and rt.ref_type=7 and rt.MROS_value=FAF.GoAML_get_paramS(@F_BANK_ID,121,2)
												where eml.kd_lnr=se.kd_lnr and eml.userbk_nr=se.userbk_nr and left(eml.email_adr,1)<>'@'
												FOR XML PATH (''),type, ELEMENTS) as "emails",
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,48,1)='ok'
													then
														coalesce(spi.identification, 
															(select
																FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__type') as "type",
																FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__number') as "number",
																FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__issue_country') as "issue_country",
																FAF.GoAML_char_process(
																	case
																		when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
																		then
																			case
																				when FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__type')=FAF.GoAML_get_paramS(@F_BANK_ID,6,1)
																				then FAF.GoAML_get_paramS(@F_BANK_ID,6,2)
																				else NULL
																			end
																		else NULL
																	end
																,FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpi__comments')) as "comments"
															FOR XML PATH ('identification'),type, ELEMENTS)
														)
													else spi.identification
												end as "identifications",
												FAF.GoAML_char_process(
													case 
														when FAF.GoAML_get_paramS(@F_BANK_ID,34,1)='ok' and ((se.tod_dat >= convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) and cast(se.tod_dat as date)<=@cur_date)
															or (se.tod_jahr>=cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) and se.tod_jahr<=year(@cur_date)))
														then FAF.GoAML_get_paramS(@F_BANK_ID,34,2)
														else NULL
													end
												,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__deceased'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__deceased')) as "deceased",
												FAF.GoAML_char_process(
													case 
														when se.tod_dat is NULL or se.tod_dat < convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or cast(se.tod_dat as date)>@cur_date
														then 
															case 
																when se.tod_jahr is NULL or se.tod_jahr<cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) or se.tod_jahr>year(@cur_date)
																then NULL
																else cast(se.tod_jahr as varchar) + '-01-01T00:00:00' 
															end 
														else left(convert(varchar,cast(se.tod_dat as datetime),126),19)
													end
												,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__date_deceased'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__date_deceased')) as "date_deceased",
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,68,1)='ok' and len(se.uid_nr)>1
													then FAF.GoAML_char_process(se.uid_nr,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__tax_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__tax_number'))
													else NULL
												end as "tax_number",
												FAF.GoAML_char_process_WR(
													case
														when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
														then
															'Client number: ' + @kd_nr + 
															case 
																when len(@partners)>0
																then ' (partner in the partnership ' + @partners + ')' 
																else ''
															end +
															case
																when cntr1.iso_land_cd is NULL and FAF.GoAML_char_process(se.land_lnr_nat_bez,NULL,0) is not NULL and FAF.GoAML_char_process(se.iso_land_code_nat,NULL,0) is not NULL
																then '; ' + char(13) + char(10) + 'Actual country is ' + FAF.GoAML_char_process(se.land_lnr_nat_bez,'',0) + ' (' + FAF.GoAML_char_process(se.iso_land_code_nat,'',0) + ')'
																else ''
															end
														else NULL
													end
												,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__comments')) as "comments"
											FOR XML PATH (''),type, ELEMENTS) as "t_person",
											FAF.GoAML_char_process(@rol,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__role'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__role')) as "role",
											case
												when FAF.GoAML_get_paramS(@F_BANK_ID,105,1)='ok' or @dt_fr>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or 
													@dt_to<=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112)
												then 
													(select
														FAF.GoAML_char_process(
															convert(varchar,cast((case when @dt_fr>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) then @dt_fr else @dt_fr0 end) as datetime),126)
														,FAF.GoAML_get_paramV(@F_BANK_ID,'rdr_valid_from'),FAF.GoAML_get_paramL(@F_BANK_ID,'rdr_valid_from')) as "valid_from",
														case 
															when @dt_fr>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)
															then NULL
															else FAF.GoAML_get_paramS(@F_BANK_ID,105,2)
														end as "is_approx_from_date",
														FAF.GoAML_char_process(
															case 
																when @dt_to<=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112)
																then convert(varchar,cast(@dt_to as datetime),126)
																else NULL
															end
														,FAF.GoAML_get_paramV(@F_BANK_ID,'rdr_valid_to'),FAF.GoAML_get_paramL(@F_BANK_ID,'rdr_valid_to')) as "valid_to"
													FOR XML PATH (''),type, ELEMENTS)
												else NULL
											end as "relation_date_range",
											case
												when FAF.GoAML_char_process(@rol,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__role'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__role')) = FAF.GoAML_get_paramS(@F_BANK_ID,20,1)
												then 'Role(s) of the client: ' + char(13) + char(10) + '   ' + isnull(@comm,FAF.GoAML_get_paramS(@F_BANK_ID,20,2))
												else NULL
											end as "comments"
										from 
											FAF.FA_MROS_KD_STAMM se
										left join 
											FAF.FA_MROS_KD_ADR_XML sea
										on sea.kd_lnr=se.kd_lnr and sea.userbk_nr=se.userbk_nr
										left join
											FAF.FA_MROS_KD_EMAIL_XML spp
										on spp.kd_lnr=se.kd_lnr and spp.userbk_nr=se.userbk_nr
										left join
											FAF.FA_MROS_KD_VSB_XML spi
										on spi.kd_lnr=se.kd_lnr and spi.userbk_nr=se.userbk_nr
										left join
											FAF.FA_MROS_INT_LAND cntr1
										on cntr1.iso_land_cd=se.iso_land_code_nat and cntr1.userbk_nr=se.userbk_nr
										left join
											FAF.FA_MROS_INT_LAND cntr2
										on cntr2.iso_land_cd=se.iso_land_code_nat_2 and cntr2.userbk_nr=se.userbk_nr
										left join
											FAF.FA_MROS_REFERENCE_TABLE gd
										on se.userbk_nr=gd.userbk_nr and cast(se.GESCHLECHT_CD as varchar)=gd.finnova_value and gd.ref_type=4
										where se.kd_lnr=@n_id and se.userbk_nr=@F_BANK_ID and se.partner_flag=0
										FOR XML PATH ('account_related_person'),type, ELEMENTS);
								end;

								--partnership
								if @kd_tp=2
								begin
									set @xml_cl5 =
										(select
											FAF.GoAML_char_process(@e_rol,FAF.GoAML_get_paramV(@F_BANK_ID,'are__account_entity_relation'),FAF.GoAML_get_paramL(@F_BANK_ID,'are__account_entity_relation')) as "account_entity_relation",
											(select
												FAF.GoAML_char_process(@kd_nm,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__name'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__name')) as "name",
												FAF.GoAML_char_process(
													case
														when FAF.GoAML_get_paramS(@F_BANK_ID,78,2) = 'ok'
														then lf.MROS_value
														else FAF.GoAML_get_paramS(@F_BANK_ID,54,2)
													end
												,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_legal_form'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_legal_form')) as "incorporation_legal_form",
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,43,1)='ok'
													then spp.phones
													else NULL
												end as "phones",
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,49,1)='ok'
													then
														coalesce(sea.addresses,
															(select
																FAF.GoAML_get_paramS(@F_BANK_ID,9,2) as "address_type",
																FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address') as "address",
																FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__city') as "city",
																FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__country_code') as "country_code"
															FOR XML PATH ('address'),type, ELEMENTS)
														)
													else sea.addresses
												end as "addresses",
												FAF.GoAML_char_process(cntr1.iso_land_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_country_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_country_code')) as "incorporation_country_code",
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,67,1)='ok' and len(se.uid_nr)>1
													then FAF.GoAML_char_process(se.uid_nr,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__tax_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__tax_number'))
													else NULL
												end as "tax_number",
												FAF.GoAML_char_process(FAF.GoAML_get_paramS(@F_BANK_ID,55,2),FAF.GoAML_get_paramV(@F_BANK_ID,'temc__tax_reg_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__tax_reg_number')) as "tax_reg_number",
												FAF.GoAML_char_process_WR(
													case
														when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
														then
															'Client number: ' + @kd_nr + 
															case 
																when @partners is not NULL 
																then char(13) + char(10) + 'It is the partnership of the clients: ' + ltrim(@partners)
																else ''
															end + 
															case 
																when FAF.GoAML_get_paramS(@F_BANK_ID,78,2) = 'ok' and FAF.GoAML_get_paramS(@F_BANK_ID,32,1)=FAF.GoAML_char_process(lf.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_legal_form'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_legal_form'))
																then char(13) + char(10) + coalesce(FAF.GoAML_char_process(lf.comments,FAF.GoAML_get_paramS(@F_BANK_ID,32,2),0),'')
																else ''
															end + 
															case
																when cntr1.iso_land_cd is NULL and FAF.GoAML_char_process(se.land_lnr_nat_bez,NULL,0) is not NULL and FAF.GoAML_char_process(se.iso_land_code_nat,NULL,0) is not NULL
																then char(13) + char(10) + 'Actual country is ' + FAF.GoAML_char_process(se.land_lnr_nat_bez,'',0) + ' (' + FAF.GoAML_char_process(se.iso_land_code_nat,'',0) + ')'
																else ''
															end
														else NULL
													end
												,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__comments')) as "comments"
											FOR XML PATH (''),type, ELEMENTS) as "entity",
											case
												when @dt_fr_e>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or @dt_to_e<=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112)
												then 
													(select
														FAF.GoAML_char_process(
															convert(varchar,cast((case when @dt_fr_e>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) then @dt_fr_e else @dt_fr0 end) as datetime),126)
														,FAF.GoAML_get_paramV(@F_BANK_ID,'rdr_valid_from'),FAF.GoAML_get_paramL(@F_BANK_ID,'rdr_valid_from')) as "valid_from",
														case 
															when @dt_fr_e>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)
															then NULL
															else FAF.GoAML_get_paramS(@F_BANK_ID,105,2)
														end as "is_approx_from_date",
														case 
															when @dt_to_e<=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112)
															then left(convert(varchar,cast(@dt_to_e as datetime),126),19)
															else NULL
														end as "valid_to"
													FOR XML PATH (''),type, ELEMENTS)
												else NULL
											end as "relation_date_range",
											case
												when @e_rol = FAF.GoAML_get_paramS(@F_BANK_ID,21,1) or @e_rol is NULL
												then 'Role(s) of the client: ' + char(13) + char(10) + '   ' + isnull(@e_comm,FAF.GoAML_get_paramS(@F_BANK_ID,21,2))
												else NULL
											end as "comments"
										from 
											FAF.FA_MROS_KD_STAMM se
										left join
											FAF.FA_MROS_KD_EMAIL_XML spp
										on spp.kd_lnr=se.kd_lnr and spp.userbk_nr=se.userbk_nr
										left join 
											FAF.FA_MROS_KD_ADR_XML sea
										on sea.kd_lnr=se.kd_lnr and sea.userbk_nr=se.userbk_nr
										left join
											FAF.FA_MROS_INT_LAND cntr1
										on cntr1.iso_land_cd=se.iso_land_code_nat and cntr1.userbk_nr=se.userbk_nr
										left join
											FAF.FA_MROS_REFERENCE_TABLE lf
										on se.userbk_nr=lf.userbk_nr and cast(se.kd_rstat_cd as varchar)=lf.finnova_value and lf.ref_type=8
										where se.kd_lnr=@n_id and se.userbk_nr=@F_BANK_ID and se.partner_flag=0
										FOR XML PATH ('account_related_entity'),type, ELEMENTS);
								end;

								--entity
								if @kd_tp=3
								begin
									set @xml_cl5 =
										(select
											FAF.GoAML_char_process(@e_rol,FAF.GoAML_get_paramV(@F_BANK_ID,'are__account_entity_relation'),FAF.GoAML_get_paramL(@F_BANK_ID,'are__account_entity_relation')) as "account_entity_relation",
											(select
												FAF.GoAML_char_process(se.kd_bez,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__name'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__name')) as "name",
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,70,1)='ok'
													then FAF.GoAML_char_process(se.kd_bez,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__commercial_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__commercial_name'))
													else NULL
												end as "commercial_name",
												FAF.GoAML_char_process(lf.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_legal_form'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_legal_form')) as "incorporation_legal_form",
												FAF.GoAML_char_process(
													case
														when FAF.GoAML_get_paramS(@F_BANK_ID,71,1)='ok'
														then vsb.kd_ausw_nr
														else se.uid_nr
													end
												,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_number')) as "incorporation_number",
												FAF.GoAML_char_process(se.noga_bez,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__business'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__business')) as "business",
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,43,1)='ok'
													then spp.phones
													else NULL
												end as "phones",
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,49,1)='ok'
													then
														coalesce(sea.addresses,
															(select
																FAF.GoAML_get_paramS(@F_BANK_ID,7,2) as "address_type",
																FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address') as "address",
																FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__city') as "city",
																FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__country_code') as "country_code"
															FOR XML PATH ('address'),type, ELEMENTS)
														)
													else sea.addresses
												end as "addresses",
												(select FAF.GoAML_char_process(lower(eml.email_adr),FAF.GoAML_get_paramV(@F_BANK_ID,'temc__email'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__email')) as "email"
												from FAF.FA_MROS_KD_EMAIL eml
												join FAF.FA_MROS_REFERENCE_TABLE rt
												on eml.userbk_nr=rt.userbk_nr and cast(eml.mail_art_cd as varchar)=rt.finnova_value and rt.ref_type=7 and rt.MROS_value=FAF.GoAML_get_paramS(@F_BANK_ID,121,2)
												where eml.kd_lnr=se.kd_lnr and eml.userbk_nr=se.userbk_nr and left(eml.email_adr,1)<>'@'
												FOR XML PATH (''),type, ELEMENTS) as "emails",
												(select FAF.GoAML_char_process(ema.email_adr,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__url'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__url')) as "url"
												from FAF.FA_MROS_KD_EMAIL ema 
												where ema.kd_lnr=se.kd_lnr and ema.userbk_nr=se.userbk_nr and ema.mail_art_cd=FAF.GoAML_get_paramS(@F_BANK_ID,122,2)
												FOR XML PATH (''),type, ELEMENTS) as "urls",
												FAF.GoAML_char_process(se.geburt_ort,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_state'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_state')) as "incorporation_state",
												FAF.GoAML_char_process(cntr1.iso_land_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_country_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_country_code')) as "incorporation_country_code",
												FAF.GoAML_char_process(
													case 
														when se.geburt_dat is NULL or se.geburt_dat < convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or cast(se.geburt_dat as date)>@cur_date
														then 
															case 
																when se.geburt_jahr is NULL or se.geburt_jahr<cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) or se.geburt_jahr>year(@cur_date)
																then NULL
																else cast(se.geburt_jahr as varchar) + '-01-01T00:00:00' 
															end 
														else left(convert(varchar,cast(se.geburt_dat as datetime),126),19)
													end 
												,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_date'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_date')) as "incorporation_date",
												FAF.GoAML_char_process(
													case 
														when FAF.GoAML_get_paramS(@F_BANK_ID,28,1)='ok' 
															and ((se.tod_dat >= convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) and cast(se.tod_dat as date)<=@cur_date) or (se.tod_jahr>=cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) and se.tod_jahr<=year(@cur_date)))
														then FAF.GoAML_get_paramS(@F_BANK_ID,28,2) 
														else NULL
													end 
												,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__business_closed'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__business_closed')) as "business_closed",
												FAF.GoAML_char_process(
													case 
														when se.tod_dat is NULL or se.tod_dat < convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or cast(se.tod_dat as date)>@cur_date
														then 
															case 
																when se.tod_jahr is NULL or se.tod_jahr<cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) or se.tod_jahr>year(@cur_date)
																then NULL
																else cast(se.tod_jahr as varchar) + '-01-01T00:00:00' 
															end 
														else left(convert(varchar,cast(se.tod_dat as datetime),126),19)
													end 
												,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__date_business_closed'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__date_business_closed')) as "date_business_closed",
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,67,1)='ok' and len(se.uid_nr)>1
													then FAF.GoAML_char_process(se.uid_nr,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__tax_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__tax_number'))
													else NULL
												end as "tax_number",
												FAF.GoAML_char_process(
													case
														when FAF.GoAML_get_paramS(@F_BANK_ID,104,1)='ok'
														then NULL
														else tax.MROS_value
													end
												,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__tax_reg_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__tax_reg_number')) as "tax_reg_number",
												FAF.GoAML_char_process_WR(
													case
														when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
														then
															'Client number: ' + se.edit_kd_nr + 
															case
																when cntr1.iso_land_cd is NULL and FAF.GoAML_char_process(se.land_lnr_nat_bez,NULL,0) is not NULL and FAF.GoAML_char_process(se.iso_land_code_nat,NULL,0) is not NULL
																then '; ' + char(13) + char(10) + 'Actual country is ' + FAF.GoAML_char_process(se.land_lnr_nat_bez,'',0) + ' (' + FAF.GoAML_char_process(se.iso_land_code_nat,'',0) + ')'
																else ''
															end + 
															case 
																when FAF.GoAML_get_paramS(@F_BANK_ID,32,1)=FAF.GoAML_char_process(lf.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_legal_form'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_legal_form'))
																then '; ' + char(13) + char(10) + coalesce(FAF.GoAML_char_process(lf.comments,FAF.GoAML_get_paramS(@F_BANK_ID,32,2),0),'')
																else ''
															end
														else NULL
													end
												,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__comments')) as "comments"
											FOR XML PATH (''),type, ELEMENTS) as "entity",
											case
												when @dt_fr_e>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or @dt_to_e<=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112)
												then 
													(select
														FAF.GoAML_char_process(
															convert(varchar,cast((case when @dt_fr_e>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) then @dt_fr_e else @dt_fr0 end) as datetime),126)
														,FAF.GoAML_get_paramV(@F_BANK_ID,'rdr_valid_from'),FAF.GoAML_get_paramL(@F_BANK_ID,'rdr_valid_from')) as "valid_from",
														case 
															when @dt_fr_e>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)
															then NULL
															else FAF.GoAML_get_paramS(@F_BANK_ID,105,2)
														end as "is_approx_from_date",
														case 
															when @dt_to_e<=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,2),112)
															then left(convert(varchar,cast(@dt_to_e as datetime),126),19)
															else NULL
														end as "valid_to"
													FOR XML PATH (''),type, ELEMENTS)
												else NULL
											end as "relation_date_range",
											case
												when @e_rol = FAF.GoAML_get_paramS(@F_BANK_ID,21,1) or @e_rol is NULL
												then 'Role(s) of the client: ' + char(13) + char(10) + '   ' + isnull(@e_comm,FAF.GoAML_get_paramS(@F_BANK_ID,21,2))
												else NULL
											end as "comments"
										from 
											FAF.FA_MROS_KD_STAMM se
										left join 
											FAF.FA_MROS_KD_ADR_XML sea
										on sea.kd_lnr=se.kd_lnr and sea.userbk_nr=se.userbk_nr
										left join
											FAF.FA_MROS_KD_EMAIL_XML spp
										on spp.kd_lnr=se.kd_lnr and spp.userbk_nr=se.userbk_nr
										left join
											FAF.FA_MROS_INT_LAND cntr1
										on cntr1.iso_land_cd=se.iso_land_code_nat and cntr1.userbk_nr=se.userbk_nr
										left join
											FAF.FA_MROS_REFERENCE_TABLE lf
										on se.userbk_nr=lf.userbk_nr and cast(se.kd_rstat_cd as varchar)=lf.finnova_value and lf.ref_type=8
										left join
											FAF.FA_MROS_REFERENCE_TABLE tax
										on se.userbk_nr=tax.userbk_nr and se.KD_SITZ_GSCH_CD=tax.finnova_value and tax.ref_type=3
										left join
											(select userbk_nr,kd_lnr,kd_ausw_nr,
												row_number() over (partition by userbk_nr,kd_lnr order by ausstell_dat desc) as rn
											from FAF.FA_MROS_KD_VSB where kd_ausw_nr is not NULL and cast(iddoc_cd as varchar)=FAF.GoAML_get_paramS(@F_BANK_ID,71,2)) vsb
										on se.userbk_nr=vsb.userbk_nr and se.kd_lnr=vsb.kd_lnr and vsb.rn=1
										where se.kd_lnr=@n_id and se.userbk_nr=@F_BANK_ID and se.partner_flag=0
										FOR XML PATH ('account_related_entity'),type, ELEMENTS);
								end;								
							end;
						end;
					
					if @xml_cl is not NULL
						set @xml_kd = (select @xml_kd,@xml_cl FOR XML PATH (''),type, ELEMENTS);
					if @xml_cl5 is not NULL
						set @xml_kd5 = (select @xml_kd5,@xml_cl5 FOR XML PATH (''),type, ELEMENTS);
					set @jj = @jj + 1;
				end;

				set @xml_cl = 
					(select 
						@xml_cl0,
						@xml_kd5 as "related_entities",
						case 
							when FAF.GoAML_get_paramS(@F_BANK_ID,50,1)='ok' and @xml_kd is NULL 
							then '' 
							else @xml_kd 
						end as "related_persons" 
					FOR XML PATH (''));

				--if @xml_cl is NULL
				--begin
				--	set @xml_cl = 
				--		(select
				--			@xml_dp as "account_related_person/t_person",
				--			case
				--				when FAF.GoAML_get_paramS(@F_BANK_ID,60,1)='ok'
				--				then FAF.GoAML_get_paramS(@F_BANK_ID,60,2)
				--				else FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__role')
				--			end as "account_related_person/role",
				--			case
				--				when FAF.GoAML_get_paramS(@F_BANK_ID,105,1)='ok'
				--				then 
				--					(select
				--						FAF.GoAML_get_paramV(@F_BANK_ID,'rdr_valid_from') as "valid_from",
				--						FAF.GoAML_get_paramS(@F_BANK_ID,105,2) as "is_approx_from_date"
				--					FOR XML PATH (''),type, ELEMENTS)
				--				else NULL
				--			end as "account_related_person/relation_date_range"
				--		FOR XML PATH ('related_persons'),type, ELEMENTS);
				--end;

				if FAF.GoAML_get_paramS(@F_BANK_ID,86,1)='ok'
				begin
					select @kt_nr = edit_kt_nr, @curr=iso_wrg_cd from FAF.FA_MROS_KT_STAMM where kt_lnr=@kt_id and userbk_nr=@F_BANK_ID;

					set @ac_fnd = (select count(*) from FAF.FA_MROS_KT_STAMM where edit_kt_nr=@kt_nr and kd_lnr=@cl_id and userbk_nr=@F_BANK_ID);
					if @ac_fnd>1
					begin
						set @xml_ac_fnd = 
							(select 
								FAF.GoAML_char_process(wrg.iso_wrg_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'af__currency_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'af__currency_code')) as "currency_code",
								FAF.GoAML_char_process(
									cast(cast(sld.saldo_kwrg as decimal(15,2)) as varchar)
								,FAF.GoAML_get_paramV(@F_BANK_ID,'af__currency_balance'),FAF.GoAML_get_paramL(@F_BANK_ID,'af__currency_balance')) as "currency_balance",
								FAF.GoAML_char_process(convert(varchar,@cur_date,23) + 'T00:00:00',FAF.GoAML_get_paramV(@F_BANK_ID,'af__currency_balance_date'),FAF.GoAML_get_paramL(@F_BANK_ID,'af__currency_balance_date')) as "currency_balance_date"
							from 
								FAF.FA_MROS_KT_STAMM kt 
							join
								FAF.FA_MROS_KT_SALDO sld 
							on kt.kt_lnr=sld.kt_lnr and kt.userbk_nr=sld.userbk_nr
							left join
								FAF.FA_MROS_INT_WRG wrg
							on kt.iso_wrg_cd=wrg.iso_wrg_cd and kt.userbk_nr=wrg.userbk_nr
							where kt.edit_kt_nr=@kt_nr and kt.kd_lnr=@cl_id and kt.userbk_nr=@F_BANK_ID
							FOR XML PATH ('account_fund'),type, ELEMENTS);
					end;

					if FAF.GoAML_get_paramS(@F_BANK_ID,117,1)='ok' and @curr<>FAF.GoAML_get_paramS(@F_BANK_ID,117,2)
					begin
						set @xml_ac_fnd = 
							(select 
								FAF.GoAML_char_process(FAF.GoAML_get_paramS(@F_BANK_ID,117,2),FAF.GoAML_get_paramV(@F_BANK_ID,'af__currency_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'af__currency_code')) as "currency_code",
								FAF.GoAML_char_process(
									cast(cast(isnull(sld.saldo_bwrg,0) as decimal(15,2)) as varchar)
								,FAF.GoAML_get_paramV(@F_BANK_ID,'af__currency_balance'),FAF.GoAML_get_paramL(@F_BANK_ID,'af__currency_balance')) as "currency_balance",
								FAF.GoAML_char_process(convert(varchar,@cur_date,23) + 'T00:00:00',FAF.GoAML_get_paramV(@F_BANK_ID,'af__currency_balance_date'),FAF.GoAML_get_paramL(@F_BANK_ID,'af__currency_balance_date')) as "currency_balance_date"
							from 
								FAF.FA_MROS_KT_STAMM kt 
							left join
								FAF.FA_MROS_KT_SALDO sld 
							on kt.kt_lnr=sld.kt_lnr and kt.userbk_nr=sld.userbk_nr
							where kt.kt_lnr=@kt_id and kt.userbk_nr=@F_BANK_ID
							FOR XML PATH ('account_fund'),type, ELEMENTS);
					end;
				end;

				set @xml_ac = 
					(select 
						FAF.GoAML_char_process(@inst_name,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__institution_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__institution_name')) as "institution_name",
						FAF.GoAML_char_process(@swif,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__swift'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__swift')) as "swift",
						case
							when FAF.GoAML_get_paramS(@F_BANK_ID,85,1)='ok'
							then FAF.GoAML_char_process(@cntr,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__institution_country'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__institution_country'))
							else NULL
						end as "institution_country",
						FAF.GoAML_char_process(
							case
								when FAF.GoAML_get_paramS(@F_BANK_ID,37,1)='ok'
								then stz.MROS_value
								else NULL
							end
						,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__branch'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__branch')) as "branch",
						case 
							when FAF.GoAML_get_paramS(@F_BANK_ID,89,1)='ok'
							then 
								FAF.GoAML_char_process(
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,38,1)='ok'
										then 
											case
												when FAF.GoAML_get_paramS(@F_BANK_ID,110,1)='ok' and s5.iban_id is NULL
												then FAF.GoAML_get_paramS(@F_BANK_ID,90,2)
												when len(s5.iban_id)<15 
												then FAF.GoAML_get_paramS(@F_BANK_ID,91,1)
												when substring(s5.iban_id,3,2) not like '[0-9][0-9]' 
												then FAF.GoAML_get_paramS(@F_BANK_ID,91,1)
												when (select count(*) from FAF.FA_MROS_INT_LAND where userbk_nr=@F_BANK_ID and iso_land_cd=substring(s5.iban_id,1,2))=0 
												then FAF.GoAML_get_paramS(@F_BANK_ID,91,1)
												else FAF.GoAML_get_paramS(@F_BANK_ID,90,1)
											end
										else FAF.GoAML_get_paramS(@F_BANK_ID,90,2)
									end
								,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__account_category'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__account_category'))
							else NULL
						end as "account_category",
						FAF.GoAML_char_process(
							case
								when FAF.GoAML_get_paramS(@F_BANK_ID,38,1)='ok'
								then 
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,110,1)='ok'
										then isnull(s5.iban_id,s5.edit_kt_nr)
										else s5.iban_id
									end
								else s5.edit_kt_nr
							end
						,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__account'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__account')) as "account",
						FAF.GoAML_char_process(wrg.iso_wrg_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__currency_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__currency_code')) as "currency_code",
						case
							when FAF.GoAML_get_paramS(@F_BANK_ID,86,1)='ok'
							then @xml_ac_fnd
							else NULL
						end as "account_funds",
						FAF.GoAML_char_process(coalesce(s5.rubrik,s5.kt_art_nr_bez),FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__account_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__account_name')) as "account_name",
						FAF.GoAML_char_process(
							case 
								when len(s5.iban_id)<15 then NULL
								when substring(s5.iban_id,3,2) not like '[0-9][0-9]' then NULL
								when (select count(*) from FAF.FA_MROS_INT_LAND where userbk_nr=@F_BANK_ID and iso_land_cd=substring(s5.iban_id,1,2))=0 then NULL
								else s5.iban_id
							end
						,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__iban'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__iban')) as "iban",
						FAF.GoAML_char_process(@cl_nr,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__client_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__client_number')) as "client_number",
						FAF.GoAML_char_process(isnull(ac_tp.MROS_value,ac_tp1.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__account_type'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__account_type')) as "account_type",
						--account owner
						@xml_cl,
						FAF.GoAML_char_process(
							case 
								when s5.eroeff_dat>=convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) and cast(s5.eroeff_dat as date)<=@cur_date
								then left(convert(varchar,cast(s5.eroeff_dat as datetime),126),19) 
								else NULL
							end
						,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__opened'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__opened')) as "opened",
						FAF.GoAML_char_process(
							case 
								when s5.aufheb_dat>=coalesce(s5.eroeff_dat,convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)) and cast(s5.aufheb_dat as date)<=@cur_date
								then left(convert(varchar,cast(s5.aufheb_dat as datetime),126),19)
								else NULL
							end
						,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__closed'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__closed')) as "closed",
						FAF.GoAML_char_process(
							case 
								when FAF.GoAML_get_paramS(@F_BANK_ID,73,1)='ok' and s5.aufheb_dat is not NULL and cast(s5.aufheb_dat as date)<=@cur_date and s5.aufheb_dat>=coalesce(s5.eroeff_dat,convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112))
								then FAF.GoAML_get_paramS(@F_BANK_ID,73,2)
								when FAF.GoAML_get_paramS(@F_BANK_ID,16,1)='ok' 
									and (s5.aufheb_dat is NULL or s5.aufheb_dat<coalesce(s5.eroeff_dat,convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)) or cast(s5.aufheb_dat as date)>@cur_date)
								then 
									coalesce(cast(cast(
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,111,1)='ok'
											then sld.saldo_kwrg
											else sld.saldo_bwrg
										end
									as decimal(15,2)) as varchar),FAF.GoAML_get_paramS(@F_BANK_ID,16,2)) 
								else NULL
							end
						,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__balance'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__balance')) as "balance",
						FAF.GoAML_char_process(
							case 
								when FAF.GoAML_get_paramS(@F_BANK_ID,73,1)='ok' and s5.aufheb_dat is not NULL and cast(s5.aufheb_dat as date)<=@cur_date and s5.aufheb_dat>=coalesce(s5.eroeff_dat,convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112))
								then convert(varchar,s5.aufheb_dat,23) + 'T00:00:00'
								when FAF.GoAML_get_paramS(@F_BANK_ID,16,1)='ok' 
									and (s5.aufheb_dat is NULL or s5.aufheb_dat<coalesce(s5.eroeff_dat,convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)) or cast(s5.aufheb_dat as date)>@cur_date)
								then convert(varchar,@cur_date,23) + 'T00:00:00'
								else NULL
							end
						,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__date_balance'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__date_balance')) as "date_balance",
						FAF.GoAML_char_process(
							case 
								when FAF.GoAML_get_paramS(@F_BANK_ID,66,1)='ok'
								then 
									case 
										when s5.aufheb_cd is NULL 
										then FAF.GoAML_get_paramS(@F_BANK_ID,18,1)
										else stcd.MROS_value
									end
								else
									case 
										when (s5.aufheb_dat>=coalesce(s5.eroeff_dat,convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)) and cast(s5.aufheb_dat as date)<=@cur_date) --or s5.aufheb_cd='ZZ'
										then FAF.GoAML_get_paramS(@F_BANK_ID,18,2)
										else FAF.GoAML_get_paramS(@F_BANK_ID,18,1)
									end
							end
						,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__status_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__status_code')) as "status_code",
						case 
							when FAF.GoAML_get_paramS(@F_BANK_ID,95,1)='ok' 
							then 
								FAF.GoAML_char_process(
									case
										when FAF.GoAML_get_paramS(@F_BANK_ID,108,1)='ok'
										then convert(varchar,@cur_date,23)+'T00:00:00' 
										else convert(varchar,sysdatetime(),126)
									end
								,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__status_date'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__status_date'))
							else NULL 
						end as "status_date",
						FAF.GoAML_char_process(
							case
								when FAF.GoAML_get_paramS(@F_BANK_ID,39,1)='ok'
								then 
									case 
										when FAF.GoAML_get_paramS(@F_BANK_ID,22,1)=FAF.GoAML_char_process(wrg.iso_wrg_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__currency_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__currency_code')) 
											or (s5.aufheb_dat>=coalesce(s5.eroeff_dat,convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112)) and cast(s5.aufheb_dat as date)<=@cur_date)
											or FAF.GoAML_get_paramS(@F_BANK_ID,111,1)='ok'
										then NULL
										else coalesce(cast(cast(sld.saldo_kwrg as decimal(15,2)) as varchar),FAF.GoAML_get_paramS(@F_BANK_ID,22,2))
									end
								else NULL
							end
						,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__beneficiary'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__beneficiary')) as "beneficiary",
						FAF.GoAML_char_process(
							case
								when FAF.GoAML_get_paramS(@F_BANK_ID,40,1)='ok'
								then s5.sitz_nr_bez
								else NULL
							end
						,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__beneficiary_comment'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__beneficiary_comment')) as "beneficiary_comment",
						FAF.GoAML_char_process_WR(
							case
								when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
								then
									stuff(
										case 
											when wrg.iso_wrg_cd is NULL and FAF.GoAML_char_process(s5.wrg_bez,NULL,0) is not NULL and FAF.GoAML_char_process(s5.iso_wrg_cd,NULL,0) is not NULL
											then '. ' + char(13) + char(10) + 'Actual currency is ' + FAF.GoAML_char_process(s5.wrg_bez,'',0) + ' (' + FAF.GoAML_char_process(s5.iso_wrg_cd,'',0) + ')'
											else ''
										end + 
										case
											when FAF.GoAML_get_paramS(@F_BANK_ID,24,1)=FAF.GoAML_char_process(isnull(ac_tp.MROS_value,ac_tp1.MROS_value),FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__account_type'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__account_type'))
											then '. ' + char(13) + char(10) + coalesce(FAF.GoAML_char_process(case when ac_tp.MROS_value is not NULL then ac_tp.comments else ac_tp1.comments end,FAF.GoAML_get_paramS(@F_BANK_ID,24,2),0),'')
											else ''
										end
									, 1, LEN('. ' + char(13) + char(10)), '')
								else NULL
							end
						,FAF.GoAML_get_paramV(@F_BANK_ID,'tamc__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'tamc__comments')) as "comments"
					from
						FAF.FA_MROS_KT_STAMM s5
					left join
						FAF.FA_MROS_KT_SALDO sld
					on s5.kt_lnr=sld.kt_lnr and s5.userbk_nr=sld.userbk_nr
					left join
						FAF.FA_MROS_INT_WRG wrg
					on s5.iso_wrg_cd=wrg.iso_wrg_cd and s5.userbk_nr=wrg.userbk_nr
					left join
						FAF.FA_MROS_REFERENCE_TABLE stz
					on s5.userbk_nr=stz.userbk_nr and cast(s5.sitz_nr as varchar)=stz.finnova_value and stz.ref_type=1
					left join
						FAF.FA_MROS_REFERENCE_TABLE ac_tp
					on s5.userbk_nr=ac_tp.userbk_nr and cast(s5.kt_art_ber_cd as varchar)=ac_tp.finnova_value and ac_tp.ref_type=9
					left join
						FAF.FA_MROS_REFERENCE_TABLE ac_tp1
					on s5.userbk_nr=ac_tp1.userbk_nr and cast(s5.arbgeb_nr as varchar)=ac_tp1.finnova_value and ac_tp1.ref_type=31
					left join
						FAF.FA_MROS_REFERENCE_TABLE stcd
					on s5.userbk_nr=stcd.userbk_nr and cast(s5.aufheb_cd as varchar)=stcd.finnova_value and stcd.ref_type=25
					where s5.kt_lnr=@kt_id and s5.userbk_nr=@F_BANK_ID
					FOR XML PATH (''),type, ELEMENTS);
				
				if @xml_ac is not NULL
				begin
					set @xml_ac = 
						(select
							case 
								when FAF.GoAML_get_paramS(@F_BANK_ID,128,1)='ok'
								then 
									case 
										when FAF.GoAML_get_paramS(@F_BANK_ID,53,1)='ok'
										then FAF.GoAML_get_paramS(@F_BANK_ID,53,2)
										else NULL
									end
								else NULL
							end as "role",
							@xml_ac as "account_my_client",
							case 
								when FAF.GoAML_get_paramS(@F_BANK_ID,128,1)='ok'
								then NULL
								else
									case 
										when FAF.GoAML_get_paramS(@F_BANK_ID,53,1)='ok'
										then FAF.GoAML_get_paramS(@F_BANK_ID,53,2)
										else NULL
									end
							end as "significance",
							FAF.GoAML_char_process(
								coalesce(@s_reason,
									case 
										when FAF.GoAML_get_paramS(@F_BANK_ID,53,1)='ok' and FAF.GoAML_get_paramS(@F_BANK_ID,53,2)=FAF.GoAML_get_paramS(@F_BANK_ID,36,1)
										then FAF.GoAML_get_paramS(@F_BANK_ID,36,2)
										else NULL
									end
								)
							,FAF.GoAML_get_paramV(@F_BANK_ID,'rpt__reason'),FAF.GoAML_get_paramL(@F_BANK_ID,'rpt__reason')) as "reason"
						FOR XML PATH ('report_party'),type, ELEMENTS);

					set @itms = @itms + 1;
					set @xml_r = (select @xml_r,@xml_ac FOR XML PATH (''),type, ELEMENTS);
					if @itms=@max_items
						break;
				end;
			end;

			set @ii = @ii + 1;
		end;

		--clients
		if @itms<@max_items
		begin
			set @ii = 1;
			while @ii<=@nn0
			begin
				set @cl_id = NULL;
				set @cl_tp = NULL;
				set @cl_nr = NULL;
				set @cl_nm = NULL;
				set @auf_dt = NULL;
				set @stat = NULL;
				set @partners = NULL;
				set @mm = NULL;
				set @mm0 = NULL;
				set @mmm = (select count(*) from @KD_KD_ext);

				select 
					@cl_id = cl.nID, 
					@s_reason = cl.chID
				from @CLNTS cl where cl.id=@ii;

				if @cl_id is not NULL
				begin
					select
						@cl_tp = se.kd_htyp_cd,
						@cl_nm = 
							case 
								when se.kd_htyp_cd=1 
								then isnull(se.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(se.name_1,'') 
								else se.kd_bez 
							end,
						@cl_nr = se.edit_kd_nr,
						@stat = se.kd_stat_cd
					from 
						FAF.FA_MROS_KD_STAMM se
					where se.kd_lnr=@cl_id and se.userbk_nr=@F_BANK_ID and se.partner_flag=0;

					set @auf_dt = @cur_date;
					if @stat<>0
					begin
						select
							@auf_dt = max(isnull(aufheb_dat,convert(date,'20991231',112)))
						from 
							FAF.FA_MROS_KT_STAMM kt
						where kd_lnr=@cl_id and userbk_nr=@F_BANK_ID;

						if @auf_dt is NULL or @auf_dt = convert(date,'20991231',112)
							set @auf_dt = @cur_date;
						else
							set @auf_dt = dateadd(day,-7,@auf_dt);
					end;

					if @cl_tp=2
					begin
						select 
							@st1 = FAF.GoAML_char_process(vorname,'',4000),
							@st2 = FAF.GoAML_char_process(name_1,'',4000)
						from 
							FAF.FA_MROS_KD_STAMM
						where kd_lnr=@cl_id and userbk_nr=@F_BANK_ID and partner_flag=0;
						select 
							@st3 = FAF.GoAML_char_process(vorname,'',4000),
							@st4 = FAF.GoAML_char_process(name_1,'',4000)
						from 
							FAF.FA_MROS_KD_STAMM
						where kd_lnr=@cl_id and userbk_nr=@F_BANK_ID and partner_flag=1;
								
						if len(@st3+@st4)>0
							begin
								if @st2=@st4 
									set @cl_nm = @st1 + case when len(@st1)>0 then ' and ' else '' end + @st3 + ' ' + @st4;
								else
									set @cl_nm = @st1 + ' ' + @st2 + case when len(@st1+@st2)>0 then ' and ' else '' end + @st3 + ' ' + @st4;
							end;
						else
							set @cl_nm = @st1 + ' ' + @st2;

						--partners from initial partnership
						insert into @KD_KD_ext
						select kdkd.kd_lnr_zuw as nID,NULL as rl,NULL as cmm,@cl_nr as ptn,kd.kd_htyp_cd as kdtp,kd.edit_kd_nr as kdnr,
							case
								when kd.kd_htyp_cd=3 then kd.kd_bez
								when kd.kd_htyp_cd=2 and kd.name_1=kd1.name_1 
								then isnull(kd.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' and ' + isnull(kd1.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd.name_1,'')
								when kd.kd_htyp_cd=2 and kd1.userbk_nr is not NULL
								then isnull(kd.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd.name_1,'') + ' and ' + isnull(kd1.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd1.name_1,'')
								else isnull(kd.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd.name_1,'')
							end as kdnm,
							@ii as srt,3 as flg
						from
							(select kdkd1.kd_lnr_zuw 
							from 
								FAF.FA_MROS_KD_KD kdkd1
							join
								FAF.FA_MROS_REFERENCE_TABLE rt_1
							on cast(kdkd1.kd_kd_verb_cd as varchar)=rt_1.Finnova_value and rt_1.userbk_nr=kdkd1.userbk_nr and rt_1.ref_type=12 and rt_1.activ_MROS=1 and rt_1.special_mark='DOPPELPERSON'
							where kdkd1.userbk_nr=@F_BANK_ID and kdkd1.kd_lnr=@cl_id and kdkd1.kd_lnr_zuw<>@cl_id
								and (kdkd1.valid_bis_dat is NULL or kdkd1.valid_bis_dat>=@auf_dt)
								and (kdkd1.valid_von_dat is NULL or kdkd1.valid_von_dat<=@auf_dt)
							union
							select kdkd2.kd_lnr as kd_lnr_zuw
							from 
								FAF.FA_MROS_KD_KD kdkd2
							join
								FAF.FA_MROS_REFERENCE_TABLE rt_2
							on cast(kdkd2.kd_kd_verb_cd as varchar)=rt_2.Finnova_value and rt_2.userbk_nr=kdkd2.userbk_nr and rt_2.ref_type=13 and rt_2.activ_MROS=1 and rt_2.special_mark='DOPPELPERSON'
							where kdkd2.userbk_nr=@F_BANK_ID and kdkd2.kd_lnr_zuw=@cl_id and kdkd2.kd_lnr<>@cl_id
								and (kdkd2.valid_bis_dat is NULL or kdkd2.valid_bis_dat>=@auf_dt)
								and (kdkd2.valid_von_dat is NULL or kdkd2.valid_von_dat<=@auf_dt)
							) kdkd
						join
							FAF.FA_MROS_KD_STAMM kd
						on kd.userbk_nr=@F_BANK_ID and kd.kd_lnr=kdkd.kd_lnr_zuw and kd.partner_flag=0
						left join
							FAF.FA_MROS_KD_STAMM kd1
						on kd1.userbk_nr=@F_BANK_ID and kd1.kd_lnr=kdkd.kd_lnr_zuw and kd1.partner_flag=1;

						set @mm = (select count(*) from @KD_KD_ext) - isnull(@mmm,0);
						if @mm>0
						begin
							set @partners = 
								(select STUFF(
									(select ', ' + coalesce(kdkd.kdnr,'') from @KD_KD_ext kdkd where kdkd.id>isnull(@mmm,0) FOR XML PATH(''))
								, 1, LEN(', '), ''));
						end;
					end;

					--initial client
					insert into @KD_KD_ext
					select @cl_id as nID,
						case 
							when FAF.GoAML_get_paramS(@F_BANK_ID,53,1)='ok'
							then FAF.GoAML_get_paramS(@F_BANK_ID,53,2)
							else NULL
						end as rl,
						@s_reason as cmm,@partners as ptn,@cl_tp as kdtp,@cl_nr as kdnr,@cl_nm as kdnm,@ii as srt,4 as flg;

					set @mm0 = (select count(*) from @KD_KD_ext) - isnull(@mmm,0);
					set @jj = 1;
					while @jj<=@mm0
					begin
						set @n_id = NULL;
						set @kd_nm = NULL;
						select @n_id = nID,@kd_nm=kdnm from @KD_KD_ext where id=@jj + isnull(@mmm,0);
						if @n_id is not NULL
						begin
							if FAF.GoAML_get_paramS(@F_BANK_ID,77,1) = 'ok'
								begin
									with
										links as
											(select kdkd1.kd_lnr_zuw,rt1.mros_value,
												case when FAF.GoAML_char_process(rt1.special_mark,NULL,4000) is NULL then rt1.comments else rt1.special_mark end as comments
											from 
												FAF.FA_MROS_KD_KD kdkd1
											left join
												FAF.FA_MROS_REFERENCE_TABLE rt1
											on cast(kdkd1.kd_kd_verb_cd as varchar)=rt1.Finnova_value and rt1.userbk_nr=kdkd1.userbk_nr and rt1.ref_type=12
											where kdkd1.userbk_nr=@F_BANK_ID and kdkd1.kd_lnr=@n_id and kdkd1.kd_lnr_zuw<>@n_id and kdkd1.kd_lnr_zuw<>@cl_id
												and (kdkd1.valid_bis_dat is NULL or kdkd1.valid_bis_dat>=@auf_dt)
												and (kdkd1.valid_von_dat is NULL or kdkd1.valid_von_dat<=@auf_dt)
												and isnull(rt1.activ_MROS,1)=1
											union
											select kdkd2.kd_lnr as kd_lnr_zuw,rt2.mros_value,
												case when FAF.GoAML_char_process(rt2.special_mark,NULL,4000) is NULL then rt2.comments else rt2.special_mark end as comments
											from 
												FAF.FA_MROS_KD_KD kdkd2
											left join
												FAF.FA_MROS_REFERENCE_TABLE rt2
											on cast(kdkd2.kd_kd_verb_cd as varchar)=rt2.Finnova_value and rt2.userbk_nr=kdkd2.userbk_nr and rt2.ref_type=13
											where kdkd2.userbk_nr=@F_BANK_ID and kdkd2.kd_lnr_zuw=@n_id and kdkd2.kd_lnr<>@n_id and kdkd2.kd_lnr<>@cl_id
												and (kdkd2.valid_bis_dat is NULL or kdkd2.valid_bis_dat>=@auf_dt)
												and (kdkd2.valid_von_dat is NULL or kdkd2.valid_von_dat<=@auf_dt)
												and isnull(rt2.activ_MROS,1)=1
											union
											select vtz.kd_lnr as kd_lnr_zuw,rt3.mros_value,
												case when FAF.GoAML_char_process(rt3.special_mark,NULL,4000) is NULL then rt3.comments else rt3.special_mark end as comments
											from 
												FAF.FA_MROS_VT_STAMM vts
											join
												FAF.FA_MROS_VT_ZUORD vtz
											on vts.userbk_nr=vtz.userbk_nr and vts.vt_lnr=vtz.vt_lnr
											left join
												FAF.FA_MROS_REFERENCE_TABLE rt3
											on cast(vts.vt_typ_cd as varchar)+'_'+cast(vtz.sb_sign_recht_cd as varchar)=rt3.Finnova_value and rt3.userbk_nr=vts.userbk_nr and rt3.ref_type=16
											where vts.userbk_nr=@F_BANK_ID and vts.kd_lnr=@n_id and vtz.kd_lnr<>@n_id and vtz.kd_lnr<>@cl_id
												and (vts.kt_lnr is NULL or @jj=1 and vts.kt_lnr is not NULL)
												and (vts.ende_dat is NULL or vts.ende_dat>=@auf_dt)
												and (vts.beginn_dat is NULL or vts.beginn_dat<=@auf_dt)
												and (vtz.beg_dat is NULL or vtz.beg_dat<=@auf_dt)
												and (vtz.aufheb_dat is NULL or vtz.aufheb_dat>=@auf_dt)
												and isnull(rt3.activ_MROS,1)=1
											union
											select vts.kd_lnr as kd_lnr_zuw,rt4.mros_value,
												case when FAF.GoAML_char_process(rt4.special_mark,NULL,4000) is NULL then rt4.comments else rt4.special_mark end as comments
											from 
												FAF.FA_MROS_VT_STAMM vts
											join
												FAF.FA_MROS_VT_ZUORD vtz
											on vts.userbk_nr=vtz.userbk_nr and vts.vt_lnr=vtz.vt_lnr
											left join
												FAF.FA_MROS_REFERENCE_TABLE rt4
											on cast(vts.vt_typ_cd as varchar)+'_'+cast(vtz.sb_sign_recht_cd as varchar)=rt4.Finnova_value and rt4.userbk_nr=vts.userbk_nr and rt4.ref_type=17
											where vtz.userbk_nr=@F_BANK_ID and vtz.kd_lnr=@n_id and vts.kd_lnr<>@n_id and vts.kd_lnr<>@cl_id
												and (vts.ende_dat is NULL or vts.ende_dat>=@auf_dt)
												and (vts.beginn_dat is NULL or vts.beginn_dat<=@auf_dt)
												and (vtz.beg_dat is NULL or vtz.beg_dat<=@auf_dt)
												and (vtz.aufheb_dat is NULL or vtz.aufheb_dat>=@auf_dt)
												and isnull(rt4.activ_MROS,1)=1
											)
									insert into @KD_KD_ext
									select 
										kdkd.kd_lnr_zuw as nID,
										case when kdkd.mn_mros_value=kdkd.mx_mros_value then kdkd.mn_mros_value else '-9999555595595959955' end as rl,
										'The client is related to the client ' + coalesce(@kd_nm,'') + case when kdkd.comments is not NULL then ', role: ' + kdkd.comments else '' end as cmm,
										NULL as ptn,kd.kd_htyp_cd as kdtp,kd.edit_kd_nr as kdnr,
										case
											when kd.kd_htyp_cd=3 then kd.kd_bez
											when kd.kd_htyp_cd=2 and kd.name_1=kd1.name_1 
											then isnull(kd.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' and ' + isnull(kd1.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd.name_1,'')
											when kd.kd_htyp_cd=2 and kd1.userbk_nr is not NULL
											then isnull(kd.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd.name_1,'') + ' and ' + isnull(kd1.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd1.name_1,'')
											else isnull(kd.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd.name_1,'')
										end as kdnm,
										@jj+@nn0+isnull(@mmm,0) as srt,2 as flg
									from
										(select t0.kd_lnr_zuw,min(t0.mros_value) as mn_mros_value,max(t0.mros_value) as mx_mros_value,
											STUFF(
												(select distinct case when len(t2.comments)>0 then ', '+t2.comments else '' end 
												from links t2 
												where t0.kd_lnr_zuw=t2.kd_lnr_zuw 
												FOR XML PATH(''))
											, 1, LEN(', '), '') as comments
										from 
											links t0
										group by t0.kd_lnr_zuw) kdkd
									join
										FAF.FA_MROS_KD_STAMM kd
									on kd.userbk_nr=@F_BANK_ID and kd.kd_lnr=kdkd.kd_lnr_zuw and kd.partner_flag=0
									left join
										FAF.FA_MROS_KD_STAMM kd1
									on kd1.userbk_nr=@F_BANK_ID and kd1.kd_lnr=kdkd.kd_lnr_zuw and kd1.partner_flag=1;
								end;
							else
								begin
									with
										links as
											(select kdkd1.kd_lnr_zuw,rt1.mros_value,
												case when FAF.GoAML_char_process(rt1.special_mark,NULL,4000) is NULL then rt1.comments else rt1.special_mark end as comments
											from 
												FAF.FA_MROS_KD_KD kdkd1
											left join
												FAF.FA_MROS_REFERENCE_TABLE rt1
											on cast(kdkd1.kd_kd_verb_cd as varchar)=rt1.Finnova_value and rt1.userbk_nr=kdkd1.userbk_nr and rt1.ref_type=12
											where kdkd1.userbk_nr=@F_BANK_ID and kdkd1.kd_lnr=@n_id and kdkd1.kd_lnr_zuw<>@n_id and kdkd1.kd_lnr_zuw<>@cl_id
												and (kdkd1.valid_bis_dat is NULL or kdkd1.valid_bis_dat>=@auf_dt)
												and (kdkd1.valid_von_dat is NULL or kdkd1.valid_von_dat<=@auf_dt)
												and isnull(rt1.activ_MROS,1)=1
											union
											select kdkd2.kd_lnr as kd_lnr_zuw,rt2.mros_value,
												case when FAF.GoAML_char_process(rt2.special_mark,NULL,4000) is NULL then rt2.comments else rt2.special_mark end as comments
											from 
												FAF.FA_MROS_KD_KD kdkd2
											left join
												FAF.FA_MROS_REFERENCE_TABLE rt2
											on cast(kdkd2.kd_kd_verb_cd as varchar)=rt2.Finnova_value and rt2.userbk_nr=kdkd2.userbk_nr and rt2.ref_type=13
											where kdkd2.userbk_nr=@F_BANK_ID and kdkd2.kd_lnr_zuw=@n_id and kdkd2.kd_lnr<>@n_id and kdkd2.kd_lnr<>@cl_id
												and (kdkd2.valid_bis_dat is NULL or kdkd2.valid_bis_dat>=@auf_dt)
												and (kdkd2.valid_von_dat is NULL or kdkd2.valid_von_dat<=@auf_dt)
												and isnull(rt2.activ_MROS,1)=1
											union
											select vtz.kd_lnr as kd_lnr_zuw,rt3.mros_value,
												case when FAF.GoAML_char_process(rt3.special_mark,NULL,4000) is NULL then rt3.comments else rt3.special_mark end as comments
											from 
												FAF.FA_MROS_VT_STAMM vts
											join
												FAF.FA_MROS_VT_ZUORD vtz
											on vts.userbk_nr=vtz.userbk_nr and vts.vt_lnr=vtz.vt_lnr
											left join
												FAF.FA_MROS_REFERENCE_TABLE rt3
											on cast(vts.vt_typ_cd as varchar)=rt3.Finnova_value and rt3.userbk_nr=vts.userbk_nr and rt3.ref_type=16
											where vts.userbk_nr=@F_BANK_ID and vts.kd_lnr=@n_id and vtz.kd_lnr<>@n_id and vtz.kd_lnr<>@cl_id
												and (vts.kt_lnr is NULL or @jj=1 and vts.kt_lnr is not NULL)
												and (vts.ende_dat is NULL or vts.ende_dat>=@auf_dt)
												and (vts.beginn_dat is NULL or vts.beginn_dat<=@auf_dt)
												and (vtz.beg_dat is NULL or vtz.beg_dat<=@auf_dt)
												and (vtz.aufheb_dat is NULL or vtz.aufheb_dat>=@auf_dt)
												and isnull(rt3.activ_MROS,1)=1
											union
											select vts.kd_lnr as kd_lnr_zuw,rt4.mros_value,
												case when FAF.GoAML_char_process(rt4.special_mark,NULL,4000) is NULL then rt4.comments else rt4.special_mark end as comments
											from 
												FAF.FA_MROS_VT_STAMM vts
											join
												FAF.FA_MROS_VT_ZUORD vtz
											on vts.userbk_nr=vtz.userbk_nr and vts.vt_lnr=vtz.vt_lnr
											left join
												FAF.FA_MROS_REFERENCE_TABLE rt4
											on cast(vts.vt_typ_cd as varchar)=rt4.Finnova_value and rt4.userbk_nr=vts.userbk_nr and rt4.ref_type=17
											where vtz.userbk_nr=@F_BANK_ID and vtz.kd_lnr=@n_id and vts.kd_lnr<>@n_id and vts.kd_lnr<>@cl_id
												and (vts.ende_dat is NULL or vts.ende_dat>=@auf_dt)
												and (vts.beginn_dat is NULL or vts.beginn_dat<=@auf_dt)
												and (vtz.beg_dat is NULL or vtz.beg_dat<=@auf_dt)
												and (vtz.aufheb_dat is NULL or vtz.aufheb_dat>=@auf_dt)
												and isnull(rt4.activ_MROS,1)=1
											)
									insert into @KD_KD_ext
									select 
										kdkd.kd_lnr_zuw as nID,
										case when kdkd.mn_mros_value=kdkd.mx_mros_value then kdkd.mn_mros_value else '-9999555595595959955' end as rl,
										'The client is related to the client ' + coalesce(@kd_nm,'') + case when kdkd.comments is not NULL then ', role: ' + kdkd.comments else '' end as cmm,
										NULL as ptn,kd.kd_htyp_cd as kdtp,kd.edit_kd_nr as kdnr,
										case
											when kd.kd_htyp_cd=3 then kd.kd_bez
											when kd.kd_htyp_cd=2 and kd.name_1=kd1.name_1 
											then isnull(kd.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' and ' + isnull(kd1.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd.name_1,'')
											when kd.kd_htyp_cd=2 and kd1.userbk_nr is not NULL
											then isnull(kd.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd.name_1,'') + ' and ' + isnull(kd1.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd1.name_1,'')
											else isnull(kd.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd.name_1,'')
										end as kdnm,
										@jj+@nn0+isnull(@mmm,0) as srt,2 as flg
									from
										(select t0.kd_lnr_zuw,min(t0.mros_value) as mn_mros_value,max(t0.mros_value) as mx_mros_value,
											STUFF(
												(select distinct case when len(t2.comments)>0 then ', '+t2.comments else '' end 
												from links t2 
												where t0.kd_lnr_zuw=t2.kd_lnr_zuw 
												FOR XML PATH(''))
											, 1, LEN(', '), '') as comments
										from 
											links t0
										group by t0.kd_lnr_zuw) kdkd
									join
										FAF.FA_MROS_KD_STAMM kd
									on kd.userbk_nr=@F_BANK_ID and kd.kd_lnr=kdkd.kd_lnr_zuw and kd.partner_flag=0
									left join
										FAF.FA_MROS_KD_STAMM kd1
									on kd1.userbk_nr=@F_BANK_ID and kd1.kd_lnr=kdkd.kd_lnr_zuw and kd1.partner_flag=1;
								end;
						end;

						set @jj = @jj + 1;
					end;
					
					set @mm = (select count(*) from @KD_KD_ext) - isnull(@mmm,0);
					if @mm>@mm0
					begin
						set @jj = @mm0 + 1;
						while @jj<=@mm
						begin
							set @n_id = NULL;
							set @rol = NULL;
							set @kd_nm = NULL;
							set @kd_nr = NULL;
							set @kd_tp = NULL;
							set @partners = NULL;
							set @kkk = NULL;

							select @n_id = nID,@rol=rl,@kd_nm=kdnm,@kd_nr=kdnr,@kd_tp=kdtp,@kkk=srt from @KD_KD_ext where id=@jj + isnull(@mmm,0);
							if @kd_tp=2
							begin
								--doppelpersons of clients linked to the initial client
								set @mm0 = (select count(*) from @KD_KD_ext) - isnull(@mmm,0);
								with
									links as
										(select kdkd1.kd_lnr_zuw,
											case when FAF.GoAML_char_process(rt_1.special_mark,NULL,4000) is NULL then rt_1.comments else rt_1.special_mark end as comments
										from 
											FAF.FA_MROS_KD_KD kdkd1
										join
											FAF.FA_MROS_REFERENCE_TABLE rt_1
										on cast(kdkd1.kd_kd_verb_cd as varchar)=rt_1.Finnova_value and rt_1.userbk_nr=kdkd1.userbk_nr and rt_1.ref_type=12 and rt_1.activ_MROS=1 and rt_1.special_mark='DOPPELPERSON'
										where kdkd1.userbk_nr=@F_BANK_ID and kdkd1.kd_lnr=@n_id and kdkd1.kd_lnr_zuw<>@n_id
											and (kdkd1.valid_bis_dat is NULL or kdkd1.valid_bis_dat>=@auf_dt)
											and (kdkd1.valid_von_dat is NULL or kdkd1.valid_von_dat<=@auf_dt)
										union
										select kdkd2.kd_lnr as kd_lnr_zuw,
											case when FAF.GoAML_char_process(rt_2.special_mark,NULL,4000) is NULL then rt_2.comments else rt_2.special_mark end as comments
										from 
											FAF.FA_MROS_KD_KD kdkd2
										join
											FAF.FA_MROS_REFERENCE_TABLE rt_2
										on cast(kdkd2.kd_kd_verb_cd as varchar)=rt_2.Finnova_value and rt_2.userbk_nr=kdkd2.userbk_nr and rt_2.ref_type=13 and rt_2.activ_MROS=1 and rt_2.special_mark='DOPPELPERSON'
										where kdkd2.userbk_nr=@F_BANK_ID and kdkd2.kd_lnr_zuw=@n_id and kdkd2.kd_lnr<>@n_id
											and (kdkd2.valid_bis_dat is NULL or kdkd2.valid_bis_dat>=@auf_dt)
											and (kdkd2.valid_von_dat is NULL or kdkd2.valid_von_dat<=@auf_dt)
										)
								insert into @KD_KD_ext
								select 
									kdkd.kd_lnr_zuw as nID,@rol as rl,
									'The client is related to the client ' + coalesce(@kd_nm,'') + case when kdkd.comments is not NULL then ', role: ' + kdkd.comments else '' end as cmm,
									@kd_nr as ptn,kd.kd_htyp_cd as kdtp,kd.edit_kd_nr as kdnr,
									case
										when kd.kd_htyp_cd=3 then kd.kd_bez
										when kd.kd_htyp_cd=2 and kd.name_1=kd1.name_1 
										then isnull(kd.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' and ' + isnull(kd1.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd.name_1,'')
										when kd.kd_htyp_cd=2 and kd1.userbk_nr is not NULL
										then isnull(kd.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd.name_1,'') + ' and ' + isnull(kd1.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd1.name_1,'')
										else isnull(kd.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name')) + ' ' + isnull(kd.name_1,'')
									end as kdnm,
									@kkk as srt,1 as flg
								from
									(select t0.kd_lnr_zuw,
										STUFF(
											(select case when len(t2.comments)>0 then ', '+t2.comments else '' end 
											from links t2 
											where t0.kd_lnr_zuw=t2.kd_lnr_zuw 
											FOR XML PATH(''))
										, 1, LEN(', '), '') as comments
									from 
										links t0
									group by t0.kd_lnr_zuw) kdkd
								join
									FAF.FA_MROS_KD_STAMM kd
								on kd.userbk_nr=@F_BANK_ID and kd.kd_lnr=kdkd.kd_lnr_zuw and kd.partner_flag=0
								left join
									FAF.FA_MROS_KD_STAMM kd1
								on kd1.userbk_nr=@F_BANK_ID and kd1.kd_lnr=kdkd.kd_lnr_zuw and kd1.partner_flag=1;

								if (select count(*) from @KD_KD_ext)>@mm0 + isnull(@mmm,0)
								begin
									set @partners = 
										(select STUFF(
											(select ', ' + coalesce(kdnr,'') from @KD_KD_ext where id>@mm0 + isnull(@mmm,0) FOR XML PATH(''))
										, 1, LEN(', '), ''));

									if @partners is not NULL
										insert into @KD_KD_ext select @n_id as nID,NULL as rl,NULL as cmm,@partners as ptn,NULL as kdtp,NULL as kdnr,NULL as kdnm,NULL as srt,NULL as flg;
								end;
							end;

							set @jj = @jj + 1;
						end;
					end;
				end;

				set @ii = @ii + 1;
			end;

			insert into @KD_KD_ext1
			select nID,
				case 
					when flg=4 then 
						case 
							when FAF.GoAML_get_paramS(@F_BANK_ID,53,1)='ok'
							then FAF.GoAML_get_paramS(@F_BANK_ID,53,2)
							else NULL
						end
					when mn_rl='-9999555595595959955' then NULL
					when mn_rl=mx_rl then mn_rl
					else NULL
				end as rl,
				case when flg=4 then cmm0 else cmm end as cmm,
				ptn,kdtp,kdnr,kdnm,srt,flg
			from
				(select t0.nID,max(t0.rl) as mx_rl,min(t0.rl) as mn_rl,max(t0.ptn) as ptn,max(t0.kdtp) as kdtp,max(t0.kdnr) as kdnr,
					max(t0.kdnm) as kdnm,min(t0.srt) as srt,max(t0.flg) as flg,
					max(case when t0.flg=4 then t0.cmm else NULL end) as cmm0,
					STUFF(
						(select distinct case when len(t2.cmm)>0 then '; ' + t2.cmm else '' end 
						from @KD_KD_ext t2 
						where t0.nID=t2.nID 
						FOR XML PATH(''))
					, 1, LEN('; '), '') as cmm
				from 
					@KD_KD_ext t0
				group by t0.nID) s
			order by srt,flg desc;

			set @mm = (select count(*) from @KD_KD_ext1);
			--prepare client xml
			set @jj = 1;
			while @jj<=@mm
			begin
				set @n_id = NULL;
				set @rol = NULL;
				set @comm = NULL;
				set @kd_nr = NULL;
				set @kd_nm = NULL;
				set @kd_tp = NULL;
				set @partners = NULL;
				set @xml_cl = NULL;
				select @n_id = nID,@rol=rl,@comm=cmm,@partners=ptn,@kd_nr=kdnr,@kd_tp=kdtp,@kd_nm=kdnm from @KD_KD_ext1 where id=@jj;

				--person
				if @kd_tp=1
				begin
					set @xml_cl =
						(select
							FAF.GoAML_char_process(gd.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__gender'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__gender')) as "gender",
							FAF.GoAML_char_process(se.titel,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__title'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__title')) as "title",
							FAF.GoAML_char_process(se.vorname,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__first_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__first_name')) as "first_name",
							case 
								when FAF.GoAML_get_paramS(@F_BANK_ID,44,1)='ok'
								then upper(FAF.GoAML_char_process(se.name_1,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__last_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__last_name')))
								else FAF.GoAML_char_process(se.name_1,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__last_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__last_name'))
							end as "last_name",
							FAF.GoAML_char_process(
								case 
									when se.geburt_dat is NULL or se.geburt_dat < convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or cast(se.geburt_dat as date)>@cur_date
									then 
										case 
											when se.geburt_jahr is NULL or se.geburt_jahr<cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) or se.geburt_jahr>year(@cur_date)
											then NULL
											else cast(se.geburt_jahr as varchar) + '-01-01T00:00:00' 
										end 
									else left(convert(varchar,cast(se.geburt_dat as datetime),126),19)
								end
							,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__birthdate'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__birthdate')) as "birthdate",
							FAF.GoAML_char_process(se.geburt_ort,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__birth_place'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__birth_place')) as "birth_place",
							FAF.GoAML_char_process(
								case
									when FAF.GoAML_get_paramS(@F_BANK_ID,42,1)='ok'
									then
										case 
											when FAF.GoAML_get_paramS(@F_BANK_ID,98,1)='ok'
											then
												case 
													when FAF.GoAML_get_paramS(@F_BANK_ID,64,1)='ok'
													then
														case 
															when isnull(se.kd_pep_cd,se.kd_pep_cd_auto) is NULL then 'UNBEKANNT'
															when isnull(se.kd_pep_cd,se.kd_pep_cd_auto) = '0' then 'NEIN'
															else 'JA'
														end
													else
														case 
															when isnull(se.kd_pep_cd,se.kd_pep_cd_auto) is NULL then 'unbekannt'
															when isnull(se.kd_pep_cd,se.kd_pep_cd_auto) = '0' then 'nein'
															else 'ja'
														end
												end
											else
												case 
													when FAF.GoAML_get_paramS(@F_BANK_ID,64,1)='ok'
													then
														case 
															when se.kd_pep_cd is NULL then 'UNBEKANNT'
															when se.kd_pep_cd = '0' then 'NEIN'
															else 'JA'
														end
													else
														case 
															when se.kd_pep_cd is NULL then 'unbekannt'
															when se.kd_pep_cd = '0' then 'nein'
															else 'ja'
														end
												end
										end
									else NULL
								end
							,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__id_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__id_number')) as "id_number",
							FAF.GoAML_char_process(cntr1.iso_land_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__nationality1'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__nationality1')) as "nationality1",
							FAF.GoAML_char_process(cntr2.iso_land_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__nationality2'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__nationality2')) as "nationality2",
							spp.phones as "phones",
							case
								when FAF.GoAML_get_paramS(@F_BANK_ID,47,1)='ok'
								then
									coalesce(sea.addresses,
										(select
											FAF.GoAML_get_paramS(@F_BANK_ID,8,2) as "address_type",
											FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address') as "address",
											FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__city') as "city",
											FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__country_code') as "country_code"
										FOR XML PATH ('address'),type, ELEMENTS)
									)
								else sea.addresses
							end as "addresses",
							(select FAF.GoAML_char_process(lower(eml.email_adr),FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__email'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__email')) as "email"
							from FAF.FA_MROS_KD_EMAIL eml
							join FAF.FA_MROS_REFERENCE_TABLE rt
							on eml.userbk_nr=rt.userbk_nr and cast(eml.mail_art_cd as varchar)=rt.finnova_value and rt.ref_type=7 and rt.MROS_value=FAF.GoAML_get_paramS(@F_BANK_ID,121,2)
							where eml.kd_lnr=se.kd_lnr and eml.userbk_nr=se.userbk_nr and left(eml.email_adr,1)<>'@'
							FOR XML PATH (''),type, ELEMENTS) as "emails",
							case
								when FAF.GoAML_get_paramS(@F_BANK_ID,48,1)='ok'
								then
									coalesce(spi.identification, 
										(select
											FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__type') as "type",
											FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__number') as "number",
											FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__issue_country') as "issue_country",
											FAF.GoAML_char_process(
												case
													when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
													then
														case
															when FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__type')=FAF.GoAML_get_paramS(@F_BANK_ID,6,1)
															then FAF.GoAML_get_paramS(@F_BANK_ID,6,2)
															else NULL
														end
													else NULL
												end
											,FAF.GoAML_get_paramV(@F_BANK_ID,'tpi__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpi__comments')) as "comments"
										FOR XML PATH ('identification'),type, ELEMENTS)
									)
								else spi.identification
							end as "identifications",
							FAF.GoAML_char_process(
								case 
									when FAF.GoAML_get_paramS(@F_BANK_ID,34,1)='ok' and ((se.tod_dat >= convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) and cast(se.tod_dat as date)<=@cur_date)
										or (se.tod_jahr>=cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) and se.tod_jahr<=year(@cur_date)))
									then FAF.GoAML_get_paramS(@F_BANK_ID,34,2)
									else NULL
								end
							,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__deceased'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__deceased')) as "deceased",
							FAF.GoAML_char_process(
								case 
									when se.tod_dat is NULL or se.tod_dat < convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or cast(se.tod_dat as date)>@cur_date
									then 
										case 
											when se.tod_jahr is NULL or se.tod_jahr<cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) or se.tod_jahr>year(@cur_date)
											then NULL
											else cast(se.tod_jahr as varchar) + '-01-01T00:00:00' 
										end 
									else left(convert(varchar,cast(se.tod_dat as datetime),126),19)
								end
							,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__date_deceased'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__date_deceased')) as "date_deceased",
							case
								when FAF.GoAML_get_paramS(@F_BANK_ID,68,1)='ok' and len(se.uid_nr)>1
								then FAF.GoAML_char_process(se.uid_nr,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__tax_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__tax_number'))
								else NULL
							end as "tax_number",
							FAF.GoAML_char_process_WR(
								case
									when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
									then
										'Client number: ' + se.edit_kd_nr + 
										case 
											when len(@partners)>0
											then ' (partner in the partnership ' + @partners + ')' 
											else ''
										end +
										case
											when cntr1.iso_land_cd is NULL and FAF.GoAML_char_process(se.land_lnr_nat_bez,NULL,0) is not NULL and FAF.GoAML_char_process(se.iso_land_code_nat,NULL,0) is not NULL
											then '; ' + char(13) + char(10) + 'Actual country is ' + FAF.GoAML_char_process(se.land_lnr_nat_bez,'',0) + ' (' + FAF.GoAML_char_process(se.iso_land_code_nat,'',0) + ')'
											else ''
										end
									else NULL
								end
							,FAF.GoAML_get_paramV(@F_BANK_ID,'tpmc__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'tpmc__comments')) as "comments"
						from 
							FAF.FA_MROS_KD_STAMM se
						left join 
							FAF.FA_MROS_KD_ADR_XML sea
						on sea.kd_lnr=se.kd_lnr and sea.userbk_nr=se.userbk_nr
						left join
							FAF.FA_MROS_KD_EMAIL_XML spp
						on spp.kd_lnr=se.kd_lnr and spp.userbk_nr=se.userbk_nr
						left join
							FAF.FA_MROS_KD_VSB_XML spi
						on spi.kd_lnr=se.kd_lnr and spi.userbk_nr=se.userbk_nr
						left join
							FAF.FA_MROS_INT_LAND cntr1
						on cntr1.iso_land_cd=se.iso_land_code_nat and cntr1.userbk_nr=se.userbk_nr
						left join
							FAF.FA_MROS_INT_LAND cntr2
						on cntr2.iso_land_cd=se.iso_land_code_nat_2 and cntr2.userbk_nr=se.userbk_nr
						left join
							FAF.FA_MROS_REFERENCE_TABLE gd
						on se.userbk_nr=gd.userbk_nr and cast(se.GESCHLECHT_CD as varchar)=gd.finnova_value and gd.ref_type=4
						where se.kd_lnr=@n_id and se.userbk_nr=@F_BANK_ID and se.partner_flag=0
						FOR XML PATH (''),type, ELEMENTS);
				end;

				--partnership
				if @kd_tp=2
				begin
					set @xml_cl =
						(select
							FAF.GoAML_char_process(@kd_nm,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__name'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__name')) as "name",
							FAF.GoAML_char_process(
								case
									when FAF.GoAML_get_paramS(@F_BANK_ID,78,2) = 'ok'
									then lf.MROS_value
									else FAF.GoAML_get_paramS(@F_BANK_ID,54,2)
								end
							,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_legal_form'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_legal_form')) as "incorporation_legal_form",
							case
								when FAF.GoAML_get_paramS(@F_BANK_ID,43,1)='ok'
								then spp.phones
								else NULL
							end as "phones",
							case
								when FAF.GoAML_get_paramS(@F_BANK_ID,49,1)='ok'
								then
									coalesce(sea.addresses,
										(select
											FAF.GoAML_get_paramS(@F_BANK_ID,9,2) as "address_type",
											FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address') as "address",
											FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__city') as "city",
											FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__country_code') as "country_code"
										FOR XML PATH ('address'),type, ELEMENTS)
									)
								else sea.addresses
							end as "addresses",
							FAF.GoAML_char_process(cntr1.iso_land_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_country_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_country_code')) as "incorporation_country_code",
							case
								when FAF.GoAML_get_paramS(@F_BANK_ID,67,1)='ok' and len(se.uid_nr)>1
								then FAF.GoAML_char_process(se.uid_nr,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__tax_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__tax_number'))
								else NULL
							end as "tax_number",
							FAF.GoAML_get_paramS(@F_BANK_ID,55,2) as "tax_reg_number",
							FAF.GoAML_char_process_WR(
								case
									when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
									then
										'Client number: ' + se.edit_kd_nr + 
										case 
											when len(@partners)>0
											then char(13) + char(10) + 'It is the partnership of the clients: ' + ltrim(@partners)
											else ''
										end +
										case 
											when FAF.GoAML_get_paramS(@F_BANK_ID,78,2) = 'ok' and FAF.GoAML_get_paramS(@F_BANK_ID,32,1)=FAF.GoAML_char_process(lf.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_legal_form'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_legal_form'))
											then char(13) + char(10) + coalesce(FAF.GoAML_char_process(lf.comments,FAF.GoAML_get_paramS(@F_BANK_ID,32,2),0),'')
											else ''
										end + 
										case
											when cntr1.iso_land_cd is NULL and FAF.GoAML_char_process(se.land_lnr_nat_bez,NULL,0) is not NULL and FAF.GoAML_char_process(se.iso_land_code_nat,NULL,0) is not NULL
											then char(13) + char(10) + 'Actual country is ' + FAF.GoAML_char_process(se.land_lnr_nat_bez,'',0) + ' (' + FAF.GoAML_char_process(se.iso_land_code_nat,'',0) + ')'
											else ''
										end
									else NULL
								end
							,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__comments')) as "comments"
						from 
							FAF.FA_MROS_KD_STAMM se
						left join 
							FAF.FA_MROS_KD_ADR_XML sea
						on sea.kd_lnr=se.kd_lnr and sea.userbk_nr=se.userbk_nr
						left join
							FAF.FA_MROS_KD_EMAIL_XML spp
						on spp.kd_lnr=se.kd_lnr and spp.userbk_nr=se.userbk_nr
						left join
							FAF.FA_MROS_INT_LAND cntr1
						on cntr1.iso_land_cd=se.iso_land_code_nat and cntr1.userbk_nr=se.userbk_nr
						left join
							FAF.FA_MROS_INT_LAND cntr2
						on cntr2.iso_land_cd=se.iso_land_code_nat_2 and cntr2.userbk_nr=se.userbk_nr
						left join
							FAF.FA_MROS_REFERENCE_TABLE lf
						on se.userbk_nr=lf.userbk_nr and cast(se.kd_rstat_cd as varchar)=lf.finnova_value and lf.ref_type=8
						where se.kd_lnr=@n_id and se.userbk_nr=@F_BANK_ID and se.partner_flag=0
						FOR XML PATH (''),type, ELEMENTS);
				end;

				--entity
				if @kd_tp=3
				begin
					set @xml_cl =
						(select
							FAF.GoAML_char_process(se.kd_bez,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__name'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__name')) as "name",
							case
								when FAF.GoAML_get_paramS(@F_BANK_ID,70,1)='ok'
								then FAF.GoAML_char_process(se.kd_bez,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__commercial_name'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__commercial_name'))
								else NULL
							end as "commercial_name",
							FAF.GoAML_char_process(lf.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_legal_form'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_legal_form')) as "incorporation_legal_form",
							FAF.GoAML_char_process(
								case
									when FAF.GoAML_get_paramS(@F_BANK_ID,71,1)='ok'
									then vsb.kd_ausw_nr
									else se.uid_nr
								end
							,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_number')) as "incorporation_number",
							FAF.GoAML_char_process(se.noga_bez,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__business'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__business')) as "business",
							case
								when FAF.GoAML_get_paramS(@F_BANK_ID,43,1)='ok'
								then spp.phones
								else NULL
							end as "phones",
							case
								when FAF.GoAML_get_paramS(@F_BANK_ID,49,1)='ok'
								then
									coalesce(sea.addresses,
										(select
											FAF.GoAML_get_paramS(@F_BANK_ID,7,2) as "address_type",
											FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__address') as "address",
											FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__city') as "city",
											FAF.GoAML_get_paramV(@F_BANK_ID,'tadr__country_code') as "country_code"
										FOR XML PATH ('address'),type, ELEMENTS)
									)
								else sea.addresses
							end as "addresses",
							(select FAF.GoAML_char_process(lower(eml.email_adr),FAF.GoAML_get_paramV(@F_BANK_ID,'temc__email'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__email')) as "email"
							from FAF.FA_MROS_KD_EMAIL eml
							join FAF.FA_MROS_REFERENCE_TABLE rt
							on eml.userbk_nr=rt.userbk_nr and cast(eml.mail_art_cd as varchar)=rt.finnova_value and rt.ref_type=7 and rt.MROS_value=FAF.GoAML_get_paramS(@F_BANK_ID,121,2)
							where eml.kd_lnr=se.kd_lnr and eml.userbk_nr=se.userbk_nr and left(eml.email_adr,1)<>'@'
							FOR XML PATH (''),type, ELEMENTS) as "emails",
							(select FAF.GoAML_char_process(ema.email_adr,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__url'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__url')) as "url"
							from FAF.FA_MROS_KD_EMAIL ema 
							where ema.kd_lnr=se.kd_lnr and ema.userbk_nr=se.userbk_nr and ema.mail_art_cd=FAF.GoAML_get_paramS(@F_BANK_ID,122,2)
							FOR XML PATH (''),type, ELEMENTS) as "urls",
							FAF.GoAML_char_process(se.geburt_ort,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_state'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_state')) as "incorporation_state",
							FAF.GoAML_char_process(cntr1.iso_land_cd,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_country_code'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_country_code')) as "incorporation_country_code",
							FAF.GoAML_char_process(
								case 
									when se.geburt_dat is NULL or se.geburt_dat < convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or cast(se.geburt_dat as date)>@cur_date
									then 
										case 
											when se.geburt_jahr is NULL or se.geburt_jahr<cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) or se.geburt_jahr>year(@cur_date)
											then NULL
											else cast(se.geburt_jahr as varchar) + '-01-01T00:00:00' 
										end 
									else left(convert(varchar,cast(se.geburt_dat as datetime),126),19)
								end 
							,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_date'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_date')) as "incorporation_date",
							FAF.GoAML_char_process(
								case 
									when FAF.GoAML_get_paramS(@F_BANK_ID,28,1)='ok' 
										and ((se.tod_dat >= convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) and cast(se.tod_dat as date)<=@cur_date) or (se.tod_jahr>=cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) and se.tod_jahr<=year(@cur_date)))
									then FAF.GoAML_get_paramS(@F_BANK_ID,28,2) 
									else NULL
								end 
							,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__business_closed'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__business_closed')) as "business_closed",
							FAF.GoAML_char_process(
								case 
									when se.tod_dat is NULL or se.tod_dat < convert(date,FAF.GoAML_get_paramS(@F_BANK_ID,115,1),112) or cast(se.tod_dat as date)>@cur_date
									then 
										case 
											when se.tod_jahr is NULL or se.tod_jahr<cast(left(FAF.GoAML_get_paramS(@F_BANK_ID,115,1),4) as integer) or se.tod_jahr>year(@cur_date)
											then NULL
											else cast(se.tod_jahr as varchar) + '-01-01T00:00:00' 
										end 
									else left(convert(varchar,cast(se.tod_dat as datetime),126),19)
								end 
							,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__date_business_closed'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__date_business_closed')) as "date_business_closed",
							case
								when FAF.GoAML_get_paramS(@F_BANK_ID,67,1)='ok' and len(se.uid_nr)>1
								then FAF.GoAML_char_process(se.uid_nr,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__tax_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__tax_number'))
								else NULL
							end as "tax_number",
							FAF.GoAML_char_process(
								case
									when FAF.GoAML_get_paramS(@F_BANK_ID,104,1)='ok'
									then NULL
									else tax.MROS_value
								end
							,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__tax_reg_number'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__tax_reg_number')) as "tax_reg_number",
							FAF.GoAML_char_process_WR(
								case
									when FAF.GoAML_get_paramS(@F_BANK_ID,112,1)='ok'
									then
										'Client number: ' + se.edit_kd_nr + 
										case
											when cntr1.iso_land_cd is NULL and FAF.GoAML_char_process(se.land_lnr_nat_bez,NULL,0) is not NULL and FAF.GoAML_char_process(se.iso_land_code_nat,NULL,0) is not NULL
											then '; ' + char(13) + char(10) + 'Actual country is ' + FAF.GoAML_char_process(se.land_lnr_nat_bez,'',0) + ' (' + FAF.GoAML_char_process(se.iso_land_code_nat,'',0) + ')'
											else ''
										end + 
										case 
											when FAF.GoAML_get_paramS(@F_BANK_ID,32,1)=FAF.GoAML_char_process(lf.MROS_value,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__incorporation_legal_form'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__incorporation_legal_form'))
											then '; ' + char(13) + char(10) + coalesce(FAF.GoAML_char_process(lf.comments,FAF.GoAML_get_paramS(@F_BANK_ID,32,2),0),'')
											else ''
										end
									else NULL
								end
							,FAF.GoAML_get_paramV(@F_BANK_ID,'temc__comments'),FAF.GoAML_get_paramL(@F_BANK_ID,'temc__comments')) as "comments"
						from 
							FAF.FA_MROS_KD_STAMM se
						left join 
							FAF.FA_MROS_KD_ADR_XML sea
						on sea.kd_lnr=se.kd_lnr and sea.userbk_nr=se.userbk_nr
						left join
							FAF.FA_MROS_KD_EMAIL_XML spp
						on spp.kd_lnr=se.kd_lnr and spp.userbk_nr=se.userbk_nr
						left join
							FAF.FA_MROS_INT_LAND cntr1
						on cntr1.iso_land_cd=se.iso_land_code_nat and cntr1.userbk_nr=se.userbk_nr
						left join
							FAF.FA_MROS_REFERENCE_TABLE lf
						on se.userbk_nr=lf.userbk_nr and cast(se.kd_rstat_cd as varchar)=lf.finnova_value and lf.ref_type=8
						left join
							FAF.FA_MROS_REFERENCE_TABLE tax
						on se.userbk_nr=tax.userbk_nr and se.KD_SITZ_GSCH_CD=tax.finnova_value and tax.ref_type=3
						left join
							(select userbk_nr,kd_lnr,kd_ausw_nr,
								row_number() over (partition by userbk_nr,kd_lnr order by ausstell_dat desc) as rn
							from FAF.FA_MROS_KD_VSB where kd_ausw_nr is not NULL and cast(iddoc_cd as varchar)=FAF.GoAML_get_paramS(@F_BANK_ID,71,2)) vsb
						on se.userbk_nr=vsb.userbk_nr and se.kd_lnr=vsb.kd_lnr and vsb.rn=1
						where se.kd_lnr=@n_id and se.userbk_nr=@F_BANK_ID and se.partner_flag=0
						FOR XML PATH (''),type, ELEMENTS);
				end;

				if @xml_cl is not NULL
				begin
					set @xml_cl = 
						(select 
							case 
								when FAF.GoAML_get_paramS(@F_BANK_ID,128,1)='ok'
								then FAF.GoAML_char_process(@rol,FAF.GoAML_get_paramV(@F_BANK_ID,'rpt__significance'),FAF.GoAML_get_paramL(@F_BANK_ID,'rpt__significance'))
								else NULL
							end as "role",
							case
								when @kd_tp>1 
								then NULL
								else @xml_cl
							end as "person_my_client",
							case
								when @kd_tp=1 
								then NULL
								else @xml_cl
							end as "entity_my_client",
							case 
								when FAF.GoAML_get_paramS(@F_BANK_ID,128,1)='ok'
								then NULL
								else FAF.GoAML_char_process(@rol,FAF.GoAML_get_paramV(@F_BANK_ID,'rpt__significance'),FAF.GoAML_get_paramL(@F_BANK_ID,'rpt__significance'))
							end as "significance",
							FAF.GoAML_char_process(
								coalesce(@comm,
									case 
										when FAF.GoAML_get_paramS(@F_BANK_ID,53,1)='ok' and FAF.GoAML_get_paramS(@F_BANK_ID,53,2)=FAF.GoAML_get_paramS(@F_BANK_ID,36,1)
										then FAF.GoAML_get_paramS(@F_BANK_ID,36,2)
										else NULL
									end
								)
							,FAF.GoAML_get_paramV(@F_BANK_ID,'rpt__reason'),FAF.GoAML_get_paramL(@F_BANK_ID,'rpt__reason')) as "reason"
						FOR XML PATH ('report_party'),type, ELEMENTS);

					set @itms = @itms + 1;
					set @xml_r = (select @xml_r,@xml_cl FOR XML PATH (''),type, ELEMENTS);
					if @itms = @max_items
						break;
				end;

				set @jj = @jj + 1;
			end;
		end;

		--remain part of the report
		set @xml_r = 
			(select 
				(select @xml_r FOR XML PATH ('report_parties'),type, ELEMENTS) 
			FOR XML PATH ('activity'),type, ELEMENTS);
		set @xml_r =FAF.head_part_of_report (@F_BANK_ID,@RP_NAME,@RP_SURNAME,@RP_PHONE,@RP_EMAIL,@RP_OCCUPATION,@RP_ADDRESS,@RP_CITY,@FIU_REF_NUMBER,@ENTITY_REFERENCE,@REPORT_REASON,@R_ACTION,@REPORT_INDICATORS,@xml_r,0,@REPORT_CODE,@N_BR);
	end;

	return(@xml_r);
end;
go

