StockInItemForm.vue 7.9 KB

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