/// <summary>
        /// CreatedBy:Piyumi
        /// CreatedDate:2016/2/23
        /// Get all not advanced unit details by loan id
        /// </summary>
        /// <param name="loanId"></param>
        /// <returns>unitList</returns>
        public List<Unit> GetNotAdvancedUnitDetailsByLoanId(int loanId)
        {

            DataHandler dataHandler = new DataHandler();
            List<object[]> parameterList = new List<object[]>();
            List<Unit> unitList = new List<Unit>();

            parameterList.Add(new object[] { "@loan_id", loanId });
            try
            {
                DataSet dataSet =  dataHandler.GetDataSet("spGetNotAdvancedUnitDetailsByLoanId", parameterList);

                if (dataSet != null && dataSet.Tables.Count != 0 && dataSet.Tables[0].Rows.Count != 0)
                {
                    foreach (DataRow reader in dataSet.Tables[0].Rows)
                    {
                        Unit unit = new Unit();

                        unit.UnitId = reader["unit_id"].ToString();
                        unit.CreatedDate = Convert.ToDateTime(reader["created_date"].ToString());
                        unit.IdentificationNumber = reader["identification_number"].ToString();
                        unit.Year = Convert.ToInt32(reader["year"].ToString());
                        unit.Make = reader["make"].ToString();
                        unit.Model = reader["model"].ToString();
                        unit.Cost = Convert.ToDecimal(reader["cost"].ToString());
                        unit.AdvanceAmount = Convert.ToDecimal(reader["advance_amount"].ToString());
                        unit.IsTitleUploaded = Convert.ToBoolean(reader["IsTitleUploaded"]) ? 1 : 0;

                        unitList.Add(unit);
                    }
                    return unitList;
                }
                else {
                    return unitList;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// CreatedBy:Piyumi
        /// CreatedDate:2016/2/24
        /// 
        /// Advance a selected item
        /// 
        /// 
        /// </summary>
        /// <param name="advanceDate"></param>
        /// <param name="loanId"></param>
        /// <param name="unitObj"></param>
        /// <param name="userId"></param>
        /// <returns>countVal</returns>
        public int AdvanceItem(Unit unitObj, int loanId, int userId, DateTime advanceDate)
        {
            int countVal = 0;

            DataHandler dataHandler = new DataHandler();
            List<object[]> parameterList = new List<object[]>();

            try
            {
                parameterList.Add(new object[] { "@loan_id", loanId });
                parameterList.Add(new object[] { "@user_id", userId });
                parameterList.Add(new object[] { "@advance_date", advanceDate });
                parameterList.Add(new object[] { "@unit_id", unitObj.UnitId });
                parameterList.Add(new object[] { "@advance_amount", unitObj.AdvanceAmount });

                this.GetLoanCurtailmentDetails(loanId, unitObj.UnitId, advanceDate, unitObj.AdvanceAmount, unitObj.Cost);

                countVal = dataHandler.ExecuteSQLWithIntOutPutParam("spAdvanceAllSelectedItems", parameterList);
                parameterList.Clear();

                return countVal;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Insert unit to database
        /// 
        /// CreatedBy:  Kanishka 
        /// CreatedDate:02/24/2016
        /// 
        /// EditedBy: Kasun
        /// EditedDate: 03/09/2016
        /// Add seperate code for units for passing as object list
        /// 
        /// EditedBy: Piyumi
        /// EditedDate: 03/16/2016
        /// add isActive field to parameter list
        /// 
        /// EditedBy: Irfan
        /// EditedDate: 4/25/2016
        /// adding output value IDNumber for logging purpose
        /// 
        /// EditedBy: Kasun
        /// EditedDate: 6/15/2016
        /// Roleback SP for add unit
        /// </summary>
        /// <param name="unit"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public string InsertUnit(Unit unit, int userId ,string loanCode,out string IDNumber)
        {

            DataHandler dataHandler = new DataHandler();
            List<object[]> paramertList = new List<object[]>();

            paramertList.Add(new object[] { "@loan_id", unit.LoanId });
            paramertList.Add(new object[] { "@user_id", userId });
            paramertList.Add(new object[] { "@loan_code", loanCode });
            paramertList.Add(new object[] { "@created_date", DateTime.Now });
            paramertList.Add(new object[] { "@unit_type_id", unit.UnitTypeId });
            if (unit.UnitTypeId == 1)
            {
                paramertList.Add(new object[] { "@identification_number", unit.vehicle.IdentificationNumber });
                paramertList.Add(new object[] { "@year", unit.vehicle.Year });
                paramertList.Add(new object[] { "@make", unit.vehicle.Make });
                paramertList.Add(new object[] { "@model", unit.vehicle.Model });
                paramertList.Add(new object[] { "@color", unit.vehicle.Color });
                paramertList.Add(new object[] { "@trim", unit.vehicle.Trim });
                paramertList.Add(new object[] { "@miles", unit.vehicle.Miles });
                paramertList.Add(new object[] { "@new_or_used", unit.vehicle.NewOrUsed });
                paramertList.Add(new object[] { "@length", unit.Length });
                paramertList.Add(new object[] { "@hitch_style", unit.HitchStyle });
                paramertList.Add(new object[] { "@speed", unit.Speed });
                paramertList.Add(new object[] { "@trailer_id", unit.TrailerId });
                paramertList.Add(new object[] { "@engine_serial", unit.EngineSerial });
                IDNumber = unit.vehicle.IdentificationNumber;
            }
            else if (unit.UnitTypeId == 2)
            {
                paramertList.Add(new object[] { "@identification_number", unit.rv.IdentificationNumber });
                paramertList.Add(new object[] { "@year", unit.rv.Year });
                paramertList.Add(new object[] { "@make", unit.rv.Make });
                paramertList.Add(new object[] { "@model", unit.rv.Model });
                paramertList.Add(new object[] { "@color", unit.Color });
                paramertList.Add(new object[] { "@trim", unit.Trim });
                paramertList.Add(new object[] { "@miles", unit.rv.Miles });
                paramertList.Add(new object[] { "@new_or_used", unit.rv.NewOrUsed });
                paramertList.Add(new object[] { "@length", unit.rv.Length });
                paramertList.Add(new object[] { "@hitch_style", unit.HitchStyle });
                paramertList.Add(new object[] { "@speed", unit.Speed });
                paramertList.Add(new object[] { "@trailer_id", unit.TrailerId });
                paramertList.Add(new object[] { "@engine_serial", unit.EngineSerial });
                IDNumber = unit.rv.IdentificationNumber;

            }
            else if (unit.UnitTypeId == 3)
            {
                paramertList.Add(new object[] { "@identification_number", unit.camper.IdentificationNumber });
                paramertList.Add(new object[] { "@year", unit.camper.Year });
                paramertList.Add(new object[] { "@make", unit.camper.Make });
                paramertList.Add(new object[] { "@model", unit.camper.Model });
                paramertList.Add(new object[] { "@color", unit.Color });
                paramertList.Add(new object[] { "@trim", unit.Trim });
                paramertList.Add(new object[] { "@miles", unit.Miles });
                paramertList.Add(new object[] { "@new_or_used", unit.camper.NewOrUsed });
                paramertList.Add(new object[] { "@length", unit.camper.Length });
                paramertList.Add(new object[] { "@hitch_style", unit.camper.HitchStyle });
                paramertList.Add(new object[] { "@speed", unit.Speed });
                paramertList.Add(new object[] { "@trailer_id", unit.TrailerId });
                paramertList.Add(new object[] { "@engine_serial", unit.EngineSerial });
                IDNumber = unit.camper.IdentificationNumber;
            }
            else if (unit.UnitTypeId == 4)
            {
                paramertList.Add(new object[] { "@identification_number", unit.atv.IdentificationNumber });
                paramertList.Add(new object[] { "@year", unit.atv.Year });
                paramertList.Add(new object[] { "@make", unit.atv.Make });
                paramertList.Add(new object[] { "@model", unit.atv.Model });
                paramertList.Add(new object[] { "@color", unit.Color });
                paramertList.Add(new object[] { "@trim", unit.Trim });
                paramertList.Add(new object[] { "@miles", unit.atv.Miles });
                paramertList.Add(new object[] { "@new_or_used", unit.atv.NewOrUsed });
                paramertList.Add(new object[] { "@length", unit.Length });
                paramertList.Add(new object[] { "@hitch_style", unit.HitchStyle });
                paramertList.Add(new object[] { "@speed", unit.Speed });
                paramertList.Add(new object[] { "@trailer_id", unit.TrailerId });
                paramertList.Add(new object[] { "@engine_serial", unit.EngineSerial });
                IDNumber = unit.atv.IdentificationNumber;
            }
            else if (unit.UnitTypeId == 5)
            {
                paramertList.Add(new object[] { "@identification_number", unit.boat.IdentificationNumber });
                paramertList.Add(new object[] { "@year", unit.boat.Year });
                paramertList.Add(new object[] { "@make", unit.boat.Make });
                paramertList.Add(new object[] { "@model", "" });
                paramertList.Add(new object[] { "@color", unit.Color });
                paramertList.Add(new object[] { "@trim", unit.Trim });
                paramertList.Add(new object[] { "@miles", unit.Miles });
                paramertList.Add(new object[] { "@new_or_used", unit.boat.NewOrUsed });
                paramertList.Add(new object[] { "@length", unit.Length });
                paramertList.Add(new object[] { "@hitch_style", unit.HitchStyle });
                paramertList.Add(new object[] { "@speed", unit.Speed });
                paramertList.Add(new object[] { "@trailer_id", unit.boat.TrailerId });
                paramertList.Add(new object[] { "@engine_serial", unit.boat.EngineSerial });
                IDNumber = unit.boat.IdentificationNumber;
            }
            else if (unit.UnitTypeId == 6)
            {
                paramertList.Add(new object[] { "@identification_number", unit.motorcycle.IdentificationNumber });
                paramertList.Add(new object[] { "@year", unit.motorcycle.Year });
                paramertList.Add(new object[] { "@make", unit.motorcycle.Make });
                paramertList.Add(new object[] { "@model", unit.motorcycle.Model });
                paramertList.Add(new object[] { "@color", unit.motorcycle.Color });
                paramertList.Add(new object[] { "@trim", unit.Trim });
                paramertList.Add(new object[] { "@miles", unit.motorcycle.Miles });
                paramertList.Add(new object[] { "@new_or_used", unit.motorcycle.NewOrUsed });
                paramertList.Add(new object[] { "@length", unit.Length });
                paramertList.Add(new object[] { "@hitch_style", unit.HitchStyle });
                paramertList.Add(new object[] { "@speed", unit.Speed });
                paramertList.Add(new object[] { "@trailer_id", unit.TrailerId });
                paramertList.Add(new object[] { "@engine_serial", unit.EngineSerial });
                IDNumber = unit.motorcycle.IdentificationNumber;
            }
            else if (unit.UnitTypeId == 7)
            {
                paramertList.Add(new object[] { "@identification_number", unit.snowmobile.IdentificationNumber });
                paramertList.Add(new object[] { "@year", unit.snowmobile.Year });
                paramertList.Add(new object[] { "@make", unit.snowmobile.Make });
                paramertList.Add(new object[] { "@model", unit.snowmobile.Model });
                paramertList.Add(new object[] { "@color", unit.Color });
                paramertList.Add(new object[] { "@trim", unit.Trim });
                paramertList.Add(new object[] { "@miles", unit.Miles });
                paramertList.Add(new object[] { "@new_or_used", unit.snowmobile.NewOrUsed });
                paramertList.Add(new object[] { "@length", unit.Length });
                paramertList.Add(new object[] { "@hitch_style", unit.HitchStyle });
                paramertList.Add(new object[] { "@speed", unit.Speed });
                paramertList.Add(new object[] { "@trailer_id", unit.TrailerId });
                paramertList.Add(new object[] { "@engine_serial", unit.EngineSerial });
                IDNumber = unit.snowmobile.IdentificationNumber;
            }
            else if (unit.UnitTypeId == 8)
            {
                paramertList.Add(new object[] { "@identification_number", unit.heavyequipment.SerialNumber });
                paramertList.Add(new object[] { "@year", unit.heavyequipment.Year });
                paramertList.Add(new object[] { "@make", unit.heavyequipment.Make });
                paramertList.Add(new object[] { "@model", "" });
                paramertList.Add(new object[] { "@color", unit.Color });
                paramertList.Add(new object[] { "@trim", unit.Trim });
                paramertList.Add(new object[] { "@miles", unit.Miles });
                paramertList.Add(new object[] { "@new_or_used", unit.NewOrUsed });
                paramertList.Add(new object[] { "@length", unit.Length });
                paramertList.Add(new object[] { "@hitch_style", unit.HitchStyle });
                paramertList.Add(new object[] { "@speed", unit.Speed });
                paramertList.Add(new object[] { "@trailer_id", unit.TrailerId });
                paramertList.Add(new object[] { "@engine_serial", unit.EngineSerial });
                IDNumber = unit.heavyequipment.SerialNumber;
            }
            else
            {
                paramertList.Add(new object[] { "@identification_number", unit.IdentificationNumber });
                paramertList.Add(new object[] { "@year", unit.Year });
                paramertList.Add(new object[] { "@make", unit.Make });
                paramertList.Add(new object[] { "@model", unit.Model });
                paramertList.Add(new object[] { "@color", unit.Color });
                paramertList.Add(new object[] { "@trim", unit.Trim });
                paramertList.Add(new object[] { "@miles", unit.Miles });
                paramertList.Add(new object[] { "@new_or_used", unit.NewOrUsed });
                paramertList.Add(new object[] { "@length", unit.Length });
                paramertList.Add(new object[] { "@hitch_style", unit.HitchStyle });
                paramertList.Add(new object[] { "@speed", unit.Speed });
                paramertList.Add(new object[] { "@trailer_id", unit.TrailerId });
                paramertList.Add(new object[] { "@engine_serial", unit.EngineSerial });
                IDNumber = unit.IdentificationNumber;
            }

            paramertList.Add(new object[] { "@cost", unit.Cost });
            paramertList.Add(new object[] { "@advance_amount", unit.AdvanceAmount });

            if (unit.TitleReceived == "Yes")
            {
                unit.TitleStatus = 1;
            }
            else
            {
                unit.TitleStatus = 0;
            }
            paramertList.Add(new object[] { "@title_status", unit.TitleStatus });
            paramertList.Add(new object[] { "@note", unit.Note });
            paramertList.Add(new object[] { "@add_or_advance", unit.AddAndAdvance });
            paramertList.Add(new object[] { "@is_advanced", unit.IsAdvanced });
            if (unit.IsAdvanced == true)
            {
                unit.UnitStatus = 1;
                paramertList.Add(new object[] { "@advance_date", unit.AdvanceDate });

            }
            else {
                unit.UnitStatus = 0;
                paramertList.Add(new object[] { "@advance_date", null });
            }
            paramertList.Add(new object[] { "@unit_status", unit.UnitStatus });
            paramertList.Add(new object[] { "@is_approved", unit.IsApproved });
            paramertList.Add(new object[] { "@status", unit.Status });

            try
            {
                string val = dataHandler.ExecuteSQLWithStringReturnVal("spInsertUnitDetailsRoleBack", paramertList);


                if (!string.IsNullOrEmpty(val) && unit.AddAndAdvance)
                {
                    unit.UnitId = val;
                    if (this.GetLoanCurtailmentDetails(unit.LoanId, unit.UnitId, unit.AdvanceDate, unit.AdvanceAmount, unit.Cost))
                    {
                        return val;
                    }
                    else
                    {
                        return "";
                    }
                }
                else if (!string.IsNullOrEmpty(val)) {
                    return val;
                }
                else {
                    return "";
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public int insertFreeDetailsForAdvance(Unit unit,int loanID)
        {
            try
            {
                string fee_type = "";
                string fee_due_method = "";
                decimal fee_amount = 0;
                int fee_due_date = 0;
                DateTime fee_billdate = unit.AdvanceDate;
                string v_vin = "", v_year = "", v_model = "", v_make = "";

                DataHandler dataHandler = new DataHandler();

                List<object[]> paramertList1 = new List<object[]>();
                paramertList1.Add(new object[] { "@loan_id", loanID });
                DataSet dataSet = dataHandler.GetDataSet("spGetAdvanceFeeData", paramertList1);

                if (dataSet != null && dataSet.Tables.Count != 0 && dataSet.Tables[0].Rows.Count != 0)
                {
                    foreach (DataRow row in dataSet.Tables[0].Rows)
                    {
                        fee_due_method = row["payment_due_method"].ToString();
                        fee_type = row["advance_fee_calculate_type"].ToString();
                        fee_amount = decimal.Parse(row["advance_fee_amount"].ToString());
                        if (fee_type != "")
                        {
                            if (row["payment_due_date"].ToString() == "EoM")
                            {
                                fee_due_date = DateTime.DaysInMonth(fee_billdate.Year, fee_billdate.Month + 1);
                            }
                            else
                            {
                                fee_due_date = int.Parse(row["payment_due_date"].ToString());
                            }
                        }
                    }
                }
                v_vin = unit.IdentificationNumber;
                v_make = unit.Make;
                v_model = unit.Model;
                v_year = unit.Year.ToString();

                string discription = fee_due_method + "," + v_vin + "," + v_year + "," + v_make + "," + v_model;

                if (fee_due_method == "Time of Advance")
                {
                    List<object[]> paramertList = new List<object[]>();
                    paramertList.Add(new object[] { "@loan_id", loanID });
                    paramertList.Add(new object[] { "@unit_id", unit.UnitId });
                    paramertList.Add(new object[] { "@type", "advanceFee" });
                    paramertList.Add(new object[] { "@description", discription });
                    paramertList.Add(new object[] { "@amount", fee_amount });
                    paramertList.Add(new object[] { "@due_date", unit.AdvanceDate });
                    paramertList.Add(new object[] { "@bill_due_date", unit.AdvanceDate });

                    dataHandler.ExecuteSQL("spInsertAdvanceFee", paramertList);
                }
                else if (fee_due_method == "Once a Month")
                {
                    if (fee_type == "Month")
                    {
                        fee_billdate = fee_billdate.AddMonths(1);
                        fee_billdate = new DateTime(fee_billdate.Year, fee_billdate.Month, fee_due_date);
                    }
                    else if (fee_type == "PayPeriod")
                    {
                        if (fee_billdate.Date.Day > fee_due_date)
                        {
                            fee_billdate = fee_billdate.AddMonths(1);
                            fee_billdate = new DateTime(fee_billdate.Year, fee_billdate.Month, fee_due_date);
                        }
                        else
                        {
                            fee_billdate = new DateTime(fee_billdate.Year, fee_billdate.Month, fee_due_date);
                        }
                    }

                    List<object[]> paramertList = new List<object[]>();
                    paramertList.Add(new object[] { "@loan_id", loanID });
                    paramertList.Add(new object[] { "@unit_id", unit.UnitId });
                    paramertList.Add(new object[] { "@type", "advanceFee" });
                    paramertList.Add(new object[] { "@description", discription });
                    paramertList.Add(new object[] { "@amount", fee_amount });
                    paramertList.Add(new object[] { "@due_date", unit.AdvanceDate });
                    paramertList.Add(new object[] { "@bill_due_date", fee_billdate });

                    dataHandler.ExecuteSQL("spInsertAdvanceFee", paramertList);
                }



                return 0;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        /// <summary>
        /// CreatedBy:Piyumi
        /// CreatedDate: 03/17/2016
        /// get titles list by identification number
        /// </summary>
        /// <param name="unit"></param>
        /// <param name="loanCode"></param>
        /// <returns>true/false</returns>
        public bool UpdateTitle(Unit unit,string loanCode,int userId)
        {
            DataHandler dataHandler = new DataHandler();
            List<object[]> paramertList = new List<object[]>();
            paramertList.Add(new object[] { "@unit_id", unit.UnitId });
            //paramertList.Add(new object[] { "@identification_number", unit.IdentificationNumber });
            //paramertList.Add(new object[] { "@year", unit.Year });
            //paramertList.Add(new object[] { "@make", unit.Make });
            //paramertList.Add(new object[] { "@model", unit.Model });
            paramertList.Add(new object[] { "@title_status", unit.TitleStatus });
            paramertList.Add(new object[] { "@loan_code", loanCode });
            paramertList.Add(new object[] { "@user_id", userId });
            paramertList.Add(new object[] { "@modified_date", DateTime.Now });

            try
            {
                return dataHandler.ExecuteSQL("spUpdateTitleStatus", paramertList) ? true : false;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// CreatedBy:Piyumi
        /// CreatedDate: 03/16/2016
        /// get titles list by identification number
        /// </summary>
        /// <param name="identificationNumber"></param>
        /// <returns>resultList</returns>
        public List<Unit> SearchTitle(string loanCode,string identificationNumber)
        {
            DataHandler dataHandler = new DataHandler();
            List<object[]> paramertList = new List<object[]>();
           
            List<Unit> resultList = new List<Unit>();
            if (!string.IsNullOrEmpty(identificationNumber))
            {
                paramertList.Add(new object[] { "@loan_code", loanCode });
                paramertList.Add(new object[] { "@identification_number", identificationNumber });
                try
                {
                    DataSet dataSet = dataHandler.GetDataSet("spGetTitlesByIdentificationNumber", paramertList);
                    if (dataSet != null && dataSet.Tables.Count != 0)
                    {
                        foreach (DataRow dataRow in dataSet.Tables[0].Rows)
                        {
                            Unit title = new Unit();
                            if(!string.IsNullOrEmpty(dataRow["unit_status"].ToString()) && int.Parse(dataRow["unit_status"].ToString()) == 0)
                            {
                                title.AdvanceDateStr = "";
                            }
                            else
                            {
                                if (!string.IsNullOrEmpty(dataRow["advance_date"].ToString()))
                                {
                                    title.AdvanceDateStr = Convert.ToDateTime(dataRow["advance_date"]).ToString("MM/dd/yyy");
                                }
                            }
                            
                            //else
                            //{
                            //    title.AdvanceDate = "";
                            //}    
                            title.UnitId = dataRow["unit_id"].ToString();
                            title.IdentificationNumber = dataRow["identification_number"].ToString();
                            title.Year = int.Parse(dataRow["year"].ToString());
                            title.Make = dataRow["make"].ToString();
                            title.Model = dataRow["model"].ToString();

                            if (!string.IsNullOrEmpty(dataRow["title_status"].ToString()))
                            {
                                if (int.Parse(dataRow["title_status"].ToString()) == 0)
                                {
                                    title.CurrentTitleStatus = "Not Received";
                                }
                                else if (int.Parse(dataRow["title_status"].ToString()) == 1)
                                {
                                    title.CurrentTitleStatus = "Received";
                                }
                                else if (int.Parse(dataRow["title_status"].ToString()) == 2)
                                {
                                    title.CurrentTitleStatus = "Returned to Dealer";
                                }
                                else if (int.Parse(dataRow["title_status"].ToString()) == 3)
                                {
                                    title.CurrentTitleStatus = "Sent to Bank";
                                }
                            }
                            else
                            {
                                title.CurrentTitleStatus = "";
                            }
                            if (!string.IsNullOrEmpty(dataRow["unit_status"].ToString()))
                            {
                                title.UnitStatus = int.Parse(dataRow["unit_status"].ToString());
                                if (int.Parse(dataRow["unit_status"].ToString()) == 0)
                                {
                                    title.CurrentUnitStatus = "InActive";
                                }
                                else if (int.Parse(dataRow["unit_status"].ToString()) == 1)
                                {
                                    title.CurrentUnitStatus = "Active";
                                }
                                else if (int.Parse(dataRow["unit_status"].ToString()) == 2)
                                {
                                    title.CurrentUnitStatus = "Paid";
                                }
                            }

                            else
                            {
                                title.CurrentUnitStatus = "";
                            }
                            resultList.Add(title);
                        }

                        return resultList;
                    }
                    else
                    {
                        return null;
                    }

                }
                catch (Exception ex)
                {
                    throw ex;
                }

            }
            return null;
        }
        /// <summary>
        /// Frontend page: Advance Unit
        /// Title: Advance one unit
        /// Designed: Nadeeka
        /// User story:
        /// Developed: Nadeeka
        /// Date created: 02/24/2016
        /// </summary>
        /// <param name="model"></param>
        /// <returns>Return partial view</returns>
        public int UpdateAdvance(BankLoanSystem.Models.Unit unit)
        {
            string loanCode;

            try
            {
                //convert session to string variable
                loanCode = Session["loanCode"].ToString();
            }
            catch (Exception)
            {
                throw;
            }
            //get loan details by loan code
            LoanSetupStep1 loanSetupStep1 = (new LoanSetupAccess()).GetLoanDetailsByLoanCode(loanCode);

            ViewBag.ErrorMsg = "";
            UnitAccess unitAccess = new UnitAccess();

            //check Session["notAdvancedList"] is not null
            if (Session["notAdvancedList"] != null)
            {
                //convert Session["notAdvancedList"] to list object
                List <Models.Unit> lstUnit = (List <Models.Unit>)Session["notAdvancedList"];
                //check advance amount can not be edited and given advance amount in loanset up and advance amount of selected unit is equal
                if (!loanSetupStep1.isEditAllowable && lstUnit.Find(a => a.UnitId == unit.UnitId).AdvanceAmount != unit.AdvanceAmount)
                {
                    TempData["updateReslt"] = 3;
                    return(3);
                }
            }
            //get result of advance one item
            int reslt = unitAccess.AdvanceItem(unit, loanSetupStep1.loanId, userData.UserId, unit.AdvanceDate);

            TempData["updateReslt"] = reslt;

            // after success save**
            //check result of advance
            if (reslt == 1)
            {
                //if mention advance fee, then insert in to fee table - asanka
                if ((Session["loanDashboard"] != null) || (Session["oneLoanDashboard"] != null))
                {
                    Loan loanObj = new Loan();
                    if (Session["loanDashboard"] != null)
                    {
                        loanObj = (Loan)Session["loanDashboard"];
                    }
                    else if (Session["oneLoanDashboard"] != null)
                    {
                        loanObj = (Loan)Session["oneLoanDashboard"];
                    }
                    if (loanObj.AdvanceFee == 1)
                    {
                        //check advance amount and other details
                        unitAccess.insertFreeDetailsForAdvance(unit, loanSetupStep1.loanId);
                    }
                }

                //insert to log

                Log log = new Log(userData.UserId, userData.Company_Id, userData.BranchId, loanSetupStep1.loanId, "Advance Unit", "Advanced Unit:" + unit.IdentificationNumber + (unit.Cost * loanSetupStep1.advancePercentage / 100 != unit.AdvanceAmount ? ",Advance amount edited to: " + unit.AdvanceAmount : ",Advance amount: " + unit.AdvanceAmount) + " ,Advance date:" + unit.AdvanceDate, DateTime.Now);

                int islog = (new LogAccess()).InsertLog(log);
                // saving for reporting purpose
                if (Session["AdvItems"] == null)
                {
                    List <Models.Unit> unitlist = new List <Models.Unit>();
                    unitlist.Add(unit);
                    Session["AdvItems"] = unitlist;
                }
                else
                {
                    List <Models.Unit> unitlist = new List <Models.Unit>();
                    unitlist = (List <Models.Unit>)Session["AdvItems"];
                    unitlist.Add(unit);
                    Session["AdvItems"] = unitlist;
                }
            }


            return(reslt);
        }
        /*

            Frontend page: Report viewr(Report Page)
            Title: Get unit details by title status
            Designed: Kanishk SHM
            User story: 
            Developed: Kanishk SHM
            Date created: 

        */
        public List<Unit> GeUnitDetailsByTitleStatus(int loanId, int titleStatus)
        {

            List<Unit> units = new List<Unit>();

            DataHandler dataHandler = new DataHandler();
            List<object[]> paramertList = new List<object[]>();
            paramertList.Add(new object[] { "@loan_id", loanId });
            paramertList.Add(new object[] { "@title_status", titleStatus });

            DataSet dataSet = dataHandler.GetDataSet("spGetUnitsByLoanIdTitleStatus", paramertList);

            if (dataSet != null && dataSet.Tables.Count != 0)
            {
                foreach (DataRow dataRow in dataSet.Tables[0].Rows)
                {
                    Unit unit = new Unit();

                    unit.IdentificationNumber = dataRow["identification_number"].ToString();
                    unit.Year = Convert.ToInt32(dataRow["year"]);
                    unit.Make = dataRow["make"].ToString();
                    unit.Model = dataRow["model"].ToString();
                    unit.AdvanceDate = Convert.ToDateTime(dataRow["advance_date"].ToString());
                    unit.AdvanceDateStr = unit.AdvanceDate.ToString("MM/dd/yyyy")
                    ;
                    unit.TitleStatus = Convert.ToInt32(dataRow["title_status"]);

                    if (unit.TitleStatus == 0)
                    {
                        unit.TitleStatusText = "Not Received";
                    }
                    else if (unit.TitleStatus == 1)
                    {
                        unit.TitleStatusText = "Received";
                    }
                    else if (unit.TitleStatus == 2)
                    {
                        unit.TitleStatusText = "Returned to Dealer";
                    }

                    unit.AdvanceAmount = (dataRow["advance_amount"]) != DBNull.Value ? (Decimal)dataRow["advance_amount"] : (Decimal)0.00;
                    unit.IsAdvanced = Convert.ToBoolean(dataRow["is_advanced"]);
                    unit.CreatedDate = Convert.ToDateTime(dataRow["created_date"].ToString());

                    units.Add(unit);

                }
            }

            return units;

        }