FPSMS-frontend
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

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