public async Task <IActionResult> ChangeCompanyAsync(string guid) { this.Response.Cookies.Append("Company", guid, new CookieOptions() { MaxAge = new TimeSpan(30, 0, 0, 0) }); var userConext = HttpContext.User; var userId = userManager.GetUserId(userConext); if (!string.IsNullOrEmpty(userId)) { var companyList = companyAppRepository.All().Where(e => e.ApplicationUserId == userId); var activeCompanies = companyList.Where(e => e.IsActive == true).ToList(); if (activeCompanies != null) { if (activeCompanies.Count == 1) { var oldActive = activeCompanies.FirstOrDefault(e => e.IsActive == true); oldActive.IsActive = false; companyAppRepository.Update(oldActive); } else { foreach (var activeCompany in activeCompanies) { activeCompany.IsActive = false; companyAppRepository.Update(activeCompany); } } } var newActive = companyList.Where(e => e.GUID == guid).FirstOrDefault(); if (newActive == null) { return(BadRequest()); } newActive.IsActive = true; companyAppRepository.Update(newActive); await companyAppRepository.SaveChangesAsync(); return(Redirect("/Home")); } return(BadRequest()); }
public async Task <IViewComponentResult> InvokeAsync() { var userConext = HttpContext.User; var userId = userManager.GetUserId(userConext); if (!string.IsNullOrEmpty(userId)) { var companyList = companyAppRepository.All().Where(e => e.ApplicationUserId == userId).ToList(); if (companyList.Count > 0) { var activeCompanies = companyList.Where(e => e.IsActive == true).ToList(); if (activeCompanies.Count > 1) { foreach (var activeCompany in activeCompanies) { activeCompany.IsActive = false; companyAppRepository.Update(activeCompany); } await companyAppRepository.SaveChangesAsync(); } var companyNavBars = companyList.Select(c => new CompanyNavBar() { Id = c.Id, Name = c.CompanyName, GUID = c.GUID, IsActive = c.IsActive }); this.ViewBag.Companies = companyNavBars; return(View()); } } return(View()); }
public async Task <bool> Edit(CompanyDto companyDto) { try { using var transaction = await companyRepository.Context.Database.BeginTransactionAsync(); var company = await companyRepository.All().OrderBy(c => c.Id).LastAsync(); if (company.IsVatRegistered && !companyDto.IsVatRegistered) { var vatTypeId = await vatTypeService.SetCorrectVatTypeOnNonVatRegisteredCompanyAsync(); await productService.SetAllProductToVatType(vatTypeId); } company.Name = companyDto.Name; company.Address = companyDto.Address; company.City = companyDto.City; company.Country = companyDto.Country; company.Description = companyDto.Description; company.EIK = companyDto.EIK; company.Email = companyDto.Email; company.IsVatRegistered = companyDto.IsVatRegistered; company.LogoPath = companyDto.LogoPath; company.MOL = companyDto.MOL; company.VatId = companyDto.VatId; companyRepository.Update(company); var companyApp = this.companyAppRepository.All().Where(c => c.GUID == company.GUID).FirstOrDefault(); if (companyDto.IsActive == true) { var allCompanyApp = this.companyAppRepository.All().Where(c => c.ApplicationUserId == companyApp.ApplicationUserId); foreach (var comp in allCompanyApp) { comp.IsActive = false; companyAppRepository.Update(comp); } } var slug = stringGenerator.GenerateSlug(companyDto.Name); companyDto.CompanySlug = slug; companyApp.CompanyName = companyDto.Name; companyApp.CompanySlug = slug; companyApp.Description = companyDto.Description; companyApp.IsActive = companyDto.IsActive; companyApp.IsVatRegistered = companyDto.IsVatRegistered; companyAppRepository.Update(companyApp); await companyRepository.SaveChangesAsync(); await companyAppRepository.SaveChangesAsync(); await transaction.CommitAsync(); } catch (Exception) { return(false); throw; } return(true); }