price fixes
This commit is contained in:
parent
2940f00142
commit
8d81940faf
4 changed files with 66 additions and 47 deletions
|
|
@ -2,6 +2,7 @@ package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
@ -20,14 +21,24 @@ func (in Price) String() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParsePrice(in string) (Price, error) {
|
func ParsePrice(in string) (Price, error) {
|
||||||
|
log.Println(in)
|
||||||
|
in = strings.ReplaceAll(in, ",", ".")
|
||||||
|
log.Println(in)
|
||||||
if !strings.Contains(in, ".") {
|
if !strings.Contains(in, ".") {
|
||||||
price, err := strconv.Atoi(in)
|
price, err := strconv.Atoi(in)
|
||||||
return Price(price) * 100, err
|
return Price(price) * 100, err
|
||||||
}
|
}
|
||||||
before, after, _ := strings.Cut(in, ".")
|
before, after, _ := strings.Cut(in, ".")
|
||||||
if len(after) != 2 {
|
if len(after) > 2 {
|
||||||
return 0, fmt.Errorf("Wrong format, requires exactly 2 decimal places after '.'")
|
return 0, fmt.Errorf("Wrong format, requires no more than 2 decimal places after the separator")
|
||||||
}
|
}
|
||||||
price, err := strconv.Atoi(before + after)
|
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
|
return Price(price), err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,11 @@ func TestParsePrice(t *testing.T) {
|
||||||
if err != nil || p != 199 {
|
if err != nil || p != 199 {
|
||||||
t.Fatal(s, p)
|
t.Fatal(s, p)
|
||||||
}
|
}
|
||||||
|
s = "1,99"
|
||||||
|
p, err = ParsePrice(s)
|
||||||
|
if err != nil || p != 199 {
|
||||||
|
t.Fatal(s, p)
|
||||||
|
}
|
||||||
s = "1123"
|
s = "1123"
|
||||||
p, err = ParsePrice(s)
|
p, err = ParsePrice(s)
|
||||||
if err != nil || p != 112300 {
|
if err != nil || p != 112300 {
|
||||||
|
|
@ -41,15 +46,4 @@ func TestParsePrice(t *testing.T) {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatal(s, "should error")
|
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")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,71 +3,85 @@ function changeCake(id, name, price, delta) {
|
||||||
if (input != null) {
|
if (input != null) {
|
||||||
var qty = Number(input.value) + delta;
|
var qty = Number(input.value) + delta;
|
||||||
if (qty <= 0) {
|
if (qty <= 0) {
|
||||||
var item = input.closest('li');
|
var item = input.closest("li");
|
||||||
if (item) item.remove();
|
if (item) item.remove();
|
||||||
updateTotals();
|
updateTotals();
|
||||||
updateCatalogueQty(id, 0);
|
updateCatalogueQty(id, 0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
input.value = qty;
|
input.value = qty;
|
||||||
var item = input.closest('li');
|
var item = input.closest("li");
|
||||||
item.querySelector('.qty').innerText = qty;
|
item.querySelector(".qty").innerText = qty;
|
||||||
item.querySelector('.price').innerText = (price * qty) + ' PLN';
|
item.querySelector(".price").innerText = price * qty + " PLN";
|
||||||
updateTotals();
|
updateTotals();
|
||||||
updateCatalogueQty(id, qty);
|
updateCatalogueQty(id, qty);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (delta <= 0) return;
|
if (delta <= 0) return;
|
||||||
var item = document.createElement('li');
|
var item = document.createElement("li");
|
||||||
item.setAttribute('data-cake-id', id);
|
item.setAttribute("data-cake-id", id);
|
||||||
item.innerHTML =
|
item.innerHTML =
|
||||||
'<div style="flex:1;min-width:0;padding-right:0.5rem">' +
|
'<div style="flex:1;min-width:0;padding-right:0.5rem">' +
|
||||||
'<div class="name">' + name + '</div>' +
|
'<div class="name">' +
|
||||||
'<div class="detail">' + price + ' PLN × <span class="qty">1</span></div>' +
|
name +
|
||||||
'</div>' +
|
"</div>" +
|
||||||
'<div class="price">' + price + ' PLN</div>' +
|
'<div class="detail">' +
|
||||||
|
price +
|
||||||
|
' PLN × <span class="qty">1</span></div>' +
|
||||||
|
"</div>" +
|
||||||
|
'<div class="price">' +
|
||||||
|
price +
|
||||||
|
" PLN</div>" +
|
||||||
'<button type="button" class="remove" onclick="removeCake(this)">' +
|
'<button type="button" class="remove" onclick="removeCake(this)">' +
|
||||||
'<span class="material-symbols-outlined">close</span>' +
|
'<span class="material-symbols-outlined">close</span>' +
|
||||||
'</button>' +
|
"</button>" +
|
||||||
'<input type="hidden" name="cake[' + id + ']" value="1">';
|
'<input type="hidden" name="cake[' +
|
||||||
document.getElementById('basket-items').appendChild(item);
|
id +
|
||||||
|
']" value="1">';
|
||||||
|
document.getElementById("basket-items").appendChild(item);
|
||||||
updateTotals();
|
updateTotals();
|
||||||
updateCatalogueQty(id, 1);
|
updateCatalogueQty(id, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
function removeCake(btn) {
|
function removeCake(btn) {
|
||||||
var item = btn.closest('li');
|
var item = btn.closest("li");
|
||||||
var id = item.getAttribute('data-cake-id');
|
var id = item.getAttribute("data-cake-id");
|
||||||
item.remove();
|
item.remove();
|
||||||
updateTotals();
|
updateTotals();
|
||||||
updateCatalogueQty(id, 0);
|
updateCatalogueQty(id, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateTotals() {
|
|
||||||
var subtotal = 0;
|
var subtotal = 0;
|
||||||
var items = document.querySelectorAll('#basket-items li');
|
|
||||||
|
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() {
|
||||||
|
subtotal = 0;
|
||||||
|
var items = document.querySelectorAll("#basket-items li");
|
||||||
items.forEach(function (item) {
|
items.forEach(function (item) {
|
||||||
var priceText = item.querySelector('.detail').innerText;
|
var priceText = item.querySelector(".detail").innerText;
|
||||||
var price = Number(priceText.split(' ')[0]);
|
var price = Number(priceText.split(" ")[0]);
|
||||||
var qty = Number(item.querySelector('.qty').innerText);
|
var qty = Number(item.querySelector(".qty").innerText);
|
||||||
subtotal += price * qty;
|
subtotal += price * qty;
|
||||||
});
|
});
|
||||||
var paid = Number(document.querySelector('[name="paid"]')?.value || 0);
|
document.getElementById("subtotal-price").innerText = subtotal + " PLN";
|
||||||
document.getElementById('subtotal-price').innerText = subtotal + ' PLN';
|
updatePrepaid();
|
||||||
document.getElementById('paid-amount').innerText = paid + ' PLN';
|
|
||||||
document.getElementById('total-price').innerText = (subtotal - paid) + ' PLN';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateCatalogueQty(id, qty) {
|
function updateCatalogueQty(id, qty) {
|
||||||
var el = document.getElementById('qty-' + id);
|
var el = document.getElementById("qty-" + id);
|
||||||
if (el) el.innerText = qty;
|
if (el) el.innerText = qty;
|
||||||
}
|
}
|
||||||
|
|
||||||
function filterCatalogue(input) {
|
function filterCatalogue(input) {
|
||||||
var query = input.value.toLowerCase();
|
var query = input.value.toLowerCase();
|
||||||
var items = document.querySelectorAll('#catalogue-grid > li');
|
var items = document.querySelectorAll("#catalogue-grid > li");
|
||||||
items.forEach(function (item) {
|
items.forEach(function (item) {
|
||||||
var name = item.getAttribute('data-name').toLowerCase();
|
var name = item.getAttribute("data-name").toLowerCase();
|
||||||
item.style.display = name.includes(query) ? '' : 'none';
|
item.style.display = name.includes(query) ? "" : "none";
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,7 @@ templ OrderForm(order models.Order, catalogue []models.Cake) {
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label>Zaliczka</label>
|
<label>Zaliczka</label>
|
||||||
<input class="form-input" type="number" name="paid" min="0" step="0.01" value={ order.Paid.String() }/>
|
<input class="form-input" type="number" name="paid" min="0" step="0.01" value={ order.Paid.String() } oninput={ templ.JSFuncCall("updatePrepaid") }/>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group" style="grid-column:span 2">
|
<div class="form-group" style="grid-column:span 2">
|
||||||
<label>Uwagi</label>
|
<label>Uwagi</label>
|
||||||
|
|
@ -92,11 +92,11 @@ templ OrderForm(order models.Order, catalogue []models.Cake) {
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="actions">
|
<div class="actions">
|
||||||
<button type="button" class="qty minus" onclick={ templ.JSFuncCall("changeCake", c.ID, c.Name, c.Price, -1) }>
|
<button type="button" class="qty minus" onclick={ templ.JSFuncCall("changeCake", c.ID, c.Name, c.Price.String(), -1) }>
|
||||||
<span class="material-symbols-outlined">remove</span>
|
<span class="material-symbols-outlined">remove</span>
|
||||||
</button>
|
</button>
|
||||||
<span class="value" id={ "qty-" + strconv.Itoa(c.ID) }>0</span>
|
<span class="value" id={ "qty-" + strconv.Itoa(c.ID) }>0</span>
|
||||||
<button type="button" class="qty plus" onclick={ templ.JSFuncCall("changeCake", c.ID, c.Name, c.Price, 1) }>
|
<button type="button" class="qty plus" onclick={ templ.JSFuncCall("changeCake", c.ID, c.Name, c.Price.String(), 1) }>
|
||||||
<span class="material-symbols-outlined">add</span>
|
<span class="material-symbols-outlined">add</span>
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue