-
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 #24 from shariquerik/create-deal-doctype
refactor: Create deal doctype & fix code accordingly
- Loading branch information
Showing
29 changed files
with
672 additions
and
345 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,226 @@ | ||
import json | ||
|
||
import frappe | ||
from frappe import _ | ||
from frappe.desk.form.load import get_docinfo | ||
|
||
@frappe.whitelist() | ||
def get_activities(name): | ||
if frappe.db.exists("CRM Deal", name): | ||
return get_deal_activities(name) | ||
elif frappe.db.exists("CRM Lead", name): | ||
return get_lead_activities(name) | ||
else: | ||
frappe.throw(_("Document not found"), frappe.DoesNotExistError) | ||
|
||
def get_deal_activities(name): | ||
get_docinfo('', "CRM Deal", name) | ||
docinfo = frappe.response["docinfo"] | ||
deal_fields_meta = frappe.get_meta("CRM Deal").fields | ||
|
||
doc = frappe.db.get_values("CRM Deal", name, ["creation", "owner", "lead"])[0] | ||
lead = doc[2] | ||
|
||
activities = [] | ||
creation_text = "created this deal" | ||
|
||
if lead: | ||
activities = get_lead_activities(lead) | ||
creation_text = "converted the lead to this deal" | ||
|
||
activities.append({ | ||
"activity_type": "creation", | ||
"creation": doc[0], | ||
"owner": doc[1], | ||
"data": creation_text, | ||
"is_lead": False, | ||
}) | ||
|
||
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 deal_fields_meta if f.fieldname == change[0]), None) | ||
|
||
if field_label == "Lead" or (not change[1] and not change[2]): | ||
continue | ||
|
||
activity_type = "changed" | ||
data = { | ||
"field": change[0], | ||
"field_label": field_label, | ||
"old_value": change[1], | ||
"value": change[2], | ||
} | ||
|
||
if not change[1] and change[2]: | ||
activity_type = "added" | ||
data = { | ||
"field": change[0], | ||
"field_label": field_label, | ||
"value": change[2], | ||
} | ||
elif change[1] and not change[2]: | ||
activity_type = "removed" | ||
data = { | ||
"field": change[0], | ||
"field_label": field_label, | ||
"value": change[1], | ||
} | ||
|
||
activity = { | ||
"activity_type": activity_type, | ||
"creation": version.creation, | ||
"owner": version.owner, | ||
"data": data, | ||
"is_lead": False, | ||
"options": field_option, | ||
} | ||
activities.append(activity) | ||
|
||
for communication in docinfo.communications: | ||
activity = { | ||
"activity_type": "communication", | ||
"creation": communication.creation, | ||
"data": { | ||
"subject": communication.subject, | ||
"content": communication.content, | ||
"sender_full_name": communication.sender_full_name, | ||
"sender": communication.sender, | ||
"recipients": communication.recipients, | ||
"cc": communication.cc, | ||
"bcc": communication.bcc, | ||
"read_by_recipient": communication.read_by_recipient, | ||
}, | ||
"is_lead": False, | ||
} | ||
activities.append(activity) | ||
|
||
activities.sort(key=lambda x: x["creation"], reverse=True) | ||
activities = handle_multiple_versions(activities) | ||
|
||
return activities | ||
|
||
def get_lead_activities(name): | ||
get_docinfo('', "CRM Lead", name) | ||
docinfo = frappe.response["docinfo"] | ||
lead_fields_meta = frappe.get_meta("CRM Lead").fields | ||
|
||
doc = frappe.db.get_values("CRM Lead", name, ["creation", "owner"])[0] | ||
activities = [{ | ||
"activity_type": "creation", | ||
"creation": doc[0], | ||
"owner": doc[1], | ||
"data": "created this lead", | ||
"is_lead": True, | ||
}] | ||
|
||
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) | ||
|
||
if field_label == "Converted" or (not change[1] and not change[2]): | ||
continue | ||
|
||
activity_type = "changed" | ||
data = { | ||
"field": change[0], | ||
"field_label": field_label, | ||
"old_value": change[1], | ||
"value": change[2], | ||
} | ||
|
||
if not change[1] and change[2]: | ||
activity_type = "added" | ||
data = { | ||
"field": change[0], | ||
"field_label": field_label, | ||
"value": change[2], | ||
} | ||
elif change[1] and not change[2]: | ||
activity_type = "removed" | ||
data = { | ||
"field": change[0], | ||
"field_label": field_label, | ||
"value": change[1], | ||
} | ||
|
||
activity = { | ||
"activity_type": activity_type, | ||
"creation": version.creation, | ||
"owner": version.owner, | ||
"data": data, | ||
"is_lead": True, | ||
"options": field_option, | ||
} | ||
activities.append(activity) | ||
|
||
for communication in docinfo.communications: | ||
activity = { | ||
"activity_type": "communication", | ||
"creation": communication.creation, | ||
"data": { | ||
"subject": communication.subject, | ||
"content": communication.content, | ||
"sender_full_name": communication.sender_full_name, | ||
"sender": communication.sender, | ||
"recipients": communication.recipients, | ||
"cc": communication.cc, | ||
"bcc": communication.bcc, | ||
"read_by_recipient": communication.read_by_recipient, | ||
}, | ||
"is_lead": True, | ||
} | ||
activities.append(activity) | ||
|
||
activities.sort(key=lambda x: x["creation"], reverse=True) | ||
activities = handle_multiple_versions(activities) | ||
|
||
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 |
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
Empty file.
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,25 @@ | ||
import json | ||
|
||
import frappe | ||
from frappe import _ | ||
from frappe.desk.form.load import get_docinfo | ||
from crm.fcrm.doctype.crm_lead.api import get_activities as get_lead_activities | ||
|
||
|
||
@frappe.whitelist() | ||
def get_deal(name): | ||
Deal = frappe.qb.DocType("CRM Deal") | ||
|
||
query = ( | ||
frappe.qb.from_(Deal) | ||
.select("*") | ||
.where(Deal.name == name) | ||
.limit(1) | ||
) | ||
|
||
deal = query.run(as_dict=True) | ||
if not len(deal): | ||
frappe.throw(_("Deal not found"), frappe.DoesNotExistError) | ||
deal = deal.pop() | ||
|
||
return deal |
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,8 @@ | ||
// Copyright (c) 2023, Frappe Technologies Pvt. Ltd. and contributors | ||
// For license information, please see license.txt | ||
|
||
// frappe.ui.form.on("CRM Deal", { | ||
// refresh(frm) { | ||
|
||
// }, | ||
// }); |
Oops, something went wrong.