Skip to content

Commit

Permalink
fix: fetch from parent not working for custom field
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitwaghchaure committed Sep 6, 2022
1 parent 3c688df commit 76ae4d8
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 3 deletions.
2 changes: 2 additions & 0 deletions erpnext/controllers/selling_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ def get_item_list(self):
"sales_invoice_item": d.get("sales_invoice_item"),
"dn_detail": d.get("dn_detail"),
"incoming_rate": p.get("incoming_rate"),
"item_row": p,
}
)
)
Expand All @@ -334,6 +335,7 @@ def get_item_list(self):
"sales_invoice_item": d.get("sales_invoice_item"),
"dn_detail": d.get("dn_detail"),
"incoming_rate": d.get("incoming_rate"),
"item_row": d,
}
)
)
Expand Down
12 changes: 12 additions & 0 deletions erpnext/controllers/stock_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,10 @@ def get_sl_entries(self, d, args):
return sl_dict

def update_inventory_dimensions(self, row, sl_dict) -> None:
# To handle delivery note and sales invoice
if row.get("item_row"):
row = row.get("item_row")

dimensions = get_evaluated_inventory_dimension(row, sl_dict, parent_doc=self)
for dimension in dimensions:
if not dimension:
Expand All @@ -407,9 +411,17 @@ def update_inventory_dimensions(self, row, sl_dict) -> None:
"DocField", {"parent": self.doctype, "options": dimension.fetch_from_parent}, "fieldname"
)

if not fieldname:
fieldname = frappe.get_cached_value(
"Custom Field", {"dt": self.doctype, "options": dimension.fetch_from_parent}, "fieldname"
)

if fieldname and self.get(fieldname):
sl_dict[dimension.target_fieldname] = self.get(fieldname)

if sl_dict[dimension.target_fieldname]:
row.set(dimension.source_fieldname, sl_dict[dimension.target_fieldname])

def make_sl_entries(self, sl_entries, allow_negative_stock=False, via_landed_cost_voucher=False):
from erpnext.stock.stock_ledger import make_sl_entries

Expand Down
28 changes: 28 additions & 0 deletions erpnext/stock/doctype/inventory_dimension/inventory_dimension.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,34 @@ frappe.ui.form.on('Inventory Dimension', {
}
},

onload(frm) {
frm.trigger("set_parent_fields");
},

document_type(frm) {
frm.trigger("set_parent_fields");
},

set_parent_fields(frm) {
if (frm.doc.apply_to_all_doctypes) {
frm.set_df_property("fetch_from_parent", "options", frm.doc.reference_document);
} else if (frm.doc.document_type && frm.doc.istable) {
frappe.call({
method: 'erpnext.stock.doctype.inventory_dimension.inventory_dimension.get_parent_fields',
args: {
child_doctype: frm.doc.document_type,
dimension_name: frm.doc.reference_document
},
callback: (r) => {
if (r.message && r.message.length) {
frm.set_df_property("fetch_from_parent", "options",
[""].concat(r.message));
}
}
})
}
},

delete_dimension(frm) {
let msg = (`
Custom fields related to this dimension will be deleted on deletion of dimension.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,15 @@
"fieldtype": "Column Break"
},
{
"depends_on": "istable",
"description": "Set fieldname or DocType name like Supplier, Customer etc.",
"fieldname": "fetch_from_parent",
"fieldtype": "Data",
"fieldtype": "Select",
"label": "Fetch Value From Parent Form"
}
],
"index_web_pages_for_search": 1,
"links": [],
"modified": "2022-08-17 11:43:24.722441",
"modified": "2022-09-02 13:29:04.098469",
"modified_by": "Administrator",
"module": "Stock",
"name": "Inventory Dimension",
Expand Down
27 changes: 27 additions & 0 deletions erpnext/stock/doctype/inventory_dimension/inventory_dimension.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,30 @@ def get_inventory_dimensions():
def delete_dimension(dimension):
doc = frappe.get_doc("Inventory Dimension", dimension)
doc.delete()


@frappe.whitelist()
def get_parent_fields(child_doctype, dimension_name):
parent_doctypes = frappe.get_all(
"DocField", fields=["parent"], filters={"options": child_doctype}
)

fields = []

fields.extend(
frappe.get_all(
"DocField",
fields=["fieldname as value", "label"],
filters={"options": dimension_name, "parent": ("in", [d.parent for d in parent_doctypes])},
)
)

fields.extend(
frappe.get_all(
"Custom Field",
fields=["fieldname as value", "label"],
filters={"options": dimension_name, "dt": ("in", [d.parent for d in parent_doctypes])},
)
)

return fields

0 comments on commit 76ae4d8

Please sign in to comment.