fix(stock): validate company for receipt documents and expense accounts

(cherry picked from commit 15dfc08a31)
This commit is contained in:
Sudharsanan11
2026-02-18 23:59:42 +05:30
committed by Mergify
parent bd76ef29cc
commit 44620884c1

View File

@@ -66,6 +66,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"):
@@ -101,11 +102,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"
@@ -137,6 +154,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"))