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(Transactions.PriceOverwrite * (CASE WHEN (Accounts.AccountType IN (4, 9)) THEN -1 ELSE 1 END)*  (CASE WHEN (Transactions.Debit = 0) THEN -1 ELSE 1 END)) 
  FROM Transactions WITH (NOLOCK) 
    INNER JOIN Accounts WITH (NOLOCK) ON Accounts.AccountNumber = Transactions.AccountNumber 
  WHERE FolioNumber= @FolioNumber AND ShowOnBillFlag=1 AND MovedDeleted=0
  IF @Total < 0.001 AND @Total > -0.001 
    SET @Total = 0

  RETURN @Total
END

