Esempio n. 1
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);
        }
        /*Note that this method is applicable to employees with hourly rate*/
        public void GenerateEmployeeDailySalaryByDateRange(DateTime dateFrom, DateTime dateTo)
        {
            //Delete existing by date range
            DeleteByDateRange(dateFrom, dateTo);

            IList <TotalEmployeeHours> totalEmployeeHours =
                _totalEmployeeHoursService.GetByDateRange(dateFrom, dateTo);

            Double restDayRate               = Double.Parse(_settingService.GetByKey(SettingValue.RATE_REST_DAY));
            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));

            foreach (TotalEmployeeHours totalHours in totalEmployeeHours)
            {
                EmployeeInfo employeeInfo = _employeeInfoService.GetByEmployeeId(totalHours.EmployeeId);

                var hourlyRate = _employeeSalaryService.GetEmployeeHourlyRate(employeeInfo);

                var employeeWorkSchedule =
                    _employeeWorkScheduleService.GetByEmployeeId(totalHours.EmployeeId);

                //No work schedule, no computation
                if (employeeWorkSchedule != null)
                {
                    DateTime date           = totalHours.Date;
                    Double   rateMultiplier = 0;

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

                    //Check if rest day and not special holiday
                    if (isRestDay &&
                        !(holiday != null && !holiday.IsRegularHoliday))
                    {
                        rateMultiplier *= restDayRate;
                    }

                    //Check if holiday
                    if (holiday != null)
                    {
                        if (holiday.IsRegularHoliday)
                        {
                            rateMultiplier *= holidayRegularRate;
                        }
                        else
                        {
                            if (isRestDay)
                            {
                                rateMultiplier *= holidaySpecialRestDayRate;
                            }
                            else
                            {
                                rateMultiplier *= holidaySpecialRate;
                            }
                        }
                    }
                    //if OT
                    if (totalHours.Type == RateType.OverTime)
                    {
                        //If holiday use holiday ot rate
                        if (holiday != null)
                        {
                            rateMultiplier *= OTRateHoliday;
                        }
                        else
                        {
                            rateMultiplier *= OTRate;
                        }
                    }

                    decimal totalPayment = 0;

                    //if NightDif
                    if (totalHours.Type == RateType.NightDifferential)
                    {
                        //rateMultiplier *= nightDiffRate;

                        /*if (rateMultiplier > 1)
                         * {
                         *  totalPayment = ((decimal)(totalHours.Hours * (rateMultiplier - 1)) * hourlyRate);
                         * }*/
                        totalPayment += (decimal)(nightDiffRate * totalHours.Hours * rateMultiplier);
                    }
                    else
                    {
                        totalPayment = ((decimal)(totalHours.Hours * rateMultiplier)) * hourlyRate;
                    }

                    var employeeDailySalary = new EmployeeDailyPayroll
                    {
                        EmployeeId           = totalHours.EmployeeId,
                        Date                 = totalHours.Date,
                        TotalPay             = totalPayment,
                        TotalEmployeeHoursId = totalHours.TotalEmployeeHoursId,
                        RateType             = totalHours.Type
                    };

                    //Save
                    _employeeDailyPayrollRepository.Add(employeeDailySalary);
                }
            }
            _unitOfWork.Commit();

            //Generate holiday pays
            GenerateEmployeeHolidayPay(dateFrom, dateTo);
            _unitOfWork.Commit();
        }