/// <summary>
        /// Frontend page: Advance Unit
        /// Title: Advance more than one unit
        /// Designed: Nadeeka
        /// User story:
        /// Developed: Nadeeka
        /// Date created: 02/25/2016
        /// CreatedBy : Nadeeka
        /// </summary>
        /// <param name="model"></param>
        /// <returns>Return partial view</returns>
        public int UpdateAdvanceAll(ListViewModel list)
        {           
            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 = "";
            //check Session["notAdvancedList"] is not null
            if (Session["notAdvancedList"] != null)
            {
                List<Models.Unit> lstUnit = (List<Models.Unit>)Session["notAdvancedList"];
                foreach (Models.Unit modifiedUnit in list.ItemList)
                {
                    //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 == modifiedUnit.UnitId).AdvanceAmount != modifiedUnit.AdvanceAmount)
                    {
                        TempData["updateReslt"] = 3;
                        return 0;
                    }
                }

            }

            UnitAccess unitAccess = new UnitAccess();
            //get result of advance item list
            int reslt = unitAccess.AdvanceItemList(list.ItemList, loanSetupStep1.loanId, userData.UserId, list.ItemList[0].AdvanceDate);
            TempData["updateReslt"] = reslt;

            // after success save**
            if (reslt == 1)
            {
                string[] arrList = new string[list.ItemList.Count];
                int i = 0;
                foreach (var x in list.ItemList)
                {
                    if (!string.IsNullOrEmpty(x.UnitId))
                    {
                        arrList[i] = "Advanced Unit: " + x.IdentificationNumber + " ,Advance amount:" + x.AdvanceAmount + " ,Advance date: " + x.AdvanceDate;
                        i++;
                    }
                }
                
         
                string units = string.Join(",", arrList);
                //insert log data
                Log log = new Log(userData.UserId, userData.Company_Id, userData.BranchId, loanSetupStep1.loanId, "Advance Unit", units, DateTime.Now);

                int islog = (new LogAccess()).InsertLog(log);
                //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
                    {
                        loanObj = (Loan)Session["oneLoanDashboard"];
                    }
                        //loanObj = (Loan)Session["loanDashboard"]; 
                    if (loanObj.AdvanceFee == 1)
                    {
                        //check advance amount and other details
                        foreach (BankLoanSystem.Models.Unit unitObj in list.ItemList)
                        {
                            unitAccess.insertFreeDetailsForAdvance(unitObj, loanSetupStep1.loanId);
                        }
                    }
                }

                // saving for reporting purpose
                if (Session["AdvItems"] == null)
                {
            Session["AdvItems"] = list.ItemList;
            }
            else
            {
                List<Models.Unit> unitlist = new List<Models.Unit>();
                unitlist = (List<Models.Unit>)Session["AdvItems"];
                unitlist.AddRange(list.ItemList);
                Session["AdvItems"] = unitlist;
            }
            }
            return reslt;        
        }
        /// <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;
        }