cake-order-tracker/server/templates/helpers.go
2026-06-11 08:48:01 +02:00

84 lines
1.4 KiB
Go

package templates
import (
"fmt"
"strconv"
"strings"
"git.bronku.xyz/bronku/cake-order-tracker/models"
)
func cakeTitle(cake models.Cake) string {
if cake.ID != 0 {
return "Ciasto #" + strconv.Itoa(cake.ID)
}
return "Ciasto nowe"
}
func orderTitle(order models.Order) string {
if order.ID != 0 {
return "Edytuj zamówienie #" + strconv.Itoa(order.ID)
}
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 {
if o.Status != "done" {
n++
}
}
return n
}
func countDone(orders []models.Order) int {
n := 0
for _, o := range orders {
if o.Status == "done" {
n++
}
}
return n
}
func estimateRevenue(orders []models.Order) int {
total := 0
for _, o := range orders {
for _, c := range o.Cakes {
total += c.Price * c.Amount
}
}
return total
}
func cakeItemsDesc(cakes []models.Cake) string {
desc := ""
for i, c := range cakes {
if i > 0 {
desc += ", "
}
desc += c.Name + " (x" + strconv.Itoa(c.Amount) + ")"
}
return desc
}
func orderScript(order models.Order) string {
var b strings.Builder
b.WriteString("<script>")
if order.ID != 0 {
for _, c := range order.Cakes {
b.WriteString(fmt.Sprintf("updateCatalogueQty(%d,%d);", c.ID, c.Amount))
}
}
b.WriteString("updateTotals();</script>")
return b.String()
}