Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 

84 rindas
2.6 KiB

  1. "use client";
  2. import { CostAndExpenseReportFilter, CostAndExpenseReportRequest } from "@/app/api/reports";
  3. import { useTranslation } from "react-i18next";
  4. import SearchBox, { Criterion } from "../SearchBox";
  5. import { useMemo } from "react";
  6. import { TeamResult } from "@/app/api/team";
  7. import { Customer } from "@/app/api/customer";
  8. import { fetchCostAndExpenseReport } from "@/app/api/reports/actions";
  9. import { downloadFile } from "@/app/utils/commonUtil";
  10. interface Props {
  11. team: TeamResult[];
  12. customer: Customer[];
  13. }
  14. type SearchQuery = Partial<Omit<CostAndExpenseReportFilter, "id">>;
  15. type SearchParamNames = keyof SearchQuery;
  16. const CostAndExpenseReport: React.FC<Props> = ({ team, customer }) => {
  17. const { t } = useTranslation("report");
  18. const teamCombo = team.map((t) => `${t.name} - ${t.code}`);
  19. const custCombo = customer.map(c => `${c.name} - ${c.code}`)
  20. const searchCriteria: Criterion<SearchParamNames>[] = useMemo(
  21. () => [
  22. {
  23. label: t("Team"),
  24. paramName: "team",
  25. type: "select",
  26. options: teamCombo,
  27. needAll: true,
  28. },
  29. {
  30. label: t("Client"),
  31. paramName: "customer",
  32. type: "select",
  33. options: custCombo,
  34. needAll: true,
  35. },
  36. {
  37. label: t("Remaining Percentage"),
  38. paramName: "budgetPercentage",
  39. type: "number",
  40. },
  41. ],
  42. [t]
  43. );
  44. return (
  45. <>
  46. <SearchBox
  47. criteria={searchCriteria}
  48. onSearch={async (query: any) => {
  49. let index = 0
  50. let postData: CostAndExpenseReportRequest = {
  51. teamId: null,
  52. clientId: null,
  53. budgetPercentage: 0.5
  54. }
  55. console.log(query.budgetPercentage)
  56. if (query.team.length > 0 && query.team.toLocaleLowerCase() !== "all") {
  57. index = teamCombo.findIndex(team => team === query.team)
  58. postData.teamId = team[index].id
  59. }
  60. if (query.customer.length > 0 && query.customer.toLocaleLowerCase() !== "all") {
  61. index = custCombo.findIndex(customer => customer === query.customer)
  62. postData.clientId = customer[index].id
  63. }
  64. if (Boolean(query.budgetPercentage)) {
  65. postData.budgetPercentage = query.budgetPercentage/100
  66. }
  67. console.log(postData)
  68. const response = await fetchCostAndExpenseReport(postData)
  69. if (response) {
  70. downloadFile(new Uint8Array(response.blobValue), response.filename!!)
  71. }
  72. }}
  73. />
  74. </>
  75. );
  76. };
  77. export default CostAndExpenseReport;