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.
 
 
 

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