Fix wrong calendar numbers (#2484)

Signed-off-by: Denis Maslennikov <denis.maslennikov@gmail.com>
This commit is contained in:
Denis Maslennikov 2022-12-29 12:52:53 +07:00 committed by GitHub
parent 415e545d00
commit 9daef6444d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 211 additions and 162 deletions

View File

@ -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<Ref<RequestType>, RequestType> = new Map<Ref<RequestType>, 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<Ref<Employee>, EmployeeReports> = new Map()
@ -174,7 +184,16 @@
<YearView {departmentStaff} {employeeRequests} {types} {currentDate} />
{:else if mode === CalendarMode.Month}
{#if display === 'chart'}
<MonthView {departmentStaff} {employeeRequests} {types} {startDate} {editableList} {currentDate} {timeReports} />
<MonthView
{departmentStaff}
{employeeRequests}
{types}
{startDate}
{endDate}
{editableList}
{currentDate}
{timeReports}
/>
{:else if display === 'stats'}
<MonthTableView {departmentStaff} {employeeRequests} {types} {currentDate} {timeReports} />
{/if}

View File

@ -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<Ref<Staff>, Request[]>
export let timeReports: Map<Ref<Employee>, 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<Staff>, date: Date): Request[] {
const endDate = getEndDate(date.getFullYear(), date.getMonth())
function getRequests (employee: Ref<Staff>, 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<string, BuildModelKey> {
@ -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<string, BuildModelKey> {
const typevals = getTypeVals(month)
function getOverrideConfig (startDate: Date): Map<string, BuildModelKey> {
const typevals = getTypeVals(startDate)
const endDate = getEndDate(startDate.getFullYear(), startDate.getMonth())
return new Map<string, BuildModelKey>([
[
'@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<Staff>, month), month.getMonth(), types) -
getTotal(getRequests(a._id as Ref<Staff>, month), month.getMonth(), types)
getTotal(getStatRequests(b._id as Ref<Staff>, startDate), startDate, endDate, types) -
getTotal(getStatRequests(a._id as Ref<Staff>, 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<Staff>, month), month.getMonth(), types) -
getTotal(getRequests(a._id as Ref<Staff>, month), month.getMonth(), types)
getTotal(getStatRequests(b._id as Ref<Staff>, startDate), startDate, endDate, types) -
getTotal(getStatRequests(a._id as Ref<Staff>, 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<Staff>, month), month.getMonth(), types, (a) =>
getTotal(getStatRequests(b._id as Ref<Staff>, startDate), startDate, endDate, types, (a) =>
a < 0 ? Math.abs(a) : 0
) -
getTotal(getRequests(a._id as Ref<Staff>, month), month.getMonth(), types, (a) => (a < 0 ? Math.abs(a) : 0))
getTotal(getStatRequests(a._id as Ref<Staff>, 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<Staff>, month), month.getMonth(), types, (a) =>
getTotal(getStatRequests(b._id as Ref<Staff>, startDate), startDate, endDate, types, (a) =>
a > 0 ? Math.abs(a) : 0
) -
getTotal(getRequests(a._id as Ref<Staff>, month), month.getMonth(), types, (a) => (a > 0 ? Math.abs(a) : 0))
getTotal(getStatRequests(a._id as Ref<Staff>, startDate), startDate, endDate, types, (a) =>
a > 0 ? Math.abs(a) : 0
)
}
],
...typevals

View File

@ -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<Ref<Staff>, Request[]>, date: Date, employee?: Ref<Staff>): 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)}
</td>
<td class="p-1 text-center">
{#if rTime !== undefined}
@ -173,22 +151,22 @@
{/if}
</td>
{#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]}
<td
class="w-9 max-w-9 min-w-9"
class:today={areDatesEqual(todayDate, date)}
class:weekend={isWeekend(date)}
class:today={areDatesEqual(todayDate, day)}
class:weekend={isWeekend(day)}
class:cursor-pointer={editable}
class:hovered={i === hoveredIndex}
class:firstLine={row === 0}
class:lastLine={row === departmentStaff.length - 1}
use:tooltip={tooltipValue}
on:click={(e) => createRequest(e, date, employee)}
on:click={(e) => createRequest(e, day, employee)}
on:mousemove={() => {
hoveredColumn = i
}}
@ -211,7 +189,7 @@
<Label label={hr.string.Summary} />
</td>
<td class="flex-center p-1 whitespace-nowrap text-center summary">
{getTotal(Array.from(employeeRequests.values()).flat(), startDate.getMonth(), types)}
{getTotal(Array.from(employeeRequests.values()).flat(), startDate, endDate, types)}
</td>
<td class="p-1 text-center summary">
{floorFractionDigits(
@ -222,13 +200,13 @@
)}
</td>
{#each values as value, i}
{@const date = getDay(startDate, value)}
{@const requests = getRequests(employeeRequests, date)}
{@const day = getDay(startDate, value)}
{@const requests = getRequests(employeeRequests, day)}
<td
class="p-1 text-center summary"
class:hovered={i === hoveredIndex}
class:weekend={isWeekend(date)}
class:today={areDatesEqual(todayDate, date)}
class:weekend={isWeekend(day)}
class:today={areDatesEqual(todayDate, day)}
on:mousemove={() => {
hoveredColumn = i
}}
@ -236,11 +214,11 @@
hoveredColumn = -1
}}
>
{getTotal(requests, startDate.getMonth(), types)}
{getTotal(requests, day, day, types)}
</td>
{/each}
</tr>
</tbody>
</tbody>getTotal
</table>
</Scroller>
{:else}

View File

@ -20,9 +20,9 @@
export let value: Staff
export let display: (requests: Request[]) => number | string
export let month: Date
export let getRequests: (employee: Ref<Staff>, date: Date) => Request[]
export let getStatRequests: (employee: Ref<Staff>, date: Date) => Request[]
$: reqs = getRequests(value._id, month)
$: reqs = getStatRequests(value._id, month)
$: _value = display(reqs)
</script>

View File

@ -19,7 +19,7 @@
import type { Request, RequestType, Staff } from '@hcengineering/hr'
import { Label, LabelAndProps, Scroller, tableHRscheduleY, tooltip } from '@hcengineering/ui'
import hr from '../../plugin'
import { fromTzDate, getMonth, getTotal, weekDays } from '../../utils'
import { getEndDate, getRequests, getStartDate, getTotal, isToday, weekDays } from '../../utils'
import RequestsPopup from '../RequestsPopup.svelte'
export let currentDate: Date = new Date()
@ -29,31 +29,6 @@
export let employeeRequests: Map<Ref<Staff>, Request[]>
const todayDate = new Date()
function getRequests (employeeRequests: Map<Ref<Staff>, Request[]>, date: Date, employee?: Ref<Staff>): 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 getEndDate (date: Date): number {
return new Date(date).setMonth(date.getMonth() + 1)
}
function getTooltip (requests: Request[]): LabelAndProps | undefined {
if (requests.length === 0) return
return {
@ -80,10 +55,10 @@
<Label label={contact.string.Employee} />
</th>
{#each values as value, i}
{@const month = getMonth(currentDate, value)}
{@const startDate = getStartDate(currentDate.getFullYear(), value)}
<th
class="fixed first-row"
class:today={month.getFullYear() === todayDate.getFullYear() && month.getMonth() === todayDate.getMonth()}
class:today={isToday(startDate)}
on:mousemove={() => {
hoveredIndex = i
}}
@ -91,7 +66,7 @@
hoveredIndex = -1
}}
>
{getMonthName(month)}
{getMonthName(startDate)}
</th>
{/each}
</tr>
@ -102,13 +77,10 @@
</span>
</th>
{#each values as value, i}
{@const month = getMonth(currentDate, value)}
<th
class="fixed last-row"
class:today={month.getFullYear() === todayDate.getFullYear() && month.getMonth() === todayDate.getMonth()}
>
{@const startDate = getStartDate(currentDate.getFullYear(), value)}
<th class="fixed last-row" class:today={isToday(startDate)}>
<span class="flex-center">
{weekDays(month.getUTCFullYear(), month.getUTCMonth())}
{weekDays(startDate.getUTCFullYear(), startDate.getUTCMonth())}
</span>
</th>
{/each}
@ -121,18 +93,14 @@
<EmployeePresenter value={employee} />
</td>
{#each values as value, i}
{@const month = getMonth(currentDate, value)}
{@const requests = getRequests(employeeRequests, month, employee._id)}
{@const startDate = getStartDate(currentDate.getFullYear(), value)}
{@const endDate = getEndDate(currentDate.getFullYear(), value)}
{@const requests = getRequests(employeeRequests, startDate, endDate, employee._id)}
{@const tooltipValue = getTooltip(requests)}
{#key tooltipValue}
<td
class:today={month.getFullYear() === todayDate.getFullYear() &&
month.getMonth() === todayDate.getMonth()}
class="fixed td-body"
use:tooltip={tooltipValue}
>
<td class:today={isToday(startDate)} class="fixed td-body" use:tooltip={tooltipValue}>
<div class="flex-center">
{getTotal(requests, value, types)}
{getTotal(requests, startDate, endDate, types)}
</div>
</td>
{/key}
@ -144,14 +112,12 @@
<Label label={hr.string.Summary} />
</td>
{#each values as value, i}
{@const month = getMonth(currentDate, value)}
{@const requests = getRequests(employeeRequests, month)}
<td
class:today={month.getFullYear() === todayDate.getFullYear() && month.getMonth() === todayDate.getMonth()}
class="fixed td-body summary"
>
{@const startDate = getStartDate(currentDate.getFullYear(), value)}
{@const endDate = getEndDate(currentDate.getFullYear(), value)}
{@const requests = getRequests(employeeRequests, startDate, endDate)}
<td class:today={isToday(startDate)} class="fixed td-body summary">
<div class="flex-center">
{getTotal(requests, value, types)}
{getTotal(requests, startDate, endDate, types)}
</div>
</td>
{/each}

View File

@ -1,11 +1,13 @@
import { Employee, formatName } from '@hcengineering/contact'
import { Ref, TxOperations } from '@hcengineering/core'
import { Department, Request, RequestType, TzDate } from '@hcengineering/hr'
import { Department, Request, RequestType, Staff, TzDate } from '@hcengineering/hr'
import { MessageBox } from '@hcengineering/presentation'
import { TimeSpendReport } from '@hcengineering/tracker'
import { isWeekend, showPopup } from '@hcengineering/ui'
import { isWeekend, MILLISECONDS_IN_DAY, showPopup } from '@hcengineering/ui'
import hr from './plugin'
const todayDate = new Date()
export async function addMember (client: TxOperations, employee?: Employee, value?: Department): Promise<void> {
if (employee === null || employee === undefined || value === undefined) {
return
@ -99,14 +101,69 @@ export function getMonth (date: Date, m: number): Date {
return date
}
export function getRequestDays (request: Request, types: Map<Ref<RequestType>, RequestType>, month: number): number[] {
/**
* @public
*/
export function getStartDate (year: number, month?: number): Date {
return new Date(new Date(year, month !== undefined ? month : 0).setHours(0, 0, 0, 0))
}
/**
* @public
*/
export function getEndDate (year: number, month?: number): Date {
return new Date(
new Date(
new Date(
month !== undefined && month === 11 ? year + 1 : year,
month !== undefined ? (month + 1) % 12 : 11
).setDate(0)
).setHours(23, 59, 59, 999)
)
}
/**
* @public
*/
export function isToday (date: Date): boolean {
return date.getFullYear() === todayDate.getFullYear() && date.getMonth() === todayDate.getMonth()
}
/**
* @public
*/
export function getRequests (
employeeRequests: Map<Ref<Staff>, Request[]>,
startDate: Date,
endDate?: Date,
employee?: Ref<Staff>
): Request[] {
const startTime = new Date(startDate).setHours(0, 0, 0, 0)
const endTime =
endDate != null ? new Date(endDate).setHours(23, 59, 59, 999) : new Date(startDate).setHours(23, 59, 59, 999)
let requests
if (employee != null) {
requests = employeeRequests.get(employee)
} else {
requests = Array.from(employeeRequests.values()).flat()
}
if (requests === undefined) return []
return requests.filter(
(request) => fromTzDate(request.tzDate) <= endTime && fromTzDate(request.tzDueDate) > startTime
)
}
export function getRequestDates (
request: Request,
types: Map<Ref<RequestType>, RequestType>,
year: number,
month: number
): number[] {
const type = types.get(request.type)
const startDate =
request.tzDate.month === month ? fromTzDate(request.tzDate) : fromTzDate({ ...request.tzDate, month, day: 1 })
const startDate = request.tzDate.month === month ? fromTzDate(request.tzDate) : new Date().setFullYear(year, month, 1)
const endDate =
request.tzDueDate.month === month
? fromTzDate(request.tzDueDate)
: fromTzDate({ ...request.tzDueDate, month: month + 1, day: 0 })
request.tzDueDate.month === month ? fromTzDate(request.tzDueDate) : new Date().setFullYear(year, month + 1, 0)
const days = Math.floor(Math.abs((1 + endDate - startDate) / 1000 / 60 / 60 / 24)) + 1
const stDate = new Date(startDate)
const stDateDate = stDate.getDate()
@ -117,17 +174,43 @@ export function getRequestDays (request: Request, types: Map<Ref<RequestType>, R
return ds
}
export function getRequestDays (
request: Request,
types: Map<Ref<RequestType>, RequestType>,
startDate: Date,
endDate: Date
): number {
const type = types.get(request.type)
const startTime = new Date(fromTzDate(request.tzDate)).setHours(0, 0, 0, 0)
const endTime = new Date(fromTzDate(request.tzDueDate)).setHours(23, 59, 59, 999)
const end = endDate.getTime()
let current = startDate.getTime()
let days = 0
while (current <= end) {
if (
current >= startTime &&
current <= endTime &&
((type?.value ?? -1) > 0 || ((type?.value ?? -1) < 0 && !isWeekend(new Date(current))))
) {
days++
}
current += MILLISECONDS_IN_DAY
}
return days
}
export function getTotal (
requests: Request[],
month: number,
startDate: Date,
endDate: Date,
types: Map<Ref<RequestType>, RequestType>,
f: (v: number) => number = (f) => f
): number {
let total = 0
for (const request of requests) {
const ds = getRequestDays(request, types, month)
const ds = getRequestDays(request, types, startDate, endDate)
const type = types.get(request.type)
const val = Math.ceil(ds.length) * (type?.value ?? 0)
const val = ds * (type?.value ?? 0)
total += f(val)
}
return total