swt23w23/src/main/java/catering/inventory/ConsumableMutateForm.java

86 lines
3 KiB
Java

/*
* Copyright (C) 2023 Simon Bruder
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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<Double> getPromotionPrice() {
return promotionPrice;
}
public void setWholesalePrice(Double wholesalePrice) {
this.wholesalePrice = wholesalePrice;
}
public void setPromotionPrice(Optional<Double> 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");
}
}
}