import React from 'react'; import GreyReactBox from 'grey-react-box'; import { chain, bignumber } from 'mathjs'; import InvoiceControllerState from '../InvoiceControllerState'; import { WrappedFormUtils } from 'kts-components-antd-x3/lib/form/Form'; import LineAttributeType from '../InvoiceControllerState/GoodsListState/LineAttributeType'; export default class InvoiceControllerForm extends GreyReactBox { /** form 字典 */ readonly formList = new Map(); /** 注册 form */ useForm(key: string, value: WrappedFormUtils) { /** 注册 form 到组件 */ React.useEffect(() => { this.formList.set(key, value); return () => { this.formList.delete(key); }; }, [value, key]); } /** 校验所有表单 */ async validateFields(): Promise<{ errors: Map; values: Map }> { await this.wait(); const _errors = new Map(); const _values = new Map(); const arr = Array.from(this.formList); for (let i = 0; i < arr.length; i++) { const key = arr[i][0]; const { errors, values } = await validateFields(arr[i][1]); errors && _errors.set(key, errors); _values && _values.set(key, values); } // 列表数据 if (this.state.goodsListState.form) { try { await this.state.goodsListState.form?.validateFields() } catch (error: any) { _errors.set('goodsList', error.errors); } } _values.set('goodsList', this.state.goodsListState.goodsList.slice()); //货物运输列表 if (this.state.freightListState.form) { try { await this.state.freightListState.form?.validateFields() } catch (error: any) { _errors.set('freightList', error.errors); } } _values.set('freightList', this.state.freightListState.goodsList.slice()); // 金额(含税) _values.set('lineAmountIncludeTax', (() => { let sum = chain(bignumber(0)); this.state.goodsListState.goodsList.forEach(e => { if (!e || e.lineAttribute === LineAttributeType.赠品行) return; sum = sum.add(bignumber(e.lineAmountIncludeTax || 0)); }); return sum.done().toNumber(); })()); // 金额(不含税) _values.set('lineAmountExcludeTax', (() => { let sum = chain(bignumber(0)); this.state.goodsListState.goodsList.forEach(e => { if (!e || e.lineAttribute === LineAttributeType.赠品行) return; sum = sum.add(bignumber(e.lineAmountExcludeTax || 0)); }); return sum.done().toNumber(); })()); // 税额 _values.set('taxAmount', (() => { let sum = chain(bignumber(0)); this.state.goodsListState.goodsList.forEach((e) => { if (!e || e.lineAttribute === LineAttributeType.赠品行) return; sum = sum.add(bignumber(e.taxAmount || 0)); }); return sum.done().toNumber(); })()); // 含税标记 _values.set('isTaxIncluded', this.state.goodsListState.isTaxIncluded) return { errors: _errors, values: _values }; } } const validateFields = (form: WrappedFormUtils) => { return new Promise((resolve: (v: any) => void) => { form.validateFields((errors, values) => { resolve({ errors, values }); }); }); };