fsanyoto commited on
Commit
f811b22
·
verified ·
1 Parent(s): f45c4c2

Deploy AIOS web (React glide grid + FastAPI slice)

Browse files
RELEASES.json CHANGED
@@ -1,4 +1,4 @@
1
  {
2
- "current": "ad5cbc72",
3
  "releases": []
4
  }
 
1
  {
2
+ "current": "31302488a",
3
  "releases": []
4
  }
VERSION CHANGED
@@ -1 +1 @@
1
- ad5cbc72
 
1
+ 31302488a
api/test_odoo_rank_refresh.py CHANGED
@@ -72,6 +72,10 @@ def _with_collaborators(source, nodes, ns):
72
  return have
73
 
74
 
 
 
 
 
75
  class RankRefreshTests(unittest.TestCase):
76
  def setUp(self):
77
  self.stack = ExitStack()
@@ -195,7 +199,7 @@ class RankRefreshTests(unittest.TestCase):
195
  clock = SimpleNamespace(sleep=sleep, time=lambda: 100 + cycle[0] * 60,
196
  strftime=lambda *_: 'synthetic-time', gmtime=lambda: ())
197
  ns = dict(RESYNC_STATE={'consecutiveFailures': 0}, _RESYNC_NOTICE=[None],
198
- _configured_mirror_entities=lambda *_: None,
199
  _resolve_resync_cadence=lambda: dict(resolvedSeconds=60, manual=False,
200
  tenant='synthetic', tenantSource='test', tenantSeconds=60,
201
  envSeconds=60, disagreement=None),
@@ -480,7 +484,7 @@ class RankRefreshTests(unittest.TestCase):
480
 
481
  # ── HOTFIX 2026-09-29 (owner): "only sync daily at the fastest", and only what changed ──────
482
  def run_daily_loop(self, memory, current, *, now=10_000.0, cadence=86400, manual=False,
483
- errors=(), max_sleeps=1):
484
  """Drive the real loop with a persisted memory; stop at its `max_sleeps`-th wait."""
485
  source = ast.parse(Path(__file__).with_name('main.py').read_text(encoding='utf-8'))
486
  functions = {'_store_resync_loop', '_resync_rebuild_reason',
@@ -518,6 +522,7 @@ class RankRefreshTests(unittest.TestCase):
518
  strftime=lambda *_: 'synthetic-time', gmtime=lambda: ())
519
  ns = dict(RESYNC_STATE={'consecutiveFailures': 0}, _RESYNC_NOTICE=[None],
520
  _configured_mirror_entities=lambda *_: None,
 
521
  _resolve_resync_cadence=lambda: dict(
522
  resolvedSeconds=cadence, manual=manual, tenant='synthetic',
523
  tenantSource='test', tenantSeconds=cadence, envSeconds=cadence,
@@ -538,13 +543,18 @@ class RankRefreshTests(unittest.TestCase):
538
  import harness
539
  from core import connector_sync
540
  wake = SimpleNamespace(wait=sleep, clear=lambda: None)
 
541
  with patch.dict(sys.modules, {'time': clock, 'harness.runtime': runtime}), \
542
  patch.object(harness, 'runtime', runtime, create=True), \
543
  patch.object(connector_sync, 'sync_wake_event', return_value=wake), \
544
  patch.object(connector_sync, 'take_sync_requests', return_value=[]), \
545
- redirect_stdout(io.StringIO()):
546
- with self.assertRaisesRegex(StopIteration, 'bounded synthetic'):
547
- ns['_store_resync_loop']()
 
 
 
 
548
  self.net.assert_not_called()
549
  return events, sleeps
550
 
@@ -591,6 +601,25 @@ class RankRefreshTests(unittest.TestCase):
591
  self.assertIn(('rebuild', None), events)
592
  self.assertNotIn('fresh', [e[0] for e in events])
593
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
594
  def test_a_changed_entity_rebuilds_only_its_tables(self):
595
  events, _ = self.run_daily_loop(
596
  {'passAt': None, 'fullRebuildAt': 10_000.0 - 600,
 
72
  return have
73
 
74
 
75
+ #: W-6864: main.py's `_freeze_holds` reads this import, so every loop namespace stubs it (freeze off).
76
+ FREEZE_OFF = SimpleNamespace(enabled=lambda: False)
77
+
78
+
79
  class RankRefreshTests(unittest.TestCase):
80
  def setUp(self):
81
  self.stack = ExitStack()
 
199
  clock = SimpleNamespace(sleep=sleep, time=lambda: 100 + cycle[0] * 60,
200
  strftime=lambda *_: 'synthetic-time', gmtime=lambda: ())
201
  ns = dict(RESYNC_STATE={'consecutiveFailures': 0}, _RESYNC_NOTICE=[None],
202
+ _configured_mirror_entities=lambda *_: None, freeze_notice=FREEZE_OFF,
203
  _resolve_resync_cadence=lambda: dict(resolvedSeconds=60, manual=False,
204
  tenant='synthetic', tenantSource='test', tenantSeconds=60,
205
  envSeconds=60, disagreement=None),
 
484
 
485
  # ── HOTFIX 2026-09-29 (owner): "only sync daily at the fastest", and only what changed ──────
486
  def run_daily_loop(self, memory, current, *, now=10_000.0, cadence=86400, manual=False,
487
+ errors=(), max_sleeps=1, frozen=False):
488
  """Drive the real loop with a persisted memory; stop at its `max_sleeps`-th wait."""
489
  source = ast.parse(Path(__file__).with_name('main.py').read_text(encoding='utf-8'))
490
  functions = {'_store_resync_loop', '_resync_rebuild_reason',
 
522
  strftime=lambda *_: 'synthetic-time', gmtime=lambda: ())
523
  ns = dict(RESYNC_STATE={'consecutiveFailures': 0}, _RESYNC_NOTICE=[None],
524
  _configured_mirror_entities=lambda *_: None,
525
+ freeze_notice=SimpleNamespace(enabled=lambda: frozen),
526
  _resolve_resync_cadence=lambda: dict(
527
  resolvedSeconds=cadence, manual=manual, tenant='synthetic',
528
  tenantSource='test', tenantSeconds=cadence, envSeconds=cadence,
 
543
  import harness
544
  from core import connector_sync
545
  wake = SimpleNamespace(wait=sleep, clear=lambda: None)
546
+ out = io.StringIO()
547
  with patch.dict(sys.modules, {'time': clock, 'harness.runtime': runtime}), \
548
  patch.object(harness, 'runtime', runtime, create=True), \
549
  patch.object(connector_sync, 'sync_wake_event', return_value=wake), \
550
  patch.object(connector_sync, 'take_sync_requests', return_value=[]), \
551
+ redirect_stdout(out):
552
+ if frozen:
553
+ self.assertIsNone(ns['_store_resync_loop']())
554
+ else:
555
+ with self.assertRaisesRegex(StopIteration, 'bounded synthetic'):
556
+ ns['_store_resync_loop']()
557
+ self.loop_output = out.getvalue()
558
  self.net.assert_not_called()
559
  return events, sleeps
560
 
 
601
  self.assertIn(('rebuild', None), events)
602
  self.assertNotIn('fresh', [e[0] for e in events])
603
 
604
+ def test_maintenance_freeze_holds_the_resident_loop(self):
605
+ """W-6864: a maintenance freeze holds the resident loop before its first pass"""
606
+ # The same inputs unfrozen rebuild in full (the test above), so the stub is load-bearing.
607
+ events, sleeps = self.run_daily_loop(
608
+ {'passAt': None, 'fullRebuildAt': None, 'fingerprints': None},
609
+ {'res_partner': (3, 7)}, frozen=True)
610
+ self.assertEqual((events, sleeps), ([], []))
611
+ self.assertIn('maintenance freeze holds store resync', self.loop_output)
612
+
613
+ def test_an_unstubbed_main_import_is_a_named_fixture_gap(self):
614
+ """W-6864 NC: an unstubbed main.py import stays a named FixtureGap"""
615
+ source = ast.parse(Path(__file__).with_name('main.py').read_text(encoding='utf-8'))
616
+ holds = [node for node in source.body
617
+ if isinstance(node, ast.FunctionDef) and node.name == '_freeze_holds']
618
+ self.assertEqual(len(holds), 1)
619
+ with self.assertRaisesRegex(FixtureGap, "reads 'freeze_notice'"):
620
+ _with_collaborators(source, holds, {})
621
+ self.assertEqual(_with_collaborators(source, holds, {'freeze_notice': FREEZE_OFF}), holds)
622
+
623
  def test_a_changed_entity_rebuilds_only_its_tables(self):
624
  events, _ = self.run_daily_loop(
625
  {'passAt': None, 'fullRebuildAt': 10_000.0 - 600,
platform/modules/procurement.py CHANGED
@@ -11,7 +11,7 @@ from Odoo (read-only).
11
  Reorder math (validated on the v24.4 Backend):
12
  8-month demand = units sold same 8-month window last year (all channels)
13
  avg/day = 8M demand / window days
14
- days of cover = (on-hand + incoming PO) / avg/day
15
  reorder point = lead-time days * avg/day
16
  Reorder QTY = ceil(reorder point * 1.05) [Baseline]
17
  Reorder +trend = Reorder QTY * (YTD units this yr / last yr)
@@ -183,6 +183,13 @@ def recommendations(t=None):
183
  if not by_code:
184
  return []
185
 
 
 
 
 
 
 
 
186
  rows = []
187
  for code, p in by_code.items():
188
  pid = p['id']
@@ -198,15 +205,20 @@ def recommendations(t=None):
198
  avg_day = d8 / window_days if window_days else 0.0
199
  on_hand = p.get('qty_available') or 0.0
200
  po = inpo.get(pid, 0.0)
201
- avail = on_hand + po
202
- days_cover = (avail / avg_day) if avg_day > 0 else (None if avail <= 0 else 9999.0)
 
 
 
203
  reorder_pt = lead * avg_day
204
  rbase = math.ceil(reorder_pt * (1 + _SAFETY)) if reorder_pt > 0 else 0
205
  yn, yp = ytd_now.get(pid, 0.0), ytd_prev.get(pid, 0.0)
206
  trend = (yn / yp) if yp > 0 else None
207
  rtrend = int(round(rbase * trend)) if trend is not None else rbase
208
- buy = (days_cover is not None and lead > 0 and avg_day > 0 and days_cover < lead)
 
209
  rows.append({
 
210
  'code': code, 'product': meta.get('desc') or p.get('name') or code,
211
  'unit': meta.get('unit') or '', 'qty_per_unit': meta.get('qty_per_unit'),
212
  'vendor': meta.get('vendor') or '(no supplier)', 'country': meta.get('country') or '',
@@ -226,6 +238,10 @@ def recommendations(t=None):
226
  'cheapest_price': cheapest['price'] if cheapest else None,
227
  'alt_vendors': priced,
228
  })
 
 
 
 
229
  return rows
230
 
231
 
@@ -596,4 +612,61 @@ def validate(t=None, team_id=None):
596
  checks.append({'check': 'Vendor reliability: Σ(per-vendor PO value) == Σ(row PO value)',
597
  'a': round(vv, 2), 'b': round(rv, 2), 'gap': round(vv - rv, 2),
598
  'ok': abs(vv - rv) <= 1.0})
 
599
  return checks
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  Reorder math (validated on the v24.4 Backend):
12
  8-month demand = units sold same 8-month window last year (all channels)
13
  avg/day = 8M demand / window days
14
+ days of cover = (on-hand - FBA + incoming PO) / avg/day
15
  reorder point = lead-time days * avg/day
16
  Reorder QTY = ceil(reorder point * 1.05) [Baseline]
17
  Reorder +trend = Reorder QTY * (YTD units this yr / last yr)
 
183
  if not by_code:
184
  return []
185
 
186
+ try:
187
+ import modules.warehouse as warehouse
188
+ fba_by_pid, fba_report = warehouse.fba_qty_by_product_id(
189
+ [p['id'] for p in by_code.values()])
190
+ except Exception as exc:
191
+ fba_by_pid, fba_report = None, {
192
+ 'available': False, 'cause': 'the FBA warehouse read failed: ' + type(exc).__name__}
193
  rows = []
194
  for code, p in by_code.items():
195
  pid = p['id']
 
205
  avg_day = d8 / window_days if window_days else 0.0
206
  on_hand = p.get('qty_available') or 0.0
207
  po = inpo.get(pid, 0.0)
208
+ fba = (fba_by_pid or {}).get(pid)
209
+ fba_known = isinstance(fba, (int, float)) and fba_report.get('available') is not False
210
+ avail = on_hand - fba + po if fba_known else None
211
+ days_cover = ((avail / avg_day) if avg_day > 0 else
212
+ (None if avail <= 0 else 9999.0)) if avail is not None else None
213
  reorder_pt = lead * avg_day
214
  rbase = math.ceil(reorder_pt * (1 + _SAFETY)) if reorder_pt > 0 else 0
215
  yn, yp = ytd_now.get(pid, 0.0), ytd_prev.get(pid, 0.0)
216
  trend = (yn / yp) if yp > 0 else None
217
  rtrend = int(round(rbase * trend)) if trend is not None else rbase
218
+ buy = ((days_cover is not None and lead > 0 and avg_day > 0 and days_cover < lead)
219
+ if fba_known else None)
220
  rows.append({
221
+ '_product_id': pid, '_fba': fba,
222
  'code': code, 'product': meta.get('desc') or p.get('name') or code,
223
  'unit': meta.get('unit') or '', 'qty_per_unit': meta.get('qty_per_unit'),
224
  'vendor': meta.get('vendor') or '(no supplier)', 'country': meta.get('country') or '',
 
238
  'cheapest_price': cheapest['price'] if cheapest else None,
239
  'alt_vendors': priced,
240
  })
241
+ if not fba_known:
242
+ rows[-1]['_coverage_report'] = {**fba_report, 'available': False,
243
+ 'cause': fba_report.get('cause')
244
+ or 'FBA stock unavailable for this product'}
245
  return rows
246
 
247
 
 
612
  checks.append({'check': 'Vendor reliability: Σ(per-vendor PO value) == Σ(row PO value)',
613
  'a': round(vv, 2), 'b': round(rv, 2), 'gap': round(vv - rv, 2),
614
  'ok': abs(vv - rv) <= 1.0})
615
+ checks.extend(validate_coverage(recommendations(t=t), t=t))
616
  return checks
617
+
618
+
619
+ def validate_coverage(rows, t=None, oracle=None, demand=None):
620
+ """Reconcile procurement cover and buy arithmetic to independent live non-FBA quant stock."""
621
+ import modules.product_data as product_data
622
+ stock = product_data.coverage_quant_oracle() if oracle is None else oracle
623
+ if not stock.get('available') or not rows:
624
+ return [{'check': 'Procurement coverage source refusal: non-FBA stock is required',
625
+ 'a': 0, 'b': 1, 'gap': -1, 'ok': False,
626
+ 'detail': {'source_refusal': stock.get('cause') or 'no procurement rows'}}]
627
+ t = t or P.today()
628
+ ps, pe = _proj_window(t)
629
+ window_days = max(1, (pe - ps).days)
630
+ if demand is None:
631
+ f, to = _add_months(t, -12).isoformat(), _add_months(pe, -12).isoformat()
632
+ groups = O.get_odoo().read_group('sale.order.line', _demand_domain(f, to),
633
+ ['product_uom_qty:sum'], ['product_id'], lazy=False)
634
+ demand = {O.m2o_id(g['product_id']): float(g.get('product_uom_qty') or 0.0)
635
+ for g in groups if g.get('product_id')}
636
+ unavailable, wrong_days, wrong_buy = [], [], []
637
+ for r in rows:
638
+ pid = r.get('_product_id')
639
+ known = (pid is not None and isinstance(r.get('_fba'), (int, float))
640
+ and r.get('_coverage_report', {}).get('available') is not False)
641
+ expected_days, expected_buy = None, None
642
+ if not known:
643
+ unavailable.append({'code': r['code'],
644
+ 'cause': r.get('_coverage_report', {}).get('cause')
645
+ or 'FBA stock unavailable for this product'})
646
+ else:
647
+ effective = stock['non_fba_by_pid'].get(pid, 0.0) + float(r.get('incoming_po') or 0.0)
648
+ daily = demand.get(pid, 0.0) / window_days
649
+ expected_days = effective / daily if daily > 0 else (
650
+ None if effective <= 0 else 9999.0)
651
+ lead = r.get('lead') or 0
652
+ expected_buy = (expected_days is not None and lead > 0 and daily > 0
653
+ and expected_days < lead)
654
+ actual_days = r.get('days_cover')
655
+ if not (actual_days == expected_days or (
656
+ isinstance(actual_days, (int, float)) and isinstance(expected_days, (int, float))
657
+ and abs(actual_days - expected_days) < 1e-7)):
658
+ wrong_days.append(r['code'])
659
+ if r.get('buy') != expected_buy:
660
+ wrong_buy.append(r['code'])
661
+ detail = stock.get('detail', {})
662
+ return [
663
+ {'check': 'Procurement coverage source: each SKU has a measured FBA quantity',
664
+ 'a': len(rows) - len(unavailable), 'b': len(rows), 'gap': -len(unavailable),
665
+ 'ok': not unavailable, 'detail': {**detail, 'source_refusal': unavailable}},
666
+ {'check': 'Procurement days_cover == independent non-FBA quants plus inbound',
667
+ 'a': len(rows) - len(wrong_days), 'b': len(rows), 'gap': -len(wrong_days),
668
+ 'ok': not wrong_days, 'detail': {**detail, 'wrong_codes': wrong_days}},
669
+ {'check': 'Procurement buy == independent non-FBA cover below lead time',
670
+ 'a': len(rows) - len(wrong_buy), 'b': len(rows), 'gap': -len(wrong_buy),
671
+ 'ok': not wrong_buy, 'detail': {**detail, 'wrong_codes': wrong_buy}},
672
+ ]
platform/modules/product_data.py CHANGED
@@ -553,7 +553,7 @@ def _dos_with_inbound(on_hand, incoming, qty_ltm):
553
  return (None if dos_raw == float('inf') else round(float(dos_raw), 0)), bucket
554
 
555
 
556
- def _rescope_inventory(e, share, incoming=None):
557
  """`(qty_ltm, dos, bucket)` recomputed for ONE business unit's sales rate.
558
 
559
  The formulas are `inventory.sku_inventory`'s, applied to a BU-shaped numerator — NOT a second
@@ -561,8 +561,8 @@ def _rescope_inventory(e, share, incoming=None):
561
  copies of a threshold table is how the Product grid and the Inventory page start disagreeing
562
  about which SKUs are dead.
563
 
564
- ⚠ `on_hand` is whatever the warehouse holds, unscoped — so a Fisch reader's `dos` answers
565
- "how long does ALL our stock last at Fisch's rate", which is the question a Fisch salesperson
566
  actually has. It is deliberately NOT a pro-rated share of the shelf: there is no such shelf,
567
  and inventing one would put a number on screen that no Odoo query could reproduce.
568
  """
@@ -576,10 +576,12 @@ def _rescope_inventory(e, share, incoming=None):
576
  _dos, _bucket = _dos_with_inbound(None, incoming, 0.0)
577
  return None, _dos, _bucket
578
  qty = float(e.get("qty_ltm") or 0.0) * float(share or 0.0)
 
 
579
  # ⭐ THE FORMULA MOVED TO `_dos_with_inbound` (owner 2026-08-19) so the scoped and consolidated
580
  # paths cannot answer days-of-supply differently. Only the NUMERATOR is BU-shaped: `qty` is
581
  # this unit's share of LTM units, while the shelf and the inbound are one warehouse's.
582
- dos, bucket = _dos_with_inbound(on_hand, incoming, qty)
583
  return qty, dos, bucket
584
 
585
 
@@ -833,15 +835,27 @@ def pool(team_id=None, t=None, raw_fields=()):
833
  # ⭐⭐ OWNER 2026-08-19: *"Days of supply field should INCLUDE inbound quantities."* Read
834
  # here rather than in the cover-gap block below, because `dos` now needs it too.
835
  incoming = meta.get("incoming")
 
 
 
 
 
836
  if consolidated:
837
  # ⛔ RECOMPUTED, NOT `e['dos']`. `inventory.sku_inventory` returns a shelf-only
838
  # days-of-supply and knows nothing about purchase orders; taking its figure here would
839
  # ship the pre-ruling number under the new column description.
840
  qty_ltm = e.get("qty_ltm")
841
- dos, bucket = _dos_with_inbound(e.get("on_hand"), incoming, qty_ltm)
 
842
  else:
843
  qty_ltm, dos, bucket = _rescope_inventory(
844
- e, bu_share.get(code, _DEFAULT_SHARE), incoming)
 
 
 
 
 
 
845
  row.update({
846
  # UNSCOPED on purpose (owner 2026-08-11): one warehouse, no per-brand shelf.
847
  "on_hand": e.get("on_hand"),
@@ -852,7 +866,7 @@ def pool(team_id=None, t=None, raw_fields=()):
852
  # at Amazon" is measured. BLANK means the source could not answer at all, which is
853
  # `fba_by_code is None` (no FBA warehouse, or the read failed) or a code carrying no
854
  # Odoo product id. Those are different sentences and only one of them is a number.
855
- "fba": (fba_by_code or {}).get(code),
856
  "unit_cost": e.get("unit_cost"),
857
  "inv_value": e.get("inv_value"),
858
  # W48C-T09 — this is not `unit_cost`. The older field falls back to LTM COGS when
@@ -923,7 +937,7 @@ def pool(team_id=None, t=None, raw_fields=()):
923
  row["demand_fwd"] = round(fwd) if isinstance(fwd, (int, float)) and fwd > 0 else None
924
  daily = (fwd / float(products.FORWARD_DAYS)) if isinstance(fwd, (int, float)) \
925
  and fwd > 0 else None
926
- on_shelf = row["on_hand"]
927
  effective = ((on_shelf or 0.0) + (incoming or 0.0)
928
  if isinstance(on_shelf, (int, float)) else None)
929
  if daily and isinstance(effective, (int, float)) and isinstance(lead, (int, float)) \
@@ -1392,33 +1406,6 @@ def validate(team_id=None, t=None):
1392
  fresh_inventory = inventory.sku_inventory(t=t)
1393
  checks.append(reconcile_inventory_value_per_quantity(rows, fresh_inventory))
1394
 
1395
- # ⭐⭐ OWNER 2026-08-19 — `dos` COUNTS INBOUND, and this is the leg that proves it rather
1396
- # than trusting the expression. Two halves, because either alone is passable by accident:
1397
- # (a) EVERY row's dos re-derives from (on_hand + incoming) / (qty_ltm / 365), and
1398
- # (b) at least one row's dos is STRICTLY GREATER than the shelf-only figure would be.
1399
- # Without (b) the check stays green if `incoming` silently becomes 0 everywhere — the
1400
- # column would read exactly as it did before the ruling, and nothing would say so.
1401
- moved, wrong = 0, []
1402
- for r in rows:
1403
- oh, inc, q = r.get("on_hand"), r.get("incoming") or 0.0, r.get("qty_ltm")
1404
- if oh is None or not isinstance(q, (int, float)) or q <= 0:
1405
- continue
1406
- want = round((float(oh) + float(inc)) / (float(q) / 365.0), 0)
1407
- if r.get("dos") != want:
1408
- wrong.append(r["code"])
1409
- if inc > 0 and want > round(float(oh) / (float(q) / 365.0), 0):
1410
- moved += 1
1411
- checks.append({
1412
- "check": "Days of supply counts INBOUND units: every row re-derives from "
1413
- "(on_hand + incoming) / daily, and inbound demonstrably moves it",
1414
- "ours": moved, "theirs": len([r for r in rows if (r.get("incoming") or 0) > 0]),
1415
- "ok": not wrong and moved > 0,
1416
- "detail": {"rows_not_re_deriving": wrong[:10], "n_wrong": len(wrong),
1417
- "rows_where_inbound_raised_dos": moved,
1418
- "note": "a 0 in 'rows_where_inbound_raised_dos' means the column reads as "
1419
- "it did BEFORE the ruling, which is the silent-regression case."},
1420
- })
1421
-
1422
  # ⭐ RULE 8 — the FORWARD demand basis against a DIRECT Odoo read_group over the same
1423
  # reference window. ⛔ The oracle re-reads Odoo itself rather than re-calling
1424
  # `forward_demand_by_code`, which would reconcile the number with itself — the self-sealing
@@ -1457,9 +1444,137 @@ def validate(team_id=None, t=None):
1457
  checks.extend(validate_stock_measures(t=t))
1458
  checks.extend(validate_fba(rows))
1459
  checks.extend(validate_linked_metric(team_id=team_id))
 
1460
  return checks
1461
 
1462
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1463
  def validate_linked_metric(customer_id=None, product_id=None, team_id=None,
1464
  date_from=None, date_to=None):
1465
  """Reconcile one customer/product revenue cell to an independent live Odoo aggregate.
@@ -1578,7 +1693,12 @@ def validate_fba(rows, oracle=None, sample=10):
1578
  ⚠ RUN ON `pool()` ROWS, NEVER ON `rows_from_pool` OUTPUT. `fba` is an `int` field, so the wire
1579
  projection rounds it and a fractional quant would read as a spurious sub-unit gap.
1580
  """
1581
- wh, by_pid, detail = oracle if oracle is not None else fba_quant_oracle()
 
 
 
 
 
1582
  rows = list(rows or ())
1583
  valued = [r for r in rows if isinstance(r.get('fba'), (int, float))
1584
  and not isinstance(r.get('fba'), bool)]
 
553
  return (None if dos_raw == float('inf') else round(float(dos_raw), 0)), bucket
554
 
555
 
556
+ def _rescope_inventory(e, share, incoming=None, fba=0.0):
557
  """`(qty_ltm, dos, bucket)` recomputed for ONE business unit's sales rate.
558
 
559
  The formulas are `inventory.sku_inventory`'s, applied to a BU-shaped numerator — NOT a second
 
561
  copies of a threshold table is how the Product grid and the Inventory page start disagreeing
562
  about which SKUs are dead.
563
 
564
+ ⚠ Stock stays warehouse-wide after subtracting FBA, so a Fisch reader's `dos` answers
565
+ "how long does non-FBA stock last at Fisch's rate", which is the question a Fisch salesperson
566
  actually has. It is deliberately NOT a pro-rated share of the shelf: there is no such shelf,
567
  and inventing one would put a number on screen that no Odoo query could reproduce.
568
  """
 
576
  _dos, _bucket = _dos_with_inbound(None, incoming, 0.0)
577
  return None, _dos, _bucket
578
  qty = float(e.get("qty_ltm") or 0.0) * float(share or 0.0)
579
+ if not isinstance(fba, (int, float)):
580
+ return qty, None, None
581
  # ⭐ THE FORMULA MOVED TO `_dos_with_inbound` (owner 2026-08-19) so the scoped and consolidated
582
  # paths cannot answer days-of-supply differently. Only the NUMERATOR is BU-shaped: `qty` is
583
  # this unit's share of LTM units, while the shelf and the inbound are one warehouse's.
584
+ dos, bucket = _dos_with_inbound(float(on_hand) - fba, incoming, qty)
585
  return qty, dos, bucket
586
 
587
 
 
835
  # ⭐⭐ OWNER 2026-08-19: *"Days of supply field should INCLUDE inbound quantities."* Read
836
  # here rather than in the cover-gap block below, because `dos` now needs it too.
837
  incoming = meta.get("incoming")
838
+ fba = (fba_by_code or {}).get(code)
839
+ fba_known = isinstance(fba, (int, float)) and _fba_report.get("available") is not False
840
+ on_hand = e.get("on_hand")
841
+ non_fba = (float(on_hand) - fba
842
+ if isinstance(on_hand, (int, float)) and fba_known else None)
843
  if consolidated:
844
  # ⛔ RECOMPUTED, NOT `e['dos']`. `inventory.sku_inventory` returns a shelf-only
845
  # days-of-supply and knows nothing about purchase orders; taking its figure here would
846
  # ship the pre-ruling number under the new column description.
847
  qty_ltm = e.get("qty_ltm")
848
+ dos, bucket = (_dos_with_inbound(non_fba, incoming, qty_ltm)
849
+ if fba_known else (None, None))
850
  else:
851
  qty_ltm, dos, bucket = _rescope_inventory(
852
+ e, bu_share.get(code, _DEFAULT_SHARE), incoming, fba if fba_known else None)
853
+ if not fba_known:
854
+ dos, bucket = None, None
855
+ if not fba_known:
856
+ row["_coverage_report"] = {**_fba_report, "available": False,
857
+ "cause": _fba_report.get("cause")
858
+ or "FBA stock unavailable for this product"}
859
  row.update({
860
  # UNSCOPED on purpose (owner 2026-08-11): one warehouse, no per-brand shelf.
861
  "on_hand": e.get("on_hand"),
 
866
  # at Amazon" is measured. BLANK means the source could not answer at all, which is
867
  # `fba_by_code is None` (no FBA warehouse, or the read failed) or a code carrying no
868
  # Odoo product id. Those are different sentences and only one of them is a number.
869
+ "fba": fba,
870
  "unit_cost": e.get("unit_cost"),
871
  "inv_value": e.get("inv_value"),
872
  # W48C-T09 — this is not `unit_cost`. The older field falls back to LTM COGS when
 
937
  row["demand_fwd"] = round(fwd) if isinstance(fwd, (int, float)) and fwd > 0 else None
938
  daily = (fwd / float(products.FORWARD_DAYS)) if isinstance(fwd, (int, float)) \
939
  and fwd > 0 else None
940
+ on_shelf = non_fba
941
  effective = ((on_shelf or 0.0) + (incoming or 0.0)
942
  if isinstance(on_shelf, (int, float)) else None)
943
  if daily and isinstance(effective, (int, float)) and isinstance(lead, (int, float)) \
 
1406
  fresh_inventory = inventory.sku_inventory(t=t)
1407
  checks.append(reconcile_inventory_value_per_quantity(rows, fresh_inventory))
1408
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1409
  # ⭐ RULE 8 — the FORWARD demand basis against a DIRECT Odoo read_group over the same
1410
  # reference window. ⛔ The oracle re-reads Odoo itself rather than re-calling
1411
  # `forward_demand_by_code`, which would reconcile the number with itself — the self-sealing
 
1444
  checks.extend(validate_stock_measures(t=t))
1445
  checks.extend(validate_fba(rows))
1446
  checks.extend(validate_linked_metric(team_id=team_id))
1447
+ checks.extend(validate_coverage(rows, t=t, team_id=team_id))
1448
  return checks
1449
 
1450
 
1451
+ def coverage_quant_oracle():
1452
+ """Read signed internal stock.quant totals outside and inside the independently resolved FBA subtree."""
1453
+ try:
1454
+ o = O.get_odoo()
1455
+ found = o.search_read('stock.warehouse', [('code', '=', 'FBA')],
1456
+ ['id', 'code', 'view_location_id'])
1457
+ if not found:
1458
+ return {'available': False, 'cause': 'no stock.warehouse carries the code FBA'}
1459
+ wh = found[0]
1460
+ root = O.m2o_id(wh.get('view_location_id'))
1461
+ if not root:
1462
+ return {'available': False, 'cause': 'the FBA warehouse has no location subtree'}
1463
+ quantities = []
1464
+ for exclude_fba in (True, False):
1465
+ domain = [('location_id.usage', '=', 'internal')]
1466
+ if exclude_fba:
1467
+ domain.append('!')
1468
+ domain.append(('location_id', 'child_of', root))
1469
+ groups = o.read_group('stock.quant', domain,
1470
+ ['quantity:sum'], ['product_id'], lazy=False)
1471
+ quantities.append({O.m2o_id(g['product_id']): float(g.get('quantity') or 0.0)
1472
+ for g in groups if g.get('product_id')})
1473
+ records = o.search_read('product.product', [('active', 'in', [True, False])],
1474
+ ['id', 'default_code', 'active'])
1475
+ ids_by_code = {}
1476
+ for p in records:
1477
+ if p.get('default_code') and p.get('active') is not False:
1478
+ code = str(p['default_code']).strip()
1479
+ ids_by_code.setdefault(code, []).append(p['id'])
1480
+ return {'available': True, 'non_fba_by_pid': quantities[0],
1481
+ 'fba_by_pid': quantities[1], 'ids_by_code': ids_by_code,
1482
+ 'detail': {'warehouse_id': wh['id'], 'location_root': root,
1483
+ 'source': 'live stock.quant quantity:sum excluding the FBA subtree'}}
1484
+ except Exception as exc:
1485
+ return {'available': False, 'cause': 'non-FBA stock.quant read failed: ' + type(exc).__name__}
1486
+
1487
+
1488
+ def validate_coverage(rows, t=None, team_id=None, oracle=None, demand=None):
1489
+ """Reconcile coverage and its signals to independent live non-FBA quant stock in either BU scope."""
1490
+ stock = coverage_quant_oracle() if oracle is None else oracle
1491
+ if not stock.get('available') or not rows:
1492
+ return [{'check': 'Coverage source refusal: independent non-FBA stock is required',
1493
+ 'a': 0, 'b': 1, 'gap': -1, 'ok': False,
1494
+ 'detail': {'team_id': team_id, 'source_refusal': stock.get('cause')
1495
+ or 'the product pool has no rows'}}]
1496
+ if demand is None:
1497
+ demand, _report = products.forward_demand_by_code(t=t, team_id=team_id)
1498
+ outside, inside = stock['non_fba_by_pid'], stock['fba_by_pid']
1499
+ ids_by_code = stock['ids_by_code']
1500
+ unavailable, wrong_dos, wrong_cover, residuals = [], [], [], {}
1501
+ for r in rows:
1502
+ days = None
1503
+ code, pid = r['code'], r.get('product_id')
1504
+ known = (isinstance(r.get('fba'), (int, float))
1505
+ and r.get('_coverage_report', {}).get('available') is not False)
1506
+ if not known:
1507
+ unavailable.append({'code': code, 'cause': r.get('_coverage_report', {}).get('cause')
1508
+ or 'FBA stock unavailable for this product'})
1509
+ expected_dos, expected_bucket = None, None
1510
+ shelf = None
1511
+ else:
1512
+ ids = ids_by_code.get(code)
1513
+ # Inventory groups active SKU ids, while the FBA column selects the catalogue id.
1514
+ residual = sum(inside.get(i, 0.0) for i in (ids or []) if i != pid)
1515
+ if residual:
1516
+ residuals[code] = residual
1517
+ shelf = sum(outside.get(i, 0.0) for i in ids) + residual if ids else None
1518
+ qty = r.get('qty_ltm') or 0.0
1519
+ if shelf is None:
1520
+ expected_dos, expected_bucket = None, 'No stock record'
1521
+ else:
1522
+ effective = shelf + float(r.get('incoming') or 0.0)
1523
+ days = effective / (qty / 365.0) if qty > 0 else (
1524
+ float('inf') if effective > 0 else 0.0)
1525
+ expected_dos = None if days == float('inf') else round(days, 0)
1526
+ if shelf <= 0:
1527
+ expected_bucket = 'Out of stock'
1528
+ elif qty <= 0:
1529
+ expected_bucket = 'No recent sales'
1530
+ else:
1531
+ bands = ((0, 15, 'Critical (<15d)'), (15, 45, 'Lean (15 to 45d)'),
1532
+ (45, 120, 'Healthy (45 to 120d)'), (120, 270, 'Ample (120 to 270d)'),
1533
+ (270, 540, 'Overstock (270 to 540d)'))
1534
+ expected_bucket = next((label for lower, upper, label in bands
1535
+ if lower <= days < upper),
1536
+ 'Excess (>540d)')
1537
+ actual_dos = r.get('dos')
1538
+ dos_matches = actual_dos == expected_dos
1539
+ if isinstance(actual_dos, (int, float)) and days is not None and _math.isfinite(days):
1540
+ # Independent float aggregates can straddle an exact half-day rounding boundary.
1541
+ dos_matches = abs(actual_dos - days) <= 0.5 + 1e-7
1542
+ if not dos_matches or r.get('stock_bucket') != expected_bucket:
1543
+ wrong_dos.append(code)
1544
+ fwd, lead = demand.get(code), r.get('lead_days')
1545
+ expected = (None, None, None)
1546
+ cover_days = None
1547
+ if shelf is not None and isinstance(fwd, (int, float)) and fwd > 0 \
1548
+ and isinstance(lead, (int, float)) and lead > 0:
1549
+ daily = fwd / float(products.FORWARD_DAYS)
1550
+ effective = shelf + float(r.get('incoming') or 0.0)
1551
+ short = daily * lead - effective
1552
+ units = int(_math.ceil(short) if short > 0 else _math.floor(short))
1553
+ cover_days = effective / daily - lead
1554
+ expected = (units, int(round(cover_days)),
1555
+ 'Buy now' if units > 0 else 'OK')
1556
+ actual = (r.get('cover_gap_units'), r.get('cover_gap_d'), r.get('buy_now'))
1557
+ cover_matches = actual == expected
1558
+ if cover_days is not None and isinstance(actual[1], (int, float)):
1559
+ cover_matches = (actual[0] == expected[0] and actual[2] == expected[2]
1560
+ and abs(actual[1] - cover_days) <= 0.5 + 1e-7)
1561
+ if not cover_matches:
1562
+ wrong_cover.append(code)
1563
+ detail = {**stock.get('detail', {}), 'team_id': team_id,
1564
+ 'fba_sibling_residual_by_code': residuals}
1565
+ return [
1566
+ {'check': 'Coverage source: each product has a measured FBA quantity',
1567
+ 'a': len(rows) - len(unavailable), 'b': len(rows), 'gap': -len(unavailable),
1568
+ 'ok': not unavailable, 'detail': {**detail, 'source_refusal': unavailable}},
1569
+ {'check': 'Days of supply and stock_bucket == independent non-FBA quants plus inbound',
1570
+ 'a': len(rows) - len(wrong_dos), 'b': len(rows), 'gap': -len(wrong_dos),
1571
+ 'ok': not wrong_dos, 'detail': {**detail, 'wrong_codes': wrong_dos}},
1572
+ {'check': 'Cover gap units, days and buy_now == independent non-FBA quants plus inbound',
1573
+ 'a': len(rows) - len(wrong_cover), 'b': len(rows), 'gap': -len(wrong_cover),
1574
+ 'ok': not wrong_cover, 'detail': {**detail, 'wrong_codes': wrong_cover}},
1575
+ ]
1576
+
1577
+
1578
  def validate_linked_metric(customer_id=None, product_id=None, team_id=None,
1579
  date_from=None, date_to=None):
1580
  """Reconcile one customer/product revenue cell to an independent live Odoo aggregate.
 
1693
  ⚠ RUN ON `pool()` ROWS, NEVER ON `rows_from_pool` OUTPUT. `fba` is an `int` field, so the wire
1694
  projection rounds it and a fractional quant would read as a spurious sub-unit gap.
1695
  """
1696
+ try:
1697
+ wh, by_pid, detail = oracle if oracle is not None else fba_quant_oracle()
1698
+ except Exception as exc:
1699
+ return [{'check': 'FBA source refusal: independent stock.quant read failed',
1700
+ 'a': 0, 'b': 1, 'gap': -1, 'ok': False,
1701
+ 'detail': {'source_refusal': type(exc).__name__}}]
1702
  rows = list(rows or ())
1703
  valued = [r for r in rows if isinstance(r.get('fba'), (int, float))
1704
  and not isinstance(r.get('fba'), bool)]
web/public/AIOS_DEPLOY_RECEIPT CHANGED
@@ -1 +1 @@
1
- 13ee55f772c3ecc603c0d979593c37f7a351adda070b568312b92f8aaab20a74
 
1
+ 3a063f9e9e49991823ca608e4f24205ada7577cd2883b7fe73fb9bd8f96738ca