Skip to content

Commit

Permalink
fix: pos accounting dimension fieldname error (backport frappe#45899) (
Browse files Browse the repository at this point in the history
…frappe#45921)

* fix: pos accounting dimension fieldname error (frappe#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 60a5f4f)

# 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 <diptanil@frappe.io>
  • Loading branch information
2 people authored and 0xD0M1M0 committed Feb 21, 2025
1 parent 6c71abf commit 3347d72
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
29 changes: 8 additions & 21 deletions erpnext/accounts/doctype/pos_profile/pos_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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. <br>"
"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"),
)
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 3347d72

Please sign in to comment.