blob: c543c387c03a48fb2649c931bcbf64208504834b [file] [log] [blame]
Rohan Bansal76825ab2020-04-28 16:08:52 +05301import frappe
Nabin Hait29b87152022-07-22 15:33:23 +05302from frappe.desk.notifications import notify_mentions
Nabin Haitf904ac52022-06-10 11:15:22 +05303from frappe.model.document import Document
Nabin Hait925b9d92022-06-27 21:58:19 +05304from frappe.utils import cstr, now, today
5from pypika import functions
Rohan Bansal76825ab2020-04-28 16:08:52 +05306
7
8def update_lead_phone_numbers(contact, method):
9 if contact.phone_nos:
10 contact_lead = contact.get_link_for("Lead")
11 if contact_lead:
12 phone = mobile_no = contact.phone_nos[0].phone
13
14 if len(contact.phone_nos) > 1:
15 # get the default phone number
Ankush Menat494bd9e2022-03-28 18:52:46 +053016 primary_phones = [
17 phone_doc.phone for phone_doc in contact.phone_nos if phone_doc.is_primary_phone
18 ]
Rohan Bansal76825ab2020-04-28 16:08:52 +053019 if primary_phones:
20 phone = primary_phones[0]
21
22 # get the default mobile number
Ankush Menat494bd9e2022-03-28 18:52:46 +053023 primary_mobile_nos = [
24 phone_doc.phone for phone_doc in contact.phone_nos if phone_doc.is_primary_mobile_no
25 ]
Rohan Bansal76825ab2020-04-28 16:08:52 +053026 if primary_mobile_nos:
27 mobile_no = primary_mobile_nos[0]
28
29 lead = frappe.get_doc("Lead", contact_lead)
Myuddin khatrifc0e45d2020-05-07 15:11:39 +053030 lead.db_set("phone", phone)
31 lead.db_set("mobile_no", mobile_no)
Anupam6eb77932021-11-19 10:13:05 +053032
Ankush Menat494bd9e2022-03-28 18:52:46 +053033
Anupam6eb77932021-11-19 10:13:05 +053034def copy_comments(doctype, docname, doc):
Ankush Menat494bd9e2022-03-28 18:52:46 +053035 comments = frappe.db.get_values(
36 "Comment",
37 filters={"reference_doctype": doctype, "reference_name": docname, "comment_type": "Comment"},
38 fieldname="*",
39 )
Anupam6eb77932021-11-19 10:13:05 +053040 for comment in comments:
Ankush Menat494bd9e2022-03-28 18:52:46 +053041 comment = frappe.get_doc(comment.update({"doctype": "Comment"}))
Anupam6eb77932021-11-19 10:13:05 +053042 comment.name = None
43 comment.reference_doctype = doc.doctype
44 comment.reference_name = doc.name
45 comment.insert()
46
Ankush Menat494bd9e2022-03-28 18:52:46 +053047
Nabin Haitf904ac52022-06-10 11:15:22 +053048def link_communications(doctype, docname, doc):
Anupam31ac1012021-12-17 19:21:08 +053049 communication_list = get_linked_communication_list(doctype, docname)
50
51 for communication in communication_list:
52 communication_doc = frappe.get_doc("Communication", communication)
53 communication_doc.add_link(doc.doctype, doc.name, autosave=True)
54
Ankush Menat494bd9e2022-03-28 18:52:46 +053055
Anupam31ac1012021-12-17 19:21:08 +053056def get_linked_communication_list(doctype, docname):
Ankush Menat494bd9e2022-03-28 18:52:46 +053057 communications = frappe.get_all(
58 "Communication", filters={"reference_doctype": doctype, "reference_name": docname}, pluck="name"
59 )
60 communication_links = frappe.get_all(
61 "Communication Link",
62 {"link_doctype": doctype, "link_name": docname, "parent": ("not in", communications)},
63 pluck="parent",
64 )
Anupam5b40d9e2021-12-18 20:12:57 +053065
Anupam31ac1012021-12-17 19:21:08 +053066 return communications + communication_links
Nabin Haitf904ac52022-06-10 11:15:22 +053067
68
69def link_communications_with_prospect(communication, method):
70 prospect = get_linked_prospect(communication.reference_doctype, communication.reference_name)
71
72 if prospect:
73 already_linked = any(
74 [
75 d.name
76 for d in communication.get("timeline_links")
77 if d.link_doctype == "Prospect" and d.link_name == prospect
78 ]
79 )
80 if not already_linked:
81 row = communication.append("timeline_links")
82 row.link_doctype = "Prospect"
83 row.link_name = prospect
84 row.db_update()
85
86
87def get_linked_prospect(reference_doctype, reference_name):
88 prospect = None
89 if reference_doctype == "Lead":
90 prospect = frappe.db.get_value("Prospect Lead", {"lead": reference_name}, "parent")
91
92 elif reference_doctype == "Opportunity":
93 opportunity_from, party_name = frappe.db.get_value(
94 "Opportunity", reference_name, ["opportunity_from", "party_name"]
95 )
96 if opportunity_from == "Lead":
Akhil Narang3effaf22024-03-27 11:37:26 +053097 prospect = frappe.db.get_value("Prospect Opportunity", {"opportunity": reference_name}, "parent")
Nabin Haitf904ac52022-06-10 11:15:22 +053098 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
Ankush Menat63b97952022-11-28 10:58:35 +0530121 todo_doc.save()
Nabin Haitf904ac52022-06-10 11:15:22 +0530122
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
Nabin Hait925b9d92022-06-27 21:58:19 +0530180def open_leads_opportunities_based_on_todays_event():
181 event = frappe.qb.DocType("Event")
182 event_link = frappe.qb.DocType("Event Participants")
183
184 query = (
185 frappe.qb.from_(event)
186 .join(event_link)
187 .on(event_link.parent == event.name)
188 .select(event_link.reference_doctype, event_link.reference_docname)
189 .where(
190 (event_link.reference_doctype.isin(["Lead", "Opportunity"]))
191 & (event.status == "Open")
192 & (functions.Date(event.starts_on) == today())
193 )
194 )
195 data = query.run(as_dict=True)
196
197 for d in data:
198 frappe.db.set_value(d.reference_doctype, d.reference_docname, "status", "Open")
199
200
Nabin Haitf904ac52022-06-10 11:15:22 +0530201class CRMNote(Document):
202 @frappe.whitelist()
203 def add_note(self, note):
204 self.append("notes", {"note": note, "added_by": frappe.session.user, "added_on": now()})
205 self.save()
Nabin Hait29b87152022-07-22 15:33:23 +0530206 notify_mentions(self.doctype, self.name, note)
Nabin Haitf904ac52022-06-10 11:15:22 +0530207
208 @frappe.whitelist()
209 def edit_note(self, note, row_id):
210 for d in self.notes:
211 if cstr(d.name) == row_id:
212 d.note = note
213 d.db_update()
214
215 @frappe.whitelist()
216 def delete_note(self, row_id):
217 for d in self.notes:
218 if cstr(d.name) == row_id:
219 self.remove(d)
220 break
221 self.save()