SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE proc [dbo].[p_test_accounts_delete]
	@delete_i bit = 1,
	@delete_t bit = 1,
	@delete_a bit = 1,
	@delete_b bit = 1,
	@delete_p bit = 1,
	@delete_cp bit = 1,
	@delete_csd bit = 1,
	@delete_pl bit = 1
as

	/*
	[DGE]
	Cette procédure est utilisée pour tester les fonctionnalités. 
	Il ne faut pas l'utiliser sur les bases de production
	*/


	--select	* from acc_accounts_types 

	-- invoices
	if @delete_i != 0 
	BEGIN

		  DELETE FROM pol_policies_situations_main_invoices
		  DELETE FROM ris_risks_situations_main_invoices
		  DELETE FROM pol_policies_situations_regul_invoices
		  DELETE FROM pol_policies_accounts
		  
		delete acc_invoices_risks_variables_commission_taxes_cies
		delete acc_invoices_risks_variables_commission_taxes
		delete acc_invoices_risks_variables_taxes_cies
		delete acc_invoices_risks_variables_taxes
		delete acc_invoices_risks_variables_introducers
		delete acc_invoices_risks_variables_cies
		delete acc_invoices_risks_variables
		delete acc_invoices_risks_situations
		
		delete acc_invoices_policies_variables_commission_taxes_cies
		delete acc_invoices_policies_variables_commission_taxes
		delete acc_invoices_policies_variables_taxes_cies
		delete acc_invoices_policies_variables_taxes
		delete acc_invoices_policies_variables_introducers
		delete acc_invoices_policies_variables_cies
		delete acc_invoices_policies_variables
		delete acc_invoices_operations

		delete acc_invoices

		delete acc_accounts where a_type_code = 'I'
	end


	-- commission sharing deduced
	if @delete_cp != 0
	begin
		delete acc_commission_sharing_deduced

		delete acc_accounts where a_type_code = 'CSD'
	end
	
	-- transactions
	if @delete_t != 0
	begin
		delete acc_transactions 
		
		delete acc_trn_bordereaux_details
		delete acc_trn_bordereaux_accounts
		delete acc_trn_bordereaux_departments

		delete acc_trn_bordereaux_companies
		delete acc_trn_bordereaux_introducers

		update acc_trn_bordereaux set trnb_tmp = 0
		delete acc_trn_bordereaux

		delete	l
		from	acc_accounts_links l
				inner join acc_accounts a on a.a_account_id = l.alnk_account_id__dst
		
		delete	l
		from	acc_accounts_links l
				inner join acc_accounts a on a.a_account_id = l.alnk_account_id__src
				
				
		delete acc_accounts where a_type_code = 'T'
	end
	
	-- acomptes
	if @delete_a != 0
	begin
		delete acc_advances 
		
		delete acc_accounts where a_type_code = 'A'
	end
	
	if @delete_b != 0
	begin
		delete acc_bordereaux_variables_taxes
		delete acc_bordereaux_variables
		delete acc_bordereaux_products
		delete acc_bordereaux_details
		delete acc_bordereaux_companies
		delete acc_bordereaux
		
		update acc_bordereaux set bdr_operation_code = 'IS'
		delete acc_bordereaux
		
		delete acc_accounts where a_type_code = 'B'		
	end
	
	-- paiements
	if @delete_p != 0
	begin
		delete acc_payments
		
		delete acc_accounts where a_type_code = 'P'
	end	

	-- règlements sinistres
	if @delete_cp != 0
	begin
		delete claim_variables_accounts

		delete acc_accounts where a_type_code in ('CP', 'CR')
	end


	-- perte - profit
	if @delete_pl != 0
	begin 
		delete acc_profits_losses

		delete acc_accounts where a_type_code = 'PL'
	end



GO
