fix: get child table meta

This commit is contained in:
Shariq Ansari 2024-12-23 16:10:25 +05:30
parent 1450163379
commit ed11fb48cb
3 changed files with 157 additions and 86 deletions

View File

@ -557,6 +557,9 @@ def get_fields_meta(doctype, restricted_fieldtypes=None, as_array=False):
fields_meta = {}
for field in fields:
fields_meta[field.get("fieldname")] = field
if field.get("fieldtype") == "Table":
_fields = frappe.get_meta(field.get("options")).fields
fields_meta[field.get("fieldname")] = {"df": field, "fields": _fields}
return fields_meta

View File

@ -1,6 +1,5 @@
# Copyright (c) 2023, Frappe Technologies Pvt. Ltd. and contributors
# For license information, please see license.txt
import json
import frappe
from frappe import _
@ -8,7 +7,9 @@ from frappe.desk.form.assign_to import add as assign
from frappe.model.document import Document
from crm.fcrm.doctype.crm_service_level_agreement.utils import get_sla
from crm.fcrm.doctype.crm_status_change_log.crm_status_change_log import add_status_change_log
from crm.fcrm.doctype.crm_status_change_log.crm_status_change_log import (
add_status_change_log,
)
class CRMDeal(Document):
@ -94,19 +95,26 @@ class CRMDeal(Document):
shared_with = [d.user for d in docshares] + [agent]
for user in shared_with:
if user == agent and not frappe.db.exists("DocShare", {"user": agent, "share_name": self.name, "share_doctype": self.doctype}):
if user == agent and not frappe.db.exists(
"DocShare",
{"user": agent, "share_name": self.name, "share_doctype": self.doctype},
):
frappe.share.add_docshare(
self.doctype, self.name, agent, write=1, flags={"ignore_share_permission": True}
self.doctype,
self.name,
agent,
write=1,
flags={"ignore_share_permission": True},
)
elif user != agent:
frappe.share.remove(self.doctype, self.name, user)
def set_sla(self):
"""
Find an SLA to apply to the deal.
"""
if self.sla: return
if self.sla:
return
sla = get_sla(self)
if not sla:
@ -129,48 +137,48 @@ class CRMDeal(Document):
def default_list_data():
columns = [
{
'label': 'Organization',
'type': 'Link',
'key': 'organization',
'options': 'CRM Organization',
'width': '11rem',
"label": "Organization",
"type": "Link",
"key": "organization",
"options": "CRM Organization",
"width": "11rem",
},
{
'label': 'Amount',
'type': 'Currency',
'key': 'annual_revenue',
'align': 'right',
'width': '9rem',
"label": "Annual Revenue",
"type": "Currency",
"key": "annual_revenue",
"align": "right",
"width": "9rem",
},
{
'label': 'Status',
'type': 'Select',
'key': 'status',
'width': '10rem',
"label": "Status",
"type": "Select",
"key": "status",
"width": "10rem",
},
{
'label': 'Email',
'type': 'Data',
'key': 'email',
'width': '12rem',
"label": "Email",
"type": "Data",
"key": "email",
"width": "12rem",
},
{
'label': 'Mobile No',
'type': 'Data',
'key': 'mobile_no',
'width': '11rem',
"label": "Mobile No",
"type": "Data",
"key": "mobile_no",
"width": "11rem",
},
{
'label': 'Assigned To',
'type': 'Text',
'key': '_assign',
'width': '10rem',
"label": "Assigned To",
"type": "Text",
"key": "_assign",
"width": "10rem",
},
{
'label': 'Last Modified',
'type': 'Datetime',
'key': 'modified',
'width': '8rem',
"label": "Last Modified",
"type": "Datetime",
"key": "modified",
"width": "8rem",
},
]
rows = [
@ -189,16 +197,17 @@ class CRMDeal(Document):
"modified",
"_assign",
]
return {'columns': columns, 'rows': rows}
return {"columns": columns, "rows": rows}
@staticmethod
def default_kanban_settings():
return {
"column_field": "status",
"title_field": "organization",
"kanban_fields": '["annual_revenue", "email", "mobile_no", "_assign", "modified"]'
"kanban_fields": '["annual_revenue", "email", "mobile_no", "_assign", "modified"]',
}
@frappe.whitelist()
def add_contact(deal, contact):
if not frappe.has_permission("CRM Deal", "write", deal):
@ -209,6 +218,7 @@ def add_contact(deal, contact):
deal.save()
return True
@frappe.whitelist()
def remove_contact(deal, contact):
if not frappe.has_permission("CRM Deal", "write", deal):
@ -219,6 +229,7 @@ def remove_contact(deal, contact):
deal.save()
return True
@frappe.whitelist()
def set_primary_contact(deal, contact):
if not frappe.has_permission("CRM Deal", "write", deal):
@ -229,11 +240,14 @@ def set_primary_contact(deal, contact):
deal.save()
return True
def create_organization(doc):
if not doc.get("organization_name"):
return
existing_organization = frappe.db.exists("CRM Organization", {"organization_name": doc.get("organization_name")})
existing_organization = frappe.db.exists(
"CRM Organization", {"organization_name": doc.get("organization_name")}
)
if existing_organization:
return existing_organization
@ -250,6 +264,7 @@ def create_organization(doc):
organization.insert(ignore_permissions=True)
return organization.name
def contact_exists(doc):
email_exist = frappe.db.exists("Contact Email", {"email_id": doc.get("email")})
mobile_exist = frappe.db.exists("Contact Phone", {"phone": doc.get("mobile_no")})
@ -262,6 +277,7 @@ def contact_exists(doc):
return False
def create_contact(doc):
existing_contact = contact_exists(doc)
if existing_contact:
@ -288,18 +304,23 @@ def create_contact(doc):
return contact.name
@frappe.whitelist()
def create_deal(args):
deal = frappe.new_doc("CRM Deal")
contact = args.get("contact")
if not contact and (args.get("first_name") or args.get("last_name") or args.get("email") or args.get("mobile_no")):
if not contact and (
args.get("first_name") or args.get("last_name") or args.get("email") or args.get("mobile_no")
):
contact = create_contact(args)
deal.update({
"organization": args.get("organization") or create_organization(args),
"contacts": [{"contact": contact, "is_primary": 1}] if contact else [],
})
deal.update(
{
"organization": args.get("organization") or create_organization(args),
"contacts": [{"contact": contact, "is_primary": 1}] if contact else [],
}
)
args.pop("organization", None)

View File

@ -1,15 +1,16 @@
# Copyright (c) 2023, Frappe Technologies Pvt. Ltd. and contributors
# For license information, please see license.txt
import json
import frappe
from frappe import _
from frappe.desk.form.assign_to import add as assign
from frappe.model.document import Document
from frappe.utils import has_gravatar, validate_email_address
from crm.fcrm.doctype.crm_service_level_agreement.utils import get_sla
from crm.fcrm.doctype.crm_status_change_log.crm_status_change_log import add_status_change_log
from crm.fcrm.doctype.crm_status_change_log.crm_status_change_log import (
add_status_change_log,
)
class CRMLead(Document):
@ -37,7 +38,15 @@ class CRMLead(Document):
def set_full_name(self):
if self.first_name:
self.lead_name = " ".join(
filter(None, [self.salutation, self.first_name, self.middle_name, self.last_name])
filter(
None,
[
self.salutation,
self.first_name,
self.middle_name,
self.last_name,
],
)
)
def set_lead_name(self):
@ -92,9 +101,16 @@ class CRMLead(Document):
shared_with = [d.user for d in docshares] + [agent]
for user in shared_with:
if user == agent and not frappe.db.exists("DocShare", {"user": agent, "share_name": self.name, "share_doctype": self.doctype}):
if user == agent and not frappe.db.exists(
"DocShare",
{"user": agent, "share_name": self.name, "share_doctype": self.doctype},
):
frappe.share.add_docshare(
self.doctype, self.name, agent, write=1, flags={"ignore_share_permission": True}
self.doctype,
self.name,
agent,
write=1,
flags={"ignore_share_permission": True},
)
elif user != agent:
frappe.share.remove(self.doctype, self.name, user)
@ -188,8 +204,36 @@ class CRMLead(Document):
"lead_owner": "deal_owner",
}
restricted_fieldtypes = ["Tab Break", "Section Break", "Column Break", "HTML", "Button", "Attach", "Table"]
restricted_map_fields = ["name", "naming_series", "creation", "owner", "modified", "modified_by", "idx", "docstatus", "status", "email", "mobile_no", "phone", "sla", "sla_status", "response_by", "first_response_time", "first_responded_on", "communication_status", "sla_creation"]
restricted_fieldtypes = [
"Tab Break",
"Section Break",
"Column Break",
"HTML",
"Button",
"Attach",
"Table",
]
restricted_map_fields = [
"name",
"naming_series",
"creation",
"owner",
"modified",
"modified_by",
"idx",
"docstatus",
"status",
"email",
"mobile_no",
"phone",
"sla",
"sla_status",
"response_by",
"first_response_time",
"first_responded_on",
"communication_status",
"sla_creation",
]
for field in self.meta.fields:
if field.fieldtype in restricted_fieldtypes:
@ -222,7 +266,7 @@ class CRMLead(Document):
"sla_status": self.sla_status,
"communication_status": self.communication_status,
"first_response_time": self.first_response_time,
"first_responded_on": self.first_responded_on
"first_responded_on": self.first_responded_on,
}
)
@ -233,7 +277,8 @@ class CRMLead(Document):
"""
Find an SLA to apply to the lead.
"""
if self.sla: return
if self.sla:
return
sla = get_sla(self)
if not sla:
@ -263,47 +308,47 @@ class CRMLead(Document):
def default_list_data():
columns = [
{
'label': 'Name',
'type': 'Data',
'key': 'lead_name',
'width': '12rem',
"label": "Name",
"type": "Data",
"key": "lead_name",
"width": "12rem",
},
{
'label': 'Organization',
'type': 'Link',
'key': 'organization',
'options': 'CRM Organization',
'width': '10rem',
"label": "Organization",
"type": "Link",
"key": "organization",
"options": "CRM Organization",
"width": "10rem",
},
{
'label': 'Status',
'type': 'Select',
'key': 'status',
'width': '8rem',
"label": "Status",
"type": "Select",
"key": "status",
"width": "8rem",
},
{
'label': 'Email',
'type': 'Data',
'key': 'email',
'width': '12rem',
"label": "Email",
"type": "Data",
"key": "email",
"width": "12rem",
},
{
'label': 'Mobile No',
'type': 'Data',
'key': 'mobile_no',
'width': '11rem',
"label": "Mobile No",
"type": "Data",
"key": "mobile_no",
"width": "11rem",
},
{
'label': 'Assigned To',
'type': 'Text',
'key': '_assign',
'width': '10rem',
"label": "Assigned To",
"type": "Text",
"key": "_assign",
"width": "10rem",
},
{
'label': 'Last Modified',
'type': 'Datetime',
'key': 'modified',
'width': '8rem',
"label": "Last Modified",
"type": "Datetime",
"key": "modified",
"width": "8rem",
},
]
rows = [
@ -323,20 +368,22 @@ class CRMLead(Document):
"_assign",
"image",
]
return {'columns': columns, 'rows': rows}
return {"columns": columns, "rows": rows}
@staticmethod
def default_kanban_settings():
return {
"column_field": "status",
"title_field": "lead_name",
"kanban_fields": '["organization", "email", "mobile_no", "_assign", "modified"]'
"kanban_fields": '["organization", "email", "mobile_no", "_assign", "modified"]',
}
@frappe.whitelist()
def convert_to_deal(lead, doc=None):
if not (doc and doc.flags.get("ignore_permissions")) and not frappe.has_permission("CRM Lead", "write", lead):
if not (doc and doc.flags.get("ignore_permissions")) and not frappe.has_permission(
"CRM Lead", "write", lead
):
frappe.throw(_("Not allowed to convert Lead to Deal"), frappe.PermissionError)
lead = frappe.get_cached_doc("CRM Lead", lead)