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