2023-11-07 20:53:31 +01:00
|
|
|
package catering.order;
|
|
|
|
|
2023-12-07 16:58:29 +01:00
|
|
|
import catering.catalog.Rentable;
|
2023-11-23 16:57:44 +01:00
|
|
|
import catering.staff.Employee;
|
2023-11-26 16:59:50 +01:00
|
|
|
import org.javamoney.moneta.Money;
|
2023-12-07 16:58:29 +01:00
|
|
|
import org.salespointframework.inventory.UniqueInventory;
|
|
|
|
import org.salespointframework.inventory.UniqueInventoryItem;
|
2023-11-19 13:00:21 +01:00
|
|
|
import org.salespointframework.order.Cart;
|
2023-12-07 16:58:29 +01:00
|
|
|
import org.salespointframework.order.CartItem;
|
|
|
|
import org.salespointframework.order.OrderLine;
|
2023-11-19 13:00:21 +01:00
|
|
|
|
2023-11-26 16:59:50 +01:00
|
|
|
import javax.money.MonetaryAmount;
|
2023-11-26 12:10:28 +01:00
|
|
|
import java.time.Duration;
|
2023-11-19 13:00:21 +01:00
|
|
|
import java.time.LocalDateTime;
|
2023-11-26 12:10:28 +01:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
2023-11-19 13:00:21 +01:00
|
|
|
|
|
|
|
public class CustomCart extends Cart {
|
2023-12-03 21:32:12 +01:00
|
|
|
private Set<Employee> staff;
|
2023-11-19 13:00:21 +01:00
|
|
|
private OrderType orderType;
|
|
|
|
private LocalDateTime start;
|
|
|
|
private LocalDateTime finish;
|
|
|
|
private final String formatterPattern;
|
|
|
|
|
|
|
|
// Constructor
|
|
|
|
public CustomCart(OrderType orderType, LocalDateTime start, LocalDateTime finish) {
|
|
|
|
super();
|
2023-11-26 12:10:28 +01:00
|
|
|
this.staff = new HashSet<>();
|
2023-11-19 13:00:21 +01:00
|
|
|
this.orderType = orderType;
|
|
|
|
this.start = start;
|
|
|
|
this.finish = finish;
|
2023-11-26 12:10:28 +01:00
|
|
|
this.formatterPattern = "dd.MM.yyyy, HH:mm 'Uhr'";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds an employee to the cart
|
|
|
|
*/
|
2023-11-23 16:57:44 +01:00
|
|
|
public boolean addEmployee(Employee employee) {
|
2023-12-03 21:32:12 +01:00
|
|
|
for (Employee myEmployee : staff) {
|
2023-11-23 16:57:44 +01:00
|
|
|
if (myEmployee.equals(employee)) {
|
2023-11-26 16:59:50 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2023-12-03 21:32:12 +01:00
|
|
|
return staff.add(employee);
|
2023-11-26 12:10:28 +01:00
|
|
|
}
|
|
|
|
|
2023-11-23 16:57:44 +01:00
|
|
|
public Set<Employee> getStaff() {
|
2023-11-26 12:10:28 +01:00
|
|
|
return staff;
|
|
|
|
}
|
|
|
|
|
2023-11-23 16:57:44 +01:00
|
|
|
public boolean removeEmployee(Employee employee) {
|
2023-12-03 21:32:12 +01:00
|
|
|
for (Employee myEmployee : staff) {
|
2023-11-23 16:57:44 +01:00
|
|
|
if (myEmployee.equals(employee)) {
|
2023-12-03 21:32:12 +01:00
|
|
|
return staff.remove(myEmployee);
|
2023-11-26 16:59:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2023-11-26 12:10:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add staff to order (analogous to cart.addItemsTo(order))
|
|
|
|
*/
|
|
|
|
public CustomOrder addStaffTo(CustomOrder order) {
|
2023-12-03 21:32:12 +01:00
|
|
|
for (Employee employee : staff) {
|
2023-11-23 16:57:44 +01:00
|
|
|
order.addEmployee(employee);
|
2023-11-26 12:10:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return order;
|
2023-11-07 20:53:31 +01:00
|
|
|
}
|
|
|
|
|
2023-12-07 16:58:29 +01:00
|
|
|
/**
|
|
|
|
* Add ChargeLines for rental costs of each Rentable to {@param order}
|
|
|
|
*/
|
|
|
|
public CustomOrder addRentablesToOrder(CustomOrder order, UniqueInventory<UniqueInventoryItem> inventory) {
|
|
|
|
for (CartItem item : this) {
|
|
|
|
if (item.getProduct() instanceof Rentable rentable) {
|
|
|
|
OrderLine orderLine = order.getOrderLines(rentable).stream().findFirst().get();
|
|
|
|
|
|
|
|
order.addChargeLine(
|
|
|
|
rentable.getPrice()
|
|
|
|
.multiply(getDurationInHours())
|
|
|
|
.multiply(item.getQuantity().getAmount()),
|
|
|
|
"rental costs",
|
|
|
|
orderLine
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// neutralises automatic reduction of rentables in inventory
|
|
|
|
inventory.save(inventory.findByProduct(rentable).get().increaseQuantity(orderLine.getQuantity()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return order;
|
|
|
|
}
|
|
|
|
|
2023-11-19 13:00:21 +01:00
|
|
|
public OrderType getOrderType() {
|
2023-11-07 20:53:31 +01:00
|
|
|
return orderType;
|
|
|
|
}
|
|
|
|
|
2023-11-19 13:00:21 +01:00
|
|
|
public void setOrderType(OrderType orderType) {
|
|
|
|
this.orderType = orderType;
|
|
|
|
}
|
|
|
|
|
|
|
|
public LocalDateTime getStart() {
|
|
|
|
return start;
|
2023-11-07 20:53:31 +01:00
|
|
|
}
|
|
|
|
|
2023-11-19 13:00:21 +01:00
|
|
|
public void setStart(LocalDateTime start) {
|
|
|
|
this.start = start;
|
2023-11-07 20:53:31 +01:00
|
|
|
}
|
|
|
|
|
2023-11-26 12:10:28 +01:00
|
|
|
/**
|
2023-12-07 16:58:29 +01:00
|
|
|
* @return hours between start and finish
|
2023-11-26 12:10:28 +01:00
|
|
|
*/
|
2023-12-07 16:58:29 +01:00
|
|
|
public int getDurationInHours() {
|
|
|
|
return (int) (Duration.between(start, finish).getSeconds() / 3600);
|
2023-11-26 12:10:28 +01:00
|
|
|
}
|
|
|
|
|
2023-11-19 13:00:21 +01:00
|
|
|
public LocalDateTime getFinish() {
|
|
|
|
return finish;
|
2023-11-07 20:53:31 +01:00
|
|
|
}
|
|
|
|
|
2023-11-19 13:00:21 +01:00
|
|
|
public void setFinish(LocalDateTime finish) {
|
|
|
|
this.finish = finish;
|
2023-11-07 20:53:31 +01:00
|
|
|
}
|
2023-11-07 22:23:48 +01:00
|
|
|
|
2023-11-19 13:00:21 +01:00
|
|
|
public String getFormatterPattern() {
|
|
|
|
return formatterPattern;
|
2023-11-07 22:23:48 +01:00
|
|
|
}
|
2023-11-26 16:59:50 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public MonetaryAmount getPrice() {
|
2023-12-07 16:58:29 +01:00
|
|
|
MonetaryAmount total = Money.of(0, "EUR");
|
|
|
|
|
|
|
|
for (CartItem item : this) {
|
|
|
|
if (item.getProduct() instanceof Rentable rentable) {
|
|
|
|
// final_price = (price + price * time) * quantity
|
|
|
|
total = total.add(rentable.getPriceForTime(getDurationInHours()).multiply(item.getQuantity().getAmount()));
|
|
|
|
} else {
|
|
|
|
total = total.add(item.getProduct().getPrice().multiply(item.getQuantity().getAmount()));
|
|
|
|
}
|
2023-11-26 16:59:50 +01:00
|
|
|
}
|
|
|
|
|
2023-12-03 21:32:12 +01:00
|
|
|
for (Employee employee : staff) {
|
2023-12-07 16:58:29 +01:00
|
|
|
total = total.add(Money.of(getDurationInHours() * employee.getWage(), "EUR"));
|
2023-12-03 21:32:12 +01:00
|
|
|
}
|
|
|
|
|
2023-11-26 16:59:50 +01:00
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void clear() {
|
|
|
|
super.clear();
|
2023-12-03 21:32:12 +01:00
|
|
|
staff = new HashSet<>();
|
2023-11-26 16:59:50 +01:00
|
|
|
}
|
2023-11-07 20:53:31 +01:00
|
|
|
}
|