cardinal

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

ModuleUse it for
Cardinal.Appuse Cardinal.App. Also installed?(ctx, :erp) and installed(ctx)
Cardinal.CtxThe tenant, user, roles, locale and clock of one request or job. site_url(ctx)
Cardinal.DBRecords. See Reading and writing records
Cardinal.ControllerLifecycle hooks. See Controllers
Cardinal.Rpc@rpc and call/3. See RPC and REST
Cardinal.Jobsenqueue/4. See Jobs and scheduler
Cardinal.PatchData migrations. See Fixtures and patches
Cardinal.ReportCode Reports and run/3. See Reports
Cardinal.ChartSource, Cardinal.WorkspaceCharts and workspaces. See Workspaces and dashboards
Cardinal.ApiRouteSee API routes
Cardinal.Automation, Cardinal.Automation.StepSee Automation steps
Cardinal.PrincipalSet, Cardinal.PermSee Role rules and sets. Perm.unrestricted?/3 tells whether the user reads every record of a RecType
Cardinal.CLI.CommandSee CLI commands
Cardinal.Mail, Cardinal.SMS, Cardinal.Push, Cardinal.Telegram, Cardinal.Pdf, Cardinal.TemplateSee Print formats and mail
Cardinal.Realtime, Cardinal.NotifySee Realtime and the bell
Cardinal.I18nt/3, tc/4. See Translations
Cardinal.MetaA 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.MapperOne record started from another. Below
Cardinal.Namingnext_name(ctx, "SN-.#####", rec), the next name of a series
Cardinal.Moneyprecision/1, round/3, round_to_fraction/4 on Decimals, by the site's rounding method
Cardinal.Datestoday/1, now/1, datetime/3 in the site's time zone. Use these, not Date.utc_today/0
Cardinal.Expreval(ctx, source, scope), a formula over named values. Below
Cardinal.Assignadd/5, remove/4, list/3: ToDo assignments on a record
Cardinal.Shareadd/5, remove/4: share a record with a user
Cardinal.DefaultsThe signed-in user's defaults: get_user_default/2, set_user_default/3, clear_user_default/2
Cardinal.Filesupload(ctx, path, file_name, values) as a File, read(ctx, name)
Cardinal.BlobsLarge files outside File, served to signed-in users at /api/blob/<app>/<key>. put/4, size/3, delete/3
Cardinal.Sheetread(bytes, "csv" | "xlsx") to rows, write(rows, ext)
Cardinal.HTTPOutside HTTP calls through the outbound guard: request/5,6, request_with_headers/5,6, stream/6,7
Cardinal.Searchglobal(ctx, q, opts), global search as the user
Cardinal.Workflowcheck/2, what a Workflow save would refuse, and roles/1
Cardinal.AuthLogin passwords: set_login_password/4, has_password?/2, active_sessions/2
Cardinal.Googlesettings/1 and directions/5 through Google Settings
Cardinal.TestTest 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)}
  })
end
app.exs
create: [%{from: "Gate Offer", to: "Gate Sale", rpc: "gate.mappers.make_sale", depends_on: "doc.docstatus == 1"}]
Map keyMeaning
rectypeThe target RecType. Required
field_map%{source_field => target_field}
field_no_mapTarget fields left alone
validation%{field => {"=", value}} the source must match
postprocessfn source, target, source_parent -> target end
conditionOn a child map: fn row -> boolean end keeps a row
reset_valueOn 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.

On this page