use of com.axelor.apps.base.service.weeklyplanning.WeeklyPlanningService in project axelor-open-suite by axelor.
the class TimesheetServiceImpl method prefillLines.
@Override
public void prefillLines(Timesheet timesheet) throws AxelorException {
PublicHolidayService holidayService = Beans.get(PublicHolidayService.class);
LeaveService leaveService = Beans.get(LeaveService.class);
WeeklyPlanningService weeklyPlanningService = Beans.get(WeeklyPlanningService.class);
AppTimesheet appTimesheet = appHumanResourceService.getAppTimesheet();
LocalDate fromDate = timesheet.getFromDate();
LocalDate toDate = timesheet.getToDate();
User user = timesheet.getUser();
Employee employee = user.getEmployee();
HRConfig config = timesheet.getCompany().getHrConfig();
WeeklyPlanning weeklyPlanning = employee != null ? employee.getWeeklyPlanning() : config.getWeeklyPlanning();
EventsPlanning holidayPlanning = employee != null ? employee.getPublicHolidayEventsPlanning() : config.getPublicHolidayEventsPlanning();
for (LocalDate date = fromDate; !date.isAfter(toDate); date = date.plusDays(1)) {
BigDecimal dayValueInHours = weeklyPlanningService.getWorkingDayValueInHours(weeklyPlanning, date, LocalTime.MIN, LocalTime.MAX);
if (appTimesheet.getCreateLinesForHolidays() && holidayService.checkPublicHolidayDay(date, holidayPlanning)) {
timesheetLineService.createTimesheetLine(user, date, timesheet, dayValueInHours, I18n.get(IExceptionMessage.TIMESHEET_HOLIDAY));
} else if (appTimesheet.getCreateLinesForLeaves()) {
List<LeaveRequest> leaveList = leaveService.getLeaves(user, date);
BigDecimal totalLeaveHours = BigDecimal.ZERO;
if (ObjectUtils.notEmpty(leaveList)) {
for (LeaveRequest leave : leaveList) {
BigDecimal leaveHours = leaveService.computeDuration(leave, date, date);
if (leave.getLeaveReason().getUnitSelect() == LeaveReasonRepository.UNIT_SELECT_DAYS) {
leaveHours = leaveHours.multiply(dayValueInHours);
}
totalLeaveHours = totalLeaveHours.add(leaveHours);
}
timesheetLineService.createTimesheetLine(user, date, timesheet, totalLeaveHours, I18n.get(IExceptionMessage.TIMESHEET_DAY_LEAVE));
}
}
}
}
use of com.axelor.apps.base.service.weeklyplanning.WeeklyPlanningService in project axelor-open-suite by axelor.
the class OperationOrderServiceImpl method chargeByMachineDays.
public List<Map<String, Object>> chargeByMachineDays(LocalDateTime fromDateTime, LocalDateTime toDateTime) throws AxelorException {
List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
fromDateTime = fromDateTime.withHour(0).withMinute(0);
toDateTime = toDateTime.withHour(23).withMinute(59);
LocalDateTime itDateTime = LocalDateTime.parse(fromDateTime.toString(), DateTimeFormatter.ISO_DATE_TIME);
if (Duration.between(fromDateTime, toDateTime).toDays() > 500) {
throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.CHARGE_MACHINE_DAYS));
}
List<OperationOrder> operationOrderListTemp = Beans.get(OperationOrderRepository.class).all().filter("self.plannedStartDateT <= ?2 AND self.plannedEndDateT >= ?1", fromDateTime, toDateTime).fetch();
Set<String> machineNameList = new HashSet<String>();
for (OperationOrder operationOrder : operationOrderListTemp) {
if (operationOrder.getWorkCenter() != null && operationOrder.getWorkCenter().getMachine() != null) {
if (!machineNameList.contains(operationOrder.getWorkCenter().getMachine().getName())) {
machineNameList.add(operationOrder.getWorkCenter().getMachine().getName());
}
}
}
while (!itDateTime.isAfter(toDateTime)) {
List<OperationOrder> operationOrderList = Beans.get(OperationOrderRepository.class).all().filter("self.plannedStartDateT <= ?2 AND self.plannedEndDateT >= ?1", itDateTime, itDateTime.plusHours(1)).fetch();
Map<String, BigDecimal> map = new HashMap<String, BigDecimal>();
WeeklyPlanningService weeklyPlanningService = Beans.get(WeeklyPlanningService.class);
for (OperationOrder operationOrder : operationOrderList) {
if (operationOrder.getWorkCenter() != null && operationOrder.getWorkCenter().getMachine() != null) {
String machine = operationOrder.getWorkCenter().getMachine().getName();
long numberOfMinutes = 0;
if (operationOrder.getPlannedStartDateT().isBefore(itDateTime)) {
numberOfMinutes = Duration.between(itDateTime, operationOrder.getPlannedEndDateT()).toMinutes();
} else if (operationOrder.getPlannedEndDateT().isAfter(itDateTime.plusHours(1))) {
numberOfMinutes = Duration.between(operationOrder.getPlannedStartDateT(), itDateTime.plusHours(1)).toMinutes();
} else {
numberOfMinutes = Duration.between(operationOrder.getPlannedStartDateT(), operationOrder.getPlannedEndDateT()).toMinutes();
}
if (numberOfMinutes > 60) {
numberOfMinutes = 60;
}
long numberOfMinutesPerDay = 0;
if (operationOrder.getWorkCenter().getMachine().getWeeklyPlanning() != null) {
DayPlanning dayPlanning = weeklyPlanningService.findDayPlanning(operationOrder.getWorkCenter().getMachine().getWeeklyPlanning(), LocalDateTime.parse(itDateTime.toString(), DateTimeFormatter.ISO_DATE_TIME).toLocalDate());
if (dayPlanning != null) {
if (dayPlanning.getMorningFrom() != null && dayPlanning.getMorningTo() != null) {
numberOfMinutesPerDay = Duration.between(dayPlanning.getMorningFrom(), dayPlanning.getMorningTo()).toMinutes();
}
if (dayPlanning.getAfternoonFrom() != null && dayPlanning.getAfternoonTo() != null) {
numberOfMinutesPerDay += Duration.between(dayPlanning.getAfternoonFrom(), dayPlanning.getAfternoonTo()).toMinutes();
}
if (dayPlanning.getMorningFrom() != null && dayPlanning.getMorningTo() == null && dayPlanning.getAfternoonFrom() == null && dayPlanning.getAfternoonTo() != null) {
numberOfMinutesPerDay += Duration.between(dayPlanning.getMorningFrom(), dayPlanning.getAfternoonTo()).toMinutes();
}
} else {
numberOfMinutesPerDay = 0;
}
} else {
numberOfMinutesPerDay = 60 * 24;
}
if (numberOfMinutesPerDay != 0) {
BigDecimal percentage = new BigDecimal(numberOfMinutes).multiply(new BigDecimal(100)).divide(new BigDecimal(numberOfMinutesPerDay), 2, RoundingMode.HALF_UP);
if (map.containsKey(machine)) {
map.put(machine, map.get(machine).add(percentage));
} else {
map.put(machine, percentage);
}
}
}
}
Set<String> keyList = map.keySet();
for (String key : machineNameList) {
if (keyList.contains(key)) {
int found = 0;
for (Map<String, Object> mapIt : dataList) {
if (mapIt.get("dateTime").equals((Object) itDateTime.format(DATE_FORMAT)) && mapIt.get("machine").equals((Object) key)) {
mapIt.put("charge", new BigDecimal(mapIt.get("charge").toString()).add(map.get(key)));
found = 1;
break;
}
}
if (found == 0) {
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put("dateTime", (Object) itDateTime.format(DATE_FORMAT));
dataMap.put("charge", (Object) map.get(key));
dataMap.put("machine", (Object) key);
dataList.add(dataMap);
}
}
}
itDateTime = itDateTime.plusHours(1);
}
return dataList;
}
Aggregations