public ActionResult Create() { var viewmodel = new ServerTimeEntryAddViewModel(); viewmodel.SelectedServers = _serverService.GetServers().Select(t => new SelectListItem() { Text = t.FullName, Value = t.Id.ToString() }); viewmodel.SelectedPaySources = _paySourceService.GetPaySources().Select(t => new SelectListItem() { Text = t.Description, Value = t.Id.ToString() }); viewmodel.SelectedPrograms = new List <SelectListItem>(); return(View(viewmodel)); }
public ActionResult Create(ServerTimeEntryAddViewModel viewmodel) { if (ModelState.IsValid) { var entity = Mapper.Map <ServerTimeEntryAddViewModel, ServerTimeEntry>(viewmodel); _serverTimeEntryService.AddServerTimeEntry(entity); Success($"<strong>Time Entry</strong> was successfully added."); return(RedirectToAction("Index")); } viewmodel.SelectedServers = _serverService.GetServers().Select(t => new SelectListItem() { Text = t.FullName, Value = t.Id.ToString() }); viewmodel.SelectedPaySources = _paySourceService.GetPaySources().Select(t => new SelectListItem() { Text = t.Description, Value = t.Id.ToString() }); viewmodel.SelectedPrograms = new List <SelectListItem>(); return(View(viewmodel)); }
public ActionResult Upload(UploadedExcelSheetViewModel viewmodel) { if (ModelState.IsValid) // validate file exist { if (viewmodel.ExcelFile != null && viewmodel.ExcelFile.ContentLength > 0) { var fileName = Path.GetFileName(viewmodel.ExcelFile.FileName); var path = Path.Combine(Server.MapPath("~/Uploads/ServerTimeEntries/"), DateTime.Now.GetTimeStamp() + "_" + fileName); List <ServerTimeEntry> timeEntries = new List <ServerTimeEntry>(); viewmodel.ExcelFile.SaveAs(path); // save a copy of the uploaded file. // convert the uploaded file into datatable, then add/update db entities. var columnsToImport = new string[] { "Server ID", "Current Pay Source", "Begin Date", "Duration" }; var dtServers = ImportUtils.ImportXlsxToDataTable(viewmodel.ExcelFile.InputStream, true, columnsToImport); var invalidServers = new List <int>(); var invalidPaysources = new List <int>(); foreach (var row in dtServers.AsEnumerable().ToList()) { var timeEntryViewModel = new ServerTimeEntryAddViewModel() { ServerId = int.Parse(row["Server ID"].ToString()), PaySourceId = int.Parse(row["Current Pay Source"].ToString()), BeginDate = DateTime.Parse(row["Begin Date"].ToString()), Duration = TimeSpan.Parse(row["Duration"].ToString()) }; var existedServer = _serverService.GetByVendorId(timeEntryViewModel.ServerId); if (existedServer == null) { if (!invalidServers.Any(t => t == timeEntryViewModel.ServerId)) { invalidServers.Add(timeEntryViewModel.ServerId); } } var existedPaySource = _paySourceService.GetByVendorId(timeEntryViewModel.PaySourceId); if (existedPaySource == null) { if (!invalidPaysources.Any(t => t == timeEntryViewModel.PaySourceId)) { invalidPaysources.Add(timeEntryViewModel.PaySourceId); } } if (existedServer != null && existedPaySource != null) { // check if entity already exists. var entity = Mapper.Map <ServerTimeEntryAddViewModel, ServerTimeEntry>(timeEntryViewModel); entity.ServerId = existedServer.Id; entity.PaySourceId = existedPaySource.Id; entity.ProgramId = existedPaySource.Programs.Any() ? existedPaySource.Programs.ToList()[0].Id : (int?)null; if (!_serverTimeEntryService.TimeEntryExists(entity)) { timeEntries.Add(entity); } } } if (invalidServers.Any() || invalidPaysources.Any()) { invalidServers.ForEach(invalidServerId => { ModelState.AddModelError("", $"Invalid Server Id with value ={invalidServerId}"); }); invalidPaysources.ForEach(invalidPaysource => { ModelState.AddModelError("", $"Invalid PaySource Id with value ={invalidPaysource}"); }); return(View(viewmodel)); } else { _serverTimeEntryService.AddServerTimeEntries(timeEntries); } Success($"<strong>{timeEntries.Count}</strong> Time Entries have been successfully added. <br\\>" + $"<strong>{dtServers.Rows.Count - timeEntries.Count}</strong> Time Entries are duplicated and have been skipped."); } return(RedirectToAction("Index")); } return(View(viewmodel)); }