gaoluyang
2026-11-02 2fe330a44f9e8bdd8a1afd6124c57851f2a5b7ea
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
<script lang="ts" setup>
import type { MesProWorkOrderBomApi } from '#/api/mes/pro/workorder/bom';
 
import { computed, ref, watch } from 'vue';
 
import { Alert, Table } from 'ant-design-vue';
 
import { getWorkOrderBomItemPreview } from '#/api/mes/pro/workorder/bom';
 
const props = defineProps<{
  productId?: number;
  quantity?: number;
}>();
 
const loading = ref(false);
const list = ref<MesProWorkOrderBomApi.BomItemPreview[]>([]);
 
/** 库存不充足的辅料(可用库存 + 在途采购 < 需求数量) */
const insufficientList = computed(() =>
  list.value.filter((item) => item.stockSufficient === false),
);
const enoughCount = computed(
  () => list.value.filter((item) => item.stockSufficient !== false).length,
);
/** 库存不足的辅料文案:物料名称(还差数量 单位) */
const insufficientText = computed(() =>
  insufficientList.value
    .map((item) => {
      const gap =
        (item.quantity ?? 0) -
        (item.stockQuantity ?? 0) -
        (item.purchaseOrderQuantity ?? 0);
      return `${item.itemName ?? item.itemCode}(还差 ${gap} ${item.unitMeasureName ?? ''})`;
    })
    .join('、'),
);
 
const columns = [
  { title: '物料编码', dataIndex: 'itemCode', width: 140 },
  { title: '物料名称', dataIndex: 'itemName', width: 180 },
  { title: '规格型号', dataIndex: 'itemSpecification', width: 140 },
  { title: '需求数量', dataIndex: 'quantity', width: 110 },
  { title: '单位', dataIndex: 'unitMeasureName', width: 80 },
  { title: '可用库存', dataIndex: 'stockQuantity', width: 110 },
  { title: '在途采购', dataIndex: 'purchaseOrderQuantity', width: 110 },
  { title: '库存判断', dataIndex: 'stockSufficient', width: 110 },
];
 
async function load() {
  list.value = [];
  if (!props.productId || !props.quantity || props.quantity <= 0) {
    return;
  }
  loading.value = true;
  try {
    list.value = await getWorkOrderBomItemPreview(
      props.productId,
      props.quantity,
    );
  } finally {
    loading.value = false;
  }
}
 
watch(() => [props.productId, props.quantity], load, { immediate: true });
 
defineExpose({
  getInsufficientItems: () => insufficientList.value,
  getList: () => list.value,
  refresh: load,
});
</script>
 
<template>
  <div>
    <div v-if="!productId || !quantity" class="py-8 text-center text-gray-400">
      请先选择产品并填写订单数量,系统将按工艺路线自动计算辅料用量
    </div>
    <template v-else>
      <Alert
        v-if="!loading && list.length > 0 && insufficientList.length > 0"
        class="mb-3"
        type="warning"
        show-icon
      >
        <template #message>
          有 {{ insufficientList.length }} 项辅料库存不足(可用库存 + 在途采购 <
          需求数量),需先行采购;另有 {{ enoughCount }} 项库存充足。
        </template>
        <template #description>
          <div class="leading-6">{{ insufficientText }}</div>
        </template>
      </Alert>
      <Alert
        v-else-if="!loading && list.length > 0"
        class="mb-3"
        type="success"
        show-icon
        message="全部辅料库存充足,可直接提交生成工单"
      />
      <Alert
        v-else-if="!loading"
        class="mb-3"
        type="info"
        show-icon
        message="未计算到辅料需求,请确认产品已配置工艺路线及投料 BOM"
      />
      <Table
        :columns="columns"
        :data-source="list"
        :loading="loading"
        :pagination="false"
        :scroll="{ y: 320 }"
        bordered
        row-key="itemId"
        size="small"
      >
        <template #bodyCell="{ column, record }">
          <template v-if="column.dataIndex === 'stockSufficient'">
            <span v-if="record.stockSufficient === false" class="text-red-500">
              库存不足
            </span>
            <span v-else class="text-green-600">充足</span>
          </template>
        </template>
      </Table>
    </template>
  </div>
</template>