Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: validate department based on company in expense claim (backport #2582) #2625

Merged
merged 5 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading