Skip to content
ERPForAll DocsDevelopment

    Purchase orders: states, operations and extension steps

    The purchase addon (depends = ["base", "account"], an application) owns purchase.order. The contract is ADR 0197 D5; the cases are docs/specs/purchase/purchase-min.md and docs/specs/purchase/access.md. Mail, print, receipts and bills come with their own addons and slices.

    Operation (RPC, effect = write)Legal fromResult
    button_confirmdraft, sentpurchase through the approval step when _approval_allowed says yes, else to approve
    button_approveto approvethe approval step; a caller the approval question refuses gets an access error naming purchase.group_purchase_manager
    button_cancelevery state but cancel, not lockedthe cancel step; orders already cancelled are left as they are
    button_draftcanceldraft
    button_lockpurchaselocked; an order already locked is left as it is
    button_unlocklockedunlocked; purchase administrators only, checked first

    Every operation locks its rows in id order without waiting (a held lock is an immediate lock error) and refuses the whole call when one order is not in a legal state, naming each such order and its state. Nothing is written then. State changes go through the base writer (Records::write_base), so a write override of another addon never sees them.

    A client write never sets state, locked, date_approve or name; create refuses the first three (an order is born a draft, unlocked, without approval date) and keeps a number it is given. create also refuses these fields and the order amounts as default_<field> context keys, since the base create applies those after the checks; the lines refuse their amounts, the received quantity, state and date_approve the same way, also when they are created through the order’s order_line commands. A confirmed order keeps its vendor, currency and company; a cancelled order keeps every field; a locked order keeps the fields of _protected_fields. Each refusal is a validation error naming the field, and the order when there is one. A default_<field> context key for a field create refuses (the three above and the amounts) is refused by name too, also with sudo, so a create or a copy cannot go around the refusal. Only a cancelled order is deleted.

    purchase_stock implements receipt generation as an _approve extension, so both the automatic approval from confirmation and the separate approval button reach it. The addon uses the incoming operation of the company’s one active warehouse and its configured source and destination. The vendor can supply its company-specific supplier location. Quantities move in the purchase line’s unit and stock performs the conversion to the product unit. Service lines and zero quantities need no move. Missing unit falls back through the stock move’s product unit and a missing line schedule uses the order date.

    The hook creates the picking, moves and immutable order/line links in its approval savepoint, then uses the standard stock confirmation method. Receipt and move identities have restrictive foreign keys. Cancelling the still-open direct receipt uses stock’s cancellation operation in the same transaction as the order’s cancel step. Completing a linked incoming move sets the purchase line’s qty_received in its purchase unit. The calculation excludes scrap and independent returns. Replacing receipts after reset, propagating backorders, and attributing purchase credits are separate contracts.

    The bridge grants purchase users read, create and change access to incoming transfers and their moves and operations. That access follows the actual transfer. A move uses its transfer’s operation type, and its details use that same transfer. Record changes must satisfy those associations before and after the write. Cancelling a receipt can remove its open reservation details. Approval first checks read access to every source line and its copied fields. An inaccessible line or source reference refuses the entire order. Existing stock company rules still apply. Install or upgrade the linked native purchase_stock addon to publish its security data before using the bridge.

    An order created without a number, or with New, draws the next number of the purchase.order sequence for its company through erp_sdk::sequence::draw_by_code, dated by the deadline in the user’s time zone. The addon ships one shared sequence (purchase.seq_purchase_order, prefix PO, padding 5). A company gets its own series when an administrator adds a sequence with the same code and that company under Technical, Sequences. Without any sequence for the code the create is refused, naming the code and the company.

    purchase.order.line holds product, description, quantity, unit, planned date, unit price, discount and taxes. What a create leaves out is filled from the product and the order:

    • unit: the product’s unit; description: its purchase description; taxes: its vendor taxes of the order company;
    • unit price and discount: the vendor price for the order’s vendor, quantity, unit and deadline date, through the product seller service (_select_seller_exact of product.product, run under the order’s company), else the product cost; converted to the line unit and the order currency;
    • planned date: the deadline plus the vendor price’s lead time (the deadline itself without a vendor price).

    A given unit price counts as set by hand unless the values also carry technical_price_unit with the same value: the line form sends both untouched (default 0), so a line saved from the form without a typed price is priced, and its default discount 0 then takes the vendor’s discount. A price given without the technical price (an import, a copy) is kept. A vendor price always has a lead time (the field is required, one day by default; the product upgrade fills older rows); a tax without a sequence and a product without a cost currency are refused by name instead of being read as 0 or another currency.

    A later change of quantity, unit or product prices the line again, unless someone set its price by hand; a new product also takes the unit, vendor taxes and description of the new product, and a new vendor on the order prices its lines again the same way. The stored company and vendor of a line and its technical price are never written through the line. Subtotal, tax and total come from erp_addon_account::tax::compute and are written after every create, write and delete of a line, with the order’s untaxed amount, taxes, total, total in the company currency and earliest planned date. A client never writes these amounts, the selected vendor price row or the received quantity (that one comes with receipts), neither in the values nor as a default_<field> context key. A tax with a negative distribution (reverse charge) is refused by name until the tax computation supports it. Confirming an order with a line without a product is refused. Lines of a locked or cancelled order do not change, and lines of a confirmed order are added but not deleted. A change of the lines of an approved order that takes its total in the company currency above the limit the caller may approve is refused naming the order; managers, a lower total and a raise inside the limit pass, and the lines of an order waiting for approval stay editable. A company change is refused on an order that has lines; the deadline is refused once the order is approved. The total in the company currency converts at the UTC calendar day of the deadline (a rate dated that day does not count yet), whoever saves the order; the user’s time zone is not used for stored amounts. A line created without tax_ids takes the product’s vendor taxes; a given set, the empty one included, is kept.

    To change how a line is priced, extend the seller service of product (ADR 0197 D6, worked example), not the line.

    views/purchase_views.xml holds the screens, in the view dialect the server admits (views.md):

    • the Purchase app (purchase.menu_purchase_root, purchase users) with Orders > Requests for Quotation (purchase.purchase_rfq, every order) and Orders > Purchase Orders (purchase.purchase_form_action, state purchase), and Configuration > Settings (purchase.purchase_settings_action, purchase administrators), the shared Settings page opened on the Purchase app;
    • one list, form and search of purchase.order for both actions. The form header offers the operations above, each shown only in its legal states (Approve and Unlock for purchase administrators); the lines are the one2many order_line, shown with the default list and form of purchase.order.line (view_purchase_order_line_list, view_purchase_order_line_form) and edited in a dialog;
    • a new order form starts with the active company, its currency, the user as buyer and now as deadline (default_get); the number is drawn on save.

    To add a column or a field, inherit purchase.purchase_order_list, purchase.purchase_order_form or the line views from your addon (views.md, inheritance); a line form needs no inline sub-view.

    Three company settings, on the Settings page under Purchase, block Orders; administrators (base.group_system) write them, purchase users read them:

    KeyValuesDefault
    purchase.po_double_validationone_step, two_stepone_step
    purchase.po_double_validation_amount0 to 10^12, in the company currency5,000
    purchase.po_lockedit, lockedit

    With two_step, an order whose total in the company currency is not under the amount waits for a purchase administrator; the approval question converts the order total at that moment, and a change of an order’s company converts the stored total into the new company’s currency; with lock, the approval step locks the order. Each order of a call uses its own company’s values. Native code reads them with erp_addon_purchase::settings::OrderPolicy::read.

    Declare purchase in your addon’s depends, #[extend(PurchaseOrder)] in your crate, and override one of these methods with #[method(override)], calling sup exactly once (ADR 0197 D6). None is callable over RPC.

    MethodCalledUse it to
    _approval_allowed → Vec<bool> (one answer per order, id order)by confirm and approveadd an approval rule
    _approvefor the orders that become purchase orderscreate receipts, start follow-up work
    _action_cancelfor the orders being cancelled, never twice for one ordercancel what the order started
    _mark_rfqs_as_sentafter an RFQ is sent or printedreact to sending
    _protected_fields → Vec<String> (vendor, currency, company, lines)by write on a locked orderadd your fields to what a locked order keeps

    Override the step, never the button: the buttons do the state checks and call the steps once per order.

    The original direct receipt exposes its source through the computed field stock.picking.purchase_order_id. Both directions resolve the immutable transfer association and apply the caller’s read access to each endpoint.

    On a direct vendor bill item, account.move.line.purchase_source_line_id resolves the saved association for the current user. The comparison fields in the item form read the linked order and its terms through that source. Each field requires the Purchase user group. The association table stays private. A manual invoice line has no purchase source. The fields are computed, and install adds no accounting columns.

    The mutating purchase.order.action_create_vendor_bill method takes one approved order. It returns a window result for the saved bill’s account.view_invoice_form in a dialog. The caller needs purchase access and accounting access. The action enters the normal Accounting create path with a vendor, company, currency, purchase reference and explicit invoice line commands. Line identity uses private, transaction-owned associations. Accessing or editing a bill continues to use the caller’s Accounting rights.

    The database-scoped System Parameter purchase.max_direct_bill_source_rows defaults to 10,000 and admits values from 1 through 100,000. It bounds the original purchase memberships and tax associations separately. Each probe reads at most the configured limit plus one witness, with access checked before revealing a size error. Subsequent calls on the same PO open the existing first bill.

    The purchase.order.action_bill_remaining RPC uses the same native invoice creator and form. It subtracts active bill-line quantities in the original purchase unit from each purchase line’s received quantity. The unique keys on purchase.direct.bill.line allow a source line once on each bill and an accounting item once globally. The bounded technical parameter also covers existing and proposed source associations. The first-bill action still opens the oldest existing bill for the order.

    Install the optional native addon purchase_three_way_match to add the non-stored, caller-scoped purchase_match_status on account.move and account.move.line. On linked vendor bills, the service compares complete source quantities across active linked items, original units, currency, product, agreed price and discount. It uses purchase.max_direct_bill_source_rows for the bill and history probes. Expanding this configured bound applies to the matching read and the direct creation workflow. Purchase dispatches its private _invalidate_purchase_bill_review hook when source links or purchase terms change.

    Purchase Stock 1.3.0 registers the Buy action on stock rules and the shared route purchase_stock.route_buy. Module installation and upgrade provision receiving rules from each eligible warehouse’s active incoming operation, supplier source and internal stock destination. New warehouse creation invokes the same provisioning after Inventory creates its topology. Stock stores the generated rule under the warehouse, addon and buy_receipt role. The lifecycle preserves administrator-modified, archived and existing rules. At shared destinations, a rule with the warehouse company or empty company prevents a duplicate. A company-specific Buy route provisions only warehouses of its owner. The manual action checks completeness against active rules in that same company or shared scope and reads the chosen rule as the caller.

    On one existing active stock.warehouse.orderpoint, the write RPC action_replenish_buy opens the exact purchase order form. The company-scoped stock forecast supplies the shortfall and the product’s seller selection supplies the vendor. Order and order-line creation use caller rights. The private purchase.stock.replenishment.link has a unique source orderpoint. It starts empty and is created only with a real draft. A pending quotation is reused across workers. Once the quotation leaves its pending states, a later request reevaluates the forecast.