/// <summary> /// Will call this in case of insert new record is falied /// </summary> /// <param name="prefixFor">Enum PrefixFor</param> public static void LastIdRemoveOne(PrefixForEnum prefixFor) { SetupEntities db = new SetupEntities(); int prefixForId = (int)prefixFor; var lastIndexObj = db.PrefixLastIds.Where(x => x.PrefixForId == prefixForId).FirstOrDefault(); lastIndexObj.LastId = lastIndexObj.LastId - 1; db.SaveChanges(); }
// CRUD Operation For Country public JsonResult CRUDCountry(IList <Country> objCountry, string _Mode) { Country _Country = new Country(); var _List = new object(); var _UserRoles = Session["UserRoleByForm"]; var _UserInfo = Session["UserInfo"]; if (ObjUserRole != null) { if (objCountry != null) { if (objCountry.Count > 0) { // Set Properties in Object _Country = objCountry[0]; _Country.MakerWorkStationId = Dns.GetHostName(); } if (_Mode == "D") { // For Delete dbSetup.Countries.Remove(dbSetup.Countries.Find(_Country.CountryId)); dbSetup.SaveChanges(); } else if (objCountry.Count > 0 && objCountry[0].CountryId > 0) { _Country.UpdatedBy = ((SMS_User)_UserInfo).UserId; _Country.UpdatedDate = DateTime.Now; // For Update dbSetup.Entry(_Country).State = EntityState.Modified; dbSetup.SaveChanges(); } else if (_Mode == "I") { _Country.CreatedBy = ((SMS_User)_UserInfo).UserId; _Country.CreatedDate = DateTime.Now; // For Insert dbSetup.Countries.Add(_Country); dbSetup.SaveChanges(); } } else { _List = dbSetup.Countries.ToList(); } } return(Json(new { Country = _List, _UserRoles }, JsonRequestBehavior.AllowGet)); }
public static string AddEditPrefix(FormCollection form) { string isSaved = "true"; SetupEntities db = new SetupEntities(); var count = db.PrefixSetups.Count(); PrefixSetup prefixSetupDb; for (int i = 0; i < count - 1; i++) { //prefix for Id int prefixForId = int.Parse(form["PrefixSetup[" + i + "].PrefixForId"]); prefixSetupDb = db.PrefixSetups.Where(x => x.PrefixForId == prefixForId).FirstOrDefault(); prefixSetupDb.Delimiter = form["PrefixSetup[" + i + "].Delimiter"]; prefixSetupDb.IncludeMonth = bool.Parse(form["PrefixSetup[" + i + "].IncludeMonth"]); prefixSetupDb.IncludeYear = bool.Parse(form["PrefixSetup[" + i + "].IncludeYear"]); prefixSetupDb.NumberAfterChar = bool.Parse(form["PrefixSetup[" + i + "].NumberAfterChar"]); prefixSetupDb.PrefixChar = form["PrefixSetup[" + i + "].PrefixChar"]; prefixSetupDb.ResetNumberEvery = form["PrefixSetup[" + i + "].ResetNumberEvery"]; } db.SaveChanges(); return(isSaved); }
public static string AssignUserIdToEmpId(int empId, string userId) { string isSaved = "true"; SetupEntities db = new SetupEntities(); UserEmployee userEmp = new UserEmployee() { EmpId = empId, UserId = userId }; db.UserEmployees.Add(userEmp); try { db.SaveChanges(); } catch (Exception ex) { isSaved = "false" + ex.Message; throw; } return(isSaved); }
public static string GeneratePrefixCode(PrefixForEnum prefixFor, bool forSubmit) { StringBuilder prefixCode = new StringBuilder(); SetupEntities db = new SetupEntities(); int prefixForId = (int)prefixFor; PrefixSetup prefixSetup = db.PrefixSetups.Include("PrefixLastId") .Where(x => x.PrefixForId == prefixForId).FirstOrDefault(); //Take the Id PrefixLastId prefixLastIdObj = prefixSetup.PrefixLastId; string currentId = prefixLastIdObj.LastId.ToString(); //Reset Id every DateTime lastUpdateDate = prefixLastIdObj.LastUpdateDate.Value; if (prefixSetup.ResetNumberEvery == "month") { //If reset is active .. must include month and year .. even if it was wrong inseted in the setup // to avoid duplicate codes prefixSetup.IncludeMonth = true; prefixSetup.IncludeYear = true; if (lastUpdateDate.Month != DateTime.Now.Month) // New record in new month .. reset the ID { currentId = "1"; } } if (prefixSetup.ResetNumberEvery == "year") { //If reset is active .. must include year .. even if it was wrong inseted in the setup // to avoid duplicate codes // prefixSetup.IncludeMonth = true; prefixSetup.IncludeYear = true; if (lastUpdateDate.Year != DateTime.Now.Year) // New record in new year .. reset the ID { currentId = "1"; } } if (!forSubmit) { currentId = "??"; } //build the prefix if (prefixSetup.NumberAfterChar.Value) { prefixCode.Append(prefixSetup.PrefixChar); prefixCode.Append(currentId); } else { prefixCode.Append(currentId); prefixCode.Append(prefixSetup.PrefixChar); } if (prefixSetup.IncludeMonth.Value) { prefixCode.Append(prefixSetup.Delimiter); prefixCode.Append(DateTime.Now.Month); } if (prefixSetup.IncludeYear.Value) { prefixCode.Append(prefixSetup.Delimiter); prefixCode.Append(DateTime.Now.Year); } if (forSubmit) { if (prefixSetup.ResetNumberEvery == "never") { //update lastId = lastId +1 prefixLastIdObj.LastId = prefixLastIdObj.LastId + 1; } else if (prefixSetup.ResetNumberEvery == "month") { if (lastUpdateDate.Month != DateTime.Now.Month) // New record in new month .. reset the ID { prefixLastIdObj.LastId = 2; // one is alrady taken ..static codeing above } else // reset monthly but already Id rested by anoth record { prefixLastIdObj.LastId = prefixLastIdObj.LastId + 1; } } if (prefixSetup.ResetNumberEvery == "year") { if (lastUpdateDate.Year != DateTime.Now.Year) // New record in new year .. reset the ID { prefixLastIdObj.LastId = 2; // one is alrady taken ..static codeing above } else // reset yearly but already Id rested by anoth record { prefixLastIdObj.LastId = prefixLastIdObj.LastId + 1; } } prefixLastIdObj.LastUpdateDate = DateTime.Now; try { db.SaveChanges(); } catch { throw; } } return(prefixCode.ToString()); }
internal static string AddEditSecRights(System.Web.Mvc.FormCollection form) { string isSaved = "true"; int empId = int.Parse(form["EmpId"]); string userId = GetUserIdByEmpId(empId); //Will be added into AspNetUserRoles to allow edit for closde operations string isSuperUser = form["IsSuperUser"]; SetupEntities db = new SetupEntities(); ScreenActionUser actionUser; foreach (var key in form.AllKeys) { if (key.StartsWith("ActionId")) { string index = key.Split('[').ToArray()[1].Replace("]", ""); int screenId = int.Parse(form["ScreenId[" + index + "]"]); var userActionsDb = db.ScreenActionUsers.Where(x => x.UserId == userId && x.ScreenId == screenId).ToList(); foreach (var item in userActionsDb) { db.ScreenActionUsers.Remove(item); } string actionId = form[key]; if (actionId.Contains(",")) { string[] actionIds = actionId.Split(',').ToArray(); for (int i = 0; i < actionIds.Length; i++) { actionUser = new ScreenActionUser() { ActionId = int.Parse(actionIds[i]), ScreenId = screenId, UserId = userId }; db.ScreenActionUsers.Add(actionUser); } } else { actionUser = new ScreenActionUser() { ActionId = int.Parse(actionId), ScreenId = screenId, UserId = userId }; db.ScreenActionUsers.Add(actionUser); } } } //Check if added as super user before bool isExists = db.AspNetUserRoles.Any(x => x.UserId == userId && x.RoleId == "1"); if (!isExists && isSuperUser.ToLower() == "true") { AspNetUserRole aspNetUserRole = new AspNetUserRole(); aspNetUserRole.RoleId = "1"; aspNetUserRole.UserId = userId; db.AspNetUserRoles.Add(aspNetUserRole); } else if (isExists && isSuperUser.ToLower() == "false") // removed from super user role { var aspNetUserRole = db.AspNetUserRoles.Where(x => x.UserId == userId && x.RoleId == "1").FirstOrDefault(); db.AspNetUserRoles.Remove(aspNetUserRole); } try { db.SaveChanges(); } catch (Exception ex) { isSaved = "false " + ex.Message; } return(isSaved); }