diff --git a/plugins/hr-resources/src/components/ScheduleView.svelte b/plugins/hr-resources/src/components/ScheduleView.svelte index 1ff17590bb..cfa7637d54 100644 --- a/plugins/hr-resources/src/components/ScheduleView.svelte +++ b/plugins/hr-resources/src/components/ScheduleView.svelte @@ -31,14 +31,16 @@ export let mode: CalendarMode export let display: 'chart' | 'stats' - $: startDate = new Date( - new Date(mode === CalendarMode.Year ? new Date(currentDate).setMonth(0) : currentDate).setDate(1) - ) - $: endDate = new Date( + $: startDate = mode === CalendarMode.Year - ? new Date(new Date(startDate).setFullYear(new Date(startDate).getFullYear() + 1)).setDate(0) - : new Date(new Date(startDate).setMonth(new Date(startDate).getMonth() + 1)).setDate(0) - ) + ? getStartDate(currentDate.getFullYear(), 0) + : getStartDate(currentDate.getFullYear(), currentDate.getMonth()) + + $: endDate = + mode === CalendarMode.Year + ? getEndDate(currentDate.getFullYear(), 11) + : getEndDate(currentDate.getFullYear(), currentDate.getMonth()) + $: departments = [department, ...getDescendants(department, descendants)] const lq = createQuery() @@ -47,6 +49,7 @@ const currentEmployee = (getCurrentAccount() as EmployeeAccount).employee let staff: Staff[] = [] + let reqests: Request[] = [] let types: Map, RequestType> = new Map, RequestType>() typeQuery.query(hr.class.RequestType, {}, (res) => { @@ -87,27 +90,34 @@ hr.class.Request, { 'tzDueDate.year': { $gte: startDate.getFullYear() }, - 'tzDueDate.month': { $gte: startDate.getMonth() }, 'tzDate.year': { $lte: endDate.getFullYear() }, - 'tzDate.month': { $lte: endDate.getMonth() }, space: { $in: departments } }, (res) => { - employeeRequests.clear() - for (const request of res) { - const requests = employeeRequests.get(request.attachedTo) ?? [] - requests.push(request) - if (request.attachedTo) { - employeeRequests.set(request.attachedTo, requests) - } - } - employeeRequests = employeeRequests + reqests = res } ) } $: update(departments, startDate, endDate) + function updateRequest (reqests: Request[], startDate: Date, endDate: Date) { + const res = reqests.filter( + (r) => fromTzDate(r.tzDueDate) >= startDate.getTime() && fromTzDate(r.tzDate) <= endDate.getTime() + ) + employeeRequests.clear() + for (const request of res) { + const requests = employeeRequests.get(request.attachedTo) ?? [] + requests.push(request) + if (request.attachedTo) { + employeeRequests.set(request.attachedTo, requests) + } + } + employeeRequests = employeeRequests + } + + $: updateRequest(reqests, startDate, endDate) + function updateEditableList () { editableList = [] departmentById.forEach((department) => { @@ -139,7 +149,7 @@ const reportQuery = createQuery() import tracker from '@hcengineering/tracker' - import { EmployeeReports } from '../utils' + import { EmployeeReports, fromTzDate, getEndDate, getStartDate } from '../utils' let timeReports: Map, EmployeeReports> = new Map() @@ -174,7 +184,16 @@ {:else if mode === CalendarMode.Month} {#if display === 'chart'} - + {:else if display === 'stats'} {/if} diff --git a/plugins/hr-resources/src/components/schedule/MonthTableView.svelte b/plugins/hr-resources/src/components/schedule/MonthTableView.svelte index af28ba47f7..fd171687a8 100644 --- a/plugins/hr-resources/src/components/schedule/MonthTableView.svelte +++ b/plugins/hr-resources/src/components/schedule/MonthTableView.svelte @@ -22,7 +22,17 @@ import view, { BuildModelKey, Viewlet, ViewletPreference } from '@hcengineering/view' import { Table, ViewletSettingButton } from '@hcengineering/view-resources' import hr from '../../plugin' - import { EmployeeReports, fromTzDate, getMonth, getRequestDays, getTotal, tableToCSV, weekDays } from '../../utils' + import { + EmployeeReports, + getEndDate, + getMonth, + getRequestDates, + getRequests, + getStartDate, + getTotal, + tableToCSV, + weekDays + } from '../../utils' import StatPresenter from './StatPresenter.svelte' import ReportPresenter from './ReportPresenter.svelte' @@ -34,30 +44,18 @@ export let employeeRequests: Map, Request[]> export let timeReports: Map, EmployeeReports> - $: month = getMonth(currentDate, currentDate.getMonth()) + $: month = getStartDate(currentDate.getFullYear(), currentDate.getMonth()) // getMonth(currentDate, currentDate.getMonth()) $: wDays = weekDays(month.getUTCFullYear(), month.getUTCMonth()) function getDateRange (request: Request): string { - const ds = getRequestDays(request, types, month.getMonth()) + const ds = getRequestDates(request, types, month.getFullYear(), month.getMonth()) return ds.join(' ') } - function getEndDate (date: Date): number { - return new Date(date).setMonth(date.getMonth() + 1) - } + function getStatRequests (employee: Ref, date: Date): Request[] { + const endDate = getEndDate(date.getFullYear(), date.getMonth()) - function getRequests (employee: Ref, date: Date): Request[] { - const requests = employeeRequests.get(employee) - if (requests === undefined) return [] - const res: Request[] = [] - const time = date.getTime() - const endTime = getEndDate(date) - for (const request of requests) { - if (fromTzDate(request.tzDate) <= endTime && fromTzDate(request.tzDueDate) > time) { - res.push(request) - } - } - return res + return getRequests(employeeRequests, date, endDate, employee) } function getTypeVals (month: Date): Map { @@ -69,21 +67,22 @@ label: it.label, presenter: StatPresenter, props: { - month: month ?? getMonth(currentDate, currentDate.getMonth()), + month: month ?? getStartDate(currentDate.getFullYear(), currentDate.getMonth()), display: (req: Request[]) => req .filter((r) => r.type === it._id) .map((it) => getDateRange(it)) .join(' '), - getRequests + getStatRequests } } ]) ) } - function getOverrideConfig (month: Date): Map { - const typevals = getTypeVals(month) + function getOverrideConfig (startDate: Date): Map { + const typevals = getTypeVals(startDate) + const endDate = getEndDate(startDate.getFullYear(), startDate.getMonth()) return new Map([ [ '@wdCount', @@ -92,14 +91,14 @@ label: getEmbeddedLabel('Working days'), presenter: StatPresenter, props: { - month: month ?? getMonth(currentDate, currentDate.getMonth()), - display: (req: Request[]) => wDays + getTotal(req, month.getMonth(), types), - getRequests + month: startDate ?? getStartDate(currentDate.getFullYear(), currentDate.getMonth()), + display: (req: Request[]) => wDays + getTotal(req, startDate, endDate, types), + getStatRequests }, sortingKey: '@wdCount', sortingFunction: (a: Doc, b: Doc) => - getTotal(getRequests(b._id as Ref, month), month.getMonth(), types) - - getTotal(getRequests(a._id as Ref, month), month.getMonth(), types) + getTotal(getStatRequests(b._id as Ref, startDate), startDate, endDate, types) - + getTotal(getStatRequests(a._id as Ref, startDate), startDate, endDate, types) } ], [ @@ -109,13 +108,13 @@ label: getEmbeddedLabel('Reported days'), presenter: ReportPresenter, props: { - month: month ?? getMonth(currentDate, currentDate.getMonth()), + month: startDate ?? getStartDate(currentDate.getFullYear(), currentDate.getMonth()), display: (staff: Staff) => (timeReports.get(staff._id) ?? { value: 0 }).value }, sortingKey: '@wdCount', sortingFunction: (a: Doc, b: Doc) => - getTotal(getRequests(b._id as Ref, month), month.getMonth(), types) - - getTotal(getRequests(a._id as Ref, month), month.getMonth(), types) + getTotal(getStatRequests(b._id as Ref, startDate), startDate, endDate, types) - + getTotal(getStatRequests(a._id as Ref, startDate), startDate, endDate, types) } ], [ @@ -125,16 +124,18 @@ label: getEmbeddedLabel('PTOs'), presenter: StatPresenter, props: { - month: month ?? getMonth(currentDate, currentDate.getMonth()), - display: (req: Request[]) => getTotal(req, month.getMonth(), types, (a) => (a < 0 ? Math.abs(a) : 0)), - getRequests + month: startDate ?? getMonth(currentDate, currentDate.getMonth()), + display: (req: Request[]) => getTotal(req, startDate, endDate, types, (a) => (a < 0 ? Math.abs(a) : 0)), + getStatRequests }, sortingKey: '@ptoCount', sortingFunction: (a: Doc, b: Doc) => - getTotal(getRequests(b._id as Ref, month), month.getMonth(), types, (a) => + getTotal(getStatRequests(b._id as Ref, startDate), startDate, endDate, types, (a) => a < 0 ? Math.abs(a) : 0 ) - - getTotal(getRequests(a._id as Ref, month), month.getMonth(), types, (a) => (a < 0 ? Math.abs(a) : 0)) + getTotal(getStatRequests(a._id as Ref, startDate), startDate, endDate, types, (a) => + a < 0 ? Math.abs(a) : 0 + ) } ], [ @@ -144,16 +145,18 @@ label: getEmbeddedLabel('EXTRa'), presenter: StatPresenter, props: { - month: month ?? getMonth(currentDate, currentDate.getMonth()), - display: (req: Request[]) => getTotal(req, month.getMonth(), types, (a) => (a > 0 ? Math.abs(a) : 0)), - getRequests + month: startDate ?? getMonth(currentDate, currentDate.getMonth()), + display: (req: Request[]) => getTotal(req, startDate, endDate, types, (a) => (a > 0 ? Math.abs(a) : 0)), + getStatRequests }, sortingKey: '@extraCount', sortingFunction: (a: Doc, b: Doc) => - getTotal(getRequests(b._id as Ref, month), month.getMonth(), types, (a) => + getTotal(getStatRequests(b._id as Ref, startDate), startDate, endDate, types, (a) => a > 0 ? Math.abs(a) : 0 ) - - getTotal(getRequests(a._id as Ref, month), month.getMonth(), types, (a) => (a > 0 ? Math.abs(a) : 0)) + getTotal(getStatRequests(a._id as Ref, startDate), startDate, endDate, types, (a) => + a > 0 ? Math.abs(a) : 0 + ) } ], ...typevals diff --git a/plugins/hr-resources/src/components/schedule/MonthView.svelte b/plugins/hr-resources/src/components/schedule/MonthView.svelte index d7d5d503a1..8e53cb1424 100644 --- a/plugins/hr-resources/src/components/schedule/MonthView.svelte +++ b/plugins/hr-resources/src/components/schedule/MonthView.svelte @@ -34,7 +34,7 @@ tooltip } from '@hcengineering/ui' import hr from '../../plugin' - import { EmployeeReports, fromTzDate, getTotal } from '../../utils' + import { EmployeeReports, getRequests, getTotal } from '../../utils' import CreateRequest from '../CreateRequest.svelte' import RequestsPopup from '../RequestsPopup.svelte' import ScheduleRequests from '../ScheduleRequests.svelte' @@ -42,6 +42,7 @@ export let currentDate: Date = new Date() export let startDate: Date + export let endDate: Date export let departmentStaff: Staff[] @@ -52,25 +53,6 @@ const todayDate = new Date() - function getRequests (employeeRequests: Map, Request[]>, date: Date, employee?: Ref): Request[] { - let requests = undefined - if (employee) { - requests = employeeRequests.get(employee) - } else { - requests = Array.from(employeeRequests.values()).flat() - } - if (requests === undefined) return [] - const res: Request[] = [] - const time = date.getTime() - const endTime = getEndDate(date) - for (const request of requests) { - if (fromTzDate(request.tzDate) <= endTime && fromTzDate(request.tzDueDate) > time) { - res.push(request) - } - } - return res - } - function createRequest (e: MouseEvent, date: Date, staff: Staff): void { if (!isEditable(staff)) return const readonly = editableList.length === 1 @@ -93,10 +75,6 @@ return editableList.includes(employee._id) } - function getEndDate (date: Date): number { - return new Date(date).setDate(date.getDate() + 1) - } - function getTooltip (requests: Request[]): LabelAndProps | undefined { if (requests.length === 0) return return { @@ -163,7 +141,7 @@ class:firstLine={row === 0} class:lastLine={row === departmentStaff.length - 1} > - {getTotal(requests, startDate.getMonth(), types)} + {getTotal(requests, startDate, endDate, types)} {#if rTime !== undefined} @@ -173,22 +151,22 @@ {/if} {#each values as value, i} - {@const date = getDay(startDate, value)} - {@const requests = getRequests(employeeRequests, date, employee._id)} + {@const day = getDay(startDate, value)} + {@const requests = getRequests(employeeRequests, day, day, employee._id)} {@const editable = isEditable(employee)} {@const tooltipValue = getTooltip(requests)} - {@const ww = findReports(employee, date, timeReports)} + {@const ww = findReports(employee, day, timeReports)} {#key [tooltipValue, editable]} createRequest(e, date, employee)} + on:click={(e) => createRequest(e, day, employee)} on:mousemove={() => { hoveredColumn = i }} @@ -211,7 +189,7 @@