-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from shariquerik/contact-linking
feat: Contact linking
- Loading branch information
Showing
27 changed files
with
1,456 additions
and
568 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import frappe | ||
|
||
|
||
def validate(doc, method): | ||
set_primary_email(doc) | ||
set_primary_mobile_no(doc) | ||
doc.set_primary_email() | ||
doc.set_primary("mobile_no") | ||
|
||
|
||
def set_primary_email(doc): | ||
if not doc.email_ids: | ||
return | ||
|
||
if len(doc.email_ids) == 1: | ||
doc.email_ids[0].is_primary = 1 | ||
|
||
|
||
def set_primary_mobile_no(doc): | ||
if not doc.phone_nos: | ||
return | ||
|
||
if len(doc.phone_nos) == 1: | ||
doc.phone_nos[0].is_primary_mobile_no = 1 | ||
|
||
|
||
@frappe.whitelist() | ||
def get_linked_deals(contact): | ||
"""Get linked deals for a contact""" | ||
|
||
if not frappe.has_permission("Contact", "read", contact): | ||
frappe.throw("Not permitted", frappe.PermissionError) | ||
|
||
deal_names = frappe.get_all( | ||
"CRM Contacts", | ||
filters={"contact": contact, "parenttype": "CRM Deal"}, | ||
fields=["parent"], | ||
distinct=True, | ||
) | ||
|
||
# get deals data | ||
deals = [] | ||
for d in deal_names: | ||
deal = frappe.get_cached_doc( | ||
"CRM Deal", | ||
d.parent, | ||
fields=[ | ||
"name", | ||
"organization", | ||
"annual_revenue", | ||
"status", | ||
"email", | ||
"mobile_no", | ||
"deal_owner", | ||
"modified", | ||
], | ||
) | ||
deals.append(deal.as_dict()) | ||
|
||
return deals | ||
|
||
|
||
@frappe.whitelist() | ||
def create_new(contact, field, value): | ||
"""Create new email or phone for a contact""" | ||
if not frappe.has_permission("Contact", "write", contact): | ||
frappe.throw("Not permitted", frappe.PermissionError) | ||
|
||
contact = frappe.get_doc("Contact", contact) | ||
|
||
if field == "email": | ||
contact.append("email_ids", {"email_id": value}) | ||
elif field in ("mobile_no", "phone"): | ||
contact.append("phone_nos", {"phone": value}) | ||
else: | ||
frappe.throw("Invalid field") | ||
|
||
contact.save() | ||
return True | ||
|
||
|
||
@frappe.whitelist() | ||
def set_as_primary(contact, field, value): | ||
"""Set email or phone as primary for a contact""" | ||
if not frappe.has_permission("Contact", "write", contact): | ||
frappe.throw("Not permitted", frappe.PermissionError) | ||
|
||
contact = frappe.get_doc("Contact", contact) | ||
|
||
if field == "email": | ||
for email in contact.email_ids: | ||
if email.email_id == value: | ||
email.is_primary = 1 | ||
else: | ||
email.is_primary = 0 | ||
elif field in ("mobile_no", "phone"): | ||
name = "is_primary_mobile_no" if field == "mobile_no" else "is_primary_phone" | ||
for phone in contact.phone_nos: | ||
if phone.phone == value: | ||
phone.set(name, 1) | ||
else: | ||
phone.set(name, 0) | ||
else: | ||
frappe.throw("Invalid field") | ||
|
||
contact.save() | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.