CREATE PROCEDURE dbo.SubscriptionCreate @AWareNumber INT, @ASubscritionStartDate DATETIME, @AWarePriceCurrentPeriod MONEY OUTPUT, @AWarePriceNextPeriod MONEY OUTPUT, @ANextPeriodStart DATETIME OUTPUT,
  @ABookingFeeAmount MONEY OUTPUT, @ABookingFeeWareNumber INT OUTPUT AS
/*
  Created by Kent Johansen, June 2024
  Calculate price for subscription.
  Returns:
    @AWarePriceCurrentPeriod: Price for current period
    @AWarePriceNextPeriod: Price for next period. If booking is made after cut off date, then there should also be paid for next period. If 0, then no next period
    @ANextPeriodStart: Date for start of Next period

  Changes:
    Bug: Fix for "Price not set when not proportionate".
*/

SET NOCOUNT ON
DECLARE @LShowPrint BIT
SET @LShowPrint = 0

SET @AWarePriceCurrentPeriod = 0
SET @AWarePriceNextPeriod = 0

DECLARE @LProportionatelyPayment BIT
DECLARE @LPeriodStartType INT
DECLARE @LCutOffDays INT
DECLARE @LWarePrice MONEY

DECLARE @LCurDT DATETIME
DECLARE @LDays INT
DECLARE @LDaysInCurrentPeriod INT
DECLARE @LPeriodStart DATETIME
DECLARE @LPeriodEnd DATETIME
DECLARE @LCutOffDate DATE

DECLARE @UseDecimals BIT
DECLARE @NumberOfDecimals INT

SELECT @UseDecimals = CONVERT(BIT, KeyValue) FROM HotelSetup WITH (NOLOCK) WHERE SearchKey = 'UseDecimalsInArrangement'
SET @UseDecimals = ISNULL(@UseDecimals, 0)

SELECT @NumberOfDecimals = CONVERT(INT, KeyValue) FROM HotelSetup WITH (NOLOCK) WHERE SearchKey = 'NumberOfDecimalsInPrices'
SET @NumberOfDecimals = ISNULL(@NumberOfDecimals, 0)
IF @UseDecimals = 0
  SET @NumberOfDecimals = 0

EXEC SystemDateTime '', @LCurDT OUTPUT
SET @LCurDT = CAST(@LCurDT AS DATE)

SET @ASubscritionStartDate = ISNULL(@ASubscritionStartDate, @LCurDT)
SET @ASubscritionStartDate = CAST(@ASubscritionStartDate AS DATE)

SELECT @LProportionatelyPayment = ISNULL(WareTicketType.ProportionatelyPayment, 0), @LPeriodStartType = WareTicketType.PeriodStartType, 
  @LCutOffDays = ISNULL(WareTicketType.CutOffDays, 0), @LWarePrice = Ware.Price
FROM WareTicketType WITH (NOLOCK)
INNER JOIN Ware WITH (NOLOCK) ON Ware.WareNumber = WareTicketType.FK_WareNumber
WHERE WareTicketType.FK_WareNumber = @AWareNumber

DECLARE @DateFirst INT
SELECT @DateFirst = @@DATEFIRST
SET DATEFIRST 1 -- monday

SELECT @LPeriodStart = PeriodStart, @LPeriodEnd = PeriodEnd FROM dbo.WareTicketGetPeriodStartEnd (@LPeriodStartType, @ASubscritionStartDate)

SET DATEFIRST @DateFirst

SET @LPeriodStart = CAST(@LPeriodStart AS DATE)
SET @LPeriodEnd = CAST(@LPeriodEnd AS DATE)

IF @LShowPrint = 1
BEGIN
  PRINT '@LProportionatelyPayment'
  PRINT @LProportionatelyPayment
  PRINT '@LPeriodStartType'
  PRINT @LPeriodStartType
  PRINT '@LCutOffDays'
  PRINT @LCutOffDays
  PRINT '@LWarePrice'
  PRINT @LWarePrice

  PRINT '@LCurDT'
  PRINT @LCurDT
  PRINT '@LPeriodStart'
  PRINT @LPeriodStart
  PRINT '@LPeriodEnd'
  PRINT @LPeriodEnd
END

SET @LCutOffDate = CAST(DATEADD(DAY, (@LCutOffDays*-1), @LPeriodEnd) AS DATE)
IF @LShowPrint = 1
BEGIN
  PRINT '@LCutOffDate'
  PRINT @LCutOffDate
END

IF @LProportionatelyPayment = 1
BEGIN
  -- Calc Ware price for rest of period
  SET @LDaysInCurrentPeriod = DATEDIFF(DAY, @LPeriodStart, @LPeriodEnd) + 1
  SET @LDays = DATEDIFF(DAY, @ASubscritionStartDate, @LPeriodEnd)

  IF @LShowPrint = 1
  BEGIN
    PRINT '@LDaysInCurrentPeriod'
    PRINT @LDaysInCurrentPeriod
    PRINT '@LWarePrice'
    PRINT @LWarePrice
    PRINT '@LDays'
    PRINT @LDays
  END

  IF @LDays > 0
    SET @AWarePriceCurrentPeriod = @LWarePrice / @LDaysInCurrentPeriod * @LDays
END
ELSE
  SET @AWarePriceCurrentPeriod = @LWarePrice

IF @LCutOffDays > 0
BEGIN
  IF @ASubscritionStartDate > @LCutOffDate
  BEGIN 
    SET @AWarePriceNextPeriod = @LWarePrice
    SET @ANextPeriodStart = DATEADD(DAY, 1, @LPeriodEnd)
  END
END

SELECT @ABookingFeeAmount = ISNULL(Ware.Price, 0), @ABookingFeeWareNumber = ISNULL(WareAddOn.FK_WareNumberRelated, -1)
FROM WareAddon WITH (NOLOCK)
INNER JOIN Ware WITH (NOLOCK) ON Ware.WareNumber = WareAddon.FK_WareNumberRelated
WHERE WareAddon.FK_WareNumber = @AWareNumber
AND WareAddon.AddOnType = 2

SET @AWarePriceCurrentPeriod = ROUND(@AWarePriceCurrentPeriod, @NumberOfDecimals)
SET @AWarePriceNextPeriod = ROUND(@AWarePriceNextPeriod, @NumberOfDecimals)
SET @ABookingFeeAmount = ROUND(@ABookingFeeAmount, @NumberOfDecimals)