Skip to content

Commit

Permalink
Merge pull request #24 from shariquerik/create-deal-doctype
Browse files Browse the repository at this point in the history
refactor: Create deal doctype & fix code accordingly
  • Loading branch information
shariquerik authored Nov 7, 2023
2 parents 6ad550d + 5b10e80 commit 168a3b3
Show file tree
Hide file tree
Showing 29 changed files with 672 additions and 345 deletions.
226 changes: 226 additions & 0 deletions crm/api/activities.py
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
24 changes: 16 additions & 8 deletions crm/fcrm/doctype/crm_call_log/crm_call_log.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"duration",
"medium",
"start_time",
"lead",
"reference_doctype",
"reference_docname",
"column_break_ufnp",
"to",
"type",
Expand Down Expand Up @@ -89,12 +90,6 @@
"fieldtype": "Datetime",
"label": "End Time"
},
{
"fieldname": "lead",
"fieldtype": "Link",
"label": "Lead/Deal",
"options": "CRM Lead"
},
{
"fieldname": "note",
"fieldtype": "Link",
Expand All @@ -114,11 +109,24 @@
"fieldtype": "Link",
"label": "Caller",
"options": "User"
},
{
"default": "CRM Lead",
"fieldname": "reference_doctype",
"fieldtype": "Link",
"label": "Reference Document Type",
"options": "DocType"
},
{
"fieldname": "reference_docname",
"fieldtype": "Dynamic Link",
"label": "Reference Name",
"options": "reference_doctype"
}
],
"index_web_pages_for_search": 1,
"links": [],
"modified": "2023-08-30 15:39:46.613734",
"modified": "2023-11-07 13:52:40.504747",
"modified_by": "Administrator",
"module": "FCRM",
"name": "CRM Call Log",
Expand Down
Empty file.
25 changes: 25 additions & 0 deletions crm/fcrm/doctype/crm_deal/api.py
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
8 changes: 8 additions & 0 deletions crm/fcrm/doctype/crm_deal/crm_deal.js
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) {

// },
// });
Loading

0 comments on commit 168a3b3

Please sign in to comment.