erpnext-impl-controllers
29
总安装量
9
周安装量
#12549
全站排名
安装命令
npx skills add https://github.com/openaec-foundation/erpnext_anthropic_claude_development_skill_package --skill erpnext-impl-controllers
Agent 安装分布
claude-code
7
opencode
5
github-copilot
5
codex
5
amp
5
Skill 文档
ERPNext Controllers – Implementation
This skill helps you determine HOW to implement server-side DocType logic. For exact syntax, see erpnext-syntax-controllers.
Version: v14/v15/v16 compatible
Main Decision: Controller vs Server Script?
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â WHAT DO YOU NEED? â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¤
â â
â ⺠Import external libraries (requests, pandas, numpy) â
â âââ Controller â â
â â
â ⺠Complex multi-document transactions with rollback â
â âââ Controller â â
â â
â ⺠Full Python power (try/except, classes, generators) â
â âââ Controller â â
â â
â ⺠Extend/override standard ERPNext DocType â
â âââ Controller (override_doctype_class in hooks.py) â
â â
â ⺠Quick validation without custom app â
â âââ Server Script â
â â
â ⺠Simple auto-fill or calculation â
â âââ Server Script â
â â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
Rule: Controllers for custom apps with full Python power. Server Scripts for quick no-code solutions.
Decision Tree: Which Hook?
WHAT DO YOU WANT TO DO?
â
ââ⺠Validate data or calculate fields before save?
â ââ⺠validate
â NOTE: Changes to self ARE saved
â
ââ⺠Action AFTER save (emails, linked docs, logs)?
â ââ⺠on_update
â â ï¸ Changes to self are NOT saved! Use db_set instead
â
ââ⺠Only for NEW documents?
â ââ⺠after_insert
â
ââ⺠Only for SUBMIT (docstatus 0â1)?
â ââ⺠Check before submit? â before_submit
â ââ⺠Action after submit? â on_submit
â
ââ⺠Only for CANCEL (docstatus 1â2)?
â ââ⺠Prevent cancel? â before_cancel
â ââ⺠Cleanup after cancel? â on_cancel
â
ââ⺠Before DELETE?
â ââ⺠on_trash
â
ââ⺠Custom document naming?
â ââ⺠autoname
â
ââ⺠Detect any change (including db_set)?
ââ⺠on_change
â See references/decision-tree.md for complete decision tree with all hooks.
CRITICAL: Changes After on_update
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â â ï¸ CHANGES TO self AFTER on_update ARE NOT SAVED â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¤
â â
â â WRONG - This does NOTHING: â
â def on_update(self): â
â self.status = "Completed" # NOT SAVED! â
â â
â â
CORRECT - Use db_set: â
â def on_update(self): â
â frappe.db.set_value(self.doctype, self.name, â
â "status", "Completed") â
â â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
Hook Comparison: validate vs on_update
| Aspect | validate | on_update |
|---|---|---|
| When | Before DB write | After DB write |
| Changes to self | â Saved | â NOT saved |
| Can throw error | â Aborts save | â ï¸ Already saved |
| Use for | Validation, calculations | Notifications, linked docs |
| get_doc_before_save() | â Available | â Available |
Common Implementation Patterns
Pattern 1: Validation with Error
def validate(self):
if not self.items:
frappe.throw(_("At least one item is required"))
if self.from_date > self.to_date:
frappe.throw(_("From Date cannot be after To Date"))
Pattern 2: Auto-Calculate Fields
def validate(self):
self.total = sum(item.amount for item in self.items)
self.tax_amount = self.total * 0.1
self.grand_total = self.total + self.tax_amount
Pattern 3: Detect Field Changes
def validate(self):
old_doc = self.get_doc_before_save()
if old_doc and old_doc.status != self.status:
self.flags.status_changed = True
def on_update(self):
if self.flags.get('status_changed'):
self.notify_status_change()
Pattern 4: Post-Save Actions
def on_update(self):
# Update linked document
if self.linked_doc:
frappe.db.set_value("Other DocType", self.linked_doc,
"status", "Updated")
# Send notification (never fails the save)
try:
self.send_notification()
except Exception:
frappe.log_error("Notification failed")
Pattern 5: Custom Naming
from frappe.model.naming import getseries
def autoname(self):
# Format: CUST-ABC-001
prefix = f"CUST-{self.customer[:3].upper()}-"
self.name = getseries(prefix, 3)
â See references/workflows.md for more implementation patterns.
Submittable Documents Workflow
DRAFT (docstatus=0)
â
âââ save() â validate â on_update
â
âââ submit()
â
âââ validate
âââ before_submit â Last chance to abort
âââ [DB: docstatus=1]
âââ on_update
âââ on_submit â Post-submit actions
SUBMITTED (docstatus=1)
â
âââ cancel()
â
âââ before_cancel â Last chance to abort
âââ [DB: docstatus=2]
âââ on_cancel â Reverse actions
âââ [check_no_back_links]
Submittable Implementation
def before_submit(self):
# Validation that only applies on submit
if self.total > 50000 and not self.manager_approval:
frappe.throw(_("Manager approval required for orders over 50,000"))
def on_submit(self):
# Actions after submit
self.update_stock_ledger()
self.make_gl_entries()
def before_cancel(self):
# Prevent cancel if linked docs exist
if self.has_linked_invoices():
frappe.throw(_("Cannot cancel - linked invoices exist"))
def on_cancel(self):
# Reverse submitted actions
self.reverse_stock_ledger()
self.reverse_gl_entries()
Controller Override (hooks.py)
Method 1: Full Override
# hooks.py
override_doctype_class = {
"Sales Invoice": "myapp.overrides.CustomSalesInvoice"
}
# myapp/overrides.py
from erpnext.accounts.doctype.sales_invoice.sales_invoice import SalesInvoice
class CustomSalesInvoice(SalesInvoice):
def validate(self):
super().validate() # ALWAYS call parent
self.custom_validation()
Method 2: Add Event Handler (Safer)
# hooks.py
doc_events = {
"Sales Invoice": {
"validate": "myapp.events.validate_sales_invoice",
}
}
# myapp/events.py
def validate_sales_invoice(doc, method=None):
if doc.grand_total < 0:
frappe.throw(_("Invalid total"))
V16: extend_doctype_class (New)
# hooks.py (v16+)
extend_doctype_class = {
"Sales Invoice": "myapp.extends.SalesInvoiceExtend"
}
# myapp/extends.py - Only methods to add/override
class SalesInvoiceExtend:
def custom_method(self):
pass
Flags System
# Document-level flags
doc.flags.ignore_permissions = True # Bypass permissions
doc.flags.ignore_validate = True # Skip validate()
doc.flags.ignore_mandatory = True # Skip required fields
# Custom flags for inter-hook communication
def validate(self):
if self.is_urgent:
self.flags.needs_notification = True
def on_update(self):
if self.flags.get('needs_notification'):
self.notify_team()
# Insert/save with flags
doc.insert(ignore_permissions=True, ignore_mandatory=True)
doc.save(ignore_permissions=True)
Execution Order Reference
INSERT (New Document)
before_insert â before_naming â autoname â before_validate â
validate â before_save â [DB INSERT] â after_insert â
on_update â on_change
SAVE (Existing Document)
before_validate â validate â before_save â [DB UPDATE] â
on_update â on_change
SUBMIT
validate â before_submit â [DB: docstatus=1] â on_update â
on_submit â on_change
â See references/decision-tree.md for all execution orders.
Quick Anti-Pattern Check
| â Don’t | â Do Instead |
|---|---|
self.x = y in on_update |
frappe.db.set_value(...) |
frappe.db.commit() in hooks |
Let framework handle commits |
| Heavy operations in validate | Use frappe.enqueue() in on_update |
self.save() in on_update |
Causes infinite loop! |
| Assume hook order across docs | Each doc has its own cycle |
â See references/anti-patterns.md for complete list.
References
- decision-tree.md – Complete hook selection with all execution orders
- workflows.md – Extended implementation patterns
- examples.md – Complete working examples
- anti-patterns.md – Common mistakes to avoid