CREATE PROCEDURE dbo.PostPackageLine @MoveToPackageDeb BIT, @MoveFromPackageDeb BIT, @UseCashDesk BIT, @ACashDesk INT, @VatOfPacket FLOAT, @ResultAccountNumber INT, @BalanceAccountNumber INT, 
  @VATAccountNumber INT, @VatValueWare FLOAT, @DayProduct BIT, @PostForfait BIT, @RealDateTimeForTransaction DATETIME, @ProductNumber INT, @PacketNumber INT, 
  @Adult INT, @Children1 INT, @Children2 INT, @PostDatetime DateTime, @UserIDNumber INT, @FolioNumber INT, @Pieces INT, @WarePrice FLOAT, @CurrencyNumber INT, 
  @WareNumber INT, @WareText VARCHAR(100), @PackageDebAccount INT, @FK_TurnoverDepartmentNumber INT, @GuestNumber INT, @ProductWareLinkNumber INT, @HotelCustomerNumber INT, @AArrangementLineNumber INT = NULL, @ALocalPieces INT = NULL AS
SET NOCOUNT ON
/*
Changes:
 KJ 20150326: Ext: Added arranagementlinenumber. Handle for multiple precharge.
    
*/

DECLARE @GlobalTransactionNumber INT
DECLARE @LastTransactionNumber INT
DECLARE @WarePriceWithOutVAT FLOAT
DECLARE @AccNoForfaitRevenue INT
DECLARE @AccNoForfaitRebooked INT
DECLARE @LDebitInverted BIT
DECLARE @LCreditInverted BIT

IF @ALocalPieces <> @Pieces
BEGIN
  SET @LDebitInverted = 0
  SET @LCreditInverted = 1
END
ELSE
BEGIN
  SET @LDebitInverted = 1
  SET @LCreditInverted = 0
END

IF ISNULL(@DayProduct, 0) = 0
BEGIN
  -- Post Forfait if "Forfait, Revenue" and "Forfait, Rebooked" accounttype exists
  SELECT @AccNoForfaitRevenue = ISNULL(AccountNumber, 0) FROM Accounts WITH (NOLOCK) WHERE AccountType = 10 AND ISNULL(MarkAsDeleted, 0) = 0
  SELECT @AccNoForfaitRebooked = ISNULL(AccountNumber, 0) FROM Accounts WITH (NOLOCK) WHERE AccountType = 11 AND ISNULL(MarkAsDeleted, 0) = 0
END

IF @AArrangementLineNumber <= 0
  SET @AArrangementLineNumber = NULL

IF @MoveToPackageDeb = 1
BEGIN
  EXEC GetNextNumber 'GlobalTransactionIdentity', 1, @GlobalTransactionNumber OUTPUT

  IF @Pieces > 0
  BEGIN
    -- Krediter vare incl moms på balancekonto
    INSERT INTO Transactions (GlobalTransactionNumber, OriginalGlobalTransactionNumber, RealDateTimeForTransaction, ProductNumber, PackageNumber, PAXadult, PAXChild1, PAXChild2, DateStamp, UserIDNumber, FolioNumber, Pieces, PriceOverwrite, CurrencyNumber, ShowOnBillFlag, Debit, MovedDeleted, WareNumber, TextOverwrite, Payment, AccountNumber, VATValue, FK_DepartmentNumber, OriginalDate, ExchangeRateOverwrite, GuestNumber, ProductWareLinkNumber, HotelCustomerNumber, FK_ArrangementLineNumber)
    VALUES(@GlobalTransactionNumber, @GlobalTransactionNumber, @RealDateTimeForTransaction, @ProductNumber, @PacketNumber, @Adult, @Children1, @Children2, @PostDatetime, @UserIDNumber, @FolioNumber, @Pieces, @WarePrice, @CurrencyNumber, 0, @LCreditInverted, 0, @WareNumber, @WareText, 0, @PackageDebAccount, 0, @FK_TurnoverDepartmentNumber, @PostDatetime, 1, @GuestNumber, @ProductWareLinkNumber, @HotelCustomerNumber, @AArrangementLineNumber)
    IF @UseCashDesk = 1
    BEGIN
      SELECT @LastTransactionNumber = @@Identity
      EXEC TransactionsRelationInsert @LastTransactionNumber, @ACashDesk, @UserIDNumber
    END
  END
  ELSE
  BEGIN
    -- Krediter vare på omsætning på resultat excl moms ER RETTET
    -- Debiter vare incl moms på balancekonto ER RETTET
    INSERT INTO Transactions (GlobalTransactionNumber, OriginalGlobalTransactionNumber, RealDateTimeForTransaction, ProductNumber, PackageNumber, PAXadult, PAXChild1, PAXChild2, DateStamp, UserIDNumber, FolioNumber, Pieces, PriceOverwrite, CurrencyNumber, ShowOnBillFlag, Debit, MovedDeleted, WareNumber, TextOverwrite, Payment, AccountNumber, VATValue, FK_DepartmentNumber, OriginalDate, ExchangeRateOverwrite, GuestNumber, ProductWareLinkNumber, HotelCustomerNumber, FK_ArrangementLineNumber)
    VALUES(@GlobalTransactionNumber, @GlobalTransactionNumber, @RealDateTimeForTransaction, @ProductNumber, @PacketNumber, @Adult, @Children1, @Children2, @PostDatetime, @UserIDNumber, @FolioNumber, @Pieces, @WarePrice, @CurrencyNumber, 0, @LDebitInverted, 0, @WareNumber, @WareText, 0, @PackageDebAccount, 0, @FK_TurnoverDepartmentNumber, @PostDatetime, 1, @GuestNumber, @ProductWareLinkNumber, @HotelCustomerNumber, @AArrangementLineNumber)
    IF @UseCashDesk = 1
    BEGIN
      SELECT @LastTransactionNumber = @@Identity
      EXEC TransactionsRelationInsert @LastTransactionNumber, @ACashDesk, @UserIDNumber
    END
  END
END
--move from package deb

IF @MoveFromPackageDeb = 1
BEGIN
  EXEC GetNextNumber 'GlobalTransactionIdentity', 1, @GlobalTransactionNumber OUTPUT
        
  IF @VatOfPacket > 0
    SET @WarePriceWithOutVAT = @WarePrice - @VatOfPacket
  ELSE
    SET @WarePriceWithOutVAT = ABS(-@WarePrice - @VatOfPacket)

  if @Pieces > 0 
  begin
    -- Krediter momsen for varen på balancen (moms)
    INSERT INTO Transactions (GlobalTransactionNumber, OriginalGlobalTransactionNumber, RealDateTimeForTransaction, ProductNumber, PackageNumber, PAXadult, PAXChild1, PAXChild2, DateStamp, UserIDNumber, FolioNumber, Pieces, PriceOverwrite, CurrencyNumber, ShowOnBillFlag, Debit, MovedDeleted, WareNumber, TextOverwrite, Payment, AccountNumber, VATValue, FK_DepartmentNumber, OriginalDate, ExchangeRateOverwrite, GuestNumber, ProductWareLinkNumber, HotelCustomerNumber, FK_ArrangementLineNumber)
    VALUES(@GlobalTransactionNumber, @GlobalTransactionNumber, @RealDateTimeForTransaction, @ProductNumber, @PacketNumber, @Adult, @Children1, @Children2, @PostDatetime, @UserIDNumber,@FolioNumber, @Pieces, @WarePriceWithOutVAT, @CurrencyNumber, 0, @LCreditInverted, 0, @WareNumber, @WareText, 0, @ResultAccountNumber, @VatValueWare, @FK_TurnoverDepartmentNumber, @PostDatetime, 1, @GuestNumber, @ProductWareLinkNumber, @HotelCustomerNumber, @AArrangementLineNumber)
    IF @UseCashDesk = 1
    BEGIN
      SELECT @LastTransactionNumber = @@Identity
      EXEC TransactionsRelationInsert @LastTransactionNumber, @ACashDesk, @UserIDNumber
    END

    -- Debiter vare incl moms på balancekonto
    INSERT INTO Transactions (GlobalTransactionNumber, OriginalGlobalTransactionNumber, RealDateTimeForTransaction, ProductNumber, PackageNumber, PAXadult, PAXChild1, PAXChild2, DateStamp, UserIDNumber, FolioNumber, Pieces, PriceOverwrite, CurrencyNumber, ShowOnBillFlag, Debit, MovedDeleted, WareNumber, TextOverwrite, Payment, AccountNumber, VATValue, FK_DepartmentNumber, OriginalDate,ExchangeRateOverwrite, GuestNumber, ProductWareLinkNumber, HotelCustomerNumber, FK_ArrangementLineNumber)
    VALUES(@GlobalTransactionNumber, @GlobalTransactionNumber, @RealDateTimeForTransaction, @ProductNumber, @PacketNumber, @Adult, @Children1, @Children2, @PostDatetime, @UserIDNumber, @FolioNumber, @Pieces, @WarePrice, @CurrencyNumber, 0, @LDebitInverted, 0, @WareNumber, @WareText, 0, @PackageDebAccount, @VatValueWare, @FK_TurnoverDepartmentNumber, @PostDatetime, 1, @GuestNumber, @ProductWareLinkNumber, @HotelCustomerNumber, @AArrangementLineNumber)
    IF @UseCashDesk = 1
    BEGIN
      SELECT @LastTransactionNumber = @@Identity
      EXEC TransactionsRelationInsert @LastTransactionNumber, @ACashDesk, @UserIDNumber
    END

    -- Krediter momsen for varen på balancen (moms)
    INSERT INTO Transactions (GlobalTransactionNumber, OriginalGlobalTransactionNumber, RealDateTimeForTransaction, ProductNumber, PackageNumber, PAXadult, PAXChild1, PAXChild2, DateStamp, UserIDNumber, FolioNumber, Pieces, PriceOverwrite, CurrencyNumber, ShowOnBillFlag, Debit, MovedDeleted, WareNumber, TextOverwrite, Payment, AccountNumber, VATVAlue, FK_DepartmentNumber, OriginalDate,ExchangeRateOverwrite, GuestNumber, ProductWareLinkNumber, HotelCustomerNumber, FK_ArrangementLineNumber)
    VALUES(@GlobalTransactionNumber, @GlobalTransactionNumber, @RealDateTimeForTransaction, @ProductNumber, @PacketNumber, @Adult, @Children1, @Children2, @PostDatetime, @UserIDNumber,@FolioNumber, @Pieces, @VatOfPacket, @CurrencyNumber, 0, @LCreditInverted, 0, @WareNumber, @WareText, 0, @VATAccountNumber, @VatValueWare, @FK_TurnoverDepartmentNumber, @PostDatetime, 1, @GuestNumber, @ProductWareLinkNumber, @HotelCustomerNumber, @AArrangementLineNumber)
    IF @UseCashDesk = 1
    BEGIN
      SELECT @LastTransactionNumber = @@Identity
      EXEC TransactionsRelationInsert @LastTransactionNumber, @ACashDesk, @UserIDNumber
    END
  end
  else
  begin
    -- Debiter vare på omsætning på resultat excl moms
    INSERT INTO Transactions (GlobalTransactionNumber, OriginalGlobalTransactionNumber, RealDateTimeForTransaction, ProductNumber, PackageNumber, PAXadult, PAXChild1, PAXChild2, DateStamp, UserIDNumber, FolioNumber, Pieces, PriceOverwrite, CurrencyNumber, ShowOnBillFlag, Debit, MovedDeleted, WareNumber, TextOverwrite, Payment, AccountNumber, VATValue, FK_DepartmentNumber, OriginalDate, ExchangeRateOverwrite, GuestNumber, ProductWareLinkNumber, HotelCustomerNumber, FK_ArrangementLineNumber)
    VALUES(@GlobalTransactionNumber, @GlobalTransactionNumber, @RealDateTimeForTransaction, @ProductNumber, @PacketNumber, @Adult, @Children1, @Children2, @PostDatetime, @UserIDNumber,@FolioNumber, @Pieces, @WarePriceWithOutVAT, @CurrencyNumber, 0, @LDebitInverted, 0, @WareNumber, @WareText , 0, @ResultAccountNumber, @VatValueWare, @FK_TurnoverDepartmentNumber, @PostDatetime, 1, @GuestNumber, @ProductWareLinkNumber, @HotelCustomerNumber, @AArrangementLineNumber)
    IF @UseCashDesk = 1
    BEGIN
      SELECT @LastTransactionNumber = @@Identity
      EXEC TransactionsRelationInsert @LastTransactionNumber, @ACashDesk, @UserIDNumber
    END

    -- Krediter vare incl moms på balancekonto
    INSERT INTO Transactions (GlobalTransactionNumber, OriginalGlobalTransactionNumber, RealDateTimeForTransaction, ProductNumber, PackageNumber, PAXadult, PAXChild1, PAXChild2, DateStamp, UserIDNumber, FolioNumber, Pieces, PriceOverwrite, CurrencyNumber, ShowOnBillFlag, Debit, MovedDeleted, WareNumber, TextOverwrite, Payment, AccountNumber, VATValue, FK_DepartmentNumber, OriginalDate, ExchangeRateOverwrite, GuestNumber, ProductWareLinkNumber, HotelCustomerNumber, FK_ArrangementLineNumber)
    VALUES(@GlobalTransactionNumber, @GlobalTransactionNumber, @RealDateTimeForTransaction, @ProductNumber, @PacketNumber, @Adult, @Children1, @Children2, @PostDatetime, @UserIDNumber, @FolioNumber, @Pieces, @WarePrice, @CurrencyNumber, 0, @LCreditInverted, 0, @WareNumber, @WareText, 0, @PackageDebAccount, @VatValueWare, @FK_TurnoverDepartmentNumber, @PostDatetime, 1, @GuestNumber, @ProductWareLinkNumber, @HotelCustomerNumber, @AArrangementLineNumber)
    IF @UseCashDesk = 1
    BEGIN
      SELECT @LastTransactionNumber = @@Identity
      EXEC TransactionsRelationInsert @LastTransactionNumber, @ACashDesk, @UserIDNumber
    END

    -- Debiter momsen for varen på balancen (moms)
    INSERT INTO Transactions (GlobalTransactionNumber, OriginalGlobalTransactionNumber, RealDateTimeForTransaction, ProductNumber, PackageNumber, PAXadult, PAXChild1, PAXChild2, DateStamp, UserIDNumber, FolioNumber, Pieces, PriceOverwrite, CurrencyNumber, ShowOnBillFlag, Debit, MovedDeleted, WareNumber, TextOverwrite, Payment, AccountNumber, VATVAlue, FK_DepartmentNumber, OriginalDate, ExchangeRateOverwrite, GuestNumber, ProductWareLinkNumber, HotelCustomerNumber, FK_ArrangementLineNumber)
    VALUES(@GlobalTransactionNumber, @GlobalTransactionNumber, @RealDateTimeForTransaction, @ProductNumber, @PacketNumber, @Adult, @Children1, @Children2, @PostDatetime, @UserIDNumber,@FolioNumber, @Pieces,@VatOfPacket, @CurrencyNumber, 0, @LDebitInverted, 0, @WareNumber, @WareText, 0, @VATAccountNumber, @VatValueWare, @FK_TurnoverDepartmentNumber, @PostDatetime, 1, @GuestNumber, @ProductWareLinkNumber, @HotelCustomerNumber, @AArrangementLineNumber)
    IF @UseCashDesk = 1
    BEGIN
      SELECT @LastTransactionNumber = @@Identity
      EXEC TransactionsRelationInsert @LastTransactionNumber, @ACashDesk, @UserIDNumber
    END
  end

  -- Forfait (france)
  IF @DayProduct = 0 AND @WarePrice > 0 AND @AccNoForfaitRevenue > 0 AND @AccNoForfaitRebooked > 0 AND @PostForfait = 1
  BEGIN
    EXEC GetNextNumber 'GlobalTransactionIdentity', 1, @GlobalTransactionNumber OUTPUT

    IF (@Pieces>0) 
    BEGIN
      -- Credit Forfait Revenue
      INSERT INTO Transactions (GlobalTransactionNumber, OriginalGlobalTransactionNumber, RealDateTimeForTransaction, ProductNumber, PackageNumber, PAXadult, PAXChild1, PAXChild2, DateStamp, UserIDNumber, FolioNumber, Pieces, PriceOverwrite, CurrencyNumber, ShowOnBillFlag, Debit, MovedDeleted, WareNumber, TextOverwrite, Payment, AccountNumber, VATVAlue, FK_DepartmentNumber, OriginalDate, ExchangeRateOverwrite, GuestNumber, ProductWareLinkNumber, HotelCustomerNumber, FK_ArrangementLineNumber)
      VALUES(@GlobalTransactionNumber, @GlobalTransactionNumber, @RealDateTimeForTransaction, @ProductNumber, @PacketNumber, @Adult, @Children1, @Children2, @PostDatetime, @UserIDNumber, @FolioNumber, @Pieces, @WarePrice, @CurrencyNumber, 0, @LCreditInverted, 0, @WareNumber, @WareText, 0, @AccNoForfaitRevenue, 0, @FK_TurnoverDepartmentNumber, @PostDatetime, 1, @GuestNumber, @ProductWareLinkNumber, @HotelCustomerNumber, @AArrangementLineNumber)
      IF @UseCashDesk = 1
      BEGIN
        SELECT @LastTransactionNumber = @@Identity
        EXEC TransactionsRelationInsert @LastTransactionNumber, @ACashDesk, @UserIDNumber
      END

      -- Debit Forfait Rebooked
      INSERT INTO Transactions (GlobalTransactionNumber, OriginalGlobalTransactionNumber, RealDateTimeForTransaction, ProductNumber, PackageNumber, PAXadult, PAXChild1, PAXChild2, DateStamp, UserIDNumber, FolioNumber, Pieces, PriceOverwrite, CurrencyNumber, ShowOnBillFlag, Debit, MovedDeleted, WareNumber, TextOverwrite, Payment, AccountNumber, VATVAlue, FK_DepartmentNumber, OriginalDate, ExchangeRateOverwrite, GuestNumber, ProductWareLinkNumber, HotelCustomerNumber, FK_ArrangementLineNumber)
      VALUES(@GlobalTransactionNumber, @GlobalTransactionNumber, @RealDateTimeForTransaction, @ProductNumber, @PacketNumber, @Adult, @Children1, @Children2, @PostDatetime, @UserIDNumber, @FolioNumber, @Pieces, @WarePrice, @CurrencyNumber, 0, @LDebitInverted, 0, @WareNumber, @WareText, 0, @AccNoForfaitRebooked, 0, @FK_TurnoverDepartmentNumber, @PostDatetime, 1, @GuestNumber, @ProductWareLinkNumber, @HotelCustomerNumber, @AArrangementLineNumber)
      IF @UseCashDesk = 1
      BEGIN
        SELECT @LastTransactionNumber = @@Identity
        EXEC TransactionsRelationInsert @LastTransactionNumber, @ACashDesk, @UserIDNumber
      END
    END
    ELSE
    BEGIN
      -- Credit Forfait Revenue
      INSERT INTO Transactions (GlobalTransactionNumber, OriginalGlobalTransactionNumber, RealDateTimeForTransaction, ProductNumber, PackageNumber, PAXadult, PAXChild1, PAXChild2, DateStamp, UserIDNumber, FolioNumber, Pieces, PriceOverwrite, CurrencyNumber, ShowOnBillFlag, Debit, MovedDeleted, WareNumber, TextOverwrite, Payment, AccountNumber, VATVAlue, FK_DepartmentNumber, OriginalDate, ExchangeRateOverwrite, GuestNumber, ProductWareLinkNumber, HotelCustomerNumber, FK_ArrangementLineNumber)
      VALUES(@GlobalTransactionNumber, @GlobalTransactionNumber, @RealDateTimeForTransaction, @ProductNumber, @PacketNumber, @Adult, @Children1, @Children2, @PostDatetime, @UserIDNumber, @FolioNumber, @Pieces, @WarePrice, @CurrencyNumber, 0, @LDebitInverted, 0, @WareNumber, @WareText, 0, @AccNoForfaitRevenue, 0, @FK_TurnoverDepartmentNumber, @PostDatetime, 1, @GuestNumber, @ProductWareLinkNumber, @HotelCustomerNumber, @AArrangementLineNumber)
      IF @UseCashDesk = 1
      BEGIN
        SELECT @LastTransactionNumber = @@Identity
        EXEC TransactionsRelationInsert @LastTransactionNumber, @ACashDesk, @UserIDNumber
      END

      -- Debit Forfait Rebooked
      INSERT INTO Transactions (GlobalTransactionNumber, OriginalGlobalTransactionNumber, RealDateTimeForTransaction, ProductNumber, PackageNumber, PAXadult, PAXChild1, PAXChild2, DateStamp, UserIDNumber, FolioNumber, Pieces, PriceOverwrite, CurrencyNumber, ShowOnBillFlag, Debit, MovedDeleted, WareNumber, TextOverwrite, Payment, AccountNumber, VATVAlue, FK_DepartmentNumber, OriginalDate, ExchangeRateOverwrite, GuestNumber, ProductWareLinkNumber, HotelCustomerNumber, FK_ArrangementLineNumber)
      VALUES(@GlobalTransactionNumber, @GlobalTransactionNumber, @RealDateTimeForTransaction, @ProductNumber, @PacketNumber, @Adult, @Children1, @Children2, @PostDatetime, @UserIDNumber, @FolioNumber, @Pieces, @WarePrice, @CurrencyNumber, 0, @LCreditInverted, 0, @WareNumber, @WareText, 0, @AccNoForfaitRebooked, 0, @FK_TurnoverDepartmentNumber, @PostDatetime, 1, @GuestNumber, @ProductWareLinkNumber, @HotelCustomerNumber, @AArrangementLineNumber)
      IF @UseCashDesk = 1
      BEGIN
        SELECT @LastTransactionNumber = @@Identity
        EXEC TransactionsRelationInsert @LastTransactionNumber, @ACashDesk, @UserIDNumber
      END
    END   
  END
END