trying to fix price
This commit is contained in:
parent
7feee7f8a3
commit
8194cea85e
10 changed files with 145 additions and 54 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
33
models/price.go
Normal file
33
models/price.go
Normal file
|
|
@ -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
|
||||
}
|
||||
55
models/price_test.go
Normal file
55
models/price_test.go
Normal file
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
10
seed.go
10
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()
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,14 +24,14 @@ templ CakeForm(cake models.Cake) {
|
|||
</h2>
|
||||
</div>
|
||||
<form method="post" class="card-body">
|
||||
<input type="hidden" name="id" value={ strconv.Itoa(cake.ID) } />
|
||||
<input type="hidden" name="id" value={ strconv.Itoa(cake.ID) }/>
|
||||
<div class="form-group">
|
||||
<label>Nazwa *</label>
|
||||
<input class="form-input" type="text" name="name" value={ cake.Name } placeholder="Nazwa ciasta" />
|
||||
<input class="form-input" type="text" name="name" value={ cake.Name } placeholder="Nazwa ciasta"/>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Cena</label>
|
||||
<input class="form-input" type="number" name="price" min="0" value={ strconv.Itoa(cake.Price) } />
|
||||
<input class="form-input" type="number" name="price" min="0" value={ cake.Price.String() }/>
|
||||
</div>
|
||||
<div class="form-actions">
|
||||
<a href="/cakes" class="btn btn-outline">Anuluj</a>
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ templ CakesMain(cakes []models.Cake) {
|
|||
</td>
|
||||
<td style="font-weight:500">{ c.Name }</td>
|
||||
<td class="text-on-surface-variant">#{ strconv.Itoa(c.ID) }</td>
|
||||
<td class="text-right">{ strconv.Itoa(c.Price) } PLN</td>
|
||||
<td class="text-right">{ c.Price.String() } PLN</td>
|
||||
<td class="text-center">
|
||||
<a href={ templ.URL("/cake/" + strconv.Itoa(c.ID)) } class="action-btn">
|
||||
<span class="material-symbols-outlined">edit</span>
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ templ OrderPage(order models.Order, catalogue []models.Cake) {
|
|||
|
||||
templ OrderForm(order models.Order, catalogue []models.Cake) {
|
||||
<form method="post" class="split-pane" style="flex:1;min-height:0">
|
||||
<input type="hidden" name="id" value={ strconv.Itoa(order.ID) } />
|
||||
<input type="hidden" name="id" value={ strconv.Itoa(order.ID) }/>
|
||||
<div class="split-pane-left">
|
||||
<section class="card">
|
||||
<div class="card-header">
|
||||
|
|
@ -25,34 +25,44 @@ templ OrderForm(order models.Order, catalogue []models.Cake) {
|
|||
<div class="form-grid">
|
||||
<div class="form-group">
|
||||
<label>Imię *</label>
|
||||
<input class="form-input" type="text" name="name" value={ order.Name } placeholder="Jan" />
|
||||
<input class="form-input" type="text" name="name" value={ order.Name } placeholder="Jan"/>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Nazwisko *</label>
|
||||
<input class="form-input" type="text" name="surname" value={ order.Surname } placeholder="Kowalski" />
|
||||
<input class="form-input" type="text" name="surname" value={ order.Surname } placeholder="Kowalski"/>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Telefon</label>
|
||||
<input class="form-input" type="tel" name="phone" value={ order.Phone } placeholder="+48 123 456 789" />
|
||||
<input class="form-input" type="tel" name="phone" value={ order.Phone } placeholder="+48 123 456 789"/>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Lokalizacja</label>
|
||||
<select class="form-input" name="location">
|
||||
<option if order.Location == "Kartuzy" { selected } value="Kartuzy">Kartuzy</option>
|
||||
<option if order.Location == "Somonino" { selected } value="Somonino">Somonino</option>
|
||||
<option
|
||||
if order.Location == "Kartuzy" {
|
||||
selected
|
||||
}
|
||||
value="Kartuzy"
|
||||
>Kartuzy</option>
|
||||
<option
|
||||
if order.Location == "Somonino" {
|
||||
selected
|
||||
}
|
||||
value="Somonino"
|
||||
>Somonino</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Data dostawy *</label>
|
||||
<input class="form-input" type="date" name="date" value={ order.Date.Format("2006-01-02") } />
|
||||
<input class="form-input" type="date" name="date" value={ order.Date.Format("2006-01-02") }/>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Zaliczka</label>
|
||||
<input class="form-input" type="number" name="paid" min="0" value={ strconv.Itoa(order.Paid) } />
|
||||
<input class="form-input" type="number" name="paid" min="0" step="0.01" value={ order.Paid.String() }/>
|
||||
</div>
|
||||
<div class="form-group" style="grid-column:span 2">
|
||||
<label>Uwagi</label>
|
||||
<input class="form-input" type="text" placeholder="Alergie, specjalne życzenia..." />
|
||||
<input class="form-input" type="text" placeholder="Alergie, specjalne życzenia..."/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
@ -65,7 +75,7 @@ templ OrderForm(order models.Order, catalogue []models.Cake) {
|
|||
</h2>
|
||||
<div class="topnav-search" style="width:180px">
|
||||
<span class="material-symbols-outlined" style="left:6px;font-size:14px">search</span>
|
||||
<input class="form-input" style="height:28px;padding-left:26px;font-size:12px" type="text" placeholder="Filtruj produkty..." oninput="filterCatalogue(this)" />
|
||||
<input class="form-input" style="height:28px;padding-left:26px;font-size:12px" type="text" placeholder="Filtruj produkty..." oninput="filterCatalogue(this)"/>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card-body" style="overflow-y:auto">
|
||||
|
|
@ -78,7 +88,7 @@ templ OrderForm(order models.Order, catalogue []models.Cake) {
|
|||
</div>
|
||||
<div style="min-width:0">
|
||||
<div class="catalogue-item-name">{ c.Name }</div>
|
||||
<div class="catalogue-item-price">{ strconv.Itoa(c.Price) } PLN</div>
|
||||
<div class="catalogue-item-price">{ c.Price.String() } PLN</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="catalogue-item-actions">
|
||||
|
|
@ -109,28 +119,28 @@ templ OrderForm(order models.Order, catalogue []models.Cake) {
|
|||
<div class="summary-item" data-cake-id={ strconv.Itoa(c.ID) }>
|
||||
<div class="summary-item-info">
|
||||
<div class="summary-item-name cake-name">{ c.Name }</div>
|
||||
<div class="summary-item-detail cake-price">{ strconv.Itoa(c.Price) } PLN × <span class="cake-qty">{ strconv.Itoa(c.Amount) }</span></div>
|
||||
<div class="summary-item-detail cake-price">{ c.Price.String() } PLN × <span class="cake-qty">{ strconv.Itoa(c.Amount) }</span></div>
|
||||
</div>
|
||||
<div class="summary-item-price cake-total">{ strconv.Itoa(c.Total()) } PLN</div>
|
||||
<div class="summary-item-price cake-total">{ c.Total().String() } PLN</div>
|
||||
<button type="button" class="summary-item-remove" onclick="removeCake(this)">
|
||||
<span class="material-symbols-outlined">close</span>
|
||||
</button>
|
||||
<input type="hidden" name={ "cake[" + strconv.Itoa(c.ID) + "]" } value={ strconv.Itoa(c.Amount) } />
|
||||
<input type="hidden" name={ "cake[" + strconv.Itoa(c.ID) + "]" } value={ strconv.Itoa(c.Amount) }/>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
<div class="summary-totals">
|
||||
<div class="summary-total-row">
|
||||
<span>Subtotal</span>
|
||||
<span id="subtotal-price">{ strconv.Itoa(orderSubtotal(order)) } PLN</span>
|
||||
<span id="subtotal-price">{ order.Subtotal().String() } PLN</span>
|
||||
</div>
|
||||
<div class="summary-total-row">
|
||||
<span>Zaliczka</span>
|
||||
<span id="paid-amount">{ strconv.Itoa(order.Paid) } PLN</span>
|
||||
<span id="paid-amount">{ order.Paid.String() } PLN</span>
|
||||
</div>
|
||||
<div class="summary-total-row total">
|
||||
<span>Do zapłaty</span>
|
||||
<span id="total-price">{ strconv.Itoa(orderSubtotal(order) - order.Paid) } PLN</span>
|
||||
<span id="total-price">{ order.Total().String() } PLN</span>
|
||||
</div>
|
||||
<button type="submit" class="btn-finalize">
|
||||
<span class="material-symbols-outlined">check_circle</span>
|
||||
|
|
|
|||
|
|
@ -47,14 +47,13 @@ templ OrdersMain(first, last string, orders []models.Order) {
|
|||
<span class="material-symbols-outlined">payments</span>
|
||||
Przychód (szac.)
|
||||
</span>
|
||||
<span class="metric-value">{ strconv.Itoa(estimateRevenue(orders)) } PLN</span>
|
||||
<span class="metric-value">{ estimateRevenue(orders).String() } PLN</span>
|
||||
<span class="metric-change positive">
|
||||
<span class="material-symbols-outlined">trending_up</span>
|
||||
W bieżącym miesiącu
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<section class="card">
|
||||
<div class="card-header">
|
||||
<h2>
|
||||
|
|
@ -65,11 +64,11 @@ templ OrdersMain(first, last string, orders []models.Order) {
|
|||
<form hx-get="/orders/search" hx-target="#results-table" hx-trigger="keyup delay:500ms, change" style="display:flex;align-items:center;gap:8px">
|
||||
<div class="form-group" style="flex-direction:row;align-items:center;gap:4px">
|
||||
<label style="font-size:11px;white-space:nowrap">Od</label>
|
||||
<input class="form-input" style="width:140px" type="date" value={ first } name="from">
|
||||
<input class="form-input" style="width:140px" type="date" value={ first } name="from"/>
|
||||
</div>
|
||||
<div class="form-group" style="flex-direction:row;align-items:center;gap:4px">
|
||||
<label style="font-size:11px;white-space:nowrap">Do</label>
|
||||
<input class="form-input" style="width:140px" type="date" value={ last } name="to">
|
||||
<input class="form-input" style="width:140px" type="date" value={ last } name="to"/>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
|
@ -97,7 +96,7 @@ templ OrdersTable(orders []models.Order) {
|
|||
for _, o := range orders {
|
||||
<tr>
|
||||
<td><a href={ templ.URL("/order/" + strconv.Itoa(o.ID)) } class="order-id">#{ strconv.Itoa(o.ID) }</a></td>
|
||||
<td class="customer-name">{ o.Surname } { o.Name }<br /><span class="text-on-surface-variant" style="font-size:11px;font-weight:400">{ o.Phone }</span></td>
|
||||
<td class="customer-name">{ o.Surname } { o.Name }<br/><span class="text-on-surface-variant" style="font-size:11px;font-weight:400">{ o.Phone }</span></td>
|
||||
<td class="text-on-surface-variant">{ o.Date.Format("2006-01-02 15:04") }</td>
|
||||
<td>
|
||||
<span class="truncate" title={ cakeItemsDesc(o.Cakes) }>
|
||||
|
|
@ -109,7 +108,7 @@ templ OrdersTable(orders []models.Order) {
|
|||
}
|
||||
</span>
|
||||
</td>
|
||||
<td class="text-right" style="font-weight:700">{ strconv.Itoa(o.Total()) } PLN</td>
|
||||
<td class="text-right" style="font-weight:700">{ o.Total().String() } PLN</td>
|
||||
<td class="text-center">
|
||||
<span class={ "status-chip " + o.Status }>
|
||||
if o.Status == "accepted" {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue