コード例 #1
0
        public async Task <IActionResult> Main(string itemid, int?id)
        {
            MyUser            user   = _context.MyUser.Where(p => p.Email == HttpContext.User.Identity.Name).ToList().FirstOrDefault();
            int               userID = user.UserId;
            AdjustmentVoucher AV;

            if (id == 0)
            {
                AV = new AdjustmentVoucher
                {
                    UserId = userID,
                    Status = "Pending"
                };
                _context.Add(AV);
                await _context.SaveChangesAsync();

                id = AV.AdjustId;
            }
            else
            {
                AV = _context.AdjustmentVoucher.Find(id);
            }
            AVDetails checkAVD = _context.Avdetails.Where(d => d.AdjustId == AV.AdjustId & d.ItemId == itemid).SingleOrDefault();
            AVDetails avd;

            if (checkAVD == null)
            {
                avd          = new AVDetails();
                avd.AdjustId = AV.AdjustId;
                avd.ItemId   = itemid;
                _context.Add(avd);
                await _context.SaveChangesAsync();
            }
            return(Redirect("~/AVDetails/ViewAv/" + id));
        }
コード例 #2
0
        public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;

            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme);
                    await _emailSender.SendEmailConfirmationAsync(model.Email, callbackUrl);

                    await _signInManager.SignInAsync(user, isPersistent : false);

                    await _userManager.AddToRoleAsync(user, model.RoleName);

                    _logger.LogInformation("User created a new account with password.");


                    #region set table connection
                    //connect with our own user table here.
                    Login lg = _context.Login.Where(p => p.Email == model.Email).FirstOrDefault();
                    if (lg == null)
                    {
                        lg          = new Login();
                        lg.Email    = model.Email;
                        lg.Password = "******";
                        _context.Add(lg);
                        _context.SaveChanges();

                        MyUser u = new MyUser();
                        u.Name   = model.Name;
                        u.DeptId = _context.Department.Where(p => p.Name == model.Department).FirstOrDefault().DeptId;
                        u.RoleId = _context.Role.Where(p => p.Name == model.RoleName).FirstOrDefault().RoleId;

                        u.Email = model.Email;
                        _context.Add(u);
                        _context.SaveChanges();
                    }


                    #endregion

                    return(RedirectToLocal(returnUrl));
                }
                AddErrors(result);
            }

            return(View(model));
        }
        public async Task <IActionResult> Index(List <int> numlist)
        {
            PurchaseOrder po          = new PurchaseOrder();
            var           itemContext = from m in _context.Item.Include(p => p.Cat) where m.Cat.Supplier1 == supplierId || m.Cat.Supplier2 == supplierId || m.Cat.Supplier3 == supplierId select m;

            try
            {
                po.SupplierId   = supplierId;
                po.PurchaseDate = DateTime.Now.Date;
                _context.Add(po);
                await _context.SaveChangesAsync();

                var pocontext = from m in _context.PurchaseOrder select m;
                List <PurchaseOrder> polist = await pocontext.ToListAsync();

                int PoId = polist[polist.Count - 1].PoId;

                foreach (string s in idlist)
                {
                    PODetails pods = new PODetails();
                    pods.ItemId     = s;
                    pods.Item       = itemContext.Where(p => p.ItemId == s).ToList().First();
                    pods.PoId       = PoId;
                    pods.QtyOrdered = numlist[idlist.IndexOf(s)];
                    _context.Add(pods);
                    _context.SaveChanges();
                }
            }
            catch (Exception e)
            {
                System.Console.Write(e.ToString());
                return(NotFound());
            }
            List <PODetails> podlist = new List <PODetails>();

            idlist   = new List <string>();
            IsPosted = true;
            return(RedirectToAction(nameof(ViewOrderHistory)));
        }
コード例 #4
0
        public async Task <IActionResult> Create([Bind("ItemId,ItemName,CatId,ReorderLvl,ReorderQty,Uom,Stock,Unitprice")] Item item)
        {
            if (ModelState.IsValid)
            {
                _context.Add(item);
                await _context.SaveChangesAsync();

                #region Email to remind departments of new item.
                List <MyUser> employees = new List <MyUser>();
                employees = _context.MyUser.Where(x => x.RoleId == 1).ToList();

                var message = new MimeMessage();
                message.From.Add(new MailboxAddress("SSIS", "*****@*****.**"));
                foreach (MyUser e in employees)
                {
                    message.To.Add(new MailboxAddress(e.Name, e.Email));
                }
                message.Subject = item.ItemName + " now available!";
                message.Body    = new TextPart("plain")
                {
                    Text = "Dear colleagues, " +
                           "Please be informed that a new item called " + item.ItemName +
                           " is now available in the stationary catalogue. You can now order it." +
                           "This is an automatically generated email. Please do not reply."
                };
                using (var client = new SmtpClient())
                {
                    client.Connect("smtp.gmail.com", 587, false);
                    client.Authenticate("*****@*****.**", "team2team2");
                    client.Send(message);
                    client.Disconnect(true);
                }
                #endregion

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CatId"] = new SelectList(_context.Category, "CatId", "CatName", item.CatId);
            return(View(item));
        }
コード例 #5
0
        public async Task <IActionResult> Action(string SubmitButton, int id, [Bind("RequestId,UserId,Remarks,IsCompleted")] Request request)
        {
            MyUser employee = new MyUser();

            employee = _context.MyUser.Where(x => x.UserId == request.UserId).ToList().FirstOrDefault();

            #region Email to remind the requester.
            var message = new MimeMessage();
            message.From.Add(new MailboxAddress("SSIS", "*****@*****.**"));
            message.To.Add(new MailboxAddress(employee.Name, employee.Email));
            message.Subject = "Your Request is handled";
            message.Body    = new TextPart("plain")
            {
                Text = "Dear " + employee.Name + ", " +
                       "" +
                       "Your order request has been processed by your department head. You may login to SSIS system to check for results." +
                       "" +
                       "This is an automatic-generated email. Please do not reply."
            };
            using (var client = new SmtpClient())
            {
                client.Connect("smtp.gmail.com", 587, false);
                client.Authenticate("*****@*****.**", "team2team2");
                client.Send(message);
                client.Disconnect(true);
            }
            #endregion

            if (id != request.RequestId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    List <RequestDetails> rdlist = _context.RequestDetails.Where(m => m.RequestId == request.RequestId).ToList();
                    string buttonClicked         = SubmitButton;
                    if (buttonClicked == "Approved")
                    {
                        request.Status       = "Approved";
                        request.Approvaltime = DateTime.Now;

                        for (int i = 0; i < rdlist.Count; i++)
                        {
                            string rdlistitemid    = rdlist[i].ItemId;
                            int    rdlistitemstock = rdlist[i].RequestedQty;
                            Item   item            = await _context.Item.SingleOrDefaultAsync(m => m.ItemId == rdlistitemid);

                            int itemStock = (int)item.Stock;

                            if (item.Stock <= 0)
                            {
                                rdlist[i].Type = "Preorder";
                                _context.Update(rdlist[i]);
                                await _context.SaveChangesAsync();
                            }
                            else if (rdlistitemstock <= itemStock)
                            {
                                rdlist[i].Type = "Order";
                                _context.Update(rdlist[i]);
                                item.Stock = itemStock - rdlistitemstock;
                                _context.Update(item);
                                await _context.SaveChangesAsync();
                            }

                            else
                            {
                                int preorderqty = rdlist[i].RequestedQty - itemStock;
                                rdlist[i].RequestedQty = itemStock;
                                rdlist[i].Type         = "Order";
                                _context.Update(rdlist[i]);

                                RequestDetails rdnew = new RequestDetails();
                                rdnew.RequestId    = rdlist[i].RequestId;
                                rdnew.ItemId       = rdlist[i].ItemId;
                                rdnew.RequestedQty = preorderqty;
                                rdnew.Type         = "Preorder";
                                _context.Add(rdnew);
                                item.Stock = 0;
                                _context.Update(item);
                                await _context.SaveChangesAsync();
                            }
                        }
                        _context.Update(request);
                        await _context.SaveChangesAsync();
                    }
                    else if (buttonClicked == "Rejected")
                    {
                        request.Status       = "Rejected";
                        request.Approvaltime = DateTime.Now;
                        _context.Update(request);
                        await _context.SaveChangesAsync();
                    }
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!RequestExists(request.RequestId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Pending)));
            }
            ViewData["UserId"] = new SelectList(_context.MyUser, "UserId", "Email", request.UserId);
            return(View(request));
        }
コード例 #6
0
        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));
        }
コード例 #7
0
        //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());
            }
        }