From b972f763f30e2ab5b6e8f6aa5c59a103ccf22970 Mon Sep 17 00:00:00 2001 From: Pranav Nachnekar Date: Tue, 24 Dec 2019 11:16:46 +0000 Subject: [PATCH] fix: added validation for cost center (#19932) * fix: add validation for cost centers in sales invoice * use `get_cached_value` for getting compnay, formatting of error message --- erpnext/accounts/doctype/sales_invoice/sales_invoice.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py index 0f4d4451be..9ea5a51e3f 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py @@ -90,6 +90,7 @@ class SalesInvoice(SellingController): self.validate_account_for_change_amount() self.validate_fixed_asset() self.set_income_account_for_fixed_assets() + self.validate_item_cost_centers() validate_inter_company_party(self.doctype, self.customer, self.company, self.inter_company_invoice_reference) if cint(self.is_pos): @@ -147,6 +148,12 @@ class SalesInvoice(SellingController): elif asset.status in ("Scrapped", "Cancelled", "Sold"): frappe.throw(_("Row #{0}: Asset {1} cannot be submitted, it is already {2}").format(d.idx, d.asset, asset.status)) + def validate_item_cost_centers(self): + for item in self.items: + cost_center_company = frappe.get_cached_value("Cost Center", item.cost_center, "company") + if cost_center_company != self.company: + frappe.throw(_("Row #{0}: Cost Center {1} does not belong to company {2}").format(frappe.bold(item.idx), frappe.bold(item.cost_center), frappe.bold(self.company))) + def before_save(self): set_account_for_mode_of_payment(self)