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; |
| 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 Mishra | de13faf | 2019-07-09 11:53:31 +0530 | [diff] [blame] | 23 | if (in_list(['Sales Order', 'Delivery Note', 'Sales Invoice'], doc.doctype)) |
Nabin Hait | fcc0246 | 2017-05-04 12:11:48 +0530 | [diff] [blame] | 24 | this.show(doc.contact_person, 'Customer', doc.customer, '', default_msg[doc.doctype]); |
Anurag Mishra | e9dd9b8 | 2019-07-09 17:54:00 +0530 | [diff] [blame] | 25 | else if (doc.doctype === 'Quotation') |
Anurag Mishra | de13faf | 2019-07-09 11:53:31 +0530 | [diff] [blame] | 26 | this.show(doc.contact_person, 'Customer', doc.party_name, '', default_msg[doc.doctype]); |
Rushabh Mehta | ba83e9c | 2014-06-05 17:56:12 +0530 | [diff] [blame] | 27 | else if (in_list(['Purchase Order', 'Purchase Receipt'], doc.doctype)) |
Nabin Hait | fcc0246 | 2017-05-04 12:11:48 +0530 | [diff] [blame] | 28 | this.show(doc.contact_person, 'Supplier', doc.supplier, '', default_msg[doc.doctype]); |
Rushabh Mehta | ba83e9c | 2014-06-05 17:56:12 +0530 | [diff] [blame] | 29 | 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 Hait | fcc0246 | 2017-05-04 12:11:48 +0530 | [diff] [blame] | 38 | this.get_contact_number = function(contact, ref_doctype, ref_name) { |
Rushabh Mehta | ba83e9c | 2014-06-05 17:56:12 +0530 | [diff] [blame] | 39 | frappe.call({ |
ckosiegbu | 1ac6bcb | 2017-08-08 10:25:30 +0100 | [diff] [blame] | 40 | method: "frappe.core.doctype.sms_settings.sms_settings.get_contact_number", |
Rushabh Mehta | ba83e9c | 2014-06-05 17:56:12 +0530 | [diff] [blame] | 41 | args: { |
Nabin Hait | fcc0246 | 2017-05-04 12:11:48 +0530 | [diff] [blame] | 42 | contact_name: contact, |
| 43 | ref_doctype: ref_doctype, |
| 44 | ref_name: ref_name |
Rushabh Mehta | ba83e9c | 2014-06-05 17:56:12 +0530 | [diff] [blame] | 45 | }, |
| 46 | callback: function(r) { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 47 | if(r.exc) { frappe.msgprint(r.exc); return; } |
Rushabh Mehta | ba83e9c | 2014-06-05 17:56:12 +0530 | [diff] [blame] | 48 | me.number = r.message; |
| 49 | me.show_dialog(); |
| 50 | } |
| 51 | }); |
| 52 | }; |
| 53 | |
Nabin Hait | fcc0246 | 2017-05-04 12:11:48 +0530 | [diff] [blame] | 54 | this.show = function(contact, ref_doctype, ref_name, mobile_nos, message) { |
Rushabh Mehta | ba83e9c | 2014-06-05 17:56:12 +0530 | [diff] [blame] | 55 | this.message = message; |
| 56 | if (mobile_nos) { |
| 57 | me.number = mobile_nos; |
| 58 | me.show_dialog(); |
| 59 | } else if (contact){ |
Nabin Hait | fcc0246 | 2017-05-04 12:11:48 +0530 | [diff] [blame] | 60 | this.get_contact_number(contact, ref_doctype, ref_name) |
Rushabh Mehta | ba83e9c | 2014-06-05 17:56:12 +0530 | [diff] [blame] | 61 | } 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({ |
ckosiegbu | 1ac6bcb | 2017-08-08 10:25:30 +0100 | [diff] [blame] | 90 | method: "frappe.core.doctype.sms_settings.sms_settings.send_sms", |
Rushabh Mehta | ba83e9c | 2014-06-05 17:56:12 +0530 | [diff] [blame] | 91 | args: { |
| 92 | receiver_list: [v.number], |
| 93 | msg: v.message |
| 94 | }, |
| 95 | callback: function(r) { |
| 96 | $(btn).done_working(); |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 97 | if(r.exc) {frappe.msgprint(r.exc); return; } |
Rushabh Mehta | ba83e9c | 2014-06-05 17:56:12 +0530 | [diff] [blame] | 98 | me.dialog.hide(); |
| 99 | } |
| 100 | }); |
| 101 | } |
| 102 | } |
| 103 | this.dialog = d; |
| 104 | } |
| 105 | this.setup(); |
| 106 | } |