reconciliationIncrementalRules.js
2.34 KB
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
import { GOODS_TYPE_GROUP_RULE_OP } from "../../financialRules/utils/goodsTypeGroupRuleOpType"
/**
* 对账规则行上可能存在的服务端主键(与 reconciliation.getEffectRule / saveRule 约定一致)。
* @param {object|null|undefined} rule
* @returns {string}
*/
export function getStableReconciliationRuleId(rule) {
if (!rule || typeof rule !== "object") return ""
const a = rule.groupDivisionRuleId
const b = rule.id
const s = a != null && String(a).trim() !== "" ? String(a).trim() : ""
if (s) return s
return b != null && String(b).trim() !== "" ? String(b).trim() : ""
}
/**
* 将缓存中的主键合并进本次校验通过的快照,便于增量 UPDATE 判型。
* @param {object} snapshot - handleConfirm / getBackendFormat 产物
* @param {object|null|undefined} cachedRule - goodsTypeGroupReconciliationRules 槽位
* @returns {object}
*/
export function mergeReconciliationRuleServerFields(snapshot, cachedRule) {
if (!snapshot || typeof snapshot !== "object") return snapshot
if (!cachedRule || typeof cachedRule !== "object") return snapshot
const out = { ...snapshot }
if (
cachedRule.groupDivisionRuleId != null
&& String(cachedRule.groupDivisionRuleId).trim() !== ""
) {
out.groupDivisionRuleId = cachedRule.groupDivisionRuleId
}
if (cachedRule.id != null && String(cachedRule.id).trim() !== "") {
out.id = cachedRule.id
}
if (
cachedRule.groupSettleRuleId != null
&& String(cachedRule.groupSettleRuleId).trim() !== ""
) {
out.groupSettleRuleId = cachedRule.groupSettleRuleId
}
return out
}
/**
* 与财务 buildGoodsTypeGroupIncrementalRules 同款语义:仅「本轮已校验」的 Tab 进入增量数组。
* @param {Record<number, object>} validatedByTabIndex
* @param {number[]} touchedIndices
* @returns {Array<object>}
*/
export function buildReconciliationIncrementalRules(
validatedByTabIndex,
touchedIndices
) {
const incrementalRules = []
for (let j = 0; j < touchedIndices.length; j++) {
const idx = touchedIndices[j]
const rule = validatedByTabIndex[idx]
if (!rule) continue
const idStr = getStableReconciliationRuleId(rule)
const type = idStr
? GOODS_TYPE_GROUP_RULE_OP.UPDATE
: GOODS_TYPE_GROUP_RULE_OP.ADD
incrementalRules.push({
...rule,
type,
tabPaneIndex: idx
})
}
return incrementalRules
}