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.
 
 

96 rivejä
2.6 KiB

  1. "use client";
  2. import {
  3. ProjectCompletionReportFilter,
  4. ProjectCompletionReportRequest,
  5. } from "@/app/api/reports";
  6. import React, { useMemo, useState } from "react";
  7. import { useTranslation } from "react-i18next";
  8. import SearchBox, { Criterion } from "../SearchBox";
  9. import dayjs from "dayjs";
  10. import { INPUT_DATE_FORMAT } from "@/app/utils/formatUtil";
  11. import { downloadFile } from "@/app/utils/commonUtil";
  12. import { fetchProjectCompletionReport } from "@/app/api/reports/actions";
  13. interface Props {
  14. // team: TeamResult[]
  15. // customer: Customer[]
  16. }
  17. type SearchQuery = Partial<Omit<ProjectCompletionReportFilter, "id">>;
  18. type SearchParamNames = keyof SearchQuery;
  19. const ProjectCompletionReport: React.FC<Props> = (
  20. {
  21. // team,
  22. // customer
  23. }
  24. ) => {
  25. const { t } = useTranslation("report");
  26. const [error, setError] = useState("");
  27. const outstandingList = ["Regular", "Outstanding Accounts Receivable"]
  28. const searchCriteria: Criterion<SearchParamNames>[] = useMemo(
  29. () => [
  30. {
  31. label: t("startDate"),
  32. label2: t("endDate"),
  33. paramName: "startDate",
  34. type: "dateRange",
  35. },
  36. {
  37. label: t("Type"),
  38. paramName: "outstanding",
  39. type: "select",
  40. needAll: false,
  41. options: outstandingList
  42. },
  43. ],
  44. [t]
  45. );
  46. return (
  47. <>
  48. <SearchBox
  49. criteria={searchCriteria}
  50. onSearch={async (query: any) => {
  51. console.log(query);
  52. let postData: ProjectCompletionReportRequest = {
  53. startDate: "",
  54. endDate: dayjs().format(INPUT_DATE_FORMAT).toString(),
  55. outstanding: false
  56. };
  57. if (query.endDate && query.endDate.length > 0) {
  58. postData.endDate = query.endDate;
  59. }
  60. // check if start date exist
  61. if (query.startDate.length === 0) {
  62. setError(t("Start Date cant be empty"));
  63. } else {
  64. postData.startDate = query.startDate;
  65. if (query.outstanding && query.outstanding === "Outstanding Accounts Receivable") {
  66. // outstanding report
  67. postData.outstanding = true
  68. }
  69. console.log(postData)
  70. const response =
  71. await fetchProjectCompletionReport(
  72. postData
  73. );
  74. // normal report
  75. if (response) {
  76. downloadFile(
  77. new Uint8Array(response.blobValue),
  78. response.filename!!
  79. );
  80. }
  81. }
  82. }}
  83. />
  84. </>
  85. );
  86. };
  87. export default ProjectCompletionReport;