public ActionResult _Create(StockCreateViewModel model)
        {
            StockLiabilityListCreateViewModel liabilities = (StockLiabilityListCreateViewModel)HttpContext.Session["LIABILITIES"];
            double totalLiabilityValue = 0;

            if (liabilities != null)
            {
                totalLiabilityValue = liabilities.Liabilities.Sum(x => x.Value.HasValue ? x.Value.Value : 0);
            }

            if (model.StockValue < totalLiabilityValue && totalLiabilityValue > 0)
            {
                ModelState.AddModelError("CompareStockValueAndLiabilityValue", "Giá trị tổng số nợ không vượt quá giá trị cổ phiếu");
            }

            if (StockQueries.CheckExistStock(UserQueries.GetCurrentUsername(), model.Name))
            {
                ModelState.AddModelError("DuplicateName", "Cổ phiếu này đã tồn tại");
            }

            if (ModelState.IsValid)
            {
                HttpContext.Session["STOCK"] = model;
                return(Content("success"));
            }
            else
            {
                return(PartialView("_StockForm", model));
            }
        }
        public ActionResult DeleteTempLiability(int id)
        {
            StockLiabilityListCreateViewModel liabilities = (StockLiabilityListCreateViewModel)HttpContext.Session["LIABILITIES"];

            liabilities.Liabilities.Remove(liabilities.Liabilities.Where(x => x.Id == id).FirstOrDefault());
            return(Content("success"));
        }
        public ActionResult _LiabilityTable()
        {
            StockLiabilityListCreateViewModel liabilities = (StockLiabilityListCreateViewModel)HttpContext.Session["LIABILITIES"];
            StockLiabilityListViewModel       model       = new StockLiabilityListViewModel();

            if (liabilities == null)
            {
                liabilities = new StockLiabilityListCreateViewModel();
            }
            foreach (var liability in liabilities.Liabilities)
            {
                StockLiabilityViewModel viewModel = new StockLiabilityViewModel();
                viewModel.Id               = liability.Id;
                viewModel.Source           = liability.Source;
                viewModel.Value            = liability.Value;
                viewModel.InterestRate     = liability.InterestRate / 100;
                viewModel.InterestRatePerX = StockLiabilityQueries.Helper.GetInterestTypePerX(liability.InterestRatePerX);
                viewModel.StartDate        = liability.StartDate;
                viewModel.EndDate          = liability.EndDate;
                viewModel.InterestType     = StockLiabilityQueries.Helper.GetInterestType(liability.InterestType);
                viewModel.PaymentPeriod    = StockLiabilityQueries.Helper.CalculateTimePeriod(viewModel.StartDate.Value, viewModel.EndDate.Value);
                model.Liabilities.Add(viewModel);
            }
            return(PartialView(model));
        }
        public ActionResult _LiabilityUpdateForm(StockLiabilityCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                StockLiabilityListCreateViewModel liabilities = (StockLiabilityListCreateViewModel)HttpContext.Session["LIABILITIES"];
                StockLiabilityCreateViewModel     updateModel = liabilities.Liabilities.Where(x => x.Id == model.Id).FirstOrDefault();

                updateModel.Value = model.Value;

                StockCreateViewModel stock = (StockCreateViewModel)HttpContext.Session["STOCK"];
                double totalLiabilityValue = liabilities != null?liabilities.Liabilities.Sum(x => x.Value.HasValue?x.Value.Value : 0) : 0;

                if (stock.StockValue < totalLiabilityValue && totalLiabilityValue > 0)
                {
                    ModelState.AddModelError("CompareStockValueAndLiabilityValue", "Giá trị tổng số nợ không vượt quá giá trị cổ phiếu");
                    return(PartialView(model));
                }
                else
                {
                    updateModel.Id                     = model.Id;
                    updateModel.Source                 = model.Source;
                    updateModel.InterestType           = model.InterestType;
                    updateModel.InterestRate           = model.InterestRate;
                    updateModel.StartDate              = model.StartDate;
                    updateModel.EndDate                = model.EndDate;
                    HttpContext.Session["LIABILITIES"] = liabilities;
                    return(Content("success"));
                }
            }
            else
            {
                return(PartialView(model));
            }
        }
        public ActionResult _LiabilityUpdateForm(int id)
        {
            StockLiabilityListCreateViewModel liabilities = (StockLiabilityListCreateViewModel)HttpContext.Session["LIABILITIES"];
            StockLiabilityCreateViewModel     model       = liabilities.Liabilities.Where(x => x.Id == id).FirstOrDefault();

            return(PartialView(model));
        }
        public ActionResult _LiabilityForm(StockLiabilityCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                int id = 1;
                StockLiabilityListCreateViewModel liabilities = (StockLiabilityListCreateViewModel)HttpContext.Session["LIABILITIES"];
                StockCreateViewModel stock = (StockCreateViewModel)HttpContext.Session["STOCK"];
                double totalLiabilityValue = liabilities != null?liabilities.Liabilities.Sum(x => x.Value.HasValue?x.Value.Value : 0) : 0;

                if (stock.StockValue < totalLiabilityValue + model.Value && totalLiabilityValue + model.Value > 0)
                {
                    ModelState.AddModelError("CompareStockValueAndLiabilityValue", "Giá trị tổng số nợ không vượt quá giá trị cổ phiếu");
                    return(PartialView(model));
                }
                else
                {
                    if (liabilities == null)
                    {
                        liabilities = new StockLiabilityListCreateViewModel();
                    }
                    else
                    {
                        if (liabilities.Liabilities.Count > 0)
                        {
                            id = liabilities.Liabilities.Max(x => x.Id) + 1;
                        }
                        else
                        {
                            id = 1;
                        }
                    }
                    model.Id = id;
                    liabilities.Liabilities.Add(model);
                    HttpContext.Session["LIABILITIES"] = liabilities;
                    return(Content("success"));
                }
            }
            else
            {
                return(PartialView(model));
            }
        }
        public ActionResult _Confirm()
        {
            DateTime current = DateTime.Now;

            StockCreateViewModel model     = (StockCreateViewModel)HttpContext.Session["STOCK"];
            StockViewModel       viewModel = new StockViewModel();

            viewModel.Name = model.Name;
            viewModel.Note = model.Note;
            viewModel.Transactions.Transactions.Add(new StockTransactionViewModel
            {
                NumberOfStock    = model.NumberOfStock,
                SpotRice         = model.SpotRice,
                StockValue       = model.StockValue,
                ExpectedDividend = model.ExpectedDividend / 100,
            });

            StockLiabilityListCreateViewModel liabilities = (StockLiabilityListCreateViewModel)HttpContext.Session["LIABILITIES"];

            if (liabilities != null && liabilities.Liabilities.Count > 0)
            {
                foreach (var liability in liabilities.Liabilities)
                {
                    StockLiabilityViewModel liabilityViewModel = new StockLiabilityViewModel();
                    liabilityViewModel.Source           = liability.Source;
                    liabilityViewModel.Value            = liability.Value;
                    liabilityViewModel.InterestType     = StockLiabilityQueries.Helper.GetInterestType(liability.InterestType);
                    liabilityViewModel.InterestRatePerX = StockLiabilityQueries.Helper.GetInterestTypePerX(liability.InterestRatePerX);
                    liabilityViewModel.InterestRate     = liability.InterestRate / 100;
                    liabilityViewModel.StartDate        = liability.StartDate.Value;
                    liabilityViewModel.EndDate          = liability.EndDate.Value;
                    liabilityViewModel.PaymentPeriod    = StockLiabilityQueries.Helper.CalculateTimePeriod(liabilityViewModel.StartDate.Value, liabilityViewModel.EndDate.Value);

                    if (liabilityViewModel.StartDate <= current && current <= liabilityViewModel.EndDate)
                    {
                        int    currentPeriod = Helper.CalculateTimePeriod(liabilityViewModel.StartDate.Value, DateTime.Now);
                        double interestRate  = liability.InterestRatePerX == (int)Constants.Constants.INTEREST_RATE_PER.MONTH ? liability.InterestRate.Value / 100 : liability.InterestRate.Value / 1200;
                        liabilityViewModel.OriginalInterestPayment = liabilityViewModel.Value.Value * interestRate;
                        //Fixed interest type
                        if (liability.InterestType == (int)Constants.Constants.INTEREST_TYPE.FIXED)
                        {
                            liabilityViewModel.MonthlyOriginalPayment = liabilityViewModel.Value.Value / liabilityViewModel.PaymentPeriod;
                            liabilityViewModel.MonthlyInterestPayment = liabilityViewModel.Value.Value * interestRate;
                            liabilityViewModel.TotalMonthlyPayment    = liabilityViewModel.MonthlyOriginalPayment + liabilityViewModel.MonthlyInterestPayment;
                            liabilityViewModel.TotalPayment           = RealEstateLiabilityQueries.Helper.CalculateAnnualPayment(liability);
                            liabilityViewModel.RemainedValue          = liabilityViewModel.Value.Value - liabilityViewModel.MonthlyOriginalPayment * (currentPeriod + 1);
                            liabilityViewModel.Status     = "Đang nợ";
                            liabilityViewModel.StatusCode = "label-success";
                        }
                        //Reduced interest type
                        else
                        {
                            liabilityViewModel.MonthlyOriginalPayment = liabilityViewModel.Value.Value / liabilityViewModel.PaymentPeriod;
                            liabilityViewModel.RemainedValue          = liabilityViewModel.Value.Value - liabilityViewModel.MonthlyOriginalPayment * (currentPeriod + 1);
                            liabilityViewModel.MonthlyInterestPayment = (liabilityViewModel.Value.Value - liabilityViewModel.MonthlyOriginalPayment * currentPeriod) * interestRate;
                            liabilityViewModel.TotalMonthlyPayment    = liabilityViewModel.MonthlyOriginalPayment + liabilityViewModel.MonthlyInterestPayment;
                            liabilityViewModel.TotalPayment           = RealEstateLiabilityQueries.Helper.CalculateAnnualPayment(liability);
                            liabilityViewModel.Status     = "Đang nợ";
                            liabilityViewModel.StatusCode = "label-success";
                        }
                    }
                    else
                    {
                        liabilityViewModel.MonthlyOriginalPayment = 0;
                        liabilityViewModel.MonthlyInterestPayment = 0;
                        liabilityViewModel.TotalMonthlyPayment    = 0;
                        liabilityViewModel.TotalPayment           = 0;
                        liabilityViewModel.RemainedValue          = 0;
                    }

                    viewModel.Transactions.Transactions.FirstOrDefault().Liabilities.Liabilities.Add(liabilityViewModel);
                }

                var lbts = viewModel.Transactions.Transactions.Select(x => x.Liabilities.Liabilities.Where(y => y.StartDate <= current && y.EndDate >= current));
                viewModel.TotalLiabilityValue  = lbts.Sum(x => x.Sum(y => y.Value.HasValue ? y.Value.Value : 0));
                viewModel.TotalOriginalPayment = lbts.Sum(x => x.Sum(y => y.MonthlyOriginalPayment));
                viewModel.TotalInterestPayment = lbts.Sum(x => x.Sum(y => y.MonthlyInterestPayment));
                viewModel.TotalMonthlyPayment  = lbts.Sum(x => x.Sum(y => y.TotalMonthlyPayment));
                viewModel.TotalPayment         = lbts.Sum(x => x.Sum(y => y.TotalPayment));
                viewModel.TotalRemainedValue   = lbts.Sum(x => x.Sum(y => y.RemainedValue));
                viewModel.TotalInterestRate    = viewModel.TotalLiabilityValue > 0 ? lbts.Sum(x => x.Sum(y => y.OriginalInterestPayment)) / viewModel.TotalLiabilityValue * 12 : 0;
                viewModel.RowSpan = viewModel.Transactions.Transactions.Any() ? viewModel.Transactions.Transactions.Count() + viewModel.Transactions.Transactions.Select(x => x.Liabilities.Liabilities).Count() + 4 : 4;

                if (viewModel.Transactions.Transactions.Any())
                {
                    viewModel.RowSpan = 4;
                    bool flag = false;
                    foreach (var transaction in viewModel.Transactions.Transactions)
                    {
                        if (transaction.Liabilities.Liabilities.Count() > 0)
                        {
                            if (flag == false)
                            {
                                flag = true;
                            }
                            viewModel.RowSpan += transaction.Liabilities.Liabilities.Count();
                        }
                    }
                    if (flag == true)
                    {
                        viewModel.RowSpan += 1;
                    }
                }
                else
                {
                    viewModel.RowSpan = 4;
                }
            }

            return(PartialView(viewModel));
        }