云
2 小时以前 7a93a85f75bcfc8001ea1d3b921860806a8209ac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import type { MesWmMaterialStockApi } from '#/api/mes/wm/materialstock';
 
import { getBagCodeByCode, resolveTraceType } from '#/api/mes/pro/bagCode';
import { getBatchByCode } from '#/api/mes/pro/batch';
import { getPalletByCode } from '#/api/mes/pro/pallet';
import { getMaterialStockPage } from '#/api/mes/wm/materialstock';
 
import { extractCode } from './traceCode';
 
export type TraceType = 'bag' | 'batch' | 'pallet';
 
export interface TraceObject {
  bagCount?: number;
  bagQuantity?: number;
  batchCode?: string;
  batchId?: number;
  batchStatus?: number;
  businessCode?: string;
  code?: string;
  id?: number;
  inboundBagQuantity?: number;
  inboundTime?: string;
  itemId?: number;
  itemName?: string;
  lineName?: string;
  palletCode?: string;
  palletId?: number;
  pendingBagQuantity?: number;
  produceDate?: string;
  status?: number;
  tempCode?: string;
  unitMeasureName?: string;
}
 
export interface TraceResult {
  code: string;
  data: TraceObject;
  type: TraceType;
}
 
/** 解析扫描原文,按码类型查询袋码 / 托盘 / 批次对象。 */
export async function lookupTrace(raw: string): Promise<TraceResult> {
  const code = extractCode(raw);
  const type = (await resolveTraceType(code)) as TraceType | null;
  if (!type) {
    throw new Error('未找到对应袋码、托盘或批次');
  }
  const fetchers: Record<TraceType, (value: string) => Promise<TraceObject>> = {
    bag: getBagCodeByCode,
    batch: getBatchByCode,
    pallet: getPalletByCode,
  };
  const data = await fetchers[type](code);
  if (!data?.id) {
    throw new Error('未找到对象信息');
  }
  return { code, data, type };
}
 
/** 扫码对象可带入单据的数量:袋码按 1 袋,托盘按已装袋数,批次按批次袋数。 */
export function traceQuantity(trace: TraceResult): number | undefined {
  if (trace.type === 'bag') {
    return 1;
  }
  if (trace.type === 'pallet') {
    return trace.data.bagCount;
  }
  return trace.data.bagQuantity;
}
 
/** 按扫码对象的物料与批次查找对应库存记录,供需要选择库存记录的单据带出明细。 */
export async function findMaterialStocks(
  trace: TraceResult,
  options: { stockState?: number; virtualFilter?: string } = {},
): Promise<MesWmMaterialStockApi.MaterialStock[]> {
  if (!trace.data.itemId) {
    return [];
  }
  const result = await getMaterialStockPage({
    pageNo: 1,
    pageSize: 50,
    batchCode: trace.data.batchCode,
    itemId: trace.data.itemId,
    ...options,
  });
  return result.list;
}