diff --git a/erpnext/crm/doctype/lead/lead.js b/erpnext/crm/doctype/lead/lead.js index 609eab7f9a27..e50cf9e4dd0b 100644 --- a/erpnext/crm/doctype/lead/lead.js +++ b/erpnext/crm/doctype/lead/lead.js @@ -28,18 +28,18 @@ erpnext.LeadController = class LeadController extends frappe.ui.form.Controller erpnext.toggle_naming_series(); 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(__("Quotation"), this.make_quotation, __("Create")); + this.frm.add_custom_button(__("Customer"), this.make_customer.bind(this), __("Create")); + this.frm.add_custom_button(__("Opportunity"), this.make_opportunity.bind(this), __("Create")); + this.frm.add_custom_button(__("Quotation"), this.make_quotation.bind(this), __("Create")); if (!doc.__onload.linked_prospects.length) { - this.frm.add_custom_button(__("Prospect"), this.make_prospect, __("Create")); - this.frm.add_custom_button(__("Add to Prospect"), this.add_lead_to_prospect, __("Action")); + this.frm.add_custom_button(__("Prospect"), this.make_prospect.bind(this), __("Create")); + this.frm.add_custom_button( + __("Add to Prospect"), + () => { + this.add_lead_to_prospect(this.frm); + }, + __("Action") + ); } } @@ -53,8 +53,7 @@ erpnext.LeadController = class LeadController extends frappe.ui.form.Controller this.show_activities(); } - add_lead_to_prospect() { - let me = this; + add_lead_to_prospect(frm) { frappe.prompt( [ { @@ -69,12 +68,12 @@ erpnext.LeadController = class LeadController extends frappe.ui.form.Controller frappe.call({ method: "erpnext.crm.doctype.lead.lead.add_lead_to_prospect", args: { - lead: me.frm.doc.name, + lead: frm.doc.name, prospect: data.prospect, }, callback: function (r) { if (!r.exc) { - me.frm.reload_doc(); + frm.reload_doc(); } }, freeze: true, @@ -89,70 +88,19 @@ erpnext.LeadController = class LeadController extends frappe.ui.form.Controller make_customer() { frappe.model.open_mapped_doc({ method: "erpnext.crm.doctype.lead.lead.make_customer", - frm: cur_frm, + frm: this.frm, }); } make_quotation() { frappe.model.open_mapped_doc({ method: "erpnext.crm.doctype.lead.lead.make_quotation", - frm: cur_frm, - }); - } - - make_prospect() { - frappe.model.with_doctype("Prospect", function () { - let prospect = frappe.model.get_new_doc("Prospect"); - prospect.company_name = cur_frm.doc.company_name; - prospect.no_of_employees = cur_frm.doc.no_of_employees; - prospect.industry = cur_frm.doc.industry; - prospect.market_segment = cur_frm.doc.market_segment; - prospect.territory = cur_frm.doc.territory; - prospect.fax = cur_frm.doc.fax; - prospect.website = cur_frm.doc.website; - prospect.prospect_owner = cur_frm.doc.lead_owner; - prospect.notes = cur_frm.doc.notes; - - let leads_row = frappe.model.add_child(prospect, "leads"); - leads_row.lead = cur_frm.doc.name; - - frappe.set_route("Form", "Prospect", prospect.name); - }); - } - - company_name() { - if (!this.frm.doc.lead_name) { - this.frm.set_value("lead_name", this.frm.doc.company_name); - } - } - - show_notes() { - if (this.frm.doc.docstatus == 1) return; - - const crm_notes = new erpnext.utils.CRMNotes({ frm: this.frm, - notes_wrapper: $(this.frm.fields_dict.notes_html.wrapper), }); - crm_notes.refresh(); } - show_activities() { - if (this.frm.doc.docstatus == 1) return; - - const crm_activities = new erpnext.utils.CRMActivities({ - frm: this.frm, - open_activities_wrapper: $(this.frm.fields_dict.open_activities_html.wrapper), - all_activities_wrapper: $(this.frm.fields_dict.all_activities_html.wrapper), - form_wrapper: $(this.frm.wrapper), - }); - crm_activities.refresh(); - } -}; - -extend_cscript(cur_frm.cscript, new erpnext.LeadController({ frm: cur_frm })); - -frappe.ui.form.on("Lead", { - make_opportunity: async function (frm) { + async make_opportunity() { + const frm = this.frm; let existing_prospect = ( await frappe.db.get_value( "Prospect Lead", @@ -163,10 +111,11 @@ frappe.ui.form.on("Lead", { null, "Prospect" ) - ).message.name; + ).message?.name; + let fields = []; if (!existing_prospect) { - var fields = [ + fields.push( { label: "Create Prospect", fieldname: "create_prospect", @@ -178,10 +127,14 @@ frappe.ui.form.on("Lead", { fieldname: "prospect_name", fieldtype: "Data", default: frm.doc.company_name, + reqd: 1, depends_on: "create_prospect", - }, - ]; + } + ); } + + await frm.reload_doc(); + let existing_contact = ( await frappe.db.get_value( "Contact", @@ -191,7 +144,7 @@ frappe.ui.form.on("Lead", { }, "name" ) - ).message.name; + ).message?.name; if (!existing_contact) { fields.push({ @@ -202,12 +155,11 @@ frappe.ui.form.on("Lead", { }); } - if (fields) { - var d = new frappe.ui.Dialog({ + if (fields.length) { + const d = new frappe.ui.Dialog({ title: __("Create Opportunity"), fields: fields, - primary_action: function () { - var data = d.get_values(); + primary_action: function (data) { frappe.call({ method: "create_prospect_and_contact", doc: frm.doc, @@ -235,5 +187,56 @@ frappe.ui.form.on("Lead", { frm: frm, }); } - }, -}); + } + + make_prospect() { + const me = this; + frappe.model.with_doctype("Prospect", function () { + let prospect = frappe.model.get_new_doc("Prospect"); + prospect.company_name = me.frm.doc.company_name; + prospect.no_of_employees = me.frm.doc.no_of_employees; + prospect.industry = me.frm.doc.industry; + prospect.market_segment = me.frm.doc.market_segment; + prospect.territory = me.frm.doc.territory; + prospect.fax = me.frm.doc.fax; + prospect.website = me.frm.doc.website; + prospect.prospect_owner = me.frm.doc.lead_owner; + prospect.notes = me.frm.doc.notes; + + let leads_row = frappe.model.add_child(prospect, "leads"); + leads_row.lead = me.frm.doc.name; + + frappe.set_route("Form", "Prospect", prospect.name); + }); + } + + company_name() { + if (!this.frm.doc.lead_name) { + this.frm.set_value("lead_name", this.frm.doc.company_name); + } + } + + show_notes() { + if (this.frm.doc.docstatus == 1) return; + + const crm_notes = new erpnext.utils.CRMNotes({ + frm: this.frm, + notes_wrapper: $(this.frm.fields_dict.notes_html.wrapper), + }); + crm_notes.refresh(); + } + + show_activities() { + if (this.frm.doc.docstatus == 1) return; + + const crm_activities = new erpnext.utils.CRMActivities({ + frm: this.frm, + open_activities_wrapper: $(this.frm.fields_dict.open_activities_html.wrapper), + all_activities_wrapper: $(this.frm.fields_dict.all_activities_html.wrapper), + form_wrapper: $(this.frm.wrapper), + }); + crm_activities.refresh(); + } +}; + +extend_cscript(cur_frm.cscript, new erpnext.LeadController({ frm: cur_frm }));