|
|
@@ -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<Omit<CostAndExpenseReportFilter, "id">>; |
|
|
|
type SearchParamNames = keyof SearchQuery; |
|
|
|
|
|
|
|
const CostAndExpenseReport: React.FC<Props> = ({ team, customer, needAll }) => { |
|
|
|
const CostAndExpenseReport: React.FC<Props> = ({ 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<SearchParamNames>[] = useMemo( |
|
|
|
() => [ |
|
|
@@ -35,11 +50,11 @@ const CostAndExpenseReport: React.FC<Props> = ({ 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<Props> = ({ 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 |
|
|
|