fsanyoto commited on
Commit
7943380
·
verified ·
1 Parent(s): a47761b

Deploy AIOS web (React glide grid + FastAPI slice)

Browse files
RELEASES.json CHANGED
@@ -1,4 +1,4 @@
1
  {
2
- "current": "4b1bcdd4f",
3
  "releases": []
4
  }
 
1
  {
2
+ "current": "aad6561d",
3
  "releases": []
4
  }
VERSION CHANGED
@@ -1 +1 @@
1
- 4b1bcdd4f
 
1
+ aad6561d
api/diag_aging.py CHANGED
@@ -16,7 +16,7 @@ def open_domain(O):
16
  return domain + ([("partner_id", "not in", list(excluded))] if excluded else [])
17
 
18
 
19
- def validate(session, rows, restrictions, *, today):
20
  """Tie every artifact bucket and its signed total to separate native Odoo aggregates."""
21
  from core import odoo as O
22
  from modules import ar
@@ -46,6 +46,12 @@ def validate(session, rows, restrictions, *, today):
46
  checks = [existing, {"check": "AR aging matches module buckets", "a": len(rows),
47
  "b": len(canonical), "ok": rows == [
48
  {"bucket": r["bucket"], "amount": r["amount"]} for r in canonical]}]
 
 
 
 
 
 
49
  domain = open_domain(helpers)
50
  boundary = lambda days: (today - dt.timedelta(days=days)).isoformat()
51
  buckets = [
@@ -73,10 +79,21 @@ def run(session, params):
73
  import wakeups
74
 
75
  source = prepare(session, params, "ar", "Collections")
76
- restrictions = ar_scope(session)
 
 
77
  today = wakeups._date()
78
  with source._bound():
79
- module = bind_functions(ar, {"O": ScopedOdoo(O.get_odoo(), restrictions).helpers()})
 
 
 
 
 
 
 
 
 
80
  rows = [{"bucket": row["bucket"], "amount": float(amount(row["amount"]))} for row in module.aging(today)]
81
  return {"title": "AR aging", "columns": copy.deepcopy(COLUMNS), "rows": rows,
82
- "validation": validate(session, rows, restrictions, today=today)}
 
16
  return domain + ([("partner_id", "not in", list(excluded))] if excluded else [])
17
 
18
 
19
+ def validate(session, rows, restrictions, *, today, documents=None):
20
  """Tie every artifact bucket and its signed total to separate native Odoo aggregates."""
21
  from core import odoo as O
22
  from modules import ar
 
46
  checks = [existing, {"check": "AR aging matches module buckets", "a": len(rows),
47
  "b": len(canonical), "ok": rows == [
48
  {"bucket": r["bucket"], "amount": r["amount"]} for r in canonical]}]
49
+ if documents is not None:
50
+ native = {doc["id"]: doc for doc in docs}
51
+ checks.append({"check": "AR aging invoice population and amounts", "a": len(documents), "b": len(native),
52
+ "ok": {doc["id"] for doc in documents} == set(native)
53
+ and all(abs(amount(doc["open"]) - amount(native[doc["id"]]["open"])) < Decimal("0.01")
54
+ and doc["bucket"] == native[doc["id"]]["bucket"] for doc in documents)})
55
  domain = open_domain(helpers)
56
  boundary = lambda days: (today - dt.timedelta(days=days)).isoformat()
57
  buckets = [
 
79
  import wakeups
80
 
81
  source = prepare(session, params, "ar", "Collections")
82
+ records = {}
83
+ ar_scope(session, records=records)
84
+ native_scope = ar_scope(session, native=True, records=records)
85
  today = wakeups._date()
86
  with source._bound():
87
+ excluded = set(O.excluded_partner_ids())
88
+ docs = []
89
+ for row in records["ut_odoo_invoices"]:
90
+ if (row["partner_id"] in excluded or row["move_type"] not in ("out_invoice", "out_refund")
91
+ or row["payment_state"] not in ("not_paid", "partial")):
92
+ continue
93
+ days = max((today - wakeups._date(row["due_date"])).days, 0) if row["due_date"] else 0
94
+ docs.append({"id": row["odoo_id"], "open": float(amount(row["residual"])),
95
+ "days_overdue": days, "bucket": ar._bucket(days)})
96
+ module = bind_functions(ar, {"_open_docs": lambda _today: copy.deepcopy(docs)})
97
  rows = [{"bucket": row["bucket"], "amount": float(amount(row["amount"]))} for row in module.aging(today)]
98
  return {"title": "AR aging", "columns": copy.deepcopy(COLUMNS), "rows": rows,
99
+ "validation": validate(session, rows, native_scope, today=today, documents=docs)}
api/diag_recon.py CHANGED
@@ -19,7 +19,7 @@ def partner_domain(O):
19
  return [("credit", ">", 0)] + ([("id", "not in", list(excluded))] if excluded else [])
20
 
21
 
22
- def validate(session, rows, restrictions, *, today=None, threshold=THRESHOLD):
23
  """Compare signed artifact gaps with independently read partner credit minus open documents."""
24
  from core import odoo as O
25
 
@@ -51,7 +51,16 @@ def validate(session, rows, restrictions, *, today=None, threshold=THRESHOLD):
51
  matched = sum(1 for row in rows if row.get("partner_id") in expected
52
  and all(abs(amount(row[key]) - expected[row["partner_id"]][key]) < Decimal("0.01")
53
  for key in ("odoo_receivable", "open_docs", "gap")))
54
- return [
 
 
 
 
 
 
 
 
 
55
  {"check": "Reconciliation customer identities", "a": len(rows), "b": len(expected),
56
  "ok": len(set(row_ids)) == len(rows) and set(row_ids) == set(expected)},
57
  {"check": "Reconciliation source amounts", "a": len(rows), "b": matched, "ok": matched == len(rows)},
@@ -66,12 +75,21 @@ def run(session, params):
66
  import wakeups
67
 
68
  source = prepare(session, params, "ar", "Collections")
69
- restrictions = ar_scope(session, customers=True)
 
 
70
  today = wakeups._date()
71
  with source._bound():
72
  scoped = ScopedOdoo(O.get_odoo(), restrictions)
73
  helpers = scoped.helpers()
74
- module = bind_functions(ar, {"O": helpers})
 
 
 
 
 
 
 
75
  flags = module.reconciliation_flags(today, limit=None, threshold=THRESHOLD)
76
  open_by = defaultdict(float)
77
  for doc in module._open_docs(today):
@@ -94,4 +112,4 @@ def run(session, params):
94
  "partner_id": pid})
95
  return {"title": "Bank reconciliation gaps" if rows else "No reconciliation gaps found.",
96
  "columns": copy.deepcopy(COLUMNS), "rows": rows,
97
- "validation": validate(session, rows, restrictions, today=today)}
 
19
  return [("credit", ">", 0)] + ([("id", "not in", list(excluded))] if excluded else [])
20
 
21
 
22
+ def validate(session, rows, restrictions, *, today=None, threshold=THRESHOLD, documents=None):
23
  """Compare signed artifact gaps with independently read partner credit minus open documents."""
24
  from core import odoo as O
25
 
 
51
  matched = sum(1 for row in rows if row.get("partner_id") in expected
52
  and all(abs(amount(row[key]) - expected[row["partner_id"]][key]) < Decimal("0.01")
53
  for key in ("odoo_receivable", "open_docs", "gap")))
54
+ population = []
55
+ if documents is not None:
56
+ native = {doc["id"]: amount(doc["amount_residual_signed"]) for doc in scoped.search_read(
57
+ "account.move", open_domain(helpers), ["amount_residual_signed"])}
58
+ population.append({"check": "Reconciliation invoice population and amounts",
59
+ "a": len(documents), "b": len(native),
60
+ "ok": {doc["id"] for doc in documents} == set(native)
61
+ and all(abs(amount(doc["open"]) - native[doc["id"]]) < Decimal("0.01")
62
+ for doc in documents)})
63
+ return population + [
64
  {"check": "Reconciliation customer identities", "a": len(rows), "b": len(expected),
65
  "ok": len(set(row_ids)) == len(rows) and set(row_ids) == set(expected)},
66
  {"check": "Reconciliation source amounts", "a": len(rows), "b": matched, "ok": matched == len(rows)},
 
75
  import wakeups
76
 
77
  source = prepare(session, params, "ar", "Collections")
78
+ records = {}
79
+ restrictions = ar_scope(session, customers=True, records=records)
80
+ native_scope = ar_scope(session, customers=True, native=True, records=records)
81
  today = wakeups._date()
82
  with source._bound():
83
  scoped = ScopedOdoo(O.get_odoo(), restrictions)
84
  helpers = scoped.helpers()
85
+ excluded = set(helpers.excluded_partner_ids())
86
+ people = {int(row["pid"]) for row in records["customer_data"]}
87
+ docs = [{"id": row["odoo_id"], "partner_id": [row["partner_id"], row["customer"]],
88
+ "open": float(amount(row["residual"]))}
89
+ for row in records["ut_odoo_invoices"] if row["partner_id"] in people
90
+ and row["partner_id"] not in excluded and row["move_type"] in ("out_invoice", "out_refund")
91
+ and row["payment_state"] in ("not_paid", "partial")]
92
+ module = bind_functions(ar, {"O": helpers, "_open_docs": lambda _today: copy.deepcopy(docs)})
93
  flags = module.reconciliation_flags(today, limit=None, threshold=THRESHOLD)
94
  open_by = defaultdict(float)
95
  for doc in module._open_docs(today):
 
112
  "partner_id": pid})
113
  return {"title": "Bank reconciliation gaps" if rows else "No reconciliation gaps found.",
114
  "columns": copy.deepcopy(COLUMNS), "rows": rows,
115
+ "validation": validate(session, rows, native_scope, today=today, documents=docs)}
api/diag_stockout.py CHANGED
@@ -83,7 +83,7 @@ def validate(session, rows, restrictions, *, today):
83
 
84
 
85
  def run(session, params):
86
- from core import odoo as O
87
  from loopy_file_tools import read_all
88
  import wakeups
89
 
@@ -93,6 +93,15 @@ def run(session, params):
93
  products = read_all(session, "product_data", fields=PRODUCT_FIELDS)["records"]
94
  restrictions = {"sale.order.line": [("id", "in", identities(lines, "odoo_id"))],
95
  "product.product": [("id", "in", identities(products, "product_id")), ("active", "=", True)]}
 
 
 
 
 
 
 
 
 
96
  with source._bound():
97
  client = ScopedOdoo(O.get_odoo(), restrictions)
98
  metadata = {row["id"]: row for row in client.search_read("product.product", [], ["type", "active"])}
@@ -105,4 +114,4 @@ def run(session, params):
105
  and on_hand[product["product_id"]]["type"] == "product"
106
  and amount(on_hand[product["product_id"]]["qty_available"]) <= 0]
107
  return {"title": "Stock-outs" if rows else "No stock-outs found.", "columns": copy.deepcopy(COLUMNS),
108
- "rows": rows, "validation": validate(session, rows, restrictions, today=today)}
 
83
 
84
 
85
  def run(session, params):
86
+ from core import odoo as O, perm_scope, perms
87
  from loopy_file_tools import read_all
88
  import wakeups
89
 
 
93
  products = read_all(session, "product_data", fields=PRODUCT_FIELDS)["records"]
94
  restrictions = {"sale.order.line": [("id", "in", identities(lines, "odoo_id"))],
95
  "product.product": [("id", "in", identities(products, "product_id")), ("active", "=", True)]}
96
+ native_scope = copy.deepcopy(restrictions)
97
+ for database, model in (("ut_odoo_order_lines", "sale.order.line"), ("product_data", "product.product")):
98
+ grant = perm_scope.assistant_entry(session.user, database) or {}
99
+ narrowed = (bool(grant.get("filter"))
100
+ or perm_scope.row_scope_applies(session.user, database, st=session.runtime)
101
+ or "All" not in perms.allowed_bu_labels(session.user)
102
+ or any(value is not None for value in perm_scope.derive_pool_scope(session.user, database)))
103
+ if not narrowed:
104
+ native_scope[model] = [node for node in native_scope[model] if node[0] != "id"]
105
  with source._bound():
106
  client = ScopedOdoo(O.get_odoo(), restrictions)
107
  metadata = {row["id"]: row for row in client.search_read("product.product", [], ["type", "active"])}
 
114
  and on_hand[product["product_id"]]["type"] == "product"
115
  and amount(on_hand[product["product_id"]]["qty_available"]) <= 0]
116
  return {"title": "Stock-outs" if rows else "No stock-outs found.", "columns": copy.deepcopy(COLUMNS),
117
+ "rows": rows, "validation": validate(session, rows, native_scope, today=today)}
api/loopy_bulk.py CHANGED
@@ -72,14 +72,25 @@ def _group(rows, today):
72
  def validate(session, records, overdue, grouped, *, today, partner_ids):
73
  """Tie each grouped amount and overdue age to independent, permission-bounded Odoo reads."""
74
  import wakeups
75
- from core import odoo as O
76
-
 
 
 
 
 
 
 
 
 
 
77
  ids = identities(records, "odoo_id")
78
  shown_ids = identities(overdue, "odoo_id")
79
  source = session.runtime.odoo_source()
80
  if source is None:
81
  raise ValueError("Connect Odoo before building a chase list.")
82
- restrictions = {"account.move": [("id", "in", ids), ("partner_id", "in", partner_ids)]}
 
83
  domain, _accepts = wakeups._ar_rule(today, 0)
84
  with source._bound():
85
  client = ScopedOdoo(O.get_odoo(), restrictions)
@@ -89,15 +100,23 @@ def validate(session, records, overdue, grouped, *, today, partner_ids):
89
  for row in native:
90
  partner = row.get("partner_id")
91
  pid = partner[0] if isinstance(partner, (list, tuple)) and partner else partner
92
- if type(pid) is not int or pid not in partner_ids or row["id"] not in ids:
 
93
  raise ValueError("Odoo returned an unreadable invoice.")
94
- native_rows.append({"partner_id": pid, "residual": row.get("amount_residual_signed"),
95
  "due_date": row.get("invoice_date_due")})
96
  independent = _group(native_rows, today)
97
  checks = [{"check": "Chase invoices match Odoo", "a": len(shown_ids), "b": len(native),
98
  "ok": set(shown_ids) == {row["id"] for row in native}}]
99
  checks.append({"check": "Chase customers match Odoo", "a": len(grouped), "b": len(independent),
100
  "ok": set(grouped) == set(independent)})
 
 
 
 
 
 
 
101
  for pid, item in grouped.items():
102
  other = independent.get(pid)
103
  checks.extend([
@@ -116,6 +135,8 @@ def validate(session, records, overdue, grouped, *, today, partner_ids):
116
  def chase_list(session, links):
117
  import wakeups
118
 
 
 
119
  fields = _schema(session, CUSTOMERS)
120
  names = [field["key"] for field in fields if _name_field(field)]
121
  phones = [field["key"] for field in fields if _phone_field(field)]
@@ -127,14 +148,14 @@ def chase_list(session, links):
127
  contacts = dict(zip(partner_ids, people))
128
  records = read_all(session, INVOICES, fields=INVOICE_FIELDS)["records"]
129
  # The wake-up predicate receives only rows admitted by both database walls.
130
- records = [row for row in records if row["partner_id"] in contacts]
131
  today = wakeups._date()
132
- scoped = bind_functions(wakeups, {"_mirror_rows": lambda _rt, **_kw: copy.deepcopy(records)})
133
  overdue = scoped._overdue_rows(session.runtime, today, 0)
134
  grouped = _group(overdue, today)
 
135
  if not grouped:
136
  return None
137
- checks = validate(session, records, overdue, grouped, today=today, partner_ids=partner_ids)
138
  rows = []
139
  for pid, item in grouped.items():
140
  person = contacts[pid]
 
72
  def validate(session, records, overdue, grouped, *, today, partner_ids):
73
  """Tie each grouped amount and overdue age to independent, permission-bounded Odoo reads."""
74
  import wakeups
75
+ from core import odoo as O, perm_scope, perms
76
+
77
+ if session.tenant != session.runtime.key:
78
+ raise ValueError("The chase list tenant must match its runtime.")
79
+ def narrowed(database):
80
+ grant = perm_scope.assistant_entry(session.user, database) or {}
81
+ return (bool(grant.get("filter"))
82
+ or perm_scope.row_scope_applies(session.user, database, st=session.runtime)
83
+ or "All" not in perms.allowed_bu_labels(session.user)
84
+ or any(value is not None for value in perm_scope.derive_pool_scope(session.user, database)))
85
+
86
+ invoice_wall, customer_wall = narrowed(INVOICES), narrowed(CUSTOMERS)
87
  ids = identities(records, "odoo_id")
88
  shown_ids = identities(overdue, "odoo_id")
89
  source = session.runtime.odoo_source()
90
  if source is None:
91
  raise ValueError("Connect Odoo before building a chase list.")
92
+ restrictions = {"account.move": ([("id", "in", ids)] if invoice_wall else [])
93
+ + ([("partner_id", "in", partner_ids)] if customer_wall else [])}
94
  domain, _accepts = wakeups._ar_rule(today, 0)
95
  with source._bound():
96
  client = ScopedOdoo(O.get_odoo(), restrictions)
 
100
  for row in native:
101
  partner = row.get("partner_id")
102
  pid = partner[0] if isinstance(partner, (list, tuple)) and partner else partner
103
+ if (type(pid) is not int or pid <= 0
104
+ or customer_wall and pid not in partner_ids or invoice_wall and row["id"] not in ids):
105
  raise ValueError("Odoo returned an unreadable invoice.")
106
+ native_rows.append({"odoo_id": row["id"], "partner_id": pid, "residual": row.get("amount_residual_signed"),
107
  "due_date": row.get("invoice_date_due")})
108
  independent = _group(native_rows, today)
109
  checks = [{"check": "Chase invoices match Odoo", "a": len(shown_ids), "b": len(native),
110
  "ok": set(shown_ids) == {row["id"] for row in native}}]
111
  checks.append({"check": "Chase customers match Odoo", "a": len(grouped), "b": len(independent),
112
  "ok": set(grouped) == set(independent)})
113
+ shown = {row["odoo_id"]: row for row in overdue}
114
+ matched = sum(1 for row in native_rows if row["odoo_id"] in shown
115
+ and row["partner_id"] == shown[row["odoo_id"]]["partner_id"]
116
+ and abs(amount(row["residual"]) - amount(shown[row["odoo_id"]]["residual"])) < Decimal("0.01")
117
+ and wakeups._date(row["due_date"]) == wakeups._date(shown[row["odoo_id"]]["due_date"]))
118
+ checks.append({"check": "Chase invoice amounts and dates", "a": len(shown), "b": matched,
119
+ "ok": matched == len(shown) == len(native_rows)})
120
  for pid, item in grouped.items():
121
  other = independent.get(pid)
122
  checks.extend([
 
135
  def chase_list(session, links):
136
  import wakeups
137
 
138
+ if session.tenant != session.runtime.key:
139
+ raise ValueError("The chase list tenant must match its runtime.")
140
  fields = _schema(session, CUSTOMERS)
141
  names = [field["key"] for field in fields if _name_field(field)]
142
  phones = [field["key"] for field in fields if _phone_field(field)]
 
148
  contacts = dict(zip(partner_ids, people))
149
  records = read_all(session, INVOICES, fields=INVOICE_FIELDS)["records"]
150
  # The wake-up predicate receives only rows admitted by both database walls.
151
+ permitted = [row for row in records if row["partner_id"] in contacts]
152
  today = wakeups._date()
153
+ scoped = bind_functions(wakeups, {"_mirror_rows": lambda _rt, **_kw: copy.deepcopy(permitted)})
154
  overdue = scoped._overdue_rows(session.runtime, today, 0)
155
  grouped = _group(overdue, today)
156
+ checks = validate(session, records, overdue, grouped, today=today, partner_ids=partner_ids)
157
  if not grouped:
158
  return None
 
159
  rows = []
160
  for pid, item in grouped.items():
161
  person = contacts[pid]
api/loopy_diagnostics.py CHANGED
@@ -160,17 +160,33 @@ class ScopedOdoo:
160
  return bind_functions(O, {"get_odoo": lambda: self, "excluded_partner_ids": O.excluded_partner_ids})
161
 
162
 
163
- def ar_scope(session, *, customers=False):
 
164
  from loopy_file_tools import read_all
165
 
166
- invoices = read_all(session, "ut_odoo_invoices", fields=[
167
- "odoo_id", "partner_id", "customer", "residual", "due_date", "move_type"])["records"]
168
- restrictions = {"account.move": [("id", "in", identities(invoices, "odoo_id"))]}
 
 
 
 
 
 
 
 
 
 
 
169
  if customers:
170
- people = read_all(session, "customer_data", fields=["customer", "ar_open", "ar_overdue"])["records"]
171
- pids = identities(people, "pid")
172
- restrictions["res.partner"] = [("id", "in", pids)]
173
- restrictions["account.move"].append(("partner_id", "in", pids))
 
 
 
 
174
  return restrictions
175
 
176
 
 
160
  return bind_functions(O, {"get_odoo": lambda: self, "excluded_partner_ids": O.excluded_partner_ids})
161
 
162
 
163
+ def ar_scope(session, *, customers=False, native=False, records=None):
164
+ from core import perm_scope, perms
165
  from loopy_file_tools import read_all
166
 
167
+ def narrowed(database):
168
+ grant = perm_scope.assistant_entry(session.user, database) or {}
169
+ return (bool(grant.get("filter"))
170
+ or perm_scope.row_scope_applies(session.user, database, st=session.runtime)
171
+ or "All" not in perms.allowed_bu_labels(session.user)
172
+ or any(value is not None for value in perm_scope.derive_pool_scope(session.user, database)))
173
+
174
+ records = records if records is not None else {}
175
+ if "ut_odoo_invoices" not in records:
176
+ records["ut_odoo_invoices"] = read_all(session, "ut_odoo_invoices", fields=[
177
+ "odoo_id", "partner_id", "customer", "residual", "due_date", "move_type", "payment_state"])["records"]
178
+ ids = identities(records["ut_odoo_invoices"], "odoo_id")
179
+ restrictions = {"account.move": [("id", "in", ids)]
180
+ if not native or narrowed("ut_odoo_invoices") else []}
181
  if customers:
182
+ if "customer_data" not in records:
183
+ records["customer_data"] = read_all(session, "customer_data", fields=[
184
+ "customer", "ar_open", "ar_overdue"])["records"]
185
+ pids = identities(records["customer_data"], "pid")
186
+ restrictions["res.partner"] = []
187
+ if not native or narrowed("customer_data"):
188
+ restrictions["res.partner"] = [("id", "in", pids)]
189
+ restrictions["account.move"].append(("partner_id", "in", pids))
190
  return restrictions
191
 
192
 
api/loopy_digests.py CHANGED
@@ -32,11 +32,13 @@ def _digest(rt, tenant, params, *, now, chase):
32
 
33
  worklist = run(session, {})
34
  tool_call = {"name": "diagnostic_run", "args": {"key": "stockouts"}}
35
- if worklist is None or not worklist["rows"]:
36
  return {"ok": True, "detail": "Nothing to send."}
37
  if (not worklist.get("validation")
38
  or any(check.get("ok") is not True for check in worklist["validation"])):
39
  raise ValueError("This digest could not be tied to Odoo. Try after the next sync.")
 
 
40
  saved = assistant_worklists.save(session, **worklist, resolve=resolve, door="wake",
41
  tool_call=tool_call)
42
  item = assistant_worklists.metadata(rt, saved["id"])
 
32
 
33
  worklist = run(session, {})
34
  tool_call = {"name": "diagnostic_run", "args": {"key": "stockouts"}}
35
+ if worklist is None:
36
  return {"ok": True, "detail": "Nothing to send."}
37
  if (not worklist.get("validation")
38
  or any(check.get("ok") is not True for check in worklist["validation"])):
39
  raise ValueError("This digest could not be tied to Odoo. Try after the next sync.")
40
+ if not worklist["rows"]:
41
+ return {"ok": True, "detail": "Nothing to send."}
42
  saved = assistant_worklists.save(session, **worklist, resolve=resolve, door="wake",
43
  tool_call=tool_call)
44
  item = assistant_worklists.metadata(rt, saved["id"])
api/odoo_default_scope.py CHANGED
@@ -6,14 +6,17 @@ def applies(table_key):
6
  return rel.base_table_key(table_key) in {rel.ORDERS_KEY, rel.ORDER_LINES_KEY}
7
 
8
 
9
- def leaf_present(tree):
 
 
 
10
  if isinstance(tree, list):
11
- return any(leaf_present(node) for node in tree)
12
  if not isinstance(tree, dict):
13
  return False
14
  if tree.get("colId") == "wholesale_scope":
15
  return True
16
- return any(leaf_present(tree.get(key)) for key in ("nodes", "children"))
17
 
18
 
19
  def limit_entry():
@@ -24,7 +27,7 @@ def limit_entry():
24
 
25
  def apply(table_key, query, read):
26
  """Narrow SQL using the source expression even when the scope field is hidden."""
27
- if not applies(table_key) or leaf_present(query.get("filters")):
28
  return read
29
  expression = read["spec"]["cols"].get("wholesale_scope")
30
  if expression is None:
 
6
  return rel.base_table_key(table_key) in {rel.ORDERS_KEY, rel.ORDER_LINES_KEY}
7
 
8
 
9
+ def leaf_present(tree, columns=None):
10
+ # An ignored hidden predicate cannot select a different summary population.
11
+ if columns is not None and "wholesale_scope" not in columns:
12
+ return False
13
  if isinstance(tree, list):
14
+ return any(leaf_present(node, columns) for node in tree)
15
  if not isinstance(tree, dict):
16
  return False
17
  if tree.get("colId") == "wholesale_scope":
18
  return True
19
+ return any(leaf_present(tree.get(key), columns) for key in ("nodes", "children"))
20
 
21
 
22
  def limit_entry():
 
27
 
28
  def apply(table_key, query, read):
29
  """Narrow SQL using the source expression even when the scope field is hidden."""
30
+ if not applies(table_key) or leaf_present(query.get("filters"), read.get("cols", {})):
31
  return read
32
  expression = read["spec"]["cols"].get("wholesale_scope")
33
  if expression is None:
api/routes_odoo_tables.py CHANGED
@@ -782,6 +782,7 @@ def _odoo_sources():
782
  "where": f"{rel._CONFIRMED} AND partner_id IS NOT NULL",
783
  "id": "id",
784
  "needs_excluded": True, # `wholesale_scope` is resolved against the excluded set
 
785
  "cols": {
786
  "order_no": ("name", _s),
787
  "odoo_id": ("id", _i),
@@ -839,6 +840,7 @@ def _odoo_sources():
839
  "where": f"so.{rel._CONFIRMED} AND so.partner_id IS NOT NULL",
840
  "id": "sol.id",
841
  "needs_excluded": True,
 
842
  "cols": {
843
  "odoo_id": ("sol.id", _i),
844
  "order_no": ("so.name", _s),
@@ -4095,7 +4097,7 @@ def odoo_table_aggregates(table_key: str, body: dict = Body(default=None),
4095
  with _materialised_read(table_key, session, query, payload.get("related"), ids=ids) as read:
4096
  import odoo_default_scope
4097
  if (odoo_default_scope.applies(table_key)
4098
- and not odoo_default_scope.leaf_present(query.get("filters"))):
4099
  scope_cur, _ = _mirror_cur(session, table_key)
4100
  source = _source_for(scope_cur, table_key)
4101
  if source is None:
 
782
  "where": f"{rel._CONFIRMED} AND partner_id IS NOT NULL",
783
  "id": "id",
784
  "needs_excluded": True, # `wholesale_scope` is resolved against the excluded set
785
+ "default_wholesale_scope": True,
786
  "cols": {
787
  "order_no": ("name", _s),
788
  "odoo_id": ("id", _i),
 
840
  "where": f"so.{rel._CONFIRMED} AND so.partner_id IS NOT NULL",
841
  "id": "sol.id",
842
  "needs_excluded": True,
843
+ "default_wholesale_scope": True,
844
  "cols": {
845
  "odoo_id": ("sol.id", _i),
846
  "order_no": ("so.name", _s),
 
4097
  with _materialised_read(table_key, session, query, payload.get("related"), ids=ids) as read:
4098
  import odoo_default_scope
4099
  if (odoo_default_scope.applies(table_key)
4100
+ and not odoo_default_scope.leaf_present(query.get("filters"), read["cols"])):
4101
  scope_cur, _ = _mirror_cur(session, table_key)
4102
  source = _source_for(scope_cur, table_key)
4103
  if source is None:
api/routes_sheet.py CHANGED
@@ -760,7 +760,7 @@ def distinct_values(database: str, field: str = Query(...),
760
  # Reuse the bound-cell compiler for the FROM, joins, fixed source scope and row wall.
761
  sql, params, _ = sheet_mod.build_aggregate_query(
762
  spec, {"aggregate": {"fn": "count"}}, columns, scope_nodes=[], scope_conj="and",
763
- wall_nodes=wall_nodes, wall_conj=wall_conj)
764
  except (sheet_mod.SheetRefusal, ValueError):
765
  raise err(409, "scope_not_applicable",
766
  f"Your row restriction cannot be applied to {database}. Ask an administrator to review it.")
@@ -3920,6 +3920,11 @@ def _resolve_binding(session: Session, raw_binding, *, today=None, view_memo=Non
3920
  related_bags = _related_leaf_bags(session, database, scope_nodes, needed)
3921
  cur, spec, cols = _mirror(session)(database, metric_window=window, needed=needed)
3922
  cols = dict(cols)
 
 
 
 
 
3923
  if _pivot is not None:
3924
  filter_fields = (sheet_mod.filter_columns_named(scope_nodes)
3925
  - sheet_mod.related_filter_ids(scope_nodes))
@@ -3968,9 +3973,7 @@ def _resolve_binding(session: Session, raw_binding, *, today=None, view_memo=Non
3968
  # two of them name. The contract's ORDER is unchanged: the wall is still applied and still
3969
  # checked against `cols` before the scope reaches the compiler.
3970
 
3971
- # ⚠ NOTHING IS DONE HERE ABOUT `needs_excluded`. `_source_for` has already resolved it into the
3972
- # spec's column expressions; applying it a second time would double-count the exclusion and
3973
- # break every joined spec, whose partner column is `sol.order_partner_id` or `aml.partner_id`.
3974
  try:
3975
  related = related_bags(cur, spec, cols, window=window)
3976
  sql, params, limits = sheet_mod.build_aggregate_query(
@@ -4361,7 +4364,7 @@ def view_rows(workbook_id: str, body: object = Body(default=None),
4361
  count_sql, params, limits = sheet_mod.build_aggregate_query(
4362
  spec, {"aggregate": {"fn": "count"}}, cols, scope_nodes=scope_nodes,
4363
  scope_conj=scope_conj, wall_nodes=wall_nodes, wall_conj=wall_conj, today=today,
4364
- related=related)
4365
  if limits:
4366
  raise ValueError("unapplied view filter")
4367
  except sheet_mod.SheetRefusal as exc:
@@ -4651,6 +4654,11 @@ def drill_cell(workbook_id: str, addr: str, tab: str | None = None,
4651
  with _drill_mirror(session, database, window, needed, databases) as (made, token):
4652
  cur, spec, cols = made
4653
  cols = dict(cols)
 
 
 
 
 
4654
  try:
4655
  related = related_bags(cur, spec, cols, window=window)
4656
  count_sql, count_params, limits = sheet_mod.build_aggregate_query(
 
760
  # Reuse the bound-cell compiler for the FROM, joins, fixed source scope and row wall.
761
  sql, params, _ = sheet_mod.build_aggregate_query(
762
  spec, {"aggregate": {"fn": "count"}}, columns, scope_nodes=[], scope_conj="and",
763
+ wall_nodes=wall_nodes, wall_conj=wall_conj, summary_default=False)
764
  except (sheet_mod.SheetRefusal, ValueError):
765
  raise err(409, "scope_not_applicable",
766
  f"Your row restriction cannot be applied to {database}. Ask an administrator to review it.")
 
3920
  related_bags = _related_leaf_bags(session, database, scope_nodes, needed)
3921
  cur, spec, cols = _mirror(session)(database, metric_window=window, needed=needed)
3922
  cols = dict(cols)
3923
+ # Authored filters need field access; trusted defaults and permanent walls keep the full map.
3924
+ hidden = perm_scope.hidden_keys(session.user, database, spec.get("fields") or [], st=session.runtime)
3925
+ unreadable = sorted(sheet_mod.filter_columns_named(scope_nodes) & hidden)
3926
+ if unreadable:
3927
+ raise err(403, "field_unreadable", f"You cannot filter on field {unreadable[0]} in {database}.")
3928
  if _pivot is not None:
3929
  filter_fields = (sheet_mod.filter_columns_named(scope_nodes)
3930
  - sheet_mod.related_filter_ids(scope_nodes))
 
3973
  # two of them name. The contract's ORDER is unchanged: the wall is still applied and still
3974
  # checked against `cols` before the scope reaches the compiler.
3975
 
3976
+ # The compiler applies the source's summary default unless the scope names a channel filter.
 
 
3977
  try:
3978
  related = related_bags(cur, spec, cols, window=window)
3979
  sql, params, limits = sheet_mod.build_aggregate_query(
 
4364
  count_sql, params, limits = sheet_mod.build_aggregate_query(
4365
  spec, {"aggregate": {"fn": "count"}}, cols, scope_nodes=scope_nodes,
4366
  scope_conj=scope_conj, wall_nodes=wall_nodes, wall_conj=wall_conj, today=today,
4367
+ related=related, summary_default=False)
4368
  if limits:
4369
  raise ValueError("unapplied view filter")
4370
  except sheet_mod.SheetRefusal as exc:
 
4654
  with _drill_mirror(session, database, window, needed, databases) as (made, token):
4655
  cur, spec, cols = made
4656
  cols = dict(cols)
4657
+ # Apply the aggregate's field wall before compiling either the count or the IDs.
4658
+ hidden = perm_scope.hidden_keys(session.user, database, spec.get("fields") or [], st=session.runtime)
4659
+ unreadable = sorted(sheet_mod.filter_columns_named(scope_nodes) & hidden)
4660
+ if unreadable:
4661
+ raise err(403, "field_unreadable", f"You cannot filter on field {unreadable[0]} in {database}.")
4662
  try:
4663
  related = related_bags(cur, spec, cols, window=window)
4664
  count_sql, count_params, limits = sheet_mod.build_aggregate_query(
api/test_native_population.py ADDED
@@ -0,0 +1,486 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Synthetic H7536 population controls shared by the owning verifiers."""
2
+ import copy
3
+ import datetime as dt
4
+ import io
5
+ import sys
6
+ import unittest
7
+ from pathlib import Path
8
+ from unittest.mock import patch
9
+
10
+
11
+ PLATFORM = str(Path(__file__).resolve().parents[2] / "platform")
12
+ if PLATFORM not in sys.path:
13
+ sys.path.insert(0, PLATFORM)
14
+
15
+
16
+ INVOICES = "ut_odoo_invoices"
17
+ CUSTOMERS = "customer_data"
18
+ PRODUCTS = "product_data"
19
+ LINES = "ut_odoo_order_lines"
20
+
21
+
22
+ def _unrestricted(session, role="admin"):
23
+ session.user.update(role=role, bus="all")
24
+ for grant in session.user.get("perms", {}).values():
25
+ grant.pop("filter", None)
26
+
27
+
28
+ def _fences(domain, field="id"):
29
+ return [set(node[2]) for node in domain
30
+ if isinstance(node, tuple) and node[:2] == (field, "in")]
31
+
32
+
33
+ def _library():
34
+ import verify_automation_scheduler as scheduler
35
+
36
+ return scheduler
37
+
38
+
39
+ class DiagnosticPopulation(unittest.TestCase):
40
+ def test_archived_invoice_omissions(self):
41
+ import diag_aging
42
+ import diag_recon
43
+
44
+ for role in ("admin", "user"):
45
+ for module, key in ((diag_aging, "ar_aging"), (diag_recon, "bank_reconciliation_gaps")):
46
+ with self.subTest(role=role, module=key), _library()._library_fixture(3) as fx:
47
+ _unrestricted(fx.session, role)
48
+ full = module.run(fx.session, {})
49
+ self.assertTrue(all(row["ok"] for row in full["validation"]))
50
+ fx.sources[INVOICES][:] = [row for row in fx.sources[INVOICES] if row["odoo_id"] != 1]
51
+ fx.native.calls.clear()
52
+ missing = module.run(fx.session, {})
53
+ self.assertFalse(all(row["ok"] for row in missing["validation"]))
54
+ if module is diag_aging:
55
+ bucket = lambda result: next(row["amount"] for row in result["rows"] if row["bucket"] == "1-30")
56
+ self.assertEqual((bucket(full), bucket(missing)), (225, 125))
57
+ else:
58
+ customer = lambda result: next(row for row in result["rows"] if row["partner_id"] == 1)
59
+ self.assertEqual((customer(full)["open_docs"], customer(full)["gap"]), (100, 150))
60
+ self.assertEqual((customer(missing)["open_docs"], customer(missing)["gap"]), (0, 250))
61
+ self.assertTrue(any(row["gap"] < 0 for row in full["rows"]))
62
+ self.assertTrue(any(kind == "rows" and model == "res.partner" and not _fences(domain)
63
+ for kind, model, domain in fx.native.calls))
64
+ self.assertTrue(any(kind == "ledger" and model == "account.move" and not _fences(domain)
65
+ for kind, model, domain in fx.native.calls))
66
+ with self.assertRaises(ValueError):
67
+ fx.tools.diagnostic_run(fx.session, {"key": key})
68
+ self.assertEqual(fx.rt.writes, 0)
69
+
70
+ def test_empty_mirrors_and_same_count_amount_drift(self):
71
+ import diag_aging
72
+ import diag_recon
73
+
74
+ for module in (diag_aging, diag_recon):
75
+ for change in ("empty", "amount", "cancelling_amounts", "missing_zero"):
76
+ with self.subTest(module=module.__name__, change=change), _library()._library_fixture(3) as fx:
77
+ _unrestricted(fx.session)
78
+ if change == "empty":
79
+ fx.sources[INVOICES].clear()
80
+ elif change == "missing_zero":
81
+ for source in (fx.native.records, fx.native.ledger):
82
+ source["account.move"][0]["amount_residual_signed"] = 0
83
+ fx.sources[INVOICES].pop(0)
84
+ else:
85
+ fx.sources[INVOICES][0]["residual"] += 10
86
+ if change == "cancelling_amounts":
87
+ fx.sources[INVOICES][1]["residual"] -= 10
88
+ result = module.run(fx.session, {})
89
+ self.assertFalse(all(row["ok"] for row in result["validation"]))
90
+ for module in (diag_aging, diag_recon):
91
+ with self.subTest(empty_native=module.__name__), _library()._library_fixture(0) as fx:
92
+ self.assertTrue(all(row["ok"] for row in module.run(fx.session, {})["validation"]))
93
+
94
+ def test_payment_state_remains_a_mirror_predicate(self):
95
+ import diag_aging
96
+ import diag_recon
97
+
98
+ for module in (diag_aging, diag_recon):
99
+ with self.subTest(module=module.__name__), _library()._library_fixture(3) as fx:
100
+ for source in (fx.native.records, fx.native.ledger):
101
+ source["account.move"][0]["payment_state"] = "paid"
102
+ fx.sources[INVOICES][0]["payment_state"] = "paid"
103
+ self.assertTrue(all(row["ok"] for row in module.run(fx.session, {})["validation"]))
104
+ fx.sources[INVOICES][0]["payment_state"] = "partial"
105
+ self.assertFalse(all(row["ok"] for row in module.run(fx.session, {})["validation"]))
106
+
107
+ def test_reconciliation_missing_customers(self):
108
+ import diag_recon
109
+
110
+ for empty in (False, True):
111
+ with self.subTest(empty=empty), _library()._library_fixture(3) as fx:
112
+ _unrestricted(fx.session)
113
+ fx.sources[CUSTOMERS][:] = [] if empty else fx.sources[CUSTOMERS][1:]
114
+ result = diag_recon.run(fx.session, {})
115
+ self.assertFalse(all(row["ok"] for row in result["validation"]))
116
+ self.assertTrue(any(model == "res.partner" and not _fences(domain)
117
+ for _kind, model, domain in fx.native.calls))
118
+
119
+ def test_stock_product_and_line_omissions(self):
120
+ import diag_stockout
121
+
122
+ for role in ("admin", "user"):
123
+ for database in (PRODUCTS, LINES):
124
+ for empty in (False, True):
125
+ with self.subTest(role=role, database=database, empty=empty), _library()._library_fixture(3) as fx:
126
+ _unrestricted(fx.session, role)
127
+ full = diag_stockout.run(fx.session, {})
128
+ self.assertEqual([row["product_id"] for row in full["rows"]], [1, 2])
129
+ fx.sources[database][:] = [] if empty else fx.sources[database][1:]
130
+ fx.native.calls.clear()
131
+ missing = diag_stockout.run(fx.session, {})
132
+ self.assertEqual([row["product_id"] for row in missing["rows"]], [] if empty else [2])
133
+ self.assertFalse(all(row["ok"] for row in missing["validation"]))
134
+ sale_domains = [domain for kind, model, domain in fx.native.calls if model == "sale.order.line"]
135
+ self.assertTrue(sale_domains)
136
+ self.assertTrue(all(not _fences(domain) for domain in sale_domains))
137
+ native_products = [domain for kind, model, domain in fx.native.calls
138
+ if kind == "ledger" and model == "product.product"]
139
+ self.assertTrue(native_products)
140
+ self.assertTrue(all(("active", "=", True) in domain for domain in native_products))
141
+ self.assertTrue(all(len(_fences(domain)) == 1 and 1 in _fences(domain)[0]
142
+ for domain in native_products))
143
+ with self.assertRaises(ValueError):
144
+ fx.tools.diagnostic_run(fx.session, {"key": "stockouts"})
145
+ self.assertEqual(fx.rt.writes, 0)
146
+
147
+ def test_native_paging_and_all_channel_sales(self):
148
+ import diag_aging
149
+ import diag_recon
150
+ import diag_stockout
151
+
152
+ for module in (diag_aging, diag_recon, diag_stockout):
153
+ total = 2001 if module is diag_stockout else 1001
154
+ with self.subTest(module=module.__name__), _library()._library_fixture(total) as fx:
155
+ _unrestricted(fx.session)
156
+ result = module.run(fx.session, {})
157
+ self.assertTrue(all(row["ok"] for row in result["validation"]))
158
+ model = "res.partner" if module is diag_recon else "account.move"
159
+ if module is not diag_stockout:
160
+ self.assertTrue(any(m == model and offset >= 1000 and not _fences(domain)
161
+ for m, offset, domain in fx.native.pages))
162
+ else:
163
+ self.assertTrue(any(m == "product.product" and offset >= 500
164
+ and len(_fences(domain)) == 1 for m, offset, domain in fx.native.pages))
165
+ ids = {row["product_id"] for row in result["rows"]}
166
+ self.assertIn(5, ids) # GIFTWARE still sells.
167
+ self.assertFalse(ids.intersection({7, 11, 13, 17, 19}))
168
+ for _kind, native_model, domain in fx.native.calls:
169
+ if native_model == "sale.order.line":
170
+ self.assertIn(("product_uom_qty", ">", 0), domain)
171
+ self.assertIn(("order_id.state", "in", ["sale", "done"]), domain)
172
+ self.assertIn(("order_id.date_order", ">=", "2026-07-08 00:00:00"), domain)
173
+ self.assertFalse(any(isinstance(node, tuple) and "partner" in node[0] for node in domain))
174
+
175
+
176
+ class PermissionPopulation(unittest.TestCase):
177
+ def test_independent_source_walls_and_intersections(self):
178
+ import diag_aging
179
+ import diag_recon
180
+ import diag_stockout
181
+
182
+ cases = [
183
+ (diag_aging, {INVOICES: {1}}, {"account.move": {"id": {1}}}),
184
+ (diag_recon, {CUSTOMERS: {1}}, {"account.move": {"partner_id": {1}}, "res.partner": {"id": {1}}}),
185
+ (diag_recon, {INVOICES: {1}}, {"account.move": {"id": {1}}}),
186
+ (diag_recon, {INVOICES: {2}, CUSTOMERS: {1}},
187
+ {"account.move": {"id": {2}, "partner_id": {1}}, "res.partner": {"id": {1}}}),
188
+ (diag_stockout, {PRODUCTS: {1}}, {"product.product": {"id": {1}}}),
189
+ (diag_stockout, {LINES: {1}}, {"sale.order.line": {"id": {1}}}),
190
+ (diag_stockout, {PRODUCTS: {1}, LINES: {2}},
191
+ {"product.product": {"id": {1}}, "sale.order.line": {"id": {2}}}),
192
+ ]
193
+ for role in ("admin", "user"):
194
+ for module, allowed, expected in cases:
195
+ with self.subTest(role=role, module=module.__name__, allowed=allowed), \
196
+ _library()._library_fixture(3, permitted=allowed) as fx:
197
+ fx.session.user["role"] = role
198
+ result = module.run(fx.session, {})
199
+ self.assertTrue(all(row["ok"] for row in result["validation"]))
200
+ for _kind, model, domain in fx.native.calls:
201
+ for field, ids in expected.get(model, {}).items():
202
+ self.assertIn(ids, _fences(domain, field))
203
+ if module is diag_aging:
204
+ self.assertEqual(sum(row["amount"] for row in result["rows"]), 100)
205
+ elif module is diag_stockout:
206
+ self.assertEqual([row["product_id"] for row in result["rows"]],
207
+ [] if len(allowed) == 2 else [1])
208
+ elif CUSTOMERS in allowed:
209
+ self.assertEqual([(row["partner_id"], row["open_docs"], row["gap"]) for row in result["rows"]],
210
+ [(1, 0, 250)] if INVOICES in allowed else [(1, 100, 150)])
211
+ else:
212
+ self.assertEqual({row["partner_id"]: (row["open_docs"], row["gap"]) for row in result["rows"]},
213
+ {1: (100, 150), 3: (0, 120)})
214
+
215
+ def test_bu_scope_without_permanent_filters(self):
216
+ import diag_aging
217
+ import diag_recon
218
+ import diag_stockout
219
+ from core import perm_scope
220
+
221
+ allowed = {database: {1, 2} for database in (INVOICES, CUSTOMERS, PRODUCTS, LINES)}
222
+ for module in (diag_aging, diag_recon, diag_stockout):
223
+ with self.subTest(module=module.__name__), _library()._library_fixture(3, permitted=allowed) as fx:
224
+ _unrestricted(fx.session, "user")
225
+ fx.session.user["bus"] = [6]
226
+ self.assertFalse(perm_scope.row_scope_applies(fx.session.user, INVOICES, st=fx.rt))
227
+ result = module.run(fx.session, {})
228
+ self.assertTrue(all(row["ok"] for row in result["validation"]))
229
+ self.assertTrue(all({1, 2} in _fences(domain) for _kind, _model, domain in fx.native.calls))
230
+
231
+ def test_recipient_wall_without_explicit_filter(self):
232
+ import diag_aging
233
+ import diag_recon
234
+ import diag_stockout
235
+ from core import perm_scope
236
+
237
+ for module, database in ((diag_aging, INVOICES), (diag_recon, INVOICES), (diag_stockout, LINES)):
238
+ with self.subTest(module=module.__name__), _library()._library_fixture(3, permitted={database: {1}}) as fx:
239
+ _unrestricted(fx.session)
240
+ fx.rt.values["user_tables"] = {database: {"rowAudienceField": "readers", "fields": [
241
+ {"key": "readers", "type": "user"}]}}
242
+ self.assertTrue(perm_scope.row_scope_applies(fx.session.user, database, st=fx.rt))
243
+ result = module.run(fx.session, {})
244
+ self.assertTrue(all(row["ok"] for row in result["validation"]))
245
+ model = "sale.order.line" if database == LINES else "account.move"
246
+ self.assertTrue(all({1} in _fences(domain) for _kind, key, domain in fx.native.calls if key == model))
247
+
248
+ def test_hidden_fields_and_tenant_mismatch_precede_native_reads(self):
249
+ from fastapi import HTTPException
250
+ import diag_aging
251
+ import diag_recon
252
+ import diag_stockout
253
+
254
+ for module, database, field in ((diag_aging, INVOICES, "residual"), (diag_aging, INVOICES, "payment_state"),
255
+ (diag_recon, CUSTOMERS, "ar_open"), (diag_stockout, PRODUCTS, "on_hand")):
256
+ with self.subTest(module=module.__name__), _library()._library_fixture(3, hidden={database: {field}}) as fx:
257
+ _unrestricted(fx.session)
258
+ with self.assertRaises(HTTPException):
259
+ module.run(fx.session, {})
260
+ self.assertFalse(fx.native.calls)
261
+ with _library()._library_fixture(3) as fx:
262
+ fx.session.tenant = "foreign"
263
+ with self.assertRaises(ValueError):
264
+ module.run(fx.session, {})
265
+ self.assertFalse(fx.native.calls)
266
+ self.assertFalse(fx.reads)
267
+
268
+
269
+ class DigestPopulation(unittest.TestCase):
270
+ def test_incomplete_digests_never_save_or_deliver(self):
271
+ import loopy_digests
272
+
273
+ for check_id, database in (("ar_chase_weekly", INVOICES), ("ar_chase_weekly", CUSTOMERS),
274
+ ("stockout_daily", PRODUCTS), ("stockout_daily", LINES)):
275
+ for empty in (False, True):
276
+ with self.subTest(check=check_id, database=database, empty=empty), \
277
+ _library()._digest_fixture(check_id, total=3) as fx:
278
+ _unrestricted(fx.session)
279
+ fx.sources[database][:] = [] if empty else fx.sources[database][1:]
280
+ with patch("assistant_worklists.save") as save, patch("ping_doors.notify") as notify:
281
+ with self.assertRaises(ValueError):
282
+ loopy_digests._digest(fx.rt, fx.rt.key, {"owner": "alice", "scheduleId": "population"},
283
+ now=fx.now, chase=check_id == "ar_chase_weekly")
284
+ save.assert_not_called()
285
+ notify.assert_not_called()
286
+
287
+ def test_empty_output_validation_precedes_nothing_to_send(self):
288
+ import loopy_digests
289
+
290
+ for checks in ([], [{"check": "native population", "ok": False}]):
291
+ with self.subTest(checks=checks), _library()._digest_fixture("stockout_daily", total=3) as fx:
292
+ with patch("diag_stockout.run", return_value={"rows": [], "validation": checks}), \
293
+ patch("assistant_worklists.save") as save, patch("ping_doors.notify") as notify:
294
+ with self.assertRaises(ValueError):
295
+ loopy_digests._digest(fx.rt, fx.rt.key, {"owner": "alice", "scheduleId": "population"},
296
+ now=fx.now, chase=False)
297
+ save.assert_not_called()
298
+ notify.assert_not_called()
299
+
300
+
301
+ class BulkPopulation(unittest.TestCase):
302
+ def fixture(self, count=40):
303
+ import verify_loopy_tools
304
+
305
+ return verify_loopy_tools.bulk_fixture(count=count)
306
+
307
+ def test_archived_chase_omission_and_digest(self):
308
+ import ast
309
+ from types import ModuleType
310
+
311
+ with self.fixture() as fx:
312
+ session = fx.session()
313
+ _unrestricted(session)
314
+ full = fx.bulk.chase_list(session, fx.links)
315
+ self.assertEqual(next(row["amount"] for row in full["rows"] if row["customer"].endswith("customer 1")), 126.5)
316
+ tables = session.runtime.get("user_tables")
317
+ tables[INVOICES]["rows"].pop("1")
318
+ session.runtime.update("user_tables", lambda _raw: tables)
319
+ self.assertEqual((len(fx.native[session.tenant]), len(tables[INVOICES]["rows"])), (43, 42))
320
+ self.assertEqual(sum(row["residual"] for row in tables[INVOICES]["rows"].values()
321
+ if row["partner_id"] == 1001), 25.5)
322
+ with self.assertRaisesRegex(ValueError, "could not be tied to Odoo"):
323
+ fx.bulk.chase_list(session, fx.links)
324
+ self.assertTrue(all(not _fences(domain) and not _fences(domain, "partner_id")
325
+ for _tenant, domain, _offset in fx.native_calls))
326
+ tree = ast.parse(Path(__file__).with_name("loopy_digests.py").read_text(encoding="utf-8"))
327
+ node = next(node for node in tree.body if isinstance(node, ast.FunctionDef) and node.name == "_digest")
328
+ namespace = {}
329
+ exec(compile(ast.Module(body=[node], type_ignores=[]), "loopy_digests.py", "exec"), namespace)
330
+ door = ModuleType("ping_doors")
331
+ door.notify = lambda *_a, **_kw: self.fail("A fixture attempted delivery.")
332
+ with patch.object(fx.runtime, "session_for", return_value=session), \
333
+ patch.dict(sys.modules, {"ping_doors": door}), \
334
+ patch.object(fx.worklists, "save", wraps=fx.worklists.save) as save:
335
+ with self.assertRaises(ValueError):
336
+ namespace["_digest"](session.runtime, session.tenant, {"owner": session.uname, "scheduleId": "population"},
337
+ now=dt.datetime.now(dt.timezone.utc), chase=True)
338
+ save.assert_not_called()
339
+
340
+ def test_empty_and_missing_customer_mirrors_and_amount_drift(self):
341
+ for role in ("admin", "user"):
342
+ for change in ("empty_invoices", "zero_invoices", "missing_customer", "empty_customers", "amount", "cancelling_amounts"):
343
+ with self.subTest(role=role, change=change), self.fixture() as fx:
344
+ session = fx.session()
345
+ _unrestricted(session, role)
346
+ rt = session.runtime
347
+ if "customer" in change:
348
+ stamp, people = rt.pool_cache[("pool", None, None)]
349
+ rt.pool_cache[("pool", None, None)] = (stamp + "-omitted", [] if change == "empty_customers" else people[1:])
350
+ else:
351
+ tables = rt.get("user_tables")
352
+ if change == "empty_invoices":
353
+ tables[INVOICES]["rows"].clear()
354
+ elif change == "zero_invoices":
355
+ for row in tables[INVOICES]["rows"].values():
356
+ row["residual"] = 0
357
+ else:
358
+ tables[INVOICES]["rows"]["1"]["residual"] += 10
359
+ if change == "cancelling_amounts":
360
+ tables[INVOICES]["rows"]["43"]["residual"] -= 10
361
+ rt.update("user_tables", lambda _raw: tables)
362
+ before = (copy.deepcopy(rt.documents), copy.deepcopy(rt.blobs))
363
+ with self.assertRaisesRegex(ValueError, "could not be tied to Odoo"):
364
+ fx.bulk.chase_list(session, fx.links)
365
+ self.assertEqual((rt.documents, rt.blobs), before)
366
+
367
+ def test_source_walls_admin_filters_and_disjoint_intersection(self):
368
+ for role in ("admin", "user"):
369
+ for walls in ((INVOICES,), (CUSTOMERS,), (INVOICES, CUSTOMERS)):
370
+ with self.subTest(role=role, walls=walls), self.fixture() as fx:
371
+ session = fx.session()
372
+ session.user["role"] = role
373
+ for database in (INVOICES, CUSTOMERS):
374
+ if database not in walls:
375
+ session.user["perms"][database].pop("filter", None)
376
+ full = fx.bulk.chase_list(session, fx.links)
377
+ self.assertTrue(all(check["ok"] for check in full["validation"]))
378
+ self.assertEqual(next(row["amount"] for row in full["rows"] if row["customer"].endswith("customer 1")), 126.5)
379
+ self.assertEqual(len(full["rows"]), 40 if len(walls) == 2 else 41)
380
+ for _tenant, domain, _offset in fx.native_calls:
381
+ self.assertEqual(bool(_fences(domain)), INVOICES in walls)
382
+ self.assertEqual(bool(_fences(domain, "partner_id")), CUSTOMERS in walls)
383
+ if INVOICES in walls:
384
+ self.assertNotIn(42, _fences(domain)[0])
385
+ if CUSTOMERS in walls:
386
+ self.assertNotIn(1041, _fences(domain, "partner_id")[0])
387
+ with self.fixture() as fx:
388
+ session = fx.session()
389
+ for database, pid in ((INVOICES, 1001), (CUSTOMERS, 1002)):
390
+ session.user["perms"][database]["filter"] = {"conj": "and", "nodes": [
391
+ {"colId": "partner_id", "op": "eq", "value": pid}]}
392
+ self.assertIsNone(fx.bulk.chase_list(session, fx.links))
393
+ self.assertTrue(fx.native_calls)
394
+ self.assertEqual(_fences(fx.native_calls[-1][1], "partner_id"), [{1002}])
395
+ self.assertEqual(_fences(fx.native_calls[-1][1]), [{1, 43}])
396
+
397
+ def test_recipient_wall_and_hidden_fields(self):
398
+ with self.fixture() as fx:
399
+ session = fx.session()
400
+ _unrestricted(session)
401
+ rt = session.runtime
402
+ tables = rt.get("user_tables")
403
+ table = tables[INVOICES]
404
+ table["fields"].append({"key": "readers", "label": "Readers", "type": "user"})
405
+ table["rowAudienceField"] = "readers"
406
+ for key, row in table["rows"].items():
407
+ row["readers"] = ["ann"] if key in ("1", "43") else ["bob"]
408
+ rt.update("user_tables", lambda _raw: tables)
409
+ result = fx.bulk.chase_list(session, fx.links)
410
+ self.assertEqual([row["amount"] for row in result["rows"]], [126.5])
411
+ self.assertTrue(all(_fences(domain) == [{1, 43}] for _tenant, domain, _offset in fx.native_calls))
412
+ self.assertTrue(all(not _fences(domain, "partner_id") for _tenant, domain, _offset in fx.native_calls))
413
+ fx.native_calls.clear()
414
+ session.user["perms"][INVOICES]["hiddenFields"] = ["residual"]
415
+ from fastapi import HTTPException
416
+ with self.assertRaises(HTTPException):
417
+ fx.bulk.chase_list(session, fx.links)
418
+ self.assertFalse(fx.native_calls)
419
+
420
+ def test_bu_scope_without_row_filters(self):
421
+ from core import perm_scope
422
+
423
+ with self.fixture() as fx:
424
+ session = fx.session()
425
+ _unrestricted(session, "user")
426
+ session.user["bus"] = [6]
427
+ self.assertFalse(perm_scope.row_scope_applies(session.user, INVOICES, st=session.runtime))
428
+ fx.current["session"] = session
429
+ rows = [row for row in session.runtime.get("user_tables")[INVOICES]["rows"].values()
430
+ if row["partner_id"] == 1001]
431
+ grouped = fx.bulk._group(rows, fx.today)
432
+ checks = fx.bulk.validate(session, rows, rows, grouped, today=fx.today, partner_ids=[1001])
433
+ self.assertTrue(all(check["ok"] for check in checks))
434
+ self.assertEqual(float(grouped[1001]["amount"]), 126.5)
435
+ for _tenant, domain, _offset in fx.native_calls:
436
+ self.assertEqual(_fences(domain), [{1, 43}])
437
+ self.assertEqual(_fences(domain, "partner_id"), [{1001}])
438
+
439
+ def test_unrestricted_paging_true_empty_and_tenant_mismatch(self):
440
+ with self.fixture(count=1001) as fx:
441
+ session = fx.session()
442
+ _unrestricted(session)
443
+ result = fx.bulk.chase_list(session, fx.links)
444
+ self.assertEqual(len(result["rows"]), 1003)
445
+ self.assertEqual([offset for _tenant, _domain, offset in fx.native_calls], [0, 500, 1000, 1004])
446
+ self.assertTrue(all(not _fences(domain) for _tenant, domain, _offset in fx.native_calls))
447
+ with self.fixture() as fx:
448
+ session = fx.session()
449
+ _unrestricted(session)
450
+ tables = session.runtime.get("user_tables")
451
+ tables[INVOICES]["rows"].clear()
452
+ session.runtime.update("user_tables", lambda _raw: tables)
453
+ fx.native[session.tenant].clear()
454
+ self.assertIsNone(fx.bulk.chase_list(session, fx.links))
455
+ self.assertTrue(fx.native_calls)
456
+ fx.native_calls.clear()
457
+ fx.calls.clear()
458
+ session.tenant = "foreign"
459
+ with self.assertRaises(ValueError):
460
+ fx.bulk.chase_list(session, fx.links)
461
+ self.assertFalse(fx.native_calls)
462
+ self.assertFalse(fx.calls)
463
+
464
+
465
+ def _suite(classes):
466
+ return unittest.TestSuite(unittest.defaultTestLoader.loadTestsFromTestCase(cls) for cls in classes)
467
+
468
+
469
+ def verify_diagnostics():
470
+ stream = io.StringIO()
471
+ result = unittest.TextTestRunner(stream=stream, verbosity=2).run(
472
+ _suite((DiagnosticPopulation, PermissionPopulation, DigestPopulation)))
473
+ if not result.wasSuccessful():
474
+ raise AssertionError(stream.getvalue())
475
+
476
+
477
+ def verify_bulk(checks):
478
+ stream = io.StringIO()
479
+ result = unittest.TextTestRunner(stream=stream, verbosity=2).run(_suite((BulkPopulation,)))
480
+ checks.check("H7536 independent chase populations", result.wasSuccessful())
481
+ if not result.wasSuccessful():
482
+ print(stream.getvalue())
483
+
484
+
485
+ if __name__ == "__main__":
486
+ unittest.main()
api/test_sheet_sales_scope.py ADDED
@@ -0,0 +1,373 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Synthetic H7537 coverage, invoked by verify_sheet without a live store or Odoo."""
2
+
3
+
4
+ def run(check):
5
+ import copy
6
+ import json
7
+ import socket
8
+ from contextlib import ExitStack
9
+ from types import SimpleNamespace
10
+ from unittest.mock import patch
11
+
12
+ import duckdb
13
+ import _fake_store
14
+ import odoo_default_scope
15
+ import odoo_relational as rel
16
+ import routes_odoo_tables as rot
17
+ import routes_sheet as rs
18
+ from core import perm_scope, sheets, user_tables, view_templates
19
+ from deps import Session
20
+ from fastapi import HTTPException
21
+ from modules import sheet
22
+
23
+ print("\n[H7537. Sheet sales scope, production specs and synthetic rows]")
24
+ fake = _fake_store.FakeStore()
25
+ fake.tenant = SimpleNamespace(config={"modules": "all"})
26
+ sources = rot._odoo_sources()
27
+ sales = (rel.ORDERS_KEY, rel.ORDER_LINES_KEY)
28
+ check("H7537 only orders and order lines declare a wholesale summary default",
29
+ {key for key, spec in sources.items() if spec.get("default_wholesale_scope")}
30
+ == set(sales))
31
+ fields = {key: factory() for _bucket, key, _label, factory in rel.TABLES if key in sales}
32
+ registry = {key: copy.deepcopy(sources[key]) for key in sales}
33
+ for key in sales:
34
+ namespaced = rel.table_key_for(key, "fixture_connection")
35
+ registry[namespaced] = copy.deepcopy(sources[key])
36
+ fields[namespaced] = rel.namespaced_fields(fields[key], "fixture_connection")
37
+ fake.put(user_tables.STORE_KEY, {
38
+ key: {"key": key, "name": key, "createdBy": "reader", "fields": value, "rows": {}}
39
+ for key, value in fields.items()})
40
+ notice = [odoo_default_scope.limit_entry()]
41
+ included = {"colId": "wholesale_scope", "op": "eq", "value": "1"}
42
+ excluded = {"colId": "wholesale_scope", "op": "isEmpty"}
43
+
44
+ def session(role="admin", wall=None, hidden=()):
45
+ return Session(tenant="h7537_fixture", claims={}, runtime=fake, user={
46
+ "username": "owner" if role == "admin" else "reader", "role": role,
47
+ "modules": "all", "perms": {key: {
48
+ "access": True, "filter": wall, "hiddenFields": list(hidden)} for key in fields}})
49
+
50
+ admin = session()
51
+ book = sheets.create_workbook(fake, "Scope fixture", created_by="owner")
52
+ tab = book["sheets"][0]["id"]
53
+
54
+ def binding(key, fn="sum", nodes=None, conj="and", scope=None):
55
+ field = "amount_untaxed" if rel.base_table_key(key) == rel.ORDERS_KEY else "price_subtotal"
56
+ return {"database": key, "aggregate": {"fn": fn, **({"field": field} if fn != "count" else {})},
57
+ "scope": scope or {"filter": {"nodes": nodes or [], "conj": conj}}}
58
+
59
+ def drill(reader, raw, draft_filter=None):
60
+ current = sheets.get_workbook(fake, book["id"])
61
+ current["sheets"][0]["cells"]["A1"] = {"kind": "bound", "binding": raw}
62
+ sheets.put_workbook(fake, book["id"], current)
63
+ return rs.drill_cell(book["id"], "A1", tab=tab, session=reader, offset=0, limit=100,
64
+ draft_filter=draft_filter)
65
+
66
+ def refusal(call):
67
+ try:
68
+ call()
69
+ except HTTPException as exc:
70
+ return exc.status_code, (exc.detail.get("error") or {}).get("code")
71
+ return None
72
+
73
+ def hidden_refused(reader, raw, label):
74
+ check("H7537 hidden authored filter refuses sum " + label,
75
+ refusal(lambda: rs._resolve_binding(reader, raw)) == (403, "field_unreadable"))
76
+ count_binding = {**raw, "aggregate": {"fn": "count"}}
77
+ check("H7537 hidden authored filter refuses count " + label,
78
+ refusal(lambda: rs._resolve_binding(reader, count_binding)) == (403, "field_unreadable"))
79
+ check("H7537 hidden authored filter refuses drill " + label,
80
+ refusal(lambda: drill(reader, raw)) == (403, "field_unreadable"))
81
+
82
+ with ExitStack() as stack:
83
+ stack.enter_context(patch.object(socket.socket, "connect",
84
+ side_effect=AssertionError("H7537 network call")))
85
+ stack.enter_context(_fake_store.installed(fake))
86
+ cur = duckdb.connect(":memory:")
87
+ stack.callback(cur.close)
88
+ cur.execute("CREATE TABLE sale_order(id BIGINT, name VARCHAR, state VARCHAR, "
89
+ "partner_id BIGINT, partner_name VARCHAR, amount_untaxed DOUBLE, "
90
+ "team_name VARCHAR, team_id BIGINT)")
91
+ cur.execute("CREATE TABLE sale_order_line(id BIGINT, order_id BIGINT, "
92
+ "order_partner_id BIGINT, order_partner_name VARCHAR, price_subtotal DOUBLE)")
93
+
94
+ def insert(rows):
95
+ cur.executemany("INSERT INTO sale_order VALUES (?, ?, ?, ?, ?, ?, ?, ?)", rows)
96
+ cur.executemany("INSERT INTO sale_order_line VALUES (?, ?, ?, ?, ?)",
97
+ [(row[0], row[0], row[3], row[4], row[5]) for row in rows])
98
+
99
+ insert([(1, "RI-1", "sale", 7, "Royal customer", 10, "Royal", 6),
100
+ (2, "RI-2", "done", 99, "GIFTWARE DEALS", 90, "Royal", 6)])
101
+ stack.enter_context(patch.object(rot, "_sources", return_value=registry))
102
+ stack.enter_context(patch.object(rel, "excluded_ids", return_value={99}))
103
+ stack.enter_context(patch.object(rot, "_mirror_cur", return_value=(cur, None)))
104
+ stack.enter_context(patch.object(rot, "_page_defs", return_value=(fake, None)))
105
+ # Link decoration is outside the aggregate and drill population contract.
106
+ stack.enter_context(patch.object(rot, "_fill_odoo_link_cells", return_value=None))
107
+ # Cache identity is stubbed; database admission, field closure and wall compilation are real.
108
+ stack.enter_context(patch.object(rs, "_drill_tokens", return_value={}))
109
+ stack.enter_context(patch.object(perm_scope, "wall_stamp", return_value={}))
110
+
111
+ def compiled(key, nodes=None, conj="and", wall=None, hidden=()):
112
+ # Resolve the same production dictionaries used by the archived release observer.
113
+ spec = rot._source_for(cur, key)
114
+ columns = rot._filter_columns(spec, fields[key])
115
+ columns = {name: value for name, value in columns.items() if name not in hidden}
116
+ before = copy.deepcopy((spec, columns, nodes, wall))
117
+ args = {"scope_nodes": nodes or [], "scope_conj": conj,
118
+ "wall_nodes": wall or [], "wall_conj": "and"}
119
+ count_sql, count_params, count_limits = sheet.build_aggregate_query(
120
+ spec, binding(key, "count"), columns, **args)
121
+ sum_sql, sum_params, sum_limits = sheet.build_aggregate_query(
122
+ spec, binding(key), columns, **args)
123
+ ids_sql, ids_params, ids_limits = sheet.build_ids_query(spec, columns, **args)
124
+ result = (cur.execute(count_sql, count_params).fetchone()[0],
125
+ cur.execute(sum_sql, sum_params).fetchone()[0],
126
+ sorted(row[0] for row in cur.execute(ids_sql, ids_params).fetchall()))
127
+ check("H7537 compiler leaves specs, columns and filters unchanged " + key,
128
+ before == (spec, columns, nodes, wall))
129
+ check("H7537 aggregate and drill limits agree " + key,
130
+ count_limits == sum_limits == ids_limits)
131
+ return result, count_limits
132
+
133
+ for key in registry:
134
+ result, limits = compiled(key)
135
+ check("H7537 original two-row report returns count 1, sum 10 and id 1 " + key,
136
+ result == (1, 10, [1]) and limits == notice)
137
+ # The independent Odoo validator supplies a source expression without a caller column map.
138
+ spec = rot._source_for(cur, key)
139
+ read = odoo_default_scope.apply(key, {"filters": []}, {
140
+ "spec": spec, "where": spec.get("where"), "limits": []})
141
+ source = spec.get("from_sql") or spec["table"]
142
+ value = spec["cols"][binding(key)["aggregate"]["field"]][0]
143
+ check("H7537 validator default works without a permission column map " + key,
144
+ cur.execute(f"SELECT COUNT(*), SUM({value}) FROM {source} WHERE {read['where']}")
145
+ .fetchone() == (1, 10) and read["limits"] == notice)
146
+
147
+ insert([(3, "RI-3", "draft", 7, "Royal customer", 1000, "Royal", 6),
148
+ (4, "RI-4", "sale", None, "No partner", 2000, "Royal", 6),
149
+ (5, "FF-5", "done", 8, "Fisch customer", 300, "Fisch", 5),
150
+ (6, "RI-6", "done", 7, "Royal customer", 40, "Royal", 6),
151
+ (7, "RI-7", "cancel", 7, "Royal customer", 4000, "Royal", 6)])
152
+ cases = [
153
+ ("default", [], "and", (3, 350, [1, 5, 6]), notice),
154
+ ("explicit wholesale", [included], "and", (3, 350, [1, 5, 6]), []),
155
+ ("explicit excluded", [excluded], "and", (1, 90, [2]), []),
156
+ ("both channels", [included, excluded], "or", (4, 440, [1, 2, 5, 6]), []),
157
+ ("disjoint channels", [included, excluded], "and", (0, 0, []), []),
158
+ ("unrelated OR", [{"colId": "order_no", "op": "eq", "value": "RI-1"},
159
+ {"colId": "order_no", "op": "eq", "value": "RI-2"}],
160
+ "or", (1, 10, [1]), notice),
161
+ ("nested excluded", [{"conj": "and", "children": [
162
+ {"conj": "or", "children": [excluded]}]}], "and", (1, 90, [2]), []),
163
+ ]
164
+ # Order prefixes identify the fixture's two BUs. The second leaf is an independent row wall.
165
+ bu_wall = [{"colId": "order_no", "op": "contains", "value": "RI-"}]
166
+ row_wall = [{"colId": "order_no", "op": "neq", "value": "RI-6"}]
167
+ for key in registry:
168
+ for label, nodes, conj, expected, expected_limits in cases:
169
+ result, limits = compiled(key, nodes, conj)
170
+ check("H7537 " + label + " population " + key,
171
+ result == expected and limits == expected_limits)
172
+ raw = binding(key, nodes=nodes, conj=conj)
173
+ visible_reader = session("user")
174
+ answer = rs._resolve_binding(visible_reader, raw)
175
+ count = rs._resolve_binding(visible_reader, binding(key, "count", nodes, conj))
176
+ drilled = drill(visible_reader, raw)
177
+ data = rot.odoo_table_aggregates(key, body={
178
+ "filters": nodes, "filterConj": conj,
179
+ "aggs": [{"colId": raw["aggregate"]["field"], "agg": "sum"}]}, session=visible_reader)
180
+ check("H7537 Data, Sheet and drill share fixture totals " + label + " " + key,
181
+ (count["value"], answer["value"], sorted(drilled["record_ids"])) == expected
182
+ and drilled["count"] == data["total_rows"] == expected[0]
183
+ and data["aggregates"][raw["aggregate"]["field"]]
184
+ == (expected[1] if expected[0] else None)
185
+ and answer["limits"] == count["limits"] == drilled["limits"]
186
+ == data["limits"] == expected_limits)
187
+
188
+ hidden_result, hidden_limits = compiled(key, hidden={"wholesale_scope"})
189
+ check("H7537 hidden scope uses the resolved source expression " + key,
190
+ hidden_result == (3, 350, [1, 5, 6]) and hidden_limits == notice)
191
+ hidden_explicit, hidden_explicit_limits = compiled(
192
+ key, [excluded], hidden={"wholesale_scope"})
193
+ check("H7537 an uncompiled scope leaf cannot suppress the compiler default " + key,
194
+ hidden_explicit == (3, 350, [1, 5, 6])
195
+ and [item["effect"] for item in hidden_explicit_limits]
196
+ == ["filter_ignored", "scope_default"])
197
+ hidden_reader = session("user", hidden=["wholesale_scope"])
198
+ check("H7537 real field permissions hide scope " + key,
199
+ "wholesale_scope" in perm_scope.hidden_keys(
200
+ hidden_reader.user, key, fields[key], st=fake))
201
+ hidden_answer = rs._resolve_binding(hidden_reader, binding(key))
202
+ hidden_count = rs._resolve_binding(hidden_reader, binding(key, "count"))
203
+ hidden_drill = drill(hidden_reader, binding(key))
204
+ hidden_data = rot.odoo_table_aggregates(key, body={"aggs": [
205
+ {"colId": binding(key)["aggregate"]["field"], "agg": "sum"}]}, session=hidden_reader)
206
+ check("H7537 hidden scope stays absent from drill fields and rows " + key,
207
+ hidden_answer["value"] == 350 and hidden_answer["limits"] == notice
208
+ and hidden_count["value"] == hidden_drill["count"] == hidden_data["total_rows"] == 3
209
+ and sorted(hidden_drill["record_ids"]) == [1, 5, 6]
210
+ and hidden_data["aggregates"][binding(key)["aggregate"]["field"]] == 350
211
+ and hidden_count["limits"] == hidden_drill["limits"] == hidden_data["limits"] == notice
212
+ and "wholesale_scope" not in {f["key"] for f in hidden_drill["page"]["fields"]}
213
+ and all("wholesale_scope" not in row for row in hidden_drill["page"]["rows"]))
214
+
215
+ hidden_cases = [
216
+ ("isEmpty", [excluded], "and", (3, 350)),
217
+ ("eq", [included], "and", (3, 350)),
218
+ ("channel OR", [included, excluded], "or", (3, 350)),
219
+ ("nested", [{"conj": "and", "children": [
220
+ {"conj": "or", "children": [excluded]}]}], "and", (3, 350)),
221
+ ("mixed OR", [excluded, {"colId": "order_no", "op": "eq", "value": "RI-1"}],
222
+ "or", (1, 10)),
223
+ ("excluded row OR", [excluded, {"colId": "order_no", "op": "eq", "value": "RI-2"}],
224
+ "or", (0, None)),
225
+ ]
226
+ for label, nodes, conj, expected in hidden_cases:
227
+ raw = binding(key, nodes=nodes, conj=conj)
228
+ hidden_refused(hidden_reader, raw, label + " " + key)
229
+ check("H7537 hidden draft filter refuses drill " + label + " " + key,
230
+ refusal(lambda: drill(hidden_reader, binding(key),
231
+ json.dumps({"nodes": nodes, "conj": conj}))) == (403, "field_unreadable"))
232
+ data = rot.odoo_table_aggregates(key, body={
233
+ "filters": nodes, "filterConj": conj,
234
+ "aggs": [{"colId": raw["aggregate"]["field"], "agg": "sum"}]}, session=hidden_reader)
235
+ check("H7537 Data ignores hidden predicates but keeps wholesale default " + label + " " + key,
236
+ (data["total_rows"], data["aggregates"][raw["aggregate"]["field"]]) == expected
237
+ and [item["effect"] for item in data["limits"]] == ["filter_ignored", "scope_default"])
238
+
239
+ other_hidden_reader = session("user", hidden=["order_no"])
240
+ hidden_refused(other_hidden_reader,
241
+ binding(key, nodes=[excluded, row_wall[0]], conj="or"),
242
+ "visible channel with another hidden field " + key)
243
+ visible_channel = binding(key, nodes=[excluded])
244
+ answer = rs._resolve_binding(other_hidden_reader, visible_channel)
245
+ drilled = drill(other_hidden_reader, visible_channel)
246
+ data = rot.odoo_table_aggregates(key, body={"filters": [excluded, row_wall[0]],
247
+ "filterConj": "or", "aggs": [
248
+ {"colId": visible_channel["aggregate"]["field"], "agg": "sum"}]},
249
+ session=other_hidden_reader)
250
+ check("H7537 hiding another field preserves visible channel selection " + key,
251
+ answer["value"] == 90 and drilled["count"] == data["total_rows"] == 1
252
+ and drilled["record_ids"] == [2] and answer["limits"] == drilled["limits"] == []
253
+ and data["aggregates"][visible_channel["aggregate"]["field"]] == 90
254
+ and [item["effect"] for item in data["limits"]] == ["filter_ignored"])
255
+
256
+ key_bu_wall = ([{"colId": "team", "op": "eq", "value": "Royal"}]
257
+ if rel.base_table_key(key) == rel.ORDERS_KEY else bu_wall)
258
+ for label, walls, expected in (
259
+ ("BU", key_bu_wall, (2, 50, [1, 6])),
260
+ ("row", row_wall, (2, 310, [1, 5])),
261
+ ("BU and row", key_bu_wall + row_wall, (1, 10, [1]))):
262
+ reader = session("user", wall={"conj": "and", "nodes": walls})
263
+ result, limits = compiled(key, wall=walls)
264
+ answer = rs._resolve_binding(reader, binding(key))
265
+ drilled = drill(reader, binding(key))
266
+ data = rot.odoo_table_aggregates(key, body={"aggs": [
267
+ {"colId": binding(key)["aggregate"]["field"], "agg": "sum"}]}, session=reader)
268
+ check("H7537 " + label + " walls constrain all three doors " + key,
269
+ result == expected and answer["value"] == expected[1]
270
+ and (drilled["count"], sorted(drilled["record_ids"])) == (expected[0], expected[2])
271
+ and data["total_rows"] == expected[0]
272
+ and data["aggregates"][binding(key)["aggregate"]["field"]] == expected[1]
273
+ and limits == answer["limits"] == drilled["limits"] == data["limits"] == notice)
274
+ explicit = drill(reader, binding(key, nodes=[included, excluded], conj="or"))
275
+ check("H7537 explicit channels cannot bypass " + label + " walls " + key,
276
+ sorted(explicit["record_ids"]) == sorted(expected[2] + [2])
277
+ and explicit["count"] == expected[0] + 1 and explicit["limits"] == [])
278
+
279
+ hidden_wall_reader = session("user", wall={"conj": "and", "nodes": walls},
280
+ hidden=["wholesale_scope", *sheet.filter_columns_named(walls)])
281
+ answer = rs._resolve_binding(hidden_wall_reader, binding(key))
282
+ drilled = drill(hidden_wall_reader, binding(key))
283
+ data = rot.odoo_table_aggregates(key, body={"filters": [excluded], "aggs": [
284
+ {"colId": binding(key)["aggregate"]["field"], "agg": "sum"}]}, session=hidden_wall_reader)
285
+ check("H7537 hidden fields preserve trusted " + label + " walls and default " + key,
286
+ answer["value"] == expected[1]
287
+ and (drilled["count"], sorted(drilled["record_ids"])) == (expected[0], expected[2])
288
+ and answer["limits"] == drilled["limits"] == notice
289
+ and data["total_rows"] == expected[0]
290
+ and data["aggregates"][binding(key)["aggregate"]["field"]] == expected[1]
291
+ and [item["effect"] for item in data["limits"]] == ["filter_ignored", "scope_default"])
292
+ hidden_refused(hidden_wall_reader, binding(key, nodes=[included, excluded], conj="or"),
293
+ label + " wall " + key)
294
+
295
+ wall_only, wall_only_limits = compiled(key, wall=[excluded])
296
+ check("H7537 a permanent scope wall cannot suppress the summary default " + key,
297
+ wall_only == (0, 0, []) and wall_only_limits == notice)
298
+ scope_wall_reader = session("user", wall={"nodes": [excluded], "conj": "and"},
299
+ hidden=["wholesale_scope"])
300
+ answer = rs._resolve_binding(scope_wall_reader, binding(key))
301
+ drilled = drill(scope_wall_reader, binding(key))
302
+ data = rot.odoo_table_aggregates(key, body={"filters": [excluded]}, session=scope_wall_reader)
303
+ check("H7537 hidden permanent channel wall intersects the trusted default " + key,
304
+ answer["value"] == drilled["count"] == data["total_rows"] == 0
305
+ and drilled["record_ids"] == [] and answer["limits"] == drilled["limits"] == notice
306
+ and [item["effect"] for item in data["limits"]] == ["filter_ignored", "scope_default"])
307
+
308
+ saved_nodes = [{"conj": "or", "children": [included, excluded]}]
309
+ fake.put(view_templates.workspace_key(key), {"owner": {"views": {
310
+ "all_channels": {"id": "all_channels", "name": "Both channels", "type": "grid",
311
+ "config": {"filters": saved_nodes, "filterConj": "and"}},
312
+ "default_channel": {"id": "default_channel", "name": "Default channel", "type": "grid",
313
+ "config": {"filters": row_wall, "filterConj": "and"}}}}})
314
+ saved = binding(key, scope={"view_id": "all_channels", "view_name": "Both channels",
315
+ "filter": {"nodes": row_wall, "conj": "and"}})
316
+ answer, drilled = rs._resolve_binding(admin, saved), drill(admin, saved)
317
+ check("H7537 saved nested channel choice intersects the extra filter " + key,
318
+ answer["value"] == 400 and sorted(drilled["record_ids"]) == [1, 2, 5]
319
+ and drilled["count"] == 3 and answer["limits"] == drilled["limits"] == [])
320
+ saved_default = binding(key, scope={"view_id": "default_channel"})
321
+ answer = rs._resolve_binding(admin, saved_default)
322
+ drilled = drill(admin, saved_default)
323
+ check("H7537 saved views without a channel filter retain the default " + key,
324
+ answer["value"] == 310 and sorted(drilled["record_ids"]) == [1, 5]
325
+ and drilled["count"] == 2 and answer["limits"] == drilled["limits"] == notice)
326
+ workspace = fake.get(view_templates.workspace_key(key))
327
+ workspace["reader"] = copy.deepcopy(workspace["owner"])
328
+ fake.put(view_templates.workspace_key(key), workspace)
329
+ hidden_refused(hidden_reader, saved, "saved nested channel filter " + key)
330
+ answer, drilled = rs._resolve_binding(hidden_reader, saved_default), drill(hidden_reader, saved_default)
331
+ check("H7537 hidden default applies through a saved visible filter " + key,
332
+ answer["value"] == 310 and sorted(drilled["record_ids"]) == [1, 5]
333
+ and drilled["count"] == 2 and answer["limits"] == drilled["limits"] == notice)
334
+
335
+ denied = session("user")
336
+ denied.user["perms"][key]["access"] = False
337
+ with patch.object(rot, "_mirror_cur", side_effect=AssertionError("unreadable mirror read")):
338
+ check("H7537 unreadable aggregate refuses before mirror access " + key,
339
+ refusal(lambda: rs._resolve_binding(denied, binding(key)))
340
+ == (403, "database_unreadable"))
341
+ check("H7537 unreadable drill refuses before mirror access " + key,
342
+ refusal(lambda: drill(denied, binding(key))) == (403, "database_unreadable"))
343
+
344
+ # The generic Data row door retains both channels, including the excluded partner.
345
+ rows = rot.odoo_table_rows(key, session=admin, offset=0, limit=100, filters=None,
346
+ filterConj="and", sorts=None, search=None, related=None, groupBy=None, groupKey=None)
347
+ check("H7537 generic Data rows stay all-channel " + key,
348
+ sorted(row["pid"] for row in rows["rows"]) == [1, 2, 5, 6]
349
+ and rows["total"] == 4
350
+ and not any(limit["effect"] == "scope_default" for limit in rows.get("limits", [])))
351
+ choices = rs.distinct_values(key, "customer", offset=0, limit=100, search="", session=admin)
352
+ check("H7537 distinct choices retain the excluded channel " + key,
353
+ set(choices["values"]) == {"Royal customer", "Fisch customer", "GIFTWARE DEALS"})
354
+ placed = {"database": key, "mode": "view", "viewType": "grid",
355
+ "fields": ["order_no", binding(key)["aggregate"]["field"]],
356
+ "filter": {"nodes": [], "conj": "and"}, "sort": [],
357
+ "block": {"cols": 2, "rows": 100}, "limit": 100}
358
+ with patch.object(rs, "_registry_assembly", return_value={"fields": fields[key]}):
359
+ placed_rows = rs.view_rows(book["id"], body={"binding": placed}, session=admin)
360
+ check("H7537 placed rows retain both channels without a summary refusal " + key,
361
+ isinstance(placed_rows, dict) and placed_rows["total"] == 4
362
+ and sorted(row["pid"] for row in placed_rows["rows"]) == [1, 2, 5, 6])
363
+
364
+ # A wholesale column alone must not opt another summary into the sales policy.
365
+ for key in ("inventory_data", "product_data", "dead_stock_data", "ut_amzn_orders"):
366
+ spec = {"table": "sale_order", "id": "id", "where": "", "needs_excluded": True,
367
+ "cols": {"wholesale_scope": ("CASE WHEN partner_id = 99 THEN '' ELSE '1' END", str)}}
368
+ cols = {"amount": {"sql": "amount_untaxed", "type": "number", "aggregate": False}}
369
+ sql, params, limits = sheet.build_aggregate_query(spec, {
370
+ "database": key, "aggregate": {"fn": "sum", "field": "amount"}}, cols,
371
+ scope_nodes=[], scope_conj="and")
372
+ check("H7537 other summaries stay all-channel " + key,
373
+ cur.execute(sql, params).fetchone()[0] == 7440 and limits == [])
platform/modules/sheet.py CHANGED
@@ -1229,29 +1229,8 @@ def _related_predicate(related, columns, *, today=None, cohort_sets=None):
1229
 
1230
  def build_aggregate_query(spec, binding, columns, *, scope_nodes, scope_conj,
1231
  wall_nodes=None, wall_conj="and", today=None, cohort_sets=None,
1232
- related=None):
1233
- """`(sql, params, limits)` for one bound cell.
1234
-
1235
- ⛔⛔ **THE SPEC'S OWN `where` IS NOT OPTIONAL AND IT IS NOT AN OPTIMISATION.** It carries the
1236
- FIXED wholesale scope (`state IN ('sale','done') AND partner_id IS NOT NULL` for orders), which
1237
- is the project's standing "scope is fixed" convention expressed as SQL. A query built from
1238
- `cols` alone counts DRAFT and CANCELLED rows, so it returns a bigger number that looks entirely
1239
- reasonable and disagrees with every grid and module reading the same data. That is the worst
1240
- class of defect this ticket can ship, because nothing goes red.
1241
-
1242
- ⭐⭐ **THE EXCLUDED PARTNER SET IS ALREADY BAKED IN AND MUST NOT BE APPLIED AGAIN HERE.**
1243
- `routes_odoo_tables._source_for` resolves `needs_excluded` BEFORE this function ever sees the
1244
- spec: it reads `excluded_ids(cur)` and substitutes them into any column expression carrying
1245
- `{excluded}`, then returns `{**spec, "cols": <resolved>}`. Adding a second `partner_id NOT IN
1246
- (...)` clause here would double-apply it, and worse, it would be WRONG on every joined spec:
1247
- order lines address the column as `sol.order_partner_id` and GL lines as `aml.partner_id`, so a
1248
- bare `partner_id` would either match nothing or fail to compile. The spec arrives resolved; use
1249
- it as given.
1250
-
1251
- ⚠ `from_sql` beats `table` where a spec declares it. The joined specs (order lines, GL lines)
1252
- carry `from_sql` plus a `tables` alias map, and reading `table` alone would query one side of
1253
- the join with column expressions written for both.
1254
- """
1255
  select, _read = aggregate_sql(spec, binding["aggregate"], columns)
1256
  where = []
1257
  params = []
@@ -1260,6 +1239,33 @@ def build_aggregate_query(spec, binding, columns, *, scope_nodes, scope_conj,
1260
  if base:
1261
  where.append(f"({base})")
1262
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1263
  scope_compiled = None
1264
  related_limits = []
1265
  for nodes, conj, is_scope in ((wall_nodes, wall_conj, False),
@@ -1282,7 +1288,7 @@ def build_aggregate_query(spec, binding, columns, *, scope_nodes, scope_conj,
1282
  # ⭐ The limits are measured against what the compiler REALLY used, so a leaf that fell out for
1283
  # any reason is reported. See `widen_limits` for the measurement that made this necessary.
1284
  return sql, params, (widen_limits(scope_nodes, columns, scope_compiled)
1285
- + related_limits)
1286
 
1287
 
1288
  def build_ids_query(spec, columns, *, scope_nodes, scope_conj, wall_nodes=None, wall_conj="and",
 
1229
 
1230
  def build_aggregate_query(spec, binding, columns, *, scope_nodes, scope_conj,
1231
  wall_nodes=None, wall_conj="and", today=None, cohort_sets=None,
1232
+ related=None, summary_default=True):
1233
+ """Compile a bound cell with the source predicate, summary default, walls, and authored scope."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1234
  select, _read = aggregate_sql(spec, binding["aggregate"], columns)
1235
  where = []
1236
  params = []
 
1239
  if base:
1240
  where.append(f"({base})")
1241
 
1242
+ def scope_leaf_present(tree):
1243
+ if isinstance(tree, list):
1244
+ return any(scope_leaf_present(node) for node in tree)
1245
+ if not isinstance(tree, dict):
1246
+ return False
1247
+ return ((tree.get("colId") == "wholesale_scope" and "wholesale_scope" in columns)
1248
+ or any(scope_leaf_present(tree.get(key)) for key in ("nodes", "children")))
1249
+
1250
+ default_limits = []
1251
+ if summary_default and spec.get("default_wholesale_scope") and not scope_leaf_present(scope_nodes):
1252
+ # The resolved source expression remains usable when the scope field is hidden.
1253
+ expression = (spec.get("cols") or {}).get("wholesale_scope")
1254
+ if expression is None:
1255
+ raise SheetRefusal("binding_invalid")
1256
+ default = compile_scope(
1257
+ [{"colId": "wholesale_scope", "op": "eq", "value": "1"}], "and",
1258
+ {"wholesale_scope": {"sql": expression[0], "type": "text", "aggregate": False}})
1259
+ if default is None:
1260
+ raise SheetRefusal("binding_invalid")
1261
+ where.append(f"({default.sql})")
1262
+ params.extend(default.params)
1263
+ default_limits.append({
1264
+ "subject": "Totals", "effect": "scope_default",
1265
+ "cause": "Totals leave out the Amazon channel (GIFTWARE DEALS).",
1266
+ "recommendation": "Show the Amazon channel by ticking Not in wholesale scope in the filter.",
1267
+ })
1268
+
1269
  scope_compiled = None
1270
  related_limits = []
1271
  for nodes, conj, is_scope in ((wall_nodes, wall_conj, False),
 
1288
  # ⭐ The limits are measured against what the compiler REALLY used, so a leaf that fell out for
1289
  # any reason is reported. See `widen_limits` for the measurement that made this necessary.
1290
  return sql, params, (widen_limits(scope_nodes, columns, scope_compiled)
1291
+ + related_limits + default_limits)
1292
 
1293
 
1294
  def build_ids_query(spec, columns, *, scope_nodes, scope_conj, wall_nodes=None, wall_conj="and",
web/public/AIOS_DEPLOY_RECEIPT CHANGED
@@ -1 +1 @@
1
- ab7a9ffd4eb3c70ce981245bb69d3f3723cb485df317cd19b7bbeec355c9bac1
 
1
+ 54708218fccdd9e6b7c534602bafcf749a8a6fbf40ee3f7b23fbef31982757de