Beispiel #1
0
        public async Task <RedirectToRouteResult> SendUrgentItemsMail()
        {
            var mailer = new EmailController();
            var mail   = mailer.UrgentItemsWarning();

            mail.DeliverAsync();
            var log = "Сообщение о нехватке оборудования на складе отправлено на адреса: " + mail.Mail.To;
            await log.WriteToLogAsync(EventLogEntryType.Information, "Mailer");

            return(RedirectToAction("Index"));
        }
Beispiel #2
0
        // GET: SupportRequests/AcceptRequest/5
        public async Task <ActionResult> AcceptRequest(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            var supportRequest = await _db.SupportRequests.FindAsync(id);

            if (supportRequest == null)
            {
                return(HttpNotFound());
            }
            //request is already accepted or iser is not in IT-dep
            if (supportRequest.State != 0 || !User.IsInRole(@"RIVS\IT-Dep"))
            {
                return(RedirectToAction("Index"));
            }
            var login       = User.Identity.Name;
            var accountName = login.Substring(5, login.Length - 5);
            var person      = _db.Persons.FirstOrDefault(p => p.AccountName == accountName);

            if (person == null)
            {
                return(RedirectToAction("Index"));
            }
            supportRequest.State            = 1;
            supportRequest.StartTime        = DateTime.Now;
            supportRequest.To               = person;
            _db.Entry(supportRequest).State = EntityState.Modified;
            await _db.SaveChangesAsync();

            using (var mailer = new EmailController())
            {
                //mail for user (job started)
                var mail = await mailer.Accepted((int)id);

                mail?.Deliver();
            }

            return(RedirectToAction("Index"));
        }
Beispiel #3
0
        public async Task <ActionResult> FinishRequest(SupportRequestViewModel requestViewModel)
        {
            var login       = User.Identity.Name;
            var accountName = login.Substring(5, login.Length - 5);
            var itPerson    = await _db.Persons.FirstOrDefaultAsync(p => p.AccountName == accountName);

            if (!ModelState.IsValid || itPerson == null)
            {
                return(View("Finish", requestViewModel));
            }
            if (!User.IsInRole(@"RIVS\IT-Dep"))
            {
                // return view with error message
                ModelState.AddModelError(string.Empty, "Ошибка авторизации!");
                return(View("Finish", requestViewModel));
            }

            var supportRequest = await _db.SupportRequests.FindAsync(requestViewModel.Id);

            if (supportRequest == null)
            {
                return(HttpNotFound());
            }

            int hardwareId;

            int.TryParse(requestViewModel.HardwareId, out hardwareId);

            supportRequest.Comment           = requestViewModel.Comment;
            supportRequest.SoftwareInstalled = requestViewModel.SoftwareInstalled;
            supportRequest.SoftwareRemoved   = requestViewModel.SoftwareRemoved;
            supportRequest.SoftwareRepaired  = requestViewModel.SoftwareRepaired;
            supportRequest.SoftwareUpdated   = requestViewModel.SoftwareUpdated;
            supportRequest.HardwareId        = hardwareId;
            supportRequest.HardwareQuantity  = requestViewModel.HardwareQuantity > 0 && hardwareId > 0 ? requestViewModel.HardwareQuantity : 0;
            supportRequest.OtherActions      = requestViewModel.OtherActions;
            supportRequest.State             = 2;
            supportRequest.FinishTime        = DateTime.Now;

            //grant selected hardware
            if (hardwareId > 0 && requestViewModel.HardwareQuantity > 0)
            {
                // find item in db
                var editItem = await _db.Items.FindAsync(hardwareId);

                if (editItem == null)
                {
                    return(HttpNotFound());
                }

                // check if we give more than we have
                if (editItem.Quantity < requestViewModel.HardwareQuantity)
                {
                    // return view with error message
                    ModelState.AddModelError(string.Empty, "Нельзя выдать больше, чем есть в наличии (" + editItem.Quantity + " шт.)!");
                    return(View("Finish", requestViewModel));
                }

                // decrease item quantity
                editItem.Quantity -= requestViewModel.HardwareQuantity;

                // create new history item
                var newHistory = new History
                {
                    Recieved = false,
                    Date     = DateTime.Now,
                    Item     = editItem,
                    Quantity = requestViewModel.HardwareQuantity,
                    WhoGave  = itPerson,
                    WhoTook  = supportRequest.From
                };
                // add new history item to db
                _db.Histories.Add(newHistory);
                // add new history item to modified item history list
                editItem.Histories.Add(newHistory);
                _db.Entry(editItem).State = EntityState.Modified;
            }

            _db.Entry(supportRequest).State = EntityState.Modified;
            await _db.SaveChangesAsync();

            using (var mailer = new EmailController())
            {
                //mail for user (job finished)
                var mail = await mailer.Finished(requestViewModel.Id);

                mail?.Deliver();
            }

            return(RedirectToAction("Index"));
        }
Beispiel #4
0
        public async Task <ActionResult> Edit(SupportRequestViewModel requestViewModel)
        {
            var started       = false;
            var itUserChanged = false;
            var login         = User.Identity.Name;
            var accountName   = login.Substring(5, login.Length - 5);
            var person        = await _db.Persons.FirstOrDefaultAsync(p => p.AccountName == accountName);

            if (person == null || !ModelState.IsValid)
            {
                // return view with error message
                if (person == null)
                {
                    ModelState.AddModelError(string.Empty, "Ошибка авторизации!");
                }
                return(View(requestViewModel.EditByIt ? "EditByIt" : "EditByUser", requestViewModel));
            }
            var supportRequest = await _db.SupportRequests.FindAsync(requestViewModel.Id);

            if (supportRequest == null)
            {
                return(HttpNotFound());
            }
            supportRequest.Category = requestViewModel.Category;

            //a file was attached
            if (requestViewModel.Upload != null && requestViewModel.Upload.ContentLength > 0)
            {
                if (requestViewModel.Upload.ContentLength > 10485760)
                {
                    ModelState.AddModelError(string.Empty, "Файл должен быть меньше 10 МБ!");
                    return(View(requestViewModel.EditByIt ? "EditByIt" : "EditByUser", requestViewModel));
                }
                var fileName = Path.GetFileName(requestViewModel.Upload.FileName);
                if (!string.IsNullOrEmpty(fileName))
                {
                    var dir      = Directory.CreateDirectory(Path.Combine(HostingEnvironment.MapPath(@"~/SupportFiles"), Guid.NewGuid().ToString()));
                    var filePath = Path.Combine(dir.FullName, fileName);
                    requestViewModel.Upload.SaveAs(filePath);
                    var requestFile = new SupportFile
                    {
                        Path = filePath
                    };
                    _db.SupportFiles.Add(requestFile);
                    supportRequest.File = requestFile;
                }
            }

            //edit by user
            if (!requestViewModel.EditByIt)
            {
                int mark;
                supportRequest.Text = requestViewModel.Text;
                supportRequest.Mark = int.TryParse(requestViewModel.Mark, out mark) ? mark : 0;
                if (supportRequest.State == 2 && supportRequest.Mark == 0)
                {
                    requestViewModel.State = 2;
                    ModelState.AddModelError(string.Empty, "Пожалуйста, выберите оценку");
                    return(View("EditByUser", requestViewModel));
                }
                supportRequest.FeedBack = requestViewModel.FeedBack;
                supportRequest.Urgency  = requestViewModel.Urgency;
            }

            //edit by IT-user
            else
            {
                if (requestViewModel.ToId != 0)
                {
                    var itUser = await _db.Persons.FindAsync(requestViewModel.ToId);

                    if (itUser != null)
                    {
                        //new request assigned to an IT-user - start it
                        if (supportRequest.State == 0)
                        {
                            supportRequest.State     = 1;
                            supportRequest.StartTime = DateTime.Now;
                            started = true;
                        }
                        else if (supportRequest.To != itUser)
                        {
                            itUserChanged = true;
                        }
                        supportRequest.To = itUser;
                    }
                }
                supportRequest.Comment           = requestViewModel.Comment;
                supportRequest.SoftwareInstalled = requestViewModel.SoftwareInstalled;
                supportRequest.SoftwareRemoved   = requestViewModel.SoftwareRemoved;
                supportRequest.SoftwareRepaired  = requestViewModel.SoftwareRepaired;
                supportRequest.SoftwareUpdated   = requestViewModel.SoftwareUpdated;
                //supportRequest.HardwareId = requestViewModel.HardwareId;
                //supportRequest.HardwareReplaced = requestViewModel.HardwareReplaced;
                supportRequest.OtherActions = requestViewModel.OtherActions;
            }

            _db.Entry(supportRequest).State = EntityState.Modified;
            await _db.SaveChangesAsync();

            using (var mailer = new EmailController())
            {
                EmailResult mail = null;
                //edit by IT-user
                if (requestViewModel.EditByIt)
                {
                    //job started right now, IT-user selected
                    if (started)
                    {
                        //mail for user (job started)
                        mail = await mailer.Accepted(requestViewModel.Id);

                        mail?.Deliver();
                        //mail for selected IT user (request assigned)
                        mail = await mailer.EditByIt(requestViewModel.Id, User.Identity.Name.GetUserName());

                        mail?.Deliver();
                    }
                    //job in progress, IT-user changed by another IT-user
                    else if (itUserChanged)
                    {
                        //mail for selected IT user (request assigned)
                        mail = await mailer.EditByIt(requestViewModel.Id, User.Identity.Name.GetUserName());

                        mail?.Deliver();
                    }
                }
                //edit by user
                else
                {
                    switch (supportRequest.State)
                    {
                    //job in progress
                    case 1:
                        //mail for selected IT user (request changed)
                        mail = await mailer.EditByUser(requestViewModel.Id);

                        break;

                    //job is done, user added mark and feedback
                    case 2:
                        //mail for IT managers
                        mail = await mailer.Feedback(requestViewModel.Id);

                        break;
                    }
                    mail?.Deliver();
                }
            }

            return(RedirectToAction("Index"));
        }
Beispiel #5
0
        public async Task <ActionResult> Create(SupportRequestViewModel supportRequest)
        {
            if (!ModelState.IsValid)
            {
                return(View(supportRequest));
            }
            if (string.IsNullOrEmpty(supportRequest.Text))
            {
                // return view with error message
                ModelState.AddModelError(string.Empty, "Введите текст заявки!");
                return(View(supportRequest));
            }
            var fromIt      = false;
            var login       = User.Identity.Name;
            var accountName = login.Substring(5, login.Length - 5);
            var person      = _db.Persons.FirstOrDefault(p => p.AccountName == accountName);

            if (person == null)
            {
                ModelState.AddModelError(string.Empty, "Ошибка авторизации!");
                return(View(supportRequest));
            }
            Computer    comp        = null;
            SupportFile requestFile = null;

            //support request has attached file
            if (supportRequest.Upload != null && supportRequest.Upload.ContentLength > 0)
            {
                if (supportRequest.Upload.ContentLength > 10485760)
                {
                    ModelState.AddModelError(string.Empty, "Файл должен быть меньше 10 МБ!");
                    return(View(supportRequest));
                }
                var fileName = Path.GetFileName(supportRequest.Upload.FileName);
                if (!string.IsNullOrEmpty(fileName))
                {
                    var dir      = Directory.CreateDirectory(Path.Combine(HostingEnvironment.MapPath(@"~/SupportFiles"), Guid.NewGuid().ToString()));
                    var filePath = Path.Combine(dir.FullName, fileName);
                    supportRequest.Upload.SaveAs(filePath);
                    requestFile = new SupportFile
                    {
                        Path = filePath
                    };
                    _db.SupportFiles.Add(requestFile);
                }
            }

            var newRequest = new SupportRequest
            {
                Text         = supportRequest.Text,
                Urgency      = supportRequest.Urgency,
                Category     = supportRequest.Category,
                CreationTime = DateTime.Now,
                FinishTime   = null,
                File         = requestFile
            };

            //created by IT-user
            if (supportRequest.ToId != 0 && supportRequest.FromId != 0)
            {
                var ituser = await _db.Persons.FindAsync(supportRequest.ToId);

                var user = await _db.Persons.FindAsync(supportRequest.FromId);

                if (ituser == null || user == null)
                {
                    ModelState.AddModelError(string.Empty, "Пользователь не найден!");
                    return(View(supportRequest));
                }
                comp                 = _db.Computers.Where(c => c.Owner.Id == user.Id).OrderByDescending(c => c.LastReportDate).FirstOrDefault();
                newRequest.To        = ituser;
                newRequest.From      = user;
                newRequest.State     = 1;
                newRequest.StartTime = DateTime.Now;
                user.SupportRequests.Add(newRequest);
                _db.Entry(user).State = EntityState.Modified;
                fromIt = true;
            }

            //created by ordinary user
            else
            {
                var compName = StaticData.GetCompName(Request.UserHostName);
                if (!string.IsNullOrEmpty(compName))
                {
                    comp = _db.Computers.FirstOrDefault(c => c.ComputerName == compName);
                }
                newRequest.From      = person;
                newRequest.State     = 0;
                newRequest.StartTime = null;
                person.SupportRequests.Add(newRequest);
                _db.Entry(person).State = EntityState.Modified;
            }

            if (comp != null)
            {
                newRequest.FromComputer = comp;
                comp.SupportRequests.Add(newRequest);
                _db.Entry(comp).State = EntityState.Modified;
            }

            _db.SupportRequests.Add(newRequest);
            _db.SaveChanges();

            using (var mailer = new EmailController())
            {
                EmailResult mail;
                if (fromIt)
                {
                    //mail for selected IT user
                    mail = await mailer.EditByIt(newRequest.Id, User.Identity.Name.GetUserName());

                    mail?.Deliver();
                    //mail for user (job started)
                    mail = await mailer.Accepted(newRequest.Id);

                    mail?.Deliver();
                }
                else
                {
                    //mail for IT users
                    mail = await mailer.NewFromUser(newRequest.Id);

                    mail?.Deliver();
                }
            }

            return(RedirectToAction("Index"));
        }