fsanyoto commited on
Commit
45ed984
Β·
verified Β·
1 Parent(s): e90881a

Deploy Data Management Platform (rename + sales forecast removed)

Browse files
Files changed (4) hide show
  1. aios_grid_fields.json +8 -0
  2. app.py +34 -1
  3. modules/ar.py +77 -0
  4. modules/customer_data.py +34 -14
aios_grid_fields.json CHANGED
@@ -196,6 +196,14 @@
196
  "default": false,
197
  "note": "Forward exposure: draft invoices + open + overdue + confirmed-but-uninvoiced order value. The number to check before accepting the next order. Odoo's credit_limit is set on 20 of 1,548 customers, so exposure is the answer, not headroom."
198
  },
 
 
 
 
 
 
 
 
199
  {
200
  "key": "top_category",
201
  "label": "Top category",
 
196
  "default": false,
197
  "note": "Forward exposure: draft invoices + open + overdue + confirmed-but-uninvoiced order value. The number to check before accepting the next order. Odoo's credit_limit is set on 20 of 1,548 customers, so exposure is the answer, not headroom."
198
  },
199
+ {
200
+ "key": "days_to_pay",
201
+ "label": "Days to pay",
202
+ "type": "int",
203
+ "source": "odoo",
204
+ "default": false,
205
+ "note": "Average days from invoice to SETTLEMENT (the date the receivable was fully reconciled, not the payment document date), over the last 3 years. Refreshed on a NIGHTLY snapshot rather than per page load β€” computing it takes ~5 minutes, so the table reads the answer. Blank means no fully-settled invoice in the window, or the first snapshot has not run yet; it is never 0, because 'pays in 0 days' would be a false claim."
206
+ },
207
  {
208
  "key": "top_category",
209
  "label": "Top category",
app.py CHANGED
@@ -7719,6 +7719,17 @@ def maybe_sync_store(force=False):
7719
  if all(v.get('phase') == 'live' for v in res.values()):
7720
  break
7721
  s['error'] = None
 
 
 
 
 
 
 
 
 
 
 
7722
  except Exception as e:
7723
  s['error'] = str(e)[:300]
7724
  _tel.error('store-sync', e)
@@ -7822,6 +7833,24 @@ def _nav_goto(nav_cur, wf, param):
7822
  _sync_page_param(param)
7823
 
7824
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7825
  def _rail_refresh():
7826
  """Footer 'Refresh data', reachable from the rail. An on_click callback already reruns the
7827
  script, so this must NOT call st.rerun() β€” that would be the second full run the grid's
@@ -7829,6 +7858,7 @@ def _rail_refresh():
7829
  st.cache_data.clear()
7830
  prewarm_reset()
7831
  maybe_sync_store(force=True)
 
7832
 
7833
 
7834
  def _nav_rail():
@@ -8064,10 +8094,13 @@ def main():
8064
  if st.button('Refresh data', width='stretch', icon=':material/refresh:',
8065
  help='Pull everything fresh from Odoo in the background β€” the '
8066
  'dashboards keep serving the current data and switch to '
8067
- 'the new pull as each module completes.'):
 
 
8068
  st.cache_data.clear()
8069
  prewarm_reset()
8070
  maybe_sync_store(force=True)
 
8071
  st.rerun()
8072
  st.button('Settings', width='stretch', icon=':material/settings:',
8073
  on_click=_nav_sentinel, args=('__settings__', 'settings'),
 
7719
  if all(v.get('phase') == 'live' for v in res.values()):
7720
  break
7721
  s['error'] = None
7722
+ # The NIGHTLY days-to-pay snapshot (owner, 2026-07-27), on the back of the pass that
7723
+ # is already off the render path. It costs ~300s and refreshes at most once every
7724
+ # 20h, so in the steady state this is a store read that returns 'fresh' instantly.
7725
+ # Deliberately AFTER the sync and inside the same try: if it fails, the column goes
7726
+ # blank and the store sync above has already succeeded β€” a derived statistic must
7727
+ # never be able to take the data layer down with it.
7728
+ try:
7729
+ import modules.ar as _ar_mod
7730
+ _ar_mod.refresh_days_to_pay_snapshot()
7731
+ except Exception as _dtp_err:
7732
+ _tel.error('dtp-snapshot', _dtp_err)
7733
  except Exception as e:
7734
  s['error'] = str(e)[:300]
7735
  _tel.error('store-sync', e)
 
7833
  _sync_page_param(param)
7834
 
7835
 
7836
+ def _refresh_dtp_async():
7837
+ """Kick the days-to-pay snapshot on a DAEMON THREAD, forced past its 20h stale gate.
7838
+
7839
+ Owner 2026-07-27: *"make sure the data refresh with a Refresh data button too."* The snapshot
7840
+ costs ~5 minutes, so doing it inline would freeze the click β€” and "Refresh data" that hangs
7841
+ the app for five minutes is worse than one that quietly takes a while. Off-thread, the button
7842
+ stays instant, every other cache clears immediately, and this column catches up on its own.
7843
+ Forced, because a manual refresh means "now", not "if it happens to be stale".
7844
+ """
7845
+ def _go():
7846
+ try:
7847
+ import modules.ar as _ar_mod
7848
+ _ar_mod.refresh_days_to_pay_snapshot(force=True)
7849
+ except Exception as _e:
7850
+ _tel.error('dtp-snapshot-manual', _e)
7851
+ _threading.Thread(target=_go, daemon=True, name='dtp-refresh').start()
7852
+
7853
+
7854
  def _rail_refresh():
7855
  """Footer 'Refresh data', reachable from the rail. An on_click callback already reruns the
7856
  script, so this must NOT call st.rerun() β€” that would be the second full run the grid's
 
7858
  st.cache_data.clear()
7859
  prewarm_reset()
7860
  maybe_sync_store(force=True)
7861
+ _refresh_dtp_async()
7862
 
7863
 
7864
  def _nav_rail():
 
8094
  if st.button('Refresh data', width='stretch', icon=':material/refresh:',
8095
  help='Pull everything fresh from Odoo in the background β€” the '
8096
  'dashboards keep serving the current data and switch to '
8097
+ 'the new pull as each module completes. Days to pay is the one '
8098
+ 'slow statistic (~5 min); it refreshes on its own thread and the '
8099
+ 'column updates when it lands.'):
8100
  st.cache_data.clear()
8101
  prewarm_reset()
8102
  maybe_sync_store(force=True)
8103
+ _refresh_dtp_async()
8104
  st.rerun()
8105
  st.button('Settings', width='stretch', icon=':material/settings:',
8106
  on_click=_nav_sentinel, args=('__settings__', 'settings'),
modules/ar.py CHANGED
@@ -12,9 +12,86 @@ from pathlib import Path
12
  sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
13
  import core.odoo as O
14
  import core.periods as P
 
15
 
16
  BUCKETS = ['Current', '1-30', '31-60', '61-90', '90+']
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
  def _bucket(days):
20
  if days <= 0:
 
12
  sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
13
  import core.odoo as O
14
  import core.periods as P
15
+ import core.store as store
16
 
17
  BUCKETS = ['Current', '1-30', '31-60', '61-90', '90+']
18
 
19
+ #: Store key for the NIGHTLY days-to-pay snapshot (owner, 2026-07-27).
20
+ #:
21
+ #: `days_to_pay()` costs ~300s β€” it walks three years of reconciled receivable lines and their
22
+ #: full-reconcile groups to find each invoice's SETTLEMENT date. That is fine for a page somebody
23
+ #: opens on purpose, and unacceptable inside `customer_data.pool()`, which every Customer-table
24
+ #: render and every Space container start depends on. So the expensive half runs on a schedule
25
+ #: and the table reads the ANSWER.
26
+ #:
27
+ #: Shape: {'computed': 'YYYY-MM-DD HH:MM', 'years': 3, 'days': {'<pid>': avg_days|None}}.
28
+ #: JSON object keys are strings, which is why the reader coerces back to int.
29
+ DTP_KEY = 'ar_days_to_pay'
30
+ #: Refresh when the snapshot is older than this. 20h rather than 24 so a daily cadence never
31
+ #: skips a day by drifting a few minutes later each time.
32
+ DTP_MAX_AGE_HOURS = 20
33
+
34
+
35
+ def days_to_pay_snapshot():
36
+ """`{pid: avg_days}` from the last snapshot β€” CHEAP, and it never raises.
37
+
38
+ Returns `{}` when the snapshot is absent, unreadable or malformed. A blank column is the
39
+ correct degradation: the alternative is a Customer table that will not render because a
40
+ derived statistic is missing, and `days_to_pay` is a statistic, not a fact the page is about.
41
+ """
42
+ try:
43
+ snap = store.get(DTP_KEY) or {}
44
+ days = snap.get('days') or {}
45
+ out = {}
46
+ for k, v in days.items():
47
+ try:
48
+ out[int(k)] = None if v is None else float(v)
49
+ except (TypeError, ValueError):
50
+ continue
51
+ return out
52
+ except Exception:
53
+ return {}
54
+
55
+
56
+ def days_to_pay_computed_at():
57
+ """When the snapshot was taken ('' if there is none) β€” so the column can say how old it is."""
58
+ try:
59
+ return str((store.get(DTP_KEY) or {}).get('computed') or '')
60
+ except Exception:
61
+ return ''
62
+
63
+
64
+ def refresh_days_to_pay_snapshot(max_age_hours=DTP_MAX_AGE_HOURS, t=None, force=False):
65
+ """Recompute + persist the snapshot IF it is stale. Returns what it did, and never raises.
66
+
67
+ Called from the app's existing background store-sync thread, so the ~300s cost is paid off
68
+ the render path by whichever container happens to be awake. Deliberately NOT called from
69
+ `pool()`: a lazy compute would just move the five minutes onto whoever opened the page first.
70
+
71
+ ⚠ Writes through `store.put` on a key nothing else owns. It is a DERIVED artifact β€” losing it
72
+ costs one recompute and nothing else, which is why it is safe to overwrite wholesale rather
73
+ than read-modify-write.
74
+ """
75
+ if not store.available():
76
+ return {'skipped': 'no store'}
77
+ try:
78
+ if not force:
79
+ at = days_to_pay_computed_at()
80
+ if at:
81
+ age = dt.datetime.now() - dt.datetime.strptime(at, '%Y-%m-%d %H:%M')
82
+ if age < dt.timedelta(hours=max_age_hours):
83
+ return {'skipped': 'fresh', 'age_h': round(age.total_seconds() / 3600, 1)}
84
+ except Exception:
85
+ pass # an unparseable stamp means "recompute", never "crash"
86
+ try:
87
+ res = days_to_pay(t)
88
+ days = {str(r['pid']): r.get('avg_days') for r in (res.get('_all_rows') or [])}
89
+ store.put(DTP_KEY, {'computed': dt.datetime.now().strftime('%Y-%m-%d %H:%M'),
90
+ 'years': 3, 'days': days})
91
+ return {'refreshed': len(days)}
92
+ except Exception as e:
93
+ return {'error': str(e)[:200]}
94
+
95
 
96
  def _bucket(days):
97
  if days <= 0:
modules/customer_data.py CHANGED
@@ -67,13 +67,14 @@ def _ar_attrs(t=None):
67
  ⚠ `credit_limit` is deliberately NOT a column: 20 of 1,548 customers have one. EXPOSURE is
68
  the number that answers "how much are we out on this customer", and it needs no limit.
69
 
70
- ⚠ `days_to_pay` is deliberately NOT here either, and that is a COST decision, not an
71
- oversight. MEASURED 2026-07-27: `ar.credit_exposure()` costs 9s; `ar.days_to_pay()` costs
72
- **301s**, because settlement date means walking three years of reconciled receivable lines
73
- and their full-reconcile groups. Putting it in `pool()` would add five minutes to every
74
- Customer-table build β€” including every Space container start. It already exists, per
75
- customer, on Collections β†’ payment behaviour. Getting it onto this table needs a nightly
76
- precompute into the store, which is its own piece of work, not a column.
 
77
  """
78
  out = {}
79
  try:
@@ -86,10 +87,14 @@ def _ar_attrs(t=None):
86
  }
87
  except Exception:
88
  pass
 
 
 
 
89
  return out
90
 
91
 
92
- def _mix_attrs(pids=None, team_id=None, t=None):
93
  """{pid: {top_category, sku_count, top_sku, top_category_pct}} β€” the product-mix family.
94
 
95
  ONE read_group over sale.order.line by (partner, product) across the LTM window, then the
@@ -100,13 +105,23 @@ def _mix_attrs(pids=None, team_id=None, t=None):
100
 
101
  `top_category_pct` is that category's share of the customer's LTM line revenue β€” the number
102
  that says whether "top" means dominant or merely first.
 
 
 
 
 
 
103
  """
104
  t = t or P.today()
105
  mf, mt = P.ltm(t)
 
 
 
 
 
106
  dom = [('order_id.state', 'in', ['sale', 'done']),
107
  ('order_id.date_order', '>=', str(mf)), ('order_id.date_order', '<=', str(mt))]
108
- if pids:
109
- dom.append(('order_partner_id', 'in', list(pids)))
110
  rows = O.read_group('sale.order.line', dom, ['price_subtotal:sum'],
111
  ['order_partner_id', 'product_id'], lazy=False)
112
  prod_ids = {O.m2o_id(r.get('product_id')) for r in rows if r.get('product_id')}
@@ -127,7 +142,7 @@ def _mix_attrs(pids=None, team_id=None, t=None):
127
  for r in rows:
128
  pid = O.m2o_id(r.get('order_partner_id'))
129
  prod = O.m2o_id(r.get('product_id'))
130
- if pid is None or prod is None:
131
  continue
132
  val = r.get('price_subtotal', 0.0) or 0.0
133
  e = per.setdefault(pid, {'skus': set(), 'by_cat': {}, 'by_sku': {}, 'total': 0.0})
@@ -172,13 +187,17 @@ def _salesperson_attrs(pids=None, t=None):
172
  """
173
  t = t or P.today()
174
  mf, mt = P.ltm(t)
175
- dom = S.order_domain(str(mf), str(mt), None, partner_ids=list(pids) if pids else None)
 
 
 
 
176
  rows = O.read_group('sale.order', dom, ['amount_untaxed:sum'],
177
  ['partner_id', 'user_id'], lazy=False)
178
  per = {}
179
  for r in rows:
180
  pid = O.m2o_id(r.get('partner_id'))
181
- if pid is None:
182
  continue
183
  who = O.m2o_name(r.get('user_id')) or '(none)'
184
  n, rev = r.get('__count', 0) or 0, r.get('amount_untaxed', 0.0) or 0.0
@@ -209,7 +228,7 @@ def pool(agent_name=None, team_id=None):
209
  # raising: a customer table that cannot render because AR is momentarily unreachable is a
210
  # worse failure than one with a blank column.
211
  ar_a = _ar_attrs(t)
212
- mix_a = _mix_attrs(all_pids, team_id, t)
213
  sp_a = _salesperson_attrs(all_pids, t)
214
  # customers whose only activity sits in the older half of the 24-month cadence window
215
  # appear in no revenue map β€” fetch their names directly (else the lapsed cohort, exactly
@@ -255,6 +274,7 @@ def pool(agent_name=None, team_id=None):
255
  'ar_open': (ar_a.get(pid) or {}).get('ar_open', 0.0),
256
  'ar_overdue': (ar_a.get(pid) or {}).get('ar_overdue', 0.0),
257
  'ar_exposure': (ar_a.get(pid) or {}).get('ar_exposure', 0.0),
 
258
  # Product mix, LTM, line grain, ALL-channel.
259
  'top_category': (mix_a.get(pid) or {}).get('top_category', '(none)'),
260
  'top_category_pct': (mix_a.get(pid) or {}).get('top_category_pct'),
 
67
  ⚠ `credit_limit` is deliberately NOT a column: 20 of 1,548 customers have one. EXPOSURE is
68
  the number that answers "how much are we out on this customer", and it needs no limit.
69
 
70
+ ⚠ `days_to_pay` is NOT COMPUTED here β€” it is READ from a nightly snapshot. MEASURED
71
+ 2026-07-27: `ar.credit_exposure()` costs 9s; `ar.days_to_pay()` costs **301s**, because
72
+ settlement date means walking three years of reconciled receivable lines and their
73
+ full-reconcile groups. Computing it in `pool()` would add five minutes to every Customer-table
74
+ build and every Space container start; computing it LAZILY would just move those five minutes
75
+ onto whoever opened the page first. So the expensive half runs on the app's existing
76
+ background store-sync thread (`ar.refresh_days_to_pay_snapshot`) and this reads the answer
77
+ out of the store for nothing. A missing snapshot yields a blank column, never a slow page.
78
  """
79
  out = {}
80
  try:
 
87
  }
88
  except Exception:
89
  pass
90
+ for pid, avg in ar.days_to_pay_snapshot().items():
91
+ # A customer with an AR row but no settled invoice in the window keeps `None` β€” blank,
92
+ # not 0. "Pays in 0 days" is a claim, and a false one.
93
+ out.setdefault(pid, {})['days_to_pay'] = avg
94
  return out
95
 
96
 
97
+ def _mix_attrs(pids=None, t=None):
98
  """{pid: {top_category, sku_count, top_sku, top_category_pct}} β€” the product-mix family.
99
 
100
  ONE read_group over sale.order.line by (partner, product) across the LTM window, then the
 
105
 
106
  `top_category_pct` is that category's share of the customer's LTM line revenue β€” the number
107
  that says whether "top" means dominant or merely first.
108
+
109
+ ⚠ There is NO `team_id` parameter, and its absence is the point rather than an omission: for
110
+ a BU-scoped viewer the revenue columns beside this one ARE team-scoped while these are not,
111
+ so "top category" can name products bought through a channel that viewer's revenue figures
112
+ exclude. That is the same trade the inventory modules make, and it is the right one β€” a
113
+ customer's product mix is a fact about the customer.
114
  """
115
  t = t or P.today()
116
  mf, mt = P.ltm(t)
117
+ # ⚠ `pids` NARROWS THE RESULT, NEVER THE DOMAIN β€” and that is not a style choice. Putting
118
+ # the caller's ~1,500 partner ids into an `in` clause on a LINE-grain read_group makes Odoo
119
+ # answer **502 Bad Gateway**; the same query unfiltered returns in 22s. Measured 2026-07-27,
120
+ # after the filtered form took the live Customer page past 20 MINUTES without rendering.
121
+ # The grouped result is per-partner anyway, so dropping the extras in Python costs nothing.
122
  dom = [('order_id.state', 'in', ['sale', 'done']),
123
  ('order_id.date_order', '>=', str(mf)), ('order_id.date_order', '<=', str(mt))]
124
+ keep = set(pids) if pids else None
 
125
  rows = O.read_group('sale.order.line', dom, ['price_subtotal:sum'],
126
  ['order_partner_id', 'product_id'], lazy=False)
127
  prod_ids = {O.m2o_id(r.get('product_id')) for r in rows if r.get('product_id')}
 
142
  for r in rows:
143
  pid = O.m2o_id(r.get('order_partner_id'))
144
  prod = O.m2o_id(r.get('product_id'))
145
+ if pid is None or prod is None or (keep is not None and pid not in keep):
146
  continue
147
  val = r.get('price_subtotal', 0.0) or 0.0
148
  e = per.setdefault(pid, {'skus': set(), 'by_cat': {}, 'by_sku': {}, 'total': 0.0})
 
187
  """
188
  t = t or P.today()
189
  mf, mt = P.ltm(t)
190
+ # Same rule as `_mix_attrs`: the pid list narrows the RESULT, not the domain. Order grain is
191
+ # far smaller than line grain so this one never actually 502'd, but a caller's id list has no
192
+ # business being pushed into a query whose grouping already answers per partner.
193
+ keep = set(pids) if pids else None
194
+ dom = S.order_domain(str(mf), str(mt), None)
195
  rows = O.read_group('sale.order', dom, ['amount_untaxed:sum'],
196
  ['partner_id', 'user_id'], lazy=False)
197
  per = {}
198
  for r in rows:
199
  pid = O.m2o_id(r.get('partner_id'))
200
+ if pid is None or (keep is not None and pid not in keep):
201
  continue
202
  who = O.m2o_name(r.get('user_id')) or '(none)'
203
  n, rev = r.get('__count', 0) or 0, r.get('amount_untaxed', 0.0) or 0.0
 
228
  # raising: a customer table that cannot render because AR is momentarily unreachable is a
229
  # worse failure than one with a blank column.
230
  ar_a = _ar_attrs(t)
231
+ mix_a = _mix_attrs(all_pids, t)
232
  sp_a = _salesperson_attrs(all_pids, t)
233
  # customers whose only activity sits in the older half of the 24-month cadence window
234
  # appear in no revenue map β€” fetch their names directly (else the lapsed cohort, exactly
 
274
  'ar_open': (ar_a.get(pid) or {}).get('ar_open', 0.0),
275
  'ar_overdue': (ar_a.get(pid) or {}).get('ar_overdue', 0.0),
276
  'ar_exposure': (ar_a.get(pid) or {}).get('ar_exposure', 0.0),
277
+ 'days_to_pay': (ar_a.get(pid) or {}).get('days_to_pay'),
278
  # Product mix, LTM, line grain, ALL-channel.
279
  'top_category': (mix_a.get(pid) or {}).get('top_category', '(none)'),
280
  'top_category_pct': (mix_a.get(pid) or {}).get('top_category_pct'),