ContractForm.vue 11 KB

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