Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
127 lines
5.1 KiB
Go
127 lines
5.1 KiB
Go
package erpsetup
|
|
|
|
// What the registry expects in ERPNext. Keep in step with internal/erp
|
|
// (Customer.tenant_id, Lead fields, UsageRecord) and the webhook receiver
|
|
// (internal/server/erp.go, erp.WebhookEvent).
|
|
|
|
func customField(dt, fieldname, label, fieldtype, after string, unique bool) map[string]any {
|
|
f := map[string]any{
|
|
"doctype": "Custom Field",
|
|
"name": dt + "-" + fieldname,
|
|
"dt": dt,
|
|
"fieldname": fieldname,
|
|
"label": label,
|
|
"fieldtype": fieldtype,
|
|
"insert_after": after,
|
|
"no_copy": 1,
|
|
"description": "Set by the BreakPilot tenant-registry. Do not edit.",
|
|
}
|
|
if unique {
|
|
f["unique"] = 1
|
|
}
|
|
return f
|
|
}
|
|
|
|
func customFields(leadDoctype string) []map[string]any {
|
|
return []map[string]any{
|
|
customField("Customer", "tenant_id", "Tenant ID", "Data", "customer_name", true),
|
|
customField("Customer", "tenant_slug", "Tenant slug", "Data", "tenant_id", false),
|
|
customField(leadDoctype, "tenant_id", "Tenant ID", "Data", "company_name", false),
|
|
customField(leadDoctype, "requested_product", "Requested product", "Data", "tenant_id", false),
|
|
}
|
|
}
|
|
|
|
func usageRecordDoctype() map[string]any {
|
|
field := func(name, label, typ string, extra map[string]any) map[string]any {
|
|
f := map[string]any{"fieldname": name, "label": label, "fieldtype": typ, "in_list_view": 1}
|
|
for k, v := range extra {
|
|
f[k] = v
|
|
}
|
|
return f
|
|
}
|
|
return map[string]any{
|
|
"doctype": "DocType",
|
|
"name": "Usage Record",
|
|
"module": "Selling",
|
|
"custom": 1,
|
|
"autoname": "hash",
|
|
"track_changes": 0,
|
|
"description": "Metered usage pushed by the BreakPilot tenant-registry (ERP/02).",
|
|
"fields": []map[string]any{
|
|
field("customer", "Customer", "Link", map[string]any{"options": "Customer", "reqd": 1}),
|
|
field("tenant_id", "Tenant ID", "Data", map[string]any{"reqd": 1}),
|
|
field("product", "Product", "Data", map[string]any{"reqd": 1}),
|
|
field("metric", "Metric", "Data", map[string]any{"reqd": 1}),
|
|
field("qty", "Quantity", "Float", map[string]any{"reqd": 1}),
|
|
field("occurred_at", "Occurred at", "Datetime", map[string]any{"reqd": 1}),
|
|
field("idempotency_key", "Idempotency key", "Data", map[string]any{"reqd": 1, "unique": 1, "in_list_view": 0}),
|
|
},
|
|
"permissions": []map[string]any{
|
|
{"role": "System Manager", "read": 1, "write": 1, "create": 1, "delete": 1, "report": 1, "export": 1},
|
|
{"role": "Accounts Manager", "read": 1, "report": 1, "export": 1},
|
|
{"role": "Sales Manager", "read": 1, "report": 1, "export": 1},
|
|
// The registry's API user (platform-registry) holds Sales User and
|
|
// Accounts User, not System Manager: it only appends usage.
|
|
{"role": "Sales User", "read": 1, "create": 1},
|
|
},
|
|
}
|
|
}
|
|
|
|
type webhook struct {
|
|
Doctype string
|
|
DocEvent string
|
|
Condition string
|
|
Event string // the registry event the JSON carries
|
|
JSON string // Jinja template of the body
|
|
URL string
|
|
Secret string
|
|
}
|
|
|
|
func (w webhook) doc() map[string]any {
|
|
return map[string]any{
|
|
"doctype": "Webhook",
|
|
"webhook_doctype": w.Doctype,
|
|
"webhook_docevent": w.DocEvent,
|
|
"condition": w.Condition,
|
|
"request_url": w.URL,
|
|
"request_method": "POST",
|
|
"request_structure": "JSON",
|
|
"webhook_json": w.JSON,
|
|
"enable_security": 1,
|
|
"webhook_secret": w.Secret,
|
|
"enabled": 1,
|
|
"webhook_headers": []map[string]any{{"key": "Content-Type", "value": "application/json"}},
|
|
}
|
|
}
|
|
|
|
// The body templates render erp.WebhookEvent. "modified" makes the receiver's
|
|
// fallback event id unique per document version (live Frappe sends no
|
|
// X-Frappe-Event-Id).
|
|
func webhooks(url, secret string) []webhook {
|
|
ws := []webhook{
|
|
{
|
|
Doctype: "Sales Order", DocEvent: "on_submit", Event: "sales_order.submit",
|
|
JSON: `{"event":"sales_order.submit","doctype":"Sales Order","name":"{{ doc.name }}","modified":"{{ doc.modified }}","customer":"{{ doc.customer }}","items":[{% for i in doc.items %}{"item_code":"{{ i.item_code }}","qty":{{ i.qty }}}{% if not loop.last %},{% endif %}{% endfor %}]}`,
|
|
},
|
|
{
|
|
Doctype: "Sales Invoice", DocEvent: "on_update_after_submit", Event: "sales_invoice.overdue",
|
|
Condition: `doc.status == "Overdue"`,
|
|
JSON: `{"event":"sales_invoice.overdue","doctype":"Sales Invoice","name":"{{ doc.name }}","modified":"{{ doc.modified }}","customer":"{{ doc.customer }}","invoice":"{{ doc.name }}","days_overdue":{{ frappe.utils.date_diff(frappe.utils.nowdate(), doc.due_date) }}}`,
|
|
},
|
|
{
|
|
Doctype: "Payment Entry", DocEvent: "on_submit", Event: "payment_entry.submit",
|
|
Condition: `doc.party_type == "Customer"`,
|
|
JSON: `{"event":"payment_entry.submit","doctype":"Payment Entry","name":"{{ doc.name }}","modified":"{{ doc.modified }}","customer":"{{ doc.party }}","amount":{{ doc.paid_amount }}}`,
|
|
},
|
|
{
|
|
Doctype: "Subscription", DocEvent: "on_update", Event: "subscription.cancel",
|
|
Condition: `doc.status == "Cancelled"`,
|
|
JSON: `{"event":"subscription.cancel","doctype":"Subscription","name":"{{ doc.name }}","modified":"{{ doc.modified }}","customer":"{{ doc.party }}","subscription":"{{ doc.name }}"}`,
|
|
},
|
|
}
|
|
for i := range ws {
|
|
ws[i].URL, ws[i].Secret = url, secret
|
|
}
|
|
return ws
|
|
}
|