Skip to content

Calculate totals and other computed fields

Calculate each order line and the order total using field recipes. This is the default for values from the same record or loaded relations.

Complete example: acme_order_with_positions

Purpose (translated from upstream README)

Orders. An order links a customer, embedded positions and recipe totals. The reverse table in the customer detail filters orders by customer.id. Positions have no independent identity or permissions. Field recipes calculate during ordinary saves.
Adapt (translated from upstream README)
Rename all acme_* module, workflow, event and resource identifiers together; update references, layout paths, filters, translation keys and tests. Add fields to the schema, layouts and both languages. Copy files relative to the system root. Add native modules from fixtures/ only when missing; merge existing definitions. Menu entries and preview data are optional. Check variants separately instead of applying them alongside the main definition. Check with php bakery plan <system>, php bakery apply <system>, then php bakery system test-run <system>. In the API repository, AcmeFeatureExamplesTest runs the same plan/apply steps and workflow tests in a disposable PHPUnit system database. These checks do not verify UI rendering or client-only actions. Extend positions with additional child fields; adapt quantity, unit_price, line_total and total together. The test runner currently does not calculate field recipes for create_resource, unlike REST. Cases therefore exercise recipes through the optional recalculation workflow (action/save). This workflow and its tests can be omitted in the target system; forms do not need it.

Files relative to the system root:

acme_order_with_positions
├── README.md
├── menu.bake.json
├── modules/
│ ├── acme_order_customers/
│ │ ├── acme_order_customers.detail.json
│ │ ├── acme_order_customers.index.json
│ │ └── acme_order_customers.module.bake.json
│ └── acme_orders/
│ ├── acme_orders.detail.json
│ ├── acme_orders.index.json
│ └── acme_orders.module.bake.json
├── preview/
│ └── sample.bake.json
├── tests/
│ ├── acme_order_empty.test.workflow.json
│ └── acme_order_totals.test.workflow.json
├── translations/
│ ├── de.json
│ ├── en.json
│ └── translations.bake.json
└── workflows/
└── acme_recalculate_order.workflow.bake.json
README.md
README.md
# acme_order_with_positions
Aufträge / Orders. Auftrag mit verknüpftem Kunden, eingebetteten Positionen und Recipe-Summen. Die Rückrichtung im Kundendetail filtert Aufträge auf customer.id. Positionen haben keine eigene Identität oder Rechte. Feld-Recipes rechnen beim normalen Speichervorgang.
## Dateien
- `modules/`: Schema sowie Detail- und Index-Layouts.
- `menu.bake.json`: Navigation (in ein vorhandenes Menü übernehmen).
- `translations/`: deutsche/englische Labels und Bakery-Ressource.
- `workflows/`: optionaler, rollenbeschränkter Neuberechnungs-Webhook.
- `tests/`: Business-Tests mit Fixtures und `action/save` zur Recipe-Berechnung.
- `preview/`: optionale Bakery-Beispieldaten; nur in einer Vorschau anwenden.
## Anpassen
Alle `acme_*`-Modul-, Workflow-, Event- und Ressourcen-Identifier gemeinsam umbenennen; Referenzen, Layoutpfade, Filter, Übersetzungsschlüssel und Tests mitändern. Weitere Felder in Schema, Layout und beiden Sprachen ergänzen. Dateien relativ zur Systemwurzel kopieren. Native Module aus `fixtures/` nur ergänzen, wenn sie fehlen; bestehende Definitionen zusammenführen. Menüeinträge und Vorschaudaten sind optional. Varianten separat prüfen, nicht zusätzlich zur Hauptdefinition anwenden.
Prüfen: `php bakery plan <system>`, `php bakery apply <system>`, dann `php bakery system test-run <system>`. In diesem Repository führt `AcmeFeatureExamplesTest` dieselben Plan-/Apply-Schritte und die Workflow-Tests in der wegwerfbaren PHPUnit-Systemdatenbank aus. UI-Darstellung und reine Client-Aktionen sind damit nicht geprüft.
Positionen um weitere Unterfelder ergänzen; `quantity`, `unit_price`, `line_total` und `total` zusammen anpassen.
Der Test-Runner berechnet beim Trigger `create_resource` derzeit keine Feld-Recipes (anders als REST). Deshalb testen die Fälle die Recipes über den optionalen Neuberechnungs-Workflow (`action/save`). Dieser und die Tests dürfen im Zielsystem entfallen; Formulare brauchen ihn nicht.
menu.bake.json
menu.bake.json
[
{
"resource_menu": "acme_order_with_positions",
"resource": {
"name": "ACME Inc.",
"elements": [
"acme_order_customers",
"acme_orders"
]
}
}
]
modules/acme_order_customers/acme_order_customers.detail.json
modules/acme_order_customers/acme_order_customers.detail.json
{
"tabs": [
{
"identifier": "general",
"rows": [
{
"cols": [
{
"span": 24,
"components": [
{
"type": "field_group",
"identifier": "general",
"options": {
"fields": [
"name",
"email"
]
}
},
{
"type": "resource_table",
"identifier": "customer_orders",
"options": {
"module": "acme_orders",
"pre_filters": [
[
{
"column": "customer.id",
"operator": "=",
"field": "id"
}
]
]
}
}
]
}
]
}
]
}
]
}
modules/acme_order_customers/acme_order_customers.index.json
modules/acme_order_customers/acme_order_customers.index.json
{
"tabs": [
{
"identifier": "general",
"rows": [
{
"cols": [
{
"span": 24,
"components": [
{
"type": "resource_table",
"identifier": "records",
"options": {
"module": "acme_order_customers"
}
}
]
}
]
}
]
}
]
}
modules/acme_order_customers/acme_order_customers.module.bake.json
modules/acme_order_customers/acme_order_customers.module.bake.json
[
{
"resource_module": "acme_order_customers",
"resource": {
"identifier": "acme_order_customers",
"title": "name",
"fields": [
{
"identifier": "name",
"type": "text"
},
{
"identifier": "email",
"type": "email"
}
],
"layouts": {
"detail": "/modules/acme_order_customers/acme_order_customers.detail",
"index": "/modules/acme_order_customers/acme_order_customers.index"
}
}
}
]
modules/acme_orders/acme_orders.detail.json
modules/acme_orders/acme_orders.detail.json
{
"tabs": [
{
"identifier": "general",
"rows": [
{
"cols": [
{
"span": 24,
"components": [
{
"type": "field_group",
"identifier": "general",
"options": {
"fields": [
"name",
"customer",
"positions",
"total"
]
}
}
]
}
]
}
]
}
]
}
modules/acme_orders/acme_orders.index.json
modules/acme_orders/acme_orders.index.json
{
"tabs": [
{
"identifier": "general",
"rows": [
{
"cols": [
{
"span": 24,
"components": [
{
"type": "resource_table",
"identifier": "records",
"options": {
"module": "acme_orders"
}
}
]
}
]
}
]
}
]
}
modules/acme_orders/acme_orders.module.bake.json
modules/acme_orders/acme_orders.module.bake.json
[
{
"resource_module": "acme_orders",
"resource": {
"identifier": "acme_orders",
"title": "name",
"fields": [
{
"identifier": "name",
"type": "text"
},
{
"identifier": "customer",
"type": "select",
"options": {
"references": "acme_order_customers"
}
},
{
"identifier": "positions",
"type": "list",
"options": {
"fields": [
{
"identifier": "description",
"type": "text"
},
{
"identifier": "quantity",
"type": "number"
},
{
"identifier": "unit_price",
"type": "currency"
},
{
"identifier": "line_total",
"type": "currency",
"recipe": "quantity * unit_price"
}
]
}
},
{
"identifier": "total",
"type": "currency",
"recipe": "sum(positions[*].line_total)"
}
],
"layouts": {
"detail": "/modules/acme_orders/acme_orders.detail",
"index": "/modules/acme_orders/acme_orders.index"
}
}
}
]
preview/sample.bake.json
preview/sample.bake.json
[
{
"resource_entity": "acme_order_sample_customer",
"resource": {
"module": "acme_order_customers",
"fields": {
"name": "ACME Inc.",
"email": "customer@example.invalid"
}
}
},
{
"resource_entity": "acme_order_sample",
"depends_on": "resource_entity.acme_order_sample_customer",
"resource": {
"module": "acme_orders",
"fields": {
"name": "ACME Order",
"customer": "${resource_entity.acme_order_sample_customer}",
"positions": [
{
"description": "Service",
"quantity": 2,
"unit_price": 25
},
{
"description": "Delivery",
"quantity": 1,
"unit_price": 10
}
]
}
}
}
]
tests/acme_order_empty.test.workflow.json
tests/acme_order_empty.test.workflow.json
{
"identifier": "acme_order_empty",
"fixtures": [
{
"type": "entity",
"module": "acme_orders",
"alias": "order",
"fields": {
"name": "Empty order",
"positions": []
}
}
],
"trigger": {
"type": "dispatch_sync",
"entry": "acme_recalculate_order",
"data": {
"prototype": "fixture:order"
}
},
"assertions": [
{
"type": "field_equals",
"module": "acme_orders",
"field": "total",
"value": "0.00",
"selector": {
"name": "Empty order"
}
}
],
"workflow": "acme_recalculate_order"
}
tests/acme_order_totals.test.workflow.json
tests/acme_order_totals.test.workflow.json
{
"identifier": "acme_order_totals",
"fixtures": [
{
"type": "entity",
"module": "acme_order_customers",
"alias": "customer",
"fields": {
"name": "ACME Inc.",
"email": "customer@example.invalid"
}
},
{
"type": "entity",
"module": "acme_orders",
"alias": "order",
"fields": {
"name": "ACME Test Order",
"customer": "fixture:customer",
"positions": [
{
"description": "Service",
"quantity": 2,
"unit_price": 25
},
{
"description": "Delivery",
"quantity": 1,
"unit_price": 10
}
]
}
}
],
"trigger": {
"type": "dispatch_sync",
"entry": "acme_recalculate_order",
"data": {
"prototype": "fixture:order"
}
},
"assertions": [
{
"type": "field_equals",
"module": "acme_orders",
"field": "total",
"value": "60.00",
"selector": {
"name": "ACME Test Order"
}
},
{
"type": "entity_exists",
"module": "acme_orders",
"where": {
"name": "ACME Test Order",
"customer.id": "fixture:customer.id"
}
}
],
"workflow": "acme_recalculate_order"
}
translations/de.json
translations/de.json
{
"modules": {
"acme_order_customers": {
"title": "Kunden",
"fields": {
"name": "Name",
"email": "E-Mail"
},
"tabs": {
"general": "Allgemein"
}
},
"acme_orders": {
"title": "Aufträge",
"fields": {
"name": "Name",
"customer": "Kunde",
"positions": "Positionen",
"total": "Gesamtsumme",
"description": "Beschreibung",
"quantity": "Menge",
"unit_price": "Einzelpreis",
"line_total": "Positionssumme"
},
"tabs": {
"general": "Allgemein"
}
}
}
}
translations/en.json
translations/en.json
{
"modules": {
"acme_order_customers": {
"title": "Customers",
"fields": {
"name": "Name",
"email": "Email"
},
"tabs": {
"general": "General"
}
},
"acme_orders": {
"title": "Orders",
"fields": {
"name": "Name",
"customer": "Customer",
"positions": "Positions",
"total": "Total",
"description": "Description",
"quantity": "Quantity",
"unit_price": "Unit price",
"line_total": "Line total"
},
"tabs": {
"general": "General"
}
}
}
}
translations/translations.bake.json
translations/translations.bake.json
[
{
"resource_translations": "de",
"resource": {
"identifier": "de",
"code": "de_DE",
"name": "Deutsch",
"dir": "ltr",
"provider": "deDE",
"data": "${file('translations/de.json')}"
}
},
{
"resource_translations": "en",
"resource": {
"identifier": "en",
"code": "en_US",
"name": "English",
"dir": "ltr",
"provider": "enUS",
"data": "${file('translations/en.json')}"
}
}
]
workflows/acme_recalculate_order.workflow.bake.json
workflows/acme_recalculate_order.workflow.bake.json
[
{
"resource_workflow": "acme_recalculate_order",
"resource": {
"identifier": "acme_recalculate_order",
"title": "ACME Inc. — acme_recalculate_order",
"active": true,
"async": false,
"entry": "acme_recalculate_order",
"elements": [
{
"name": "acme_recalculate_order",
"type": "event/webhook",
"options": {
"module": "acme_orders",
"public": false,
"roles": [
"admin"
]
},
"set": {
"order": "prototype:"
},
"to": {
"default": {
"save": [
"default"
]
}
}
},
{
"name": "save",
"type": "action/save",
"options": {
"triggerEvents": false
},
"in": {
"default": "$order"
},
"to": {}
}
],
"events": [
{
"identifier": "acme_recalculate_order",
"type": "webhook",
"module": "acme_orders"
}
]
}
}
]

Source: brezel/api@572da72238ad1aab7b60287c898eaede2c08a876

Agents: system_authoring_command reference kind=feature query=acme_order_with_positions

The order uses an embedded list with quantity, unit price and calculated line totals. The parent recipe calculates the total. Top-level recipe controls the value and locks the input; options.recipes controls presentation options. Recipes recalculate on ordinary create/update saves and live in the client where the functions are available.

Copy the example files relative to your system root. Merge existing native modules, menus and roles; rename all related identifiers together. Apply preview data only in a disposable preview. Run the plan, apply and tests against that preview:

Terminal window
php bakery plan <system>
php bakery apply <system>
php bakery system test-run <system>

acme_order_totals and acme_order_empty cover populated and empty orders. They save through the optional recalculation webhook because test-runner create_resource does not calculate recipes. Ordinary forms do not require that button. Confirm live totals in the browser.

  • Recommended for calculations across records: create/update workflow → op/set → action/save; avoid event loops on the follow-up save.
  • Recommended for an explicit recalculation action: action/recalculate; verify persistence in your workflow rather than assuming every use saves.
  • Recommended for display only: option recipes or table aggregates; neither replaces a stored business value.
  • Not recommended: changing values in beforeSave during ordinary UI/REST writes; those changes are not persisted.

Field options · Recipes by runtime · Recalculate · Workflow tests · Assertions