import { setHours, setMinutes } from 'date-fns' import moment from 'moment' import timelineUtils from '@/plugins/tm@dashboard/tasks/_utils/timeline.utils' export default { computed: { calendarData() { if (!this.subtasks || !this.blockers) return const blockers = this._generateBlockers() return { tasks: [...this._generateNonWorking(), ...this._generateTasks(), ...blockers.filter(blocker => blocker.type != 'home_office')], homeoffices: [...blockers.filter(blocker => blocker.type == 'home_office')] } }, splitDays() { const days = [] const homeoffice = {} const now = moment() this.filter.team.employees.forEach((employee, index) => { const selectedDate = moment(this.filter.start) const capacityMinutes = this._getRemainingWorkCapacityMinutes(employee) let scheduledCapacityMinutes = 0 this.calendarData.tasks.forEach(task => { // console.log(task) if ((task.subtask?.employee_id != employee.id) && (task.blocker?.employee?.id != employee.id)) return if (task.type == 'non-working-hours' || task.subtask?.is_finished || task.blocker?.is_finished) return if (selectedDate.isBefore(moment(), 'days')) { // Past return } else if (selectedDate.isSame(moment(), 'days')) { // Today } else { scheduledCapacityMinutes += timelineUtils._getDifferenceInMinutes(task._real_end, task._real_start) } }) this.calendarData.homeoffices.forEach(task => { if (task.blocker.employee?.id != employee.id) return homeoffice[task.split] = true }) const unscheduledTime = capacityMinutes - scheduledCapacityMinutes days.push({ id: index + 1, name: employee.name, fullname: `${employee.name} ${employee.surname}`, initials: employee.name.charAt(0).toUpperCase() + employee.surname.charAt(0).toUpperCase(), unscheduledTime: unscheduledTime > 0 ? (unscheduledTime / 60).toFixed(1) : 0, isHomeoffice: homeoffice[index + 1] ? true : false }) }) return days }, }, methods: { _generateNonWorking() { const nonWorkingHours = [] const date = new Date(this.filter.start) let earliestHour = null this.calendarConfig.date = date const { start: dayStart, end: dayEnd } = this._getDayStartEnd() this.filter.team.employees.forEach((employee, index) => { if (!earliestHour || (employee.work_start) < earliestHour) earliestHour = employee.work_start nonWorkingHours.push({ title: "Non working hours", start: dayStart, end: this._setMinutesToDate(employee.work_start * 60), split: index + 1, type: 'non-working-hours', draggable: false }) nonWorkingHours.push({ title: "Non working hours", start: this._setMinutesToDate(employee.work_end * 60), end: dayEnd, split: index + 1, type: 'non-working-hours', draggable: false }) }) this.calendarConfig.scrollHour = (earliestHour) - 1 return nonWorkingHours }, _generateTasks() { const tasks = [] const calendarDate = moment(this.filter.start).format('DD-MM-YYYY') this.subtasks.forEach(subtask => { let split = null let employee = null this.filter.team.employees.forEach((e, index) => { if (e.id == subtask.employee_id) { split = index + 1 employee = e } }) subtask.timeblocks.forEach(timeblock => { const formattedDate = moment(timeblock.start).format('DD-MM-YYYY') if (formattedDate == calendarDate) { tasks.push({ id: subtask.id, start: new Date(timeblock.start), end: new Date(timeblock.end), master_task: subtask.master_task, name: employee?.name, surname: employee?.surname, // date: new Date(timeblock.start), isWeekend: moment(timeblock.start).day() % 6 == 0, initials: employee?.name?.charAt(0)?.toUpperCase() + employee?.surname?.charAt(0)?.toUpperCase(), split: split, project_code: subtask.project_code, type: 'subtask', subtask, _timeblock_id: timeblock.id, _real_start: new Date(timeblock.start), _real_end: new Date(timeblock.end) }) } }) }) return tasks }, _generateBlockers() { const blockers = [] this.blockers.forEach(blocker => { let split = null let employee = null this.filter.team.employees.forEach((e, index) => { if (e.id == blocker.employee?.id) { split = index + 1 employee = e } }) blockers.push({ blocker, split, start: new Date(blocker.start), end: new Date(blocker.end), type: blocker.type, isBlocker: true, initials: `${employee?.name[0]}${employee?.surname[0]}`, color: '#B00020', draggable: false, isWeekend: moment(blocker.start).day() % 6 == 0, _real_start: new Date(blocker.start), _real_end: new Date(blocker.end) }) }) return blockers }, _getDayStartEnd() { return { start: this._setMinutesToDate(0), end: this._setMinutesToDate(1439) } }, _setMinutesToDate(minutes, date = new Date(this.filter.start)) { date.setHours(Math.floor(minutes / 60), minutes % 60, 0, 0) return date }, _getRemainingWorkCapacityMinutes(employee) { const date = moment(this.filter.start) const now = moment() if (now.isAfter(date, 'days')) { // Past dates return 0 } else if (now.isBefore(date, 'days')) { // Future dates return (employee.work_end - employee.work_start) * 60 } // Today const workStartMinutes = employee.work_start * 60 const workEndMinutes = employee.work_end * 60 const elapsedMinutesOfCurrenyDay = now.diff(moment().startOf('day'), 'minutes') if (workStartMinutes > elapsedMinutesOfCurrenyDay) return workEndMinutes - workStartMinutes else if (workEndMinutes < elapsedMinutesOfCurrenyDay) return 0 else return workEndMinutes - elapsedMinutesOfCurrenyDay } } }