Implement inventory prototype

This commit is contained in:
Simon Bruder 2023-11-05 16:11:36 +01:00
parent 3bfab465af
commit b37d26d0f1
Signed by: simon
GPG key ID: 8D3C82F9F309F8EC
5 changed files with 316 additions and 0 deletions

View file

@ -0,0 +1,107 @@
/*
* 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 org.salespointframework.catalog.Product;
import org.salespointframework.inventory.UniqueInventory;
import org.salespointframework.inventory.UniqueInventoryItem;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import catering.catalog.CatalogDummy;
import catering.catalog.CateringCatalog;
import jakarta.validation.Valid;
@Controller
class InventoryController {
private final UniqueInventory<UniqueInventoryItem> inventory;
private final CateringCatalog cateringCatalog;
InventoryController(UniqueInventory<UniqueInventoryItem> inventory, CateringCatalog cateringCatalog) {
Assert.notNull(inventory, "Inventory must not be null!");
Assert.notNull(inventory, "CateringCatalog must not be null!");
this.inventory = inventory;
this.cateringCatalog = cateringCatalog;
}
@GetMapping("/inventory")
String list(Model model) {
model.addAttribute("inventory", inventory.findAll());
return "inventory";
}
@GetMapping("/inventory/edit/{pid}")
String edit(Model model, @PathVariable Product pid) {
model.addAttribute("product", pid);
model.addAttribute("item", inventory.findByProduct(pid).get());
return "inventory-mutate";
}
@PostMapping("/inventory/edit/{pid}")
String edit(@Valid InventoryMutateForm form, Errors result, @PathVariable Product pid) {
if (result.hasErrors()) {
return "redirect:/inventory/edit/" + pid.getId();
}
CatalogDummy product = (CatalogDummy) pid;
UniqueInventoryItem item = inventory.findByProduct(pid).get();
product.setName(form.getName());
product.setType(form.getType());
product.setPrice(form.getRetailPrice());
product.setWholesalePrice(form.getWholesalePrice());
product.setPromotionPrice(form.getPromotionPrice().orElse(null));
product = cateringCatalog.save(product);
// no setQuantity in enterprise java
// (though returing a modified object is actually nice)
inventory.save(item.increaseQuantity(form.getQuantity().subtract(item.getQuantity())));
return "redirect:/inventory";
}
@GetMapping("/inventory/add")
String add() {
return "inventory-mutate";
}
@PostMapping("/inventory/add")
String add(@Valid InventoryMutateForm form, Errors result) {
if (result.hasErrors()) {
return "inventory-mutate";
}
inventory.save(new UniqueInventoryItem(
cateringCatalog.save(new CatalogDummy(form.getName(), form.getType(), form.getRetailPrice(),
form.getWholesalePrice(), form.getPromotionPrice().orElse(null))),
form.getQuantity()));
return "redirect:/inventory";
}
@GetMapping("/inventory/delete/{pid}")
String delete(@PathVariable Product pid) {
UniqueInventoryItem item = inventory.findByProduct(pid).get();
inventory.delete(item);
cateringCatalog.delete((CatalogDummy) pid);
return "redirect:/inventory";
}
}

View file

@ -0,0 +1,49 @@
/*
* 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 org.salespointframework.core.DataInitializer;
import org.salespointframework.inventory.UniqueInventory;
import org.salespointframework.inventory.UniqueInventoryItem;
import org.salespointframework.quantity.Quantity;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import catering.catalog.CateringCatalog;
@Component
class InventoryInitializer implements DataInitializer {
private final UniqueInventory<UniqueInventoryItem> inventory;
private final CateringCatalog cateringCatalog;
InventoryInitializer(UniqueInventory<UniqueInventoryItem> inventory, CateringCatalog cateringCatalog) {
Assert.notNull(inventory, "Inventory must not be null!");
Assert.notNull(cateringCatalog, "CateringCatalog must not be null!");
this.inventory = inventory;
this.cateringCatalog = cateringCatalog;
}
@Override
public void initialize() {
cateringCatalog.findAll().forEach(product -> {
if (inventory.findByProduct(product).isEmpty()) {
inventory.save(new UniqueInventoryItem(product, Quantity.of(10)));
}
});
}
}

View file

@ -0,0 +1,74 @@
/*
* 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 org.javamoney.moneta.Money;
import org.salespointframework.quantity.Quantity;
import catering.catalog.CatalogDummyType;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.PositiveOrZero; // NonNegative in enterprise java
class InventoryMutateForm {
private final @NotNull CatalogDummyType type;
private final @NotEmpty String name;
private final @NotNull Quantity quantity;
private final @NotNull MonetaryAmount wholesalePrice, retailPrice;
private final @NotNull Optional<MonetaryAmount> promotionPrice;
public InventoryMutateForm(@NotNull CatalogDummyType type, @NotEmpty String name,
@NotNull Quantity quantity, @PositiveOrZero double wholesalePrice, @PositiveOrZero double retailPrice,
@PositiveOrZero Optional<Double> promotionPrice) {
this.type = type;
this.name = name;
this.quantity = quantity;
this.wholesalePrice = Money.of(wholesalePrice, EURO);
this.retailPrice = Money.of(retailPrice, EURO);
this.promotionPrice = promotionPrice.map(price -> (MonetaryAmount) Money.of(price, EURO));
}
public CatalogDummyType getType() {
return type;
}
public String getName() {
return name;
}
public Quantity getQuantity() {
return quantity;
}
public MonetaryAmount getWholesalePrice() {
return wholesalePrice;
}
public MonetaryAmount getRetailPrice() {
return retailPrice;
}
public Optional<MonetaryAmount> getPromotionPrice() {
return promotionPrice;
}
}

View file

@ -0,0 +1,42 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security"
<head>
</head>
<body>
<h1>Lagerverwaltung</h1>
<h2 th:text="${'Produkt ' + (product == null ? 'anlegen' : 'bearbeiten')}"></h2>
<!-- TODO: maybe migrate to th:field (which is a pain) -->
<form method="post">
<div>
<label for="type">Typ</label>
<input type="radio" name="type" th:each="type : ${T(catering.catalog.CatalogDummyType).values()}" th:value="${type}" th:text="${type}" th:checked="${type.name() == product?.type?.name()}" required/>
</div>
<div>
<label for="name">Produktname</label>
<input type="text" name="name" th:value="${product?.name}" required/>
</div>
<div>
<label for="quantity">Menge im Bestand</label>
<input type="number" name="quantity" th:value="${item?.quantity}" required/>
</div>
<div>
<label for="wholesalePrice">Einkaufspreis</label>
<input type="number" name="wholesalePrice" step="0.01" min="0" th:value="${#numbers.formatDecimal(product?.wholesalePrice?.getNumber()?.doubleValueExact(), 1, 2)}" required/>
</div>
<div>
<label for="retailPrice">UVP</label>
<input type="number" name="retailPrice" step="0.01" min="0" th:value="${#numbers.formatDecimal(product?.price?.getNumber()?.doubleValueExact(), 1, 2)}" required/>
</div>
<div>
<!-- FIXME darf nur bei angeboten als teil von partyservice angezeigt werden -->
<label for="promotionPrice">Aktionspreis</label>
<input type="number" name="promotionPrice" step="0.01" min="0" th:value="${#numbers.formatDecimal(product?.promotionPrice?.getNumber()?.doubleValueExact(), 1, 2)}"/>
</div>
<div>
<button type="submit" th:text="${product == null ? 'Hinzufügen' : 'Bearbeiten'}"></button>
</div>
<!-- KANN: Bild und Beschreibungstext -->
</form>
</body>
</html>

View file

@ -0,0 +1,44 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security"
<head>
</head>
<body>
<h1>Lagerverwaltung</h1>
<table border>
<thead>
<!-- TODO: i18n? -->
<tr>
<th>ID</th> <!-- FIXME UUIDs are long -->
<th>Produktname</th>
<th>Menge</th>
<th>Einkaufspreis</th>
<th>UVP</th>
<th>Gesamtwert</th>
<th></th> <!-- FIXME: this should be replaced by something more reasonable once CSS comes into play -->
</tr>
</thead>
<tbody>
<tr th:each="item : ${inventory}">
<td th:text="${item.id}"></td>
<td th:text="${item.product.name}"></td>
<td th:text="${item.quantity}"></td>
<td th:text="${item.product.wholesalePrice}"></td>
<td th:if="${item.product.promotionPrice != null}"><del th:text="${item.product.price}"></del> <span th:text="${item.product.promotionPrice}"></span></td>
<td th:if="${item.product.promotionPrice == null}" th:text="${item.product.price}"></td>
<td th:text="${item.product.wholesalePrice.multiply(item.quantity.getAmount())}"></td>
<td><a th:href="@{/inventory/edit/{id}(id=${item.product.id})}"><button></button></a><a th:href="@{/inventory/delete/{id}(id=${item.product.id})}"><button>X</button></a></td>
</tr>
<tr>
<td><a href="/inventory/add"><button></button></a></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td><!-- TODO (optional): add total monetary value of inventory -->
<td></td>
</tr>
</tbody>
</table>
</body>
</html>