an updated and hopefully faster version of the ST Toolbox
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

87 lines
2.4 KiB

package web
import (
"fmt"
"html/template"
"log"
"marmic/servicetrade-toolbox/internal/api"
"net/http"
"strings"
)
func InvoicesHandler(w http.ResponseWriter, r *http.Request) {
session, ok := r.Context().Value("session").(*api.Session)
if !ok {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
if r.Method == "GET" {
handleInvoiceSearch(w, r, session)
return
}
tmpl := template.Must(template.ParseFiles("templates/layout.html", "templates/partials/invoices.html"))
data := map[string]interface{}{
"Title": "Invoices",
}
if r.Header.Get("HX-Request") == "true" {
tmpl.ExecuteTemplate(w, "content", data)
} else {
tmpl.Execute(w, data)
}
}
func handleInvoiceSearch(w http.ResponseWriter, r *http.Request, session *api.Session) {
searchTerm := strings.TrimSpace(r.URL.Query().Get("search"))
if searchTerm == "" {
log.Println("Empty search term, returning empty response")
w.WriteHeader(http.StatusOK)
return
}
log.Printf("Searching for invoice with term: %s", searchTerm)
invoice, err := session.GetInvoice(searchTerm)
log.Printf("GetInvoice result - invoice: %+v, err: %v", invoice, err)
if err != nil {
log.Printf("Error fetching invoice: %v", err)
w.WriteHeader(http.StatusOK)
errorMsg := fmt.Sprintf("Error fetching invoice: %v", err)
if strings.Contains(err.Error(), "access forbidden") {
errorMsg = "You do not have permission to view this invoice."
}
tmpl := template.Must(template.ParseFiles("templates/partials/invoice_search_results.html"))
tmpl.ExecuteTemplate(w, "invoice_search_results", map[string]interface{}{
"Error": true,
"ErrorMsg": errorMsg,
"SearchTerm": searchTerm,
})
return
}
if invoice == nil {
log.Printf("No invoice found for: %s", searchTerm)
w.WriteHeader(http.StatusOK)
tmpl := template.Must(template.ParseFiles("templates/partials/invoice_search_results.html"))
tmpl.ExecuteTemplate(w, "invoice_search_results", map[string]interface{}{
"NotFound": true,
"ErrorMsg": fmt.Sprintf("No invoice found for: %s", searchTerm),
"SearchTerm": searchTerm,
})
return
}
log.Printf("Invoice found: %+v", invoice)
tmpl := template.Must(template.ParseFiles("templates/partials/invoice_search_results.html"))
err = tmpl.ExecuteTemplate(w, "invoice_search_results", invoice)
if err != nil {
log.Printf("Error executing template: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
}
}