// Dispatch (agent/dispatch.php, functions/dispatch.php). // // The queue - tickets nobody has, with an agent and nothing booked, or left // behind - beside the board: Day (a time grid per agent), Week (a row per agent, // a column per day) or Workload (each agent's load). Dropping a ticket on an // agent and a time, or moving a booking, opens the Schedule window and books it // as the ticket's own Schedule window does (ajax.php?dispatch_book). A booking // dragged back onto the queue is taken off the schedule. Under it, This week. // Times come and go on the agent's own clock - the server moves them. The view, // the agents shown and weekends are kept to the account (dispatch_state). (function () { 'use strict'; const root = document.getElementById('itflowDispatch'); if (!root || typeof FullCalendar === 'undefined') { return; } const csrf = root.dataset.csrf; const canEdit = root.dataset.canEdit === '1'; const agents = JSON.parse(root.dataset.agents || '[]'); const firstDay = Number(root.dataset.firstDay) || 0; const defaultMinutes = Number(root.dataset.defaultMinutes) || 60; const lengths = JSON.parse(root.dataset.lengths || '[]'); const businessHours = JSON.parse(root.dataset.businessHours || 'null'); const zone = root.dataset.zone || ''; const queueEl = root.querySelector('[data-dispatch-queue]'); const board = root.querySelector('[data-dispatch-board]'); const statsEl = root.querySelector('[data-dispatch-stats]'); const titleEl = root.querySelector('[data-dispatch-title]'); const priorityTones = {Urgent: 'danger', High: 'warning', Medium: 'primary', Low: 'secondary'}; const saved = JSON.parse(root.dataset.state || '{}'); const state = { view: saved.view || 'day', hidden: Array.isArray(saved.hidden) ? saved.hidden.map(Number) : [], weekends: !!saved.weekends, date: parseYmd(root.dataset.initialDate), tab: 'unassigned', filters: {} }; let calendars = []; let sortables = []; let queueDraggable = null; let queueTickets = []; let lastItems = []; const expanded = {}; // ---------- helpers ---------- function pad(n) { return String(n).padStart(2, '0'); } function ymd(date) { return date.getFullYear() + '-' + pad(date.getMonth() + 1) + '-' + pad(date.getDate()); } function hm(date) { return pad(date.getHours()) + ':' + pad(date.getMinutes()); } function parseYmd(text) { const m = /^(\d{4})-(\d{2})-(\d{2})/.exec(text || ''); return m ? new Date(+m[1], +m[2] - 1, +m[3]) : new Date(); } function parseDateTime(text) { const m = /^(\d{4})-(\d{2})-(\d{2})[ T](\d{2}):(\d{2})/.exec(String(text || '')); return m ? new Date(+m[1], +m[2] - 1, +m[3], +m[4], +m[5]) : null; } function addDays(date, n) { const d = new Date(date.getTime()); d.setDate(d.getDate() + n); return d; } function weekStart(date) { return addDays(date, -((date.getDay() - firstDay + 7) % 7)); } function esc(text) { const div = document.createElement('div'); div.textContent = text == null ? '' : String(text); return div.innerHTML; } function lengthText(minutes) { const h = Math.floor(minutes / 60); const m = minutes % 60; return ((h ? h + (h > 1 ? ' hours' : ' hour') : '') + (m ? ' ' + m + ' min' : '')).trim(); } function minutesBetween(start, end) { const a = parseDateTime(start); const b = parseDateTime(end); return a && b ? Math.max(0, Math.round((b - a) / 60000)) : 0; } function toast(message, type) { if (typeof itflowToast === 'function') { itflowToast(message, type || 'success'); } } // Now on the agent's own clock, when they work away from the company's function zoneNow() { const parts = {}; new Intl.DateTimeFormat('en-CA', {timeZone: zone, hourCycle: 'h23', year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit'}) .formatToParts(new Date()).forEach(function (part) { parts[part.type] = part.value; }); return parts.year + '-' + parts.month + '-' + parts.day + 'T' + parts.hour + ':' + parts.minute + ':' + parts.second; } function today() { return zone ? parseYmd(zoneNow()) : new Date(); } function request(method, params) { const query = new URLSearchParams(params).toString(); const options = {method: method, credentials: 'same-origin'}; if (method === 'POST') { options.headers = {'Content-Type': 'application/x-www-form-urlencoded'}; options.body = query; } return fetch('ajax.php' + (method === 'GET' ? '?' + query : ''), options).then(function (res) { return res.json().catch(function () { return {error: 'Something went wrong - reload the page and try again.'}; }); }).then(function (json) { if (json && json.error) { throw new Error(json.error); } return json; }); } function post(params) { return request('POST', Object.assign({csrf_token: csrf}, params)); } let saveTimer = null; function saveState() { clearTimeout(saveTimer); saveTimer = setTimeout(function () { post({dispatch_state: 1, view: state.view, hidden: state.hidden.join(','), weekends: state.weekends ? 1 : 0}).catch(function () { // Not kept this time - the page still shows it }); }, 600); } function agentById(id) { return agents.find(function (a) { return a.id === Number(id); }); } function shownAgents() { return agents.filter(function (a) { return state.hidden.indexOf(a.id) === -1; }); } function dot(color) { return ''; } function businessStart() { return businessHours && businessHours.startTime && businessHours.startTime !== '00:00' ? businessHours.startTime : '09:00'; } // ---------- asking: the Schedule window, and taking a booking off ---------- const scheduleModalEl = document.getElementById('dispatchScheduleModal'); const scheduleForm = scheduleModalEl.querySelector('[data-dispatch-schedule]'); const scheduleError = scheduleModalEl.querySelector('[data-dispatch-schedule-error]'); let scheduling = null; // Book a ticket: [ticket] from the queue or the board, at [start] (a Date), for [agentId] - resolves true once booked function schedule(ticket, start, agentId, minutes) { if (!canEdit) { return Promise.resolve(false); } return new Promise(function (resolve) { scheduling = {ticket: ticket, resolve: resolve, done: false}; scheduleModalEl.querySelector('[data-dispatch-schedule-ref]').textContent = ticket.reference || ''; scheduleModalEl.querySelector('[data-dispatch-schedule-ticket]').innerHTML = '' + esc(ticket.reference) + ' ' + esc(ticket.subject) + '' + '
' + esc([ticket.client, ticket.priority, ticket.status, ticket.booked ? 'booked ' + ticket.booked : ''].filter(Boolean).join(' · ')) + '
'; const length = minutes || ticket.minutes || defaultMinutes; const list = lengths.indexOf(length) === -1 ? lengths.concat([length]).sort(function (a, b) { return a - b; }) : lengths; scheduleForm.elements.minutes.innerHTML = list.map(function (m) { return ''; }).join(''); scheduleForm.elements.minutes.value = String(length); scheduleForm.elements.agent_id.value = String(agentId || ticket.agent || (agents[0] || {}).id || ''); scheduleForm.elements.start.value = ymd(start) + 'T' + hm(start); scheduleForm.elements.onsite.value = ticket.onsite ? '1' : '0'; scheduleError.hidden = true; loadOthers(ticket); bootstrap.Modal.getOrCreateInstance(scheduleModalEl).show(); }); } // The contact's other open tickets under the ticket, each with a way to merge it with this one const othersEl = scheduleModalEl.querySelector('[data-dispatch-schedule-others]'); function loadOthers(ticket) { othersEl.hidden = true; othersEl.innerHTML = ''; request('GET', {dispatch_contact_tickets: ticket.id}).then(function (json) { if (!scheduling || scheduling.ticket.id !== ticket.id || !json.tickets || !json.tickets.length) { return; } const n = json.tickets.length; othersEl.innerHTML = '
' + n + ' other open ticket' + (n === 1 ? '' : 's') + ' for ' + esc(json.contact) + '
' + '
The same problem? Merge them - one is kept, the other is closed with a note pointing to it.
' + json.tickets.map(function (o) { return '
' + '
' + esc(o.reference) + ' ' + esc(o.subject) + '' + '
' + esc([o.status, o.agent, o.booked ? 'booked ' + o.booked : ''].filter(Boolean).join(' · ')) + '
' + (canEdit ? '' : '') + '
'; }).join(''); othersEl.hidden = false; }).catch(function () { // Nothing to show - the window works without it }); } // Merging: which way was picked, why, and whether the replies go too othersEl.addEventListener('click', function (e) { const pick = e.target.closest('[data-merge]'); if (pick && scheduling) { const row = pick.closest('[data-other]'); const otherId = Number(row.dataset.other); const otherRef = row.querySelector('a').textContent.split(' ')[0]; const keepThis = pick.dataset.merge === 'keep-this'; othersEl.querySelectorAll('[data-merge-form]').forEach(function (f) { f.remove(); }); const form = document.createElement('div'); form.className = 'bg-body-tertiary rounded p-2 mt-1'; form.setAttribute('data-merge-form', ''); form.dataset.child = String(keepThis ? otherId : scheduling.ticket.id); form.dataset.parent = String(keepThis ? scheduling.ticket.id : otherId); form.innerHTML = '
' + esc(keepThis ? otherRef : scheduling.ticket.reference) + ' is closed and merged into ' + esc(keepThis ? scheduling.ticket.reference : otherRef) + '.
' + '' + '
' + '
' + '' + '
'; row.appendChild(form); form.querySelector('[data-merge-reason]').focus(); return; } if (e.target.closest('[data-merge-cancel]')) { e.target.closest('[data-merge-form]').remove(); return; } const go = e.target.closest('[data-merge-go]'); if (go && scheduling) { const form = go.closest('[data-merge-form]'); const child = Number(form.dataset.child); go.disabled = true; post({ dispatch_merge: 1, child_id: child, parent_id: form.dataset.parent, comment: form.querySelector('[data-merge-reason]').value, move_replies: form.querySelector('[data-merge-replies]').checked ? 1 : 0 }).then(function (json) { toast(json.message, 'success'); if (child === scheduling.ticket.id) { // This one is closed now - nothing left to book bootstrap.Modal.getOrCreateInstance(scheduleModalEl).hide(); } else { form.closest('[data-other]').remove(); if (!othersEl.querySelector('[data-other]')) { othersEl.hidden = true; } } loadQueue(); refreshBoard(); }).catch(function (error) { go.disabled = false; toast(error.message, 'error'); }); } }); scheduleForm.addEventListener('submit', function (e) { e.preventDefault(); if (!scheduling) { return; } const button = scheduleForm.querySelector('[type=submit]'); button.disabled = true; const tell = scheduleForm.elements.tell_client; post({ dispatch_book: 1, ticket_id: scheduling.ticket.id, agent_id: scheduleForm.elements.agent_id.value, start: scheduleForm.elements.start.value.replace('T', ' ') + ':00', minutes: scheduleForm.elements.minutes.value, onsite: scheduleForm.elements.onsite.value, tell_client: tell ? (tell.checked ? 1 : 0) : 1 }).then(function (json) { scheduling.done = true; toast(json.message, 'success'); if (json.conflicts && json.conflicts.length) { toast('Already booked then: ' + json.conflicts.join('; '), 'warning'); } bootstrap.Modal.getOrCreateInstance(scheduleModalEl).hide(); reloadAll(); }).catch(function (error) { scheduleError.textContent = error.message; scheduleError.hidden = false; }).finally(function () { button.disabled = false; }); }); scheduleModalEl.addEventListener('hidden.bs.modal', function () { if (scheduling) { const done = scheduling.done; scheduling.resolve(done); scheduling = null; if (!done) { refreshBoard(); // anything dragged goes back where it was } } }); const confirmEl = document.getElementById('dispatchConfirmModal'); let confirming = null; function confirmBox(title, text) { return new Promise(function (resolve) { confirming = {resolve: resolve, ok: false}; confirmEl.querySelector('[data-dispatch-confirm-title]').textContent = title; confirmEl.querySelector('[data-dispatch-confirm-text]').textContent = text; bootstrap.Modal.getOrCreateInstance(confirmEl).show(); }); } confirmEl.querySelector('[data-dispatch-confirm-ok]').addEventListener('click', function () { if (confirming) { confirming.ok = true; } bootstrap.Modal.getOrCreateInstance(confirmEl).hide(); }); confirmEl.addEventListener('hidden.bs.modal', function () { if (confirming) { confirming.resolve(confirming.ok); confirming = null; } }); function unbook(ticket) { if (!canEdit) { return; } confirmBox('Take ' + (ticket.reference || 'it') + ' off the schedule?', 'The agent - and the contact and watchers, when client emails are on - are sent the cancellation. It goes back to the queue.').then(function (ok) { if (!ok) { refreshBoard(); return; } post({dispatch_unbook: 1, ticket_id: ticket.id}).then(function (json) { toast(json.message, 'success'); reloadAll(); }).catch(function (error) { toast(error.message, 'error'); refreshBoard(); }); }); } function moveEvent(props, start, end, allDay, from, to) { post({ dispatch_event_move: 1, event_id: props.event_id, from: from, to: to, all_day: allDay ? 1 : 0, start: ymd(start) + ' ' + (allDay ? '00:00' : hm(start)) + ':00', end: end && !allDay ? ymd(end) + ' ' + hm(end) + ':00' : '' }).then(function (json) { toast(json.message, 'success'); refreshBoard(); }).catch(function (error) { toast(error.message, 'error'); refreshBoard(); }); } // ---------- the queue ---------- function ticketFromQueue(el) { return queueTickets.find(function (t) { return t.id === Number(el.dataset.ticket); }) || {id: Number(el.dataset.ticket), reference: el.dataset.reference || ''}; } function ticketFromItem(event, props) { return { id: Number(props.ticket_id), reference: props.reference || '', subject: event.title, agent: Number(props.agent), onsite: props.icon === 'fa-car' ? 1 : 0, minutes: event.end && event.start ? Math.round((event.end - event.start) / 60000) : defaultMinutes }; } function loadQueue() { request('GET', Object.assign({dispatch_queue: state.tab}, state.filters)).then(function (json) { queueTickets = json.tickets || []; Object.keys(json.counts || {}).forEach(function (key) { const badge = root.querySelector('[data-dispatch-count="' + key + '"]'); if (badge) { badge.textContent = json.counts[key] ? String(json.counts[key]) : ''; } }); root.querySelectorAll('[data-dispatch-tab]').forEach(function (tab) { tab.classList.toggle('active', tab.dataset.dispatchTab === state.tab); }); if (!queueTickets.length) { queueEl.innerHTML = '
' + esc({ unassigned: 'Every open ticket has an agent.', unbooked: 'Nothing waiting - every ticket with an agent is booked.', left_behind: 'Nothing left behind - every open ticket booked before today has been moved on or done.' }[state.tab]) + '
'; return; } queueEl.innerHTML = queueTickets.map(function (t) { return '
' + '
' + '' + esc(t.reference + ' ' + t.subject) + '
' + '
' + esc([t.client, t.agent_name, t.booked ? 'booked ' + t.booked : t.age, t.onsite ? 'Onsite' : ''].filter(Boolean).join(' · ')) + '
'; }).join(''); }).catch(function (error) { queueEl.innerHTML = '
' + esc(error.message) + '
'; }); } queueEl.addEventListener('click', function (e) { const item = e.target.closest('.itflow-dispatch-item'); if (item) { openModal('modals/ticket/ticket.php?id=' + Number(item.dataset.ticket)); } }); root.querySelectorAll('[data-dispatch-tab]').forEach(function (tab) { tab.addEventListener('click', function (e) { e.preventDefault(); state.tab = tab.dataset.dispatchTab; loadQueue(); }); }); let filterTimer = null; root.querySelectorAll('[data-dispatch-filter]').forEach(function (input) { input.addEventListener(input.tagName === 'SELECT' ? 'change' : 'input', function () { state.filters[input.dataset.dispatchFilter] = input.value; clearTimeout(filterTimer); filterTimer = setTimeout(loadQueue, input.tagName === 'SELECT' ? 0 : 300); }); }); function openModal(url) { const link = document.createElement('a'); link.href = '#'; link.className = 'ajax-modal d-none'; link.dataset.modalUrl = url; link.dataset.modalSize = 'lg'; document.body.appendChild(link); link.click(); link.remove(); } function overQueue(jsEvent) { const rect = queueEl.getBoundingClientRect(); return jsEvent && jsEvent.clientX >= rect.left && jsEvent.clientX <= rect.right && jsEvent.clientY >= rect.top && jsEvent.clientY <= rect.bottom; } // ---------- the board ---------- function range() { if (state.view === 'week') { const start = weekStart(state.date); return [start, addDays(start, 6)]; } return [state.date, state.date]; } function loadItems() { const r = range(); return request('GET', {dispatch_items: 1, from: ymd(r[0]), to: ymd(r[1]), agents: shownAgents().map(function (a) { return a.id; }).join(',')}).then(function (items) { lastItems = Array.isArray(items) ? items : []; return lastItems; }); } function teardown() { calendars.forEach(function (cal) { cal.destroy(); }); calendars = []; sortables.forEach(function (s) { s.destroy(); }); sortables = []; if (queueDraggable) { queueDraggable.destroy(); queueDraggable = null; } } function render() { teardown(); root.querySelectorAll('[data-dispatch-view]').forEach(function (b) { b.classList.toggle('active', b.dataset.dispatchView === state.view); }); root.querySelectorAll('[data-dispatch-nav], [data-dispatch-date]').forEach(function (el) { el.disabled = state.view === 'workload'; }); root.querySelector('[data-dispatch-date]').value = ymd(state.date); root.querySelector('[data-dispatch-agent-count]').textContent = '(' + shownAgents().length + '/' + agents.length + ')'; const r = range(); titleEl.textContent = state.view === 'workload' ? 'Workload - open tickets and the hours booked this week' : (state.view === 'week' ? r[0].toLocaleDateString(undefined, {month: 'short', day: 'numeric'}) + ' - ' + r[1].toLocaleDateString(undefined, {month: 'short', day: 'numeric', year: 'numeric'}) : state.date.toLocaleDateString(undefined, {weekday: 'long', month: 'long', day: 'numeric', year: 'numeric'})); if (!shownAgents().length) { board.innerHTML = '
Pick the agents to show under Agents.
'; return; } if (state.view === 'day') { renderDay(); } else if (state.view === 'week') { renderWeek(); } else { renderWorkload(); } } // [scroll] opens the day at its first hour - a new day; a refresh keeps the place function refreshBoard(scroll) { if (state.view === 'day') { loadItems().then(function (items) { calendars.forEach(function (cal) { cal.removeAllEventSources(); cal.addEventSource(items.filter(function (item) { return Number(item.extendedProps.agent) === cal.dispatchAgent; })); }); dayTotals(items); if (scroll === true) { requestAnimationFrame(scrollDay); } }); } else { render(); } } function reloadAll() { loadQueue(); refreshBoard(); loadStats(); } // An item drawn as on the calendar page: time, icon, title, and where / who function itemContent(arg) { const props = arg.event.extendedProps || {}; const wrap = document.createElement('div'); wrap.className = 'itflow-cal-event ' + (arg.event.allDay ? 'is-daygrid' : 'is-timegrid'); // Onsite work stands out - an orange strip with a car across the top, as on the calendar if (props.icon === 'fa-car') { wrap.classList.add('is-onsite'); if (!arg.event.allDay) { const strip = document.createElement('div'); strip.className = 'itflow-cal-onsite'; strip.innerHTML = 'Onsite'; wrap.appendChild(strip); } } if (arg.timeText && !arg.event.allDay) { const time = document.createElement('span'); time.className = 'itflow-cal-time'; time.textContent = arg.timeText; wrap.appendChild(time); } if (props.icon) { const icon = document.createElement('i'); icon.className = 'fas fa-fw ' + props.icon + ' itflow-cal-icon'; wrap.appendChild(icon); } const title = document.createElement('span'); title.className = 'itflow-cal-title'; title.textContent = arg.event.title; wrap.appendChild(title); if (props.detail && !arg.event.allDay) { const detail = document.createElement('div'); detail.className = 'itflow-cal-detail'; detail.textContent = props.detail; wrap.appendChild(detail); } return {domNodes: [wrap]}; } // The hours the day's grid shows - the working day, with room either side function slotRange() { if (zone || !businessHours || businessHours.startTime === '00:00') { return ['00:00:00', '24:00:00']; } const start = businessHours.startTime < '06:00' ? businessHours.startTime : '06:00'; const end = businessHours.endTime > '21:00' ? businessHours.endTime : '21:00'; return [start + ':00', end === '24:00' ? '24:00:00' : end + ':00']; } // Day: a time grid for each agent, side by side function renderDay() { const shown = shownAgents(); board.innerHTML = '
' + shown.map(function (a) { return '
' + dot(a.color) + esc(a.name) + '
'; }).join('') + '
'; const slots = slotRange(); board.querySelectorAll('.itflow-dispatch-col').forEach(function (col, index) { const agentId = Number(col.dataset.agent); const cal = new FullCalendar.Calendar(col.querySelector('[data-dispatch-cal]'), { initialView: 'timeGridDay', initialDate: ymd(state.date), headerToolbar: false, dayHeaders: false, allDaySlot: true, allDayText: '', height: 'auto', slotMinTime: slots[0], slotMaxTime: slots[1], now: zone ? zoneNow : undefined, slotDuration: '00:30:00', snapDuration: '00:15:00', slotEventOverlap: false, nowIndicator: true, editable: canEdit, droppable: canEdit, eventDurationEditable: canEdit, // The company's hours mean nothing on another clock - left unshaded there businessHours: zone ? false : (businessHours || false), // The times down the side once, on the first column - the others keep a blank the // label's height, so the rows line up across slotHeaderContent: function (arg) { return index === 0 ? arg.text : '\u00a0'; }, eventContent: itemContent, eventDidMount: function (info) { const props = info.event.extendedProps || {}; info.el.dataset.kind = props.kind || ''; info.el.setAttribute('title', info.event.title + (props.detail ? '\n' + props.detail : '')); }, eventClick: function (info) { info.jsEvent.preventDefault(); const props = info.event.extendedProps || {}; if (props.modal) { openModal(props.modal); } }, // A drag over empty time: New Ticket for this agent, booked for the time dragged over - // a drag, not a click; saving it comes back here on that day selectable: canEdit, selectMirror: true, selectMinDistance: 5, selectAllow: function (info) { return !info.allDay; }, select: function (info) { cal.unselect(); const params = new URLSearchParams({ assigned_to: String(agentId), schedule: ymd(info.start) + 'T' + hm(info.start), schedule_minutes: String(Math.round((info.end - info.start) / 60000)), return_to: 'dispatch' }); openModal('modals/ticket/ticket_add.php?' + params.toString()); }, // From the queue - a booking from another agent's column comes in by eventReceive drop: function (info) { if (!queueEl.contains(info.draggedEl)) { return; } const start = new Date(info.date.getTime()); if (info.allDay) { const parts = businessStart().split(':'); start.setHours(Number(parts[0]), Number(parts[1]), 0, 0); } schedule(ticketFromQueue(info.draggedEl), start, agentId); }, // To another time, the same agent eventDrop: function (info) { const props = info.event.extendedProps || {}; if (props.kind === 'event') { moveEvent(props, info.event.start, info.event.end, info.event.allDay, agentId, agentId); return; } schedule(ticketFromItem(info.event, props), info.event.start, agentId); }, eventResize: function (info) { const props = info.event.extendedProps || {}; if (props.kind === 'event') { moveEvent(props, info.event.start, info.event.end, false, agentId, agentId); return; } const ticket = ticketFromItem(info.event, props); schedule(ticket, info.event.start, agentId, ticket.minutes); }, // From another agent's column eventReceive: function (info) { const props = info.event.extendedProps || {}; if (props.kind === 'event') { moveEvent(props, info.event.start, info.event.end, info.event.allDay, Number(props.agent), agentId); return; } schedule(ticketFromItem(info.event, props), info.event.start, agentId); }, // Back onto the queue eventDragStart: function () { queueEl.classList.add('is-drop-target'); }, eventDragStop: function (info) { queueEl.classList.remove('is-drop-target'); const props = info.event.extendedProps || {}; if (props.kind === 'ticket' && overQueue(info.jsEvent)) { unbook(ticketFromItem(info.event, props)); } } }); cal.dispatchAgent = agentId; cal.render(); calendars.push(cal); }); if (canEdit && FullCalendar.Interaction && FullCalendar.Interaction.Draggable) { queueDraggable = new FullCalendar.Interaction.Draggable(queueEl, { itemSelector: '.itflow-dispatch-item', eventData: function (item) { const minutes = ticketFromQueue(item).minutes || defaultMinutes; return {title: item.dataset.reference, duration: pad(Math.floor(minutes / 60)) + ':' + pad(minutes % 60), create: false, color: '#6c757d'}; } }); } refreshBoard(true); } // The hour to open the day at: the working day's start - or, when the board runs around the clock, // an hour before now today and 8am on other days function scrollHour() { if (slotRange()[0] !== '00:00:00') { return Number(businessStart().split(':')[0]); } if (ymd(state.date) === ymd(today())) { return Math.max(0, (zone ? parseDateTime(zoneNow()) : new Date()).getHours() - 1); } return 8; } // One scroller for every column - opened at scrollHour(), once the bookings are in function scrollDay() { const scroller = board.querySelector('.itflow-dispatch-cols'); const slot = scroller ? scroller.querySelector('.itflow-dispatch-col [data-time="' + pad(scrollHour()) + ':00:00"]') : null; if (slot) { const head = scroller.querySelector('.itflow-dispatch-col-head'); scroller.scrollTop += slot.getBoundingClientRect().top - scroller.getBoundingClientRect().top - (head ? head.offsetHeight : 0) - 4; } } function dayTotals(items) { board.querySelectorAll('.itflow-dispatch-col').forEach(function (col) { let minutes = 0; items.forEach(function (item) { if (Number(item.extendedProps.agent) === Number(col.dataset.agent) && ['ticket', 'helper'].indexOf(item.extendedProps.kind) !== -1 && !item.allDay) { minutes += minutesBetween(item.start, item.end); } }); col.querySelector('[data-dispatch-booked]').textContent = minutes ? lengthText(minutes) + ' booked' : ''; }); } // Week: a row for each agent, a column for each day function renderWeek() { const start = range()[0]; const days = []; for (let i = 0; i < 7; i++) { const d = addDays(start, i); if (state.weekends || (d.getDay() !== 0 && d.getDay() !== 6)) { days.push(d); } } board.innerHTML = '
Loading...
'; loadItems().then(function (items) { const now = ymd(today()); let html = '
'; days.forEach(function (d) { html += ''; }); html += ''; shownAgents().forEach(function (a) { // The week's booked hours - weekends too, shown or not, as Workload counts them let week = 0; items.forEach(function (item) { if (Number(item.extendedProps.agent) === a.id && ['ticket', 'helper'].indexOf(item.extendedProps.kind) !== -1 && !item.allDay) { week += minutesBetween(item.start, item.end); } }); const cells = days.map(function (d) { const day = ymd(d); const chips = items.filter(function (item) { return Number(item.extendedProps.agent) === a.id && String(item.start).substring(0, 10) === day; }).sort(function (x, y) { return String(x.start).localeCompare(String(y.start)); }); const off = businessHours && businessHours.daysOfWeek && businessHours.daysOfWeek.indexOf(d.getDay()) === -1; // A box of a fixed height - one that grew as a booking passed over would move the rows under the pointer return ''; }).join(''); html += '' + cells + ''; }); board.innerHTML = html + '
Agent' + esc(d.toLocaleDateString(undefined, {weekday: 'short', month: 'short', day: 'numeric'})) + '
' + chips.map(function (item) { const props = item.extendedProps || {}; const minutes = item.allDay ? 0 : minutesBetween(item.start, item.end); const movable = !!item.editable; return '
' + (props.icon ? '' : '') + esc((item.allDay ? '' : String(item.start).substring(11, 16) + ' ') + item.title) + '
'; }).join('') + '
' + dot(a.color) + esc(a.name) + '
' + (week ? esc(lengthText(week)) + ' booked' : '') + '
'; board.querySelectorAll('.itflow-dispatch-chip').forEach(function (chip) { chip.addEventListener('click', function () { if (chip.dataset.modal) { openModal(chip.dataset.modal); } }); }); if (!canEdit || typeof Sortable === 'undefined') { return; } board.querySelectorAll('.itflow-dispatch-cell-list').forEach(function (cell) { sortables.push(Sortable.create(cell, { group: {name: 'dispatch', pull: true, put: true}, draggable: '.is-movable', sort: false, onAdd: function (evt) { const el = evt.item; const agentId = Number(cell.dataset.agent); const day = parseYmd(cell.dataset.date); const from = evt.from; el.remove(); if (from === queueEl) { // From the queue: the start of the working day const parts = businessStart().split(':'); day.setHours(Number(parts[0]), Number(parts[1]), 0, 0); schedule(ticketFromQueue(el), day, agentId); return; } const was = parseDateTime(el.dataset.start) || day; day.setHours(was.getHours(), was.getMinutes(), 0, 0); const fromAgent = Number(from.dataset.agent); if (el.dataset.kind === 'event') { const length = Number(el.dataset.minutes) * 60000; moveEvent({event_id: Number(el.dataset.event)}, day, length ? new Date(day.getTime() + length) : null, el.dataset.allDay === '1', fromAgent, agentId); return; } schedule({id: Number(el.dataset.ticket), reference: el.dataset.reference, subject: el.title.split('\n')[0], onsite: Number(el.dataset.onsite), agent: fromAgent}, day, agentId, Number(el.dataset.minutes) || defaultMinutes); } })); }); // The queue takes from the board (to take a booking off) and gives to it sortables.push(Sortable.create(queueEl, { group: {name: 'dispatch', pull: 'clone', put: true}, draggable: '.itflow-dispatch-item.is-movable, .itflow-dispatch-chip', sort: false, onAdd: function (evt) { const el = evt.item; el.remove(); if (el.dataset.kind === 'ticket') { unbook({id: Number(el.dataset.ticket), reference: el.dataset.reference}); } else { refreshBoard(); } } })); }).catch(function (error) { board.innerHTML = '
' + esc(error.message) + '
'; }); } // Workload: each agent's open tickets, SLAs and booked hours - drop a ticket on an agent to give it to them function bar(minutes, capacity) { const pct = capacity ? Math.min(100, Math.round(minutes / capacity * 100)) : 0; const tone = capacity && minutes > capacity ? 'bg-danger' : (pct >= 85 ? 'bg-warning' : 'bg-primary'); return '
' + esc(lengthText(minutes) || '0 min') + (capacity ? ' of ' + esc(lengthText(capacity)) + '' : '') + '
' + '
'; } function renderWorkload() { board.innerHTML = '
Loading...
'; request('GET', {dispatch_workload: 1, agents: shownAgents().map(function (a) { return a.id; }).join(','), first_day: firstDay}).then(function (json) { const flagTones = {behind: 'danger', watch: 'warning', overloaded: 'warning', room: 'success'}; let html = '
' + ''; (json.agents || []).forEach(function (a) { html += '' + '' + '' + '' + '' + '' + '' + '' + ''; html += ''; }); html += '
AgentOpenSLAPast dueNot bookedQuietBooked todayBooked this week
' + dot(a.color) + '' + esc(a.name) + ' ' + (a.flags || []).map(function (f) { return '' + esc(f.text) + ''; }).join('') + (a.oldest ? '
Oldest ' + esc(a.oldest) + '
' : '') + '
' + a.open + (a.helping ? ' (' + a.helping + ' helping)' : '') + '' + (a.sla.breached ? '' + a.sla.breached + '' : '0') + ' / ' + (a.sla.at_risk ? '' + a.sla.at_risk + '' : '0') + '' + (a.overdue ? '' + a.overdue + '' : '0') + '' + a.unbooked + '' + a.quiet + '' + bar(a.booked_today, a.capacity_today) + '' + bar(a.booked_week, a.capacity_week) + '
' + (a.tickets.length ? '' : '
No open tickets.
') + '
Team average: ' + esc(json.team.open) + ' open, ' + esc(lengthText(json.team.booked_week) || '0 min') + ' booked this week. Click an agent for their tickets' + (canEdit ? ' - drop a ticket from the queue on one to give it to them.' : '.') + '
'; board.innerHTML = html; board.querySelectorAll('.itflow-dispatch-workload-row').forEach(function (row) { row.addEventListener('click', function () { const id = Number(row.dataset.agent); expanded[id] = !expanded[id]; board.querySelector('[data-tickets-for="' + id + '"]').hidden = !expanded[id]; }); }); if (!canEdit || typeof Sortable === 'undefined') { return; } board.querySelectorAll('.itflow-dispatch-drop').forEach(function (drop) { sortables.push(Sortable.create(drop, { group: {name: 'dispatch', pull: false, put: true}, sort: false, onAdd: function (evt) { const ticket = ticketFromQueue(evt.item); evt.item.remove(); post({dispatch_assign: 1, ticket_id: ticket.id, agent_id: drop.dataset.agent}).then(function (json) { toast(json.message, 'success'); reloadAll(); }).catch(function (error) { toast(error.message, 'error'); }); } })); }); sortables.push(Sortable.create(queueEl, { group: {name: 'dispatch', pull: 'clone', put: false}, draggable: '.itflow-dispatch-item.is-movable', sort: false, onStart: function () { board.classList.add('is-dragging'); }, onEnd: function () { board.classList.remove('is-dragging'); } })); }).catch(function (error) { board.innerHTML = '
' + esc(error.message) + '
'; }); } // ---------- This week ---------- let statsChart = null; function loadStats() { request('GET', {dispatch_week: 1, agents: shownAgents().map(function (a) { return a.id; }).join(','), first_day: firstDay}).then(function (json) { const t = json.totals; const s = json.sla; const from = parseYmd(json.week[0]); const to = parseYmd(json.week[1]); root.querySelector('[data-dispatch-week]').textContent = from.toLocaleDateString(undefined, {month: 'short', day: 'numeric'}) + ' - ' + to.toLocaleDateString(undefined, {month: 'short', day: 'numeric'}); const tile = function (label, value, sub) { return '
' + esc(label) + '
' + esc(value) + '
' + sub + '
'; }; const signed = function (n) { return (n > 0 ? '+' : '') + n; }; let html = '
' + tile('In', t.inbound, esc('Last week ' + t.last_inbound)) + tile('Closed', t.closed, esc('Last week ' + t.last_closed + ' · 4-week average ' + t.avg_closed)) + tile('In minus closed', signed(t.net), esc(t.net > 0 ? 'The queue grew' : (t.net < 0 ? 'The queue shrank' : 'Even'))) + tile('Open now', t.open, esc((s.breached_now || 0) + ' SLA breached · ' + (s.at_risk_now || 0) + ' close to it')) + tile('First responses', (s.response_met || 0) + ' met', esc((s.response_missed || 0) + ' missed this week')) + tile('Resolutions', (s.resolution_met || 0) + ' met', esc((s.resolution_missed || 0) + ' missed this week')) + '
' + '
' + (json.agents || []).map(function (a) { return ''; }).join('') + '
AgentGivenClosedOpenSLA missed
' + dot(a.color) + esc(a.name) + '' + a.assigned + '' + a.closed + '' + a.open + '' + (a.sla_missed ? '' + a.sla_missed + '' : '0') + '
'; statsEl.innerHTML = html; if (statsChart) { statsChart.destroy(); statsChart = null; } if (typeof Chart === 'undefined') { return; } const css = getComputedStyle(document.documentElement); const v = function (name, fallback) { return (css.getPropertyValue(name) || '').trim() || fallback; }; const base = typeof itflowChartDefaults === 'function' ? itflowChartDefaults() : {grid: 'rgba(0,0,0,.1)'}; statsChart = new Chart(statsEl.querySelector('canvas'), { type: 'bar', data: { labels: json.days.map(function (d) { return d.label; }), datasets: [ {label: 'In', data: json.days.map(function (d) { return d.future ? null : d.inbound; }), backgroundColor: v('--itflow-accent', '#007bff'), borderRadius: 4, maxBarThickness: 18}, {label: 'Closed', data: json.days.map(function (d) { return d.future ? null : d.closed; }), backgroundColor: v('--bs-success', '#198754'), borderRadius: 4, maxBarThickness: 18} ] }, options: { responsive: true, maintainAspectRatio: false, interaction: {mode: 'index', intersect: false}, plugins: {legend: {position: 'bottom', labels: {boxWidth: 12}}}, scales: { x: {grid: {display: false}}, y: {beginAtZero: true, ticks: {precision: 0}, grid: {color: base.grid}} } } }); }).catch(function (error) { statsEl.innerHTML = '
' + esc(error.message) + '
'; }); } // ---------- the toolbar ---------- root.querySelectorAll('[data-dispatch-view]').forEach(function (button) { button.addEventListener('click', function () { state.view = button.dataset.dispatchView; saveState(); render(); }); }); root.querySelectorAll('[data-dispatch-nav]').forEach(function (button) { button.addEventListener('click', function () { const step = Number(button.dataset.dispatchNav); state.date = step === 0 ? today() : addDays(state.date, step * (state.view === 'week' ? 7 : 1)); render(); }); }); root.querySelector('[data-dispatch-date]').addEventListener('change', function (e) { if (e.target.value) { state.date = parseYmd(e.target.value); render(); } }); root.querySelectorAll('[data-dispatch-agent]').forEach(function (box) { box.addEventListener('change', function () { const id = Number(box.value); state.hidden = state.hidden.filter(function (h) { return h !== id; }); if (!box.checked) { state.hidden.push(id); } saveState(); render(); loadStats(); }); }); root.querySelector('[data-dispatch-weekends]').addEventListener('change', function (e) { state.weekends = e.target.checked; saveState(); if (state.view === 'week') { render(); } }); // The ticket's quick look can resolve, reassign or book a ticket document.addEventListener('hidden.bs.modal', function (e) { if (e.target !== scheduleModalEl && e.target !== confirmEl) { reloadAll(); } }); loadQueue(); render(); loadStats(); })();