public decimal ComputeEmployeeAllowance(int totalDays, int totalWorkHoursPerDay, EmployeeInfo employee, DateTime payrollStartDate, DateTime payrollEndDate) { double totalHours = 0; //Get regular and OT hours per day List <TotalEmployeeHours> employeeTotalHours = new List <TotalEmployeeHours>(_totalEmployeeHourService .GetByTypeAndDateRange(employee.EmployeeId, RateType.Regular, payrollStartDate, payrollEndDate)); employeeTotalHours.AddRange(_totalEmployeeHourService .GetByTypeAndDateRange(employee.EmployeeId, RateType.OverTime, payrollStartDate, payrollEndDate)); employeeTotalHours = employeeTotalHours.OrderByDescending(e => e.Date).ToList(); if (employeeTotalHours != null && employeeTotalHours.Count > 1) { DateTime?tempDate = null; double dayHours = 0; var last = employeeTotalHours.Last(); foreach (TotalEmployeeHours employeeHours in employeeTotalHours) { //If different date add dayhours to totalhours and set dat hours to 0 if (tempDate != null && tempDate != employeeHours.Date) { totalHours += (dayHours > totalWorkHoursPerDay ? totalWorkHoursPerDay : dayHours); dayHours = 0; } dayHours = dayHours + employeeHours.Hours; tempDate = employeeHours.Date; //If last iteration if (last.Equals(employeeHours)) { totalHours += (dayHours > totalWorkHoursPerDay ? totalWorkHoursPerDay : dayHours); } } //Compute total allowance var totalAllowanceHours = totalDays * totalWorkHoursPerDay; Decimal totalAllowancePerHour = employee.Allowance.Value / ((decimal)totalDays * (decimal)totalWorkHoursPerDay); return((decimal)totalHours * totalAllowancePerHour); } else { return(0); } }
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); } } } } } }