|
|
@ -4,9 +4,9 @@ import ( |
|
|
"bytes" |
|
|
"bytes" |
|
|
"encoding/json" |
|
|
"encoding/json" |
|
|
"fmt" |
|
|
"fmt" |
|
|
"html/template" |
|
|
|
|
|
"io" |
|
|
"io" |
|
|
"log" |
|
|
"log" |
|
|
|
|
|
root "marmic/servicetrade-toolbox" |
|
|
"marmic/servicetrade-toolbox/internal/api" |
|
|
"marmic/servicetrade-toolbox/internal/api" |
|
|
"net/http" |
|
|
"net/http" |
|
|
"strings" |
|
|
"strings" |
|
|
@ -23,7 +23,7 @@ var statusButtons = []StatusButton{ |
|
|
{"draft", "Draft Invoice", "success-button", "Are you sure you want to draft this invoice?"}, |
|
|
{"draft", "Draft Invoice", "success-button", "Are you sure you want to draft this invoice?"}, |
|
|
{"ok", "Ok Invoice", "success-button", "Are you sure you want to mark this invoice as OK?"}, |
|
|
{"ok", "Ok Invoice", "success-button", "Are you sure you want to mark this invoice as OK?"}, |
|
|
{"fail", "Fail Invoice", "caution-button", "Are you sure you want to fail this invoice?"}, |
|
|
{"fail", "Fail Invoice", "caution-button", "Are you sure you want to fail this invoice?"}, |
|
|
{"pending", "Pending Invoice", "caution-button", "Are you sure you want to mark this invoice as pending?"}, |
|
|
{"pending_accounting", "Pending Invoice", "caution-button", "Are you sure you want to mark this invoice as pending?"}, |
|
|
{"processed", "Process Invoice", "warning-button", "Are you sure you want to process this invoice?"}, |
|
|
{"processed", "Process Invoice", "warning-button", "Are you sure you want to process this invoice?"}, |
|
|
{"void", "Void Invoice", "warning-button", "Are you sure you want to void this invoice?"}, |
|
|
{"void", "Void Invoice", "warning-button", "Are you sure you want to void this invoice?"}, |
|
|
} |
|
|
} |
|
|
@ -40,19 +40,28 @@ func InvoicesHandler(w http.ResponseWriter, r *http.Request) { |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
tmpl := template.Must(template.ParseFiles("templates/layout.html", "templates/partials/invoices.html")) |
|
|
tmpl := root.WebTemplates |
|
|
data := map[string]interface{}{ |
|
|
data := map[string]interface{}{ |
|
|
"Title": "Invoices", |
|
|
"Title": "Invoices", |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var err error |
|
|
|
|
|
|
|
|
if r.Header.Get("HX-Request") == "true" { |
|
|
if r.Header.Get("HX-Request") == "true" { |
|
|
tmpl.ExecuteTemplate(w, "content", data) |
|
|
err = tmpl.ExecuteTemplate(w, "content", data) |
|
|
} else { |
|
|
} else { |
|
|
tmpl.Execute(w, data) |
|
|
err = tmpl.ExecuteTemplate(w, "layout.html", data) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
log.Printf("Template execution error: %v", err) |
|
|
|
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError) |
|
|
|
|
|
return |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func handleInvoiceSearch(w http.ResponseWriter, r *http.Request, session *api.Session) { |
|
|
func handleInvoiceSearch(w http.ResponseWriter, r *http.Request, session *api.Session) { |
|
|
|
|
|
tmpl := root.WebTemplates |
|
|
searchTerm := strings.TrimSpace(r.URL.Query().Get("search")) |
|
|
searchTerm := strings.TrimSpace(r.URL.Query().Get("search")) |
|
|
|
|
|
|
|
|
if searchTerm == "" { |
|
|
if searchTerm == "" { |
|
|
@ -74,7 +83,6 @@ func handleInvoiceSearch(w http.ResponseWriter, r *http.Request, session *api.Se |
|
|
if strings.Contains(err.Error(), "access forbidden") { |
|
|
if strings.Contains(err.Error(), "access forbidden") { |
|
|
errorMsg = "You do not have permission to view this invoice." |
|
|
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{}{ |
|
|
tmpl.ExecuteTemplate(w, "invoice_search_results", map[string]interface{}{ |
|
|
"Error": true, |
|
|
"Error": true, |
|
|
"ErrorMsg": errorMsg, |
|
|
"ErrorMsg": errorMsg, |
|
|
@ -86,7 +94,6 @@ func handleInvoiceSearch(w http.ResponseWriter, r *http.Request, session *api.Se |
|
|
if invoice == nil { |
|
|
if invoice == nil { |
|
|
log.Printf("No invoice found for: %s", searchTerm) |
|
|
log.Printf("No invoice found for: %s", searchTerm) |
|
|
w.WriteHeader(http.StatusOK) |
|
|
w.WriteHeader(http.StatusOK) |
|
|
tmpl := template.Must(template.ParseFiles("templates/partials/invoice_search_results.html")) |
|
|
|
|
|
tmpl.ExecuteTemplate(w, "invoice_search_results", map[string]interface{}{ |
|
|
tmpl.ExecuteTemplate(w, "invoice_search_results", map[string]interface{}{ |
|
|
"NotFound": true, |
|
|
"NotFound": true, |
|
|
"ErrorMsg": fmt.Sprintf("No invoice found for: %s", searchTerm), |
|
|
"ErrorMsg": fmt.Sprintf("No invoice found for: %s", searchTerm), |
|
|
@ -106,7 +113,6 @@ func handleInvoiceSearch(w http.ResponseWriter, r *http.Request, session *api.Se |
|
|
invoice["buttons"] = getInvoiceStatusButtons(invoice["id"].(string), invoice["status"].(string)) |
|
|
invoice["buttons"] = getInvoiceStatusButtons(invoice["id"].(string), invoice["status"].(string)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
tmpl := template.Must(template.ParseFiles("templates/partials/invoice_search_results.html")) |
|
|
|
|
|
err = tmpl.ExecuteTemplate(w, "invoice_search_results", invoice) |
|
|
err = tmpl.ExecuteTemplate(w, "invoice_search_results", invoice) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
log.Printf("Error executing template: %v", err) |
|
|
log.Printf("Error executing template: %v", err) |
|
|
@ -137,6 +143,7 @@ func getInvoiceStatusButtons(invoiceID, currentStatus string) []map[string]strin |
|
|
default: |
|
|
default: |
|
|
// For all other statuses, show all buttons except the current status
|
|
|
// For all other statuses, show all buttons except the current status
|
|
|
for _, button := range statusButtons { |
|
|
for _, button := range statusButtons { |
|
|
|
|
|
fmt.Printf("btn status: %s, curr status: %s", button.Status, currentStatus) |
|
|
if button.Status != currentStatus { |
|
|
if button.Status != currentStatus { |
|
|
buttons = append(buttons, map[string]string{ |
|
|
buttons = append(buttons, map[string]string{ |
|
|
"Action": fmt.Sprintf("/%s-invoice/%s", button.Status, invoiceID), |
|
|
"Action": fmt.Sprintf("/%s-invoice/%s", button.Status, invoiceID), |
|
|
@ -185,7 +192,7 @@ func UpdateInvoiceStatusHandler(w http.ResponseWriter, r *http.Request) { |
|
|
"draft": true, |
|
|
"draft": true, |
|
|
"fail": true, |
|
|
"fail": true, |
|
|
"ok": true, |
|
|
"ok": true, |
|
|
"pending": true, |
|
|
"pending_accounting": true, |
|
|
"processed": true, |
|
|
"processed": true, |
|
|
"void": true, |
|
|
"void": true, |
|
|
} |
|
|
} |
|
|
@ -231,8 +238,7 @@ func UpdateInvoiceStatusHandler(w http.ResponseWriter, r *http.Request) { |
|
|
log.Printf("Updated invoice after status change to %s: %+v", status, invoice) |
|
|
log.Printf("Updated invoice after status change to %s: %+v", status, invoice) |
|
|
|
|
|
|
|
|
// Render the updated invoice details
|
|
|
// Render the updated invoice details
|
|
|
tmpl := template.Must(template.ParseFiles("templates/partials/invoice_search_results.html")) |
|
|
err = root.WebTemplates.ExecuteTemplate(w, "invoice_search_results", invoice) |
|
|
err = tmpl.ExecuteTemplate(w, "invoice_search_results", invoice) |
|
|
|
|
|
if err != nil { |
|
|
if err != nil { |
|
|
log.Printf("Error executing template: %v", err) |
|
|
log.Printf("Error executing template: %v", err) |
|
|
http.Error(w, "Error rendering template", http.StatusInternalServerError) |
|
|
http.Error(w, "Error rendering template", http.StatusInternalServerError) |
|
|
|