lambda-erp 1.4.39__tar.gz → 1.4.42__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.
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/PKG-INFO +1 -1
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/chat.py +8 -7
- lambda_erp-1.4.42/api/llm_provider.py +153 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/pdf.py +7 -1
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/providers.py +132 -13
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/accounting/purchase_invoice.py +46 -1
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/database.py +15 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/pyproject.toml +1 -1
- lambda_erp-1.4.39/api/llm_provider.py +0 -110
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/.gitignore +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/LICENSE +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/README.md +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/__init__.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/attachments.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/auth.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/bootstrap.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/chat_links.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/demo_limits.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/deps.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/erp_context.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/errors.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/list_columns.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/list_values.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/mail.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/main.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/mcp_oauth.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/mentions.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/menu_layout.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/number_series.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/oauth.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/order_actions.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/pdf_contract.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/pdf_exports.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/pdf_fonts.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/pdf_pages.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/pdf_profiles.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/pdf_templates.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/pricing_actions.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/record_attachments.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/record_notes.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/remarks_md.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/routers/__init__.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/routers/accounting.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/routers/actions.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/routers/admin.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/routers/analytics.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/routers/availability.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/routers/bank_reconciliation.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/routers/bank_statements.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/routers/chat_api.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/routers/documents.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/routers/masters.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/routers/mcp.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/routers/pdf_templates.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/routers/proposals.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/routers/reports.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/routers/setup.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/services.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/stock_actions.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/templates/_proposal_totals.html +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/templates/_record_base.html +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/templates/delivery_note.html +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/templates/document.html +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/templates/proposal.html +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/templates/record.html +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/templates/reservation.html +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/time_filters.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/tool_permissions.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/tool_results.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/api/write_context.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/docs/agents/README.md +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/frontend/LICENSE +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/frontend/README.md +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/frontend/src/api/client.ts +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/__init__.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/accounting/__init__.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/accounting/bank_account.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/accounting/bank_reconciliation.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/accounting/bank_statement_import.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/accounting/bank_transaction.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/accounting/budget.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/accounting/camt.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/accounting/chart_of_accounts.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/accounting/general_ledger.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/accounting/journal_entry.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/accounting/payment_entry.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/accounting/pos_invoice.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/accounting/revaluation.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/accounting/sales_invoice.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/accounting/settlement.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/accounting/setup/__init__.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/accounting/setup/engine.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/accounting/setup/pack.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/accounting/setup/packs/__init__.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/accounting/setup/packs/ch.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/accounting/setup/packs/de_common.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/accounting/setup/packs/de_skr03.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/accounting/setup/packs/de_skr04.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/accounting/setup/packs/generic.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/accounting/setup/profiles.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/accounting/setup/spine.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/accounting/subscription.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/addresses.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/assets/__init__.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/assets/asset.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/assets/lifecycle.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/assets/reservation.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/backlog.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/bundles.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/buying/__init__.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/buying/purchase_order.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/controllers/__init__.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/controllers/currency.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/controllers/defaults.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/controllers/item_price.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/controllers/item_prices.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/controllers/price_resolution.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/controllers/pricing_rule.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/controllers/tax_defaults.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/controllers/taxes_and_totals.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/customer_terms.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/exceptions.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/hooks.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/master_integrity.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/model.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/order_closing.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/permissions.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/selling/__init__.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/selling/proposal.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/selling/quotation.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/selling/sales_order.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/simulation.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/stock/__init__.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/stock/consignment.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/stock/delivery_note.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/stock/purchase_receipt.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/stock/reservation.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/stock/serial.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/stock/stock_count.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/stock/stock_entry.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/stock/stock_ledger.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/timestamps.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/units.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/utils.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/validation.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/lambda_erp/workflow.py +0 -0
- {lambda_erp-1.4.39 → lambda_erp-1.4.42}/terraform/README.md +0 -0
|
@@ -40,7 +40,7 @@ from api.demo_limits import (
|
|
|
40
40
|
is_demo_role,
|
|
41
41
|
limiter as demo_limiter,
|
|
42
42
|
)
|
|
43
|
-
from api.llm_provider import client_kwargs, llm_configured, not_configured_message
|
|
43
|
+
from api.llm_provider import client_kwargs, llm_configured, llm_settings, not_configured_message
|
|
44
44
|
from api.providers import cost_of_openai_call, cost_of_transcription
|
|
45
45
|
from api.routers.masters import create_master_record, update_master_record, delete_master_record
|
|
46
46
|
from api.routers.analytics import DATASET_FILTER_DESCRIPTION
|
|
@@ -3206,6 +3206,7 @@ Purchase Order → Purchase Receipt (receiving) / Purchase Invoice (billing) →
|
|
|
3206
3206
|
- **Purchase Order:** Commitment to buy from supplier. No financial or stock impact.
|
|
3207
3207
|
- **Purchase Receipt:** Receives goods into warehouse. **Posts stock entries** (inventory increases). Requires warehouse on each item.
|
|
3208
3208
|
- **Purchase Invoice:** Records supplier bill. Always **posts GL entries** and creates outstanding amount.
|
|
3209
|
+
Set `bill_no` to the supplier's invoice number (preserved verbatim, checked for duplicates per supplier) and `bill_date` to the date printed on the supplier's bill (YYYY-MM-DD, if given) whenever mentioned or visible on an uploaded bill — never drop them or put them into `remarks`.
|
|
3209
3210
|
For non-stock/services: Debit Expense, Credit Accounts Payable.
|
|
3210
3211
|
For stock after a prior Purchase Receipt: Debit Stock Received But Not Billed, Credit Accounts Payable.
|
|
3211
3212
|
If `update_stock=1`: the Purchase Invoice also receives the goods into stock, so stock items require a warehouse and the posting is Debit Stock In Hand, Credit Accounts Payable.
|
|
@@ -3526,7 +3527,7 @@ Users can attach PDFs and images (receipts, bills, contracts, screenshots) to th
|
|
|
3526
3527
|
|
|
3527
3528
|
- **For the current message** — any attached images/PDFs are already in your context; read them carefully and use the content (e.g., parse a supplier invoice into a Purchase Invoice).
|
|
3528
3529
|
- **For files uploaded earlier in the chat** — if the user asks about a previously-uploaded file that is no longer in your immediate context, call `list_chat_attachments` to see what's available, then `retrieve_chat_attachment(attachment_id)` to load it. The retrieved content will appear in the next turn as a user message you can read.
|
|
3529
|
-
- **When a user uploads a bill/invoice and asks you to "add it" or "create a purchase invoice"**, extract supplier, line items, quantities, rates, and totals from the document and call `create_document("purchase-invoice", {...})`. Confirm details back to the user and ask about anything ambiguous (e.g., which existing supplier it matches)."""
|
|
3530
|
+
- **When a user uploads a bill/invoice and asks you to "add it" or "create a purchase invoice"**, extract supplier, supplier invoice number (`bill_no`), supplier invoice date (`bill_date`, if printed on the bill), line items, quantities, rates, and totals from the document and call `create_document("purchase-invoice", {...})`. Confirm details back to the user and ask about anything ambiguous (e.g., which existing supplier it matches)."""
|
|
3530
3531
|
|
|
3531
3532
|
|
|
3532
3533
|
# ---------------------------------------------------------------------------
|
|
@@ -3827,7 +3828,7 @@ def _generate_report_spec_via_openai(
|
|
|
3827
3828
|
actual_cost_usd=cost_of_openai_call(model, usage),
|
|
3828
3829
|
ip=client_ip or "unknown",
|
|
3829
3830
|
role=user_role,
|
|
3830
|
-
provider=
|
|
3831
|
+
provider=llm_settings().provider,
|
|
3831
3832
|
model=model,
|
|
3832
3833
|
prompt_tokens=int(getattr(usage, "input_tokens", 0) or 0) if usage else 0,
|
|
3833
3834
|
completion_tokens=int(getattr(usage, "output_tokens", 0) or 0) if usage else 0,
|
|
@@ -3937,7 +3938,7 @@ async def generate_title(
|
|
|
3937
3938
|
actual_cost_usd=cost_of_openai_call(title_model, usage),
|
|
3938
3939
|
ip=client_ip or "unknown",
|
|
3939
3940
|
role=user_role,
|
|
3940
|
-
provider=
|
|
3941
|
+
provider=llm_settings().provider,
|
|
3941
3942
|
model=title_model,
|
|
3942
3943
|
prompt_tokens=int(getattr(usage, "input_tokens", 0) or 0) if usage else 0,
|
|
3943
3944
|
completion_tokens=int(getattr(usage, "output_tokens", 0) or 0) if usage else 0,
|
|
@@ -4423,7 +4424,7 @@ async def run_thinking_loop(
|
|
|
4423
4424
|
actual_cost_usd=cost_of_openai_call(model_name, usage),
|
|
4424
4425
|
ip=client_ip or "unknown",
|
|
4425
4426
|
role=user_role,
|
|
4426
|
-
provider=
|
|
4427
|
+
provider=llm_settings().provider,
|
|
4427
4428
|
model=model_name,
|
|
4428
4429
|
prompt_tokens=int(getattr(usage, "prompt_tokens", 0) or 0) if usage else 0,
|
|
4429
4430
|
completion_tokens=int(getattr(usage, "completion_tokens", 0) or 0) if usage else 0,
|
|
@@ -4602,7 +4603,7 @@ async def run_thinking_loop(
|
|
|
4602
4603
|
)
|
|
4603
4604
|
demo_limiter.settle(
|
|
4604
4605
|
None, actual_cost_usd=cost_of_openai_call(_chat_model(), usage),
|
|
4605
|
-
ip=client_ip or "unknown", role=user_role, provider=
|
|
4606
|
+
ip=client_ip or "unknown", role=user_role, provider=llm_settings().provider,
|
|
4606
4607
|
model=_chat_model(),
|
|
4607
4608
|
prompt_tokens=int(getattr(usage, "prompt_tokens", 0) or 0) if usage else 0,
|
|
4608
4609
|
completion_tokens=int(getattr(usage, "completion_tokens", 0) or 0) if usage else 0,
|
|
@@ -5218,7 +5219,7 @@ async def chat_websocket(
|
|
|
5218
5219
|
client_ip or "unknown",
|
|
5219
5220
|
cost_of_transcription(TRANSCRIPTION_MODEL, audio_seconds),
|
|
5220
5221
|
role=ws_user_role,
|
|
5221
|
-
provider=
|
|
5222
|
+
provider=llm_settings().provider,
|
|
5222
5223
|
model=TRANSCRIPTION_MODEL,
|
|
5223
5224
|
session_id=data.get("session_id"),
|
|
5224
5225
|
)
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"""Where the ERP's model calls go.
|
|
2
|
+
|
|
3
|
+
LAMBDA_ERP_LLM_PROVIDER=openai (default) OpenAI directly with OPENAI_API_KEY.
|
|
4
|
+
OPENAI_BASE_URL is honoured by the SDK as
|
|
5
|
+
before, for any OpenAI-compatible endpoint.
|
|
6
|
+
LAMBDA_ERP_LLM_PROVIDER=lambda Lambda's model gateway (lambda.dev): the
|
|
7
|
+
same OpenAI API, billed to the Lambda
|
|
8
|
+
organization the key belongs to.
|
|
9
|
+
LAMBDA_LLM_API_KEY a Lambda model key
|
|
10
|
+
(scope llm:erp)
|
|
11
|
+
LAMBDA_LLM_BASE_URL default
|
|
12
|
+
https://platform.lambda.dev/v1/llm/openai
|
|
13
|
+
LAMBDA_ERP_LLM_PROVIDER=azure_foundry Customer-owned Azure AI Foundry (AIServices)
|
|
14
|
+
account via the OpenAI-compatible v1 endpoint
|
|
15
|
+
(https://<account>.openai.azure.com/openai/v1/).
|
|
16
|
+
AZURE_FOUNDRY_API_KEY account key
|
|
17
|
+
AZURE_FOUNDRY_BASE_URL v1 endpoint URL (required)
|
|
18
|
+
AZURE_FOUNDRY_DATA_ZONE global | eu | us (default global)
|
|
19
|
+
|
|
20
|
+
The gateway relays what the ERP uses: Responses (chat, report specialist,
|
|
21
|
+
titles), file uploads for attachments and voice transcription. Chat
|
|
22
|
+
Completions (ERP_CHAT_API=chat) is not available through it. With the lambda
|
|
23
|
+
provider each call names the ERP user (``X-Lambda-ERP-User``, the login name),
|
|
24
|
+
so Lambda's usage view can split the organization's cost by person; OpenAI
|
|
25
|
+
never sees that header.
|
|
26
|
+
|
|
27
|
+
Every OpenAI client in the ERP is built from ``client_kwargs()``.
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
from __future__ import annotations
|
|
31
|
+
|
|
32
|
+
import os
|
|
33
|
+
from dataclasses import dataclass
|
|
34
|
+
from typing import Optional
|
|
35
|
+
|
|
36
|
+
import httpx
|
|
37
|
+
from openai import OpenAI
|
|
38
|
+
|
|
39
|
+
DEFAULT_LAMBDA_BASE_URL = "https://platform.lambda.dev/v1/llm/openai"
|
|
40
|
+
ERP_USER_HEADER = "X-Lambda-ERP-User"
|
|
41
|
+
_PLACEHOLDERS = {"", "sk-your-key-here"}
|
|
42
|
+
_AZURE_FOUNDRY_PRICING_TABLES = {
|
|
43
|
+
"global": "openai",
|
|
44
|
+
"eu": "azure_foundry_eu",
|
|
45
|
+
"us": "azure_foundry_us",
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@dataclass(frozen=True)
|
|
50
|
+
class LLMSettings:
|
|
51
|
+
provider: str # "openai", "lambda", or "azure_foundry"
|
|
52
|
+
api_key: str
|
|
53
|
+
base_url: Optional[str] # None: the SDK's default (or OPENAI_BASE_URL)
|
|
54
|
+
key_variable: str # the env variable the key comes from, for messages
|
|
55
|
+
pricing_table: str = "openai" # "openai", "azure_foundry_eu", or "azure_foundry_us"
|
|
56
|
+
|
|
57
|
+
@property
|
|
58
|
+
def configured(self) -> bool:
|
|
59
|
+
if self.api_key.strip() in _PLACEHOLDERS:
|
|
60
|
+
return False
|
|
61
|
+
if self.provider == "azure_foundry" and not self.base_url:
|
|
62
|
+
return False
|
|
63
|
+
return True
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def llm_settings() -> LLMSettings:
|
|
67
|
+
"""Read on every call, so a changed environment applies without a restart."""
|
|
68
|
+
provider = (os.environ.get("LAMBDA_ERP_LLM_PROVIDER") or "openai").strip().lower()
|
|
69
|
+
if provider == "lambda":
|
|
70
|
+
return LLMSettings(
|
|
71
|
+
provider="lambda",
|
|
72
|
+
api_key=os.environ.get("LAMBDA_LLM_API_KEY", ""),
|
|
73
|
+
base_url=(os.environ.get("LAMBDA_LLM_BASE_URL") or DEFAULT_LAMBDA_BASE_URL).rstrip("/"),
|
|
74
|
+
key_variable="LAMBDA_LLM_API_KEY",
|
|
75
|
+
pricing_table="openai",
|
|
76
|
+
)
|
|
77
|
+
if provider == "azure_foundry":
|
|
78
|
+
zone = (os.environ.get("AZURE_FOUNDRY_DATA_ZONE") or "global").strip().lower()
|
|
79
|
+
pricing_table = _AZURE_FOUNDRY_PRICING_TABLES.get(zone)
|
|
80
|
+
if pricing_table is None:
|
|
81
|
+
raise RuntimeError(f"AZURE_FOUNDRY_DATA_ZONE={zone!r}: use 'global', 'eu', or 'us'")
|
|
82
|
+
base_url = (os.environ.get("AZURE_FOUNDRY_BASE_URL") or "").strip().rstrip("/") or None
|
|
83
|
+
return LLMSettings(
|
|
84
|
+
provider="azure_foundry",
|
|
85
|
+
api_key=os.environ.get("AZURE_FOUNDRY_API_KEY", ""),
|
|
86
|
+
base_url=base_url,
|
|
87
|
+
key_variable="AZURE_FOUNDRY_API_KEY",
|
|
88
|
+
pricing_table=pricing_table,
|
|
89
|
+
)
|
|
90
|
+
if provider != "openai":
|
|
91
|
+
raise RuntimeError(
|
|
92
|
+
f"LAMBDA_ERP_LLM_PROVIDER={provider!r}: use 'openai', 'lambda', or 'azure_foundry'"
|
|
93
|
+
)
|
|
94
|
+
return LLMSettings(
|
|
95
|
+
provider="openai",
|
|
96
|
+
api_key=os.environ.get("OPENAI_API_KEY", ""),
|
|
97
|
+
base_url=None,
|
|
98
|
+
key_variable="OPENAI_API_KEY",
|
|
99
|
+
pricing_table="openai",
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def llm_configured() -> bool:
|
|
104
|
+
try:
|
|
105
|
+
return llm_settings().configured
|
|
106
|
+
except RuntimeError:
|
|
107
|
+
return False
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def not_configured_message() -> str:
|
|
111
|
+
try:
|
|
112
|
+
settings = llm_settings()
|
|
113
|
+
except RuntimeError as exc:
|
|
114
|
+
return str(exc)
|
|
115
|
+
if settings.api_key.strip() in _PLACEHOLDERS:
|
|
116
|
+
return f"{settings.key_variable} is not configured"
|
|
117
|
+
if settings.provider == "azure_foundry" and not settings.base_url:
|
|
118
|
+
return "AZURE_FOUNDRY_BASE_URL is not configured"
|
|
119
|
+
return f"{settings.key_variable} is not configured"
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def _client_timeout(timeout):
|
|
123
|
+
"""The timeout in the type the installed OpenAI SDK expects.
|
|
124
|
+
|
|
125
|
+
Since openai 3.x the SDK runs on its own HTTP client (httpx2), and httpx2
|
|
126
|
+
2.17 rejects an ``httpx.Timeout``. ``openai.Timeout`` is always the SDK's
|
|
127
|
+
own type, so convert instead of depending on which httpx the SDK uses.
|
|
128
|
+
"""
|
|
129
|
+
import openai
|
|
130
|
+
if timeout is None or isinstance(timeout, (int, float, openai.Timeout)):
|
|
131
|
+
return timeout
|
|
132
|
+
return openai.Timeout(connect=timeout.connect, read=timeout.read, write=timeout.write, pool=timeout.pool)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def client_kwargs(timeout: httpx.Timeout, erp_user: Optional[str] = None) -> dict:
|
|
136
|
+
"""Keyword arguments for ``OpenAI(...)`` with the configured provider."""
|
|
137
|
+
settings = llm_settings()
|
|
138
|
+
if settings.provider == "azure_foundry" and not settings.base_url:
|
|
139
|
+
raise RuntimeError("AZURE_FOUNDRY_BASE_URL is not configured")
|
|
140
|
+
headers = {}
|
|
141
|
+
if settings.provider == "lambda" and erp_user:
|
|
142
|
+
headers[ERP_USER_HEADER] = str(erp_user)[:100]
|
|
143
|
+
kwargs = {"api_key": settings.api_key, "timeout": _client_timeout(timeout)}
|
|
144
|
+
if settings.base_url:
|
|
145
|
+
kwargs["base_url"] = settings.base_url
|
|
146
|
+
if headers:
|
|
147
|
+
kwargs["default_headers"] = headers
|
|
148
|
+
return kwargs
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def openai_client(timeout: httpx.Timeout, erp_user: Optional[str] = None) -> OpenAI:
|
|
152
|
+
"""An OpenAI client for the configured provider."""
|
|
153
|
+
return OpenAI(**client_kwargs(timeout, erp_user))
|
|
@@ -95,7 +95,7 @@ class RenderedPDF:
|
|
|
95
95
|
# closing text "Gültig bis {{ valid_till }}". Plain substitution, not Jinja:
|
|
96
96
|
# unknown names stay as written.
|
|
97
97
|
TEXT_PLACEHOLDERS = ("name", "customer_name", "party_name", "company", "transaction_date", "posting_date",
|
|
98
|
-
"valid_till", "due_date", "delivery_date", "schedule_date", "currency")
|
|
98
|
+
"bill_no", "bill_date", "valid_till", "due_date", "delivery_date", "schedule_date", "currency")
|
|
99
99
|
_PLACEHOLDER = re.compile(r"\{\{\s*(" + "|".join(TEXT_PLACEHOLDERS) + r")\s*\}\}")
|
|
100
100
|
|
|
101
101
|
|
|
@@ -289,10 +289,16 @@ def _render_commercial_pdf(doctype, name, doc, contract, layout=None, blocked=No
|
|
|
289
289
|
meta_fields.append({"label": "Date", "value": doc["transaction_date"]})
|
|
290
290
|
if doc.get("due_date"):
|
|
291
291
|
meta_fields.append({"label": "Due Date", "value": doc["due_date"]})
|
|
292
|
+
if doc.get("bill_no"):
|
|
293
|
+
meta_fields.append({"label": "Supplier Invoice No.", "value": doc["bill_no"]})
|
|
294
|
+
if doc.get("bill_date"):
|
|
295
|
+
meta_fields.append({"label": "Supplier Invoice Date", "value": doc["bill_date"]})
|
|
292
296
|
if doc.get("valid_till"):
|
|
293
297
|
meta_fields.append({"label": "Valid Till", "value": doc["valid_till"]})
|
|
294
298
|
if doc.get("delivery_date"):
|
|
295
299
|
meta_fields.append({"label": "Delivery Date", "value": doc["delivery_date"]})
|
|
300
|
+
elif doc.get("schedule_date"):
|
|
301
|
+
meta_fields.append({"label": "Delivery Date", "value": doc["schedule_date"]})
|
|
296
302
|
if doc.get("return_against"):
|
|
297
303
|
meta_fields.append({"label": "Return Against", "value": doc["return_against"]})
|
|
298
304
|
if doc.get("status"):
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"""Pricing data for external LLM APIs the chat orchestrator uses.
|
|
2
2
|
|
|
3
|
-
Mirrors the shape of `lambda-web/backend/providers.py`. Lists the OpenAI
|
|
4
|
-
|
|
5
|
-
see docs/llm-pricing.md for sources and billing scope.
|
|
3
|
+
Mirrors the shape of `lambda-web/backend/providers.py`. Lists the OpenAI
|
|
4
|
+
Standard/Global rates as well as verified Azure AI Foundry EU and US Data Zone
|
|
5
|
+
rates (USD per 1M tokens); see docs/llm-pricing.md for sources and billing scope.
|
|
6
6
|
|
|
7
7
|
Consumers import `cost_of_openai_call` to turn an SDK `response.usage` object
|
|
8
8
|
into a USD amount. Both Chat Completions and Responses usage shapes are
|
|
@@ -15,7 +15,7 @@ from typing import Any, Optional
|
|
|
15
15
|
|
|
16
16
|
|
|
17
17
|
# ---------------------------------------------------------------------------
|
|
18
|
-
# Pricing dicts
|
|
18
|
+
# Pricing dicts (USD per 1M tokens)
|
|
19
19
|
# ---------------------------------------------------------------------------
|
|
20
20
|
|
|
21
21
|
OPENAI_PRICING: dict[str, dict[str, Any]] = {
|
|
@@ -69,6 +69,100 @@ OPENAI_PRICING: dict[str, dict[str, Any]] = {
|
|
|
69
69
|
},
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
+
# Verified live from Azure Retail Prices API
|
|
73
|
+
# (https://prices.azure.com/api/retail/prices, serviceName eq 'Foundry Models',
|
|
74
|
+
# productName eq 'Azure OpenAI GPT6', Std DZ) on 2026-12-02.
|
|
75
|
+
# EU Data Zone (switzerlandnorth):
|
|
76
|
+
AZURE_FOUNDRY_EU_PRICING: dict[str, dict[str, Any]] = {
|
|
77
|
+
"gpt-6.2-astra": {
|
|
78
|
+
"tiered": True,
|
|
79
|
+
"tiers": [
|
|
80
|
+
{"max_input_tokens": 272_000, "input": 9.60, "cached_input": 0.48, "cache_write": 12.00, "cache_write_12h": 24.00, "output": 48.00},
|
|
81
|
+
{"max_input_tokens": None, "input": 19.20, "cached_input": 0.96, "cache_write": 24.00, "cache_write_12h": 48.00, "output": 72.00},
|
|
82
|
+
],
|
|
83
|
+
},
|
|
84
|
+
"gpt-6.2-sol": {
|
|
85
|
+
"tiered": True,
|
|
86
|
+
"tiers": [
|
|
87
|
+
{"max_input_tokens": 272_000, "input": 2.40, "cached_input": 0.12, "cache_write": 3.00, "cache_write_12h": 6.00, "output": 12.00},
|
|
88
|
+
{"max_input_tokens": None, "input": 4.80, "cached_input": 0.24, "cache_write": 6.00, "cache_write_12h": 12.00, "output": 18.00},
|
|
89
|
+
],
|
|
90
|
+
},
|
|
91
|
+
"gpt-6-astra": {
|
|
92
|
+
"tiered": True,
|
|
93
|
+
"tiers": [
|
|
94
|
+
{"max_input_tokens": 272_000, "input": 12.00, "cached_input": 1.20, "cache_write": 15.00, "output": 60.00},
|
|
95
|
+
{"max_input_tokens": None, "input": 24.00, "cached_input": 2.40, "cache_write": 30.00, "output": 90.00},
|
|
96
|
+
],
|
|
97
|
+
},
|
|
98
|
+
"gpt-6.1-sol": {
|
|
99
|
+
"tiered": True,
|
|
100
|
+
"tiers": [
|
|
101
|
+
{"max_input_tokens": 272_000, "input": 2.40, "cached_input": 0.12, "cache_write": 3.00, "output": 12.00},
|
|
102
|
+
{"max_input_tokens": None, "input": 4.80, "cached_input": 0.24, "cache_write": 6.00, "output": 18.00},
|
|
103
|
+
],
|
|
104
|
+
},
|
|
105
|
+
"gpt-6-luna": {
|
|
106
|
+
"tiered": True,
|
|
107
|
+
"tiers": [
|
|
108
|
+
{"max_input_tokens": 272_000, "input": 0.12, "cached_input": 0.012, "cache_write": 0.15, "output": 0.60},
|
|
109
|
+
{"max_input_tokens": None, "input": 0.24, "cached_input": 0.024, "cache_write": 0.30, "output": 0.90},
|
|
110
|
+
],
|
|
111
|
+
},
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
# US Data Zone (eastus2), verified 2026-12-02:
|
|
115
|
+
AZURE_FOUNDRY_US_PRICING: dict[str, dict[str, Any]] = {
|
|
116
|
+
"gpt-6.2-astra": {
|
|
117
|
+
"tiered": True,
|
|
118
|
+
"tiers": [
|
|
119
|
+
{"max_input_tokens": 272_000, "input": 8.80, "cached_input": 0.44, "cache_write": 11.00, "cache_write_12h": 22.00, "output": 44.00},
|
|
120
|
+
{"max_input_tokens": None, "input": 17.60, "cached_input": 0.88, "cache_write": 22.00, "cache_write_12h": 44.00, "output": 66.00},
|
|
121
|
+
],
|
|
122
|
+
},
|
|
123
|
+
"gpt-6.2-sol": {
|
|
124
|
+
"tiered": True,
|
|
125
|
+
"tiers": [
|
|
126
|
+
{"max_input_tokens": 272_000, "input": 2.20, "cached_input": 0.11, "cache_write": 2.75, "cache_write_12h": 5.50, "output": 11.00},
|
|
127
|
+
{"max_input_tokens": None, "input": 4.40, "cached_input": 0.22, "cache_write": 5.50, "cache_write_12h": 11.00, "output": 16.50},
|
|
128
|
+
],
|
|
129
|
+
},
|
|
130
|
+
"gpt-6-astra": {
|
|
131
|
+
"tiered": True,
|
|
132
|
+
"tiers": [
|
|
133
|
+
{"max_input_tokens": 272_000, "input": 11.00, "cached_input": 1.10, "cache_write": 13.75, "output": 55.00},
|
|
134
|
+
{"max_input_tokens": None, "input": 22.00, "cached_input": 2.20, "cache_write": 27.50, "output": 82.50},
|
|
135
|
+
],
|
|
136
|
+
},
|
|
137
|
+
"gpt-6.1-sol": {
|
|
138
|
+
"tiered": True,
|
|
139
|
+
"tiers": [
|
|
140
|
+
{"max_input_tokens": 272_000, "input": 2.20, "cached_input": 0.11, "cache_write": 2.75, "output": 11.00},
|
|
141
|
+
{"max_input_tokens": None, "input": 4.40, "cached_input": 0.22, "cache_write": 5.50, "output": 16.50},
|
|
142
|
+
],
|
|
143
|
+
},
|
|
144
|
+
"gpt-6.1-terra": {
|
|
145
|
+
"tiered": True,
|
|
146
|
+
"tiers": [
|
|
147
|
+
{"max_input_tokens": 272_000, "input": 1.10, "cached_input": 0.055, "cache_write": 1.375, "output": 5.50},
|
|
148
|
+
{"max_input_tokens": None, "input": 2.20, "cached_input": 0.11, "cache_write": 2.75, "output": 8.25},
|
|
149
|
+
],
|
|
150
|
+
},
|
|
151
|
+
"gpt-6-luna": {
|
|
152
|
+
"tiered": True,
|
|
153
|
+
"tiers": [
|
|
154
|
+
{"max_input_tokens": 272_000, "input": 0.11, "cached_input": 0.011, "cache_write": 0.1375, "output": 0.55},
|
|
155
|
+
{"max_input_tokens": None, "input": 0.22, "cached_input": 0.022, "cache_write": 0.275, "output": 0.825},
|
|
156
|
+
],
|
|
157
|
+
},
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
PRICING_TABLES: dict[str, dict[str, dict[str, Any]]] = {
|
|
161
|
+
"openai": OPENAI_PRICING,
|
|
162
|
+
"azure_foundry_eu": AZURE_FOUNDRY_EU_PRICING,
|
|
163
|
+
"azure_foundry_us": AZURE_FOUNDRY_US_PRICING,
|
|
164
|
+
}
|
|
165
|
+
|
|
72
166
|
# Speech-to-text, priced per minute of audio (not per token).
|
|
73
167
|
TRANSCRIBE_PRICING: dict[str, dict[str, float]] = {
|
|
74
168
|
"gpt-transcribe": {"per_minute": 0.0045},
|
|
@@ -79,14 +173,31 @@ TRANSCRIBE_PRICING: dict[str, dict[str, float]] = {
|
|
|
79
173
|
|
|
80
174
|
|
|
81
175
|
# ---------------------------------------------------------------------------
|
|
82
|
-
# Rate lookup — unknown model IDs are estimated at the gpt-6.1-sol rates
|
|
83
|
-
# Register a model explicitly before using
|
|
176
|
+
# Rate lookup — unknown model IDs are estimated at the gpt-6.1-sol rates
|
|
177
|
+
# within the selected pricing table. Register a model explicitly before using
|
|
178
|
+
# it; this fallback is not a price cap.
|
|
84
179
|
# ---------------------------------------------------------------------------
|
|
85
180
|
|
|
86
|
-
def
|
|
87
|
-
|
|
181
|
+
def _resolve_pricing_table(pricing_table: Optional[str]) -> dict[str, dict[str, Any]]:
|
|
182
|
+
if pricing_table is None:
|
|
183
|
+
from api.llm_provider import llm_settings
|
|
184
|
+
try:
|
|
185
|
+
pricing_table = llm_settings().pricing_table
|
|
186
|
+
except RuntimeError:
|
|
187
|
+
pricing_table = "openai"
|
|
188
|
+
return PRICING_TABLES.get(pricing_table, OPENAI_PRICING)
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def get_openai_rates(
|
|
192
|
+
model: str,
|
|
193
|
+
input_tokens: int = 0,
|
|
194
|
+
*,
|
|
195
|
+
pricing_table: Optional[str] = None,
|
|
196
|
+
) -> dict[str, float]:
|
|
197
|
+
table = _resolve_pricing_table(pricing_table)
|
|
198
|
+
entry = table.get(model)
|
|
88
199
|
if entry is None:
|
|
89
|
-
entry =
|
|
200
|
+
entry = table["gpt-6.1-sol"]
|
|
90
201
|
if entry.get("tiered"):
|
|
91
202
|
for tier in entry["tiers"]:
|
|
92
203
|
cap = tier.get("max_input_tokens")
|
|
@@ -101,11 +212,17 @@ def get_openai_rates(model: str, input_tokens: int = 0) -> dict[str, float]:
|
|
|
101
212
|
# and degrade to 0 so callers don't need to pre-check.
|
|
102
213
|
# ---------------------------------------------------------------------------
|
|
103
214
|
|
|
104
|
-
def cost_of_openai_call(
|
|
105
|
-
|
|
215
|
+
def cost_of_openai_call(
|
|
216
|
+
model: str,
|
|
217
|
+
usage: Optional[Any],
|
|
218
|
+
*,
|
|
219
|
+
pricing_table: Optional[str] = None,
|
|
220
|
+
) -> float:
|
|
221
|
+
"""USD cost of one OpenAI-compatible Chat Completions or Responses API call.
|
|
106
222
|
|
|
107
223
|
Understands either endpoint's cached-token breakdown so cache hits are
|
|
108
|
-
billed at the discounted rate.
|
|
224
|
+
billed at the discounted rate. Uses the active provider's pricing table
|
|
225
|
+
when ``pricing_table`` is not passed."""
|
|
109
226
|
if usage is None:
|
|
110
227
|
return 0.0
|
|
111
228
|
prompt_tokens = int(
|
|
@@ -127,7 +244,7 @@ def cost_of_openai_call(model: str, usage: Optional[Any]) -> float:
|
|
|
127
244
|
if details is not None:
|
|
128
245
|
cached = int(getattr(details, "cached_tokens", 0) or 0)
|
|
129
246
|
|
|
130
|
-
rates = get_openai_rates(model, input_tokens=prompt_tokens)
|
|
247
|
+
rates = get_openai_rates(model, input_tokens=prompt_tokens, pricing_table=pricing_table)
|
|
131
248
|
uncached = max(prompt_tokens - cached, 0)
|
|
132
249
|
cached_rate = rates.get("cached_input", rates["input"])
|
|
133
250
|
total = (
|
|
@@ -136,6 +253,8 @@ def cost_of_openai_call(model: str, usage: Optional[Any]) -> float:
|
|
|
136
253
|
+ completion_tokens * rates["output"]
|
|
137
254
|
)
|
|
138
255
|
return total / 1_000_000
|
|
256
|
+
|
|
257
|
+
|
|
139
258
|
def cost_of_transcription(model: str, audio_seconds: float) -> float:
|
|
140
259
|
"""USD cost of one speech-to-text call, billed per minute of audio.
|
|
141
260
|
|
|
@@ -71,6 +71,9 @@ class PurchaseInvoice(Document):
|
|
|
71
71
|
if not self.posting_date:
|
|
72
72
|
self.posting_date = nowdate()
|
|
73
73
|
|
|
74
|
+
self._normalize_bill_fields()
|
|
75
|
+
self._validate_duplicate_bill_no()
|
|
76
|
+
|
|
74
77
|
apply_external_source_defaults(self)
|
|
75
78
|
from lambda_erp.workflow import apply_amount_only
|
|
76
79
|
apply_amount_only(self)
|
|
@@ -97,7 +100,46 @@ class PurchaseInvoice(Document):
|
|
|
97
100
|
self._data["outstanding_amount"] = flt(self.grand_total, 2)
|
|
98
101
|
|
|
99
102
|
if not self.due_date:
|
|
100
|
-
self._data["due_date"] = add_days(self.posting_date, 30)
|
|
103
|
+
self._data["due_date"] = add_days(self.bill_date or self.posting_date, 30)
|
|
104
|
+
|
|
105
|
+
def _normalize_bill_fields(self):
|
|
106
|
+
"""Trim surrounding whitespace on supplier bill_no / bill_date; empty becomes None."""
|
|
107
|
+
if "bill_no" in self._data and self._data["bill_no"] is not None:
|
|
108
|
+
trimmed = str(self._data["bill_no"]).strip()
|
|
109
|
+
self._data["bill_no"] = trimmed or None
|
|
110
|
+
if "bill_date" in self._data and self._data["bill_date"] is not None:
|
|
111
|
+
trimmed = str(self._data["bill_date"]).strip()
|
|
112
|
+
self._data["bill_date"] = trimmed or None
|
|
113
|
+
|
|
114
|
+
def _validate_duplicate_bill_no(self):
|
|
115
|
+
"""Reject duplicate supplier invoice numbers per (company, supplier)
|
|
116
|
+
against already-submitted non-return Purchase Invoices. Returns (Debit
|
|
117
|
+
Notes) are exempt so a supplier credit note can reference the original
|
|
118
|
+
bill or carry its own number."""
|
|
119
|
+
if flt(self.get("is_return")):
|
|
120
|
+
return
|
|
121
|
+
bill_no = str(self._data.get("bill_no") or "").strip()
|
|
122
|
+
if not bill_no or not self.supplier or not self.company:
|
|
123
|
+
return
|
|
124
|
+
db = get_db()
|
|
125
|
+
if "bill_no" not in db._get_table_columns(self.DOCTYPE):
|
|
126
|
+
return
|
|
127
|
+
rows = db.sql(
|
|
128
|
+
'SELECT name FROM "Purchase Invoice" '
|
|
129
|
+
'WHERE company = ? AND supplier = ? '
|
|
130
|
+
' AND docstatus = 1 '
|
|
131
|
+
' AND COALESCE(discarded, 0) = 0 '
|
|
132
|
+
' AND COALESCE(is_return, 0) = 0 '
|
|
133
|
+
' AND name != ? '
|
|
134
|
+
" AND LOWER(TRIM(COALESCE(bill_no, ''))) = LOWER(?) "
|
|
135
|
+
'LIMIT 1',
|
|
136
|
+
[self.company, self.supplier, self.name or "", bill_no],
|
|
137
|
+
)
|
|
138
|
+
if rows:
|
|
139
|
+
raise ValidationError(
|
|
140
|
+
f"Duplicate supplier invoice number '{bill_no}' for supplier {self.supplier}: "
|
|
141
|
+
f"already recorded on Purchase Invoice {rows[0]['name']}"
|
|
142
|
+
)
|
|
101
143
|
|
|
102
144
|
def _set_supplier_name(self):
|
|
103
145
|
if not self.supplier_name and self.supplier:
|
|
@@ -310,6 +352,9 @@ class PurchaseInvoice(Document):
|
|
|
310
352
|
routed to Stock In Hand in _set_missing_accounts so the existing
|
|
311
353
|
GL loop naturally books Dr Stock In Hand for those lines.
|
|
312
354
|
"""
|
|
355
|
+
self._normalize_bill_fields()
|
|
356
|
+
self._validate_duplicate_bill_no()
|
|
357
|
+
|
|
313
358
|
if flt(self.get("update_stock")):
|
|
314
359
|
sl_entries = self._get_stock_sl_entries()
|
|
315
360
|
if sl_entries:
|
|
@@ -918,6 +918,8 @@ class Database:
|
|
|
918
918
|
supplier TEXT,
|
|
919
919
|
supplier_name TEXT,
|
|
920
920
|
posting_date TEXT,
|
|
921
|
+
bill_no TEXT,
|
|
922
|
+
bill_date TEXT,
|
|
921
923
|
due_date TEXT,
|
|
922
924
|
company TEXT,
|
|
923
925
|
currency TEXT DEFAULT 'USD',
|
|
@@ -3942,6 +3944,18 @@ def _m074_customer_notes(db: "Database") -> None:
|
|
|
3942
3944
|
db.ensure_column("Sales Invoice", "customer_note", "TEXT")
|
|
3943
3945
|
|
|
3944
3946
|
|
|
3947
|
+
def _m075_purchase_invoice_bill_no(db: "Database") -> None:
|
|
3948
|
+
"""Supplier invoice number and date on Purchase Invoice (`bill_no`,
|
|
3949
|
+
`bill_date`), plus an index for per-supplier duplicate checks and search."""
|
|
3950
|
+
db.ensure_column("Purchase Invoice", "bill_no", "TEXT")
|
|
3951
|
+
db.ensure_column("Purchase Invoice", "bill_date", "TEXT")
|
|
3952
|
+
db.conn.execute(db._ddl(
|
|
3953
|
+
'CREATE INDEX IF NOT EXISTS "ix_purchase_invoice_bill_no" '
|
|
3954
|
+
'ON "Purchase Invoice" (company, supplier, bill_no)'
|
|
3955
|
+
))
|
|
3956
|
+
db.conn.commit()
|
|
3957
|
+
|
|
3958
|
+
|
|
3945
3959
|
Database.MIGRATIONS = [
|
|
3946
3960
|
(1, "chat_message_session_id", _m001_chat_message_session_id),
|
|
3947
3961
|
(2, "chat_session_user_id", _m002_chat_session_user_id),
|
|
@@ -4017,6 +4031,7 @@ Database.MIGRATIONS = [
|
|
|
4017
4031
|
(72, "mail_log", _m072_mail_log),
|
|
4018
4032
|
(73, "customer_terms", _m073_customer_terms),
|
|
4019
4033
|
(74, "customer_notes", _m074_customer_notes),
|
|
4034
|
+
(75, "purchase_invoice_bill_no", _m075_purchase_invoice_bill_no),
|
|
4020
4035
|
]
|
|
4021
4036
|
|
|
4022
4037
|
|