Skip to content

Commit

Permalink
fix: Add company filters for account
Browse files Browse the repository at this point in the history
(cherry picked from commit ecca9cb)

# Conflicts:
#	erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py
  • Loading branch information
deepeshgarg007 authored and mergify[bot] committed Aug 18, 2023
1 parent 01ae513 commit e62ffa9
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,23 @@ def validate(self):
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:
message = _("Cannot change Reference Document Type.")
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)
Expand Down
29 changes: 20 additions & 9 deletions erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -1769,10 +1769,10 @@ def test_offsetting_entries_for_accounting_dimensions(self):
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(
Expand All @@ -1781,7 +1781,7 @@ def test_offsetting_entries_for_accounting_dimensions(self):
expected_gle,
nowdate(),
voucher_type="Purchase Invoice",
check_acc_dimensions=True,
additional_columns=["branch"],
)
clear_dimension_defaults("Branch")
disable_dimension()
Expand All @@ -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 = (
Expand Down Expand Up @@ -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)

Expand All @@ -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):
Expand Down
7 changes: 6 additions & 1 deletion erpnext/accounts/general_ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand All @@ -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


Expand Down
3 changes: 2 additions & 1 deletion erpnext/accounts/report/trial_balance/test_trial_balance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
{
Expand Down

0 comments on commit e62ffa9

Please sign in to comment.