CREATE PROCEDURE dbo.RevParGuruProduction @AStartDate DATETIME, @AEndDate DATETIME, @AHotelNumberCSV VARCHAR(100) AS
SET NOCOUNT ON

/*
  Created by Kent Johansen June 2012
  Changes;
  
*/

DECLARE @LInclProvision BIT

SELECT @LInclProvision = CONVERT(BIT, KeyValue) FROM HotelSetUp WITH (NOLOCK) WHERE SearchKey='IncludeProvisionInMIS'
SET @LInclProvision = ISNULL(@LInclProvision, 0)

SELECT MISTableView.Datestamp, MISTableView.SegmentNumber, Segments.Text,
SUM(ISNULL(MISTableView.ActualPriceExPackageExVat, 0)) + SUM(ISNULL(MISTableView.BookedPriceExPackageExVat, 0)) AS Amount,
SUM(ISNULL(MISTableView.ActualNumberOfRooms, 0)) + SUM(ISNULL(MISTableView.BookedNumberOfRooms, 0)) AS NumberOfRooms
FROM MISTableView WITH (NOLOCK)
INNER JOIN Customer WITH (NOLOCK) ON Customer.CustomerNumber = MISTableView.HotelCustomerNumber
  AND (ISNULL(@AHotelNumberCSV, '-1') = '-1' OR Customer.HotelNumber IN (SELECT DISTINCT Number FROM iter_intlist_to_table(@AHotelNumberCSV)))
INNER JOIN Segments WITH (NOLOCK) ON Segments.Number = MISTableView.SegmentNumber
WHERE MISTableView.Datestamp >= CONVERT(CHAR(8), @AStartDate, 112)
AND MISTableView.DateStamp <= CONVERT(CHAR(8), @AEndDate, 112)
AND ValueType = 1 --AND MISTableView.MISData IN (1)
AND (@LInclProvision = 1 OR ISNULL(MISTableView.WareType, 0) NOT IN (10,11,12,13,14,15,16,17,18))
GROUP BY MISTableView.Datestamp, MISTableView.SegmentNumber, Segments.Text