
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

CREATE procedure [dbo].[p_database_init]
	@opt_seed int,
	@opt_db_version varchar(50),
	@branch_actor_id int,
	@branch_internal_reference ais_code_medium,
	@branch_address_id int
as
	/*
	select * from vw_act_branches

	exec p_database_init @opt_seed = 10000000, @opt_db_version = '1.0.0.1276', @branch_actor_id = 9, @branch_internal_reference = 'REU_1', @branch_address_id = 9
	*/

	if exists(select 1 from ais_options)
	BEGIN
		print 'ais_options already contains data, p_database_init wil exit'
	end
	else
	begin

		insert into ais_options	(	opt_seed,	opt_db_version,		opt_allow_update_variables)
		values					(	@opt_seed,	@opt_db_version,	0)

		if not exists(select 1 from acc_currencies where cur_currency_code = 'EUR')
		BEGIN
			insert into acc_currencies	(cur_currency_code, cur_l1, cur_use_decimals)
			values						('EUR',				'EURO',	1)
		end

		if not exists(select 1 from act_branches where br_actor_id = @branch_actor_id)
		BEGIN
			insert into adr_addresses	(	adr_address_id,		adr_name)
			values						(	@branch_address_id, @branch_internal_reference)

			insert into act_actors	(	act_actor_id,		act_type_code,	act_internal_reference,		act_address_id,		act_freeze)
			values					(	@branch_actor_id,	'BRA',			@branch_internal_reference,	@branch_address_id,	0 )

			insert into act_branches(	br_actor_id,		br_currency_code)
			values					(	@branch_actor_id,	'EUR')

			insert into act_actors_addresses(adra_actor_id,adra_address_id) values(@branch_actor_id,@branch_address_id)

			exec p_branches_add_default_department @branch_id = @branch_actor_id

		end 

		if not exists(select 1 from usr_profils where pr_profil_code = 'admin')
		BEGIN
			insert into usr_profils	( pr_profil_code,	pr_l1,		pr_sys,		pr_freeze_allowed,	pr_freeze,	pr_admin,	pr_allow_save_grids_layout)
			values					( 'admin',			'admin',	1,			1,					0,			1,			1)					
		end

		if not exists(select 1 from usr_users where usr_user_code = 'admin')
		BEGIN
			insert into usr_users	( usr_user_code,	usr_profil_code,		usr_name,		usr_culture_code,	usr_freeze,		usr_sys,	usr_date_setup, usr_pwd)
			values					( 'admin',			'admin',				'admin',		'en-US',			0,				1,			getdate(),		'')					
		end

		if not EXISTS(select 1 from usr_users_branches_departments where usrbdep_branch_id = @branch_actor_id and usrbdep_user_code = 'admin')
		begin
			insert into usr_users_branches_departments	( usrbdep_branch_id,	usrbdep_user_code,	usrbdep_department_code)
			values										( @branch_actor_id,		'admin',			@branch_internal_reference)			
		end

		
	end

/*
delete from ais_options
delete from usr_users_branches_departments
delete from usr_users
delete from usr_profils
delete from act_branches_departments
delete from act_branches
delete from act_actors
delete from adr_addresses

select * from ais_options
select * from usr_profils
select * from usr_users
select * from usr_users_branches_departments
select * from act_branches_departments
select * from act_branches
select * from act_actors
select * from adr_addresses

sp_help usr_users
sp_help usr_profils
sp_help act_branches
sp_help act_actors
sp_help adr_addresses

select * from act_types
*/
GO
