From 3347d72dbc7190de6bfa50de6a75bda2cd65155b Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Mon, 17 Feb 2025 14:13:34 +0530 Subject: [PATCH] fix: pos accounting dimension fieldname error (backport #45899) (#45921) * fix: pos accounting dimension fieldname error (#45899) * fix: pos accounting dimension fieldname error * fix: method to get enabled accounting dimensions * fix: fetch enabled accounting dimensions * fix: clear flags for accounting_dimensions_details on_update * refactor: validation for doctype * fix: using get_checks_for_pl_and_bs_accounts for accounting dimensions (cherry picked from commit 60a5f4f30de2e3b9f6951ae370ab25e1609fe3e0) # Conflicts: # erpnext/accounts/doctype/pos_invoice_merge_log/pos_invoice_merge_log.py * chore: resolve conflict * chore: resolve linter issue * fix: resolve linter issue * chore: resolve linter issue * chore: resolve linter issue * chore: resolve linter issue --------- Co-authored-by: Diptanil Saha --- .../accounting_dimension.py | 11 ++++--- .../pos_invoice_merge_log.py | 17 ++++++----- .../doctype/pos_profile/pos_profile.py | 29 +++++-------------- 3 files changed, 25 insertions(+), 32 deletions(-) diff --git a/erpnext/accounts/doctype/accounting_dimension/accounting_dimension.py b/erpnext/accounts/doctype/accounting_dimension/accounting_dimension.py index f8eeba8466219..81937469f21ad 100644 --- a/erpnext/accounts/doctype/accounting_dimension/accounting_dimension.py +++ b/erpnext/accounts/doctype/accounting_dimension/accounting_dimension.py @@ -41,6 +41,11 @@ def before_insert(self): self.set_fieldname_and_label() def validate(self): + self.validate_doctype() + validate_column_name(self.fieldname) + self.validate_dimension_defaults() + + def validate_doctype(self): if self.document_type in ( *core_doctypes_list, "Accounting Dimension", @@ -62,9 +67,6 @@ def validate(self): if not self.is_new(): self.validate_document_type_change() - validate_column_name(self.fieldname) - self.validate_dimension_defaults() - def validate_document_type_change(self): doctype_before_save = frappe.db.get_value("Accounting Dimension", self.name, "document_type") if doctype_before_save != self.document_type: @@ -103,6 +105,7 @@ def set_fieldname_and_label(self): def on_update(self): frappe.flags.accounting_dimensions = None + frappe.flags.accounting_dimensions_details = None def make_dimension_in_accounting_doctypes(doc, doclist=None): @@ -263,7 +266,7 @@ def get_checks_for_pl_and_bs_accounts(): frappe.flags.accounting_dimensions_details = frappe.db.sql( """SELECT p.label, p.disabled, p.fieldname, c.default_dimension, c.company, c.mandatory_for_pl, c.mandatory_for_bs FROM `tabAccounting Dimension`p ,`tabAccounting Dimension Detail` c - WHERE p.name = c.parent""", + WHERE p.name = c.parent AND p.disabled = 0""", as_dict=1, ) diff --git a/erpnext/accounts/doctype/pos_invoice_merge_log/pos_invoice_merge_log.py b/erpnext/accounts/doctype/pos_invoice_merge_log/pos_invoice_merge_log.py index 18136a033b017..a8dd4c41c92a6 100644 --- a/erpnext/accounts/doctype/pos_invoice_merge_log/pos_invoice_merge_log.py +++ b/erpnext/accounts/doctype/pos_invoice_merge_log/pos_invoice_merge_log.py @@ -12,7 +12,9 @@ from frappe.utils.background_jobs import enqueue, is_job_enqueued from frappe.utils.scheduler import is_scheduler_inactive -from erpnext.accounts.doctype.pos_profile.pos_profile import required_accounting_dimensions +from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import ( + get_checks_for_pl_and_bs_accounts, +) class POSInvoiceMergeLog(Document): @@ -292,22 +294,23 @@ def merge_pos_invoice_into(self, invoice, data): invoice.disable_rounded_total = cint( frappe.db.get_value("POS Profile", invoice.pos_profile, "disable_rounded_total") ) - accounting_dimensions = required_accounting_dimensions() + accounting_dimensions = get_checks_for_pl_and_bs_accounts() + accounting_dimensions_fields = [d.fieldname for d in accounting_dimensions] dimension_values = frappe.db.get_value( - "POS Profile", {"name": invoice.pos_profile}, accounting_dimensions, as_dict=1 + "POS Profile", {"name": invoice.pos_profile}, accounting_dimensions_fields, as_dict=1 ) for dimension in accounting_dimensions: - dimension_value = dimension_values.get(dimension) + dimension_value = dimension_values.get(dimension.fieldname) - if not dimension_value: + if not dimension_value and (dimension.mandatory_for_pl or dimension.mandatory_for_bs): frappe.throw( _("Please set Accounting Dimension {} in {}").format( - frappe.bold(frappe.unscrub(dimension)), + frappe.bold(dimension.label), frappe.get_desk_link("POS Profile", invoice.pos_profile), ) ) - invoice.set(dimension, dimension_value) + invoice.set(dimension.fieldname, dimension_value) if self.merge_invoices_based_on == "Customer Group": invoice.flags.ignore_pos_profile = True diff --git a/erpnext/accounts/doctype/pos_profile/pos_profile.py b/erpnext/accounts/doctype/pos_profile/pos_profile.py index cb9ae67fffd3d..3b5aa5f75bdf4 100644 --- a/erpnext/accounts/doctype/pos_profile/pos_profile.py +++ b/erpnext/accounts/doctype/pos_profile/pos_profile.py @@ -7,6 +7,10 @@ from frappe.model.document import Document from frappe.utils import get_link_to_form, now +from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import ( + get_checks_for_pl_and_bs_accounts, +) + class POSProfile(Document): # begin: auto-generated types @@ -70,15 +74,15 @@ def validate(self): self.validate_accounting_dimensions() def validate_accounting_dimensions(self): - acc_dim_names = required_accounting_dimensions() - for acc_dim in acc_dim_names: - if not self.get(acc_dim): + acc_dims = get_checks_for_pl_and_bs_accounts() + for acc_dim in acc_dims: + if not self.get(acc_dim.fieldname) and (acc_dim.mandatory_for_pl or acc_dim.mandatory_for_bs): frappe.throw( _( "{0} is a mandatory Accounting Dimension.
" "Please set a value for {0} in Accounting Dimensions section." ).format( - unscrub(frappe.bold(acc_dim)), + frappe.bold(acc_dim.label), ), title=_("Mandatory Accounting Dimension"), ) @@ -216,23 +220,6 @@ def get_child_nodes(group_type, root): ) -def required_accounting_dimensions(): - p = frappe.qb.DocType("Accounting Dimension") - c = frappe.qb.DocType("Accounting Dimension Detail") - - acc_dim_doc = ( - frappe.qb.from_(p) - .inner_join(c) - .on(p.name == c.parent) - .select(c.parent) - .where((c.mandatory_for_bs == 1) | (c.mandatory_for_pl == 1)) - .where(p.disabled == 0) - ).run(as_dict=1) - - acc_dim_names = [scrub(d.parent) for d in acc_dim_doc] - return acc_dim_names - - @frappe.whitelist() @frappe.validate_and_sanitize_search_inputs def pos_profile_query(doctype, txt, searchfield, start, page_len, filters):