public bool InsertOrUpdateApproval(tbl_Taxi_Approval_Infor approval) { try { var approveCheck = db.tbl_Taxi_Approval_Infor.FirstOrDefault(x => x.Request_Id == approval.Request_Id && x.Process == approval.Process); if (approveCheck == null) { db.tbl_Taxi_Approval_Infor.Add(approval); } else { approveCheck.ApproverId = approval.ApproverId; approveCheck.Process = approval.Process; approveCheck.State = approval.State; approveCheck.UpdateDate = approval.UpdateDate; approveCheck.Remark = approval.Remark; } db.SaveChanges(); return(true); } catch (Exception ex) { WriteLogError.Write("InsertOrUpdateApproval", ex.ToString()); return(false); } }
public ActionResult Approval(List <ApprovalModel> approval) { bool check = true; foreach (var item in approval) { if (item.type != 0) { // set approval tbl_Taxi_Approval_Infor approve = new tbl_Taxi_Approval_Infor(); approve.Request_Id = item.Id; approve.Process = item.process; approve.ApproverId = mEmployee.employee.employee_id; approve.Remark = (item.comment != null) ? item.comment.ToString() : null; approve.UpdateDate = DateTime.Now; approve.State = item.type; var approvalDao = new Taxi_ApprovalDao().InsertOrUpdateApproval(approve); // If approve process 2 -> insert new row var resultNew = true; if (item.type == 1 && item.process == 1) { tbl_Taxi_Approval_Infor approveNew = new tbl_Taxi_Approval_Infor(); approveNew.Request_Id = item.Id; approveNew.Process = 2; approveNew.UpdateDate = DateTime.Now; approveNew.State = 0; approveNew.DepartmentName = DepartmentLevel2; resultNew = new Taxi_ApprovalDao().InsertOrUpdateApproval(approveNew); } if (!approvalDao || resultNew == false) { check = false; } } } if (check == true) { Notification("Success", "Approval request successfully", MyConstants.NOTIFY_SUCCESS); TempData["ApprovalTotalL1"] = new Taxi_ApprovalDao().GetTotalApproval(mEmployee.employee.employee_id, 1); TempData["ApprovalTotalL2"] = new Taxi_ApprovalDao().GetTotalApproval(mEmployee.employee.employee_id, 2); } else { Notification("False", "Approval request failure, error: ", MyConstants.NOTIFY_ERROR); } return(RedirectToAction("Approval")); }
public ActionResult CreateRequest(CreateRequestModel request) { try { // Insert request infor tbl_Taxi_Request_Infor taxiInfor = new tbl_Taxi_Request_Infor(); taxiInfor.Id = request.Id; taxiInfor.EmployeeId = request.EmployeeId; taxiInfor.Purpose = request.Purpose; taxiInfor.ScheduleTime = request.ScheduleTime; taxiInfor.Head = adWebHelper.GetHeadOfFunction(mEmployee.employee.access_token, mEmployee.employee.departmentName).ad_user_displayName; taxiInfor.Pickup = request.Pickup; taxiInfor.DropOff1 = request.DropOff1; taxiInfor.DropOff2 = request.DropOff2; taxiInfor.CreateDate = DateTime.Now; taxiInfor.Remark = request.Remark; var resultRequest = new Taxi_RequestDao().InsertOrUpdateRequest(taxiInfor); // Insert personal infor var checkInsertDetail = true; if (request.ListVisitorCSV != "" && request.ListVisitorCSV != null) { System.IO.File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + "\\TempCSV.csv", request.ListVisitorCSV, System.Text.Encoding.UTF8); List <CSVModel> values = System.IO.File.ReadAllLines(AppDomain.CurrentDomain.BaseDirectory + "\\TempCSV.csv") .Skip(1) .Select(v => FromCsv(v)) .ToList(); foreach (var item in values) { if (checkInsertDetail == true) { tbl_Taxi_User_Infor user = new tbl_Taxi_User_Infor(); user.Name = item.VisitorName; user.EmployeeId = item.NationalId; user.SLM_Name = item.CompanyName; user.Taxi_Request_Infor_Id = request.Id; user.Remark = item.Remark; user.Taxi_Request_Infor_Id = request.Id; // Two record if register dropoff 2 if (request.DropOff2 != "" && request.DropOff2 != null) { user.Process = 1; checkInsertDetail = new Taxi_UserDao().InsertOrUpdateUser(user); user.Process = 2; } checkInsertDetail = new Taxi_UserDao().InsertOrUpdateUser(user); } } } else { foreach (var item in request.Visitor) { if (checkInsertDetail == true) { item.Taxi_Request_Infor_Id = request.Id; // Two record if register dropoff 2 if (request.DropOff2 != "" && request.DropOff2 != null) { item.Process = 1; checkInsertDetail = new Taxi_UserDao().InsertOrUpdateUser(item); item.Process = 2; } checkInsertDetail = new Taxi_UserDao().InsertOrUpdateUser(item); } } } // Insert approval infor tbl_Taxi_Approval_Infor approval = new tbl_Taxi_Approval_Infor(); approval.Request_Id = (Guid)request.Id; approval.DepartmentName = mEmployee.employee.departmentName; approval.Process = 1; approval.State = 0; approval.UpdateDate = DateTime.Now; var resultApproval = new Taxi_ApprovalDao().InsertOrUpdateApproval(approval); if (resultRequest == true && resultApproval == true && checkInsertDetail == true) { Notification("Success", "Create request successfully", MyConstants.NOTIFY_SUCCESS); return(RedirectToAction("Index", "Home", new { type = 0 })); } else { //Rollback - delete when failure var approvalDeleteResult = new Taxi_ApprovalDao().RemoveApprovalByRequestId(request.Id); var userDeleteResult = new Taxi_UserDao().RemoveUserByRequestId(request.Id); var requestDeleteResult = new Taxi_RequestDao().RemoveRequest(request.Id); WriteLogError.Write("Insert Request Failure: ", "Request: " + resultRequest + " Visitor: " + checkInsertDetail + " Approval: " + resultApproval); WriteLogError.Write("Rollback Request Failure: ", "Request: " + requestDeleteResult + " Visitor: " + userDeleteResult + " Approval: " + approvalDeleteResult); Notification("False", "Create request failure, Please recheck all information!", MyConstants.NOTIFY_ERROR); } } catch (Exception ex) { Notification("False", "Create request failure, error: " + ex.ToString(), MyConstants.NOTIFY_ERROR); } return(RedirectToAction("CreateRequest", "Request")); }