blob: 63833da5af3f5406267db856a95fb33dbca3c483 [file] [log] [blame]
Anand Doshi885e0742015-03-03 14:55:30 +05301// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +05302// License: GNU General Public License v3. See license.txt
3
Faris Ansariab74ca72017-05-30 12:54:42 +05304erpnext.SMSManager = function SMSManager(doc) {
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +05305 var me = this;
Ankush Menatec74a5e2024-03-10 19:45:40 +05306 this.setup = function () {
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +05307 var default_msg = {
Ankush Menatec74a5e2024-03-10 19:45:40 +05308 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 Mehtaba83e9c2014-06-05 17:56:12 +053030
barredterrad2387512024-03-19 12:03:36 +010031 if (["Sales Order", "Delivery Note", "Sales Invoice"].includes(doc.doctype))
Ankush Menatec74a5e2024-03-10 19:45:40 +053032 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]);
barredterrad2387512024-03-19 12:03:36 +010035 else if (["Purchase Order", "Purchase Receipt"].includes(doc.doctype))
Ankush Menatec74a5e2024-03-10 19:45:40 +053036 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 Mehtaba83e9c2014-06-05 17:56:12 +053041 };
42
Ankush Menatec74a5e2024-03-10 19:45:40 +053043 this.get_contact_number = function (contact, ref_doctype, ref_name) {
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +053044 frappe.call({
ckosiegbu1ac6bcb2017-08-08 10:25:30 +010045 method: "frappe.core.doctype.sms_settings.sms_settings.get_contact_number",
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +053046 args: {
Nabin Haitfcc02462017-05-04 12:11:48 +053047 contact_name: contact,
48 ref_doctype: ref_doctype,
Ankush Menatec74a5e2024-03-10 19:45:40 +053049 ref_name: ref_name,
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +053050 },
Ankush Menatec74a5e2024-03-10 19:45:40 +053051 callback: function (r) {
52 if (r.exc) {
53 frappe.msgprint(r.exc);
54 return;
55 }
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +053056 me.number = r.message;
57 me.show_dialog();
Ankush Menatec74a5e2024-03-10 19:45:40 +053058 },
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +053059 });
60 };
61
Ankush Menatec74a5e2024-03-10 19:45:40 +053062 this.show = function (contact, ref_doctype, ref_name, mobile_nos, message) {
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +053063 this.message = message;
64 if (mobile_nos) {
65 me.number = mobile_nos;
66 me.show_dialog();
Ankush Menatec74a5e2024-03-10 19:45:40 +053067 } else if (contact) {
68 this.get_contact_number(contact, ref_doctype, ref_name);
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +053069 } else {
70 me.show_dialog();
71 }
Ankush Menatec74a5e2024-03-10 19:45:40 +053072 };
73 this.show_dialog = function () {
74 if (!me.dialog) me.make_dialog();
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +053075 me.dialog.set_values({
Ankush Menatec74a5e2024-03-10 19:45:40 +053076 message: me.message,
77 number: me.number,
78 });
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +053079 me.dialog.show();
Ankush Menatec74a5e2024-03-10 19:45:40 +053080 };
81 this.make_dialog = function () {
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +053082 var d = new frappe.ui.Dialog({
Ankush Menatec74a5e2024-03-10 19:45:40 +053083 title: "Send SMS",
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +053084 width: 400,
85 fields: [
Ankush Menatec74a5e2024-03-10 19:45:40 +053086 { 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 Mehtaba83e9c2014-06-05 17:56:12 +053092 var btn = d.fields_dict.send.input;
93 var v = me.dialog.get_values();
Ankush Menatec74a5e2024-03-10 19:45:40 +053094 if (v) {
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +053095 $(btn).set_working();
96 frappe.call({
ckosiegbu1ac6bcb2017-08-08 10:25:30 +010097 method: "frappe.core.doctype.sms_settings.sms_settings.send_sms",
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +053098 args: {
99 receiver_list: [v.number],
Ankush Menatec74a5e2024-03-10 19:45:40 +0530100 msg: v.message,
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +0530101 },
Ankush Menatec74a5e2024-03-10 19:45:40 +0530102 callback: function (r) {
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +0530103 $(btn).done_working();
Ankush Menatec74a5e2024-03-10 19:45:40 +0530104 if (r.exc) {
105 frappe.msgprint(r.exc);
106 return;
107 }
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +0530108 me.dialog.hide();
Ankush Menatec74a5e2024-03-10 19:45:40 +0530109 },
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +0530110 });
111 }
Ankush Menatec74a5e2024-03-10 19:45:40 +0530112 };
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +0530113 this.dialog = d;
Ankush Menatec74a5e2024-03-10 19:45:40 +0530114 };
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +0530115 this.setup();
Ankush Menatec74a5e2024-03-10 19:45:40 +0530116};