Bind inventory mutate form to template

Co-authored-by: Paul Heimer <heimerp54@gmail.com>
This commit is contained in:
Simon Bruder 2023-11-21 23:01:33 +01:00
parent 695bc1d821
commit 6bd1c23e2d
Signed by: simon
GPG key ID: 8D3C82F9F309F8EC
2 changed files with 32 additions and 12 deletions

View file

@ -17,6 +17,12 @@
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.UniqueInventory;
@ -57,8 +63,22 @@ class InventoryController {
@PreAuthorize("hasRole('ADMIN')")
@GetMapping("/inventory/edit/{pid}")
String edit(Model model, @PathVariable Product pid) {
model.addAttribute("product", pid);
model.addAttribute("item", inventory.findByProduct(pid).get());
CatalogDummy product = (CatalogDummy) pid;
UniqueInventoryItem item = inventory.findByProduct(pid).get();
return edit(model, pid,
new InventoryMutateForm(product.getType(),
product.getName(),
item.getQuantity(),
product.getWholesalePrice().getNumber().doubleValueExact(),
product.getPrice().getNumber().doubleValueExact(),
Optional.ofNullable(product.getPromotionPrice())
.map(MonetaryAmount::getNumber)
.map(NumberValue::doubleValueExact)));
}
String edit(Model model, @PathVariable Product pid, InventoryMutateForm form) {
model.addAttribute("actionIsAdd", false);
model.addAttribute("form", form);
return "inventory-mutate";
}

View file

@ -5,37 +5,37 @@
layout:decorate="~{layout.html(title='Lagerverwaltung')}">
<body>
<div layout:fragment="content">
<h2 th:text="${'Produkt ' + (product == null ? 'anlegen' : 'bearbeiten')}"></h2>
<!-- TODO: maybe migrate to th:field (which is a pain) -->
<form method="post">
<h2 th:text="${'Produkt ' + (actionIsAdd ? 'anlegen' : 'bearbeiten')}"></h2>
<form method="post" th:object="${form}">
<div class="mb-3">
<label class="form-label" for="type">Typ</label>
<div class="form-check form-check-inline" th:each="type : ${T(catering.catalog.CatalogDummyType).values()}">
<input class="form-check-input" type="radio" name="type" th:value="${type}" th:text="${type}" th:checked="${type.name() == product?.type?.name()}" required/>
<input class="form-check-input" type="radio" th:field="*{type}" th:value="${type}" th:text="${type}" required/>
</div>
</div>
<div class="mb-3">
<label class="form-label" for="name">Produktname</label>
<input class="form-control" type="text" name="name" th:value="${product?.name}" required/>
<input class="form-control" type="text" th:field="*{name}" required/>
</div>
<div class="mb-3">
<label class="form-label" for="quantity">Menge im Bestand</label>
<input class="form-control" type="number" name="quantity" th:value="${item?.quantity}" required/>
<input class="form-control" type="number" th:field="*{quantity}" required/>
</div>
<!-- the prices arent bound with th:field as they need special formatting -->
<div class="mb-3">
<label class="form-label" for="wholesalePrice">Einkaufspreis</label>
<input class="form-control" type="number" name="wholesalePrice" step="0.01" min="0" th:value="${#numbers.formatDecimal(product?.wholesalePrice?.getNumber()?.doubleValueExact(), 1, 2)}" required/>
<input class="form-control" type="number" th:field="*{wholesalePrice}" step="0.01" min="0" required/>
</div>
<div class="mb-3">
<label class="form-label" for="retailPrice">UVP</label>
<input class="form-control" type="number" name="retailPrice" step="0.01" min="0" th:value="${#numbers.formatDecimal(product?.price?.getNumber()?.doubleValueExact(), 1, 2)}" required/>
<input class="form-control" type="number" th:field="*{retailPrice}" step="0.01" min="0" required/>
</div>
<div class="mb-3">
<!-- FIXME darf nur bei angeboten als teil von partyservice angezeigt werden -->
<label class="form-label" for="promotionPrice">Aktionspreis</label>
<input class="form-control" type="number" name="promotionPrice" step="0.01" min="0" th:value="${#numbers.formatDecimal(product?.promotionPrice?.getNumber()?.doubleValueExact(), 1, 2)}"/>
<input class="form-control" type="number" th:field="*{promotionPrice}" step="0.01" min="0"/>
</div>
<button class="btn btn-primary" type="submit" th:text="${product == null ? 'Hinzufügen' : 'Bearbeiten'}"></button>
<button class="btn btn-primary" type="submit" th:text="${actionIsAdd ? 'Hinzufügen' : 'Bearbeiten'}"></button>
<!-- KANN: Bild und Beschreibungstext -->
</form>
</div>