fix: Add company filters for account

(cherry picked from commit ecca9cb023)

# Conflicts:
#	erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py
This commit is contained in:
Deepesh Garg
2023-07-29 11:52:54 +05:30
committed by Mergify
parent 01ae513f70
commit e62ffa990d
5 changed files with 49 additions and 11 deletions

View File

@@ -15,6 +15,17 @@ frappe.ui.form.on('Accounting Dimension', {
};
});
frm.set_query("offsetting_account", "dimension_defaults", function(doc, cdt, cdn) {
let d = locals[cdt][cdn];
return {
filters: {
company: d.company,
root_type: ["in", ["Asset", "Liability"]],
is_group: 0
}
}
});
if (!frm.is_new()) {
frm.add_custom_button(__('Show {0}', [frm.doc.document_type]), function () {
frappe.set_route("List", frm.doc.document_type);

View File

@@ -39,6 +39,8 @@ class AccountingDimension(Document):
if not self.is_new():
self.validate_document_type_change()
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:
@@ -46,6 +48,14 @@ class AccountingDimension(Document):
message += _("Please create a new Accounting Dimension if required.")
frappe.throw(message)
def validate_dimension_defaults(self):
companies = []
for default in self.get("dimension_defaults"):
if default.company not in companies:
companies.append(default.company)
else:
frappe.throw(_("Company {0} is added more than once").format(frappe.bold(default.company)))
def after_insert(self):
if frappe.flags.in_test:
make_dimension_in_accounting_doctypes(doc=self)

View File

@@ -1769,10 +1769,10 @@ class TestPurchaseInvoice(unittest.TestCase, StockTestMixin):
pi.submit()
expected_gle = [
["_Test Account Cost for Goods Sold - _TC", 1000, 0.0, nowdate(), {"branch": branch2.branch}],
["Creditors - _TC", 0.0, 1000, nowdate(), {"branch": branch1.branch}],
["Offsetting - _TC", 1000, 0.0, nowdate(), {"branch": branch1.branch}],
["Offsetting - _TC", 0.0, 1000, nowdate(), {"branch": branch2.branch}],
["_Test Account Cost for Goods Sold - _TC", 1000, 0.0, nowdate(), branch2.branch],
["Creditors - _TC", 0.0, 1000, nowdate(), branch1.branch],
["Offsetting - _TC", 1000, 0.0, nowdate(), branch1.branch],
["Offsetting - _TC", 0.0, 1000, nowdate(), branch2.branch],
]
check_gl_entries(
@@ -1781,7 +1781,7 @@ class TestPurchaseInvoice(unittest.TestCase, StockTestMixin):
expected_gle,
nowdate(),
voucher_type="Purchase Invoice",
check_acc_dimensions=True,
additional_columns=["branch"],
)
clear_dimension_defaults("Branch")
disable_dimension()
@@ -1804,7 +1804,7 @@ def check_gl_entries(
expected_gle,
posting_date,
voucher_type="Purchase Invoice",
check_acc_dimensions=False,
additional_columns=None,
):
gl = frappe.qb.DocType("GL Entry")
query = (
@@ -1869,10 +1869,18 @@ def check_gl_entries(doc, voucher_no, expected_gle, posting_date):
as_dict=1,
)
<<<<<<< HEAD
<<<<<<< HEAD
=======
if check_acc_dimensions:
for col in list(expected_gle[0][4].keys()):
query = query.select(col)
=======
if additional_columns:
for col in additional_columns:
query = query.select(gl[col])
>>>>>>> ecca9cb023 (fix: Add company filters for account)
gl_entries = query.run(as_dict=True)
>>>>>>> 77deac4fb9 (test: PI offsetting entry for accounting dimension)
@@ -1881,9 +1889,12 @@ def check_gl_entries(doc, voucher_no, expected_gle, posting_date):
doc.assertEqual(expected_gle[i][1], gle.debit)
doc.assertEqual(expected_gle[i][2], gle.credit)
doc.assertEqual(getdate(expected_gle[i][3]), gle.posting_date)
if check_acc_dimensions:
for acc_dimension in expected_gle[i][4]:
doc.assertEqual(expected_gle[i][4][acc_dimension], gle[acc_dimension])
if additional_columns:
j = 4
for col in additional_columns:
doc.assertEqual(expected_gle[i][j], gle[col])
j += 1
def create_tax_witholding_category(category_name, company, account):

View File

@@ -27,8 +27,8 @@ def make_gl_entries(
from_repost=False,
):
if gl_map:
make_acc_dimensions_offsetting_entry(gl_map)
if not cancel:
make_acc_dimensions_offsetting_entry(gl_map)
validate_accounting_period(gl_map)
validate_disabled_accounts(gl_map)
gl_map = process_gl_map(gl_map, merge_entries)
@@ -61,6 +61,7 @@ def make_acc_dimensions_offsetting_entry(gl_map):
return
offsetting_entries = []
for gle in gl_map:
for dimension in accounting_dimensions_to_offset:
offsetting_entry = gle.copy()
@@ -79,12 +80,14 @@ def make_acc_dimensions_offsetting_entry(gl_map):
)
offsetting_entry["against_voucher_type"] = None
offsetting_entries.append(offsetting_entry)
gl_map += offsetting_entries
def get_accounting_dimensions_for_offsetting_entry(gl_map, company):
acc_dimension = frappe.qb.DocType("Accounting Dimension")
dimension_detail = frappe.qb.DocType("Accounting Dimension Detail")
acc_dimensions = (
frappe.qb.from_(acc_dimension)
.inner_join(dimension_detail)
@@ -96,11 +99,13 @@ def get_accounting_dimensions_for_offsetting_entry(gl_map, company):
& (dimension_detail.automatically_post_balancing_accounting_entry == 1)
)
).run(as_dict=True)
accounting_dimensions_to_offset = []
for acc_dimension in acc_dimensions:
values = set([entry.get(acc_dimension.fieldname) for entry in gl_map])
if len(values) > 1:
accounting_dimensions_to_offset.append(acc_dimension)
return accounting_dimensions_to_offset

View File

@@ -91,7 +91,8 @@ def create_accounting_dimension(**args):
accounting_dimension = frappe.new_doc("Accounting Dimension")
accounting_dimension.document_type = document_type
accounting_dimension.insert()
accounting_dimension.save()
accounting_dimension.set("dimension_defaults", [])
accounting_dimension.append(
"dimension_defaults",
{