FPSMS-frontend
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 

305 строки
9.2 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. approverName: string | null;
  76. currentStockTakeItemNumber: number;
  77. totalInventoryLotNumber: number;
  78. stockTakeId: number;
  79. stockTakerName: string | null;
  80. totalItemNumber: number;
  81. startTime: string | null;
  82. endTime: string | null;
  83. reStockTakeTrueFalse: boolean;
  84. }
  85. export const importStockTake = async (data: FormData) => {
  86. const importStockTake = await serverFetchJson<string>(
  87. `${BASE_API_URL}/stockTake/import`,
  88. {
  89. method: "POST",
  90. body: data,
  91. },
  92. );
  93. return importStockTake;
  94. }
  95. export const getStockTakeRecords = async () => {
  96. const stockTakeRecords = await serverFetchJson<AllPickedStockTakeListReponse[]>( // 改为 serverFetchJson
  97. `${BASE_API_URL}/stockTakeRecord/AllPickedStockOutRecordList`,
  98. {
  99. method: "GET",
  100. },
  101. );
  102. return stockTakeRecords;
  103. }
  104. export const getApproverStockTakeRecords = async () => {
  105. const stockTakeRecords = await serverFetchJson<AllPickedStockTakeListReponse[]>( // 改为 serverFetchJson
  106. `${BASE_API_URL}/stockTakeRecord/AllApproverStockTakeList`,
  107. {
  108. method: "GET",
  109. },
  110. );
  111. return stockTakeRecords;
  112. }
  113. export const createStockTakeForSections = async () => {
  114. const createStockTakeForSections = await serverFetchJson<Map<string, string>>(
  115. `${BASE_API_URL}/stockTake/createForSections`,
  116. {
  117. method: "POST",
  118. },
  119. );
  120. return createStockTakeForSections;
  121. }
  122. export const saveStockTakeRecord = async (
  123. request: SaveStockTakeRecordRequest,
  124. stockTakeId: number,
  125. stockTakerId: number
  126. ) => {
  127. try {
  128. const result = await serverFetchJson<any>(
  129. `${BASE_API_URL}/stockTakeRecord/saveStockTakeRecord?stockTakeId=${stockTakeId}&stockTakerId=${stockTakerId}`,
  130. {
  131. method: "POST",
  132. headers: {
  133. "Content-Type": "application/json",
  134. },
  135. body: JSON.stringify(request),
  136. },
  137. );
  138. console.log('saveStockTakeRecord: request:', request);
  139. console.log('saveStockTakeRecord: stockTakeId:', stockTakeId);
  140. console.log('saveStockTakeRecord: stockTakerId:', stockTakerId);
  141. return result;
  142. } catch (error: any) {
  143. // 尝试从错误响应中提取消息
  144. if (error?.response) {
  145. try {
  146. const errorData = await error.response.json();
  147. const errorWithMessage = new Error(errorData.message || errorData.error || "Failed to save stock take record");
  148. (errorWithMessage as any).response = error.response;
  149. throw errorWithMessage;
  150. } catch {
  151. throw error;
  152. }
  153. }
  154. throw error;
  155. }
  156. }
  157. export interface BatchSaveStockTakeRecordRequest {
  158. stockTakeId: number;
  159. stockTakeSection: string;
  160. stockTakerId: number;
  161. //stockTakerName: string;
  162. }
  163. export interface BatchSaveStockTakeRecordResponse {
  164. successCount: number;
  165. errorCount: number;
  166. errors: string[];
  167. }
  168. export const batchSaveStockTakeRecords = cache(async (data: BatchSaveStockTakeRecordRequest) => {
  169. return serverFetchJson<BatchSaveStockTakeRecordResponse>(`${BASE_API_URL}/stockTakeRecord/batchSaveStockTakeRecords`,
  170. {
  171. method: "POST",
  172. body: JSON.stringify(data),
  173. headers: { "Content-Type": "application/json" },
  174. })
  175. })
  176. // Add these interfaces and functions
  177. export interface SaveApproverStockTakeRecordRequest {
  178. stockTakeRecordId?: number | null;
  179. qty: number;
  180. badQty: number;
  181. approverId?: number | null;
  182. approverQty?: number | null;
  183. approverBadQty?: number | null;
  184. }
  185. export interface BatchSaveApproverStockTakeRecordRequest {
  186. stockTakeId: number;
  187. stockTakeSection: string;
  188. approverId: number;
  189. }
  190. export interface BatchSaveApproverStockTakeRecordResponse {
  191. successCount: number;
  192. errorCount: number;
  193. errors: string[];
  194. }
  195. export const saveApproverStockTakeRecord = async (
  196. request: SaveApproverStockTakeRecordRequest,
  197. stockTakeId: number
  198. ) => {
  199. try {
  200. const result = await serverFetchJson<any>(
  201. `${BASE_API_URL}/stockTakeRecord/saveApproverStockTakeRecord?stockTakeId=${stockTakeId}`,
  202. {
  203. method: "POST",
  204. headers: {
  205. "Content-Type": "application/json",
  206. },
  207. body: JSON.stringify(request),
  208. },
  209. );
  210. return result;
  211. } catch (error: any) {
  212. if (error?.response) {
  213. try {
  214. const errorData = await error.response.json();
  215. const errorWithMessage = new Error(errorData.message || errorData.error || "Failed to save approver stock take record");
  216. (errorWithMessage as any).response = error.response;
  217. throw errorWithMessage;
  218. } catch {
  219. throw error;
  220. }
  221. }
  222. throw error;
  223. }
  224. }
  225. export const batchSaveApproverStockTakeRecords = cache(async (data: BatchSaveApproverStockTakeRecordRequest) => {
  226. return serverFetchJson<BatchSaveApproverStockTakeRecordResponse>(
  227. `${BASE_API_URL}/stockTakeRecord/batchSaveApproverStockTakeRecords`,
  228. {
  229. method: "POST",
  230. body: JSON.stringify(data),
  231. headers: { "Content-Type": "application/json" },
  232. }
  233. )
  234. }
  235. )
  236. export const updateStockTakeRecordStatusToNotMatch = async (
  237. stockTakeRecordId: number
  238. ) => {
  239. try {
  240. const result = await serverFetchJson<any>(
  241. `${BASE_API_URL}/stockTakeRecord/updateStockTakeRecordStatusToNotMatch?stockTakeRecordId=${stockTakeRecordId}`,
  242. {
  243. method: "POST",
  244. },
  245. );
  246. return result;
  247. } catch (error: any) {
  248. if (error?.response) {
  249. try {
  250. const errorData = await error.response.json();
  251. const errorWithMessage = new Error(errorData.message || errorData.error || "Failed to update stock take record status");
  252. (errorWithMessage as any).response = error.response;
  253. throw errorWithMessage;
  254. } catch {
  255. throw error;
  256. }
  257. }
  258. throw error;
  259. }
  260. }
  261. export const getInventoryLotDetailsBySectionNotMatch = async (
  262. stockTakeSection: string,
  263. stockTakeId?: number | null
  264. ) => {
  265. console.log('🌐 [API] getInventoryLotDetailsBySectionNotMatch called with:', {
  266. stockTakeSection,
  267. stockTakeId
  268. });
  269. const encodedSection = encodeURIComponent(stockTakeSection);
  270. let url = `${BASE_API_URL}/stockTakeRecord/inventoryLotDetailsBySectionNotMatch?stockTakeSection=${encodedSection}`;
  271. if (stockTakeId != null && stockTakeId > 0) {
  272. url += `&stockTakeId=${stockTakeId}`;
  273. }
  274. console.log(' [API] Full URL:', url);
  275. const details = await serverFetchJson<InventoryLotDetailResponse[]>(
  276. url,
  277. {
  278. method: "GET",
  279. },
  280. );
  281. console.log('[API] Response received:', details);
  282. return details;
  283. }