king
2022-10-10 5354b648a9765d8c9ca3f8c7ce065b58c3d55716
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
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { Form, Row, Col, Select, Button, InputNumber, Input, Tooltip } from 'antd'
import { QuestionCircleOutlined } from '@ant-design/icons'
 
import './index.scss'
 
class BillCodeForm extends Component {
  static propTpyes = {
    btn: PropTypes.object,          // 按钮信息
    fields: PropTypes.array,        // 表单
    billcodes: PropTypes.array,     // 表单
    columns: PropTypes.array,       // 表单
    modular: PropTypes.array,       // 表单
    modularDetail: PropTypes.array, // 表单
    orderChange: PropTypes.func     // 表单
  }
 
  state = {
    editItem: null,
    modularDetail: [],
    funFields: [],
    billFields: [],
    type: '1',
    TypeCharOne: 'Lp'
  }
 
  UNSAFE_componentWillMount() {
    let _modularDetail = []
    let _billFields = []
 
    if (this.props.modular.length > 0) {
      _modularDetail = this.props.modularDetail.filter(item => item.BID === this.props.modular[0].ID)
    }
 
    let fieldMap = new Map()
    this.props.fields.forEach(_field => {
      if (_field.type === 'text' && !fieldMap.has(_field.field.toLowerCase())) {
        _billFields.push(_field)
        fieldMap.set(_field.field.toLowerCase(), true)
      }
    })
 
    if (this.props.btn.Ot !== 'notRequired') {
      this.props.columns.forEach(_field => {
        if (_field.type === 'text' && !fieldMap.has(_field.field.toLowerCase())) {
          _billFields.push(_field)
          fieldMap.set(_field.field.toLowerCase(), true)
        }
      })
    }
 
    let _usedfields = this.props.billcodes.map(item => item.field)
 
    this.setState({
      modularDetail: _modularDetail,
      funFields: this.props.fields.filter(field => field.type === 'funcvar' && !_usedfields.includes(field.field)),
      billFields: _billFields
    })
  }
 
  UNSAFE_componentWillReceiveProps (nextProps) {
    if (!is(fromJS(this.props.modular), fromJS(nextProps.modular))) {
      let _detail = nextProps.modularDetail.filter(item => item.BID === nextProps.modular[0].ID)
 
      this.setState({
        modularDetail: _detail
      })
    }
  }
 
  edit = (record) => {
    let _usedfields = this.props.billcodes.map(item => item.field)
    let _funFields = this.props.fields.filter(field => field.type === 'funcvar' && (!_usedfields.includes(field.field) || field.field === record.field))
 
    if (record.TypeCharOne === 'Y' || record.TypeCharOne === 'n') {
      let _detail = this.props.modularDetail.filter(item => item.BID === record.ModularCode)
      let _modularDetailCode = ''
 
      if (_detail.filter(item => item.ModularDetailCode === record.ModularDetailCode).length > 0) {
        _modularDetailCode = record.ModularDetailCode
      }
 
      this.setState({
        type: '2',
        funFields: _funFields,
        editItem: record,
        modularDetail: _detail,
        TypeCharOne: record.TypeCharOne
      }, () => {
        this.props.form.setFieldsValue({
          field: record.field,
          TypeCharOne: record.TypeCharOne,
          ModularCode: record.ModularCode,
          ModularDetailCode: _modularDetailCode,
        })
      })
    } else {
      this.setState({
        type: '1',
        funFields: _funFields,
        editItem: record,
        TypeCharOne: record.TypeCharOne
      }, () => {
        let _form = {
          field: record.field,
          TypeCharOne: record.TypeCharOne,
          Type: record.Type,
          linkField: record.linkField
        }
        if (record.TypeCharOne === 'Lp') {
          _form.mark = record.mark || ''
        }
        this.props.form.setFieldsValue(_form)
      })
    }
  }
 
  voucherChange = (value) => {
    let _detail = this.props.modularDetail.filter(item => item.BID === value)
 
    this.setState({
      modularDetail: _detail
    })
    this.props.form.setFieldsValue({
      ModularDetailCode: _detail[0] ? _detail[0].ModularDetailCode : ''
    })
  }
 
  TypeCharOneChange = (value) => {
    if (value === 'Y' || value === 'n') {
      this.setState({
        type: '2',
        TypeCharOne: value
      })
    } else {
      this.setState({
        type: '1',
        TypeCharOne: value
      })
    }
  }
 
  handleConfirm = () => {
    const { funFields, billFields } = this.state
    // 表单提交时检查输入值是否正确
    this.props.form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        values.uuid = this.state.editItem ? this.state.editItem.uuid : ''
 
        let _funField = funFields.filter(item => item.field === values.field)[0]
        values.fieldName = _funField ? _funField.label : ''
 
        // 一级菜单值为20190203125926873D6029A9C511041719420时TypeCharTwo=TableCode,其他用BillCode
        if (values.ModularCode === '20190203125926873D6029A9C511041719420' || values.ModularCode === '01') {
          values.TypeCharTwo = 'TableCode'
        } else {
          values.TypeCharTwo = 'BillCode'
        }
        // 设置类型名称,用于列表显示
        let billType = {
          Y: '单号',
          n: '编码',
          Lp: '行号',
          BN: '批次',
        }
        values.billType = billType[values.TypeCharOne]
 
        if (values.TypeCharOne === 'Y' || values.TypeCharOne === 'n') {
          // 设置凭证类型中文名
          let ModularCode = this.props.modular.filter(item => item.ID === values.ModularCode)[0]
          values.ModularCodeName = ModularCode.NameNO
 
          // 设置流水号位数
          let _detail = this.state.modularDetail.filter(item => item.ModularDetailCode === values.ModularDetailCode)[0]
          values.Type = _detail.Type
        } else {
          let _billField = billFields.filter(item => item.field === values.linkField)[0]
          values.linkFieldName = _billField ? _billField.label : ''
        }
 
        this.setState({
          editItem: null
        }, () => {
          this.props.orderChange(values)
          let _usedfields = this.props.billcodes.map(item => item.field)
 
          this.setState({
            funFields: this.props.fields.filter(field => field.type === 'funcvar' && !_usedfields.includes(field.field)),
          })
        })
        let _form = {
          field: ''
        }
        if (this.state.TypeCharOne === 'Lp') {
          _form.mark = ''
        }
        this.props.form.setFieldsValue(_form)
      }
    })
  }
 
  render() {
    const { getFieldDecorator } = this.props.form
    const { type, TypeCharOne } = this.state
    const formItemLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 10 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 14 }
      }
    }
 
    return (
      <Form {...formItemLayout} className="verify-form mk-fix-form-height">
        <Row gutter={24}>
          <Col span={7}>
            <Form.Item label="函数变量">
              {getFieldDecorator('field', {
                initialValue: '',
                rules: [
                  {
                    required: true,
                    message: '请选择函数变量!'
                  }
                ]
              })(
                <Select>
                  {this.state.funFields.map(option =>
                    <Select.Option title={option.label} id={option.uuid} key={option.uuid} value={option.field}>
                      {option.label}
                    </Select.Option>
                  )}
                </Select>
              )}
            </Form.Item>
          </Col>
          <Col span={7}>
            <Form.Item label={'类型'}>
              {getFieldDecorator('TypeCharOne', {
                initialValue: 'Lp',
                rules: [
                  {
                    required: true,
                    message: '请选择类型!'
                  }
                ]
              })(
                <Select onChange={(value) => {this.TypeCharOneChange(value)}}>
                  <Select.Option value="Y"> 单号 </Select.Option>
                  <Select.Option value="n"> 编码 </Select.Option>
                  <Select.Option value="Lp"> 行号 </Select.Option>
                  <Select.Option value="BN"> 批次 </Select.Option>
                </Select>
              )}
            </Form.Item>
          </Col>
          {type === '1' ? <Col span={7}>
            <Form.Item label={'关联字段'}>
              {getFieldDecorator('linkField', {
                initialValue: '',
                rules: [
                  {
                    required: true,
                    message: '请选择关联字段!'
                  }
                ]
              })(
                <Select>
                  {this.state.billFields.map(option =>
                    <Select.Option title={option.label} id={option.uuid} key={option.uuid} value={option.field}>
                      {option.label}
                    </Select.Option>
                  )}
                </Select>
              )}
            </Form.Item>
          </Col> : null}
          {type === '2' ? <Col span={7}>
            <Form.Item label={'凭证类型'}>
              {getFieldDecorator('ModularCode', {
                initialValue: this.props.modular[0] ? this.props.modular[0].ID : '',
                rules: [
                  {
                    required: true,
                    message: '请选择凭证类型!'
                  }
                ]
              })(
                <Select
                  showSearch
                  filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
                  onChange={(value) => {this.voucherChange(value)}}
                >
                  {this.props.modular.map(option =>
                    <Select.Option title={option.NameNO} id={option.ID} key={option.ID} value={option.ID}>
                      {option.NameNO}
                    </Select.Option>
                  )}
                </Select>
              )}
            </Form.Item>
          </Col> : null}
          <Col span={3} className="add">
            <Button onClick={this.handleConfirm} className="mk-green">
              保存
            </Button>
          </Col>
          {type === '1' ? <Col span={7}>
            <Form.Item label={'位数'}>
              {getFieldDecorator('Type', {
                initialValue: 4,
                rules: [
                  {
                    required: true,
                    message: '请输入位数!'
                  }
                ]
              })(<InputNumber min={1} max={10} precision={0} />)}
            </Form.Item>
          </Col> : null}
          {type === '2' ? <Col span={7}>
            <Form.Item label={'凭证标识'}>
              {getFieldDecorator('ModularDetailCode', {
                initialValue: this.state.modularDetail[0] ? this.state.modularDetail[0].ModularDetailCode : '',
                rules: [
                  {
                    required: true,
                    message: '请选择凭证标识!'
                  }
                ]
              })(
                <Select
                  showSearch
                  filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
                >
                  {this.state.modularDetail.map(option =>
                    <Select.Option style={{whiteSpace: 'unset'}} title={option.CodeName} id={option.ModularDetailCode} key={option.ModularDetailCode} value={option.ModularDetailCode}>
                      {option.CodeName}
                    </Select.Option>
                  )}
                </Select>
              )}
            </Form.Item>
          </Col> : null}
          {TypeCharOne === 'Lp' ? <Col span={7}>
            <Form.Item label={
              <Tooltip placement="topLeft" title="该标识用于生成行号规则">
                <QuestionCircleOutlined className="mk-form-tip" />
                标识
              </Tooltip>
            }>
              {getFieldDecorator('mark', {
                initialValue: '',
                rules: [
                  {
                    required: true,
                    message: '请输入标识!'
                  },
                  {
                    pattern: /^[a-zA-Z0-9]*$/ig,
                    message: '请输入数字、字母以及_'
                  }
                ]
              })(<Input placeholder="" autoComplete="off" />)}
            </Form.Item>
          </Col> : null}
        </Row>
      </Form>
    )
  }
}
 
export default Form.create()(BillCodeForm)