From b24f135cf307a4560670d63c98331b1769daea1f Mon Sep 17 00:00:00 2001 From: zakhy Date: Fri, 9 Aug 2024 13:32:04 +0700 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=B2=D1=80=D0=B5=D0=BC=D0=B5=D0=BD=D0=B8=20?= =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=8B=20=D1=84=D0=B0=D0=B1=D1=80?= =?UTF-8?q?=D0=B8=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../templates/fabric_worktime_widget.html | 32 +++---- app/static/js/charts/worktime.js | 92 +++++++++++++++++-- 2 files changed, 97 insertions(+), 27 deletions(-) diff --git a/app/apps/main/templates/fabric_worktime_widget.html b/app/apps/main/templates/fabric_worktime_widget.html index 2ba6d87..fcb9a30 100644 --- a/app/apps/main/templates/fabric_worktime_widget.html +++ b/app/apps/main/templates/fabric_worktime_widget.html @@ -14,7 +14,7 @@
-
+
За смену
@@ -23,25 +23,23 @@ Под нагрузкой - - 0ч. - + 0 ч.
Без нагрузки - - 0ч. + 0 ч.
Не работала - 0ч. @@ -50,7 +48,7 @@ Общее время работы - 0ч. @@ -74,7 +72,7 @@ Под нагрузкой - 0ч. @@ -83,7 +81,7 @@ Без нагрузки - 0ч. @@ -92,7 +90,7 @@ Не работала - 0ч. @@ -101,7 +99,7 @@ Общее время работы - 0ч. @@ -125,7 +123,7 @@ Под нагрузкой - 0ч. @@ -134,7 +132,7 @@ Без нагрузки - 0ч. @@ -143,7 +141,7 @@ Не работала - 0ч. @@ -152,7 +150,7 @@ Общее время работы - 0ч. diff --git a/app/static/js/charts/worktime.js b/app/static/js/charts/worktime.js index 757a8c8..36c8c9d 100644 --- a/app/static/js/charts/worktime.js +++ b/app/static/js/charts/worktime.js @@ -1,4 +1,73 @@ (function () { + + function parseDuration(durationStr) { + // Разбиваем строку по символам времени + const parts = durationStr.match(/\d+(?=DT|H|M|S)/g); + let days = 0, hours = 0, minutes = 0, seconds = 0; + + if (parts) { + days = parseInt(parts[0]) || 0; + hours = parseInt(parts[1]) || 0; + minutes = parseInt(parts[2]) || 0; + seconds = parseInt(parts[3]) || 0; + } + + return { + days: days, + hours: hours, + minutes: minutes, + seconds: seconds + }; + } + + function addDurations(duration1, duration2) { + // Складываем соответствующие поля двух объектов длительности + let totalSeconds = duration1.seconds + duration2.seconds; + let totalMinutes = duration1.minutes + duration2.minutes; + let totalHours = duration1.hours + duration2.hours; + let totalDays = duration1.days + duration2.days; + + // Учет переполнений + if (totalSeconds >= 60) { + totalMinutes += Math.floor(totalSeconds / 60); + totalSeconds %= 60; + } + + if (totalMinutes >= 60) { + totalHours += Math.floor(totalMinutes / 60); + totalMinutes %= 60; + } + + if (totalHours >= 24) { + totalDays += Math.floor(totalHours / 24); + totalHours %= 24; + } + + return { + days: totalDays, + hours: totalHours, + minutes: totalMinutes, + seconds: totalSeconds + }; + } + + function makeTimeString(timeObj) { + let timeString = '' + if (timeObj.days !== 0) { + timeString += `${timeObj.days} д. ` + } + if (timeObj.hours !== 0) { + timeString += `${timeObj.hours} ч. ` + } + if (timeObj.minutes !== 0) { + timeString += `${timeObj.minutes} м. ` + } + if (timeString === '') { + timeString = '0 ч. 0 м.' + } + return timeString + } + const workTimeChartOptions = { chart: { type: 'donut', @@ -34,30 +103,33 @@ monthlyWorkChart.render(); function fetchWorkTime() { - fetch('worktime/').then(response => { + fetch('worktime/durations').then(response => { return response.json() }).then(json => { for (let item in json) { for (let status in json[item]) { let chartElement = document.querySelector(`#${item}${status}`) - chartElement.textContent = json[item][status] + ' ч.' + let timeObj = parseDuration(json[item][status]) + chartElement.textContent = makeTimeString(timeObj) } - let chartElementSUM = document.querySelector(`#${item}SUM`) - chartElementSUM.textContent = (json[item]['WO'] + json[item]['WA']).toFixed(0) + ' ч.' - let workedWO = json[item]['WO'] - let workedWA = json[item]['WA'] - let workedNW = json[item]['NW'] + let workedWO = parseDuration(json[item]['WO']) + let workedWA = parseDuration(json[item]['WA']) + let workedNW = parseDuration(json[item]['NW']) + const sumObj = addDurations(workedWO, workedWA) + const sumTimeString = makeTimeString(sumObj) + const spanSum = document.querySelector(`#${item}SUM`) + spanSum.textContent = sumTimeString switch (item) { case 'daily': - dailyWorkChart.updateSeries([workedWO, workedWA, workedNW]) + dailyWorkChart.updateSeries([workedWO.hours, workedWA.hours, workedNW.hours]) break case 'weekly': - weeklyWorkChart.updateSeries([workedWO, workedWA, workedNW]) + weeklyWorkChart.updateSeries([workedWO.hours, workedWA.hours, workedNW.hours]) break case 'monthly': - monthlyWorkChart.updateSeries([workedWO, workedWA, workedNW]) + monthlyWorkChart.updateSeries([workedWO.hours, workedWA.hours, workedNW.hours]) break } }