From d85f3169957c8428f155e69a60ca066ef5ae8339 Mon Sep 17 00:00:00 2001 From: Bernhard Sirlinger Date: Wed, 12 Jun 2024 13:22:25 +0200 Subject: [PATCH] fix: correct creation of opportunity with lead dashboard plus button (#40987) --- erpnext/crm/doctype/lead/lead.js | 180 +++++++++++++++---------------- 1 file changed, 86 insertions(+), 94 deletions(-) diff --git a/erpnext/crm/doctype/lead/lead.js b/erpnext/crm/doctype/lead/lead.js index 848d697461a..8e568a2babe 100644 --- a/erpnext/crm/doctype/lead/lead.js +++ b/erpnext/crm/doctype/lead/lead.js @@ -29,13 +29,7 @@ erpnext.LeadController = class LeadController extends frappe.ui.form.Controller if (!this.frm.is_new() && doc.__onload && !doc.__onload.is_customer) { this.frm.add_custom_button(__("Customer"), this.make_customer, __("Create")); - this.frm.add_custom_button( - __("Opportunity"), - function () { - me.frm.trigger("make_opportunity"); - }, - __("Create") - ); + this.frm.add_custom_button(__("Opportunity"), this.make_opportunity, __("Create")); this.frm.add_custom_button(__("Quotation"), this.make_quotation, __("Create")); if (!doc.__onload.linked_prospects.length) { this.frm.add_custom_button(__("Prospect"), this.make_prospect, __("Create")); @@ -100,6 +94,91 @@ erpnext.LeadController = class LeadController extends frappe.ui.form.Controller }); } + async make_opportunity() { + let existing_prospect = ( + await frappe.db.get_value( + "Prospect Lead", + { + lead: this.frm.doc.name, + }, + "name", + null, + "Prospect" + ) + ).message.name; + + if (!existing_prospect) { + var fields = [ + { + label: "Create Prospect", + fieldname: "create_prospect", + fieldtype: "Check", + default: 1, + }, + { + label: "Prospect Name", + fieldname: "prospect_name", + fieldtype: "Data", + default: this.frm.doc.company_name, + depends_on: "create_prospect", + }, + ]; + } + let existing_contact = ( + await frappe.db.get_value( + "Contact", + { + first_name: this.frm.doc.first_name || this.frm.doc.lead_name, + last_name: this.frm.doc.last_name, + }, + "name" + ) + ).message.name; + + if (!existing_contact) { + fields.push({ + label: "Create Contact", + fieldname: "create_contact", + fieldtype: "Check", + default: "1", + }); + } + + if (fields) { + var d = new frappe.ui.Dialog({ + title: __("Create Opportunity"), + fields: fields, + primary_action: function () { + var data = d.get_values(); + frappe.call({ + method: "create_prospect_and_contact", + doc: this.frm.doc, + args: { + data: data, + }, + freeze: true, + callback: function (r) { + if (!r.exc) { + frappe.model.open_mapped_doc({ + method: "erpnext.crm.doctype.lead.lead.make_opportunity", + frm: this.frm, + }); + } + d.hide(); + }, + }); + }, + primary_action_label: __("Create"), + }); + d.show(); + } else { + frappe.model.open_mapped_doc({ + method: "erpnext.crm.doctype.lead.lead.make_opportunity", + frm: this.frm, + }); + } + } + make_prospect() { const me = this; frappe.model.with_doctype("Prospect", function () { @@ -151,90 +230,3 @@ erpnext.LeadController = class LeadController extends frappe.ui.form.Controller }; extend_cscript(cur_frm.cscript, new erpnext.LeadController({ frm: cur_frm })); - -frappe.ui.form.on("Lead", { - make_opportunity: async function (frm) { - let existing_prospect = ( - await frappe.db.get_value( - "Prospect Lead", - { - lead: frm.doc.name, - }, - "name", - null, - "Prospect" - ) - ).message.name; - - if (!existing_prospect) { - var fields = [ - { - label: "Create Prospect", - fieldname: "create_prospect", - fieldtype: "Check", - default: 1, - }, - { - label: "Prospect Name", - fieldname: "prospect_name", - fieldtype: "Data", - default: frm.doc.company_name, - depends_on: "create_prospect", - }, - ]; - } - let existing_contact = ( - await frappe.db.get_value( - "Contact", - { - first_name: frm.doc.first_name || frm.doc.lead_name, - last_name: frm.doc.last_name, - }, - "name" - ) - ).message.name; - - if (!existing_contact) { - fields.push({ - label: "Create Contact", - fieldname: "create_contact", - fieldtype: "Check", - default: "1", - }); - } - - if (fields) { - var d = new frappe.ui.Dialog({ - title: __("Create Opportunity"), - fields: fields, - primary_action: function () { - var data = d.get_values(); - frappe.call({ - method: "create_prospect_and_contact", - doc: frm.doc, - args: { - data: data, - }, - freeze: true, - callback: function (r) { - if (!r.exc) { - frappe.model.open_mapped_doc({ - method: "erpnext.crm.doctype.lead.lead.make_opportunity", - frm: frm, - }); - } - d.hide(); - }, - }); - }, - primary_action_label: __("Create"), - }); - d.show(); - } else { - frappe.model.open_mapped_doc({ - method: "erpnext.crm.doctype.lead.lead.make_opportunity", - frm: frm, - }); - } - }, -});