gm-printer 10.37.2 → 11.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gm-printer",
3
- "version": "10.37.2",
3
+ "version": "11.0.0",
4
4
  "description": "diy printer",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -72,15 +72,13 @@ class EditorTitle extends React.Component {
72
72
  }
73
73
 
74
74
  handleTestPrint = () => {
75
- // 新增结款单每页合计标识,测试打印时原样透传到打印入口
76
- const { mockData, editStore, isSomeSubtotalTr } = this.props
75
+ const { mockData, editStore } = this.props
77
76
  doPrint(
78
77
  {
79
78
  config: toJS(editStore.config),
80
79
  data: mockData
81
80
  },
82
- true,
83
- isSomeSubtotalTr
81
+ true
84
82
  )
85
83
  }
86
84
 
@@ -142,14 +140,10 @@ class EditorTitle extends React.Component {
142
140
  EditorTitle.propTypes = {
143
141
  onSave: PropTypes.func.isRequired,
144
142
  isPurchase: PropTypes.bool,
145
- editStore: PropTypes.object,
146
- // 标识是否为结款单每页合计场景
147
- isSomeSubtotalTr: PropTypes.bool
143
+ editStore: PropTypes.object
148
144
  }
149
145
  EditorTitle.defaultProps = {
150
- isPurchase: false,
151
- // 默认按非结款单每页合计场景处理
152
- isSomeSubtotalTr: false
146
+ isPurchase: false
153
147
  }
154
148
 
155
149
  EditorTitle.wrappedComponent.propTypes = {
@@ -1,6 +1,4 @@
1
1
  import withShadowDom from '../common/hoc_with_shadow_dom'
2
2
  import Editor from './editor'
3
- import Editor22 from './editor2'
4
3
 
5
4
  export default withShadowDom(Editor)
6
- export const Editor2 = withShadowDom(Editor22)
@@ -42,8 +42,7 @@ class Editor extends React.Component {
42
42
 
43
43
  {showEditor && (
44
44
  <div className='gm-printer-edit-zone'>
45
- {/* 透传结款单每页合计标识给标题区,用于测试打印时走对应逻辑 */}
46
- <EditorTitle onSave={onSave} isSomeSubtotalTr={isSomeSubtotalTr} />
45
+ <EditorTitle onSave={onSave} />
47
46
  <Gap height='10px' />
48
47
  <EditorSelect />
49
48
  <Gap height='5px' />
package/src/index.js CHANGED
@@ -1,7 +1,6 @@
1
1
  // ‼️优先初始化语言设置(必须先初始化语言)
2
2
  import '../locales'
3
- import Editor, { Editor2 } from './editor'
4
-
3
+ import Editor from './editor'
5
4
  import EditorPurchase from './editor_purchase'
6
5
  import EditorStockIn from './editor_stockin'
7
6
  import EditorStockOut from './editor_stockout'
@@ -28,7 +27,6 @@ export { setLocale } from '../locales'
28
27
 
29
28
  export {
30
29
  Editor,
31
- Editor2,
32
30
  EditorStockIn,
33
31
  EditorStockOut,
34
32
  EditorPurchase,
@@ -1,6 +1,11 @@
1
1
  import React from 'react'
2
2
  import _ from 'lodash'
3
- import { getDataKey, isMultiTable } from '../util'
3
+ import {
4
+ getDataKey,
5
+ isMultiTable,
6
+ formatSumWithPrecision,
7
+ getHighPrecisionField
8
+ } from '../util'
4
9
  import Big from 'big.js'
5
10
  import { observer } from 'mobx-react'
6
11
  import { get } from 'mobx'
@@ -16,17 +21,27 @@ const regExp = text => {
16
21
  * @param key
17
22
  * @param dataList
18
23
  * @param summaryFieldsResultToNumber 需要转换为数字的汇总字段
24
+ * @param formatter 精度格式化函数,由 stationv2 传入 AmountPrecisionUtil(precision_control)
25
+ * @param highPrecisionMapping 高精度字段映射,优先用高精度字段求和
19
26
  * @returns {*|string}
20
27
  */
21
- const sumCol = (key, dataList, summaryFieldsResultToNumber = []) => {
28
+ const sumCol = (
29
+ key,
30
+ dataList,
31
+ summaryFieldsResultToNumber = [],
32
+ formatter,
33
+ highPrecisionMapping
34
+ ) => {
22
35
  let result
23
36
  try {
24
- result = dataList
25
- .reduce((acc, item) => {
26
- acc = acc.plus(item[key] || 0)
27
- return acc
28
- }, Big(0))
29
- .toFixed(2)
37
+ // 优先使用高精度字段求和
38
+ const sumKey = getHighPrecisionField(key, highPrecisionMapping)
39
+ const sum = dataList.reduce((acc, item) => {
40
+ const val = item[sumKey] ?? item._origin?.[sumKey] ?? 0
41
+ acc = acc.plus(val)
42
+ return acc
43
+ }, Big(0))
44
+ result = formatSumWithPrecision(sum, formatter)
30
45
 
31
46
  if (summaryFieldsResultToNumber.includes(key)) {
32
47
  result = Number(result)
@@ -66,6 +81,8 @@ const PageSummary = props => {
66
81
  regExp(item)
67
82
  )
68
83
  }
84
+ const formatter = printerStore.config?.payAmountFormatter
85
+ const highPrecisionMapping = printerStore.config?.highPrecisionFieldMapping
69
86
  return (
70
87
  <tr>
71
88
  {_.map(columns, (col, index) => {
@@ -79,7 +96,13 @@ const PageSummary = props => {
79
96
  summaryConfig.summaryColumns
80
97
  .map(text => regExp(text))
81
98
  .includes(key) && key
82
- ? sumCol(key, currentPageTableData, summaryFieldsResultToNumber)
99
+ ? sumCol(
100
+ key,
101
+ currentPageTableData,
102
+ summaryFieldsResultToNumber,
103
+ formatter,
104
+ highPrecisionMapping
105
+ )
83
106
  : ' '
84
107
  }
85
108
 
@@ -1,5 +1,5 @@
1
1
  import i18next from '../../locales'
2
- import { action, observable, computed } from 'mobx'
2
+ import { action, observable, computed, toJS } from 'mobx'
3
3
  import {
4
4
  getSumTrHeight,
5
5
  isMultiTable,
@@ -530,7 +530,7 @@ class PrinterStore {
530
530
  })({
531
531
  ...this.data.common,
532
532
  [i18next.t('当前页码')]: pageIndex + 1,
533
- [i18next.t('页码总数')]: Math.max(1, this.pages.length),
533
+ [i18next.t('页码总数')]: this.pages.length,
534
534
  price: price
535
535
  })
536
536
  } catch (err) {
@@ -604,7 +604,7 @@ class PrinterStore {
604
604
 
605
605
  [i18next.t('列')]: list[index],
606
606
  [i18next.t('当前页码')]: pageIndex + 1,
607
- [i18next.t('页码总数')]: Math.max(1, this.pages.length),
607
+ [i18next.t('页码总数')]: this.pages.length,
608
608
  price: price // 提供一个价格处理函数
609
609
  })
610
610
  } catch (err) {
@@ -626,7 +626,7 @@ class PrinterStore {
626
626
  ...this.data.common,
627
627
  [i18next.t('列')]: list[index],
628
628
  [i18next.t('当前页码')]: pageIndex + 1,
629
- [i18next.t('页码总数')]: Math.max(1, this.pages.length),
629
+ [i18next.t('页码总数')]: this.pages.length,
630
630
  price: price // 提供一个价格处理函数
631
631
  })
632
632
  res =
@@ -2,7 +2,11 @@ import React from 'react'
2
2
  import PropTypes from 'prop-types'
3
3
  import _ from 'lodash'
4
4
  // import Big from 'big.js'
5
- import { coverDigit2Uppercase, getDataKey } from '../util'
5
+ import {
6
+ coverDigit2Uppercase,
7
+ getDataKey,
8
+ formatSumWithPrecision
9
+ } from '../util'
6
10
  import { observer } from 'mobx-react'
7
11
  import classNames from 'classnames'
8
12
  import Big from 'big.js'
@@ -15,20 +19,30 @@ const DiyOverallOrder = props => {
15
19
  } = props
16
20
  const tableData = printerStore.data._table[getDataKey(dataKey)] || []
17
21
 
22
+ const formatter = printerStore.config?.payAmountFormatter
23
+ const highPrecisionMapping = printerStore.config?.highPrecisionFieldMapping
24
+
18
25
  // 计算合计
19
26
  const sumData = field => {
20
- return _.reduce(
27
+ // 查找高精度字段,有则直接从原始数据取值,避免 templateTable 的精度损失
28
+ const hpField = highPrecisionMapping?.[field]
29
+ const sum = _.reduce(
21
30
  tableData,
22
31
  (a, b, i) => {
23
32
  let result = a
24
- const bRes = printerStore
25
- .templateTable(field, dataKey, i, pageIndex)
26
- .replace(/\(\)/g, '')
27
- result = a.plus(+bRes || 0)
33
+ if (hpField) {
34
+ result = a.plus(b._origin?.[hpField] ?? b[hpField] ?? 0)
35
+ } else {
36
+ const bRes = printerStore
37
+ .templateTable(field, dataKey, i, pageIndex)
38
+ .replace(/\(\)/g, '')
39
+ result = a.plus(+bRes || 0)
40
+ }
28
41
  return result
29
42
  },
30
43
  Big(0)
31
- ).toFixed(2)
44
+ )
45
+ return formatSumWithPrecision(sum, formatter)
32
46
  }
33
47
 
34
48
  if (!diyOverallOrder?.show || !printerStore?.ready) {
@@ -1,7 +1,12 @@
1
1
  import React from 'react'
2
2
  import PropTypes from 'prop-types'
3
3
  import _ from 'lodash'
4
- import { coverDigit2Uppercase, getDataKey, isMultiTable } from '../util'
4
+ import {
5
+ coverDigit2Uppercase,
6
+ getDataKey,
7
+ isMultiTable,
8
+ formatSumWithPrecision
9
+ } from '../util'
5
10
  import { MULTI_SUFFIX, MULTI_SUFFIX3 } from '../config'
6
11
  import { observer } from 'mobx-react'
7
12
  import classNames from 'classnames'
@@ -43,14 +48,20 @@ const DiySummary = props => {
43
48
  // 判断是否是多栏表格
44
49
  const isMulti = isMultiTable(dataKey)
45
50
 
51
+ const formatter = printerStore.config?.payAmountFormatter
52
+ const highPrecisionMapping = printerStore.config?.highPrecisionFieldMapping
53
+
46
54
  // 计算合计
47
55
  const sumData = field => {
56
+ // 查找高精度字段,有则直接从原始数据取值,避免 templateTable 的精度损失
57
+ const hpField = highPrecisionMapping?.[field]
58
+
48
59
  if (isMulti) {
49
- return _.reduce(
60
+ // 多栏表格暂不使用高精度字段,保持原有 templateTable 逻辑
61
+ const sum = _.reduce(
50
62
  tableData,
51
63
  (a, b, i) => {
52
64
  let result = a
53
- // 先通过 templateTable 获取原始字段的值
54
65
  const bRes = getRes(field, i)
55
66
  result = a.plus(+bRes || 0)
56
67
 
@@ -65,10 +76,24 @@ const DiySummary = props => {
65
76
  return result
66
77
  },
67
78
  Big(0)
68
- ).toFixed(2)
79
+ )
80
+ return formatSumWithPrecision(sum, formatter)
81
+ }
82
+
83
+ // 单栏:有高精度字段则直接从原始数据取值
84
+ if (hpField) {
85
+ const sum = _.reduce(
86
+ tableData,
87
+ (a, b, i) => {
88
+ return a.plus(b._origin?.[hpField] ?? b[hpField] ?? 0)
89
+ },
90
+ Big(0)
91
+ )
92
+ return formatSumWithPrecision(sum, formatter)
69
93
  }
70
94
 
71
- return _.reduce(
95
+ // 兜底:无高精度字段,使用 templateTable
96
+ const sum = _.reduce(
72
97
  tableData,
73
98
  (a, b, i) => {
74
99
  let result = a
@@ -77,7 +102,8 @@ const DiySummary = props => {
77
102
  return result
78
103
  },
79
104
  Big(0)
80
- ).toFixed(2)
105
+ )
106
+ return formatSumWithPrecision(sum, formatter)
81
107
 
82
108
  function getRes(field, i) {
83
109
  return printerStore
@@ -4,7 +4,11 @@ import PropTypes from 'prop-types'
4
4
  import _ from 'lodash'
5
5
  import { MULTI_SUFFIX, MULTI_SUFFIX3 } from '../config'
6
6
  import Big from 'big.js'
7
- import { coverDigit2Uppercase, getDataKey } from '../util'
7
+ import {
8
+ coverDigit2Uppercase,
9
+ getDataKey,
10
+ formatSumWithPrecision
11
+ } from '../util'
8
12
  import { observer } from 'mobx-react'
9
13
  import { get } from 'mobx'
10
14
  import classNames from 'classnames'
@@ -45,58 +49,47 @@ const SubtotalTr = props => {
45
49
  isSomeSubtotalTr
46
50
  } = props
47
51
  const tableData = printerStore.data._table[getDataKey(dataKey, arrange)] || []
48
- // 计算合计
49
- const sumData = (list, field) => {
50
- return _.reduce(
51
- list,
52
- (a, b) => {
53
- let result = a
54
-
55
- const _origin = b._origin || {}
56
- const _origin2 = b['_origin' + MULTI_SUFFIX] || {}
57
- const _origin3 = b['_origin' + MULTI_SUFFIX3] || {}
58
-
59
- result = a.plus(_origin[field] || 0)
60
- if (_origin2[field]) {
61
- result = result.plus(_origin2[field])
62
- }
63
- if (_origin3[field]) {
64
- result = result.plus(_origin3[field])
65
- }
66
- return result
67
- },
68
- Big(0)
69
- ).toFixed(2)
70
- }
71
52
 
72
53
  // 计算合计
73
- const sumData2 = (list, field) => {
54
+ const sumData2 = (list, field, formatter, highPrecisionMapping) => {
55
+ // 在字段名转换前,查找是否有高精度字段
56
+ const hpField = highPrecisionMapping?.[field]
57
+
74
58
  // 兼容之前
75
59
  if (field === 'total_item_price') field = '下单金额'
76
60
  if (field === 'real_item_price') field = '出库金额'
77
61
  if (field === 'settle_money2') field = '结算金额'
78
62
  if (field === 'total_money2') field = '金额'
79
- return _.reduce(
63
+
64
+ const sum = _.reduce(
80
65
  list,
81
66
  (a, b) => {
82
67
  let result = a
83
- result = a.plus(b[field] || 0)
84
- if (b?.[field + MULTI_SUFFIX]) {
68
+ // 优先使用高精度字段(从 _origin 中取原始数据)
69
+ if (hpField) {
70
+ result = a.plus(b._origin?.[hpField] ?? b[hpField] ?? 0)
71
+ } else {
72
+ result = a.plus(b[field] || 0)
73
+ }
74
+ if (!hpField && b?.[field + MULTI_SUFFIX]) {
85
75
  result = result.plus(b?.[field + MULTI_SUFFIX])
86
76
  }
87
- if (b?.[field + MULTI_SUFFIX3]) {
77
+ if (!hpField && b?.[field + MULTI_SUFFIX3]) {
88
78
  result = result.plus(b?.[field + MULTI_SUFFIX3])
89
79
  }
90
80
  return result
91
81
  },
92
82
  Big(0)
93
- ).toFixed(2)
83
+ )
84
+ return formatSumWithPrecision(sum, formatter)
94
85
  }
95
86
 
96
87
  const getData = field => {
97
88
  const data = printerStore.data.common
98
89
  return data?.[field] ?? ''
99
90
  }
91
+ const formatter = printerStore.config?.payAmountFormatter
92
+ const highPrecisionMapping = printerStore.config?.highPrecisionFieldMapping
100
93
  // 每页小计
101
94
  // show 是否展示小计,fields<Array> 合计的字段和展现的name,displayName 是否显示名字
102
95
  if (show && printerStore.ready) {
@@ -107,7 +100,12 @@ const SubtotalTr = props => {
107
100
  const sum = {}
108
101
 
109
102
  _.each(fields, v => {
110
- sum[v.name] = sumData2(list, v.valueField)
103
+ sum[v.name] = sumData2(
104
+ list,
105
+ v.valueField,
106
+ formatter,
107
+ highPrecisionMapping
108
+ )
111
109
  })
112
110
  let subtotalStr = ''
113
111
  for (const name in sum) {
@@ -165,9 +163,14 @@ const SubtotalTr = props => {
165
163
  {item.type === 'useSummarize'
166
164
  ? getData(item.valueField)
167
165
  : index === 0
168
- ? sumData2(list, item.valueField)
166
+ ? sumData2(
167
+ list,
168
+ item.valueField,
169
+ formatter,
170
+ highPrecisionMapping
171
+ )
169
172
  : ''}
170
- {/* {index === 0 ? sumData(list, item.valueField) : ''} */}
173
+ {/* {index === 0 ? sumData(list, item.valueField, formatter) : ''} */}
171
174
  </span>
172
175
  {subtotal?.needUpperCase && ( // 是否需要大写金额
173
176
  <span>
@@ -177,7 +180,12 @@ const SubtotalTr = props => {
177
180
  : index === 0
178
181
  ? '大写:' +
179
182
  coverDigit2Uppercase(
180
- sumData2(list, item.valueField)
183
+ sumData2(
184
+ list,
185
+ item.valueField,
186
+ formatter,
187
+ highPrecisionMapping
188
+ )
181
189
  )
182
190
  : ''}
183
191
  </span>
package/src/util.js CHANGED
@@ -353,6 +353,27 @@ const collectGroups = (tableData, range, key = '_collect') => {
353
353
  }
354
354
  }
355
355
 
356
+ /**
357
+ * 使用精度格式化函数格式化 Big.js 求和结果,无格式化函数时兜底 toFixed(2)
358
+ * @param {Big} sum - Big.js 实例
359
+ * @param {Function} [formatter] - 可选的精度格式化函数
360
+ * @returns {string}
361
+ */
362
+ const formatSumWithPrecision = (sum, formatter) => {
363
+ return formatter ? formatter(sum).toFixed() : sum.toFixed(2)
364
+ }
365
+
366
+ /**
367
+ * 根据高精度字段映射,获取求和时应使用的字段名
368
+ * 如果字段有对应的高精度版本,返回高精度字段名,否则返回原字段名
369
+ * @param {string} field - 原始字段名
370
+ * @param {Object} mapping - 高精度字段映射,格式:{ normalField: 'high_precision_field' }
371
+ * @returns {string}
372
+ */
373
+ const getHighPrecisionField = (field, mapping) => {
374
+ return mapping?.[field] || field
375
+ }
376
+
356
377
  export {
357
378
  collectGroups,
358
379
  getHeight,
@@ -373,5 +394,7 @@ export {
373
394
  caclSingleDetailsPageHeight,
374
395
  getArrayMid,
375
396
  getColSpanLength,
376
- getOverallOrderTrHeight
397
+ getOverallOrderTrHeight,
398
+ formatSumWithPrecision,
399
+ getHighPrecisionField
377
400
  }