brotherton-erpnext/erpnext/public/js/call_popup/call_popup.js

213 lines
6.1 KiB
JavaScript
Raw Normal View History

2019-05-21 02:27:06 +00:00
class CallPopup {
2019-06-06 09:18:37 +00:00
constructor(call_log) {
this.caller_number = call_log.from;
2019-05-22 01:08:25 +00:00
this.call_log = call_log;
2019-06-17 03:16:38 +00:00
this.setup_listener();
2019-05-16 18:25:35 +00:00
this.make();
}
make() {
2019-05-18 10:41:29 +00:00
this.dialog = new frappe.ui.Dialog({
'static': true,
'minimizable': true,
2019-05-16 18:25:35 +00:00
'fields': [{
2019-05-27 05:08:43 +00:00
'fieldname': 'caller_info',
2019-05-16 18:25:35 +00:00
'fieldtype': 'HTML'
}, {
2019-05-24 04:41:48 +00:00
'fielname': 'last_interaction',
'fieldtype': 'Section Break',
2019-06-18 05:58:14 +00:00
'label': __('Activity'),
2019-05-16 18:25:35 +00:00
}, {
2019-05-18 10:41:29 +00:00
'fieldtype': 'Small Text',
2019-06-18 05:58:14 +00:00
'label': __('Last Communication'),
2019-05-16 18:25:35 +00:00
'fieldname': 'last_communication',
2019-06-07 06:22:39 +00:00
'read_only': true,
'default': `<i class="text-muted">${__('No communication found.')}<i>`
2019-05-24 04:41:48 +00:00
}, {
'fieldtype': 'Small Text',
2019-06-18 05:58:14 +00:00
'label': __('Last Issue'),
2019-05-24 04:41:48 +00:00
'fieldname': 'last_issue',
2019-06-07 06:22:39 +00:00
'read_only': true,
'default': `<i class="text-muted">${__('No issue raised by the customer.')}<i>`
2019-05-24 04:41:48 +00:00
}, {
2019-05-27 05:08:43 +00:00
'fieldtype': 'Column Break',
2019-05-16 18:25:35 +00:00
}, {
2019-05-18 10:41:29 +00:00
'fieldtype': 'Small Text',
2019-06-18 05:58:14 +00:00
'label': __('Call Summary'),
2019-05-24 04:41:48 +00:00
'fieldname': 'call_summary',
2019-05-18 10:41:29 +00:00
}, {
'fieldtype': 'Button',
2019-06-18 05:58:14 +00:00
'label': __('Save'),
2019-05-18 10:41:29 +00:00
'click': () => {
const call_summary = this.dialog.get_value('call_summary');
if (!call_summary) return;
2019-05-31 08:12:22 +00:00
frappe.xcall('erpnext.crm.doctype.utils.add_call_summary', {
'docname': this.call_log.id,
'summary': call_summary,
2019-05-24 04:41:48 +00:00
}).then(() => {
this.close_modal();
frappe.show_alert({
message: `${__('Call Summary Saved')}<br><a class="text-small text-muted" href="#Form/Call Log/${this.call_log.name}">${__('View call log')}</a>`,
indicator: 'green'
});
2019-05-24 04:41:48 +00:00
});
2019-05-18 10:41:29 +00:00
}
2019-05-27 05:08:43 +00:00
}],
2019-05-16 18:25:35 +00:00
});
2019-06-06 09:18:37 +00:00
this.set_call_status();
2019-05-27 05:08:43 +00:00
this.make_caller_info_section();
2019-05-18 10:41:29 +00:00
this.dialog.get_close_btn().show();
2019-05-27 10:00:41 +00:00
this.dialog.$body.addClass('call-popup');
2019-06-11 06:51:30 +00:00
this.dialog.set_secondary_action(this.close_modal.bind(this));
2019-06-18 05:58:14 +00:00
frappe.utils.play_sound('incoming-call');
2019-05-24 04:41:48 +00:00
this.dialog.show();
2019-05-16 18:25:35 +00:00
}
2019-05-27 05:08:43 +00:00
make_caller_info_section() {
const wrapper = this.dialog.fields_dict['caller_info'].$wrapper;
wrapper.append('<div class="text-muted"> Loading... </div>');
frappe.xcall('erpnext.crm.doctype.utils.get_document_with_phone_number', {
2019-05-27 05:08:43 +00:00
'number': this.caller_number
}).then(contact_doc => {
wrapper.empty();
2019-05-24 04:41:48 +00:00
const contact = this.contact = contact_doc;
if (!contact) {
2019-06-11 06:51:30 +00:00
this.setup_unknown_caller(wrapper);
} else {
2019-06-11 06:51:30 +00:00
this.setup_known_caller(wrapper);
2019-05-27 05:08:43 +00:00
this.set_call_status();
2019-05-24 04:41:48 +00:00
this.make_last_interaction_section();
}
});
2019-05-16 18:25:35 +00:00
}
2019-06-11 06:51:30 +00:00
setup_unknown_caller(wrapper) {
wrapper.append(`
<div class="caller-info">
<b>${__('Unknown Number')}:</b> ${this.caller_number}
<button
class="margin-left btn btn-new btn-default btn-xs"
data-doctype="Contact"
title="Make New Contact">
<i class="octicon octicon-plus text-medium"></i>
</button>
</div>
`).find('button').click(
() => frappe.set_route(`Form/Contact/New Contact?phone=${this.caller_number}`)
);
}
setup_known_caller(wrapper) {
const contact = this.contact;
2019-06-17 03:29:12 +00:00
const contact_name = frappe.utils.get_form_link(contact.doctype, contact.name, true, this.get_caller_name());
2019-06-11 06:51:30 +00:00
const links = contact.links ? contact.links : [];
let contact_links = '';
links.forEach(link => {
contact_links += `<div>${link.link_doctype}: ${frappe.utils.get_form_link(link.link_doctype, link.link_name, true)}</div>`;
});
wrapper.append(`
<div class="caller-info flex">
${frappe.avatar(null, 'avatar-xl', contact.name, contact.image)}
<div>
<h5>${contact_name}</h5>
<div>${contact.mobile_no || ''}</div>
<div>${contact.phone_no || ''}</div>
${contact_links}
</div>
</div>
`);
}
2019-05-27 05:08:43 +00:00
set_indicator(color, blink=false) {
2019-06-05 04:34:51 +00:00
let classes = `indicator ${color} ${blink ? 'blink': ''}`;
this.dialog.header.find('.indicator').attr('class', classes);
2019-05-16 18:25:35 +00:00
}
2019-05-21 02:27:06 +00:00
set_call_status(call_status) {
2019-05-21 02:27:06 +00:00
let title = '';
call_status = call_status || this.call_log.status;
2019-06-06 09:18:37 +00:00
if (['Ringing'].includes(call_status) || !call_status) {
2019-06-17 03:29:12 +00:00
title = __('Incoming call from {0}', [this.get_caller_name()]);
2019-05-27 05:08:43 +00:00
this.set_indicator('blue', true);
2019-06-06 09:18:37 +00:00
} else if (call_status === 'In Progress') {
2019-05-22 01:08:25 +00:00
title = __('Call Connected');
this.set_indicator('yellow');
} else if (call_status === 'Missed') {
this.set_indicator('red');
title = __('Call Missed');
2019-06-06 09:18:37 +00:00
} else if (['Completed', 'Disconnected'].includes(call_status)) {
2019-05-27 05:08:43 +00:00
this.set_indicator('red');
title = __('Call Disconnected');
2019-05-24 04:41:48 +00:00
} else {
this.set_indicator('blue');
title = call_status;
2019-05-21 02:27:06 +00:00
}
this.dialog.set_title(title);
}
2019-06-06 09:18:37 +00:00
update_call_log(call_log) {
this.call_log = call_log;
2019-05-22 01:08:25 +00:00
this.set_call_status();
2019-05-21 02:27:06 +00:00
}
close_modal() {
this.dialog.hide();
2019-06-11 06:51:30 +00:00
delete erpnext.call_popup;
}
call_disconnected(call_log) {
2019-06-18 05:58:14 +00:00
frappe.utils.play_sound('call-disconnect');
this.update_call_log(call_log);
setTimeout(() => {
if (!this.dialog.get_value('call_summary')) {
this.close_modal();
}
}, 10000);
}
2019-05-24 04:41:48 +00:00
make_last_interaction_section() {
frappe.xcall('erpnext.crm.doctype.utils.get_last_interaction', {
2019-05-27 05:08:43 +00:00
'number': this.caller_number,
2019-05-24 04:41:48 +00:00
'reference_doc': this.contact
}).then(data => {
const comm_field = this.dialog.fields_dict["last_communication"];
2019-05-24 04:41:48 +00:00
if (data.last_communication) {
const comm = data.last_communication;
comm_field.set_value(comm.content);
}
if (data.last_issue) {
const issue = data.last_issue;
const issue_field = this.dialog.fields_dict["last_issue"];
issue_field.set_value(issue.subject);
2019-06-07 06:22:39 +00:00
issue_field.$wrapper.append(`<a class="text-medium" href="#List/Issue?customer=${issue.customer}">
${__('View all issues from {0}', [issue.customer])}
2019-06-07 06:22:39 +00:00
</a>`);
2019-05-24 04:41:48 +00:00
}
});
}
2019-06-17 03:29:12 +00:00
get_caller_name() {
return this.contact ? this.contact.lead_name || this.contact.name || '' : this.caller_number;
}
2019-06-17 03:16:38 +00:00
setup_listener() {
frappe.realtime.on(`call_${this.call_log.id}_disconnected`, call_log => {
this.call_disconnected(call_log);
// Remove call disconnect listener after the call is disconnected
frappe.realtime.off(`call_${this.call_log.id}_disconnected`);
});
}
2019-05-16 18:25:35 +00:00
}
2019-05-18 10:41:29 +00:00
$(document).on('app_ready', function () {
2019-06-06 09:18:37 +00:00
frappe.realtime.on('show_call_popup', call_log => {
2019-05-21 02:27:06 +00:00
if (!erpnext.call_popup) {
2019-06-06 09:18:37 +00:00
erpnext.call_popup = new CallPopup(call_log);
2019-05-21 02:27:06 +00:00
} else {
2019-06-06 09:18:37 +00:00
erpnext.call_popup.update_call_log(call_log);
2019-05-22 01:08:25 +00:00
erpnext.call_popup.dialog.show();
2019-05-21 02:27:06 +00:00
}
2019-05-16 18:25:35 +00:00
});
});