Snippets

Fabio Bottan Employee Timesheet reminder job

Created by Fabio Bottan
1
2
3
<selection name="meta.schedule.job.select" id="hr.meta.schedule.job.select">
	<option value="com.axelor.apps.hr.job.TimesheetReminderEmployee">com.axelor.apps.hr.job.TimesheetReminderEmployee</option>
</selection>
package com.axelor.apps.hr.job;

import com.axelor.apps.base.db.Company;
import com.axelor.apps.base.db.repo.CompanyRepository;
import com.axelor.apps.hr.db.Employee;
import com.axelor.apps.hr.db.HRConfig;
import com.axelor.apps.hr.db.repo.EmployeeHRRepository;
import com.axelor.apps.hr.db.repo.TimesheetHRRepository;
import com.axelor.apps.hr.service.config.HRConfigService;
import com.axelor.apps.message.service.TemplateMessageService;
import com.axelor.exception.AxelorException;
import com.google.inject.Inject;
import java.lang.invoke.MethodHandles;
import java.time.LocalDate;
import java.util.List;
import javax.transaction.Transactional;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TimesheetReminderEmployee implements Job {

  private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

  protected EmployeeHRRepository employeeRepository;
  protected TemplateMessageService templateMessageService;
  protected HRConfigService hrConfigService;
  protected CompanyRepository companyRepository;
  protected TimesheetHRRepository timesheetHRRepository;

  @Inject
  public TimesheetReminderEmployee(
      EmployeeHRRepository employeeRepository,
      TemplateMessageService templateMessageService,
      HRConfigService hrConfigService,
      CompanyRepository companyRepository,
      TimesheetHRRepository timesheetHRRepository) {
    this.employeeRepository = employeeRepository;
    this.templateMessageService = templateMessageService;
    this.hrConfigService = hrConfigService;
    this.companyRepository = companyRepository;
    this.timesheetHRRepository = timesheetHRRepository;
  }

  private boolean isRunning(JobExecutionContext context) {
    try {
      return context
          .getScheduler()
          .getCurrentlyExecutingJobs()
          .stream()
          .filter(j -> j.getTrigger().equals(context.getTrigger()))
          .filter(j -> !j.getFireInstanceId().equals(context.getFireInstanceId()))
          .findFirst()
          .isPresent();
    } catch (SchedulerException e) {
      return false;
    }
  }

  @Override
  @Transactional
  public void execute(JobExecutionContext context) throws JobExecutionException {
    log.info("TimesheetReminderEmployee - START JOB");
    try {
      List<Company> companyList = this.companyRepository.all().fetch();

      for (Company company : companyList) {
        HRConfig hrConfig = hrConfigService.getHRConfig(company);
        if (hrConfig.getTimesheetMailNotification()) {
          // Return list of all employee who didn't submitted timesheet today;
          List<Employee> employeeList =
              this.employeeRepository.findAllEmployeeForTimesheetReminderPayCompany(company);
          employeeList.forEach(
              employee -> {
                LocalDate today = LocalDate.now();
                if (employee.getDateOfHire().isBefore(today)
                    && (employee
                                .getEmploymentContractList()
                                .get(employee.getEmploymentContractList().size() - 1)
                                .getEndDate()
                            == null
                        || employee
                            .getEmploymentContractList()
                            .get(employee.getEmploymentContractList().size() - 1)
                            .getEndDate()
                            .isAfter(today))) {
                  if (this.timesheetHRRepository
                          .findForTimesheetReminderToday(employee, company, today)
                          .size()
                      == 0) {
                    // If employee didn't submitted yet his timesheet we send reminder email
                    log.info(
                        "Reminding Timesheet to {}", employee.getContactPartner().getFullName());
                    try {
                      templateMessageService.generateAndSendMessage(
                          employee, hrConfigService.getReminderTimesheetTemplate(hrConfig));
                    } catch (Exception e) {
                      log.error("TimesheetReminderEmployee Error Sending email to employee", e);
                    }
                  }
                }
              });
        }
      }
    } catch (AxelorException e) {
      log.error("TimesheetReminderEmployee Error", e);
      throw new JobExecutionException(e);
    } finally {
      log.info("TimesheetReminderEmployee - COMPLETED EXECUTE");
    }
  }
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.