package catering.order; import catering.staff.Staff; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.OneToMany; import org.javamoney.moneta.Money; import org.salespointframework.order.Order; import org.salespointframework.payment.Cash; import org.salespointframework.useraccount.UserAccount; import org.springframework.data.annotation.Id; import javax.money.MonetaryAmount; import java.time.Duration; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.HashSet; import java.util.Set; @Entity public class CustomOrder extends Order { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @OneToMany private Set staff; private OrderType orderType = OrderType.SOMETHING_ELSE; private LocalDateTime start; private LocalDateTime finish; private boolean invoiceAvailable = false; private String formatterPattern = "dd.MM.yyyy, HH:mm 'Uhr'"; // Constructor public CustomOrder(UserAccount.UserAccountIdentifier identifier, CustomCart cart) { super(identifier, Cash.CASH); this.staff = new HashSet<>(); this.orderType = cart.getOrderType(); this.start = cart.getStart(); this.finish = cart.getFinish(); this.formatterPattern = cart.getFormatterPattern(); } public CustomOrder() {} /** * 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; } /** * Adds an employee to the order and adds a new {@Link ChangeLine} containing the costs */ public boolean addStaff(Staff staff) { MonetaryAmount cost = Money.of(12.0, "EUR") .multiply(getDurationInHours(this.start, this.finish)); // TODO: Get salary from staff if (this.staff.add(staff)) { super.addChargeLine(cost, staff.getId().toString()); return true; } return false; } public Set getStaff() { return staff; } public OrderType getOrderType() { return orderType; } public LocalDateTime getStart() { return start; } /** * @return start DateTime in a formatted manner */ public String getFormattedStart() { return start.format(DateTimeFormatter.ofPattern(formatterPattern)); } public LocalDateTime getFinish() { return finish; } /** * @return finish DateTime in a formatted manner */ public String getFormattedFinish() { return finish.format(DateTimeFormatter.ofPattern(formatterPattern)); } public boolean isInvoiceAvailable() { return invoiceAvailable; } public void setInvoiceAvailable(boolean available) { this.invoiceAvailable = available; } }