FPSMS-frontend
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

212 linhas
6.7 KiB

  1. "use client";
  2. import { InventoryLotLineResult, InventoryResult } from "@/app/api/inventory";
  3. import { useTranslation } from "react-i18next";
  4. import SearchBox, { Criterion } from "../SearchBox";
  5. import { useCallback, useEffect, useMemo, useState } from "react";
  6. import { orderBy, uniq, uniqBy } from "lodash";
  7. import SearchResults, { Column } from "../SearchResults";
  8. import { CheckCircleOutline, DoDisturb } from "@mui/icons-material";
  9. import InventoryTable from "./InventoryTable";
  10. import { defaultPagingController } from "../SearchResults/SearchResults";
  11. import InventoryLotLineTable from "./InventoryLotLineTable";
  12. import { SearchInventory, SearchInventoryLotLine, fetchInventories, fetchInventoryLotLines } from "@/app/api/inventory/actions";
  13. interface Props {
  14. inventories: InventoryResult[];
  15. }
  16. type SearchQuery = Partial<
  17. Omit<
  18. InventoryResult,
  19. | "id"
  20. | "qty"
  21. | "uomCode"
  22. | "uomUdfudesc"
  23. | "germPerSmallestUnit"
  24. | "qtyPerSmallestUnit"
  25. | "itemSmallestUnit"
  26. | "price"
  27. | "description"
  28. | "category"
  29. >
  30. >;
  31. type SearchParamNames = keyof SearchQuery;
  32. const InventorySearch: React.FC<Props> = ({ inventories }) => {
  33. const { t } = useTranslation(["inventory", "common"]);
  34. // Inventory
  35. const [filteredInventories, setFilteredInventories] = useState<InventoryResult[]>([]);
  36. const [inventoriesPagingController, setInventoriesPagingController] = useState(defaultPagingController)
  37. const [inventoriesTotalCount, setInventoriesTotalCount] = useState(0)
  38. const [item, setItem] = useState<InventoryResult | null>(null)
  39. // Inventory Lot Line
  40. const [filteredInventoryLotLines, setFilteredInventoryLotLines] = useState<InventoryLotLineResult[]>([]);
  41. const [inventoryLotLinesPagingController, setInventoryLotLinesPagingController] = useState(defaultPagingController)
  42. const [inventoryLotLinesTotalCount, setInventoryLotLinesTotalCount] = useState(0)
  43. const [inputs, setInputs] = useState<Record<SearchParamNames, string>>({
  44. itemId: "",
  45. itemCode: "",
  46. itemName: "",
  47. itemType: "",
  48. onHandQty: "",
  49. onHoldQty: "",
  50. unavailableQty: "",
  51. availableQty: "",
  52. currencyName: "",
  53. status: "",
  54. baseUom: "",
  55. });
  56. const searchCriteria: Criterion<SearchParamNames>[] = useMemo(
  57. () => [
  58. { label: t("Code"), paramName: "itemCode", type: "text" },
  59. { label: t("Name"), paramName: "itemName", type: "text" },
  60. {
  61. label: t("Type"),
  62. paramName: "itemType",
  63. type: "select",
  64. options: uniq(inventories.map((i) => i.itemType)),
  65. },
  66. // {
  67. // label: t("Status"),
  68. // paramName: "status",
  69. // type: "select",
  70. // options: uniq(inventories.map((i) => i.status)),
  71. // },
  72. ],
  73. [t],
  74. );
  75. const refetchInventoryData = useCallback(async (
  76. query: Record<SearchParamNames, string>,
  77. actionType: "reset" | "search" | "paging"
  78. ) => {
  79. const params: SearchInventory = {
  80. code: query?.itemCode ?? '',
  81. name: query?.itemName ?? '',
  82. type: query?.itemType.toLowerCase() === "all" ? '' : query?.itemType ?? '',
  83. pageNum: inventoriesPagingController.pageNum - 1,
  84. pageSize: inventoriesPagingController.pageSize
  85. }
  86. const response = await fetchInventories(params)
  87. if (response) {
  88. console.log(response)
  89. setInventoriesTotalCount(response.total);
  90. switch (actionType) {
  91. case "reset":
  92. case "search":
  93. setFilteredInventories(() => response.records);
  94. break;
  95. case "paging":
  96. setFilteredInventories((fi) =>
  97. // orderBy(
  98. uniqBy([...fi, ...response.records], "id")
  99. // , ["id"], ["desc"])
  100. );
  101. }
  102. }
  103. }, [inventoriesPagingController, setInventoriesPagingController])
  104. useEffect(() => {
  105. refetchInventoryData(inputs, "paging")
  106. }, [inventoriesPagingController])
  107. const refetchInventoryLotLineData = useCallback(async (
  108. itemId: number | null,
  109. actionType: "reset" | "search" | "paging"
  110. ) => {
  111. if (!itemId) {
  112. setItem(null)
  113. setInventoryLotLinesTotalCount(0);
  114. setFilteredInventoryLotLines([])
  115. return
  116. }
  117. const params: SearchInventoryLotLine = {
  118. itemId: itemId,
  119. pageNum: inventoriesPagingController.pageNum - 1,
  120. pageSize: inventoriesPagingController.pageSize
  121. }
  122. const response = await fetchInventoryLotLines(params)
  123. if (response) {
  124. setInventoryLotLinesTotalCount(response.total);
  125. switch (actionType) {
  126. case "reset":
  127. case "search":
  128. setFilteredInventoryLotLines(() => response.records);
  129. break;
  130. case "paging":
  131. setFilteredInventoryLotLines((fi) =>
  132. orderBy(
  133. uniqBy([...fi, ...response.records], "id"),
  134. ["id"], ["desc"]
  135. ));
  136. }
  137. }
  138. }, [inventoriesPagingController, setInventoriesPagingController])
  139. useEffect(() => {
  140. refetchInventoryData(inputs, "paging")
  141. }, [inventoriesPagingController])
  142. const onReset = useCallback(() => {
  143. refetchInventoryData(inputs, "reset");
  144. refetchInventoryLotLineData(null, "reset");
  145. // setFilteredInventories(inventories);
  146. }, []);
  147. const onInventoryRowClick = useCallback((item: InventoryResult) => {
  148. setItem(item)
  149. refetchInventoryLotLineData(item.itemId, "search")
  150. }, [])
  151. return (
  152. <>
  153. <SearchBox
  154. criteria={searchCriteria}
  155. onSearch={(query) => {
  156. // console.log(query)
  157. // console.log(inventories)
  158. setInputs(() => query)
  159. refetchInventoryData(query, "search")
  160. // setFilteredInventories(
  161. // inventories.filter(
  162. // (i) =>
  163. // i.itemCode.toLowerCase().includes(query.itemCode.toLowerCase()) &&
  164. // i.itemName.toLowerCase().includes(query.itemName.toLowerCase()) &&
  165. // (query.itemType == "All" ||
  166. // i.itemType.toLowerCase().includes(query.itemType.toLowerCase())) &&
  167. // (query.status == "All" ||
  168. // i.status.toLowerCase().includes(query.status.toLowerCase())),
  169. // ),
  170. // );
  171. }}
  172. onReset={onReset}
  173. />
  174. <InventoryTable
  175. inventories={filteredInventories}
  176. pagingController={inventoriesPagingController}
  177. setPagingController={setInventoriesPagingController}
  178. totalCount={inventoriesTotalCount}
  179. onRowClick={onInventoryRowClick}
  180. />
  181. <InventoryLotLineTable
  182. inventoryLotLines={filteredInventoryLotLines}
  183. pagingController={inventoryLotLinesPagingController}
  184. setPagingController={setInventoryLotLinesPagingController}
  185. totalCount={inventoryLotLinesTotalCount}
  186. item={item}
  187. />
  188. </>
  189. );
  190. };
  191. export default InventorySearch;