gm-printer 10.34.0 → 10.35.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.
@@ -31,47 +31,118 @@ const DiyOverallOrder = props => {
31
31
  ).toFixed(2)
32
32
  }
33
33
 
34
+ if (!diyOverallOrder?.show || !printerStore?.ready) {
35
+ return null
36
+ }
37
+
38
+ const leftField = diyOverallOrder.fields?.[0]
39
+ if (!leftField) return null
40
+ const rightName = leftField.rightName || ''
41
+
42
+ // 左右显示分离
43
+ const isUpperLowerCaseSeparate = diyOverallOrder?.isUpperLowerCaseSeparate
44
+ // 大写金额在前
45
+ const isUpperCaseBefore = diyOverallOrder?.isUpperCaseBefore
46
+ // 大写金额
47
+ const needUpperCase = diyOverallOrder?.needUpperCase
48
+ // 小写金额
49
+ const numericValue = sumData(leftField.valueField)
50
+ // 大写金额
51
+ const upperCaseValue = needUpperCase ? coverDigit2Uppercase(numericValue) : ''
52
+
53
+ const leftText = () => {
54
+ if (isUpperLowerCaseSeparate) {
55
+ // 大写金额在前
56
+ if (isUpperCaseBefore) {
57
+ return upperCaseValue
58
+ }
59
+ // 小写金额在前
60
+ return numericValue
61
+ }
62
+
63
+ // 大写金额在前且需要大写金额
64
+ if (isUpperCaseBefore) {
65
+ return `${upperCaseValue} ${numericValue}`
66
+ }
67
+
68
+ if (needUpperCase) {
69
+ return `${numericValue} ${upperCaseValue}`
70
+ }
71
+
72
+ return numericValue
73
+ }
74
+
75
+ const rightText = () => {
76
+ if (isUpperLowerCaseSeparate) {
77
+ if (isUpperCaseBefore) {
78
+ return `${rightName} ${numericValue}`
79
+ }
80
+ return `${rightName} ${upperCaseValue}`
81
+ }
82
+
83
+ return ''
84
+ }
34
85
  return (
35
- diyOverallOrder?.show &&
36
- printerStore?.ready && (
37
- <tr>
38
- {_.map(diyOverallOrder.fields, (item, index) => {
39
- return (
40
- <td colSpan={item.colSpan ?? 99} key={index}>
41
- <div style={{ ...item.style }} className='gm-flex-page'>
42
- {item.name}
43
- <div
44
- className={classNames('gm-flex-page', {
45
- 'gm-flex-justify-between-page':
46
- diyOverallOrder?.isUpperLowerCaseSeparate,
47
- 'gm-flex-grow-page':
48
- diyOverallOrder?.isUpperLowerCaseSeparate
49
- })}
50
- >
51
- <span
52
- className={
53
- diyOverallOrder?.isUpperCaseBefore
54
- ? 'gm-printer-subtotal-isUpperCaseBefore-inter'
55
- : ''
56
- }
57
- >
58
- {sumData(item.valueField)}
59
- </span>
60
- {diyOverallOrder?.needUpperCase && (
61
- <span>
62
- {coverDigit2Uppercase(sumData(item.valueField))
63
- ? coverDigit2Uppercase(sumData(item.valueField))
64
- : ''}
65
- </span>
66
- )}
67
- </div>
68
- </div>
69
- </td>
70
- )
71
- })}
72
- </tr>
73
- )
86
+ <tr>
87
+ <td colSpan={99}>
88
+ <div style={{ ...leftField.style }} className='gm-flex-page'>
89
+ {leftField.name}
90
+ <div
91
+ className={classNames('gm-flex-page', {
92
+ 'gm-flex-justify-between-page': isUpperLowerCaseSeparate,
93
+ 'gm-flex-grow-page': isUpperLowerCaseSeparate
94
+ })}
95
+ >
96
+ {/* 左侧金额 */}
97
+ <span>{leftText()}</span>
98
+
99
+ <span>{rightText()}</span>
100
+ </div>
101
+ </div>
102
+ </td>
103
+ </tr>
74
104
  )
105
+ // return (
106
+ // diyOverallOrder?.show &&
107
+ // printerStore?.ready && (
108
+ // <tr>
109
+ // {_.map(diyOverallOrder.fields, (item, index) => {
110
+ // return (
111
+ // <td colSpan={item.colSpan ?? 99} key={index}>
112
+ // <div style={{ ...item.style }} className='gm-flex-page'>
113
+ // {item.name}
114
+ // <div
115
+ // className={classNames('gm-flex-page', {
116
+ // 'gm-flex-justify-between-page':
117
+ // diyOverallOrder?.isUpperLowerCaseSeparate,
118
+ // 'gm-flex-grow-page':
119
+ // diyOverallOrder?.isUpperLowerCaseSeparate
120
+ // })}
121
+ // >
122
+ // <span
123
+ // className={
124
+ // diyOverallOrder?.isUpperCaseBefore
125
+ // ? 'gm-printer-subtotal-isUpperCaseBefore-inter'
126
+ // : ''
127
+ // }
128
+ // >
129
+ // {sumData(item.valueField)}
130
+ // </span>
131
+ // {diyOverallOrder?.needUpperCase && (
132
+ // <span>
133
+ // {coverDigit2Uppercase(sumData(item.valueField))
134
+ // ? coverDigit2Uppercase(sumData(item.valueField))
135
+ // : ''}
136
+ // </span>
137
+ // )}
138
+ // </div>
139
+ // </div>
140
+ // </td>
141
+ // )
142
+ // })}
143
+ // </tr>
144
+ // )
145
+ // )
75
146
  }
76
147
  DiyOverallOrder.propTypes = {
77
148
  config: PropTypes.object.isRequired,
@@ -0,0 +1,127 @@
1
+ import React from 'react'
2
+ import PropTypes from 'prop-types'
3
+ import _ from 'lodash'
4
+ import { coverDigit2Uppercase, getDataKey } from '../util'
5
+ import { observer } from 'mobx-react'
6
+ import classNames from 'classnames'
7
+ import Big from 'big.js'
8
+
9
+ /**
10
+ * 通用的自定义合计组件
11
+ * @param {string} configKey - 配置键名
12
+ * @param {Function} getTableData - 获取表格数据的函数,用于区分整单合计和每页合计
13
+ */
14
+ const DiySummary = props => {
15
+ const {
16
+ configKey,
17
+ config: { dataKey, arrange },
18
+ printerStore,
19
+ range,
20
+ pageIndex,
21
+ getTableData
22
+ } = props
23
+
24
+ const diyConfig = props.config[configKey]
25
+
26
+ // 根据 configKey 决定如何获取数据
27
+ const tableData = getTableData
28
+ ? getTableData(printerStore, dataKey, arrange, range)
29
+ : printerStore.data._table[getDataKey(dataKey, arrange)] || []
30
+
31
+ // 计算合计
32
+ const sumData = field => {
33
+ return _.reduce(
34
+ tableData,
35
+ (a, b, i) => {
36
+ let result = a
37
+ const bRes = printerStore
38
+ .templateTable(field, dataKey, range ? range.begin + i : i, pageIndex)
39
+ .replace(/\(\)/g, '')
40
+ result = a.plus(+bRes || 0)
41
+ return result
42
+ },
43
+ Big(0)
44
+ ).toFixed(2)
45
+ }
46
+
47
+ if (!diyConfig?.show || !printerStore?.ready) {
48
+ return null
49
+ }
50
+
51
+ const leftField = diyConfig.fields?.[0]
52
+ if (!leftField) {
53
+ return null
54
+ }
55
+
56
+ const rightName = leftField.rightName || ''
57
+ const isUpperLowerCaseSeparate = diyConfig?.isUpperLowerCaseSeparate
58
+ const isUpperCaseBefore = diyConfig?.isUpperCaseBefore
59
+ const needUpperCase = diyConfig?.needUpperCase
60
+ const numericValue = sumData(leftField.valueField)
61
+ const upperCaseValue = needUpperCase ? coverDigit2Uppercase(numericValue) : ''
62
+
63
+ const leftText = () => {
64
+ if (isUpperLowerCaseSeparate) {
65
+ // 大写金额在前
66
+ if (isUpperCaseBefore) {
67
+ return upperCaseValue
68
+ }
69
+ // 小写金额在前
70
+ return numericValue
71
+ }
72
+
73
+ // 大写金额在前且需要大写金额
74
+ if (isUpperCaseBefore) {
75
+ return `${upperCaseValue} ${numericValue}`
76
+ }
77
+
78
+ if (needUpperCase) {
79
+ return `${numericValue} ${upperCaseValue}`
80
+ }
81
+
82
+ return numericValue
83
+ }
84
+
85
+ const rightText = () => {
86
+ if (isUpperLowerCaseSeparate) {
87
+ if (isUpperCaseBefore) {
88
+ return `${rightName} ${numericValue}`
89
+ }
90
+ return `${rightName} ${upperCaseValue}`
91
+ }
92
+
93
+ return ''
94
+ }
95
+
96
+ return (
97
+ <tr>
98
+ <td colSpan={99}>
99
+ <div style={{ ...leftField.style }} className='gm-flex-page'>
100
+ {leftField.name}
101
+ <div
102
+ className={classNames('gm-flex-page', {
103
+ 'gm-flex-justify-between-page': isUpperLowerCaseSeparate,
104
+ 'gm-flex-grow-page': isUpperLowerCaseSeparate
105
+ })}
106
+ >
107
+ {/* 左侧金额 */}
108
+ <span>{leftText()}</span>
109
+
110
+ <span>{rightText()}</span>
111
+ </div>
112
+ </div>
113
+ </td>
114
+ </tr>
115
+ )
116
+ }
117
+
118
+ DiySummary.propTypes = {
119
+ configKey: PropTypes.string.isRequired,
120
+ config: PropTypes.object.isRequired,
121
+ printerStore: PropTypes.object,
122
+ range: PropTypes.object,
123
+ pageIndex: PropTypes.number,
124
+ getTableData: PropTypes.func
125
+ }
126
+
127
+ export default observer(DiySummary)
package/src/util.js CHANGED
@@ -293,7 +293,49 @@ const getColSpanLength = table => {
293
293
  : table.columns.length
294
294
  }
295
295
 
296
+ /**
297
+ * 收集分类小计的组
298
+ * @param {*} tableData
299
+ * @param {*} range
300
+ * @param {*} key
301
+ * @returns
302
+ */
303
+ const collectGroups = (tableData, range, key = '_collect') => {
304
+ const groups = []
305
+ let currentGroup = []
306
+ let currentStart = range.begin
307
+
308
+ for (let idx = range.begin; idx < range.end; idx++) {
309
+ const row = tableData[idx]
310
+ const isSpecial = row && row[key]
311
+
312
+ if (isSpecial) {
313
+ if (currentGroup.length) {
314
+ groups.push({
315
+ groupKey: extractCategoryName(row[key]?.text),
316
+ begin: currentStart,
317
+ end: idx,
318
+ specialIndex: idx
319
+ })
320
+ currentGroup = []
321
+ }
322
+ currentStart = idx + 1
323
+ } else if (row && Object.keys(row).length) {
324
+ currentGroup.push(row)
325
+ }
326
+ }
327
+
328
+ return groups
329
+
330
+ function extractCategoryName(text) {
331
+ if (!text) return ''
332
+ const match = text.match(/^(.*?)[::]/)
333
+ return match ? match[1] : ''
334
+ }
335
+ }
336
+
296
337
  export {
338
+ collectGroups,
297
339
  getHeight,
298
340
  getWidth,
299
341
  pxAdd,