
CREATE PROCEDURE [dbo].[OnlineAddWare] @Date CHAR(8), @Time CHAR(5), @Amount INT,  @WareText VARCHAR(50), @Price MONEY,  @Totalprice MONEY, @WareNumber INT , @Refnumber INT, @lastRecordID INT OUTPUT, @HallNumberID INT = NULL, @GuestNumberNamelist INT = 0 AS
SET NOCOUNT ON
/*
    Created by Troels Buch Juni 2004
    
    Changes:
    TB20120121: Removed TotalPrice.
	TB20101122: Added @GuestNumberNamelist	
    AV 20090225 length of date and time variables decreased to right size and BEGIN TRAN COMMIT TRAN removed
    AV 20080116 bug fix for net setting correct hotelnumber
    AV 2007 0410 use the new deparment XTab
    AV 20070202: Correct load of exdeparment
    AV 20070129 : Insert the Note from the ware into the arrangementline
    AV 20060907 New Parameter : HallNumberID: used for Online Hall Booking
    KJ 20060505: AutoCharge department read from departments 
    TB 20050211: Return  @lastRecordID as output parameter
    TB 20050126: Add Departments to arrangementlines
    TB 20050126: Add Time to arrangementlines

    NOTES: 	- 

*/
--DECLARE @lastRecordID INT
--Declare @ExDepartment CHAR(1)
DECLARE @ExDepartment INT
--DECLARE @DepartmentService  VARCHAR(10)
DECLARE @PK_Department INT
DECLARE @Note VARCHAR(4000)


DECLARE @DominoEnabled BIT
DECLARE @ErrorLevel INT
SET @ErrorLevel = 00

/*
  Error Levels:
  00 : No errors
*/

SELECT @DominoEnabled=CONVERT(SMALLINT,KeyValue) FROM HotelSetup(NOLOCK) WHERE SearchKey='CentralBookingType'
  IF ISNULL(@DominoEnabled, 0) = 0
    SET @DominoEnabled = 0


--Select @ExDepartment = keyvalue from hotelsetup (nolock) where searchkey = 'DefaultExDepartment'
SELECT @Note = Note FROM Ware [NOLOCK] WHERE Warenumber = @WareNumber


DECLARE @HotelNumber INT

IF @DominoEnabled = 1
  SELECT @HotelNumber = HotelNumber FROM ReservationMain (NOLOCK) WHERE ReferenceNumber = @Refnumber
ELSE
  SET @HotelNumber = -1

SELECT TOP 1 @ExDepartment = FK_Department FROM WareDepartmentsXTab (NOLOCK) 
INNER JOIN Departments (NOLOCK) ON Departments.PK_Department = WareDepartmentsXTab.FK_Department AND WareDepartmentsXTab.Type = 4
AND ((@HotelNumber > -1 AND Departments.HotelNumber = @HotelNumber) OR @HotelNumber = -1)
WHERE WareNumber = @WareNumber


IF ISNULL(@ExDepartment, 0) = 0
BEGIN
  IF @DominoEnabled = 1
    SELECT TOP 1 @ExDepartment = PK_Department FROM Departments (NOLOCK) WHERE AutoCharge = 1 AND HotelNumber = @HotelNumber
  ELSE
    SELECT TOP 1 @ExDepartment = PK_Department FROM Departments (NOLOCK) WHERE AutoCharge = 1
END

INSERT INTO ArrangementLines ([Date], [Time], Pieces, WareText, Price, /*TotalPrice,*/ WareNumber, FK_ExDepartment, HallNumber, Note) VALUES 
  (@Date, @Time, @Amount, @WareText, @Price, /*@Totalprice,*/ @WareNumber, @ExDepartment, @HallNumberID, @Note)
SELECT @lastRecordID = @@Identity FROM ArrangementLines (NOLOCK)

INSERT INTO ArrangementReservationsXTab(ArrangementLineNumber, ReferenceNumber, GuestNumber, PlusProductLineNumber) VALUES (@lastRecordID, @Refnumber, @GuestNumberNamelist, 0)

DECLARE DepCur Cursor Forward_Only For
SELECT DISTINCT FK_Department FROM WareDepartmentsXTab  (NOLOCK)
LEFT OUTER JOIN Departments (NOLOCK) ON WareDepartmentsXTab.FK_Department = Departments.PK_Department
WHERE WareNumber = @WareNumber AND Type = 2 AND (@HotelNumber = -1 OR Departments.HotelNumber = @HotelNumber)
OPEN DepCur
FETCH NEXT FROM DepCur INTO @PK_Department
WHILE  (@@FETCH_STATUS <> -1)
BEGIN
 IF NOT EXISTS (SELECT FK_ArrangementLineNumber FROM ArrangementLineDepartmentXTab(NOLOCK) WHERE FK_ArrangementLineNumber = @lastRecordID AND FK_DepartmentNumber = @PK_Department)
   INSERT INTO ArrangementLineDepartmentXTab (FK_ArrangementLineNumber, FK_DepartmentNumber) VALUES (@lastRecordID, @PK_Department)
 FETCH NEXT FROM DepCur INTO @PK_Department
END
CLOSE DepCur
DEALLOCATE DepCur

IF @ErrorLevel > 00
BEGIN
  SET @Refnumber = 0
  --SET @Price = 0
  RETURN
END
GO

