|
|
@@ -1,6 +1,6 @@ |
|
|
|
"use client"; |
|
|
|
import { FinancialByProject, fetchFinancialSummaryByProjectV2 } from '@/app/api/financialsummary'; |
|
|
|
import { Box, Card, CardContent, CardHeader, FormControl, InputLabel, MenuItem, Select, Stack } from '@mui/material'; |
|
|
|
import { Box, Card, CardContent, CardHeader, FormControl, FormControlLabel, InputLabel, MenuItem, Select, Stack, Switch } from '@mui/material'; |
|
|
|
import { useCallback, useEffect, useMemo, useState } from 'react'; |
|
|
|
import { useTranslation } from "react-i18next"; |
|
|
|
import ProjectFinancialCard from '../ProjectFinancialSummary/ProjectFinancialCard'; |
|
|
@@ -72,6 +72,7 @@ const FinancialSummaryPage: React.FC<Props> = ({ |
|
|
|
const [teamId, setTeamId] = useState(_teamId) |
|
|
|
const [isCardClickedIndex, setIsCardClickedIndex] = useState(_teamId || 0); |
|
|
|
const [period, setPeriod] = useState(0); |
|
|
|
const [isFuture, setIsFuture] = useState(false); |
|
|
|
|
|
|
|
const dateMap: DateParams = useMemo(() => { |
|
|
|
const thisYear = currDate <= `${currYear}-${endDate}` ? |
|
|
@@ -90,7 +91,16 @@ const FinancialSummaryPage: React.FC<Props> = ({ |
|
|
|
} |
|
|
|
return map |
|
|
|
}, [currDate, currYear, currFinancialYear, startDate, endDate, lengthOfCombo]) |
|
|
|
|
|
|
|
|
|
|
|
const futureDateMap: DateParams = useMemo(() => { |
|
|
|
const futureMap: DateParams = {}; |
|
|
|
for (let i = 0; i < lengthOfCombo; i++){ |
|
|
|
const financialStartDate = `${currYear+i}-${startDate}`; // Assuming financial year starts on April 1 |
|
|
|
const financialEndDate = `${currYear+i+1}-${endDate}`; // Assuming financial year ends on March 31 |
|
|
|
futureMap[i] = { startDate: financialStartDate, endDate: financialEndDate }; |
|
|
|
} |
|
|
|
return futureMap |
|
|
|
}, [currDate, currYear, startDate, endDate, lengthOfCombo]) |
|
|
|
// const comboList: string[] = useMemo(() => { |
|
|
|
// const list = ["All"] |
|
|
|
// var lastYear = "" |
|
|
@@ -118,6 +128,27 @@ const FinancialSummaryPage: React.FC<Props> = ({ |
|
|
|
return list; |
|
|
|
}, [currFinancialYear, lengthOfCombo, t]); |
|
|
|
|
|
|
|
const futureList: string[] = useMemo(() => { |
|
|
|
const result = []; |
|
|
|
|
|
|
|
// Get the keys of the futureDateMap |
|
|
|
const years = Object.keys(futureDateMap); |
|
|
|
|
|
|
|
// Iterate over the keys except the last one |
|
|
|
for (let i = 0; i < years.length - 1; i++) { |
|
|
|
const year = years[i]; |
|
|
|
result.push(`${year} - ${parseInt(year) + 1}`); |
|
|
|
} |
|
|
|
|
|
|
|
// Handle the last item separately |
|
|
|
if (years.length > 0) { |
|
|
|
const lastYear = years[years.length - 1]; |
|
|
|
result.push(`> ${lastYear}`); |
|
|
|
} |
|
|
|
|
|
|
|
return result; |
|
|
|
}, [futureDateMap]) |
|
|
|
|
|
|
|
const fetchFinancialSummaryByProject = useCallback(async (endDate: string, startDate: string) => { |
|
|
|
setIsLoading(true) |
|
|
|
const data = await fetchFinancialSummaryByProjectV2(_teamId, endDate, startDate) |
|
|
@@ -126,7 +157,7 @@ const FinancialSummaryPage: React.FC<Props> = ({ |
|
|
|
setByProject(data) |
|
|
|
setByClient(sumUpByClient(data)) |
|
|
|
setIsLoading(false) |
|
|
|
}, [setMainData, setByTeam, setByProject, setByClient]) |
|
|
|
}, [setMainData, setByTeam, setByProject, setByClient, isFuture]) |
|
|
|
|
|
|
|
const handleCardClick = useCallback((teamId: number) => { |
|
|
|
setIsCardClickedIndex(teamId) |
|
|
@@ -134,14 +165,25 @@ const FinancialSummaryPage: React.FC<Props> = ({ |
|
|
|
}, []); |
|
|
|
|
|
|
|
const handleFilter = useCallback(async (value: number) => { |
|
|
|
setPeriod(value) |
|
|
|
console.log(value) |
|
|
|
var _startDate = dateMap[value as keyof DateParams].startDate |
|
|
|
var _endDate = dateMap[value as keyof DateParams].endDate |
|
|
|
console.log(_startDate) |
|
|
|
console.log(_endDate) |
|
|
|
if (isFuture){ |
|
|
|
setPeriod(value) |
|
|
|
console.log(value) |
|
|
|
var _startDate = dateMap[value as keyof DateParams].startDate |
|
|
|
var _endDate = dateMap[value as keyof DateParams].endDate |
|
|
|
console.log(_startDate) |
|
|
|
console.log(_endDate) |
|
|
|
}else{ |
|
|
|
setPeriod(value) |
|
|
|
console.log(value) |
|
|
|
var _startDate = futureDateMap[value as keyof DateParams].startDate |
|
|
|
var _endDate = futureDateMap[value as keyof DateParams].endDate |
|
|
|
console.log(_startDate) |
|
|
|
console.log(_endDate) |
|
|
|
} |
|
|
|
|
|
|
|
await fetchFinancialSummaryByProject(_endDate, _startDate) |
|
|
|
}, [isCardClickedIndex]) |
|
|
|
|
|
|
|
}, [isCardClickedIndex, isFuture]) |
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
if (teamId > 0) { |
|
|
@@ -159,8 +201,8 @@ const FinancialSummaryPage: React.FC<Props> = ({ |
|
|
|
<Card sx={{ display: "block" }}> |
|
|
|
{/* <CardHeader className="text-slate-500" title= {t("Filter")}/> */} |
|
|
|
<CardContent component={Stack} spacing={1}> |
|
|
|
<Box sx={{ minWidth: 120 }}> |
|
|
|
<FormControl fullWidth> |
|
|
|
<Box sx={{ minWidth: 120, display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}> |
|
|
|
<FormControl fullWidth sx={{ mr: 2 }}> |
|
|
|
<InputLabel id="demo-simple-select-label">Financial Year</InputLabel> |
|
|
|
<Select |
|
|
|
labelId="demo-simple-select-label" |
|
|
@@ -170,12 +212,31 @@ const FinancialSummaryPage: React.FC<Props> = ({ |
|
|
|
onChange={(e) => handleFilter(Number(e.target.value))} |
|
|
|
> |
|
|
|
{ |
|
|
|
comboList.map((str, i) => { |
|
|
|
return <MenuItem key={i} value={i}>{str}</MenuItem> |
|
|
|
isFuture |
|
|
|
? |
|
|
|
comboList.map((str, i) => ( |
|
|
|
<MenuItem key={i} value={i}>{str}</MenuItem> |
|
|
|
)) |
|
|
|
: |
|
|
|
Object.entries(futureDateMap).map(([year, dates], index) => { |
|
|
|
const isLastItem = index === Object.keys(futureDateMap).length - 1; |
|
|
|
const str = isLastItem ? `> ${dates.startDate.slice(0,4)}` : `${dates.startDate.slice(0,4)} - ${dates.endDate.slice(0,4)}`; |
|
|
|
return <MenuItem key={year} value={year}>{str}</MenuItem>; |
|
|
|
}) |
|
|
|
} |
|
|
|
</Select> |
|
|
|
</FormControl> |
|
|
|
<FormControlLabel |
|
|
|
control={ |
|
|
|
<Switch |
|
|
|
checked={!isFuture} |
|
|
|
onChange={() => setIsFuture(!isFuture)} |
|
|
|
name="toggleSwitch" |
|
|
|
color="primary" |
|
|
|
/> |
|
|
|
} |
|
|
|
label={isFuture ? t("Past Years") : t("Future Years")} |
|
|
|
/> |
|
|
|
</Box> |
|
|
|
</CardContent> |
|
|
|
</Card> |
|
|
@@ -191,7 +252,7 @@ const FinancialSummaryPage: React.FC<Props> = ({ |
|
|
|
TotalActiveProjectNumber={allTeam.activeProject} |
|
|
|
TotalFees={allTeam.totalFee} |
|
|
|
TotalBudget={allTeam.totalBudget} |
|
|
|
TotalCumulative={allTeam.manhourExpense + allTeam.projectExpense} |
|
|
|
TotalCumulative={allTeam.manhourExpense} |
|
|
|
TotalProjectExpense={allTeam.projectExpense} |
|
|
|
TotalInvoicedAmount={allTeam.invoicedAmount} |
|
|
|
TotalUnInvoicedAmount={allTeam.totalFee - allTeam.invoicedAmount} |
|
|
@@ -212,7 +273,7 @@ const FinancialSummaryPage: React.FC<Props> = ({ |
|
|
|
TotalActiveProjectNumber={record.activeProject} |
|
|
|
TotalFees={record.totalFee} |
|
|
|
TotalBudget={record.totalBudget} |
|
|
|
TotalCumulative={record.projectExpense + record.manhourExpense} |
|
|
|
TotalCumulative={record.manhourExpense} |
|
|
|
TotalProjectExpense={record.projectExpense} |
|
|
|
TotalInvoicedAmount={record.invoicedAmount} |
|
|
|
TotalUnInvoicedAmount={Math.abs(record.totalFee - record.invoicedAmount)} |
|
|
|