CREATE PROCEDURE [dbo].[GetProductInfo] @FkAccountTaxGroup INT OUTPUT, @TaxPercent FLOAT OUTPUT, @TaxFrom INT, @DayProduct INT 
, @ProductNumber INT, @SegmentNumber INT, @PointofSaleNumber INT, @BookedTypeNumber INT AS
SET NOCOUNT ON
/*
  nb20190704: Created
  NOTE: Find the product tax and AccountTaxGroup key
  STATUS: Ver 1
*/
DECLARE @DebugLevel TINYINT, @Msg VARCHAR(2000)
SET @DebugLevel = 0
IF @DebugLevel >= 1 
  PRINT '--- [GetProductInfo] begin'
IF @DebugLevel = 2 
BEGIN
  SET @Msg = 'DECLARE @FkAccountTaxGroup INT, @TaxPercent REAL, @TaxFrom INT, @KindInputId INT, @DayProduct INT
  , @ProductNumber INT, @SegmentNumber INT, @PointofSaleNumber INT, @BookedTypeNumber INT '+CHAR(13)
  IF ISNULL( @FkAccountTaxGroup, -1) = -1 
    SET @Msg = @Msg+'SET @FkAccountTaxGroup = NULL'+CHAR(13)
  ELSE
    SET @Msg = @Msg+'SET @FkAccountTaxGroup = '+CONVERT(VARCHAR, @FkAccountTaxGroup)+CHAR(13)
  IF ISNULL( @TaxPercent, -1) = -1 
    SET @Msg = @Msg+'SET @TaxPercent = NULL'+CHAR(13)
  ELSE
    SET @Msg = @Msg + 'SET @TaxPercent = '+CONVERT(VARCHAR,@TaxPercent)+CHAR(13)
  IF ISNULL( @TaxFrom, -1) = -1 
    SET @Msg = @Msg+'SET @TaxFrom = NULL'+CHAR(13)
  ELSE
    SET @Msg = @Msg + 'SET @TaxFrom = '+CONVERT(VARCHAR,@TaxFrom)+CHAR(13)
  IF ISNULL( @DayProduct, -1) = -1 
    SET @Msg = @Msg+'SET @DayProduct = NULL'+CHAR(13)
  ELSE
    SET @Msg = @Msg + 'SET @DayProduct = '+CONVERT(VARCHAR,@DayProduct)+CHAR(13)
  IF ISNULL( @ProductNumber, -1) = -1 
    SET @Msg = @Msg+'SET @ProductNumber = NULL'+CHAR(13)
  ELSE
    SET @Msg = @Msg + 'SET @ProductNumber = '+CONVERT(VARCHAR, @ProductNumber)+CHAR(13)
  IF ISNULL( @SegmentNumber, -1) = -1 
    SET @Msg = @Msg+'SET @SegmentNumber = NULL'+CHAR(13)
  ELSE
    SET @Msg = @Msg + 'SET @SegmentNumber = '+CONVERT(VARCHAR,@SegmentNumber)+CHAR(13)
  IF ISNULL( @PointofSaleNumber, -1) = -1 
    SET @Msg = @Msg+'SET @PointofSaleNumber = NULL'+CHAR(13)
  ELSE
    SET @Msg = @Msg + 'SET @PointofSaleNumber = '+CONVERT(VARCHAR,@PointofSaleNumber)+CHAR(13)
  IF ISNULL( @BookedTypeNumber, -1) = -1 
    SET @Msg = @Msg+'SET @BookedTypeNumber = NULL'+CHAR(13)
  ELSE
    SET @Msg = @Msg+'SET @BookedTypeNumber = '+CONVERT(VARCHAR, @BookedTypeNumber)+CHAR(13)

  SET @Msg = @Msg+'EXEC dbo.GetProductInfo @FkAccountTaxGroup OUTPUT, @TaxPercent OUTPUT, @TaxFrom, @DayProduct, @ProductNumber, @SegmentNumber, @PointofSaleNumber, @BookedTypeNumber'+ CHAR(13)
  SET @Msg = @Msg + 'PRINT ''@FkAccountTaxGroup = ''+ISNULL(CONVERT(varchar, @FkAccountTaxGroup), '''')' + CHAR(13)
  SET @Msg = @Msg + 'PRINT ''@TaxPercent = ''+ISNULL(CONVERT(varchar, @TaxPercent), '''')' + CHAR(13)
  PRINT '--- Call SP GetProductInfo begin ---'
  PRINT @Msg
  PRINT '--- Call SP GetProductInfo end ---'
END

DECLARE @KindInputId INT
, @AccountResult INT
, @WareNumberDummy INT
, @AccountBalanceDummy INT
, @AccountTaxDummy INT
, @WareTextDummy VARCHAR
, @IsDayProduct BIT
IF ((@TaxFrom>=0) AND (@TaxFrom<3))
BEGIN
  IF (@TaxFrom=0) -- Get Vat From Segment
    SET @KindInputId = @SegmentNumber
  ELSE IF (@TaxFrom=1) -- Get Vat From POS
    SET @KindInputId = @PointOfSaleNumber
  ELSE IF (@TaxFrom=2) -- Get Vat From Roomtype -- TODO ConferenceRoom if DayProduct
    SET @KindInputId = @BookedTypeNumber
END
ELSE --Get Vat From Producttype
  SET @KindInputId = @ProductNumber
IF @DayProduct = 1
  SET @IsDayProduct = 1
ELSE 
  SET @IsDayProduct = 0
EXEC GetVatValueAndAccountnumbers @IsDayProduct, @TaxFrom, @KindInputId, @WareNumberDummy, @AccountBalanceDummy, @AccountResult OUTPUT, @AccountTaxDummy, @TaxPercent OUTPUT, @WareTextDummy
IF NOT @FkAccountTaxGroup IS NULL 
BEGIN
  SELECT @FkAccountTaxGroup = FkAccountVatGroup FROM Accounts (NOLOCK) WHERE AccountNumber = @AccountResult
  IF ISNULL(@FkAccountTaxGroup, 0) = 0
    SELECT TOP 1 @FkAccountTaxGroup = PkAccountVatGroup FROM AccountVatGroup (NOLOCK) WHERE Profile = 0 ORDER BY OrderNumber
END
IF @DebugLevel >= 1 
  PRINT '--- [GetProductInfo] end'
