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:
@@ -184,7 +184,7 @@ Throughout, also notice:
|
|||||||
|
|
||||||
### Sources
|
### Sources
|
||||||
|
|
||||||
- [ ] *Fetch* on a Market entry finds new items from its feeds and
|
- [x] *Fetch* on a Market entry finds new items from its feeds and
|
||||||
sitemap, and the list shows when it was last checked.
|
sitemap, and the list shows when it was last checked.
|
||||||
- [x] Items can be dismissed and restored.
|
- [x] Items can be dismissed and restored.
|
||||||
- [ ] Your own products (`/content/own`) show your sites' and feeds' new
|
- [ ] Your own products (`/content/own`) show your sites' and feeds' new
|
||||||
|
|||||||
@@ -151,7 +151,8 @@ func manual(d *db.DB) (map[string]string, error) {
|
|||||||
return out, rows.Err()
|
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
|
// 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
|
// Account's or one of its Contacts'), then the email's domain (not free
|
||||||
// mail). Paying customers come first, by MRR.
|
// mail). Paying customers come first, by MRR.
|
||||||
@@ -168,19 +169,12 @@ func Matches(d *db.DB, model *Model) ([]Match, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
billed := map[string]bool{}
|
paying, err := spenders(d)
|
||||||
rows, err := d.Query(`SELECT DISTINCT customer FROM stripe_objects WHERE kind = 'invoice' AND status IN ('paid', 'open')`)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
for rows.Next() {
|
|
||||||
var c string
|
|
||||||
rows.Scan(&c)
|
|
||||||
billed[c] = true
|
|
||||||
}
|
|
||||||
rows.Close()
|
|
||||||
prospects := map[string]string{}
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -217,7 +211,7 @@ func Matches(d *db.DB, model *Model) ([]Match, error) {
|
|||||||
}
|
}
|
||||||
var out []Match
|
var out []Match
|
||||||
for _, c := range custs {
|
for _, c := range custs {
|
||||||
if !billed[c.ID] {
|
if !paying[c.ID] {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
m := Match{Customer: c.Customer, MRR: now[c.ID], Prospect: prospects[c.ID]}
|
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
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -39,9 +39,22 @@ func TestMatches(t *testing.T) {
|
|||||||
} {
|
} {
|
||||||
exec(`INSERT INTO stripe_objects (kind, id, customer, data) VALUES ('customer', ?, ?, ?)`, c.id, c.id, c.data)
|
exec(`INSERT INTO stripe_objects (kind, id, customer, data) VALUES ('customer', ?, ?, ?)`, c.id, c.id, c.data)
|
||||||
if c.id != "cus_unbilled" {
|
if c.id != "cus_unbilled" {
|
||||||
exec(`INSERT INTO stripe_objects (kind, id, customer, status, currency, data) VALUES ('invoice', ?, ?, 'paid', 'usd', '{}')`, "in_"+c.id, c.id)
|
exec(`INSERT INTO stripe_objects (kind, id, customer, status, currency, data) VALUES ('invoice', ?, ?, 'paid', 'usd', '{"amount_paid":1000}')`, "in_"+c.id, c.id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Customers who never spent anything aren't listed: a $0 trial invoice,
|
||||||
|
// only an open invoice, or a payment refunded in full.
|
||||||
|
for _, c := range []struct{ id, status, data string }{
|
||||||
|
{"cus_trial", "paid", `{"amount_paid":0}`},
|
||||||
|
{"cus_open", "open", `{"amount_paid":0}`},
|
||||||
|
{"cus_refunded", "paid", `{"amount_paid":500}`},
|
||||||
|
} {
|
||||||
|
exec(`INSERT INTO stripe_objects (kind, id, customer, data) VALUES ('customer', ?, ?, '{"name":"X","email":"x@x.example"}')`, c.id, c.id)
|
||||||
|
exec(`INSERT INTO stripe_objects (kind, id, customer, status, currency, data) VALUES ('invoice', ?, ?, ?, 'usd', ?)`, "in_"+c.id, c.id, c.status, c.data)
|
||||||
|
}
|
||||||
|
exec(`INSERT INTO stripe_objects (kind, id, customer, status, currency, amount, data) VALUES ('refund', 're_1', 'cus_refunded', 'succeeded', 'usd', 500, '{}')`)
|
||||||
|
// A partial refund leaves them a customer.
|
||||||
|
exec(`INSERT INTO stripe_objects (kind, id, customer, status, currency, amount, data) VALUES ('refund', 're_2', 'cus_beta', 'succeeded', 'usd', 400, '{}')`)
|
||||||
exec(`INSERT INTO crm_actions (key, kind, entity, fields, status, espo_id) VALUES ('stripe-customer:cus_new', 'create', 'Account', '{"name":"Newco"}', 'done', 'a9')`)
|
exec(`INSERT INTO crm_actions (key, kind, entity, fields, status, espo_id) VALUES ('stripe-customer:cus_new', 'create', 'Account', '{"name":"Newco"}', 'done', 'a9')`)
|
||||||
|
|
||||||
ms, err := Matches(d, nil)
|
ms, err := Matches(d, nil)
|
||||||
@@ -52,8 +65,10 @@ func TestMatches(t *testing.T) {
|
|||||||
for _, m := range ms {
|
for _, m := range ms {
|
||||||
got[m.Customer.ID] = m
|
got[m.Customer.ID] = m
|
||||||
}
|
}
|
||||||
if _, ok := got["cus_unbilled"]; ok {
|
for _, id := range []string{"cus_unbilled", "cus_trial", "cus_open", "cus_refunded"} {
|
||||||
t.Error("a customer never billed is listed")
|
if _, ok := got[id]; ok {
|
||||||
|
t.Errorf("%s spent nothing, but is listed", id)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for id, want := range map[string]struct{ account, how string }{
|
for id, want := range map[string]struct{ account, how string }{
|
||||||
"cus_ann": {"a1", MatchEmail}, "cus_beta": {"a2", MatchStoredID}, "cus_meta": {"a1", MatchStoredID},
|
"cus_ann": {"a1", MatchEmail}, "cus_beta": {"a2", MatchStoredID}, "cus_meta": {"a1", MatchStoredID},
|
||||||
|
|||||||
@@ -116,7 +116,7 @@ func TestCustomersPage(t *testing.T) {
|
|||||||
s.DB.Exec(`INSERT INTO crm_records (entity, espo_id, name, email, domain, data, synced_at) VALUES ('Account', 'a1', 'Acme', '', 'acme.example', '{}', '')`)
|
s.DB.Exec(`INSERT INTO crm_records (entity, espo_id, name, email, domain, data, synced_at) VALUES ('Account', 'a1', 'Acme', '', 'acme.example', '{}', '')`)
|
||||||
s.DB.Exec(`INSERT INTO stripe_objects (kind, id, customer, data) VALUES ('customer', 'cus_x', 'cus_x', '{"name":"Xco","email":"x@xco.example"}'),
|
s.DB.Exec(`INSERT INTO stripe_objects (kind, id, customer, data) VALUES ('customer', 'cus_x', 'cus_x', '{"name":"Xco","email":"x@xco.example"}'),
|
||||||
('customer', 'cus_a', 'cus_a', '{"name":"Acme Ltd","email":"pay@acme.example"}')`)
|
('customer', 'cus_a', 'cus_a', '{"name":"Acme Ltd","email":"pay@acme.example"}')`)
|
||||||
s.DB.Exec(`INSERT INTO stripe_objects (kind, id, customer, status, currency, data) VALUES ('invoice', 'in_1', 'cus_x', 'paid', 'usd', '{}'), ('invoice', 'in_2', 'cus_a', 'paid', 'usd', '{}')`)
|
s.DB.Exec(`INSERT INTO stripe_objects (kind, id, customer, status, currency, data) VALUES ('invoice', 'in_1', 'cus_x', 'paid', 'usd', '{"amount_paid":1000}'), ('invoice', 'in_2', 'cus_a', 'paid', 'usd', '{"amount_paid":1000}')`)
|
||||||
body := do(h, http.MethodGet, "/metrics/customers", nil, nil).Body.String()
|
body := do(h, http.MethodGet, "/metrics/customers", nil, nil).Body.String()
|
||||||
if !strings.Contains(body, "Xco") || strings.Contains(body, "Acme Ltd") {
|
if !strings.Contains(body, "Xco") || strings.Contains(body, "Acme Ltd") {
|
||||||
t.Fatal("the to-fix list should hold only the unmatched customer")
|
t.Fatal("the to-fix list should hold only the unmatched customer")
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{{define "content"}}{{with .Data}}{{$d := .}}
|
{{define "content"}}{{with .Data}}{{$d := .}}
|
||||||
<div class="heading"><h1>Customers and the CRM</h1></div>
|
<div class="heading"><h1>Customers and the CRM</h1></div>
|
||||||
<p class="lede">Each Stripe customer with billing, matched to its EspoCRM Account: first by a stored ID (a Stripe customer ID in the Account, or an EspoCRM ID in Stripe's metadata), then by email (the Account's, or one of its Contacts'), then by the email's domain (free mail like Gmail doesn't count). Fix what it gets wrong; your choice is kept. Pipeline influence and per-account numbers build on this.</p>
|
<p class="lede">Each Stripe customer who has spent more than $0 (paid invoices, less refunds; trials and unpaid invoices don't count), matched to its EspoCRM Account: first by a stored ID (a Stripe customer ID in the Account, or an EspoCRM ID in Stripe's metadata), then by email (the Account's, or one of its Contacts'), then by the email's domain (free mail like Gmail doesn't count). Fix what it gets wrong; your choice is kept. Pipeline influence and per-account numbers build on this.</p>
|
||||||
|
|
||||||
<section class="card">
|
<section class="card">
|
||||||
{{if not .CRMCount}}
|
{{if not .CRMCount}}
|
||||||
|
|||||||
Reference in New Issue
Block a user