Search in sources :

Example 1 with HolidaySearch

use of com.autentia.tnt.dao.search.HolidaySearch in project TNTConcept by autentia.

the class UserHolidaysStateManager method calculateUserHolidaysState.

public UserHolidaysState calculateUserHolidaysState(User usuario, Date year) {
    UserHolidaysState uhs = new UserHolidaysState();
    uhs.setUser(usuario);
    // WorkingAgreement attribute is an HB proxy not initialized, we need to get it
    // from DB
    WorkingAgreementDAO workingAgreementDao = WorkingAgreementDAO.getDefault();
    WorkingAgreement agreement = workingAgreementDao.loadById(usuario.getAgreement().getId());
    // and refresh the user instance agreement attribute
    usuario.setAgreement(agreement);
    uhs.setTotalYear(agreement.getHolidays());
    int acceptedHolidays = 0;
    if (year != null) {
        HolidayManager fiestasManager = HolidayManager.getDefault();
        // We must take in account previous year holidays and next year holidays
        Date firstDayOfYear = com.autentia.tnt.util.DateUtils.getFirstDayOfYear(year);
        Date lastDayOfYear = com.autentia.tnt.util.DateUtils.getLastDayOfYear(year);
        Calendar calendarFirstDayOfYear = Calendar.getInstance();
        calendarFirstDayOfYear.setTime(firstDayOfYear);
        calendarFirstDayOfYear.add(Calendar.YEAR, -1);
        Calendar calendarLastDayOfYear = Calendar.getInstance();
        calendarLastDayOfYear.setTime(lastDayOfYear);
        calendarLastDayOfYear.add(Calendar.YEAR, 1);
        HolidaySearch fiestaSearch = new HolidaySearch();
        fiestaSearch.setStartDate(calendarFirstDayOfYear.getTime());
        fiestaSearch.setEndDate(calendarLastDayOfYear.getTime());
        List<Holiday> allFiestas = fiestasManager.getAllEntities(fiestaSearch, null);
        firstDayOfYear = com.autentia.tnt.util.DateUtils.getFirstDayOfYear(year);
        calendarFirstDayOfYear.setTime(firstDayOfYear);
        lastDayOfYear = com.autentia.tnt.util.DateUtils.getLastDayOfYear(year);
        calendarLastDayOfYear.setTime(lastDayOfYear);
        RequestHolidayManager holyManager = RequestHolidayManager.getDefault();
        RequestHolidaySearch holSearch = new RequestHolidaySearch();
        holSearch.setUserRequest(uhs.getUser());
        holSearch.setState(HolidayState.ACCEPT);
        holSearch.setStartChargeYear(calendarFirstDayOfYear.getTime());
        holSearch.setEndChargeYear(calendarLastDayOfYear.getTime());
        List<RequestHoliday> listH = holyManager.getAllEntities(holSearch, null);
        for (RequestHoliday rH : listH) {
            Calendar cActual = Calendar.getInstance();
            cActual.setTime(rH.getBeginDate());
            while (!cActual.getTime().after(rH.getFinalDate())) {
                if (cActual.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY && cActual.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
                    boolean isFiesta = false;
                    for (Holiday fiest : allFiestas) {
                        Calendar cFiesta = Calendar.getInstance();
                        cFiesta.setTime(fiest.getDate());
                        if (cFiesta.get(Calendar.YEAR) == cActual.get(Calendar.YEAR) && cFiesta.get(Calendar.MONTH) == cActual.get(Calendar.MONTH) && cFiesta.get(Calendar.DAY_OF_MONTH) == cActual.get(Calendar.DAY_OF_MONTH)) {
                            isFiesta = true;
                        }
                    }
                    if (!isFiesta) {
                        acceptedHolidays++;
                    }
                }
                cActual.add(Calendar.DAY_OF_MONTH, 1);
            }
        }
        uhs.setTotalAccepted(acceptedHolidays);
        Calendar calAuxCont = Calendar.getInstance();
        calAuxCont.setTime(uhs.getUser().getStartDate());
        Calendar calAux = Calendar.getInstance();
        calAux.setTime(year);
        int yearCharge = calAux.get(Calendar.YEAR);
        int yearContract = calAuxCont.get(Calendar.YEAR);
        if (yearCharge == yearContract) {
            // Dividimos los días de cada usuario entre los dias del año.
            double ratio = uhs.getUser().getAgreement().getHolidays() / 360.0;
            int dayContract = calAuxCont.get(Calendar.DAY_OF_YEAR);
            int workedDays = (360 - dayContract);
            double holidayDays = workedDays * ratio;
            double aux = Math.round(holidayDays);
            uhs.setTotalYear((int) aux);
        }
    }
    return uhs;
}
Also used : Calendar(java.util.Calendar) UserHolidaysState(com.autentia.tnt.businessobject.UserHolidaysState) Date(java.util.Date) WorkingAgreementDAO(com.autentia.tnt.dao.hibernate.WorkingAgreementDAO) RequestHoliday(com.autentia.tnt.businessobject.RequestHoliday) Holiday(com.autentia.tnt.businessobject.Holiday) RequestHoliday(com.autentia.tnt.businessobject.RequestHoliday) HolidaySearch(com.autentia.tnt.dao.search.HolidaySearch) RequestHolidaySearch(com.autentia.tnt.dao.search.RequestHolidaySearch) RequestHolidaySearch(com.autentia.tnt.dao.search.RequestHolidaySearch) WorkingAgreement(com.autentia.tnt.businessobject.WorkingAgreement)

Example 2 with HolidaySearch

use of com.autentia.tnt.dao.search.HolidaySearch in project TNTConcept by autentia.

the class UserHolidaysStateManager method getWorkingDays.

/**
 * @return Devuelve el número de días laborables que hay entre dos fechas
 */
public int getWorkingDays(Date fromDate, Date toDate) {
    int total = 0;
    // Evitamos un bucle infinito en el bucle que viene a continuación
    if (fromDate.before(toDate) || DateUtils.isSameDay(fromDate, toDate)) {
        HolidaySearch fiestaSearch = new HolidaySearch();
        Date current = (Date) fromDate.clone();
        fiestaSearch.setStartDate(fromDate);
        fiestaSearch.setEndDate(toDate);
        List<Holiday> allFiestas = HolidayManager.getDefault().getAllEntities(fiestaSearch, null);
        Calendar fromYear = Calendar.getInstance();
        Calendar toYear = Calendar.getInstance();
        fromYear.setTime(fromDate);
        toYear.setTime(toDate);
        while (current.before(toDate) || DateUtils.isSameDay(current, toDate)) {
            if (!this.isHoliday(allFiestas, current)) {
                total++;
            }
            current = DateUtils.addDays(current, 1);
        }
    }
    return total;
}
Also used : Holiday(com.autentia.tnt.businessobject.Holiday) RequestHoliday(com.autentia.tnt.businessobject.RequestHoliday) Calendar(java.util.Calendar) HolidaySearch(com.autentia.tnt.dao.search.HolidaySearch) RequestHolidaySearch(com.autentia.tnt.dao.search.RequestHolidaySearch) Date(java.util.Date)

Example 3 with HolidaySearch

use of com.autentia.tnt.dao.search.HolidaySearch in project TNTConcept by autentia.

the class AvailabilityBean method fillModelHolidays.

/**
 * Fill holidays in listOccupations for that user.
 *
 * @param user
 * @param model
 */
private void fillModelHolidays(User user, OcupationModel model) {
    Calendar calendarFirstDayOfMonth = Calendar.getInstance();
    Calendar calendarLastDayOfMonth = Calendar.getInstance();
    Date firstDayOfMonth = DateUtils.getFirstDayOfMonth(selectedDate);
    Date lastDayOfMonth = DateUtils.getLastDayOfMonth(selectedDate);
    calendarFirstDayOfMonth.setTime(firstDayOfMonth);
    calendarLastDayOfMonth.setTime(lastDayOfMonth);
    calendarFirstDayOfMonth.add(Calendar.MONTH, -1);
    calendarLastDayOfMonth.add(Calendar.MONTH, 1);
    RequestHolidayManager rhManager = RequestHolidayManager.getDefault();
    RequestHolidaySearch rhSearch = new RequestHolidaySearch();
    rhSearch.setUserRequest(user);
    rhSearch.setState(HolidayState.ACCEPT);
    rhSearch.setStartBeginDate(calendarFirstDayOfMonth.getTime());
    rhSearch.setEndBeginDate(calendarLastDayOfMonth.getTime());
    rhSearch.setStartFinalDate(calendarFirstDayOfMonth.getTime());
    rhSearch.setEndFinalDate(calendarLastDayOfMonth.getTime());
    List<RequestHoliday> listH = rhManager.getAllEntities(rhSearch, null);
    for (RequestHoliday rH : listH) {
        OcupationEntryImpl oc = new OcupationEntryImpl();
        oc.setStart(DateUtils.minHourInDate(rH.getBeginDate()));
        oc.setEnd(DateUtils.maxHourInDate(rH.getFinalDate()));
        oc.setVacances(true);
        oc.setHoliday(false);
        oc.setDescription(FacesUtils.getMessage("activity.acceptedHolidays"));
        model.addOcupationEntry(oc);
    }
    HolidaySearch monthSearch = new HolidaySearch();
    HolidayManager holidayManager = HolidayManager.getDefault();
    monthSearch.setStartDate(calendarFirstDayOfMonth.getTime());
    monthSearch.setEndDate(calendarLastDayOfMonth.getTime());
    List<Holiday> allHolidays = holidayManager.getAllEntities(monthSearch, null);
    for (Holiday holiday : allHolidays) {
        OcupationEntryImpl oc = new OcupationEntryImpl();
        oc.setStart(DateUtils.minHourInDate(holiday.getDate()));
        oc.setEnd(DateUtils.maxHourInDate(holiday.getDate()));
        oc.setVacances(false);
        oc.setHoliday(true);
        oc.setDescription(holiday.getDescription());
        model.addOcupationEntry(oc);
    }
}
Also used : RequestHoliday(com.autentia.tnt.businessobject.RequestHoliday) Holiday(com.autentia.tnt.businessobject.Holiday) RequestHoliday(com.autentia.tnt.businessobject.RequestHoliday) Calendar(java.util.Calendar) RequestHolidayManager(com.autentia.tnt.manager.holiday.RequestHolidayManager) HolidayManager(com.autentia.tnt.manager.holiday.HolidayManager) RequestHolidayManager(com.autentia.tnt.manager.holiday.RequestHolidayManager) HolidaySearch(com.autentia.tnt.dao.search.HolidaySearch) RequestHolidaySearch(com.autentia.tnt.dao.search.RequestHolidaySearch) RequestHolidaySearch(com.autentia.tnt.dao.search.RequestHolidaySearch) Date(java.util.Date)

Example 4 with HolidaySearch

use of com.autentia.tnt.dao.search.HolidaySearch in project TNTConcept by autentia.

the class ActivityBean method fillHolidays.

private void fillHolidays(SimpleScheduleModel model) {
    Calendar calMin = Calendar.getInstance();
    Calendar calMax = Calendar.getInstance();
    calMin = getMinimumSearchTime(selectedDate);
    calMax = getMaximumSearchTime(selectedDate);
    RequestHolidayManager rhManager = RequestHolidayManager.getDefault();
    RequestHolidaySearch rhSearch = new RequestHolidaySearch();
    rhSearch.setUserRequest(authManager.getCurrentPrincipal().getUser());
    rhSearch.setState(HolidayState.ACCEPT);
    rhSearch.setStartBeginDate(calMin.getTime());
    rhSearch.setEndBeginDate(calMax.getTime());
    rhSearch.setStartFinalDate(calMin.getTime());
    rhSearch.setEndFinalDate(calMax.getTime());
    List<RequestHoliday> listH = rhManager.getAllEntities(rhSearch, null);
    for (RequestHoliday rH : listH) {
        Calendar cActual = Calendar.getInstance();
        cActual.setTime(rH.getBeginDate());
        while (!cActual.getTime().after(rH.getFinalDate())) {
            if (cActual.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY && cActual.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
                model.setHoliday(cActual.getTime(), FacesUtils.getMessage("activity.acceptedHolidays"));
            }
            cActual.add(Calendar.DAY_OF_MONTH, 1);
        }
    }
    HolidaySearch monthSearch = new HolidaySearch();
    monthSearch.setStartDate(calMin.getTime());
    monthSearch.setEndDate(calMax.getTime());
    List<Holiday> allHolidays = holidayManager.getAllEntities(monthSearch, null);
    for (Holiday holiday : allHolidays) {
        model.setHoliday(holiday.getDate(), holiday.getDescription());
    }
}
Also used : RequestHoliday(com.autentia.tnt.businessobject.RequestHoliday) Holiday(com.autentia.tnt.businessobject.Holiday) RequestHoliday(com.autentia.tnt.businessobject.RequestHoliday) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) RequestHolidayManager(com.autentia.tnt.manager.holiday.RequestHolidayManager) HolidaySearch(com.autentia.tnt.dao.search.HolidaySearch) RequestHolidaySearch(com.autentia.tnt.dao.search.RequestHolidaySearch) RequestHolidaySearch(com.autentia.tnt.dao.search.RequestHolidaySearch)

Example 5 with HolidaySearch

use of com.autentia.tnt.dao.search.HolidaySearch in project TNTConcept by autentia.

the class ActivityBean method calculateHolidays.

private int calculateHolidays(List<Integer> nonWorkingDays, Date date, int daysInMonth) {
    LocalDate selectedDay = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).toLocalDate();
    LocalDate firstDaySelectedMonth = selectedDay.withDayOfMonth(1);
    LocalDate lastDaySelectedMonth = selectedDay.with(TemporalAdjusters.lastDayOfMonth());
    Date beginOfMonth = Date.from(firstDaySelectedMonth.atStartOfDay(ZoneId.systemDefault()).toInstant());
    Date endOfMonth = Date.from(lastDaySelectedMonth.atStartOfDay(ZoneId.systemDefault()).toInstant());
    HolidaySearch monthSearch = new HolidaySearch();
    monthSearch.setStartDate(beginOfMonth);
    monthSearch.setEndDate(endOfMonth);
    List<Holiday> allHolidays = holidayManager.getAllEntities(monthSearch, null);
    Calendar calMin = Calendar.getInstance();
    Calendar calMax = Calendar.getInstance();
    calMin.setTime(beginOfMonth);
    calMax.setTime(endOfMonth);
    int holidays = 0;
    for (Holiday holiday : allHolidays) {
        LocalDateTime holidayValue = LocalDateTime.ofInstant(holiday.getDate().toInstant(), ZoneId.systemDefault());
        int day = holidayValue.getDayOfMonth();
        if (!nonWorkingDays.contains(day) && day <= daysInMonth) {
            holidays++;
            nonWorkingDays.add(day);
        }
    }
    return holidays;
}
Also used : LocalDateTime(java.time.LocalDateTime) Holiday(com.autentia.tnt.businessobject.Holiday) RequestHoliday(com.autentia.tnt.businessobject.RequestHoliday) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) HolidaySearch(com.autentia.tnt.dao.search.HolidaySearch) RequestHolidaySearch(com.autentia.tnt.dao.search.RequestHolidaySearch) LocalDate(java.time.LocalDate) Date(java.util.Date) LocalDate(java.time.LocalDate)

Aggregations

Holiday (com.autentia.tnt.businessobject.Holiday)5 RequestHoliday (com.autentia.tnt.businessobject.RequestHoliday)5 HolidaySearch (com.autentia.tnt.dao.search.HolidaySearch)5 RequestHolidaySearch (com.autentia.tnt.dao.search.RequestHolidaySearch)5 Calendar (java.util.Calendar)5 Date (java.util.Date)4 RequestHolidayManager (com.autentia.tnt.manager.holiday.RequestHolidayManager)2 GregorianCalendar (java.util.GregorianCalendar)2 UserHolidaysState (com.autentia.tnt.businessobject.UserHolidaysState)1 WorkingAgreement (com.autentia.tnt.businessobject.WorkingAgreement)1 WorkingAgreementDAO (com.autentia.tnt.dao.hibernate.WorkingAgreementDAO)1 HolidayManager (com.autentia.tnt.manager.holiday.HolidayManager)1 LocalDate (java.time.LocalDate)1 LocalDateTime (java.time.LocalDateTime)1