public IActionResult List()
        {
            if (!RoleIs(Role.Administrator, Role.Manager))
            {
                return(Forbid());
            }

            var registrations = _registrationService.AllInclude(x => x.Employee, x => x.Entrance.Building);

            return(View(GetViewModel(registrations, new ReportFilterForm())));
        }
        public IActionResult List()
        {
            if (!RoleIs(Role.Manager, Role.SecurityGuard))
            {
                return(Forbid());
            }

            var registrations =
                _registrationService
                .AllInclude(x => x.Employee, x => x.Entrance.Building)
                .ForPeriod(_timeService.Now, _timeService.Now)
                .ToList();

            if (RoleIs(Role.Manager))
            {
                var user = _userProvider.User;

                var manager = _userService.GetById(user.Id) as Manager;

                registrations = registrations.Where(x => _employeeService.GetById(x.Employee.Id).Department.Id == manager.Department.Id).ToList();
            }
            else if (RoleIs(Role.SecurityGuard))
            {
                var user = _userProvider.User;

                var securityGuard = _userService.GetById(user.Id) as SecurityGuard;

                registrations = registrations.Where(x => x.Entrance.Building.Id == securityGuard.Entrance.Building.Id).ToList();
            }

            var form = new IndicatorsListForm
            {
                DateFrom      = _timeService.Now,
                DateTo        = _timeService.Now,
                DayIndicators = registrations.GroupBy(x => x.DateTime.Date).Select(x => new DayIndicatorViewModel
                {
                    DateTime       = x.Key,
                    TotalCameCount = registrations.Where(y => y.EventType == RegistrationEventType.Coming).GroupBy(y => y.Employee).Count(),
                    TotalGoneCount = registrations.Where(y => y.EventType == RegistrationEventType.Leaving).GroupBy(y => y.Employee).Count(),
                    In             = registrations
                                     .GroupBy(y => y.Employee)
                                     .Where(y => y.OrderBy(z => z.DateTime).Last().EventType == RegistrationEventType.Coming)
                                     .Select(y => _mapper.Map <EmployeeViewModel>(y.Key))
                })
            };

            return(View(form));
        }
Ejemplo n.º 3
0
        public void Notify(TimeSpan workDayStartTime)
        {
            var employeesWithLateness = new List <Employee>();
            var missingEmployees      = new List <Employee>();

            var lastRegistrationsByEmployee =
                _registrationService
                .AllInclude(x => x.Employee, x => x.Entrance)
                .GroupBy(x => x.Employee)
                .Select(x => new
            {
                Employee         = x.Key,
                LastRegistration = x.Last()
            })
                .ToList();

            _employeeService.AllActive().ToList().ForEach(x =>
            {
                var employeeWithLastRegistration = lastRegistrationsByEmployee.SingleOrDefault(y => y.Employee.Id == x.Id);

                if (employeeWithLastRegistration is null)
                {
                    missingEmployees.Add(x);
                }
                else if (employeeWithLastRegistration.LastRegistration.EventType is RegistrationEventType.Leaving)
                {
                    missingEmployees.Add(x);
                }
                else
                {
                    var comingTime = employeeWithLastRegistration.LastRegistration.DateTime.TimeOfDay;

                    if (comingTime > workDayStartTime)
                    {
                        employeesWithLateness.Add(x);
                    }
                }
            });

            var text = CreateEmailTextBody(missingEmployees, employeesWithLateness);

            var recipientEmails = _userService.GetAllActive().Where(x => x.NeedNotify).Select(x => x.Email);

            foreach (var email in recipientEmails)
            {
                _mailService.Send(email, new MailMessage
                {
                    Subject = "Оповещение о явке сотрудников",
                    Body    = text
                });
            }
        }