Metrics → Customers: only customers who have spent more than $0
A customer counts when their paid invoices' amount_paid, less refunds, is above zero. Trials ($0 invoices), fully discounted invoices, only-open invoices and payments refunded in full no longer put someone on the list. Also ticks the Market Fetch check in TEST.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -151,7 +151,8 @@ func manual(d *db.DB) (map[string]string, error) {
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// Matches matches every Stripe customer with billing to an Account: the
|
||||
// Matches matches every Stripe customer who has spent something to an
|
||||
// Account: the
|
||||
// user's choice first, then a stored ID (either side), then email (an
|
||||
// Account's or one of its Contacts'), then the email's domain (not free
|
||||
// mail). Paying customers come first, by MRR.
|
||||
@@ -168,19 +169,12 @@ func Matches(d *db.DB, model *Model) ([]Match, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
billed := map[string]bool{}
|
||||
rows, err := d.Query(`SELECT DISTINCT customer FROM stripe_objects WHERE kind = 'invoice' AND status IN ('paid', 'open')`)
|
||||
paying, err := spenders(d)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for rows.Next() {
|
||||
var c string
|
||||
rows.Scan(&c)
|
||||
billed[c] = true
|
||||
}
|
||||
rows.Close()
|
||||
prospects := map[string]string{}
|
||||
rows, err = d.Query(`SELECT id, subject FROM stripe_customers WHERE subject NOT LIKE 'stripe:%'`)
|
||||
rows, err := d.Query(`SELECT id, subject FROM stripe_customers WHERE subject NOT LIKE 'stripe:%'`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -217,7 +211,7 @@ func Matches(d *db.DB, model *Model) ([]Match, error) {
|
||||
}
|
||||
var out []Match
|
||||
for _, c := range custs {
|
||||
if !billed[c.ID] {
|
||||
if !paying[c.ID] {
|
||||
continue
|
||||
}
|
||||
m := Match{Customer: c.Customer, MRR: now[c.ID], Prospect: prospects[c.ID]}
|
||||
@@ -312,3 +306,51 @@ func MatchSummary(ms []Match) map[string]int {
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// spenders are the customers whose total spend is above $0: what their
|
||||
// paid invoices charged (amount_paid, so $0 and fully discounted invoices
|
||||
// count as nothing), less what was refunded. Customers who only had a
|
||||
// trial, or have only open invoices, aren't customers yet.
|
||||
func spenders(d *db.DB) (map[string]bool, error) {
|
||||
type row struct {
|
||||
kind, customer, data string
|
||||
amount int64
|
||||
}
|
||||
rows, err := d.Query(`SELECT kind, customer, amount, data FROM stripe_objects
|
||||
WHERE customer != '' AND ((kind = 'invoice' AND status = 'paid') OR (kind = 'refund' AND status IN ('succeeded', 'pending')))`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var all []row
|
||||
for rows.Next() {
|
||||
var r row
|
||||
if err := rows.Scan(&r.kind, &r.customer, &r.amount, &r.data); err != nil {
|
||||
rows.Close()
|
||||
return nil, err
|
||||
}
|
||||
all = append(all, r)
|
||||
}
|
||||
rows.Close()
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
spend := map[string]int64{}
|
||||
for _, r := range all {
|
||||
if r.kind == KindRefund {
|
||||
spend[r.customer] -= r.amount
|
||||
continue
|
||||
}
|
||||
var inv struct {
|
||||
AmountPaid int64 `json:"amount_paid"`
|
||||
}
|
||||
_ = json.Unmarshal([]byte(r.data), &inv)
|
||||
spend[r.customer] += inv.AmountPaid
|
||||
}
|
||||
out := map[string]bool{}
|
||||
for c, v := range spend {
|
||||
if v > 0 {
|
||||
out[c] = true
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user