SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE [dbo].[p_claim_variables_accounts_delete_by_table_id]
	@table_id type_table_id READONLY,
	@update_pay				bit,
	@update_rec				bit
AS

	/*
	DGE

	Suppression des règlements sinistres / encaissements des recours

	*/


	set nocount on;


	-- vérification
	if exists ( select	1
				from	claim_variables_accounts a
						inner join @table_id t on t.id = a.clacc_account_id
				where	a.clacc_operation_code != 'IS' )
	begin
		raiserror('Cannot delete validated lines.', 16,1)
		return		

	end


	declare	@claims type_table_id


	if @update_pay != 0 or @update_rec != 0 
	begin 

		insert into	@claims(id)
		select	distinct clacc_claim_id
		from	claim_variables_accounts a
				inner join @table_id t on t.id = a.clacc_account_id

	end



	delete	a
	from	claim_variables_accounts a
			inner join @table_id t on t.id = a.clacc_account_id

	delete	a
	from	acc_accounts a
			inner join @table_id t on t.id = a.a_account_id


	if @update_pay != 0
	begin
		exec p_claim_variables_update_pay @claims, -1
	end

	if @update_rec != 0
	begin
		exec p_claim_variables_update_rec @claims, -1
	end

GO
