SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
create procedure [dbo].[p_invoice_introducers]
	@invoice_id int	
as

	/*
	declare @app table(introducer_id int not null, amount ais_decimal2 null)
	
	insert INTO @app(	introducer_id,			amount)
	select				invpolvi_introducer_id, SUM(invpolvi_value)
	from acc_invoices_policies_variables_introducers 
	where invpolvi_invoice_id = @invoice_id
	GROUP by invpolvi_introducer_id
	
	insert INTO @app(	introducer_id,			amount)
	select				invrisvi_introducer_id, SUM(invrisvi_value) 
	from acc_invoices_risks_variables_introducers 
	where invrisvi_invoice_id = @invoice_id
	GROUP by invrisvi_introducer_id
	*/
	
	select a.act_internal_reference, a.int_actor_id,act_date_setup, a.adr_complete_name --, invrisvi_value = t.amount
	from vw_act_introducers a
		inner join (
					/*SELECT introducer_id, SUM(amount) as amount
					from @app 
					group BY introducer_id
					*/
					select distinct introducer_id = invpolvi_introducer_id
					from acc_invoices_policies_variables_introducers 
					where invpolvi_invoice_id = @invoice_id
					
					union 
					
					select distinct	introducer_id = invrisvi_introducer_id
					from acc_invoices_risks_variables_introducers 
					where invrisvi_invoice_id = @invoice_id
					
					) as t ON a.int_actor_id = t.introducer_id
GO
