Add orderCalender

This commit is contained in:
Theo Reichert 2023-11-21 22:59:04 +01:00 committed by Simon Bruder
parent 391ddaf03b
commit b836a38942
Signed by: simon
GPG key ID: 8D3C82F9F309F8EC

View file

@ -11,6 +11,7 @@ import org.salespointframework.useraccount.Role;
import org.salespointframework.useraccount.UserAccount;
import org.salespointframework.useraccount.web.LoggedIn;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.query.Procedure;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
@ -23,10 +24,7 @@ import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalUnit;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.*;
import java.util.stream.Collectors;
@ -196,4 +194,54 @@ public class OrderController {
}
return "redirect:/event";
}
@GetMapping("/orders/calender")
public String calender(Model model) {
ArrayList<ArrayList<String>> datesOfMonth = new ArrayList<ArrayList<String>>(28);
LocalDateTime startDateTime = LocalDateTime.now();
LocalDateTime endDateTime = startDateTime.plusDays(27).withHour(23).withMinute(59).withSecond(59); // FIXME: set me to end of day
LocalDate startDate = startDateTime.toLocalDate();
LocalDate endDate = endDateTime.toLocalDate();
// create all dates of the calender
for (LocalDate date = startDate; !date.isAfter(endDate); date = date.plusDays(1)) {
ArrayList<String> x = new ArrayList<String>(2);
x.add(Integer.toString(date.getDayOfMonth()));
x.add(date.toString());
datesOfMonth.add(x);
}
// add each order overlapping with the calender to the days it overlaps with
for (CustomOrder order : this.orderManagement.findBy(Interval.from(startDateTime).to(endDateTime))) {
int start_index_inclusive = Math.max((int) startDate.until(order.getStart().toLocalDate(), ChronoUnit.DAYS),0);
int end_index_exclusive = Math.min((int) startDate.until(order.getFinish().toLocalDate(), ChronoUnit.DAYS), 27) + 1;
String order_id = Objects.requireNonNull(order.getId()).toString();
for (int i = start_index_inclusive; i < end_index_exclusive; i++) {
datesOfMonth.get(i).add(order_id);
}
}
// get names of weekdays for table header starting with the current day
LocalDate endOfWeekDate = startDate.plusDays(6);
ArrayList<String> dayNames = new ArrayList<String>(7);
for (LocalDate date = startDate; !date.isAfter(endOfWeekDate); date = date.plusDays(1)) {
dayNames.add(date.getDayOfWeek().toString());
}
ArrayList<ArrayList<ArrayList<String>>> weeksOfMonth = new ArrayList<ArrayList<ArrayList<String>>>();
for (int i = 0; i < 4; i++) {
weeksOfMonth.add(new ArrayList<ArrayList<String>>(7));
for (int j = 0; j < 7; j++) {
weeksOfMonth.get(i).add(new ArrayList<String>());
weeksOfMonth.get(i).get(j).addAll(datesOfMonth.get(
(i==0) ? j : (j==0) ? i : i*j
)
);
}
}
model.addAttribute("weeksOfMonth", weeksOfMonth);
model.addAttribute("dayNames", dayNames);
return "orders_calender";
}
}