Exercise create a total weight field in erpnext Sales Order
$ 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: [email protected]
App Icon (default 'octicon octicon-file-directory'): icon-book
App Color (default 'grey'): blue
App License (default 'MIT'): MIT
$ cat sites/currentsite.txt
site1.local
$ bench --site site1.local install-app erpnext_shipping
$ cat apps/erpnext_shipping/requirements.txt
frappe
erpnext
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
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]
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",
}
}