CREATE PROCEDURE [dbo].[DebtorExportCleanup] @AUserIdNumber INT AS
SET NOCOUNT ON

/*
  Created by: Kent Johansen Febuary 2009
  Used to delete illegal ledger,vaucher payments. If not invoicebill is on the transaction, then
    it's not a legal transactions, and is deleted. Then the folio is then showed in Invoice plan.
*/

DECLARE @SystemDate CHAR(8)
DECLARE @TranNumber INT
DECLARE @HotelCustomerNumber INT

SELECT @SystemDate = SystemDate FROM System WITH (NOLOCK)

DECLARE db_cur1 CURSOR STATIC READ_ONLY FOR
  SELECT T.TransactionNumber, T.HotelCustomerNumber
  FROM Transactions T WITH (NOLOCK)
  WHERE ISNULL(T.InvoiceBillNumber, 0) <= 0
  AND (CONVERT(CHAR(8), T.DateStamp, 112) = @SystemDate) 
  AND T.ShowOnBillFlag = 1 
  AND T.MovedDeleted = 0 
  AND T.Payment IN (1,2,14)
OPEN db_cur1

FETCH FIRST FROM db_cur1 INTO @TranNumber, @HotelCustomerNumber 

WHILE @@FETCH_STATUS = 0
BEGIN
  EXEC PostMoveDeleteTransaction @TranNumber, @AUserIdNumber, -1, @HotelCustomerNumber

  FETCH NEXT FROM db_cur1 INTO @TranNumber, @HotelCustomerNumber 
END

CLOSE db_cur1
DEALLOCATE db_cur1