public static LiabilityListViewModel GetLiabilityByUser(string username, int type)
        {
            Entities           entities    = new Entities();
            List <Liabilities> queryResult = (from liability in entities.Liabilities
                                              where liability.Username.Equals(username) && liability.LiabilityType == type &&
                                              !liability.DisabledDate.HasValue
                                              select liability).ToList();
            List <LiabilityViewModel> list = new List <LiabilityViewModel>();

            foreach (var liability in queryResult)
            {
                LiabilityViewModel model = GetLiabilityViewModelByLiability(liability);
                list.Add(model);
            }

            LiabilityListViewModel result = new LiabilityListViewModel
            {
                List = list,
                Type = type,
                TotalOriginalValue          = list.Select(x => x.Liability.OriginalValue.HasValue ? x.Liability.OriginalValue.Value : 0).Sum(),
                TotalLiabilityValue         = list.Select(x => x.Liability.Value).DefaultIfEmpty(0).Sum(),
                TotalMonthlyInterestPayment = list.Select(x => x.MonthlyInterestPayment).DefaultIfEmpty(0).Sum(),
                TotalMonthlyOriginalPayment = list.Select(x => x.MonthlyOriginalPayment).DefaultIfEmpty(0).Sum(),
                TotalMonthlyPayment         = list.Select(x => x.MonthlyPayment).DefaultIfEmpty(0).Sum(),
                RemainedValue = list.Select(x => x.RemainedValue).DefaultIfEmpty(0).Sum()
            };

            result.AverageInterestRate = result.TotalMonthlyInterestPayment / result.TotalLiabilityValue * 1200;
            return(result);
        }
        // GET: Liability
        public ActionResult LiabilityTable(int type)
        {
            LiabilityListViewModel model = LiabilityQueries.GetLiabilityByUser(UserQueries.GetCurrentUsername(), type);

            return(View(model));
        }
        public ActionResult _Report(int type)
        {
            LiabilityListViewModel model = LiabilityQueries.GetLiabilityByUser(UserQueries.GetCurrentUsername(), type);

            return(PartialView(model));
        }
        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);
        }