public ActionResult Create(TimekeepingSynthesisViewModel model) { if (ModelState.IsValid) { var TimekeepingSynthesis = new TimekeepingSynthesis(); AutoMapper.Mapper.Map(model, TimekeepingSynthesis); TimekeepingSynthesis.IsDeleted = false; TimekeepingSynthesis.CreatedUserId = WebSecurity.CurrentUserId; TimekeepingSynthesis.ModifiedUserId = WebSecurity.CurrentUserId; TimekeepingSynthesis.AssignedUserId = WebSecurity.CurrentUserId; TimekeepingSynthesis.CreatedDate = DateTime.Now; TimekeepingSynthesis.ModifiedDate = DateTime.Now; TimekeepingSynthesisRepository.InsertTimekeepingSynthesis(TimekeepingSynthesis); TempData[Globals.SuccessMessageKey] = App_GlobalResources.Wording.InsertSuccess; return(RedirectToAction("Index")); } return(View(model)); }
public static void CreateTimekeeping(int?Year, int?Month, int?Id) { Erp.Domain.Staff.Repositories.HolidaysRepository holidayRepository = new Erp.Domain.Staff.Repositories.HolidaysRepository(new Domain.Staff.ErpStaffDbContext()); Erp.Domain.Staff.Repositories.TimekeepingSynthesisRepository timekeepingSynthesisRepository = new Erp.Domain.Staff.Repositories.TimekeepingSynthesisRepository(new Domain.Staff.ErpStaffDbContext()); Erp.Domain.Repositories.CategoryRepository categoryRepository = new Erp.Domain.Repositories.CategoryRepository(new Domain.ErpDbContext()); Erp.Domain.Staff.Repositories.WorkSchedulesRepository workSchedulesRepository = new Erp.Domain.Staff.Repositories.WorkSchedulesRepository(new Domain.Staff.ErpStaffDbContext()); //lấy danh sách chấm công từng ngày trong tháng var listTinmeKeeping = workSchedulesRepository.GetvwAllWorkSchedules().Where(x => x.TimekeepingListId.Value == Id).ToList(); var timekeepingList = listTinmeKeeping.Select(i => new WorkSchedulesViewModel { BranchDepartmentId = i.BranchDepartmentId, Code = i.Code, CodeShifts = i.CodeShifts, Color = i.Color, Day = i.Day, DayOff = i.DayOff, DayOffCode = i.DayOffCode, DayOffName = i.DayOffName, EndTime = i.EndTime, EndTimeIn = i.EndTimeIn, Id = i.Id, HoursIn = i.HoursIn, HoursOut = i.HoursOut, Month = i.Month, Name = i.Name, NameShifts = i.NameShifts, Sale_BranchId = i.Sale_BranchId, ShiftsId = i.ShiftsId, StaffId = i.StaffId, StartTime = i.StartTime, StartTimeOut = i.StartTimeOut, Symbol = i.Symbol, Timekeeping = i.Timekeeping, Total_minute_work = i.Total_minute_work, Total_minute_work_early = i.Total_minute_work_early, Total_minute_work_late = i.Total_minute_work_late, Total_minute_work_overtime = i.Total_minute_work_overtime, UserEnrollNumber = i.UserEnrollNumber, Year = i.Year, TimekeepingListId = i.TimekeepingListId, Absent = i.Absent }).OrderBy(x => x.ShiftsId).ToList(); //lấy danh sách nhân viên trong phòng ban, nên lấy theo danh sách phân công để chính xác var StaffList = timekeepingList .GroupBy(x => new { x.StaffId }, (key, group) => new WorkSchedulesViewModel { StaffId = key.StaffId, Code = group.FirstOrDefault().Code, Name = group.FirstOrDefault().Name, Id = group.FirstOrDefault().Id, TimekeepingListId = group.FirstOrDefault().TimekeepingListId, Year = group.FirstOrDefault().Year, Month = group.FirstOrDefault().Month }).ToList(); //lấy danh sách ngày lễ trog năm var holiday = holidayRepository.GetAllHolidays(); //lấy danh sách các ngày cần xem chấm công var datesNghiBu = new List <DateTime>(); var datesNghiLe = new List <DateTime>(); var datesTatCaNgayNghi = new List <DateTime>(); //dựa vào tháng, năm truyền vào lấy ngày bắt đầu, kết thúc của tháng DateTime aDateTime = new DateTime(Year.Value, Month.Value, 1); // Cộng thêm 1 tháng và trừ đi một ngày. DateTime retDateTime = aDateTime.AddMonths(1).AddDays(-1); // ..duyệt từng ngày for (DateTime dt = aDateTime; dt <= retDateTime; dt = dt.AddDays(1)) { //nếu là ngày nghỉ bù của ngày lễ thì thêm vào list datesNghiBu if (holiday.Where(x => x.DayStart <= dt && dt <= x.DayEnd && x.DayOffset == true).Count() > 0) { datesNghiBu.Add(dt); } //nếu là ngày nghỉ lễ thì thêm váo list datesNghiLe if (holiday.Where(x => x.DayStart <= dt && dt <= x.DayEnd && x.DayOffset != true).Count() > 0) { datesNghiLe.Add(dt); } //lấy danh sách tất cả ngày nghỉ if (holiday.Where(x => x.DayStart <= dt && dt <= x.DayEnd).Count() > 0) { datesTatCaNgayNghi.Add(dt); } } // ngày nghỉ mặc định trong tuần... theo từng công ty khác nhau thì ngày nghỉ khác nhau. var DayOff = categoryRepository.GetCategoryByCode("DayOffDefault").Where(x => x.Value == "True").AsEnumerable().ToList(); //duyệt danh sách nhân viên foreach (var item in StaffList) { var timekeepingSynthesis = new TimekeepingSynthesis(); //kiểm tra xem nhân viên có được tổng hợp chấm công theo tháng đó chưa>>> nếu có chỉ cập nhật lại. var list = timekeepingSynthesisRepository.GetTimekeepingSynthesisByStaff(item.StaffId.Value, item.Month.Value, item.Year.Value); if (list != null) { timekeepingSynthesisRepository.DeleteTimekeepingSynthesis(list.Id); } //dựa vào bảng workSchedules tổng hợp chấm công timekeepingSynthesis.IsDeleted = false; timekeepingSynthesis.CreatedUserId = WebSecurity.CurrentUserId; timekeepingSynthesis.ModifiedUserId = WebSecurity.CurrentUserId; timekeepingSynthesis.AssignedUserId = WebSecurity.CurrentUserId; timekeepingSynthesis.CreatedDate = DateTime.Now; timekeepingSynthesis.ModifiedDate = DateTime.Now; //tính số phút về sớm timekeepingSynthesis.GioVeSom = Math.Round(timekeepingList.Where(x => x.StaffId == item.StaffId).Sum(x => Convert.ToDouble(x.Total_minute_work_early)) / Convert.ToDouble(60), 2); //tính giờ đi trễ. timekeepingSynthesis.GioDiTre = Math.Round(timekeepingList.Where(x => x.StaffId == item.StaffId).Sum(x => Convert.ToDouble(x.Total_minute_work_late)) / Convert.ToDouble(60), 2); //lưu lại id chấm công của tháng đó timekeepingSynthesis.TimekeepingListId = item.TimekeepingListId; //tính ngày nghỉ phép. //timekeeping là có tính công //absent là vắng group by theo ngày để đếm (vì 1 ngày có thể làm nhiều ca khác nhau.) timekeepingSynthesis.NgayNghiCoPhep = timekeepingList.Where(x => x.StaffId == item.StaffId && x.Timekeeping == true && x.Absent == true).GroupBy(x => x.Day).Count(); // var aaa = timekeepingList.Where(x => x.StaffId == item.StaffId); List <WorkSchedulesViewModel> listIdDayWork = aaa.Where(id1 => !DayOff.Any(id2 => id2.OrderNo.Value.ToString() == id1.Day.Value.DayOfWeek.ToString())).ToList(); List <WorkSchedulesViewModel> listIdDayOff = aaa.Where(id1 => DayOff.Any(id2 => id2.OrderNo.Value.ToString() == id1.Day.Value.DayOfWeek.ToString())).ToList(); List <WorkSchedulesViewModel> listIdNgayLe = aaa.Where(id1 => datesTatCaNgayNghi.Any(id2 => id2.DayOfWeek == id1.Day.Value.DayOfWeek)).ToList(); //tính tổng số phút làm việc trong ngày thường timekeepingSynthesis.TrongGioNgayThuong = Math.Round((listIdDayWork.Sum(x => Convert.ToDouble(x.Total_minute_work))) / Convert.ToDouble(60), 2); //tính tổng số phút làm việc trong ngày nghỉ cuối tuần (t7, chủ nhật) timekeepingSynthesis.TrongGioNgayNghi = Math.Round((listIdDayOff.Sum(x => Convert.ToDouble(x.Total_minute_work))) / Convert.ToDouble(60), 2); //tính tổng số phút làm việc trong ngày lễ timekeepingSynthesis.TrongGioNgayLe = Math.Round((listIdNgayLe.Sum(x => Convert.ToDouble(x.Total_minute_work))) / Convert.ToDouble(60), 2); //tính tổng số phút tăng ca ngày lễ timekeepingSynthesis.TangCaNgayLe = Math.Round((listIdNgayLe.Sum(x => Convert.ToDouble(x.Total_minute_work_overtime))) / Convert.ToDouble(60), 2); //tính tổng số phút tăng ca ngày nghỉ timekeepingSynthesis.TangCaNgayNghi = Math.Round((listIdDayOff.Sum(x => Convert.ToDouble(x.Total_minute_work_overtime))) / Convert.ToDouble(60), 2); //tính tổng số phút tăng ca ngày thường timekeepingSynthesis.TangCaNgayThuong = Math.Round((listIdDayWork.Sum(x => Convert.ToDouble(x.Total_minute_work_overtime))) / Convert.ToDouble(60), 2); //tính số ngày công thực tế là ngày phân công đi làm. int ngay_di_lam = timekeepingList.Where(x => x.StaffId == item.StaffId && x.Absent == false).GroupBy(x => x.Day).Count(); int nghi_co_phep = timekeepingList.Where(x => x.StaffId == item.StaffId && x.Absent == true && x.Timekeeping == true).GroupBy(x => x.Day).Count(); timekeepingSynthesis.NgayCongThucTe = ngay_di_lam + nghi_co_phep; //timekeepingSynthesis.NgayNghiKoPhep = item.NgayNghiKhongPhep; timekeepingSynthesis.StaffId = item.StaffId; //timekeepingSynthesis.TongNgayNghi = item.TongNgayNghi; timekeepingSynthesis.Year = item.Year; timekeepingSynthesis.Month = item.Month; //tính số phút làm ban đêm timekeepingSynthesis.GioCaDem = Math.Round((timekeepingList.Where(x => x.StaffId == item.StaffId && x.NightShifts == true).Sum(x => Convert.ToDouble(x.Total_minute_work))) / Convert.ToDouble(60), 2); //đếm số ngày nghỉ bù timekeepingSynthesis.SoNgayNghiBu = datesNghiBu.Count(); //số ngày nghỉ lễ timekeepingSynthesis.SoNgayNghiLe = datesNghiLe.Count(); timekeepingSynthesis.NgayVeSom = aaa.Where(x => x.DayOffCode == "Sm" || x.DayOffCode == "Tr,Sm").Count(); timekeepingSynthesis.NgayDiTre = aaa.Where(x => x.DayOffCode == "Tr" || x.DayOffCode == "Tr,Sm").Count(); timekeepingSynthesisRepository.InsertTimekeepingSynthesis(timekeepingSynthesis); } }