DjangoERP architecture¶
How DjangoERP is put together: the module map and dependency direction, the invariants that hold it together, the GL posting matrix, the MRP algorithm, and which open-source designs were adopted and why.
DjangoERP is built outward from a double-entry accounting core into a modular ERP with end-to-end order-to-cash, procure-to-pay and make-to-stock processing plus a bare-minimum-but-credible MRP planning engine. This document maps the modules, the invariants that hold the system together, the GL posting matrix, the planning algorithm, and the open-source systems whose designs were deliberately adopted.
Module map and dependency direction¶
Modules depend downward only (Tryton's decomposition discipline): master data knows nothing about documents, documents know nothing about planning, and the ledgers (journal, inventory) are written through services, never directly.
flowchart TD
simulation -. services only .-> mrp & sales & purchasing & manufacturing & invoices & journal
workspace -. public API + read models .-> mrp & sales & purchasing & manufacturing & invoices & journal & inventory
mrp --> sales & purchasing & manufacturing
manufacturing --> inventory & products
purchasing --> partners & products & inventory
sales --> partners & products & inventory & invoices
invoices --> partners & journal & coa
inventory --> products
products --> coa
partners --> coa
purchasing & sales & manufacturing --> journal
journal --> coa
subgraph financial core
coa
journal
invoices
reports
end
reports --> journal & coa
| App | Owns | Depends on |
|---|---|---|
core |
TimestampMixin, DocumentSequence (atomic numbering), core.audit (audit-trail writer), core.exceptions (the API's 409 for a refused delete), core.viewsets (DELETE removes drafts only), core.demo (demo company), demo_erp command |
accounts (audit model) |
partners |
BusinessPartner — one row can be customer and/or supplier, carries AR/AP control accounts |
coa |
products |
UnitOfMeasure, ProductCategory (GL account mappings), Product (buy/make, costs, lead time, safety stock, lot sizing) |
coa, partners |
inventory |
Warehouse, StockMove (append-only stock ledger), StockLevel (cached on-hand + moving average) |
products |
purchasing |
PurchaseOrder/PurchaseOrderLine, receipt flow |
partners, products, inventory, journal |
sales |
SalesOrder/SalesOrderLine, shipment and invoicing flow |
partners, products, inventory, invoices, journal |
manufacturing |
BillOfMaterials/BOMLine, WorkOrder, completion flow |
products, inventory, journal |
mrp |
MRPRun, PlannedOrder, the planning engine and plan-to-order conversion |
everything above |
simulation |
The agent sandbox: Scenario, ProductionLine, Agent, AgentDecision, ProductionJob, SupplierPayable, TickSnapshot, Experiment; the tick engine, agent brains, what-ifs and the /console/ client |
everything above, through services only |
workspace |
No models. The /app/ shell and its registry (introspected from every viewset and serializer, with a per-role access matrix from the permission classes), and read-only document-flow, search, overview and statement read models under /api/workspace/ |
every app's viewsets and models, read-only; writes go through the other apps' API |
The invariants¶
The two ledgers follow the same law: only posted rows exist. Journal entries count only when status='posted'; stock moves count only when status='done'. Both are append-only — corrections are reversing entries and opposing moves, never edits. The API enforces this: posted journal entries reject every change except voiding, completed stock moves reject all changes, posted invoices and payments reject edits. DELETE removes drafts only: a posted or voided entry, a posted invoice or payment, a done move and a confirmed or later order answer 400 (core.viewsets.ProtectedHistoryDestroyMixin), and pre_delete guards refuse the same ledger deletes through the ORM or the admin, so the general ledger never loses an entry its source documents point to.
A manual entry is corrected with POST /api/journal/entries/{id}/reverse/ (journal.services.reverse_entry): a posted mirror entry with every debit and credit swapped, dated the day of the correction (or the date given), linked to the original by reverses/reversed_by and audit-logged as journal.reverse. The original stays posted, so each period keeps the history it reported, and the pair nets every account to zero. Only posted manual entries qualify: an entry a business document posted is corrected through that document, and neither half of a reversal pair can be voided.
The journal's line rules live in one place. journal.services.validate_lines (non-negative amounts quantized to the cent, a debit or a credit per line but never both, a non-zero debit and credit, equal totals) runs on the API path and inside post_entry, and JournalLine carries matching check constraints (debit >= 0, credit >= 0, not both) for any write that goes around them. Manual and system entries are numbered from the same JE sequence, so a client can never take a number a later posting needs. JournalLine.account is PROTECT: an account with ledger history cannot be deleted, only retired with is_active=false, and the trial balance and general ledger keep reporting a retired account's posted balance. Any delete a PROTECT foreign key refuses answers 409, not 500.
Cached balances are derived, provably. StockLevel is the cache of SUM(done moves) per product/warehouse, maintained under select_for_update row locks in the same transaction as the move, and inventory.services.rebuild_stock_levels() reconstructs it from the ledger (pinned by test).
Documents are state machines. Every transition — confirm, receive, ship, complete, post, convert, cancel — is a named service function that validates preconditions, does all its work in one transaction, and writes the audit trail. Views and serializers never assign status directly.
Posting is idempotent. JournalEntry.source_reference is unique; every system posting is keyed to its business event (grn:GRN-00001, shipment:SHP-00001, wo:WO-00001:complete, invoice:INV-00001, payment:PAY-00001, and reversal:<id> for a reversing entry), so a retried flow returns the existing entry instead of double-posting.
Stock is valued at moving average, per product per warehouse (ERPNext's formula): receipts re-weight average_cost = (old_qty*old_avg + receipt_value) / new_qty; issues leave at the current average and can never drive on-hand negative. Every completed move snapshots unit_cost and the 2dp total_value its GL posting used, so the stock ledger and the general ledger cannot disagree.
Decimal everywhere: quantities are Decimal(12,3), unit costs/prices Decimal(12,4), monetary document totals 2dp, and the journal keeps its wider 20,2 lines. GL amounts are quantized to the cent before posting.
Document lifecycles¶
stateDiagram-v2
direction LR
state "Purchase order" as PO {
[*] --> draft
draft --> confirmed: confirm()
confirmed --> partially_received: receive() partial
partially_received --> received: receive() rest
confirmed --> received: receive()
draft --> cancelled: cancel()
confirmed --> cancelled: cancel() if nothing received
}
Sales orders mirror this (draft -> confirmed -> partially_shipped -> shipped -> invoiced), work orders run draft -> confirmed -> in_progress -> completed, and MRP runs follow the async report pattern (pending -> running -> completed | failed). Planned orders promote planned -> converted (frePPLe's proposal ladder) or are cancelled.
API permission matrix¶
Roles live in UserRole and are checked by the classes in accounts/permissions.py; an account with no UserRole row (a bare superuser included) is denied by every class below.
| Area | Read (GET) | Write (POST, PUT, PATCH, DELETE, posting actions) |
|---|---|---|
| Journal entries, stock moves, orders, MRP | accountant | accountant |
| Journal lines, invoices, payments | accountant, auditor | accountant |
| Company, saved reports | any role (saved reports: own, or all for accountants) | accountant |
| Report templates | any authenticated user | accountant |
| Master data (partners, products, warehouses, BOMs, chart of accounts) | any authenticated user | any authenticated user |
admin implies accountant and auditor. core/test_permissions.py walks the ledger, invoice, payment, company and saved-report routes through every role.
GL posting matrix¶
Account resolution is explicit configuration, never guessed: product categories carry inventory_account, revenue_account, cogs_account; partners carry receivable_account / payable_account; payments carry a deposit_account; posting raises a configuration error naming the missing account.
| Business event | Debit | Credit | Source |
|---|---|---|---|
| Goods receipt (GRN) | Inventory (per product category), at PO price | Supplier payable (accrual) | purchasing.services.receive_purchase_order |
| Work order completion | Inventory of finished good, at rolled-up component value plus any absorbed conversion cost | Inventory of components, at moving average; each absorption account for its conversion_costs share |
manufacturing.services.complete_work_order |
| Shipment | COGS (per category), at moving average | Inventory (per category) | sales.services.ship_sales_order |
| Customer invoice posted | Partner receivable | Revenue (per line product category, or invoice fallback account) | invoices.services.post_invoice |
| Customer payment posted | Deposit (cash/bank) account | Partner receivable | invoices.services.post_payment |
| Supplier settlement | Supplier payable | Cash | manual journal entry (vendor-bill matching is roadmap) |
| Reversal of a manual entry | The original's credits | The original's debits | journal.services.reverse_entry |
Revenue recognition is deliberately split from shipment: shipping posts COGS only, and revenue posts when the invoice posts, keeping the two events separately auditable.
The MRP engine¶
mrp.engine implements classic regenerative, level-by-level (Orlicky) MRP I — the textbook algorithm none of the surveyed systems implements quite this directly:
- Low-level codes are computed over the active-BOM graph (components sit strictly below every parent), so each product is netted exactly once, after all demand on it is known.
- Demand: open confirmed sales-order lines (due at
requested_date), component requirements of open work orders, safety-stock top-ups, and — during the run — component demand exploded from planned make orders at their release dates. - Supply: on-hand stock, open confirmed purchase-order line quantities, open work-order quantities.
- Netting allocates available supply to demand in due-date order; each shortfall becomes a
PlannedOrdersized by the product's lot policy (lot-for-lot, floored tomin_order_qty, rounded up toorder_multiple). - Lead-time offsetting: release date = due date −
lead_time_days; releases landing before the run'sas_of_dateare clamped and flaggedexpedited. - Pegging: every planned order records the demand element it covers (
demand_reference) and, for exploded requirements, the parent planned order — a where-used trail from a raw-material buy back to the customer order. - Conversion (a separate, deliberate action) groups buy proposals into one draft purchase order per supplier and creates one draft work order per make proposal. Drafts are invisible to the next run until confirmed — planning proposes, people commit.
Capacity is assumed infinite (no routings/work centers), buckets are continuous dates, and the plan is regenerative per run. Those are the standard MRP I simplifications; the upgrade paths are listed in the roadmap.
End-to-end demonstration¶
python manage.py demo_erp runs the whole loop through the same services the API uses and fails non-zero if the books do not balance:
sequenceDiagram
participant SO as Sales order
participant MRP as MRP run
participant PO as Purchase order
participant WO as Work order
participant ST as Stock ledger
participant GL as General ledger
SO->>MRP: confirmed demand (5 WIDGET)
MRP->>PO: planned buys, grouped per supplier
MRP->>WO: planned make, pegged to SO
PO->>ST: receive components
PO->>GL: Dr Inventory / Cr AP accrual
WO->>ST: consume components, receive FG at rolled-up cost
WO->>GL: Dr FG inventory / Cr component inventory
SO->>ST: ship at moving average
SO->>GL: Dr COGS / Cr Inventory
SO->>GL: invoice: Dr AR / Cr Revenue, payment: Dr Cash / Cr AR
The same flow is pinned with exact numbers by core/tests.py::EndToEndERPFlowTests, which ends by asserting the trial balance foots, the balance sheet balances with current-period earnings folded into equity, net income equals revenue minus COGS, the audit trail recorded every transition, and a second regenerative MRP run plans nothing because the first cycle satisfied all demand.
Open-source designs adopted, and licensing¶
Per the consolidation research (source-verified against each project), these patterns were adopted — as re-implemented designs, not copied code. ERPNext (GPL-3), Odoo (LGPL-3), Tryton (GPL-3) and frePPLe (AGPL-3) are copyleft: ideas, schemas, formulas and state charts are fair game, their code is not, and none was copied. InvenTree (MIT) would be safe to copy from; its patterns were used as the floor reference.
| Adopted here | Pattern source |
|---|---|
| Append-only signed stock ledger + cached per-warehouse level | ERPNext Stock Ledger Entry + Bin; Odoo stock.move + stock.quant split |
| Moving-average formula per product/warehouse | ERPNext stock_ledger.py (re-derived) |
Document numbering via a sequence table (DocumentSequence) |
ERPNext naming series; django-sequences (BSD) is the drop-in library alternative if gapless multi-process numbering ever needs hardening |
| One partner with customer/supplier flags | Odoo/Tryton party model |
Line-level fulfillment counters (received_quantity, shipped_quantity) |
Odoo qty_received / qty_delivered |
| Named transition services as the only status writers | Tryton @Workflow.transition discipline |
| GL account defaults on product category and partner | ERPNext item-group / party defaults |
| Planned-order promotion ladder and expedite flags | frePPLe operationplan lifecycle |
| Supplier-grouped conversion of planned buys | Odoo vendor merge |
Lot sizing (min_order_qty + order_multiple) |
Odoo orderpoint rounding, frePPLe size_minimum/multiple |
| No negative stock, no backdated repost | ERPNext's largest complexity (its repost subsystem), removed by constraint |
Library survey (all MIT/BSD/Apache, safe here): django-sequences (numbering), django-fsm-2 (transition sugar), django-simple-history / django-auditlog (row history). The core keeps zero new runtime dependencies — the audit requirement is met by wiring the existing accounts.AuditLog through core.audit.log_event from every service transition, and transitions are enforced by plain guarded functions.
The agent simulation sandbox¶
simulation drives the whole ERP with agents over simulated time (ADR 0003, simulation/README.md). Each tick runs the agents in business-day order — market, supplier prices, maintenance, customers, sales manager, buyer (safety stock, MRP at the simulated date, conversion of due planned orders, priced purchase orders), supplier deliveries, planner (finite-capacity jobs on production lines), line crews and machines, shipping and billing, plant manager, controller — and every write is a call to the services above, made as the agent's own user and wrapped in a savepoint so a refused action is recorded instead of failing the tick. The tick is one transaction: the controller compiles the trial balance and balance sheet and reconciles the stock, receivables, payables and fixed-asset subledgers with the GL, and an unbalanced tick rolls back and halts the scenario. It only runs where SIMULATION_SANDBOX is on.
Agents revise their policies at a cadence through a brain: a seeded heuristic, or a small Claude model whose structured-output answers are clamped to each field's bounds and fall back to the heuristic on any failure, with each call's cost posted to 6300 AI agent compute. Conversion costs are absorbed into finished goods at a normal-capacity rate per line through complete_work_order(conversion_costs=...) (ADR 0002), so idle capacity surfaces as under-absorption against 5390. What-if experiments run a baseline and a variant from the current state on common random numbers inside transactions that always roll back.
| Sandbox event | Debit | Credit | Source |
|---|---|---|---|
| Period costs | Direct and indirect labor, variable overhead, maintenance, management, facility, AI compute, interest | Cash | simulation.engine controller via post_entry |
| Depreciation | Depreciation 5330 | Accumulated depreciation 1590 | controller via post_entry |
| Supplier payment | Accounts payable | Cash | controller via post_entry (keyed sim:<id>:pay:<GRN>) |
| Revolver draw / repayment | Cash / Revolver 2400 | Revolver 2400 / Cash | controller via post_entry |
| Line capex / retirement | Machinery 1500 / accumulated depreciation + loss on disposal 6500 | Cash / Machinery 1500 | simulation.world.commission_line / decommission_line |
The workspace shell¶
workspace is the front end for every module (ADR 0004, workspace/README.md). It owns no data and no rules: registry.py describes each viewset's fields, filters, actions and endpoints by introspecting the serializers and the router, and decides which roles can read or write each resource by running the viewset's own permission classes, so the screens and the API cannot disagree about access. Lists, forms and actions call the public endpoints; transitions are the documents' @actions, so every posting still goes through the services above. The one thing it adds on the server is read models that cross documents: a document's flow (the chain from source to settlement, built from audit events, source_reference prefixes and foreign keys), its journal entries and stock moves, its audit trail, global search, the home page's work queues and the statements, each section filtered by what the requesting role could read through the owning viewset.
Deliberate exclusions (roadmap)¶
Labor and overhead now reach product cost through complete_work_order(conversion_costs=...) (ADR 0002), with the rate supplied by the caller; the sandbox supplies one per production line. Each exclusion names the reference system that shows the upgrade path: routings/work centers and finite capacity in the core (frePPLe; the sandbox's ProductionLine is a simulation-only stand-in), WIP staging warehouse and job cards (ERPNext), vendor-bill three-way match against the GRN accrual (classic GR/IR), stock reservations (Odoo reserved_quantity), UoM conversion rings (ERPNext conversion_factor idiom), product variants (all systems — their biggest accidental complexity, deliberately absent), lot/serial tracking, multi-currency, backdated valuation repost, BOM component snapshots on work orders, credit notes, and per-location bins. The EDGAR/XBRL module remains a later consolidation pass per the README roadmap.