SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE [dbo].[p_grid_act_branch_used_cies]
	@branch_id AS INT,
	@branch_used_company_id as varchar(15) = '%',
	@family_code as ais_code_short = '%'
AS

/*
	@company_id - camp de filtrare
	@family_code
		- '%' - nu se tine cont de acest camp, query-ul intoarce toate companiile (filtreaza dupa @company_id)
		- != '%' - sunt returnate companiile apartinand familiei specificate (tabela pr_families_cies), cu filtru pe @company_id
*/


set nocount on

declare @id int 

if @branch_used_company_id = '%'
	set @id = 0
else
	set @id = convert(int, @branch_used_company_id)


select	*
from	vw_act_cies vac 
		INNER JOIN vw_act_branches_used_cies vabuc ON		vabuc.pol_company_id = vac.cie_actor_id
														AND vabuc.pol_branch_id = @branch_id
where	(@id = 0 or act_actor_id = @id)
and		(
			(@family_code = '%')
			
			OR
			
			EXISTS	(
					SELECT	1
					FROM	pr_families_cies
					WHERE	prfc_family_code = @family_code
					AND		prfc_company_id = cie_actor_id )
		)

GO
