public static void CaculateSalaryByMY(int month, int year)
        {
            List <Employee> employees = EmployeeController.GetListEmployee();

            foreach (Employee employee in employees)
            {
                Salary salary = SalaryController.GetSalaryByIDEmpMY(employee.ID, month, year);

                List <Models.Task> tasks = TaskController.GetListTaskByIDEmp(employee.ID);

                tasks = tasks.Where(t => t.EndDate.Value.Month == month && t.EndDate.Value.Year == year).ToList();

                if (salary == null)
                {
                    int id = SalaryController.GetIDFromDB();
                    salary = new Salary
                    {
                        ID          = id,
                        TotalHours  = 0,
                        Rewards     = 0,
                        Month       = month,
                        Year        = year,
                        TotalSalary = 0,
                        IDEmployee  = employee.ID
                    };
                }
                salary.Rewards    = tasks.Count;
                salary.TotalHours = 0;

                List <Work> works = WorkController.GetListWorkByIDEmp(employee.ID);

                if (works != null)
                {
                    foreach (Work work in works)
                    {
                        DateTime dateTime = DateTime.Parse(work.CheckIn.ToString());

                        if (dateTime.Month == month && dateTime.Year == year)
                        {
                            if (work.CheckOut != null)
                            {
                                TimeSpan totalDate = DateTime.Parse(work.CheckOut.ToString()) - DateTime.Parse(work.CheckIn.ToString());

                                salary.TotalHours += totalDate.TotalHours;
                            }
                        }
                    }
                }

                salary.TotalHours = Math.Round(Convert.ToDouble(salary.TotalHours), 2);

                salary.TotalSalary = Convert.ToInt32(salary.TotalHours * employee.HourlyWages + salary.Rewards * 100000);

                UpdateSalary(salary);
            }
        }
示例#2
0
        public static bool DeleteEmployee(string ID)
        {
            try
            {
                using (var _context = new DBILABEntities())
                {
                    Employee employee = (from e in _context.Employees
                                         where e.ID == ID
                                         select e).Single();

                    #region DeleteWork

                    List <Work> works = WorkController.GetListWorkByIDEmp(ID);

                    if (works != null)
                    {
                        foreach (Work work in works)
                        {
                            bool oldValidateOnSaveEnabled = _context.Configuration.ValidateOnSaveEnabled;
                            try
                            {
                                _context.Configuration.ValidateOnSaveEnabled = false;

                                _context.Works.Attach(work);
                                _context.Entry(work).State = System.Data.Entity.EntityState.Deleted;
                                _context.SaveChanges();
                            }
                            finally
                            {
                                _context.Configuration.ValidateOnSaveEnabled = oldValidateOnSaveEnabled;
                            }
                        }
                    }
                    #endregion
                    #region DeleteSalary

                    List <Salary> salaries = SalaryController.GetSalaryByIDEmp(ID);

                    if (salaries != null)
                    {
                        foreach (Salary salary in salaries)
                        {
                            bool oldValidateOnSaveEnabled = _context.Configuration.ValidateOnSaveEnabled;
                            try
                            {
                                _context.Configuration.ValidateOnSaveEnabled = false;

                                _context.Salaries.Attach(salary);
                                _context.Entry(salary).State = System.Data.Entity.EntityState.Deleted;
                                _context.SaveChanges();
                            }
                            finally
                            {
                                _context.Configuration.ValidateOnSaveEnabled = oldValidateOnSaveEnabled;
                            }
                        }
                    }
                    #endregion

                    #region DeleteTask
                    foreach (var task in employee.Tasks)
                    {
                        foreach (var e in task.Employees)
                        {
                            if (e.ID == employee.ID)
                            {
                                task.Employees.Remove(e);
                                break;
                            }
                        }
                    }

                    #endregion
                    _context.Employees.Remove(employee);
                    _context.SaveChanges();
                    return(true);
                }
            }
            catch
            {
                return(false);
            }
        }