ContractForm.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <template>
  2. <Dialog v-model="dialogVisible" :title="dialogTitle" width="1280">
  3. <el-form
  4. ref="formRef"
  5. v-loading="formLoading"
  6. :model="formData"
  7. :rules="formRules"
  8. label-width="120px"
  9. >
  10. <el-row>
  11. <el-col :span="8">
  12. <el-form-item label="合同编号" prop="no">
  13. <el-input disabled v-model="formData.no" placeholder="保存时自动生成" />
  14. </el-form-item>
  15. </el-col>
  16. <el-col :span="8">
  17. <el-form-item label="合同名称" prop="name">
  18. <el-input v-model="formData.name" placeholder="请输入合同名称" />
  19. </el-form-item>
  20. </el-col>
  21. <el-col :span="8">
  22. <el-form-item label="负责人" prop="ownerUserId">
  23. <el-select
  24. v-model="formData.ownerUserId"
  25. :disabled="formType !== 'create'"
  26. class="w-1/1"
  27. >
  28. <el-option
  29. v-for="item in userOptions"
  30. :key="item.id"
  31. :label="item.nickname"
  32. :value="item.id"
  33. />
  34. </el-select>
  35. </el-form-item>
  36. </el-col>
  37. </el-row>
  38. <el-row>
  39. <el-col :span="8">
  40. <el-form-item label="客户名称" prop="customerId">
  41. <el-select
  42. v-model="formData.customerId"
  43. placeholder="请选择客户"
  44. class="w-1/1"
  45. @change="handleCustomerChange"
  46. >
  47. <el-option
  48. v-for="item in customerList"
  49. :key="item.id"
  50. :label="item.name"
  51. :value="item.id"
  52. />
  53. </el-select>
  54. </el-form-item>
  55. </el-col>
  56. <el-col :span="8">
  57. <el-form-item label="商机名称" prop="businessId">
  58. <el-select
  59. @change="handleBusinessChange"
  60. :disabled="!formData.customerId"
  61. v-model="formData.businessId"
  62. class="w-1/1"
  63. >
  64. <el-option
  65. v-for="item in getBusinessOptions"
  66. :key="item.id"
  67. :label="item.name"
  68. :value="item.id!"
  69. />
  70. </el-select>
  71. </el-form-item>
  72. </el-col>
  73. </el-row>
  74. <el-row>
  75. <el-col :span="8">
  76. <el-form-item label="下单日期" prop="orderDate">
  77. <el-date-picker
  78. v-model="formData.orderDate"
  79. placeholder="选择下单日期"
  80. type="date"
  81. value-format="x"
  82. class="!w-1/1"
  83. />
  84. </el-form-item>
  85. </el-col>
  86. <el-col :span="8">
  87. <el-form-item label="开始时间" prop="startTime">
  88. <el-date-picker
  89. v-model="formData.startTime"
  90. placeholder="选择开始时间"
  91. type="date"
  92. value-format="x"
  93. class="!w-1/1"
  94. />
  95. </el-form-item>
  96. </el-col>
  97. <el-col :span="8">
  98. <el-form-item label="结束时间" prop="endTime">
  99. <el-date-picker
  100. v-model="formData.endTime"
  101. placeholder="选择结束时间"
  102. type="date"
  103. value-format="x"
  104. class="!w-1/1"
  105. />
  106. </el-form-item>
  107. </el-col>
  108. </el-row>
  109. <el-row>
  110. <el-col :span="8">
  111. <el-form-item label="公司签约人" prop="signUserId">
  112. <el-select v-model="formData.signUserId" class="w-1/1">
  113. <el-option
  114. v-for="item in userOptions"
  115. :key="item.id"
  116. :label="item.nickname"
  117. :value="item.id!"
  118. />
  119. </el-select>
  120. </el-form-item>
  121. </el-col>
  122. <el-col :span="8">
  123. <el-form-item label="客户签约人" prop="signContactId">
  124. <el-select
  125. v-model="formData.signContactId"
  126. :disabled="!formData.customerId"
  127. class="w-1/1"
  128. >
  129. <el-option
  130. v-for="item in getContactOptions"
  131. :key="item.id"
  132. :label="item.name"
  133. :value="item.id"
  134. />
  135. </el-select>
  136. </el-form-item>
  137. </el-col>
  138. <el-col :span="8">
  139. <el-form-item label="备注" prop="remark">
  140. <el-input v-model="formData.remark" placeholder="请输入备注" type="textarea" />
  141. </el-form-item>
  142. </el-col>
  143. </el-row>
  144. <!-- 子表的表单 -->
  145. <ContentWrap>
  146. <el-tabs v-model="subTabsName" class="-mt-15px -mb-10px">
  147. <el-tab-pane label="产品清单" name="product">
  148. <ContractProductForm
  149. ref="productFormRef"
  150. :products="formData.products"
  151. :disabled="disabled"
  152. />
  153. </el-tab-pane>
  154. </el-tabs>
  155. </ContentWrap>
  156. <el-row>
  157. <el-col :span="8">
  158. <el-form-item label="产品总金额" prop="totalProductPrice">
  159. <el-input
  160. disabled
  161. v-model="formData.totalProductPrice"
  162. :formatter="erpPriceInputFormatter"
  163. />
  164. </el-form-item>
  165. </el-col>
  166. <el-col :span="8">
  167. <el-form-item label="整单折扣(%)" prop="discountPercent">
  168. <el-input-number
  169. v-model="formData.discountPercent"
  170. placeholder="请输入整单折扣"
  171. controls-position="right"
  172. :min="0"
  173. :precision="2"
  174. class="!w-1/1"
  175. />
  176. </el-form-item>
  177. </el-col>
  178. <el-col :span="8">
  179. <el-form-item label="折扣后金额" prop="price">
  180. <el-input
  181. disabled
  182. v-model="formData.totalPrice"
  183. placeholder="请输入商机金额"
  184. :formatter="erpPriceInputFormatter"
  185. />
  186. </el-form-item>
  187. </el-col>
  188. </el-row>
  189. </el-form>
  190. <template #footer>
  191. <el-button :disabled="formLoading" type="primary" @click="submitForm">保存</el-button>
  192. <el-button @click="dialogVisible = false">取 消</el-button>
  193. </template>
  194. </Dialog>
  195. </template>
  196. <script lang="ts" setup>
  197. import * as CustomerApi from '@/api/crm/customer'
  198. import * as ContractApi from '@/api/crm/contract'
  199. import * as UserApi from '@/api/system/user'
  200. import * as ContactApi from '@/api/crm/contact'
  201. import * as BusinessApi from '@/api/crm/business'
  202. import { erpPriceInputFormatter, erpPriceMultiply } from '@/utils'
  203. import { useUserStore } from '@/store/modules/user'
  204. import ContractProductForm from '@/views/crm/contract/components/ContractProductForm.vue'
  205. import { bu } from '../../../../dist-prod/assets/index-9eac537b'
  206. const { t } = useI18n() // 国际化
  207. const message = useMessage() // 消息弹窗
  208. const dialogVisible = ref(false) // 弹窗的是否展示
  209. const dialogTitle = ref('') // 弹窗的标题
  210. const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  211. const formType = ref('') // 表单的类型:create - 新增;update - 修改
  212. const formData = ref({
  213. id: undefined,
  214. no: undefined,
  215. name: undefined,
  216. customerId: undefined,
  217. businessId: undefined,
  218. orderDate: undefined,
  219. startTime: undefined,
  220. endTime: undefined,
  221. signUserId: undefined,
  222. signContactId: undefined,
  223. ownerUserId: undefined,
  224. discountPercent: 0,
  225. totalProductPrice: undefined,
  226. remark: undefined,
  227. products: []
  228. })
  229. const formRules = reactive({
  230. name: [{ required: true, message: '合同名称不能为空', trigger: 'blur' }],
  231. customerId: [{ required: true, message: '客户不能为空', trigger: 'blur' }],
  232. orderDate: [{ required: true, message: '下单日期不能为空', trigger: 'blur' }],
  233. ownerUserId: [{ required: true, message: '负责人不能为空', trigger: 'blur' }]
  234. })
  235. const formRef = ref() // 表单 Ref
  236. const userOptions = ref<UserApi.UserVO[]>([]) // 用户列表
  237. // TODO 芋艿:统一的客户选择面板
  238. const customerList = ref([]) // 客户列表的数据
  239. const businessList = ref<BusinessApi.BusinessVO[]>([])
  240. const contactList = ref<ContactApi.ContactVO[]>([])
  241. /** 子表的表单 */
  242. const subTabsName = ref('product')
  243. const productFormRef = ref()
  244. /** 计算 discountPrice、totalPrice 价格 */
  245. watch(
  246. () => formData.value,
  247. (val) => {
  248. if (!val) {
  249. return
  250. }
  251. const totalProductPrice = val.products.reduce((prev, curr) => prev + curr.totalPrice, 0)
  252. const discountPrice =
  253. val.discountPercent != null
  254. ? erpPriceMultiply(totalProductPrice, val.discountPercent / 100.0)
  255. : 0
  256. const totalPrice = totalProductPrice - discountPrice
  257. // 赋值
  258. formData.value.totalProductPrice = totalProductPrice
  259. formData.value.totalPrice = totalPrice
  260. },
  261. { deep: true }
  262. )
  263. /** 打开弹窗 */
  264. const open = async (type: string, id?: number) => {
  265. dialogVisible.value = true
  266. dialogTitle.value = t('action.' + type)
  267. formType.value = type
  268. resetForm()
  269. // 修改时,设置数据
  270. if (id) {
  271. formLoading.value = true
  272. try {
  273. formData.value = await ContractApi.getContract(id)
  274. } finally {
  275. formLoading.value = false
  276. }
  277. }
  278. // 获得客户列表
  279. customerList.value = await CustomerApi.getCustomerSimpleList()
  280. // 获得用户列表
  281. userOptions.value = await UserApi.getSimpleUserList()
  282. // 默认新建时选中自己
  283. if (formType.value === 'create') {
  284. formData.value.ownerUserId = useUserStore().getUser.id
  285. }
  286. // 获取联系人
  287. contactList.value = await ContactApi.getSimpleContactList()
  288. // 获得商机列表
  289. businessList.value = await BusinessApi.getSimpleBusinessList()
  290. }
  291. defineExpose({ open }) // 提供 open 方法,用于打开弹窗
  292. /** 提交表单 */
  293. const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
  294. const submitForm = async () => {
  295. // 校验表单
  296. if (!formRef) return
  297. const valid = await formRef.value.validate()
  298. if (!valid) return
  299. // 提交请求
  300. formLoading.value = true
  301. productFormRef.value.validate()
  302. try {
  303. const data = unref(formData.value) as unknown as ContractApi.ContractVO
  304. if (formType.value === 'create') {
  305. await ContractApi.createContract(data)
  306. message.success(t('common.createSuccess'))
  307. } else {
  308. await ContractApi.updateContract(data)
  309. message.success(t('common.updateSuccess'))
  310. }
  311. dialogVisible.value = false
  312. // 发送操作成功的事件
  313. emit('success')
  314. } finally {
  315. formLoading.value = false
  316. }
  317. }
  318. /** 重置表单 */
  319. const resetForm = () => {
  320. formData.value = {
  321. id: undefined,
  322. no: undefined,
  323. name: undefined,
  324. customerId: undefined,
  325. businessId: undefined,
  326. orderDate: undefined,
  327. startTime: undefined,
  328. endTime: undefined,
  329. signUserId: undefined,
  330. signContactId: undefined,
  331. ownerUserId: undefined,
  332. discountPercent: 0,
  333. totalProductPrice: undefined,
  334. remark: undefined,
  335. products: []
  336. }
  337. formRef.value?.resetFields()
  338. }
  339. /** 处理切换客户 */
  340. const handleCustomerChange = () => {
  341. formData.value.businessId = undefined
  342. formData.value.signContactId = undefined
  343. formData.value.products = []
  344. }
  345. /** 处理商机变化 */
  346. const handleBusinessChange = async (businessId: number) => {
  347. const business = await BusinessApi.getBusiness(businessId)
  348. business.products.forEach((item) => {
  349. item.contractPrice = item.businessPrice
  350. })
  351. formData.value.products = business.products
  352. }
  353. /** 动态获取客户联系人 */
  354. const getContactOptions = computed(() =>
  355. contactList.value.filter((item) => item.customerId == formData.value.customerId)
  356. )
  357. /** 动态获取商机 */
  358. const getBusinessOptions = computed(() =>
  359. businessList.value.filter((item) => item.customerId == formData.value.customerId)
  360. )
  361. </script>