index.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. import Authorization from '@/components/Authorization'
  2. import DatePicker from '@/components/DatePicker'
  3. import MoneyInput from '@/components/MoneyInput'
  4. import { contractStore, tenderStore } from '@/store/mobx'
  5. import { ContractType } from '@/store/mobx/contract'
  6. import { iModalCommonProps } from '@/types/contract'
  7. import { apiContractSection } from '@/utils/common/api'
  8. import { contractTreeBaseId } from '@/utils/common/constStatus'
  9. import consts from '@/utils/consts'
  10. import { dayjsFormat, formatMoney, handleAutoCode } from '@/utils/util'
  11. import { Button, Form, Input, Modal, Select, TreeSelect } from 'antd'
  12. import locale from 'antd/es/date-picker/locale/zh_CN'
  13. import dayjs from 'dayjs'
  14. import React, { useEffect, useState, useMemo } from 'react'
  15. import { apiGetReturnWay } from '../Tabs/Receivable/api'
  16. import styles from './index.module.scss'
  17. interface ContractSection {
  18. id: string;
  19. treeType: number;
  20. parentId: string;
  21. name: string;
  22. depth: number;
  23. serial: number;
  24. attribution: string;
  25. code: string;
  26. projectId: string;
  27. bidsectionId: string;
  28. contractId: string;
  29. contractName: string;
  30. contractCode: string;
  31. contractPrice: string;
  32. contractReturned: string;
  33. contractsPaid: string;
  34. contractStatus: number;
  35. contractLocking: number;
  36. createTime: string;
  37. children?: any;
  38. templateNumber: number;
  39. operation: string;
  40. elderBrother: boolean;
  41. isEnd: boolean;
  42. key: string;
  43. title: string;
  44. }
  45. const ContractModal: React.FC<iModalCommonProps> = ({ modalObj: { type, visible, confirmLoading, contractType }, onConfirm, onCancel, reload, row }) => {
  46. const { Option } = Select
  47. const [ contractSection, setContractSection ] = useState<ContractSection[]>([])
  48. const [ form ] = Form.useForm()
  49. const [ options, setOptions ] = useState<string[]>([])
  50. const modalObj = {
  51. create: {
  52. title: '新建合同',
  53. cancelText: '取消',
  54. okText: '确认添加'
  55. },
  56. update: {
  57. title: '编辑合同',
  58. cancelText: '取消',
  59. okText: '确认'
  60. },
  61. close: {
  62. title: '关闭合同',
  63. cancelText: '取消',
  64. okText: '确认关闭'
  65. },
  66. del: {
  67. title: '删除合同',
  68. cancelText: '取消',
  69. okText: '确认删除'
  70. },
  71. unlock: {
  72. title: '解锁合同',
  73. cancelText: '取消',
  74. okText: '确认解锁'
  75. },
  76. add: {
  77. title: contractType === ContractType.INCOME ? '添加回款' : '添加支付',
  78. cancelText: '关闭',
  79. okText: '确认'
  80. }
  81. }
  82. const initTreeSection = async () => {
  83. const { code = -1, sectionTree: data = {} } = await apiContractSection(tenderStore.bid, contractType === ContractType.INCOME ? consts.CONTRACT_TREE.RETURN : consts.CONTRACT_TREE.PAID)
  84. if (code === consts.RET_CODE.SUCCESS) {
  85. setContractSection(data.children)
  86. }
  87. }
  88. useEffect(() => {
  89. if (visible) {
  90. form.setFieldsValue({ treeId: row.id, bidsectionId: tenderStore.bid })
  91. if (type === 'create') {
  92. initTreeSection()
  93. form.setFieldsValue({ treeId: row.contractId === contractTreeBaseId ? row.id : row.parentId })
  94. }
  95. if (type === 'update') {
  96. const { content = '', name = '', price = '', partyA = '', partyB = '', partyASigner = '', partyBSigner = '', signerTime = '', remarks = '' } = contractStore.contract
  97. form.setFieldsValue({ content, name, price, partyA, partyB, partyASigner, partyBSigner, signerTime: signerTime ? dayjs(signerTime) : '', remarks })
  98. } else if (type === 'add') {
  99. apiGetReturnWay().then(({ code = -1, data = [] }) => {
  100. if (code === consts.RET_CODE.SUCCESS) {
  101. const options = data.map((item: string) => (
  102. <Option key={item} value={item}>
  103. {item}
  104. </Option>
  105. ))
  106. setOptions(options)
  107. }
  108. })
  109. form.setFieldsValue({ contractsId: contractStore.contract.id })
  110. } else {
  111. form.setFieldsValue({ id: contractStore.contract.id })
  112. }
  113. }
  114. }, [ visible ])
  115. const autoCode = async () => {
  116. const ruleArr = await handleAutoCode(tenderStore.tender.bidsectionId, contractType === ContractType.INCOME ? 'contractReturnRule' : 'contractPaidRule')
  117. form.setFieldsValue({ code: ruleArr.join('-') })
  118. }
  119. // 处理添加回款的金额不应该超出最大值
  120. const maxPrice = parseFloat(contractStore.contract.price) - parseFloat(contractType === ContractType.INCOME ? contractStore.contract.returned : contractStore.contract.paid)
  121. const minPrice = parseFloat(contractType === ContractType.INCOME ? row.contractReturned : row.contractsPaid)
  122. return (
  123. <Modal
  124. getContainer={false}
  125. visible={visible}
  126. title={modalObj[type]?.title}
  127. onCancel={() => {
  128. form.resetFields()
  129. onCancel()
  130. }}
  131. footer={
  132. <div className="pi-justify-end">
  133. {type === 'update' ? (
  134. <Authorization type="contract" auth="delete">
  135. <Button type="primary" key="delete" size="small" danger onClick={() => reload('del')}>
  136. 删除合同
  137. </Button>
  138. </Authorization>
  139. ) : null}
  140. <Button type="default" size="small" key="cancel" className="pi-btn-secondary" onClick={() => {
  141. form.resetFields()
  142. onCancel()
  143. }}>
  144. {modalObj[type]?.cancelText}
  145. </Button>
  146. <Button
  147. type="primary"
  148. size="small"
  149. key="ok"
  150. loading={confirmLoading}
  151. danger={type === 'del'}
  152. onClick={() => {
  153. form
  154. .validateFields()
  155. .then(values => {
  156. form.resetFields()
  157. if (type === 'update') {
  158. values.signerTime = dayjsFormat(values.signerTime, 'YYYY-MM-DD HH:mm:ss')
  159. }
  160. if (type === 'del') {
  161. delete values.warningText
  162. }
  163. if (type === 'add') {
  164. values.time = dayjsFormat(values.time, 'YYYY-MM-DD HH:mm:ss')
  165. }
  166. onConfirm(values, type)
  167. })
  168. .catch(info => {
  169. console.error('Validate Failed:', info)
  170. })
  171. }}>
  172. {modalObj[type]?.okText}
  173. </Button>
  174. </div>
  175. }>
  176. <Form form={form} layout="vertical" style={type === 'update' ? { maxHeight: '482px', overflowY: 'scroll', paddingRight: 5 } : { overflow: 'hidden' }}>
  177. <Form.Item name="bidsectionId" hidden>
  178. <Input />
  179. </Form.Item>
  180. <Form.Item name="treeId" label="合同劳务" hidden={type === 'create' ? false : true} rules={[ { required: true, message: '请选择项目节' } ]}>
  181. <TreeSelect treeData={contractSection} />
  182. </Form.Item>
  183. {/* <Form.Item name="treeId" label="合同劳务" hidden>
  184. <Input />
  185. </Form.Item> */}
  186. {type === 'create' ? (
  187. <>
  188. <Form.Item name="code" label="合同编号" rules={[ { required: true, message: '请输入合同编号' } ]} className={styles.contractFormItem}>
  189. <Input
  190. autoComplete='off'
  191. addonAfter={
  192. <span className="pi-pd-lr-11" onClick={() => autoCode()}>
  193. 自动编号
  194. </span>
  195. } />
  196. </Form.Item>
  197. <Form.Item name="name" label="合同名称" rules={[ { required: true, message: '请输入合同名称' } ]}>
  198. <Input placeholder="输入合同名称" autoComplete='off'/>
  199. </Form.Item>
  200. {/* <Form.Item name="contractsType" label="合同类型" rules={[ { required: true, message: '请选择合同类型' } ]}>
  201. <Select showSearch>
  202. <Option value={1}>支出合同</Option>
  203. <Option value={2}>收入合同</Option>
  204. </Select>
  205. </Form.Item> */}
  206. <Form.Item label="合同金额" name="price" rules={[ { required: true, message: '请输入合同金额' } ]} className={styles.MoneyFormItem}>
  207. {/* <Input type="number" placeholder="输入合同金额" addonAfter={<span>元</span>} /> */}
  208. <MoneyInput />
  209. </Form.Item>
  210. </>
  211. ) : (
  212. ''
  213. )}
  214. {type === 'update' ? (
  215. <>
  216. <Form.Item name="content" label="项目内容" rules={[ { required: true, message: '请输入项目内容' } ]}>
  217. <Input />
  218. </Form.Item>
  219. <Form.Item name="name" label="合同名称" rules={[ { required: true, message: '请输入项目内容' } ]}>
  220. <Input />
  221. </Form.Item>
  222. <Form.Item label="合同金额" name="price" rules={[ { required: true, message: '请输入合同金额' }, () => ({
  223. validator(_, value) {
  224. if (value && parseFloat(value) < minPrice) {
  225. return Promise.reject(`当前金额不能低于${minPrice}`)
  226. }
  227. return Promise.resolve()
  228. }
  229. }) ]}>
  230. <MoneyInput />
  231. </Form.Item>
  232. <Form.Item name="partyA" label="甲方" rules={[ { required: true, message: '请输入甲方' } ]}>
  233. <Input />
  234. </Form.Item>
  235. <Form.Item name="partyASigner" label="甲方签约人" rules={[ { required: true, message: '请输入甲方签约人' } ]}>
  236. <Input />
  237. </Form.Item>
  238. <Form.Item name="partyB" label="乙方" rules={[ { required: true, message: '请输入乙方' } ]}>
  239. <Input />
  240. </Form.Item>
  241. <Form.Item name="partyBSigner" label="乙方签约人" rules={[ { required: true, message: '请输入乙方签约人' } ]}>
  242. <Input />
  243. </Form.Item>
  244. <Form.Item name="signerTime" label="合同签约日期" rules={[ { required: true, message: '请选择签约日期' } ]}>
  245. <DatePicker allowClear locale={locale} className="pi-width-100P" />
  246. </Form.Item>
  247. <Form.Item name="remarks" label="备注">
  248. <Input.TextArea maxLength={100} />
  249. </Form.Item>
  250. </>
  251. ) : null
  252. }
  253. {type === 'close' ? (
  254. <>
  255. <Form.Item name="id" hidden>
  256. <Input />
  257. </Form.Item>
  258. <span>关闭后,合同将锁定,无法进行编辑、上传文件等操作。</span>
  259. </>
  260. ) : (
  261. ''
  262. )}
  263. {type === 'unlock' ? (
  264. <>
  265. <Form.Item name="id" hidden>
  266. <Input />
  267. </Form.Item>
  268. <span>解锁后,合同将锁定,无法进行编辑、上传文件等操作。</span>
  269. </>
  270. ) : (
  271. ''
  272. )}
  273. {type === 'del' ? (
  274. <>
  275. <Form.Item name="id" hidden>
  276. <Input />
  277. </Form.Item>
  278. <p className="mb-2">删除后,数据无法恢复,请谨慎操作。</p>
  279. <p className="mb-2">
  280. 请在下方文本框输入文本「<span className="pi-red">确认删除本合同</span>」,以此确认删除操作。
  281. </p>
  282. <Form.Item
  283. name="warningText"
  284. rules={[
  285. () => ({
  286. validator(rule, value) {
  287. if (!value || value !== '确认删除本合同') {
  288. return Promise.reject('请按照提示信息进行删除操作!')
  289. }
  290. return Promise.resolve()
  291. }
  292. })
  293. ]}>
  294. <Input placeholder="输入文本, 确认删除" />
  295. </Form.Item>
  296. </>
  297. ) : (
  298. ''
  299. )}
  300. {type === 'add' ? (
  301. <>
  302. <Form.Item name="contractsId" hidden>
  303. <Input />
  304. </Form.Item>
  305. <Form.Item name="time" label={`${contractType === ContractType.INCOME ? '回款' : '支付'}日期`} rules={[ { required: true, message: `请选择${contractType === ContractType.INCOME ? '回款' : '支付'}日期` } ]}>
  306. <DatePicker allowClear locale={locale} className="pi-width-100P" />
  307. </Form.Item>
  308. <Form.Item name="price" label={`${contractType === ContractType.INCOME ? '回款' : '支付'}金额`} rules={[ { required: true, message: `请输入${contractType === ContractType.INCOME ? '回款' : '支付'}金额` }, () => ({
  309. validator(_, value) {
  310. if (value && parseFloat(value) > maxPrice) {
  311. return Promise.reject(`当前金额不能大于${maxPrice}`)
  312. }
  313. return Promise.resolve()
  314. }
  315. }) ]}>
  316. {/* <Input onChange={handleReturnPrice}/> */}
  317. <MoneyInput />
  318. </Form.Item>
  319. <Form.Item name="way" label={`${contractType === ContractType.INCOME ? '回款' : '支付'}方式`} rules={[ { required: true, message: `请选择${contractType === ContractType.INCOME ? '回款' : '支付'}方式` } ]}>
  320. <Select>{options}</Select>
  321. </Form.Item>
  322. <Form.Item name="remarks" label="备注">
  323. <Input.TextArea maxLength={100} />
  324. </Form.Item>
  325. </>
  326. ) : (
  327. ''
  328. )}
  329. </Form>
  330. </Modal>
  331. )
  332. }
  333. export default ContractModal