public static void CalculatePercentageOfInterest(decimal runningBalance, LoanPaymentAggregation l) { if (runningBalance == 0) { l.Percentage = 0; } else { l.Percentage = ((l.Interest * 12) / runningBalance) * 100; } }
public static decimal CalculateTheBalances(MyMoney money, Account account, ObservableCollection <LoanPaymentAggregation> loanPayementsView) { decimal runningBalance = 0; foreach (LoanPaymentAggregation l in loanPayementsView) { //------------------------------------------------------------- // Check to see if we need to re-calculate // the Principal & Interest amounts using the Percentage // if (l.Principal == 0 && l.Interest == 0 && l.Percentage != 0) { // // Recalculate the Interest using the Percentage // l.Interest = runningBalance * (l.Percentage / 100) / 12; // and the Principal if we know the total original Payment l.Principal = l.Payment - l.Interest; } if (l.Interest != 0 && runningBalance != 0) { // Reverse calculation of the interest rate LoanPaymentAggregation.CalculatePercentageOfInterest(runningBalance, l); } // Reduce the debt by the amount of the Principal paid // // -1 will reverse the amount if this is a mortgage it will change this to -900 * -1 == 900 // runningBalance += l.Principal * -1; // Snap shot the balance due at each payment l.Balance = runningBalance; } return(runningBalance); }
private static LoanPaymentAggregation AddPaymentIfMatchingCategoriesForPrincipalOrInterest( ObservableCollection <LoanPaymentAggregation> view, Category category, Category categoryPrincipal, Category categoryInterest, Transaction t, Split s, // Could be null if this is a basic single transaction with no split, Account transactionAccount, DateTime date, decimal amount ) { if (categoryPrincipal == null || categoryInterest == null) { return(null); } bool matchedThePrincipalCategory = false; bool matchedTheInterestCategory = false; decimal principal = 0; decimal interest = 0; if (categoryPrincipal.Contains(category)) { principal = amount; matchedThePrincipalCategory = true; } if (categoryInterest.Contains(category)) { interest = amount; matchedTheInterestCategory = true; } if (matchedThePrincipalCategory == false && matchedTheInterestCategory == false) { // No match to either return(null); } LoanPaymentAggregation lp = GetLastEntry(view); if (lp != null && lp.Transaction == t) { // This is from the same transaction split entry // we will attempt to merge the PRINCIPAL and the INTEREST onto the same View Loan Entry } else { // New view projection lp = new LoanPaymentAggregation(); lp.Transaction = t; lp.Payment = TransactionAmountMinusAllOtherUnrelatedSplits(t, categoryPrincipal, categoryInterest); lp.Account = transactionAccount; lp.Date = date; lp.Category = category; view.Add(lp); } if (matchedThePrincipalCategory) { lp.Principal = principal; } if (matchedTheInterestCategory) { lp.Interest = interest; } if (s != null) { if (matchedThePrincipalCategory) { lp.SplitForPrincipal = s; } if (matchedTheInterestCategory) { lp.SplitForInterest = s; } } return(lp); }
/// <summary> /// Loans are built from 2 Categories - Principal + Interested /// </summary> /// <param name="toDate"></param> static public ObservableCollection <LoanPaymentAggregation> GetLoanPayementsAggregation(MyMoney money, Account a) { ObservableCollection <LoanPaymentAggregation> view = new ObservableCollection <LoanPaymentAggregation>(); Category categoryPrincipal = a.CategoryForPrincipal; Category categoryInterest = a.CategoryForInterest; //----------------------------------------------------------------- // Get the loan transaction related to the 2 categories set // to this account. Category for Principal & Category for Interest // foreach (Transaction t in money.Transactions.GetAllTransactions()) { if (t.Splits != null && t.Splits.Count > 0) { foreach (Split s in t.Splits) { if (s.Category != null) { AddPaymentIfMatchingCategoriesForPrincipalOrInterest(view, s.Category, categoryPrincipal, categoryInterest, t, s, t.Account, t.Date, s.Amount); } } } else { if (t.Category != null) { AddPaymentIfMatchingCategoriesForPrincipalOrInterest(view, t.Category, categoryPrincipal, categoryInterest, t, null, t.Account, t.Date, t.Amount); } } } //----------------------------------------------------------------- // Additional manual entry made for this Loan // foreach (LoanPayment l in money.LoanPayments) { if (!l.IsDeleted) { if (l.AccountId == a.Id) { LoanPaymentAggregation lp = new LoanPaymentAggregation(); lp.Account = a; lp.Date = l.Date; lp.Principal = l.Principal; lp.Interest = l.Interest; lp.Payment = l.Principal + l.Interest; lp.LoanPayementManualEntry = l; view.Add(lp); } } } //----------------------------------------------------------------- // Sort and recalculate the running balance for all the payment made // to this Loan // var sorted = from item in view orderby item.Date ascending select item; ObservableCollection <LoanPaymentAggregation> list = new ObservableCollection <LoanPaymentAggregation>(sorted); // cannot carry a credit on a loan account, so if the balance is > 0 make it 0. a.Balance = CalculateTheBalances(money, a, list); return(list); }