import React from 'react'; import Icon from '@ant-design/icons'; import { decorator } from "grey-react-box"; import { Form } from "kts-components-antd-x3"; import { AutoComplete, Button, Checkbox, Col, Input, InputProps, Row, Tooltip } from 'kts-xui' import { CheckboxChangeEvent } from 'kts-components-antd-x4/lib/checkbox'; import { FormComponentProps } from "kts-components-antd-x3/lib/form"; import { WrappedFormUtils } from 'kts-components-antd-x3/lib/form/Form'; import { ReactComponent as ArrowUpSvg } from './svg/arrowUp.svg'; import { ReactComponent as ArrowDownSvg } from './svg/arrowDown.svg'; import { ReactComponent as PlusSvg } from './svg/plus.svg' import InvoiceController from "../../../InvoiceController"; import Invoice from "../../../../Invoice"; import './index.less'; import classNames from 'classnames'; const RULES = { companyName: (label: string) => [ { required: true, message: `${label}必填` }, { max: 100, message: `${label}内容超长` } ], taxId: (label: string) => [ { max: 20, message: `${label}内容超长` }, { pattern: /^([a-zA-Z0-9])*$/g, message: `${label}仅能数字、英文` } ], buyerTelPhone: (label: string) => [ { max: 100, message: `${label}内容超长` }, { pattern: /^[0-9\s\-\+]+$/g, message: `${label}仅能数字、空格、-、+` } ], sellerTelPhone: (label: string) => [ { required: true, message: `${label}必填` }, { max: 100, message: `${label}内容超长` }, { pattern: /^[0-9\s\-\+]+$/g, message: `${label}仅能数字、空格、-、+` } ], bankName: (label: string) => [ { max: 100, message: `${label}内容超长` } ], bankAccount: (label: string) => [ { max: 100, message: `${label}内容超长` }, { pattern: /^([0-9a-zA-Z-\s])*$/g, message: `${label}仅能数字、字母、-、空格` } ] } const formatCompanyName = (e: any) => { const value = e?.target?.value; if (value) { return value.replace('(', '(').replace(')', ')').replace(' ', ''); } } export interface IStakeholder { /** 是否显示导入按钮 */ isShowImportButton?: boolean; /** 点击了 */ onClickImportButton?: (e: InvoiceController) => void; /** 查看当前可用授信额度 */ lineCredit?: React.ReactNode; /** 票种类型 */ isVatNormal?: boolean; /** 是否可以选择自然人 */ isNaturalPerson?: boolean; /** 是否展开 */ isExpand?: boolean; /** 购方,销方银行信息 勾选按钮是否显示 **/ leqi?: boolean; /** 购方,销方地址电话 勾选按钮是否显示 **/ riskData?: boolean; invoiceMarkCallback?: (value: any) => void; } /** 干系人 */ export default decorator(Form.create())(props => { const { form, isShowImportButton, onClickImportButton, isVatNormal = false } = props; const [isExpand, setExpand] = React.useState(typeof props.isExpand === 'boolean' ? props.isExpand : true); const { getFieldDecorator } = form; const controller = Invoice.useInvoiceController(); const model = controller.useMemo(s => s.model, []); const disableds = controller.useMemo(s => s.stakeholder.disableds || [], []); const rulesMap = controller.useMemo(s => s.stakeholder.rulesMap || {}, []); /** 乐企 */ const leqispanW = React.useMemo(() => { if (props.leqi) { return 0; } else { return 2; } }, [props.leqi, props.riskData]); /** 直连 */ const riskspanW = React.useMemo(() => { if (props.riskData) { return 0; } else { return 2; } }, [props.leqi, props.riskData]); const readOnly = React.useMemo(() => { if (model === 'readOnly') return true; if (model === 'prefab') return true; return false; }, [model]); const onExpand = React.useCallback(() => { setExpand(e => !e) }, []); const isReadOnly = React.useCallback(field => readOnly || disableds.indexOf(field) >= 0, [disableds, readOnly]); const getRules = React.useCallback((field, defaultRules) => { if (isReadOnly(field)) return []; return rulesMap[field] || defaultRules; }, [rulesMap, isReadOnly, isReadOnly]); controller.useForm('stakeholder', form); // props.isExpand 变化的时候更新 React.useEffect(() => { setExpand(typeof props.isExpand === 'boolean' ? props.isExpand : true); }, [props.isExpand]) return (
{'购买方信息'.split('').map(e => {e})} { !readOnly && }
{ props.isNaturalPerson === true && {getFieldDecorator('naturalPersonFlag', { })( )} }
{getFieldDecorator('buyerCode', { rules: [{ required: true, message: '客户代码必填' }], })( // } onClick={() => { onClickImportButton && onClickImportButton(controller) }} /> } /> )}
{getFieldDecorator('buyerName', { rules: getRules('buyerName', RULES.companyName('购买方名称')), })( } onClick={() => { onClickImportButton && onClickImportButton(controller) }} /> } /> )} {getFieldDecorator('buyerTaxId', { rules: getRules('buyerTaxId', [{ required: !isVatNormal, message: '购买方纳税人识别号必填' }, ...RULES.taxId('购买方纳税人识别号')]) })( )} {getFieldDecorator('buyerAddress', { rules: getRules('buyerAddress', [{ max: 100, message: '购买方地址内容超长' }]) })()} {getFieldDecorator('buyerPhone', { rules: getRules('buyerPhone', RULES.buyerTelPhone('购买方电话')) })()} {getFieldDecorator('buyerBank', { rules: getRules('buyerBank', RULES.bankName('购买方开户银行')) })()} {getFieldDecorator('buyerAccount', { rules: getRules('buyerAccount', RULES.bankAccount('购买方银行账号')) })()} {(props.leqi) && {getFieldDecorator('buyerBankAccountFlag', { valuePropName: 'checked', })(是否展示)} }
{'销售方信息'.split('').map(e => {e})} { !readOnly && }
{ !readOnly && props.lineCredit &&
}
{getFieldDecorator('supplierName', { rules: getRules('supplierName', RULES.companyName('销售方名称')), getValueFromEvent: formatCompanyName })()} {getFieldDecorator('supplierTaxId', { rules: getRules('supplierTaxId', [{ required: true, message: '销售方纳税人识别号必填' }, ...RULES.taxId('销售方纳税人识别号')]) })()} {getFieldDecorator('sellerAddress', { rules: getRules('sellerAddress', [{ max: 300, message: '销售方地址内容超长' }]) })()} {getFieldDecorator('sellerPhone', { rules: getRules('sellerPhone', RULES.sellerTelPhone('销售方电话')) })()} {getFieldDecorator('sellerBank', { rules: getRules('sellerBank', RULES.bankName('销售方开户银行')) })()} {getFieldDecorator('sellerAccount', { rules: getRules('sellerAccount', RULES.bankAccount('销售方银行账号')) })()} {(props.leqi) && {getFieldDecorator('sellerBankAccountFlag', { valuePropName: 'checked', })(是否展示)} }
) }) function NaturalPersonFlag(props: { // NATURAL("自然人"), COMPANY("非自然人"); value?: 'COMPANY' | 'NATURAL', onChange?: (e: 'COMPANY' | 'NATURAL') => void readOnly?: boolean; }) { const [value, setValue] = React.useState(props.value || 'COMPANY'); const onChange = React.useCallback((e: CheckboxChangeEvent) => { if (props.readOnly) return; setValue(e.target.checked ? 'NATURAL' : 'COMPANY'); props.onChange && props.onChange(e.target.checked ? 'NATURAL' : 'COMPANY'); }, [props.onChange, props.readOnly]) React.useEffect(() => { setValue(props.value || 'COMPANY') }, [props.value]) return ( 是否开票给自然人 ) } function BuyerNameInput(props: InputProps & { fieldName: string; myform: WrappedFormUtils }) { const { fieldName, myform: form } = props; const controller = Invoice.useInvoiceController(); const rootElement = controller.useMemo(s => s.rootElement, []); const autoComplete = controller.useMemo(s => s.autoComplete, []) const [options, setOptions] = React.useState([]) const onChangeAutoComplete = React.useCallback(value => { const record = options.filter(e => e[fieldName] === value)[0] as any; if (!record || !form) return; form.setFieldsValue(record); setOptions([]) }, [options, fieldName, form]) const onSearch = React.useCallback(async (searchText: string) => { await Discontinue.start(); try { if (fieldName === 'buyerName') { if (autoComplete.onBuyerNameSearch) { setOptions(await autoComplete.onBuyerNameSearch(searchText)); } } else if (fieldName === 'buyerTaxId') { if (autoComplete.onBuyerTaxIdSearch) { setOptions(await autoComplete.onBuyerTaxIdSearch(searchText)); } } else if (fieldName === 'buyerCode') { if (autoComplete.onBuyerCodeSearch) { setOptions(await autoComplete.onBuyerCodeSearch(searchText)); } } } catch (error) { setOptions([]); throw error; } }, [autoComplete.onBuyerNameSearch, fieldName]) if (props.readOnly) return {props.value}; return ( ({ value: e[fieldName] }))} onChange={onChangeAutoComplete} value={props.value} getPopupContainer={() => rootElement || document.body} style={{ width: '100%' }} > ) } class MyInput extends React.Component { render() { const props = this.props; if (props.readOnly) { return {props.value} } else { return } } } class Discontinue { private static timer: any; static start = (interval = 200) => new Promise(resolve => { clearTimeout(Discontinue.timer); Discontinue.timer = setTimeout(resolve, interval); }) }