use of com.axelor.apps.hr.db.LeaveRequest in project axelor-open-suite by axelor.
the class LeaveServiceImpl method getLeaves.
public List<LeaveRequest> getLeaves(User user, LocalDate date) {
List<LeaveRequest> leavesList = new ArrayList<>();
List<LeaveRequest> leaves = leaveRequestRepo.all().filter("self.user = :userId AND self.statusSelect IN (:awaitingValidation,:validated)").bind("userId", user).bind("awaitingValidation", LeaveRequestRepository.STATUS_AWAITING_VALIDATION).bind("validated", LeaveRequestRepository.STATUS_VALIDATED).fetch();
if (ObjectUtils.notEmpty(leaves)) {
for (LeaveRequest leave : leaves) {
LocalDate from = leave.getFromDateT().toLocalDate();
LocalDate to = leave.getToDateT().toLocalDate();
if ((from.isBefore(date) && to.isAfter(date)) || from.isEqual(date) || to.isEqual(date)) {
leavesList.add(leave);
}
}
}
return leavesList;
}
use of com.axelor.apps.hr.db.LeaveRequest in project axelor-open-suite by axelor.
the class HumanResourceMobileController method insertLeave.
/*
* This method is used in mobile application.
* It was in LeaveServiceImpl
* @param request
* @param response
*
* POST /open-suite-webapp/ws/action/com.axelor.apps.hr.mobile.HumanResourceMobileController:insertLeave
* Content-Type: application/json
*
* URL: com.axelor.apps.hr.mobile.HumanResourceMobileController:insertLeave
* fields: leaveReason, fromDateT, startOn, toDateT, endOn, comment
*
* payload:
* { "data": {
* "action": "com.axelor.apps.hr.mobile.HumanResourceMobileController:insertLeave",
* "leaveReason": 10,
* "fromDateT": "2018-02-22T10:30:00",
* "startOn": 1,
* "toDateT": "2018-02-24T:19:30:00",
* "endOn": 1,
* "comment": "no"
* } }
*/
@Transactional(rollbackOn = { Exception.class })
public void insertLeave(ActionRequest request, ActionResponse response) throws AxelorException {
AppBaseService appBaseService = Beans.get(AppBaseService.class);
User user = AuthUtils.getUser();
Map<String, Object> requestData = request.getData();
LeaveReason leaveReason = Beans.get(LeaveReasonRepository.class).find(Long.valueOf(requestData.get("leaveReason").toString()));
Employee employee = user.getEmployee();
if (employee == null) {
throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.LEAVE_USER_EMPLOYEE), user.getName());
}
if (leaveReason != null) {
LeaveRequest leave = new LeaveRequest();
leave.setUser(user);
Company company = null;
if (employee.getMainEmploymentContract() != null) {
company = employee.getMainEmploymentContract().getPayCompany();
}
leave.setCompany(company);
LeaveLine leaveLine = Beans.get(LeaveLineRepository.class).all().filter("self.employee = ?1 AND self.leaveReason = ?2", employee, leaveReason).fetchOne();
if (leaveLine == null) {
throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.LEAVE_LINE), employee.getName(), leaveReason.getName());
}
leave.setLeaveReason(leaveReason);
leave.setRequestDate(appBaseService.getTodayDate(company));
if (requestData.get("fromDateT") != null) {
leave.setFromDateT(LocalDateTime.parse(requestData.get("fromDateT").toString(), DateTimeFormatter.ISO_LOCAL_DATE_TIME));
}
leave.setStartOnSelect(new Integer(requestData.get("startOn").toString()));
if (requestData.get("toDateT") != null) {
leave.setToDateT(LocalDateTime.parse(requestData.get("toDateT").toString(), DateTimeFormatter.ISO_LOCAL_DATE_TIME));
}
leave.setEndOnSelect(new Integer(requestData.get("endOn").toString()));
leave.setDuration(Beans.get(LeaveService.class).computeDuration(leave));
leave.setStatusSelect(LeaveRequestRepository.STATUS_AWAITING_VALIDATION);
if (requestData.get("comments") != null) {
leave.setComments(requestData.get("comments").toString());
}
leave = Beans.get(LeaveRequestRepository.class).save(leave);
response.setTotal(1);
response.setValue("id", leave.getId());
}
}
use of com.axelor.apps.hr.db.LeaveRequest in project axelor-open-suite by axelor.
the class PayrollPreparationService method fillInLeaves.
public List<PayrollLeave> fillInLeaves(PayrollPreparation payrollPreparation) throws AxelorException {
List<PayrollLeave> payrollLeaveList = new ArrayList<>();
LocalDate fromDate = payrollPreparation.getPeriod().getFromDate();
LocalDate toDate = payrollPreparation.getPeriod().getToDate();
Employee employee = payrollPreparation.getEmployee();
if (employee.getWeeklyPlanning() == null) {
throw new AxelorException(payrollPreparation, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.EMPLOYEE_PLANNING), employee.getName());
}
List<LeaveRequest> leaveRequestList = leaveRequestRepo.all().filter("self.statusSelect = ?4 AND self.user.employee = ?3 AND ((self.fromDateT BETWEEN ?2 AND ?1 OR self.toDateT BETWEEN ?2 AND ?1) OR (?1 BETWEEN self.fromDateT AND self.toDateT OR ?2 BETWEEN self.fromDateT AND self.toDateT))", toDate, fromDate, employee, LeaveRequestRepository.STATUS_VALIDATED).fetch();
for (LeaveRequest leaveRequest : leaveRequestList) {
PayrollLeave payrollLeave = new PayrollLeave();
if (leaveRequest.getFromDateT().toLocalDate().isBefore(fromDate)) {
payrollLeave.setFromDate(fromDate);
} else {
payrollLeave.setFromDate(leaveRequest.getFromDateT().toLocalDate());
}
if (leaveRequest.getToDateT().toLocalDate().isAfter(toDate)) {
payrollLeave.setToDate(toDate);
} else {
payrollLeave.setToDate(leaveRequest.getToDateT().toLocalDate());
}
payrollLeave.setDuration(leaveService.computeLeaveDaysByLeaveRequest(fromDate, toDate, leaveRequest, employee));
payrollLeave.setLeaveReason(leaveRequest.getLeaveReason());
payrollLeave.setLeaveRequest(leaveRequest);
payrollLeaveList.add(payrollLeave);
}
return payrollLeaveList;
}
use of com.axelor.apps.hr.db.LeaveRequest in project axelor-open-suite by axelor.
the class TimesheetReportServiceImpl method getLeaveHours.
private BigDecimal getLeaveHours(User user, LocalDate date, BigDecimal dailyWorkingHours) throws AxelorException {
List<LeaveRequest> leavesList = leaveService.getLeaves(user, date);
BigDecimal totalLeaveHours = BigDecimal.ZERO;
for (LeaveRequest leave : leavesList) {
BigDecimal leaveHours = leaveService.computeDuration(leave, date, date);
if (leave.getLeaveReason().getUnitSelect() == LeaveReasonRepository.UNIT_SELECT_DAYS) {
leaveHours = leaveHours.multiply(dailyWorkingHours);
}
totalLeaveHours = totalLeaveHours.add(leaveHours);
}
return totalLeaveHours;
}
use of com.axelor.apps.hr.db.LeaveRequest in project axelor-open-suite by axelor.
the class LeaveController method cancel.
public void cancel(ActionRequest request, ActionResponse response) {
try {
LeaveRequest leave = request.getContext().asType(LeaveRequest.class);
leave = Beans.get(LeaveRequestRepository.class).find(leave.getId());
LeaveService leaveService = Beans.get(LeaveService.class);
leaveService.cancel(leave);
Message message = leaveService.sendCancellationEmail(leave);
if (message != null && message.getStatusSelect() == MessageRepository.STATUS_SENT) {
response.setFlash(String.format(I18n.get("Email sent to %s"), Beans.get(MessageServiceBaseImpl.class).getToRecipients(message)));
}
} catch (Exception e) {
TraceBackService.trace(response, e);
} finally {
response.setReload(true);
}
}
Aggregations