PurchaseOrderItemForm.vue 8.7 KB

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