CREATE PROCEDURE [dbo].[GPIPostWareOnRoomOrFolio]  @RoomNumber INT, @FolioNumber INT, @WareNumber INT, @Price MONEY, 
										   @WareTextOverwrite VARCHAR(100), @Success BIT OUT,  
										   @HotelNumber INT = 0, @UserIDNumber INT = 2,  
										   @Pieces INT = 1,@RefNo INT = 0, @GuestNo INT = 0
AS
SET NOCOUNT ON

/* 
	Created by : CC September 2018.
	Purpose    : Facilitate minibar posting from primarily Piccoline but maybe in future other GPI modules, hence te name.
*/

DECLARE @Dep		INT;
DECLARE @GTN		INT;
DECLARE @HotCustNo	INT;
DECLARE @CashDesk	INT = -1;
DECLARE @p28		INT = -1;
DECLARE @p38		INT = -1; 


DECLARE	@Note VarChar(50) =  Convert( VarChar(6), @RoomNumber);
--SET     @WareTextOverWrite = 'MiniBar ' + @WareTextOverwrite;
IF ISNULL (@WareTextOverWrite, '') = '' SELECT @WareTextOverwrite = WareName FROM dbo.Ware WHERE WareNumber = @WareNumber;
SELECT @Dep		  = PK_Department FROM dbo.Departments WITH (NOLOCK) WHERE dbo.Departments.Autocharge= 1 AND dbo.Departments.HotelNumber= @HotelNumber;
SELECT @HotCustNo = CustomerNumber FROM dbo.Customer WITH (NOLOCK) WHERE ( (dbo.Customer.CustomerTypeNumber = 7) AND ( dbo.Customer.HotelNumber = @HotelNumber) AND ISNULL( dbo.Customer.MARKASDELETED, 0) = 0 );


BEGIN TRANSACTION
	EXEC   dbo.GetNextNumber 'GLOBALTRANSACTIONIDENTITY', 1, @GTN OUT ;

	BEGIN TRY
-- this is the old line		EXEC dbo.Postering NULL,0,1,@RefNo,@GuestNo,@RoomNumber,@UserIDNumber,0,0,0,0,0,@WareTextOverwrite,@Pieces,@Price,-1,0,0,0,0,@Dep,@WareNumber,0,@GTN,0,0,@Note,@p28 OUT, @HotCustNo    ,NULL,0,0,NULL,0,0,0,0,@p38 output,@CashDesk,NULL,0,0
		IF @FolioNumber > 0 
		BEGIN
		  SET @RoomNumber = 0;
		 -- We cannot reuse a folionumber since it might be open in Cashier and this might result in recordlocks. UPDATE dbo.Folios SET OpenFolio = 1 WHERE FolioNumber = @FolioNumber;
		END
		ELSE 
		BEGIN
		  SET @FolioNumber = 0;
		END
		EXEC dbo.Postering NULL,0,1,@RefNo,@GuestNo,@RoomNumber,@UserIDNumber,@FolioNumber,0,0,0,0,@WareTextOverwrite,@Pieces,@Price,-1,0,0,0,0,@Dep,@WareNumber,0,@GTN,0,0,@Note,@p28 OUT, @HotCustNo    ,NULL,0,0,NULL,0,0,0,0,@p38 output,@CashDesk,NULL,0,0
COMMIT TRANSACTION
		SET @Success = 1;
	END TRY
	BEGIN CATCH
ROLLBACK TRANSACTION
		SET @Success = 0;
	END CATCH



