SaleOutForm.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <template>
  2. <Dialog :title="dialogTitle" v-model="dialogVisible" width="1440">
  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="outTime">
  19. <el-date-picker
  20. v-model="formData.outTime"
  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="orderNo">
  30. <el-input v-model="formData.orderNo" readonly>
  31. <template #append>
  32. <el-button @click="openSaleOrderOutEnableList">
  33. <Icon icon="ep:search" /> 选择
  34. </el-button>
  35. </template>
  36. </el-input>
  37. </el-form-item>
  38. </el-col>
  39. <el-col :span="8">
  40. <el-form-item label="客户" prop="customerId">
  41. <el-select
  42. v-model="formData.customerId"
  43. clearable
  44. filterable
  45. placeholder="请选择客户"
  46. class="!w-1/1"
  47. >
  48. <el-option
  49. v-for="item in customerList"
  50. :key="item.id"
  51. :label="item.name"
  52. :value="item.id"
  53. />
  54. </el-select>
  55. </el-form-item>
  56. </el-col>
  57. <el-col :span="8">
  58. <el-form-item label="销售人员" prop="saleUserId">
  59. <el-select
  60. v-model="formData.saleUserId"
  61. clearable
  62. filterable
  63. placeholder="请选择销售人员"
  64. class="!w-1/1"
  65. >
  66. <el-option
  67. v-for="item in userList"
  68. :key="item.id"
  69. :label="item.nickname"
  70. :value="item.id"
  71. />
  72. </el-select>
  73. </el-form-item>
  74. </el-col>
  75. <el-col :span="16">
  76. <el-form-item label="备注" prop="remark">
  77. <el-input
  78. type="textarea"
  79. v-model="formData.remark"
  80. :rows="1"
  81. placeholder="请输入备注"
  82. />
  83. </el-form-item>
  84. </el-col>
  85. <el-col :span="8">
  86. <el-form-item label="附件" prop="fileUrl">
  87. <UploadFile :is-show-tip="false" v-model="formData.fileUrl" :limit="1" />
  88. </el-form-item>
  89. </el-col>
  90. </el-row>
  91. <!-- 子表的表单 -->
  92. <ContentWrap>
  93. <el-tabs v-model="subTabsName" class="-mt-15px -mb-10px">
  94. <el-tab-pane label="出库产品清单" name="item">
  95. <SaleOutItemForm ref="itemFormRef" :items="formData.items" :disabled="disabled" />
  96. </el-tab-pane>
  97. </el-tabs>
  98. </ContentWrap>
  99. <el-row :gutter="20">
  100. <el-col :span="8">
  101. <el-form-item label="优惠率(%)" prop="discountPercent">
  102. <el-input-number
  103. v-model="formData.discountPercent"
  104. controls-position="right"
  105. :min="0"
  106. :precision="2"
  107. placeholder="请输入优惠率"
  108. class="!w-1/1"
  109. />
  110. </el-form-item>
  111. </el-col>
  112. <el-col :span="8">
  113. <el-form-item label="收款优惠" prop="discountPrice">
  114. <el-input
  115. disabled
  116. v-model="formData.discountPrice"
  117. :formatter="erpPriceInputFormatter"
  118. />
  119. </el-form-item>
  120. </el-col>
  121. <el-col :span="8">
  122. <el-form-item label="优惠后金额">
  123. <el-input disabled v-model="formData.totalPrice" :formatter="erpPriceInputFormatter" />
  124. </el-form-item>
  125. </el-col>
  126. <el-col :span="8">
  127. <el-form-item label="其它费用" prop="otherPrice">
  128. <el-input-number
  129. v-model="formData.otherPrice"
  130. controls-position="right"
  131. :min="0"
  132. :precision="2"
  133. placeholder="请输入其它费用"
  134. class="!w-1/1"
  135. />
  136. </el-form-item>
  137. </el-col>
  138. <el-col :span="8">
  139. <el-form-item label="结算账户" prop="accountId">
  140. <el-select
  141. v-model="formData.accountId"
  142. clearable
  143. filterable
  144. placeholder="请选择结算账户"
  145. class="!w-1/1"
  146. >
  147. <el-option
  148. v-for="item in accountList"
  149. :key="item.id"
  150. :label="item.name"
  151. :value="item.id"
  152. />
  153. </el-select>
  154. </el-form-item>
  155. </el-col>
  156. <el-col :span="8">
  157. <el-form-item label="本次收款" prop="payPrice">
  158. <el-input-number
  159. v-model="formData.payPrice"
  160. controls-position="right"
  161. :min="0"
  162. :precision="2"
  163. placeholder="请输入本次收款"
  164. class="!w-1/1"
  165. />
  166. </el-form-item>
  167. </el-col>
  168. </el-row>
  169. </el-form>
  170. <template #footer>
  171. <el-button @click="submitForm" type="primary" :disabled="formLoading" v-if="!disabled">
  172. 确 定
  173. </el-button>
  174. <el-button @click="dialogVisible = false">取 消</el-button>
  175. </template>
  176. </Dialog>
  177. <!-- 可出库的订单列表 -->
  178. <SaleOrderOutEnableList ref="saleOrderOutEnableListRef" @success="handleSaleOrderChange" />
  179. </template>
  180. <script setup lang="ts">
  181. import { SaleOutApi, SaleOutVO } from '@/api/erp/sale/out'
  182. import SaleOutItemForm from './components/SaleOutItemForm.vue'
  183. import { CustomerApi, CustomerVO } from '@/api/erp/sale/customer'
  184. import { AccountApi, AccountVO } from '@/api/erp/finance/account'
  185. import { erpPriceInputFormatter, erpPriceMultiply } from '@/utils'
  186. import SaleOrderOutEnableList from '@/views/erp/sale/order/components/SaleOrderOutEnableList.vue'
  187. import { SaleOrderVO } from '@/api/erp/sale/order'
  188. import * as UserApi from '@/api/system/user'
  189. /** ERP 销售出库表单 */
  190. defineOptions({ name: 'SaleOutForm' })
  191. const { t } = useI18n() // 国际化
  192. const message = useMessage() // 消息弹窗
  193. const dialogVisible = ref(false) // 弹窗的是否展示
  194. const dialogTitle = ref('') // 弹窗的标题
  195. const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  196. const formType = ref('') // 表单的类型:create - 新增;update - 修改;detail - 详情
  197. const formData = ref({
  198. id: undefined,
  199. customerId: undefined,
  200. accountId: undefined,
  201. saleUserId: undefined,
  202. outTime: undefined,
  203. remark: undefined,
  204. fileUrl: '',
  205. discountPercent: 0,
  206. discountPrice: 0,
  207. totalPrice: 0,
  208. otherPrice: 0,
  209. payPrice: 0,
  210. orderNo: undefined,
  211. items: [],
  212. no: undefined // 出库单号,后端返回
  213. })
  214. const formRules = reactive({
  215. customerId: [{ required: true, message: '客户不能为空', trigger: 'blur' }],
  216. outTime: [{ required: true, message: '出库时间不能为空', trigger: 'blur' }],
  217. payPrice: [{ required: true, message: '本次收款不能为空', trigger: 'blur' }]
  218. })
  219. const disabled = computed(() => formType.value === 'detail')
  220. const formRef = ref() // 表单 Ref
  221. const customerList = ref<CustomerVO[]>([]) // 客户列表
  222. const accountList = ref<AccountVO[]>([]) // 账户列表
  223. const userList = ref<UserApi.UserVO[]>([]) // 用户列表
  224. /** 子表的表单 */
  225. const subTabsName = ref('item')
  226. const itemFormRef = ref()
  227. /** 计算 discountPrice、totalPrice 价格 */
  228. watch(
  229. () => formData.value,
  230. (val) => {
  231. if (!val) {
  232. return
  233. }
  234. // 计算
  235. const totalPrice = val.items.reduce((prev, curr) => prev + curr.totalPrice, 0)
  236. const discountPrice =
  237. val.discountPercent != null ? erpPriceMultiply(totalPrice, val.discountPercent / 100.0) : 0
  238. // debugger
  239. // TODO 芋艿:这里有问题
  240. const payPrice = totalPrice - discountPrice + val.otherPrice
  241. // 赋值
  242. formData.value.discountPrice = discountPrice
  243. formData.value.totalPrice = totalPrice - discountPrice
  244. formData.value.payPrice = payPrice
  245. },
  246. { deep: true }
  247. )
  248. /** 打开弹窗 */
  249. const open = async (type: string, id?: number) => {
  250. dialogVisible.value = true
  251. dialogTitle.value = t('action.' + type)
  252. formType.value = type
  253. resetForm()
  254. // 修改时,设置数据
  255. if (id) {
  256. formLoading.value = true
  257. try {
  258. formData.value = await SaleOutApi.getSaleOut(id)
  259. } finally {
  260. formLoading.value = false
  261. }
  262. }
  263. // 加载客户列表
  264. customerList.value = await CustomerApi.getCustomerSimpleList()
  265. // 加载用户列表
  266. userList.value = await UserApi.getSimpleUserList()
  267. // 加载账户列表
  268. accountList.value = await AccountApi.getAccountSimpleList()
  269. const defaultAccount = accountList.value.find((item) => item.defaultStatus)
  270. if (defaultAccount) {
  271. formData.value.accountId = defaultAccount.id
  272. }
  273. }
  274. defineExpose({ open }) // 提供 open 方法,用于打开弹窗
  275. /** 打开【可出库的订单列表】弹窗 */
  276. const saleOrderOutEnableListRef = ref() // 可出库的订单列表 Ref
  277. const openSaleOrderOutEnableList = () => {
  278. saleOrderOutEnableListRef.value.open()
  279. }
  280. const handleSaleOrderChange = (order: SaleOrderVO) => {
  281. // 将订单设置到出库单
  282. formData.value.orderId = order.id
  283. formData.value.orderNo = order.no
  284. formData.value.customerId = order.customerId
  285. formData.value.accountId = order.accountId
  286. formData.value.saleUserId = order.saleUserId
  287. formData.value.discountPercent = order.discountPercent
  288. formData.value.remark = order.remark
  289. formData.value.fileUrl = order.fileUrl
  290. // 将订单项设置到出库单项
  291. formData.value.items = order.items.filter((item) => item.count > item.outCount)
  292. }
  293. /** 提交表单 */
  294. const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
  295. const submitForm = async () => {
  296. // 校验表单
  297. await formRef.value.validate()
  298. await itemFormRef.value.validate()
  299. // 提交请求
  300. formLoading.value = true
  301. try {
  302. const data = formData.value as unknown as SaleOutVO
  303. if (formType.value === 'create') {
  304. await SaleOutApi.createSaleOut(data)
  305. message.success(t('common.createSuccess'))
  306. } else {
  307. await SaleOutApi.updateSaleOut(data)
  308. message.success(t('common.updateSuccess'))
  309. }
  310. dialogVisible.value = false
  311. // 发送操作成功的事件
  312. emit('success')
  313. } finally {
  314. formLoading.value = false
  315. }
  316. }
  317. /** 重置表单 */
  318. const resetForm = () => {
  319. formData.value = {
  320. id: undefined,
  321. customerId: undefined,
  322. accountId: undefined,
  323. saleUserId: undefined,
  324. outTime: undefined,
  325. remark: undefined,
  326. fileUrl: undefined,
  327. discountPercent: 0,
  328. discountPrice: 0,
  329. totalPrice: 0,
  330. otherPrice: 0,
  331. payPrice: 0,
  332. items: []
  333. }
  334. formRef.value?.resetFields()
  335. }
  336. </script>