diff --git a/crm/fcrm/doctype/crm_lead/api.py b/crm/fcrm/doctype/crm_lead/api.py index 740a6e11..5f22efb4 100644 --- a/crm/fcrm/doctype/crm_lead/api.py +++ b/crm/fcrm/doctype/crm_lead/api.py @@ -21,29 +21,39 @@ def get_lead(name): frappe.throw(_("Lead not found"), frappe.DoesNotExistError) lead = lead.pop() + return lead + +@frappe.whitelist() +def get_activities(name): get_docinfo('', "CRM Lead", name) docinfo = frappe.response["docinfo"] - activities = get_activities(lead, docinfo) - - return { **lead, 'activities': activities } - -def get_activities(doc, docinfo): lead_fields_meta = frappe.get_meta("CRM Lead").fields + doc = frappe.db.get_values("CRM Lead", name, ["creation", "owner", "created_as_deal"])[0] + created_as_deal = doc[2] + is_lead = False if created_as_deal else True activities = [{ "activity_type": "creation", - "creation": doc.creation, - "owner": doc.owner, - "data": "created this lead", + "creation": doc[0], + "owner": doc[1], + "data": "created this " + ("deal" if created_as_deal else "lead"), + "is_lead": is_lead, }] + docinfo.versions.reverse() + for version in docinfo.versions: data = json.loads(version.data) if not data.get("changed"): continue + + field_option = None + if change := data.get("changed")[0]: + field_label, field_option = next(((f.label, f.options) for f in lead_fields_meta if f.fieldname == change[0]), None) activity_type = "changed" - field_label = next((f.label for f in lead_fields_meta if f.fieldname == change[0]), None) + if field_label == "Lead Owner" and (created_as_deal or not is_lead): + field_label = "Deal Owner" data = { "field": change[0], "field_label": field_label, @@ -59,6 +69,9 @@ def get_activities(doc, docinfo): "field_label": field_label, "value": change[2], } + if field_label == "Is Deal" and change[2] and is_lead: + activity_type = "deal" + is_lead = False elif change[1] and not change[2]: activity_type = "removed" data = { @@ -72,15 +85,8 @@ def get_activities(doc, docinfo): "creation": version.creation, "owner": version.owner, "data": data, - } - activities.append(activity) - - for comment in docinfo.comments: - activity = { - "activity_type": "comment", - "creation": comment.creation, - "owner": comment.owner, - "data": comment.content, + "is_lead": is_lead, + "options": field_option, } activities.append(activity) @@ -98,9 +104,44 @@ def get_activities(doc, docinfo): "bcc": communication.bcc, "read_by_recipient": communication.read_by_recipient, }, + "is_lead": is_lead, } activities.append(activity) activities.sort(key=lambda x: x["creation"], reverse=True) + activities = handle_multiple_versions(activities) - return activities \ No newline at end of file + return activities + +def handle_multiple_versions(versions): + activities = [] + grouped_versions = [] + old_version = None + for version in versions: + is_version = version["activity_type"] in ["changed", "added", "removed"] + if not is_version: + activities.append(version) + if not old_version: + old_version = version + if is_version: grouped_versions.append(version) + continue + if is_version and old_version.get("owner") and version["owner"] == old_version["owner"]: + grouped_versions.append(version) + else: + if grouped_versions: + activities.append(parse_grouped_versions(grouped_versions)) + grouped_versions = [] + if is_version: grouped_versions.append(version) + old_version = version + if version == versions[-1] and grouped_versions: + activities.append(parse_grouped_versions(grouped_versions)) + + return activities + +def parse_grouped_versions(versions): + version = versions[0] + if len(versions) == 1: + return version + other_versions = versions[1:] + version["other_versions"] = other_versions + return version \ No newline at end of file diff --git a/crm/fcrm/doctype/crm_lead/crm_lead.json b/crm/fcrm/doctype/crm_lead/crm_lead.json index 23e66072..2c774ba4 100644 --- a/crm/fcrm/doctype/crm_lead/crm_lead.json +++ b/crm/fcrm/doctype/crm_lead/crm_lead.json @@ -14,6 +14,7 @@ "middle_name", "last_name", "is_deal", + "created_as_deal", "column_break_izjs", "lead_name", "gender", @@ -251,12 +252,19 @@ { "fieldname": "section_break_jyxr", "fieldtype": "Section Break" + }, + { + "default": "0", + "fieldname": "created_as_deal", + "fieldtype": "Check", + "hidden": 1, + "label": "Created as Deal" } ], "image_field": "image", "index_web_pages_for_search": 1, "links": [], - "modified": "2023-08-25 19:21:22.778067", + "modified": "2023-09-27 18:54:18.196159", "modified_by": "Administrator", "module": "FCRM", "name": "CRM Lead", diff --git a/frontend/src/components/Activities.vue b/frontend/src/components/Activities.vue index 0d1328e3..2ffde9a1 100644 --- a/frontend/src/components/Activities.vue +++ b/frontend/src/components/Activities.vue @@ -1,26 +1,28 @@