blob: a7003a272d09a70b74db1a6cda7888173a2aec3c [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;
6 this.setup = function() {
7 var default_msg = {
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' : 'Sales Order ' + doc.name + ' has been created against '
12 + (doc.quotation_no ? ('Quote No:' + doc.quotation_no) : '')
13 + (doc.po_no ? (' for your PO: ' + doc.po_no) : ''),
14 'Delivery Note' : 'Items has been delivered against delivery note: ' + doc.name
15 + (doc.po_no ? (' for your PO: ' + doc.po_no) : ''),
16 'Sales Invoice': 'Invoice ' + doc.name + ' has been sent via email '
17 + (doc.po_no ? (' for your PO: ' + doc.po_no) : ''),
18 'Material Request' : 'Material Request ' + doc.name + ' has been raised in the system',
19 'Purchase Order' : 'Purchase Order ' + doc.name + ' has been sent via email',
20 'Purchase Receipt' : 'Items has been received against purchase receipt: ' + doc.name
21 }
22
Anurag Mishrade13faf2019-07-09 11:53:31 +053023 if (in_list(['Sales Order', 'Delivery Note', 'Sales Invoice'], doc.doctype))
Nabin Haitfcc02462017-05-04 12:11:48 +053024 this.show(doc.contact_person, 'Customer', doc.customer, '', default_msg[doc.doctype]);
Anurag Mishrade13faf2019-07-09 11:53:31 +053025 else if (in_list(['Quotation'], doc.doctype))
26 this.show(doc.contact_person, 'Customer', doc.party_name, '', default_msg[doc.doctype]);
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +053027 else if (in_list(['Purchase Order', 'Purchase Receipt'], doc.doctype))
Nabin Haitfcc02462017-05-04 12:11:48 +053028 this.show(doc.contact_person, 'Supplier', doc.supplier, '', default_msg[doc.doctype]);
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +053029 else if (doc.doctype == 'Lead')
30 this.show('', '', '', doc.mobile_no, default_msg[doc.doctype]);
31 else if (doc.doctype == 'Opportunity')
32 this.show('', '', '', doc.contact_no, default_msg[doc.doctype]);
33 else if (doc.doctype == 'Material Request')
34 this.show('', '', '', '', default_msg[doc.doctype]);
35
36 };
37
Nabin Haitfcc02462017-05-04 12:11:48 +053038 this.get_contact_number = function(contact, ref_doctype, ref_name) {
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +053039 frappe.call({
ckosiegbu1ac6bcb2017-08-08 10:25:30 +010040 method: "frappe.core.doctype.sms_settings.sms_settings.get_contact_number",
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +053041 args: {
Nabin Haitfcc02462017-05-04 12:11:48 +053042 contact_name: contact,
43 ref_doctype: ref_doctype,
44 ref_name: ref_name
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +053045 },
46 callback: function(r) {
Faris Ansariab74ca72017-05-30 12:54:42 +053047 if(r.exc) { frappe.msgprint(r.exc); return; }
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +053048 me.number = r.message;
49 me.show_dialog();
50 }
51 });
52 };
53
Nabin Haitfcc02462017-05-04 12:11:48 +053054 this.show = function(contact, ref_doctype, ref_name, mobile_nos, message) {
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +053055 this.message = message;
56 if (mobile_nos) {
57 me.number = mobile_nos;
58 me.show_dialog();
59 } else if (contact){
Nabin Haitfcc02462017-05-04 12:11:48 +053060 this.get_contact_number(contact, ref_doctype, ref_name)
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +053061 } else {
62 me.show_dialog();
63 }
64 }
65 this.show_dialog = function() {
66 if(!me.dialog)
67 me.make_dialog();
68 me.dialog.set_values({
69 'message': me.message,
70 'number': me.number
71 })
72 me.dialog.show();
73 }
74 this.make_dialog = function() {
75 var d = new frappe.ui.Dialog({
76 title: 'Send SMS',
77 width: 400,
78 fields: [
79 {fieldname:'number', fieldtype:'Data', label:'Mobile Number', reqd:1},
80 {fieldname:'message', fieldtype:'Text', label:'Message', reqd:1},
81 {fieldname:'send', fieldtype:'Button', label:'Send'}
82 ]
83 })
84 d.fields_dict.send.input.onclick = function() {
85 var btn = d.fields_dict.send.input;
86 var v = me.dialog.get_values();
87 if(v) {
88 $(btn).set_working();
89 frappe.call({
ckosiegbu1ac6bcb2017-08-08 10:25:30 +010090 method: "frappe.core.doctype.sms_settings.sms_settings.send_sms",
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +053091 args: {
92 receiver_list: [v.number],
93 msg: v.message
94 },
95 callback: function(r) {
96 $(btn).done_working();
Faris Ansariab74ca72017-05-30 12:54:42 +053097 if(r.exc) {frappe.msgprint(r.exc); return; }
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +053098 me.dialog.hide();
99 }
100 });
101 }
102 }
103 this.dialog = d;
104 }
105 this.setup();
106}