/// <summary> /// Sua Don Vi /// </summary> /// <param name="Org"></param> /// <returns></returns> public int UpdateOrganization(UpdateOrg model) { int result = 0; try { TblOrganization OrgChk = db.TblOrganization.Where(r => r.OrganizationId == model.Org.OrganizationId && r.IsDelete == false).FirstOrDefault(); using (var ts = new TransactionScope()) { if (OrgChk != null) { if (OrgChk.OrganizationCode != model.Org.OrganizationCode) { if (db.TblOrganization.Where(u => u.IsDelete == false && u.OrganizationCode == model.Org.OrganizationCode).Count() > 0) { // Duplicate DuplicateOrgCode return(result = 411); } } if (OrgChk.OrganizationEmail != model.Org.OrganizationEmail) { if (db.TblOrganization.Where(u => u.IsDelete == false && u.OrganizationEmail == model.Org.OrganizationEmail).Count() > 0) { // Duplicate DuplicateEmail return(result = 412); } } if (OrgChk.OrganizationTaxCode != model.Org.OrganizationTaxCode) { if (db.TblOrganization.Where(u => u.IsDelete == false && u.OrganizationTaxCode == model.Org.OrganizationTaxCode).Count() > 0) { // Duplicate DuplicateOrgTaxCode return(result = 413); } } OrgChk.CreateBy = model.Org.CreateBy; OrgChk.IsDelete = model.Org.IsDelete; OrgChk.CreateDate = model.Org.CreateDate; OrgChk.IsActive = model.Org.IsActive; OrgChk.IsLock = model.Org.IsLock; OrgChk.UpdateBy = model.Org.UpdateBy; OrgChk.UpdateDate = DateTime.Now; OrgChk.OrganizationAddress = model.Org.OrganizationAddress; OrgChk.OrganizationCode = model.Org.OrganizationCode.TrimEnd().TrimStart(); OrgChk.OrganizationEmail = model.Org.OrganizationEmail.TrimEnd().TrimStart(); OrgChk.OrganizationFrom = model.Org.OrganizationFrom; OrgChk.OrganizationHomePage = model.Org.OrganizationHomePage; OrgChk.OrganizationName = model.Org.OrganizationName.TrimEnd().TrimStart(); OrgChk.OrganizationParentCode = model.Org.OrganizationParentCode; OrgChk.OrganizationPhone = model.Org.OrganizationPhone; OrgChk.OrganizationRemark = model.Org.OrganizationRemark; OrgChk.OrganizationTaxCode = model.Org.OrganizationTaxCode.TrimEnd().TrimStart(); OrgChk.OrganizationTo = model.Org.OrganizationTo; OrgChk.OrganizationLogo = model.Org.OrganizationLogo != null ? model.Org.OrganizationLogo : OrgChk.OrganizationLogo; OrgChk.OrganizationNote = model.Org.OrganizationNote; OrgChk.OrganizationSphereId = model.Org.OrganizationSphereId; db.Entry(OrgChk).State = EntityState.Modified; db.SaveChanges(); // Add goi dich vu List <TblOrganizationServicePack> deleteAU = db.TblOrganizationServicePack.Where(au => au.OrganizationId == model.Org.OrganizationId).ToList <TblOrganizationServicePack>(); foreach (var item in deleteAU) { db.TblOrganizationServicePack.Remove(item); db.SaveChanges(); } } foreach (var item in model.tblServicePack) { TblOrganizationServicePack ru = new TblOrganizationServicePack(); ru.OrganizationId = model.Org.OrganizationId; ru.ServicePackId = item.Id; // id new created db.TblOrganizationServicePack.Add(ru); db.SaveChanges(); } result = 2; ts.Complete(); } 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; } }