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
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
<script lang="ts" setup>
import type { MesProWorkOrderBomApi } from '#/api/mes/pro/workorder/bom';
 
import { computed, nextTick, ref } from 'vue';
 
import { formatDate } from '@vben/utils';
 
import { getWorkOrder } from '#/api/mes/pro/workorder';
import type { MesProWorkOrderApi } from '#/api/mes/pro/workorder';
import { getWorkOrderBomItemListByWorkOrderId } from '#/api/mes/pro/workorder/bom';
import { getReturnableIssueLines } from '#/api/mes/wm/productissue/line';
 
defineOptions({ name: 'MesProWorkOrderPrint' });
 
const PRINT_CLASS = 'mes-pro-work-order-printing';
 
const workOrder = ref<MesProWorkOrderApi.WorkOrder>({});
const bomList = ref<MesProWorkOrderBomApi.WorkOrderBom[]>([]);
const issuedMap = ref<Record<number, number>>({});
 
/** 打印行:在 BOM 物料基础上补充已领数量 */
const printRows = computed(() =>
  bomList.value.map((item, index) => ({
    index: index + 1,
    ...item,
    issuedQuantity: item.itemId ? (issuedMap.value[item.itemId] ?? 0) : 0,
  })),
);
 
/** 等待打印 DOM 完成绘制,避免浏览器打印到旧内容 */
function waitForPaint() {
  return new Promise<void>((resolve) => {
    requestAnimationFrame(() => {
      requestAnimationFrame(() => resolve());
    });
  });
}
 
/** 退出打印模式,恢复当前页面显示 */
function removePrintMode() {
  document.body.classList.remove(PRINT_CLASS);
}
 
/** 打印领料单:按工单 BOM 生成领料单,已领数量取该工单已完成领料单的汇总 */
async function print(workOrderId: number) {
  const [detail, bom] = await Promise.all([
    getWorkOrder(workOrderId),
    getWorkOrderBomItemListByWorkOrderId(workOrderId),
  ]);
  workOrder.value = detail || {};
  bomList.value = bom || [];
  // 已领数量:已完成领料单的可退料行中 quantity 即"领料数量 - 已退数量"
  try {
    const lines = await getReturnableIssueLines(workOrderId);
    const map: Record<number, number> = {};
    (lines || []).forEach((line) => {
      if (!line.itemId) {
        return;
      }
      map[line.itemId] = (map[line.itemId] ?? 0) + (line.quantity ?? 0);
    });
    issuedMap.value = map;
  } catch {
    issuedMap.value = {};
  }
  await nextTick();
  await waitForPaint();
  document.body.classList.add(PRINT_CLASS);
  window.addEventListener('afterprint', removePrintMode, { once: true });
  window.print();
}
 
defineExpose({ print });
</script>
 
<template>
  <Teleport to="body">
    <div
      class="mes-pro-work-order-print pointer-events-none fixed left-0 top-0 z-[-1] w-full bg-white text-[#303133] opacity-0"
    >
      <div class="mb-2">
        <h2 class="m-0 text-center text-[1.5em] font-bold leading-[1.2]">
          生产领料单
        </h2>
        <div class="mt-2 grid grid-cols-2 gap-x-6 gap-y-1 text-[0.85em]">
          <div>工单编号:{{ workOrder.code || '-' }}</div>
          <div>作业名称:{{ workOrder.name || '-' }}</div>
          <div>产品名称:{{ workOrder.productName || '-' }}</div>
          <div>产品编码:{{ workOrder.productCode || '-' }}</div>
          <div>生产数量:{{ workOrder.quantity ?? '-' }}</div>
          <div>单位:{{ workOrder.unitMeasureName || '-' }}</div>
          <div>工厂:{{ workOrder.factoryName || '-' }}</div>
          <div>车间:{{ workOrder.workshopName || '-' }}</div>
          <div>
            需求日期:{{
              formatDate(workOrder.requestDate, 'YYYY-MM-DD') || '-'
            }}
          </div>
          <div>打印时间:{{ formatDate(new Date()) }}</div>
        </div>
      </div>
      <table class="w-full border-collapse text-[0.8em]">
        <thead>
          <tr>
            <th>序号</th>
            <th>物料编码</th>
            <th>物料名称</th>
            <th>规格型号</th>
            <th>单位</th>
            <th>需求数量</th>
            <th>已领数量</th>
            <th>本次领料</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="row in printRows" :key="row.id || row.itemId">
            <td class="text-center">{{ row.index }}</td>
            <td>{{ row.itemCode || '-' }}</td>
            <td>{{ row.itemName || '-' }}</td>
            <td>{{ row.itemSpecification || '-' }}</td>
            <td class="text-center">{{ row.unitMeasureName || '-' }}</td>
            <td class="text-right">{{ row.quantity ?? '-' }}</td>
            <td class="text-right">{{ row.issuedQuantity }}</td>
            <td></td>
          </tr>
          <tr v-if="printRows.length === 0">
            <td class="text-center" colspan="8">暂无辅料需求</td>
          </tr>
        </tbody>
      </table>
      <div class="mt-6 flex justify-between text-[0.85em]">
        <span>领料人:____________</span>
        <span>发料人:____________</span>
        <span>仓管员:____________</span>
        <span>领料日期:____________</span>
      </div>
    </div>
  </Teleport>
</template>
 
<style scoped>
@page {
  margin: 8mm 10mm;
}
 
.mes-pro-work-order-print table {
  border: 1px solid #333;
}
 
.mes-pro-work-order-print th,
.mes-pro-work-order-print td {
  padding: 4px 6px;
  border: 1px solid #333;
  word-break: break-all;
}
 
.mes-pro-work-order-print th {
  font-weight: 600;
  text-align: center;
  background: #f2f2f2;
}
 
@media print {
  :global(body.mes-pro-work-order-printing) {
    padding: 0 !important;
    margin: 0 !important;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
 
  :global(body.mes-pro-work-order-printing *) {
    visibility: hidden !important;
  }
 
  :global(body.mes-pro-work-order-printing .mes-pro-work-order-print),
  :global(body.mes-pro-work-order-printing .mes-pro-work-order-print *) {
    visibility: visible !important;
  }
 
  :global(body.mes-pro-work-order-printing .mes-pro-work-order-print) {
    position: absolute;
    top: 0;
    left: 0;
    z-index: auto;
    box-sizing: border-box;
    width: 100%;
    padding: 0 !important;
    margin: 0 !important;
    pointer-events: auto;
    opacity: 1;
  }
}
</style>