public ActionResult SaveMissionRequest(ErrandFormRequestViewModel model, OptionsViewModel moreInfo, bool clear = false) { List <Error> errors = new List <Error>(); if (ModelState.IsValid) { if (ServerValidationEnabled) { errors = _hrUnitOfWork.CompanyRepository.CheckForm(new CheckParm { CompanyId = CompanyId, ObjectName = "ErrandRequest", TableName = "ErrandRequests", Columns = Models.Utils.GetColumnViews(ModelState).Where(s => s.Name != "MultiDays").ToList(), ParentColumn = "CompanyId", Culture = Language }); if (errors.Count() > 0) { foreach (var e in errors) { foreach (var errorMsg in e.errors) { ModelState.AddModelError(errorMsg.field, errorMsg.message); } } return(Json(Models.Utils.ParseFormErrors(ModelState))); } } bool IsValid = true; string conflictMessage = string.Empty; string message = "OK"; if (model.MultiDays) { // check Server date with Start date if (model.StartDate.Date < DateTime.Today.Date) { ModelState.AddModelError("StartDate", MsgUtils.Instance.Trls("Errand DateLess than Now")); return(Json(Models.Utils.ParseFormErrors(ModelState))); } IsValid = Constants.CheckDateRangAndTasksConflict.IsValid(_hrUnitOfWork, model.EmpId.ToString(), Constants.Sources.Errand, model.Id, model.StartDate.Date, model.EndDate.Date, DateTime.Today, Language, out conflictMessage); } else { var currentime = DateTime.Now.GetUniversalDatetime(User.Identity.GetTimeZone()); //TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById(User.Identity.GetTimeZone())); if (Convert.ToDateTime(model.StartTime) < currentime) { ModelState.AddModelError("StartTime", MsgUtils.Instance.Trls("Start Less than Now")); return(Json(Models.Utils.ParseFormErrors(ModelState))); } IsValid = Constants.CheckDateRangAndTasksConflict.IsValid(_hrUnitOfWork, model.EmpId.ToString(), Constants.Sources.Errand, model.Id, Convert.ToDateTime(model.StartTime), Convert.ToDateTime(model.EndTime), DateTime.Today, Language, out conflictMessage); } if (!IsValid) { ModelState.AddModelError("", conflictMessage); return(Json(Models.Utils.ParseFormErrors(ModelState))); } var record = _hrUnitOfWork.Repository <ErrandRequest>().FirstOrDefault(j => j.Id == model.Id); var isRequired = _hrUnitOfWork.Repository <Workflow>().Where(w => w.Source == "ErrandRequest" && w.CompanyId == CompanyId).Select(a => a.IsRequired).FirstOrDefault(); if (record == null) { record = new ErrandRequest(); AutoMapper(new Models.AutoMapperParm { Destination = record, Source = model, ObjectName = "ErrandRequest", Options = moreInfo, Transtype = TransType.Insert }); record.CreatedTime = DateTime.Now; record.CreatedUser = UserName; record.CompanyId = CompanyId; record.ApprovalStatus = (byte)(model.submit ? (isRequired ? 2 : 6) : 1); if (model.MultiDays) { record.StartDate = model.StartDate; record.EndDate = model.EndDate; } else { record.StartDate = Convert.ToDateTime(model.StartTime); record.EndDate = Convert.ToDateTime(model.EndTime); } _hrUnitOfWork.MissionRepository.Add(record); } else { AutoMapper(new Models.AutoMapperParm { Destination = record, Source = model, ObjectName = "ErrandRequest", Options = moreInfo, Transtype = TransType.Update }); record.ModifiedTime = DateTime.Now; record.ModifiedUser = UserName; record.CompanyId = CompanyId; record.ApprovalStatus = (byte)(model.submit ? (isRequired ? 2 : 6) : 1); if (model.MultiDays) { record.StartDate = model.StartDate; record.EndDate = model.EndDate; } else { record.StartDate = Convert.ToDateTime(model.StartTime); record.EndDate = Convert.ToDateTime(model.EndTime); } _hrUnitOfWork.MissionRepository.Attach(record); _hrUnitOfWork.MissionRepository.Entry(record).State = EntityState.Modified; } if (errors.Count > 0) { return(Json(errors.First().errors.First().message)); } var trans = _hrUnitOfWork.BeginTransaction(); var Errors = SaveChanges(Language); if (Errors.Count > 0) { message = Errors.First().errors.First().message; trans.Rollback(); trans.Dispose(); return(Json(message)); } #region workflow if (isRequired && model.submit) { WfViewModel wf = new WfViewModel() { Source = "ErrandRequest", SourceId = record.CompanyId, DocumentId = record.Id, RequesterEmpId = record.EmpId, ApprovalStatus = 2, CreatedUser = UserName, }; var wfTrans = _hrUnitOfWork.LeaveRepository.AddWorkFlow(wf, Language); if (wfTrans == null && wf.WorkFlowStatus != "Success") { record.ApprovalStatus = 1; message += "," + (new JavaScriptSerializer()).Serialize(new { model = record, error = wf.WorkFlowStatus }); _hrUnitOfWork.MissionRepository.Attach(record); _hrUnitOfWork.MissionRepository.Entry(record).State = EntityState.Modified; } else if (wfTrans != null) { _hrUnitOfWork.LeaveRepository.Add(wfTrans); } errors = Save(Language); if (Errors.Count > 0) { message = Errors.First().errors.First().message; trans.Rollback(); trans.Dispose(); return(Json(message)); } } #endregion if (message == "OK") { if (clear) { model = new ErrandFormRequestViewModel(); message += "," + (new JavaScriptSerializer()).Serialize(model); } else { message += "," + (new JavaScriptSerializer()).Serialize(record); } trans.Commit(); } return(Json(message)); } else { return(Json(Models.Utils.ParseFormErrors(ModelState))); } }
public ActionResult Details(LeaveReqViewModel model, OptionsViewModel moreInfo, bool clear) { List <Error> errors = new List <Error>(); if (ModelState.IsValid) { if (ServerValidationEnabled) { errors = _hrUnitOfWork.CompanyRepository.CheckForm(new CheckParm { CompanyId = CompanyId, ObjectName = "LeaveRequest", TableName = "LeaveRequests", Columns = Models.Utils.GetColumnViews(ModelState.Where(a => !a.Key.Contains('.'))), ParentColumn = "CompanyId", Culture = Language }); if (errors.Count() > 0) { foreach (var e in errors) { foreach (var errorMsg in e.errors) { ModelState.AddModelError(errorMsg.field, errorMsg.message); } } return(Json(Models.Utils.ParseFormErrors(ModelState))); } } } else { return(Json(Models.Utils.ParseFormErrors(ModelState))); } LeaveRequest request = _hrUnitOfWork.LeaveRepository.Get(model.Id); var type = _hrUnitOfWork.LeaveRepository.GetLeaveType(model.TypeId); byte version; byte.TryParse(Request.Form["version"], out version); string message = "OK"; if (type.AllowFraction && model.DayFraction != 0) { switch (model.DayFraction) { case 1: case 3: model.NofDays = 0.25f; break; case 2: case 4: model.NofDays = 0.5f; break; } } ///Save if (model.Id == 0) { /// New request = new LeaveRequest(); model.BalanceBefore = model.BalBefore; AutoMapperParm parms = new AutoMapperParm() { Source = model, Destination = request, Version = version, ObjectName = "LeaveRequest", Options = moreInfo, Transtype = TransType.Insert }; AutoMapper(parms); request.CompanyId = CompanyId; request.PeriodId = _hrUnitOfWork.LeaveRepository.GetLeaveRequestPeriod(type.CalendarId, request.StartDate, Language, out message); request.ApprovalStatus = (byte)(model.submit ? (type.ExWorkflow ? 6 : 2) : 1); //ApprovalStatus 1- New, 2- Submit 6- Approved //AbsenceType 8- Casual model.CompanyId = request.CompanyId; model.PeriodId = request.PeriodId; model.ApprovalStatus = request.ApprovalStatus; if (type.ExWorkflow && model.submit) { request.ActualStartDate = request.StartDate; request.ActualEndDate = request.EndDate; request.ActualNofDays = request.NofDays; model.ActualStartDate = request.StartDate; model.ActualEndDate = request.EndDate; model.ActualNofDays = request.NofDays; } request.CreatedUser = UserName; request.CreatedTime = DateTime.Now; _hrUnitOfWork.LeaveRepository.Add(request); if (!type.ExWorkflow && !clear && request.ApprovalStatus == 1) { model.ForceUpload = _hrUnitOfWork.Repository <RequestWf>().Where(a => a.Source == "Leave" && a.SourceId == type.Id).Select(a => a.ForceUpload).FirstOrDefault() ? 1 : 0; } } else { /// Edit AutoMapperParm parms = new AutoMapperParm() { Source = model, Destination = request, Version = version, ObjectName = "LeaveRequest", Options = moreInfo, Transtype = TransType.Update }; AutoMapper(parms); if (model.submit) { _hrUnitOfWork.TrainingRepository.AddTrail(new AddTrailViewModel() { ColumnName = "ApprovalStatus", CompanyId = CompanyId, ObjectName = "LeaveRequest", SourceId = request.Id.ToString(), UserName = UserName, Version = Convert.ToByte(Request.Form["Version"]), ValueAfter = MsgUtils.Instance.Trls("Submit"), ValueBefore = MsgUtils.Instance.Trls("Darft"), Transtype = (byte)TransType.Update }); } request.ApprovalStatus = (byte)(model.submit ? (type.ExWorkflow ? 6 : 2) : model.ApprovalStatus); //1- New, 2- Submit 6- Approved //AbsenceType 8- Casual model.ApprovalStatus = request.ApprovalStatus; request.ModifiedUser = UserName; request.ModifiedTime = DateTime.Now; if (type.ExWorkflow && model.submit) { request.ActualStartDate = request.StartDate; request.ActualEndDate = request.EndDate; request.ActualNofDays = request.NofDays; model.ActualStartDate = request.StartDate; model.ActualEndDate = request.EndDate; model.ActualNofDays = request.NofDays; } _hrUnitOfWork.LeaveRepository.Attach(request); _hrUnitOfWork.LeaveRepository.Entry(request).State = EntityState.Modified; } if (model.submit && type.ExWorkflow && request.ApprovalStatus == 1) { _hrUnitOfWork.LeaveRepository.AddAcceptLeaveTrans(request, UserName); } // update using transactions var trans = _hrUnitOfWork.BeginTransaction(); // #First save changes var Errors = SaveChanges(Language); if (Errors.Count > 0) { message = Errors.First().errors.First().message; trans.Rollback(); trans.Dispose(); // return string error message return(Json(message)); } model.Id = request.Id; if (model.submit) //Casual && type.AbsenceType != 8 { WfViewModel wf = new WfViewModel() { Source = "Leave", SourceId = request.TypeId, DocumentId = request.Id, RequesterEmpId = request.EmpId, ApprovalStatus = 2, CreatedUser = UserName, }; var wfTrans = _hrUnitOfWork.LeaveRepository.AddWorkFlow(wf, Language); if (wfTrans == null && wf.WorkFlowStatus != "Success") { request.ApprovalStatus = 1; message += "," + (new JavaScriptSerializer()).Serialize(new { model = request, error = wf.WorkFlowStatus }); _hrUnitOfWork.LeaveRepository.Attach(request); _hrUnitOfWork.LeaveRepository.Entry(request).State = EntityState.Modified; } else if (wfTrans != null) { _hrUnitOfWork.LeaveRepository.Add(wfTrans); } // #Second Save changes Errors = Save(Language); if (Errors.Count > 0) { message = Errors.First().errors.First().message; trans.Rollback(); trans.Dispose(); // return string error message return(Json(message)); } } if (clear) { model = new LeaveReqViewModel(); } if (message == "OK") { // send OK,model message += "," + ((new JavaScriptSerializer()).Serialize(new { model = model })); trans.Commit(); } return(Json(message)); }