import { RecordLeaveInput, RecordTimesheetInput, } from "@/app/api/timesheets/actions"; import { Box, Card, CardActionArea, CardContent, Stack, Typography, } from "@mui/material"; import union from "lodash/union"; import { useCallback, useEffect, useMemo } from "react"; import dayjs, { Dayjs } from "dayjs"; import { getHolidayForDate, getPublicHolidaysForNYears } from "@/app/utils/holidayUtils"; import { HolidaysResult } from "@/app/api/holidays"; import { manhourFormatter, shortDateFormatter } from "@/app/utils/formatUtil"; import { useTranslation } from "react-i18next"; import pickBy from "lodash/pickBy"; interface Props { currentMonth: Dayjs; timesheet: RecordTimesheetInput; leaves: RecordLeaveInput; companyHolidays: HolidaysResult[]; onDateSelect: (date: string) => void; } const MonthlySummary: React.FC = ({ timesheet, leaves, currentMonth, companyHolidays, onDateSelect, }) => { const { t, i18n: { language }, } = useTranslation("home"); // calendar related const holidays = useMemo(() => { const holidays = getPublicHolidaysForNYears(1, currentMonth.year()).map(holiday => holiday.date) return holidays.filter(date => { return currentMonth.isSame(dayjs(date), "month") }) }, [currentMonth]); const timesheetForCurrentMonth = useMemo(() => { return pickBy(timesheet, (_, date) => { return currentMonth.isSame(dayjs(date), "month"); }); }, [currentMonth, timesheet]); const leavesForCurrentMonth = useMemo(() => { return pickBy(leaves, (_, date) => { return currentMonth.isSame(dayjs(date), "month"); }); }, [currentMonth, leaves]); const days = useMemo(() => { return union( Object.keys(timesheetForCurrentMonth), Object.keys(leavesForCurrentMonth), holidays ); }, [timesheetForCurrentMonth, leavesForCurrentMonth, holidays]).sort(); const makeSelectDate = useCallback( (date: string) => () => { onDateSelect(date); }, [onDateSelect], ); useEffect(()=> { console.log(holidays) console.log(timesheetForCurrentMonth) },[currentMonth, timesheetForCurrentMonth]) return ( {t("Monthly Summary")} {days.map((day, index) => { const dayJsObj = dayjs(day); const holiday = getHolidayForDate(day, companyHolidays); const isHoliday = holiday || dayJsObj.day() === 0 || dayJsObj.day() === 6; const ls = leavesForCurrentMonth[day]; const leaveHours = ls?.reduce((acc, entry) => acc + entry.inputHours, 0) || 0; const ts = timesheetForCurrentMonth[day]; const timesheetNormalHours = ts?.reduce((acc, entry) => acc + (entry.inputHours || 0), 0) || 0; const timesheetOtHours = ts?.reduce((acc, entry) => acc + (entry.otHours || 0), 0) || 0; const timesheetHours = timesheetNormalHours + timesheetOtHours; return ( {shortDateFormatter(language).format(dayJsObj.toDate())} {holiday && ( {`(${holiday.title})`} )} {t("Timesheet Hours")} {manhourFormatter.format(timesheetHours)} {t("Leave Hours")} {manhourFormatter.format(leaveHours)} {t("Daily Total Hours")} {manhourFormatter.format(timesheetHours + leaveHours)} ); })} {`${t("Total Monthly Work Hours")}: ${manhourFormatter.format( Object.values(timesheetForCurrentMonth) .flatMap((entries) => entries) .map((entry) => (entry.inputHours ?? 0) + (entry.otHours ?? 0)) .reduce((acc, cur) => { return acc + cur; }, 0), )}`} {`${t("Total Monthly Leave Hours")}: ${manhourFormatter.format( Object.values(leavesForCurrentMonth) .flatMap((entries) => entries) .map((entry) => entry.inputHours) .reduce((acc, cur) => { return acc + cur; }, 0), )}`} ); }; export default MonthlySummary;