SaleOrderItemForm.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <template>
  2. <el-form
  3. ref="formRef"
  4. :model="formData"
  5. :rules="formRules"
  6. v-loading="formLoading"
  7. label-width="0px"
  8. :inline-message="true"
  9. :disabled="disabled"
  10. >
  11. <el-table :data="formData" show-summary :summary-method="getSummaries" class="-mt-10px">
  12. <el-table-column label="序号" type="index" align="center" width="60" />
  13. <el-table-column label="产品名称" min-width="180">
  14. <template #default="{ row, $index }">
  15. <el-form-item :prop="`${$index}.productId`" :rules="formRules.productId" class="mb-0px!">
  16. <el-select
  17. v-model="row.productId"
  18. filterable
  19. @change="onChangeProduct($event, row)"
  20. placeholder="请选择产品"
  21. >
  22. <el-option
  23. v-for="item in productList"
  24. :key="item.id"
  25. :label="item.name"
  26. :value="item.id"
  27. />
  28. </el-select>
  29. </el-form-item>
  30. </template>
  31. </el-table-column>
  32. <el-table-column label="库存" min-width="100">
  33. <template #default="{ row }">
  34. <el-form-item class="mb-0px!">
  35. <el-input disabled v-model="row.stockCount" :formatter="erpCountInputFormatter" />
  36. </el-form-item>
  37. </template>
  38. </el-table-column>
  39. <el-table-column label="条码" min-width="150">
  40. <template #default="{ row }">
  41. <el-form-item class="mb-0px!">
  42. <el-input disabled v-model="row.productBarCode" />
  43. </el-form-item>
  44. </template>
  45. </el-table-column>
  46. <el-table-column label="单位" min-width="80">
  47. <template #default="{ row }">
  48. <el-form-item class="mb-0px!">
  49. <el-input disabled v-model="row.productUnitName" />
  50. </el-form-item>
  51. </template>
  52. </el-table-column>
  53. <el-table-column label="数量" prop="count" fixed="right" min-width="140">
  54. <template #default="{ row, $index }">
  55. <el-form-item :prop="`${$index}.count`" :rules="formRules.count" class="mb-0px!">
  56. <el-input-number
  57. v-model="row.count"
  58. controls-position="right"
  59. :min="0.001"
  60. :precision="3"
  61. class="!w-100%"
  62. />
  63. </el-form-item>
  64. </template>
  65. </el-table-column>
  66. <el-table-column label="产品单价" fixed="right" min-width="120">
  67. <template #default="{ row, $index }">
  68. <el-form-item :prop="`${$index}.productPrice`" class="mb-0px!">
  69. <el-input-number
  70. v-model="row.productPrice"
  71. controls-position="right"
  72. :min="0.01"
  73. :precision="2"
  74. class="!w-100%"
  75. />
  76. </el-form-item>
  77. </template>
  78. </el-table-column>
  79. <el-table-column label="金额" prop="totalProductPrice" fixed="right" min-width="100">
  80. <template #default="{ row, $index }">
  81. <el-form-item :prop="`${$index}.totalProductPrice`" class="mb-0px!">
  82. <el-input
  83. disabled
  84. v-model="row.totalProductPrice"
  85. :formatter="erpPriceInputFormatter"
  86. />
  87. </el-form-item>
  88. </template>
  89. </el-table-column>
  90. <el-table-column label="税率(%)" fixed="right" min-width="115">
  91. <template #default="{ row, $index }">
  92. <el-form-item :prop="`${$index}.taxPercent`" class="mb-0px!">
  93. <el-input-number
  94. v-model="row.taxPercent"
  95. controls-position="right"
  96. :min="0.01"
  97. :precision="2"
  98. class="!w-100%"
  99. />
  100. </el-form-item>
  101. </template>
  102. </el-table-column>
  103. <el-table-column label="税额" prop="taxPrice" fixed="right" min-width="120">
  104. <template #default="{ row, $index }">
  105. <el-form-item :prop="`${$index}.taxPrice`" class="mb-0px!">
  106. <el-form-item :prop="`${$index}.taxPrice`" class="mb-0px!">
  107. <el-input disabled v-model="row.taxPrice" :formatter="erpPriceInputFormatter" />
  108. </el-form-item>
  109. </el-form-item>
  110. </template>
  111. </el-table-column>
  112. <el-table-column label="税额合计" prop="totalPrice" fixed="right" min-width="100">
  113. <template #default="{ row, $index }">
  114. <el-form-item :prop="`${$index}.totalPrice`" class="mb-0px!">
  115. <el-input disabled v-model="row.totalPrice" :formatter="erpPriceInputFormatter" />
  116. </el-form-item>
  117. </template>
  118. </el-table-column>
  119. <el-table-column label="备注" min-width="150">
  120. <template #default="{ row, $index }">
  121. <el-form-item :prop="`${$index}.remark`" class="mb-0px!">
  122. <el-input v-model="row.remark" placeholder="请输入备注" />
  123. </el-form-item>
  124. </template>
  125. </el-table-column>
  126. <el-table-column align="center" fixed="right" label="操作" width="60">
  127. <template #default="{ $index }">
  128. <el-button @click="handleDelete($index)" link>—</el-button>
  129. </template>
  130. </el-table-column>
  131. </el-table>
  132. </el-form>
  133. <el-row justify="center" class="mt-3" v-if="!disabled">
  134. <el-button @click="handleAdd" round>+ 添加出库产品</el-button>
  135. </el-row>
  136. </template>
  137. <script setup lang="ts">
  138. import { ProductApi, ProductVO } from '@/api/erp/product/product'
  139. import { StockApi } from '@/api/erp/stock/stock'
  140. import {
  141. erpCountInputFormatter,
  142. erpPriceInputFormatter,
  143. erpPriceMultiply,
  144. getSumValue
  145. } from '@/utils'
  146. const props = defineProps<{
  147. items: undefined
  148. disabled: false
  149. }>()
  150. const formLoading = ref(false) // 表单的加载中
  151. const formData = ref([])
  152. const formRules = reactive({
  153. inId: [{ required: true, message: '出库编号不能为空', trigger: 'blur' }],
  154. warehouseId: [{ required: true, message: '仓库不能为空', trigger: 'blur' }],
  155. productId: [{ required: true, message: '产品不能为空', trigger: 'blur' }],
  156. count: [{ required: true, message: '产品数量不能为空', trigger: 'blur' }]
  157. })
  158. const formRef = ref([]) // 表单 Ref
  159. const productList = ref<ProductVO[]>([]) // 产品列表
  160. /** 初始化设置出库项 */
  161. watch(
  162. () => props.items,
  163. async (val) => {
  164. formData.value = val
  165. },
  166. { immediate: true }
  167. )
  168. /** 监听合同产品变化,计算合同产品总价 */
  169. watch(
  170. () => formData.value,
  171. (val) => {
  172. if (!val || val.length === 0) {
  173. return
  174. }
  175. // 循环处理
  176. val.forEach((item) => {
  177. item.totalProductPrice = erpPriceMultiply(item.productPrice, item.count)
  178. item.taxPrice = erpPriceMultiply(item.totalProductPrice, item.taxPercent / 100.0)
  179. if (item.totalProductPrice != null) {
  180. item.totalPrice = item.totalProductPrice + (item.taxPrice || 0)
  181. } else {
  182. item.totalPrice = undefined
  183. }
  184. })
  185. },
  186. { deep: true }
  187. )
  188. /** 合计 */
  189. const getSummaries = (param: SummaryMethodProps) => {
  190. const { columns, data } = param
  191. const sums: string[] = []
  192. columns.forEach((column, index: number) => {
  193. if (index === 0) {
  194. sums[index] = '合计'
  195. return
  196. }
  197. if (['count', 'totalProductPrice', 'taxPrice', 'totalPrice'].includes(column.property)) {
  198. const sum = getSumValue(data.map((item) => Number(item[column.property])))
  199. sums[index] =
  200. column.property === 'count' ? erpCountInputFormatter(sum) : erpPriceInputFormatter(sum)
  201. } else {
  202. sums[index] = ''
  203. }
  204. })
  205. return sums
  206. }
  207. /** 新增按钮操作 */
  208. const handleAdd = () => {
  209. const row = {
  210. id: undefined,
  211. productId: undefined,
  212. productUnitName: undefined, // 产品单位
  213. productBarCode: undefined, // 产品条码
  214. productPrice: undefined,
  215. stockCount: undefined,
  216. count: 1,
  217. totalProductPrice: undefined,
  218. taxPercent: undefined,
  219. taxPrice: undefined,
  220. totalPrice: undefined,
  221. remark: undefined
  222. }
  223. formData.value.push(row)
  224. }
  225. /** 删除按钮操作 */
  226. const handleDelete = (index: number) => {
  227. formData.value.splice(index, 1)
  228. }
  229. /** 处理产品变更 */
  230. const onChangeProduct = (productId, row) => {
  231. const product = productList.value.find((item) => item.id === productId)
  232. if (product) {
  233. row.productUnitName = product.unitName
  234. row.productBarCode = product.barCode
  235. row.productPrice = product.salePrice
  236. }
  237. // 加载库存
  238. setStockCount(row)
  239. }
  240. /** 加载库存 */
  241. const setStockCount = async (row: any) => {
  242. if (!row.productId) {
  243. return
  244. }
  245. const count = await StockApi.getStockCount(row.productId)
  246. row.stockCount = count || 0
  247. }
  248. /** 表单校验 */
  249. const validate = () => {
  250. return formRef.value.validate()
  251. }
  252. defineExpose({ validate })
  253. /** 初始化 */
  254. onMounted(async () => {
  255. productList.value = await ProductApi.getProductSimpleList()
  256. // 默认添加一个
  257. if (formData.value.length === 0) {
  258. handleAdd()
  259. }
  260. })
  261. </script>