CREATE PROCEDURE dbo.RecurringPatternGetWareUpdate @AStartDate DATETIME, @AEndDate DATETIME AS
/*
  Created by Kent Johansen, May 2017
  Calc the data for RecurringPatternGetWare sp. Casche the data to optimize the online booking

  Changes:
    KJ 20241707: OPT: Use RecurringPatternAvailableWares
    KJ 20221216: Ext: Multiple rules per WareLinkGroup.
    Bug: Fix for "Column name or number of supplied values does not match table definition"
*/
SET NOCOUNT ON

DECLARE @WareNumber INT
DECLARE @AWareNumberTmp INT
SELECT @AWareNumberTmp = OnlineBookTypeNumber FROM OnlineBookType WITH (NOLOCK) WHERE Number = 29 -- 29 er en fast type (warewithrules)

CREATE TABLE #tmp
(
  WareNumber INT, 
  WareName VARCHAR(50),
  Price MONEY, 
  DefaultArrangementTime DATETIME, 
  MaxPieces INT, 
  DeliveryType TINYINT, 
  DeliveryChargeType TINYINT, 
  DeliveryChargeTypeText VARCHAR(200), 
  Duration INT, 
  Note VARCHAR(8000), 
  Sortnumber INT,
  tmp TINYINT,
  IsPlaceTextNeeded BIT,
  PlaceTextDescription VARCHAR(MAX)
)

INSERT INTO #tmp
  EXEC OnlineFindWares @AHotelNumber = -1, @AWareNumber = @AWareNumberTmp, @ALangSameAsPicasso = 1, @ACountryNumber = 1

DECLARE @WarePeriods [RecurringPatternAvailableWaresTable]

INSERT INTO @WarePeriods
SELECT #tmp.WareNumber, CAST(@AStartDate AS DATE), DATEADD(DAY, 1, CAST(@AEndDate AS DATE)), -1
FROM #tmp
DROP TABLE #tmp

CREATE TABLE #tmp2
(
  WareNumber INT,
  StartDateTime DATETIME,
  EndDateTime DATETIME,
  Available INT,
  WareLinkGroupID INT
)

INSERT INTO #tmp2
  EXEC [dbo].[RecurringPatternAvailableWares] @WarePeriods=@WarePeriods, @ReturnSpecified=1

DELETE RecurringPatternWareCache

INSERT INTO RecurringPatternWareCache
SELECT WareNumber, StartDateTime,  DATEADD(MILLISECOND, -3, EndDateTime), Available
FROM #tmp2

DROP TABLE #tmp2