Skip to content

Commit 7b0d34e

Browse files
fix: inventory dimensions should not be mandatory unnecesarily (backport #54064) (#54134)
* fix: inventory dimensions should not be mandatory unnecesarily (#54064) (cherry picked from commit 6e44b89) # Conflicts: # erpnext/patches.txt * chore: resolve conflicts --------- Co-authored-by: Mihir Kandoi <[email protected]>
1 parent 856ba24 commit 7b0d34e

5 files changed

Lines changed: 86 additions & 19 deletions

File tree

erpnext/patches.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,3 +473,4 @@ erpnext.patches.v16_0.enable_serial_batch_setting
473473
erpnext.patches.v16_0.update_requested_qty_packed_item
474474
erpnext.patches.v16_0.remove_payables_receivables_workspace
475475
erpnext.patches.v16_0.co_by_product_patch
476+
erpnext.patches.v16_0.depends_on_inv_dimensions
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import frappe
2+
3+
4+
def get_inventory_dimensions():
5+
return frappe.get_all(
6+
"Inventory Dimension",
7+
fields=[
8+
"target_fieldname as fieldname",
9+
"source_fieldname",
10+
"reference_document as doctype",
11+
"reqd",
12+
"mandatory_depends_on",
13+
],
14+
order_by="creation",
15+
distinct=True,
16+
)
17+
18+
19+
def get_display_depends_on(doctype):
20+
if doctype not in [
21+
"Stock Entry Detail",
22+
"Sales Invoice Item",
23+
"Delivery Note Item",
24+
"Purchase Invoice Item",
25+
"Purchase Receipt Item",
26+
]:
27+
return
28+
29+
display_depends_on = ""
30+
31+
if doctype in ["Purchase Invoice Item", "Purchase Receipt Item"]:
32+
display_depends_on = "eval:parent.is_internal_supplier == 1"
33+
elif doctype != "Stock Entry Detail":
34+
display_depends_on = "eval:parent.is_internal_customer == 1"
35+
elif doctype == "Stock Entry Detail":
36+
display_depends_on = "eval:doc.t_warehouse"
37+
38+
return display_depends_on
39+
40+
41+
def execute():
42+
for dimension in get_inventory_dimensions():
43+
frappe.set_value(
44+
"Custom Field",
45+
{"fieldname": dimension.source_fieldname, "dt": "Stock Entry Detail"},
46+
"depends_on",
47+
"eval:doc.s_warehouse",
48+
)
49+
frappe.set_value(
50+
"Custom Field",
51+
{"fieldname": dimension.source_fieldname, "dt": "Stock Entry Detail", "reqd": 1},
52+
{"mandatory_depends_on": "eval:doc.s_warehouse", "reqd": 0},
53+
)
54+
frappe.set_value(
55+
"Custom Field",
56+
{
57+
"fieldname": f"to_{dimension.fieldname}",
58+
"dt": "Stock Entry Detail",
59+
"depends_on": "eval:parent.purpose != 'Material Issue'",
60+
},
61+
"depends_on",
62+
"eval:doc.t_warehouse",
63+
)
64+
if display_depends_on := get_display_depends_on(dimension.doctype):
65+
frappe.set_value(
66+
"Custom Field",
67+
{"fieldname": dimension.fieldname, "dt": dimension.doctype},
68+
"mandatory_depends_on",
69+
display_depends_on if dimension.reqd else dimension.mandatory_depends_on,
70+
)

erpnext/stock/doctype/inventory_dimension/inventory_dimension.json

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
"field_order": [
99
"dimension_details_tab",
1010
"dimension_name",
11-
"reference_document",
1211
"column_break_4",
13-
"disabled",
12+
"reference_document",
1413
"field_mapping_section",
1514
"source_fieldname",
1615
"column_break_9",
@@ -93,12 +92,6 @@
9392
"fieldtype": "Check",
9493
"label": "Apply to All Inventory Documents"
9594
},
96-
{
97-
"default": "0",
98-
"fieldname": "disabled",
99-
"fieldtype": "Check",
100-
"label": "Disabled"
101-
},
10295
{
10396
"fieldname": "target_fieldname",
10497
"fieldtype": "Data",
@@ -159,6 +152,7 @@
159152
"label": "Conditional Rule Examples"
160153
},
161154
{
155+
"depends_on": "eval:!doc.apply_to_all_doctypes",
162156
"description": "To apply condition on parent field use parent.field_name and to apply condition on child table use doc.field_name. Here field_name could be based on the actual column name of the respective field.",
163157
"fieldname": "mandatory_depends_on",
164158
"fieldtype": "Small Text",
@@ -188,7 +182,7 @@
188182
],
189183
"index_web_pages_for_search": 1,
190184
"links": [],
191-
"modified": "2025-07-07 15:51:29.329064",
185+
"modified": "2026-04-08 10:10:16.884388",
192186
"modified_by": "Administrator",
193187
"module": "Stock",
194188
"name": "Inventory Dimension",

erpnext/stock/doctype/inventory_dimension/inventory_dimension.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ class InventoryDimension(Document):
3232
apply_to_all_doctypes: DF.Check
3333
condition: DF.Code | None
3434
dimension_name: DF.Data
35-
disabled: DF.Check
3635
document_type: DF.Link | None
3736
fetch_from_parent: DF.Literal[None]
3837
istable: DF.Check
@@ -76,7 +75,6 @@ def do_not_update_document(self):
7675

7776
old_doc = self._doc_before_save
7877
allow_to_edit_fields = [
79-
"disabled",
8078
"fetch_from_parent",
8179
"type_of_transaction",
8280
"condition",
@@ -120,6 +118,7 @@ def delete_custom_fields(self):
120118
def reset_value(self):
121119
if self.apply_to_all_doctypes:
122120
self.type_of_transaction = ""
121+
self.mandatory_depends_on = ""
123122

124123
self.istable = 0
125124
for field in ["document_type", "condition"]:
@@ -184,8 +183,12 @@ def get_dimension_fields(self, doctype=None):
184183
label=_(label),
185184
depends_on="eval:doc.s_warehouse" if doctype == "Stock Entry Detail" else "",
186185
search_index=1,
187-
reqd=self.reqd,
188-
mandatory_depends_on=self.mandatory_depends_on,
186+
reqd=1
187+
if self.reqd and not self.mandatory_depends_on and doctype != "Stock Entry Detail"
188+
else 0,
189+
mandatory_depends_on="eval:doc.s_warehouse"
190+
if self.reqd and doctype == "Stock Entry Detail"
191+
else self.mandatory_depends_on,
189192
),
190193
]
191194

@@ -296,12 +299,13 @@ def add_transfer_field(self, doctype, dimension_fields):
296299
options=self.reference_document,
297300
label=label,
298301
depends_on=display_depends_on,
302+
mandatory_depends_on=display_depends_on if self.reqd else self.mandatory_depends_on,
299303
),
300304
]
301305
)
302306

303307

304-
def field_exists(doctype, fieldname) -> str or None:
308+
def field_exists(doctype, fieldname) -> str | None:
305309
return frappe.db.get_value("DocField", {"parent": doctype, "fieldname": fieldname}, "name")
306310

307311

@@ -372,7 +376,6 @@ def get_document_wise_inventory_dimensions(doctype) -> dict:
372376
"type_of_transaction",
373377
"fetch_from_parent",
374378
],
375-
filters={"disabled": 0},
376379
or_filters={"document_type": doctype, "apply_to_all_doctypes": 1},
377380
)
378381

@@ -389,7 +392,6 @@ def get_inventory_dimensions():
389392
"validate_negative_stock",
390393
"name as dimension_name",
391394
],
392-
filters={"disabled": 0},
393395
order_by="creation",
394396
distinct=True,
395397
)

erpnext/stock/doctype/inventory_dimension/test_inventory_dimension.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,17 +211,17 @@ def test_check_mandatory_dimensions(self):
211211
doc = create_inventory_dimension(
212212
reference_document="Pallet",
213213
type_of_transaction="Outward",
214-
dimension_name="Pallet",
214+
dimension_name="Pallet 75",
215215
apply_to_all_doctypes=0,
216-
document_type="Stock Entry Detail",
216+
document_type="Delivery Note Item",
217217
)
218218

219219
doc.reqd = 1
220220
doc.save()
221221

222222
self.assertTrue(
223223
frappe.db.get_value(
224-
"Custom Field", {"fieldname": "pallet", "dt": "Stock Entry Detail", "reqd": 1}, "name"
224+
"Custom Field", {"fieldname": "pallet_75", "dt": "Delivery Note Item", "reqd": 1}, "name"
225225
)
226226
)
227227

0 commit comments

Comments
 (0)