Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: First preference to parent cost center rather than round off cost center #30754

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -1369,7 +1369,9 @@ def make_gle_for_rounding_adjustment(self, gl_entries):
if (
not self.is_internal_transfer() and self.rounding_adjustment and self.base_rounding_adjustment
):
round_off_account, round_off_cost_center = get_round_off_account_and_cost_center(self.company)
round_off_account, round_off_cost_center = get_round_off_account_and_cost_center(
self.company, "Purchase Invoice", self.name
)

gl_entries.append(
self.get_gl_dict(
Expand Down
4 changes: 3 additions & 1 deletion erpnext/accounts/doctype/sales_invoice/sales_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -1466,7 +1466,9 @@ def make_gle_for_rounding_adjustment(self, gl_entries):
and self.base_rounding_adjustment
and not self.is_internal_transfer()
):
round_off_account, round_off_cost_center = get_round_off_account_and_cost_center(self.company)
round_off_account, round_off_cost_center = get_round_off_account_and_cost_center(
self.company, "Sales Invoice", self.name
)

gl_entries.append(
self.get_gl_dict(
Expand Down
23 changes: 23 additions & 0 deletions erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -1977,6 +1977,13 @@ def test_rounding_adjustment_2(self):
self.assertEqual(expected_values[gle.account][2], gle.credit)

def test_rounding_adjustment_3(self):
from erpnext.accounts.doctype.accounting_dimension.test_accounting_dimension import (
create_dimension,
disable_dimension,
)

create_dimension()

si = create_sales_invoice(do_not_save=True)
si.items = []
for d in [(1122, 2), (1122.01, 1), (1122.01, 1)]:
Expand Down Expand Up @@ -2004,6 +2011,10 @@ def test_rounding_adjustment_3(self):
"included_in_print_rate": 1,
},
)

si.cost_center = "_Test Cost Center 2 - _TC"
si.location = "Block 1"

si.save()
si.submit()
self.assertEqual(si.net_total, 4007.16)
Expand Down Expand Up @@ -2039,6 +2050,18 @@ def test_rounding_adjustment_3(self):

self.assertEqual(debit_credit_diff, 0)

round_off_gle = frappe.db.get_value(
"GL Entry",
{"voucher_type": "Sales Invoice", "voucher_no": si.name, "account": "Round Off - _TC"},
["cost_center", "location"],
as_dict=1,
)

self.assertEqual(round_off_gle.cost_center, "_Test Cost Center 2 - _TC")
self.assertEqual(round_off_gle.location, "Block 1")

disable_dimension()

def test_sales_invoice_with_shipping_rule(self):
from erpnext.accounts.doctype.shipping_rule.test_shipping_rule import create_shipping_rule

Expand Down
33 changes: 31 additions & 2 deletions erpnext/accounts/general_ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ def raise_debit_credit_not_equal_error(debit_credit_diff, voucher_type, voucher_

def make_round_off_gle(gl_map, debit_credit_diff, precision):
round_off_account, round_off_cost_center = get_round_off_account_and_cost_center(
gl_map[0].company
gl_map[0].company, gl_map[0].voucher_type, gl_map[0].voucher_no
)
round_off_account_exists = False
round_off_gle = frappe._dict()
Expand Down Expand Up @@ -392,14 +392,43 @@ def make_round_off_gle(gl_map, debit_credit_diff, precision):
}
)

update_accounting_dimensions(round_off_gle)

if not round_off_account_exists:
gl_map.append(round_off_gle)


def get_round_off_account_and_cost_center(company):
def update_accounting_dimensions(round_off_gle):
dimensions = get_accounting_dimensions()
meta = frappe.get_meta(round_off_gle["voucher_type"])
has_all_dimensions = True

for dimension in dimensions:
if not meta.has_field(dimension):
has_all_dimensions = False

if dimensions and has_all_dimensions:
dimension_values = frappe.db.get_value(
round_off_gle["voucher_type"], round_off_gle["voucher_no"], dimensions, as_dict=1
)

for dimension in dimensions:
round_off_gle[dimension] = dimension_values.get(dimension)


def get_round_off_account_and_cost_center(company, voucher_type, voucher_no):
round_off_account, round_off_cost_center = frappe.get_cached_value(
"Company", company, ["round_off_account", "round_off_cost_center"]
) or [None, None]

meta = frappe.get_meta(voucher_type)

# Give first preference to parent cost center for round off GLE
if meta.has_field("cost_center"):
parent_cost_center = frappe.db.get_value(voucher_type, voucher_no, "cost_center")
if parent_cost_center:
round_off_cost_center = parent_cost_center

if not round_off_account:
frappe.throw(_("Please mention Round Off Account in Company"))

Expand Down