FPSMS-frontend
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 

245 rader
6.1 KiB

  1. "use server";
  2. // import { BASE_API_URL } from "@/config/api";
  3. import { BASE_API_URL } from "../../../config/api";
  4. // import { ServerFetchError, serverFetchJson, serverFetchWithNoContent } from "@/app/utils/fetchUtil";
  5. import { revalidateTag } from "next/cache";
  6. import { cache } from "react";
  7. import { PoResult, StockInLine } from ".";
  8. //import { serverFetchJson } from "@/app/utils/fetchUtil";
  9. import { serverFetchJson, serverFetchWithNoContent } from "../../utils/fetchUtil";
  10. import { QcItemResult } from "../settings/qcItem";
  11. import { RecordsRes } from "../utils";
  12. import { Uom } from "../settings/uom";
  13. import { convertObjToURLSearchParams } from "@/app/utils/commonUtil";
  14. // import { BASE_API_URL } from "@/config/api";
  15. export interface PostStockInLineResponse<T> {
  16. id: number | null;
  17. name: string;
  18. code: string;
  19. type?: string;
  20. message: string | null;
  21. errorPosition: string | keyof T;
  22. entity: T | T[];
  23. // entity: StockInLine | StockInLine[]
  24. }
  25. export interface StockInLineEntry {
  26. id?: number;
  27. itemId: number;
  28. acceptedQty: number;
  29. purchaseOrderId?: number;
  30. purchaseOrderLineId?: number;
  31. jobOrderId?: number;
  32. status?: string;
  33. expiryDate?: string;
  34. productLotNo?: string;
  35. receiptDate?: string;
  36. dnDate?: string;
  37. dnNo?: string;
  38. }
  39. export interface QcResult{
  40. id?: number;
  41. qcItemId: number;
  42. qcPassed?: boolean;
  43. failQty?: number;
  44. remarks?: string;
  45. escalationLogId?: number;
  46. }
  47. export interface StockInInput {
  48. status: string;
  49. poCode: string;
  50. productLotNo?: string;
  51. dnNo?: string;
  52. dnDate?: string;
  53. itemName: string;
  54. lotNo?: string;
  55. invoiceNo?: string;
  56. receiptDate: string;
  57. supplier: string;
  58. acceptedQty: number;
  59. qty: number;
  60. receivedQty: number;
  61. acceptedWeight?: number;
  62. productionDate?: string;
  63. expiryDate: string;
  64. uom: Uom;
  65. }
  66. export interface QCInput {
  67. status: string;
  68. acceptQty: number;
  69. passingQty: number;
  70. sampleRate?: number;
  71. sampleWeight?: number;
  72. totalWeight?: number;
  73. qcAccept: boolean;
  74. qcDecision?: number;
  75. qcResult: QcResult[];
  76. }
  77. export interface EscalationInput {
  78. status: string;
  79. remarks?: string;
  80. reason?: string;
  81. handlerId: number;
  82. productLotNo?: string;
  83. acceptedQty?: number; // this is the qty to be escalated
  84. // escalationQty: number
  85. }
  86. export interface PutAwayLine {
  87. id?: number
  88. qty: number
  89. warehouseId: number;
  90. warehouse: string;
  91. printQty: number;
  92. _isNew?: boolean;
  93. }
  94. export interface PutAwayInput {
  95. status: string;
  96. acceptedQty: number;
  97. warehouseId: number;
  98. putAwayLines: PutAwayLine[]
  99. }
  100. export type ModalFormInput = Partial<
  101. QCInput & StockInInput & PutAwayInput
  102. > & {
  103. escalationLog? : Partial<EscalationInput>
  104. };
  105. export interface PrintQrCodeForSilRequest {
  106. stockInLineId: number;
  107. printerId: number;
  108. printQty?: number;
  109. }
  110. export const testFetch = cache(async (id: number) => {
  111. return serverFetchJson<PoResult>(`${BASE_API_URL}/po/detail/${id}`, {
  112. next: { tags: ["po"] },
  113. });
  114. });
  115. export const fetchStockInLineInfo = cache(async (stockInLineId: number) => {
  116. return serverFetchJson<StockInLine>(
  117. `${BASE_API_URL}/stockInLine/${stockInLineId}`,
  118. {
  119. next: { tags: ["stockInLine"] },
  120. },
  121. );
  122. });
  123. export const createStockInLine = async (data: StockInLineEntry) => {
  124. const stockInLine = await serverFetchJson<
  125. PostStockInLineResponse<StockInLine>
  126. >(`${BASE_API_URL}/stockInLine/create`, {
  127. method: "POST",
  128. body: JSON.stringify(data),
  129. headers: { "Content-Type": "application/json" },
  130. });
  131. // revalidateTag("po");
  132. return stockInLine;
  133. };
  134. export const updateStockInLine = async (
  135. data: StockInLineEntry & ModalFormInput,
  136. ) => {
  137. const stockInLine = await serverFetchJson<
  138. PostStockInLineResponse<StockInLine & ModalFormInput>
  139. >(`${BASE_API_URL}/stockInLine/update`, {
  140. method: "POST",
  141. body: JSON.stringify(data),
  142. headers: { "Content-Type": "application/json" },
  143. });
  144. // revalidateTag("po");
  145. return stockInLine;
  146. };
  147. export const startPo = async (poId: number) => {
  148. const po = await serverFetchJson<PostStockInLineResponse<PoResult>>(
  149. `${BASE_API_URL}/po/start/${poId}`,
  150. {
  151. method: "POST",
  152. body: JSON.stringify({ poId }),
  153. headers: { "Content-Type": "application/json" },
  154. },
  155. );
  156. revalidateTag("po");
  157. return po;
  158. };
  159. export const checkPolAndCompletePo = async (poId: number) => {
  160. const po = await serverFetchJson<PostStockInLineResponse<PoResult>>(
  161. `${BASE_API_URL}/po/check/${poId}`,
  162. {
  163. method: "POST",
  164. body: JSON.stringify({ poId }),
  165. headers: { "Content-Type": "application/json" },
  166. },
  167. );
  168. revalidateTag("po");
  169. return po;
  170. };
  171. export const fetchPoInClient = cache(async (id: number) => {
  172. return serverFetchJson<PoResult>(`${BASE_API_URL}/po/detail/${id}`, {
  173. next: { tags: ["po"] },
  174. });
  175. });
  176. export const fetchPoListClient = cache(
  177. async (queryParams?: Record<string, any>) => {
  178. if (queryParams) {
  179. const queryString = new URLSearchParams(queryParams).toString();
  180. return serverFetchJson<RecordsRes<PoResult[]>>(
  181. `${BASE_API_URL}/po/list?${queryString}`,
  182. {
  183. method: "GET",
  184. next: { tags: ["po"] },
  185. },
  186. );
  187. } else {
  188. return serverFetchJson<RecordsRes<PoResult[]>>(
  189. `${BASE_API_URL}/po/list`,
  190. {
  191. method: "GET",
  192. next: { tags: ["po"] },
  193. },
  194. );
  195. }
  196. },
  197. );
  198. export const testing = cache(async (queryParams?: Record<string, any>) => {
  199. if (queryParams) {
  200. const queryString = new URLSearchParams(queryParams).toString();
  201. return serverFetchJson<RecordsRes<PoResult[]>>(
  202. `${BASE_API_URL}/po/testing?${queryString}`,
  203. {
  204. method: "GET",
  205. next: { tags: ["po"] },
  206. },
  207. );
  208. } else {
  209. return serverFetchJson<RecordsRes<PoResult[]>>(
  210. `${BASE_API_URL}/po/testing`,
  211. {
  212. method: "GET",
  213. next: { tags: ["po"] },
  214. },
  215. );
  216. }
  217. });
  218. export const printQrCodeForSil = cache(async(data: PrintQrCodeForSilRequest) => {
  219. const params = convertObjToURLSearchParams(data)
  220. return serverFetchWithNoContent(`${BASE_API_URL}/stockInLine/printQrCode?${params}`,
  221. {
  222. method: "GET",
  223. headers: { "Content-Type": "application/json" },
  224. next: {
  225. tags: ["printQrCodeForSil"],
  226. },
  227. },
  228. )
  229. })