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.
 
 
 

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