You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

63 line
2.6 KiB

  1. "use client";
  2. import React, { useMemo } from "react";
  3. import SearchBox, { Criterion } from "../SearchBox";
  4. import { useTranslation } from "react-i18next";
  5. import { ProjectResult } from "@/app/api/projects";
  6. import { ProjectPandLReportFilter } from "@/app/api/reports";
  7. import { fetchProjectPandLReport } from "@/app/api/reports/actions";
  8. import { downloadFile } from "@/app/utils/commonUtil";
  9. import { dateTypeCombo } from "@/app/utils/comboUtil";
  10. import { FormHelperText } from "@mui/material";
  11. import { errorDialog, errorDialogWithContent } from "../Swal/CustomAlerts";
  12. interface Props {
  13. projects: ProjectResult[];
  14. }
  15. type SearchQuery = Partial<Omit<ProjectPandLReportFilter, "id">>;
  16. type SearchParamNames = keyof SearchQuery;
  17. const GenerateProjectPandLReport: React.FC<Props> = ({ projects }) => {
  18. const { t } = useTranslation("report");
  19. const projectCombo = projects.map(project => `${project.code} - ${project.name}`)
  20. const searchCriteria: Criterion<SearchParamNames>[] = useMemo(
  21. () => [
  22. { label: t("Project *"), paramName: "project", type: "select", options: projectCombo, needAll: false},
  23. { label: t("Start Month *"), label2: t("End Month *"), paramName: "startMonth", type: "dateRange", needMonth: true },
  24. ],
  25. [t],
  26. );
  27. return (
  28. <>
  29. <SearchBox
  30. criteria={searchCriteria}
  31. onSearch={async (query) => {
  32. if (query.project.length > 0 && query.project.toLocaleLowerCase() !== "all") {
  33. const projectIndex = projectCombo.findIndex(project => project === query.project)
  34. console.log(projects[projectIndex].id, query.startMonth, query.startMonthTo)
  35. if(projects[projectIndex].id != null && query.startMonth != "" && query.startMonthTo != undefined){
  36. const response = await fetchProjectPandLReport({ projectId: projects[projectIndex].id, startMonth: query.startMonth, endMonth: query.startMonthTo })
  37. if (response) {
  38. downloadFile(new Uint8Array(response.blobValue), response.filename!!)
  39. }
  40. }else{
  41. errorDialogWithContent(t("Download Fail"),
  42. t(`Please check the required field`), t)
  43. .then(() => {
  44. window.location.reload()
  45. })
  46. }
  47. }
  48. }}
  49. formType={"download"}
  50. />
  51. </>
  52. );
  53. };
  54. export default GenerateProjectPandLReport;