SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO








CREATE PROCEDURE [dbo].[p_ext_policy_replace_duplicate_invoice]
	@source_account_id INT,
	@destination_account_id INT,
	@destination_policy_situation_id INT,
	@start_date DATE,
	@end_date DATE,
	@utc_date DATETIME,
	@operation_code ais_code_short,
	@user_code ais_code_medium,
	@prorate_coeff DECIMAL(20,15),
	@transcodage_situations_risques type_source_destination READONLY,
	@transcodage_variables_ids type_source_destination READONLY,
	@transcodage_taxes_ids type_source_destination READONLY
AS
	SET NOCOUNT ON;

	--DECLARE @debug_transcodage_situations_risques XML = (SELECT * FROM @transcodage_situations_risques FOR XML AUTO ,ROOT('r'))
	--DECLARE @debug_transcodage_variables_ids XML = (SELECT * FROM @transcodage_variables_ids FOR XML AUTO ,ROOT('r'))
	--DECLARE @debug_transcodage_taxes_ids XML = (SELECT * FROM @transcodage_taxes_ids FOR XML AUTO ,ROOT('r'))

	/*
		COMPTE ET QUITTANCE
	*/
	DECLARE @branch_id INT
	DECLARE @department_code ais_code_medium
	DECLARE @nodecimals INT

	SELECT	@branch_id = pp.pol_branch_id,
			@department_code = pp.pol_department_code,
			@nodecimals = CASE WHEN ac.cur_use_decimals = 1 THEN 2 ELSE 0 end
	FROM pol_policies pp
		INNER JOIN pol_policies_situations pps ON pps.pols_policy_id = pp.pol_policy_id
		INNER JOIN acc_invoices ai ON ai.inv_policy_situation_id = pps.pols_policy_situation_id
		INNER JOIN act_branches ab ON ab.br_actor_id = pp.pol_branch_id
		INNER JOIN acc_currencies ac ON ab.br_currency_code = ac.cur_currency_code
	WHERE ai.inv_invoice_id = @source_account_id

	-- création de la ligne compte client
	INSERT INTO acc_accounts(	a_account_id,				a_type_code,	a_branch_id, a_amount,											a_operation_utc_date,	a_lettering,	a_currency_code,	a_actor_id)
	SELECT						@destination_account_id,	a_type_code,	a_branch_id, ROUND(aa.a_amount * @prorate_coeff, @nodecimals),	@utc_date,				NULL,			aa.a_currency_code,	aa.a_actor_id
	FROM acc_accounts aa
	WHERE aa.a_account_id = @source_account_id

	--DECLARE @debug_acc_accounts XML = (SELECT * FROM acc_accounts WHERE a_account_id = @destination_account_id FOR XML AUTO ,ROOT('r'))

	-- création de la ligne quittance
	INSERT INTO acc_invoices (
			inv_invoice_id,						inv_policy_situation_id,			inv_start_date,						inv_end_date,					inv_ratio,							inv_operation_code__last, 
			inv_cr_date,						--inv_db_date, inv_an_date,			inv_ec_date,						inv_rmb_date,					inv_lettering,						inv_type_code__cts, 
			--inv_cts_fee,						inv_cts_date,						inv_awr_date,						inv_inf_date,					
			inv_monthly_payment_status_code,	inv_monthly_payment_reject_count,	inv_monthly_payment_date,			inv_observations)
	SELECT	@destination_account_id,			@destination_policy_situation_id,	@start_date,						@end_date,						1,									@operation_code,
			@start_date,
			inv_monthly_payment_status_code,	inv_monthly_payment_reject_count,	inv_monthly_payment_date,			inv_observations
	FROM acc_invoices ai
	WHERE ai.inv_invoice_id = @source_account_id

	-- mettre à jour les dates en fonction de l'opération
	IF @operation_code = 'CR'
	BEGIN
		UPDATE acc_invoices
		SET inv_cr_date = @start_date
		WHERE inv_invoice_id = @destination_account_id
	END
	ELSE
	BEGIN
		IF @operation_code = 'DB'	
		BEGIN
			UPDATE acc_invoices
			SET inv_cr_date = @start_date,
				inv_db_date = @start_date
			WHERE inv_invoice_id = @destination_account_id
		END
		ELSE
		BEGIN
			IF @operation_code = 'RMB'
			BEGIN
				UPDATE acc_invoices
				SET inv_cr_date = @start_date,
					inv_db_date = @start_date,
					inv_rmb_date = @start_date
				WHERE inv_invoice_id = @destination_account_id
			END
		END
	END

	--DECLARE @debug_acc_invoices XML = (SELECT * FROM acc_invoices WHERE inv_invoice_id = @destination_account_id FOR XML AUTO ,ROOT('r'))

	-- ajouter l'historique de l'opération
	INSERT INTO acc_invoices_operations (
			invop_invoice_id,			invop_operation_utc_date,	invop_operation_code,		invop_user_code,	invop_obs,	invop_branch_id,	invop_department_code)
	VALUES (@destination_account_id,	@utc_date,					@operation_code,			@user_code,			null,		@branch_id,			@department_code);

	--DECLARE @debug_acc_invoices_operations XML = (SELECT * FROM acc_invoices_operations WHERE invop_invoice_id = @destination_account_id FOR XML AUTO ,ROOT('r'))

	/*
		Variables contrat
	*/

	-- copier et calculer les variables contrat
	INSERT INTO acc_invoices_policies_variables 
			(	invpolv_invoice_id,			invpolv_policy_situation_id,		invpolv_variable_id,	invpolv_coef_reduction, 
				invpolv_tf, 
				invpolv_taxes, 
				invpolv_ti, 
				invpolv_commission_tf, 
				invpolv_commission_taxes, 
				invpolv_commission_ti, 
				invpolv_commission_percent)
	SELECT		@destination_account_id,	@destination_policy_situation_id,	tvi.destination_id,		invpolv_coef_reduction,	
				ROUND(invpolv_tf * @prorate_coeff, @nodecimals),
				ROUND(invpolv_taxes * @prorate_coeff, @nodecimals),
				ROUND(invpolv_ti * @prorate_coeff, @nodecimals),
				ROUND(invpolv_commission_tf * @prorate_coeff, @nodecimals),
				ROUND(invpolv_commission_taxes * @prorate_coeff, @nodecimals),
				ROUND(invpolv_commission_ti * @prorate_coeff, @nodecimals),
				invpolv_commission_percent
	FROM acc_invoices_policies_variables aipv
		INNER JOIN @transcodage_variables_ids tvi ON tvi.source_id = aipv.invpolv_variable_id
	WHERE aipv.invpolv_invoice_id = @source_account_id

	--DECLARE @debug_acc_invoices_policies_variables XML = (SELECT * FROM acc_invoices_policies_variables WHERE invpolv_invoice_id = @destination_account_id FOR XML AUTO ,ROOT('r'))

	-- copier et calculer les taxes sur les commissions des variables contrat
	INSERT INTO acc_invoices_policies_variables_commission_taxes (
				invpolvct_invoice_id,		invpolvct_policy_situation_id,		invpolvct_variable_id,	invpolvct_tax_id,	invpolvct_tax_value)
	SELECT		@destination_account_id,	@destination_policy_situation_id,	tvi.destination_id,		tti.destination_id,	ROUND(invpolvct_tax_value * @prorate_coeff, @nodecimals)
	FROM acc_invoices_policies_variables_commission_taxes aipvct
		INNER JOIN @transcodage_variables_ids tvi ON tvi.source_id = aipvct.invpolvct_variable_id
		INNER JOIN @transcodage_taxes_ids tti ON tti.source_id = aipvct.invpolvct_tax_id
	WHERE aipvct.invpolvct_invoice_id = @source_account_id

	--DECLARE @debug_acc_invoices_policies_variables_commission_taxes XML = (SELECT * FROM acc_invoices_policies_variables_commission_taxes WHERE invpolvct_invoice_id = @destination_account_id FOR XML AUTO ,ROOT('r'))

	-- copier et calculer les taxes sur les variables contrat
	INSERT INTO acc_invoices_policies_variables_taxes (
				invpolvt_invoice_id,		invpolvt_policy_situation_id,		invpolvt_variable_id,	invpolvt_tax_id,	invpolvt_tax_value)
	SELECT		@destination_account_id,	@destination_policy_situation_id	,tvi.destination_id,	tti.destination_id,	ROUND(invpolvt_tax_value * @prorate_coeff, @nodecimals)
	FROM acc_invoices_policies_variables_taxes aipvt
		INNER JOIN @transcodage_variables_ids tvi ON tvi.source_id = aipvt.invpolvt_variable_id
		INNER JOIN @transcodage_taxes_ids tti	ON tti.source_id = aipvt.invpolvt_tax_id
	WHERE aipvt.invpolvt_invoice_id = @source_account_id

	--DECLARE @debug_acc_invoices_policies_variables_taxes XML = (SELECT * FROM acc_invoices_policies_variables_taxes WHERE invpolvt_invoice_id = @destination_account_id FOR XML AUTO ,ROOT('r'))

	-- si necessaire recalculer le total des taxes et le ttc des variables contrat
	IF ABS(@prorate_coeff) != 1.0 
	BEGIN

		UPDATE aipv
			SET invpolv_commission_taxes = ISNULL(sum_taxes.tax_sum,0),
				invpolv_commission_ti = aipv.invpolv_commission_tf + ISNULL(sum_taxes.tax_not_included_in_net_sum,0)
		FROM acc_invoices_policies_variables aipv
			INNER JOIN (	SELECT	policy_situation_id = aipvct.invpolvct_policy_situation_id,
									invoice_id = aipvct.invpolvct_invoice_id,
									variable_id = aipvct.invpolvct_variable_id,
									tax_sum = SUM(aipvct.invpolvct_tax_value),
									tax_not_included_in_net_sum = SUM(	CASE
																			WHEN at.acctax_net_included = 0 THEN aipvct.invpolvct_tax_value
																			ELSE 0
																		END)
							FROM acc_invoices_policies_variables_commission_taxes aipvct
								INNER JOIN acc_taxes at ON	at.acctax_tax_id = aipvct.invpolvct_tax_id
							WHERE aipvct.invpolvct_invoice_id = @destination_account_id
							GROUP BY aipvct.invpolvct_policy_situation_id, aipvct.invpolvct_invoice_id, aipvct.invpolvct_variable_id 
						)	AS sum_taxes ON		sum_taxes.policy_situation_id = aipv.invpolv_policy_situation_id		
											AND	sum_taxes.invoice_id = aipv.invpolv_invoice_id
											AND sum_taxes.variable_id = aipv.invpolv_variable_id

		UPDATE aipv
			SET invpolv_taxes = ISNULL(sum_taxes.tax_sum,0),
				invpolv_ti = aipv.invpolv_tf + ISNULL(sum_taxes.tax_not_included_in_net_sum,0)
		FROM acc_invoices_policies_variables aipv
			INNER JOIN (	SELECT	policy_situation_id = aipvt.invpolvt_policy_situation_id,
									invoice_id = aipvt.invpolvt_invoice_id,
									variable_id = aipvt.invpolvt_variable_id,
									tax_sum = SUM(aipvt.invpolvt_tax_value),
									tax_not_included_in_net_sum = SUM(	CASE
																			WHEN at.acctax_net_included = 0 THEN aipvt.invpolvt_tax_value
																			ELSE 0
																		END)
							FROM acc_invoices_policies_variables_taxes aipvt
								INNER JOIN acc_taxes at ON	at.acctax_tax_id = aipvt.invpolvt_tax_id
							WHERE aipvt.invpolvt_invoice_id = @destination_account_id
							GROUP BY aipvt.invpolvt_policy_situation_id, aipvt.invpolvt_invoice_id, aipvt.invpolvt_variable_id 
						)	AS sum_taxes ON		sum_taxes.policy_situation_id = aipv.invpolv_policy_situation_id		
											AND	sum_taxes.invoice_id = aipv.invpolv_invoice_id
											AND sum_taxes.variable_id = aipv.invpolv_variable_id

		--DECLARE @debug_acc_invoices_policies_variables_after_update XML = (SELECT * FROM acc_invoices_policies_variables WHERE invpolv_invoice_id = @destination_account_id FOR XML AUTO ,ROOT('r'))

	END

	--DECLARE @debug_acc_invoices_policies_variables_introducers_source XML = (SELECT * FROM acc_invoices_policies_variables_introducers WHERE invpolvi_invoice_id = @source_account_id FOR XML AUTO ,ROOT('r'))

	-- copier et calculer la retrocession apporteurs pour les variables contrat
	INSERT INTO acc_invoices_policies_variables_introducers (
			invpolvi_invoice_id,		invpolvi_policy_situation_id,		invpolvi_variable_id,	invpolvi_introducer_id, invpolvi_value,										invpolvi_commission_sharing_deduced)
	SELECT	@destination_account_id,	@destination_policy_situation_id,	tvi.destination_id,		invpolvi_introducer_id, ROUND(invpolvi_value * @prorate_coeff, @nodecimals),invpolvi_commission_sharing_deduced
	FROM acc_invoices_policies_variables_introducers aipvi
		INNER JOIN @transcodage_variables_ids tvi ON tvi.source_id = aipvi.invpolvi_variable_id
	WHERE aipvi.invpolvi_invoice_id = @source_account_id


	--DECLARE @debug_acc_invoices_policies_variables_introducers_destination XML = (SELECT * FROM acc_invoices_policies_variables_introducers WHERE invpolvi_invoice_id = @destination_account_id FOR XML AUTO ,ROOT('r'))

	/*
		Variables et situations risques
	*/

	-- copier les situations risques
	INSERT INTO acc_invoices_risks_situations (
			invris_invoice_id,			invris_risk_situation_id,	invris_start_date,	invris_end_date)
	SELECT	@destination_account_id,	tsr.destination_id,			@start_date,		@end_date	
	FROM acc_invoices_risks_situations airs
		INNER JOIN @transcodage_situations_risques tsr ON tsr.source_id = airs.invris_risk_situation_id
	WHERE airs.invris_invoice_id = @source_account_id

	--DECLARE @debug_acc_invoices_risks_situations XML = (SELECT * FROM acc_invoices_risks_situations WHERE invris_invoice_id = @destination_account_id FOR XML AUTO ,ROOT('r'))

	

	-- copier et calculer les variables risques
	INSERT INTO acc_invoices_risks_variables (
				invrisv_invoice_id,			invrisv_risk_situation_id,		invrisv_variable_id,		invrisv_coef_reduction,
				invrisv_tf,
				invrisv_taxes,
				invrisv_ti,
				invrisv_commission_tf,
				invrisv_commission_taxes,
				invrisv_commission_ti,
				invrisv_commission_percent)
	SELECT
				@destination_account_id,	tsr.destination_id,				tvi.destination_id,			invrisv_coef_reduction,
				ROUND(invrisv_tf * @prorate_coeff, @nodecimals),
				ROUND(invrisv_taxes * @prorate_coeff, @nodecimals),
				ROUND(invrisv_ti * @prorate_coeff, @nodecimals),
				ROUND(invrisv_commission_tf * @prorate_coeff, @nodecimals),
				ROUND(invrisv_commission_taxes * @prorate_coeff, @nodecimals),
				ROUND(invrisv_commission_ti * @prorate_coeff, @nodecimals),
				invrisv_commission_percent
	FROM acc_invoices_risks_variables airv
		INNER JOIN @transcodage_situations_risques tsr ON tsr.source_id = airv.invrisv_risk_situation_id
		INNER JOIN @transcodage_variables_ids tvi	ON tvi.source_id = airv.invrisv_variable_id
	WHERE airv.invrisv_invoice_id = @source_account_id

	--DECLARE @debug_acc_invoices_risks_variables XML = (SELECT * FROM acc_invoices_risks_variables WHERE invrisv_invoice_id = @destination_account_id FOR XML AUTO ,ROOT('r'))

	--DECLARE @debug_acc_invoices_risks_variables_introducers_source XML = (SELECT * FROM acc_invoices_risks_variables_introducers WHERE invrisvi_invoice_id = @source_account_id FOR XML AUTO ,ROOT('r'))

	--SELECT * FROM acc_invoices_risks_variables_introducers WHERE invrisvi_invoice_id = @source_account_id

	--select * from ris_variables where risv_risk_situation_id in (SELECT distinct airvi.invrisvi_risk_situation_id FROM acc_invoices_risks_variables_introducers airvi WHERE invrisvi_invoice_id = @source_account_id)

	--select * from @transcodage_situations_risques

	--select * 
	--from @transcodage_situations_risques tsr 
	--	inner join (SELECT distinct airvi.invrisvi_risk_situation_id FROM acc_invoices_risks_variables_introducers airvi WHERE invrisvi_invoice_id = @source_account_id) as t on t.invrisvi_risk_situation_id = tsr.source_id
	--	inner join ris_variables rv on rv.risv_risk_situation_id = tsr.destination_id

	--SELECT	@destination_account_id,	tsr.destination_id,				tvi.destination_id,		invrisvi_introducer_id,		ROUND(invrisvi_value * @prorate_coeff, @nodecimals),	invrisvi_commission_sharing_deduced
	--FROM acc_invoices_risks_variables_introducers airvi
	--	INNER JOIN @transcodage_situations_risques tsr ON tsr.source_id = airvi.invrisvi_risk_situation_id
	--	INNER JOIN @transcodage_variables_ids tvi	ON tvi.source_id = airvi.invrisvi_variable_id
	--WHERE airvi.invrisvi_invoice_id = @source_account_id
	--and not exists(select 1 from ris_variables rv where rv.risv_risk_situation_id = tsr.destination_id and rv.risv_variable_id = tvi.destination_id)

	-- copier et calculer la retrocession apporteurs pour les variables risques
	INSERT INTO acc_invoices_risks_variables_introducers (
			invrisvi_invoice_id,		invrisvi_risk_situation_id,		invrisvi_variable_id,	invrisvi_introducer_id,		invrisvi_value,											invrisvi_commission_sharing_deduced)
	SELECT	@destination_account_id,	tsr.destination_id,				tvi.destination_id,		invrisvi_introducer_id,		ROUND(invrisvi_value * @prorate_coeff, @nodecimals),	invrisvi_commission_sharing_deduced
	FROM acc_invoices_risks_variables_introducers airvi
		INNER JOIN @transcodage_situations_risques tsr ON tsr.source_id = airvi.invrisvi_risk_situation_id
		INNER JOIN @transcodage_variables_ids tvi	ON tvi.source_id = airvi.invrisvi_variable_id
	WHERE airvi.invrisvi_invoice_id = @source_account_id

	--DECLARE @debug_acc_invoices_risks_variables_introducers_destination XML = (SELECT * FROM acc_invoices_risks_variables_introducers WHERE invrisvi_invoice_id = @destination_account_id FOR XML AUTO ,ROOT('r'))

	-- copier et calculer les taxes sur les commissions des variables risques
	INSERT INTO acc_invoices_risks_variables_commission_taxes (
			invrisvct_invoice_id,		invrisvct_risk_situation_id,		invrisvct_variable_id,	invrisvct_tax_id,	invrisvct_tax_value)
	SELECT	@destination_account_id,	tsr.destination_id,					tvi.destination_id,		tti.destination_id,	ROUND(invrisvct_tax_value * @prorate_coeff, @nodecimals)
	FROM acc_invoices_risks_variables_commission_taxes airvct
		INNER JOIN @transcodage_situations_risques tsr ON tsr.source_id = airvct.invrisvct_risk_situation_id
		INNER JOIN @transcodage_variables_ids tvi	ON tvi.source_id = airvct.invrisvct_variable_id
		INNER JOIN @transcodage_taxes_ids tti ON tti.source_id = airvct.invrisvct_tax_id
	WHERE airvct.invrisvct_invoice_id = @source_account_id

	--DECLARE @debug_acc_invoices_risks_variables_commission_taxes XML = (SELECT * FROM acc_invoices_risks_variables_commission_taxes WHERE invrisvct_invoice_id = @destination_account_id FOR XML AUTO ,ROOT('r'))

	-- copier et calculer les taxes sur les variables risques
	INSERT INTO acc_invoices_risks_variables_taxes (
			invrisvt_invoice_id,		invrisvt_risk_situation_id,		invrisvt_variable_id,	invrisvt_tax_id,	invrisvt_tax_value)
	SELECT	@destination_account_id,	tsr.destination_id,				tvi.destination_id,		tti.destination_id,	ROUND(invrisvt_tax_value * @prorate_coeff, @nodecimals)
	FROM acc_invoices_risks_variables_taxes airvt
		INNER JOIN @transcodage_situations_risques tsr ON tsr.source_id = airvt.invrisvt_risk_situation_id
		INNER JOIN @transcodage_variables_ids tvi ON tvi.source_id = airvt.invrisvt_variable_id
		INNER JOIN @transcodage_taxes_ids tti ON tti.source_id = airvt.invrisvt_tax_id
	WHERE airvt.invrisvt_invoice_id = @source_account_id

	--DECLARE @debug_acc_invoices_risks_variables_taxes XML = (SELECT * FROM acc_invoices_risks_variables_taxes WHERE invrisvt_invoice_id = @destination_account_id FOR XML AUTO ,ROOT('r'))


	-- si necessaire recalculer le total des taxes et le ttc des variables contrat
	IF ABS(@prorate_coeff) != 1.0 
	BEGIN

		UPDATE airv
			SET invrisv_commission_taxes = ISNULL(sum_taxes.tax_sum,0),
				invrisv_commission_ti = airv.invrisv_commission_tf + ISNULL(sum_taxes.tax_not_included_in_net_sum,0)
		FROM acc_invoices_risks_variables airv
			INNER JOIN (	SELECT	risk_situation_id = airvct.invrisvct_risk_situation_id,
									invoice_id = airvct.invrisvct_invoice_id,
									variable_id = airvct.invrisvct_variable_id,
									tax_sum = SUM(airvct.invrisvct_tax_value),
									tax_not_included_in_net_sum = SUM(	CASE
																			WHEN at.acctax_net_included = 0 THEN airvct.invrisvct_tax_value
																			ELSE 0
																		END)
							FROM acc_invoices_risks_variables_commission_taxes airvct
								INNER JOIN acc_taxes [at] ON	[at].acctax_tax_id = airvct.invrisvct_tax_id
							WHERE airvct.invrisvct_invoice_id = @destination_account_id
							GROUP BY airvct.invrisvct_risk_situation_id, airvct.invrisvct_invoice_id, airvct.invrisvct_variable_id 
						)	AS sum_taxes ON		sum_taxes.risk_situation_id = airv.invrisv_risk_situation_id		
											AND	sum_taxes.invoice_id = airv.invrisv_invoice_id
											AND sum_taxes.variable_id = airv.invrisv_variable_id

		UPDATE airv
			SET invrisv_taxes = ISNULL(sum_taxes.tax_sum,0),
				invrisv_ti = airv.invrisv_tf + ISNULL(sum_taxes.tax_not_included_in_net_sum,0)
		FROM acc_invoices_risks_variables airv
			INNER JOIN (	SELECT	risk_situation_id = airvt.invrisvt_risk_situation_id,
									invoice_id = airvt.invrisvt_invoice_id,
									variable_id = airvt.invrisvt_variable_id,
									tax_sum = SUM(airvt.invrisvt_tax_value),
									tax_not_included_in_net_sum = SUM(	CASE
																			WHEN at.acctax_net_included = 0 THEN airvt.invrisvt_tax_value
																			ELSE 0
																		END)
							FROM acc_invoices_risks_variables_taxes airvt
								INNER JOIN acc_taxes at ON	at.acctax_tax_id = airvt.invrisvt_tax_id
							WHERE airvt.invrisvt_invoice_id = @destination_account_id
							GROUP BY airvt.invrisvt_risk_situation_id, airvt.invrisvt_invoice_id, airvt.invrisvt_variable_id 
						)	AS sum_taxes ON		sum_taxes.risk_situation_id = airv.invrisv_risk_situation_id		
											AND	sum_taxes.invoice_id = airv.invrisv_invoice_id
											AND sum_taxes.variable_id = airv.invrisv_variable_id

		--DECLARE @debug_acc_invoices_risks_variables_afetr_update XML = (SELECT * FROM acc_invoices_risks_variables WHERE invrisv_invoice_id = @destination_account_id FOR XML AUTO ,ROOT('r'))

	END


	/*
		Compte
	*/
	-- si necessaire recalculer le total de la quittance
	IF ABS(@prorate_coeff) != 1.0 
	BEGIN

		UPDATE aa
			SET a_amount =		ISNULL	(
											(	SELECT SUM(airv.invrisv_ti)
												FROM acc_invoices_risks_variables airv
												WHERE airv.invrisv_invoice_id = @destination_account_id
											), 0
										) 
							+
								ISNULL	(
											(	SELECT SUM(aipv.invpolv_ti)
												FROM acc_invoices_policies_variables aipv
												WHERE aipv.invpolv_invoice_id = @destination_account_id
											), 0
										) 
								
		FROM acc_accounts aa
		WHERE aa.a_account_id = @destination_account_id

		--DECLARE @debug_acc_accounts_after_update XML = (SELECT * FROM acc_accounts WHERE a_account_id = @destination_account_id FOR XML AUTO ,ROOT('r'))
		
	END



GO
