- Go module with chi router, bcrypt - SQLite schema for 9 tables - Auth, Clients, Customers, Services, Scheduling, Payments, Q&A handlers - HTMX template layouts
88 lines
3.8 KiB
Go
88 lines
3.8 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"go-crm/internal/db"
|
|
)
|
|
|
|
func ListPayments(w http.ResponseWriter, r *http.Request) {
|
|
limit, _ := strconv.Atoi(r.URL.Query().Get("limit"))
|
|
offset, _ := strconv.Atoi(r.URL.Query().Get("offset"))
|
|
clientID, _ := strconv.ParseInt(r.URL.Query().Get("client_id"), 10, 64)
|
|
if limit == 0 {
|
|
limit = 20
|
|
}
|
|
|
|
payments, err := db.ListPayments(DB, clientID, limit, offset)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/html")
|
|
w.Write([]byte(`<!DOCTYPE html><html><head><script src="https://unpkg.com/htmx.org@1.9.10"></script></head><body><h1>Payments</h1><button class="btn" onclick="document.getElementById('paymentForm').style.display='block'">Add Payment</button><div id="paymentForm" style="display:none; margin-top:1rem;"><form hx-post="/payments" hx-target="#paymentList"><input type="number" name="client_id" placeholder="Client ID" required><input type="number" name="customer_id" placeholder="Customer ID" required><input type="number" name="schedule_id" placeholder="Schedule ID"><input type="checkbox" name="has_paid"><label>Paid</label><input type="number" name="amount" placeholder="Amount" step="0.01"><input type="date" name="payment_date"><input type="text" name="payment_method" placeholder="Payment Method"><button type="submit">Add</button></form></div><table><thead><tr><th>Customer</th><th>Amount</th><th>Paid</th><th>Date</th><th>Method</th><th>Actions</th></tr></thead><tbody id="paymentList">`))
|
|
for _, p := range payments {
|
|
paid := "No"
|
|
if p.HasPaid {
|
|
paid = "Yes"
|
|
}
|
|
w.Write([]byte(`<tr><td>` + strconv.FormatInt(p.CustomerID, 10) + `</td><td>` + strconv.FormatFloat(p.Amount, 'f', 2, 64) + `</td><td>` + paid + `</td><td>` + p.PaymentDate + `</td><td>` + p.PaymentMethod + `</td><td><a href="/payments/` + strconv.FormatInt(p.PaymentID, 10) + `">View</a></td></tr>`))
|
|
}
|
|
w.Write([]byte(`</tbody></table></body></html>`))
|
|
}
|
|
|
|
func CreatePayment(w http.ResponseWriter, r *http.Request) {
|
|
r.ParseForm()
|
|
clientID, _ := strconv.ParseInt(r.FormValue("client_id"), 10, 64)
|
|
customerID, _ := strconv.ParseInt(r.FormValue("customer_id"), 10, 64)
|
|
scheduleID, _ := strconv.ParseInt(r.FormValue("schedule_id"), 10, 64)
|
|
amount, _ := strconv.ParseFloat(r.FormValue("amount"), 64)
|
|
hasPaid := r.FormValue("has_paid") == "on"
|
|
|
|
payment := db.Payment{
|
|
ClientID: clientID,
|
|
CustomerID: customerID,
|
|
ScheduleID: scheduleID,
|
|
HasPaid: hasPaid,
|
|
Amount: amount,
|
|
PaymentDate: r.FormValue("payment_date"),
|
|
PaymentMethod: r.FormValue("payment_method"),
|
|
CreatedAt: time.Now().Unix(),
|
|
}
|
|
|
|
if err := payment.Create(DB); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("HX-Refresh", "true")
|
|
}
|
|
|
|
func ViewPayment(w http.ResponseWriter, r *http.Request) {
|
|
id, _ := strconv.ParseInt(r.URL.Query().Get("id"), 10)
|
|
var p db.Payment
|
|
var hasPaid int
|
|
err := DB.QueryRow("SELECT payment_id, client_id, customer_id, schedule_id, has_paid, amount, payment_date, payment_method, created_at FROM payments WHERE payment_id = ?", id).Scan(&p.PaymentID, &p.ClientID, &p.CustomerID, &p.ScheduleID, &hasPaid, &p.Amount, &p.PaymentDate, &p.PaymentMethod, &p.CreatedAt)
|
|
if err != nil {
|
|
http.Error(w, "Payment not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
p.HasPaid = hasPaid == 1
|
|
|
|
w.Header().Set("Content-Type", "text/html")
|
|
w.Write([]byte(`<!DOCTYPE html><body><h1>Payment</h1><p>Amount: ` + strconv.FormatFloat(p.Amount, 'f', 2, 64) + `</p><p>Paid: ` + strconv.FormatBool(p.HasPaid) + `</p><p>Method: ` + p.PaymentMethod + `</p><a href="/payments">Back</a></body></html>`))
|
|
}
|
|
|
|
func UpdatePayment(w http.ResponseWriter, r *http.Request) {}
|
|
|
|
func DeletePayment(w http.ResponseWriter, r *http.Request) {}
|
|
|
|
func boolToStr(b bool) string {
|
|
if b {
|
|
return "Yes"
|
|
}
|
|
return "No"
|
|
} |