PurchaseOrderItemForm.vue 8.6 KB

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