Skip to content

Commit

Permalink
Merge pull request #2625 from frappe/mergify/bp/version-15-hotfix/pr-…
Browse files Browse the repository at this point in the history
…2582

fix: validate department based on company in expense claim (backport #2582)
  • Loading branch information
ruchamahabal authored Jan 13, 2025
2 parents ddd0abf + 99065c5 commit a148b29
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
8 changes: 8 additions & 0 deletions hrms/hr/doctype/expense_claim/expense_claim.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 14 additions & 0 deletions hrms/hr/doctype/expense_claim/expense_claim.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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")

Expand Down Expand Up @@ -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()
Expand Down
8 changes: 8 additions & 0 deletions hrms/hr/doctype/expense_claim/test_expense_claim.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit a148b29

Please sign in to comment.