Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save eclecticmiraclecat/d14e8b7a5f19a0437f84353d067e1d1d to your computer and use it in GitHub Desktop.
Save eclecticmiraclecat/d14e8b7a5f19a0437f84353d067e1d1d to your computer and use it in GitHub Desktop.

Frappe - Creating a Custom Application & Integrating it with ERPNext

Exercise create a total weight field in erpnext Sales Order

Custom Application

create new app

$ bench new-app erpnext_shipping
INFO:bench.app:creating new app erpnext_shipping
App Title (default: Erpnext Shipping): Manage shipping
App Description: Manage shipping
App Publisher: frappe
App Email: frappe@example.com
App Icon (default 'octicon octicon-file-directory'): icon-book
App Color (default 'grey'): blue
App License (default 'MIT'): MIT

install app to site

$ cat sites/currentsite.txt 
site1.local

$ bench --site site1.local install-app erpnext_shipping

Integrating Custom App with ERPNext

$ cat apps/erpnext_shipping/requirements.txt
frappe
erpnext

interaction between apps are done in hooks.py

since Sales Order is in ERPNext, the calculation will be triggered from custom app through hooks.py

$ ls apps/erpnext_shipping/erpnext_shipping/hooks.py

Custom Fields

add field in browser

Setup > Customize Form > Sales Order Item
1. Weight per unit custom (Float)
2. Weight custom          (Float)

Setup > Customize Form > Sales Order
1. Total weight custom   (Float)   [Read Only]

Scripting in Custom App

should be executed from hooks.py

$ vim apps/erpnext_shipping/erpnext_shipping/manage_shipping/sales_order_custom.py
# apps/erpnext_shipping/erpnext_shipping/manage_shipping/sales_order_custom.py

from frappe.utils import flt

def calculate_total_weight(doc, method):
    total_weight = 0.0
    for d in doc.items:
        d.weight_custom = flt(d.weight_per_unit_custom) * flt(d.qty)
        total_weight += d.weight_custom

    doc.total_weight_custom = total_weight
# apps/erpnext_shipping/erpnext_shipping/hooks.py

doc_events = {
      "Sales Order": {
              "validate": "erpnext_shipping.manage_shipping.sales_order_custom.calculate_total_weight",
      }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment