lambda-erp 1.4.20__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.20 → lambda_erp-1.4.21}/PKG-INFO +1 -1
  2. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/services.py +6 -35
  3. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/accounting/subscription.py +3 -0
  4. lambda_erp-1.4.21/lambda_erp/controllers/tax_defaults.py +67 -0
  5. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/controllers/taxes_and_totals.py +138 -141
  6. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/stock/consignment.py +3 -0
  7. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/utils.py +40 -2
  8. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/pyproject.toml +1 -1
  9. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/.gitignore +0 -0
  10. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/LICENSE +0 -0
  11. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/README.md +0 -0
  12. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/__init__.py +0 -0
  13. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/attachments.py +0 -0
  14. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/auth.py +0 -0
  15. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/bootstrap.py +0 -0
  16. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/chat.py +0 -0
  17. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/chat_links.py +0 -0
  18. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/demo_limits.py +0 -0
  19. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/deps.py +0 -0
  20. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/erp_context.py +0 -0
  21. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/errors.py +0 -0
  22. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/list_values.py +0 -0
  23. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/llm_provider.py +0 -0
  24. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/main.py +0 -0
  25. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/mcp_oauth.py +0 -0
  26. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/mentions.py +0 -0
  27. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/menu_layout.py +0 -0
  28. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/oauth.py +0 -0
  29. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/pdf.py +0 -0
  30. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/pdf_contract.py +0 -0
  31. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/pdf_exports.py +0 -0
  32. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/pdf_fonts.py +0 -0
  33. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/pdf_pages.py +0 -0
  34. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/pdf_profiles.py +0 -0
  35. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/pdf_templates.py +0 -0
  36. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/pricing_actions.py +0 -0
  37. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/providers.py +0 -0
  38. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/record_attachments.py +0 -0
  39. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/record_notes.py +0 -0
  40. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/remarks_md.py +0 -0
  41. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/routers/__init__.py +0 -0
  42. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/routers/accounting.py +0 -0
  43. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/routers/actions.py +0 -0
  44. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/routers/admin.py +0 -0
  45. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/routers/analytics.py +0 -0
  46. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/routers/availability.py +0 -0
  47. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/routers/bank_reconciliation.py +0 -0
  48. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/routers/bank_statements.py +0 -0
  49. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/routers/chat_api.py +0 -0
  50. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/routers/documents.py +0 -0
  51. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/routers/masters.py +0 -0
  52. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/routers/mcp.py +0 -0
  53. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/routers/pdf_templates.py +0 -0
  54. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/routers/proposals.py +0 -0
  55. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/routers/reports.py +0 -0
  56. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/routers/setup.py +0 -0
  57. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/stock_actions.py +0 -0
  58. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/templates/_proposal_totals.html +0 -0
  59. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/templates/_record_base.html +0 -0
  60. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/templates/delivery_note.html +0 -0
  61. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/templates/document.html +0 -0
  62. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/templates/proposal.html +0 -0
  63. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/templates/record.html +0 -0
  64. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/templates/reservation.html +0 -0
  65. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/time_filters.py +0 -0
  66. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/tool_permissions.py +0 -0
  67. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/api/write_context.py +0 -0
  68. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/docs/agents/README.md +0 -0
  69. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/frontend/LICENSE +0 -0
  70. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/frontend/README.md +0 -0
  71. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/frontend/src/api/client.ts +0 -0
  72. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/__init__.py +0 -0
  73. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/accounting/__init__.py +0 -0
  74. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/accounting/bank_account.py +0 -0
  75. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/accounting/bank_reconciliation.py +0 -0
  76. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/accounting/bank_statement_import.py +0 -0
  77. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/accounting/bank_transaction.py +0 -0
  78. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/accounting/budget.py +0 -0
  79. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/accounting/camt.py +0 -0
  80. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/accounting/chart_of_accounts.py +0 -0
  81. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/accounting/general_ledger.py +0 -0
  82. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/accounting/journal_entry.py +0 -0
  83. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/accounting/payment_entry.py +0 -0
  84. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/accounting/pos_invoice.py +0 -0
  85. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/accounting/purchase_invoice.py +0 -0
  86. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/accounting/revaluation.py +0 -0
  87. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/accounting/sales_invoice.py +0 -0
  88. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/accounting/settlement.py +0 -0
  89. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/accounting/setup/__init__.py +0 -0
  90. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/accounting/setup/engine.py +0 -0
  91. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/accounting/setup/pack.py +0 -0
  92. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/accounting/setup/packs/__init__.py +0 -0
  93. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/accounting/setup/packs/ch.py +0 -0
  94. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/accounting/setup/packs/de_common.py +0 -0
  95. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/accounting/setup/packs/de_skr03.py +0 -0
  96. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/accounting/setup/packs/de_skr04.py +0 -0
  97. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/accounting/setup/packs/generic.py +0 -0
  98. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/accounting/setup/profiles.py +0 -0
  99. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/accounting/setup/spine.py +0 -0
  100. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/addresses.py +0 -0
  101. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/assets/__init__.py +0 -0
  102. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/assets/asset.py +0 -0
  103. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/assets/lifecycle.py +0 -0
  104. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/assets/reservation.py +0 -0
  105. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/backlog.py +0 -0
  106. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/bundles.py +0 -0
  107. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/buying/__init__.py +0 -0
  108. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/buying/purchase_order.py +0 -0
  109. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/controllers/__init__.py +0 -0
  110. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/controllers/currency.py +0 -0
  111. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/controllers/defaults.py +0 -0
  112. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/controllers/item_price.py +0 -0
  113. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/controllers/item_prices.py +0 -0
  114. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/controllers/price_resolution.py +0 -0
  115. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/controllers/pricing_rule.py +0 -0
  116. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/database.py +0 -0
  117. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/exceptions.py +0 -0
  118. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/hooks.py +0 -0
  119. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/master_integrity.py +0 -0
  120. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/model.py +0 -0
  121. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/permissions.py +0 -0
  122. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/selling/__init__.py +0 -0
  123. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/selling/proposal.py +0 -0
  124. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/selling/quotation.py +0 -0
  125. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/selling/sales_order.py +0 -0
  126. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/simulation.py +0 -0
  127. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/stock/__init__.py +0 -0
  128. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/stock/delivery_note.py +0 -0
  129. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/stock/purchase_receipt.py +0 -0
  130. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/stock/reservation.py +0 -0
  131. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/stock/serial.py +0 -0
  132. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/stock/stock_count.py +0 -0
  133. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/stock/stock_entry.py +0 -0
  134. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/stock/stock_ledger.py +0 -0
  135. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/timestamps.py +0 -0
  136. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/validation.py +0 -0
  137. {lambda_erp-1.4.20 → lambda_erp-1.4.21}/lambda_erp/workflow.py +0 -0
  138. {lambda_erp-1.4.20 → 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.20
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
@@ -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:
@@ -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
@@ -10,12 +10,32 @@ charge types:
10
10
  - "On Item Quantity": fixed amount per unit
11
11
 
12
12
  Taxes can be inclusive (included in the printed price) or exclusive (added on top).
13
+
14
+ Rounding
15
+ --------
16
+ Amounts are calculated as decimal numbers and rounded half-up (away from
17
+ zero) to the cent, at these points and nowhere in between:
18
+
19
+ - a line: rate x qty -> amount, once per line;
20
+ - a tax row: the tax of all lines is added up unrounded, then the row is
21
+ rounded once (3 x 31.995 = 95.985 -> 95.99, not 3 x 32.00 and not 95.98);
22
+ - the totals are sums of those rounded amounts;
23
+ - base-currency amounts are the rounded document amounts times the
24
+ conversion rate, rounded once.
25
+
26
+ A return is the same calculation with negative quantities and gives exactly
27
+ the negative amounts. `rounded_total` is the grand total rounded half-up to
28
+ whole currency units, with `rounding_adjustment` the difference; both are
29
+ informational: nothing is posted, owed or paid from them.
13
30
  """
14
31
 
15
32
  import json
16
- from lambda_erp.utils import flt, cint, _dict
33
+ from decimal import Decimal
34
+ from lambda_erp.utils import flt, cint, _dict, dec, money
17
35
  from lambda_erp.exceptions import ValidationError
18
36
 
37
+ HUNDRED = Decimal(100)
38
+
19
39
  class TaxCalculator:
20
40
  """Calculate taxes and totals for a transaction document.
21
41
 
@@ -57,7 +77,7 @@ class TaxCalculator:
57
77
  - Price list rate -> discounted rate
58
78
  - Base currency conversion
59
79
  """
60
- conversion_rate = flt(self.doc.get("conversion_rate") or 1.0)
80
+ conversion_rate = dec(self.doc.get("conversion_rate") or 1.0)
61
81
 
62
82
  for item in self.doc.get("items"):
63
83
  # Apply discount to get rate from price_list_rate
@@ -66,30 +86,26 @@ class TaxCalculator:
66
86
  elif flt(item.get("discount_percentage")) == 100:
67
87
  item["rate"] = 0.0
68
88
  elif item.get("price_list_rate") is not None and (flt(item['price_list_rate']) != 0 or item.get('rate') is None):
69
- if item.get("rate") is None or flt(item.get("discount_percentage")) > 0:
70
- item["rate"] = flt(
71
- flt(item["price_list_rate"]) * (1.0 - flt(item.get("discount_percentage")) / 100.0),
72
- 2,
73
- )
74
- item["discount_amount"] = flt(
75
- flt(item["price_list_rate"]) * flt(item.get("discount_percentage")) / 100.0, 2
76
- )
89
+ list_rate, percent = dec(item["price_list_rate"]), dec(item.get("discount_percentage"))
90
+ if item.get("rate") is None or percent > 0:
91
+ item["rate"] = money(list_rate * (1 - percent / HUNDRED))
92
+ item["discount_amount"] = money(list_rate * percent / HUNDRED)
77
93
  elif item.get("discount_amount"):
78
- item["rate"] = flt(item["price_list_rate"]) - flt(item["discount_amount"])
94
+ item["rate"] = float(list_rate - dec(item["discount_amount"]))
79
95
 
96
+ rate = dec(item.get("rate"))
80
97
  # Set net_rate = rate (before inclusive tax adjustment)
81
- item["net_rate"] = flt(item.get("rate"), 2)
98
+ item["net_rate"] = money(rate)
82
99
 
83
- # Calculate amount
84
- qty = flt(item.get("qty", 0))
85
- item["amount"] = flt(flt(item.get("rate")) * qty, 2)
86
- item["net_amount"] = flt(item["amount"], 2)
100
+ # Calculate amount: the decimal product, rounded once
101
+ item["amount"] = money(rate * dec(item.get("qty", 0)))
102
+ item["net_amount"] = item["amount"]
87
103
 
88
104
  # Set base currency values
89
- item["base_rate"] = flt(flt(item.get("rate")) * conversion_rate, 2)
90
- item["base_amount"] = flt(flt(item["amount"]) * conversion_rate, 2)
91
- item["base_net_rate"] = flt(flt(item["net_rate"]) * conversion_rate, 2)
92
- item["base_net_amount"] = flt(flt(item["net_amount"]) * conversion_rate, 2)
105
+ item["base_rate"] = money(rate * conversion_rate)
106
+ item["base_amount"] = money(dec(item["amount"]) * conversion_rate)
107
+ item["base_net_rate"] = money(dec(item["net_rate"]) * conversion_rate)
108
+ item["base_net_amount"] = money(dec(item["net_amount"]) * conversion_rate)
93
109
 
94
110
  def initialize_taxes(self):
95
111
  """Reset tax computation fields before recalculating.
@@ -124,71 +140,66 @@ class TaxCalculator:
124
140
  if not any(cint(tax.get("included_in_print_rate")) for tax in taxes):
125
141
  return
126
142
 
143
+ conversion_rate = dec(self.doc.get("conversion_rate") or 1.0)
127
144
  for item in self.doc.get("items"):
128
145
  item_tax_map = self._load_item_tax_rate(item.get("item_tax_rate"))
129
- cumulated_tax_fraction = 0
130
- total_inclusive_tax_amount_per_qty = 0
146
+ cumulated_tax_fraction = Decimal(0)
147
+ total_inclusive_tax_amount_per_qty = Decimal(0)
148
+ fractions = [] # per tax row: (tax fraction, grand total fraction), exact
131
149
 
132
150
  for i, tax in enumerate(taxes):
133
151
  tax_fraction, inclusive_amount_per_qty = self._get_current_tax_fraction(
134
- tax, item_tax_map, i
152
+ tax, item_tax_map, i, fractions
135
153
  )
136
- tax["tax_fraction_for_current_item"] = tax_fraction
137
-
138
- if i == 0:
139
- tax["grand_total_fraction_for_current_item"] = 1 + tax_fraction
140
- else:
141
- tax["grand_total_fraction_for_current_item"] = (
142
- taxes[i - 1].get("grand_total_fraction_for_current_item", 0) + tax_fraction
143
- )
154
+ grand_fraction = (1 + tax_fraction) if i == 0 else fractions[i - 1][1] + tax_fraction
155
+ fractions.append((tax_fraction, grand_fraction))
156
+ tax["tax_fraction_for_current_item"] = float(tax_fraction)
157
+ tax["grand_total_fraction_for_current_item"] = float(grand_fraction)
144
158
 
145
159
  cumulated_tax_fraction += tax_fraction
146
- total_inclusive_tax_amount_per_qty += inclusive_amount_per_qty * flt(item.get("qty"))
160
+ total_inclusive_tax_amount_per_qty += inclusive_amount_per_qty * dec(item.get("qty"))
147
161
 
148
162
  if item.get("qty") and (cumulated_tax_fraction or total_inclusive_tax_amount_per_qty):
149
- amount = flt(item["amount"]) - total_inclusive_tax_amount_per_qty
150
- item["net_amount"] = flt(amount / (1 + cumulated_tax_fraction), 2)
151
- item["net_rate"] = flt(item["net_amount"] / flt(item["qty"]), 2)
163
+ amount = dec(item["amount"]) - total_inclusive_tax_amount_per_qty
164
+ item["net_amount"] = money(amount / (1 + cumulated_tax_fraction))
165
+ item["net_rate"] = money(dec(item["net_amount"]) / dec(item["qty"]))
152
166
 
153
- conversion_rate = flt(self.doc.get("conversion_rate") or 1.0)
154
- item["base_net_rate"] = flt(item["net_rate"] * conversion_rate, 2)
155
- item["base_net_amount"] = flt(item["net_amount"] * conversion_rate, 2)
167
+ item["base_net_rate"] = money(dec(item["net_rate"]) * conversion_rate)
168
+ item["base_net_amount"] = money(dec(item["net_amount"]) * conversion_rate)
156
169
 
157
- def _get_current_tax_fraction(self, tax, item_tax_map, idx):
158
- """Get the tax fraction for back-calculating exclusive rate from inclusive price."""
159
- current_tax_fraction = 0
160
- inclusive_tax_amount_per_qty = 0
170
+ def _get_current_tax_fraction(self, tax, item_tax_map, idx, fractions=()):
171
+ """Get the tax fraction for back-calculating exclusive rate from inclusive price.
172
+
173
+ `fractions` are the exact (tax, grand total) fractions of the rows
174
+ before this one, for rows that refer to a previous row."""
175
+ current_tax_fraction = Decimal(0)
176
+ inclusive_tax_amount_per_qty = Decimal(0)
161
177
 
162
178
  if cint(tax.get("included_in_print_rate")):
163
179
  tax_rate = self._get_tax_rate(tax, item_tax_map)
164
180
  charge_type = tax.get("charge_type", "On Net Total")
165
- taxes = self.doc.get("taxes") or []
166
181
 
167
182
  if charge_type == "On Net Total":
168
- current_tax_fraction = tax_rate / 100.0
183
+ current_tax_fraction = tax_rate / HUNDRED
169
184
  elif charge_type == "On Previous Row Amount":
170
185
  row_id = cint(tax.get("row_id", 0)) - 1
171
- if 0 <= row_id < len(taxes):
172
- current_tax_fraction = (tax_rate / 100.0) * taxes[row_id].get(
173
- "tax_fraction_for_current_item", 0
174
- )
186
+ if 0 <= row_id < len(fractions):
187
+ current_tax_fraction = (tax_rate / HUNDRED) * fractions[row_id][0]
175
188
  elif charge_type == "On Previous Row Total":
176
189
  row_id = cint(tax.get("row_id", 0)) - 1
177
- if 0 <= row_id < len(taxes):
178
- current_tax_fraction = (tax_rate / 100.0) * taxes[row_id].get(
179
- "grand_total_fraction_for_current_item", 0
180
- )
190
+ if 0 <= row_id < len(fractions):
191
+ current_tax_fraction = (tax_rate / HUNDRED) * fractions[row_id][1]
181
192
  elif charge_type == "On Item Quantity":
182
- inclusive_tax_amount_per_qty = flt(tax_rate)
193
+ inclusive_tax_amount_per_qty = tax_rate
183
194
 
184
195
  return current_tax_fraction, inclusive_tax_amount_per_qty
185
196
 
186
197
  def _get_tax_rate(self, tax, item_tax_map):
187
- """Get tax rate, checking item-specific overrides first."""
198
+ """Get tax rate (a Decimal), checking item-specific overrides first."""
188
199
  account_head = tax.get("account_head", "")
189
200
  if account_head in item_tax_map:
190
- return flt(item_tax_map[account_head])
191
- return flt(tax.get("rate", 0))
201
+ return dec(item_tax_map[account_head])
202
+ return dec(tax.get("rate", 0))
192
203
 
193
204
  def _load_item_tax_rate(self, item_tax_rate):
194
205
  """Parse item_tax_rate JSON string to dict."""
@@ -204,21 +215,17 @@ class TaxCalculator:
204
215
  def calculate_net_total(self):
205
216
  """Sum up item amounts to get document totals."""
206
217
  doc = self.doc
207
- doc["total_qty"] = 0
208
- doc["total"] = 0
209
- doc["base_total"] = 0
210
- doc["net_total"] = 0
211
- doc["base_net_total"] = 0
212
-
218
+ sums = {field: Decimal(0) for field in ("total_qty", "total", "base_total", "net_total", "base_net_total")}
213
219
  for item in doc.get("items"):
214
- doc["total_qty"] = flt(doc["total_qty"]) + flt(item.get("qty", 0))
215
- doc["total"] = flt(doc["total"]) + flt(item.get("amount", 0))
216
- doc["base_total"] = flt(doc["base_total"]) + flt(item.get("base_amount", 0))
217
- doc["net_total"] = flt(doc["net_total"]) + flt(item.get("net_amount", 0))
218
- doc["base_net_total"] = flt(doc["base_net_total"]) + flt(item.get("base_net_amount", 0))
220
+ sums["total_qty"] += dec(item.get("qty", 0))
221
+ sums["total"] += dec(item.get("amount", 0))
222
+ sums["base_total"] += dec(item.get("base_amount", 0))
223
+ sums["net_total"] += dec(item.get("net_amount", 0))
224
+ sums["base_net_total"] += dec(item.get("base_net_amount", 0))
219
225
 
226
+ doc["total_qty"] = float(sums["total_qty"])
220
227
  for field in ["total", "base_total", "net_total", "base_net_total"]:
221
- doc[field] = flt(doc[field], 2)
228
+ doc[field] = money(sums[field])
222
229
 
223
230
  def calculate_taxes(self):
224
231
  """Calculate tax amounts row by row, item by item.
@@ -228,7 +235,8 @@ class TaxCalculator:
228
235
  the charge_type (On Net Total, On Previous Row Amount, Actual, etc.).
229
236
 
230
237
  The running total accumulates so each subsequent tax row can reference
231
- the cumulative total from previous rows.
238
+ the cumulative total from previous rows. The per-item amounts stay
239
+ exact decimals; each tax row is rounded once, after all items.
232
240
  """
233
241
  taxes = self.doc.get("taxes") or []
234
242
  items = self.doc.get("items") or []
@@ -236,84 +244,75 @@ class TaxCalculator:
236
244
  return
237
245
 
238
246
  # For "Actual" charge type, distribute evenly across items
239
- actual_tax_dict = {}
240
- for tax in taxes:
241
- if tax.get("charge_type") == "Actual":
242
- actual_tax_dict[tax.get("idx", 0)] = flt(tax.get("tax_amount", 0))
247
+ actual_left = {i: dec(tax.get("tax_amount", 0)) for i, tax in enumerate(taxes)
248
+ if tax.get("charge_type") == "Actual"}
249
+ accumulated = [Decimal(0)] * len(taxes)
243
250
 
244
251
  for n, item in enumerate(items):
245
252
  item_tax_map = self._load_item_tax_rate(item.get("item_tax_rate"))
253
+ current = [] # this item's exact tax per row
254
+ running = [] # this item's exact running total per row
246
255
 
247
256
  for i, tax in enumerate(taxes):
248
- current_tax_amount = self._get_current_tax_amount(item, tax, item_tax_map)
257
+ amount = self._get_current_tax_amount(item, tax, item_tax_map, current, running)
249
258
 
250
259
  # Adjust divisional loss to the last item (the reference implementation pattern)
251
- if tax.get("charge_type") == "Actual":
252
- idx = tax.get("idx", 0)
253
- actual_tax_dict[idx] = actual_tax_dict.get(idx, 0) - current_tax_amount
260
+ if i in actual_left:
261
+ actual_left[i] -= amount
254
262
  if n == len(items) - 1:
255
- current_tax_amount += actual_tax_dict.get(idx, 0)
256
-
257
- # Accumulate tax amount
258
- if tax.get("charge_type") != "Actual":
259
- tax["tax_amount"] = flt(tax.get("tax_amount", 0)) + current_tax_amount
260
-
261
- tax["tax_amount_for_current_item"] = current_tax_amount
262
-
263
- # Build running grand total for this item
264
- if i == 0:
265
- tax["grand_total_for_current_item"] = flt(item.get("net_amount", 0)) + current_tax_amount
263
+ amount += actual_left[i]
266
264
  else:
267
- tax["grand_total_for_current_item"] = flt(
268
- taxes[i - 1].get("grand_total_for_current_item", 0)
269
- ) + current_tax_amount
265
+ accumulated[i] += amount
270
266
 
271
- # Set cumulative totals on each tax row
272
- for i, tax in enumerate(taxes):
273
- tax["tax_amount"] = flt(tax.get("tax_amount", 0), 2)
274
- if i == 0:
275
- tax["total"] = flt(self.doc.get("net_total", 0)) + tax["tax_amount"]
276
- else:
277
- tax["total"] = flt(taxes[i - 1]["total"]) + tax["tax_amount"]
267
+ current.append(amount)
268
+ running.append((dec(item.get("net_amount", 0)) if i == 0 else running[i - 1]) + amount)
269
+ tax["tax_amount_for_current_item"] = float(amount)
270
+ tax["grand_total_for_current_item"] = float(running[i])
278
271
 
279
- tax["total"] = flt(tax["total"], 2)
272
+ # Round each tax row once, then set the cumulative totals
273
+ conversion_rate = dec(self.doc.get("conversion_rate") or 1.0)
274
+ for i, tax in enumerate(taxes):
275
+ tax["tax_amount"] = money(tax.get("tax_amount", 0)) if i in actual_left else money(accumulated[i])
276
+ previous = dec(self.doc.get("net_total", 0)) if i == 0 else dec(taxes[i - 1]["total"])
277
+ tax["total"] = money(previous + dec(tax["tax_amount"]))
280
278
 
281
279
  # Base currency
282
- conversion_rate = flt(self.doc.get("conversion_rate") or 1.0)
283
- tax["base_tax_amount"] = flt(tax["tax_amount"] * conversion_rate, 2)
284
- tax["base_total"] = flt(tax["total"] * conversion_rate, 2)
280
+ tax["base_tax_amount"] = money(dec(tax["tax_amount"]) * conversion_rate)
281
+ tax["base_total"] = money(dec(tax["total"]) * conversion_rate)
285
282
 
286
- def _get_current_tax_amount(self, item, tax, item_tax_map):
287
- """Compute tax for one item + one tax row."""
283
+ def _get_current_tax_amount(self, item, tax, item_tax_map, current=(), running=()):
284
+ """Compute tax for one item + one tax row, as an exact Decimal.
285
+
286
+ `current` and `running` are this item's exact tax amounts and running
287
+ totals of the rows before this one."""
288
288
  tax_rate = self._get_tax_rate(tax, item_tax_map)
289
289
  charge_type = tax.get("charge_type", "On Net Total")
290
- taxes = self.doc.get("taxes") or []
290
+ net_amount = dec(item.get("net_amount", 0))
291
291
 
292
292
  if charge_type == "Actual":
293
293
  # Distribute actual amount proportionally across items
294
- total = flt(self.doc.get("net_total")) or 1
295
- proportion = flt(item.get("net_amount", 0)) / total
296
- return flt(tax.get("tax_amount", 0)) * proportion
294
+ total = dec(self.doc.get("net_total")) or Decimal(1)
295
+ return dec(tax.get("tax_amount", 0)) * net_amount / total
297
296
 
298
297
  elif charge_type == "On Net Total":
299
- return flt(item.get("net_amount", 0)) * tax_rate / 100.0
298
+ return net_amount * tax_rate / HUNDRED
300
299
 
301
300
  elif charge_type == "On Previous Row Amount":
302
301
  row_id = cint(tax.get("row_id", 0)) - 1
303
- if 0 <= row_id < len(taxes):
304
- return flt(taxes[row_id].get("tax_amount_for_current_item", 0)) * tax_rate / 100.0
305
- return 0
302
+ if 0 <= row_id < len(current):
303
+ return current[row_id] * tax_rate / HUNDRED
304
+ return Decimal(0)
306
305
 
307
306
  elif charge_type == "On Previous Row Total":
308
307
  row_id = cint(tax.get("row_id", 0)) - 1
309
- if 0 <= row_id < len(taxes):
310
- return flt(taxes[row_id].get("grand_total_for_current_item", 0)) * tax_rate / 100.0
311
- return 0
308
+ if 0 <= row_id < len(running):
309
+ return running[row_id] * tax_rate / HUNDRED
310
+ return Decimal(0)
312
311
 
313
312
  elif charge_type == "On Item Quantity":
314
- return flt(item.get("qty", 0)) * tax_rate
313
+ return dec(item.get("qty", 0)) * tax_rate
315
314
 
316
- return 0
315
+ return Decimal(0)
317
316
 
318
317
  def calculate_totals(self):
319
318
  """Calculate grand_total, rounded_total, etc."""
@@ -321,50 +320,48 @@ class TaxCalculator:
321
320
  taxes = doc.get("taxes") or []
322
321
 
323
322
  if taxes:
324
- doc["grand_total"] = flt(taxes[-1]["total"], 2)
325
- doc["total_taxes_and_charges"] = flt(doc["grand_total"]) - flt(doc["net_total"])
323
+ doc["grand_total"] = money(taxes[-1]["total"])
324
+ doc["total_taxes_and_charges"] = money(dec(doc["grand_total"]) - dec(doc["net_total"]))
326
325
  else:
327
- doc["grand_total"] = flt(doc["net_total"], 2)
326
+ doc["grand_total"] = money(doc["net_total"])
328
327
  doc["total_taxes_and_charges"] = 0
329
328
 
330
- doc["total_taxes_and_charges"] = flt(doc["total_taxes_and_charges"], 2)
329
+ conversion_rate = dec(doc.get("conversion_rate") or 1.0)
330
+ doc["base_grand_total"] = money(dec(doc["grand_total"]) * conversion_rate)
331
+ doc["base_total_taxes_and_charges"] = money(dec(doc["total_taxes_and_charges"]) * conversion_rate)
331
332
 
332
- conversion_rate = flt(doc.get("conversion_rate") or 1.0)
333
- doc["base_grand_total"] = flt(flt(doc["grand_total"]) * conversion_rate, 2)
334
- doc["base_total_taxes_and_charges"] = flt(
335
- flt(doc["total_taxes_and_charges"]) * conversion_rate, 2
336
- )
333
+ self._set_rounded_total()
337
334
 
338
- # Rounded total
339
- doc["rounded_total"] = round(flt(doc["grand_total"]))
340
- doc["rounding_adjustment"] = flt(doc["rounded_total"]) - flt(doc["grand_total"])
335
+ def _set_rounded_total(self):
336
+ """The grand total in whole currency units, half-up, and what that
337
+ differs by. Informational: nothing is posted or owed from it."""
338
+ doc = self.doc
339
+ doc["rounded_total"] = money(doc["grand_total"], 0)
340
+ doc["rounding_adjustment"] = money(dec(doc["rounded_total"]) - dec(doc["grand_total"]))
341
341
 
342
342
  def apply_discount_amount(self):
343
343
  """Apply a flat discount amount to the document."""
344
- discount = flt(self.doc.get("discount_amount"))
344
+ discount = dec(self.doc.get("discount_amount"))
345
345
  if not discount:
346
346
  return
347
347
 
348
348
  if self.doc.get("apply_discount_on") == "Net Total":
349
349
  # Distribute discount across items proportionally
350
- net_total = flt(self.doc.get("net_total")) or 1
350
+ net_total = dec(self.doc.get("net_total")) or Decimal(1)
351
351
  for item in self.doc.get("items"):
352
- proportion = flt(item.get("net_amount", 0)) / net_total
353
- item_discount = flt(discount * proportion, 2)
354
- item["net_amount"] = flt(item["net_amount"]) - item_discount
352
+ item_discount = dec(money(discount * dec(item.get("net_amount", 0)) / net_total))
353
+ item["net_amount"] = float(dec(item["net_amount"]) - item_discount)
355
354
  if flt(item.get("qty")):
356
- item["net_rate"] = flt(item["net_amount"] / flt(item["qty"]), 2)
355
+ item["net_rate"] = money(dec(item["net_amount"]) / dec(item["qty"]))
357
356
 
358
357
  self.discount_amount_applied = True
359
358
  self._calculate()
360
359
  else:
361
360
  # Discount on Grand Total - subtract from grand total
362
- self.doc["grand_total"] = flt(self.doc["grand_total"]) - discount
363
- self.doc["base_grand_total"] = flt(self.doc["grand_total"]) * flt(
364
- self.doc.get("conversion_rate") or 1.0
365
- )
366
- self.doc["rounded_total"] = round(flt(self.doc["grand_total"]))
367
- self.doc["rounding_adjustment"] = flt(self.doc["rounded_total"]) - flt(self.doc["grand_total"])
361
+ self.doc["grand_total"] = money(dec(self.doc["grand_total"]) - discount)
362
+ self.doc["base_grand_total"] = money(
363
+ dec(self.doc["grand_total"]) * dec(self.doc.get("conversion_rate") or 1.0))
364
+ self._set_rounded_total()
368
365
 
369
366
  def calculate_taxes_and_totals(doc):
370
367
  """Convenience function matching the reference implementation's pattern."""
@@ -183,6 +183,9 @@ class ConsignmentWithdrawal(Document):
183
183
  if row.get("rate") is not None:
184
184
  line["rate"] = flt(row["rate"])
185
185
  invoice.append("items", line)
186
+ # The sale is taxed like any other invoice of the company.
187
+ from lambda_erp.controllers.tax_defaults import apply_default_taxes
188
+ apply_default_taxes(invoice)
186
189
  invoice.save()
187
190
  invoice.submit()
188
191
  self._data["sales_invoice"] = invoice.name
@@ -5,13 +5,49 @@ These replace framework.utils.flt, cint, getdate, etc. that are used
5
5
  throughout the the reference implementation business logic.
6
6
  """
7
7
 
8
+ import math
9
+ from decimal import Decimal, InvalidOperation, ROUND_HALF_UP
8
10
  import math
9
11
  from datetime import date, datetime, timedelta, timezone
10
12
  from decimal import Decimal, ROUND_HALF_UP
11
13
 
14
+ def dec(value):
15
+ """A number as a Decimal with the decimal value it was written as: the
16
+ float 8.1 is Decimal('8.1'), not the binary 8.0999999999999996447....
17
+ None, '' and anything that is not a number are 0."""
18
+ if isinstance(value, Decimal):
19
+ return value
20
+ if value is None or value == "" or isinstance(value, bool):
21
+ return Decimal(int(value)) if isinstance(value, bool) else Decimal(0)
22
+ try:
23
+ if isinstance(value, float):
24
+ # repr() is the shortest text that reads back as the same float.
25
+ return Decimal(repr(value)) if math.isfinite(value) else Decimal(0)
26
+ return Decimal(str(value).strip())
27
+ except (InvalidOperation, ValueError, TypeError):
28
+ return Decimal(0)
29
+
30
+
31
+ def round_half_up(value, precision=2):
32
+ """Round a Decimal (or number) to `precision` places, halves away from
33
+ zero: 95.985 -> 95.99, -95.985 -> -95.99, 2.5 -> 3 at precision 0."""
34
+ return dec(value).quantize(Decimal(1).scaleb(-int(precision)), rounding=ROUND_HALF_UP)
35
+
36
+
37
+ def money(value, precision=2):
38
+ """An amount rounded the way the books round: decimal, halves away from
39
+ zero, to the cent unless another precision is asked for. Returns a float
40
+ for storage and JSON; calculate with dec() before, not with the result."""
41
+ return float(round_half_up(value, precision))
42
+
43
+
12
44
  def flt(value, precision=None):
13
45
  """Convert to float, with optional rounding precision. Safe numeric
14
- conversion used everywhere amounts are handled."""
46
+ conversion used everywhere amounts are handled.
47
+
48
+ Rounding is decimal and half-up (see money): Python's round() works on
49
+ the binary value and to even, which turns 2.675 into 2.67 and 0.125 into
50
+ 0.12 and so loses or gains a cent depending on the digits."""
15
51
  if value is None or value == "":
16
52
  return 0.0
17
53
  try:
@@ -19,7 +55,9 @@ def flt(value, precision=None):
19
55
  except (ValueError, TypeError):
20
56
  return 0.0
21
57
  if precision is not None:
22
- value = round(value, precision)
58
+ if not math.isfinite(value):
59
+ return value
60
+ value = float(round_half_up(value, precision))
23
61
  return value
24
62
 
25
63
  def cint(value):
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "lambda-erp"
3
- version = "1.4.20"
3
+ version = "1.4.21"
4
4
  description = "Core ERP logic - accounting, sales, purchasing, inventory"
5
5
  readme = "README.md"
6
6
  license = "Apache-2.0"
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes