public ActionResult Assign(FormCollection fc) { var model = new TimekeepingListViewModel(); var Id = Request["timekeepingListId"]; var timekeepingList = timekeepingListRepository.GetTimekeepingListById(Convert.ToInt32(Id)); var holiday = holidayRepository.GetAllHolidays().AsEnumerable().ToList(); var staff = StaffsRepository.GetvwAllStaffs().Where(x => x.BranchDepartmentId == timekeepingList.DepartmentId); ViewBag.staffList = staff; ViewBag.DayHoliday = holiday; DateTime aDateTime = new DateTime(timekeepingList.Year.Value, timekeepingList.Month.Value, 1); // Cộng thêm 1 tháng và trừ đi một ngày. DateTime retDateTime = aDateTime.AddMonths(1).AddDays(-1); ViewBag.aDateTime = aDateTime; ViewBag.retDateTime = retDateTime; var DayOff = categoryRepository.GetCategoryByCode("DayOffDefault").Where(x => x.Value == "True").AsEnumerable().ToList(); ViewBag.DayOff = DayOff; var ShiftsList = shiftsRepository.GetAllShifts().Where(x => x.CategoryShifts == timekeepingList.CategoryShifts).AsEnumerable(); ViewBag.ShiftsList = ShiftsList; var user = userRepository.GetUserById(WebSecurity.CurrentUserId); List <string> listIdShifts = new List <string>(); if (Request["shifts_id"] != null) { listIdShifts = Request["shifts_id"].Split(',').ToList(); } List <string> ListWorkSchedules = new List <string>(); foreach (var item in staff) { List <string> listIdCurrent = WorkSchedulesRepository.GetAllWorkSchedules().AsEnumerable().Where(x => aDateTime <= x.Day && x.Day <= retDateTime && x.ShiftsId != null && x.StaffId == item.Id).Select(x => x.ShiftsId.ToString().Replace(x.ShiftsId.ToString(), x.StaffId + "-" + x.Day.Value.ToString("dd/MM/yyyy") + "-" + x.ShiftsId)).ToList(); ListWorkSchedules = ListWorkSchedules.Union(listIdCurrent).ToList(); } // query id new to insert (not in database) List <string> listIdNew = listIdShifts.Where(id1 => !ListWorkSchedules.Any(id2 => id2 == id1)).ToList(); //query id to delete not listIdFromRequest List <string> listIdToDelete = ListWorkSchedules.Where(id1 => !listIdShifts.Any(id2 => id2 == id1)).ToList(); foreach (var id in listIdNew) { var item = new WorkSchedules(); item.CreatedUserId = WebSecurity.CurrentUserId; item.ModifiedUserId = WebSecurity.CurrentUserId; item.CreatedDate = DateTime.Now; item.ModifiedDate = DateTime.Now; item.IsDeleted = false; string[] arrVal = id.Split('-'); item.StaffId = int.Parse(arrVal[0], CultureInfo.InstalledUICulture); item.Day = DateTime.ParseExact(arrVal[1], "dd/MM/yyyy", CultureInfo.InvariantCulture); item.ShiftsId = int.Parse(arrVal[2], CultureInfo.InvariantCulture); item.TimekeepingListId = Convert.ToInt32(Id); WorkSchedulesRepository.InsertWorkSchedules(item); } foreach (var id in listIdToDelete) { string[] arrVal = id.Split('-'); int StaffId = int.Parse(arrVal[0], CultureInfo.InstalledUICulture); string Day = arrVal[1]; int ShiftsId = int.Parse(arrVal[2], CultureInfo.InvariantCulture); WorkSchedulesRepository.Delete(Day, StaffId, ShiftsId); } timekeepingList.Status = "timekeeping"; timekeepingListRepository.UpdateTimekeepingList(timekeepingList); // TempData[Globals.SuccessMessageKey] = "Lưu phân công ca làm việc thành công"; return(RedirectToAction("Detail", "TimekeepingList", new { area = "Staff", Id = Convert.ToInt32(Id) })); }
public ActionResult Create(TimekeepingListViewModel model) { if (ModelState.IsValid) { var TimekeepingList = new TimekeepingList(); AutoMapper.Mapper.Map(model, TimekeepingList); TimekeepingList.IsDeleted = false; TimekeepingList.CreatedUserId = WebSecurity.CurrentUserId; TimekeepingList.ModifiedUserId = WebSecurity.CurrentUserId; TimekeepingList.AssignedUserId = WebSecurity.CurrentUserId; TimekeepingList.CreatedDate = DateTime.Now; TimekeepingList.ModifiedDate = DateTime.Now; TimekeepingList.CheckSalary = false; if (model.Sale_BranchId != null) { if (model.DepartmentId != null) { var department = departmentRepository.GetvwBranchDepartmentById(model.DepartmentId.Value); TimekeepingList.Name = "Danh sách chấm công tháng " + model.Month + " năm " + model.Year + " - " + department.Staff_DepartmentId + " - " + department.BranchName; } else { var branch = branchRepository.GetBranchById(model.Sale_BranchId.Value); TimekeepingList.Name = "Danh sách chấm công tháng " + model.Month + " năm " + model.Year + " - " + branch.Name; } } else { TimekeepingList.Name = "Danh sách chấm công tháng " + model.Month + " năm " + model.Year + " - Tất cả chi nhánh"; } //mặc định khởi tạo danh sách thì trạng thái là pending... nếu có thêm phân công tự động thì là assigned if (model.CategoryShifts == "Full-time") { TimekeepingList.Status = "timekeeping"; } else { TimekeepingList.Status = "assign"; } TimekeepingListRepository.InsertTimekeepingList(TimekeepingList); var prefix2 = Erp.BackOffice.Helpers.Common.GetSetting("prefixOrderNo_timekeepingList"); TimekeepingList.Code = Erp.BackOffice.Helpers.Common.GetCode(prefix2, TimekeepingList.Id); TimekeepingListRepository.UpdateTimekeepingList(TimekeepingList); //nếu danh sách khởi tạo chấm công là Toàn thời gian thì tự động tạo phân công cho tất cả nhân viên trong phòng ban if (model.CategoryShifts == "Full-time") { //chuẩn bị dữ liệu ngày nghỉ để duyệt ngày nghỉ thì không phân công. var DayOff = categoryRepository.GetCategoryByCode("DayOffDefault").Where(x => x.Value == "True").AsEnumerable().ToList(); //lấy ca làm việc toàn thời gian ra. var shift = shiftsRepository.GetAllShifts().Where(x => x.CategoryShifts == "Full-time").OrderByDescending(x => x.CreatedDate).FirstOrDefault(); //lấy danh sách nhân viên của phòng ban để phân công. List <vwStaffs> list_staff_insert = new List <vwStaffs>(); var staff = StaffsRepository.GetvwAllStaffs().Where(x => !string.IsNullOrEmpty(x.BranchName) && x.IsWorking == true); if (model.Sale_BranchId != null) { if (model.DepartmentId != null) { list_staff_insert = staff.Where(x => x.BranchDepartmentId == model.DepartmentId).ToList(); } else { list_staff_insert = staff.Where(x => x.Sale_BranchId == model.Sale_BranchId).ToList(); } } else { list_staff_insert = staff.OrderBy(x => x.Id).ToList(); } //dựa vào tháng năm của danh sách khởi tạo ở trên, tạo ra list ngày trong tháng. DateTime aDateTime = new DateTime(TimekeepingList.Year.Value, TimekeepingList.Month.Value, 1); // Cộng thêm 1 tháng và trừ đi một ngày. DateTime retDateTime = aDateTime.AddMonths(1).AddDays(-1); //phần duyệt ngày trong tháng để thêm phân công cho từng nhân viên. for (DateTime dt = aDateTime; dt <= retDateTime; dt = dt.AddDays(1)) { if (DayOff.Where(x => Convert.ToInt32(x.OrderNo) == (int)dt.DayOfWeek && x.Value == "True").Count() <= 0) { foreach (var i in list_staff_insert) { var item = new WorkSchedules(); item.CreatedUserId = WebSecurity.CurrentUserId; item.ModifiedUserId = WebSecurity.CurrentUserId; item.CreatedDate = DateTime.Now; item.ModifiedDate = DateTime.Now; item.IsDeleted = false; item.StaffId = i.Id; item.Day = dt; item.ShiftsId = shift.Id; item.TimekeepingListId = TimekeepingList.Id; WorkSchedulesRepository.InsertWorkSchedules(item); } } } } //TempData[Globals.SuccessMessageKey] = App_GlobalResources.Wording.InsertSuccess; return(RedirectToAction("Detail", "TimekeepingList", new { area = "Staff", Id = TimekeepingList.Id })); } return(View(model)); }