CREATE PROCEDURE dbo.PostMissingTransaction @AGlobalTransactionNumber INT AS
SET NOCOUNT ON

/*
  Created by Kent Johansen December 2008

  Changes:
    KJ 20140611: Ext: CashDesk per hotel. Case #CR073269
    KJ 20101004: Bug: Fix for "syntax error"
    KJ 20100325: Ext: Added MovedDeleted = 1
    KJ 20091208: Ext: Added CashDesk
    KJ 20091029: Handle GlobalTransactionNumber = 0
*/
IF @AGlobalTransactionNumber > 0
BEGIN

DECLARE @OrgTransactionNumber INT
DECLARE @OrgDateStamp DATETIME
DECLARE @OrgUserIdNumber INT
DECLARE @OrgFolioNumber INT
DECLARE @OrgPieces INT
DECLARE @OrgPriceOverwrite FLOAT
DECLARE @OrgCurrencyNumber INT
DECLARE @OrgShowOnBillFlag BIT
DECLARE @OrgPAXadult INT
DECLARE @OrgPAXchild1 INT
DECLARE @OrgPAXchild2 INT
DECLARE @OrgTextOverwrite VARCHAR(100)
DECLARE @OrgExchangeRateOverwrite FLOAT
DECLARE @FK_OrgDepartmentOverwrite INT
DECLARE @OrgDebit BIT
DECLARE @OrgVATValue MONEY
DECLARE @OrgMovedDeleted BIT
DECLARE @OrgMovedFolioNr INT
DECLARE @OrgWareNumber INT
DECLARE @OrgPayment INT
DECLARE @OrgInvoiceBillNumber INT
DECLARE @OrgAccountNumber INT
DECLARE @OrgGlobalTransactionNumber INT
DECLARE @OrgProductNumber INT
DECLARE @OrgPackageNumber INT
DECLARE @OrgRealDateTimeforTransaction DATETIME
DECLARE @OrgNote VARCHAR(10)
DECLARE @OrgGuestNumber INT
DECLARE @OrgOriginalDate DATETIME
DECLARE @OrgProductWareLinkNumber INT
DECLARE @OrgDepositfollowupNumber INT
DECLARE @OrgAutoReceivedPayment BIT
DECLARE @OrgHotelCustomerNumber INT
DECLARE @OrgChargedWithCard BIT
DECLARE @CashDesk INT

DECLARE @CurrentDateStr VARCHAR(8)
DECLARE @CurrentDateTime DATETIME

SELECT @CurrentDateStr = SYSTEMDATE FROM System WITH (NOLOCK) WHERE Number = 1
SET @CurrentDateTime = CONVERT(DATETIME, @CurrentDateStr + ' ' + CONVERT(CHAR(8), CURRENT_TIMESTAMP, 108))

DECLARE @TechotelUser INT
SELECT @TechotelUser = UserIdNumber FROM Personel WITH (NOLOCK) WHERE UserTypeNumber = 2 AND ActivePerson = 1 AND ISNULL(MarkAsDeleted,0) = 0 ORDER BY UserIdNumber DESC
IF ISNULL(@TechotelUser, 0) = 0
  SET @TechotelUser = 1

DECLARE @NewDebit BIT

DECLARE @UseCashDesk BIT
DECLARE @LastTransactionNumber INT

SELECT TransactionNumber, DateStamp, UserIDNumber, FolioNumber, Pieces, PriceOverwrite, CurrencyNumber, ShowOnBillFlag, PAXadult, PAXchild1, PAXchild2, TextOverwrite, ExchangeRateOverwrite,
  FK_DepartmentNumber, Debit, VATValue, MovedDeleted, MovedFolioNr, WareNumber, Payment, InvoiceBillNumber, AccountNumber, GlobalTransactionNumber,
  ProductNumber, PackageNumber, RealDateTimeforTransaction, Note, GuestNumber, OriginalDate, ProductWareLinkNumber, DepositfollowupNumber, AutoReceivedPayment,
  HotelCustomerNumber, ChargedWithCard
INTO #tblTransactions FROM Transactions WITH (NOLOCK)
WHERE GlobalTransactionNumber = @AGlobalTransactionNumber
AND MovedDeleted = 1

DECLARE db_cur CURSOR FOR
  SELECT TransactionNumber, DateStamp, UserIDNumber, FolioNumber, Pieces, PriceOverwrite, CurrencyNumber, ShowOnBillFlag, PAXadult, PAXchild1, PAXchild2, TextOverwrite, ExchangeRateOverwrite,
    FK_DepartmentNumber, Debit, VATValue, MovedDeleted, MovedFolioNr, WareNumber, Payment, InvoiceBillNumber, AccountNumber, GlobalTransactionNumber,
    ProductNumber, PackageNumber, RealDateTimeforTransaction, Note, GuestNumber, OriginalDate, ProductWareLinkNumber, DepositfollowupNumber, AutoReceivedPayment,
    HotelCustomerNumber, ChargedWithCard, FK_CashBox
  FROM #tblTransactions WITH (NOLOCK)
  INNER JOIN TransactionsRelation WITH (NOLOCK) ON TransactionsRelation.FK_TransactionNumber = #tblTransactions.TransactionNumber
  WHERE GlobalTransactionNumber = @AGlobalTransactionNumber
OPEN db_cur

FETCH NEXT FROM db_cur INTO @OrgTransactionNumber, @OrgDateStamp, @OrgUserIdNumber, @OrgFolioNumber, @OrgPieces, @OrgPriceOverwrite, @OrgCurrencyNumber, @OrgShowOnBillFlag, @OrgPAXadult, @OrgPAXchild1, 
  @OrgPAXchild2, @OrgTextOverwrite, @OrgExchangeRateOverwrite, @FK_OrgDepartmentOverwrite, @OrgDebit, @OrgVATValue, @OrgMovedDeleted, @OrgMovedFolioNr, @OrgWareNumber,
  @OrgPayment, @OrgInvoiceBillNumber, @OrgAccountNumber, @OrgGlobalTransactionNumber, @OrgProductNumber, @OrgPackageNumber, @OrgRealDateTimeforTransaction,
  @OrgNote, @OrgGuestNumber, @OrgOriginalDate, @OrgProductWareLinkNumber, @OrgDepositfollowupNumber, @OrgAutoReceivedPayment, @OrgHotelCustomerNumber, @OrgChargedWithCard, @CashDesk

WHILE @@FETCH_STATUS = 0
BEGIN
  -- insert new trans with new DateStamp, UserIdNumber, Pieces*-1, Debit, RealDateTimeForTransaction, InvoiceBillNumber = null
  IF @OrgDebit = 1
    SET @NewDebit = 0
  ELSE
    SET @NewDebit = 1

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

  INSERT INTO Transactions (DateStamp, UserIDNumber, FolioNumber, Pieces, PriceOverwrite, CurrencyNumber, ShowOnBillFlag, PAXadult, PAXchild1, PAXchild2,
    TextOverwrite, ExchangeRateOverwrite, FK_DepartmentNumber, Debit, VATValue, MovedDeleted, MovedFolioNr, WareNumber, Payment, InvoiceBillNumber, AccountNumber,
    GlobalTransactionNumber, OriginalGlobalTransactionNumber, ProductNumber, PackageNumber, RealDateTimeforTransaction, Note, GuestNumber, OriginalDate, ProductWareLinkNumber, DepositfollowupNumber, 
    AutoReceivedPayment, HotelCustomerNumber, ChargedWithCard) VALUES 
    (@CurrentDateTime, @TechotelUser, @OrgFolioNumber, @OrgPieces*-1, @OrgPriceOverwrite, @OrgCurrencyNumber, @OrgShowOnBillFlag, @OrgPaxAdult, @OrgPaxChild1, @OrgPaxChild2,
    @OrgTextOverwrite, @OrgExchangeRateOverwrite, @FK_OrgDepartmentOverwrite, @NewDebit, @OrgVatValue, @OrgMovedDeleted, @OrgMovedFolioNr, @OrgWareNumber, @OrgPayment, NULL, @OrgAccountNumber,
    @OrgGlobalTransactionNumber, @OrgGlobalTransactionNumber, @OrgProductNumber, @OrgPackageNumber, @CurrentDateTime, @OrgNote, @OrgGuestNumber, @OrgOriginalDate, @OrgProductWareLinkNumber, @OrgDepositFollowupNumber,
    @OrgAutoReceivedPayment, @OrgHotelCustomerNumber, @OrgChargedWithCard)

  IF @UseCashDesk = 1
  BEGIN
    SELECT @LastTransactionNumber = @@Identity
    EXEC TransactionsRelationInsert @LastTransactionNumber, @CashDesk, @TechotelUser
  END

  FETCH NEXT FROM db_cur INTO @OrgTransactionNumber, @OrgDateStamp, @OrgUserIdNumber, @OrgFolioNumber, @OrgPieces, @OrgPriceOverwrite, @OrgCurrencyNumber, @OrgShowOnBillFlag, @OrgPAXadult, @OrgPAXchild1, 
    @OrgPAXchild2, @OrgTextOverwrite, @OrgExchangeRateOverwrite, @FK_OrgDepartmentOverwrite, @OrgDebit, @OrgVATValue, @OrgMovedDeleted, @OrgMovedFolioNr, @OrgWareNumber,
    @OrgPayment, @OrgInvoiceBillNumber, @OrgAccountNumber, @OrgGlobalTransactionNumber, @OrgProductNumber, @OrgPackageNumber, @OrgRealDateTimeforTransaction,
    @OrgNote, @OrgGuestNumber, @OrgOriginalDate, @OrgProductWareLinkNumber, @OrgDepositfollowupNumber, @OrgAutoReceivedPayment, @OrgHotelCustomerNumber, @OrgChargedWithCard, @CashDesk
END

CLOSE db_cur
DEALLOCATE db_cur

DROP TABLE #tblTransactions
END