您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 

127 行
4.5 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 { fetchMonthlyWorkHoursReport, fetchProjectCashFlowReport, fetchProjectResourceOverconsumptionReport } from "@/app/api/reports/actions";
  7. import { downloadFile, readIntFromString } from "@/app/utils/commonUtil";
  8. import { BASE_API_URL } from "@/config/api";
  9. import { ProjectResourceOverconsumptionReportFilter, ProjectResourceOverconsumptionReportRequest } from "@/app/api/reports";
  10. import { StaffResult } from "@/app/api/staff";
  11. import { TeamResult } from "@/app/api/team";
  12. import { Customer } from "@/app/api/customer";
  13. import { Subsidiary } from "@/app/api/subsidiary";
  14. import CostAndExpenseReportLoading from "../CostAndExpenseReport/CostAndExpenseReportLoading";
  15. interface Props {
  16. team: TeamResult[]
  17. customer: Customer[]
  18. subsidiaries: Subsidiary[]
  19. needAll: boolean
  20. }
  21. type combo = {
  22. value: string,
  23. label: string,
  24. group: string
  25. }
  26. type SearchQuery = Partial<Omit<ProjectResourceOverconsumptionReportFilter, "id">>;
  27. type SearchParamNames = keyof SearchQuery;
  28. const ResourceOverconsumptionReport: React.FC<Props> = ({ team, customer, subsidiaries, needAll }) => {
  29. const { t } = useTranslation("report");
  30. const statusCombo = ["Potential Overconsumption"]
  31. const sortedTeam = team.sort((a, b) => a.code.localeCompare(b.code));
  32. const teamCombo = sortedTeam.map(t => `${t.code} - ${t.name}`)
  33. const custCombo: combo[] = customer.map(c => ({
  34. value: `custId-${c.id}`,
  35. label: `${c.code} - ${c.name}`,
  36. group: t("Client")
  37. }))
  38. .sort((a: combo, b: combo) => {
  39. const [_custType_A, id_A] = readIntFromString(a.value) as [string, number]
  40. const [_custType_B, id_B] = readIntFromString(b.value) as [string, number]
  41. return (id_A - id_B)
  42. });
  43. console.log(custCombo)
  44. const subsidiariesCombo: combo[] = subsidiaries.map(sub => ({
  45. value: `subsidiaryId-${sub.id}`,
  46. label: `${sub.code} - ${sub.name}`,
  47. group: t("Subsidiary")
  48. }))
  49. .sort((a: combo, b: combo) => {
  50. const [_custType_A, id_A] = readIntFromString(a.value) as [string, number]
  51. const [_custType_B, id_B] = readIntFromString(b.value) as [string, number]
  52. return (id_A - id_B)
  53. });
  54. console.log(subsidiariesCombo)
  55. const searchCriteria: Criterion<SearchParamNames>[] = useMemo(
  56. () => [
  57. {
  58. label: t("Team"),
  59. paramName: "team",
  60. type: "select",
  61. options: teamCombo,
  62. needAll: needAll
  63. },
  64. {
  65. label: t("Client"),
  66. paramName: "customer",
  67. type: "autocomplete",
  68. options: [...subsidiariesCombo, ...custCombo],
  69. // needAll: true
  70. },
  71. {
  72. label: t("Status"),
  73. paramName: "status",
  74. type: "select",
  75. options: statusCombo,
  76. needAll: true
  77. },
  78. {
  79. label: t("Potential Overconsumption Threshold"),
  80. paramName: "lowerLimit",
  81. type: "number",
  82. },
  83. ],
  84. [t],
  85. );
  86. return (
  87. <>
  88. <SearchBox
  89. formType={"download"}
  90. criteria={searchCriteria}
  91. onSearch={async (query: any) => {
  92. const [custType, id] = readIntFromString(query.customer) as [string, number]
  93. let postData: ProjectResourceOverconsumptionReportRequest = {
  94. status: "All",
  95. lowerLimit: 0.9
  96. }
  97. if (query.team.length > 0 && query.team.toLocaleLowerCase() !== "all") {
  98. const index = teamCombo.findIndex(team => team === query.team)
  99. const teamId = team[index].id
  100. postData.teamId = teamId
  101. }
  102. if (Boolean(query.lowerLimit)) {
  103. postData.lowerLimit = query.lowerLimit/100
  104. }
  105. if (id) {
  106. postData[custType] = id
  107. }
  108. postData.status = query.status
  109. console.log(postData)
  110. const response = await fetchProjectResourceOverconsumptionReport(postData)
  111. if (response) {
  112. downloadFile(new Uint8Array(response.blobValue), response.filename!!)
  113. }
  114. }
  115. }
  116. />
  117. </>
  118. )
  119. }
  120. export default ResourceOverconsumptionReport