SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

CREATE procedure [dbo].[p_acc_transactions_check]
	@table_id type_table_id READONLY
as 

	set nocount on;

	/*

	DGE

	Vérification des transactions avant leur suppression
	- on vérifie que le type des transactions est connu
	- on vérifie que les transactions ne sont pas incluses dans un bordereau

	*/

	-- type inconnu
	select  distinct atrn_transaction_id
	from	@table_id t
			inner join acc_transactions trn on trn.atrn_transaction_id = t.id
	where	trn.atrn_type_code is null
	
	-- transactions contenues dans un bordereau de transactions
	select	distinct trnbd_transaction_id
	from	@table_id t
			inner join acc_trn_bordereaux_details bd	on bd.trnbd_transaction_id = t.id

	-- transactions de type Acompte, associées a des acomptes repris
	select	distinct atrn_transaction_id
	from	acc_transactions trn
			inner join @table_id t on t.id = trn.atrn_transaction_id
			inner join acc_accounts a on a.a_account_id = trn.atrn_transaction_id

			inner join vw_acc_advances adv	on	adv.a_actor_id = a.a_actor_id
											and	adv.adv_lettering = a.a_lettering
	where	trn.atrn_type_code = 'aco'
	and		adv.adv_taken != 0
			



	
GO
