public DateTime GetNextPayrollStartDate(DateTime?date)
        {
            DateTime?payrollStartDate = _employeePayrollRepository.GetNextPayrollStartDate();

            if (payrollStartDate == null)
            {
                var d = DateTime.Now;
                if (date != null)
                {
                    d = date.Value;
                }
                //TODO more frequency support
                switch (_frequency)
                {
                case FrequencyType.Weekly:
                    //Note that the job should always schedule the day after the payroll end date
                    var startOfWeeklyPayroll = (DayOfWeek)Enum.Parse(typeof(DayOfWeek),
                                                                     _settingService.GetByKey(PAYROLL_WEEK_START));

                    if (d.DayOfWeek == startOfWeeklyPayroll)
                    {
                        d = d.AddDays(-7);
                    }

                    payrollStartDate = d.StartOfWeek(startOfWeeklyPayroll);

                    break;
                }
            }
            return(payrollStartDate.Value);
        }
Example #2
0
        protected virtual IEnumerable <AttendanceViewModel> GetAttendance(string startDate, string endDate, int employeeId)
        {
            //do not display the edit link if attendance date < last payroll date
            var nextPayrollDate = _employeePayrollRepository.GetNextPayrollStartDate(); //this is actually the last payroll date
            var lastPayrollDate = nextPayrollDate ?? DateTime.MinValue;
            var sDate           = startDate.ToDateTime();
            var eDate           = endDate.ToDateTime();

            var holidays = _holidayRepository.Find(x => x.IsActive && x.Date >= sDate && x.Date <= eDate).ToList();

            Func <IEnumerable <EmployeeHours>, bool> isNotEmpty = x => x != null && x.Any(y => y != null);
            Func <DateTime, bool> isHoliday        = x => holidays.Any(y => y.Date == x.Date);
            Func <DateTime, bool> isRegularHoliday = x =>
            {
                var holiday = holidays.FirstOrDefault(y => y.Date == x.Date);
                return(holiday != null ? holiday.IsRegularHoliday : false);
            };

            var result    = _attendanceService.GetAttendanceAndHoursByDate(sDate, eDate, employeeId).ToList();
            var viewModel = result.MapCollection <AttendanceDao, AttendanceViewModel>((s, d) =>
            {
                d.Editable          = s.ClockIn > lastPayrollDate;
                d.RegularHours      = isNotEmpty(s.EmployeeHours) ? s.EmployeeHours.Where(x => x.Type == RateType.Regular).Sum(x => x.Hours) : 0;
                d.Overtime          = isNotEmpty(s.EmployeeHours) ? s.EmployeeHours.Where(x => x.Type == RateType.OverTime).Sum(x => x.Hours) : 0;
                d.NightDifferential = isNotEmpty(s.EmployeeHours) ? s.EmployeeHours.Where(x => x.Type == RateType.NightDifferential).Sum(x => x.Hours) : 0;
                d.IsHoliday         = isHoliday(s.ClockIn);
                d.IsRegularHoliday  = isRegularHoliday(s.ClockIn);
                d.Breakdown         = s.EmployeeHours.Where(x => x != null).ToList().GroupBy(x => x.Date).Select(x => new AttendanceBreakdownViewModel
                {
                    Date = x.Key,
                    NightDifferential = isNotEmpty(x) ? x.Where(y => y.Type == RateType.NightDifferential).Sum(y => y.Hours) : 0,
                    Overtime          = isNotEmpty(x) ?  x.Where(y => y.Type == RateType.OverTime).Sum(y => y.Hours) : 0,
                    RegularHours      = isNotEmpty(x) ? x.Where(y => y.Type == RateType.Regular).Sum(y => y.Hours) : 0,
                    IsHoliday         = isHoliday(x.Key),
                    IsRegularHoliday  = isRegularHoliday(x.Key)
                });
            });

            return(viewModel);
        }