blob: 6ce8bb115007576945849ea80a285e2e4a0238a8 [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
23 if (in_list(['Quotation', '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]);
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +053025 else if (in_list(['Purchase Order', 'Purchase Receipt'], doc.doctype))
Nabin Haitfcc02462017-05-04 12:11:48 +053026 this.show(doc.contact_person, 'Supplier', doc.supplier, '', default_msg[doc.doctype]);
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +053027 else if (doc.doctype == 'Lead')
28 this.show('', '', '', doc.mobile_no, default_msg[doc.doctype]);
29 else if (doc.doctype == 'Opportunity')
30 this.show('', '', '', doc.contact_no, default_msg[doc.doctype]);
31 else if (doc.doctype == 'Material Request')
32 this.show('', '', '', '', default_msg[doc.doctype]);
33
34 };
35
Nabin Haitfcc02462017-05-04 12:11:48 +053036 this.get_contact_number = function(contact, ref_doctype, ref_name) {
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +053037 frappe.call({
ckosiegbu1ac6bcb2017-08-08 10:25:30 +010038 method: "frappe.core.doctype.sms_settings.sms_settings.get_contact_number",
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +053039 args: {
Nabin Haitfcc02462017-05-04 12:11:48 +053040 contact_name: contact,
41 ref_doctype: ref_doctype,
42 ref_name: ref_name
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +053043 },
44 callback: function(r) {
Faris Ansariab74ca72017-05-30 12:54:42 +053045 if(r.exc) { frappe.msgprint(r.exc); return; }
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +053046 me.number = r.message;
47 me.show_dialog();
48 }
49 });
50 };
51
Nabin Haitfcc02462017-05-04 12:11:48 +053052 this.show = function(contact, ref_doctype, ref_name, mobile_nos, message) {
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +053053 this.message = message;
54 if (mobile_nos) {
55 me.number = mobile_nos;
56 me.show_dialog();
57 } else if (contact){
Nabin Haitfcc02462017-05-04 12:11:48 +053058 this.get_contact_number(contact, ref_doctype, ref_name)
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +053059 } else {
60 me.show_dialog();
61 }
62 }
63 this.show_dialog = function() {
64 if(!me.dialog)
65 me.make_dialog();
66 me.dialog.set_values({
67 'message': me.message,
68 'number': me.number
69 })
70 me.dialog.show();
71 }
72 this.make_dialog = function() {
73 var d = new frappe.ui.Dialog({
74 title: 'Send SMS',
75 width: 400,
76 fields: [
77 {fieldname:'number', fieldtype:'Data', label:'Mobile Number', reqd:1},
78 {fieldname:'message', fieldtype:'Text', label:'Message', reqd:1},
79 {fieldname:'send', fieldtype:'Button', label:'Send'}
80 ]
81 })
82 d.fields_dict.send.input.onclick = function() {
83 var btn = d.fields_dict.send.input;
84 var v = me.dialog.get_values();
85 if(v) {
86 $(btn).set_working();
87 frappe.call({
ckosiegbu1ac6bcb2017-08-08 10:25:30 +010088 method: "frappe.core.doctype.sms_settings.sms_settings.send_sms",
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +053089 args: {
90 receiver_list: [v.number],
91 msg: v.message
92 },
93 callback: function(r) {
94 $(btn).done_working();
Faris Ansariab74ca72017-05-30 12:54:42 +053095 if(r.exc) {frappe.msgprint(r.exc); return; }
Rushabh Mehtaba83e9c2014-06-05 17:56:12 +053096 me.dialog.hide();
97 }
98 });
99 }
100 }
101 this.dialog = d;
102 }
103 this.setup();
104}