BusinessForm.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <template>
  2. <Dialog :title="dialogTitle" v-model="dialogVisible" width="1280">
  3. <el-form
  4. ref="formRef"
  5. :model="formData"
  6. :rules="formRules"
  7. label-width="120px"
  8. v-loading="formLoading"
  9. >
  10. <el-row>
  11. <el-col :span="8">
  12. <el-form-item label="商机名称" prop="name">
  13. <el-input v-model="formData.name" placeholder="请输入商机名称" />
  14. </el-form-item>
  15. </el-col>
  16. <el-col :span="8">
  17. <el-form-item label="负责人" prop="ownerUserId">
  18. <el-select
  19. v-model="formData.ownerUserId"
  20. :disabled="formType !== 'create'"
  21. class="w-1/1"
  22. >
  23. <el-option
  24. v-for="item in userOptions"
  25. :key="item.id"
  26. :label="item.nickname"
  27. :value="item.id"
  28. />
  29. </el-select>
  30. </el-form-item>
  31. </el-col>
  32. <el-col :span="8">
  33. <el-form-item label="客户名称" prop="customerId">
  34. <el-select
  35. :disabled="formData.customerDefault"
  36. v-model="formData.customerId"
  37. placeholder="请选择客户"
  38. class="w-1/1"
  39. >
  40. <el-option
  41. v-for="item in customerList"
  42. :key="item.id"
  43. :label="item.name"
  44. :value="item.id"
  45. />
  46. </el-select>
  47. </el-form-item>
  48. </el-col>
  49. </el-row>
  50. <el-row>
  51. <el-col :span="8">
  52. <el-form-item label="商机状态组" prop="statusTypeId">
  53. <el-select
  54. v-model="formData.statusTypeId"
  55. placeholder="请选择商机状态组"
  56. clearable
  57. class="w-1/1"
  58. :disabled="formType !== 'create'"
  59. >
  60. <el-option
  61. v-for="item in statusTypeList"
  62. :key="item.id"
  63. :label="item.name"
  64. :value="item.id"
  65. />
  66. </el-select>
  67. </el-form-item>
  68. </el-col>
  69. <el-col :span="8">
  70. <el-form-item label="预计成交日期" prop="dealTime">
  71. <el-date-picker
  72. v-model="formData.dealTime"
  73. type="date"
  74. value-format="x"
  75. placeholder="选择预计成交日期"
  76. class="!w-1/1"
  77. />
  78. </el-form-item>
  79. </el-col>
  80. <el-col :span="8">
  81. <el-form-item label="备注" prop="remark">
  82. <el-input type="textarea" v-model="formData.remark" placeholder="请输入备注" />
  83. </el-form-item>
  84. </el-col>
  85. </el-row>
  86. <!-- 子表的表单 -->
  87. <ContentWrap>
  88. <el-tabs v-model="subTabsName" class="-mt-15px -mb-10px">
  89. <el-tab-pane label="产品清单" name="product">
  90. <BusinessProductForm
  91. ref="productFormRef"
  92. :products="formData.products"
  93. :disabled="disabled"
  94. />
  95. </el-tab-pane>
  96. </el-tabs>
  97. </ContentWrap>
  98. <el-row>
  99. <el-col :span="8">
  100. <el-form-item label="产品总金额" prop="totalProductPrice">
  101. <el-input
  102. disabled
  103. v-model="formData.totalProductPrice"
  104. :formatter="erpPriceInputFormatter"
  105. />
  106. </el-form-item>
  107. </el-col>
  108. <el-col :span="8">
  109. <el-form-item label="整单折扣(%)" prop="discountPercent">
  110. <el-input-number
  111. v-model="formData.discountPercent"
  112. placeholder="请输入整单折扣"
  113. controls-position="right"
  114. :min="0"
  115. :precision="2"
  116. class="!w-1/1"
  117. />
  118. </el-form-item>
  119. </el-col>
  120. <el-col :span="8">
  121. <el-form-item label="折扣后金额" prop="price">
  122. <el-input
  123. disabled
  124. v-model="formData.totalPrice"
  125. placeholder="请输入商机金额"
  126. :formatter="erpPriceInputFormatter"
  127. />
  128. </el-form-item>
  129. </el-col>
  130. </el-row>
  131. </el-form>
  132. <template #footer>
  133. <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
  134. <el-button @click="dialogVisible = false">取 消</el-button>
  135. </template>
  136. </Dialog>
  137. </template>
  138. <script setup lang="ts">
  139. import * as BusinessApi from '@/api/crm/business'
  140. import * as BusinessStatusApi from '@/api/crm/business/status'
  141. import * as CustomerApi from '@/api/crm/customer'
  142. import * as UserApi from '@/api/system/user'
  143. import { useUserStore } from '@/store/modules/user'
  144. import BusinessProductForm from './components/BusinessProductForm.vue'
  145. import { erpPriceInputFormatter, erpPriceMultiply } from '@/utils'
  146. const { t } = useI18n() // 国际化
  147. const message = useMessage() // 消息弹窗
  148. const dialogVisible = ref(false) // 弹窗的是否展示
  149. const dialogTitle = ref('') // 弹窗的标题
  150. const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  151. const formType = ref('') // 表单的类型:create - 新增;update - 修改
  152. const formData = ref({
  153. id: undefined,
  154. name: undefined,
  155. customerId: undefined,
  156. ownerUserId: undefined,
  157. statusTypeId: undefined,
  158. dealTime: undefined,
  159. discountPercent: 0,
  160. totalProductPrice: undefined,
  161. totalPrice: undefined,
  162. remark: undefined,
  163. products: [],
  164. contactId: undefined,
  165. customerDefault: false
  166. })
  167. const formRules = reactive({
  168. name: [{ required: true, message: '商机名称不能为空', trigger: 'blur' }],
  169. customerId: [{ required: true, message: '客户不能为空', trigger: 'blur' }],
  170. ownerUserId: [{ required: true, message: '负责人不能为空', trigger: 'blur' }],
  171. statusTypeId: [{ required: true, message: '商机状态组不能为空', trigger: 'blur' }]
  172. })
  173. const formRef = ref() // 表单 Ref
  174. const userOptions = ref<UserApi.UserVO[]>([]) // 用户列表
  175. const statusTypeList = ref([]) // 商机状态类型列表
  176. // TODO 芋艿:统一的客户选择面板
  177. const customerList = ref([]) // 客户列表的数据
  178. /** 子表的表单 */
  179. const subTabsName = ref('product')
  180. const productFormRef = ref()
  181. /** 计算 discountPrice、totalPrice 价格 */
  182. watch(
  183. () => formData.value,
  184. (val) => {
  185. if (!val) {
  186. return
  187. }
  188. const totalProductPrice = val.products.reduce((prev, curr) => prev + curr.totalPrice, 0)
  189. const discountPrice =
  190. val.discountPercent != null
  191. ? erpPriceMultiply(totalProductPrice, val.discountPercent / 100.0)
  192. : 0
  193. const totalPrice = totalProductPrice - discountPrice
  194. // 赋值
  195. formData.value.totalProductPrice = totalProductPrice
  196. formData.value.totalPrice = totalPrice
  197. },
  198. { deep: true }
  199. )
  200. /** 打开弹窗 */
  201. const open = async (type: string, id?: number, customerId?: number, contactId?: number) => {
  202. dialogVisible.value = true
  203. dialogTitle.value = t('action.' + type)
  204. formType.value = type
  205. resetForm()
  206. // 修改时,设置数据
  207. if (id) {
  208. formLoading.value = true
  209. try {
  210. formData.value = await BusinessApi.getBusiness(id)
  211. } finally {
  212. formLoading.value = false
  213. }
  214. } else {
  215. if (customerId) {
  216. formData.value.customerId = customerId
  217. formData.value.customerDefault = true // 默认客户的选择,不允许变
  218. }
  219. // 自动关联 contactId 联系人编号
  220. if (contactId) {
  221. formData.value.contactId = contactId
  222. }
  223. }
  224. // 获得客户列表
  225. customerList.value = await CustomerApi.getCustomerSimpleList()
  226. // 加载商机状态类型列表
  227. statusTypeList.value = await BusinessStatusApi.getBusinessStatusTypeSimpleList()
  228. // 获得用户列表
  229. userOptions.value = await UserApi.getSimpleUserList()
  230. // 默认新建时选中自己
  231. if (formType.value === 'create') {
  232. formData.value.ownerUserId = useUserStore().getUser.id
  233. }
  234. }
  235. defineExpose({ open }) // 提供 open 方法,用于打开弹窗
  236. /** 提交表单 */
  237. const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
  238. const submitForm = async () => {
  239. // 校验表单
  240. if (!formRef) return
  241. const valid = await formRef.value.validate()
  242. if (!valid) return
  243. await productFormRef.value.validate()
  244. // 提交请求
  245. formLoading.value = true
  246. try {
  247. const data = formData.value as unknown as BusinessApi.BusinessVO
  248. if (formType.value === 'create') {
  249. await BusinessApi.createBusiness(data)
  250. message.success(t('common.createSuccess'))
  251. } else {
  252. await BusinessApi.updateBusiness(data)
  253. message.success(t('common.updateSuccess'))
  254. }
  255. dialogVisible.value = false
  256. // 发送操作成功的事件
  257. emit('success')
  258. } finally {
  259. formLoading.value = false
  260. }
  261. }
  262. /** 重置表单 */
  263. const resetForm = () => {
  264. formData.value = {
  265. id: undefined,
  266. name: undefined,
  267. customerId: undefined,
  268. ownerUserId: undefined,
  269. statusTypeId: undefined,
  270. dealTime: undefined,
  271. discountPercent: 0,
  272. totalProductPrice: undefined,
  273. totalPrice: undefined,
  274. remark: undefined,
  275. products: [],
  276. contactId: undefined,
  277. customerDefault: false
  278. }
  279. formRef.value?.resetFields()
  280. }
  281. </script>