From ea1b4e99dac410732731ec4b77b9af8c3ffc9a87 Mon Sep 17 00:00:00 2001 From: "MSI\\2Fi" Date: Wed, 5 Jun 2024 16:16:25 +0800 Subject: [PATCH] Add subsidary to cost and expense report --- src/app/api/reports/index.ts | 3 +- .../CostAndExpenseReport.tsx | 32 +++++++++++++++---- .../CostAndExpenseReportWrapper.tsx | 4 ++- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/app/api/reports/index.ts b/src/app/api/reports/index.ts index 29f1797..2356a2c 100644 --- a/src/app/api/reports/index.ts +++ b/src/app/api/reports/index.ts @@ -112,5 +112,6 @@ export interface CostAndExpenseReportFilter { export interface CostAndExpenseReportRequest { teamId: number | null; clientId: number | null; - budgetPercentage: number; + budgetPercentage: number | null; + type: string; } diff --git a/src/components/CostAndExpenseReport/CostAndExpenseReport.tsx b/src/components/CostAndExpenseReport/CostAndExpenseReport.tsx index 1b837ee..be06e91 100644 --- a/src/components/CostAndExpenseReport/CostAndExpenseReport.tsx +++ b/src/components/CostAndExpenseReport/CostAndExpenseReport.tsx @@ -5,22 +5,37 @@ import SearchBox, { Criterion } from "../SearchBox"; import { useMemo } from "react"; import { TeamResult } from "@/app/api/team"; import { Customer } from "@/app/api/customer"; +import { Subsidiary } from "@/app/api/subsidiary"; import { fetchCostAndExpenseReport } from "@/app/api/reports/actions"; import { downloadFile } from "@/app/utils/commonUtil"; interface Props { team: TeamResult[]; customer: Customer[]; + subsidiary: Subsidiary[]; needAll: boolean | undefined; } type SearchQuery = Partial>; type SearchParamNames = keyof SearchQuery; -const CostAndExpenseReport: React.FC = ({ team, customer, needAll }) => { +const CostAndExpenseReport: React.FC = ({ team, customer, subsidiary, needAll }) => { const { t } = useTranslation("report"); const teamCombo = team.map((t) => `${t.name} - ${t.code}`); - const custCombo = customer.map(c => ({label: `${c.name} - ${c.code}`, value: c.id})) + // const custCombo = customer.map(c => ({label: `${c.name} - ${c.code}`, value: c.id})) + + const custCombo = customer.map(customer => ({ + value: `client: ${customer.id}` , + label: `${customer.code} - ${customer.name}`, + group: t("Client") +})) + + const subsidiaryCombo = subsidiary.map(subsidiary => ({ + value: `subsidiary: ${subsidiary.id}`, + label: `${subsidiary.code} - ${subsidiary.name}`, + group: t("Subsidiary") + })) + const searchCriteria: Criterion[] = useMemo( () => [ @@ -35,11 +50,11 @@ const CostAndExpenseReport: React.FC = ({ team, customer, needAll }) => { label: t("Client"), paramName: "customer", type: "autocomplete", - options: custCombo, + options: [...subsidiaryCombo, ...custCombo], needAll: true }, { - label: t("Remaining Percentage"), + label: t("Remaining Percentage (<= %)"), paramName: "budgetPercentage", type: "number", }, @@ -57,15 +72,20 @@ const CostAndExpenseReport: React.FC = ({ team, customer, needAll }) => { let postData: CostAndExpenseReportRequest = { teamId: null, clientId: null, - budgetPercentage: 0.5 + budgetPercentage: null, + type: "all" } console.log(query.budgetPercentage) + const customerIndex = custCombo.findIndex(customer => customer.value === query.customer) + const subsidiaryIndex = subsidiaryCombo.findIndex(subsidiary => subsidiary.value === query.customer) if (query.team.length > 0 && query.team.toLocaleLowerCase() !== "all") { index = teamCombo.findIndex(team => team === query.team) postData.teamId = team[index].id } if (typeof query.customer === "string" && query.customer.toLocaleLowerCase() !== "all") { - postData.clientId = query.customer + // postData.clientId = query.customer + postData.clientId = customerIndex >= 0 ? customer[customerIndex].id : subsidiaryIndex >= 0 ? subsidiary[subsidiaryIndex].id : null + postData.type = customerIndex >= 0 ? "client" : subsidiaryIndex >= 0 ? "subsidiary" : "all" } if (Boolean(query.budgetPercentage)) { postData.budgetPercentage = query.budgetPercentage/100 diff --git a/src/components/CostAndExpenseReport/CostAndExpenseReportWrapper.tsx b/src/components/CostAndExpenseReport/CostAndExpenseReportWrapper.tsx index 3c2a5d9..d373800 100644 --- a/src/components/CostAndExpenseReport/CostAndExpenseReportWrapper.tsx +++ b/src/components/CostAndExpenseReport/CostAndExpenseReportWrapper.tsx @@ -1,5 +1,6 @@ import React from "react"; import { fetchAllCustomers } from "@/app/api/customer"; +import { fetchAllSubsidiaries } from "@/app/api/subsidiary"; import { fetchIndivTeam, fetchTeam } from "@/app/api/team"; import CostAndExpenseReport from "./CostAndExpenseReport"; import CostAndExpenseReportLoading from "./CostAndExpenseReportLoading"; @@ -16,6 +17,7 @@ const CostAndExpenseReportWrapper: React.FC & SubComponents = async () => { const teamId = session.staff?.team.id const role = session.role let customers = await fetchAllCustomers() + let subsidiaries = await fetchAllSubsidiaries() let teams = await fetchTeam() let needAll = true @@ -24,7 +26,7 @@ const CostAndExpenseReportWrapper: React.FC & SubComponents = async () => { teams = teams.filter((team) => team.id === teamId); } - return + return }; CostAndExpenseReportWrapper.Loading = CostAndExpenseReportLoading;