/// <summary> /// // Delete User To Org /// </summary> /// <param name="OrgUser"></param> /// <returns></returns> public int DeleteUserOrg(TblOrganizationUser OrgUser) { int result = 0; try { TblOrganizationUser OrgChk = db.TblOrganizationUser.Where(r => r.OrganizationId == OrgUser.OrganizationId && r.UserId == OrgUser.UserId).FirstOrDefault(); if (OrgChk != null) { db.TblOrganizationUser.Remove(OrgChk); db.SaveChanges(); result = 1; } return(result); } catch (Exception ex) { return(result); throw ex; } }
/// <summary> /// add don vi /// </summary> /// <param name="Org"></param> /// <param name="token"></param> /// <returns></returns> public int AddOrganization(UpdateOrg model, string token) { int result = 0; string nameOrg = ""; try { TblOrganization OrgChk = db.TblOrganization.Where(r => (r.OrganizationTaxCode == model.Org.OrganizationTaxCode && r.IsDelete == false) || (r.OrganizationCode == model.Org.OrganizationCode && r.IsDelete == false) || (r.OrganizationEmail == model.Org.OrganizationEmail && r.IsDelete == false)).FirstOrDefault(); if (OrgChk == null) { // Send mail string pass = ""; string UserName = ""; //HaiHM comment Line under "Pass" //string Pass = User.Password; string UserEmail = ""; using (var ts = new TransactionScope()) { TblOrganization OrgAdd = new TblOrganization(); OrgAdd.IsDelete = false; OrgAdd.CreateDate = DateTime.Now; OrgAdd.CreateBy = model.Org.CreateBy; OrgAdd.IsActive = model.Org.IsActive; OrgAdd.IsLock = model.Org.IsLock; OrgAdd.UpdateBy = model.Org.UpdateBy; OrgAdd.UpdateDate = DateTime.Now; OrgAdd.OrganizationAddress = model.Org.OrganizationAddress; OrgAdd.OrganizationCode = model.Org.OrganizationCode.TrimEnd().TrimStart(); OrgAdd.OrganizationEmail = model.Org.OrganizationEmail.TrimEnd().TrimStart(); OrgAdd.OrganizationFrom = model.Org.OrganizationFrom; OrgAdd.OrganizationHomePage = model.Org.OrganizationHomePage; OrgAdd.OrganizationName = model.Org.OrganizationName.TrimEnd().TrimStart(); OrgAdd.OrganizationParentCode = model.Org.OrganizationParentCode; OrgAdd.OrganizationPhone = model.Org.OrganizationPhone; OrgAdd.OrganizationRemark = model.Org.OrganizationRemark; OrgAdd.OrganizationTaxCode = model.Org.OrganizationTaxCode.TrimEnd().TrimStart(); OrgAdd.OrganizationTo = model.Org.OrganizationTo; OrgAdd.OrganizationLogo = model.Org.OrganizationLogo; OrgAdd.OrganizationNote = model.Org.OrganizationNote; OrgAdd.OrganizationSphereId = model.Org.OrganizationSphereId; db.TblOrganization.Add(OrgAdd); db.SaveChanges(); nameOrg = OrgAdd.OrganizationName; // ADD admin TblUsers User = new TblUsers(); List <TblUsers> lst = db.TblUsers.Where(u => u.UserName == model.Org.OrganizationCode + "_Admin").ToList(); if (lst != null) { if (lst.Count > 0) { foreach (var item in lst) { TblUsers users = db.TblUsers.Where(u => u.Id == item.Id).FirstOrDefault(); if (users != null) { users.IsDelete = true; db.Entry(users).State = EntityState.Modified; db.SaveChanges(); } } } } User.UserName = model.Org.OrganizationCode + "_Admin"; //update isdelete pass = "******"; User.Password = organizationConmon.HashPassword(pass); User.CreateDate = DateTime.Now; User.IsDelete = false; User.IsLock = false; User.CreateBy = model.Org.CreateBy; User.Email = model.Org.OrganizationEmail; User.EmailConfirmed = false; User.HistoryPassword = organizationConmon.HashPassword(pass) + "="; User.LoginFail = 0; User.LastLogin = DateTime.Now; User.ExpirationDate = DateTime.Now.AddDays(OrganizationConstant.ExpirationDate); db.TblUsers.Add(User); db.SaveChanges(); int UserID = User.Id; int OrganizationId = OrgAdd.OrganizationId; // Send mail UserName = User.UserName; //HaiHM comment Line under "Pass" //string Pass = User.Password; UserEmail = User.Email; TblOrganizationUser OrgUser = new TblOrganizationUser { UserId = UserID, OrganizationId = OrganizationId }; db.TblOrganizationUser.Add(OrgUser); db.SaveChanges(); //ADD goi dich vu foreach (var item in model.tblServicePack) { TblOrganizationServicePack ru = new TblOrganizationServicePack(); ru.OrganizationId = OrganizationId; ru.ServicePackId = item.Id; // id new created db.TblOrganizationServicePack.Add(ru); db.SaveChanges(); } //HaiHM modified AddAuthorityAndRoleForUserAdmin(OrganizationId, model.Org.CreateBy, UserID); ts.Complete(); } if (!string.IsNullOrEmpty(UserEmail)) { SendMail(nameOrg, UserEmail, UserName, pass, ""); result = 1; GenerateDB(model.Org.OrganizationCode, token); } } else { if (db.TblOrganization.Where(u => u.IsDelete == false && u.OrganizationCode == model.Org.OrganizationCode).Count() > 0) { // Duplicate AddDuplicateOrgCode return(result = 411); } if (db.TblOrganization.Where(u => u.IsDelete == false && u.OrganizationEmail == model.Org.OrganizationEmail).Count() > 0) { // Duplicate AddDuplicateEmail return(result = 412); } if (db.TblOrganization.Where(u => u.IsDelete == false && u.OrganizationTaxCode == model.Org.OrganizationTaxCode).Count() > 0) { // Duplicate AddDuplicateOrgTaxCode return(result = 413); } } return(result); } catch (Exception ex) { return(result); throw ex; } }