mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-13 02:01:21 +00:00
Merge pull request #45533 from frappe/mergify/bp/version-14-hotfix/pr-45302
feat: add company level validation for accounting dimension (backport #45302)
This commit is contained in:
@@ -31,7 +31,8 @@
|
|||||||
"label": "Reference Document Type",
|
"label": "Reference Document Type",
|
||||||
"options": "DocType",
|
"options": "DocType",
|
||||||
"read_only_depends_on": "eval:!doc.__islocal",
|
"read_only_depends_on": "eval:!doc.__islocal",
|
||||||
"reqd": 1
|
"reqd": 1,
|
||||||
|
"search_index": 1
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"default": "0",
|
"default": "0",
|
||||||
|
|||||||
@@ -3860,6 +3860,7 @@ class TestSalesInvoice(FrappeTestCase):
|
|||||||
si = create_sales_invoice(do_not_submit=True)
|
si = create_sales_invoice(do_not_submit=True)
|
||||||
|
|
||||||
project = frappe.new_doc("Project")
|
project = frappe.new_doc("Project")
|
||||||
|
project.company = "_Test Company"
|
||||||
project.project_name = "Test Total Billed Amount"
|
project.project_name = "Test Total Billed Amount"
|
||||||
project.save()
|
project.save()
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ from collections import defaultdict
|
|||||||
import frappe
|
import frappe
|
||||||
from frappe import _, bold, qb, throw
|
from frappe import _, bold, qb, throw
|
||||||
from frappe.model.workflow import get_workflow_name, is_transition_condition_satisfied
|
from frappe.model.workflow import get_workflow_name, is_transition_condition_satisfied
|
||||||
from frappe.query_builder import Criterion
|
from frappe.query_builder import Criterion, DocType
|
||||||
from frappe.query_builder.custom import ConstantColumn
|
from frappe.query_builder.custom import ConstantColumn
|
||||||
from frappe.query_builder.functions import Abs, Sum
|
from frappe.query_builder.functions import Abs, Sum
|
||||||
from frappe.utils import (
|
from frappe.utils import (
|
||||||
@@ -250,6 +250,7 @@ class AccountsController(TransactionBase):
|
|||||||
apply_pricing_rule_on_transaction(self)
|
apply_pricing_rule_on_transaction(self)
|
||||||
|
|
||||||
self.set_total_in_words()
|
self.set_total_in_words()
|
||||||
|
self.validate_company_in_accounting_dimension()
|
||||||
|
|
||||||
def init_internal_values(self):
|
def init_internal_values(self):
|
||||||
# init all the internal values as 0 on sa
|
# init all the internal values as 0 on sa
|
||||||
@@ -355,6 +356,39 @@ class AccountsController(TransactionBase):
|
|||||||
(sle.voucher_type == self.doctype) & (sle.voucher_no == self.name)
|
(sle.voucher_type == self.doctype) & (sle.voucher_no == self.name)
|
||||||
).run()
|
).run()
|
||||||
|
|
||||||
|
def validate_company_in_accounting_dimension(self):
|
||||||
|
doc_field = DocType("DocField")
|
||||||
|
accounting_dimension = DocType("Accounting Dimension")
|
||||||
|
dimension_list = (
|
||||||
|
frappe.qb.from_(accounting_dimension)
|
||||||
|
.select(accounting_dimension.document_type)
|
||||||
|
.join(doc_field)
|
||||||
|
.on(doc_field.parent == accounting_dimension.document_type)
|
||||||
|
.where(doc_field.fieldname == "company")
|
||||||
|
).run(as_list=True)
|
||||||
|
|
||||||
|
dimension_list = sum(dimension_list, ["Project"])
|
||||||
|
self.validate_company(dimension_list)
|
||||||
|
|
||||||
|
for child in self.get_all_children() or []:
|
||||||
|
self.validate_company(dimension_list, child)
|
||||||
|
|
||||||
|
def validate_company(self, dimension_list, child=None):
|
||||||
|
for dimension in dimension_list:
|
||||||
|
if not child:
|
||||||
|
dimension_value = self.get(frappe.scrub(dimension))
|
||||||
|
else:
|
||||||
|
dimension_value = child.get(frappe.scrub(dimension))
|
||||||
|
|
||||||
|
if dimension_value:
|
||||||
|
company = frappe.get_cached_value(dimension, dimension_value, "company")
|
||||||
|
if company and company != self.company:
|
||||||
|
frappe.throw(
|
||||||
|
_("{0}: {1} does not belong to the Company: {2}").format(
|
||||||
|
dimension, frappe.bold(dimension_value), self.company
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
def validate_return_against_account(self):
|
def validate_return_against_account(self):
|
||||||
if self.doctype in ["Sales Invoice", "Purchase Invoice"] and self.is_return and self.return_against:
|
if self.doctype in ["Sales Invoice", "Purchase Invoice"] and self.is_return and self.return_against:
|
||||||
cr_dr_account_field = "debit_to" if self.doctype == "Sales Invoice" else "credit_to"
|
cr_dr_account_field = "debit_to" if self.doctype == "Sales Invoice" else "credit_to"
|
||||||
|
|||||||
@@ -1344,32 +1344,32 @@ class TestAccountsController(FrappeTestCase):
|
|||||||
|
|
||||||
# Invoices
|
# Invoices
|
||||||
si1 = self.create_sales_invoice(qty=1, rate=rate_in_account_currency, do_not_submit=True)
|
si1 = self.create_sales_invoice(qty=1, rate=rate_in_account_currency, do_not_submit=True)
|
||||||
si1.department = "Management"
|
si1.department = "Management - _TC"
|
||||||
si1.save().submit()
|
si1.save().submit()
|
||||||
|
|
||||||
si2 = self.create_sales_invoice(qty=1, rate=rate_in_account_currency, do_not_submit=True)
|
si2 = self.create_sales_invoice(qty=1, rate=rate_in_account_currency, do_not_submit=True)
|
||||||
si2.department = "Operations"
|
si2.department = "Operations - _TC"
|
||||||
si2.save().submit()
|
si2.save().submit()
|
||||||
|
|
||||||
# Payments
|
# Payments
|
||||||
cr_note1 = self.create_sales_invoice(qty=-1, conversion_rate=75, rate=1, do_not_save=True)
|
cr_note1 = self.create_sales_invoice(qty=-1, conversion_rate=75, rate=1, do_not_save=True)
|
||||||
cr_note1.department = "Management"
|
cr_note1.department = "Management - _TC"
|
||||||
cr_note1.is_return = 1
|
cr_note1.is_return = 1
|
||||||
cr_note1.save().submit()
|
cr_note1.save().submit()
|
||||||
|
|
||||||
cr_note2 = self.create_sales_invoice(qty=-1, conversion_rate=75, rate=1, do_not_save=True)
|
cr_note2 = self.create_sales_invoice(qty=-1, conversion_rate=75, rate=1, do_not_save=True)
|
||||||
cr_note2.department = "Legal"
|
cr_note2.department = "Legal - _TC"
|
||||||
cr_note2.is_return = 1
|
cr_note2.is_return = 1
|
||||||
cr_note2.save().submit()
|
cr_note2.save().submit()
|
||||||
|
|
||||||
pe1 = get_payment_entry(si1.doctype, si1.name)
|
pe1 = get_payment_entry(si1.doctype, si1.name)
|
||||||
pe1.references = []
|
pe1.references = []
|
||||||
pe1.department = "Research & Development"
|
pe1.department = "Research & Development - _TC"
|
||||||
pe1.save().submit()
|
pe1.save().submit()
|
||||||
|
|
||||||
pe2 = get_payment_entry(si1.doctype, si1.name)
|
pe2 = get_payment_entry(si1.doctype, si1.name)
|
||||||
pe2.references = []
|
pe2.references = []
|
||||||
pe2.department = "Management"
|
pe2.department = "Management - _TC"
|
||||||
pe2.save().submit()
|
pe2.save().submit()
|
||||||
|
|
||||||
je1 = self.create_journal_entry(
|
je1 = self.create_journal_entry(
|
||||||
@@ -1382,7 +1382,7 @@ class TestAccountsController(FrappeTestCase):
|
|||||||
)
|
)
|
||||||
je1.accounts[0].party_type = "Customer"
|
je1.accounts[0].party_type = "Customer"
|
||||||
je1.accounts[0].party = self.customer
|
je1.accounts[0].party = self.customer
|
||||||
je1.accounts[0].department = "Management"
|
je1.accounts[0].department = "Management - _TC"
|
||||||
je1.save().submit()
|
je1.save().submit()
|
||||||
|
|
||||||
# assert dimension filter's result
|
# assert dimension filter's result
|
||||||
@@ -1391,17 +1391,17 @@ class TestAccountsController(FrappeTestCase):
|
|||||||
self.assertEqual(len(pr.invoices), 2)
|
self.assertEqual(len(pr.invoices), 2)
|
||||||
self.assertEqual(len(pr.payments), 5)
|
self.assertEqual(len(pr.payments), 5)
|
||||||
|
|
||||||
pr.department = "Legal"
|
pr.department = "Legal - _TC"
|
||||||
pr.get_unreconciled_entries()
|
pr.get_unreconciled_entries()
|
||||||
self.assertEqual(len(pr.invoices), 0)
|
self.assertEqual(len(pr.invoices), 0)
|
||||||
self.assertEqual(len(pr.payments), 1)
|
self.assertEqual(len(pr.payments), 1)
|
||||||
|
|
||||||
pr.department = "Management"
|
pr.department = "Management - _TC"
|
||||||
pr.get_unreconciled_entries()
|
pr.get_unreconciled_entries()
|
||||||
self.assertEqual(len(pr.invoices), 1)
|
self.assertEqual(len(pr.invoices), 1)
|
||||||
self.assertEqual(len(pr.payments), 3)
|
self.assertEqual(len(pr.payments), 3)
|
||||||
|
|
||||||
pr.department = "Research & Development"
|
pr.department = "Research & Development - _TC"
|
||||||
pr.get_unreconciled_entries()
|
pr.get_unreconciled_entries()
|
||||||
self.assertEqual(len(pr.invoices), 0)
|
self.assertEqual(len(pr.invoices), 0)
|
||||||
self.assertEqual(len(pr.payments), 1)
|
self.assertEqual(len(pr.payments), 1)
|
||||||
@@ -1413,17 +1413,17 @@ class TestAccountsController(FrappeTestCase):
|
|||||||
|
|
||||||
# Invoice
|
# Invoice
|
||||||
si = self.create_sales_invoice(qty=1, rate=rate_in_account_currency, do_not_submit=True)
|
si = self.create_sales_invoice(qty=1, rate=rate_in_account_currency, do_not_submit=True)
|
||||||
si.department = "Management"
|
si.department = "Management - _TC"
|
||||||
si.save().submit()
|
si.save().submit()
|
||||||
|
|
||||||
# Payment
|
# Payment
|
||||||
cr_note = self.create_sales_invoice(qty=-1, conversion_rate=75, rate=1, do_not_save=True)
|
cr_note = self.create_sales_invoice(qty=-1, conversion_rate=75, rate=1, do_not_save=True)
|
||||||
cr_note.department = "Management"
|
cr_note.department = "Management - _TC"
|
||||||
cr_note.is_return = 1
|
cr_note.is_return = 1
|
||||||
cr_note.save().submit()
|
cr_note.save().submit()
|
||||||
|
|
||||||
pr = self.create_payment_reconciliation()
|
pr = self.create_payment_reconciliation()
|
||||||
pr.department = "Management"
|
pr.department = "Management - _TC"
|
||||||
pr.get_unreconciled_entries()
|
pr.get_unreconciled_entries()
|
||||||
self.assertEqual(len(pr.invoices), 1)
|
self.assertEqual(len(pr.invoices), 1)
|
||||||
self.assertEqual(len(pr.payments), 1)
|
self.assertEqual(len(pr.payments), 1)
|
||||||
@@ -1456,7 +1456,7 @@ class TestAccountsController(FrappeTestCase):
|
|||||||
# Sales Invoice in Foreign Currency
|
# Sales Invoice in Foreign Currency
|
||||||
self.setup_dimensions()
|
self.setup_dimensions()
|
||||||
rate_in_account_currency = 1
|
rate_in_account_currency = 1
|
||||||
dpt = "Research & Development"
|
dpt = "Research & Development - _TC"
|
||||||
|
|
||||||
si = self.create_sales_invoice(qty=1, rate=rate_in_account_currency, do_not_save=True)
|
si = self.create_sales_invoice(qty=1, rate=rate_in_account_currency, do_not_save=True)
|
||||||
si.department = dpt
|
si.department = dpt
|
||||||
@@ -1492,7 +1492,7 @@ class TestAccountsController(FrappeTestCase):
|
|||||||
|
|
||||||
def test_93_dimension_inheritance_on_advance(self):
|
def test_93_dimension_inheritance_on_advance(self):
|
||||||
self.setup_dimensions()
|
self.setup_dimensions()
|
||||||
dpt = "Research & Development"
|
dpt = "Research & Development - _TC"
|
||||||
|
|
||||||
adv = self.create_payment_entry(amount=1, source_exc_rate=85)
|
adv = self.create_payment_entry(amount=1, source_exc_rate=85)
|
||||||
adv.department = dpt
|
adv.department = dpt
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
[
|
[
|
||||||
{
|
{
|
||||||
"project_name": "_Test Project",
|
"project_name": "_Test Project",
|
||||||
"status": "Open"
|
"status": "Open",
|
||||||
|
"company": "_Test Company"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
Reference in New Issue
Block a user