FPSMS-frontend
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.
 
 

185 lines
5.0 KiB

  1. "use server";
  2. import { BASE_API_URL } from "@/config/api";
  3. // import { ServerFetchError, serverFetchJson, serverFetchWithNoContent } from "@/app/utils/fetchUtil";
  4. import { revalidateTag } from "next/cache";
  5. import { cache } from "react";
  6. import { Pageable, serverFetchJson } from "@/app/utils/fetchUtil";
  7. import { QcItemResult } from "../settings/qcItem";
  8. import { RecordsRes } from "../utils";
  9. import { convertObjToURLSearchParams } from "@/app/utils/commonUtil";
  10. import { InventoryLotLineResult, InventoryResult } from ".";
  11. // import { BASE_API_URL } from "@/config/api";
  12. export interface LotLineInfo {
  13. inventoryLotLineId: number;
  14. itemId: number;
  15. itemNo: string;
  16. itemName: string;
  17. lotNo: string;
  18. remainingQty: number;
  19. uom: string;
  20. }
  21. export interface SearchInventoryLotLine extends Pageable {
  22. itemId: number;
  23. }
  24. export interface SearchInventory extends Pageable {
  25. code: string;
  26. name: string;
  27. type: string;
  28. lotNo?: string;
  29. }
  30. export interface InventoryResultByPage {
  31. total: number;
  32. records: InventoryResult[];
  33. }
  34. export interface UpdateInventoryLotLineStatusRequest {
  35. inventoryLotLineId: number;
  36. status: string;
  37. }
  38. export interface InventoryLotLineResultByPage {
  39. total: number;
  40. records: InventoryLotLineResult[];
  41. }
  42. export interface PostInventoryLotLineResponse<T = null> {
  43. id: number | null;
  44. name: string;
  45. code: string;
  46. type?: string;
  47. message: string | null;
  48. errorPosition: string
  49. entity?: T | T[];
  50. consoCode?: string;
  51. }
  52. export interface QrCodeAnalysisResponse {
  53. itemId: number;
  54. itemCode: string;
  55. itemName: string;
  56. scanned: ScannedLotInfo;
  57. sameItemLots: SameItemLotInfo[];
  58. }
  59. export interface ScannedLotInfo {
  60. stockInLineId: number;
  61. lotNo: string;
  62. inventoryLotLineId: number;
  63. }
  64. export interface SameItemLotInfo {
  65. lotNo: string;
  66. inventoryLotLineId: number;
  67. availableQty: number;
  68. uom: string;
  69. }
  70. export const analyzeQrCode = async (data: {
  71. itemId: number;
  72. stockInLineId: number;
  73. }) => {
  74. return serverFetchJson<QrCodeAnalysisResponse>(`${BASE_API_URL}/inventoryLotLine/analyze-qr-code`, {
  75. method: 'POST',
  76. body: JSON.stringify(data),
  77. headers: { "Content-Type": "application/json" },
  78. });
  79. };
  80. export const updateInventoryStatus = async (data: {
  81. itemId: number;
  82. lotId: number;
  83. status: string;
  84. qty: number;
  85. }) => {
  86. return serverFetchJson(`${BASE_API_URL}/inventory/update-status`, {
  87. method: 'PUT',
  88. body: JSON.stringify(data)
  89. });
  90. };
  91. export const fetchLotDetail = cache(async (stockInLineId: number) => {
  92. return serverFetchJson<LotLineInfo>(
  93. `${BASE_API_URL}/inventoryLotLine/lot-detail/${stockInLineId}`,
  94. {
  95. method: "GET",
  96. next: { tags: ["inventory"] },
  97. },
  98. );
  99. });
  100. export const updateInventoryLotLineStatus = async (data: {
  101. inventoryLotLineId: number;
  102. status: string;
  103. //qty: number;
  104. //operation?: string;
  105. }) => {
  106. // return await serverFetchJson(`${BASE_API_URL}/inventoryLotLine/updateStatus`, {
  107. // next: { tags: ["inventoryLotLine"] },
  108. // method: 'POST',
  109. // body: JSON.stringify(data)
  110. // });
  111. return await serverFetchJson<PostInventoryLotLineResponse<InventoryLotLineResult>>(`${BASE_API_URL}/inventoryLotLine/updateStatus`, {
  112. next: { tags: ["inventoryLotLine"] },
  113. method: 'POST',
  114. body: JSON.stringify(data),
  115. headers: { "Content-Type": "application/json" },
  116. });
  117. // revalidateTag("po");
  118. };
  119. export const fetchInventories = cache(async (data: SearchInventory) => {
  120. const queryStr = convertObjToURLSearchParams(data)
  121. return serverFetchJson<InventoryResultByPage>(`${BASE_API_URL}/inventory/getRecordByPage?${queryStr}`,
  122. { next: { tags: ["inventories"] } }
  123. )
  124. })
  125. export const fetchInventoryLotLines = cache(async (data: SearchInventoryLotLine) => {
  126. const queryStr = convertObjToURLSearchParams(data)
  127. return serverFetchJson<InventoryLotLineResultByPage>(`${BASE_API_URL}/inventoryLotLine/getRecordByPage?${queryStr}`, {
  128. next: { tags: ["inventoryLotLines"] },
  129. });
  130. });
  131. export const updateInventoryLotLineQuantities = async (data: {
  132. inventoryLotLineId: number;
  133. qty: number;
  134. operation: string;
  135. status: string;
  136. }) => {
  137. const result = await serverFetchJson<any>(
  138. `${BASE_API_URL}/inventoryLotLine/updateQuantities`,
  139. {
  140. method: "POST",
  141. body: JSON.stringify(data),
  142. headers: { "Content-Type": "application/json" },
  143. },
  144. );
  145. revalidateTag("pickorder");
  146. return result;
  147. };
  148. //STOCK TRANSFER
  149. export interface CreateStockTransferRequest {
  150. inventoryLotLineId: number;
  151. transferredQty: number;
  152. warehouseId: number;
  153. }
  154. export interface MessageResponse {
  155. id: number | null;
  156. name: string;
  157. code: string;
  158. type: string;
  159. message: string | null;
  160. errorPosition: string | null;
  161. }
  162. export const createStockTransfer = async (data: CreateStockTransferRequest) => {
  163. const result = await serverFetchJson<MessageResponse>(
  164. `${BASE_API_URL}/stockTransferRecord/create`,
  165. {
  166. method: "POST",
  167. body: JSON.stringify(data),
  168. headers: { "Content-Type": "application/json" },
  169. },
  170. );
  171. revalidateTag("inventoryLotLines");
  172. revalidateTag("inventories");
  173. return result;
  174. };