(async () => { const old = document.getElementById('mytime'); if (old) { old.remove(); return; } if (!window.BX || !BX.bitrix_sessid) { alert('Открой портал Битрикс24 и войди в него'); return; } const norm = (window.myTimeConfig || {}).norm || 0; const call = async (method, params) => { const r = await fetch('/rest/' + method + '.json?sessid=' + BX.bitrix_sessid(), { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(params), }); const d = await r.json(); if (d.error) throw d.error + ' ' + (d.error_description || ''); return d.result; }; const pad = n => String(n).padStart(2, '0'); const hm = s => Math.floor(s / 3600) + ':' + pad(Math.floor(s % 3600 / 60)); const esc = s => String(s).replace(/[&<>"]/g, c => ({ '&': '&', '<': '<', '>': '>', '"': '"' }[c])); const box = document.createElement('div'); box.id = 'mytime'; box.style.cssText = 'position:fixed;top:70px;right:20px;z-index:99999;background:#fff;padding:16px 20px;' + 'box-shadow:0 4px 24px rgba(0,0,0,.25);border-radius:8px;max-height:80vh;overflow:auto;font:14px/1.5 sans-serif;color:#333'; const now = new Date(); box.innerHTML = 'Моё время ' + '
'; document.body.append(box); const out = document.getElementById('mytime-out'); document.getElementById('mytime-x').onclick = () => box.remove(); async function run() { const ym = document.getElementById('mytime-m').value; const [y, m] = ym.split('-').map(Number); const next = m === 12 ? (y + 1) + '-01' : y + '-' + pad(m + 1); out.textContent = 'Считаю…'; try { const me = (await call('user.current', {})).ID; const perTask = {}; for (let page = 1; ; page++) { const items = await call('task.elapseditem.getlist', { ORDER: { ID: 'asc' }, FILTER: { USER_ID: me, '>=CREATED_DATE': ym + '-01T00:00:00', ' a + perTask[id], 0); const left = norm * 3600 - total; const normRow = !norm ? '' : '' + (left > 0 ? 'До нормы ' + norm + ' ч осталось ' + hm(left) + '' : 'Норма ' + norm + ' ч выполнена' + (left < 0 ? ', сверх нормы ' + hm(-left) : '')) + ''; out.innerHTML = '' + ids.sort((a, b) => perTask[b] - perTask[a]).map(id => '' + '' ).join('') + '' + normRow + '
' + hm(perTask[id]) + '#' + id + ' ' + esc(titles[id] || '') + '
' + hm(total) + 'Итого за ' + ym + '
'; } catch (e) { out.textContent = 'Ошибка: ' + e; } } document.getElementById('mytime-m').onchange = run; run(); })();