diff --git a/backend/src/components/sap/environmentCodeReport.ts b/backend/src/components/sap/environmentCodeReport.ts index 7904e564..d2ce86f8 100644 --- a/backend/src/components/sap/environmentCodeReport.ts +++ b/backend/src/components/sap/environmentCodeReport.ts @@ -5,6 +5,7 @@ import { getPool, sql, textToTsQuery } from '@backend/db'; import { EnvironmentCodeReportQuery, environmentCodeReportSchema } from '@shared/schema/sapReport'; function environmentCodeReportFragment(params?: Partial) { + const years = params?.filters?.years ?? []; return sql.fragment` WITH total_actuals AS ( SELECT @@ -14,9 +15,9 @@ function environmentCodeReportFragment(params?: Partial 0 ? sql.fragment` - WHERE fiscal_year = ${params.filters.year} + WHERE fiscal_year = ANY(${sql.array(years, 'int4')}) ` : sql.fragment`` } diff --git a/frontend/src/components/forms/MultiSelect.tsx b/frontend/src/components/forms/MultiSelect.tsx index 3232daaf..397dc765 100644 --- a/frontend/src/components/forms/MultiSelect.tsx +++ b/frontend/src/components/forms/MultiSelect.tsx @@ -49,7 +49,9 @@ export function MultiSelect({ const autocompleteRef = useRef(); function getOption(id: string | null) { - return options.find((option) => getOptionId?.(option) === id) ?? (id as T); + const option = options.find((option) => getOptionId?.(option) === id); + // If an option wasn't found, return the ID as numeric or string + return option ?? ((isNaN(Number(id)) ? id : Number(id)) as T); } function getLabel(optionId: string | null) { diff --git a/frontend/src/stores/sapReport/environmentalCodeReportFilters.ts b/frontend/src/stores/sapReport/environmentalCodeReportFilters.ts index b24734e4..2ea496d7 100644 --- a/frontend/src/stores/sapReport/environmentalCodeReportFilters.ts +++ b/frontend/src/stores/sapReport/environmentalCodeReportFilters.ts @@ -9,7 +9,7 @@ export const environmentalCodeReportFilterAtom = atom o.prop('text')); @@ -18,7 +18,7 @@ export const reasonsForEnvironmentalInvestmentAtom = focusAtom( environmentalCodeReportFilterAtom, (o) => o.prop('reasonsForEnvironmentalInvestment') ); -export const yearAtom = focusAtom(environmentalCodeReportFilterAtom, (o) => o.prop('year')); +export const yearsAtom = focusAtom(environmentalCodeReportFilterAtom, (o) => o.prop('years')); export function useDebouncedEnvironmentalCodeReportFilters() { const filters = useAtomValue(environmentalCodeReportFilterAtom); diff --git a/frontend/src/views/SapReports/EnvironmentalCodeReportFilters.tsx b/frontend/src/views/SapReports/EnvironmentalCodeReportFilters.tsx index 4a47f9c4..1ee67a59 100644 --- a/frontend/src/views/SapReports/EnvironmentalCodeReportFilters.tsx +++ b/frontend/src/views/SapReports/EnvironmentalCodeReportFilters.tsx @@ -22,7 +22,7 @@ import { reasonsForEnvironmentalInvestmentAtom, textAtom, useDebouncedEnvironmentalCodeReportFilters, - yearAtom, + yearsAtom, } from '@frontend/stores/sapReport/environmentalCodeReportFilters'; import { ReportSummary } from './ReportSummary'; @@ -36,7 +36,7 @@ export function EnvironmentalCodeReportFilters() { const [reasonsForEnvironmentalInvestment, setReasonsForEnvironmentalInvestment] = useAtom( reasonsForEnvironmentalInvestmentAtom ); - const [year, setYear] = useAtom(yearAtom); + const [years, setYears] = useAtom(yearsAtom); const filters = useAtomValue(environmentalCodeReportFilterAtom); const { data: allPlants, isLoading: allPlantsLoading } = trpc.sapReport.getPlants.useQuery(); @@ -111,12 +111,14 @@ export function EnvironmentalCodeReportFilters() { {tr('sapReports.environmentCodes.year')} setYear(year == null ? null : Number(year))} - multiple={false} + value={years} + onChange={(years) => { + setYears(years); + }} + multiple /> diff --git a/shared/src/schema/sapReport.ts b/shared/src/schema/sapReport.ts index cded9866..918309ba 100644 --- a/shared/src/schema/sapReport.ts +++ b/shared/src/schema/sapReport.ts @@ -19,7 +19,7 @@ export const environmentCodeReportFilterSchema = z.object({ text: z.string().nullable(), plants: z.array(z.string()), reasonsForEnvironmentalInvestment: z.array(z.string()), - year: z.number().nullable(), + years: z.array(z.number()), }), });