lambda-erp 1.4.19__tar.gz → 1.4.21__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (138) hide show
  1. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/PKG-INFO +1 -1
  2. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/chat.py +18 -2
  3. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/record_attachments.py +85 -12
  4. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/record_notes.py +9 -0
  5. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/services.py +15 -35
  6. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/frontend/src/api/client.ts +10 -1
  7. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/accounting/revaluation.py +6 -0
  8. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/accounting/subscription.py +3 -0
  9. lambda_erp-1.4.21/lambda_erp/controllers/tax_defaults.py +67 -0
  10. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/controllers/taxes_and_totals.py +138 -141
  11. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/database.py +9 -0
  12. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/model.py +2 -0
  13. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/permissions.py +38 -0
  14. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/stock/consignment.py +3 -0
  15. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/utils.py +40 -2
  16. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/validation.py +28 -0
  17. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/pyproject.toml +1 -1
  18. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/.gitignore +0 -0
  19. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/LICENSE +0 -0
  20. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/README.md +0 -0
  21. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/__init__.py +0 -0
  22. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/attachments.py +0 -0
  23. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/auth.py +0 -0
  24. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/bootstrap.py +0 -0
  25. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/chat_links.py +0 -0
  26. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/demo_limits.py +0 -0
  27. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/deps.py +0 -0
  28. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/erp_context.py +0 -0
  29. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/errors.py +0 -0
  30. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/list_values.py +0 -0
  31. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/llm_provider.py +0 -0
  32. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/main.py +0 -0
  33. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/mcp_oauth.py +0 -0
  34. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/mentions.py +0 -0
  35. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/menu_layout.py +0 -0
  36. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/oauth.py +0 -0
  37. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/pdf.py +0 -0
  38. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/pdf_contract.py +0 -0
  39. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/pdf_exports.py +0 -0
  40. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/pdf_fonts.py +0 -0
  41. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/pdf_pages.py +0 -0
  42. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/pdf_profiles.py +0 -0
  43. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/pdf_templates.py +0 -0
  44. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/pricing_actions.py +0 -0
  45. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/providers.py +0 -0
  46. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/remarks_md.py +0 -0
  47. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/routers/__init__.py +0 -0
  48. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/routers/accounting.py +0 -0
  49. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/routers/actions.py +0 -0
  50. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/routers/admin.py +0 -0
  51. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/routers/analytics.py +0 -0
  52. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/routers/availability.py +0 -0
  53. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/routers/bank_reconciliation.py +0 -0
  54. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/routers/bank_statements.py +0 -0
  55. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/routers/chat_api.py +0 -0
  56. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/routers/documents.py +0 -0
  57. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/routers/masters.py +0 -0
  58. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/routers/mcp.py +0 -0
  59. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/routers/pdf_templates.py +0 -0
  60. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/routers/proposals.py +0 -0
  61. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/routers/reports.py +0 -0
  62. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/routers/setup.py +0 -0
  63. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/stock_actions.py +0 -0
  64. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/templates/_proposal_totals.html +0 -0
  65. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/templates/_record_base.html +0 -0
  66. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/templates/delivery_note.html +0 -0
  67. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/templates/document.html +0 -0
  68. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/templates/proposal.html +0 -0
  69. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/templates/record.html +0 -0
  70. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/templates/reservation.html +0 -0
  71. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/time_filters.py +0 -0
  72. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/tool_permissions.py +0 -0
  73. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/api/write_context.py +0 -0
  74. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/docs/agents/README.md +0 -0
  75. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/frontend/LICENSE +0 -0
  76. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/frontend/README.md +0 -0
  77. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/__init__.py +0 -0
  78. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/accounting/__init__.py +0 -0
  79. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/accounting/bank_account.py +0 -0
  80. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/accounting/bank_reconciliation.py +0 -0
  81. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/accounting/bank_statement_import.py +0 -0
  82. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/accounting/bank_transaction.py +0 -0
  83. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/accounting/budget.py +0 -0
  84. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/accounting/camt.py +0 -0
  85. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/accounting/chart_of_accounts.py +0 -0
  86. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/accounting/general_ledger.py +0 -0
  87. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/accounting/journal_entry.py +0 -0
  88. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/accounting/payment_entry.py +0 -0
  89. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/accounting/pos_invoice.py +0 -0
  90. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/accounting/purchase_invoice.py +0 -0
  91. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/accounting/sales_invoice.py +0 -0
  92. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/accounting/settlement.py +0 -0
  93. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/accounting/setup/__init__.py +0 -0
  94. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/accounting/setup/engine.py +0 -0
  95. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/accounting/setup/pack.py +0 -0
  96. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/accounting/setup/packs/__init__.py +0 -0
  97. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/accounting/setup/packs/ch.py +0 -0
  98. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/accounting/setup/packs/de_common.py +0 -0
  99. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/accounting/setup/packs/de_skr03.py +0 -0
  100. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/accounting/setup/packs/de_skr04.py +0 -0
  101. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/accounting/setup/packs/generic.py +0 -0
  102. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/accounting/setup/profiles.py +0 -0
  103. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/accounting/setup/spine.py +0 -0
  104. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/addresses.py +0 -0
  105. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/assets/__init__.py +0 -0
  106. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/assets/asset.py +0 -0
  107. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/assets/lifecycle.py +0 -0
  108. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/assets/reservation.py +0 -0
  109. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/backlog.py +0 -0
  110. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/bundles.py +0 -0
  111. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/buying/__init__.py +0 -0
  112. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/buying/purchase_order.py +0 -0
  113. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/controllers/__init__.py +0 -0
  114. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/controllers/currency.py +0 -0
  115. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/controllers/defaults.py +0 -0
  116. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/controllers/item_price.py +0 -0
  117. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/controllers/item_prices.py +0 -0
  118. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/controllers/price_resolution.py +0 -0
  119. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/controllers/pricing_rule.py +0 -0
  120. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/exceptions.py +0 -0
  121. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/hooks.py +0 -0
  122. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/master_integrity.py +0 -0
  123. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/selling/__init__.py +0 -0
  124. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/selling/proposal.py +0 -0
  125. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/selling/quotation.py +0 -0
  126. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/selling/sales_order.py +0 -0
  127. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/simulation.py +0 -0
  128. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/stock/__init__.py +0 -0
  129. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/stock/delivery_note.py +0 -0
  130. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/stock/purchase_receipt.py +0 -0
  131. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/stock/reservation.py +0 -0
  132. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/stock/serial.py +0 -0
  133. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/stock/stock_count.py +0 -0
  134. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/stock/stock_entry.py +0 -0
  135. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/stock/stock_ledger.py +0 -0
  136. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/timestamps.py +0 -0
  137. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/lambda_erp/workflow.py +0 -0
  138. {lambda_erp-1.4.19 → lambda_erp-1.4.21}/terraform/README.md +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: lambda-erp
3
- Version: 1.4.19
3
+ Version: 1.4.21
4
4
  Summary: Core ERP logic - accounting, sales, purchasing, inventory
5
5
  Author: TORUS INVESTMENTS AG
6
6
  License-Expression: Apache-2.0
@@ -1960,6 +1960,10 @@ def _handle_get_current_time(args):
1960
1960
 
1961
1961
 
1962
1962
  def _handle_retrieve_chat_history(args, session_id=None):
1963
+ # Only ever the conversation the caller is in; without one there is
1964
+ # nothing to retrieve (and never other people's conversations).
1965
+ if not session_id:
1966
+ return {"error": "retrieve_chat_history is only available inside a chat conversation."}
1963
1967
  db = get_db()
1964
1968
  date_from = args.get("date_from")
1965
1969
  date_to = args.get("date_to")
@@ -2582,13 +2586,25 @@ def _prompt_company_context() -> str:
2582
2586
  )
2583
2587
 
2584
2588
 
2585
- def _prompt_analytics_context() -> str:
2589
+ def _prompt_analytics_context(user_info: dict | None = None) -> str:
2586
2590
  db = get_db()
2587
2591
  datasets = [
2588
2592
  ("sales invoices", "Sales Invoice", "posting_date", "docstatus = 1"),
2589
2593
  ("purchase invoices", "Purchase Invoice", "posting_date", "docstatus = 1"),
2590
2594
  ("payments", "Payment Entry", "posting_date", "docstatus = 1"),
2591
2595
  ]
2596
+ # The prompt is data that reaches the model: a restricted user gets no
2597
+ # counts or date ranges of what their access profile or record scope hides.
2598
+ from lambda_erp import permissions
2599
+ restrictions = (user_info or {}).get("restrictions")
2600
+ if restrictions:
2601
+ if restrictions.get("scope"):
2602
+ datasets = []
2603
+ else:
2604
+ names = {"Sales Invoice": "sales_invoices", "Purchase Invoice": "purchase_invoices", "Payment Entry": "payments"}
2605
+ with permissions.acting_as(user_info):
2606
+ datasets = [entry for entry in datasets
2607
+ if permissions.can(entry[1], "read") and not permissions.artifact_denied("dataset", names[entry[1]])]
2592
2608
  lines: list[str] = []
2593
2609
  for label, table, date_field, where in datasets:
2594
2610
  try:
@@ -2667,7 +2683,7 @@ def build_system_prompt(user_info: dict | None = None, channel: str = "web"):
2667
2683
  user_name = user_info.get("full_name", "User") if user_info else "User"
2668
2684
  user_role = user_info.get("role", "viewer") if user_info else "viewer"
2669
2685
  company_context = _prompt_company_context()
2670
- analytics_context = _prompt_analytics_context()
2686
+ analytics_context = _prompt_analytics_context(user_info)
2671
2687
  uom_context = _prompt_uom_context()
2672
2688
  validation_context = _prompt_validation_context()
2673
2689
 
@@ -8,6 +8,12 @@ too) and is never printed with it.
8
8
  Whoever may read the record may list and download its files; adding and
9
9
  removing them needs the manager role. The demo user gets neither.
10
10
 
11
+ A file's content cannot be redacted like a field, so each file carries a
12
+ sensitivity class as a whole (lambda_erp.permissions, free content): its own
13
+ when one was set, else the class of the record type it is kept with. A file
14
+ on a purchase order or a supplier counts as purchase prices and does not
15
+ exist for a user who does not see those.
16
+
11
17
  REST:
12
18
  GET /api/record-attachments?record_type=sales-order&record_id=SO-0001
13
19
  POST /api/record-attachments (multipart: record_type, record_id, file, description)
@@ -87,25 +93,68 @@ def safe_filename(filename):
87
93
  return name or "attachment"
88
94
 
89
95
 
96
+ def effective_class(row):
97
+ """The sensitivity class of a stored file: its own, else its record type's."""
98
+ from lambda_erp import permissions
99
+ own = row.get("sensitivity")
100
+ if own:
101
+ return None if own == permissions.HARMLESS else own
102
+ return permissions.content_class(_target(row["record_type"]))
103
+
104
+
105
+ def _hidden_from(row, user):
106
+ from lambda_erp import permissions
107
+ with permissions.acting_as(user):
108
+ return permissions.content_hidden(effective_class(row))
109
+
110
+
111
+ def _check_sensitivity(value, record_type, user, current=None):
112
+ """A sensitivity setting a caller may give: empty (the record type's
113
+ class), 'none' (nothing sensitive) or a class. Only someone who sees the
114
+ classes involved can set or lift one."""
115
+ from lambda_erp import permissions
116
+ value = (str(value).strip() or None) if value is not None else None
117
+ if value not in (None, permissions.HARMLESS) and value not in permissions.SENSITIVITY_CLASSES:
118
+ raise HTTPException(422, f"sensitivity must be empty, '{permissions.HARMLESS}' or one of "
119
+ f"{', '.join(sorted(permissions.SENSITIVITY_CLASSES))}")
120
+ default = permissions.content_class(_target(record_type))
121
+ with permissions.acting_as(user):
122
+ involved = {default, current, value} - {None, permissions.HARMLESS}
123
+ blind = sorted(cls for cls in involved if permissions.content_hidden(cls))
124
+ if blind and (value is not None or current is not None):
125
+ raise HTTPException(403, "Your access profile hides this kind of content, so you cannot classify files "
126
+ "kept with this record")
127
+ return value
128
+
129
+
90
130
  def _public(row):
91
131
  return {"id": row["name"], "record_type": row["record_type"], "record_id": row["record_id"],
92
132
  "filename": row["filename"], "content_type": row["content_type"], "size": row["size"],
133
+ "sensitivity": row.get("sensitivity") or None, "sensitivity_class": effective_class(row),
93
134
  "description": row.get("description"), "uploaded_by": row.get("uploaded_by_name") or row.get("uploaded_by"),
94
135
  "created_at": row.get("creation"), "url": f"/api/record-attachments/{row['name']}"}
95
136
 
96
137
 
97
138
  def list_attachments(record_type, record_id, user):
98
139
  _require_record(record_type, record_id, user)
140
+ from lambda_erp import permissions
99
141
  rows = get_db().sql(
100
142
  f'SELECT name, record_type, record_id, filename, content_type, size, description, uploaded_by, '
101
- f'uploaded_by_name, creation FROM "{TABLE}" WHERE record_type = ? AND record_id = ? ORDER BY creation, name',
102
- [record_type, record_id])
103
- return {"attachments": [_public(row) for row in rows]}
143
+ f'uploaded_by_name, creation, sensitivity FROM "{TABLE}" WHERE record_type = ? AND record_id = ? '
144
+ f'ORDER BY creation, name', [record_type, record_id])
145
+ # Files of a class hidden from the caller are not there for them, name included.
146
+ return {"attachments": [_public(row) for row in rows if not _hidden_from(row, user)],
147
+ "default_class": permissions.content_class(_target(record_type)),
148
+ "sensitivity_classes": dict(permissions.SENSITIVITY_CLASSES)}
149
+
104
150
 
151
+ def add_attachment(record_type, record_id, filename, data, user, description=None, sensitivity=None):
152
+ """Keep a file with a record. Returns its public description.
105
153
 
106
- def add_attachment(record_type, record_id, filename, data, user, description=None):
107
- """Keep a file with a record. Returns its public description."""
154
+ `sensitivity`: empty for the record type's class, 'none' for "nothing
155
+ sensitive in here", or a sensitivity class."""
108
156
  _require_record(record_type, record_id, user, write=True)
157
+ sensitivity = _check_sensitivity(sensitivity, record_type, user)
109
158
  filename = safe_filename(filename)
110
159
  extension = os.path.splitext(filename)[1].lower()
111
160
  if extension not in ALLOWED:
@@ -123,13 +172,13 @@ def add_attachment(record_type, record_id, filename, data, user, description=Non
123
172
  name = uuid.uuid4().hex
124
173
  db.sql(
125
174
  f'INSERT INTO "{TABLE}" (name, record_type, record_id, filename, content_type, size, sha256, data, '
126
- f'description, uploaded_by, uploaded_by_name, creation) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
175
+ f'description, uploaded_by, uploaded_by_name, creation, sensitivity) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
127
176
  [name, record_type, record_id, filename, ALLOWED[extension], len(data), hashlib.sha256(data).hexdigest(),
128
177
  data, (str(description).strip()[:500] or None) if description else None,
129
- user.get("user_id") or user.get("name"), user.get("full_name") or user.get("email"), now()])
178
+ user.get("user_id") or user.get("name"), user.get("full_name") or user.get("email"), now(), sensitivity])
130
179
  db.commit()
131
180
  row = db.sql(f'SELECT name, record_type, record_id, filename, content_type, size, description, uploaded_by, '
132
- f'uploaded_by_name, creation FROM "{TABLE}" WHERE name = ?', [name])[0]
181
+ f'uploaded_by_name, creation, sensitivity FROM "{TABLE}" WHERE name = ?', [name])[0]
133
182
  return _public(row)
134
183
 
135
184
 
@@ -138,6 +187,8 @@ def _load(attachment_id, user, write=False):
138
187
  if not rows:
139
188
  raise HTTPException(404, "Attachment not found")
140
189
  _require_record(rows[0]["record_type"], rows[0]["record_id"], user, write=write)
190
+ if _hidden_from(rows[0], user):
191
+ raise HTTPException(404, "Attachment not found")
141
192
  return rows[0]
142
193
 
143
194
 
@@ -155,11 +206,29 @@ def list_route(record_type: str, record_id: str, user: dict = Depends(get_curren
155
206
 
156
207
  @router.post("")
157
208
  async def upload_route(record_type: str = Form(...), record_id: str = Form(...), file: UploadFile = File(...),
158
- description: str | None = Form(None), user: dict = Depends(get_current_user)):
209
+ description: str | None = Form(None), sensitivity: str | None = Form(None),
210
+ user: dict = Depends(get_current_user)):
159
211
  # Check access before reading the body into memory.
160
212
  _require_record(record_type, record_id, user, write=True)
213
+ _check_sensitivity(sensitivity, record_type, user)
161
214
  data = await file.read(MAX_SIZE + 1)
162
- return add_attachment(record_type, record_id, file.filename, data, user, description)
215
+ return add_attachment(record_type, record_id, file.filename, data, user, description, sensitivity)
216
+
217
+
218
+ class Classify(mentions.Input):
219
+ sensitivity: str | None = Field(default=None, max_length=40)
220
+
221
+
222
+ @router.put("/{attachment_id}")
223
+ def classify_route(attachment_id: str, body: Classify, user: dict = Depends(get_current_user)):
224
+ """Change what a file counts as: empty (the record type's class), 'none'
225
+ or a sensitivity class."""
226
+ row = _load(attachment_id, user, write=True)
227
+ value = _check_sensitivity(body.sensitivity, row["record_type"], user, current=row.get("sensitivity") or "default")
228
+ db = get_db()
229
+ db.sql(f'UPDATE "{TABLE}" SET sensitivity = ? WHERE name = ?', [value, row["name"]])
230
+ db.commit()
231
+ return _public({**row, "sensitivity": value})
163
232
 
164
233
 
165
234
  @router.get("/{attachment_id}")
@@ -198,6 +267,7 @@ class AttachChatFile(mentions.Input):
198
267
  record_id: str = Field(min_length=1, max_length=200)
199
268
  attachment_id: str = Field(min_length=1, max_length=100)
200
269
  description: str | None = Field(default=None, max_length=500)
270
+ sensitivity: str | None = Field(default=None, max_length=40)
201
271
 
202
272
 
203
273
  def _list_action(args, user):
@@ -212,7 +282,7 @@ def _attach_chat_file(args, user):
212
282
  if not found:
213
283
  raise HTTPException(404, "That file was not uploaded to one of your conversations")
214
284
  return add_attachment(data.record_type, data.record_id, found[0]["filename"], found[0]["data"], user,
215
- data.description)
285
+ data.description, data.sensitivity)
216
286
 
217
287
 
218
288
  def initialize_record_attachments():
@@ -227,4 +297,7 @@ def initialize_record_attachments():
227
297
  parameters=AttachChatFile.model_json_schema(),
228
298
  description="Keep a file the user uploaded to this conversation with a document or master record, e.g. "
229
299
  "the customer's order e-mail or PDF with the sales order made from it. attachment_id is the "
230
- "id of the uploaded file. It does not change the record and is never printed.")
300
+ "id of the uploaded file. It does not change the record and is never printed. Leave sensitivity "
301
+ "empty: the file then counts as what its record type is about (a file on a purchase order or "
302
+ "supplier is hidden from users who do not see purchase prices). Set a sensitivity class only "
303
+ "when the user says what the file contains.")
@@ -82,6 +82,10 @@ def _can_access(record_type, record_id, user):
82
82
  try:
83
83
  with permissions.acting_as(user):
84
84
  permissions.require_readable(target[0], record_id)
85
+ # A note is free text: what colleagues write on a purchase order or
86
+ # a supplier counts as what those records are about.
87
+ if permissions.content_hidden(permissions.content_class(target[0])):
88
+ return False
85
89
  except PermissionDenied:
86
90
  return False
87
91
  return True
@@ -103,6 +107,11 @@ def _resolve(record_type, note_ids, user):
103
107
  rows = get_db().sql(f'SELECT name, record_id, body FROM "{TABLE}" WHERE record_type = ? AND name IN ('
104
108
  + ','.join('?' for _ in note_ids) + ')', [record_type, *note_ids])
105
109
  records = _existing(target[0], {row["record_id"] for row in rows})
110
+ # A mention does not open a record: a note on a record the reader may not
111
+ # see (access profile, record scope, hidden content) stays out of their
112
+ # inbox, excerpt and title included.
113
+ records = {record_id: record for record_id, record in records.items()
114
+ if _can_access(record_type, record_id, user)}
106
115
  return {row["name"]: {
107
116
  "record_id": row["record_id"], "title": _title(record_type, records[row["record_id"]]),
108
117
  "body": row["body"], "view_url": view_url(record_type, row["record_id"], row["name"]),
@@ -536,15 +536,6 @@ def get_document_class(doctype_slug: str):
536
536
  return doctype, DOCUMENT_CLASSES[doctype]
537
537
 
538
538
 
539
- # Which Company default a doc's `taxes` table draws from — keyed by the taxes
540
- # child doctype, so any doc that has a taxes table is covered without a hardcoded
541
- # doctype list. Selling -> sales default, buying -> purchase default.
542
- _TAX_CHILD_DEFAULT_FIELD = {
543
- "Sales Taxes and Charges": "default_sales_tax_template",
544
- "Purchase Taxes and Charges": "default_purchase_tax_template",
545
- }
546
-
547
-
548
539
  def _default_tax_rows(cls, data: dict) -> list | None:
549
540
  """`taxes[]` rows to seed a new document from its company's default Tax
550
541
  Template, or None to leave taxes untouched.
@@ -552,34 +543,14 @@ def _default_tax_rows(cls, data: dict) -> list | None:
552
543
  Applied ONLY when the caller omits `taxes` entirely — the exact case the chat
553
544
  model kept hitting (it forgets to add MWST). An explicit `taxes: []` is left
554
545
  alone, so a deliberately tax-free doc (export, reverse-charge, exempt) is
555
- expressed by passing an empty list. The document's own tax calc then computes
556
- `tax_amount` from the rate, so this only seeds charge_type/account/rate.
546
+ expressed by passing an empty list. The rule itself lives in the core
547
+ (lambda_erp.controllers.tax_defaults), which also applies it to invoices
548
+ the core writes on its own.
557
549
  """
558
- taxes_child = cls.CHILD_TABLES.get("taxes")
559
- if not taxes_child or "taxes" in data:
560
- return None
561
- field = _TAX_CHILD_DEFAULT_FIELD.get(taxes_child[0])
562
- company = data.get("company")
563
- if not field or not company:
564
- return None
565
- db = get_db()
566
- template = db.get_value("Company", company, field)
567
- if not template:
550
+ if "taxes" in data:
568
551
  return None
569
- details = db.get_all("Tax Template Detail", filters={"parent": template},
570
- fields=["*"], order_by="idx")
571
- if not details:
572
- return None
573
- return [
574
- {
575
- "charge_type": d.get("charge_type") or "On Net Total",
576
- "account_head": d.get("account_head"),
577
- "rate": d.get("rate") or 0,
578
- "description": d.get("description") or template,
579
- "add_deduct_tax": "Add",
580
- }
581
- for d in details
582
- ]
552
+ from lambda_erp.controllers.tax_defaults import default_tax_rows
553
+ return default_tax_rows(cls, data.get("company"))
583
554
 
584
555
 
585
556
  def document_field_metadata(doctype_slug: str) -> dict:
@@ -650,6 +621,15 @@ def _document_validation(doc):
650
621
  warnings.append({'code': 'price_missing', 'message': (
651
622
  f"Row {idx}: no price found for {row.get('item_code')} (no Item Price applies and the item "
652
623
  f"has no standard rate); the line is priced at 0.")})
624
+ # A tax row without an account cannot be posted. Said on the draft, and
625
+ # on quotations and orders too, where it would otherwise travel unnoticed
626
+ # into the invoice.
627
+ from lambda_erp.validation import tax_rows_without_account
628
+ if doc.docstatus == 0:
629
+ for idx, label in tax_rows_without_account(doc):
630
+ warnings.append({'code': 'tax_account_missing', 'field': 'taxes', 'message': (
631
+ f"Taxes row {idx} ({label}) has no account. An invoice with this tax row cannot be submitted "
632
+ f"until the tax account is set on the row.")})
653
633
  if doc.DOCTYPE == 'Proposal' and (not doc.get('customer') or not doc.get('company') or not doc.get('quotations')):
654
634
  warnings.append({'code': 'proposal_incomplete', 'message': 'Draft saved; PDF output requires Customer, Company and at least one Quotation.'})
655
635
  if doc.DOCTYPE == "Reservation" and not doc.get("asset") and doc.get("status") in {"Reserved", "Out"}:
@@ -268,6 +268,10 @@ export interface RecordAttachment {
268
268
  description?: string | null;
269
269
  uploaded_by?: string | null;
270
270
  created_at?: string | null;
271
+ // What the file counts as: its own setting ("none" = nothing sensitive, a
272
+ // class, or null for the record type's class) and the class that results.
273
+ sensitivity?: string | null;
274
+ sensitivity_class?: string | null;
271
275
  }
272
276
 
273
277
  export interface AccessProfile {
@@ -819,7 +823,12 @@ export const api = {
819
823
  request<any>(`/auth/access-profiles/${encodeURIComponent(name)}`, { method: "DELETE" }),
820
824
  // Files kept with a document or master record.
821
825
  listRecordAttachments: (recordType: string, recordId: string) =>
822
- request<{ attachments: RecordAttachment[] }>(`/record-attachments${qs({ record_type: recordType, record_id: recordId })}`),
826
+ request<{ attachments: RecordAttachment[]; default_class?: string | null; sensitivity_classes?: Record<string, string> }>(
827
+ `/record-attachments${qs({ record_type: recordType, record_id: recordId })}`),
828
+ classifyRecordAttachment: (id: string, sensitivity: string | null) =>
829
+ request<RecordAttachment>(`/record-attachments/${encodeURIComponent(id)}`, {
830
+ method: "PUT", body: JSON.stringify({ sensitivity }),
831
+ }),
823
832
  uploadRecordAttachment: (recordType: string, recordId: string, file: File, description?: string) => {
824
833
  const form = new FormData();
825
834
  form.append("record_type", recordType);
@@ -130,6 +130,12 @@ def run_period_revaluation(company, date=None, *, post=True):
130
130
  breakdown only, nothing posted. Raises if a foreign balance exists but no
131
131
  Unrealized Exchange Gain/Loss account is configured.
132
132
  """
133
+ # It lists open receivables and payables and posts to the ledger: closed
134
+ # to a user whose access profile hides the ledger, or who is restricted
135
+ # to assigned records (lambda_erp.permissions).
136
+ from lambda_erp import permissions
137
+ permissions.require("Journal Entry", "create" if post else "read")
138
+ permissions.require_no_record_scope("the currency revaluation")
133
139
  db = get_db()
134
140
  on_date = date or nowdate()
135
141
  base_ccy = db.get_value("Company", company, "default_currency") or "USD"
@@ -183,5 +183,8 @@ class Subscription(Document):
183
183
  rate=flt(plan.get("rate", 0)),
184
184
  ))
185
185
 
186
+ # A subscription names no taxes of its own: the company's default applies.
187
+ from lambda_erp.controllers.tax_defaults import apply_default_taxes
188
+ apply_default_taxes(invoice)
186
189
  invoice.save()
187
190
  return invoice
@@ -0,0 +1,67 @@
1
+ """The company's default taxes for a new document.
2
+
3
+ A company names a default sales and a default purchase Tax Template. A new
4
+ document that says nothing about taxes gets the rows of the template for its
5
+ side, whoever creates it: the document service (form, REST, chat, MCP) or the
6
+ core itself when it writes an invoice (consignment withdrawal, subscription).
7
+
8
+ taxes not given the company's default rows, if it has a template
9
+ taxes given as [] deliberately no tax (export, exempt): left empty
10
+ taxes given kept as they are, nothing added
11
+
12
+ The rows are copied into the document. From then on they are the document's
13
+ own: a later change of the template or of the company default reaches new
14
+ documents only.
15
+ """
16
+ from lambda_erp.database import get_db
17
+
18
+ # The taxes child table decides the side, so every document type with a
19
+ # taxes table is covered without a list of types.
20
+ TEMPLATE_FIELDS = {
21
+ "Sales Taxes and Charges": "default_sales_tax_template",
22
+ "Purchase Taxes and Charges": "default_purchase_tax_template",
23
+ }
24
+
25
+
26
+ def default_tax_rows(document_class, company):
27
+ """The tax rows a new document of this class takes from its company's
28
+ default Tax Template, or None when the class has no taxes, the company
29
+ names no template for its side, or the template is empty."""
30
+ taxes_child = getattr(document_class, "CHILD_TABLES", {}).get("taxes")
31
+ field = TEMPLATE_FIELDS.get(taxes_child[0]) if taxes_child else None
32
+ if not field or not company:
33
+ return None
34
+ db = get_db()
35
+ template = db.get_value("Company", company, field)
36
+ if not template:
37
+ return None
38
+ # A template of another company is not this company's default.
39
+ owner = db.get_value("Tax Template", template, "company")
40
+ if owner and owner != company:
41
+ return None
42
+ details = db.get_all("Tax Template Detail", filters={"parent": template}, fields=["*"], order_by="idx")
43
+ if not details:
44
+ return None
45
+ return [{
46
+ "charge_type": detail.get("charge_type") or "On Net Total",
47
+ "account_head": detail.get("account_head"),
48
+ "rate": detail.get("rate") or 0,
49
+ "description": detail.get("description") or template,
50
+ "add_deduct_tax": "Add",
51
+ } for detail in details]
52
+
53
+
54
+ def apply_default_taxes(doc):
55
+ """Give a document the core has just built, and that carries no tax rows,
56
+ its company's default taxes. For invoices written from something that has
57
+ no taxes of its own; a document made from another one copies that one's
58
+ rows instead. Returns True when rows were added."""
59
+ if doc.get("taxes"):
60
+ return False
61
+ rows = default_tax_rows(type(doc), doc.get("company"))
62
+ if not rows:
63
+ return False
64
+ from lambda_erp.utils import _dict
65
+ for row in rows:
66
+ doc.append("taxes", _dict(row))
67
+ return True