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.

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