예제 #1
0
        public int GenerateEmployeeHours(DateTime fromDate, DateTime toDate)
        {
            //Get all active employee with the same frequency
            var employees = _employeeInfoService.GetAllActive();

            //Delete existing entries with the same date
            var employeeHoursList = this.GetByDateRange(fromDate, toDate);

            _employeeHoursRepository.DeleteAll(employeeHoursList);
            _unitOfWork.Commit();

            foreach (var employee in employees)
            {
                employeeWorkSchedule = _employeeWorkScheduleService.GetByEmployeeId(employee.EmployeeId);

                //Will not compute if work schedule is null
                if (employeeWorkSchedule != null)
                {
                    foreach (DateTime d in DatetimeExtension.EachDay(fromDate, toDate))
                    {
                        day = d;
                        //Get all employee attendance within date range
                        // Will not include attendance without clockout
                        IList <Attendance> attendanceList = _attendanceService
                                                            .GetAttendanceForProcessing(employee.EmployeeId, day);

                        ComputeEmployeeHours(attendanceList, day);
                    }
                }
            }

            _unitOfWork.Commit();

            return(0);
        }
예제 #2
0
        public int GenerateEmployeeHours(DateTime fromDate, DateTime toDate)
        {
            //Get all active employee with the same frequency
            var employees = _employeeInfoService.GetAllActive();

            //Delete existing entries with the same date
            //var employeeHoursList = this.GetByDateRange(fromDate, toDate);
            //_employeeHoursRepository.DeleteAll(employeeHoursList);
            //_unitOfWork.Commit();

            foreach (var employee in employees)
            {
                employeeWorkSchedule = _employeeWorkScheduleService.GetByEmployeeId(employee.EmployeeId);

                //Will not compute if work schedule is null
                if (employeeWorkSchedule != null)
                {
                    foreach (DateTime d in DatetimeExtension.EachDay(fromDate, toDate))
                    {
                        ComputeEmployeeHours(d, employee.EmployeeId);
                    }
                }
            }

            _unitOfWork.Commit();

            return(0);
        }
예제 #3
0
        /*Note that this method is applicable to employees with hourly rate*/
        public void GenerateEmployeePayrollItemByDateRange(DateTime payrollDate, DateTime payrollStartDate, DateTime payrollEndDate)
        {
            //Delete existing by date range
            DeleteByDateRange(payrollDate, payrollDate);

            Double restDayRate               = Double.Parse(_settingService.GetByKey(SettingValue.RATE_REST_DAY));
            Double restDayRateOT             = Double.Parse(_settingService.GetByKey(SettingValue.RATE_REST_DAY_OT));
            Double OTRate                    = Double.Parse(_settingService.GetByKey(SettingValue.RATE_OT));
            Double nightDiffRate             = Double.Parse(_settingService.GetByKey(SettingValue.RATE_NIGHTDIF));
            Double holidayRegularRate        = Double.Parse(_settingService.GetByKey(SettingValue.RATE_HOLIDAY_REGULAR));
            Double holidaySpecialRate        = Double.Parse(_settingService.GetByKey(SettingValue.RATE_HOLIDAY_SPECIAL));
            Double OTRateHoliday             = Double.Parse(_settingService.GetByKey(SettingValue.RATE_OT_HOLIDAY));
            Double holidaySpecialRestDayRate = Double.Parse(_settingService.GetByKey(SettingValue.RATE_HOLIDAY_SPECIAL_REST_DAY));

            IList <TotalEmployeeHours> totalEmployeeHours =
                _totalEmployeeHoursService.GetByDateRange(payrollStartDate, payrollEndDate);

            //Get all active employee
            var activeEmployeeList = _employeeInfoService.GetAllActive();

            foreach (EmployeeInfo employee in activeEmployeeList)
            {
                var employeeWorkSchedule =
                    _employeeWorkScheduleService.GetByEmployeeId(employee.EmployeeId);
                if (employeeWorkSchedule == null)
                {
                    continue;
                }
                var workSchedule = employeeWorkSchedule.WorkSchedule;

                //Get all total employee hours
                var employeeTotalHoursList = totalEmployeeHours.Where(h => h.EmployeeId == employee.EmployeeId)
                                             .OrderByDescending(h => h.Date);

                var employeePayrollItemList = new List <EmployeePayrollItem>();
                foreach (DateTime day in DatetimeExtension.EachDay(payrollStartDate, payrollEndDate))
                {
                    //Get all total employee hours for the day
                    var employeeTotalHoursListPerDay = employeeTotalHoursList.Where(h => h.Date == day);

                    foreach (TotalEmployeeHours totalHours in employeeTotalHoursListPerDay)
                    {
                        var    hourlyRate     = _employeeSalaryService.GetEmployeeHourlyRate(employee);
                        Double rateMultiplier = 1;

                        //No work schedule, no computation
                        if (employeeWorkSchedule != null)
                        {
                            DateTime date     = totalHours.Date;
                            RateType rateType = totalHours.Type;

                            var     isRestDay = date.IsRestDay(workSchedule.WeekStart, workSchedule.WeekEnd);
                            Holiday holiday   = _holidayService.GetHoliday(date);

                            Decimal totalPayment = 0;
                            //If NightDif
                            if (rateType == RateType.NightDifferential)
                            {
                                totalPayment += (decimal)(nightDiffRate * totalHours.Hours);
                                hourlyRate    = (decimal)nightDiffRate;
                            }
                            else
                            {
                                //If employee didn't work the day before holiday will not get holiday pay
                                if (holiday != null)
                                {
                                    var yesterdayHours = _totalEmployeeHoursService.GetByEmployeeDate(employee.EmployeeId, day.AddDays(-1));
                                    var qualifiedForOT = (yesterdayHours != null && yesterdayHours.Count > 0);

                                    if (!qualifiedForOT)
                                    {
                                        var lastDayOfWork = getLastDayOfWork(day, workSchedule);
                                        var employeeHours = _totalEmployeeHoursService.GetByEmployeeDate(employee.EmployeeId, lastDayOfWork);

                                        qualifiedForOT = (employeeHours != null && employeeHours.Count > 0);
                                    }


                                    //Set holiday to null if the employee didn't work the day before holiday
                                    if (holiday.IsAlwaysPayable == false)
                                    {
                                        if (!qualifiedForOT)
                                        {
                                            holiday = null;
                                        }
                                    }
                                }

                                //Check if with holiday pay
                                if (holiday != null)
                                {
                                    if (holiday.IsRegularHoliday)
                                    {
                                        rateMultiplier *= holidayRegularRate;
                                        if (isRestDay)
                                        {
                                            rateMultiplier *= restDayRate;
                                            //Regular holiday rest day OT
                                            if (totalHours.Type == RateType.OverTime)
                                            {
                                                rateMultiplier *= OTRateHoliday;
                                                rateType        = RateType.RegularHolidayRestDayOT;
                                            }
                                            else
                                            {
                                                //Regular holiday rest day
                                                rateType = RateType.RegularHolidayRestDay;
                                            }
                                        }
                                        else
                                        {
                                            //Regular Holiday OT
                                            if (totalHours.Type == RateType.OverTime)
                                            {
                                                rateMultiplier *= OTRateHoliday;
                                                rateType        = RateType.RegularHolidayOT;
                                            }
                                            else
                                            {
                                                //Regular Holiday
                                                rateType = RateType.RegularHoliday;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        //Special Holiday
                                        //Rest day
                                        if (isRestDay)
                                        {
                                            rateMultiplier = holidaySpecialRestDayRate;
                                            //Special holiday rest day OT
                                            if (totalHours.Type == RateType.OverTime)
                                            {
                                                rateMultiplier *= OTRateHoliday;
                                                rateType        = RateType.SpecialHolidayRestDayOT;
                                            }
                                            else
                                            {
                                                //Special holiday rest day
                                                rateType = RateType.SpecialHolidayRestDay;
                                            }
                                        }
                                        else
                                        {
                                            rateMultiplier *= holidaySpecialRate;
                                            //Special Holiday OT
                                            if (totalHours.Type == RateType.OverTime)
                                            {
                                                rateMultiplier *= OTRateHoliday;
                                                rateType        = RateType.SpecialHolidayOT;
                                            }
                                            else
                                            {
                                                //Special Holiday
                                                rateType = RateType.SpecialHoliday;
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    //Check if rest day and not special holiday
                                    if (isRestDay)
                                    {
                                        rateMultiplier *= restDayRate;
                                        if (rateType == RateType.OverTime)
                                        {
                                            rateMultiplier *= restDayRateOT;
                                            rateType        = RateType.RestDayOT;
                                        }
                                        else
                                        {
                                            rateType = RateType.RestDay;
                                        }
                                    }
                                    else
                                    {
                                        if (rateType == RateType.OverTime)
                                        {
                                            rateMultiplier *= OTRate;
                                        }
                                    }
                                }

                                totalPayment = (decimal)hourlyRate * (decimal)totalHours.Hours * (decimal)rateMultiplier;
                            }

                            //Get existing
                            var employeePayrollItem = employeePayrollItemList.Where(pi =>
                                                                                    pi.EmployeeId == employee.EmployeeId && pi.RateType == rateType).FirstOrDefault();
                            if (employeePayrollItem == null)
                            {
                                //Create new entry
                                employeePayrollItem = new EmployeePayrollItem()
                                {
                                    Multiplier  = rateMultiplier,
                                    EmployeeId  = employee.EmployeeId,
                                    TotalHours  = totalHours.Hours,
                                    TotalAmount = totalPayment,
                                    RatePerHour = hourlyRate,
                                    PayrollDate = payrollDate,
                                    RateType    = rateType
                                };

                                employeePayrollItemList.Add(employeePayrollItem);
                            }
                            else
                            {
                                //Update Entry
                                employeePayrollItem.TotalHours  += totalHours.Hours;
                                employeePayrollItem.TotalAmount += totalPayment;
                            }
                        }
                    }
                }
                //Insert all payroll items
                foreach (EmployeePayrollItem payrollItem in employeePayrollItemList)
                {
                    _employeePayrollItemRepository.Add(payrollItem);
                }
            }
            _unitOfWork.Commit();

            //Generate holiday pays
            GenerateEmployeeHolidayPay(payrollDate, payrollStartDate, payrollEndDate);

            //Generate leave pays
            GenerateEmployeeLeavesPay(payrollDate, payrollStartDate, payrollEndDate);
        }
        public void GenerateEmployeeHolidayPay(DateTime payrollStartDate, DateTime payrollEndDate)
        {
            //Get all active employees
            IList <EmployeeInfo> employees = _employeeInfoService.GetAllActive();

            foreach (DateTime day in DatetimeExtension.EachDay(payrollStartDate, payrollEndDate))
            {
                //Check if holiday
                var  holiday = _holidayService.GetHoliday(day);
                bool isSpecialHolidayPaid = Convert.ToInt32(_settingService.GetByKey(SettingValue.PAYROLL_IS_SPHOLIDAY_WITH_PAY)) > 0;

                if (holiday != null && (holiday.IsRegularHoliday || isSpecialHolidayPaid))
                {
                    foreach (EmployeeInfo employee in employees)
                    {
                        WorkSchedule workSchedule = _employeeWorkScheduleService.GetByEmployeeId(employee.EmployeeId).WorkSchedule;

                        if (workSchedule != null)
                        {
                            //Check if within schedule
                            if (day.IsRestDay(workSchedule.WeekStart, workSchedule.WeekEnd))
                            {
                                //Don't proceed
                                continue;
                            }
                        }
                        else
                        {
                            //No work schedule, no holiday pay
                            continue;
                        }

                        //If with schedule on this date, generate holiday pay

                        //Check if already have daily entry
                        EmployeeDailyPayroll dailyPayroll = _employeeDailyPayrollRepository.GetByDate(employee.EmployeeId, day);

                        int workHours  = Convert.ToInt32(_settingService.GetByKey(SettingValue.PAYROLL_REGULAR_HOURS));
                        var hourlyRate = _employeeSalaryService.GetEmployeeHourlyRate(employee);

                        //If null create a holiday pay
                        if (dailyPayroll == null)
                        {
                            EmployeeDailyPayroll newDailyPayroll = new EmployeeDailyPayroll
                            {
                                EmployeeId = employee.EmployeeId,
                                Date       = day,
                                TotalPay   = hourlyRate * workHours,
                                //RateType = RateType.
                            };

                            _employeeDailyPayrollRepository.Add(newDailyPayroll);
                        }
                        else
                        {
                            //If existing create new for remaining unpaid hours
                            //if total hours worked is less than regular working hours
                            //Get total hours worked
                            IList <TotalEmployeeHours> employeeHours =
                                _totalEmployeeHoursService.GetByTypeAndDateRange(employee.EmployeeId, null, payrollStartDate, payrollEndDate);
                            var totalEmployeeHours = employeeHours.Sum(h => h.Hours);
                            if (totalEmployeeHours < workHours)
                            {
                                var remainingUnpaidHours =
                                    Convert.ToDecimal(workHours - totalEmployeeHours);

                                EmployeeDailyPayroll newDailyPayroll = new EmployeeDailyPayroll
                                {
                                    EmployeeId = employee.EmployeeId,
                                    Date       = day,
                                    TotalPay   = hourlyRate * remainingUnpaidHours,
                                    //RateType = RateType.Holiday
                                };
                                _employeeDailyPayrollRepository.Add(newDailyPayroll);
                            }
                        }
                    }
                }
            }
        }