diff --git a/hrms/hr/doctype/expense_claim/expense_claim.js b/hrms/hr/doctype/expense_claim/expense_claim.js index 0f826807bd..32c02611f2 100644 --- a/hrms/hr/doctype/expense_claim/expense_claim.js +++ b/hrms/hr/doctype/expense_claim/expense_claim.js @@ -64,6 +64,14 @@ frappe.ui.form.on("Expense Claim", { query: "erpnext.controllers.queries.employee_query", }; }); + + frm.set_query("department", function () { + return { + filters: { + company: frm.doc.company, + }, + }; + }); }, onload: function (frm) { diff --git a/hrms/hr/doctype/expense_claim/expense_claim.py b/hrms/hr/doctype/expense_claim/expense_claim.py index 04a02ba5d1..0717eed569 100644 --- a/hrms/hr/doctype/expense_claim/expense_claim.py +++ b/hrms/hr/doctype/expense_claim/expense_claim.py @@ -29,6 +29,10 @@ class ExpenseApproverIdentityError(frappe.ValidationError): pass +class MismatchError(frappe.ValidationError): + pass + + class ExpenseClaim(AccountsController, PWANotificationsMixin): def onload(self): self.get("__onload").make_payment_via_journal_entry = frappe.db.get_single_value( @@ -47,6 +51,7 @@ def validate(self): self.set_expense_account(validate=True) self.calculate_taxes() self.set_status() + self.validate_company_and_department() if self.task and not self.project: self.project = frappe.db.get_value("Task", self.task, "project") @@ -83,6 +88,15 @@ def set_status(self, update=False): else: self.status = status + def validate_company_and_department(self): + if self.department: + company = frappe.db.get_value("Department", self.department, "company") + if company and self.company != company: + frappe.throw( + _("Department {0} does not belong to company: {1}").format(self.department, self.company), + exc=MismatchError, + ) + def on_update(self): share_doc_with_approver(self, self.expense_approver) self.publish_update() diff --git a/hrms/hr/doctype/expense_claim/test_expense_claim.py b/hrms/hr/doctype/expense_claim/test_expense_claim.py index 2d74922eae..8c12ad6afa 100644 --- a/hrms/hr/doctype/expense_claim/test_expense_claim.py +++ b/hrms/hr/doctype/expense_claim/test_expense_claim.py @@ -10,6 +10,7 @@ from erpnext.setup.doctype.employee.test_employee import make_employee from hrms.hr.doctype.expense_claim.expense_claim import ( + MismatchError, get_outstanding_amount_for_claim, make_bank_entry, make_expense_claim_for_delivery_trip, @@ -568,6 +569,13 @@ def test_repost(self): ) self.assertEqual(ledger_balance, expected_data) + def test_company_department_validation(self): + # validate company and department + expense_claim = frappe.new_doc("Expense Claim") + expense_claim.company = "_Test Company 3" + expense_claim.department = "Accounts - _TC2" + self.assertRaises(MismatchError, expense_claim.save) + def get_payable_account(company): return frappe.get_cached_value("Company", company, "default_payable_account")