public async Task <IActionResult> Index()
        {
            if (User.Identity.IsAuthenticated)
            {
                Worker currentWorker = await _userManager.FindByNameAsync(User.Identity.Name);

                db.Entry(currentWorker).Reference(w => w.Sector).Load();
                db.Entry(currentWorker.Sector).Reference(s => s.Subunit).Load();
                HomeViewModel model = new HomeViewModel()
                {
                    FIO       = currentWorker.Family + " " + currentWorker.Name[0] + "." + (string.IsNullOrEmpty(currentWorker.Otch) ? "" : (currentWorker.Otch[0] + ".")),
                    Post      = currentWorker.Post,
                    Subunit   = currentWorker.Sector.Subunit.Name,
                    Sector    = currentWorker.Sector.SectorName,
                    UserRoles = await _userManager.GetRolesAsync(currentWorker)
                };

                return(View(model));
            }
            else
            {
                HomeViewModel model = new HomeViewModel();

                return(View(model));
            }
        }
        public async Task <IActionResult> EditUser(string id)
        {
            Worker worker = await _userManager.FindByIdAsync(id);

            if (worker == null)
            {
                return(NotFound());
            }

            db.Entry(worker).Reference(w => w.Sector).Load();
            db.Entry(worker.Sector).Reference(s => s.Subunit).Load();

            EditUserViewModel model = new EditUserViewModel(db)
            {
                Id        = id,
                Login     = worker.UserName,
                Family    = worker.Family,
                Name      = worker.Name,
                Otch      = worker.Otch,
                Post      = worker.Post,
                Subunit   = worker.Sector.Subunit.Id,
                Sector    = worker.Sector.Id,
                AllRoles  = _roleManager.Roles.ToList(),
                UserRoles = await _userManager.GetRolesAsync(worker)
            };

            return(View(model));
        }
        public async Task <IActionResult> Index(int?subunit, int?sector, string query, int page = 1, SortStateEvent sortOrder = SortStateEvent.FailCountAsc)
        {
            int pageSize = 10;
            IQueryable <Event> events = db.Events
                                        .Include(e => e.Check)
                                        .Include(e => e.DeveloperNavigation)
                                        .Include(e => e.Check.Sector)
                                        .Include(e => e.Check.Sector.Subunit)
                                        .Include(e => e.DeveloperNavigation.Sector)
                                        .Include(e => e.DeveloperNavigation.Sector.Subunit);

            events = events.Where(e => e.IsActive && e.IsCorrect);
            if (User.IsInRole("DEVEL") && !User.IsInRole("EVENT_DETAIL") && !User.IsInRole("CONTROL"))
            {
                Worker worker = await _userManager.FindByNameAsync(User.Identity.Name);

                if (worker != null)
                {
                    events = events.Where(e => e.Developer == worker.Id);
                }
            }
            else if (User.IsInRole("EVENT_DETAIL") && !User.IsInRole("CONTROL"))
            {
                Worker worker = await _userManager.FindByNameAsync(User.Identity.Name);

                db.Entry(worker).Reference(w => w.Sector).Load();
                if (worker != null)
                {
                    events = events.Where(e => e.Check.Sector.SubunitId == worker.Sector.SubunitId);
                }
            }

            if (subunit != null && subunit != 0)
            {
                events = events.Where(e => e.Check.Sector.SubunitId == subunit);

                if (sector != null && sector != 0)
                {
                    events = events.Where(e => e.Check.SectorId == sector);
                }
            }

            if (!String.IsNullOrEmpty(query))
            {
                events = events.Where(e => e.Check.FailCount.ToUpper().Contains(query.ToUpper()) ||
                                      e.Check.FailDescription.ToUpper().Contains(query.ToUpper()) ||
                                      e.Description.ToUpper().Contains(query.ToUpper()) ||
                                      e.ResponsWorker.ToUpper().Contains(query.ToUpper()) ||
                                      e.DeveloperNavigation.Sector.Subunit.Name.ToUpper().Contains(query.ToUpper()) ||
                                      e.DeveloperNavigation.Family.ToUpper().Contains(query.ToUpper()) ||
                                      e.Report.ToUpper().Contains(query.ToUpper()));
            }

            switch (sortOrder)
            {
            case SortStateEvent.FailCountDesc:
                events = events.OrderByDescending(e => e.Check.FailCount);
                break;

            case SortStateEvent.CheckSubunitAsc:
                events = events.OrderBy(e => e.Check.Sector.Subunit.Name);
                break;

            case SortStateEvent.CheckSubunitDesc:
                events = events.OrderByDescending(e => e.Check.Sector.Subunit.Name);
                break;

            case SortStateEvent.CheckSectorAsc:
                events = events.OrderBy(e => e.Check.Sector.SectorName);
                break;

            case SortStateEvent.CheckSectorDesc:
                events = events.OrderByDescending(e => e.Check.Sector.SectorName);
                break;

            case SortStateEvent.FailDescriptionAsc:
                events = events.OrderBy(e => e.Check.FailDescription);
                break;

            case SortStateEvent.FailDescriptionDesc:
                events = events.OrderByDescending(e => e.Check.FailDescription);
                break;

            case SortStateEvent.EventDescriptionAsc:
                events = events.OrderBy(e => e.Description);
                break;

            case SortStateEvent.EventDescriptionDesc:
                events = events.OrderByDescending(e => e.Description);
                break;

            case SortStateEvent.ResponseWorkerAsc:
                events = events.OrderBy(e => e.ResponsWorker);
                break;

            case SortStateEvent.ResponseWorkerDesc:
                events = events.OrderByDescending(e => e.ResponsWorker);
                break;

            case SortStateEvent.DueDateAsc:
                events = events.OrderBy(e => e.DueDate);
                break;

            case SortStateEvent.DueDateDesc:
                events = events.OrderByDescending(e => e.DueDate);
                break;

            case SortStateEvent.DeveloperSubunitAsc:
                events = events.OrderBy(e => e.DeveloperNavigation.Sector.Subunit.Name);
                break;

            case SortStateEvent.DeveloperSubunitDesc:
                events = events.OrderByDescending(e => e.DeveloperNavigation.Sector.Subunit.Name);
                break;

            case SortStateEvent.DeveloperAsc:
                events = events.OrderBy(e => e.DeveloperNavigation.Family);
                break;

            case SortStateEvent.DeveloperDesc:
                events = events.OrderByDescending(e => e.DeveloperNavigation.Family);
                break;

            case SortStateEvent.ReportAsc:
                events = events.OrderBy(e => e.Report);
                break;

            case SortStateEvent.ReportDesc:
                events = events.OrderByDescending(e => e.Report);
                break;

            default:
                events = events.OrderBy(e => e.Check.FailCount);
                break;
            }

            var count = await events.CountAsync();

            var items = await events.Skip((page - 1) *pageSize).Take(pageSize).ToListAsync();

            EventListViewModel model = new EventListViewModel
            {
                FilterViewModel = new FilterEventViewModel(db.Subunits.ToList(), db.Sectors.ToList(), subunit, sector, query),
                SortViewModel   = new SortEventViewModel(sortOrder),
                PageViewModel   = new PageViewModel(count, page, pageSize),
                Events          = items
            };

            return(View(model));
        }
Пример #4
0
        public async Task <IActionResult> Index(int?subunit, int?sector, string query, int page = 1, SortStateCheck sortOrder = SortStateCheck.FailCountAsc)
        {
            int pageSize = 10;
            IQueryable <Check> checks = db.Check.Include(c => c.RegWorkerNavigation)
                                        .Include(c => c.RegWorkerNavigation.Sector)
                                        .Include(c => c.RegWorkerNavigation.Sector.Subunit)
                                        .Include(c => c.Sector)
                                        .Include(c => c.Sector.Subunit)
                                        .Include(c => c.Events);

            checks = checks.Where(c => c.IsActive && c.IsCorrect);
            if (User.IsInRole("REG") && !User.IsInRole("CHECK_DETAIL") && !User.IsInRole("CONTROL"))
            {
                Worker worker = await _userManager.FindByNameAsync(User.Identity.Name);

                if (worker != null)
                {
                    checks = checks.Where(c => c.RegWorker == worker.Id);
                }
            }
            else if (User.IsInRole("CHECK_DETAIL") && !User.IsInRole("CONTROL"))
            {
                Worker worker = await _userManager.FindByNameAsync(User.Identity.Name);

                if (worker != null)
                {
                    db.Entry(worker).Reference(w => w.Sector).Load();
                    checks = checks.Where(c => c.Sector.SubunitId == worker.Sector.SubunitId || c.RegWorker == worker.Id);
                }
            }

            if (subunit != null && subunit != 0)
            {
                checks = checks.Where(c => c.Sector.SubunitId == subunit);

                if (sector != null && sector != 0)
                {
                    checks = checks.Where(c => c.SectorId == sector);
                }
            }
            if (!String.IsNullOrEmpty(query))
            {
                checks = checks.Where(c => c.FailCount.ToUpper().Contains(query.ToUpper()) ||
                                      c.RegWorkerNavigation.Family.ToUpper().Contains(query.ToUpper()) ||
                                      c.ControlIndicator.ToUpper().Contains(query.ToUpper()) ||
                                      c.FailDescription.ToUpper().Contains(query.ToUpper()) ||
                                      c.RegWorkerNavigation.Sector.Subunit.Name.ToUpper().Contains(query.ToUpper())
                                      );
            }

            checks = sortOrder switch
            {
                SortStateCheck.FailCountDesc => checks.OrderByDescending(c => c.FailCount),
                SortStateCheck.CheckSubunitAsc => checks.OrderBy(c => c.Sector.Subunit.Name),
                SortStateCheck.CheckSubunitDesc => checks.OrderByDescending(c => c.Sector.Subunit.Name),
                SortStateCheck.CheckDateAsc => checks.OrderBy(c => c.CheckDate),
                SortStateCheck.CheckDateDesc => checks.OrderByDescending(c => c.CheckDate),
                SortStateCheck.SectorAsc => checks.OrderBy(c => c.Sector.SectorName),
                SortStateCheck.SectorDesc => checks.OrderByDescending(c => c.Sector.SectorName),
                SortStateCheck.RegWorkerAsc => checks.OrderBy(c => c.RegWorkerNavigation.Family),
                SortStateCheck.RegWorkerDesc => checks.OrderByDescending(c => c.RegWorkerNavigation.Family),
                SortStateCheck.ControlIndicatorAsc => checks.OrderBy(c => c.ControlIndicator),
                SortStateCheck.ControlIndicatorDesc => checks.OrderByDescending(c => c.ControlIndicator),
                SortStateCheck.FailDescriptionAsc => checks.OrderBy(c => c.FailDescription),
                SortStateCheck.FailDescriptionDesc => checks.OrderByDescending(c => c.FailDescription),
                SortStateCheck.RegSubunitAsc => checks.OrderBy(c => c.RegWorkerNavigation.Sector.Subunit.Name),
                SortStateCheck.RegSubunitDesc => checks.OrderByDescending(c => c.RegWorkerNavigation.Sector.Subunit.Name),
                _ => checks.OrderBy(c => c.FailCount),
            };
            var count = await checks.CountAsync();

            var items = await checks.Skip((page - 1) *pageSize).Take(pageSize).ToListAsync();

            CheckListViewModel model = new CheckListViewModel
            {
                PageViewModel   = new PageViewModel(count, page, pageSize),
                SortViewModel   = new SortCheckViewModel(sortOrder),
                FilterViewModel = new FilterCheckViewModel(db.Subunits.ToList(), db.Sectors.ToList(), subunit, sector, query),
                Checks          = items
            };

            return(View(model));
        }
        public async Task <IActionResult> CreateReport()
        {
            IQueryable <Check> CheckList = db.Check.Include(c => c.RegWorkerNavigation)
                                           .Include(c => c.RegWorkerNavigation.Sector)
                                           .Include(c => c.RegWorkerNavigation.Sector.Subunit)
                                           .Include(c => c.Sector)
                                           .Include(c => c.Sector.Subunit)
                                           .Include(c => c.Events)
                                           .Include(c => c.DeleteWorkerNavigation)
                                           .Include(c => c.Shows);

            foreach (Check ch in CheckList)
            {
                if (ch.DeleteWorkerNavigation != null)
                {
                    db.Entry(ch.DeleteWorkerNavigation).Reference(w => w.Sector).Load();
                    db.Entry(ch.DeleteWorkerNavigation.Sector).Reference(s => s.Subunit).Load();
                }

                foreach (Event ev in ch.Events)
                {
                    db.Entry(ev).Reference(e => e.DeveloperNavigation).Load();
                    db.Entry(ev.DeveloperNavigation).Reference(w => w.Sector).Load();
                    db.Entry(ev.DeveloperNavigation.Sector).Reference(s => s.Subunit).Load();


                    db.Entry(ev).Reference(e => e.ReportWorkerNavigation).Load();
                    if (ev.ReportWorkerNavigation != null)
                    {
                        db.Entry(ev.ReportWorkerNavigation).Reference(w => w.Sector).Load();
                        db.Entry(ev.ReportWorkerNavigation.Sector).Reference(s => s.Subunit).Load();
                    }

                    db.Entry(ev).Reference(e => e.DeleteWorkerNavigation).Load();
                    if (ev.DeleteWorkerNavigation != null)
                    {
                        db.Entry(ev.DeleteWorkerNavigation).Reference(w => w.Sector).Load();
                        db.Entry(ev.DeleteWorkerNavigation.Sector).Reference(s => s.Subunit).Load();
                    }
                }

                foreach (Show sh in ch.Shows)
                {
                    db.Entry(sh).Reference(s => s.Worker).Load();
                    db.Entry(sh.Worker).Reference(w => w.Sector).Load();
                    db.Entry(sh.Worker.Sector).Reference(s => s.Subunit).Load();
                }
            }

            byte[] reportContent = createXlsx(CheckList);

            string fileName = "Отчет_" + DateTime.Now.ToString("yyMMddHHmmss") + ".xlsx";

            if (reportContent != null && reportContent.Length > 0)
            {
                return(File(
                           fileContents: reportContent,
                           contentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                           fileDownloadName: fileName));
            }

            return(NotFound());
        }