Edit.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <template>
  2. <div class="page-card" v-loading="loading">
  3. <el-form :model="form" label-width="90px" style="max-width:880px">
  4. <el-form-item label="工单号"><el-input v-model="form.no" /></el-form-item>
  5. <el-form-item label="产品">
  6. <el-select
  7. v-model="form.product_id"
  8. filterable
  9. clearable
  10. placeholder="可不选,选择后带出名称和工序"
  11. style="width:100%"
  12. @change="onProduct"
  13. >
  14. <el-option v-for="item in products" :key="item.id" :label="item.name + ' / ' + item.code" :value="item.id" />
  15. </el-select>
  16. </el-form-item>
  17. <el-form-item label="名称">
  18. <el-input v-model="form.product_name" placeholder="选择产品后自动带出,不选产品则手动填写" />
  19. </el-form-item>
  20. <el-form-item label="规格">
  21. <el-input v-model="form.spec" placeholder="选择产品后自动带出,也可手动填写" />
  22. </el-form-item>
  23. <el-form-item label="批次"><el-input v-model="form.batch_no" /></el-form-item>
  24. <el-form-item label="客户"><el-input v-model="form.customer" /></el-form-item>
  25. <el-form-item label="计划日期"><el-date-picker v-model="form.plan_date" value-format="YYYY-MM-DD" /></el-form-item>
  26. <el-form-item label="备注"><el-input v-model="form.remark" type="textarea" /></el-form-item>
  27. <el-form-item label="工序">
  28. <ProcessEditor v-model="form.processes" />
  29. </el-form-item>
  30. <el-form-item>
  31. <el-button type="primary" :loading="saving" @click="onSave">保存</el-button>
  32. <el-button v-if="form.id && hasPerms('print.do')" type="success" @click="openPrint({ bizType: 'workorder', bizId: form.id, no: form.no, name: form.product_name })">套打</el-button>
  33. <el-button v-if="form.id && hasPerms('workorder.delete')" type="danger" plain @click="onDel">删除</el-button>
  34. <el-button @click="$router.push('/workorders')">返回</el-button>
  35. </el-form-item>
  36. </el-form>
  37. </div>
  38. </template>
  39. <script setup>
  40. import { onMounted, reactive, ref } from 'vue'
  41. import { useRoute, useRouter } from 'vue-router'
  42. import { ElMessage, ElMessageBox } from 'element-plus'
  43. import ProcessEditor from '@/components/ProcessEditor.vue'
  44. import { productInfo, productList, workorderDel, workorderInfo, workorderSave } from '@/api/ps'
  45. import { openPrint } from '@/utils/print'
  46. import { hasPerms } from '@/utils/auth'
  47. const route = useRoute()
  48. const router = useRouter()
  49. const loading = ref(false)
  50. const saving = ref(false)
  51. const products = ref([])
  52. const form = reactive({
  53. id: 0,
  54. no: '',
  55. product_id: '',
  56. product_name: '',
  57. spec: '',
  58. batch_no: '',
  59. customer: '',
  60. plan_date: '',
  61. remark: '',
  62. status: 'draft',
  63. processes: [],
  64. })
  65. onMounted(async () => {
  66. const list = await productList({ limit: 200 })
  67. products.value = list.rows || []
  68. if (!route.params.id) {
  69. return
  70. }
  71. loading.value = true
  72. try {
  73. const row = await workorderInfo(route.params.id)
  74. Object.assign(form, row)
  75. form.product_id = row.product_id || ''
  76. form.product_name = row.product_name || (row.product && row.product.name) || ''
  77. form.spec = row.spec || (row.product && row.product.spec) || ''
  78. form.processes = row.processes || []
  79. } finally {
  80. loading.value = false
  81. }
  82. })
  83. async function onProduct(id) {
  84. if (!id) {
  85. return
  86. }
  87. const row = await productInfo(id)
  88. form.product_name = row.name || ''
  89. form.spec = row.spec || ''
  90. if (form.processes && form.processes.length) {
  91. return
  92. }
  93. form.processes = (row.processes || []).map((item) => ({
  94. seq: item.seq,
  95. code: item.code,
  96. name: item.name,
  97. workstation: item.workstation,
  98. standard_time: item.standard_time,
  99. qc_points: item.qc_points,
  100. remark: item.remark,
  101. }))
  102. }
  103. async function onDel() {
  104. await ElMessageBox.confirm('删除后不可恢复,确定删除该工单?', '删除工单')
  105. await workorderDel(form.id)
  106. ElMessage.success('已删除')
  107. router.push('/workorders')
  108. }
  109. async function onSave() {
  110. if (!String(form.no || '').trim()) {
  111. ElMessage.warning('请填写工单号')
  112. return
  113. }
  114. if (!form.product_id && !String(form.product_name || '').trim()) {
  115. ElMessage.warning('请选择产品或填写名称')
  116. return
  117. }
  118. saving.value = true
  119. try {
  120. const { qty, sheet_qty, product, ...rest } = form
  121. await workorderSave({
  122. ...rest,
  123. product_id: Number(form.product_id) || 0,
  124. product_name: String(form.product_name || '').trim(),
  125. })
  126. ElMessage.success('已保存')
  127. router.push('/workorders')
  128. } finally {
  129. saving.value = false
  130. }
  131. }
  132. </script>