SaleOrderItemForm.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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-number
  108. v-model="row.taxPrice"
  109. controls-position="right"
  110. :min="0.01"
  111. :precision="2"
  112. class="!w-100%"
  113. />
  114. </el-form-item>
  115. </el-form-item>
  116. </template>
  117. </el-table-column>
  118. <el-table-column label="税额合计" prop="totalPrice" fixed="right" min-width="100">
  119. <template #default="{ row, $index }">
  120. <el-form-item :prop="`${$index}.totalPrice`" class="mb-0px!">
  121. <el-input disabled v-model="row.totalPrice" :formatter="erpPriceInputFormatter" />
  122. </el-form-item>
  123. </template>
  124. </el-table-column>
  125. <el-table-column label="备注" min-width="150">
  126. <template #default="{ row, $index }">
  127. <el-form-item :prop="`${$index}.remark`" class="mb-0px!">
  128. <el-input v-model="row.remark" placeholder="请输入备注" />
  129. </el-form-item>
  130. </template>
  131. </el-table-column>
  132. <el-table-column align="center" fixed="right" label="操作" width="60">
  133. <template #default="{ $index }">
  134. <el-button @click="handleDelete($index)" link>—</el-button>
  135. </template>
  136. </el-table-column>
  137. </el-table>
  138. </el-form>
  139. <el-row justify="center" class="mt-3" v-if="!disabled">
  140. <el-button @click="handleAdd" round>+ 添加出库产品</el-button>
  141. </el-row>
  142. </template>
  143. <script setup lang="ts">
  144. import { ProductApi, ProductVO } from '@/api/erp/product/product'
  145. import { StockApi } from '@/api/erp/stock/stock'
  146. import {
  147. erpCountInputFormatter,
  148. erpPriceInputFormatter,
  149. erpPriceMultiply,
  150. getSumValue
  151. } from '@/utils'
  152. const props = defineProps<{
  153. items: undefined
  154. disabled: false
  155. }>()
  156. const formLoading = ref(false) // 表单的加载中
  157. const formData = ref([])
  158. const formRules = reactive({
  159. inId: [{ required: true, message: '出库编号不能为空', trigger: 'blur' }],
  160. warehouseId: [{ required: true, message: '仓库不能为空', trigger: 'blur' }],
  161. productId: [{ required: true, message: '产品不能为空', trigger: 'blur' }],
  162. count: [{ required: true, message: '产品数量不能为空', trigger: 'blur' }]
  163. })
  164. const formRef = ref([]) // 表单 Ref
  165. const productList = ref<ProductVO[]>([]) // 产品列表
  166. /** 初始化设置出库项 */
  167. watch(
  168. () => props.items,
  169. async (val) => {
  170. formData.value = val
  171. },
  172. { immediate: true }
  173. )
  174. /** 监听合同产品变化,计算合同产品总价 */
  175. watch(
  176. () => formData.value,
  177. (val) => {
  178. if (!val || val.length === 0) {
  179. return
  180. }
  181. // 循环处理
  182. val.forEach((item) => {
  183. item.totalProductPrice = erpPriceMultiply(item.productPrice, item.count)
  184. item.taxPrice = erpPriceMultiply(item.totalProductPrice, item.taxPercent / 100.0)
  185. if (item.totalProductPrice != null) {
  186. item.totalPrice = item.totalProductPrice + (item.taxPrice || 0)
  187. } else {
  188. item.totalPrice = undefined
  189. }
  190. })
  191. },
  192. { deep: true }
  193. )
  194. /** 合计 */
  195. const getSummaries = (param: SummaryMethodProps) => {
  196. const { columns, data } = param
  197. const sums: string[] = []
  198. columns.forEach((column, index: number) => {
  199. if (index === 0) {
  200. sums[index] = '合计'
  201. return
  202. }
  203. if (['count', 'totalProductPrice', 'taxPrice', 'totalPrice'].includes(column.property)) {
  204. const sum = getSumValue(data.map((item) => Number(item[column.property])))
  205. sums[index] =
  206. column.property === 'count' ? erpCountInputFormatter(sum) : erpPriceInputFormatter(sum)
  207. } else {
  208. sums[index] = ''
  209. }
  210. })
  211. return sums
  212. }
  213. /** 新增按钮操作 */
  214. const handleAdd = () => {
  215. const row = {
  216. id: undefined,
  217. productId: undefined,
  218. productUnitName: undefined, // 产品单位
  219. productBarCode: undefined, // 产品条码
  220. productPrice: undefined,
  221. stockCount: undefined,
  222. count: 1,
  223. totalProductPrice: undefined,
  224. taxPercent: undefined,
  225. taxPrice: undefined,
  226. totalPrice: undefined,
  227. remark: undefined
  228. }
  229. formData.value.push(row)
  230. }
  231. /** 删除按钮操作 */
  232. const handleDelete = (index: number) => {
  233. formData.value.splice(index, 1)
  234. }
  235. /** 处理产品变更 */
  236. const onChangeProduct = (productId, row) => {
  237. const product = productList.value.find((item) => item.id === productId)
  238. if (product) {
  239. row.productUnitName = product.unitName
  240. row.productBarCode = product.barCode
  241. row.productPrice = product.minPrice
  242. }
  243. // 加载库存
  244. setStockCount(row)
  245. }
  246. /** 加载库存 */
  247. const setStockCount = async (row: any) => {
  248. if (!row.productId) {
  249. return
  250. }
  251. const count = await StockApi.getStockCount(row.productId)
  252. row.stockCount = count || 0
  253. }
  254. /** 表单校验 */
  255. const validate = () => {
  256. return formRef.value.validate()
  257. }
  258. defineExpose({ validate })
  259. /** 初始化 */
  260. onMounted(async () => {
  261. productList.value = await ProductApi.getProductSimpleList()
  262. // 默认添加一个
  263. if (formData.value.length === 0) {
  264. handleAdd()
  265. }
  266. })
  267. </script>