CREATE PROCEDURE dbo.PMIPmsUpdateData @ADataType INT, @ASubType INT, @ADateStamp CHAR(8), @ADataTypeValue INT, @ADataTypeName VARCHAR(100), @APieces INT, 
  @AAdults INT, @AChildren INT, @ARevenue MONEY, @AType TINYINT AS
SET NOCOUNT ON
/*
  Created by Kent Johansen January 2013
  Update the temporary table 
  DataType: 1=Market/Segment, 2=Room type, 3=Rate code/Product, 4=BookingChannel/xx, 5=Booking Source/yy
  SubType: 
    PmsHistory: 1=Total, 2=Noshow, 3=Departure, 4=Arrival
    PmsOTB: 1=TotalDifinite, 2=DepartureDifinite, 3=ArrivalDifinite, 4=TotalTentative, 5=DepartureTentative, 6=ArrivalTentative
  Changes:
    KJ 20120226: Ext: Added @AType for support of PmsOTB
  
*/

SET @APieces = ISNULL(@APieces, 0)
SET @AAdults = ISNULL(@AAdults, 0)
SET @AChildren = ISNULL(@AChildren, 0)
SET @ARevenue = ISNULL(@ARevenue, 0)

UPDATE #tmp SET Pieces = Pieces + @APieces, Adults = Adults + @AAdults, Children = Children + @AChildren, Revenue = Revenue + @ARevenue
WHERE DataType = @ADataType
AND SubType = @ASubType
AND Date = @ADateStamp
AND DataTypeValue = @ADataTypeValue
  
IF @@ROWCOUNT = 0
BEGIN
  DECLARE @DataTypeName VARCHAR(20)
  DECLARE @SubTypeName VARCHAR(50)
  
  SET @DataTypeName = 
  CASE @ADataType
    WHEN 1 THEN 'Market/Segment'
    WHEN 2 THEN 'Room type'
    WHEN 3 THEN 'Rate code/Product'
    WHEN 4 THEN 'BookingChannel/xx'
    WHEN 5 THEN 'Booking Source/yy'
    ELSE ''
  END
  
  IF @AType = 1
  BEGIN
    SET @SubTypeName = 
    CASE @ASubType
      WHEN 1 THEN 'Total'
      WHEN 2 THEN 'Noshow'
      WHEN 3 THEN 'Departure'
      WHEN 4 THEN 'Arrival'
      ELSE ''
    END
  END
  ELSE
  BEGIN
    SET @SubTypeName = 
    CASE @ASubType
      WHEN 1 THEN 'TotalDifinite'
      WHEN 2 THEN 'DepartureDifinite'
      WHEN 3 THEN 'ArrivalDifinite'
      WHEN 4 THEN 'TotalTentative'
      WHEN 5 THEN 'DepartureTentative'
      WHEN 6 THEN 'ArrivalTentative'
      ELSE ''
    END
  END

  INSERT INTO #tmp VALUES (@ADataType, @DataTypeName, @ASubType, @SubTypeName, @ADateStamp, @ADataTypeValue, @ADataTypeName, @APieces, @AAdults, @AChildren, @ARevenue)
END