public static LiabilityViewModel GetLiabilityViewModelByLiability(Liabilities liability) { LiabilityViewModel result = new LiabilityViewModel(); result.Liability = liability; if (liability.LiabilityType == (int)Constants.Constants.LIABILITY_TYPE.CREDIT_CARD) { double interestPerMonth = liability.InterestRate / 1200; result.MonthlyInterestPayment = liability.Value * interestPerMonth; result.CurrentInterestRate = liability.InterestRate; } else { int totalPeriod = FormatUtility.CalculateTimePeriod(liability.StartDate.Value, liability.EndDate.Value); DateTime current = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1); int currentPeriod = FormatUtility.CalculateTimePeriod(liability.StartDate.Value, current) + 1; double interestPerMonth = liability.InterestRate / 1200; result.TotalPaymentPeriod = totalPeriod; result.MonthlyOriginalPayment = liability.Value / totalPeriod; result.MonthlyInterestPayment = liability.Value * interestPerMonth; result.MonthlyPayment = result.MonthlyInterestPayment + result.MonthlyOriginalPayment; result.CurrentInterestRate = liability.InterestRate; result.RemainedValue = liability.Value - currentPeriod * result.MonthlyOriginalPayment; } return(result); }
public static List <LiabilityPaymentViewModel> CalculatePaymentsByMonth(List <Liabilities> list, Liabilities liability, bool append) { List <LiabilityPaymentViewModel> result = new List <LiabilityPaymentViewModel>(); if (liability.LiabilityType == (int)Constants.Constants.LIABILITY_TYPE.REAL_ESTATE || liability.LiabilityType == (int)Constants.Constants.LIABILITY_TYPE.BUSINESS || liability.LiabilityType == (int)Constants.Constants.LIABILITY_TYPE.STOCK) { list = list.OrderBy(x => x.StartDate).ToList(); Liabilities parentLiability = list.Where(x => !x.ParentLiabilityId.HasValue).FirstOrDefault(); double remainLoan = parentLiability.Value; double numberOfOriginalMonths = (parentLiability.EndDate.Value.Year - parentLiability.StartDate.Value.Year) * 12 + parentLiability.EndDate.Value.Month - parentLiability.StartDate.Value.Month; double monthlyOriginalPayment = parentLiability.Value / numberOfOriginalMonths; DateTime startDate = list.Where(x => x.ParentLiabilityId.HasValue).OrderBy(x => x.StartDate.Value).FirstOrDefault().StartDate.Value; double monthlyTotalOriginalPayment = 0; foreach (var item in list) { if (item.ParentLiabilityId.HasValue) { double interestPerMonth = item.InterestRate / 1200; double numberOfRealMonths = (item.EndDate.Value.Year - item.StartDate.Value.Year) * 12 + item.EndDate.Value.Month - item.StartDate.Value.Month + 1; if (list.IndexOf(item) == (list.Count - 1)) { numberOfRealMonths = numberOfRealMonths - 1; } for (int j = 1; j <= numberOfRealMonths; j++) { LiabilityPaymentViewModel model = new LiabilityPaymentViewModel(); if (append) { if (liability.StartDate <= startDate && startDate <= liability.EndDate) { model.MonthlyOriginalPayment = monthlyOriginalPayment; if (parentLiability.InterestType == (int)Constants.Constants.INTEREST_TYPE.REDUCED) { model.MonthlyInterestPayment = remainLoan * liability.InterestRate / 1200; } else { model.MonthlyInterestPayment = parentLiability.Value * liability.InterestRate / 1200; } model.CurrentInterestRatePerYear = liability.InterestRate; model.CurrentMonth = startDate; model.Highlight = true; } else { model.MonthlyOriginalPayment = monthlyOriginalPayment; if (parentLiability.InterestType == (int)Constants.Constants.INTEREST_TYPE.REDUCED) { model.MonthlyInterestPayment = remainLoan * interestPerMonth; } else { model.MonthlyInterestPayment = parentLiability.Value * interestPerMonth; } model.CurrentInterestRatePerYear = item.InterestRate; model.CurrentMonth = startDate; } } else { model.MonthlyOriginalPayment = monthlyOriginalPayment; if (parentLiability.InterestType == (int)Constants.Constants.INTEREST_TYPE.REDUCED) { model.MonthlyInterestPayment = remainLoan * interestPerMonth; } else { model.MonthlyInterestPayment = parentLiability.Value * interestPerMonth; } model.CurrentInterestRatePerYear = item.InterestRate; model.CurrentMonth = startDate; } model.MonthlyTotalPayment = model.MonthlyOriginalPayment + model.MonthlyInterestPayment; model.RemainingLoan = remainLoan - monthlyOriginalPayment; if (!append) { if (liability.StartDate <= startDate && startDate <= liability.EndDate) { result.Add(model); } } else { result.Add(model); } monthlyTotalOriginalPayment += monthlyOriginalPayment; remainLoan -= monthlyOriginalPayment; startDate = startDate.AddMonths(1); } } } } else { int totalPeriod = FormatUtility.CalculateTimePeriod(liability.StartDate.Value, liability.EndDate.Value); DateTime currentMonth = liability.StartDate.Value; for (int i = 0; i < totalPeriod; i++) { LiabilityPaymentViewModel model = new LiabilityPaymentViewModel { CurrentInterestRatePerYear = liability.InterestRate, CurrentMonth = currentMonth, Highlight = false, MonthlyInterestPayment = liability.Value * liability.InterestRate / 1200, MonthlyOriginalPayment = liability.Value / totalPeriod }; model.MonthlyTotalPayment = model.MonthlyInterestPayment + model.MonthlyOriginalPayment; model.RemainingLoan = liability.Value - (i + 1) * model.MonthlyOriginalPayment; currentMonth = currentMonth.AddMonths(1); result.Add(model); } } return(result); }
public static LiabilityListViewModel GetLiabilityViewModelByAssetViewModel(AssetViewModel assetViewModel) { Entities entities = new Entities(); LiabilityListViewModel result = new LiabilityListViewModel(); IQueryable <Liabilities> liabilities = entities.Liabilities.Where(x => x.AssetId == assetViewModel.Asset.Id && !x.DisabledDate.HasValue); foreach (var liability in liabilities) { IQueryable <Liabilities> list; if (liability.ParentLiabilityId.HasValue) { list = liabilities.Where(x => x.Id == liability.ParentLiabilityId || x.ParentLiabilityId == liability.ParentLiabilityId); } else { list = liabilities.Where(x => x.Id == liability.Id || x.ParentLiabilityId == liability.Id); } LiabilityViewModel liabilityViewModel = new LiabilityViewModel(); DateTime current = DateTime.Now; current = new DateTime(current.Year, current.Month, 1); Liabilities parentLiability = list.Where(x => !x.ParentLiabilityId.HasValue).FirstOrDefault(); liabilityViewModel.Liability = liability; liabilityViewModel.TotalPaymentPeriod = FormatUtility.CalculateTimePeriod(liability.StartDate.Value, liability.EndDate.Value); if (liability.ParentLiabilityId.HasValue) { liabilityViewModel.CurrentInterestRate = liability.InterestRate; } else { liabilityViewModel.CurrentInterestRate = GetCurrentInterestRate(parentLiability.Id); } if (liability.StartDate <= current && current < liability.EndDate) { if (liability.LiabilityType != (int)Constants.Constants.LIABILITY_TYPE.INSURANCE) { liabilityViewModel.MonthlyOriginalPayment = liability.Value / FormatUtility.CalculateTimePeriod(parentLiability.StartDate.Value, parentLiability.EndDate.Value); } else { liabilityViewModel.MonthlyOriginalPayment = 0; } int currentPeriod = FormatUtility.CalculateTimePeriod(parentLiability.StartDate.Value, DateTime.Now); if (currentPeriod >= 0) { if (liability.LiabilityType != (int)Constants.Constants.LIABILITY_TYPE.INSURANCE) { liabilityViewModel.RemainedValue = liability.Value - currentPeriod * liabilityViewModel.MonthlyOriginalPayment; if (parentLiability.InterestType == (int)Constants.Constants.INTEREST_TYPE.REDUCED) { liabilityViewModel.MonthlyInterestPayment = liabilityViewModel.RemainedValue * liabilityViewModel.CurrentInterestRate / 1200; } else { liabilityViewModel.MonthlyInterestPayment = parentLiability.Value * liabilityViewModel.CurrentInterestRate / 1200; } } else { int totalPeriod = FormatUtility.CalculateTimePeriod(parentLiability.StartDate.Value, parentLiability.EndDate.Value); liabilityViewModel.RemainedValue = liability.Value * (totalPeriod - currentPeriod); liabilityViewModel.MonthlyOriginalPayment = liability.Value; } } else { liabilityViewModel.RemainedValue = liability.Value - 1 * liabilityViewModel.MonthlyOriginalPayment; liabilityViewModel.MonthlyInterestPayment = liability.Value; } liabilityViewModel.MonthlyPayment = liabilityViewModel.MonthlyInterestPayment + liabilityViewModel.MonthlyOriginalPayment; liabilityViewModel.AnnualPayment = liabilityViewModel.MonthlyPayment * 12; } else { liabilityViewModel.MonthlyInterestPayment = 0; liabilityViewModel.MonthlyOriginalPayment = 0; liabilityViewModel.MonthlyPayment = 0; liabilityViewModel.AnnualPayment = 0; liabilityViewModel.RemainedValue = 0; } result.List.Add(liabilityViewModel); } return(result); }