gaoluyang
2026-05-28 ca8dfc8a7dd8ba9b84d9a58c3c2a6d52a0c98a74
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
<template>
  <el-dialog v-model="dialogVisible"
             title="补料"
             width="1400px"
             @close="handleClose">
    <el-table v-loading="loading"
              :data="tableData"
              border
              row-key="id">
      <el-table-column label="工序名称"
                       prop="operationName"
                       min-width="140" />
      <el-table-column label="原料名称"
                       prop="productName"
                       min-width="140" />
      <el-table-column label="原料型号"
                       prop="model"
                       min-width="140" />
      <el-table-column label="计量单位"
                       prop="unit"
                       width="100" />
      <el-table-column label="库存数量"
                       min-width="120">
        <template #default="{ row }">
          <span :class="{ 'text-danger': isStockInsufficient(row) }">
            {{ row.stockQuantity ?? '-' }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="需求数量"
                       prop="demandedQuantity"
                       width="100" />
      <el-table-column label="领用数量"
                       prop="pickQuantity"
                       width="100" />
      <el-table-column label="已补数量"
                       prop="feedingQty"
                       width="100" />
      <el-table-column label="补料数量"
                       min-width="150">
        <template #default="{ row }">
          <el-input-number v-model="row.newSupplementQty"
                           :min="0"
                           :precision="3"
                           :step="1"
                           controls-position="right"
                           placeholder="输入补料数量"
                           style="width: 100%;"
                           :class="{ 'is-stock-insufficient': isStockInsufficient(row) }" />
        </template>
      </el-table-column>
      <el-table-column label="补料原因"
                       min-width="200">
        <template #default="{ row }">
          <el-input v-model="row.newSupplementReason"
                    placeholder="输入补料原因"
                    maxlength="200"
                    show-word-limit />
        </template>
      </el-table-column>
    </el-table>
    <template #footer>
      <span class="dialog-footer">
        <el-button v-if="hasInsufficientStock"
                   type="warning"
                   @click="openPurchaseRequestDialog">采购申请</el-button>
        <el-button type="primary"
                   :loading="submitting"
                   @click="handleSubmit">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </template>
  </el-dialog>
  <PurchaseRequestDialog v-model="purchaseRequestDialogVisible"
                         :insufficient-items="insufficientStockItems"
                         :order-row="props.orderRow"
                         @saved="handlePurchaseRequestSaved" />
</template>
 
<script setup>
  import { computed, ref, watch } from "vue";
  import { ElMessage } from "element-plus";
  import PurchaseRequestDialog from "./PurchaseRequestDialog.vue";
  import {
    listMaterialPickingDetail,
    updateMaterialPickingLedger,
    listMaterialPickingBom,
  } from "@/api/productionManagement/productionOrder.js";
 
  const props = defineProps({
    modelValue: { type: Boolean, default: false },
    orderRow: { type: Object, default: null },
  });
  const emit = defineEmits(["update:modelValue", "saved"]);
 
  const dialogVisible = computed({
    get: () => props.modelValue,
    set: val => emit("update:modelValue", val),
  });
 
  const loading = ref(false);
  const submitting = ref(false);
  const tableData = ref([]);
  const purchaseRequestDialogVisible = ref(false);
 
  // 判断库存是否不足
  const isStockInsufficient = (row) => {
    const stockQuantity = Number(row.stockQuantity ?? 0);
    const supplementQty = Number(row.newSupplementQty ?? 0);
    return supplementQty > 0 && stockQuantity > 0 && supplementQty > stockQuantity;
  };
 
  // 库存不足的行
  const insufficientStockItems = computed(() => {
    return tableData.value.filter(row => isStockInsufficient(row));
  });
 
  // 是否有库存不足
  const hasInsufficientStock = computed(() => {
    return insufficientStockItems.value.length > 0;
  });
 
  const loadData = async () => {
    if (!props.orderRow?.id) return;
    loading.value = true;
    try {
      // 获取物料明细
      const res = await listMaterialPickingDetail(props.orderRow.id);
      // 获取库存数量
      const bomRes = await listMaterialPickingBom(props.orderRow.id);
      const bomList = Array.isArray(bomRes?.data)
        ? bomRes.data
        : bomRes?.data?.records || [];
      // 创建库存数量映射
      const stockMap = new Map();
      bomList.forEach(item => {
        const key = item.materialModelId || item.productModelId;
        if (key) {
          stockMap.set(key, item.stockQuantity ?? item.stockQty ?? 0);
        }
      });
      // 合并库存数据
      tableData.value = (res.data || []).map(item => ({
        ...item,
        newSupplementQty: 0,
        newSupplementReason: "",
        stockQuantity: stockMap.get(item.productModelId) ?? null,
      }));
    } catch (e) {
      console.error("获取物料明细失败:", e);
      ElMessage.error("获取物料明细失败");
    } finally {
      loading.value = false;
    }
  };
 
  watch(
    () => dialogVisible.value,
    visible => {
      if (visible) {
        loadData();
      }
    }
  );
 
  const handleClose = () => {
    tableData.value = [];
  };
 
  const handleSubmit = async () => {
    const supplementList = tableData.value.filter(
      item => item.newSupplementQty > 0
    );
    if (supplementList.length === 0) {
      ElMessage.warning("请至少输入一条补料数量");
      return;
    }
 
    const invalidRow = supplementList.find(item => !item.newSupplementReason);
    if (invalidRow) {
      ElMessage.warning("请输入补料原因");
      return;
    }
 
    submitting.value = true;
    try {
      await updateMaterialPickingLedger({
        productionOrderId: props.orderRow.id,
        productionOrderPickDto: tableData.value.map(item => ({
          id: item.id,
          technologyOperationId: item.technologyOperationId,
          operationName: item.operationName,
          bom: item.bom === true,
          productModelId: item.productModelId,
          demandedQuantity: item.demandedQuantity,
          unit: item.unit,
          pickQuantity: item.pickQuantity,
          batchNo: item.batchNo,
          feedingQuantity: item.newSupplementQty || 0,
          feedingReason: item.newSupplementReason || "",
          pickType: 2,
        })),
      });
      ElMessage.success("补料成功");
      dialogVisible.value = false;
      emit("saved");
    } catch (e) {
      console.error("补料失败:", e);
      ElMessage.error("补料失败");
    } finally {
      submitting.value = false;
    }
  };
 
  // 打开采购申请对话框
  const openPurchaseRequestDialog = () => {
    purchaseRequestDialogVisible.value = true;
  };
 
  // 采购申请保存回调
  const handlePurchaseRequestSaved = () => {
    // 采购申请保存成功后刷新数据
    loadData();
  };
</script>
 
<style scoped lang="scss">
  .text-danger {
    color: #f56c6c;
    font-weight: bold;
  }
 
  :deep(.is-stock-insufficient) {
    .el-input__wrapper {
      box-shadow: 0 0 0 1px #f56c6c inset;
    }
  }
</style>