|
- // actions.ts
- "use server";
- import { cache } from 'react';
- import { serverFetchJson } from "@/app/utils/fetchUtil"; // 改为 serverFetchJson
- import { BASE_API_URL } from "@/config/api";
- export interface InventoryLotDetailResponse {
- id: number;
- inventoryLotId: number;
- itemId: number;
- itemCode: string;
- itemName: string;
- lotNo: string;
- expiryDate: string;
- productionDate: string;
- stockInDate: string;
- inQty: number;
- outQty: number;
- holdQty: number;
- availableQty: number;
- uom: string;
- warehouseCode: string;
- warehouseName: string;
- warehouseSlot: string;
- warehouseArea: string;
- warehouse: string;
- varianceQty: number | null;
- status: string;
- remarks: string | null;
- stockTakeRecordStatus: string;
- stockTakeRecordId: number | null;
- firstStockTakeQty: number | null;
- secondStockTakeQty: number | null;
- firstBadQty: number | null;
- secondBadQty: number | null;
- approverQty: number | null;
- approverBadQty: number | null;
- finalQty: number | null;
- }
-
- export const getInventoryLotDetailsBySection = async (
- stockTakeSection: string,
- stockTakeId?: number | null
- ) => {
- console.log('🌐 [API] getInventoryLotDetailsBySection called with:', {
- stockTakeSection,
- stockTakeId
- });
-
- const encodedSection = encodeURIComponent(stockTakeSection);
- let url = `${BASE_API_URL}/stockTakeRecord/inventoryLotDetailsBySection?stockTakeSection=${encodedSection}`;
- if (stockTakeId != null && stockTakeId > 0) {
- url += `&stockTakeId=${stockTakeId}`;
- }
-
- console.log(' [API] Full URL:', url);
-
- const details = await serverFetchJson<InventoryLotDetailResponse[]>(
- url,
- {
- method: "GET",
- },
- );
-
- console.log('[API] Response received:', details);
- return details;
- }
- export interface SaveStockTakeRecordRequest {
- stockTakeRecordId?: number | null;
- inventoryLotLineId: number;
- qty: number;
- badQty: number;
- //stockTakerName: string;
- remark?: string | null;
- }
- export interface AllPickedStockTakeListReponse {
- id: number;
- stockTakeSession: string;
- lastStockTakeDate: string | null;
- status: string|null;
- currentStockTakeItemNumber: number;
- totalInventoryLotNumber: number;
- stockTakeId: number;
- stockTakerName: string | null;
- totalItemNumber: number;
- }
-
- export const importStockTake = async (data: FormData) => {
- const importStockTake = await serverFetchJson<string>(
- `${BASE_API_URL}/stockTake/import`,
- {
- method: "POST",
- body: data,
- },
- );
- return importStockTake;
- }
-
- export const getStockTakeRecords = async () => {
- const stockTakeRecords = await serverFetchJson<AllPickedStockTakeListReponse[]>( // 改为 serverFetchJson
- `${BASE_API_URL}/stockTakeRecord/AllPickedStockOutRecordList`,
- {
- method: "GET",
- },
- );
- return stockTakeRecords;
- }
- export const getApproverStockTakeRecords = async () => {
- const stockTakeRecords = await serverFetchJson<AllPickedStockTakeListReponse[]>( // 改为 serverFetchJson
- `${BASE_API_URL}/stockTakeRecord/AllApproverStockTakeList`,
- {
- method: "GET",
- },
- );
- return stockTakeRecords;
- }
- export const createStockTakeForSections = async () => {
- const createStockTakeForSections = await serverFetchJson<Map<string, string>>(
- `${BASE_API_URL}/stockTake/createForSections`,
- {
- method: "POST",
- },
- );
- return createStockTakeForSections;
- }
- export const saveStockTakeRecord = async (
- request: SaveStockTakeRecordRequest,
- stockTakeId: number,
- stockTakerId: number
- ) => {
- try {
- const result = await serverFetchJson<any>(
-
- `${BASE_API_URL}/stockTakeRecord/saveStockTakeRecord?stockTakeId=${stockTakeId}&stockTakerId=${stockTakerId}`,
- {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify(request),
- },
- );
- console.log('saveStockTakeRecord: request:', request);
- console.log('saveStockTakeRecord: stockTakeId:', stockTakeId);
- console.log('saveStockTakeRecord: stockTakerId:', stockTakerId);
- return result;
- } catch (error: any) {
- // 尝试从错误响应中提取消息
- if (error?.response) {
- try {
- const errorData = await error.response.json();
- const errorWithMessage = new Error(errorData.message || errorData.error || "Failed to save stock take record");
- (errorWithMessage as any).response = error.response;
- throw errorWithMessage;
- } catch {
- throw error;
- }
- }
- throw error;
- }
- }
- export interface BatchSaveStockTakeRecordRequest {
- stockTakeId: number;
- stockTakeSection: string;
- stockTakerId: number;
- //stockTakerName: string;
- }
-
- export interface BatchSaveStockTakeRecordResponse {
- successCount: number;
- errorCount: number;
- errors: string[];
- }
- export const batchSaveStockTakeRecords = cache(async (data: BatchSaveStockTakeRecordRequest) => {
- return serverFetchJson<BatchSaveStockTakeRecordResponse>(`${BASE_API_URL}/stockTakeRecord/batchSaveStockTakeRecords`,
- {
- method: "POST",
- body: JSON.stringify(data),
- headers: { "Content-Type": "application/json" },
- })
- })
- // Add these interfaces and functions
-
- export interface SaveApproverStockTakeRecordRequest {
- stockTakeRecordId?: number | null;
- qty: number;
- badQty: number;
- approverId?: number | null;
- approverQty?: number | null;
- approverBadQty?: number | null;
- }
-
- export interface BatchSaveApproverStockTakeRecordRequest {
- stockTakeId: number;
- stockTakeSection: string;
- approverId: number;
- }
-
- export interface BatchSaveApproverStockTakeRecordResponse {
- successCount: number;
- errorCount: number;
- errors: string[];
- }
-
-
- export const saveApproverStockTakeRecord = async (
- request: SaveApproverStockTakeRecordRequest,
- stockTakeId: number
- ) => {
- try {
- const result = await serverFetchJson<any>(
- `${BASE_API_URL}/stockTakeRecord/saveApproverStockTakeRecord?stockTakeId=${stockTakeId}`,
- {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify(request),
- },
- );
- return result;
- } catch (error: any) {
- if (error?.response) {
- try {
- const errorData = await error.response.json();
- const errorWithMessage = new Error(errorData.message || errorData.error || "Failed to save approver stock take record");
- (errorWithMessage as any).response = error.response;
- throw errorWithMessage;
- } catch {
- throw error;
- }
- }
- throw error;
- }
- }
-
- export const batchSaveApproverStockTakeRecords = cache(async (data: BatchSaveApproverStockTakeRecordRequest) => {
- return serverFetchJson<BatchSaveApproverStockTakeRecordResponse>(
- `${BASE_API_URL}/stockTakeRecord/batchSaveApproverStockTakeRecords`,
- {
- method: "POST",
- body: JSON.stringify(data),
- headers: { "Content-Type": "application/json" },
- }
- )
- }
- )
-
- export const updateStockTakeRecordStatusToNotMatch = async (
- stockTakeRecordId: number
- ) => {
- try {
- const result = await serverFetchJson<any>(
- `${BASE_API_URL}/stockTakeRecord/updateStockTakeRecordStatusToNotMatch?stockTakeRecordId=${stockTakeRecordId}`,
- {
- method: "POST",
- },
- );
- return result;
- } catch (error: any) {
- if (error?.response) {
- try {
- const errorData = await error.response.json();
- const errorWithMessage = new Error(errorData.message || errorData.error || "Failed to update stock take record status");
- (errorWithMessage as any).response = error.response;
- throw errorWithMessage;
- } catch {
- throw error;
- }
- }
- throw error;
- }
- }
-
- export const getInventoryLotDetailsBySectionNotMatch = async (
- stockTakeSection: string,
- stockTakeId?: number | null
- ) => {
- console.log('🌐 [API] getInventoryLotDetailsBySectionNotMatch called with:', {
- stockTakeSection,
- stockTakeId
- });
-
- const encodedSection = encodeURIComponent(stockTakeSection);
- let url = `${BASE_API_URL}/stockTakeRecord/inventoryLotDetailsBySectionNotMatch?stockTakeSection=${encodedSection}`;
- if (stockTakeId != null && stockTakeId > 0) {
- url += `&stockTakeId=${stockTakeId}`;
- }
-
- console.log(' [API] Full URL:', url);
-
- const details = await serverFetchJson<InventoryLotDetailResponse[]>(
- url,
- {
- method: "GET",
- },
- );
-
- console.log('[API] Response received:', details);
- return details;
- }
|