From 83c46085fbb56e1cfb2981a7d57b8ed313b4979a Mon Sep 17 00:00:00 2001 From: Nikhil Kothari Date: Wed, 7 Jun 2023 14:14:57 +0530 Subject: [PATCH 1/5] feat: added support for mandatory dimensions per account --- .../accounting_dimension_filter.js | 10 ++++ .../accounting_dimension_filter.json | 60 ++++++++----------- .../accounting_dimension_filter.py | 7 ++- 3 files changed, 38 insertions(+), 39 deletions(-) diff --git a/erpnext/accounts/doctype/accounting_dimension_filter/accounting_dimension_filter.js b/erpnext/accounts/doctype/accounting_dimension_filter/accounting_dimension_filter.js index 8a6b021b8a..6f0b6fcd91 100644 --- a/erpnext/accounts/doctype/accounting_dimension_filter/accounting_dimension_filter.js +++ b/erpnext/accounts/doctype/accounting_dimension_filter/accounting_dimension_filter.js @@ -68,6 +68,16 @@ frappe.ui.form.on('Accounting Dimension Filter', { frm.refresh_field("dimensions"); frm.trigger('setup_filters'); }, + apply_restriction_on_values: function(frm) { + /** If restriction on values is not applied, we should set "allow_or_restrict" to "Restrict" with an empty allowed dimension table. + * Hence it's not "restricted" on any value. + */ + if (!frm.doc.apply_restriction_on_values) { + frm.set_value("allow_or_restrict", "Restrict"); + frm.clear_table("dimensions"); + frm.refresh_field("dimensions"); + } + } }); frappe.ui.form.on('Allowed Dimension', { diff --git a/erpnext/accounts/doctype/accounting_dimension_filter/accounting_dimension_filter.json b/erpnext/accounts/doctype/accounting_dimension_filter/accounting_dimension_filter.json index 0f3fbc0b8d..1f320b0a1a 100644 --- a/erpnext/accounts/doctype/accounting_dimension_filter/accounting_dimension_filter.json +++ b/erpnext/accounts/doctype/accounting_dimension_filter/accounting_dimension_filter.json @@ -10,6 +10,7 @@ "disabled", "column_break_2", "company", + "apply_restriction_on_values", "allow_or_restrict", "section_break_4", "accounts", @@ -24,94 +25,80 @@ "fieldtype": "Select", "in_list_view": 1, "label": "Accounting Dimension", - "reqd": 1, - "show_days": 1, - "show_seconds": 1 + "reqd": 1 }, { "fieldname": "column_break_2", - "fieldtype": "Column Break", - "show_days": 1, - "show_seconds": 1 + "fieldtype": "Column Break" }, { "fieldname": "section_break_4", "fieldtype": "Section Break", - "hide_border": 1, - "show_days": 1, - "show_seconds": 1 + "hide_border": 1 }, { "fieldname": "column_break_6", - "fieldtype": "Column Break", - "show_days": 1, - "show_seconds": 1 + "fieldtype": "Column Break" }, { + "depends_on": "eval:doc.apply_restriction_on_values == 1;", "fieldname": "allow_or_restrict", "fieldtype": "Select", "label": "Allow Or Restrict Dimension", "options": "Allow\nRestrict", - "reqd": 1, - "show_days": 1, - "show_seconds": 1 + "reqd": 1 }, { "fieldname": "accounts", "fieldtype": "Table", "label": "Applicable On Account", "options": "Applicable On Account", - "reqd": 1, - "show_days": 1, - "show_seconds": 1 + "reqd": 1 }, { - "depends_on": "eval:doc.accounting_dimension", + "depends_on": "eval:doc.accounting_dimension && doc.apply_restriction_on_values", "fieldname": "dimensions", "fieldtype": "Table", "label": "Applicable Dimension", - "options": "Allowed Dimension", - "reqd": 1, - "show_days": 1, - "show_seconds": 1 + "mandatory_depends_on": "eval:doc.apply_restriction_on_values == 1;", + "options": "Allowed Dimension" }, { "default": "0", "fieldname": "disabled", "fieldtype": "Check", - "label": "Disabled", - "show_days": 1, - "show_seconds": 1 + "label": "Disabled" }, { "fieldname": "company", "fieldtype": "Link", "label": "Company", "options": "Company", - "reqd": 1, - "show_days": 1, - "show_seconds": 1 + "reqd": 1 }, { "fieldname": "dimension_filter_help", "fieldtype": "HTML", - "label": "Dimension Filter Help", - "show_days": 1, - "show_seconds": 1 + "label": "Dimension Filter Help" }, { "fieldname": "section_break_10", - "fieldtype": "Section Break", - "show_days": 1, - "show_seconds": 1 + "fieldtype": "Section Break" + }, + { + "default": "0", + "fieldname": "apply_restriction_on_values", + "fieldtype": "Check", + "label": "Apply restriction on dimension values" } ], "index_web_pages_for_search": 1, "links": [], - "modified": "2021-02-03 12:04:58.678402", + "modified": "2023-06-07 13:41:36.736175", "modified_by": "Administrator", "module": "Accounts", "name": "Accounting Dimension Filter", + "naming_rule": "Expression", "owner": "Administrator", "permissions": [ { @@ -154,5 +141,6 @@ "quick_entry": 1, "sort_field": "modified", "sort_order": "DESC", + "states": [], "track_changes": 1 } \ No newline at end of file diff --git a/erpnext/accounts/doctype/accounting_dimension_filter/accounting_dimension_filter.py b/erpnext/accounts/doctype/accounting_dimension_filter/accounting_dimension_filter.py index 80f736fa5b..c0573a703c 100644 --- a/erpnext/accounts/doctype/accounting_dimension_filter/accounting_dimension_filter.py +++ b/erpnext/accounts/doctype/accounting_dimension_filter/accounting_dimension_filter.py @@ -44,12 +44,12 @@ def get_dimension_filter_map(): a.applicable_on_account, d.dimension_value, p.accounting_dimension, p.allow_or_restrict, a.is_mandatory FROM - `tabApplicable On Account` a, `tabAllowed Dimension` d, + `tabApplicable On Account` a, `tabAccounting Dimension Filter` p + LEFT JOIN `tabAllowed Dimension` d ON d.parent = p.name WHERE p.name = a.parent AND p.disabled = 0 - AND p.name = d.parent """, as_dict=1, ) @@ -76,4 +76,5 @@ def build_map(map_object, dimension, account, filter_value, allow_or_restrict, i (dimension, account), {"allowed_dimensions": [], "is_mandatory": is_mandatory, "allow_or_restrict": allow_or_restrict}, ) - map_object[(dimension, account)]["allowed_dimensions"].append(filter_value) + if filter_value: + map_object[(dimension, account)]["allowed_dimensions"].append(filter_value) From e1116bbbbb6987e37d940f1f00e3e334ed0101d0 Mon Sep 17 00:00:00 2001 From: Nikhil Kothari Date: Wed, 7 Jun 2023 14:20:42 +0530 Subject: [PATCH 2/5] fix: added server side check for allow/restrict --- .../accounting_dimension_filter.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/erpnext/accounts/doctype/accounting_dimension_filter/accounting_dimension_filter.py b/erpnext/accounts/doctype/accounting_dimension_filter/accounting_dimension_filter.py index c0573a703c..2a6c76dd4a 100644 --- a/erpnext/accounts/doctype/accounting_dimension_filter/accounting_dimension_filter.py +++ b/erpnext/accounts/doctype/accounting_dimension_filter/accounting_dimension_filter.py @@ -8,6 +8,13 @@ from frappe.model.document import Document class AccountingDimensionFilter(Document): + + def before_save(self): + # If restriction is not applied on values, then remove all the dimensions and set allow_or_restrict to Restrict + if not self.apply_restriction_on_values: + self.allow_or_restrict = "Restrict" + self.set('dimensions', []) + def validate(self): self.validate_applicable_accounts() From 83a75844755c141fca8e3885c904581d07196057 Mon Sep 17 00:00:00 2001 From: Nikhil Kothari Date: Wed, 7 Jun 2023 15:00:05 +0530 Subject: [PATCH 3/5] fix: tests now create filters with checkbox enabled --- .../accounting_dimension_filter.json | 4 ++-- .../test_accounting_dimension_filter.py | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/erpnext/accounts/doctype/accounting_dimension_filter/accounting_dimension_filter.json b/erpnext/accounts/doctype/accounting_dimension_filter/accounting_dimension_filter.json index 1f320b0a1a..2bd6c12a0a 100644 --- a/erpnext/accounts/doctype/accounting_dimension_filter/accounting_dimension_filter.json +++ b/erpnext/accounts/doctype/accounting_dimension_filter/accounting_dimension_filter.json @@ -86,7 +86,7 @@ "fieldtype": "Section Break" }, { - "default": "0", + "default": "1", "fieldname": "apply_restriction_on_values", "fieldtype": "Check", "label": "Apply restriction on dimension values" @@ -94,7 +94,7 @@ ], "index_web_pages_for_search": 1, "links": [], - "modified": "2023-06-07 13:41:36.736175", + "modified": "2023-06-07 14:59:41.869117", "modified_by": "Administrator", "module": "Accounts", "name": "Accounting Dimension Filter", diff --git a/erpnext/accounts/doctype/accounting_dimension_filter/test_accounting_dimension_filter.py b/erpnext/accounts/doctype/accounting_dimension_filter/test_accounting_dimension_filter.py index f13f2f9f27..6aba2ab253 100644 --- a/erpnext/accounts/doctype/accounting_dimension_filter/test_accounting_dimension_filter.py +++ b/erpnext/accounts/doctype/accounting_dimension_filter/test_accounting_dimension_filter.py @@ -64,6 +64,7 @@ def create_accounting_dimension_filter(): "accounting_dimension": "Cost Center", "allow_or_restrict": "Allow", "company": "_Test Company", + "apply_restriction_on_values": 1, "accounts": [ { "applicable_on_account": "Sales - _TC", @@ -85,6 +86,7 @@ def create_accounting_dimension_filter(): "doctype": "Accounting Dimension Filter", "accounting_dimension": "Department", "allow_or_restrict": "Allow", + "apply_restriction_on_values": 1, "company": "_Test Company", "accounts": [{"applicable_on_account": "Sales - _TC", "is_mandatory": 1}], "dimensions": [{"accounting_dimension": "Department", "dimension_value": "Accounts - _TC"}], From bcff4b0e5afea4d1abd17af8ef2138fa0a93bdd2 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Sun, 25 Jun 2023 16:03:58 +0530 Subject: [PATCH 4/5] chore: linting issues --- .../accounting_dimension_filter/accounting_dimension_filter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/accounting_dimension_filter/accounting_dimension_filter.py b/erpnext/accounts/doctype/accounting_dimension_filter/accounting_dimension_filter.py index 2a6c76dd4a..a79f13df05 100644 --- a/erpnext/accounts/doctype/accounting_dimension_filter/accounting_dimension_filter.py +++ b/erpnext/accounts/doctype/accounting_dimension_filter/accounting_dimension_filter.py @@ -13,7 +13,7 @@ class AccountingDimensionFilter(Document): # If restriction is not applied on values, then remove all the dimensions and set allow_or_restrict to Restrict if not self.apply_restriction_on_values: self.allow_or_restrict = "Restrict" - self.set('dimensions', []) + self.set("dimensions", []) def validate(self): self.validate_applicable_accounts() From 63b126967ea685e9d2cecc525b97864b91b48363 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Sun, 25 Jun 2023 16:24:22 +0530 Subject: [PATCH 5/5] chore: Linting Issues --- .../accounting_dimension_filter/accounting_dimension_filter.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/erpnext/accounts/doctype/accounting_dimension_filter/accounting_dimension_filter.py b/erpnext/accounts/doctype/accounting_dimension_filter/accounting_dimension_filter.py index a79f13df05..de1b82c1d5 100644 --- a/erpnext/accounts/doctype/accounting_dimension_filter/accounting_dimension_filter.py +++ b/erpnext/accounts/doctype/accounting_dimension_filter/accounting_dimension_filter.py @@ -8,13 +8,12 @@ from frappe.model.document import Document class AccountingDimensionFilter(Document): - def before_save(self): # If restriction is not applied on values, then remove all the dimensions and set allow_or_restrict to Restrict if not self.apply_restriction_on_values: self.allow_or_restrict = "Restrict" self.set("dimensions", []) - + def validate(self): self.validate_applicable_accounts()