// SPDX-License-Identifier: AGPL-3.0-or-later // SPDX-FileCopyrightText: 2023 swt23w23 package catering.inventory; import static org.salespointframework.core.Currencies.EURO; import java.util.Optional; import javax.money.MonetaryAmount; import javax.money.NumberValue; import org.javamoney.moneta.Money; import org.salespointframework.catalog.Product; import org.salespointframework.inventory.UniqueInventoryItem; import catering.catalog.Consumable; import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.PositiveOrZero; // NonNegative in enterprise java class ConsumableMutateForm extends InventoryMutateForm { private @NotNull @PositiveOrZero Double wholesalePrice; private @NotNull Optional<@PositiveOrZero Double> promotionPrice = Optional.empty(); public Double getWholesalePrice() { return wholesalePrice; } public Optional getPromotionPrice() { return promotionPrice; } public void setWholesalePrice(Double wholesalePrice) { this.wholesalePrice = wholesalePrice; } public void setPromotionPrice(Optional promotionPrice) { this.promotionPrice = promotionPrice; } @Override public Product toProduct() { return new Consumable( getName(), Money.of(getRetailPrice(), EURO), Money.of(getWholesalePrice(), EURO), getPromotionPrice().map(price -> Money.of(price, EURO)), getOrderTypes()); } public static ConsumableMutateForm of(Consumable product, UniqueInventoryItem item) { ConsumableMutateForm form = new ConsumableMutateForm(); form.setName(product.getName()); form.setQuantity(item.getQuantity()); form.setOrderTypes(orderTypesFromCategories(product.getCategories())); form.setWholesalePrice(product.getWholesalePrice().getNumber().doubleValueExact()); form.setRetailPrice(product.getRetailPrice().getNumber().doubleValueExact()); form.setPromotionPrice( product.getPromotionPrice().map(MonetaryAmount::getNumber).map(NumberValue::doubleValueExact)); return form; } @Override protected void modifyProductPrimitive(Product product) { if (product instanceof Consumable consumable) { consumable.setWholesalePrice(Money.of(getWholesalePrice(), EURO)); consumable.setPromotionPrice(getPromotionPrice().map(price -> Money.of(price, EURO))); } else { throw new IllegalArgumentException("ConsumableMutateForm can only modify instances of Consumable"); } } }