metacore Start a pilot

Showcase · ERP

An ERP, as one domain.

A ledger in double entry, with what a company buys, sells, makes, plans and pays its people. A delivery issues the units, states the invoice and posts its entry in one step, and a month closes when the controller and finance both sign off.

The domain, as its schema declares it

16 entities and 29 layers, 12 roles and 16 atoms, in one file.

Model

Each entity, split by who changes it.

A layer is a group of fields that change together: it is the unit of a permission, of an approval and of a release. Its scope is the lowest level at which a value may differ.

EntityLayerScopeKindFields
accountmainglobalReleasedname, kind
postingcompanyReleasedopen, closed_through
balancecompanyBalancesamount
journal entrypostedcompanyStated onceday, period, description, origin, reverses
journal linepostedcompanyStated onceaccount, debit, credit, cost_center
invoiceissuedcompanyStated oncedirection, partner, order, day, due_on, total, currency
settlementcompanyReleasedstatus, paid_on
suppliermainglobalReleasedname, vat_number, country, status
termscompanyReleasedpayment_days, currency
bankglobalReleased, second personiban, bank_name
customermainglobalReleasedname, vat_number, country, status
termscompanyReleasedpayment_days, currency, credit_limit
itemmainglobalReleasedname, unit, supply
costcompanyDatedstandard_cost
stockwarehouseBalanceson_hand, allocated, issued, consumed, received, produced
purchase orderplacedcompanyStated oncesupplier, day, total, currency, buyer
progresscompanyReleasedstatus, expected_on, received_on
purchase lineplacedcompanyStated onceitem, quantity, unit_price
sales orderplacedcompanyStated oncecustomer, day, total, currency, seller
progresscompanyReleasedstatus, promised_on, delivered_on
sales lineplacedcompanyStated onceitem, quantity, unit_price
componentmainglobalReleasedpart, quantity
work orderplacedcompanyStated onceitem, quantity, warehouse, due_on
progresscompanyReleasedstatus, finished_on
planned orderproposalcompanyReleaseditem, quantity, needed_on, action
decisioncompanyReleasedstatus
employeemainglobalReleasedname, hired_on, cost_center
paycompanyDated, second persongross_salary
payslipissuedcompanyStated onceemployee, period, gross, withheld, net

The entities and their layers

A released layer publishes and rolls back. A dated one holds values by period, one stated once never changes, balances add up, and an overlay is written live by another system.

Roles

A role holds what a job needs, and no more.

A role names operations on layers. Where a person works is in the grant: one role serves every scope value, and a grant gives it on one.

RoleReadsChangesDecides, starts and calls
buyersupplier, item, purchase order, purchase line, planned ordersupplier.main, supplier.terms, item.mainCalls order.
approversupplier, purchase order, purchase lineNothingApproves purchase_order.placed.
sellercustomer, item, sales order, sales line, invoicecustomer.mainCalls sell.
warehouseitem, purchase order, purchase line, sales order, sales lineNothingCalls receive and deliver.
planneritem, component, planned order, work order, purchase order, sales ordercomponent.main, planned_order.decisionCalls release.
productionitem, component, work orderNothingCalls produce.
accountantaccount, journal entry, journal line, invoice, supplier, customer, purchase order, sales orderinvoice.settlementCalls post. Starts closing.
controlleraccount, journal entry, journal line, invoiceaccount.main, account.postingRolls back account.posting. Decides the stage sign_off of closing.
financeaccount, journal entry, journal line, invoice, customer, item, purchase order, sales orderitem.cost, customer.termsApproves sales_order.placed. Decides the stage sign_off of closing.
treasurersupplier, invoicesupplier.bankApproves supplier.bank. Rolls back supplier.bank.
payrollemployee, payslipemployee.main, employee.payApproves employee.pay. Rolls back employee.pay. Calls pay_month.
auditoraccount, journal entry, journal line, invoice, supplier, customer, item, purchase order, purchase line, sales order, sales line, component, work order, planned order, employee, payslipNothingNothing

Who does what

The engine checks each operation for every caller: a person, a system or an AI agent.

Flow

People decide, and your code acts.

The schema holds no formula and no condition. Whatever computes is an atom: your code, which the engine calls outside itself or in a sandbox.

AtomKindWhat it does
period_lockGate ruleJudges each change of journal_entry.posted before it is live: pass, reject, or hold for a person.
spend_limitGate ruleJudges each change of purchase_order.placed before it is live: pass, reject, or hold for a person.
credit_limitGate ruleJudges each change of sales_order.placed before it is live: pass, reject, or hold for a person.
postHandlerChanges journal_entry.posted, journal_line.posted, account.balance and invoice.issued. Emits entry_posted. A caller with the grant calls it.
orderHandlerChanges purchase_order.placed and purchase_line.placed. Emits order_placed. A caller with the grant calls it.
send_orderHandlerChanges purchase_order.progress. Asks mail through the engine.
receiveHandlerChanges item.stock and purchase_order.progress. Emits goods_received. A caller with the grant calls it.
sellHandlerChanges sales_order.placed, sales_line.placed, item.stock and sales_order.progress. A caller with the grant calls it.
deliverHandlerChanges item.stock, sales_order.progress, invoice.issued, journal_entry.posted, journal_line.posted and account.balance. Emits goods_delivered and entry_posted. A caller with the grant calls it.
settleHandlerChanges journal_entry.posted, journal_line.posted, account.balance and invoice.settlement. Emits entry_posted.
planHandlerChanges planned_order.proposal and planned_order.decision.
releaseHandlerChanges work_order.placed and work_order.progress. A caller with the grant calls it.
produceHandlerChanges item.stock and work_order.progress. Emits work_finished. A caller with the grant calls it.
pay_monthHandlerChanges payslip.issued, journal_entry.posted, journal_line.posted and account.balance. Emits entry_posted. A caller with the grant calls it.
trial_balanceHandlerGives balanced or open_items.
lock_periodHandlerChanges account.posting.

What computes

An atom returns its effects as data, and the engine checks each one.

  1. SoldA seller calls one procedure: the order, its lines and the allocated units, in one step
  2. JudgedA gate rule holds an order above the credit of its customer
  3. PlannedEvery night a handler proposes what to buy and what to make
  4. MadeA work order takes its components from the stock, and adds what it made
  5. DeliveredThe units leave, and the invoice and its entry post in one step
  6. PaidThe bank tells through a webhook, and the payment posts
  7. ClosedThe controller and finance sign off, and the period locks

An order, step by step

Each step is a run with its own audit entry.

Connections

What comes in, and what goes out.

A third party reaches the domain through a webhook, and a handler reaches a third party through the engine. A schema holds no address and no secret: an admin binds each one in each environment.

NameKindHow
bankWebhookThe third party signs each notification. It starts settle.
after_orderTriggerAfter the event order_placed: it starts send_order.
nightly_planTriggerOn a schedule, 0 2 * * * in UTC: it starts plan.
entry_postedEventA handler emits it for a journal entry. Other domains and the event stream follow it.
order_placedEventA handler emits it for a purchase order. A trigger of the domain follows it.
goods_receivedEventA handler emits it for a purchase order. Other domains and the event stream follow it.
goods_deliveredEventA handler emits it for a sales order. Other domains and the event stream follow it.
work_finishedEventA handler emits it for a work order. Other domains and the event stream follow it.
mailThird partyA handler asks it through the engine, with a secret that your org keeps.

The connections of the domain

Every system meets the engine at one public API.

Boundaries

What the engine gives, and what you build.

metacore is not a product of this kind. It is the engine under one that you build: the guarantees come with it, and the rest is yours.

From the engineWhat you get
A public APITyped from the schema, for every entity. There is no private interface.
A back officeEvery record at every level, from the schema, with no code.
Access controlA role holds an operation, on a layer, at a scope value.
Approvals and releasesA change waits where a person must approve it, publishes in one step, and rolls back in one step.
An audit trailAn entry is written in the same step as the operation it records.
Search, events and MCPBuilt in, from the same schema.

With no code

The same for every domain.

Yours to buildHow it meets the engine
The schemaThis file. It is the whole model, and it changes only by deployment.
The atoms16 in this domain, in your repository and in your language.
The rulesYour tax rules, your pay rules and your planning rules are atoms. The schema holds no formula.
The reports of the lawYour statements, your tax returns and your filings. They read the ledger through the public API.
The bankYour bank moves the money. It tells through a webhook when a payment settled.

In your code

A product of this kind brings its own screens. Here they are yours.

Schema

The whole domain, in one file.

Every table of this page is read from this file. The check of the CLI finds no problem in it.

erp.txtpb
# proto-file: metacore/api/v1/schema.proto
# proto-message: metacore.api.v1.Schema

# The core of an ERP, as one domain: the ledger of a group of companies, its
# suppliers and its customers, its items with their stock, and what it buys,
# sells, makes, plans and pays to its people. A message of
# metacore.api.v1.Schema, in the text format of Protocol Buffers.
#
#   account         main        global     what the account is
#                   posting     company    whether a company posts to it
#                   balance     company    its money: a balance that entries add to
#   journal_entry   posted      company    an entry of the journal: stated once
#   journal_line    posted      company    a line of an entry: stated once
#   invoice         issued      company    an invoice, sent or received: stated once
#                   settlement  company    whether it is paid
#   supplier        main        global     who the supplier is
#                   terms       company    how each company pays it
#                   bank        global     where it is paid: a second person decides
#   customer        main        global     who the customer is
#                   terms       company    how it pays, and its credit
#   item            main        global     what the item is
#                   cost        company    its standard cost, by period
#                   stock       warehouse  its units: balances that many runs add to
#   purchase_order  placed      company    the order as it was placed: stated once
#                   progress    company    where the order stands
#   purchase_line   placed      company    a line of a purchase: stated once
#   sales_order     placed      company    the order as it was taken: stated once
#                   progress    company    where the order stands
#   sales_line      placed      company    a line of a sale: stated once
#   component       main        global     a line of a bill of materials
#   work_order      placed      company    what to make: stated once
#                   progress    company    where the work stands
#   planned_order   proposal    company    what the plan proposes to buy or to make
#                   decision    company    what the planner decides of it
#   employee        main        global     who the person is
#                   pay         company    the salary, by period: a second person decides
#   payslip         issued      company    the pay of a month: stated once
#
# The money of the accounts is conserved, as double entry asks: a change set
# adds to some accounts what it takes from others, and its amounts sum to
# zero. The units of an item are conserved too: they move between
# warehouses, or between on_hand, allocated, issued and consumed. What a
# supplier delivers comes from received, and what the plant makes from
# produced, the two balances with no least.
#
# A procedure states a document and adds to the balances in one change set:
# a delivery issues the units, states the invoice and posts its entry
# together, so the stock, the invoices and the ledger never differ. Gate
# rules reject an entry in a period that is closed, hold a purchase above the
# limit of its buyer, and hold a sale above the credit of its customer. The
# bank tells through a webhook when a payment settled, a plan runs every
# night, and a month closes through a workflow, where the controller and
# finance both sign off.
#
# The org declares the scopes before the first deployment: company under
# global, and warehouse under company.

entities {
  name: "account"
  key { name: "code" type: FIELD_TYPE_TEXT }
  layers {
    name: "main"
    scope: "global"
    fields { name: "name" type: FIELD_TYPE_TEXT required: true min_length: 2 max_length: 120 exposed: true }
    fields { name: "kind" type: FIELD_TYPE_TEXT required: true allowed_values: "asset" allowed_values: "liability" allowed_values: "equity" allowed_values: "revenue" allowed_values: "expense" }
  }
  # The chart of accounts is one for the group. Each company says whether it
  # posts to an account, and until when.
  layers {
    name: "posting"
    scope: "company"
    fields { name: "open" type: FIELD_TYPE_BOOLEAN }
    fields { name: "closed_through" type: FIELD_TYPE_TEXT pattern: "[0-9]{4}-[0-9]{2}" }
  }
  # A debit adds to the balance and a credit takes from it. The amounts of
  # one change set sum to zero across the accounts.
  layers {
    name: "balance"
    scope: "company"
    balances: true
    conserved {}
    fields { name: "amount" type: FIELD_TYPE_DECIMAL decimal_places: 2 }
  }
}

# An entry never changes once it is posted: a correction is an entry of its
# own, which names the one that it reverses.
entities {
  name: "journal_entry"
  key { name: "number" type: FIELD_TYPE_TEXT }
  layers {
    name: "posted"
    scope: "company"
    once: true
    fields { name: "day" type: FIELD_TYPE_DATE required: true }
    fields { name: "period" type: FIELD_TYPE_TEXT required: true pattern: "[0-9]{4}-[0-9]{2}" }
    fields { name: "description" type: FIELD_TYPE_TEXT max_length: 200 }
    fields { name: "origin" type: FIELD_TYPE_TEXT allowed_values: "manual" allowed_values: "purchase" allowed_values: "sale" allowed_values: "bank" allowed_values: "payroll" allowed_values: "production" }
    fields { name: "reverses" type: FIELD_TYPE_TEXT max_length: 40 }
  }
}

entities {
  name: "journal_line"
  key { name: "entry" type: FIELD_TYPE_TEXT }
  key { name: "position" type: FIELD_TYPE_INTEGER }
  layers {
    name: "posted"
    scope: "company"
    once: true
    fields { name: "account" type: FIELD_TYPE_REFERENCE required: true reference { entity: "account" carries: "name" } }
    fields { name: "debit" type: FIELD_TYPE_DECIMAL decimal_places: 2 least { decimal: "0" } }
    fields { name: "credit" type: FIELD_TYPE_DECIMAL decimal_places: 2 least { decimal: "0" } }
    fields { name: "cost_center" type: FIELD_TYPE_TEXT max_length: 40 }
  }
}

# An invoice that the company sent, or one that it received. It never
# changes once it is issued: a credit note is an invoice of its own.
entities {
  name: "invoice"
  key { name: "number" type: FIELD_TYPE_TEXT }
  layers {
    name: "issued"
    scope: "company"
    once: true
    fields { name: "direction" type: FIELD_TYPE_TEXT required: true allowed_values: "sent" allowed_values: "received" }
    fields { name: "partner" type: FIELD_TYPE_TEXT required: true max_length: 40 }
    fields { name: "order" type: FIELD_TYPE_TEXT max_length: 40 }
    fields { name: "day" type: FIELD_TYPE_DATE required: true }
    fields { name: "due_on" type: FIELD_TYPE_DATE }
    fields { name: "total" type: FIELD_TYPE_DECIMAL decimal_places: 2 least { decimal: "0" } }
    fields { name: "currency" type: FIELD_TYPE_TEXT allowed_values: "EUR" allowed_values: "USD" allowed_values: "GBP" }
  }
  layers {
    name: "settlement"
    scope: "company"
    fields { name: "status" type: FIELD_TYPE_TEXT allowed_values: "open" allowed_values: "paid" allowed_values: "disputed" }
    fields { name: "paid_on" type: FIELD_TYPE_DATE }
  }
}

entities {
  name: "supplier"
  key { name: "code" type: FIELD_TYPE_TEXT }
  layers {
    name: "main"
    scope: "global"
    fields { name: "name" type: FIELD_TYPE_TEXT required: true max_length: 160 exposed: true }
    fields { name: "vat_number" type: FIELD_TYPE_TEXT max_length: 20 }
    fields { name: "country" type: FIELD_TYPE_TEXT pattern: "[A-Z]{2}" }
    fields { name: "status" type: FIELD_TYPE_TEXT allowed_values: "active" allowed_values: "blocked" }
    uniques { name: "supplier_vat" fields: "vat_number" scope: "global" ignore_case: true }
  }
  layers {
    name: "terms"
    scope: "company"
    fields { name: "payment_days" type: FIELD_TYPE_INTEGER least { integer: 0 } greatest { integer: 365 } }
    fields { name: "currency" type: FIELD_TYPE_TEXT allowed_values: "EUR" allowed_values: "USD" allowed_values: "GBP" }
  }
  # Where a supplier is paid never changes by one person alone.
  layers {
    name: "bank"
    scope: "global"
    second_person: true
    hold_timeout { seconds: 259200 }
    fields { name: "iban" type: FIELD_TYPE_TEXT pattern: "[A-Z]{2}[0-9A-Z]{13,32}" restricted { read_roles: "treasurer" write_roles: "treasurer" } }
    fields { name: "bank_name" type: FIELD_TYPE_TEXT max_length: 120 }
  }
}

entities {
  name: "customer"
  key { name: "code" type: FIELD_TYPE_TEXT }
  layers {
    name: "main"
    scope: "global"
    fields { name: "name" type: FIELD_TYPE_TEXT required: true max_length: 160 exposed: true }
    fields { name: "vat_number" type: FIELD_TYPE_TEXT max_length: 20 }
    fields { name: "country" type: FIELD_TYPE_TEXT pattern: "[A-Z]{2}" }
    fields { name: "status" type: FIELD_TYPE_TEXT allowed_values: "active" allowed_values: "blocked" }
    uniques { name: "customer_vat" fields: "vat_number" scope: "global" ignore_case: true }
  }
  layers {
    name: "terms"
    scope: "company"
    fields { name: "payment_days" type: FIELD_TYPE_INTEGER least { integer: 0 } greatest { integer: 365 } }
    fields { name: "currency" type: FIELD_TYPE_TEXT allowed_values: "EUR" allowed_values: "USD" allowed_values: "GBP" }
    fields { name: "credit_limit" type: FIELD_TYPE_DECIMAL decimal_places: 2 least { decimal: "0" } }
  }
}

entities {
  name: "item"
  key { name: "sku" type: FIELD_TYPE_TEXT }
  layers {
    name: "main"
    scope: "global"
    fields { name: "name" type: FIELD_TYPE_TEXT required: true min_length: 2 max_length: 120 exposed: true }
    fields { name: "unit" type: FIELD_TYPE_TEXT allowed_values: "piece" allowed_values: "kilogram" allowed_values: "litre" allowed_values: "metre" allowed_values: "hour" }
    fields { name: "supply" type: FIELD_TYPE_TEXT allowed_values: "bought" allowed_values: "made" allowed_values: "service" }
  }
  # A standard cost holds over a period that its change names: the cost of
  # a year, or a revision from a day.
  layers {
    name: "cost"
    scope: "company"
    dated: true
    fields {
      name: "standard_cost"
      type: FIELD_TYPE_DECIMAL
      decimal_places: 4
      least { decimal: "0" }
      restricted { read_roles: "finance" write_roles: "finance" }
    }
  }
  layers {
    name: "stock"
    scope: "warehouse"
    balances: true
    conserved { key: "sku" }
    fields { name: "on_hand" type: FIELD_TYPE_INTEGER least { integer: 0 } }
    fields { name: "allocated" type: FIELD_TYPE_INTEGER least { integer: 0 } }
    fields { name: "issued" type: FIELD_TYPE_INTEGER least { integer: 0 } }
    fields { name: "consumed" type: FIELD_TYPE_INTEGER least { integer: 0 } }
    fields { name: "received" type: FIELD_TYPE_INTEGER }
    fields { name: "produced" type: FIELD_TYPE_INTEGER }
  }
}

entities {
  name: "purchase_order"
  key { name: "number" type: FIELD_TYPE_TEXT }
  layers {
    name: "placed"
    scope: "company"
    once: true
    fields { name: "supplier" type: FIELD_TYPE_REFERENCE required: true reference { entity: "supplier" carries: "name" } }
    fields { name: "day" type: FIELD_TYPE_DATE required: true }
    fields { name: "total" type: FIELD_TYPE_DECIMAL decimal_places: 2 least { decimal: "0" } }
    fields { name: "currency" type: FIELD_TYPE_TEXT allowed_values: "EUR" allowed_values: "USD" allowed_values: "GBP" }
    fields { name: "buyer" type: FIELD_TYPE_TEXT max_length: 120 }
  }
  layers {
    name: "progress"
    scope: "company"
    fields { name: "status" type: FIELD_TYPE_TEXT allowed_values: "sent" allowed_values: "confirmed" allowed_values: "received" allowed_values: "invoiced" allowed_values: "paid" }
    fields { name: "expected_on" type: FIELD_TYPE_DATE }
    fields { name: "received_on" type: FIELD_TYPE_DATE }
  }
}

entities {
  name: "purchase_line"
  key { name: "order" type: FIELD_TYPE_TEXT }
  key { name: "position" type: FIELD_TYPE_INTEGER }
  layers {
    name: "placed"
    scope: "company"
    once: true
    fields { name: "item" type: FIELD_TYPE_REFERENCE required: true reference { entity: "item" carries: "name" } }
    fields { name: "quantity" type: FIELD_TYPE_INTEGER least { integer: 1 } }
    fields { name: "unit_price" type: FIELD_TYPE_DECIMAL decimal_places: 4 least { decimal: "0" } }
  }
}

entities {
  name: "sales_order"
  key { name: "number" type: FIELD_TYPE_TEXT }
  layers {
    name: "placed"
    scope: "company"
    once: true
    fields { name: "customer" type: FIELD_TYPE_REFERENCE required: true reference { entity: "customer" carries: "name" } }
    fields { name: "day" type: FIELD_TYPE_DATE required: true }
    fields { name: "total" type: FIELD_TYPE_DECIMAL decimal_places: 2 least { decimal: "0" } }
    fields { name: "currency" type: FIELD_TYPE_TEXT allowed_values: "EUR" allowed_values: "USD" allowed_values: "GBP" }
    fields { name: "seller" type: FIELD_TYPE_TEXT max_length: 120 }
  }
  layers {
    name: "progress"
    scope: "company"
    fields { name: "status" type: FIELD_TYPE_TEXT allowed_values: "allocated" allowed_values: "delivered" allowed_values: "invoiced" allowed_values: "paid" }
    fields { name: "promised_on" type: FIELD_TYPE_DATE }
    fields { name: "delivered_on" type: FIELD_TYPE_DATE }
  }
}

entities {
  name: "sales_line"
  key { name: "order" type: FIELD_TYPE_TEXT }
  key { name: "position" type: FIELD_TYPE_INTEGER }
  layers {
    name: "placed"
    scope: "company"
    once: true
    fields { name: "item" type: FIELD_TYPE_REFERENCE required: true reference { entity: "item" carries: "name" } }
    fields { name: "quantity" type: FIELD_TYPE_INTEGER least { integer: 1 } }
    fields { name: "unit_price" type: FIELD_TYPE_DECIMAL decimal_places: 4 least { decimal: "0" } }
  }
}

# A bill of materials: the parts that one unit of an item takes.
entities {
  name: "component"
  key { name: "item" type: FIELD_TYPE_TEXT }
  key { name: "position" type: FIELD_TYPE_INTEGER }
  layers {
    name: "main"
    scope: "global"
    fields { name: "part" type: FIELD_TYPE_REFERENCE required: true reference { entity: "item" carries: "name" } }
    fields { name: "quantity" type: FIELD_TYPE_INTEGER least { integer: 1 } }
  }
}

entities {
  name: "work_order"
  key { name: "number" type: FIELD_TYPE_TEXT }
  layers {
    name: "placed"
    scope: "company"
    once: true
    fields { name: "item" type: FIELD_TYPE_REFERENCE required: true reference { entity: "item" carries: "name" } }
    fields { name: "quantity" type: FIELD_TYPE_INTEGER least { integer: 1 } }
    fields { name: "warehouse" type: FIELD_TYPE_TEXT max_length: 40 }
    fields { name: "due_on" type: FIELD_TYPE_DATE }
  }
  layers {
    name: "progress"
    scope: "company"
    fields { name: "status" type: FIELD_TYPE_TEXT allowed_values: "released" allowed_values: "started" allowed_values: "finished" }
    fields { name: "finished_on" type: FIELD_TYPE_DATE }
  }
}

# What the plan proposes, and what the planner decides of it: a handler
# states the first layer every night, and a person the second.
entities {
  name: "planned_order"
  key { name: "number" type: FIELD_TYPE_TEXT }
  layers {
    name: "proposal"
    scope: "company"
    fields { name: "item" type: FIELD_TYPE_REFERENCE required: true reference { entity: "item" carries: "name" } }
    fields { name: "quantity" type: FIELD_TYPE_INTEGER least { integer: 1 } }
    fields { name: "needed_on" type: FIELD_TYPE_DATE }
    fields { name: "action" type: FIELD_TYPE_TEXT allowed_values: "buy" allowed_values: "make" }
  }
  layers {
    name: "decision"
    scope: "company"
    fields { name: "status" type: FIELD_TYPE_TEXT allowed_values: "proposed" allowed_values: "firmed" allowed_values: "dropped" }
  }
}

entities {
  name: "employee"
  key { name: "number" type: FIELD_TYPE_TEXT }
  layers {
    name: "main"
    scope: "global"
    fields { name: "name" type: FIELD_TYPE_TEXT required: true max_length: 160 }
    fields { name: "hired_on" type: FIELD_TYPE_DATE }
    fields { name: "cost_center" type: FIELD_TYPE_TEXT max_length: 40 }
  }
  # A salary holds from a day, and never changes by one person alone.
  layers {
    name: "pay"
    scope: "company"
    dated: true
    second_person: true
    fields {
      name: "gross_salary"
      type: FIELD_TYPE_DECIMAL
      decimal_places: 2
      least { decimal: "0" }
      restricted { read_roles: "payroll" write_roles: "payroll" }
    }
  }
}

entities {
  name: "payslip"
  key { name: "number" type: FIELD_TYPE_TEXT }
  layers {
    name: "issued"
    scope: "company"
    once: true
    fields { name: "employee" type: FIELD_TYPE_REFERENCE required: true reference { entity: "employee" carries: "name" } }
    fields { name: "period" type: FIELD_TYPE_TEXT required: true pattern: "[0-9]{4}-[0-9]{2}" }
    fields { name: "gross" type: FIELD_TYPE_DECIMAL decimal_places: 2 least { decimal: "0" } restricted { read_roles: "payroll" write_roles: "payroll" } }
    fields { name: "withheld" type: FIELD_TYPE_DECIMAL decimal_places: 2 least { decimal: "0" } restricted { read_roles: "payroll" write_roles: "payroll" } }
    fields { name: "net" type: FIELD_TYPE_DECIMAL decimal_places: 2 least { decimal: "0" } restricted { read_roles: "payroll" write_roles: "payroll" } }
  }
}

roles {
  name: "buyer"
  permissions { operation: OPERATION_READ entity: "supplier" }
  permissions { operation: OPERATION_READ entity: "item" }
  permissions { operation: OPERATION_READ entity: "purchase_order" }
  permissions { operation: OPERATION_READ entity: "purchase_line" }
  permissions { operation: OPERATION_READ entity: "planned_order" }
  permissions { operation: OPERATION_CHANGE entity: "supplier" layer: "main" }
  permissions { operation: OPERATION_CHANGE entity: "supplier" layer: "terms" }
  permissions { operation: OPERATION_CHANGE entity: "item" layer: "main" }
  permissions { operation: OPERATION_CALL procedure: "order" }
}

roles {
  name: "approver"
  permissions { operation: OPERATION_READ entity: "supplier" }
  permissions { operation: OPERATION_READ entity: "purchase_order" }
  permissions { operation: OPERATION_READ entity: "purchase_line" }
  permissions { operation: OPERATION_DECIDE entity: "purchase_order" layer: "placed" }
}

roles {
  name: "seller"
  permissions { operation: OPERATION_READ entity: "customer" }
  permissions { operation: OPERATION_READ entity: "item" }
  permissions { operation: OPERATION_READ entity: "sales_order" }
  permissions { operation: OPERATION_READ entity: "sales_line" }
  permissions { operation: OPERATION_READ entity: "invoice" }
  permissions { operation: OPERATION_CHANGE entity: "customer" layer: "main" }
  permissions { operation: OPERATION_CALL procedure: "sell" }
}

roles {
  name: "warehouse"
  permissions { operation: OPERATION_READ entity: "item" }
  permissions { operation: OPERATION_READ entity: "purchase_order" }
  permissions { operation: OPERATION_READ entity: "purchase_line" }
  permissions { operation: OPERATION_READ entity: "sales_order" }
  permissions { operation: OPERATION_READ entity: "sales_line" }
  permissions { operation: OPERATION_CALL procedure: "receive" }
  permissions { operation: OPERATION_CALL procedure: "deliver" }
}

roles {
  name: "planner"
  permissions { operation: OPERATION_READ entity: "item" }
  permissions { operation: OPERATION_READ entity: "component" }
  permissions { operation: OPERATION_READ entity: "planned_order" }
  permissions { operation: OPERATION_READ entity: "work_order" }
  permissions { operation: OPERATION_READ entity: "purchase_order" }
  permissions { operation: OPERATION_READ entity: "sales_order" }
  permissions { operation: OPERATION_CHANGE entity: "component" layer: "main" }
  permissions { operation: OPERATION_CHANGE entity: "planned_order" layer: "decision" }
  permissions { operation: OPERATION_CALL procedure: "release" }
}

roles {
  name: "production"
  permissions { operation: OPERATION_READ entity: "item" }
  permissions { operation: OPERATION_READ entity: "component" }
  permissions { operation: OPERATION_READ entity: "work_order" }
  permissions { operation: OPERATION_CALL procedure: "produce" }
}

roles {
  name: "accountant"
  permissions { operation: OPERATION_READ entity: "account" }
  permissions { operation: OPERATION_READ entity: "journal_entry" }
  permissions { operation: OPERATION_READ entity: "journal_line" }
  permissions { operation: OPERATION_READ entity: "invoice" }
  permissions { operation: OPERATION_READ entity: "supplier" }
  permissions { operation: OPERATION_READ entity: "customer" }
  permissions { operation: OPERATION_READ entity: "purchase_order" }
  permissions { operation: OPERATION_READ entity: "sales_order" }
  permissions { operation: OPERATION_CHANGE entity: "invoice" layer: "settlement" }
  permissions { operation: OPERATION_CALL procedure: "post" }
  permissions { operation: OPERATION_START workflow: "closing" }
}

roles {
  name: "controller"
  permissions { operation: OPERATION_READ entity: "account" }
  permissions { operation: OPERATION_READ entity: "journal_entry" }
  permissions { operation: OPERATION_READ entity: "journal_line" }
  permissions { operation: OPERATION_READ entity: "invoice" }
  permissions { operation: OPERATION_CHANGE entity: "account" layer: "main" }
  permissions { operation: OPERATION_CHANGE entity: "account" layer: "posting" }
  permissions { operation: OPERATION_GO_BACK entity: "account" layer: "posting" }
}

roles {
  name: "finance"
  permissions { operation: OPERATION_READ entity: "account" }
  permissions { operation: OPERATION_READ entity: "journal_entry" }
  permissions { operation: OPERATION_READ entity: "journal_line" }
  permissions { operation: OPERATION_READ entity: "invoice" }
  permissions { operation: OPERATION_READ entity: "customer" }
  permissions { operation: OPERATION_READ entity: "item" }
  permissions { operation: OPERATION_READ entity: "purchase_order" }
  permissions { operation: OPERATION_READ entity: "sales_order" }
  permissions { operation: OPERATION_CHANGE entity: "item" layer: "cost" }
  permissions { operation: OPERATION_CHANGE entity: "customer" layer: "terms" }
  permissions { operation: OPERATION_DECIDE entity: "sales_order" layer: "placed" }
}

roles {
  name: "treasurer"
  permissions { operation: OPERATION_READ entity: "supplier" }
  permissions { operation: OPERATION_READ entity: "invoice" }
  permissions { operation: OPERATION_CHANGE entity: "supplier" layer: "bank" }
  permissions { operation: OPERATION_DECIDE entity: "supplier" layer: "bank" }
  permissions { operation: OPERATION_GO_BACK entity: "supplier" layer: "bank" }
}

roles {
  name: "payroll"
  permissions { operation: OPERATION_READ entity: "employee" }
  permissions { operation: OPERATION_READ entity: "payslip" }
  permissions { operation: OPERATION_CHANGE entity: "employee" layer: "main" }
  permissions { operation: OPERATION_CHANGE entity: "employee" layer: "pay" }
  permissions { operation: OPERATION_DECIDE entity: "employee" layer: "pay" }
  permissions { operation: OPERATION_GO_BACK entity: "employee" layer: "pay" }
  permissions { operation: OPERATION_CALL procedure: "pay_month" }
}

roles {
  name: "auditor"
  permissions { operation: OPERATION_READ entity: "account" }
  permissions { operation: OPERATION_READ entity: "journal_entry" }
  permissions { operation: OPERATION_READ entity: "journal_line" }
  permissions { operation: OPERATION_READ entity: "invoice" }
  permissions { operation: OPERATION_READ entity: "supplier" }
  permissions { operation: OPERATION_READ entity: "customer" }
  permissions { operation: OPERATION_READ entity: "item" }
  permissions { operation: OPERATION_READ entity: "purchase_order" }
  permissions { operation: OPERATION_READ entity: "purchase_line" }
  permissions { operation: OPERATION_READ entity: "sales_order" }
  permissions { operation: OPERATION_READ entity: "sales_line" }
  permissions { operation: OPERATION_READ entity: "component" }
  permissions { operation: OPERATION_READ entity: "work_order" }
  permissions { operation: OPERATION_READ entity: "planned_order" }
  permissions { operation: OPERATION_READ entity: "employee" }
  permissions { operation: OPERATION_READ entity: "payslip" }
}

atoms {
  name: "period_lock"
  connection: "ledger"
  timeout { seconds: 5 }
  gate_rule { layer { entity: "journal_entry" layer: "posted" } }
}

atoms {
  name: "spend_limit"
  connection: "purchasing"
  timeout { seconds: 5 }
  gate_rule { layer { entity: "purchase_order" layer: "placed" } }
}

atoms {
  name: "credit_limit"
  connection: "sales"
  timeout { seconds: 5 }
  gate_rule { layer { entity: "sales_order" layer: "placed" } }
}

# The procedure states an entry with its lines and adds to the balances of
# its accounts in one change set, so the journal and the ledger never differ.
# It states the invoice of a supplier with its entry.
atoms {
  name: "post"
  connection: "ledger"
  timeout { seconds: 15 }
  handler {
    inputs { name: "entry" type: FIELD_TYPE_TEXT required: true }
    outcomes: "posted"
    outcomes: "refused"
    changes { entity: "journal_entry" layer: "posted" }
    changes { entity: "journal_line" layer: "posted" }
    changes { entity: "account" layer: "balance" }
    changes { entity: "invoice" layer: "issued" }
    events: "entry_posted"
    exposed: true
  }
}

atoms {
  name: "order"
  connection: "purchasing"
  timeout { seconds: 15 }
  handler {
    inputs { name: "request" type: FIELD_TYPE_TEXT required: true }
    outcomes: "placed"
    outcomes: "refused"
    changes { entity: "purchase_order" layer: "placed" }
    changes { entity: "purchase_line" layer: "placed" }
    events: "order_placed"
    exposed: true
  }
}

atoms {
  name: "send_order"
  connection: "purchasing"
  timeout { seconds: 30 }
  handler {
    outcomes: "sent"
    outcomes: "failed"
    changes { entity: "purchase_order" layer: "progress" }
    asks: "mail"
  }
}

# A receipt adds the units to the stock of a warehouse, from received, and
# states where the order stands.
atoms {
  name: "receive"
  connection: "purchasing"
  timeout { seconds: 15 }
  handler {
    inputs { name: "delivery" type: FIELD_TYPE_TEXT required: true }
    outcomes: "received"
    outcomes: "refused"
    changes { entity: "item" layer: "stock" }
    changes { entity: "purchase_order" layer: "progress" }
    events: "goods_received"
    exposed: true
  }
}

# A sale states the order with its lines and allocates the units in one
# change set: of two orders that take the last unit, one publishes and the
# other fails whole.
atoms {
  name: "sell"
  connection: "sales"
  timeout { seconds: 15 }
  handler {
    inputs { name: "request" type: FIELD_TYPE_TEXT required: true }
    outcomes: "taken"
    outcomes: "refused"
    changes { entity: "sales_order" layer: "placed" }
    changes { entity: "sales_line" layer: "placed" }
    changes { entity: "item" layer: "stock" }
    changes { entity: "sales_order" layer: "progress" }
    exposed: true
  }
}

# A delivery issues the units, states the invoice and posts its entry in one
# change set.
atoms {
  name: "deliver"
  connection: "sales"
  timeout { seconds: 15 }
  handler {
    inputs { name: "shipment" type: FIELD_TYPE_TEXT required: true }
    outcomes: "delivered"
    outcomes: "refused"
    changes { entity: "item" layer: "stock" }
    changes { entity: "sales_order" layer: "progress" }
    changes { entity: "invoice" layer: "issued" }
    changes { entity: "journal_entry" layer: "posted" }
    changes { entity: "journal_line" layer: "posted" }
    changes { entity: "account" layer: "balance" }
    events: "goods_delivered"
    events: "entry_posted"
    exposed: true
  }
}

# The handler takes what the bank tells, posts the entry of the payment and
# states that the invoice is paid.
atoms {
  name: "settle"
  connection: "ledger"
  timeout { seconds: 30 }
  handler {
    outcomes: "settled"
    outcomes: "ignored"
    outcomes: "refused"
    changes { entity: "journal_entry" layer: "posted" }
    changes { entity: "journal_line" layer: "posted" }
    changes { entity: "account" layer: "balance" }
    changes { entity: "invoice" layer: "settlement" }
    events: "entry_posted"
  }
}

# The plan reads the orders, the stock and the bills of materials, and
# proposes what to buy and what to make.
atoms {
  name: "plan"
  connection: "production"
  timeout { seconds: 300 }
  handler {
    outcomes: "planned"
    outcomes: "failed"
    changes { entity: "planned_order" layer: "proposal" }
    changes { entity: "planned_order" layer: "decision" }
  }
}

atoms {
  name: "release"
  connection: "production"
  timeout { seconds: 15 }
  handler {
    inputs { name: "plan" type: FIELD_TYPE_TEXT required: true }
    outcomes: "released"
    outcomes: "refused"
    changes { entity: "work_order" layer: "placed" }
    changes { entity: "work_order" layer: "progress" }
    exposed: true
  }
}

# A finished work order takes its components from the stock, as consumed,
# and adds what it made, from produced.
atoms {
  name: "produce"
  connection: "production"
  timeout { seconds: 15 }
  handler {
    inputs { name: "work" type: FIELD_TYPE_TEXT required: true }
    outcomes: "finished"
    outcomes: "refused"
    changes { entity: "item" layer: "stock" }
    changes { entity: "work_order" layer: "progress" }
    events: "work_finished"
    exposed: true
  }
}

# The pay of a month: a payslip for each person, and one entry in the
# ledger, in one change set.
atoms {
  name: "pay_month"
  connection: "ledger"
  timeout { seconds: 120 }
  handler {
    inputs { name: "period" type: FIELD_TYPE_TEXT required: true }
    outcomes: "paid"
    outcomes: "refused"
    changes { entity: "payslip" layer: "issued" }
    changes { entity: "journal_entry" layer: "posted" }
    changes { entity: "journal_line" layer: "posted" }
    changes { entity: "account" layer: "balance" }
    events: "entry_posted"
    exposed: true
  }
}

atoms {
  name: "trial_balance"
  connection: "ledger"
  timeout { seconds: 60 }
  handler {
    inputs { name: "period" type: FIELD_TYPE_TEXT required: true }
    outcomes: "balanced"
    outcomes: "open_items"
  }
}

atoms {
  name: "lock_period"
  connection: "ledger"
  timeout { seconds: 30 }
  handler {
    inputs { name: "period" type: FIELD_TYPE_TEXT required: true }
    outcomes: "locked"
    outcomes: "refused"
    changes { entity: "account" layer: "posting" }
  }
}

events {
  name: "entry_posted"
  entity: "journal_entry"
  exposed: true
}

events {
  name: "order_placed"
  entity: "purchase_order"
}

events {
  name: "goods_received"
  entity: "purchase_order"
  exposed: true
}

events {
  name: "goods_delivered"
  entity: "sales_order"
  exposed: true
}

events {
  name: "work_finished"
  entity: "work_order"
  exposed: true
}

triggers {
  name: "after_order"
  event { event: "order_placed" }
  atom: "send_order"
}

triggers {
  name: "nightly_plan"
  schedule { cron: "0 2 * * *" time_zone: "UTC" }
  atom: "plan"
}

# A month closes in three stages: the trial balance, the sign-off of the
# controller and of finance together, and the lock of the period.
workflows {
  name: "closing"
  inputs { name: "period" type: FIELD_TYPE_TEXT required: true }
  first_stage: "check"
  stages {
    name: "check"
    handler: "trial_balance"
    transitions { outcome: "balanced" next_stage: "sign_off" }
    transitions { outcome: "open_items" }
  }
  stages {
    name: "sign_off"
    person {
      roles { role: "controller" }
      roles { role: "finance" }
      timeout { seconds: 432000 }
      outcomes: "approved"
      outcomes: "reopened"
      agreed_outcome: "approved"
      second_person: true
    }
    transitions { outcome: "approved" next_stage: "lock" }
    transitions { outcome: "reopened" }
  }
  stages {
    name: "lock"
    handler: "lock_period"
    transitions { outcome: "locked" }
    transitions { outcome: "refused" next_stage: "sign_off" }
  }
}

# The bank signs the body, and writes the signature in hex after a prefix.
webhooks {
  name: "bank"
  handler: "settle"
  proof: WEBHOOK_PROOF_SIGNATURE
  proof_header: "X-Bank-Signature"
  proof_prefix: "sha256="
  signature_encoding: SIGNATURE_ENCODING_HEX
  id_header: "X-Bank-Event-Id"
}

The schema of the domain

A message of the public contract, in the text format of Protocol Buffers.

See also

Build your domain.

Declare its schema, and take it to production, self-hosted or in a managed region.

Briefing