Implement per-month employee working hours

This is not yet finished, as the new/changed methods in CustomOrder are
quite a mess.
This commit is contained in:
Simon Bruder 2023-12-05 15:21:22 +01:00
parent ee7ab27a6d
commit fd7d05a0f6
Signed by: simon
GPG key ID: 8D3C82F9F309F8EC
2 changed files with 36 additions and 3 deletions

View file

@ -11,6 +11,7 @@ import org.springframework.data.annotation.Id;
import javax.money.MonetaryAmount;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.HashSet;
import java.util.Set;
@ -40,12 +41,37 @@ public class CustomOrder extends Order {
public CustomOrder() { }
private long getDurationInSeconds(LocalDateTime start, LocalDateTime finish) {
if (start.compareTo(finish) > 0) {
return 0;
}
return Duration.between(start, finish).getSeconds();
}
/**
* Helper function to get the amount of hours in between start and finish (analogous to CustomCart)
* @return hours between start and finish
*/
private long getDurationInHours(LocalDateTime start, LocalDateTime finish) {
return Duration.between(start, finish).getSeconds() / 3600;
return getDurationInSeconds(start, finish) / 3600;
}
public long getDurationInHours() {
return getDurationInHours(start, finish);
}
private static LocalDateTime min(LocalDateTime a, LocalDateTime b) {
return (a.compareTo(b)) < 0 ? a : b;
}
private static LocalDateTime max(LocalDateTime a, LocalDateTime b) {
return (a.compareTo(b)) > 0 ? a : b;
}
public long getDurationInSecondsDuringMonth(YearMonth month) {
LocalDateTime startInMonth = max(this.start, month.atDay(1).atStartOfDay());
LocalDateTime finishInMonth = min(this.finish, month.plusMonths(1).atDay(1).atStartOfDay());
return getDurationInSeconds(startInMonth, finishInMonth);
}
/**
@ -53,7 +79,7 @@ public class CustomOrder extends Order {
*/
public boolean addEmployee(Employee employee) {
MonetaryAmount cost = Money.of(12.0, "EUR")
.multiply(getDurationInHours(this.start, this.finish)); // TODO: Get salary from employee
.multiply(getDurationInHours()); // TODO: Get salary from employee
if (this.staff.add(employee)) {
super.addChargeLine(cost, employee.getId().toString());

View file

@ -1,6 +1,7 @@
package catering.staff;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
@ -104,4 +105,10 @@ public class StaffManagement {
.collect(Collectors.toSet());
}
public double getWorkingHoursByEmployee(Employee e, YearMonth month) {
return orderManagement.findAll(Pageable.unpaged()).stream()
.filter(order -> order.getStaff().contains(e))
.map(order -> order.getDurationInSecondsDuringMonth(month))
.collect(Collectors.summingDouble(seconds -> (double) seconds / 3600));
}
}