Ejemplo n.º 1
0
        public List <LiabilityViewModel> GetLiabilityAccounts()
        {
            var liability = glactRepo.GetByMainCategory(MainGlCategory.Liability);

            var allLiabilityAccounts = new List <LiabilityViewModel>();

            foreach (var account in liability)
            {
                var model = new LiabilityViewModel();
                model.AccountName = account.AccountName;
                model.Amount      = account.AccountBalance;

                allLiabilityAccounts.Add(model);
            }
            //adding customer's savings and loan accounts since they are liabilities to the bank
            var savingsAccounts  = new CustomerAccountRepository().GetByType(AccountType.Savings);
            var savingsLiability = new LiabilityViewModel();

            savingsLiability.AccountName = "Savings Accounts";
            savingsLiability.Amount      = savingsAccounts != null?savingsAccounts.Sum(a => a.AccountBalance) : 0;

            var currentAccounts  = new CustomerAccountRepository().GetByType(AccountType.Current);
            var currentLiability = new LiabilityViewModel();

            currentLiability.AccountName = "Current Accounts";
            currentLiability.Amount      = currentAccounts != null?currentAccounts.Sum(a => a.AccountBalance) : 0;

            allLiabilityAccounts.Add(savingsLiability);
            allLiabilityAccounts.Add(currentLiability);
            return(allLiabilityAccounts);
        }//end method
        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 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);
        }
        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);
        }