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.
 
 

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