diff --git a/src/main/java/catering/order/CustomOrder.java b/src/main/java/catering/order/CustomOrder.java index 0c3fd3f..7e3c7bb 100644 --- a/src/main/java/catering/order/CustomOrder.java +++ b/src/main/java/catering/order/CustomOrder.java @@ -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; @@ -38,14 +39,39 @@ public class CustomOrder extends Order { this.formatterPattern = cart.getFormatterPattern(); } - public CustomOrder() {} + 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()); diff --git a/src/main/java/catering/staff/StaffManagement.java b/src/main/java/catering/staff/StaffManagement.java index 3844946..2f48528 100644 --- a/src/main/java/catering/staff/StaffManagement.java +++ b/src/main/java/catering/staff/StaffManagement.java @@ -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)); + } }