SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE procedure [dbo].[p_rep_trn_summary_details]
	@transaction_id int
as 
begin
	set nocount on;

	declare	@actor_id	int,
			@lettering	int


	select	@actor_id = a_actor_id, 
			@lettering = a_lettering
	from	acc_accounts
	where	a_account_id = @transaction_id

	if exists (	select	1
				from	vw_act_clients 
				where	act_actor_id = @actor_id )
	begin
		-- client

		select	a_operation_utc_date, a.a_db, a_cr = isnull(a.a_cr, 0) + isnull(a.a_hs, 0), t.atyp_l1, t.atyp_l2, t.atyp_l3
		from	vw_acc_account_clients a
				inner join acc_accounts_types t	on	t.atyp_type_code = a.a_type_code
		where	a_type_code != 'T'
		and		a_actor_id = @actor_id
		and		a_lettering = @lettering
		order by a_operation_utc_date

	end
	else
	begin

		select	a_operation_utc_date, a.a_db, a_cr = isnull(a.a_cr, 0) + isnull(a.a_hs, 0), t.atyp_l1, t.atyp_l2, t.atyp_l3
		from	vw_acc_account_actors a
				inner join acc_accounts_types t	on	t.atyp_type_code = a.a_type_code
		where	a_type_code != 'T'
		and		a_actor_id = @actor_id
		and		a_lettering = @lettering
		order by a_operation_utc_date

	end

end
GO
