public ShiftManagementDetailModel(ShiftManagementDetail fromEntity, Dictionary <int, string> managerList) { Employee = new EmployeeInfoModel { FullName = fromEntity.Employee.LookupValue, Id = fromEntity.Employee.LookupId, EmployeeId = fromEntity.EmployeeID != null ? fromEntity.EmployeeID.LookupValue : string.Empty }; Employee.ManagerName = managerList[Convert.ToInt32(Employee.Id)]; Id = fromEntity.ID; ShiftManagementID = fromEntity.ShiftManagementID.LookupId; for (int i = 1; i <= 31; i++) { var shiftTimePropertyName = string.Format("ShiftTime{0}", i); var shiftTime = GetType().GetProperty(shiftTimePropertyName); var shiftTimeFromEntity = fromEntity.GetType().GetProperty(string.Format("ShiftTime{0}", i.ToString())).GetValue(fromEntity) as LookupItem; var shiftTimeApprovedFromEntity = fromEntity.GetType().GetProperty(string.Format("ShiftTime{0}Approval", i.ToString())).GetValue(fromEntity); var shiftValue = new ShiftTimeDetailModel(); if (shiftTimeFromEntity != null) { shiftValue.Value = shiftTimeFromEntity.LookupId.ToString(); shiftValue.Code = shiftTimeFromEntity.LookupValue; shiftValue.Approved = shiftTimeApprovedFromEntity != null ? (bool)shiftTimeApprovedFromEntity : false; } shiftTime.SetValue(this, shiftValue); } }
public ShiftManagementDetail ToEntity() { var result = new ShiftManagementDetail { ID = Id, Employee = new LookupItem { LookupId = Employee.Id, LookupValue = Employee.FullName }, ShiftManagementID = new LookupItem { LookupId = ShiftManagementID, LookupValue = ShiftManagementID.ToString() } }; for (int i = 1; i <= 31; i++) { var shiftTimePropertyName = string.Format("ShiftTime{0}", i); var shiftTime = GetType().GetProperty(shiftTimePropertyName); var shiftTimeEntity = result.GetType().GetProperty(shiftTimePropertyName); var shiftTimeApprovalEntity = result.GetType().GetProperty(shiftTimePropertyName + "Approval"); var value = shiftTime.GetValue(this) as ShiftTimeDetailModel; if (value != null) { shiftTimeEntity.SetValue(result, new LookupItem { LookupId = string.IsNullOrEmpty(value.Value) ? 0 : int.Parse(value.Value), LookupValue = value.Code }); shiftTimeApprovalEntity.SetValue(result, value.Approved); } else { shiftTimeEntity.SetValue(result, new LookupItem { LookupId = 0, LookupValue = string.Empty }); shiftTimeApprovalEntity.SetValue(result, false); } } return(result); }
public void ImportExcel() { ExcelShiftsOfDepartment excelShiftsOfDepartment = ReadExcelShiftData(); excelShiftsOfDepartment.DepartmentId = 2; //HR excelShiftsOfDepartment.Location = 2; excelShiftsOfDepartment.Month = 7; excelShiftsOfDepartment.Year = 2017; Dictionary <string, int> duplicatedEmployees = ValidateDuplicatedData(excelShiftsOfDepartment); string siteUrl = "http://rbvhspdev01:81/"; ShiftManagementDAL _shiftManagementDAL = new ShiftManagementDAL(siteUrl); ShiftManagementDetailDAL _shiftManagementDetailDAL = new ShiftManagementDetailDAL(siteUrl); List <ShiftManagement> shiftManagements = _shiftManagementDAL.GetByMonthYearDepartment(excelShiftsOfDepartment.Month, excelShiftsOfDepartment.Year, excelShiftsOfDepartment.DepartmentId, excelShiftsOfDepartment.Location); List <ShiftManagementDetail> shiftManagementDetails = new List <ShiftManagementDetail>(); if (shiftManagements != null && shiftManagements.Count > 0) { shiftManagementDetails = _shiftManagementDetailDAL.GetByShiftManagementID(shiftManagements[0].ID); } EmployeeInfoDAL _employeeInfoDAL = new EmployeeInfoDAL(siteUrl); List <EmployeeInfo> employeesOfDepartment = _employeeInfoDAL.GetByLocationAndDepartment(2, excelShiftsOfDepartment.DepartmentId, true, 4, StringConstant.EmployeeInfoList.EmployeeIDField); if (shiftManagementDetails != null && shiftManagementDetails.Count > 0) { foreach (var employeeShift in excelShiftsOfDepartment.EmployeeShifts) { EmployeeInfo employeeInfo = employeesOfDepartment.Where(e => e.EmployeeID == employeeShift.EmployeeID).FirstOrDefault(); if (employeeInfo != null) { employeeShift.EmployeeLookupID = employeeInfo.ID; } } List <int> employeeLookupIds = excelShiftsOfDepartment.EmployeeShifts.Select(e => e.EmployeeLookupID).ToList(); shiftManagementDetails = shiftManagementDetails.Where(e => employeeLookupIds.Contains(e.Employee.LookupId)).ToList(); } foreach (ExcelEmployeeShift excelEmployeeShift in excelShiftsOfDepartment.EmployeeShifts) { ShiftManagementDetail shiftManagementDetail = shiftManagementDetails.Where(e => e.Employee.LookupId == excelEmployeeShift.EmployeeLookupID).FirstOrDefault(); if (shiftManagementDetail == null) { shiftManagementDetail = new ShiftManagementDetail(); EmployeeInfo employeeInfo = employeesOfDepartment.Where(e => e.EmployeeID == excelEmployeeShift.EmployeeID).FirstOrDefault(); shiftManagementDetail.Employee = new LookupItem() { LookupId = employeeInfo.ID, LookupValue = employeeInfo.FullName }; shiftManagementDetails.Add(shiftManagementDetail); } ShiftTimeDAL _shiftTimeDAL = new ShiftTimeDAL(siteUrl); List <ShiftTime> shiftTimes = _shiftTimeDAL.GetAll(); for (int i = 0; i < 11; i++) { ShiftTime shiftTime = shiftTimes.Where(e => e.Code.ToUpper() == excelEmployeeShift.ShiftCodes[i].ToUpper()).FirstOrDefault(); if (shiftTime != null) { Type type = typeof(ShiftManagementDetail); PropertyInfo shiftApprovalInfo = type.GetProperty(string.Format("ShiftTime{0}Approval", i + 21)); object shiftApprovalValue = shiftApprovalInfo.GetValue(shiftManagementDetail, null); if (shiftApprovalValue != null && Convert.ToBoolean(shiftApprovalValue) == false) { PropertyInfo shiftInfo = type.GetProperty(string.Format("ShiftTime{0}", i + 21)); LookupItem shiftValue = shiftInfo.GetValue(shiftManagementDetail, null) as LookupItem; if (shiftValue == null) { shiftValue = new LookupItem(); } shiftValue.LookupId = shiftTime.ID; shiftValue.LookupValue = shiftTime.Code; } } } for (int i = 11; i < 31; i++) { ShiftTime shiftTime = shiftTimes.Where(e => e.Code.ToUpper() == excelEmployeeShift.ShiftCodes[i].ToUpper()).FirstOrDefault(); if (shiftTime != null) { Type type = typeof(ShiftManagementDetail); PropertyInfo shiftApprovalInfo = type.GetProperty(string.Format("ShiftTime{0}Approval", i - 10)); object shiftApprovalValue = shiftApprovalInfo.GetValue(shiftManagementDetail, null); if (shiftApprovalValue != null && Convert.ToBoolean(shiftApprovalValue) == false) { PropertyInfo shiftInfo = type.GetProperty(string.Format("ShiftTime{0}", i - 10)); LookupItem shiftValue = shiftInfo.GetValue(shiftManagementDetail, null) as LookupItem; if (shiftValue == null) { shiftValue = new LookupItem(); } shiftValue.LookupId = shiftTime.ID; shiftValue.LookupValue = shiftTime.Code; } } } } int shiftId = 0; if (shiftManagements == null || shiftManagements.Count == 0) { Biz.Models.ShiftManagement shiftManagement = new Biz.Models.ShiftManagement(); shiftManagement.Department = new LookupItem() { LookupId = excelShiftsOfDepartment.DepartmentId, LookupValue = excelShiftsOfDepartment.DepartmentId.ToString() }; shiftManagement.Location = new LookupItem() { LookupId = excelShiftsOfDepartment.Location, LookupValue = excelShiftsOfDepartment.Location.ToString() }; shiftManagement.Month = excelShiftsOfDepartment.Month; shiftManagement.Year = excelShiftsOfDepartment.Year; EmployeeInfo requester = _employeeInfoDAL.GetByADAccount(122); shiftManagement.Requester = new LookupItem() { LookupId = requester.ID, LookupValue = requester.FullName }; EmployeeInfo approvedBy = _employeeInfoDAL.GetByPositionDepartment(StringConstant.EmployeePosition.DepartmentHead, excelShiftsOfDepartment.DepartmentId, excelShiftsOfDepartment.Location).FirstOrDefault(); shiftManagement.ApprovedBy = new User() { ID = approvedBy.ADAccount.ID, UserName = approvedBy.ADAccount.UserName, FullName = approvedBy.FullName, IsGroup = false }; shiftId = _shiftManagementDAL.SaveOrUpdate(shiftManagement); } else { shiftId = shiftManagements[0].ID; } if (shiftId > 0) { foreach (ShiftManagementDetail shiftManagementDetail in shiftManagementDetails) { shiftManagementDetail.ShiftManagementID = new LookupItem() { LookupId = shiftId, LookupValue = shiftId.ToString() }; int shiftDetailId = _shiftManagementDetailDAL.SaveOrUpdate(shiftManagementDetail); } } }