Skip to content

Commit

Permalink
Merge pull request #45167 from aerele/validate-sales-person
Browse files Browse the repository at this point in the history
fix: validate linked sales person
  • Loading branch information
ruthra-kumar authored Jan 20, 2025
2 parents da34d79 + e614f07 commit f9200a9
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions erpnext/setup/doctype/sales_person/sales_person.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from frappe.query_builder import Interval
from frappe.query_builder.functions import Count, CurDate, UnixTimestamp
from frappe.utils import flt
from frappe.utils.data import get_url_to_list
from frappe.utils.nestedset import NestedSet, get_root_of

from erpnext import get_default_currency
Expand Down Expand Up @@ -42,6 +43,9 @@ class SalesPerson(NestedSet):
nsm_parent_field = "parent_sales_person"

def validate(self):
if not self.enabled:
self.validate_sales_person()

if not self.parent_sales_person:
self.parent_sales_person = get_root_of("Sales Person")

Expand Down Expand Up @@ -83,6 +87,25 @@ def on_update(self):
super().on_update()
self.validate_one_root()

def validate_sales_person(self):
sales_team = frappe.qb.DocType("Sales Team")

query = (
frappe.qb.from_(sales_team)
.select(sales_team.sales_person)
.where((sales_team.sales_person == self.name) & (sales_team.parenttype == "Customer"))
.groupby(sales_team.sales_person)
).run(as_dict=True)

if query:
frappe.throw(
_("The Sales Person is linked with {0}").format(
frappe.bold(
f"""<a href="{get_url_to_list("Customer")}?sales_person={self.name}">{"Customers"}</a>"""
)
)
)

def get_email_id(self):
if self.employee:
user = frappe.db.get_value("Employee", self.employee, "user_id")
Expand Down

0 comments on commit f9200a9

Please sign in to comment.