From 5feb8a297212825abbfafbebbae7fc44349a2144 Mon Sep 17 00:00:00 2001
From: blint <rethelyibalint@gmail.com>
Date: Sun, 20 Aug 2023 20:53:30 +0200
Subject: [PATCH] Add new Pultosch functionality and related templates

Added a new package, "pultosch", which includes server-side functionality (pultosch.go) and HTML templates for the Pultosch (cashier) interface (pultosch.template.html and recentSpends.template.html). The update provides features such as transaction oversight and budget tracking for administrators utilizing transaction and spend infos ('specialSpend', 'spendData'). Through this addition, the application will now support a comprehensive overview of recent transactions (limit default is set to 10). This will allow admins to monitor these transactions more closely for better financial management.
---
 pultosch/pultosch.go                | 153 ++++++++++++++++++++++++++++
 pultosch/pultosch.template.html     |  34 +++++++
 pultosch/recentSpends.template.html |  53 ++++++++++
 3 files changed, 240 insertions(+)
 create mode 100644 pultosch/pultosch.go
 create mode 100644 pultosch/pultosch.template.html
 create mode 100644 pultosch/recentSpends.template.html

diff --git a/pultosch/pultosch.go b/pultosch/pultosch.go
new file mode 100644
index 0000000..8e7f492
--- /dev/null
+++ b/pultosch/pultosch.go
@@ -0,0 +1,153 @@
+package pultosch
+
+import (
+	"git.sch.bme.hu/disappointment-industries/becskasszasch/db"
+	"git.sch.bme.hu/disappointment-industries/becskasszasch/helpers"
+	"git.sch.bme.hu/disappointment-industries/becskasszasch/homepage"
+	"html/template"
+	"net/http"
+	"strconv"
+)
+
+var pultoschTemplate *template.Template
+var spendsLimitTemplate *template.Template
+var router = http.NewServeMux()
+var Handler = PultoschMW(router)
+
+type specialSpend struct {
+	Schacc  string
+	Name    string
+	Product string
+	Amount  int64
+	Date    string
+}
+
+type spendData struct {
+	User        *homepage.User
+	Spends      []*specialSpend
+	ProductName string
+}
+
+type pultData struct {
+	User       *db.User
+	Dark       bool
+	LoginURL   string
+	IsAdmin    bool
+	IsPultosch bool
+
+	Error    string
+	Products []*db.Product
+	Users    []*db.User
+}
+
+func init() {
+	var err error
+
+	pultoschTemplate, err = template.New("pultosch.template.html").ParseFiles("pultosch/pultosch.template.html", "homepage/navbar.template.html", "homepage/header.template.html", "homepage/footer.template.html")
+	spendsLimitTemplate, err = template.New("recentSpends.template.html").ParseFiles("pultosch/recentSpends.template.html", "homepage/navbar.template.html", "homepage/header.template.html", "homepage/footer.template.html")
+	if err != nil {
+		panic(err)
+	}
+
+	router.HandleFunc("/spends", spendsLimitHandler)
+	router.HandleFunc("/spends/", spendsLimitHandler)
+	router.HandleFunc("/", handler)
+}
+
+func PultoschMW(h http.Handler) http.Handler {
+	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		user, err := homepage.GetUserInfoBySession(r)
+		if err != nil {
+			helpers.Logger.Println(err)
+			return
+		}
+		if user.IsPultosch {
+			h.ServeHTTP(w, r)
+		} else {
+			w.WriteHeader(http.StatusForbidden)
+		}
+	})
+}
+
+func handler(w http.ResponseWriter, r *http.Request) {
+	if r.Method == http.MethodGet {
+		user, err := homepage.GetUserInfoBySession(r)
+		if err != nil {
+			helpers.Logger.Println(err)
+			return
+		}
+
+		products, err := db.GetAvailableProducts()
+		if err != nil {
+			helpers.Logger.Println(err)
+			return
+		}
+
+		users, err := db.GetUsersSorted(db.GetDB())
+		if err != nil {
+			helpers.Logger.Println(err)
+			return
+		}
+
+		data := pultData{
+			User:     user.User,
+			Dark:     user.Dark,
+			LoginURL: user.LoginURL,
+			IsAdmin:  user.IsAdmin,
+			Products: products,
+			Users:    users,
+		}
+		er := pultoschTemplate.Execute(w, &data)
+		if er != nil {
+			helpers.Logger.Println(er)
+		}
+	} else {
+		w.WriteHeader(http.StatusMethodNotAllowed)
+	}
+}
+
+func convertSpends(spends []*db.Spend) []*specialSpend {
+	var ret []*specialSpend
+	for _, spend := range spends {
+		ret = append(ret, &specialSpend{
+			Schacc:  spend.User.SchAcc,
+			Name:    spend.User.Name,
+			Product: spend.Product.Name,
+			Amount:  spend.Amount,
+			Date:    spend.Date.String(),
+		})
+	}
+	return ret
+}
+
+func spendsLimitHandler(w http.ResponseWriter, r *http.Request) {
+	if r.Method == http.MethodGet {
+		limitStr := r.URL.Query().Get("limit")
+		limit, err := strconv.Atoi(limitStr)
+		if err != nil {
+			limit = 10 // Default value if the limit parameter is not a valid integer
+		}
+
+		spends, err := db.GetSpendsWithLimit(limit)
+		if err != nil {
+			helpers.Logger.Println(err)
+			return
+		}
+
+		usr, err := homepage.GetUserInfoBySession(r)
+		if err != nil {
+			return
+		}
+
+		sp := spendData{
+			User:   usr,
+			Spends: convertSpends(spends),
+		}
+		// You may also wish to include more data in the template's context.
+		// Make sure to create and parse a new template for this page.
+		er := spendsLimitTemplate.Execute(w, &sp)
+		if er != nil {
+			helpers.Logger.Println(er)
+		}
+	}
+}
diff --git a/pultosch/pultosch.template.html b/pultosch/pultosch.template.html
new file mode 100644
index 0000000..0cdc871
--- /dev/null
+++ b/pultosch/pultosch.template.html
@@ -0,0 +1,34 @@
+{{ template "header" .}}
+
+{{ template "navbar" . }}
+
+{{- if .User.IsPultosch }}
+
+    <form method="post" action="/api/v1/cart">
+            <label for="userSelect">Choose a user:</label><br>
+            <select id="userSelect" name="userId">
+                {{ range .Users }}
+                    <option value="{{ .SchAcc }}">{{ .SchAcc }} - {{ .Name }}</option>
+                {{ end }}
+            </select><br><br>
+
+            <div id="cart">
+                <label>Choose products:</label><br>
+                <select id="productSelect" name="productId">
+                    {{ range .Products }}
+                        <option value="{{ .ID }}">{{ .Name }}</option>
+                    {{ end }}
+                </select>
+                <label for="amount">Amount:</label>
+                <input type="number" id="amount" min="1" value="1" name="amount">
+                <input type="button" id="addToCart" value="Add to  cart"><br>
+            </div>
+
+            <input type="submit" value="Sell">
+        </form>
+
+        <script src="/static/js/cart.js"></script>
+
+{{- end }}
+
+{{ template "footer" . }}
\ No newline at end of file
diff --git a/pultosch/recentSpends.template.html b/pultosch/recentSpends.template.html
new file mode 100644
index 0000000..48570b6
--- /dev/null
+++ b/pultosch/recentSpends.template.html
@@ -0,0 +1,53 @@
+{{ template "header" .User}}
+
+{{ template "navbar" .User }}
+
+{{- if .User.IsPultosch }}
+
+<main>
+    <div class="container">
+        <div class="text-center form justify-content-center">
+            <div class="d-flex align-items-center justify-content-center mb-3">
+            <h2>Recent Spends</h2>
+                <form action="" method="get" class="ml-5" id="autoReloadForm">
+                    <div class="d-flex align-items-center">
+                      <label for="limit" class="mr-3">limit:</label>
+                      <input type="number" id="limit" name="limit" min="1" value="10" class="form-control mr-3">
+                      <input type="submit" value="Reload" class="btn btn-primary">
+                    </div>
+              </form>
+            </div>
+
+            <div class="overflow-x-auto">
+                <table class="table" id="admin-table">
+                    <thead>
+                    <tr class="text-left">
+                        <th scope="col">Schacc</th>
+                        <th scope="col">Name</th>
+                        <th scope="col">Product</th>
+                        <th scope="col">Amount</th>
+                        <th scope="col">Date</th>
+                        <!-- Add other necessary fields -->
+                    </tr>
+                    </thead>
+                    <tbody>
+                    <tr>
+                    {{range .Spends }}
+                        <td class="text-left">{{ .Schacc}}</td>
+                        <td class="text-left">{{ .Name}}</td>
+                        <td class="text-left">{{.Product}}</td>
+                        <td class="text-right">{{.Amount}}</td>
+                        <td class="text-left">{{.Date}}</td>
+                        <!-- Render other fields here -->
+                    </tr>
+                    {{end}}
+                    </tbody>
+                </table>
+            </div>
+        </div>
+    </main>
+
+
+{{- end }}
+
+{{ template "footer" . }}
\ No newline at end of file
-- 
GitLab