CREATE PROCEDURE dbo.PostDiscountLine @PostShowOnBillLine BIT, @UseCashDesk BIT, @ACashDesk INT, @GlobalTransactionNumber INT, @RealDateTimeForTransaction DATETIME, 
  @PaxAdult INT, @PaxChild1 INT, @PaxChild2 INT, @PostForDatetime DATETIME, @UserIDNumber INT, @FolioNumber INT, @Pieces INT, @WarePriceExVAT MONEY, @WarePrice MONEY, @VatOfWarePrice MONEY, 
  @MyCountryNumber INT, @WareNumber INT, @WareText VARCHAR(100), @Payment SMALLINT, @ResultAccountNumber INT, @BalanceAccountNumber INT, @VATAccountNumber INT, @VatValueWare MONEY, 
  @FK_DepartmentNumber INT, @Note VARCHAR(50), @ExchangeRate FLOAT, @ProductWareLinkNumber INT, @GuestNumber INT, @HotelCustomerNumber INT, @ChargedWithCard BIT AS
/*
  Create by Kent Johansen june 2011
  -- Code moved from Postering sp

*/
SET NOCOUNT ON

DECLARE @LastTransactionNumber INT 

-- Debiter rabat på rabatkonto excl moms
IF @Pieces > 0
BEGIN
  INSERT INTO Transactions (ProductNumber, PackageNumber, GlobalTransactionNumber, OriginalGlobalTransactionNumber, RealDateTimeForTransaction, PAXadult, PAXChild1, PAXChild2, DateStamp, UserIDNumber, FolioNumber, Pieces, PriceOverwrite, CurrencyNumber, ShowOnBillFlag, Debit, MovedDeleted, WareNumber, TextOverwrite, Payment, AccountNumber, VATValue, /*DepartmentOverwrite*/ FK_DepartmentNumber, Note, OriginalDate,ExchangeRateOverwrite,ProductWareLinkNumber,GuestNumber, HotelCustomerNumber, ChargedWithCard)
  VALUES(0, 0, @GlobalTransactionNumber, @GlobalTransactionNumber, @RealDateTimeForTransaction, @PaxAdult, @PaxChild1, @PaxChild2,  @PostForDatetime, @UserIDNumber,@FolioNumber,@Pieces,@WarePriceExVAT, @MyCountryNumber, 0, 1, 0,@WareNumber,@WareText, @Payment, @ResultAccountNumber, @VatValueWare, /*@Department*/ @FK_DepartmentNumber, @Note, @PostForDatetime,@ExchangeRate,@ProductWareLinkNumber, @GuestNumber, @HotelCustomerNumber, @ChargedWithCard)
  IF @UseCashDesk = 1
  BEGIN
    SELECT @LastTransactionNumber = @@Identity
    EXEC TransactionsRelationInsert @LastTransactionNumber, @ACashDesk, @UserIDNumber
  END

  IF @PostShowOnBillLine = 1
  BEGIN
    -- Krediter vare incl moms på debitorsamlekonto
    INSERT INTO Transactions (ProductNumber, PackageNumber, GlobalTransactionNumber, OriginalGlobalTransactionNumber, RealDateTimeForTransaction, PAXadult, PAXChild1, PAXChild2, DateStamp, UserIDNumber, FolioNumber, Pieces, PriceOverwrite, CurrencyNumber, ShowOnBillFlag, Debit, MovedDeleted, WareNumber, TextOverwrite, Payment, AccountNumber, VATValue, /*DepartmentOverwrite*/ FK_DepartmentNumber, Note, OriginalDate,ExchangeRateOverwrite,ProductWareLinkNumber,GuestNumber, HotelCustomerNumber, ChargedWithCard)
    VALUES(0, 0, @GlobalTransactionNumber, @GlobalTransactionNumber, @RealDateTimeForTransaction,  @PaxAdult, @PaxChild1, @PaxChild2, @PostForDatetime,@UserIDNumber, @FolioNumber,@Pieces, @WarePrice, @MyCountryNumber, 1, 0, 0,@WareNumber, @WareText, @Payment, @BalanceAccountNumber, @VatValueWare, /*@Department*/ @FK_DepartmentNumber, @Note, @PostForDatetime,@ExchangeRate,@ProductWareLinkNumber, @GuestNumber, @HotelCustomerNumber, @ChargedWithCard)
    IF @UseCashDesk = 1
    BEGIN
      SELECT @LastTransactionNumber = @@Identity
      EXEC TransactionsRelationInsert @LastTransactionNumber, @ACashDesk, @UserIDNumber
    END
  END  

  -- Debiter momsen for varen på balancen (moms)
  INSERT INTO Transactions (ProductNumber, PackageNumber, GlobalTransactionNumber, OriginalGlobalTransactionNumber, RealDateTimeForTransaction, PAXadult, PAXChild1, PAXChild2, DateStamp, UserIDNumber, FolioNumber, Pieces, PriceOverwrite, CurrencyNumber, ShowOnBillFlag, Debit, MovedDeleted, WareNumber, TextOverwrite, Payment, AccountNumber, VATValue, /*DepartmentOverwrite*/ FK_DepartmentNumber, Note, OriginalDate,ExchangeRateOverwrite,ProductWareLinkNumber,GuestNumber, HotelCustomerNumber, ChargedWithCard)
  VALUES(0, 0, @GlobalTransactionNumber, @GlobalTransactionNumber, @RealDateTimeForTransaction, @PaxAdult, @PaxChild1, @PaxChild2,  @PostForDatetime,@UserIDNumber,@FolioNumber,@Pieces,@VatOfWarePrice, @MyCountryNumber, 0, 1, 0,@WareNumber,@WareText, @Payment, @VATAccountNumber, @VatValueWare, /*@Department*/ @FK_DepartmentNumber, @Note, @PostForDatetime,@ExchangeRate,@ProductWareLinkNumber, @GuestNumber, @HotelCustomerNumber, @ChargedWithCard)
  IF @UseCashDesk = 1
  BEGIN
    SELECT @LastTransactionNumber = @@Identity
    EXEC TransactionsRelationInsert @LastTransactionNumber, @ACashDesk, @UserIDNumber
  END
END
ELSE IF @Pieces < 0
BEGIN
  -- Krediter vare på omsætning på resultat excl moms
  INSERT INTO Transactions (ProductNumber, PackageNumber, GlobalTransactionNumber, OriginalGlobalTransactionNumber, RealDateTimeForTransaction, PAXadult, PAXChild1, PAXChild2, DateStamp, UserIDNumber, FolioNumber, Pieces, PriceOverwrite, CurrencyNumber, ShowOnBillFlag, Debit, MovedDeleted, WareNumber, TextOverwrite, Payment, AccountNumber, VATValue, /*DepartmentOverwrite*/ FK_DepartmentNumber, Note, OriginalDate,ExchangeRateOverwrite,ProductWareLinkNumber,GuestNumber, HotelCustomerNumber, ChargedWithCard)
  VALUES(0, 0, @GlobalTransactionNumber, @GlobalTransactionNumber, @RealDateTimeForTransaction, @PaxAdult, @PaxChild1, @PaxChild2,  @PostForDatetime, @UserIDNumber,@FolioNumber,@Pieces,@WarePriceExVAT, @MyCountryNumber, 0, 0, 0,@WareNumber,@WareText, @Payment, @ResultAccountNumber, @VatValueWare, /*@Department*/ @FK_DepartmentNumber, @Note, @PostForDatetime,@ExchangeRate,@ProductWareLinkNumber, @GuestNumber, @HotelCustomerNumber, @ChargedWithCard)
  IF @UseCashDesk = 1
  BEGIN
    SELECT @LastTransactionNumber = @@Identity
    EXEC TransactionsRelationInsert @LastTransactionNumber, @ACashDesk, @UserIDNumber
  END

  IF @PostShowOnBillLine = 1
  BEGIN
    -- Debiter vare incl moms på balancekonto
    INSERT INTO Transactions (ProductNumber, PackageNumber, GlobalTransactionNumber, OriginalGlobalTransactionNumber, RealDateTimeForTransaction, PAXadult, PAXChild1, PAXChild2, DateStamp, UserIDNumber, FolioNumber, Pieces, PriceOverwrite, CurrencyNumber, ShowOnBillFlag, Debit, MovedDeleted, WareNumber, TextOverwrite, Payment, AccountNumber, VATValue, /*DepartmentOverwrite*/ FK_DepartmentNumber, Note, OriginalDate,ExchangeRateOverwrite,ProductWareLinkNumber,GuestNumber, HotelCustomerNumber, ChargedWithCard)
    VALUES(0, 0, @GlobalTransactionNumber, @GlobalTransactionNumber, @RealDateTimeForTransaction,  @PaxAdult, @PaxChild1, @PaxChild2, @PostForDatetime,@UserIDNumber, @FolioNumber,@Pieces, @WarePrice, @MyCountryNumber, 1, 1, 0,@WareNumber, @WareText, @Payment, @BalanceAccountNumber, @VatValueWare, /*@Department*/ @FK_DepartmentNumber, @Note, @PostForDatetime,@ExchangeRate,@ProductWareLinkNumber, @GuestNumber, @HotelCustomerNumber, @ChargedWithCard)
    IF @UseCashDesk = 1
    BEGIN
      SELECT @LastTransactionNumber = @@Identity
      EXEC TransactionsRelationInsert @LastTransactionNumber, @ACashDesk, @UserIDNumber
    END
  END
    
  -- Krediter momsen for varen på balancen (moms)
  INSERT INTO Transactions (ProductNumber, PackageNumber, GlobalTransactionNumber, OriginalGlobalTransactionNumber, RealDateTimeForTransaction, PAXadult, PAXChild1, PAXChild2, DateStamp, UserIDNumber, FolioNumber, Pieces, PriceOverwrite, CurrencyNumber, ShowOnBillFlag, Debit, MovedDeleted, WareNumber, TextOverwrite, Payment, AccountNumber, VATValue, /*DepartmentOverwrite*/ FK_DepartmentNumber, Note, OriginalDate,ExchangeRateOverwrite,ProductWareLinkNumber,GuestNumber, HotelCustomerNumber, ChargedWithCard)
  VALUES(0, 0, @GlobalTransactionNumber, @GlobalTransactionNumber, @RealDateTimeForTransaction, @PaxAdult, @PaxChild1, @PaxChild2,  @PostForDatetime,@UserIDNumber,@FolioNumber,@Pieces, @VatOfWarePrice, @MyCountryNumber, 0, 0, 0,@WareNumber,@WareText, @Payment, @VATAccountNumber, @VatValueWare, /*@Department*/ @FK_DepartmentNumber, @Note, @PostForDatetime,@ExchangeRate,@ProductWareLinkNumber, @GuestNumber, @HotelCustomerNumber, @ChargedWithCard)
  IF @UseCashDesk = 1
  BEGIN
    SELECT @LastTransactionNumber = @@Identity
    EXEC TransactionsRelationInsert @LastTransactionNumber, @ACashDesk, @UserIDNumber
  END
END