fix: cost center & account validation in Sales/Purchase Taxes and Charges (#25929)
diff --git a/erpnext/accounts/doctype/sales_taxes_and_charges_template/sales_taxes_and_charges_template.py b/erpnext/accounts/doctype/sales_taxes_and_charges_template/sales_taxes_and_charges_template.py
index 52d19d5..8f9eb65 100644
--- a/erpnext/accounts/doctype/sales_taxes_and_charges_template/sales_taxes_and_charges_template.py
+++ b/erpnext/accounts/doctype/sales_taxes_and_charges_template/sales_taxes_and_charges_template.py
@@ -6,7 +6,7 @@
from frappe import _
from frappe.utils import flt
from frappe.model.document import Document
-from erpnext.controllers.accounts_controller import validate_taxes_and_charges, validate_inclusive_tax
+from erpnext.controllers.accounts_controller import validate_taxes_and_charges, validate_inclusive_tax, validate_cost_center, validate_account_head
class SalesTaxesandChargesTemplate(Document):
def validate(self):
@@ -39,6 +39,8 @@
for tax in doc.get("taxes"):
validate_taxes_and_charges(tax)
+ validate_account_head(tax, doc)
+ validate_cost_center(tax, doc)
validate_inclusive_tax(tax, doc)
def validate_disabled(doc):
diff --git a/erpnext/accounts/doctype/sales_taxes_and_charges_template/test_records.json b/erpnext/accounts/doctype/sales_taxes_and_charges_template/test_records.json
index 2b737b9..74db08d 100644
--- a/erpnext/accounts/doctype/sales_taxes_and_charges_template/test_records.json
+++ b/erpnext/accounts/doctype/sales_taxes_and_charges_template/test_records.json
@@ -8,6 +8,7 @@
"charge_type": "On Net Total",
"description": "VAT",
"doctype": "Sales Taxes and Charges",
+ "cost_center": "Main - _TC",
"parentfield": "taxes",
"rate": 6
},
@@ -16,6 +17,7 @@
"charge_type": "On Net Total",
"description": "Service Tax",
"doctype": "Sales Taxes and Charges",
+ "cost_center": "Main - _TC",
"parentfield": "taxes",
"rate": 6.36
}
@@ -114,6 +116,7 @@
"charge_type": "On Net Total",
"description": "VAT",
"doctype": "Sales Taxes and Charges",
+ "cost_center": "Main - _TC",
"parentfield": "taxes",
"rate": 12
},
@@ -122,6 +125,7 @@
"charge_type": "On Net Total",
"description": "Service Tax",
"doctype": "Sales Taxes and Charges",
+ "cost_center": "Main - _TC",
"parentfield": "taxes",
"rate": 4
}
@@ -137,6 +141,7 @@
"charge_type": "On Net Total",
"description": "VAT",
"doctype": "Sales Taxes and Charges",
+ "cost_center": "Main - _TC",
"parentfield": "taxes",
"rate": 12
},
@@ -145,6 +150,7 @@
"charge_type": "On Net Total",
"description": "Service Tax",
"doctype": "Sales Taxes and Charges",
+ "cost_center": "Main - _TC",
"parentfield": "taxes",
"rate": 4
}
@@ -160,6 +166,7 @@
"charge_type": "On Net Total",
"description": "VAT",
"doctype": "Sales Taxes and Charges",
+ "cost_center": "Main - _TC",
"parentfield": "taxes",
"rate": 12
},
@@ -168,6 +175,7 @@
"charge_type": "On Net Total",
"description": "Service Tax",
"doctype": "Sales Taxes and Charges",
+ "cost_center": "Main - _TC",
"parentfield": "taxes",
"rate": 4
}
diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py
index 9e2e596..bc8e4ce 100644
--- a/erpnext/controllers/accounts_controller.py
+++ b/erpnext/controllers/accounts_controller.py
@@ -1288,6 +1288,27 @@
tax.rate = None
+def validate_account_head(tax, doc):
+ company = frappe.get_cached_value('Account',
+ tax.account_head, 'company')
+
+ if company != doc.company:
+ frappe.throw(_('Row {0}: Account {1} does not belong to Company {2}')
+ .format(tax.idx, frappe.bold(tax.account_head), frappe.bold(doc.company)), title=_('Invalid Account'))
+
+
+def validate_cost_center(tax, doc):
+ if not tax.cost_center:
+ return
+
+ company = frappe.get_cached_value('Cost Center',
+ tax.cost_center, 'company')
+
+ if company != doc.company:
+ frappe.throw(_('Row {0}: Cost Center {1} does not belong to Company {2}')
+ .format(tax.idx, frappe.bold(tax.cost_center), frappe.bold(doc.company)), title=_('Invalid Cost Center'))
+
+
def validate_inclusive_tax(tax, doc):
def _on_previous_row_error(row_range):
throw(_("To include tax in row {0} in Item rate, taxes in rows {1} must also be included").format(tax.idx, row_range))
diff --git a/erpnext/public/js/controllers/accounts.js b/erpnext/public/js/controllers/accounts.js
index 7b997a1..84c7176 100644
--- a/erpnext/public/js/controllers/accounts.js
+++ b/erpnext/public/js/controllers/accounts.js
@@ -31,6 +31,14 @@
}
}
});
+ frm.set_query("cost_center", "taxes", function(doc) {
+ return {
+ filters: {
+ "company": doc.company,
+ "is_group": 0
+ }
+ };
+ });
}
},
validate: function(frm) {
diff --git a/erpnext/setup/doctype/company/company.py b/erpnext/setup/doctype/company/company.py
index 8755125..95cbf51 100644
--- a/erpnext/setup/doctype/company/company.py
+++ b/erpnext/setup/doctype/company/company.py
@@ -108,6 +108,9 @@
frappe.flags.country_change = True
self.create_default_accounts()
self.create_default_warehouses()
+
+ if not frappe.db.get_value("Cost Center", {"is_group": 0, "company": self.name}):
+ self.create_default_cost_center()
if frappe.flags.country_change:
install_country_fixtures(self.name, self.country)
@@ -117,9 +120,6 @@
from erpnext.setup.setup_wizard.operations.install_fixtures import install_post_company_fixtures
install_post_company_fixtures(frappe._dict({'company_name': self.name}))
- if not frappe.db.get_value("Cost Center", {"is_group": 0, "company": self.name}):
- self.create_default_cost_center()
-
if not frappe.local.flags.ignore_chart_of_accounts:
self.set_default_accounts()
if self.default_cash_account:
diff --git a/erpnext/setup/setup_wizard/operations/taxes_setup.py b/erpnext/setup/setup_wizard/operations/taxes_setup.py
index cbb3dc8..bacada9 100644
--- a/erpnext/setup/setup_wizard/operations/taxes_setup.py
+++ b/erpnext/setup/setup_wizard/operations/taxes_setup.py
@@ -124,7 +124,8 @@
account_data = tax_row.get('account_head')
tax_row_defaults = {
'category': 'Total',
- 'charge_type': 'On Net Total'
+ 'charge_type': 'On Net Total',
+ 'cost_center': frappe.db.get_value('Company', company_name, 'cost_center')
}
if doctype == 'Purchase Taxes and Charges Template':