Duration workPeriod = timeline .calculateDurationAllIntervalsByAttributes(DomainConstants.WORKED_ATTRIBUTES);  if (firstClockingDate != null && wsType.getMeal().getMealBreak().contains(firstClockingDate.toTimeOfDay(), false)) {  System.out.println("funcionario entrou no intervalo de almoco");
Duration workPeriod = timeline.calculateDurationAllIntervalsByAttributes(DomainConstants.WORKED_ATTRIBUTES); System.out.println("nwp: " + workPeriod);   if (wsType.getMeal().getMinimumMealBreakInterval().isEqual(Duration.ZERO)) { System.out.println("horario de isencao de horario");  if (firstClockingDate != null && wsType.getMeal().getMealBreak().contains(firstClockingDate.toTimeOfDay(), false)) {  System.out.println("funcionario entrou no intervalo de almoco");
    public DailyBalance calculateWorkingPeriods(YearMonthDay day, DateTime firstClockingDate,            Timeline timeline) {        DailyBalance dailyBalance = new DailyBalance(day, this);        Duration firstWorkPeriod = Duration.ZERO;        Duration lastWorkPeriod = Duration.ZERO;        TimeInterval mealInterval = null;        WorkScheduleType wsType = getWorkScheduleType();        if (wsType.definedMeal()) { // o horario tem um intervalo de refeicao definido            mealInterval = timeline.calculateMealBreakInterval(wsType.getMeal().getMealBreak()); // calcular            // o intervalo de refeicao dentro do periodo de refeicao definido            System.out.println("intervalo refeicao: " + mealInterval);            if (mealInterval != null) { // funcionario fez intervalo para almoco => ha 2 periodos de                // trabalho                dailyBalance.setLunchBreak(mealInterval.getDuration());                // actualiza almoco no dailyBalance calcula primeiro periodo de trabalho do horario                // normal: deste a entrada ate' 'a saida para o almoco                firstWorkPeriod = timeline.calculateDurationAllIntervalsByAttributesToTime(                        new TimePoint(mealInterval.getStartTime(), false, null),                        DomainConstants.WORKED_ATTRIBUTES);                System.out.println("primeiro periodo de trabalho:"                        + firstWorkPeriod.toPeriod().toString());                // calcula segundo periodo de trabalho do horario normal: desde entrada depois do almoco                // ate' 'a saida                if (((WorkPeriod) wsType.getNormalWorkPeriod()).isSecondWorkPeriodDefined()) {                    lastWorkPeriod = timeline.calculateDurationAllIntervalsByAttributesFromTime(                            new TimePoint(mealInterval.getEndTime(), false, null),                            DomainConstants.WORKED_ATTRIBUTES);                    // System.out.println("segundo periodo de trabalho: "                    // +lastWorkPeriod.toDuration().toPeriod().toString());                    // dailyBalance.setNormalWorkPeriod2Balance(lastWorkPeriod.minus(((NormalWorkPeriod)wsType.getNormalWorkPeriod()).getNormalWorkPeriod2Duration()));                    // System.out.println("balance 2o periodo: " +                    // dailyBalance.getNormalWorkPeriod2Balance().toPeriod().toString());                    System.out.println("segundo periodo de trabalho:"                            + lastWorkPeriod.toPeriod().toString());                }                // dailyBalance.setWorkedOnNormalWorkPeriod(wsType.checkNormalWorkPeriodAccordingToRules(firstWorkPeriod).plus(lastWorkPeriod));                dailyBalance.setWorkedOnNormalWorkPeriod(firstWorkPeriod.plus(lastWorkPeriod));                System.out.println("saldo antes de descontar a meal: "                        + dailyBalance.getNormalWorkPeriodBalance().toString());                wsType.checkMealDurationAccordingToRules(dailyBalance);            } else { // o funcionario nao foi almocar so ha 1 periodo de trabalho                System.out.println("funcionario nao foi almocar");                Duration workPeriod = timeline                        .calculateDurationAllIntervalsByAttributes(DomainConstants.WORKED_ATTRIBUTES);                // dailyBalance.setNormalWorkPeriod1Balance(worked.minus)                if (firstClockingDate != null                        && wsType.getMeal().getMealBreak().contains(firstClockingDate.toTimeOfDay(),                                false)) {                    // funcionario entrou no intervalo de almoco                    System.out.println("funcionario entrou no intervalo de almoco");                    // considera-se o periodo desde o inicio do intervalo de almoco + desconto                    // obrigatorio de almoco                    // se funcionario entrar nesse periodo de tempo e'-lhe descontado desde a entrada                    // ate' (inicio do intervalo de almoco + desconto obrigatorio de almoco)                    TimeOfDay lunchEnd = wsType.getMeal().getLunchEnd();                    TimeInterval lunchTime = new TimeInterval(wsType.getMeal().getBeginMealBreak(),                            lunchEnd, false);                    if (lunchTime.contains(firstClockingDate.toTimeOfDay(), false)) {                        workPeriod = workPeriod.minus(new TimeInterval(firstClockingDate.toTimeOfDay(),                                lunchEnd, false).getDurationMillis());                    }                    // DateTime endLunchTime =                    // wsType.getMeal().getLunchEnd().toDateTime(firstClockingDate);                    //                    // if (firstClockingDate.isBefore(endLunchTime)) {                    // Duration enteredDuringLunchDiscount = (new Interval(firstClockingDate,                    // endLunchTime)).toDuration();                    // workPeriod = workPeriod.minus(enteredDuringLunchDiscount);                    // }                    // } else if (firstClockingDate.toTimeOfDay()                    // .isBefore(wsType.getMeal().getBeginMealBreak())) {                    // workPeriod = workPeriod.minus(wsType.getMeal().getMandatoryMealDiscount());                } else if (firstClockingDate != null                        && firstClockingDate.toTimeOfDay().isBefore(                                wsType.getMeal().getMealBreak().getStartTime())) {                    workPeriod = workPeriod.minus(wsType.getMeal().getMandatoryMealDiscount());                }                dailyBalance.setWorkedOnNormalWorkPeriod(workPeriod);                System.out.println("nwp: "                        + dailyBalance.getWorkedOnNormalWorkPeriod().toPeriod().toString());            }        } else { // meal nao esta definida - so ha 1 periodo de trabalho            Duration worked = timeline                    .calculateDurationAllIntervalsByAttributes(DomainConstants.WORKED_ATTRIBUTES);            dailyBalance.setWorkedOnNormalWorkPeriod(worked);        }        System.out.println("total worked -> "                + dailyBalance.getWorkedOnNormalWorkPeriod().toPeriod().toString());        System.out.println("devia ter trabalhado ->"                + ((WorkPeriod) wsType.getNormalWorkPeriod()).getWorkPeriodDuration().toPeriod()                        .toString());        System.out.println("saldo ->"                + dailyBalance.getWorkedOnNormalWorkPeriod().minus(                        wsType.getNormalWorkPeriod().getWorkPeriodDuration()).toPeriod().toString());        // Fixed Periods if defined        wsType.calculateFixedPeriodDuration(dailyBalance, timeline);        return dailyBalance;    }