|
|
|
@@ -3,6 +3,7 @@ |
|
|
|
import { cache } from 'react'; |
|
|
|
import { serverFetchJson } from "@/app/utils/fetchUtil"; // 改为 serverFetchJson |
|
|
|
import { BASE_API_URL } from "@/config/api"; |
|
|
|
import { stockTakeDebugLog } from "@/components/StockTakeManagement/stockTakeDebugLog"; |
|
|
|
|
|
|
|
export interface RecordsRes<T> { |
|
|
|
records: T[]; |
|
|
|
@@ -51,6 +52,31 @@ export interface InventoryLotDetailResponse { |
|
|
|
approverTime?: string | string[] | null; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* `approverInventoryLotDetailsAll*`: |
|
|
|
* - `total` = 全域 `inventory_lot_line` 中 `status = available` 筆數(與 DB COUNT 一致) |
|
|
|
* - `filteredRecordCount` = 目前 tab/篩選後筆數(分頁用) |
|
|
|
*/ |
|
|
|
export interface ApproverInventoryLotDetailsRecordsRes extends RecordsRes<InventoryLotDetailResponse> { |
|
|
|
filteredRecordCount?: number; |
|
|
|
totalWaitingForApprover?: number; |
|
|
|
totalApproved?: number; |
|
|
|
} |
|
|
|
|
|
|
|
function normalizeApproverInventoryLotDetailsRes( |
|
|
|
raw: ApproverInventoryLotDetailsRecordsRes |
|
|
|
): ApproverInventoryLotDetailsRecordsRes { |
|
|
|
const waiting = Number(raw.totalWaitingForApprover ?? 0) || 0; |
|
|
|
const approved = Number(raw.totalApproved ?? 0) || 0; |
|
|
|
return { |
|
|
|
records: Array.isArray(raw.records) ? raw.records : [], |
|
|
|
total: Number(raw.total ?? 0) || 0, |
|
|
|
filteredRecordCount: Number(raw.filteredRecordCount ?? 0) || 0, |
|
|
|
totalWaitingForApprover: waiting, |
|
|
|
totalApproved: approved, |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
export const getInventoryLotDetailsBySection = async ( |
|
|
|
stockTakeSection: string, |
|
|
|
stockTakeId?: number | null, |
|
|
|
@@ -122,13 +148,13 @@ export const getApproverInventoryLotDetailsAll = async ( |
|
|
|
} |
|
|
|
|
|
|
|
const url = `${BASE_API_URL}/stockTakeRecord/approverInventoryLotDetailsAll?${params.toString()}`; |
|
|
|
const response = await serverFetchJson<RecordsRes<InventoryLotDetailResponse>>( |
|
|
|
const response = await serverFetchJson<ApproverInventoryLotDetailsRecordsRes>( |
|
|
|
url, |
|
|
|
{ |
|
|
|
method: "GET", |
|
|
|
}, |
|
|
|
); |
|
|
|
return response; |
|
|
|
return normalizeApproverInventoryLotDetailsRes(response); |
|
|
|
} |
|
|
|
export const getApproverInventoryLotDetailsAllPending = async ( |
|
|
|
stockTakeId?: number | null, |
|
|
|
@@ -142,7 +168,8 @@ export const getApproverInventoryLotDetailsAllPending = async ( |
|
|
|
params.append("stockTakeId", String(stockTakeId)); |
|
|
|
} |
|
|
|
const url = `${BASE_API_URL}/stockTakeRecord/approverInventoryLotDetailsAllPending?${params.toString()}`; |
|
|
|
return serverFetchJson<RecordsRes<InventoryLotDetailResponse>>(url, { method: "GET" }); |
|
|
|
const response = await serverFetchJson<ApproverInventoryLotDetailsRecordsRes>(url, { method: "GET" }); |
|
|
|
return normalizeApproverInventoryLotDetailsRes(response); |
|
|
|
} |
|
|
|
export const getApproverInventoryLotDetailsAllApproved = async ( |
|
|
|
stockTakeId?: number | null, |
|
|
|
@@ -156,7 +183,8 @@ export const getApproverInventoryLotDetailsAllApproved = async ( |
|
|
|
params.append("stockTakeId", String(stockTakeId)); |
|
|
|
} |
|
|
|
const url = `${BASE_API_URL}/stockTakeRecord/approverInventoryLotDetailsAllApproved?${params.toString()}`; |
|
|
|
return serverFetchJson<RecordsRes<InventoryLotDetailResponse>>(url, { method: "GET" }); |
|
|
|
const response = await serverFetchJson<ApproverInventoryLotDetailsRecordsRes>(url, { method: "GET" }); |
|
|
|
return normalizeApproverInventoryLotDetailsRes(response); |
|
|
|
} |
|
|
|
|
|
|
|
export const importStockTake = async (data: FormData) => { |
|
|
|
@@ -242,6 +270,20 @@ export const saveStockTakeRecord = async ( |
|
|
|
console.log('saveStockTakeRecord: request:', request); |
|
|
|
console.log('saveStockTakeRecord: stockTakeId:', stockTakeId); |
|
|
|
console.log('saveStockTakeRecord: stockTakerId:', stockTakerId); |
|
|
|
// #region agent log |
|
|
|
stockTakeDebugLog( |
|
|
|
"actions.ts:saveStockTakeRecord", |
|
|
|
"server action saveStockTakeRecord ok", |
|
|
|
"H3", |
|
|
|
{ |
|
|
|
stockTakeId, |
|
|
|
stockTakerId, |
|
|
|
inventoryLotLineId: request.inventoryLotLineId, |
|
|
|
hasRecordId: request.stockTakeRecordId != null, |
|
|
|
resultId: result?.id ?? null, |
|
|
|
} |
|
|
|
); |
|
|
|
// #endregion |
|
|
|
return result; |
|
|
|
} catch (error: any) { |
|
|
|
// 尝试从错误响应中提取消息 |
|
|
|
@@ -271,12 +313,26 @@ export interface BatchSaveStockTakeRecordResponse { |
|
|
|
errors: string[]; |
|
|
|
} |
|
|
|
export const batchSaveStockTakeRecords = cache(async (data: BatchSaveStockTakeRecordRequest) => { |
|
|
|
return serverFetchJson<BatchSaveStockTakeRecordResponse>(`${BASE_API_URL}/stockTakeRecord/batchSaveStockTakeRecords`, |
|
|
|
const r = await serverFetchJson<BatchSaveStockTakeRecordResponse>(`${BASE_API_URL}/stockTakeRecord/batchSaveStockTakeRecords`, |
|
|
|
{ |
|
|
|
method: "POST", |
|
|
|
body: JSON.stringify(data), |
|
|
|
headers: { "Content-Type": "application/json" }, |
|
|
|
}) |
|
|
|
// #region agent log |
|
|
|
stockTakeDebugLog( |
|
|
|
"actions.ts:batchSaveStockTakeRecords", |
|
|
|
"server batch picker result", |
|
|
|
"H4", |
|
|
|
{ |
|
|
|
stockTakeId: data.stockTakeId, |
|
|
|
stockTakeSection: data.stockTakeSection, |
|
|
|
successCount: r.successCount, |
|
|
|
errorCount: r.errorCount, |
|
|
|
} |
|
|
|
); |
|
|
|
// #endregion |
|
|
|
return r |
|
|
|
}) |
|
|
|
// Add these interfaces and functions |
|
|
|
|
|
|
|
@@ -325,6 +381,19 @@ export const saveApproverStockTakeRecord = async ( |
|
|
|
body: JSON.stringify(request), |
|
|
|
}, |
|
|
|
); |
|
|
|
// #region agent log |
|
|
|
stockTakeDebugLog( |
|
|
|
"actions.ts:saveApproverStockTakeRecord", |
|
|
|
"server action saveApproverStockTakeRecord ok", |
|
|
|
"H3", |
|
|
|
{ |
|
|
|
stockTakeId, |
|
|
|
stockTakeRecordId: request.stockTakeRecordId ?? null, |
|
|
|
lastSelect: request.lastSelect ?? null, |
|
|
|
hasApproverQty: request.approverQty != null, |
|
|
|
} |
|
|
|
); |
|
|
|
// #endregion |
|
|
|
return result; |
|
|
|
} catch (error: any) { |
|
|
|
if (error?.response) { |
|
|
|
@@ -354,7 +423,7 @@ export const batchSaveApproverStockTakeRecords = cache(async (data: BatchSaveApp |
|
|
|
) |
|
|
|
|
|
|
|
export const batchSaveApproverStockTakeRecordsAll = cache(async (data: BatchSaveApproverStockTakeAllRequest) => { |
|
|
|
return serverFetchJson<BatchSaveApproverStockTakeRecordResponse>( |
|
|
|
const r = await serverFetchJson<BatchSaveApproverStockTakeRecordResponse>( |
|
|
|
`${BASE_API_URL}/stockTakeRecord/batchSaveApproverStockTakeRecordsAll`, |
|
|
|
{ |
|
|
|
method: "POST", |
|
|
|
@@ -362,6 +431,20 @@ export const batchSaveApproverStockTakeRecordsAll = cache(async (data: BatchSave |
|
|
|
headers: { "Content-Type": "application/json" }, |
|
|
|
} |
|
|
|
) |
|
|
|
// #region agent log |
|
|
|
stockTakeDebugLog( |
|
|
|
"actions.ts:batchSaveApproverStockTakeRecordsAll", |
|
|
|
"server batch approver-all result", |
|
|
|
"H4", |
|
|
|
{ |
|
|
|
stockTakeId: data.stockTakeId, |
|
|
|
approverId: data.approverId, |
|
|
|
successCount: r.successCount, |
|
|
|
errorCount: r.errorCount, |
|
|
|
} |
|
|
|
); |
|
|
|
// #endregion |
|
|
|
return r |
|
|
|
}) |
|
|
|
|
|
|
|
export const updateStockTakeRecordStatusToNotMatch = async ( |
|
|
|
|