CREATE FUNCTION dbo.GetFolioBalance (@FolioNumber INT)
RETURNS FLOAT
AS
/*
    KJ 20080506: Changed @Total to datatype float.
    NB20060118: MovedDeleted added to reduce the error possibility
    NB20051102: Created

    Parameters: @FolioNumber to calculate balance on
    Notes:
*/
BEGIN
  DECLARE @Total FLOAT
  SELECT @Total = SUM(T.PriceOverwrite 
  * CASE WHEN (A.AccountType IN (4, 9)) THEN -1 ELSE 1 END 
  * CASE WHEN (T.Debit = 0) THEN -1 ELSE 1 END) 
  FROM dbo.Transactions T WITH (NOLOCK) 
    JOIN dbo.Accounts A WITH (NOLOCK) ON A.AccountNumber = T.AccountNumber
  WHERE T.FolioNumber= @FolioNumber AND T.ShowOnBillFlag=1 AND T.MovedDeleted=0
  SET @Total = ISNULL(@Total, 0)
  IF @Total < 0.001 AND @Total > -0.001 
    SET @Total = 0

  RETURN @Total
END

