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

202 lines
5.7 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': [{
'fieldname': 'name',
'label': 'Name',
'default': this.get_caller_name() || __('Unknown Caller'),
'fieldtype': 'Data',
'read_only': 1
}, {
'fieldtype': 'Button',
'label': __('Open Contact'),
'click': () => frappe.set_route('Form', 'Contact', this.call_log.contact),
'depends_on': () => this.call_log.contact
}, {
'fieldtype': 'Button',
'label': __('Open Lead'),
'click': () => frappe.set_route('Form', 'Lead', this.call_log.lead),
'depends_on': () => this.call_log.lead
}, {
'fieldtype': 'Button',
'label': __('Make New Contact'),
'click': () => frappe.new_doc('Contact', { 'mobile_no': this.caller_number }),
'depends_on': () => !this.get_caller_name()
}, {
'fieldtype': 'Button',
'label': __('Make New Lead'),
'click': () => frappe.new_doc('Lead', { 'mobile_no': this.caller_number }),
'depends_on': () => !this.get_caller_name()
}, {
'fieldtype': 'Column Break',
}, {
'fieldname': 'number',
'label': 'Phone Number',
'fieldtype': 'Data',
'default': this.caller_number,
'read_only': 1
2019-05-16 18:25:35 +00:00
}, {
2019-05-24 04:41:48 +00:00
'fielname': 'last_interaction',
'fieldtype': 'Section Break',
2019-06-18 05:58:14 +00:00
'label': __('Activity'),
'depends_on': () => this.get_caller_name()
2019-05-16 18:25:35 +00:00
}, {
2019-05-18 10:41:29 +00:00
'fieldtype': 'Small Text',
'label': __('Last Issue'),
'fieldname': 'last_issue',
2019-06-07 06:22:39 +00:00
'read_only': true,
'depends_on': () => this.call_log.contact,
'default': `<i class="text-muted">${__('No issue has been raised by the caller.')}<i>`
2019-05-24 04:41:48 +00:00
}, {
'fieldtype': 'Small Text',
'label': __('Last Communication'),
'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': 'Section 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;
frappe.xcall('erpnext.communication.doctype.call_log.call_log.add_call_summary', {
'call_log': this.call_log.name,
'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-18 10:41:29 +00:00
this.dialog.get_close_btn().show();
this.make_last_interaction_section();
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
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) {
title = __('Incoming call from {0}', [this.get_caller_name() || this.caller_number]);
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();
}
}, 30000);
}
2019-05-24 04:41:48 +00:00
make_last_interaction_section() {
frappe.xcall('erpnext.crm.doctype.utils.get_last_interaction', {
'contact': this.call_log.contact,
'lead': this.call_log.lead
2019-05-24 04:41:48 +00:00
}).then(data => {
2019-07-01 08:58:59 +00:00
const comm_field = this.dialog.get_field('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;
2019-07-01 08:58:59 +00:00
const issue_field = this.dialog.get_field("last_issue");
2019-05-24 04:41:48 +00:00
issue_field.set_value(issue.subject);
issue_field.$wrapper.append(`
<a class="text-medium" href="#List/Issue?customer=${issue.customer}">
${__('View all issues from {0}', [issue.customer])}
</a>
`);
2019-05-24 04:41:48 +00:00
}
});
}
2019-06-17 03:29:12 +00:00
get_caller_name() {
let log = this.call_log;
return log.contact_name || log.lead_name;
2019-06-17 03:29:12 +00:00
}
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
});
});