SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

CREATE procedure [dbo].[p_rep_int_bdx_header]
	@branch_id			int,
	@user_code			ais_code_medium,
	@bordereau_id		int
as 

	set nocount on;


	select	-- informations concernant l'utilisateur
			usr.usr_name, usr.usr_mail, 
			-- informations concernant l'entité juridique
			branch_internal_reference = branch.act_internal_reference,
			branch_external_reference = branch.act_external_reference,
			branch_display_name = branch.br_display_name,
			branch_name = branch.adr_complete_name,
			branch_addr1 = branch.adr_address1,
			branch_addr2 = branch.adr_address2,
			branch_addr3 = branch.adr_address3,
			branch_postal_code = branch.adr_postal_code,
			branch_city = branch.adr_city,
			branch_mail = branch.adr_mail,
			branch_home_phone = branch.adr_home_phone,
			-- informations concernant l'apporteur
			int_internal_reference = i.act_internal_reference,
			int_external_reference = i.act_external_reference,
			int_repr = i.int_repr,
			int_name = i.adr_complete_name,
			int_addr1 = i.adr_address1,
			int_addr2 = i.adr_address2,
			int_addr3 = i.adr_address3,
			int_postal_code = i.adr_postal_code,
			int_city = i.adr_city,
			int_mail = i.adr_mail,
			int_home_phone = i.adr_home_phone,
			-- informations concernant le code devise 
			bdr_devise_code = i.a_currency_code,
			bdr_reference = i.bdr_reference

	from	(	select *
				from	usr_users
				where	usr_user_code = @user_code ) as usr

			inner join	(	select	*
							from	vw_act_branches 
							where	br_actor_id = @branch_id ) as branch	on 1 = 1

			inner join	(		select	i.*, a.a_currency_code, b.bdr_reference
								from	acc_bordereaux b
										inner join acc_accounts a on a.a_account_id = b.bdr_bordereau_id
										inner join vw_act_introducers i on i.act_actor_id = a.a_actor_id
								where	b.bdr_bordereau_id = @bordereau_id ) as i on 1 = 1
	





GO
