diff --git a/crm/api/doc.py b/crm/api/doc.py index 9a47b355..9795246c 100644 --- a/crm/api/doc.py +++ b/crm/api/doc.py @@ -188,11 +188,16 @@ def get_doctype_fields(doctype): else: section_fields.append(get_field_obj(field)) - all_fields = [] + section_fields = [] for section in sections: - all_fields.append(sections[section]) + section_fields.append(sections[section]) - return all_fields + fields = [field for field in fields if field.fieldtype not in "Tab Break"] + fields_meta = {} + for field in fields: + fields_meta[field.fieldname] = field + + return section_fields, fields_meta def get_field_obj(field): @@ -200,6 +205,9 @@ def get_field_obj(field): "label": field.label, "type": get_type(field), "name": field.fieldname, + "hidden": field.hidden, + "reqd": field.reqd, + "read_only": field.read_only, } obj["placeholder"] = "Add " + field.label + "..." diff --git a/crm/fcrm/doctype/crm_deal/api.py b/crm/fcrm/doctype/crm_deal/api.py index e37ca74d..2ab69e14 100644 --- a/crm/fcrm/doctype/crm_deal/api.py +++ b/crm/fcrm/doctype/crm_deal/api.py @@ -27,7 +27,7 @@ def get_deal(name): fields=["contact", "is_primary"], ) - deal["doctype_fields"] = get_doctype_fields("CRM Deal") + deal["doctype_fields"], deal["all_fields"] = get_doctype_fields("CRM Deal") deal["doctype"] = "CRM Deal" deal["_form_script"] = get_form_script('CRM Deal') return deal diff --git a/crm/fcrm/doctype/crm_deal/crm_deal.json b/crm/fcrm/doctype/crm_deal/crm_deal.json index 73ac91be..0f3f8b4a 100644 --- a/crm/fcrm/doctype/crm_deal/crm_deal.json +++ b/crm/fcrm/doctype/crm_deal/crm_deal.json @@ -52,19 +52,17 @@ "label": "Probability" }, { - "fetch_from": "organization.annual_revenue", + "fetch_from": ".annual_revenue", "fieldname": "annual_revenue", "fieldtype": "Currency", - "label": "Amount", - "read_only": 1 + "label": "Amount" }, { - "fetch_from": "organization.website", + "fetch_from": ".website", "fieldname": "website", "fieldtype": "Data", "label": "Website", - "options": "URL", - "read_only": 1 + "options": "URL" }, { "fieldname": "close_date", @@ -212,11 +210,11 @@ "options": "CRM Communication Status" }, { - "fetch_from": "organization.territory", + "fetch_from": ".territory", "fieldname": "territory", - "fieldtype": "Data", + "fieldtype": "Link", "label": "Territory", - "read_only": 1 + "options": "CRM Territory" }, { "fieldname": "source", @@ -227,7 +225,7 @@ ], "index_web_pages_for_search": 1, "links": [], - "modified": "2024-01-04 20:02:23.823826", + "modified": "2024-01-04 20:53:31.618792", "modified_by": "Administrator", "module": "FCRM", "name": "CRM Deal", diff --git a/crm/fcrm/doctype/crm_deal/crm_deal.py b/crm/fcrm/doctype/crm_deal/crm_deal.py index 7cc0b688..ee03ef52 100644 --- a/crm/fcrm/doctype/crm_deal/crm_deal.py +++ b/crm/fcrm/doctype/crm_deal/crm_deal.py @@ -17,6 +17,7 @@ class CRMDeal(Document): def validate(self): self.set_primary_contact() self.set_primary_email_mobile_no() + self.update_organization() if self.deal_owner and not self.is_new(): self.assign_agent(self.deal_owner) @@ -61,6 +62,20 @@ class CRMDeal(Document): self.email = "" self.mobile_no = "" + def update_organization(self): + if self.organization: + if self.has_value_changed("organization"): + organization = frappe.get_cached_doc("CRM Organization", self.organization) + self.website = organization.website + self.territory = organization.territory + self.annual_revenue = organization.annual_revenue + if self.has_value_changed("website"): + frappe.db.set_value("CRM Organization", self.organization, "website", self.website) + if self.has_value_changed("territory"): + frappe.db.set_value("CRM Organization", self.organization, "territory", self.territory) + if self.has_value_changed("annual_revenue"): + frappe.db.set_value("CRM Organization", self.organization, "annual_revenue", self.annual_revenue) + def assign_agent(self, agent): if not agent: return diff --git a/crm/fcrm/doctype/crm_lead/api.py b/crm/fcrm/doctype/crm_lead/api.py index 9758d045..87b64a83 100644 --- a/crm/fcrm/doctype/crm_lead/api.py +++ b/crm/fcrm/doctype/crm_lead/api.py @@ -15,7 +15,7 @@ def get_lead(name): frappe.throw(_("Lead not found"), frappe.DoesNotExistError) lead = lead.pop() - lead["doctype_fields"] = get_doctype_fields("CRM Lead") + lead["doctype_fields"], lead["all_fields"] = get_doctype_fields("CRM Lead") lead["doctype"] = "CRM Lead" lead["_form_script"] = get_form_script('CRM Lead') return lead diff --git a/crm/fcrm/doctype/crm_lead/crm_lead.json b/crm/fcrm/doctype/crm_lead/crm_lead.json index 70569d62..3090a4ae 100644 --- a/crm/fcrm/doctype/crm_lead/crm_lead.json +++ b/crm/fcrm/doctype/crm_lead/crm_lead.json @@ -101,12 +101,10 @@ "search_index": 1 }, { - "fetch_from": "organization.website", "fieldname": "website", "fieldtype": "Data", "label": "Website", - "options": "URL", - "read_only": 1 + "options": "URL" }, { "fieldname": "mobile_no", @@ -155,11 +153,10 @@ "options": "CRM Lead Source" }, { - "fetch_from": "organization.industry", "fieldname": "industry", - "fieldtype": "Data", + "fieldtype": "Link", "label": "Industry", - "read_only": 1 + "options": "CRM Industry" }, { "fieldname": "image", @@ -187,9 +184,8 @@ }, { "fieldname": "organization", - "fieldtype": "Link", - "label": "Organization", - "options": "CRM Organization" + "fieldtype": "Data", + "label": "Organization" }, { "default": "0", @@ -273,17 +269,16 @@ "options": "CRM Communication Status" }, { - "fetch_from": "organization.territory", "fieldname": "territory", - "fieldtype": "Data", + "fieldtype": "Link", "label": "Territory", - "read_only": 1 + "options": "CRM Territory" } ], "image_field": "image", "index_web_pages_for_search": 1, "links": [], - "modified": "2024-01-04 18:58:26.603817", + "modified": "2024-01-04 21:34:32.388456", "modified_by": "Administrator", "module": "FCRM", "name": "CRM Lead", diff --git a/crm/fcrm/doctype/crm_lead/crm_lead.py b/crm/fcrm/doctype/crm_lead/crm_lead.py index d9cd0174..ed080a5a 100644 --- a/crm/fcrm/doctype/crm_lead/crm_lead.py +++ b/crm/fcrm/doctype/crm_lead/crm_lead.py @@ -111,6 +111,26 @@ class CRMLead(Document): return contact.name + def create_organization(self): + if not self.organization: + return + + existing_organization = frappe.db.exists("CRM Organization", {"organization_name": self.organization}) + if existing_organization: + return existing_organization + + organization = frappe.new_doc("CRM Organization") + organization.update( + { + "organization_name": self.organization, + "website": self.website, + "territory": self.territory, + "annual_revenue": self.annual_revenue, + } + ) + organization.insert(ignore_permissions=True) + return organization.name + def contact_exists(self, throw=True): email_exist = frappe.db.exists("Contact Email", {"email_id": self.email}) phone_exist = frappe.db.exists("Contact Phone", {"phone": self.phone}) @@ -136,12 +156,12 @@ class CRMLead(Document): return False - def create_deal(self, contact): + def create_deal(self, contact, organization): deal = frappe.new_doc("CRM Deal") deal.update( { "lead": self.name, - "organization": self.organization, + "organization": organization, "deal_owner": self.lead_owner, "source": self.source, "contacts": [{"contact": contact}], @@ -279,6 +299,7 @@ def convert_to_deal(lead): lead.communication_status = 'Replied' lead.save() contact = lead.create_contact(False) - deal = lead.create_deal(contact) + organization = lead.create_organization() + deal = lead.create_deal(contact, organization) return deal diff --git a/crm/fcrm/doctype/crm_organization/crm_organization.py b/crm/fcrm/doctype/crm_organization/crm_organization.py index daca91ff..0636909c 100644 --- a/crm/fcrm/doctype/crm_organization/crm_organization.py +++ b/crm/fcrm/doctype/crm_organization/crm_organization.py @@ -1,11 +1,31 @@ # Copyright (c) 2023, Frappe Technologies Pvt. Ltd. and contributors # For license information, please see license.txt -# import frappe +import frappe from frappe.model.document import Document class CRMOrganization(Document): + def on_update(self): + self.update_deal_organization_fields() + + def update_deal_organization_fields(self): + if ( + self.has_value_changed("website") + or self.has_value_changed("territory") + or self.has_value_changed("annual_revenue") + ): + for deal in frappe.get_all( + "CRM Deal", + filters={"organization": self.name}, + ): + if self.has_value_changed("website"): + frappe.db.set_value("CRM Deal", deal.name, "website", self.website) + if self.has_value_changed("territory"): + frappe.db.set_value("CRM Deal", deal.name, "territory", self.territory) + if self.has_value_changed("annual_revenue"): + frappe.db.set_value("CRM Deal", deal.name, "annual_revenue", self.annual_revenue) + @staticmethod def sort_options(): return [ diff --git a/frontend/src/components/SectionFields.vue b/frontend/src/components/SectionFields.vue index 85c39f61..c6d504c2 100644 --- a/frontend/src/components/SectionFields.vue +++ b/frontend/src/components/SectionFields.vue @@ -3,16 +3,25 @@