Search in sources :

Example 1 with UserHolidaysState

use of com.autentia.tnt.businessobject.UserHolidaysState 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 UserHolidaysState

use of com.autentia.tnt.businessobject.UserHolidaysState in project TNTConcept by autentia.

the class UserHolidaysStateBean method getAll.

public List<UserHolidaysState> getAll() {
    List<UserHolidaysState> ret = new ArrayList<UserHolidaysState>();
    if (chargeYear == null)
        return ret;
    UserManager userManager = UserManager.getDefault();
    UserSearch searchUser = new UserSearch();
    searchUser.setActive(true);
    List<User> users = userManager.getAllEntities(searchUser, new SortCriteria(sortColumn, sortAscending));
    for (User us : users) {
        ret.add(manager.calculateUserHolidaysState(us, this.getChargeYear()));
    }
    return ret;
}
Also used : SortCriteria(com.autentia.tnt.dao.SortCriteria) User(com.autentia.tnt.businessobject.User) UserManager(com.autentia.tnt.manager.admin.UserManager) ArrayList(java.util.ArrayList) UserHolidaysState(com.autentia.tnt.businessobject.UserHolidaysState) UserSearch(com.autentia.tnt.dao.search.UserSearch)

Aggregations

UserHolidaysState (com.autentia.tnt.businessobject.UserHolidaysState)2 Holiday (com.autentia.tnt.businessobject.Holiday)1 RequestHoliday (com.autentia.tnt.businessobject.RequestHoliday)1 User (com.autentia.tnt.businessobject.User)1 WorkingAgreement (com.autentia.tnt.businessobject.WorkingAgreement)1 SortCriteria (com.autentia.tnt.dao.SortCriteria)1 WorkingAgreementDAO (com.autentia.tnt.dao.hibernate.WorkingAgreementDAO)1 HolidaySearch (com.autentia.tnt.dao.search.HolidaySearch)1 RequestHolidaySearch (com.autentia.tnt.dao.search.RequestHolidaySearch)1 UserSearch (com.autentia.tnt.dao.search.UserSearch)1 UserManager (com.autentia.tnt.manager.admin.UserManager)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1