public ActionResult Create4(VMOvertimeApproval vmOvertimeApproval, int?[] SelectedEmpIds)
        {
            VMLoggedUser LoggedInUser = Session["LoggedInUser"] as VMLoggedUser;
            List <VMOvertimeApprovalChild> vmOvertimeApprovalChildEntries = new List <VMOvertimeApprovalChild>();

            foreach (var empID in SelectedEmpIds)
            {
                VMOvertimeApprovalChild vmOvertimeApprovalChild = new VMOvertimeApprovalChild();
                string EncashableSingleOT = Request.Form["ENCS-" + empID.Value.ToString()].ToString();
                string EncashableDoubleOT = Request.Form["ENCD-" + empID.Value.ToString()].ToString();
                //string AbsentDays = Request.Form["ABDays-" + empID.Value.ToString()].ToString();
                //string CPLHours = Request.Form["CPLH-" + empID.Value.ToString()].ToString();
                string CPLHours = "0";
                vmOvertimeApprovalChild.EmpID = (int)empID;
                vmOvertimeApprovalChild.EncashableSingleOT = Convert.ToInt32(EncashableSingleOT);
                vmOvertimeApprovalChild.EncashableDoubleOT = Convert.ToInt32(EncashableDoubleOT);
                vmOvertimeApprovalChild.CPLConvertedOT     = Convert.ToInt32(CPLHours);
                //vmOvertimeApprovalChild.Absents = Convert.ToInt32(AbsentDays);
                vmOvertimeApprovalChildEntries.Add(vmOvertimeApprovalChild);
            }
            vmOvertimeApproval.OvertimeApprovalChild = vmOvertimeApprovalChildEntries.ToList();
            VMOvertimeApproval vm = OvertimeAprrovalService.GetCreate4(vmOvertimeApproval, LoggedInUser);

            //if (vm.ErrorMessages.Count == 0)
            return(RedirectToAction("Index"));
            //else
            //    return View("Create3", vm);
        }
        public ActionResult Create3(VMOvertimeApprovalSelection es, int?[] SelectedEmpIds)
        {
            VMLoggedUser LoggedInUser = Session["LoggedInUser"] as VMLoggedUser;
            //H HR Admin //U   HR Normal
            VMOvertimeApproval vmOvertimeApproval = new VMOvertimeApproval();

            vmOvertimeApproval        = OvertimeAprrovalService.GetCreate3(es, SelectedEmpIds, vmOvertimeApproval);
            ViewBag.SubmittedToUserID = ViewBag.LineManagerID = new SelectList(AppAssistant.GetLineManagers(DDService.GetUser().Where(aa => aa.UserRoleID == "H").ToList()), "PUserID", "UserName", LoggedInUser.LineManagerID);
            ViewBag.OTStatusID        = new SelectList(DDService.GetMonthOTStage().Where(aa => aa.PMonthDataOTStageID == "H" || aa.PMonthDataOTStageID == "A"), "PMonthDataOTStageID", "MonthDataOTStageName", "H");
            return(View(vmOvertimeApproval));
        }
        public VMOvertimeApproval GetCreate4(VMOvertimeApproval vm, VMLoggedUser LoggedInUser)
        {
            PayrollPeriod payrollPeriod = DDService.GetPayrollPeriod().First(aa => aa.PPayrollPeriodID == vm.PayrollPeriodID); // Get selected Payroll Period
            Expression <Func <MonthData, bool> > SpecificEntries = c => c.PayrollPeriodID == payrollPeriod.PPayrollPeriodID;
            List <MonthData> monthDatas = MonthDataReporsitory.FindBy(SpecificEntries);

            // Disable EF Validations
            OTApprovedHistoryReporsitory.ToggleEFValidations(false);
            MonthDataReporsitory.ToggleEFValidations(false);
            LeaveCPLBalanceRepository.ToggleEFValidations(false);
            LeaveQuotaYearRepository.ToggleEFValidations(false);
            foreach (var otChild in vm.OvertimeApprovalChild)
            {
                try
                {
                    if (monthDatas.Where(aa => aa.EmployeeID == otChild.EmpID).Count() > 0)
                    {
                        MonthData monthData = new MonthData();
                        monthData = monthDatas.First(aa => aa.EmployeeID == otChild.EmpID);
                        // Save History
                        OTApprovedHistory dbOTApprovedHistory = new OTApprovedHistory();
                        dbOTApprovedHistory.CreatedDateTime = DateTime.Now;
                        dbOTApprovedHistory.EmpID           = monthData.EmployeeID;
                        dbOTApprovedHistory.MonthDataID     = (int)monthData.PMonthDataID;
                        dbOTApprovedHistory.NewCPLConverted = (short)(otChild.CPLConvertedOT * 60);
                        dbOTApprovedHistory.NewSingleOT     = (short)(otChild.EncashableSingleOT * 60);
                        dbOTApprovedHistory.NewDoubleOT     = (short)(otChild.EncashableDoubleOT * 60);
                        dbOTApprovedHistory.OldCPLConverted = monthData.CPLConversionOT;
                        dbOTApprovedHistory.OldSingleOT     = monthData.EncashbaleSingleOT;
                        dbOTApprovedHistory.OldDoubleOT     = monthData.EncashbaleDoubleOT;
                        dbOTApprovedHistory.SubmittedByID   = LoggedInUser.PUserID;
                        dbOTApprovedHistory.SubmittedToID   = vm.SubmittedToUserID;
                        if (vm.OTStatusID == "A")
                        {
                            dbOTApprovedHistory.SubmittedToID = null;
                        }
                        dbOTApprovedHistory.OTStageID = vm.OTStatusID;
                        OTApprovedHistoryReporsitory.Add(dbOTApprovedHistory);
                        OTApprovedHistoryReporsitory.Save();
                        // Update Monthly Attendance
                        monthData.CPLConversionOT    = (short)(otChild.CPLConvertedOT * 60);
                        monthData.EncashbaleSingleOT = (short)(otChild.EncashableSingleOT * 60);
                        monthData.EncashbaleDoubleOT = (short)(otChild.EncashableDoubleOT * 60);
                        monthData.CPLConvertedDays   = GetCPLDaysFromHours(otChild.CPLConvertedOT);
                        if (LoggedInUser.UserRoleID == "H")// HR Admin
                        {
                            if (vm.OTStatusID == "A")
                            {
                                monthData.MonthDataStageID  = "A";
                                monthData.SubmittedToUserID = null;
                                monthData.SubmittedByUserID = LoggedInUser.PUserID;
                            }
                            else
                            {
                                monthData.SubmittedToUserID = vm.SubmittedToUserID;
                                monthData.SubmittedByUserID = LoggedInUser.PUserID;
                                monthData.MonthDataStageID  = "H";
                            }
                        }
                        else if (LoggedInUser.UserRoleID == "U")// HR Normal
                        {
                            monthData.SubmittedToUserID = vm.SubmittedToUserID;
                            monthData.SubmittedByUserID = LoggedInUser.PUserID;
                            monthData.MonthDataStageID  = "H";
                        }
                        MonthDataReporsitory.Edit(monthData);
                        MonthDataReporsitory.Save();

                        if (LoggedInUser.UserRoleID == "H")// HR Admin
                        {
                            if (vm.OTStatusID == "A")
                            {
                                //// Update Days in LeaveCPLBalance table
                                //LeaveCPLEmpBalance dbLeaveCPLBalance = new LeaveCPLEmpBalance();
                                //Expression<Func<LeaveCPLEmpBalance, bool>> SpecificEntries2 = c => c.PayrollPeriodID == payrollPeriod.PPayrollPeriodID && c.EmployeeID == monthData.EmployeeID;
                                //if (LeaveCPLBalanceRepository.FindBy(SpecificEntries2).Count > 0)
                                //{
                                //    dbLeaveCPLBalance = LeaveCPLBalanceRepository.FindBy(SpecificEntries2).First();
                                //    dbLeaveCPLBalance.CPLBalance = monthData.CPLConvertedDays;
                                //    dbLeaveCPLBalance.RemainingBalance = dbLeaveCPLBalance.CPLBalance;
                                //    dbLeaveCPLBalance.Used = 0;
                                //    LeaveCPLBalanceRepository.Edit(dbLeaveCPLBalance);
                                //}
                                //else
                                //{
                                //    dbLeaveCPLBalance.CPLBalance = monthData.CPLConvertedDays;
                                //    dbLeaveCPLBalance.EmployeeID = monthData.EmployeeID;
                                //    dbLeaveCPLBalance.ExpireDate = payrollPeriod.PREndDate.Value.AddDays(60);
                                //    dbLeaveCPLBalance.IsExpire = false;
                                //    dbLeaveCPLBalance.PayrollPeriodID = payrollPeriod.PPayrollPeriodID;
                                //    dbLeaveCPLBalance.RemainingBalance = dbLeaveCPLBalance.CPLBalance;
                                //    dbLeaveCPLBalance.StartDate = payrollPeriod.PREndDate.Value.AddDays(1);
                                //    dbLeaveCPLBalance.Used = 0;
                                //    LeaveCPLBalanceRepository.Add(dbLeaveCPLBalance);
                                //}
                                //LeaveCPLBalanceRepository.Save();

                                //if (monthData.IsCPLAdded == null || monthData.IsCPLAdded == false)
                                //{
                                //    // Update Days in Leave Quota
                                //    LeaveQuotaYear dbLeaveQuotaYear = new LeaveQuotaYear();
                                //    Expression<Func<LeaveQuotaYear, bool>> SpecificEntries3 = c => c.FinancialYearID == payrollPeriod.FinancialYearID && c.EmployeeID == monthData.EmployeeID && c.LeaveTypeID == 4;
                                //    if (LeaveQuotaYearRepository.FindBy(SpecificEntries3).Count > 0)
                                //    {
                                //        dbLeaveQuotaYear = LeaveQuotaYearRepository.FindBy(SpecificEntries3).First();
                                //        dbLeaveQuotaYear.GrandTotal = dbLeaveQuotaYear.GrandTotal + monthData.CPLConvertedDays;
                                //        dbLeaveQuotaYear.GrandRemaining = dbLeaveQuotaYear.GrandRemaining + monthData.CPLConvertedDays;
                                //        dbLeaveQuotaYear.YearlyTotal = dbLeaveQuotaYear.YearlyTotal + monthData.CPLConvertedDays;
                                //        dbLeaveQuotaYear.YearlyRemaining = dbLeaveQuotaYear.YearlyRemaining + monthData.CPLConvertedDays;
                                //        LeaveQuotaYearRepository.Edit(dbLeaveQuotaYear);
                                //    }
                                //    else
                                //    {
                                //        dbLeaveQuotaYear.EmployeeID = monthData.EmployeeID;
                                //        dbLeaveQuotaYear.FinancialYearID = payrollPeriod.FinancialYearID;
                                //        dbLeaveQuotaYear.LeaveTypeID = 4;
                                //        dbLeaveQuotaYear.GrandTotal = 0;
                                //        dbLeaveQuotaYear.GrandRemaining = 0;
                                //        dbLeaveQuotaYear.YearlyTotal = 0;
                                //        dbLeaveQuotaYear.YearlyRemaining = 0;
                                //        dbLeaveQuotaYear.GrandTotal = dbLeaveQuotaYear.GrandTotal + monthData.CPLConvertedDays;
                                //        dbLeaveQuotaYear.GrandRemaining = dbLeaveQuotaYear.GrandRemaining + monthData.CPLConvertedDays;
                                //        dbLeaveQuotaYear.YearlyTotal = dbLeaveQuotaYear.YearlyTotal + monthData.CPLConvertedDays;
                                //        dbLeaveQuotaYear.YearlyRemaining = dbLeaveQuotaYear.YearlyRemaining + monthData.CPLConvertedDays;
                                //        LeaveQuotaYearRepository.Add(dbLeaveQuotaYear);
                                //    }
                                //    LeaveQuotaYearRepository.Save();
                                //}
                                // update monthly data
                                monthData.IsCPLAdded = true;
                                MonthDataReporsitory.Edit(monthData);
                                MonthDataReporsitory.Save();
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                }
            }
            // Enable Validation
            // Disable EF Validations
            OTApprovedHistoryReporsitory.ToggleEFValidations(true);
            MonthDataReporsitory.ToggleEFValidations(true);
            LeaveCPLBalanceRepository.ToggleEFValidations(true);
            LeaveQuotaYearRepository.ToggleEFValidations(true);
            return(new VMOvertimeApproval());
        }
        public VMOvertimeApproval GetCreate3(VMOvertimeApprovalSelection es, int?[] SelectedEmployeeIds, VMOvertimeApproval vmOvertimeApproval)
        {
            List <MonthData> monthDatas   = new List <MonthData>();
            List <OTPolicy>  dbOTPolicies = DDService.GetOTPolicy();
            List <VMOvertimeApprovalChild> vmOvertimeApprovalChildList = new List <VMOvertimeApprovalChild>();
            List <VHR_EmployeeProfile>     employees = DDService.GetEmployeeInfo();
            PayrollPeriod payrollPeriod = DDService.GetPayrollPeriod().First(aa => aa.PPayrollPeriodID == es.PayrollPeriodID); // Get selected Payroll Period
            Expression <Func <MonthData, bool> > SpecificEntries = c => c.PayrollPeriodID == payrollPeriod.PPayrollPeriodID && (c.EncashbaleSingleOT > 0 || c.EncashbaleDoubleOT > 0 || c.CPLConversionOT > 0);

            monthDatas = MonthDataReporsitory.FindBy(SpecificEntries);
            foreach (int empid in SelectedEmployeeIds)
            {
                if (monthDatas.Where(aa => aa.EmployeeID == empid).Count() > 0)
                {
                    MonthData monthData = new MonthData();
                    monthData = monthDatas.First(aa => aa.EmployeeID == empid);
                    VMOvertimeApprovalChild vmOvertimeApprovalChild = new VMOvertimeApprovalChild();
                    VHR_EmployeeProfile     employee = employees.First(aa => aa.PEmployeeID == empid);// Get Specific Employee
                    vmOvertimeApprovalChild = GetConveretedOTList(vmOvertimeApprovalChild, employee, monthData, payrollPeriod);
                    vmOvertimeApprovalChildList.Add(vmOvertimeApprovalChild);
                }
            }
            vmOvertimeApproval.PayrollPeriodID       = payrollPeriod.PPayrollPeriodID;
            vmOvertimeApproval.PayrollPeriodName     = payrollPeriod.PRName;
            vmOvertimeApproval.OvertimeApprovalChild = vmOvertimeApprovalChildList;
            return(vmOvertimeApproval);
        }