CREATE PROCEDURE [dbo].[PostMoveDeleteTransaction] @ATransactionNumberStr VARCHAR(8000), @AUserIdNumber INT, @AMovedFolioNumber INT, @AHotelCustomerNumber INT, @ACashDesk1 INT = 0 AS
BEGIN

/*
  Created by Kent Johansen december 2008
  Notes: 
    If @AMovedFolioNumber > 0 then it's handled as a move to that folio otherwise it is a delete.
    This can not handle reductions
    
  Changes:
    KJ 20100708: Bug: Fix for "Wrong userid on TransactionDeleteNote"
    KJ 20100422: Bug: Fix for "Not able to move transactions"
    KJ 20100421: Bug: Fix for "Delete with more than one pieces wrong"
    KJ 20091207: Ext: Added CashDesk
    KJ 20090223: Bug: CURSOR should be LOCAL
    KJ 20090114: Bug: Fix for "not able to delete payments"
    KJ 20081212: Changed to use TransactionNumber instead of GlobalTransactionNumber
    KJ 20081211: Removed Department as input param. Use the existing from original department
 
*/

  SET NOCOUNT ON

  DECLARE @ShowPrint BIT
  SET @ShowPrint = 0

  DECLARE @TransactionNumber INT
  DECLARE @GlobalTransactionNumber INT
  DECLARE @FolioNumber INT
  DECLARE @TextOverwrite VARCHAR(100)
  DECLARE @Pieces INT
  DECLARE @PriceOverwrite FLOAT
  DECLARE @WareNumber INT
  DECLARE @ProductWareLinkNumber INT
  DECLARE @Correction BIT
  DECLARE @Note VARCHAR(10)
  DECLARE @DummyTransNrShowOnBill INT
  DECLARE @FK_DepartmentNumber INT
  DECLARE @Payment INT

  DECLARE db_cur CURSOR READ_ONLY FAST_FORWARD LOCAL FOR
    SELECT TransactionNumber, GlobalTransactionNumber, FolioNumber, TextOverwrite, Pieces, PriceOverwrite, WareNumber, ProductWareLinkNumber, Note, FK_DepartmentNumber, Payment
    FROM Transactions WITH (NOLOCK)
    WHERE TransactionNumber IN (SELECT DISTINCT Number FROM iter_intlist_to_table_No_Dummy(@ATransactionNumberStr))
    AND ShowOnBillFlag = 1
    AND MovedDeleted = 0
  OPEN db_cur

  FETCH NEXT FROM db_cur INTO @TransactionNumber, @GlobalTransactionNumber, @FolioNumber, @TextOverwrite, @Pieces, @PriceOverwrite, @WareNumber, @ProductWareLinkNumber, @Note, @FK_DepartmentNumber, @Payment
  
  WHILE @@FETCH_STATUS = 0
  BEGIN
    IF @AMovedFolioNumber > 0
    BEGIN
      -- Move
      SET @Pieces = 0
      SET @PriceOverwrite = 0
      SET @Correction = 0
    END
    ELSE
    BEGIN
      --Delete
      SET @Pieces = @Pieces*-1
      SET @Correction = 1
    END
 
    IF @ShowPrint = 1
    BEGIN
      PRINT '*****************************'
      PRINT '@AUserIDNumber'
      PRINT @AUserIDNumber
      PRINT '@FolioNumber'
      PRINT @FolioNumber
      PRINT '@TextOverwrite'
      PRINT @TextOverwrite
      PRINT '@Pieces'
      PRINT @Pieces
      PRINT '@PriceOverwrite'
      PRINT @PriceOverwrite
      PRINT '@AMovedFolioNumber'
      PRINT @AMovedFolioNumber
      PRINT '@FK_DepartmentNumber'
      PRINT @FK_DepartmentNumber
      PRINT '@WareNumber'
      PRINT @WareNumber
      PRINT '@TransactionNumber'
      PRINT @TransactionNumber
      PRINT '@Correction'
      PRINT @Correction
      PRINT '@Note'
      PRINT @Note
      PRINT '@AHotelCustomerNumber'
      PRINT @AHotelCustomerNumber
      PRINT '@ProductWareLinkNumber'
      PRINT @ProductWareLinkNumber
    END

    IF (NOT (@Pieces = 1 OR @Pieces = -1)) AND @Pieces <> 0
      SET @PriceOverwrite = (@PriceOverwrite/@Pieces) * -1

    EXEC Postering NULL, 0, 1, 0, 0, 0, @AUserIDNumber, @FolioNumber, 0, 0, 0, 0, @TextOverwrite, @Pieces, @PriceOverwrite, 0, 0, 0, 0, @AMovedFolioNumber, @FK_DepartmentNumber, @WareNumber, @Payment, 0, @TransactionNumber, @Correction, @Note, @DummyTransNrShowOnBill OUTPUT, @AHotelCustomerNumber, @ProductWareLinkNumber, 0, 0, NULL, NULL, 0, 0, 0, 0, @ACashDesk = @ACashDesk1
    UPDATE TransactionDeleteNote SET FK_TransactionNumber = @DummyTransNrShowOnBill WHERE FK_TransactionNumber = @TransactionNumber

    FETCH NEXT FROM db_cur INTO @TransactionNumber, @GlobalTransactionNumber, @FolioNumber, @TextOverwrite, @Pieces, @PriceOverwrite, @WareNumber, @ProductWareLinkNumber, @Note, @FK_DepartmentNumber, @Payment
  END
END

CLOSE db_cur
DEALLOCATE db_cur