|
|
|
@ -49,12 +49,12 @@ func handleInvoiceSearch(w http.ResponseWriter, r *http.Request, session *api.Se |
|
|
|
|
|
|
|
invoice, err := session.GetInvoice(searchTerm) |
|
|
|
|
|
|
|
log.Printf("GetInvoice result - invoice: %+v, err: %v", invoice, err) |
|
|
|
// 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) |
|
|
|
errorMsg := fmt.Sprintf("No invoice found for: %s", searchTerm) |
|
|
|
if strings.Contains(err.Error(), "access forbidden") { |
|
|
|
errorMsg = "You do not have permission to view this invoice." |
|
|
|
} |
|
|
|
@ -83,7 +83,7 @@ func handleInvoiceSearch(w http.ResponseWriter, r *http.Request, session *api.Se |
|
|
|
invoice["id"] = fmt.Sprintf("%.0f", id) |
|
|
|
} |
|
|
|
|
|
|
|
log.Printf("Invoice found: %+v", invoice) |
|
|
|
// 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) |
|
|
|
@ -93,7 +93,7 @@ func handleInvoiceSearch(w http.ResponseWriter, r *http.Request, session *api.Se |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func VoidInvoiceHandler(w http.ResponseWriter, r *http.Request) { |
|
|
|
func UpdateInvoiceStatusHandler(w http.ResponseWriter, r *http.Request) { |
|
|
|
session, ok := r.Context().Value("session").(*api.Session) |
|
|
|
if !ok { |
|
|
|
http.Error(w, "Unauthorized", http.StatusUnauthorized) |
|
|
|
@ -105,41 +105,56 @@ func VoidInvoiceHandler(w http.ResponseWriter, r *http.Request) { |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
// Extract the invoice ID from the URL
|
|
|
|
// Extract the invoice ID and status from the URL
|
|
|
|
parts := strings.Split(r.URL.Path, "/") |
|
|
|
if len(parts) < 3 { |
|
|
|
http.Error(w, "Invalid request", http.StatusBadRequest) |
|
|
|
return |
|
|
|
} |
|
|
|
invoiceID := parts[len(parts)-1] |
|
|
|
status := parts[len(parts)-2] |
|
|
|
|
|
|
|
if invoiceID == "" { |
|
|
|
http.Error(w, "Invalid invoice ID", http.StatusBadRequest) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
// Validate the status
|
|
|
|
validStatuses := map[string]bool{ |
|
|
|
"fail": true, |
|
|
|
"ok": true, |
|
|
|
"pending": true, |
|
|
|
"processed": true, |
|
|
|
"void": true, |
|
|
|
} |
|
|
|
|
|
|
|
if !validStatuses[status] { |
|
|
|
http.Error(w, "Invalid status", http.StatusBadRequest) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
// Prepare the request body
|
|
|
|
requestBody := map[string]string{"status": "void"} |
|
|
|
requestBody := map[string]string{"status": status} |
|
|
|
jsonBody, err := json.Marshal(requestBody) |
|
|
|
if err != nil { |
|
|
|
http.Error(w, "Error preparing request", http.StatusInternalServerError) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
// Send the PUT request to void the invoice
|
|
|
|
// Send the PUT request to update the invoice status
|
|
|
|
endpoint := fmt.Sprintf("/invoice/%s", invoiceID) |
|
|
|
resp, err := session.DoRequest("PUT", endpoint, bytes.NewBuffer(jsonBody)) |
|
|
|
if err != nil { |
|
|
|
log.Printf("Error voiding invoice: %v", err) |
|
|
|
http.Error(w, fmt.Sprintf("Error voiding invoice: %v", err), http.StatusInternalServerError) |
|
|
|
log.Printf("Error updating invoice status: %v", err) |
|
|
|
http.Error(w, fmt.Sprintf("Error updating invoice status: %v", err), http.StatusInternalServerError) |
|
|
|
return |
|
|
|
} |
|
|
|
defer resp.Body.Close() |
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK { |
|
|
|
body, _ := io.ReadAll(resp.Body) |
|
|
|
log.Printf("Failed to void invoice: %s", body) |
|
|
|
http.Error(w, fmt.Sprintf("Failed to void invoice: %s", body), resp.StatusCode) |
|
|
|
log.Printf("Failed to update invoice status: %s", body) |
|
|
|
http.Error(w, fmt.Sprintf("Failed to update invoice status: %s", body), resp.StatusCode) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
@ -151,7 +166,7 @@ func VoidInvoiceHandler(w http.ResponseWriter, r *http.Request) { |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
log.Printf("Updated invoice after voiding: %+v", invoice) |
|
|
|
log.Printf("Updated invoice after status change to %s: %+v", status, invoice) |
|
|
|
|
|
|
|
// Render the updated invoice details
|
|
|
|
tmpl := template.Must(template.ParseFiles("templates/partials/invoice_search_results.html")) |
|
|
|
|