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 linked sales person #45167

Merged
merged 1 commit into from
Jan 20, 2025
Merged
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
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
Loading