CREATE PROCEDURE DBO.RollHoleFolioBack @FolioNumber int AS
SET NOCOUNT ON
/*

  PERHAPS CHANGES IS NEEDED REGARDING CASE #CR032520 AND ProductWareLinkNumber

  Changes:
    KJ 20070521: Department rewrite.
*/

Declare @WareTextOverwrite varchar(150)
Declare @Pieces int
Declare @Price Money
Declare @DateStamp DateTime
DECLARE @FK_DepartmentNumber INT
Declare @UserIDNumber int
Declare @GlobalTransactionNumber int
Declare @CountryNumber int
Declare @TransNrShowOnBillOnAddTemp int
Declare @PaxAdult smallint
Declare @PaxChild1 smallint
Declare @PaxChild2 smallint
Declare @Payment smallint
Declare @WareNumber int
Declare @Note varchar(50)

SET @CountryNumber = 1

Declare #TransCur Cursor SCROLL Read_Only For
  SELECT FK_DepartmentNumber, UserIDNumber, PaxAdult, PaxChild1, PaxChild2, GlobalTransactionNumber, DateStamp, FolioNumber, Pieces, PriceOverwrite, TextOverwrite, WareNumber, Payment, Transactions.Note 
  FROM Transactions WITH (NOLOCK)
    LEFT JOIN dbo.InvoiceTransactionXtab ITX WITH (NOLOCK) ON ITX.FkTransaction = Transactions.TransactionNumber
    LEFT JOIN dbo.Invoice I WITH (NOLOCK) ON I.PkInvoice = ITX.FkInvoice AND I.CancelDate IS NULL
  WHERE ((FolioNumber= @FolioNumber) AND (MovedDeleted=0) AND ShowOnBillFlag=1 AND (I.PkInvoice IS NULL OR NOT I.CancelDate IS NULL)) Order by GlobalTransactionNumber, TransactionNumber

   Open #TransCur

   Fetch Next From #TransCur Into @FK_DepartmentNumber, @UserIDNumber, @PaxAdult, @PaxChild1, @PaxChild2, @GlobalTransactionNumber, @DateStamp, @FolioNumber, @Pieces, @Price, @WareTextOverwrite, @WareNumber, @Payment, @Note

   WHILE (@@FETCH_STATUS = 0) 
   Begin
     SET @Pieces = -1*@Pieces
     exec Postering @DateStamp, 0, 0, 0, 0, @UserIDNumber, @FolioNumber, 0, 0, 0,@WareTextOverwrite, @Pieces, @Price, @CountryNumber, @PaxAdult, @PaxChild1, @PaxChild2, 0, @FK_DepartmentNumber, @WareNumber, @Payment, @GlobalTransactionNumber,0, 1, @Note, @TransNrShowOnBillOnAddTemp OUTPUT

     Fetch Next From #TransCur Into @FK_DepartmentNumber, @UserIDNumber, @PaxAdult, @PaxChild1, @PaxChild2, @GlobalTransactionNumber,@DateStamp, @FolioNumber, @Pieces, @Price, @WareTextOverwrite, @WareNumber, @Payment, @Note
   end

Close #TransCur
Deallocate  #TransCur

