diff --git a/plugins/hr-resources/src/components/ScheduleRequests.svelte b/plugins/hr-resources/src/components/ScheduleRequests.svelte index 33eebdfdf1..7e9747a670 100644 --- a/plugins/hr-resources/src/components/ScheduleRequests.svelte +++ b/plugins/hr-resources/src/components/ScheduleRequests.svelte @@ -13,17 +13,18 @@ // limitations under the License. -->
{#each requests as request} {#await getType(request) then type} - {#if type && !(isWeekend(date) || (isHoliday(holidays, date) && noWeekendHolidayType.includes(type._id)))} + {#if type && !(isWeekend(date) || (isHoliday(getHolidayDatesForEmployee(staffDepartmentMap, employee._id, holidays), date) && noWeekendHolidayType.includes(type._id)))}
export let descendants: Map, Department[]> @@ -221,7 +223,7 @@ } } ) - let holidays: Date[] | undefined = undefined + let holidays: Map, Date[]> = new Map() const holidaysQuery = createQuery() $: holidaysQuery.query( hr.class.PublicHoliday, @@ -230,32 +232,85 @@ 'date.year': currentDate.getFullYear() }, (res) => { - holidays = res.map((holiday) => new Date(fromTzDate(holiday.date))) + const group = groupBy(res, 'department') + holidays = new Map() + for (const groupKey in group) { + holidays.set( + groupKey, + group[groupKey].map((holiday) => new Date(fromTzDate(holiday.date))) + ) + } } ) + + async function getHolidays (month: Date): Promise, Date[]>> { + const result = await client.findAll(hr.class.PublicHoliday, { + 'date.month': month.getMonth(), + 'date.year': month.getFullYear() + }) + const group = groupBy(result, 'department') + const rMap = new Map() + for (const groupKey in group) { + rMap.set( + groupKey, + group[groupKey].map((holiday) => new Date(fromTzDate(holiday.date))) + ) + } + return rMap + } + + const client = getClient() + + async function getDepartmentsForEmployee (departmentStaff: Staff[]): Promise, Department[]>> { + const map = new Map, Department[]>() + if (departmentStaff && departmentStaff.length > 0) { + const ids = departmentStaff.map((staff) => staff._id) + const staffs = await client.findAll(contact.class.EmployeeAccount, { employee: { $in: ids } }) + const departments = await client.findAll(hr.class.Department, { + members: { $in: staffs.map((staff) => staff._id) } + }) + staffs.forEach((staff) => { + const filteredDepartments = departments.filter((department) => department.members.includes(staff._id)) + map.set(staff.employee as Ref, filteredDepartments as Department[]) + }) + } + return map + } {#if departmentStaff.length} - {#if mode === CalendarMode.Year} - - {:else if mode === CalendarMode.Month} - {#if display === 'chart'} - - {:else if display === 'stats'} - + {#await getDepartmentsForEmployee(departmentStaff) then staffDepartmentMap} + {#if mode === CalendarMode.Year} + + {:else if mode === CalendarMode.Month} + {#if display === 'chart'} + + {:else if display === 'stats'} + + {/if} {/if} - {/if} + {/await} {:else}
- it._id) } }} - config={createConfig(descr, preference, month)} - options={descr.options} - /> + {#await createConfig(descr, preference, month) then config} +
it._id) } }} + {config} + options={descr.options} + /> + {/await} {/if} {/if} diff --git a/plugins/hr-resources/src/components/schedule/MonthView.svelte b/plugins/hr-resources/src/components/schedule/MonthView.svelte index ea4e4e10fb..01adb62bc1 100644 --- a/plugins/hr-resources/src/components/schedule/MonthView.svelte +++ b/plugins/hr-resources/src/components/schedule/MonthView.svelte @@ -35,7 +35,7 @@ tooltip } from '@hcengineering/ui' import hr from '../../plugin' - import { EmployeeReports, getRequests, getTotal } from '../../utils' + import { EmployeeReports, getHolidayDatesForEmployee, getRequests, getTotal, isHoliday } from '../../utils' import CreateRequest from '../CreateRequest.svelte' import RequestsPopup from '../RequestsPopup.svelte' import ScheduleRequests from '../ScheduleRequests.svelte' @@ -81,9 +81,16 @@ const noWeekendHolidayType: Ref[] = [hr.ids.PTO, hr.ids.PTO2, hr.ids.Vacation] - function getTooltip (requests: Request[], day: Date): LabelAndProps | undefined { + function getTooltip (requests: Request[], day: Date, staff: Staff): LabelAndProps | undefined { if (requests.length === 0) return - if (day && isWeekend(day) && requests.some((req) => noWeekendHolidayType.includes(req.type))) return + if ( + day && + (isWeekend(day) || + (holidays?.size > 0 && isHoliday(getHolidayDatesForEmployee(staffDepartmentMap, staff._id, holidays), day))) && + requests.some((req) => noWeekendHolidayType.includes(req.type)) + ) { + return + } return { component: RequestsPopup, props: { requests: requests.map((it) => it._id) } @@ -124,10 +131,8 @@ showPopup(CreatePublicHoliday, { date, department }) } - export let holidays: Date[] | undefined = undefined - function isHoliday (holidays: Date[], day: Date): boolean { - return holidays && holidays.some((date) => areDatesEqual(day, date)) - } + export let staffDepartmentMap: Map, Department[]> + export let holidays: Map, Date[]> {#if departmentStaff.length} @@ -144,7 +149,7 @@ {@const day = getDay(startDate, value)} @@ -230,7 +251,13 @@ {#each values as value, i} {@const day = getDay(startDate, value)} - {@const requests = getRequests(employeeRequests, day)} {/each} @@ -329,6 +355,9 @@ &.weekend:not(.today) { background-color: var(--accent-bg-color); } + &.holiday:not(.today) { + background-color: var(--system-error-60-color); + } } td:not(:last-child) { border-right: 1px solid var(--divider-color); diff --git a/plugins/hr-resources/src/components/schedule/StatPresenter.svelte b/plugins/hr-resources/src/components/schedule/StatPresenter.svelte index adc8c6481d..32e4f106a8 100644 --- a/plugins/hr-resources/src/components/schedule/StatPresenter.svelte +++ b/plugins/hr-resources/src/components/schedule/StatPresenter.svelte @@ -18,12 +18,12 @@ import { Request, Staff } from '@hcengineering/hr' export let value: Staff - export let display: (requests: Request[]) => number | string + export let display: (requests: Request[], staff: Staff) => number | string export let month: Date export let getStatRequests: (employee: Ref, date: Date) => Request[] $: reqs = getStatRequests(value._id, month) - $: _value = display(reqs) + $: _value = display(reqs, value) {_value} diff --git a/plugins/hr-resources/src/components/schedule/YearView.svelte b/plugins/hr-resources/src/components/schedule/YearView.svelte index b0b3c4d7ab..cdd5ef2dd5 100644 --- a/plugins/hr-resources/src/components/schedule/YearView.svelte +++ b/plugins/hr-resources/src/components/schedule/YearView.svelte @@ -19,8 +19,17 @@ import type { Request, RequestType, Staff } from '@hcengineering/hr' import { Label, LabelAndProps, Scroller, tableHRscheduleY, tooltip } from '@hcengineering/ui' import hr from '../../plugin' - import { getEndDate, getRequests, getStartDate, getTotal, isToday, weekDays } from '../../utils' + import { + getHolidayDatesForEmployee, + getEndDate, + getRequests, + getStartDate, + getTotal, + isToday, + weekDays + } from '../../utils' import RequestsPopup from '../RequestsPopup.svelte' + import { Department } from '@hcengineering/hr' export let currentDate: Date = new Date() @@ -29,7 +38,8 @@ export let employeeRequests: Map, Request[]> - export let holidays: Date[] | undefined = undefined + export let holidays: Map, Date[]> + export let staffDepartmentMap: Map, Department[]> function getTooltip (requests: Request[]): LabelAndProps | undefined { if (requests.length === 0) return @@ -102,7 +112,13 @@ {#key tooltipValue} {/key} @@ -116,10 +132,15 @@ {#each values as value, i} {@const startDate = getStartDate(currentDate.getFullYear(), value)} {@const endDate = getEndDate(currentDate.getFullYear(), value)} - {@const requests = getRequests(employeeRequests, startDate, endDate)} {/each} diff --git a/plugins/hr-resources/src/utils.ts b/plugins/hr-resources/src/utils.ts index d94935b296..00f624ce5c 100644 --- a/plugins/hr-resources/src/utils.ts +++ b/plugins/hr-resources/src/utils.ts @@ -226,6 +226,25 @@ export function tableToCSV (tableId: string, separator = ','): string { return csv.join('\n') } +export function getHolidayDatesForEmployee ( + departmentMap: Map, Department[]>, + employee: Ref, + holidays: Map, Date[]> +): Date[] { + if (departmentMap === undefined || departmentMap.size === 0) return [] + const deps = departmentMap.get(employee) + if (deps === undefined) return [] + if (holidays.size === 0) return [] + const dates = [] + for (const dep of deps) { + const depDates = holidays?.get(dep._id) + if (depDates !== undefined) { + dates.push(...depDates) + } + } + return dates +} + export interface EmployeeReports { reports: TimeSpendReport[] tasks: Map, Issue>
{ @@ -174,7 +179,13 @@ class:firstLine={row === 0} class:lastLine={row === departmentStaff.length - 1} > - {getTotal(requests, startDate, endDate, types, holidays)} + {getTotal( + requests, + startDate, + endDate, + types, + getHolidayDatesForEmployee(staffDepartmentMap, employee._id, holidays) + )}
0} class="h-full w-full"> {#if requests.length} - + {/if}
- {getTotal(Array.from(employeeRequests.values()).flat(), startDate, endDate, types, holidays)} + {getTotal( + Array.from(employeeRequests.values()).flat(), + startDate, + endDate, + types, + [...holidays.values()].flat() + )} {floorFractionDigits( @@ -242,11 +269,10 @@ { hoveredColumn = i @@ -255,7 +281,7 @@ hoveredColumn = -1 }} > - {getTotal(requests, day, day, types, holidays)} + {getTotal([...employeeRequests.values()].flat(), day, day, types, [...holidays.values()].flat())}
- {getTotal(requests, startDate, endDate, types, holidays)} + {getTotal( + requests, + startDate, + endDate, + types, + getHolidayDatesForEmployee(staffDepartmentMap, employee._id, holidays) + )}
- {getTotal(requests, startDate, endDate, types, holidays)} + {getTotal( + [...employeeRequests.values()].flat(), + startDate, + endDate, + types, + [...holidays.values()].flat() + )}