From 8194cea85ef7b763443f3ff9b392cb7f16183626 Mon Sep 17 00:00:00 2001 From: bronkuu Date: Mon, 15 Jun 2026 13:34:18 +0200 Subject: [PATCH] trying to fix price --- models/data.go | 18 +++++++----- models/price.go | 33 +++++++++++++++++++++ models/price_test.go | 55 +++++++++++++++++++++++++++++++++++ seed.go | 10 +++---- server/post.go | 4 +-- server/templates/cake.templ | 6 ++-- server/templates/cakes.templ | 2 +- server/templates/helpers.go | 16 ++-------- server/templates/order.templ | 44 +++++++++++++++++----------- server/templates/orders.templ | 11 ++++--- 10 files changed, 145 insertions(+), 54 deletions(-) create mode 100644 models/price.go create mode 100644 models/price_test.go diff --git a/models/data.go b/models/data.go index 9fadf02..270d79c 100644 --- a/models/data.go +++ b/models/data.go @@ -5,7 +5,7 @@ import "time" type Cake struct { Name string ID int - Price int // increments of 0.01 + Price Price // increments of 0.01 Amount int } @@ -18,18 +18,22 @@ type Order struct { Date time.Time Accepted time.Time Status string - Paid int // increments of 0.01 + Paid Price // increments of 0.01 Cakes []Cake } -func (o *Order) Total() int { - out := o.Paid * (-1) +func (o *Order) Subtotal() Price { + var out Price for _, e := range o.Cakes { - out += e.Price * e.Amount + out += e.Price * Price(e.Amount) } return out + +} +func (o *Order) Total() Price { + return o.Subtotal() - o.Paid } -func (c *Cake) Total() int { - return c.Amount * c.Price +func (c *Cake) Total() Price { + return Price(c.Amount) * c.Price } diff --git a/models/price.go b/models/price.go new file mode 100644 index 0000000..83d0266 --- /dev/null +++ b/models/price.go @@ -0,0 +1,33 @@ +package models + +import ( + "fmt" + "strconv" + "strings" +) + +type Price int + +func (in Price) String() string { + if in%100 == 0 { + return strconv.Itoa(int(in) / 100) + } else { + price := strconv.Itoa(int(in)) + i := len(price) - 2 + price = price[:i] + "." + price[i:] + return price + } +} + +func ParsePrice(in string) (Price, error) { + 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 '.'") + } + price, err := strconv.Atoi(before + after) + return Price(price), err +} diff --git a/models/price_test.go b/models/price_test.go new file mode 100644 index 0000000..efe5557 --- /dev/null +++ b/models/price_test.go @@ -0,0 +1,55 @@ +package models + +import "testing" + +func TestFormatPrice(t *testing.T) { + var a Price + var s string + a = 12345678900 + s = a.String() + if s != "123456789" { + t.Fatal(a, s) + } + a = 123456789 + s = a.String() + if s != "1234567.89" { + t.Fatal(a, s) + } +} + +func TestParsePrice(t *testing.T) { + var p Price + var s string + var err error + s = "199" + p, err = ParsePrice(s) + if err != nil || p != 19900 { + 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 { + t.Fatal(s, p) + } + s = "1123.123" + _, err = ParsePrice(s) + 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/seed.go b/seed.go index 7c07b23..dde69ec 100644 --- a/seed.go +++ b/seed.go @@ -9,19 +9,19 @@ import ( ) func seed(s *store.Store) { - malinowa := models.Cake{Name: "Malinowa Chmurka", Price: 2500} + malinowa := models.Cake{Name: "Malinowa Chmurka", Price: 25000} malinowa.ID, _ = s.SaveCake(malinowa) - sernik := models.Cake{Name: "Sernik", Price: 2200} + sernik := models.Cake{Name: "Sernik", Price: 22000} sernik.ID, _ = s.SaveCake(sernik) - mleczna := models.Cake{Name: "Mleczna Kanapka", Price: 2800} + mleczna := models.Cake{Name: "Mleczna Kanapka", Price: 28000} mleczna.ID, _ = s.SaveCake(mleczna) - trzy := models.Cake{Name: "3-Bit", Price: 1800} + trzy := models.Cake{Name: "3-Bit", Price: 18000} trzy.ID, _ = s.SaveCake(trzy) - plesniak := models.Cake{Name: "Pleśniak", Price: 2000} + plesniak := models.Cake{Name: "Pleśniak", Price: 20000} plesniak.ID, _ = s.SaveCake(plesniak) now := time.Now() diff --git a/server/post.go b/server/post.go index a5cd6a0..dde7f04 100644 --- a/server/post.go +++ b/server/post.go @@ -25,7 +25,7 @@ func (h *Server) postCake(r *http.Request) (templ.Component, int, error) { return nil, http.StatusBadRequest, err } n.Name = r.FormValue("name") - n.Price, err = strconv.Atoi(r.FormValue("price")) + n.Price, err = models.ParsePrice(r.FormValue("price")) if err != nil { return nil, http.StatusBadRequest, err } @@ -49,7 +49,7 @@ func (h *Server) postOrder(r *http.Request) (templ.Component, int, error) { if err != nil { return nil, http.StatusBadRequest, err } - n.Paid, err = strconv.Atoi(r.FormValue("paid")) + n.Paid, err = models.ParsePrice(r.FormValue("paid")) if err != nil { return nil, http.StatusBadRequest, err } diff --git a/server/templates/cake.templ b/server/templates/cake.templ index 83b6c46..362375a 100644 --- a/server/templates/cake.templ +++ b/server/templates/cake.templ @@ -24,14 +24,14 @@ templ CakeForm(cake models.Cake) {
- +
- +
- +
Anuluj diff --git a/server/templates/cakes.templ b/server/templates/cakes.templ index d931304..be498ef 100644 --- a/server/templates/cakes.templ +++ b/server/templates/cakes.templ @@ -43,7 +43,7 @@ templ CakesMain(cakes []models.Cake) { { c.Name } #{ strconv.Itoa(c.ID) } - { strconv.Itoa(c.Price) } PLN + { c.Price.String() } PLN edit diff --git a/server/templates/helpers.go b/server/templates/helpers.go index 74f6c87..dde8256 100644 --- a/server/templates/helpers.go +++ b/server/templates/helpers.go @@ -22,14 +22,6 @@ func orderTitle(order models.Order) string { return "Nowe zamówienie" } -func orderSubtotal(order models.Order) int { - sub := 0 - for _, c := range order.Cakes { - sub += c.Price * c.Amount - } - return sub -} - func countPending(orders []models.Order) int { n := 0 for _, o := range orders { @@ -50,12 +42,10 @@ func countDone(orders []models.Order) int { return n } -func estimateRevenue(orders []models.Order) int { - total := 0 +func estimateRevenue(orders []models.Order) models.Price { + var total models.Price for _, o := range orders { - for _, c := range o.Cakes { - total += c.Price * c.Amount - } + total += o.Total() } return total } diff --git a/server/templates/order.templ b/server/templates/order.templ index a12eb2e..ec8f126 100644 --- a/server/templates/order.templ +++ b/server/templates/order.templ @@ -12,7 +12,7 @@ templ OrderPage(order models.Order, catalogue []models.Cake) { templ OrderForm(order models.Order, catalogue []models.Cake) { - +
@@ -25,34 +25,44 @@ templ OrderForm(order models.Order, catalogue []models.Cake) {
- +
- +
- +
- +
- +
- +
@@ -65,7 +75,7 @@ templ OrderForm(order models.Order, catalogue []models.Cake) {
@@ -78,7 +88,7 @@ templ OrderForm(order models.Order, catalogue []models.Cake) {
{ c.Name }
-
{ strconv.Itoa(c.Price) } PLN
+
{ c.Price.String() } PLN
@@ -109,28 +119,28 @@ templ OrderForm(order models.Order, catalogue []models.Cake) {
{ c.Name }
-
{ strconv.Itoa(c.Price) } PLN × { strconv.Itoa(c.Amount) }
+
{ c.Price.String() } PLN × { strconv.Itoa(c.Amount) }
-
{ strconv.Itoa(c.Total()) } PLN
+
{ c.Total().String() } PLN
- +
}
Subtotal - { strconv.Itoa(orderSubtotal(order)) } PLN + { order.Subtotal().String() } PLN
Zaliczka - +
Do zapłaty - { strconv.Itoa(orderSubtotal(order) - order.Paid) } PLN + { order.Total().String() } PLN
-

@@ -65,11 +64,11 @@ templ OrdersMain(first, last string, orders []models.Order) {
- +
- +

@@ -97,7 +96,7 @@ templ OrdersTable(orders []models.Order) { for _, o := range orders {
#{ strconv.Itoa(o.ID) } - { o.Surname } { o.Name }
{ o.Phone } + { o.Surname } { o.Name }
{ o.Phone } { o.Date.Format("2006-01-02 15:04") } @@ -109,7 +108,7 @@ templ OrdersTable(orders []models.Order) { } - { strconv.Itoa(o.Total()) } PLN + { o.Total().String() } PLN if o.Status == "accepted" {