public ActionResult AddCurtailment(List<Curtailment> curtailmentList, CurtailmentModel curtaiulmentModel)
        {
            //calculate payment percentage
            if (userData.RoleId >= 3)
            {
                return RedirectToAction("UserLogin", "Login", new { lbl = "You are not Allowed." });
            }

            CurtailmentAccess curtailmentAccess = new CurtailmentAccess();
            StepAccess sa = new StepAccess();
            if (curtailmentAccess.InsertCurtailment(curtailmentList, loanData.loanId) > 0)
            {
                ViewBag.SuccessMsg = "Curtailment Details added successfully";
                sa.UpdateLoanSetupStep(userData.UserId,loanData.CompanyId, loanData.BranchId, loanData.nonRegisteredBranchId, loanData.loanId, 6);
               
                    Log log = new Log(userData.UserId, userData.Company_Id, loanData.BranchId, loanData.loanId, "Curtailment", "Inserted curtailment details of loan : " + loanData.loanId, DateTime.Now);

                    int islog = (new LogAccess()).InsertLog(log);

                ViewBag.Redirect = 1;
            }
            else
            {
                sa.UpdateLoanSetupStep(userData.UserId,loanData.CompanyId, loanData.BranchId, loanData.nonRegisteredBranchId, loanData.loanId, 6);
                
                    Log log = new Log(userData.UserId, userData.Company_Id, loanData.BranchId, loanData.loanId, "Curtailment", "Edited curtailment details of loan : " + loanData.loanId, DateTime.Now);

                    int islog = (new LogAccess()).InsertLog(log);
               
                ViewBag.SuccessMsg = "Curtailment Details updated successfully";
            }

            //bool loanActive = curtaiulmentModel.LoanStatus == "Yes";

            LoanSetupAccess loanAccess = new LoanSetupAccess();
            loanAccess.UpdateLoanCurtailment(curtaiulmentModel, loanData.loanId);
            TempData["LoanId"] = loanData.loanId;
            return RedirectToAction("Step10", new { lbl = "Details added successfully" });
        }
        //internal void getSelectedUnitTypes(int loanId, LoanSetupStep1 loanSetupStep1)
        //{
        //    DataHandler dataHandler = new DataHandler();
        //    List<object[]> paramertList = new List<object[]>();
        //    paramertList.Add(new object[] { "@loan_id", loanId });

        //    try
        //    {

        //    }
        //    catch
        //    {

        //    }

        //    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AutoDealersConnection"].ConnectionString))
        //    {
        //        try
        //        {
        //            using (SqlCommand cmd = new SqlCommand("spGetLoanUnitTypesByLoanId", con))
        //            {
        //                cmd.CommandType = CommandType.StoredProcedure;
        //                cmd.Parameters.Add("@loan_id", SqlDbType.Int).Value = loanId;
        //                con.Open();
        //                SqlDataReader reader = cmd.ExecuteReader();

                        
                        

        //                while (reader.Read())
        //                {
        //                    UnitType unitType = new UnitType();

        //                    unitType.unitTypeId = int.Parse(reader["unit_type_id"].ToString());
        //                    //loanSetupStep1.selectedUnitTypes.Add(unitType);







        //                }

        //                reader.Close();




                       

        //            }
        //        }
        //        catch (Exception ex)
        //        {
        //            throw ex;

        //        }
        //        finally
        //        {
        //            con.Close();
        //        }

        //    }

        //}

        internal int UpdateLoanCurtailment(CurtailmentModel curtailmentModel, int loanId)
        {
            DataHandler dataHandler = new DataHandler();
            List<object[]> paramertList = new List<object[]>();

            bool loanStatus = curtailmentModel.LoanStatus == "Yes" ? true : false;
            char calMode = curtailmentModel.CalculationBase == "Full payment" ? 'f' : 'a';
            char timeBase = curtailmentModel.TimeBase == "Months" ? 'm' : 'd';

            paramertList.Add(new object[] { "@loan_id", loanId });
            paramertList.Add(new object[] { "@loan_status", loanStatus });
            paramertList.Add(new object[] { "@curtailment_due_date", curtailmentModel.DueDate });
            paramertList.Add(new object[] { "@curtailment_auto_remind_email", curtailmentModel.AutoRemindEmail});
            paramertList.Add(new object[] { "@curtailment_remind_period", curtailmentModel.EmailRemindPeriod });
            paramertList.Add(new object[] { "@curtailment_calculation_type", calMode });
            paramertList.Add(new object[] { "@pay_off_type", timeBase });

            try
            {
                return dataHandler.ExecuteSQLReturn("spUpdateLoanCurtailmentd", paramertList);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public ActionResult Step10(string lbl)
        {
            CurtailmentModel curtailment = new CurtailmentModel();

            int userId = userData.UserId;
            if (userData.RoleId >= 3)
            {
                return RedirectToAction("UserLogin", "Login", new { lbl = "You are not Allowed." });
            }

            //check user step is valid for this step
            StepAccess sa = new StepAccess();
            if (loanData.stepId == 5)
            {
                ViewBag.LoanId = 0;
                if (lbl == "Details added successfully")
                {
                    ViewBag.SuccessMsg = "Loan setup is completed";
                    Session["loanStep"] = null;
                    if (TempData["LoanId"] != null && (int)TempData["LoanId"] > 0) {
                        ViewBag.LoanId = (int)TempData["LoanId"];
                    }
                   
                    if (HttpContext.Request.IsAjaxRequest())
                    {
                        ViewBag.AjaxRequest = 1;
                        return PartialView(curtailment);
                    }
                    return View(curtailment);
                }

                int branchId = loanData.BranchId;

                LoanSetupAccess la = new LoanSetupAccess();
                int loanId = loanData.loanId;
                CurtailmentAccess curAccess = new CurtailmentAccess();
                _loan = curAccess.GetLoanDetailsByLoanId(loanId);
                _loan.loanId = loanId;
                
                curtailment.AdvancePt = _loan.advancePercentage;
                curtailment.RemainingPercentage = curtailment.AdvancePt;

                curtailment.InfoModel = new List<Curtailment>();

                var curtailments = curAccess.retreiveCurtailmentByLoanId(loanId);

                int payPercentage = _loan.advancePercentage;
                int? totalPercentage = 0;

                int curId = 0;
                if (curtailments != null && curtailments.Count > 0)
                {
                    for (int i = 0; i < curtailments.Count; i++)
                    {
                        curId++;
                        totalPercentage += curtailments[i].Percentage;
                        curtailment.InfoModel.Add(new Curtailment { CurtailmentId = curId, TimePeriod = curtailments[i].TimePeriod, Percentage = curtailments[i].Percentage });
                    }
                    curtailment.LoanStatus = _loan.LoanStatus ? "Yes" : "No";

                    curtailment.CalculationBase = _loan.CurtailmentCalculationBase == "a" ? "Advance" : "Full payment";
                    curtailment.DueDate = _loan.CurtailmentDueDate;
                    curtailment.AutoRemindEmail = _loan.CurtailmentAutoRemindEmail;
                    curtailment.EmailRemindPeriod = _loan.CurtailmentEmailRemindPeriod;
                }

                ViewBag.CalMode = "Full Payment";
                curtailment.RemainingPercentage = payPercentage - totalPercentage;

                if (curtailment.RemainingPercentage > 0)
                    curtailment.InfoModel.Add(new Curtailment { CurtailmentId = curId + 1 });
                ViewData["objmodel"] = curtailment;

                if (HttpContext.Request.IsAjaxRequest())
                {
                    ViewBag.AjaxRequest = 1;
                    return PartialView(curtailment);
                }
                else
                {

                    return View(curtailment);
                }
            }
            return RedirectToAction("UserLogin", "Login", new { lbl = "Due to inactivity your session has timed out, please log in again." });
        }