FPSMS-frontend
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 

301 linhas
9.1 KiB

  1. // actions.ts
  2. "use server";
  3. import { cache } from 'react';
  4. import { serverFetchJson } from "@/app/utils/fetchUtil"; // 改为 serverFetchJson
  5. import { BASE_API_URL } from "@/config/api";
  6. export interface InventoryLotDetailResponse {
  7. id: number;
  8. inventoryLotId: number;
  9. itemId: number;
  10. itemCode: string;
  11. itemName: string;
  12. lotNo: string;
  13. expiryDate: string;
  14. productionDate: string;
  15. stockInDate: string;
  16. inQty: number;
  17. outQty: number;
  18. holdQty: number;
  19. availableQty: number;
  20. uom: string;
  21. warehouseCode: string;
  22. warehouseName: string;
  23. warehouseSlot: string;
  24. warehouseArea: string;
  25. warehouse: string;
  26. varianceQty: number | null;
  27. status: string;
  28. remarks: string | null;
  29. stockTakeRecordStatus: string;
  30. stockTakeRecordId: number | null;
  31. firstStockTakeQty: number | null;
  32. secondStockTakeQty: number | null;
  33. firstBadQty: number | null;
  34. secondBadQty: number | null;
  35. approverQty: number | null;
  36. approverBadQty: number | null;
  37. finalQty: number | null;
  38. }
  39. export const getInventoryLotDetailsBySection = async (
  40. stockTakeSection: string,
  41. stockTakeId?: number | null
  42. ) => {
  43. console.log('🌐 [API] getInventoryLotDetailsBySection called with:', {
  44. stockTakeSection,
  45. stockTakeId
  46. });
  47. const encodedSection = encodeURIComponent(stockTakeSection);
  48. let url = `${BASE_API_URL}/stockTakeRecord/inventoryLotDetailsBySection?stockTakeSection=${encodedSection}`;
  49. if (stockTakeId != null && stockTakeId > 0) {
  50. url += `&stockTakeId=${stockTakeId}`;
  51. }
  52. console.log(' [API] Full URL:', url);
  53. const details = await serverFetchJson<InventoryLotDetailResponse[]>(
  54. url,
  55. {
  56. method: "GET",
  57. },
  58. );
  59. console.log('[API] Response received:', details);
  60. return details;
  61. }
  62. export interface SaveStockTakeRecordRequest {
  63. stockTakeRecordId?: number | null;
  64. inventoryLotLineId: number;
  65. qty: number;
  66. badQty: number;
  67. //stockTakerName: string;
  68. remark?: string | null;
  69. }
  70. export interface AllPickedStockTakeListReponse {
  71. id: number;
  72. stockTakeSession: string;
  73. lastStockTakeDate: string | null;
  74. status: string|null;
  75. currentStockTakeItemNumber: number;
  76. totalInventoryLotNumber: number;
  77. stockTakeId: number;
  78. stockTakerName: string | null;
  79. totalItemNumber: number;
  80. }
  81. export const importStockTake = async (data: FormData) => {
  82. const importStockTake = await serverFetchJson<string>(
  83. `${BASE_API_URL}/stockTake/import`,
  84. {
  85. method: "POST",
  86. body: data,
  87. },
  88. );
  89. return importStockTake;
  90. }
  91. export const getStockTakeRecords = async () => {
  92. const stockTakeRecords = await serverFetchJson<AllPickedStockTakeListReponse[]>( // 改为 serverFetchJson
  93. `${BASE_API_URL}/stockTakeRecord/AllPickedStockOutRecordList`,
  94. {
  95. method: "GET",
  96. },
  97. );
  98. return stockTakeRecords;
  99. }
  100. export const getApproverStockTakeRecords = async () => {
  101. const stockTakeRecords = await serverFetchJson<AllPickedStockTakeListReponse[]>( // 改为 serverFetchJson
  102. `${BASE_API_URL}/stockTakeRecord/AllApproverStockTakeList`,
  103. {
  104. method: "GET",
  105. },
  106. );
  107. return stockTakeRecords;
  108. }
  109. export const createStockTakeForSections = async () => {
  110. const createStockTakeForSections = await serverFetchJson<Map<string, string>>(
  111. `${BASE_API_URL}/stockTake/createForSections`,
  112. {
  113. method: "POST",
  114. },
  115. );
  116. return createStockTakeForSections;
  117. }
  118. export const saveStockTakeRecord = async (
  119. request: SaveStockTakeRecordRequest,
  120. stockTakeId: number,
  121. stockTakerId: number
  122. ) => {
  123. try {
  124. const result = await serverFetchJson<any>(
  125. `${BASE_API_URL}/stockTakeRecord/saveStockTakeRecord?stockTakeId=${stockTakeId}&stockTakerId=${stockTakerId}`,
  126. {
  127. method: "POST",
  128. headers: {
  129. "Content-Type": "application/json",
  130. },
  131. body: JSON.stringify(request),
  132. },
  133. );
  134. console.log('saveStockTakeRecord: request:', request);
  135. console.log('saveStockTakeRecord: stockTakeId:', stockTakeId);
  136. console.log('saveStockTakeRecord: stockTakerId:', stockTakerId);
  137. return result;
  138. } catch (error: any) {
  139. // 尝试从错误响应中提取消息
  140. if (error?.response) {
  141. try {
  142. const errorData = await error.response.json();
  143. const errorWithMessage = new Error(errorData.message || errorData.error || "Failed to save stock take record");
  144. (errorWithMessage as any).response = error.response;
  145. throw errorWithMessage;
  146. } catch {
  147. throw error;
  148. }
  149. }
  150. throw error;
  151. }
  152. }
  153. export interface BatchSaveStockTakeRecordRequest {
  154. stockTakeId: number;
  155. stockTakeSection: string;
  156. stockTakerId: number;
  157. //stockTakerName: string;
  158. }
  159. export interface BatchSaveStockTakeRecordResponse {
  160. successCount: number;
  161. errorCount: number;
  162. errors: string[];
  163. }
  164. export const batchSaveStockTakeRecords = cache(async (data: BatchSaveStockTakeRecordRequest) => {
  165. return serverFetchJson<BatchSaveStockTakeRecordResponse>(`${BASE_API_URL}/stockTakeRecord/batchSaveStockTakeRecords`,
  166. {
  167. method: "POST",
  168. body: JSON.stringify(data),
  169. headers: { "Content-Type": "application/json" },
  170. })
  171. })
  172. // Add these interfaces and functions
  173. export interface SaveApproverStockTakeRecordRequest {
  174. stockTakeRecordId?: number | null;
  175. qty: number;
  176. badQty: number;
  177. approverId?: number | null;
  178. approverQty?: number | null;
  179. approverBadQty?: number | null;
  180. }
  181. export interface BatchSaveApproverStockTakeRecordRequest {
  182. stockTakeId: number;
  183. stockTakeSection: string;
  184. approverId: number;
  185. }
  186. export interface BatchSaveApproverStockTakeRecordResponse {
  187. successCount: number;
  188. errorCount: number;
  189. errors: string[];
  190. }
  191. export const saveApproverStockTakeRecord = async (
  192. request: SaveApproverStockTakeRecordRequest,
  193. stockTakeId: number
  194. ) => {
  195. try {
  196. const result = await serverFetchJson<any>(
  197. `${BASE_API_URL}/stockTakeRecord/saveApproverStockTakeRecord?stockTakeId=${stockTakeId}`,
  198. {
  199. method: "POST",
  200. headers: {
  201. "Content-Type": "application/json",
  202. },
  203. body: JSON.stringify(request),
  204. },
  205. );
  206. return result;
  207. } catch (error: any) {
  208. if (error?.response) {
  209. try {
  210. const errorData = await error.response.json();
  211. const errorWithMessage = new Error(errorData.message || errorData.error || "Failed to save approver stock take record");
  212. (errorWithMessage as any).response = error.response;
  213. throw errorWithMessage;
  214. } catch {
  215. throw error;
  216. }
  217. }
  218. throw error;
  219. }
  220. }
  221. export const batchSaveApproverStockTakeRecords = cache(async (data: BatchSaveApproverStockTakeRecordRequest) => {
  222. return serverFetchJson<BatchSaveApproverStockTakeRecordResponse>(
  223. `${BASE_API_URL}/stockTakeRecord/batchSaveApproverStockTakeRecords`,
  224. {
  225. method: "POST",
  226. body: JSON.stringify(data),
  227. headers: { "Content-Type": "application/json" },
  228. }
  229. )
  230. }
  231. )
  232. export const updateStockTakeRecordStatusToNotMatch = async (
  233. stockTakeRecordId: number
  234. ) => {
  235. try {
  236. const result = await serverFetchJson<any>(
  237. `${BASE_API_URL}/stockTakeRecord/updateStockTakeRecordStatusToNotMatch?stockTakeRecordId=${stockTakeRecordId}`,
  238. {
  239. method: "POST",
  240. },
  241. );
  242. return result;
  243. } catch (error: any) {
  244. if (error?.response) {
  245. try {
  246. const errorData = await error.response.json();
  247. const errorWithMessage = new Error(errorData.message || errorData.error || "Failed to update stock take record status");
  248. (errorWithMessage as any).response = error.response;
  249. throw errorWithMessage;
  250. } catch {
  251. throw error;
  252. }
  253. }
  254. throw error;
  255. }
  256. }
  257. export const getInventoryLotDetailsBySectionNotMatch = async (
  258. stockTakeSection: string,
  259. stockTakeId?: number | null
  260. ) => {
  261. console.log('🌐 [API] getInventoryLotDetailsBySectionNotMatch called with:', {
  262. stockTakeSection,
  263. stockTakeId
  264. });
  265. const encodedSection = encodeURIComponent(stockTakeSection);
  266. let url = `${BASE_API_URL}/stockTakeRecord/inventoryLotDetailsBySectionNotMatch?stockTakeSection=${encodedSection}`;
  267. if (stockTakeId != null && stockTakeId > 0) {
  268. url += `&stockTakeId=${stockTakeId}`;
  269. }
  270. console.log(' [API] Full URL:', url);
  271. const details = await serverFetchJson<InventoryLotDetailResponse[]>(
  272. url,
  273. {
  274. method: "GET",
  275. },
  276. );
  277. console.log('[API] Response received:', details);
  278. return details;
  279. }