Search in sources :

Example 1 with WeeklyPlanningService

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));
            }
        }
    }
}
Also used : User(com.axelor.auth.db.User) EventsPlanning(com.axelor.apps.base.db.EventsPlanning) WeeklyPlanning(com.axelor.apps.base.db.WeeklyPlanning) LeaveRequest(com.axelor.apps.hr.db.LeaveRequest) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) AppTimesheet(com.axelor.apps.base.db.AppTimesheet) Employee(com.axelor.apps.hr.db.Employee) HRConfig(com.axelor.apps.hr.db.HRConfig) LeaveService(com.axelor.apps.hr.service.leave.LeaveService) WeeklyPlanningService(com.axelor.apps.base.service.weeklyplanning.WeeklyPlanningService) List(java.util.List) PriceList(com.axelor.apps.base.db.PriceList) ArrayList(java.util.ArrayList) PublicHolidayService(com.axelor.apps.base.service.publicHoliday.PublicHolidayService)

Example 2 with WeeklyPlanningService

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;
}
Also used : LocalDateTime(java.time.LocalDateTime) AxelorException(com.axelor.exception.AxelorException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DayPlanning(com.axelor.apps.base.db.DayPlanning) BigDecimal(java.math.BigDecimal) WeeklyPlanningService(com.axelor.apps.base.service.weeklyplanning.WeeklyPlanningService) OperationOrderRepository(com.axelor.apps.production.db.repo.OperationOrderRepository) HashMap(java.util.HashMap) Map(java.util.Map) OperationOrder(com.axelor.apps.production.db.OperationOrder) HashSet(java.util.HashSet)

Aggregations

WeeklyPlanningService (com.axelor.apps.base.service.weeklyplanning.WeeklyPlanningService)2 BigDecimal (java.math.BigDecimal)2 ArrayList (java.util.ArrayList)2 AppTimesheet (com.axelor.apps.base.db.AppTimesheet)1 DayPlanning (com.axelor.apps.base.db.DayPlanning)1 EventsPlanning (com.axelor.apps.base.db.EventsPlanning)1 PriceList (com.axelor.apps.base.db.PriceList)1 WeeklyPlanning (com.axelor.apps.base.db.WeeklyPlanning)1 PublicHolidayService (com.axelor.apps.base.service.publicHoliday.PublicHolidayService)1 Employee (com.axelor.apps.hr.db.Employee)1 HRConfig (com.axelor.apps.hr.db.HRConfig)1 LeaveRequest (com.axelor.apps.hr.db.LeaveRequest)1 LeaveService (com.axelor.apps.hr.service.leave.LeaveService)1 OperationOrder (com.axelor.apps.production.db.OperationOrder)1 OperationOrderRepository (com.axelor.apps.production.db.repo.OperationOrderRepository)1 User (com.axelor.auth.db.User)1 AxelorException (com.axelor.exception.AxelorException)1 LocalDate (java.time.LocalDate)1 LocalDateTime (java.time.LocalDateTime)1 HashMap (java.util.HashMap)1