From c816f9bd0abf065231ed053889c6220bac20477c Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Thu, 6 Mar 2025 18:12:06 +0530 Subject: [PATCH] fix: validate accounting dimension company in Journal Entry & Stock Entry (backport #46204) (#46369) fix: validate accounting dimension company in Journal Entry & Stock Entry (#46204) * fix: validate accounting dimension company in journal entry and stock entry * test: update test cases to validate company-based accounting dimension * fix(test): ensure 'Pick List' company matches 'Delivery Note' to prevent test failures * chore: remove redundant lines of code (cherry picked from commit 7b6ebad9e62b4035f705de6a82fa593d823f3f68) Co-authored-by: Bhavansathru <122002510+Bhavan23@users.noreply.github.com> --- .../accounts/doctype/journal_entry/journal_entry.py | 1 + .../doctype/journal_entry/test_journal_entry.py | 3 ++- .../test_period_closing_voucher.py | 7 +++++++ .../doctype/purchase_order/test_purchase_order.py | 11 ++++++++++- erpnext/controllers/accounts_controller.py | 2 +- .../stock/doctype/delivery_note/test_delivery_note.py | 3 +++ erpnext/stock/doctype/pick_list/pick_list.py | 1 + erpnext/stock/doctype/serial_no/test_serial_no.py | 3 +++ erpnext/stock/doctype/stock_entry/stock_entry.py | 1 + 9 files changed, 29 insertions(+), 3 deletions(-) diff --git a/erpnext/accounts/doctype/journal_entry/journal_entry.py b/erpnext/accounts/doctype/journal_entry/journal_entry.py index ef2388a7eaa..4eb74cb2b6f 100644 --- a/erpnext/accounts/doctype/journal_entry/journal_entry.py +++ b/erpnext/accounts/doctype/journal_entry/journal_entry.py @@ -141,6 +141,7 @@ class JournalEntry(AccountsController): self.validate_empty_accounts_table() self.validate_inter_company_accounts() self.validate_depr_entry_voucher_type() + self.validate_company_in_accounting_dimension() self.validate_advance_accounts() if self.docstatus == 0: diff --git a/erpnext/accounts/doctype/journal_entry/test_journal_entry.py b/erpnext/accounts/doctype/journal_entry/test_journal_entry.py index 54aa3eaf96f..e95a8eaef29 100644 --- a/erpnext/accounts/doctype/journal_entry/test_journal_entry.py +++ b/erpnext/accounts/doctype/journal_entry/test_journal_entry.py @@ -591,13 +591,14 @@ def make_journal_entry( save=True, submit=False, project=None, + company=None, ): if not cost_center: cost_center = "_Test Cost Center - _TC" jv = frappe.new_doc("Journal Entry") jv.posting_date = posting_date or nowdate() - jv.company = "_Test Company" + jv.company = company or "_Test Company" jv.user_remark = "test" jv.multi_currency = 1 jv.set( diff --git a/erpnext/accounts/doctype/period_closing_voucher/test_period_closing_voucher.py b/erpnext/accounts/doctype/period_closing_voucher/test_period_closing_voucher.py index e9d65f7f856..34e2fdd9082 100644 --- a/erpnext/accounts/doctype/period_closing_voucher/test_period_closing_voucher.py +++ b/erpnext/accounts/doctype/period_closing_voucher/test_period_closing_voucher.py @@ -27,6 +27,7 @@ class TestPeriodClosingVoucher(unittest.TestCase): account1="Cash - TPC", account2="Sales - TPC", cost_center=cost_center, + company=company, save=False, ) jv1.company = company @@ -39,6 +40,7 @@ class TestPeriodClosingVoucher(unittest.TestCase): account1="Cost of Goods Sold - TPC", account2="Cash - TPC", cost_center=cost_center, + company=company, save=False, ) jv2.company = company @@ -156,6 +158,7 @@ class TestPeriodClosingVoucher(unittest.TestCase): amount=400, cost_center=cost_center, posting_date="2021-03-15", + company=company, ) jv.company = company jv.finance_book = create_finance_book().name @@ -198,6 +201,7 @@ class TestPeriodClosingVoucher(unittest.TestCase): account1="Cash - TPC", account2="Sales - TPC", cost_center=cost_center, + company=company, save=False, ) jv1.company = company @@ -220,6 +224,7 @@ class TestPeriodClosingVoucher(unittest.TestCase): account1="Cash - TPC", account2="Sales - TPC", cost_center=cost_center1, + company=company, save=False, ) jv1.company = company @@ -232,6 +237,7 @@ class TestPeriodClosingVoucher(unittest.TestCase): account1="Cash - TPC", account2="Sales - TPC", cost_center=cost_center2, + company=company, save=False, ) jv2.company = company @@ -261,6 +267,7 @@ class TestPeriodClosingVoucher(unittest.TestCase): account1="Cash - TPC", account2="Sales - TPC", cost_center=cost_center2, + company=company, save=False, ) diff --git a/erpnext/buying/doctype/purchase_order/test_purchase_order.py b/erpnext/buying/doctype/purchase_order/test_purchase_order.py index 99ad609f8df..404c83cdb50 100644 --- a/erpnext/buying/doctype/purchase_order/test_purchase_order.py +++ b/erpnext/buying/doctype/purchase_order/test_purchase_order.py @@ -920,6 +920,7 @@ class TestPurchaseOrder(FrappeTestCase): automatically_fetch_payment_terms(enable=0) def test_internal_transfer_flow(self): + from erpnext.accounts.doctype.cost_center.test_cost_center import create_cost_center from erpnext.accounts.doctype.sales_invoice.sales_invoice import ( make_inter_company_purchase_invoice, ) @@ -935,8 +936,16 @@ class TestPurchaseOrder(FrappeTestCase): prepare_data_for_internal_transfer() supplier = "_Test Internal Supplier 2" + create_cost_center( + cost_center_name="_Test Cost Center for perpetual inventory Account", + company="_Test Company with perpetual inventory", + ) + mr = make_material_request( - qty=2, company="_Test Company with perpetual inventory", warehouse="Stores - TCP1" + qty=2, + company="_Test Company with perpetual inventory", + warehouse="Stores - TCP1", + cost_center="_Test Cost Center for perpetual inventory Account - TCP1", ) po = create_purchase_order( diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 6ed55a0b55e..d67322eae8d 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -419,7 +419,7 @@ class AccountsController(TransactionBase): .where(doc_field.fieldname == "company") ).run(as_list=True) - dimension_list = sum(dimension_list, ["Project"]) + dimension_list = sum(dimension_list, ["Project", "Cost Center"]) self.validate_company(dimension_list) for child in self.get_all_children() or []: diff --git a/erpnext/stock/doctype/delivery_note/test_delivery_note.py b/erpnext/stock/doctype/delivery_note/test_delivery_note.py index f86798d17e8..3394e0ce83a 100644 --- a/erpnext/stock/doctype/delivery_note/test_delivery_note.py +++ b/erpnext/stock/doctype/delivery_note/test_delivery_note.py @@ -10,6 +10,7 @@ from frappe.tests.utils import FrappeTestCase from frappe.utils import add_days, cstr, flt, getdate, nowdate, nowtime, today from erpnext.accounts.doctype.account.test_account import get_inventory_account +from erpnext.accounts.doctype.cost_center.test_cost_center import create_cost_center from erpnext.accounts.utils import get_balance_on from erpnext.selling.doctype.product_bundle.test_product_bundle import make_product_bundle from erpnext.selling.doctype.sales_order.test_sales_order import ( @@ -1400,6 +1401,7 @@ class TestDeliveryNote(FrappeTestCase): warehouse = "Stores - TCP1" target = "Finished Goods - TCP1" customer = create_internal_customer(represents_company=company) + create_cost_center(cost_center_name="_Test Cost Center", company=company) # average rate = 128.015 rates = [101.45, 150.46, 138.25, 121.9] @@ -1414,6 +1416,7 @@ class TestDeliveryNote(FrappeTestCase): qty=4, warehouse=warehouse, target_warehouse=target, + cost_center="_Test Cost Center - TCP1", ) self.assertFalse(frappe.db.exists("GL Entry", {"voucher_no": dn.name, "voucher_type": dn.doctype})) diff --git a/erpnext/stock/doctype/pick_list/pick_list.py b/erpnext/stock/doctype/pick_list/pick_list.py index 998f15945d3..31bff657fd1 100644 --- a/erpnext/stock/doctype/pick_list/pick_list.py +++ b/erpnext/stock/doctype/pick_list/pick_list.py @@ -1195,6 +1195,7 @@ def create_delivery_note(source_name, target_doc=None): def create_dn_wo_so(pick_list): delivery_note = frappe.new_doc("Delivery Note") + delivery_note.company = pick_list.company item_table_mapper_without_so = { "doctype": "Delivery Note Item", diff --git a/erpnext/stock/doctype/serial_no/test_serial_no.py b/erpnext/stock/doctype/serial_no/test_serial_no.py index 79291af3a10..334333f366d 100644 --- a/erpnext/stock/doctype/serial_no/test_serial_no.py +++ b/erpnext/stock/doctype/serial_no/test_serial_no.py @@ -119,6 +119,7 @@ class TestSerialNo(FrappeTestCase): serial_no=[serial_nos[0]], company="_Test Company 1", warehouse=wh, + cost_center="_Test Company 1 - _TC1", ) sn_doc.reload() @@ -154,6 +155,7 @@ class TestSerialNo(FrappeTestCase): serial_no=[serial_nos[0]], company="_Test Company 1", warehouse=wh, + cost_center="_Test Company 1 - _TC1", ) # Delivery from second company @@ -163,6 +165,7 @@ class TestSerialNo(FrappeTestCase): serial_no=[serial_nos[0]], company="_Test Company 1", warehouse=wh, + cost_center="_Test Company 1 - _TC1", ) sn_doc.reload() diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.py b/erpnext/stock/doctype/stock_entry/stock_entry.py index 65db490e7b5..7ad9498c88f 100644 --- a/erpnext/stock/doctype/stock_entry/stock_entry.py +++ b/erpnext/stock/doctype/stock_entry/stock_entry.py @@ -209,6 +209,7 @@ class StockEntry(StockController): self.validate_bom() self.set_process_loss_qty() self.validate_purchase_order() + self.validate_company_in_accounting_dimension() if self.purpose in ("Manufacture", "Repack"): self.mark_finished_and_scrap_items()