SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE [dbo].[p_pol_policies_situation_invoice_unit_rate_data]
    @policy_situation_id int,
    @return_all_policy_situations bit,
	@return_policy_situation_invoices_list bit 
AS 
	set nocount on

	select pp.*
	from pol_policies_situations pps
		inner join pol_policies pp on pp.pol_policy_id = pps.pols_policy_id
	where pps.pols_policy_situation_id = @policy_situation_id

	select ppsi.*
	from pol_policies_situations_introducers ppsi
		inner join pol_policies_situations pps on pps.pols_policy_situation_id = ppsi.polsi_policy_situation_id
	where pps.pols_policy_situation_id = @policy_situation_id
		
	select ppb.*, ac.*
	from pol_policies_situations pps
		inner join pol_policies pp on pp.pol_policy_id = pps.pols_policy_id
		inner join pr_products_branches ppb on		ppb.prdb_branch_id = pp.pol_branch_id
												and ppb.prdb_product_code = pp.pol_product_code
		inner join acc_currencies ac on ac.cur_currency_code = ppb.prdb_currency_code
	where pps.pols_policy_situation_id = @policy_situation_id

	if @return_all_policy_situations = 1
	begin
		select dest.*
		from pol_policies_situations pps
			inner join pol_policies_situations dest on dest.pols_policy_id = pps.pols_policy_id
		where pps.pols_policy_situation_id = @policy_situation_id
	end 
	else
	begin
		select pps.*
		from pol_policies_situations pps
		where pps.pols_policy_situation_id = @policy_situation_id
	end 

	if @return_policy_situation_invoices_list = 1
	begin
		select ai.*
		from acc_invoices ai
		where ai.inv_policy_situation_id = @policy_situation_id 
	end

	
	


GO
