diff --git a/models/price.go b/models/price.go index 83d0266..d030034 100644 --- a/models/price.go +++ b/models/price.go @@ -2,6 +2,7 @@ package models import ( "fmt" + "log" "strconv" "strings" ) @@ -20,14 +21,24 @@ func (in Price) String() string { } func ParsePrice(in string) (Price, error) { + log.Println(in) + in = strings.ReplaceAll(in, ",", ".") + log.Println(in) if !strings.Contains(in, ".") { price, err := strconv.Atoi(in) return Price(price) * 100, err } before, after, _ := strings.Cut(in, ".") - if len(after) != 2 { - return 0, fmt.Errorf("Wrong format, requires exactly 2 decimal places after '.'") + if len(after) > 2 { + return 0, fmt.Errorf("Wrong format, requires no more than 2 decimal places after the separator") } price, err := strconv.Atoi(before + after) + if len(after) < 2 { + price *= 10 + } + if len(after) < 1 { + price *= 10 + } + log.Println(price) return Price(price), err } diff --git a/models/price_test.go b/models/price_test.go index efe5557..92258f1 100644 --- a/models/price_test.go +++ b/models/price_test.go @@ -31,6 +31,11 @@ func TestParsePrice(t *testing.T) { if err != nil || p != 199 { t.Fatal(s, p) } + s = "1,99" + p, err = ParsePrice(s) + if err != nil || p != 199 { + t.Fatal(s, p) + } s = "1123" p, err = ParsePrice(s) if err != nil || p != 112300 { @@ -41,15 +46,4 @@ func TestParsePrice(t *testing.T) { if err == nil { t.Fatal(s, "should error") } - s = "1123.3" - _, err = ParsePrice(s) - if err == nil { - t.Fatal(s, "should error") - } - - s = "1123,33" - _, err = ParsePrice(s) - if err == nil { - t.Fatal(s, "should error") - } } diff --git a/server/static/order.js b/server/static/order.js index ca03351..48df069 100644 --- a/server/static/order.js +++ b/server/static/order.js @@ -3,71 +3,85 @@ function changeCake(id, name, price, delta) { if (input != null) { var qty = Number(input.value) + delta; if (qty <= 0) { - var item = input.closest('li'); + var item = input.closest("li"); if (item) item.remove(); updateTotals(); updateCatalogueQty(id, 0); return; } input.value = qty; - var item = input.closest('li'); - item.querySelector('.qty').innerText = qty; - item.querySelector('.price').innerText = (price * qty) + ' PLN'; + var item = input.closest("li"); + item.querySelector(".qty").innerText = qty; + item.querySelector(".price").innerText = price * qty + " PLN"; updateTotals(); updateCatalogueQty(id, qty); return; } if (delta <= 0) return; - var item = document.createElement('li'); - item.setAttribute('data-cake-id', id); + var item = document.createElement("li"); + item.setAttribute("data-cake-id", id); item.innerHTML = '
' + - '
' + name + '
' + - '
' + price + ' PLN × 1
' + - '
' + - '
' + price + ' PLN
' + + '
' + + name + + "
" + + '
' + + price + + ' PLN × 1
' + + "" + + '
' + + price + + " PLN
" + '' + - ''; - document.getElementById('basket-items').appendChild(item); + 'close' + + "" + + ''; + document.getElementById("basket-items").appendChild(item); updateTotals(); updateCatalogueQty(id, 1); } function removeCake(btn) { - var item = btn.closest('li'); - var id = item.getAttribute('data-cake-id'); + var item = btn.closest("li"); + var id = item.getAttribute("data-cake-id"); item.remove(); updateTotals(); updateCatalogueQty(id, 0); } +var subtotal = 0; + +function updatePrepaid() { + var paid = Number(document.querySelector('[name="paid"]')?.value || 0); + document.getElementById("paid-amount").innerText = paid + " PLN"; + document.getElementById("total-price").innerText = subtotal - paid + " PLN"; +} + function updateTotals() { - var subtotal = 0; - var items = document.querySelectorAll('#basket-items li'); - items.forEach(function(item) { - var priceText = item.querySelector('.detail').innerText; - var price = Number(priceText.split(' ')[0]); - var qty = Number(item.querySelector('.qty').innerText); + subtotal = 0; + var items = document.querySelectorAll("#basket-items li"); + items.forEach(function (item) { + var priceText = item.querySelector(".detail").innerText; + var price = Number(priceText.split(" ")[0]); + var qty = Number(item.querySelector(".qty").innerText); subtotal += price * qty; }); - var paid = Number(document.querySelector('[name="paid"]')?.value || 0); - document.getElementById('subtotal-price').innerText = subtotal + ' PLN'; - document.getElementById('paid-amount').innerText = paid + ' PLN'; - document.getElementById('total-price').innerText = (subtotal - paid) + ' PLN'; + document.getElementById("subtotal-price").innerText = subtotal + " PLN"; + updatePrepaid(); } function updateCatalogueQty(id, qty) { - var el = document.getElementById('qty-' + id); + var el = document.getElementById("qty-" + id); if (el) el.innerText = qty; } function filterCatalogue(input) { var query = input.value.toLowerCase(); - var items = document.querySelectorAll('#catalogue-grid > li'); - items.forEach(function(item) { - var name = item.getAttribute('data-name').toLowerCase(); - item.style.display = name.includes(query) ? '' : 'none'; + var items = document.querySelectorAll("#catalogue-grid > li"); + items.forEach(function (item) { + var name = item.getAttribute("data-name").toLowerCase(); + item.style.display = name.includes(query) ? "" : "none"; }); } diff --git a/server/templates/order.templ b/server/templates/order.templ index a6ea0a0..8b4d087 100644 --- a/server/templates/order.templ +++ b/server/templates/order.templ @@ -58,7 +58,7 @@ templ OrderForm(order models.Order, catalogue []models.Cake) {
- +
@@ -92,11 +92,11 @@ templ OrderForm(order models.Order, catalogue []models.Cake) {
- 0 -