Platform modules
The Cardinal.* modules an app may call, with what each one is for.
Apps call only the Cardinal.* modules and CardinalJobs.Worker. Each call takes the ctx of the request or job and runs as its user, unless the ctx comes from Cardinal.DB.as_system/1.
Reference
| Module | Use it for |
|---|---|
Cardinal.App | use Cardinal.App. Also installed?(ctx, :erp) and installed(ctx) |
Cardinal.Ctx | The tenant, user, roles, locale and clock of one request or job. site_url(ctx) |
Cardinal.DB | Records. See Reading and writing records |
Cardinal.Controller | Lifecycle hooks. See Controllers |
Cardinal.Rpc | @rpc and call/3. See RPC and REST |
Cardinal.Jobs | enqueue/4. See Jobs and scheduler |
Cardinal.Patch | Data migrations. See Fixtures and patches |
Cardinal.Report | Code Reports and run/3. See Reports |
Cardinal.ChartSource, Cardinal.Workspace | Charts and workspaces. See Workspaces and dashboards |
Cardinal.ApiRoute | See API routes |
Cardinal.Automation, Cardinal.Automation.Step | See Automation steps |
Cardinal.PrincipalSet, Cardinal.Perm | See Role rules and sets. Perm.unrestricted?/3 tells whether the user reads every record of a RecType |
Cardinal.CLI.Command | See CLI commands |
Cardinal.Mail, Cardinal.SMS, Cardinal.Push, Cardinal.Telegram, Cardinal.Pdf, Cardinal.Template | See Print formats and mail |
Cardinal.Realtime, Cardinal.Notify | See Realtime and the bell |
Cardinal.I18n | t/3, tc/4. See Translations |
Cardinal.Meta | A RecType's compiled fields with the site's custom fields: fields/2, field/3, has_field?/3, rectype/2, list_view/2, catalog/1 |
Cardinal.Mapper | One record started from another. Below |
Cardinal.Naming | next_name(ctx, "SN-.#####", rec), the next name of a series |
Cardinal.Money | precision/1, round/3, round_to_fraction/4 on Decimals, by the site's rounding method |
Cardinal.Dates | today/1, now/1, datetime/3 in the site's time zone. Use these, not Date.utc_today/0 |
Cardinal.Expr | eval(ctx, source, scope), a formula over named values. Below |
Cardinal.Assign | add/5, remove/4, list/3: ToDo assignments on a record |
Cardinal.Share | add/5, remove/4: share a record with a user |
Cardinal.Defaults | The signed-in user's defaults: get_user_default/2, set_user_default/3, clear_user_default/2 |
Cardinal.Files | upload(ctx, path, file_name, values) as a File, read(ctx, name) |
Cardinal.Blobs | Large files outside File, served to signed-in users at /api/blob/<app>/<key>. put/4, size/3, delete/3 |
Cardinal.Sheet | read(bytes, "csv" | "xlsx") to rows, write(rows, ext) |
Cardinal.HTTP | Outside HTTP calls through the outbound guard: request/5,6, request_with_headers/5,6, stream/6,7 |
Cardinal.Search | global(ctx, q, opts), global search as the user |
Cardinal.Workflow | check/2, what a Workflow save would refuse, and roles/1 |
Cardinal.Auth | Login passwords: set_login_password/4, has_password?/2, active_sessions/2 |
Cardinal.Google | settings/1 and directions/5 through Google Settings |
Cardinal.Test | Test helpers. See Testing |
Mapping one record to another
Cardinal.Mapper.get_mapped_doc/5 is Frappe's get_mapped_doc. It returns an unsaved record of another RecType started from a stored one. Call it from the RPC a manifest create entry names:
@rpc requires: {:create, "Gate Sale"}, args: [source_name: :string]
def make_sale(ctx, %{source_name: name}) do
Cardinal.Mapper.get_mapped_doc(ctx, "Gate Offer", name, %{
"Gate Offer" => %{rectype: "Gate Sale", validation: %{"docstatus" => {"=", 1}}},
"Gate Offer Line" => %{rectype: "Gate Sale Line", condition: &(&1["qty"] > 0)}
})
endcreate: [%{from: "Gate Offer", to: "Gate Sale", rpc: "gate.mappers.make_sale", depends_on: "doc.docstatus == 1"}]| Map key | Meaning |
|---|---|
rectype | The target RecType. Required |
field_map | %{source_field => target_field} |
field_no_map | Target fields left alone |
validation | %{field => {"=", value}} the source must match |
postprocess | fn source, target, source_parent -> target end |
condition | On a child map: fn row -> boolean end keeps a row |
reset_value | On a child map: empties the target table first |
Fields with the same name copy over unless empty or no_copy.
Formulas
{:ok, amount} =
Cardinal.Expr.eval(ctx, "if gross_pay > 50000 then round(B * 0.12, 2) else 0", %{
"gross_pay" => {:number, "62000.00"},
"B" => {:number, "40000.00"}
})Numbers are exact Decimals and floats are refused. A record can go in as {:record, rectype, rec}, and its fields read as employee.date_of_birth. Cardinal.Expr.check(source, schema) compiles without running.