public ActionResult EditSafetyConcern([Bind(Include = "SafetyConcernGUID,Area,SubArea,ConditionNoted,CorrectiveAction,Severity")] SafetyConcern safetyConcern, string typeSubType, string typeSubType2, HttpPostedFileBase ImageFile) { if (Session["username"] != null && (Convert.ToInt32(Session["department"]) == constant.DEPARTMENT_HR_SAFETY || Convert.ToInt32(Session["department"]) == constant.DEPARTMENT_SUPER_ADMIN)) { using (TrailerEntities db = new TrailerEntities()) { var concern = db.SafetyConcerns.FirstOrDefault(c => c.SafetyConcernGUID == safetyConcern.SafetyConcernGUID); concern.Area = safetyConcern.Area; if (safetyConcern.SubArea == "") { concern.SubArea = null; } else { concern.SubArea = safetyConcern.SubArea; } if (safetyConcern.ConditionNoted != null) { if (!safetyConcern.ConditionNoted.EndsWith(".")) { safetyConcern.ConditionNoted += "."; } concern.ConditionNoted = safetyConcern.ConditionNoted; } if (safetyConcern.CorrectiveAction != null) { if (!safetyConcern.CorrectiveAction.EndsWith(".")) { safetyConcern.CorrectiveAction += "."; } concern.CorrectiveAction = safetyConcern.CorrectiveAction; } concern.Severity = safetyConcern.Severity; if (ImageFile != null) { if (ImageFile.ContentLength > 0) { var extension = Path.GetExtension(ImageFile.FileName); var fullPath = Server.MapPath("~/SafetyImages/") + concern.Area + " " + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + extension.ToLower(); var path = concern.Area + " " + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + extension.ToLower(); var oldPath = Server.MapPath("~/SafetyImages/" + concern.ImagePath); if (System.IO.File.Exists(oldPath)) { System.IO.File.Delete(oldPath); } ImageFile.SaveAs(fullPath); concern.ImagePath = path; } } var violations = db.CodeViolations.Where(v => v.SafetyConcernGUID == concern.SafetyConcernGUID); if (typeSubType != "") { foreach (var v in violations) { db.CodeViolations.Remove(v); } var violation = db.SafetyCodes.FirstOrDefault(d => d.TypeSubType == typeSubType); CodeViolation newViolation = new CodeViolation() { SafetyConcernGUID = concern.SafetyConcernGUID, Type = violation.Type, OshaCode = violation.OshaCode, Description = violation.Description, TypeSubType = violation.TypeSubType, }; db.CodeViolations.Add(newViolation); } if (typeSubType2 != "") { var violation = db.SafetyCodes.FirstOrDefault(d => d.TypeSubType == typeSubType2); CodeViolation newViolation = new CodeViolation() { SafetyConcernGUID = concern.SafetyConcernGUID, Type = violation.Type, OshaCode = violation.OshaCode, Description = violation.Description, TypeSubType = violation.TypeSubType, }; db.CodeViolations.Add(newViolation); } db.SaveChanges(); return(RedirectToAction(actionName: "SafetyAudit", controllerName: "Safety", routeValues: new { area = safetyConcern.Area })); } } else if (Convert.ToInt32(Session["department"]) != constant.DEPARTMENT_HR_SAFETY || Convert.ToInt32(Session["department"]) != constant.DEPARTMENT_SUPER_ADMIN) { return(RedirectToAction(actionName: "InsufficientPermissions", controllerName: "Error")); } else { return(RedirectToAction(actionName: "SignIn", controllerName: "Users")); } }
public ActionResult CreateSafetyConcern(string departments, string areaNote, string conditionNoted, string typeSubType, string typeSubType2, string correctiveAction, string severity, HttpPostedFileBase ImageFile) { if (Session["username"] != null && (Convert.ToInt32(Session["department"]) == constant.DEPARTMENT_HR_SAFETY || Convert.ToInt32(Session["department"]) == constant.DEPARTMENT_SUPER_ADMIN)) { using (TrailerEntities db = new TrailerEntities()) { var path = ""; if (ImageFile != null) { if (ImageFile.ContentLength > 0) { var extension = Path.GetExtension(ImageFile.FileName); var fullPath = Server.MapPath("~/SafetyImages/") + departments + " " + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + extension.ToLower(); path = departments + " " + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + extension.ToLower(); ImageFile.SaveAs(fullPath); } } if (!conditionNoted.EndsWith(".") && conditionNoted != "") { conditionNoted += "."; } if (!correctiveAction.EndsWith(".") && correctiveAction != "") { correctiveAction += "."; } SafetyConcern newConcern = new SafetyConcern() { Area = departments, ConditionNoted = conditionNoted, CorrectiveAction = correctiveAction, ViolationCount = 1, Status = "OPEN", DateOpened = DateTime.Now.ToString("yyyy-MM-dd"), ImagePath = path, }; if (areaNote == "") { newConcern.SubArea = null; } else { newConcern.SubArea = areaNote; } if (severity == "") { newConcern.Severity = "LOW"; } else { newConcern.Severity = severity; } db.SafetyConcerns.Add(newConcern); db.SaveChanges(); if (typeSubType != "") { var violation = db.SafetyCodes.FirstOrDefault(d => d.TypeSubType == typeSubType); CodeViolation newViolation = new CodeViolation() { SafetyConcernGUID = newConcern.SafetyConcernGUID, Type = violation.Type, OshaCode = violation.OshaCode, Description = violation.Description, TypeSubType = violation.TypeSubType, }; db.CodeViolations.Add(newViolation); } if (typeSubType2 != "") { var violation = db.SafetyCodes.FirstOrDefault(d => d.TypeSubType == typeSubType2); CodeViolation newViolation = new CodeViolation() { SafetyConcernGUID = newConcern.SafetyConcernGUID, Type = violation.Type, OshaCode = violation.OshaCode, Description = violation.Description, TypeSubType = violation.TypeSubType, }; db.CodeViolations.Add(newViolation); } db.SaveChanges(); return(RedirectToAction(actionName: "SafetyAudit", controllerName: "Safety", routeValues: new { area = departments })); } } else if (Convert.ToInt32(Session["department"]) != constant.DEPARTMENT_HR_SAFETY || Convert.ToInt32(Session["department"]) != constant.DEPARTMENT_SUPER_ADMIN) { return(RedirectToAction(actionName: "InsufficientPermissions", controllerName: "Error")); } else { return(RedirectToAction(actionName: "SignIn", controllerName: "Users")); } }