spring
6 小时以前 6361501810a76b6809162cac99b0d9c1faba3715
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<template>
  <div>
    <el-dialog
      v-model="dialogVisible"
      title="物料"
      width="1200px"
      @close="handleCloseMaterialDialog"
    >
      <el-table v-loading="materialTableLoading" :data="materialTableData" border row-key="id">
        <el-table-column label="工序名称" prop="processName" min-width="140" />
        <el-table-column label="原料名称" prop="materialName" min-width="140" />
        <el-table-column label="原料型号" prop="materialModel" min-width="140" />
        <el-table-column label="计量单位" prop="unit" min-width="100" />
        <el-table-column label="领用数量" prop="pickQty" min-width="100" />
        <el-table-column label="补料数量" prop="supplementQty" min-width="100" />
        <el-table-column label="退料数量" prop="returnQty" min-width="100" />
        <el-table-column label="实际数量" prop="actualQty" min-width="100" />
        <el-table-column label="操作" align="center" fixed="right" width="220">
          <template #default="{ row }">
            <el-button type="primary" link @click="openSupplementDialog(row)">补料</el-button>
            <el-button type="warning" link @click="openReturnDialog(row)">退料</el-button>
            <el-button type="info" link @click="openSupplementRecordDialog(row)">补料记录</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
 
    <FormDialog
      v-model="supplementDialogVisible"
      title="补料"
      width="500px"
      @confirm="handleSubmitSupplement"
    >
      <el-form ref="supplementFormRef" :model="supplementForm" :rules="supplementRules" label-width="100px">
        <el-form-item label="补料数量" prop="supplementQty">
          <el-input-number
            v-model="supplementForm.supplementQty"
            :min="0.001"
            :precision="3"
            :step="1"
            style="width: 100%;"
          />
        </el-form-item>
        <el-form-item label="补料原因" prop="supplementReason">
          <el-input
            v-model="supplementForm.supplementReason"
            type="textarea"
            :rows="3"
            maxlength="200"
            show-word-limit
            placeholder="请输入补料原因"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" :loading="supplementSubmitting" @click="handleSubmitSupplement">确定</el-button>
          <el-button @click="supplementDialogVisible = false">取消</el-button>
        </span>
      </template>
    </FormDialog>
 
    <FormDialog
      v-model="returnDialogVisible"
      title="退料"
      width="500px"
      @confirm="handleSubmitReturn"
    >
      <el-form ref="returnFormRef" :model="returnForm" :rules="returnRules" label-width="120px">
        <el-form-item label="退料数量" prop="returnQty">
          <el-input-number
            v-model="returnForm.returnQty"
            :min="0.001"
            :precision="3"
            :step="1"
            style="width: 100%;"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" :loading="returnSubmitting" @click="handleSubmitReturn">确定</el-button>
          <el-button @click="returnDialogVisible = false">取消</el-button>
        </span>
      </template>
    </FormDialog>
 
    <el-dialog v-model="supplementRecordDialogVisible" title="补料记录" width="900px">
      <el-table v-loading="supplementRecordLoading" :data="supplementRecordTableData" border row-key="id">
        <el-table-column label="补料数量" prop="supplementQty" min-width="100" />
        <el-table-column label="补料原因" prop="supplementReason" min-width="200" />
        <el-table-column label="补料人" prop="supplementUserName" min-width="120" />
        <el-table-column label="补料日期" prop="supplementTime" min-width="160" />
      </el-table>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="supplementRecordDialogVisible = false">关闭</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
 
<script setup>
import { computed, nextTick, reactive, ref, watch } from "vue";
import { ElMessage } from "element-plus";
import FormDialog from "@/components/Dialog/FormDialog.vue";
import {
  listWorkOrderMaterialLedger,
  addWorkOrderMaterialSupplement,
  addWorkOrderMaterialReturn,
  listWorkOrderMaterialSupplementRecord,
} from "@/api/productionManagement/workOrder.js";
 
const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false,
  },
  rowData: {
    type: Object,
    default: () => null,
  },
});
 
const emit = defineEmits(["update:modelValue", "refresh"]);
 
const dialogVisible = computed({
  get: () => props.modelValue,
  set: val => emit("update:modelValue", val),
});
 
const materialTableLoading = ref(false);
const materialTableData = ref([]);
const currentMaterialRow = ref(null);
const currentMaterialOrderRow = ref(null);
 
const supplementDialogVisible = ref(false);
const supplementSubmitting = ref(false);
const supplementFormRef = ref(null);
const supplementForm = reactive({
  supplementQty: null,
  supplementReason: "",
});
 
const returnDialogVisible = ref(false);
const returnSubmitting = ref(false);
const returnFormRef = ref(null);
const returnForm = reactive({
  returnQty: null,
});
 
const supplementRecordDialogVisible = ref(false);
const supplementRecordLoading = ref(false);
const supplementRecordTableData = ref([]);
 
const supplementRules = {
  supplementQty: [{ required: true, message: "请输入补料数量", trigger: "blur" }],
  supplementReason: [{ required: true, message: "请输入补料原因", trigger: "blur" }],
};
const returnRules = {
  returnQty: [{ required: true, message: "请输入退料数量", trigger: "blur" }],
};
 
const loadMaterialTable = async row => {
  if (!row?.id) return;
  currentMaterialOrderRow.value = row;
  materialTableLoading.value = true;
  materialTableData.value = [];
  try {
    const res = await listWorkOrderMaterialLedger({
      workOrderId: row.id,
      processId: row.processId,
      productProcessRouteItemId: row.productProcessRouteItemId,
    });
    materialTableData.value = res.data || [];
  } catch (e) {
    console.error("获取物料台账失败", e);
    ElMessage.error("获取物料台账失败");
  } finally {
    materialTableLoading.value = false;
  }
};
 
watch(
  () => props.modelValue,
  visible => {
    if (visible && props.rowData) {
      loadMaterialTable(props.rowData);
    }
  }
);
 
const handleCloseMaterialDialog = () => {
  materialTableData.value = [];
  currentMaterialRow.value = null;
  currentMaterialOrderRow.value = null;
};
 
const openSupplementDialog = row => {
  currentMaterialRow.value = row;
  supplementForm.supplementQty = null;
  supplementForm.supplementReason = "";
  supplementDialogVisible.value = true;
  nextTick(() => {
    supplementFormRef.value?.clearValidate();
  });
};
 
const handleSubmitSupplement = () => {
  supplementFormRef.value?.validate(async valid => {
    if (!valid || !currentMaterialRow.value?.id) {
      ElMessage.warning("缺少物料明细ID");
      return;
    }
    supplementSubmitting.value = true;
    try {
      await addWorkOrderMaterialSupplement({
        materialLedgerId: currentMaterialRow.value.id,
        supplementQty: Number(supplementForm.supplementQty),
        supplementReason: supplementForm.supplementReason,
        workOrderId: currentMaterialOrderRow.value?.id,
      });
      supplementDialogVisible.value = false;
      await loadMaterialTable(currentMaterialOrderRow.value);
      ElMessage.success("补料成功");
      emit("refresh");
    } catch (e) {
      console.error("补料失败", e);
      ElMessage.error("补料失败");
    } finally {
      supplementSubmitting.value = false;
    }
  });
};
 
const openReturnDialog = row => {
  currentMaterialRow.value = row;
  returnForm.returnQty = null;
  returnDialogVisible.value = true;
  nextTick(() => {
    returnFormRef.value?.clearValidate();
  });
};
 
const handleSubmitReturn = () => {
  returnFormRef.value?.validate(async valid => {
    if (!valid || !currentMaterialRow.value?.id) {
      ElMessage.warning("缺少物料明细ID");
      return;
    }
    const returnQty = Number(returnForm.returnQty);
    const minQty =
      Number(currentMaterialRow.value.pickQty || 0) +
      Number(currentMaterialRow.value.supplementQty || 0);
    if (returnQty < minQty) {
      ElMessage.warning(`退料数量不能低于领用数量+补料数量(${minQty})`);
      return;
    }
    returnSubmitting.value = true;
    try {
      await addWorkOrderMaterialReturn({
        materialLedgerId: currentMaterialRow.value.id,
        returnQty,
        workOrderId: currentMaterialOrderRow.value?.id,
      });
      returnDialogVisible.value = false;
      await loadMaterialTable(currentMaterialOrderRow.value);
      ElMessage.success("退料成功");
      emit("refresh");
    } catch (e) {
      console.error("退料失败", e);
      ElMessage.error("退料失败");
    } finally {
      returnSubmitting.value = false;
    }
  });
};
 
const openSupplementRecordDialog = async row => {
  supplementRecordDialogVisible.value = true;
  supplementRecordLoading.value = true;
  supplementRecordTableData.value = [];
  try {
    const res = await listWorkOrderMaterialSupplementRecord({
      materialLedgerId: row.id,
    });
    supplementRecordTableData.value = res.data || [];
  } catch (e) {
    console.error("获取补料记录失败", e);
    ElMessage.error("获取补料记录失败");
  } finally {
    supplementRecordLoading.value = false;
  }
};
</script>