CREATE PROCEDURE dbo.PostMenuWare @MenuWareNumber INT, @FK_DepartmentNumber INT, @GlobalTransactionNumber INT, @RealDateTimeForTransaction DATETIME, @PostForDatetime DATETIME, 
  @UserIDNumber INT, @FolioNumber INT, @Pieces INT, @MyCountryNumber INT, @Payment SMALLINT, @Note VARCHAR(50), @ProductWareLinkNumber INT, @HotelCustomerNumber INT, 
  @ChargedWithCard BIT, @MovedFolioNumber INT, @TakeAccountsFromWareNumber INT, @Price MONEY, @ACashDesk INT = -1, @WareTextOverwrite VARCHAR(100), @AArrangementLineNumber INT AS
SET NOCOUNT ON

/* 
Created by: MRP April 2008.

Changes:  
  KJ 20160128: Bug: Fix for "Amount is negative if menuware charged for Extras and Pieces is negative".
  KJ 20150626: Bug: Fix for "Ware is not posted as menuware when selling more than one pieces". Case #CR089655
  KJ 20150326: Bug: Added arranagementlinenumber. Handle for multiple precharge.
  KJ 20150105: Bug: Fix for "Error when post of more than one pieces of a menu ware not in percent". Case #CR081859
  KJ 20140624: Ext: Menu ware split in whole amount. Req #1561
  KJ 20140611: Ext: CashDesk per hotel. Case #CR073269
  KJ 20120116: Bug: Fix for "Wrong text when overwrite ware test on menuware". Case #CR038721 
  KJ: 20091208: Ext: Added CashDesk
  KJ: 20090303: Don't post if Percentage does not add up to 100
  KJ: 20090205: Bug: Fix for "Post negativ pieces wrong"
  KJ: 20081222: Ext: Added OriginalGlobalTransactionNumber
  KJ: 20081001: Bug: Fix for "Cutoff problem when using ROUND"
  KJ: 20080526: Bug: Fix for "Handle for Menuware and AllowZero prices."
  KJ: 20080523: Bug: Fix for "Handle for Menuware and AllowZero prices."
  KJ: 20080522: @TakeAccountsFromWareNumber Added
  KJ: 20080416: Only one GlobalTransactionNumber
  KJ: 20080415: Cleanup
  KJ: 20080415: Fix for "Only show (on folio) the menuware and not all the wares in the menu."
*/

DECLARE @Print BIT
SET @Print = 0

DECLARE @IsMenuWare BIT
DECLARE @InPercent BIT
/*
DECLARE @SumPercentage INT

SELECT @SumPercentage = SUM(mw.procentage)
FROM dbo.ware w WITH (NOLOCK) 
INNER JOIN dbo.MenuWareXTab mw WITH (NOLOCK) ON w.warenumber = mw.warenumber 
  AND mw.menunumber = @MenuWareNumber

IF @SumPercentage <> 100
  RAISERROR('Error in storedprocedure <<PostMenuWare>>: Percentage does not equals 100.',15,1)
*/
DECLARE @warename VARCHAR(50)
DECLARE @procentage MONEY
DECLARE @WareText Varchar(100)
DECLARE @MenuWarePrice MONEY
DECLARE @MenuWareName varchar(50)
DECLARE @JoinedMenuWarePrice MONEY
DECLARE @NumbersOfWares INT
DECLARE @NewPrice MONEY
DECLARE @LeftOver MONEY
DECLARE @underrun MONEY
DECLARE @ResultAccountNumber INT
DECLARE @VATAccountNumber INT
DECLARE @BalanceAccountNumber INT
DECLARE @PaymentBalanceAccountNumber INT
DECLARE @VatValueWare MONEY
DECLARE @VATValueNumber INT
DECLARE @WareSortNumber INT
DECLARE @ExchangeRate FLOAT
DECLARE @PostVAT INT
DECLARE @VatOfWarePrice MONEY
DECLARE @WarePriceExVAT MONEY
DECLARE @SingleWareNumber INT
DECLARE @CountryCode VARCHAR(4)

SELECT @MyCountryNumber = CountryNumber From Countries WITH (NOLOCK) where (CountryNumber=@MyCountryNumber) AND MarkAsDeleted=0

DECLARE @UseCashDesk BIT
DECLARE @LastTransactionNumber INT

--SELECT @UseCashDesk = CONVERT(BIT, KeyValue) FROM HotelSetup WITH (NOLOCK) WHERE Searchkey = 'FiscalPrinterSweden'
SELECT @UseCashDesk = dbo.GetHotelSetupKeyValue ('FiscalPrinterSweden', @HotelCustomerNumber)
SET @UseCashDesk = ISNULL(@UseCashDesk,0)

if @@RowCount = 0
begin
  select @CountryCode = TextValue from hotelsetup WITH (nolock) where searchkey like 'CountryCode'
  SELECT @MyCountryNumber = CountryNumber From Countries WITH (NOLOCK) where (CountryCode=@CountryCode) AND MarkAsDeleted=0

  IF @@ROWCOUNT = 0 
    SELECT TOP 1 @MyCountryNumber = CountryNumber FROM Countries WITH (NOLOCK)
end 

SET @Price = Round(ABS(@Price* @Pieces),2)

IF @TakeAccountsFromWareNumber > 0
BEGIN
  SELECT @MenuWarePrice = Price, @MenuWareName = Warename, @ResultAccountNumber = ResultAccountNumber, 
    @BalanceAccountNumber = BalanceAccountNumber
  FROM dbo.Ware 
  WHERE warenumber = @MenuWareNumber
END
ELSE
BEGIN
  SET @MenuWarePrice = @Price
  SELECT @MenuWareName = Warename, @ResultAccountNumber = ResultAccountNumber, 
    @BalanceAccountNumber = BalanceAccountNumber
  FROM dbo.Ware 
  WHERE warenumber = @MenuWareNumber
END

EXEC WareIsMenuWare @MenuWareNumber, @MenuWarePrice, @Pieces, @IsMenuWare OUTPUT

IF @IsMenuWare <> 1
  RAISERROR('Error in storedprocedure <<PostMenuWare>>: Percentage does not equals 100.',15,1)

IF @WareTextOverwrite <> ''
  SET @MenuWareName = @WareTextOverwrite

SELECT @VATValueNumber = VatValueNumber, @VATAccountNumber=VATAccountNumber 
FROM Accounts WITH (NOLOCK) 
WHERE Accounts.AccountNumber = @ResultAccountNumber

EXEC GetVatvalueFromVATValueNumber @VATValueNumber, @VatValueWare OUTPUT 

SET @ExchangeRate = 1

IF @Pieces > 0
BEGIN
  INSERT INTO Transactions (ProductNumber, PackageNumber, GlobalTransactionNumber, OriginalGlobalTransactionNumber, RealDateTimeForTransaction, DateStamp, UserIDNumber, FolioNumber, Pieces, PriceOverwrite, CurrencyNumber, ShowOnBillFlag, Debit, MovedDeleted, WareNumber, TextOverwrite, Payment, AccountNumber, VATValue, FK_DepartmentNumber, Note, OriginalDate, ExchangeRateOverwrite, ProductWareLinkNumber, HotelCustomerNumber, ChargedWithCard, FK_ArrangementLineNumber)
  VALUES(0, 0, @GlobalTransactionNumber, @GlobalTransactionNumber, @RealDateTimeForTransaction, @PostForDatetime, @UserIDNumber, @FolioNumber, @Pieces, 0, @MyCountryNumber, 0, 0, 0, @MenuWareNumber, @MenuWareName, @Payment, @ResultAccountNumber, @VatValueWare, @FK_DepartmentNumber, @Note, @PostForDatetime, @ExchangeRate, @ProductWareLinkNumber, @HotelCustomerNumber, @ChargedWithCard, @AArrangementLineNumber)
  IF @UseCashDesk = 1
  BEGIN
    SELECT @LastTransactionNumber = @@Identity
    EXEC TransactionsRelationInsert @LastTransactionNumber, @ACashDesk, @UserIDNumber
  END

  INSERT INTO Transactions (ProductNumber, PackageNumber, GlobalTransactionNumber, OriginalGlobalTransactionNumber, RealDateTimeForTransaction, DateStamp, UserIDNumber, FolioNumber, Pieces, PriceOverwrite, CurrencyNumber, ShowOnBillFlag, Debit, MovedDeleted, WareNumber, TextOverwrite, Payment, AccountNumber, VATValue, FK_DepartmentNumber, Note, OriginalDate, ExchangeRateOverwrite, ProductWareLinkNumber, HotelCustomerNumber, ChargedWithCard, FK_ArrangementLineNumber)
  VALUES(0, 0, @GlobalTransactionNumber, @GlobalTransactionNumber, @RealDateTimeForTransaction, @PostForDatetime, @UserIDNumber, @FolioNumber, @Pieces, @MenuWarePrice, @MyCountryNumber, 1, 1, 0, @MenuWareNumber, @MenuWareName, @Payment, @BalanceAccountNumber, @VatValueWare, @FK_DepartmentNumber, @Note, @PostForDatetime, @ExchangeRate, @ProductWareLinkNumber, @HotelCustomerNumber, @ChargedWithCard, @AArrangementLineNumber)
  IF @UseCashDesk = 1
  BEGIN
    SELECT @LastTransactionNumber = @@Identity
    EXEC TransactionsRelationInsert @LastTransactionNumber, @ACashDesk, @UserIDNumber
  END

  INSERT INTO Transactions (ProductNumber, PackageNumber, GlobalTransactionNumber, OriginalGlobalTransactionNumber, RealDateTimeForTransaction, DateStamp, UserIDNumber, FolioNumber, Pieces, PriceOverwrite, CurrencyNumber, ShowOnBillFlag, Debit, MovedDeleted, WareNumber, TextOverwrite, Payment, AccountNumber, VATValue, FK_DepartmentNumber, Note, OriginalDate, ExchangeRateOverwrite, ProductWareLinkNumber, HotelCustomerNumber, ChargedWithCard, FK_ArrangementLineNumber)
  VALUES(0, 0, @GlobalTransactionNumber, @GlobalTransactionNumber, @RealDateTimeForTransaction, @PostForDatetime, @UserIDNumber, @FolioNumber, @Pieces, 0, @MyCountryNumber, 0, 0, 0, @MenuWareNumber, @MenuWareName, @Payment, @VATAccountNumber, @VatValueWare, @FK_DepartmentNumber, @Note, @PostForDatetime, @ExchangeRate, @ProductWareLinkNumber, @HotelCustomerNumber, @ChargedWithCard, @AArrangementLineNumber)
  IF @UseCashDesk = 1
  BEGIN
    SELECT @LastTransactionNumber = @@Identity
    EXEC TransactionsRelationInsert @LastTransactionNumber, @ACashDesk, @UserIDNumber
  END
END
ELSE
BEGIN
  INSERT INTO Transactions (ProductNumber, PackageNumber, GlobalTransactionNumber, OriginalGlobalTransactionNumber, RealDateTimeForTransaction, DateStamp, UserIDNumber, FolioNumber, Pieces, PriceOverwrite, CurrencyNumber, ShowOnBillFlag, Debit, MovedDeleted, WareNumber, TextOverwrite, Payment, AccountNumber, VATValue, FK_DepartmentNumber, Note, OriginalDate, ExchangeRateOverwrite, ProductWareLinkNumber, HotelCustomerNumber, ChargedWithCard, FK_ArrangementLineNumber)
  VALUES(0, 0, @GlobalTransactionNumber, @GlobalTransactionNumber, @RealDateTimeForTransaction, @PostForDatetime, @UserIDNumber, @FolioNumber, @Pieces, 0, @MyCountryNumber, 0, 1, 0, @MenuWareNumber, @MenuWareName, @Payment, @ResultAccountNumber, @VatValueWare, @FK_DepartmentNumber, @Note, @PostForDatetime, @ExchangeRate, @ProductWareLinkNumber, @HotelCustomerNumber, @ChargedWithCard, @AArrangementLineNumber)
  IF @UseCashDesk = 1
  BEGIN
    SELECT @LastTransactionNumber = @@Identity
    EXEC TransactionsRelationInsert @LastTransactionNumber, @ACashDesk, @UserIDNumber
  END

  INSERT INTO Transactions (ProductNumber, PackageNumber, GlobalTransactionNumber, OriginalGlobalTransactionNumber, RealDateTimeForTransaction, DateStamp, UserIDNumber, FolioNumber, Pieces, PriceOverwrite, CurrencyNumber, ShowOnBillFlag, Debit, MovedDeleted, WareNumber, TextOverwrite, Payment, AccountNumber, VATValue, FK_DepartmentNumber, Note, OriginalDate, ExchangeRateOverwrite, ProductWareLinkNumber, HotelCustomerNumber, ChargedWithCard, FK_ArrangementLineNumber)
  VALUES(0, 0, @GlobalTransactionNumber, @GlobalTransactionNumber, @RealDateTimeForTransaction, @PostForDatetime, @UserIDNumber, @FolioNumber, @Pieces, @MenuWarePrice, @MyCountryNumber, 1, 0, 0, @MenuWareNumber, @MenuWareName, @Payment, @BalanceAccountNumber, @VatValueWare, @FK_DepartmentNumber, @Note, @PostForDatetime, @ExchangeRate, @ProductWareLinkNumber, @HotelCustomerNumber, @ChargedWithCard, @AArrangementLineNumber)
  IF @UseCashDesk = 1
  BEGIN
    SELECT @LastTransactionNumber = @@Identity
    EXEC TransactionsRelationInsert @LastTransactionNumber, @ACashDesk, @UserIDNumber
  END

  INSERT INTO Transactions (ProductNumber, PackageNumber, GlobalTransactionNumber, OriginalGlobalTransactionNumber, RealDateTimeForTransaction, DateStamp, UserIDNumber, FolioNumber, Pieces, PriceOverwrite, CurrencyNumber, ShowOnBillFlag, Debit, MovedDeleted, WareNumber, TextOverwrite, Payment, AccountNumber, VATValue, FK_DepartmentNumber, Note, OriginalDate, ExchangeRateOverwrite, ProductWareLinkNumber, HotelCustomerNumber, ChargedWithCard, FK_ArrangementLineNumber)
  VALUES(0, 0, @GlobalTransactionNumber, @GlobalTransactionNumber, @RealDateTimeForTransaction, @PostForDatetime, @UserIDNumber, @FolioNumber, @Pieces, 0, @MyCountryNumber, 0, 1, 0, @MenuWareNumber, @MenuWareName, @Payment, @VATAccountNumber, @VatValueWare, @FK_DepartmentNumber, @Note, @PostForDatetime, @ExchangeRate, @ProductWareLinkNumber, @HotelCustomerNumber, @ChargedWithCard, @AArrangementLineNumber)
  IF @UseCashDesk = 1
  BEGIN
    SELECT @LastTransactionNumber = @@Identity
    EXEC TransactionsRelationInsert @LastTransactionNumber, @ACashDesk, @UserIDNumber
  END
END

DECLARE #WareCur CURSOR static FOR
  SELECT w.warename, mw.procentage, w.ResultAccountNumber, w.BalanceAccountNumber, w.PaymentBalanceAccountNumber, w.SortNumber, w.warenumber, MenuWare.InPercent
  FROM dbo.ware w WITH (NOLOCK) 
  INNER JOIN dbo.MenuWareXTab mw WITH (NOLOCK) ON w.warenumber = mw.warenumber 
    AND mw.MenuNumber = @MenuWareNumber
    AND ISNULL(w.MarkAsDeleted , 0) = 0
  INNER JOIN MenuWare WITH (NOLOCK) ON MenuWare.FK_MenuNumber = mw.MenuNumber
OPEN #WareCur

SET @NumbersOfWares = @@CURSOR_ROWS

FETCH Next FROM #WareCur INTO @warename, @procentage, @ResultAccountNumber, @BalanceAccountNumber, @PaymentBalanceAccountNumber, @WareSortNumber, @SingleWareNumber, @InPercent

/* Split menuware (Virtual ware) into physical wares */
WHILE (@@FETCH_STATUS <> -1)
BEGIN
  IF @procentage > 0
  BEGIN
    IF @InPercent = 1
      SET @NewPrice = ((@MenuWarePrice*@procentage)/100.0)
	ELSE
      SET @NewPrice = @procentage * @Pieces

    IF @NewPrice < 0
	  SET @NewPrice = @NewPrice * -1

    Select @VATValueNumber = VatValueNumber,@VATAccountNumber=VATAccountNumber  From Accounts WITH (NOLOCK) Where Accounts.AccountNumber = @ResultAccountNumber
    exec GetVatvalueFromVATValueNumber @VATValueNumber, @VatValueWare OUTPUT 
		
    SET @VatOfWarePrice =  ISNULL(@NewPrice*(1-(1/convert(float,((@VatValueWare/100)+1)))), 0)
    SET @WarePriceExVAT = (@NewPrice - @VatOfWarePrice)

    IF @Pieces > 0
    BEGIN
      INSERT INTO Transactions (ProductNumber, PackageNumber, GlobalTransactionNumber, OriginalGlobalTransactionNumber, RealDateTimeForTransaction, DateStamp, UserIDNumber, FolioNumber, Pieces, PriceOverwrite, CurrencyNumber, ShowOnBillFlag, Debit, MovedDeleted, WareNumber, TextOverwrite, Payment, AccountNumber, VATValue, FK_DepartmentNumber, Note, OriginalDate, ExchangeRateOverwrite, ProductWareLinkNumber, HotelCustomerNumber, ChargedWithCard, FK_ArrangementLineNumber)
      VALUES(0, 0, @GlobalTransactionNumber, @GlobalTransactionNumber, @RealDateTimeForTransaction, @PostForDatetime, @UserIDNumber, @FolioNumber, @Pieces, @WarePriceExVAT, @MyCountryNumber, 0, 0, 0, @SingleWareNumber, @Warename, @Payment, @ResultAccountNumber, @VatValueWare, @FK_DepartmentNumber, @Note, @PostForDatetime, @ExchangeRate, @ProductWareLinkNumber, @HotelCustomerNumber, @ChargedWithCard, @AArrangementLineNumber)
      IF @UseCashDesk = 1
      BEGIN
        SELECT @LastTransactionNumber = @@Identity
        EXEC TransactionsRelationInsert @LastTransactionNumber, @ACashDesk, @UserIDNumber
      END

      -- Debiter vare incl moms på balancekonto
      INSERT INTO Transactions (ProductNumber, PackageNumber, GlobalTransactionNumber, OriginalGlobalTransactionNumber, RealDateTimeForTransaction, DateStamp, UserIDNumber, FolioNumber, Pieces, PriceOverwrite, CurrencyNumber, ShowOnBillFlag, Debit, MovedDeleted, WareNumber, TextOverwrite, Payment, AccountNumber, VATValue, FK_DepartmentNumber, Note, OriginalDate, ExchangeRateOverwrite, ProductWareLinkNumber, HotelCustomerNumber, ChargedWithCard, FK_ArrangementLineNumber)
      VALUES(0, 0, @GlobalTransactionNumber, @GlobalTransactionNumber, @RealDateTimeForTransaction, @PostForDatetime, @UserIDNumber, @FolioNumber, @Pieces, 0, @MyCountryNumber, 0, 1, 0, @SingleWareNumber, @Warename, @Payment, @BalanceAccountNumber, @VatValueWare, @FK_DepartmentNumber, @Note, @PostForDatetime, @ExchangeRate, @ProductWareLinkNumber, @HotelCustomerNumber, @ChargedWithCard, @AArrangementLineNumber)
      IF @UseCashDesk = 1
      BEGIN
        SELECT @LastTransactionNumber = @@Identity
        EXEC TransactionsRelationInsert @LastTransactionNumber, @ACashDesk, @UserIDNumber
      END

      -- Krediter momsen for varen på balancen (moms)
      INSERT INTO Transactions (ProductNumber, PackageNumber, GlobalTransactionNumber, OriginalGlobalTransactionNumber, RealDateTimeForTransaction, DateStamp, UserIDNumber, FolioNumber, Pieces, PriceOverwrite, CurrencyNumber, ShowOnBillFlag, Debit, MovedDeleted, WareNumber, TextOverwrite, Payment, AccountNumber, VATValue, FK_DepartmentNumber, Note, OriginalDate, ExchangeRateOverwrite, ProductWareLinkNumber, HotelCustomerNumber, ChargedWithCard, FK_ArrangementLineNumber)
      VALUES(0, 0, @GlobalTransactionNumber, @GlobalTransactionNumber, @RealDateTimeForTransaction, @PostForDatetime, @UserIDNumber, @FolioNumber, @Pieces, @VatOfWarePrice, @MyCountryNumber, 0, 0, 0, @SingleWareNumber, @Warename, @Payment, @VATAccountNumber, @VatValueWare, @FK_DepartmentNumber, @Note, @PostForDatetime, @ExchangeRate, @ProductWareLinkNumber, @HotelCustomerNumber, @ChargedWithCard, @AArrangementLineNumber)
      IF @UseCashDesk = 1
      BEGIN
        SELECT @LastTransactionNumber = @@Identity
        EXEC TransactionsRelationInsert @LastTransactionNumber, @ACashDesk, @UserIDNumber
      END
    END
    ELSE if @Pieces < 0
    BEGIN
      INSERT INTO Transactions (ProductNumber, PackageNumber, GlobalTransactionNumber, OriginalGlobalTransactionNumber, RealDateTimeForTransaction, DateStamp, UserIDNumber, FolioNumber, Pieces, PriceOverwrite, CurrencyNumber, ShowOnBillFlag, Debit, MovedDeleted, WareNumber, TextOverwrite, Payment, AccountNumber, VATValue, FK_DepartmentNumber, Note, OriginalDate, ExchangeRateOverwrite, ProductWareLinkNumber, HotelCustomerNumber, ChargedWithCard, FK_ArrangementLineNumber)
      VALUES(0, 0, @GlobalTransactionNumber, @GlobalTransactionNumber, @RealDateTimeForTransaction, @PostForDatetime, @UserIDNumber, @FolioNumber, @Pieces, @WarePriceExVAT, @MyCountryNumber, 0, 1, 0, @SingleWareNumber, @Warename, @Payment, @ResultAccountNumber, @VatValueWare, @FK_DepartmentNumber, @Note, @PostForDatetime, @ExchangeRate, @ProductWareLinkNumber, @HotelCustomerNumber, @ChargedWithCard, @AArrangementLineNumber)
      IF @UseCashDesk = 1
      BEGIN
        SELECT @LastTransactionNumber = @@Identity
        EXEC TransactionsRelationInsert @LastTransactionNumber, @ACashDesk, @UserIDNumber
      END

      -- Debiter vare incl moms på balancekonto
      INSERT INTO Transactions (ProductNumber, PackageNumber, GlobalTransactionNumber, OriginalGlobalTransactionNumber, RealDateTimeForTransaction, DateStamp, UserIDNumber, FolioNumber, Pieces, PriceOverwrite, CurrencyNumber, ShowOnBillFlag, Debit, MovedDeleted, WareNumber, TextOverwrite, Payment, AccountNumber, VATValue, FK_DepartmentNumber, Note, OriginalDate, ExchangeRateOverwrite, ProductWareLinkNumber, HotelCustomerNumber, ChargedWithCard, FK_ArrangementLineNumber)
      VALUES(0, 0, @GlobalTransactionNumber, @GlobalTransactionNumber, @RealDateTimeForTransaction, @PostForDatetime, @UserIDNumber, @FolioNumber, @Pieces, 0, @MyCountryNumber, 0, 0, 0, @SingleWareNumber, @Warename, @Payment, @BalanceAccountNumber, @VatValueWare, @FK_DepartmentNumber, @Note, @PostForDatetime, @ExchangeRate, @ProductWareLinkNumber, @HotelCustomerNumber, @ChargedWithCard, @AArrangementLineNumber)
      IF @UseCashDesk = 1
      BEGIN
        SELECT @LastTransactionNumber = @@Identity
        EXEC TransactionsRelationInsert @LastTransactionNumber, @ACashDesk, @UserIDNumber
      END

      -- Krediter momsen for varen på balancen (moms)
      INSERT INTO Transactions (ProductNumber, PackageNumber, GlobalTransactionNumber, OriginalGlobalTransactionNumber, RealDateTimeForTransaction, DateStamp, UserIDNumber, FolioNumber, Pieces, PriceOverwrite, CurrencyNumber, ShowOnBillFlag, Debit, MovedDeleted, WareNumber, TextOverwrite, Payment, AccountNumber, VATValue, FK_DepartmentNumber, Note, OriginalDate, ExchangeRateOverwrite, ProductWareLinkNumber, HotelCustomerNumber, ChargedWithCard, FK_ArrangementLineNumber)
      VALUES(0, 0, @GlobalTransactionNumber, @GlobalTransactionNumber, @RealDateTimeForTransaction, @PostForDatetime, @UserIDNumber, @FolioNumber, @Pieces, @VatOfWarePrice, @MyCountryNumber, 0, 1, 0, @SingleWareNumber, @Warename, @Payment, @VATAccountNumber, @VatValueWare, @FK_DepartmentNumber, @Note, @PostForDatetime, @ExchangeRate, @ProductWareLinkNumber, @HotelCustomerNumber, @ChargedWithCard, @AArrangementLineNumber)
      IF @UseCashDesk = 1
      BEGIN
        SELECT @LastTransactionNumber = @@Identity
        EXEC TransactionsRelationInsert @LastTransactionNumber, @ACashDesk, @UserIDNumber
      END
    END
  END

  FETCH NEXT FROM #WareCur INTO @warename, @procentage, @ResultAccountNumber, @BalanceAccountNumber, @PaymentBalanceAccountNumber, @WareSortNumber, @SingleWareNumber, @InPercent
end

CLOSE #wareCur
DEALLOCATE #wareCur