2026-02-06 17:35:29 +01:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
|
|
"erp_system/internal/models"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (h *Handler) InvoiceList(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
status := r.URL.Query().Get("status")
|
2026-02-07 07:47:20 +01:00
|
|
|
page, _ := strconv.Atoi(r.URL.Query().Get("page"))
|
|
|
|
|
if page < 1 {
|
|
|
|
|
page = 1
|
|
|
|
|
}
|
|
|
|
|
limit := 10
|
|
|
|
|
|
|
|
|
|
invoices, total, err := models.InvoiceGetPaginated(h.DB, status, page, limit)
|
2026-02-06 17:35:29 +01:00
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-07 07:47:20 +01:00
|
|
|
totalPages := (total + limit - 1) / limit
|
|
|
|
|
|
2026-02-06 17:35:29 +01:00
|
|
|
data := map[string]interface{}{
|
|
|
|
|
"Title": "Invoices",
|
|
|
|
|
"Username": h.getUsername(r),
|
|
|
|
|
"ActivePage": "invoices",
|
|
|
|
|
"Invoices": invoices,
|
|
|
|
|
"FilterStatus": status,
|
2026-02-07 07:47:20 +01:00
|
|
|
"Page": page,
|
|
|
|
|
"TotalPages": totalPages,
|
|
|
|
|
"HasPrev": page > 1,
|
|
|
|
|
"HasNext": page < totalPages,
|
|
|
|
|
"PrevPage": page - 1,
|
|
|
|
|
"NextPage": page + 1,
|
2026-02-06 17:35:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if r.Header.Get("HX-Request") == "true" && r.URL.Query().Get("partial") == "true" {
|
|
|
|
|
h.renderPartial(w, "invoices/list.html", "invoice-table", data)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-08 14:20:18 +01:00
|
|
|
h.render(w, r, []string{"layout.html", "invoices/list.html"}, data)
|
2026-02-06 17:35:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *Handler) InvoiceDetail(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
id, _ := strconv.Atoi(r.PathValue("id"))
|
|
|
|
|
invoice, err := models.InvoiceGetByID(h.DB, id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, "Invoice not found", http.StatusNotFound)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data := map[string]interface{}{
|
|
|
|
|
"Title": invoice.InvoiceNumber,
|
|
|
|
|
"Username": h.getUsername(r),
|
|
|
|
|
"ActivePage": "invoices",
|
|
|
|
|
"Invoice": invoice,
|
|
|
|
|
}
|
2026-02-08 14:20:18 +01:00
|
|
|
h.render(w, r, []string{"layout.html", "invoices/detail.html"}, data)
|
2026-02-06 17:35:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *Handler) InvoicePay(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
id, _ := strconv.Atoi(r.PathValue("id"))
|
|
|
|
|
if err := models.InvoiceMarkPaid(h.DB, id); err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if r.Header.Get("HX-Request") == "true" {
|
|
|
|
|
w.Header().Set("HX-Redirect", fmt.Sprintf("/invoices/%d", id))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
http.Redirect(w, r, fmt.Sprintf("/invoices/%d", id), http.StatusSeeOther)
|
|
|
|
|
}
|