public async Task <IActionResult> changeOrAddDelegation(string SubmitButton, string searchString, Delegation delegation) { MyUser head = _context.MyUser.Where(p => p.Email == HttpContext.User.Identity.Name).ToList().FirstOrDefault(); int id = head.DeptId; if (ModelState.IsValid) { try { Delegation de = _context.Delegation.Where(m => m.DeptId == id).FirstOrDefault(); MyUser u; if (de == null) { Delegation d = new Delegation(); d.DeptId = id; d.UserId = delegation.UserId; d.Startdate = delegation.Startdate; d.Enddate = delegation.Enddate; _context.Add(d); await _context.SaveChangesAsync(); de = _context.Delegation.Where(m => m.DeptId == id).FirstOrDefault(); } else { string buttonClicked = SubmitButton; if (buttonClicked == "Cancel") { u = _context.MyUser.Where(em => em.RoleId == 6 & em.DeptId == id).ToList().FirstOrDefault(); if (u != null) { u.RoleId = 1; _context.Update(u); ApplicationUser _user1 = await _userManager.FindByEmailAsync(u.Email); await _userManager.RemoveFromRoleAsync(_user1, "actinghead"); await _userManager.AddToRoleAsync(_user1, "employee"); } _context.Remove(de); await _context.SaveChangesAsync(); return(RedirectToAction("About", "Home")); //after cancel,directly return,no need to do the next step since have already changed the role. } else { de.DeptId = id; de.UserId = delegation.UserId; de.Startdate = delegation.Startdate; de.Enddate = delegation.Enddate; _context.Update(de); await _context.SaveChangesAsync(); } } //Deal with the delegation(null or not null) first,then deal with the role all together at last. u = _context.MyUser.Where(em => em.RoleId == 6 & em.DeptId == id).FirstOrDefault(); if (u != null) { u.RoleId = 1; _context.Update(u); //* ApplicationUser _user1 = await _userManager.FindByEmailAsync(u.Email); await _userManager.RemoveFromRoleAsync(_user1, "actinghead"); await _userManager.AddToRoleAsync(_user1, "employee"); } int eid = await _context.Delegation.Where(d => d.DeptId == id).Select(d => d.UserId).SingleAsync(); MyUser e = _context.MyUser.Find(eid); e.RoleId = 6; _context.Update(e); //* ApplicationUser _user = await _userManager.FindByEmailAsync(e.Email); await _userManager.RemoveFromRoleAsync(_user, "employee"); await _userManager.AddToRoleAsync(_user, "actinghead"); await _context.SaveChangesAsync(); #region Email to remind the delegation within the department. List <MyUser> alldepemployees = new List <MyUser>(); alldepemployees = _context.MyUser.Where(x => x.DeptId == id).ToList(); DateTime startdate = delegation.Startdate.Value; String stringstartdate = startdate.ToShortDateString(); DateTime enddate = delegation.Enddate.Value; String stringenddate = enddate.ToShortDateString(); MyUser actinghead = new MyUser(); actinghead = _context.MyUser.Where(x => x.DeptId == id && x.RoleId == 6).FirstOrDefault(); Department department = _context.Department.Where(x => x.DeptId == id).FirstOrDefault(); var message = new MimeMessage(); message.From.Add(new MailboxAddress("SSIS", "*****@*****.**")); foreach (MyUser e1 in alldepemployees) { message.To.Add(new MailboxAddress(e1.Name, e1.Email)); message.Subject = "New department actinghead"; message.Body = new TextPart("plain") { Text = "Dear " + e1.Name + "," + "I will be on leave from " + stringstartdate + " till " + stringenddate + "." + " I may not be able to respond to the emails or requests" + " due to limited email access overseas. During this period, " + actinghead.Name + " will be the acting head of" + " our department to be in charge of all matters regarding to stationery. " + Environment.NewLine + "This is an automatically generated email. Please do not reply." + Environment.NewLine + "Regards" + Environment.NewLine + department.Name + " department" }; } using (var client = new SmtpClient()) { client.Connect("smtp.gmail.com", 587, false); client.Authenticate("*****@*****.**", "team2team2"); client.Send(message); client.Disconnect(true); } #endregion } catch (DbUpdateConcurrencyException) { if (!DelegationExists(delegation.DeptId)) { return(NotFound()); } else { throw; } } return(RedirectToAction("About", "Home")); } ViewData["CurrentFilter"] = searchString; var employees = _context.MyUser.Where(u => u.DeptId == id && (u.RoleId == 1 || u.RoleId == 6)); if (!String.IsNullOrEmpty(searchString)) { employees = employees.Where(x => x.Name.Contains(searchString)); } List <MyUser> ulist = employees.ToList(); ViewData["User"] = ulist; return(View(delegation)); }
//GET: get view or generate the consolidated list by department and by item. public async Task <IActionResult> Index([FromQuery(Name = "btnGenerate")] string buttonType, string searchType, string searchString) { if (HttpContext.User.IsInRole("clerk")) { if (buttonType == "generate") { var pre = from a in _context.RequestDetails.Include(b => b.Request).Include(b => b.Item) select a; pre = pre.Where(p => p.Request.IsCompleted == null && p.IsComplete == null && p.Type == "Order"); var items = from a in _context.Item select a; var user = from a in _context.MyUser select a; var deleteDeptr = from a in _context.DeptRequest where a.ReceivedQty == null select a; List <DeptRequest> drlist = await deleteDeptr.ToListAsync(); foreach (DeptRequest dd in drlist) { _context.Remove(dd); await _context.SaveChangesAsync(); } List <RequestDetails> rdlist = await pre.ToListAsync(); List <Item> ilist = await items.ToListAsync(); List <MyUser> ulist = await user.ToListAsync(); List <DeptRequest> drlist2 = new List <DeptRequest>(); List <string> repeatDictionary = new List <string>(); foreach (RequestDetails rd in rdlist) { DeptRequest deptr = new DeptRequest(); foreach (MyUser u in ulist) { if (u.UserId == rd.Request.UserId) { deptr.DeptId = u.DeptId; } } deptr.ItemId = rd.ItemId; deptr.TotalQty = rd.RequestedQty; deptr.ReceivedQty = null; deptr.GeneratedTime = DateTime.Now; if (repeatDictionary.IndexOf(deptr.DeptId + deptr.ItemId) < 0) { repeatDictionary.Add(deptr.DeptId + deptr.ItemId); drlist2.Add(deptr); } else { drlist2.Add(deptr); } } foreach (string s in repeatDictionary) { DeptRequest deptr = new DeptRequest(); deptr.TotalQty = 0; foreach (DeptRequest dptr in drlist2) { if (dptr.DeptId + dptr.ItemId == s) { deptr.ItemId = dptr.ItemId; deptr.DeptId = dptr.DeptId; deptr.ReceivedQty = null; deptr.GeneratedTime = DateTime.Now; deptr.TotalQty += (int)dptr.TotalQty; } } _context.Add(deptr); await _context.SaveChangesAsync(); } //retrieve from other tables and calculate the quantity within every department. count++; } var dr = from m in _context.DeptRequest.Include(d => d.Item).Include(d => d.Dept).Include(d => d.Dept.Cp) where m.ReceivedQty == null select m; //searchbar controller List <String> stype = new List <string> { "ItemName", "Department", "CollectionPoint" }; if (!String.IsNullOrEmpty(searchString)) { if (!String.IsNullOrEmpty(searchType)) { if (searchType == "ItemName") { dr = dr.Where(s => s.Item.ItemName.Contains(searchString)); } else if (searchType == "Department") { dr = dr.Where(s => s.Dept.Name.Contains(searchString)); } else if (searchType == "CollectionPoint") { dr = dr.Where(s => s.Dept.Cp.Name.Contains(searchString)); } } } List <DeptRequest> drslist = await dr.ToListAsync(); List <string> binnlist = new List <string>(); List <string> drDictionary = new List <string>(); if (drslist.Count > 0) { foreach (DeptRequest d1 in drslist) { binnlist.Add("#" + d1.ItemId.Substring(1)); if (drDictionary.IndexOf(d1.ItemId) < 0) { drDictionary.Add(d1.ItemId); } } } #region By Item //code here to get the consolidated list by item: List <GroupByItemView> bilist = new List <GroupByItemView>(); foreach (string item in drDictionary) { int total = 0; foreach (DeptRequest d2 in drslist) { if (d2.ItemId == item) { total += (int)d2.TotalQty; } } GroupByItemView gbiv = new GroupByItemView(); gbiv.BinNumber = "#" + item.Substring(1); gbiv.TotalQuantity = total; gbiv.ItemName = _context.Item.Where(p => p.ItemId == item).FirstOrDefault().ItemName; bilist.Add(gbiv); } #endregion var deptRequestsVM = new DeptRequestsViewModel { deptList = new SelectList(stype), deptRequests = drslist, binlist = binnlist, byItemList = bilist }; return(View(deptRequestsVM)); } else { return(NotFound()); } }