From c6e0eb6d179c11a8765eff40f1c94e259d509ed5 Mon Sep 17 00:00:00 2001 From: Sudharsanan11 Date: Wed, 18 Feb 2026 23:59:42 +0530 Subject: [PATCH 1/3] fix(stock): validate company for receipt documents and expense accounts (cherry picked from commit 15dfc08a3181063f18147af0ad6170393531cfae) --- .../landed_cost_voucher.py | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.py b/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.py index 9d23eda3755..f57a8792598 100644 --- a/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.py +++ b/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.py @@ -75,6 +75,7 @@ class LandedCostVoucher(Document): self.check_mandatory() self.validate_receipt_documents() self.validate_line_items() + self.validate_expense_accounts() init_landed_taxes_and_totals(self) self.set_total_taxes_and_charges() if not self.get("items"): @@ -116,11 +117,27 @@ class LandedCostVoucher(Document): receipt_documents = [] for d in self.get("purchase_receipts"): - docstatus = frappe.db.get_value(d.receipt_document_type, d.receipt_document, "docstatus") + docstatus, company = frappe.get_cached_value( + d.receipt_document_type, d.receipt_document, ["docstatus", "company"] + ) if docstatus != 1: msg = f"Row {d.idx}: {d.receipt_document_type} {frappe.bold(d.receipt_document)} must be submitted" frappe.throw(_(msg), title=_("Invalid Document")) + if company != self.company: + frappe.throw( + _( + "Row {0}: {1} {2} is linked to company {3}. Please select a document belonging to company {4}." + ).format( + d.idx, + d.receipt_document_type, + frappe.bold(d.receipt_document), + frappe.bold(company), + frappe.bold(self.company), + ), + title=_("Incorrect Company"), + ) + if d.receipt_document_type == "Purchase Invoice": update_stock = frappe.db.get_value( d.receipt_document_type, d.receipt_document, "update_stock" @@ -152,6 +169,23 @@ class LandedCostVoucher(Document): _("Row {0}: Cost center is required for an item {1}").format(item.idx, item.item_code) ) + def validate_expense_accounts(self): + for t in self.taxes: + company = frappe.get_cached_value("Account", t.expense_account, "company") + + if company != self.company: + frappe.throw( + _( + "Row {0}: Expense Account {1} is linked to company {2}. Please select an account belonging to company {3}." + ).format( + t.idx, + frappe.bold(t.expense_account), + frappe.bold(company), + frappe.bold(self.company), + ), + title=_("Incorrect Account"), + ) + def set_total_taxes_and_charges(self): self.total_taxes_and_charges = sum(flt(d.base_amount) for d in self.get("taxes")) From 25c134d11ce6a50b1786b4f5194340581e2aec2b Mon Sep 17 00:00:00 2001 From: Sudharsanan11 Date: Fri, 20 Feb 2026 12:15:40 +0530 Subject: [PATCH 2/3] fix: set company based expense account (cherry picked from commit d54d0c25a23ac65c6fe1fd971bdcc0c1cab5ca1a) --- .../landed_cost_voucher/test_landed_cost_voucher.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/erpnext/stock/doctype/landed_cost_voucher/test_landed_cost_voucher.py b/erpnext/stock/doctype/landed_cost_voucher/test_landed_cost_voucher.py index 586e95f3ab2..440d828cd61 100644 --- a/erpnext/stock/doctype/landed_cost_voucher/test_landed_cost_voucher.py +++ b/erpnext/stock/doctype/landed_cost_voucher/test_landed_cost_voucher.py @@ -1260,6 +1260,7 @@ def make_landed_cost_voucher(**args): lcv = frappe.new_doc("Landed Cost Voucher") lcv.company = args.company or "_Test Company" lcv.distribute_charges_based_on = args.distribute_charges_based_on or "Amount" + expense_account = get_expense_account(args.company or "_Test Company") lcv.set( "purchase_receipts", @@ -1280,7 +1281,7 @@ def make_landed_cost_voucher(**args): [ { "description": "Shipping Charges", - "expense_account": args.expense_account or "Expenses Included In Valuation - TCP1", + "expense_account": args.expense_account or expense_account, "amount": args.charges, } ], @@ -1300,6 +1301,7 @@ def create_landed_cost_voucher(receipt_document_type, receipt_document, company, lcv = frappe.new_doc("Landed Cost Voucher") lcv.company = company lcv.distribute_charges_based_on = "Amount" + expense_account = get_expense_account(company) lcv.set( "purchase_receipts", @@ -1319,7 +1321,7 @@ def create_landed_cost_voucher(receipt_document_type, receipt_document, company, [ { "description": "Insurance Charges", - "expense_account": "Expenses Included In Valuation - TCP1", + "expense_account": expense_account, "amount": charges, } ], @@ -1334,6 +1336,11 @@ def create_landed_cost_voucher(receipt_document_type, receipt_document, company, return lcv +def get_expense_account(company): + company_abbr = frappe.get_cached_value("Company", company, "abbr") + return f"Expenses Included In Valuation - {company_abbr}" + + def distribute_landed_cost_on_items(lcv): based_on = lcv.distribute_charges_based_on.lower() total = sum(flt(d.get(based_on)) for d in lcv.get("items")) From 4350c6b9e394167c88d74c0d64e23ec4f7e9db42 Mon Sep 17 00:00:00 2001 From: Sudharsanan11 Date: Mon, 23 Feb 2026 01:59:25 +0530 Subject: [PATCH 3/3] test(stock): add test to validate company for receipts and expense accounts (cherry picked from commit d58171987c4a63de234acfd2c1f2cd53c767ad03) --- .../landed_cost_voucher.py | 6 ++++ .../test_landed_cost_voucher.py | 33 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.py b/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.py index f57a8792598..fc4bc589f9d 100644 --- a/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.py +++ b/erpnext/stock/doctype/landed_cost_voucher/landed_cost_voucher.py @@ -14,6 +14,10 @@ from erpnext.controllers.taxes_and_totals import init_landed_taxes_and_totals from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos +class IncorrectCompanyValidationError(frappe.ValidationError): + pass + + class LandedCostVoucher(Document): # begin: auto-generated types # This code is auto-generated. Do not modify anything in this block. @@ -136,6 +140,7 @@ class LandedCostVoucher(Document): frappe.bold(self.company), ), title=_("Incorrect Company"), + exc=IncorrectCompanyValidationError, ) if d.receipt_document_type == "Purchase Invoice": @@ -184,6 +189,7 @@ class LandedCostVoucher(Document): frappe.bold(self.company), ), title=_("Incorrect Account"), + exc=IncorrectCompanyValidationError, ) def set_total_taxes_and_charges(self): diff --git a/erpnext/stock/doctype/landed_cost_voucher/test_landed_cost_voucher.py b/erpnext/stock/doctype/landed_cost_voucher/test_landed_cost_voucher.py index 440d828cd61..f68351a5611 100644 --- a/erpnext/stock/doctype/landed_cost_voucher/test_landed_cost_voucher.py +++ b/erpnext/stock/doctype/landed_cost_voucher/test_landed_cost_voucher.py @@ -178,6 +178,39 @@ class TestLandedCostVoucher(IntegrationTestCase): self.assertEqual(last_sle.qty_after_transaction, last_sle_after_landed_cost.qty_after_transaction) self.assertEqual(last_sle_after_landed_cost.stock_value - last_sle.stock_value, 50.0) + def test_lcv_validates_company(self): + from erpnext.stock.doctype.landed_cost_voucher.landed_cost_voucher import ( + IncorrectCompanyValidationError, + ) + + company_a = "_Test Company" + company_b = "_Test Company with perpetual inventory" + + pr = make_purchase_receipt( + company=company_a, + warehouse="Stores - _TC", + qty=1, + rate=100, + ) + + lcv = make_landed_cost_voucher( + company=company_b, + receipt_document_type="Purchase Receipt", + receipt_document=pr.name, + charges=50, + do_not_save=True, + ) + + self.assertRaises(IncorrectCompanyValidationError, lcv.validate_receipt_documents) + lcv.company = company_a + + self.assertRaises(IncorrectCompanyValidationError, lcv.validate_expense_accounts) + lcv.taxes[0].expense_account = get_expense_account(company_a) + + lcv.save() + distribute_landed_cost_on_items(lcv) + lcv.submit() + def test_landed_cost_voucher_for_zero_purchase_rate(self): "Test impact of LCV on future stock balances." from erpnext.stock.doctype.item.test_item import make_item