From d4605771dae7d9171052cecf271bd3c716bfc93b Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Sat, 13 Jun 2026 09:08:30 +0000 Subject: [PATCH] fix: opportunity creation from contact us page (backport #55841) (#55866) * fix: opportunity creation from contact us page (#55841) (cherry picked from commit c933e34914e9ef0716451d4dc456e45d69364ff1) # Conflicts: # erpnext/crm/doctype/crm_settings/crm_settings.json # erpnext/templates/utils.py * chore: resolve conflict (crm_settings.json) * chore: resolve conflicts (send_message) --------- Co-authored-by: Diptanil Saha --- erpnext/crm/doctype/crm_settings/crm_settings.json | 11 +++++++++-- erpnext/crm/doctype/crm_settings/crm_settings.py | 13 +++++++++++++ erpnext/crm/utils.py | 5 +++++ erpnext/hooks.py | 3 +++ erpnext/templates/utils.py | 12 +++++++++++- 5 files changed, 41 insertions(+), 3 deletions(-) diff --git a/erpnext/crm/doctype/crm_settings/crm_settings.json b/erpnext/crm/doctype/crm_settings/crm_settings.json index f9dc3902c8f..28d43c97d29 100644 --- a/erpnext/crm/doctype/crm_settings/crm_settings.json +++ b/erpnext/crm/doctype/crm_settings/crm_settings.json @@ -14,6 +14,7 @@ "opportunity_section", "close_opportunity_after_days", "column_break_9", + "enable_opportunity_creation_from_contact_us", "quotation_section", "default_valid_till", "section_break_13", @@ -98,13 +99,19 @@ "fieldname": "update_timestamp_on_new_communication", "fieldtype": "Check", "label": "Update timestamp on new communication" + }, + { + "default": "0", + "fieldname": "enable_opportunity_creation_from_contact_us", + "fieldtype": "Check", + "label": "Enable Opportunity Creation from Contact Us" } ], "icon": "fa fa-cog", "index_web_pages_for_search": 1, "issingle": 1, "links": [], - "modified": "2025-01-16 16:12:14.889455", + "modified": "2026-06-11 23:09:49.750381", "modified_by": "Administrator", "module": "CRM", "name": "CRM Settings", @@ -144,4 +151,4 @@ "sort_order": "DESC", "states": [], "track_changes": 1 -} \ No newline at end of file +} diff --git a/erpnext/crm/doctype/crm_settings/crm_settings.py b/erpnext/crm/doctype/crm_settings/crm_settings.py index 23992043145..01cdaf41bde 100644 --- a/erpnext/crm/doctype/crm_settings/crm_settings.py +++ b/erpnext/crm/doctype/crm_settings/crm_settings.py @@ -2,6 +2,7 @@ # For license information, please see license.txt import frappe +from frappe import _ from frappe.model.document import Document @@ -20,8 +21,20 @@ class CRMSettings(Document): carry_forward_communication_and_comments: DF.Check close_opportunity_after_days: DF.Int default_valid_till: DF.Data | None + enable_opportunity_creation_from_contact_us: DF.Check update_timestamp_on_new_communication: DF.Check # end: auto-generated types def validate(self): frappe.db.set_default("campaign_naming_by", self.get("campaign_naming_by", "")) + self.validate_enable_opportunity_creation_from_contact_us() + + def validate_enable_opportunity_creation_from_contact_us(self): + contact_disabled = frappe.get_single_value("Contact Us Settings", "is_disabled") + + if self.enable_opportunity_creation_from_contact_us and contact_disabled: + frappe.throw( + _( + "Cannot enable Opportunity creation from Contact Us because the Contact Us form is disabled." + ) + ) diff --git a/erpnext/crm/utils.py b/erpnext/crm/utils.py index eb784c28ca7..8e6574bde4d 100644 --- a/erpnext/crm/utils.py +++ b/erpnext/crm/utils.py @@ -5,6 +5,11 @@ from frappe.utils import cstr, now, today from pypika import functions +def disable_opportunity_creation_on_contact_us_disabled(doc, method): + if doc.is_disabled: + frappe.db.set_single_value("CRM Settings", "enable_opportunity_creation_from_contact_us", 0) + + def update_lead_phone_numbers(contact, method): if contact.phone_nos: contact_lead = contact.get_link_for("Lead") diff --git a/erpnext/hooks.py b/erpnext/hooks.py index f3ccc5783a7..a1c64b60377 100644 --- a/erpnext/hooks.py +++ b/erpnext/hooks.py @@ -355,6 +355,9 @@ doc_events = { "Event": { "after_insert": "erpnext.crm.utils.link_events_with_prospect", }, + "Contact Us Settings": { + "on_update": "erpnext.crm.utils.disable_opportunity_creation_on_contact_us_disabled", + }, "Sales Invoice": { "on_submit": [ "erpnext.regional.create_transaction_log", diff --git a/erpnext/templates/utils.py b/erpnext/templates/utils.py index 15af9f0f014..fef82b8ea9e 100644 --- a/erpnext/templates/utils.py +++ b/erpnext/templates/utils.py @@ -3,10 +3,12 @@ import frappe +from frappe.rate_limiter import rate_limit from frappe.utils import escape_html -@frappe.whitelist(allow_guest=True) +@frappe.whitelist(allow_guest=True, methods=["POST"]) +@rate_limit(limit=10, seconds=3 * 60) def send_message(sender, message, subject="Website Query"): from frappe.www.contact import send_message as website_send_message @@ -14,6 +16,14 @@ def send_message(sender, message, subject="Website Query"): message = escape_html(message) + oppotunity_creation = frappe.get_single_value( + "CRM Settings", "enable_opportunity_creation_from_contact_us" + ) + + if not oppotunity_creation: + # Meant to silently fail instead of throwing error. + return + lead = customer = None customer = frappe.db.sql( """select distinct dl.link_name from `tabDynamic Link` dl