public ActionResult CPLDetailEdit(LeaveCPLEmpBalance obj)
        {
            LeaveCPLEmpBalance dbLeaveCPLBalance = LeaveCPLBalanceService.GetEdit(obj.PLeaveCPLEmpBalanceID);

            //if (obj.RemainingDays < dbLeaveCPLBalance.RemainingDays)
            //    ModelState.AddModelError("RemainingDays", "You can only increase balance");
            //if (ModelState.IsValid)
            //{
            //    if (obj.RemainingDays != dbLeaveCPLBalance.RemainingDays)
            //    {
            //        double? diff = obj.RemainingDays - dbLeaveCPLBalance.RemainingDays;
            //        dbLeaveCPLBalance.RemainingDays = dbLeaveCPLBalance.RemainingDays + diff;
            //        dbLeaveCPLBalance.RemainingDays = obj.RemainingDays;
            //        LeaveCPLBalanceService.PostEdit(dbLeaveCPLBalance);
            //        // Update LeaveQuota

            //        PayrollPeriod dbPayrollPeriod = DDService.GetAllPayrollPeriod().Where(aa => aa.PPayrollPeriodID == dbLeaveCPLBalance.PayrollPeriodID).First();
            //        Expression<Func<LeaveQuotaYear, bool>> SpecificEntries3 = c => c.FinancialYearID == dbPayrollPeriod.FinancialYearID && c.EmployeeID == dbLeaveCPLBalance.EmployeeID && c.LeaveTypeID == 4;
            //        if (LeaveQuotaYearService.GetIndexSpecific(SpecificEntries3).Count > 0)
            //        {
            //            LeaveQuotaYear dbLeaveQuotaYear = LeaveQuotaYearService.GetIndexSpecific(SpecificEntries3).First();
            //            dbLeaveQuotaYear.GrandTotal = dbLeaveQuotaYear.GrandTotal + diff;
            //            dbLeaveQuotaYear.GrandRemaining = dbLeaveQuotaYear.GrandRemaining + diff;
            //            dbLeaveQuotaYear.YearlyTotal = dbLeaveQuotaYear.YearlyTotal + diff;
            //            dbLeaveQuotaYear.YearlyRemaining = dbLeaveQuotaYear.YearlyRemaining + diff;
            //            LeaveQuotaYearService.PostEdit(dbLeaveQuotaYear);
            //        }
            //    }
            //    return Json("OK", JsonRequestBehavior.AllowGet);
            //}
            return(PartialView("CPLDetailEdit", obj));
        }
        public ActionResult CPLCreate(int?id)
        {
            LeaveCPLEmpBalance obj = new LeaveCPLEmpBalance();

            obj.EmployeeID = id;
            CPLHelper(obj);
            return(View(obj));
        }
        public ActionResult CPLCreate(LeaveCPLEmpBalance dbLeaveCPLBalance)
        {
            //if (dbLeaveCPLBalance.RemainingDays==null || dbLeaveCPLBalance.RemainingDays == 0)
            //    ModelState.AddModelError("CPLBalance", "CPL Balance must be greated than 0");
            //if (dbLeaveCPLBalance.ExpireDate ==null)
            //    ModelState.AddModelError("ExpireDate", "Expire Date cannot be null");
            //if (ModelState.IsValid)
            //{
            //    // Get PayrollPeriod
            //    PayrollPeriod dbPayrollPeriod = ATAssistant.GetPayrollPeriodObject(dbLeaveCPLBalance.EntryDateTime.Value, DDService.GetAllPayrollPeriod());
            //    //dbLeaveCPLBalance.Used = 0;
            //    dbLeaveCPLBalance.RemainingDays = dbLeaveCPLBalance.RemainingDays;
            //    dbLeaveCPLBalance.EntryDateTime = dbPayrollPeriod.PREndDate.Value.AddDays(1);
            //    LeaveCPLBalanceService.PostCreate(dbLeaveCPLBalance);
            //    // Update Leave Quota
            //    Expression<Func<LeaveQuotaYear, bool>> SpecificEntries3 = c => c.FinancialYearID == dbPayrollPeriod.FinancialYearID && c.EmployeeID == dbLeaveCPLBalance.EmployeeID && c.LeaveTypeID == 4;
            //    if (LeaveQuotaYearService.GetIndexSpecific(SpecificEntries3).Count > 0)
            //    {
            //        LeaveQuotaYear dbLeaveQuotaYear = LeaveQuotaYearService.GetIndexSpecific(SpecificEntries3).First();
            //        dbLeaveQuotaYear.GrandTotal = dbLeaveQuotaYear.GrandTotal + dbLeaveCPLBalance.CPLBalance;
            //        dbLeaveQuotaYear.GrandRemaining = dbLeaveQuotaYear.GrandRemaining + dbLeaveCPLBalance.CPLBalance;
            //        dbLeaveQuotaYear.YearlyTotal = dbLeaveQuotaYear.YearlyTotal + dbLeaveCPLBalance.CPLBalance;
            //        dbLeaveQuotaYear.YearlyRemaining = dbLeaveQuotaYear.YearlyRemaining + dbLeaveCPLBalance.CPLBalance;
            //        LeaveQuotaYearService.PostEdit(dbLeaveQuotaYear);
            //    }
            //    else
            //    {
            //        LeaveQuotaYear dbLeaveQuotaYear = new LeaveQuotaYear();
            //        dbLeaveQuotaYear.EmployeeID = dbLeaveCPLBalance.EmployeeID;
            //        dbLeaveQuotaYear.FinancialYearID = dbPayrollPeriod.FinancialYearID;
            //        dbLeaveQuotaYear.LeaveTypeID = 4;
            //        dbLeaveQuotaYear.GrandTotal = 0;
            //        dbLeaveQuotaYear.GrandRemaining = 0;
            //        dbLeaveQuotaYear.YearlyTotal = 0;
            //        dbLeaveQuotaYear.YearlyRemaining = 0;
            //        dbLeaveQuotaYear.GrandTotal = dbLeaveCPLBalance.CPLBalance;
            //        dbLeaveQuotaYear.GrandRemaining = dbLeaveCPLBalance.CPLBalance;
            //        dbLeaveQuotaYear.YearlyTotal = dbLeaveCPLBalance.CPLBalance;
            //        dbLeaveQuotaYear.YearlyRemaining = dbLeaveCPLBalance.CPLBalance;
            //        LeaveQuotaYearService.PostCreate(dbLeaveQuotaYear);
            //    }
            //    return Json("OK", JsonRequestBehavior.AllowGet);

            //}
            CPLHelper(dbLeaveCPLBalance);
            return(PartialView("Create", dbLeaveCPLBalance));
        }
        public ActionResult Create(DailyOvertime obj)
        {
            string _EmpNo = Request.Form["EmpNo"].ToString();
            Expression <Func <VHR_EmployeeProfile, bool> > SpecificEntries121 = c => c.OEmpID == _EmpNo;
            VMLoggedUser LoggedInUser       = Session["LoggedInUser"] as VMLoggedUser;
            List <VHR_EmployeeProfile> _emp = vHR_EmployeeProfile.GetIndexSpecific(SpecificEntries121);

            if (obj.OTDate == null || obj.OTDate <= DateTime.Today.AddDays(-200))
            {
                ModelState.AddModelError("OTDate", "OT Date must be valid");
            }
            else
            {
                if (!DDService.IsDateLieBetweenActivePayroll(obj.OTDate.Value))
                {
                    ModelState.AddModelError("OTDate", "Payroll Period is Closed for this date");
                }
            }
            if ((obj.SingleEncashableOT == null && obj.SingleEncashableOT <= 0) && (obj.DoubleEncashbaleOT == null && obj.DoubleEncashbaleOT <= 0) && (obj.CPLOT == null && obj.CPLOT <= 0))
            {
                ModelState.AddModelError("SingleEncashableOT", "Overtime must be valid");
            }
            if (_emp.Count == 0)
            {
                ModelState.AddModelError("OTDate", "Emp No not exist");
            }
            else
            {
                // int? EmpLocID = _emp.First().LocationID;
                //if (LoggedInUser.UserLoctions.Where(aa => aa.LocationID == EmpLocID).Count() == 0)
                //ModelState.AddModelError("OTDate", "You do not have rights to add overtime for this employee");
                int?EmpDepID = _emp.First().OUCommonID;
                if (LoggedInUser.UserDepartments.Where(aa => aa.DepartmentID == EmpDepID).Count() == 0)
                {
                    ModelState.AddModelError("OTDate", "You do not have rights to add overtime for this employee");
                }
                obj.EmployeeID = _emp.First().PEmployeeID;
                Expression <Func <DailyOvertime, bool> > SpecificEntries2 = c => c.EmployeeID == obj.EmployeeID && c.OTDate == obj.OTDate;
                if (DailyOvertimeService.GetIndexSpecific(SpecificEntries2).Count > 0)
                {
                    ModelState.AddModelError("OTDate", "Already have OT for this date");
                }
            }

            if (ModelState.IsValid)
            {
                if (obj.SingleEncashableOT > 0)
                {
                    obj.SingleEncashableOT = obj.SingleEncashableOT * 60;
                }
                if (obj.DoubleEncashbaleOT > 0)
                {
                    obj.DoubleEncashbaleOT = obj.DoubleEncashbaleOT * 60;
                }
                if (obj.CPLOT > 0)
                {
                    obj.CPLOT = obj.CPLOT * 60;
                }
                obj.AddedByUserID = LoggedInUser.PUserID;
                obj.AddedDate     = DateTime.Now;
                DailyOvertimeService.PostCreate(obj);
                // Update Attendance Data
                Expression <Func <DailyAttendance, bool> > SpecificEntries2 = c => c.EmpID == obj.EmployeeID && c.AttDate == obj.OTDate;
                List <DailyAttendance> attDatas = DailyAttendanceService.GetIndexSpecific(SpecificEntries2).ToList();
                if (attDatas.Count > 0)
                {
                    DailyAttendance attdata = attDatas.First();
                    if (obj.SingleEncashableOT > 0)
                    {
                        attdata.ApprovedOT = (short)obj.SingleEncashableOT;
                    }
                    if (obj.DoubleEncashbaleOT > 0)
                    {
                        attdata.ApprovedDoubleOT = (short)obj.DoubleEncashbaleOT;
                    }
                    if (obj.CPLOT > 0)
                    {
                        attdata.ApprovedCPL = (short)obj.CPLOT;
                    }
                    DailyAttendanceService.PostEdit(attdata);
                    DDService.ProcessMonthlyAttendance((DateTime)obj.OTDate, (int)obj.EmployeeID, obj.EmployeeID.ToString());
                    // Update Employee Pool
                    LeaveCPLPool dbLeavePoolCPL = new LeaveCPLPool();
                    Expression <Func <LeaveCPLPool, bool> > SpecificEntries3 = c => c.EmployeeID == obj.EmployeeID;

                    if (LeaveCPLPoolService.GetIndexSpecific(SpecificEntries3).Count() > 0)
                    {
                        dbLeavePoolCPL = LeaveCPLPoolService.GetIndexSpecific(SpecificEntries3).First();
                        dbLeavePoolCPL.LastEntryDateTime = DateTime.Now;
                        dbLeavePoolCPL.RemainingHours    = dbLeavePoolCPL.RemainingHours + (obj.CPLOT / 60);
                        if (dbLeavePoolCPL.RemainingHours == null)
                        {
                            dbLeavePoolCPL.RemainingHours = 0;
                            dbLeavePoolCPL.RemainingHours = (dbLeavePoolCPL.RemainingHours) + (obj.CPLOT / 60);
                        }
                        dbLeavePoolCPL.TotalHours = (dbLeavePoolCPL.TotalHours) + (obj.CPLOT / 60);
                        if (dbLeavePoolCPL.TotalHours == null)
                        {
                            dbLeavePoolCPL.TotalHours = 0;
                            dbLeavePoolCPL.TotalHours = (dbLeavePoolCPL.TotalHours) + (obj.CPLOT / 60);
                        }
                        dbLeavePoolCPL.CPLDays = 0;
                        LeaveCPLPoolService.PostEdit(dbLeavePoolCPL);
                    }
                    else
                    {
                        dbLeavePoolCPL                   = new LeaveCPLPool();
                        dbLeavePoolCPL.EmployeeID        = obj.EmployeeID;
                        dbLeavePoolCPL.LastEntryDateTime = DateTime.Now;
                        dbLeavePoolCPL.RemainingHours    = obj.CPLOT / 60;
                        dbLeavePoolCPL.TotalHours        = obj.CPLOT / 60;
                        dbLeavePoolCPL.CPLDays           = 0;
                        LeaveCPLPoolService.PostCreate(dbLeavePoolCPL);
                    }
                    // Update CPL
                    if (dbLeavePoolCPL.RemainingHours >= 4)
                    {
                        int   total     = (int)(dbLeavePoolCPL.RemainingHours / 4);
                        float remaining = (float)(dbLeavePoolCPL.RemainingHours - (total * 4));
                        float days      = (float)(total / 2.0);
                        // Add into CPL Balance
                        LeaveCPLEmpBalance dbLeaveBalanceCPL = new LeaveCPLEmpBalance();
                        dbLeaveBalanceCPL.EmployeeID    = obj.EmployeeID;
                        dbLeaveBalanceCPL.EntryDateTime = DateTime.Now;
                        dbLeaveBalanceCPL.ExpireDate    = DateTime.Today.AddDays(60);
                        dbLeaveBalanceCPL.IsExpire      = false;
                        dbLeaveBalanceCPL.RemainingDays = days;
                        dbLeaveBalanceCPL.TotalDays     = days;
                        dbLeaveBalanceCPL.Used          = 0;
                        LeaveCPLEmpBalanceService.PostCreate(dbLeaveBalanceCPL);
                        // update CPL Pool
                        dbLeavePoolCPL.RemainingHours = dbLeavePoolCPL.RemainingHours - (days * 8);
                        //dbLeavePoolCPL.RemainingHours = dbLeavePoolCPL.RemainingHours + remaining;
                        LeaveCPLPoolService.PostEdit(dbLeavePoolCPL);
                        // Get PayrollPeriod
                        PayrollPeriod dbPayrollPeriod = ATAssistant.GetPayrollPeriodObject(obj.OTDate.Value, DDService.GetAllPayrollPeriod());
                        // Update Days in Leave Quota
                        LeaveQuotaYear dbLeaveQuotaYear = new LeaveQuotaYear();
                        Expression <Func <LeaveQuotaYear, bool> > SpecificEntries4 = c => c.FinancialYearID == dbPayrollPeriod.FinancialYearID && c.EmployeeID == obj.EmployeeID && c.LeaveTypeID == 4;
                        if (LeaveQuotaYearService.GetIndexSpecific(SpecificEntries4).Count > 0)
                        {
                            dbLeaveQuotaYear                 = LeaveQuotaYearService.GetIndexSpecific(SpecificEntries4).First();
                            dbLeaveQuotaYear.GrandTotal      = dbLeaveQuotaYear.GrandTotal + days;
                            dbLeaveQuotaYear.GrandRemaining  = dbLeaveQuotaYear.GrandRemaining + days;
                            dbLeaveQuotaYear.YearlyTotal     = dbLeaveQuotaYear.YearlyTotal + days;
                            dbLeaveQuotaYear.YearlyRemaining = dbLeaveQuotaYear.YearlyRemaining + days;
                            LeaveQuotaYearService.PostEdit(dbLeaveQuotaYear);
                        }
                        else
                        {
                            dbLeaveQuotaYear.EmployeeID      = obj.EmployeeID;
                            dbLeaveQuotaYear.FinancialYearID = dbPayrollPeriod.FinancialYearID;
                            dbLeaveQuotaYear.LeaveTypeID     = 4;
                            dbLeaveQuotaYear.GrandTotal      = 0;
                            dbLeaveQuotaYear.GrandRemaining  = 0;
                            dbLeaveQuotaYear.YearlyTotal     = 0;
                            dbLeaveQuotaYear.YearlyRemaining = 0;
                            dbLeaveQuotaYear.GrandTotal      = dbLeaveQuotaYear.GrandTotal + days;
                            dbLeaveQuotaYear.GrandRemaining  = dbLeaveQuotaYear.GrandRemaining + days;
                            dbLeaveQuotaYear.YearlyTotal     = dbLeaveQuotaYear.YearlyTotal + days;
                            dbLeaveQuotaYear.YearlyRemaining = dbLeaveQuotaYear.YearlyRemaining + days;
                            LeaveQuotaYearService.PostCreate(dbLeaveQuotaYear);
                        }
                    }
                }
                //DDService.SaveAuditLog(LoggedInUser.PUserID, AuditFormAttendance.Crew, AuditTypeCommon.Add, obj.PDailyOTID, App_Start.AppAssistant.GetClientMachineInfo());
                return(Json("OK", JsonRequestBehavior.AllowGet));
            }
            return(PartialView("Create", obj));
        }
 private void CPLHelper(LeaveCPLEmpBalance leaveCPLBalance)
 {
     //ViewBag.PayrollPeriodID = new SelectList(DDService.GetPayrollPeriod().ToList().OrderBy(aa => aa.PPayrollPeriodID).ToList(), "PPayrollPeriodID", "PRName", leaveCPLBalance.PayrollPeriodID);
 }
Esempio n. 6
0
        public void UpdateLeaveBalance(LeaveApplication lvappl, int PayrollPeriodID)
        {
            try
            {
                float LvDays = (float)lvappl.NoOfDays;
                List <LeaveQuotaYear> _lvConsumed = new List <LeaveQuotaYear>();
                Expression <Func <LeaveQuotaYear, bool> > SpecificEntries = aa => aa.EmployeeID == lvappl.EmpID && aa.FinancialYearID == lvappl.FinancialYearID && aa.LeaveTypeID == lvappl.LeaveTypeID;
                _lvConsumed = LeaveQuotaYearRepo.FindBy(SpecificEntries);
                if (_lvConsumed.Count > 0)
                {
                    if (lvappl.LeaveTypeID == 1)
                    {
                        if (lvappl.IsAccum == true)
                        {
                            _lvConsumed.FirstOrDefault().GrandRemaining = (float)(_lvConsumed.FirstOrDefault().GrandRemaining + LvDays);
                            _lvConsumed.FirstOrDefault().CFRemaining    = (float)(_lvConsumed.FirstOrDefault().CFRemaining + LvDays);
                        }
                        else
                        {
                            if (_lvConsumed.FirstOrDefault().YearlyRemaining >= 0)
                            {
                                _lvConsumed.FirstOrDefault().YearlyRemaining = (float)(_lvConsumed.FirstOrDefault().YearlyRemaining + LvDays);
                            }
                            _lvConsumed.FirstOrDefault().GrandRemaining = (float)(_lvConsumed.FirstOrDefault().GrandRemaining + LvDays);
                            if (_lvConsumed.FirstOrDefault().YearlyRemaining > _lvConsumed.FirstOrDefault().YearlyTotal)
                            {
                                _lvConsumed.FirstOrDefault().CFRemaining     = _lvConsumed.FirstOrDefault().YearlyRemaining - _lvConsumed.FirstOrDefault().YearlyTotal;
                                _lvConsumed.FirstOrDefault().YearlyRemaining = _lvConsumed.FirstOrDefault().YearlyTotal;
                            }
                        }
                    }
                    else if (lvappl.LeaveTypeID == 4)
                    {
                        if (_lvConsumed.FirstOrDefault().YearlyRemaining > 0)
                        {
                            _lvConsumed.FirstOrDefault().YearlyRemaining = (float)(_lvConsumed.FirstOrDefault().YearlyRemaining + LvDays);
                        }
                        _lvConsumed.FirstOrDefault().GrandRemaining = (float)(_lvConsumed.FirstOrDefault().GrandRemaining + LvDays);
                        // Add leave from LeaveCPLBalance
                        Expression <Func <LeaveCPLEmpBalance, bool> > SpecificEntrie3 = aa => aa.EmployeeID == lvappl.EmpID && aa.IsExpire == false;
                        List <LeaveCPLEmpBalance> dbLeaveCPLBalances = LeaveCPLEmpBalanceRepo.FindBy(SpecificEntrie3).OrderBy(aa => aa.PLeaveCPLEmpBalanceID).ToList();
                        if (dbLeaveCPLBalances.Count == 1)
                        {
                            LeaveCPLEmpBalance dbLeaveCPLBalance = dbLeaveCPLBalances.First();
                            dbLeaveCPLBalance.Used          = (float)(dbLeaveCPLBalance.Used - lvappl.NoOfDays);
                            dbLeaveCPLBalance.RemainingDays = (float)(dbLeaveCPLBalance.RemainingDays + lvappl.NoOfDays);
                            LeaveCPLEmpBalanceRepo.Edit(dbLeaveCPLBalance);
                            LeaveCPLEmpBalanceRepo.Save();
                        }
                        else if (dbLeaveCPLBalances.Count > 1)
                        {
                            double?NoOfdays = lvappl.NoOfDays;
                            foreach (var dbLeaveCPLBalance in dbLeaveCPLBalances.OrderByDescending(aa => aa.PLeaveCPLEmpBalanceID).ToList())
                            {
                                if (NoOfdays > 0 && dbLeaveCPLBalance.Used > 0)
                                {
                                    dbLeaveCPLBalance.Used          = (float)(dbLeaveCPLBalance.Used - NoOfdays);
                                    dbLeaveCPLBalance.RemainingDays = (float)(dbLeaveCPLBalance.RemainingDays + NoOfdays);
                                    if (dbLeaveCPLBalance.Used < 0)
                                    {
                                        NoOfdays = -1.0 * dbLeaveCPLBalance.Used;
                                        dbLeaveCPLBalance.Used          = 0;
                                        dbLeaveCPLBalance.RemainingDays = dbLeaveCPLBalance.TotalDays;
                                    }
                                    else
                                    {
                                        NoOfdays = 0;
                                    }
                                    LeaveCPLEmpBalanceRepo.Edit(dbLeaveCPLBalance);
                                    LeaveCPLEmpBalanceRepo.Save();
                                }
                            }
                        }
                    }
                    else
                    {
                        if (_lvConsumed.FirstOrDefault().YearlyRemaining > 0)
                        {
                            _lvConsumed.FirstOrDefault().YearlyRemaining = (float)(_lvConsumed.FirstOrDefault().YearlyRemaining + LvDays);
                        }
                        _lvConsumed.FirstOrDefault().GrandRemaining = (float)(_lvConsumed.FirstOrDefault().GrandRemaining + LvDays);
                    }
                    LeaveQuotaYearRepo.Edit(_lvConsumed.FirstOrDefault());
                    LeaveQuotaYearRepo.Save();
                    LeaveQuotaPeriod atLQp = new LeaveQuotaPeriod();
                    Expression <Func <LeaveQuotaPeriod, bool> > SpecificEntrie2 = aa => aa.EmployeeID == lvappl.EmpID && aa.PayrollPeriodID == PayrollPeriodID && aa.LeaveTypeID == lvappl.LeaveTypeID;

                    if (LeaveQuotaPeriodRepo.FindBy(SpecificEntrie2).Count() > 0)
                    {
                        atLQp = LeaveQuotaPeriodRepo.FindBy(SpecificEntrie2).First();
                    }
                    atLQp.ConsumedDays  = atLQp.ConsumedDays - lvappl.NoOfDays;
                    atLQp.RemainingDays = atLQp.StartNoOfDays - atLQp.ConsumedDays;

                    LeaveQuotaPeriodRepo.Edit(atLQp);
                    LeaveQuotaPeriodRepo.Save();
                }
            }
            catch (Exception)
            {
            }
        }
Esempio n. 7
0
        public bool BalanceLeaves(LeaveApplication lvappl, LeaveType LeaveType, int PayrollPeriodID)
        {
            bool isAccLeave = false;
            List <LeaveQuotaYear> _lvConsumedYear = new List <LeaveQuotaYear>();
            Expression <Func <LeaveQuotaYear, bool> > SpecificEntries = aa => aa.EmployeeID == lvappl.EmpID && aa.FinancialYearID == lvappl.FinancialYearID && aa.LeaveTypeID == lvappl.LeaveTypeID;

            _lvConsumedYear = LeaveQuotaYearRepo.FindBy(SpecificEntries);
            float _NoOfDays = lvappl.NoOfDays;

            if (_lvConsumedYear.Count > 0)
            {
                // For Yearly Leave Quota
                if (lvappl.LeaveTypeID == 1) // AL
                {
                    if (lvappl.IsAccum == true)
                    {
                        _lvConsumedYear.FirstOrDefault().CFRemaining    = (float)(_lvConsumedYear.FirstOrDefault().CFRemaining - _NoOfDays);
                        _lvConsumedYear.FirstOrDefault().GrandRemaining = (float)(_lvConsumedYear.FirstOrDefault().GrandRemaining - _NoOfDays);
                        if (_lvConsumedYear.FirstOrDefault().CFRemaining < 0)
                        {
                            _lvConsumedYear.FirstOrDefault().YearlyRemaining = (float)(_lvConsumedYear.FirstOrDefault().YearlyRemaining + _lvConsumedYear.FirstOrDefault().CFRemaining);
                            //_lvConsumedYear.FirstOrDefault().GrandRemaining = (float)(_lvConsumedYear.FirstOrDefault().GrandRemaining + _lvConsumedYear.FirstOrDefault().CFRemaining);
                            _lvConsumedYear.FirstOrDefault().CFRemaining = 0;
                        }
                    }
                    else
                    {
                        if (_lvConsumedYear.FirstOrDefault().YearlyRemaining > 0)
                        {
                            _lvConsumedYear.FirstOrDefault().YearlyRemaining = (float)(_lvConsumedYear.FirstOrDefault().YearlyRemaining - _NoOfDays);
                        }
                        _lvConsumedYear.FirstOrDefault().GrandRemaining = (float)(_lvConsumedYear.FirstOrDefault().GrandRemaining - _NoOfDays);
                    }
                }
                else if (lvappl.LeaveTypeID == 4) //CPL
                {
                    if (_lvConsumedYear.FirstOrDefault().YearlyRemaining > 0)
                    {
                        _lvConsumedYear.FirstOrDefault().YearlyRemaining = (float)(_lvConsumedYear.FirstOrDefault().YearlyRemaining - _NoOfDays);
                    }
                    _lvConsumedYear.FirstOrDefault().GrandRemaining = (float)(_lvConsumedYear.FirstOrDefault().GrandRemaining - _NoOfDays);
                    // Subtract leave from LeaveCPLBalance
                    Expression <Func <LeaveCPLEmpBalance, bool> > SpecificEntrie3 = aa => aa.EmployeeID == lvappl.EmpID && aa.IsExpire == false && aa.RemainingDays > 0;
                    List <LeaveCPLEmpBalance> dbLeaveCPLBalances = LeaveCPLEmpBalanceRepo.FindBy(SpecificEntrie3).OrderBy(aa => aa.PLeaveCPLEmpBalanceID).ToList();
                    if (dbLeaveCPLBalances.Count == 1)
                    {
                        LeaveCPLEmpBalance dbLeaveCPLBalance = dbLeaveCPLBalances.First();
                        dbLeaveCPLBalance.Used          = (float)(dbLeaveCPLBalance.Used + _NoOfDays);
                        dbLeaveCPLBalance.RemainingDays = (float)(dbLeaveCPLBalance.RemainingDays - _NoOfDays);
                        LeaveCPLEmpBalanceRepo.Edit(dbLeaveCPLBalance);
                        LeaveCPLEmpBalanceRepo.Save();
                    }
                    else if (dbLeaveCPLBalances.Count > 1)
                    {
                        double?NoOfdays = _NoOfDays;
                        foreach (var dbLeaveCPLBalance in dbLeaveCPLBalances)
                        {
                            if (dbLeaveCPLBalance.RemainingDays > 0 && NoOfdays > 0)
                            {
                                dbLeaveCPLBalance.Used          = (float)(dbLeaveCPLBalance.Used + NoOfdays);
                                dbLeaveCPLBalance.RemainingDays = (float)(dbLeaveCPLBalance.RemainingDays - NoOfdays);
                                if (dbLeaveCPLBalance.RemainingDays < 0)
                                {
                                    NoOfdays = -1.0 * dbLeaveCPLBalance.RemainingDays;
                                    dbLeaveCPLBalance.Used          = (float)(dbLeaveCPLBalance.Used - NoOfdays);
                                    dbLeaveCPLBalance.RemainingDays = 0;
                                }
                                else
                                {
                                    NoOfdays = 0;
                                }
                                LeaveCPLEmpBalanceRepo.Edit(dbLeaveCPLBalance);
                                LeaveCPLEmpBalanceRepo.Save();
                            }
                        }
                    }
                }
                else // All others
                {
                    if (_lvConsumedYear.FirstOrDefault().YearlyRemaining > 0)
                    {
                        _lvConsumedYear.FirstOrDefault().YearlyRemaining = (float)(_lvConsumedYear.FirstOrDefault().YearlyRemaining - _NoOfDays);
                    }
                    _lvConsumedYear.FirstOrDefault().GrandRemaining = (float)(_lvConsumedYear.FirstOrDefault().GrandRemaining - _NoOfDays);
                }
                LeaveQuotaYearRepo.Edit(_lvConsumedYear.First());
                LeaveQuotaYearRepo.Save();

                LeaveQuotaPeriod atLQp = new LeaveQuotaPeriod();
                Expression <Func <LeaveQuotaPeriod, bool> > SpecificEntrie2 = aa => aa.EmployeeID == lvappl.EmpID && aa.PayrollPeriodID == PayrollPeriodID && aa.LeaveTypeID == lvappl.LeaveTypeID;

                if (LeaveQuotaPeriodRepo.FindBy(SpecificEntrie2).Count() > 0)
                {
                    atLQp = LeaveQuotaPeriodRepo.FindBy(SpecificEntrie2).First();
                }
                else
                {
                    atLQp.EmployeeID      = lvappl.EmpID;
                    atLQp.LeaveTypeID     = lvappl.LeaveTypeID;
                    atLQp.PayrollPeriodID = PayrollPeriodID;
                    atLQp.ConsumedDays    = 0;
                    atLQp.StartNoOfDays   = _lvConsumedYear.FirstOrDefault().GrandRemaining + _NoOfDays;
                    LeaveQuotaPeriodRepo.Add(atLQp);
                    LeaveQuotaPeriodRepo.Save();
                }
                atLQp = AssistantLeave.AddBalancceMonthQuota(_lvConsumedYear, lvappl, atLQp);
                LeaveQuotaPeriodRepo.Edit(atLQp);
                // Check for Post edit
                LeaveQuotaPeriodRepo.Save();
            }
            return(isAccLeave);
        }