zhou zhou
3 天以前 0a1d91e42e6c5af96e1108e9ebcc37e99eb3b22c
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
const OUT_BOUND_STATUS_META = {
  0: { label: '冻结', tagType: 'danger' },
  1: { label: '正常', tagType: 'success' }
}
 
function normalizeText(value) {
  return String(value ?? '').trim()
}
 
function normalizeOptionalText(value) {
  const text = normalizeText(value)
  return text === '-' ? '' : text
}
 
function normalizeNumber(value) {
  if (value === '' || value === null || value === undefined) {
    return 0
  }
  const numericValue = Number(value)
  return Number.isFinite(numericValue) ? numericValue : 0
}
 
function getStatusMeta(status, statusText) {
  const fallback = OUT_BOUND_STATUS_META[Number(status)] || {
    label: statusText || '-',
    tagType: 'info'
  }
  return {
    label: statusText || fallback.label,
    tagType: fallback.tagType
  }
}
 
function toNumberOrOriginal(value) {
  if (value === '' || value === null || value === undefined) {
    return value
  }
  const numericValue = Number(value)
  return Number.isFinite(numericValue) ? numericValue : value
}
 
function normalizeItemOutQty(row = {}) {
  const outQty = row.outQty !== undefined && row.outQty !== null && row.outQty !== ''
    ? normalizeNumber(row.outQty)
    : normalizeNumber(row.anfme)
  return outQty < 0 ? 0 : outQty
}
 
function compactDefinedFields(record = {}) {
  return Object.fromEntries(
    Object.entries(record).filter(([, value]) => value !== undefined && value !== null && value !== '')
  )
}
 
export function createOutBoundSearchState() {
  return {
    condition: '',
    locCode: '',
    matnrCode: '',
    maktx: '',
    batch: '',
    splrBatch: '',
    trackCode: '',
    status: '',
    memo: ''
  }
}
 
export function createOutBoundTaskState() {
  return {
    siteNo: '',
    memo: ''
  }
}
 
export function buildOutBoundSearchParams(params = {}) {
  const result = {}
  ;['condition', 'locCode', 'matnrCode', 'maktx', 'batch', 'splrBatch', 'trackCode', 'memo'].forEach((key) => {
    const value = normalizeText(params[key])
    if (value) {
      result[key] = value
    }
  })
 
  if (params.status !== '' && params.status !== undefined && params.status !== null) {
    result.status = Number(params.status)
  }
 
  return result
}
 
export function buildOutBoundPageQueryParams(params = {}) {
  return {
    current: params.current || 1,
    pageSize: params.pageSize || params.size || 20,
    ...buildOutBoundSearchParams(params)
  }
}
 
export function normalizeOutBoundRow(record = {}) {
  const statusMeta = getStatusMeta(record.status, record['status$'])
  return {
    ...record,
    locCode: normalizeText(record.locCode) || '-',
    matnrCode: normalizeText(record.matnrCode) || '-',
    maktx: normalizeText(record.maktx) || '-',
    trackCode: normalizeText(record.trackCode) || '-',
    batch: normalizeText(record.batch) || '-',
    splrBatch: normalizeText(record.splrBatch) || '-',
    unit: normalizeText(record.unit) || '-',
    anfme: normalizeNumber(record.anfme),
    workQty: normalizeNumber(record.workQty),
    qty: normalizeNumber(record.qty),
    outQty: normalizeNumber(record.outQty),
    statusText: statusMeta.label,
    statusTagType: statusMeta.tagType,
    createTimeText: normalizeText(record['createTime$'] || record.createTime) || '-',
    updateTimeText: normalizeText(record['updateTime$'] || record.updateTime) || '-',
    splrCode: normalizeText(record.splrCode) || '-',
    splrName: normalizeText(record.splrName) || '-',
    siteNo: normalizeText(record.siteNo) || '-',
    memo: normalizeText(record.memo) || '-'
  }
}
 
export function normalizeOutBoundBasketRows(rows = []) {
  if (!Array.isArray(rows)) {
    return []
  }
 
  const seen = new Set()
  return rows
    .map((row) => ({
      ...normalizeOutBoundRow(row),
      outQty: normalizeItemOutQty(row)
    }))
    .filter((row) => {
      const key = row.id ?? `${row.locId || ''}-${row.matnrCode || ''}-${row.batch || ''}`
      if (seen.has(key)) {
        return false
      }
      seen.add(key)
      return true
    })
}
 
export function mergeOutBoundBasketRows(currentRows = [], incomingRows = []) {
  const basket = new Map()
 
  normalizeOutBoundBasketRows(currentRows).forEach((row) => {
    const key = row.id ?? `${row.locId || ''}-${row.matnrCode || ''}-${row.batch || ''}`
    basket.set(key, row)
  })
 
  normalizeOutBoundBasketRows(incomingRows).forEach((row) => {
    const key = row.id ?? `${row.locId || ''}-${row.matnrCode || ''}-${row.batch || ''}`
    if (!basket.has(key)) {
      basket.set(key, row)
      return
    }
    basket.set(key, {
      ...basket.get(key),
      ...row,
      outQty: basket.get(key).outQty > 0 ? basket.get(key).outQty : row.outQty
    })
  })
 
  return Array.from(basket.values())
}
 
export function buildOutBoundGenerateTaskPayload({ siteNo, memo = '', items = [] } = {}) {
  return {
    siteNo: normalizeOptionalText(siteNo),
    memo: normalizeText(memo),
    items: Array.isArray(items)
      ? items.map((row) =>
          compactDefinedFields({
            ...row,
            id: toNumberOrOriginal(row.id),
            locId: toNumberOrOriginal(row.locId),
            orderId: toNumberOrOriginal(row.orderId),
            orderItemId: toNumberOrOriginal(row.orderItemId),
            wkType: toNumberOrOriginal(row.wkType),
            matnrId: toNumberOrOriginal(row.matnrId),
            channel: toNumberOrOriginal(row.channel),
            status: toNumberOrOriginal(row.status),
            outQty: normalizeItemOutQty(row),
            anfme: normalizeNumber(row.anfme),
            workQty: normalizeNumber(row.workQty),
            qty: normalizeNumber(row.qty),
            siteNo: normalizeOptionalText(row.siteNo),
            memo: normalizeOptionalText(row.memo)
          })
        )
      : []
  }
}
 
export function normalizeOutBoundSiteOptions(records = []) {
  if (!Array.isArray(records)) {
    return []
  }
 
  const seen = new Set()
  return records
    .map((record) => normalizeText(record.site))
    .filter((site) => {
      if (!site || seen.has(site)) {
        return false
      }
      seen.add(site)
      return true
    })
    .map((site) => ({
      label: site,
      value: site
    }))
}
 
export function getOutBoundValidationMessage({ siteNo, items = [] } = {}) {
  if (!normalizeText(siteNo)) {
    return '请选择出库站点'
  }
  if (!Array.isArray(items) || items.length === 0) {
    return '请先选择库存明细'
  }
 
  const invalidRow = items.find((row) => normalizeItemOutQty(row) <= 0)
  if (invalidRow) {
    return `库存明细 ${invalidRow.locCode || invalidRow.matnrCode || invalidRow.id || ''} 的出库数量必须大于 0`
  }
 
  const overflowRow = items.find((row) => normalizeItemOutQty(row) + normalizeNumber(row.workQty) > normalizeNumber(row.anfme))
  if (overflowRow) {
    return `库存明细 ${overflowRow.locCode || overflowRow.matnrCode || overflowRow.id || ''} 的出库数量不能超过可用数量`
  }
 
  return ''
}