37 lines
1.4 KiB
Go
37 lines
1.4 KiB
Go
package httpapi
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"encoding/csv"
|
||
|
|
"net/http"
|
||
|
|
"strconv"
|
||
|
|
"strings"
|
||
|
|
|
||
|
|
"github.com/google/uuid"
|
||
|
|
)
|
||
|
|
|
||
|
|
// Platform-admin export of actual Stripe invoice amounts (not estimated usage).
|
||
|
|
// Scope must be a single company; customer IDs are resolved server-side.
|
||
|
|
func (s *Server) handleAdminExportCompanyInvoices(w http.ResponseWriter, r *http.Request) {
|
||
|
|
id, err := uuid.Parse(strings.TrimSpace(r.URL.Query().Get("company_id")))
|
||
|
|
if err != nil {
|
||
|
|
Error(w, http.StatusBadRequest, "company_id is required")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
rows, err := s.stripeSvc().ListCompanyInvoices(r.Context(), id)
|
||
|
|
if err != nil {
|
||
|
|
LogAndError(w, http.StatusBadGateway, "could not export Stripe invoices", err)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
w.Header().Set("Content-Type", "text/csv; charset=utf-8")
|
||
|
|
w.Header().Set("Content-Disposition", `attachment; filename="descrybe-stripe-invoices.csv"`)
|
||
|
|
w.Header().Set("Cache-Control", "private, no-store")
|
||
|
|
out := csv.NewWriter(w)
|
||
|
|
_ = out.Write([]string{"company_id", "invoice_id", "number", "status", "currency", "created_unix", "amount_due_minor", "amount_paid_minor", "amount_remaining_minor", "hosted_invoice_url", "invoice_pdf"})
|
||
|
|
for _, row := range rows {
|
||
|
|
_ = out.Write([]string{id.String(), row.ID, row.Number, row.Status, row.Currency,
|
||
|
|
strconv.FormatInt(row.Created, 10), strconv.FormatInt(row.AmountDue, 10),
|
||
|
|
strconv.FormatInt(row.AmountPaid, 10), strconv.FormatInt(row.AmountRemaining, 10),
|
||
|
|
row.HostedInvoiceURL, row.InvoicePDF})
|
||
|
|
}
|
||
|
|
out.Flush()
|
||
|
|
}
|