gaoluyang
2026-04-27 b52c08544fd4ab1e8ff0f09efaac160608fd90ce
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
<template>
  <div>
    <el-dialog v-model="dialogVisible" title="领料详情" width="1400px" @close="handleClose">
      <el-table v-loading="materialDetailLoading" :data="materialDetailTableData" border row-key="id">
        <el-table-column label="原料名称" prop="materialName" min-width="160" />
        <el-table-column label="原料型号" prop="materialModel" min-width="180" />
        <el-table-column label="原纸需要量" prop="basePaperQty" min-width="120" />
        <el-table-column label="纸箱需要量" prop="cartonQty" min-width="120" />
        <el-table-column label="原纸领用数量" prop="basePaperPickQty" min-width="120" />
        <el-table-column label="纸箱领用数量" prop="cartonPickQty" min-width="120" />
        <el-table-column label="退料数量" prop="returnQty" min-width="110" />
      </el-table>
      <template #footer>
        <span class="dialog-footer">
<!--          <el-button-->
<!--            type="warning"-->
<!--            :loading="materialReturnConfirming"-->
<!--            :disabled="!canOpenReturnSummary"-->
<!--            @click="openReturnSummaryDialog"-->
<!--          >-->
<!--            退料确认-->
<!--          </el-button>-->
          <el-button @click="dialogVisible = false">取消</el-button>
        </span>
      </template>
    </el-dialog>
 
    <el-dialog v-model="returnSummaryDialogVisible" title="退料汇总确认" width="900px">
      <el-table :data="returnSummaryList" border row-key="summaryKey">
        <el-table-column label="原料名称" prop="materialName" min-width="180" />
        <el-table-column label="原料型号" prop="materialModel" min-width="180" />
        <el-table-column label="退料汇总数量" prop="returnQtyTotal" min-width="140" />
      </el-table>
 
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" :loading="materialReturnConfirming" @click="handleReturnConfirm">确认提交</el-button>
          <el-button @click="returnSummaryDialogVisible = false">取消</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
 
<script setup>
import { computed, ref, watch } from "vue";
import { ElMessage } from "element-plus";
import { listMaterialPickingDetail, confirmMaterialReturn } from "@/api/productionManagement/productionOrder.js";
 
const props = defineProps({
  modelValue: { type: Boolean, default: false },
  orderRow: { type: Object, default: null },
});
const emit = defineEmits(["update:modelValue", "confirmed"]);
 
const dialogVisible = computed({
  get: () => props.modelValue,
  set: val => emit("update:modelValue", val),
});
 
const materialDetailLoading = ref(false);
const materialDetailTableData = ref([]);
const materialReturnConfirming = ref(false);
const returnSummaryDialogVisible = ref(false);
const returnSummaryList = ref([]);
const getPickQty = item => {
  const directPick = Number(item.pickQty ?? NaN);
  if (Number.isFinite(directPick)) return directPick;
  return Number(item.basePaperPickQty || 0) + Number(item.cartonPickQty || 0);
};
const calcReturnQty = item =>
  getPickQty(item) - Number(item.actualQty || 0);
const normalizeList = (res) => {
  if (Array.isArray(res?.data)) return res.data;
  if (Array.isArray(res?.data?.records)) return res.data.records;
  if (Array.isArray(res?.records)) return res.records;
  return [];
};
const canOpenReturnSummary = computed(() => {
  const list = Array.isArray(materialDetailTableData.value) ? materialDetailTableData.value : [];
  return list.some(item => calcReturnQty(item) > 0);
});
 
const loadDetailList = async () => {
  if (!props.orderRow?.id) return;
  materialDetailLoading.value = true;
  materialDetailTableData.value = [];
  try {
    const res = await listMaterialPickingDetail({ productOrderId: props.orderRow.id });
    materialDetailTableData.value = normalizeList(res);
  } finally {
    materialDetailLoading.value = false;
  }
};
 
watch(
  () => dialogVisible.value,
  visible => {
    if (visible) {
      loadDetailList();
    }
  }
);
 
const handleClose = () => {
  materialDetailTableData.value = [];
};
 
const buildReturnSummary = () => {
  const map = new Map();
  materialDetailTableData.value.forEach(item => {
    const returnQty = calcReturnQty(item);
    if (returnQty <= 0) return;
    const key = `${item.materialModelId || ""}_${item.materialName || ""}_${item.materialModel || ""}`;
    const old = map.get(key) || {
      summaryKey: key,
      materialName: item.materialName || "",
      materialModel: item.materialModel || "",
      returnQtyTotal: 0,
    };
    old.returnQtyTotal += returnQty;
    map.set(key, old);
  });
  return Array.from(map.values());
};
 
const openReturnSummaryDialog = async () => {
  if (!canOpenReturnSummary.value) {
    ElMessage.warning("退料数量=领用数量-实际数量,且需大于0");
    return;
  }
  returnSummaryList.value = buildReturnSummary();
  returnSummaryDialogVisible.value = true;
};
 
const handleReturnConfirm = async () => {
  if (!props.orderRow?.id) return;
  materialReturnConfirming.value = true;
  try {
    await confirmMaterialReturn({
      orderId: props.orderRow.id,
      returnSummaryList: returnSummaryList.value,
    });
    returnSummaryDialogVisible.value = false;
    dialogVisible.value = false;
    emit("confirmed");
  } finally {
    materialReturnConfirming.value = false;
  }
};
</script>
 
<style scoped lang="scss"></style>