CREATE PROCEDURE dbo.PostMissingTransactionTotal @AFolioNumber INT AS
SET NOCOUNT ON
/*
Changes:
  KJ 20110816: Ext: New ProductWareLinkNumber when move and delete transactions 
*/

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(150)
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 @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 @NewProductWareLinkNumber INT
DECLARE @CurProductWareLinkNumber INT
DECLARE @OrgDepositfollowupNumber INT
DECLARE @OrgAutoReceivedPayment BIT
DECLARE @OrgHotelCustomerNumber INT
DECLARE @OrgChargedWithCard BIT
DECLARE @OrgOriginalGlobalTransactionNumber 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
 
SELECT TransactionNumber, DateStamp, UserIDNumber, FolioNumber, Pieces, PriceOverwrite, CurrencyNumber, ShowOnBillFlag, PAXadult, PAXchild1, PAXchild2, TextOverwrite, ExchangeRateOverwrite,
  FK_DepartmentNumber, Debit, VATValue, MovedDeleted, MovedFolioNr, WareNumber, Payment, AccountNumber, GlobalTransactionNumber,
  ProductNumber, PackageNumber, RealDateTimeforTransaction, Note, GuestNumber, OriginalDate, ProductWareLinkNumber, DepositfollowupNumber, AutoReceivedPayment,
  HotelCustomerNumber, ChargedWithCard, OriginalGlobalTransactionNumber
INTO #tblTransactions FROM Transactions WITH (NOLOCK)
WHERE FolioNumber IN (@AFolioNumber)
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, AccountNumber, GlobalTransactionNumber,
    ProductNumber, PackageNumber, RealDateTimeforTransaction, Note, GuestNumber, OriginalDate, ProductWareLinkNumber, DepositfollowupNumber, AutoReceivedPayment,
    HotelCustomerNumber, ChargedWithCard, OriginalGlobalTransactionNumber
  FROM #tblTransactions WITH (NOLOCK)
  ORDER BY ProductWareLinkNumber, GlobalTransactionNumber
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, @OrgAccountNumber, @OrgGlobalTransactionNumber, @OrgProductNumber, @OrgPackageNumber, @OrgRealDateTimeforTransaction,
  @OrgNote, @OrgGuestNumber, @OrgOriginalDate, @OrgProductWareLinkNumber, @OrgDepositfollowupNumber, @OrgAutoReceivedPayment, @OrgHotelCustomerNumber, @OrgChargedWithCard, @OrgOriginalGlobalTransactionNumber
 
SET @CurProductWareLinkNumber = 0

WHILE @@FETCH_STATUS = 0
BEGIN
  -- insert new trans with new DateStamp, UserIdNumber, Pieces*-1, Debit, RealDateTimeForTransaction
  IF @OrgDebit = 1
    SET @NewDebit = 0
  ELSE
    SET @NewDebit = 1
    
  IF @CurProductWareLinkNumber <> @OrgProductWareLinkNumber AND ISNULL(@OrgProductWareLinkNumber, 0) > 0 
    EXEC GetNextNumber 'PRODUCTWARELINKIDENTITY', 1, @NewProductWareLinkNumber OUTPUT
  ELSE IF ISNULL(@OrgProductWareLinkNumber, 0) = 0
    SET @NewProductWareLinkNumber = @OrgProductWareLinkNumber
 
  INSERT INTO Transactions (DateStamp, UserIDNumber, FolioNumber, Pieces, PriceOverwrite, CurrencyNumber, ShowOnBillFlag, PAXadult, PAXchild1, PAXchild2,
    TextOverwrite, ExchangeRateOverwrite, FK_DepartmentNumber, Debit, VATValue, MovedDeleted, MovedFolioNr, WareNumber, Payment, AccountNumber,
    GlobalTransactionNumber, ProductNumber, PackageNumber, RealDateTimeforTransaction, Note, GuestNumber, OriginalDate, ProductWareLinkNumber, DepositfollowupNumber, 
    AutoReceivedPayment, HotelCustomerNumber, ChargedWithCard, OriginalGlobalTransactionNumber) VALUES 
    (@CurrentDateTime, @TechotelUser, @OrgFolioNumber, @OrgPieces*-1, @OrgPriceOverwrite, @OrgCurrencyNumber, @OrgShowOnBillFlag, @OrgPaxAdult, @OrgPaxChild1, @OrgPaxChild2,
    @OrgTextOverwrite, @OrgExchangeRateOverwrite, @FK_OrgDepartmentOverwrite, @NewDebit, @OrgVatValue, @OrgMovedDeleted, @OrgMovedFolioNr, @OrgWareNumber, @OrgPayment, @OrgAccountNumber,
    @OrgGlobalTransactionNumber, @OrgProductNumber, @OrgPackageNumber, @CurrentDateTime, @OrgNote, @OrgGuestNumber, @OrgOriginalDate, @NewProductWareLinkNumber, @OrgDepositFollowupNumber,
    @OrgAutoReceivedPayment, @OrgHotelCustomerNumber, @OrgChargedWithCard, @OrgOriginalGlobalTransactionNumber)
 
  SET @CurProductWareLinkNumber = @OrgProductWareLinkNumber
  
  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, @OrgAccountNumber, @OrgGlobalTransactionNumber, @OrgProductNumber, @OrgPackageNumber, @OrgRealDateTimeforTransaction,
    @OrgNote, @OrgGuestNumber, @OrgOriginalDate, @OrgProductWareLinkNumber, @OrgDepositfollowupNumber, @OrgAutoReceivedPayment, @OrgHotelCustomerNumber, @OrgChargedWithCard, @OrgOriginalGlobalTransactionNumber
END
 
CLOSE db_cur
DEALLOCATE db_cur
 
DROP TABLE #tblTransactions