public HttpResponseMessage Xamarin_reg(WorkModel input) { try { var same = entities.Employees.Any(u => u.Username == input.Username); if (same == true) { return(Request.CreateResponse(HttpStatusCode.Unauthorized, "Username is in use, try a." + same.ToString())); } else { Employees employee = new Employees(); employee.Firstname = input.Firstname; employee.Lastname = input.Lastname; employee.Phone = input.Phone; employee.Email = input.Email; employee.Username = input.Username; employee.Password = input.Password; entities.Employees.Add(employee); entities.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.Accepted, "Successfully Created")); } } finally { entities.Dispose(); } }
public WorkModel GetModel(string workName) { panconDatabaseEntities entities = new panconDatabaseEntities(); try { string chosenWorkId = workName; WorkAssignments chosenWorkData = (from cw in entities.WorkAssignments where (cw.Active == true) && (cw.WorkAssignmentId.ToString() == chosenWorkId) select cw).FirstOrDefault(); WorkModel chosenWorkModel = new WorkModel() { WorkTitle = chosenWorkData.Title, Description = chosenWorkData.Description, Deadline = chosenWorkData.Deadline.Value }; return(chosenWorkModel); } finally { entities.Dispose(); } }
public WorkModel GetModel(string employeeName) { panconDatabaseEntities entities = new panconDatabaseEntities(); try { string chosenEmployee = employeeName; Employees employee = (from e in entities.Employees where (e.Active == true) && (e.EmployeeId.ToString() == chosenEmployee) select e).FirstOrDefault(); WorkModel chosenEmployeeModel = new WorkModel() { Firstname = employee.Firstname, Lastname = employee.Lastname, Phone = employee.Phone, Email = employee.Email, Picture = employee.EmployeePicture }; return(chosenEmployeeModel); } finally { entities.Dispose(); } }
public WorkModel GetProfileInfo(string userName) { panconDatabaseEntities entities = new panconDatabaseEntities(); try { Employees profile = (from p in entities.Employees where (p.Active == true) && (p.Username == userName) select p).FirstOrDefault(); string cont = (from c in entities.Contractors where (c.ContractorId == profile.ContractorId) select c.CompanyName).Single(); WorkModel loggedProfile = new WorkModel() { ContractorName = cont, Firstname = profile.Firstname, Lastname = profile.Lastname, Phone = profile.Phone, Email = profile.Email, Picture = profile.EmployeePicture, Username = profile.Username }; return(loggedProfile); } finally { entities.Dispose(); } }
public WorkModel GetContractorModel(string contractorName) { panconDatabaseEntities entities = new panconDatabaseEntities(); try { string chosenContractor = contractorName; Contractors contractor = (from co in entities.Contractors where (co.Active == true) && (co.CompanyName == chosenContractor) select co).FirstOrDefault(); WorkModel chosenContractorModel = new WorkModel() { ContractorId = contractor.ContractorId, ContractorName = contractor.CompanyName, ContractorContactPerson = contractor.ContactPerson, ContractorPhone = contractor.Phone, ContractorEmail = contractor.Email, VatId = contractor.VatId, HourlyRate = contractor.HourlyRate.ToString(), }; return(chosenContractorModel); } finally { entities.Dispose(); } }
public List <string[]> GetPickerData(int picker) { panconDatabaseEntities entities = new panconDatabaseEntities(); List <string[]> pickerData = new List <string[]>(); string[] emp = { "Employees:---------- " }; string[] work = { "Workassignments:---------- " }; string[] cont = { "Contractors:---------- " }; try { string[] workAssignments = (from wa in entities.WorkAssignments where (wa.Active == true) select wa.Title).ToArray(); string[] employees = (from e in entities.Employees where (e.Active == true) select e.Firstname + " " + e.Lastname).ToArray(); string[] contractors = (from co in entities.Contractors where (co.Active == true) select co.CompanyName).ToArray(); pickerData.Add(work); pickerData.Add(workAssignments); pickerData.Add(emp); pickerData.Add(employees); pickerData.Add(cont); pickerData.Add(contractors); } finally { entities.Dispose(); } return(pickerData); }
public WorkModel GetCustomerModel(string customerName) { panconDatabaseEntities entities = new panconDatabaseEntities(); try { string chosenCustomer = customerName; Customers customer = (from c in entities.Customers where (c.Active == true) && (c.CustomerName == chosenCustomer) select c).FirstOrDefault(); WorkModel chosenCustomerModel = new WorkModel() { CustomerName = customer.CustomerName, ContactPerson = customer.ContactPerson, CustomerPhone = customer.Phone, CustomerEmail = customer.Email, CustomerId = customer.CustomerId }; return(chosenCustomerModel); } finally { entities.Dispose(); } }
public ActionResult GetTimesheetsCount(string onlyComplete) { ReportChartDataViewModel model = new ReportChartDataViewModel(); panconDatabaseEntities entities = new panconDatabaseEntities(); try { model.Labels = (from wa in entities.WorkAssignments orderby wa.WorkAssignmentId select wa.Title).ToArray(); if (onlyComplete == "1") { model.Counts = (from ts in entities.Timesheets where (ts.WorkComplete == true) orderby ts.WorkAssignmentId group ts by ts.WorkAssignmentId into grp select grp.Count()).ToArray(); } else { model.Counts = (from ts in entities.Timesheets orderby ts.WorkAssignmentId group ts by ts.WorkAssignmentId into grp select grp.Count()).ToArray(); } } finally { entities.Dispose(); } return(Json(model, JsonRequestBehavior.AllowGet)); }
public ActionResult HoursPerWorkAssignmentToExcelAsCsv() { StringBuilder csv = new StringBuilder(); panconDatabaseEntities entities = new panconDatabaseEntities(); try { DateTime today = DateTime.Today; DateTime tommorrow = today.AddDays(1); List <Timesheets> allTimesheetsToday = (from ts in entities.Timesheets where (ts.StartTime > today) && (ts.StartTime < tommorrow) && (ts.WorkComplete == true) select ts).ToList(); foreach (Timesheets timesheet in allTimesheetsToday) { csv.AppendLine(timesheet.WorkAssignmentId + ";" + timesheet.StartTime + ";" + timesheet.StopTime + ";"); } } finally { entities.Dispose(); } byte[] buffer = Encoding.UTF8.GetBytes(csv.ToString()); return(File(buffer, "text/csv", "Työtunnit.csv")); }
public List <string> GetAll() { string[] employees = null; panconDatabaseEntities entities = new panconDatabaseEntities(); List <string> empList = new List <string>(); employees = (from e in entities.Employees where (e.Active == true) select e.ContractorId + " " + e.EmployeeId).ToArray(); try { foreach (var item in employees) { string[] data = item.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); string contractor = data[0]; string contr = (from c in entities.Contractors where (c.ContractorId.ToString() == contractor) select c.CompanyName).Single(); string employee = data[1]; string emp = (from e in entities.Employees where (e.EmployeeId.ToString() == employee) select e.Firstname + " " + e.Lastname).Single(); empList.Add("Contractor: " + contr + " | Name: " + emp + " ( " + employee + " )"); } } finally { entities.Dispose(); } return(empList); }
public ActionResult Index() { panconDatabaseEntities entities = new panconDatabaseEntities(); List <Employees> model = entities.Employees.ToList(); entities.Dispose(); return(View(model)); }
public bool PostChanges(WorkModel model) { panconDatabaseEntities entities = new panconDatabaseEntities(); try { Employees MyProfile = (from ce in entities.Employees where (ce.Active == true) && (ce.Firstname + " " + ce.Lastname == model.Firstname + " " + model.Lastname) select ce).FirstOrDefault(); if (MyProfile == null) { return(false); } int employeeId = MyProfile.EmployeeId; Employees existing = (from e in entities.Employees where (e.EmployeeId == employeeId) && (e.Active == true) select e).FirstOrDefault(); if (existing != null && model.Picture != null && model.Operation == "Save") { existing.Firstname = model.Firstname; existing.Lastname = model.Lastname; existing.Phone = model.Phone.ToString(); existing.Email = model.Email; existing.LastModified = DateTime.Now; existing.EmployeePicture = model.Picture; } else if (existing != null && model.Picture == null && model.Operation == "Save") { existing.Firstname = model.Firstname; existing.Lastname = model.Lastname; existing.Phone = model.Phone.ToString(); existing.Email = model.Email; existing.LastModified = DateTime.Now; } else if (existing != null && model.Operation == "SavePw") { existing.Password = model.Password; } entities.SaveChanges(); } catch { return(false); } finally { entities.Dispose(); } return(true); }
public ActionResult Update(Employees empl) { panconDatabaseEntities entities = new panconDatabaseEntities(); bool OK = false; if (empl.EmployeeId == 0) { Employees dbItem = new Employees() { Firstname = empl.Firstname, Lastname = empl.Lastname, }; entities.Employees.Add(dbItem); entities.SaveChanges(); OK = true; entities.Dispose(); return(Json(OK)); } else { Employees dbItem = (from e in entities.Employees where e.EmployeeId == empl.EmployeeId select e).FirstOrDefault(); if (dbItem != null) { dbItem.Firstname = empl.Firstname; dbItem.Lastname = empl.Lastname; entities.SaveChanges(); } ; OK = true; entities.Dispose(); return(Json(OK, JsonRequestBehavior.AllowGet)); } }
public string[] GetAll() { string[] assignmentNames = null; panconDatabaseEntities entities = new panconDatabaseEntities(); try { assignmentNames = (from wa in entities.WorkAssignments where (wa.Active == true) select wa.Title).ToArray(); } finally { entities.Dispose(); } return(assignmentNames); }
public string[] GetAll() { string[] customers = null; panconDatabaseEntities entities = new panconDatabaseEntities(); try { customers = (from c in entities.Customers where (c.Active == true) select c.CustomerName).ToArray(); } finally { entities.Dispose(); } return(customers); }
public ActionResult HoursPerWorkAssignment() { panconDatabaseEntities entities = new panconDatabaseEntities(); try { DateTime today = DateTime.Today; DateTime tommorrow = today.AddDays(1); List <Timesheets> allTimesheetsToday = (from ts in entities.Timesheets where (ts.StartTime > today) && (ts.StartTime < tommorrow) && (ts.WorkComplete == true) select ts).ToList(); List <HoursPerWorkAssignmentModel> model = new List <HoursPerWorkAssignmentModel>(); foreach (Timesheets timesheet in allTimesheetsToday) { int assignmentId = timesheet.WorkAssignmentId.Value; HoursPerWorkAssignmentModel existing = model.Where( m => m.WorkAssignmentId == assignmentId).FirstOrDefault(); if (existing != null) { existing.TotalHours += (timesheet.StopTime.Value - timesheet.StartTime.Value).TotalHours; } else { existing = new HoursPerWorkAssignmentModel() { WorkAssignmentId = assignmentId, WorkAssignmentName = timesheet.WorkAssignments.Title, TotalHours = (timesheet.StopTime.Value - timesheet.StartTime.Value).TotalHours }; model.Add(existing); } } return(View(model)); } finally { entities.Dispose(); } }
public JsonResult GetList() { panconDatabaseEntities entities = new panconDatabaseEntities(); var model = (from e in entities.Employees select new { EmployeeId = e.EmployeeId, Firstname = e.Firstname, Lastname = e.Lastname }).ToList(); string json = JsonConvert.SerializeObject(model); entities.Dispose(); return(Json(json, JsonRequestBehavior.AllowGet)); }
public string[] GetAll() { panconDatabaseEntities entities = new panconDatabaseEntities(); string[] contractors = null; try { contractors = (from c in entities.Contractors where (c.Active == true) select c.CompanyName).ToArray(); } finally { entities.Dispose(); } return(contractors); }
public List <string> TimeSheetList(int tsID) { panconDatabaseEntities entities = new panconDatabaseEntities(); List <string> realList = new List <string>(); tsID = 2; string[] sheetId = (from ts in entities.Timesheets where (ts.WorkComplete == true) select ts.ContractorId.ToString() + " " + ts.EmployeeId.ToString() + " " + ts.WorkAssignmentId.ToString()).ToArray(); try { foreach (var item in sheetId) { string[] data = item.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); string contractor = data[0]; string contr = (from c in entities.Contractors where (c.ContractorId.ToString() == contractor) select c.CompanyName).Single(); string employee = data[1]; string emp = (from e in entities.Employees where (e.EmployeeId.ToString() == employee) select e.Firstname + " " + e.Lastname).Single(); string workid = data[2]; string work = (from w in entities.WorkAssignments where (w.WorkAssignmentId.ToString() == workid) select w.Title).Single(); realList.Add(contr + " | " + emp + " | " + work + " " + "( " + workid + " )"); } } finally { entities.Dispose(); } return(realList); }
public ActionResult Delete(int id) { panconDatabaseEntities entities = new panconDatabaseEntities(); bool OK = false; Employees dbItem = (from e in entities.Employees where e.EmployeeId == id select e).FirstOrDefault(); if (dbItem != null) { entities.Employees.Remove(dbItem); entities.SaveChanges(); OK = true; } entities.Dispose(); return(Json(OK, JsonRequestBehavior.AllowGet)); }
public string[] GetAll() { panconDatabaseEntities entities = new panconDatabaseEntities(); string[] WorkAssignments = null; try { WorkAssignments = (from wa in entities.WorkAssignments where (wa.Active == true) && (wa.InProgressAt == null) select wa.Title + " Deadline: " + wa.Deadline + " ( " + wa.WorkAssignmentId + " )") .ToArray(); } finally { entities.Dispose(); } return(WorkAssignments); }
/*public string[] GetAll() * { * * string[] employeeNames = null; * panconDatabaseEntities entities = new panconDatabaseEntities(); * try * { * employeeNames = (from e in entities.Employees * where (e.Active == true) * select e.Firstname + " " + * e.Lastname).ToArray(); * } * finally * { * entities.Dispose(); * } * return employeeNames; * }*/ public byte[] GetEmployeeImage(string employeeName) { panconDatabaseEntities entities = new panconDatabaseEntities(); try { string[] nameParts = employeeName.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); string first = nameParts[0]; string last = nameParts[1]; byte[] bytes = (from e in entities.Employees where (e.Active == true) && (e.Firstname == first) && (e.Lastname == last) select e.EmployeePicture).FirstOrDefault(); return(bytes); } finally { entities.Dispose(); } }
public string[] GetWorksInProgress(int id) { panconDatabaseEntities entities = new panconDatabaseEntities(); id = 5; try { string[] chosenWorkData = (from cw in entities.WorkAssignments where (cw.Active == true) && (cw.InProgressAt != null) && (cw.Completed != true) select cw.Title + " | Started at: " + cw.InProgressAt + " ( " + cw.WorkAssignmentId + " )") .ToArray(); return(chosenWorkData); } finally { entities.Dispose(); } }
public string PutEmployeeImage() { panconDatabaseEntities entities = new panconDatabaseEntities(); try { Employees newEmployee = new Employees() { Firstname = "X", Lastname = "MAN", EmployeePicture = File.ReadAllBytes(@"C:\Users\Admin\Pictures\car.png") }; entities.Employees.Add(newEmployee); entities.SaveChanges(); return("OK!"); } finally { entities.Dispose(); } return("Error!"); }
public bool PostCustomer(WorkModel model) { panconDatabaseEntities entities = new panconDatabaseEntities(); try { if (model.CustOperation == "Save") { Customers newEntry = new Customers() { CustomerName = model.CustomerName, ContactPerson = model.ContactPerson, Phone = model.CustomerPhone, Email = model.CustomerEmail, CreatedAt = DateTime.Now, Active = true }; entities.Customers.Add(newEntry); } else if (model.CustOperation == "Modify") { Customers existing = (from c in entities.Customers where (c.CustomerId == model.CustomerId) && (c.Active == true) select c).FirstOrDefault(); if (existing != null) { existing.CustomerName = model.CustomerName; existing.ContactPerson = model.ContactPerson; existing.Phone = model.CustomerPhone; existing.Email = model.CustomerEmail; existing.LastModified = DateTime.Now; } else { return(false); } } else if (model.CustOperation == "Delete") { Customers chosenCustomer = (from cc in entities.Customers where (cc.CustomerName == model.CustomerName) select cc).FirstOrDefault(); if (chosenCustomer == null) { return(false); } int customerId = chosenCustomer.CustomerId; Customers existing = (from e in entities.Customers where (e.CustomerId == customerId) select e).FirstOrDefault(); if (existing != null) { entities.Customers.Remove(existing); } else { return(false); } } entities.SaveChanges(); } catch { return(false); } finally { entities.Dispose(); } return(true); }
public bool PostContractor(WorkModel model) { panconDatabaseEntities entities = new panconDatabaseEntities(); try { if (model.ContOperation == "Save") { Contractors newEntry = new Contractors() { CompanyName = model.ContractorName, ContactPerson = model.ContractorContactPerson, Phone = model.ContractorPhone, Email = model.ContractorEmail, VatId = model.VatId, HourlyRate = int.Parse(model.HourlyRate), Active = true, CreatedAt = DateTime.Now }; entities.Contractors.Add(newEntry); } else if (model.ContOperation == "Modify") { Contractors existing = (from co in entities.Contractors where (co.ContractorId == model.ContractorId) && (co.Active == true) select co).FirstOrDefault(); if (existing != null) { existing.CompanyName = model.ContractorName; existing.ContactPerson = model.ContractorContactPerson; existing.Phone = model.ContractorPhone; existing.Email = model.ContractorEmail; existing.VatId = model.VatId; existing.HourlyRate = int.Parse(model.HourlyRate); existing.LastModified = DateTime.Now; } else { return(false); } } else if (model.ContOperation == "Delete") { Contractors existing = (from e in entities.Contractors where (e.ContractorId == model.ContractorId) select e).FirstOrDefault(); if (existing != null) { entities.Contractors.Remove(existing); } else { return(false); } } entities.SaveChanges(); } catch { return(false); } finally { entities.Dispose(); } return(true); }
public List <string> GetChosenEntity(string Entity) { panconDatabaseEntities entities = new panconDatabaseEntities(); string[] sheet = null; string[] workAssignments = (from wa in entities.WorkAssignments where (wa.Active == true) select wa.Title).ToArray(); string[] employees = (from e in entities.Employees where (e.Active == true) select e.Firstname + " " + e.Lastname).ToArray(); string[] contractors = (from cont in entities.Contractors where (cont.Active == true) select cont.CompanyName).ToArray(); List <string[]> empData = new List <string[]>(); List <string[]> contData = new List <string[]>(); List <string[]> WorkData = new List <string[]>(); WorkData.Add(workAssignments); empData.Add(employees); contData.Add(contractors); foreach (var item in empData) { foreach (var i in item) { if (i.ToString() == Entity) { string emplo = (from e in entities.Employees where (e.Firstname + " " + e.Lastname == Entity) select e.EmployeeId.ToString()).Single(); sheet = (from ts in entities.Timesheets where (ts.EmployeeId.ToString() == emplo) select ts.ContractorId.ToString() + " " + ts.EmployeeId.ToString() + " " + ts.WorkAssignmentId.ToString()).ToArray(); } } } foreach (var itemm in WorkData) { foreach (var it in itemm) { if (it.ToString() == Entity) { string worka = (from w in entities.WorkAssignments where (w.Title == Entity) select w.WorkAssignmentId.ToString()).Single(); sheet = (from ts in entities.Timesheets where (ts.WorkAssignmentId.ToString() == worka) select ts.ContractorId.ToString() + " " + ts.EmployeeId.ToString() + " " + ts.WorkAssignmentId.ToString()).ToArray(); } } } foreach (var iten in contData) { foreach (var ite in iten) { if (ite.ToString() == Entity) { string contra = (from c in entities.Contractors where (c.CompanyName == Entity) select c.ContractorId.ToString()).Single(); sheet = (from ts in entities.Timesheets where (ts.ContractorId.ToString() == contra) select ts.ContractorId.ToString() + " " + ts.EmployeeId.ToString() + " " + ts.WorkAssignmentId.ToString()).ToArray(); } } } List <string> entityList = new List <string>(); try { foreach (var item in sheet) { string[] datas = item.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); string contractor = datas[0]; string contr = (from c in entities.Contractors where (c.ContractorId.ToString() == contractor) select c.CompanyName).Single(); string employee = datas[1]; string emp = (from e in entities.Employees where (e.EmployeeId.ToString() == employee) select e.Firstname + " " + e.Lastname).Single(); string workid = datas[2]; string work = (from w in entities.WorkAssignments where (w.WorkAssignmentId.ToString() == workid) select w.Title).Single(); entityList.Add(contr + " | " + emp + " | " + work + " " + "( " + workid + " )"); } } finally { entities.Dispose(); } return(entityList); }
public bool PostStatus(WorkAssignmentOperationModel input) { panconDatabaseEntities entities = new panconDatabaseEntities(); try { WorkAssignments assignment = (from wa in entities.WorkAssignments where (wa.Active == true) && (wa.Title == input.AssignmentTitle) select wa).FirstOrDefault(); if (assignment == null) { return(false); } if (input.Operation == "Start") { int assignmentId = assignment.WorkAssignmentId; Timesheets NewEntry = new Timesheets() { WorkAssignmentId = assignmentId, StartTime = DateTime.Now, WorkComplete = false, Active = true, CreatedAt = DateTime.Now }; entities.Timesheets.Add(NewEntry); assignment.InProgress = true; assignment.InProgressAt = DateTime.Now; assignment.LastModified = DateTime.Now; } else if (input.Operation == "Stop") { int assignmentId = assignment.WorkAssignmentId; Timesheets existing = (from ts in entities.Timesheets where (ts.WorkAssignmentId == assignmentId) && (ts.Active == true) && (ts.WorkComplete == false) orderby ts.StartTime descending select ts).FirstOrDefault(); if (existing != null) { existing.StopTime = DateTime.Now; existing.WorkComplete = true; existing.LastModified = DateTime.Now; assignment.InProgress = false; assignment.Completed = true; assignment.CompletedAt = DateTime.Now; assignment.LastModified = DateTime.Now; } else { return(false); } } entities.SaveChanges(); } catch { return(false); } finally { entities.Dispose(); } return(true); }
public bool PostEmployee(WorkModel model) { panconDatabaseEntities entities = new panconDatabaseEntities(); try { if (model.EmpOperation == "Save") { Contractors contractor = (from c in entities.Contractors where (c.CompanyName == model.ContractorName) select c).FirstOrDefault(); Employees newEntry = new Employees() { ContractorId = contractor.ContractorId, Username = model.Username, Password = model.Password, Firstname = model.Firstname, Lastname = model.Lastname, Phone = model.Phone.ToString(), Email = model.Email, CreatedAt = DateTime.Now, Active = true, }; entities.Employees.Add(newEntry); } else if (model.EmpOperation == "Modify") { Employees existing = (from e in entities.Employees where (e.EmployeeId == model.EmployeeId) && (e.Active == true) select e).FirstOrDefault(); if (existing != null) { existing.Firstname = model.Firstname; existing.Lastname = model.Lastname; existing.Phone = model.Phone.ToString(); existing.Email = model.Email; existing.LastModified = DateTime.Now; existing.EmployeePicture = model.Picture; } else { return(false); } } else if (model.EmpOperation == "Delete") { Employees existing = (from e in entities.Employees where (e.EmployeeId == model.EmployeeId) select e).FirstOrDefault(); if (existing != null) { entities.Employees.Remove(existing); } else { return(false); } } entities.SaveChanges(); } catch { return(false); } finally { entities.Dispose(); } return(true); }
public bool PostWork(WorkModel model) { panconDatabaseEntities entities = new panconDatabaseEntities(); Customers customer = (from cu in entities.Customers where (cu.Active == true) && (cu.CustomerName == model.CustomerName) select cu).FirstOrDefault(); try { if (model.Operation == "Save") { WorkAssignments newEntry = new WorkAssignments() { CustomerId = customer.CustomerId, Title = model.WorkTitle, Description = model.Description, Deadline = model.Deadline, InProgress = true, CreatedAt = DateTime.Now, Active = true }; entities.WorkAssignments.Add(newEntry); } else if (model.Operation == "Modify") { WorkAssignments existing = (from wa in entities.WorkAssignments where (wa.WorkAssignmentId == model.WorkId) && (wa.Active == true) select wa).FirstOrDefault(); if (existing != null) { existing.Title = model.WorkTitle; existing.Description = model.Description; existing.Deadline = model.Deadline; existing.LastModified = DateTime.Now; } else { return(false); } } else if (model.Operation == "Delete") { WorkAssignments existing = (from wa in entities.WorkAssignments where (wa.WorkAssignmentId == model.WorkId) select wa).FirstOrDefault(); if (existing != null) { entities.WorkAssignments.Remove(existing); } else { return(false); } } else if (model.Operation == "Assign") { WorkAssignments assignment = (from wa in entities.WorkAssignments where (wa.WorkAssignmentId == model.WorkId) && (wa.Active == true) && (wa.InProgress == true) select wa).FirstOrDefault(); if (assignment == null) { return(false); } Employees emp = (from e in entities.Employees where (e.EmployeeId == model.EmployeeId) select e).FirstOrDefault(); if (emp == null) { return(false); } int workId = assignment.WorkAssignmentId; int customerId = assignment.CustomerId.Value; assignment.InProgressAt = DateTime.Now; Timesheets newEntry = new Timesheets() { CustomerId = customerId, ContractorId = emp.ContractorId, EmployeeId = emp.EmployeeId, WorkAssignmentId = workId, StartTime = DateTime.Now, CreatedAt = DateTime.Now, Active = true, WorkComplete = false }; entities.Timesheets.Add(newEntry); } else if (model.Operation == "MarkComplete") { WorkAssignments assignment = (from wa in entities.WorkAssignments where (wa.WorkAssignmentId == model.WorkId) && (wa.Active == true) && (wa.InProgress == true) && (wa.InProgressAt != null) select wa).FirstOrDefault(); if (assignment == null) { return(false); } int workId = assignment.WorkAssignmentId; int customerId = assignment.CustomerId.Value; assignment.CompletedAt = DateTime.Now; assignment.Completed = true; assignment.InProgress = false; Timesheets existing = (from ts in entities.Timesheets where (ts.WorkAssignmentId == workId) && (ts.CustomerId == customerId) select ts).FirstOrDefault(); if (existing != null) { existing.WorkComplete = true; existing.StopTime = DateTime.Now; existing.LastModified = DateTime.Now; existing.Comments = "Work set to completed by Admin"; } } entities.SaveChanges(); } catch { return(false); } finally { entities.Dispose(); } return(true); }