public decimal GetBalance( IAccount account, DateTime asOfDate, bool reconciledOnly ) { if( account.IsAssetAccount() ) return account.StartingBalance + this.GetDebits( account, asOfDate, reconciledOnly ) - this.GetCredits( account, asOfDate, reconciledOnly ); else if( account.IsLiabilityAccount() ) return account.StartingBalance + this.GetCredits( account, asOfDate, reconciledOnly ) - this.GetDebits( account, asOfDate, reconciledOnly ); else return 0; // TODO: Should throw error? }
/// <summary> /// Get all potential matching line items for the given record. /// It is assumed the caller will prune down the list even more. /// </summary> /// <param name="account"></param> /// <param name="record"></param> /// <returns></returns> public IEnumerable<ILineItem> GetPotentialMatchingLineItems( IAccount account, IOnlineRecord record ) { TransactionType searchType; if( account.IsAssetAccount() ) { if( record.Amount < 0 ) searchType = TransactionType.Credit; else searchType = TransactionType.Debit; } else if( account.IsLiabilityAccount() ) { if( record.Amount < 0 ) searchType = TransactionType.Debit; else searchType = TransactionType.Credit; } else throw new DataProviderException( "Unsupported account type " + account.AccountType.ToString() ); List<ILineItem> matches = new List<ILineItem>(); foreach( XmlLineItem item in this.lineItems.Values ) { // Ensure is from the right account if( item.AccountId != account.Id ) continue; // Don't match unless it is the right type of transaction. if( item.TransactionType != searchType ) continue; // Never match a reconciled line item if( item.IsReconciled ) continue; // Never match a voided line item if( item.IsVoided ) continue; // Don't match a line item which was already matched to something if( item.OnlineRecordId != null ) continue; // If amounts are the same, it could be a match. if( item.Amount != Math.Abs( record.Amount ) ) continue; // But... // Online transaction must be later than or same date as transaction if( record.Date < item.TransactionDate ) continue; // If passed tests above, add to list of matches. matches.Add( item ); } return matches; }
/// <summary> /// Gets the balance forward of the account, /// as of the start of the given day. /// </summary> /// <param name="account">Account object.</param> /// <param name="asOfDate">Date for which to compute balance. Time is not used.</param> /// <returns></returns> public decimal GetBalanceForward( IAccount account, DateTime asOfDate ) { if( asOfDate < account.StartingDate ) throw new ArgumentOutOfRangeException( "Date must be after account starting date." ); decimal credits = Convert.ToDecimal( ExecuteScalar( "SELECT SUM([Amount]) FROM [{0}]" + "WHERE AccountID={1} AND TransactionType='C' AND TransactionDate<'{2:M/d/yyyy}'", accountTemplate.TableName, account.Id, asOfDate ) ); decimal debits = Convert.ToDecimal( ExecuteScalar( "SELECT SUM([Amount]) FROM [{0}]" + "WHERE AccountID={1} AND TransactionType='D' AND TransactionDate<'{2:M/d/yyyy}'", accountTemplate.TableName, account.Id, asOfDate ) ); if( account.IsAssetAccount() ) return account.StartingBalance + debits - credits; else if( account.IsLiabilityAccount() ) return account.StartingBalance + credits - debits; else throw new ArgumentException( account.Name + " is not a balance account." ); }