Anand Doshi | 885e074 | 2015-03-03 14:55:30 +0530 | [diff] [blame] | 1 | // Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
Rushabh Mehta | ba83e9c | 2014-06-05 17:56:12 +0530 | [diff] [blame] | 2 | // License: GNU General Public License v3. See license.txt |
| 3 | |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 4 | erpnext.SMSManager = function SMSManager(doc) { |
Rushabh Mehta | ba83e9c | 2014-06-05 17:56:12 +0530 | [diff] [blame] | 5 | var me = this; |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 6 | this.setup = function () { |
Rushabh Mehta | ba83e9c | 2014-06-05 17:56:12 +0530 | [diff] [blame] | 7 | var default_msg = { |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 8 | Lead: "", |
| 9 | Opportunity: "Your enquiry has been logged into the system. Ref No: " + doc.name, |
| 10 | Quotation: "Quotation " + doc.name + " has been sent via email. Thanks!", |
| 11 | "Sales Order": |
| 12 | "Sales Order " + |
| 13 | doc.name + |
| 14 | " has been created against " + |
| 15 | (doc.quotation_no ? "Quote No:" + doc.quotation_no : "") + |
| 16 | (doc.po_no ? " for your PO: " + doc.po_no : ""), |
| 17 | "Delivery Note": |
| 18 | "Items has been delivered against delivery note: " + |
| 19 | doc.name + |
| 20 | (doc.po_no ? " for your PO: " + doc.po_no : ""), |
| 21 | "Sales Invoice": |
| 22 | "Invoice " + |
| 23 | doc.name + |
| 24 | " has been sent via email " + |
| 25 | (doc.po_no ? " for your PO: " + doc.po_no : ""), |
| 26 | "Material Request": "Material Request " + doc.name + " has been raised in the system", |
| 27 | "Purchase Order": "Purchase Order " + doc.name + " has been sent via email", |
| 28 | "Purchase Receipt": "Items has been received against purchase receipt: " + doc.name, |
| 29 | }; |
Rushabh Mehta | ba83e9c | 2014-06-05 17:56:12 +0530 | [diff] [blame] | 30 | |
barredterra | d238751 | 2024-03-19 12:03:36 +0100 | [diff] [blame^] | 31 | if (["Sales Order", "Delivery Note", "Sales Invoice"].includes(doc.doctype)) |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 32 | this.show(doc.contact_person, "Customer", doc.customer, "", default_msg[doc.doctype]); |
| 33 | else if (doc.doctype === "Quotation") |
| 34 | this.show(doc.contact_person, "Customer", doc.party_name, "", default_msg[doc.doctype]); |
barredterra | d238751 | 2024-03-19 12:03:36 +0100 | [diff] [blame^] | 35 | else if (["Purchase Order", "Purchase Receipt"].includes(doc.doctype)) |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 36 | this.show(doc.contact_person, "Supplier", doc.supplier, "", default_msg[doc.doctype]); |
| 37 | else if (doc.doctype == "Lead") this.show("", "", "", doc.mobile_no, default_msg[doc.doctype]); |
| 38 | else if (doc.doctype == "Opportunity") |
| 39 | this.show("", "", "", doc.contact_no, default_msg[doc.doctype]); |
| 40 | else if (doc.doctype == "Material Request") this.show("", "", "", "", default_msg[doc.doctype]); |
Rushabh Mehta | ba83e9c | 2014-06-05 17:56:12 +0530 | [diff] [blame] | 41 | }; |
| 42 | |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 43 | this.get_contact_number = function (contact, ref_doctype, ref_name) { |
Rushabh Mehta | ba83e9c | 2014-06-05 17:56:12 +0530 | [diff] [blame] | 44 | frappe.call({ |
ckosiegbu | 1ac6bcb | 2017-08-08 10:25:30 +0100 | [diff] [blame] | 45 | method: "frappe.core.doctype.sms_settings.sms_settings.get_contact_number", |
Rushabh Mehta | ba83e9c | 2014-06-05 17:56:12 +0530 | [diff] [blame] | 46 | args: { |
Nabin Hait | fcc0246 | 2017-05-04 12:11:48 +0530 | [diff] [blame] | 47 | contact_name: contact, |
| 48 | ref_doctype: ref_doctype, |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 49 | ref_name: ref_name, |
Rushabh Mehta | ba83e9c | 2014-06-05 17:56:12 +0530 | [diff] [blame] | 50 | }, |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 51 | callback: function (r) { |
| 52 | if (r.exc) { |
| 53 | frappe.msgprint(r.exc); |
| 54 | return; |
| 55 | } |
Rushabh Mehta | ba83e9c | 2014-06-05 17:56:12 +0530 | [diff] [blame] | 56 | me.number = r.message; |
| 57 | me.show_dialog(); |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 58 | }, |
Rushabh Mehta | ba83e9c | 2014-06-05 17:56:12 +0530 | [diff] [blame] | 59 | }); |
| 60 | }; |
| 61 | |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 62 | this.show = function (contact, ref_doctype, ref_name, mobile_nos, message) { |
Rushabh Mehta | ba83e9c | 2014-06-05 17:56:12 +0530 | [diff] [blame] | 63 | this.message = message; |
| 64 | if (mobile_nos) { |
| 65 | me.number = mobile_nos; |
| 66 | me.show_dialog(); |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 67 | } else if (contact) { |
| 68 | this.get_contact_number(contact, ref_doctype, ref_name); |
Rushabh Mehta | ba83e9c | 2014-06-05 17:56:12 +0530 | [diff] [blame] | 69 | } else { |
| 70 | me.show_dialog(); |
| 71 | } |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 72 | }; |
| 73 | this.show_dialog = function () { |
| 74 | if (!me.dialog) me.make_dialog(); |
Rushabh Mehta | ba83e9c | 2014-06-05 17:56:12 +0530 | [diff] [blame] | 75 | me.dialog.set_values({ |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 76 | message: me.message, |
| 77 | number: me.number, |
| 78 | }); |
Rushabh Mehta | ba83e9c | 2014-06-05 17:56:12 +0530 | [diff] [blame] | 79 | me.dialog.show(); |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 80 | }; |
| 81 | this.make_dialog = function () { |
Rushabh Mehta | ba83e9c | 2014-06-05 17:56:12 +0530 | [diff] [blame] | 82 | var d = new frappe.ui.Dialog({ |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 83 | title: "Send SMS", |
Rushabh Mehta | ba83e9c | 2014-06-05 17:56:12 +0530 | [diff] [blame] | 84 | width: 400, |
| 85 | fields: [ |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 86 | { fieldname: "number", fieldtype: "Data", label: "Mobile Number", reqd: 1 }, |
| 87 | { fieldname: "message", fieldtype: "Text", label: "Message", reqd: 1 }, |
| 88 | { fieldname: "send", fieldtype: "Button", label: "Send" }, |
| 89 | ], |
| 90 | }); |
| 91 | d.fields_dict.send.input.onclick = function () { |
Rushabh Mehta | ba83e9c | 2014-06-05 17:56:12 +0530 | [diff] [blame] | 92 | var btn = d.fields_dict.send.input; |
| 93 | var v = me.dialog.get_values(); |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 94 | if (v) { |
Rushabh Mehta | ba83e9c | 2014-06-05 17:56:12 +0530 | [diff] [blame] | 95 | $(btn).set_working(); |
| 96 | frappe.call({ |
ckosiegbu | 1ac6bcb | 2017-08-08 10:25:30 +0100 | [diff] [blame] | 97 | method: "frappe.core.doctype.sms_settings.sms_settings.send_sms", |
Rushabh Mehta | ba83e9c | 2014-06-05 17:56:12 +0530 | [diff] [blame] | 98 | args: { |
| 99 | receiver_list: [v.number], |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 100 | msg: v.message, |
Rushabh Mehta | ba83e9c | 2014-06-05 17:56:12 +0530 | [diff] [blame] | 101 | }, |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 102 | callback: function (r) { |
Rushabh Mehta | ba83e9c | 2014-06-05 17:56:12 +0530 | [diff] [blame] | 103 | $(btn).done_working(); |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 104 | if (r.exc) { |
| 105 | frappe.msgprint(r.exc); |
| 106 | return; |
| 107 | } |
Rushabh Mehta | ba83e9c | 2014-06-05 17:56:12 +0530 | [diff] [blame] | 108 | me.dialog.hide(); |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 109 | }, |
Rushabh Mehta | ba83e9c | 2014-06-05 17:56:12 +0530 | [diff] [blame] | 110 | }); |
| 111 | } |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 112 | }; |
Rushabh Mehta | ba83e9c | 2014-06-05 17:56:12 +0530 | [diff] [blame] | 113 | this.dialog = d; |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 114 | }; |
Rushabh Mehta | ba83e9c | 2014-06-05 17:56:12 +0530 | [diff] [blame] | 115 | this.setup(); |
Ankush Menat | ec74a5e | 2024-03-10 19:45:40 +0530 | [diff] [blame] | 116 | }; |