SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
-- ================================================================================================================================================================================
-- Author:		Laba Catalin
-- Create date: 07/05/2014
-- Description:	Returns the taxes at a specific date. To obtain all fields you must join with table acc_taxes and/or nm_taxes and/or pr_product_variables_taxes
-- ================================================================================================================================================================================
CREATE function [dbo].[f_acc_taxes_by_date]
(
	@date_setup date
) 
returns @all_taxes table 
						(
							tax_id int,
							tax_code ais_code_short
						)
as 
begin

	insert into @all_taxes( tax_id,				tax_code)
	select					at.acctax_tax_id,	at.acctax_tax_code
	from acc_taxes at
		inner join (
					select	tax_code = acctax_tax_code, 
							date_setup = max(acctax_tax_date_setup) 
					from acc_taxes
					WHERE acctax_tax_date_setup <= @date_setup
					group by acctax_tax_code) as t on		at.acctax_tax_code = t.tax_code
														and at.acctax_tax_date_setup = t.date_setup

	return;
	
END
GO
