|
1 | 1 | package de.focusshift.zeiterfassung.report;
|
2 | 2 |
|
| 3 | +import de.focusshift.zeiterfassung.overtime.OvertimeDuration; |
| 4 | +import de.focusshift.zeiterfassung.timeentry.PlannedWorkingHours; |
3 | 5 | import de.focusshift.zeiterfassung.user.DateFormatter;
|
4 | 6 | import de.focusshift.zeiterfassung.user.UserId;
|
5 | 7 | import de.focusshift.zeiterfassung.usermanagement.User;
|
6 | 8 | import de.focusshift.zeiterfassung.usermanagement.UserLocalId;
|
| 9 | +import org.apache.commons.collections4.SetUtils; |
7 | 10 | import org.springframework.security.oauth2.core.oidc.user.OidcUser;
|
8 | 11 | import org.springframework.stereotype.Component;
|
9 | 12 | import org.springframework.ui.Model;
|
|
14 | 17 | import java.time.ZoneId;
|
15 | 18 | import java.time.ZonedDateTime;
|
16 | 19 | import java.time.temporal.ChronoField;
|
| 20 | +import java.util.ArrayList; |
17 | 21 | import java.util.Date;
|
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.HashSet; |
18 | 24 | import java.util.List;
|
| 25 | +import java.util.Map; |
| 26 | +import java.util.Optional; |
| 27 | +import java.util.Set; |
| 28 | +import java.util.function.Function; |
19 | 29 |
|
| 30 | +import static java.util.Comparator.comparing; |
| 31 | +import static java.util.function.Function.identity; |
20 | 32 | import static java.util.stream.Collectors.joining;
|
| 33 | +import static java.util.stream.Collectors.toList; |
| 34 | +import static java.util.stream.Collectors.toMap; |
| 35 | +import static java.util.stream.Collectors.toSet; |
21 | 36 |
|
22 | 37 | @Component
|
23 | 38 | class ReportControllerHelper {
|
@@ -101,6 +116,75 @@ DetailWeekDto toDetailWeekDto(ReportWeek reportWeek, Month monthPivot) {
|
101 | 116 | return new DetailWeekDto(Date.from(firstOfWeek.toInstant()), Date.from(lastOfWeek.toInstant()), calendarWeek, dayReports);
|
102 | 117 | }
|
103 | 118 |
|
| 119 | + ReportOvertimesDto reportOvertimesDto(ReportWeek reportWeek) { |
| 120 | + // person | M | T | W | T | F | S | S | |
| 121 | + // ----------------------------------- |
| 122 | + // john | 1 | 2 | 2 | 3 | 4 | 4 | 4 | <- `ReportOvertimeDto ( personName, overtimes )` |
| 123 | + // jane | 0 | 0 | 2 | 3 | 4 | 4 | 4 | entries in the middle of the week |
| 124 | + // jack | 0 | 0 | 0 | 0 | 0 | 0 | 0 | no entries this week |
| 125 | + // |
| 126 | + // note that the first overtime won't be empty actually, but the `accumulatedOvertimeToDate`. |
| 127 | + |
| 128 | + // build up `users` peace by peace. one person could have the first working day in the middle of the week (jane). |
| 129 | + final Set<User> users = new HashSet<>(); |
| 130 | + |
| 131 | + // {john} -> [1, 2, 2, 3, 4, 4, 4] |
| 132 | + // {jane} -> [empty, empty, 2, 3, 4, 4, 4] |
| 133 | + // {jack} -> [empty, empty, empty, empty, empty, empty, empty] (has no entries this week) |
| 134 | + final Map<User, List<Optional<OvertimeDuration>>> overtimeDurationsByUser = new HashMap<>(); |
| 135 | + |
| 136 | + // used to initiate the persons list of overtimes. |
| 137 | + // jane will be seen first on the third reportDay. she initially needs a list of `[null, null]`. |
| 138 | + int nrOfHandledDays = 0; |
| 139 | + |
| 140 | + for (ReportDay reportDay : reportWeek.reportDays()) { |
| 141 | + |
| 142 | + // planned working hours contains all users. even users without time entries at this day |
| 143 | + final Map<User, PlannedWorkingHours> plannedByUser = reportDay.plannedWorkingHoursByUser(); |
| 144 | + users.addAll(plannedByUser.keySet()); |
| 145 | + |
| 146 | + for (User user : users) { |
| 147 | + final var durations = overtimeDurationsByUser.computeIfAbsent(user, prepareOvertimeDurationList(nrOfHandledDays)); |
| 148 | + durations.add(reportDay.accumulatedOvertimeToDateEndOfBusinessByUser(user.localId())); |
| 149 | + } |
| 150 | + |
| 151 | + nrOfHandledDays++; |
| 152 | + } |
| 153 | + |
| 154 | + final Set<UserLocalId> userIdsWithDayEntries = users.stream().map(User::localId).collect(toSet()); |
| 155 | + final Map<User, List<PlannedWorkingHours>> usersWithPlannedWorkingHours = reportWeek.plannedWorkingHoursByUser(); |
| 156 | + final Map<UserLocalId, User> usersWithPlannedWorkingHoursById = usersWithPlannedWorkingHours.keySet().stream().collect(toMap(User::localId, identity())); |
| 157 | + final Set<UserLocalId> userIdsWithPlannedWorkingHours = usersWithPlannedWorkingHours.keySet().stream().map(User::localId).collect(toSet()); |
| 158 | + final SetUtils.SetView<UserLocalId> userIdsWithoutDayEntries = SetUtils.difference(userIdsWithPlannedWorkingHours, userIdsWithDayEntries); |
| 159 | + for (UserLocalId userLocalId : userIdsWithoutDayEntries) { |
| 160 | + overtimeDurationsByUser.computeIfAbsent(usersWithPlannedWorkingHoursById.get(userLocalId), prepareOvertimeDurationList(nrOfHandledDays)); |
| 161 | + } |
| 162 | + |
| 163 | + final List<ReportOvertimeDto> overtimeDtos = overtimeDurationsByUser.entrySet().stream() |
| 164 | + .map(entry -> new ReportOvertimeDto(entry.getKey().fullName(), overtimeDurationToDouble(entry.getValue()))) |
| 165 | + .sorted(comparing(ReportOvertimeDto::personName)) |
| 166 | + .collect(toList()); |
| 167 | + |
| 168 | + return new ReportOvertimesDto(reportWeek.dateOfWeeks(), overtimeDtos); |
| 169 | + } |
| 170 | + |
| 171 | + private static Function<User, List<Optional<OvertimeDuration>>> prepareOvertimeDurationList(int nrOfHandledDays) { |
| 172 | + return (unused) -> { |
| 173 | + final List<Optional<OvertimeDuration>> objects = new ArrayList<>(); |
| 174 | + for (int i = 0; i < nrOfHandledDays; i++) { |
| 175 | + objects.add(Optional.empty()); |
| 176 | + } |
| 177 | + return objects; |
| 178 | + }; |
| 179 | + } |
| 180 | + |
| 181 | + private static List<Double> overtimeDurationToDouble(List<Optional<OvertimeDuration>> overtimeDurations) { |
| 182 | + return overtimeDurations.stream() |
| 183 | + .map(maybe -> maybe.orElse(null)) |
| 184 | + .map(overtimeDuration -> overtimeDuration == null ? null : overtimeDuration.hoursDoubleValue()) |
| 185 | + .collect(toList()); |
| 186 | + } |
| 187 | + |
104 | 188 | String createUrl(String prefix, boolean allUsersSelected, List<UserLocalId> selectedUserLocalIds) {
|
105 | 189 | String url = prefix;
|
106 | 190 |
|
|
0 commit comments