From 76023f1fdcf6c9553a491b9493fc776560beb51b Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Wed, 14 Feb 2024 01:14:42 +0100 Subject: [PATCH] refactor(Sales Invoice Item): validate cost center --- .../doctype/pos_invoice_item/pos_invoice_item.py | 4 ++-- .../accounts/doctype/sales_invoice/sales_invoice.py | 8 +------- .../doctype/sales_invoice_item/sales_invoice_item.py | 11 ++++++++++- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/erpnext/accounts/doctype/pos_invoice_item/pos_invoice_item.py b/erpnext/accounts/doctype/pos_invoice_item/pos_invoice_item.py index 55a577b0c51..44358c319eb 100644 --- a/erpnext/accounts/doctype/pos_invoice_item/pos_invoice_item.py +++ b/erpnext/accounts/doctype/pos_invoice_item/pos_invoice_item.py @@ -3,10 +3,10 @@ # import frappe -from frappe.model.document import Document +from erpnext.accounts.doctype.sales_invoice_item.sales_invoice_item import SalesInvoiceItem -class POSInvoiceItem(Document): +class POSInvoiceItem(SalesInvoiceItem): # begin: auto-generated types # This code is auto-generated. Do not modify anything in this block. diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py index abc0694a63c..77cc53aa854 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py @@ -379,13 +379,7 @@ class SalesInvoice(SellingController): 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) - ) - ) + item.validate_cost_center(self.company) def validate_income_account(self): for item in self.get("items"): diff --git a/erpnext/accounts/doctype/sales_invoice_item/sales_invoice_item.py b/erpnext/accounts/doctype/sales_invoice_item/sales_invoice_item.py index c71d08e7f70..989a8ca2c9b 100644 --- a/erpnext/accounts/doctype/sales_invoice_item/sales_invoice_item.py +++ b/erpnext/accounts/doctype/sales_invoice_item/sales_invoice_item.py @@ -2,6 +2,8 @@ # License: GNU General Public License v3. See license.txt +import frappe +from frappe import _ from frappe.model.document import Document @@ -92,4 +94,11 @@ class SalesInvoiceItem(Document): weight_uom: DF.Link | None # end: auto-generated types - pass + def validate_cost_center(self, company: str): + cost_center_company = frappe.get_cached_value("Cost Center", self.cost_center, "company") + if cost_center_company != company: + frappe.throw( + _("Row #{0}: Cost Center {1} does not belong to company {2}").format( + frappe.bold(self.idx), frappe.bold(self.cost_center), frappe.bold(company) + ) + )