PurchaseOrderForm.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <template>
  2. <Dialog :title="dialogTitle" v-model="dialogVisible" width="1080">
  3. <el-form
  4. ref="formRef"
  5. :model="formData"
  6. :rules="formRules"
  7. label-width="100px"
  8. v-loading="formLoading"
  9. :disabled="disabled"
  10. >
  11. <el-row :gutter="20">
  12. <el-col :span="8">
  13. <el-form-item label="订单单号" prop="no">
  14. <el-input disabled v-model="formData.no" placeholder="保存时自动生成" />
  15. </el-form-item>
  16. </el-col>
  17. <el-col :span="8">
  18. <el-form-item label="订单时间" prop="orderTime">
  19. <el-date-picker
  20. v-model="formData.orderTime"
  21. type="date"
  22. value-format="x"
  23. placeholder="选择订单时间"
  24. class="!w-1/1"
  25. />
  26. </el-form-item>
  27. </el-col>
  28. <el-col :span="8">
  29. <el-form-item label="供应商" prop="supplierId">
  30. <el-select
  31. v-model="formData.supplierId"
  32. clearable
  33. filterable
  34. placeholder="请选择供应商"
  35. class="!w-1/1"
  36. >
  37. <el-option
  38. v-for="item in supplierList"
  39. :key="item.id"
  40. :label="item.name"
  41. :value="item.id"
  42. />
  43. </el-select>
  44. </el-form-item>
  45. </el-col>
  46. <el-col :span="16">
  47. <el-form-item label="备注" prop="remark">
  48. <el-input
  49. type="textarea"
  50. v-model="formData.remark"
  51. :rows="1"
  52. placeholder="请输入备注"
  53. />
  54. </el-form-item>
  55. </el-col>
  56. <el-col :span="8">
  57. <el-form-item label="附件" prop="fileUrl">
  58. <UploadFile :is-show-tip="false" v-model="formData.fileUrl" :limit="1" />
  59. </el-form-item>
  60. </el-col>
  61. </el-row>
  62. <!-- 子表的表单 -->
  63. <ContentWrap>
  64. <el-tabs v-model="subTabsName" class="-mt-15px -mb-10px">
  65. <el-tab-pane label="订单产品清单" name="item">
  66. <PurchaseOrderItemForm ref="itemFormRef" :items="formData.items" :disabled="disabled" />
  67. </el-tab-pane>
  68. </el-tabs>
  69. </ContentWrap>
  70. <el-row :gutter="20">
  71. <el-col :span="8">
  72. <el-form-item label="优惠率(%)" prop="discountPercent">
  73. <el-input-number
  74. v-model="formData.discountPercent"
  75. controls-position="right"
  76. :min="0"
  77. :precision="2"
  78. placeholder="请输入优惠率"
  79. class="!w-1/1"
  80. />
  81. </el-form-item>
  82. </el-col>
  83. <el-col :span="8">
  84. <el-form-item label="收款优惠" prop="discountPrice">
  85. <el-input
  86. disabled
  87. v-model="formData.discountPrice"
  88. :formatter="erpPriceInputFormatter"
  89. />
  90. </el-form-item>
  91. </el-col>
  92. <el-col :span="8">
  93. <el-form-item label="优惠后金额">
  94. <el-input disabled v-model="formData.totalPrice" :formatter="erpPriceInputFormatter" />
  95. </el-form-item>
  96. </el-col>
  97. <el-col :span="8">
  98. <el-form-item label="结算账户" prop="accountId">
  99. <el-select
  100. v-model="formData.accountId"
  101. clearable
  102. filterable
  103. placeholder="请选择结算账户"
  104. class="!w-1/1"
  105. >
  106. <el-option
  107. v-for="item in accountList"
  108. :key="item.id"
  109. :label="item.name"
  110. :value="item.id"
  111. />
  112. </el-select>
  113. </el-form-item>
  114. </el-col>
  115. <el-col :span="8">
  116. <el-form-item label="支付订金" prop="depositPrice">
  117. <el-input-number
  118. v-model="formData.depositPrice"
  119. controls-position="right"
  120. :min="0"
  121. :precision="2"
  122. placeholder="请输入支付订金"
  123. class="!w-1/1"
  124. />
  125. </el-form-item>
  126. </el-col>
  127. </el-row>
  128. </el-form>
  129. <template #footer>
  130. <el-button @click="submitForm" type="primary" :disabled="formLoading" v-if="!disabled">
  131. 确 定
  132. </el-button>
  133. <el-button @click="dialogVisible = false">取 消</el-button>
  134. </template>
  135. </Dialog>
  136. </template>
  137. <script setup lang="ts">
  138. import { PurchaseOrderApi, PurchaseOrderVO } from '@/api/erp/purchase/order'
  139. import PurchaseOrderItemForm from './components/PurchaseOrderItemForm.vue'
  140. import { SupplierApi, SupplierVO } from '@/api/erp/purchase/supplier'
  141. import { erpPriceInputFormatter, erpPriceMultiply } from '@/utils'
  142. import * as UserApi from '@/api/system/user'
  143. import { AccountApi, AccountVO } from '@/api/erp/finance/account'
  144. /** ERP 销售订单表单 */
  145. defineOptions({ name: 'PurchaseOrderForm' })
  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 - 修改;detail - 详情
  152. const formData = ref({
  153. id: undefined,
  154. supplierId: undefined,
  155. accountId: undefined,
  156. orderTime: undefined,
  157. remark: undefined,
  158. fileUrl: '',
  159. discountPercent: 0,
  160. discountPrice: 0,
  161. totalPrice: 0,
  162. depositPrice: 0,
  163. items: [],
  164. no: undefined // 订单单号,后端返回
  165. })
  166. const formRules = reactive({
  167. supplierId: [{ required: true, message: '供应商不能为空', trigger: 'blur' }],
  168. orderTime: [{ required: true, message: '订单时间不能为空', trigger: 'blur' }]
  169. })
  170. const disabled = computed(() => formType.value === 'detail')
  171. const formRef = ref() // 表单 Ref
  172. const supplierList = ref<SupplierVO[]>([]) // 供应商列表
  173. const accountList = ref<AccountVO[]>([]) // 账户列表
  174. const userList = ref<UserApi.UserVO[]>([]) // 用户列表
  175. /** 子表的表单 */
  176. const subTabsName = ref('item')
  177. const itemFormRef = ref()
  178. /** 计算 discountPrice、totalPrice 价格 */
  179. watch(
  180. () => formData.value,
  181. (val) => {
  182. if (!val) {
  183. return
  184. }
  185. const totalPrice = val.items.reduce((prev, curr) => prev + curr.totalPrice, 0)
  186. const discountPrice =
  187. val.discountPercent != null ? erpPriceMultiply(totalPrice, val.discountPercent / 100.0) : 0
  188. formData.value.discountPrice = discountPrice
  189. formData.value.totalPrice = totalPrice - discountPrice
  190. },
  191. { deep: true }
  192. )
  193. /** 打开弹窗 */
  194. const open = async (type: string, id?: number) => {
  195. dialogVisible.value = true
  196. dialogTitle.value = t('action.' + type)
  197. formType.value = type
  198. resetForm()
  199. // 修改时,设置数据
  200. if (id) {
  201. formLoading.value = true
  202. try {
  203. formData.value = await PurchaseOrderApi.getPurchaseOrder(id)
  204. } finally {
  205. formLoading.value = false
  206. }
  207. }
  208. // 加载供应商列表
  209. supplierList.value = await SupplierApi.getSupplierSimpleList()
  210. // 加载用户列表
  211. userList.value = await UserApi.getSimpleUserList()
  212. // 加载账户列表
  213. accountList.value = await AccountApi.getAccountSimpleList()
  214. const defaultAccount = accountList.value.find((item) => item.defaultStatus)
  215. if (defaultAccount) {
  216. formData.value.accountId = defaultAccount.id
  217. }
  218. }
  219. defineExpose({ open }) // 提供 open 方法,用于打开弹窗
  220. /** 提交表单 */
  221. const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
  222. const submitForm = async () => {
  223. // 校验表单
  224. await formRef.value.validate()
  225. await itemFormRef.value.validate()
  226. // 提交请求
  227. formLoading.value = true
  228. try {
  229. const data = formData.value as unknown as PurchaseOrderVO
  230. if (formType.value === 'create') {
  231. await PurchaseOrderApi.createPurchaseOrder(data)
  232. message.success(t('common.createSuccess'))
  233. } else {
  234. await PurchaseOrderApi.updatePurchaseOrder(data)
  235. message.success(t('common.updateSuccess'))
  236. }
  237. dialogVisible.value = false
  238. // 发送操作成功的事件
  239. emit('success')
  240. } finally {
  241. formLoading.value = false
  242. }
  243. }
  244. /** 重置表单 */
  245. const resetForm = () => {
  246. formData.value = {
  247. id: undefined,
  248. supplierId: undefined,
  249. accountId: undefined,
  250. orderTime: undefined,
  251. remark: undefined,
  252. fileUrl: undefined,
  253. discountPercent: 0,
  254. discountPrice: 0,
  255. totalPrice: 0,
  256. depositPrice: 0,
  257. items: []
  258. }
  259. formRef.value?.resetFields()
  260. }
  261. </script>