package handlers
import (
"net/http"
"strconv"
"time"
"go-crm/internal/db"
"github.com/go-chi/chi/v5"
)
func (a *App) ListPayments(w http.ResponseWriter, r *http.Request) {
accountID, ok := a.requireAuth(w, r)
if !ok {
return
}
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(a.DB, clientID, limit, offset)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
clients, err := db.ListClients(a.DB, accountID, limit, offset)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
customers, err := db.ListCustomers(a.DB, accountID, clientID, limit, offset)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var clientOptions, customerOptions string
clientNames := make(map[int64]string)
customerNames := make(map[int64]string)
for _, c := range clients {
clientOptions += ``
clientNames[c.ClientID] = c.Name
}
for _, cu := range customers {
customerOptions += ``
customerNames[cu.CustomerID] = cu.Name
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write([]byte(`
Payments
| Client | Customer | Amount | Paid | Date | Method | Actions |
`))
for _, p := range payments {
paid := "No"
if p.HasPaid {
paid = "Yes"
}
clientName := clientNames[p.ClientID]
if clientName == "" {
clientName = strconv.FormatInt(p.ClientID, 10)
}
customerName := customerNames[p.CustomerID]
if customerName == "" {
customerName = strconv.FormatInt(p.CustomerID, 10)
}
w.Write([]byte(`| ` + clientName + ` | ` + customerName + ` | ` + strconv.FormatFloat(p.Amount, 'f', 2, 64) + ` | ` + paid + ` | ` + p.PaymentDate + ` | ` + p.PaymentMethod + ` | View |
|
`))
}
w.Write([]byte(`
`))
}
func (a *App) CreatePayment(w http.ResponseWriter, r *http.Request) {
_, ok := a.requireAuth(w, r)
if !ok {
return
}
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(a.DB); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.Header().Set("HX-Refresh", "true")
}
func (a *App) ViewPayment(w http.ResponseWriter, r *http.Request) {
_, ok := a.requireAuth(w, r)
if !ok {
return
}
id, _ := strconv.ParseInt(r.URL.Query().Get("id"), 10, 64)
var p db.Payment
var hasPaid int
err := a.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; charset=utf-8")
w.Write([]byte(`Payment
Amount: ` + strconv.FormatFloat(p.Amount, 'f', 2, 64) + `
Paid: ` + strconv.FormatBool(p.HasPaid) + `
Method: ` + p.PaymentMethod + `
Back