Skip to content

g1t/services/billing/migrations/0039_discounts_not_comped.sql

19 lines1,353 bytesCodeBlame
1-- "Comped" becomes what it always was: a 100% discount on custom terms,
2-- with its note as the reason and its ceiling as the monthly budget at
3-- cost. Code reads a leftover `comped` row as 100% too.
4UPDATE billing_accounts SET terms_kind = 'custom', discount_percent = 100 WHERE terms_kind = 'comped';
5
6-- What a 100%-discounted workspace's usage was worth, on the entries from
7-- before discounts recorded it: charged nothing and paid by nothing, at
8-- cost plus the margin (MARGIN_PERCENT, 20), rounded up as `margin_on`
9-- does. So its statement shows every line at its price and the discount
10-- beside it. The reconciliation counts these workspaces' usage as given
11-- from their cost, not from this column, so margins do not move.
12UPDATE ledger SET discount_micros = (cost_micros * 120 + 99) / 100
13 WHERE kind = 'usage' AND COALESCE(billed_to, 'g1t') = 'g1t' AND amount_micros = 0 AND COALESCE(cost_micros, 0) > 0
14 AND discount_micros = 0 AND credit_micros = 0 AND trial_micros = 0 AND oss_micros = 0 AND given_micros = 0
15 AND workspace IN (
16 SELECT substr(id, 4) FROM billing_accounts WHERE kind = 'workspace' AND terms_kind = 'custom' AND discount_percent >= 100
17 UNION SELECT m.workspace FROM account_members m JOIN billing_accounts b ON b.id = m.account_id
18 WHERE b.terms_kind = 'custom' AND b.discount_percent >= 100
19 );