blob: 33441b166dbbbfd07c32d9afcf71efb106cd7626 [file] [log] [blame]
Rohan Bansal76825ab2020-04-28 16:08:52 +05301import frappe
Nabin Haitf904ac52022-06-10 11:15:22 +05302from frappe.model.document import Document
3from frappe.utils import cstr, now
Rohan Bansal76825ab2020-04-28 16:08:52 +05304
5
6def update_lead_phone_numbers(contact, method):
7 if contact.phone_nos:
8 contact_lead = contact.get_link_for("Lead")
9 if contact_lead:
10 phone = mobile_no = contact.phone_nos[0].phone
11
12 if len(contact.phone_nos) > 1:
13 # get the default phone number
Ankush Menat494bd9e2022-03-28 18:52:46 +053014 primary_phones = [
15 phone_doc.phone for phone_doc in contact.phone_nos if phone_doc.is_primary_phone
16 ]
Rohan Bansal76825ab2020-04-28 16:08:52 +053017 if primary_phones:
18 phone = primary_phones[0]
19
20 # get the default mobile number
Ankush Menat494bd9e2022-03-28 18:52:46 +053021 primary_mobile_nos = [
22 phone_doc.phone for phone_doc in contact.phone_nos if phone_doc.is_primary_mobile_no
23 ]
Rohan Bansal76825ab2020-04-28 16:08:52 +053024 if primary_mobile_nos:
25 mobile_no = primary_mobile_nos[0]
26
27 lead = frappe.get_doc("Lead", contact_lead)
Myuddin khatrifc0e45d2020-05-07 15:11:39 +053028 lead.db_set("phone", phone)
29 lead.db_set("mobile_no", mobile_no)
Anupam6eb77932021-11-19 10:13:05 +053030
Ankush Menat494bd9e2022-03-28 18:52:46 +053031
Anupam6eb77932021-11-19 10:13:05 +053032def copy_comments(doctype, docname, doc):
Ankush Menat494bd9e2022-03-28 18:52:46 +053033 comments = frappe.db.get_values(
34 "Comment",
35 filters={"reference_doctype": doctype, "reference_name": docname, "comment_type": "Comment"},
36 fieldname="*",
37 )
Anupam6eb77932021-11-19 10:13:05 +053038 for comment in comments:
Ankush Menat494bd9e2022-03-28 18:52:46 +053039 comment = frappe.get_doc(comment.update({"doctype": "Comment"}))
Anupam6eb77932021-11-19 10:13:05 +053040 comment.name = None
41 comment.reference_doctype = doc.doctype
42 comment.reference_name = doc.name
43 comment.insert()
44
Ankush Menat494bd9e2022-03-28 18:52:46 +053045
Nabin Haitf904ac52022-06-10 11:15:22 +053046def link_communications(doctype, docname, doc):
Anupam31ac1012021-12-17 19:21:08 +053047 communication_list = get_linked_communication_list(doctype, docname)
48
49 for communication in communication_list:
50 communication_doc = frappe.get_doc("Communication", communication)
51 communication_doc.add_link(doc.doctype, doc.name, autosave=True)
52
Ankush Menat494bd9e2022-03-28 18:52:46 +053053
Anupam31ac1012021-12-17 19:21:08 +053054def get_linked_communication_list(doctype, docname):
Ankush Menat494bd9e2022-03-28 18:52:46 +053055 communications = frappe.get_all(
56 "Communication", filters={"reference_doctype": doctype, "reference_name": docname}, pluck="name"
57 )
58 communication_links = frappe.get_all(
59 "Communication Link",
60 {"link_doctype": doctype, "link_name": docname, "parent": ("not in", communications)},
61 pluck="parent",
62 )
Anupam5b40d9e2021-12-18 20:12:57 +053063
Anupam31ac1012021-12-17 19:21:08 +053064 return communications + communication_links
Nabin Haitf904ac52022-06-10 11:15:22 +053065
66
67def link_communications_with_prospect(communication, method):
68 prospect = get_linked_prospect(communication.reference_doctype, communication.reference_name)
69
70 if prospect:
71 already_linked = any(
72 [
73 d.name
74 for d in communication.get("timeline_links")
75 if d.link_doctype == "Prospect" and d.link_name == prospect
76 ]
77 )
78 if not already_linked:
79 row = communication.append("timeline_links")
80 row.link_doctype = "Prospect"
81 row.link_name = prospect
82 row.db_update()
83
84
85def get_linked_prospect(reference_doctype, reference_name):
86 prospect = None
87 if reference_doctype == "Lead":
88 prospect = frappe.db.get_value("Prospect Lead", {"lead": reference_name}, "parent")
89
90 elif reference_doctype == "Opportunity":
91 opportunity_from, party_name = frappe.db.get_value(
92 "Opportunity", reference_name, ["opportunity_from", "party_name"]
93 )
94 if opportunity_from == "Lead":
95 prospect = frappe.db.get_value(
96 "Prospect Opportunity", {"opportunity": reference_name}, "parent"
97 )
98 if opportunity_from == "Prospect":
99 prospect = party_name
100
101 return prospect
102
103
104def link_events_with_prospect(event, method):
105 if event.event_participants:
106 ref_doctype = event.event_participants[0].reference_doctype
107 ref_docname = event.event_participants[0].reference_docname
108 prospect = get_linked_prospect(ref_doctype, ref_docname)
109 if prospect:
110 event.add_participant("Prospect", prospect)
111 event.save()
112
113
114def link_open_tasks(ref_doctype, ref_docname, doc):
115 todos = get_open_todos(ref_doctype, ref_docname)
116
117 for todo in todos:
118 todo_doc = frappe.get_doc("ToDo", todo.name)
119 todo_doc.reference_type = doc.doctype
120 todo_doc.reference_name = doc.name
121 todo_doc.db_update()
122
123
124def link_open_events(ref_doctype, ref_docname, doc):
125 events = get_open_events(ref_doctype, ref_docname)
126 for event in events:
127 event_doc = frappe.get_doc("Event", event.name)
128 event_doc.add_participant(doc.doctype, doc.name)
129 event_doc.save()
130
131
132@frappe.whitelist()
133def get_open_activities(ref_doctype, ref_docname):
134 tasks = get_open_todos(ref_doctype, ref_docname)
135 events = get_open_events(ref_doctype, ref_docname)
136
137 return {"tasks": tasks, "events": events}
138
139
140def get_open_todos(ref_doctype, ref_docname):
141 return frappe.get_all(
142 "ToDo",
143 filters={"reference_type": ref_doctype, "reference_name": ref_docname, "status": "Open"},
144 fields=[
145 "name",
146 "description",
147 "allocated_to",
148 "date",
149 ],
150 )
151
152
153def get_open_events(ref_doctype, ref_docname):
154 event = frappe.qb.DocType("Event")
155 event_link = frappe.qb.DocType("Event Participants")
156
157 query = (
158 frappe.qb.from_(event)
159 .join(event_link)
160 .on(event_link.parent == event.name)
161 .select(
162 event.name,
163 event.subject,
164 event.event_category,
165 event.starts_on,
166 event.ends_on,
167 event.description,
168 )
169 .where(
170 (event_link.reference_doctype == ref_doctype)
171 & (event_link.reference_docname == ref_docname)
172 & (event.status == "Open")
173 )
174 )
175 data = query.run(as_dict=True)
176
177 return data
178
179
180class CRMNote(Document):
181 @frappe.whitelist()
182 def add_note(self, note):
183 self.append("notes", {"note": note, "added_by": frappe.session.user, "added_on": now()})
184 self.save()
185
186 @frappe.whitelist()
187 def edit_note(self, note, row_id):
188 for d in self.notes:
189 if cstr(d.name) == row_id:
190 d.note = note
191 d.db_update()
192
193 @frappe.whitelist()
194 def delete_note(self, row_id):
195 for d in self.notes:
196 if cstr(d.name) == row_id:
197 self.remove(d)
198 break
199 self.save()