king
2024-04-28 2e5fe5427d6db393e0495598ff43d90a052f4791
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { Input, InputNumber, notification, Modal } from 'antd'
import { EllipsisOutlined } from '@ant-design/icons'
 
import Utils from '@/utils/utils.js'
import MKEmitter from '@/utils/events.js'
import SubTable from '../subTable'
import './index.scss'
 
class DetailLine extends React.Component {
  state = {
    bill_count: 0,
    unitprice: 0,
    amount_line: 0,
    visible: false
  }
 
  UNSAFE_componentWillMount() {
    let { line } = this.props
 
    this.setState({
      bill_count: line.bill_count || '',
      unitprice: line.unitprice || '',
      amount_line: line.amount_line || ''
    })
  }
 
  onChange = (value, key) => {
    let line = {...this.props.line}
 
    if (['bill_count', 'unitprice', 'amount_line'].includes(key)) {
      line[key] = value || 0
      if (isNaN(line[key])) {
        line[key] = 0
      }
      if (line[key]) {
        if (key === 'bill_count') {
          line[key] = Math.round(line[key] * 10000000000) / 10000000000
          if (line.unitprice) {
            line.amount_line = Math.round(line.unitprice * line.bill_count * 100) / 100
          }
        } else if (key === 'unitprice') {
          line[key] = Math.round(line[key] * 10000000000) / 10000000000
          if (line.bill_count) {
            line.amount_line = Math.round(line.unitprice * line.bill_count * 100) / 100
          }
        } else if (key === 'amount_line') {
          line[key] = Math.round(line[key] * 100) / 100
          if (line.bill_count) {
            line.unitprice = Math.round(line.amount_line / line.bill_count * 10000000000) / 10000000000
          } else if (line.unitprice) {
            line.bill_count = Math.round(line.amount_line / line.unitprice * 10000000000) / 10000000000
          }
        }
      } else if (key === 'amount_line') {
        line.bill_count = 0
      }
 
      if (line.amount_line) {
        line.tax_amount = line.amount_line - line.amount_line / (line.tax_rate + 1)
        line.tax_amount = Math.round(line.tax_amount * 100) / 100
      } else {
        line.tax_amount = 0
      }
    } else {
      line[key] = value
    }
 
    this.setState({
      bill_count: line.bill_count || '',
      unitprice: line.unitprice || '',
      amount_line: line.amount_line || ''
    })
 
    this.props.changeLine(line, key)
  }
 
  render() {
    const { line, delLine, trigger } = this.props
    const { bill_count, unitprice, amount_line } = this.state
    
    return <div className="mk-tr active">
      <div className="mk-td">
        <div className="mk-input">{line.productname || ''}<EllipsisOutlined onClick={trigger}/></div>
      </div>
      <div className="mk-td">
        <Input defaultValue={line.spec || ''} onChange={(e) => this.onChange(e.target.value, 'spec')}/>
      </div>
      <div className="mk-td">
        <Input defaultValue={line.unit || ''} onChange={(e) => this.onChange(e.target.value, 'unit')}/>
      </div>
      <div className="mk-td">
        <InputNumber value={bill_count} formatter={value => `${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')} parser={value => value.replace(/,*/g, '')} onChange={(val) => this.setState({bill_count: val})} onBlur={() => this.onChange(bill_count, 'bill_count')}/>
      </div>
      <div className="mk-td">
        <InputNumber value={unitprice} formatter={value => `${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')} parser={value => value.replace(/,*/g, '')} onChange={(val) => this.setState({unitprice: val})} onBlur={() => this.onChange(unitprice, 'unitprice')}/>
      </div>
      <div className="mk-td">
        <InputNumber value={amount_line} formatter={value => `${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')} parser={value => value.replace(/,*/g, '')} onChange={(val) => this.setState({amount_line: val})} onBlur={() => this.onChange(amount_line, 'amount_line')}/>
      </div>
      <div className="mk-td mk-right">{line.tax_name}</div>
      <div className="mk-td mk-right">{line.tax_amount.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ',')} <span className="del-line" onClick={() => delLine(line.uuid)}></span> </div>
    </div>
  }
}
 
class InvoiceTable extends Component {
  static propTpyes = {
    config: PropTypes.object,
    data: PropTypes.any,
    onChange: PropTypes.func
  }
 
  state = {
    data: [],
    editKey: '',
    total: {}
  }
 
  UNSAFE_componentWillMount () {
    const { data } = this.props
 
    let _data = fromJS(data).toJS()
    if (!_data.length) {
      _data = [{uuid: Utils.getguid(), productname: '', spec: '', unit: '', bill_count: '', unitprice: 0, amount_line: 0, tax_rate: '', tax_name: '', tax_amount: 0}]
    }
 
    this.setState({
      data: _data
    }, () => {
      this.getTotal(_data)
    })
  }
 
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.props), fromJS(nextProps)) || !is(fromJS(this.state), fromJS(nextState))
  }
 
  componentDidMount () {
    MKEmitter.addListener('resetDetails', this.resetDetails)
  }
 
  /**
   * @description 组件销毁,清除state更新
   */
  componentWillUnmount () {
    this.setState = () => {
      return
    }
    MKEmitter.removeListener('resetDetails', this.resetDetails)
  }
  
  changeMoneyToChinese = (money) => {
    let cnNums = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
    let cnIntRadice = ['', '拾', '佰', '仟']
    let cnIntUnits = ['', '万', '亿', '兆']
    let cnDecUnits = ['角', '分', '毫', '厘']
    let cnInteger = '整'
    let cnIntLast = '元'
    let maxNum = 999999999999999.9999 // 最大处理的数字
    let IntegerNum = null
    let DecimalNum = null
    let ChineseStr = ''
    let parts = null // 分离金额后用的数组,预定义
    let Symbol = ''  // 正负值标记
 
    if (money === 0) return ''
 
    if (money >= maxNum) return '超出最大处理数字'
 
    if(money < 0) {
      money = -money
      Symbol = '负'       
    }
    money = money.toString() // 转换为字符串
    if (money.indexOf('.') === -1) {
      IntegerNum = money
      DecimalNum = ''
    } else {
      parts = money.split('.')
      IntegerNum = parts[0]
      DecimalNum = parts[1].substr(0, 4)
    }
 
    if (parseInt(IntegerNum, 10) > 0) { // 获取整型部分转换
      let zeroCount = 0
      let IntLen = IntegerNum.length
      for (let i = 0; i < IntLen; i++) {
        let n = IntegerNum.substr(i, 1)
        let p = IntLen - i - 1
        let q = p / 4
        let m = p % 4
        
        if (n === '0') {
          zeroCount++
        } else {
          if (zeroCount > 0) {
            ChineseStr += cnNums[0]
          }
          zeroCount = 0 // 归零
          ChineseStr += cnNums[parseInt(n)] + cnIntRadice[m]
        }
 
        if (m === 0 && zeroCount < 4) {
          ChineseStr += cnIntUnits[q]
        }
      }
      ChineseStr += cnIntLast
    }
 
    if (DecimalNum !== '') { // 小数部分
      let decLen = DecimalNum.length
 
      for (let i = 0; i < decLen; i++) {
        let n = DecimalNum.substr(i, 1)
        if (n !== '0') {
          ChineseStr += cnNums[Number(n)] + cnDecUnits[i]
        }
      }
    }
    if (ChineseStr === '') {
      ChineseStr += cnNums[0] + cnIntLast + cnInteger
    } else if (DecimalNum === '') {
      ChineseStr += cnInteger
    }
 
    ChineseStr = Symbol + ChineseStr
    
    return ChineseStr
  }
 
  getTotal = (data) => {
    let price = 0
    let tax = 0
 
    data.forEach(item => {
      if (!item.productcode) return
 
      price += item.amount_line * 100
      tax += item.tax_amount * 100
    })
 
    price = price / 100
    tax = tax / 100
 
    this.setState({total: {price, tax, sum: price, sumName: this.changeMoneyToChinese(price)}})
  }
 
  resetDetails = (data) => {
    let _data = fromJS(data).toJS()
 
    if (!_data.length) {
      _data = [{uuid: Utils.getguid(), productname: '', spec: '', unit: '', bill_count: '', unitprice: 0, amount_line: 0, tax_rate: '', tax_name: '', tax_amount: 0}]
    }
 
    this.setState({data: _data}, () => {
      this.getTotal(_data)
    })
  }
 
  addLine = () => {
    const { data } = this.state
 
    let line = {uuid: Utils.getguid(), productname: '', spec: '', unit: '', bill_count: '', unitprice: 0, amount_line: 0, tax_rate: '', tax_name: '', tax_amount: 0}
 
    this.setState({data: [...data, line]})
  }
 
  delLine = () => {
    const { editKey, data } = this.state
 
    if (data.length === 1) {
      notification.warning({
        top: 92,
        message: '至少保留一行明细!',
        duration: 3
      })
      return
    }
 
    let _data = data.filter(item => item.uuid !== editKey)
 
    this.setState({data: _data}, () => {
      this.getTotal(_data)
    })
    this.props.onChange(_data)
  }
 
  changeLine = (record) => {
    const { editKey, data } = this.state
 
    let _data = data.map(item => {
      if (item.uuid === editKey) {
        return record
      } else {
        return item
      }
    })
 
 
    this.setState({data: _data}, () => {
      this.getTotal(_data)
    })
    this.props.onChange(_data)
  }
 
  checkLine = (uuid) => {
    this.setState({editKey: uuid})
  }
 
  changeDetail = (prod) => {
    const { editKey, data } = this.state
 
    let _data = data.map(item => {
      if (item.uuid === editKey) {
        item.productname = prod.productname
        item.spec = prod.spec
        item.unit = prod.unit
        item.unitprice = prod.unitprice
        item.tax_rate = prod.tax_rate || 0
        item.tax_name = prod.tax_rate * 100 + '%'
 
        if (prod.vat_special_management && prod.free_tax_mark === 'true') {
          item.tax_name = prod.vat_special_management
        }
 
        item.productcode = prod.productcode
        item.Description = prod.Description
        item.tax_classify_code = prod.tax_classify_code
        item.tax_classify_name = prod.tax_classify_name
 
        if (item.bill_count && item.unitprice) {
          item.amount_line = Math.round(item.unitprice * item.bill_count * 100) / 100
        }
        if (item.amount_line) {
          item.tax_amount = item.amount_line - item.amount_line / (item.tax_rate + 1)
          item.tax_amount = Math.round(item.tax_amount * 100) / 100
        } else {
          item.tax_amount = 0
        }
      }
 
      return item
    })
 
    this.setState({data: _data, editKey: '', visible: false}, () => {
      this.setState({editKey: editKey})
      this.getTotal(_data)
    })
    this.props.onChange(_data)
  }
 
  render() {
    const { config, tax_type } = this.props
    const { editKey, data, total, visible } = this.state
 
    return (
      <div className="detail-wrap">
        <span className="plus-line" onClick={this.addLine}></span>
        <div className="mk-th">
          <div className="mk-td">货物或应税劳务、服务名称</div>
          <div className="mk-td">规格型号</div>
          <div className="mk-td">单位</div>
          <div className="mk-td">数量</div>
          <div className="mk-td">单价(含税)</div>
          <div className="mk-td">金额(含税)</div>
          <div className="mk-td">税率</div>
          <div className="mk-td">税额</div>
        </div>
        {data.map(item => {
          if (editKey === item.uuid) {
            return <DetailLine key={item.uuid} line={item} changeLine={this.changeLine} delLine={this.delLine} trigger={() => this.setState({visible: true})}/>
          }
 
          return <div className="mk-tr" key={item.uuid} onClick={() => this.checkLine(item.uuid)}>
            <div className="mk-td mk-left">{item.productname || ''}</div>
            <div className="mk-td mk-left">{item.spec || ''}</div>
            <div className="mk-td mk-left">{item.unit || ''}</div>
            <div className="mk-td mk-right">{`${item.bill_count || ''}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}</div>
            <div className="mk-td mk-right">{`${item.unitprice || ''}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}</div>
            <div className="mk-td mk-right">{`${item.amount_line || ''}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}</div>
            <div className="mk-td mk-right">{item.tax_name}</div>
            <div className="mk-td mk-right">{item.tax_amount.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ',')}<span className="del-line" onClick={() => this.delLine(item.uuid)}></span></div>
          </div>
        })}
        <div className="mk-total">
          <div className="mk-td">合计</div>
          <div className="mk-td"></div>
          <div className="mk-td"></div>
          <div className="mk-td"></div>
          <div className="mk-td"></div>
          <div className="mk-td">¥{`${total.price}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}</div>
          <div className="mk-td"></div>
          <div className="mk-td">¥{`${total.tax}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}</div>
        </div>
        <div className="mk-upcase">
          <div className="mk-td">价税合计(大写)</div>
          <div className="mk-td">{total.sumName}</div>
          <div className="mk-td">(小写)¥{`${total.sum}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}</div>
        </div>
        <Modal
          title="商品信息"
          visible={visible}
          width="75vw"
          maskClosable={false}
          onCancel={() => { this.setState({ visible: false }) }}
          footer={null}
        >
          <SubTable config={config} tax_type={tax_type} onChange={this.changeDetail}/>
        </Modal>
      </div>
    )
  }
}
 
export default InvoiceTable