gaoluyang
2026-06-24 712aa51536236d43e87273e4ce45ac5691dffad8
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<script lang="ts" setup>
import type { WmsCheckOrderApi } from '#/api/wms/order/check';
import type { WmsCheckOrderDetailApi } from '#/api/wms/order/check/detail';
 
import { computed, nextTick, ref } from 'vue';
 
import { Barcode, BarcodeFormatEnum } from '..\..\..\..\..\packages\effects\common-ui\src';
import { DICT_TYPE } from '..\..\..\..\..\packages\constants\src';
import { getDictLabel } from '..\..\..\..\..\packages\effects\hooks\src';
import { formatDate, formatDateTime } from '..\..\..\..\..\packages\utils\src';
 
import {
  getCheckOrder,
  getCheckOrderDetailListByOrderId,
} from '#/api/wms/order/check';
import {
  formatPrice,
  formatQuantity,
  formatSumPrice,
  formatSumQuantity,
  getLossClass,
  sumPrice,
  sumQuantity,
} from '#/views/wms/utils/format';
 
import {
  getDetailActualPrice,
  getDetailDifferencePrice,
  getDetailDifferenceQuantity,
  getOrderDifferencePrice,
} from '../data';
 
interface PrintRow extends WmsCheckOrderDetailApi.CheckOrderDetail {
  actualPrice?: number;
  differencePrice?: number;
  differenceQuantity?: number;
}
 
defineOptions({ name: 'WmsCheckOrderPrint' });
 
const printData = ref<WmsCheckOrderApi.CheckOrder>({});
const tableColumnCount = 8;
 
/** 打印明细补齐实际金额和盈亏字段,避免模板重复计算 */
const printRows = computed<PrintRow[]>(() =>
  (printData.value.details || []).map((detail) => ({
    ...detail,
    actualPrice: getDetailActualPrice(detail),
    differencePrice: getDetailDifferencePrice(detail),
    differenceQuantity: getDetailDifferenceQuantity(detail),
  })),
);
 
/** 打印合计盈亏数量 */
const totalDifferenceQuantity = computed(() =>
  sumQuantity(printRows.value, (detail) => detail.differenceQuantity),
);
/** 打印合计盈亏金额 */
const totalDifferencePrice = computed(() =>
  sumPrice(printRows.value, (detail) => detail.differencePrice),
);
 
/** 等待条码和打印 DOM 完成绘制,避免浏览器打印到旧内容 */
function waitForPaint() {
  return new Promise<void>((resolve) => {
    requestAnimationFrame(() => {
      requestAnimationFrame(() => resolve());
    });
  });
}
 
/** 退出打印模式,恢复当前页面显示 */
function removePrintMode() {
  document.body.classList.remove('wms-check-order-printing');
}
 
/** 获取打印用字典文案,空值统一显示为横杠 */
function getPrintDictLabel(dictType: string, value?: number) {
  if (value === undefined || value === null) {
    return '-';
  }
  return getDictLabel(dictType, value) || '-';
}
 
/** 打印盘库单:加载数据后只展示打印区域,再调用浏览器打印 */
async function print(id: number) {
  const order = await getCheckOrder(id);
  const details = order.details || (await getCheckOrderDetailListByOrderId(id));
  printData.value = { ...order, details };
  await nextTick();
  await waitForPaint();
  document.body.classList.add('wms-check-order-printing');
  window.addEventListener('afterprint', removePrintMode, { once: true });
  window.print();
}
 
defineExpose({ print });
</script>
 
<template>
  <Teleport to="body">
    <div
      id="wmsCheckOrderPrint"
      class="wms-check-order-print pointer-events-none fixed left-0 top-0 z-[-1] w-full bg-white text-[#303133] opacity-0"
    >
      <div class="relative mb-2">
        <h2 class="m-0 text-center text-[1.5em] font-bold leading-[1.2]">
          盘库单
        </h2>
        <div v-if="printData.no" class="absolute right-0 top-0">
          <Barcode
            :content="printData.no"
            :display-value="false"
            :format="BarcodeFormatEnum.CODE39"
            :height="40"
            :width="180"
          />
        </div>
      </div>
      <div class="mb-3 grid grid-cols-3 gap-x-6 gap-y-2 text-sm leading-[1.5]">
        <div>盘库单号:{{ printData.no || '-' }}</div>
        <div>仓库:{{ printData.warehouseName || '-' }}</div>
        <div>
          盘库状态:{{
            getPrintDictLabel(DICT_TYPE.WMS_ORDER_STATUS, printData.status)
          }}
        </div>
        <div>
          单据日期:{{ formatDate(printData.orderTime, 'YYYY-MM-DD') || '-' }}
        </div>
        <div>
          盈亏数量:
          <span :class="getLossClass(printData.totalQuantity)">
            {{ formatQuantity(printData.totalQuantity) || '-' }}
          </span>
        </div>
        <div>总金额:{{ formatPrice(printData.totalPrice) || '-' }}</div>
        <div>实际金额:{{ formatPrice(printData.actualPrice) || '-' }}</div>
        <div>
          实际盈亏金额:
          <span :class="getLossClass(getOrderDifferencePrice(printData))">
            {{ formatPrice(getOrderDifferencePrice(printData)) || '-' }}
          </span>
        </div>
        <div class="col-span-3 grid grid-cols-2 gap-x-6">
          <div>
            创建:{{ formatDateTime(printData.createTime) || '-' }} /
            {{ printData.creatorName || printData.creator || '-' }}
          </div>
          <div>
            更新:{{ formatDateTime(printData.updateTime) || '-' }} /
            {{ printData.updaterName || printData.updater || '-' }}
          </div>
        </div>
        <div class="col-span-3">备注:{{ printData.remark || '-' }}</div>
      </div>
      <table class="w-full border-collapse text-[13px] leading-[1.5]">
        <thead>
          <tr>
            <th
              class="border border-solid border-[#dcdfe6] bg-[#f5f7fa] p-2 text-left font-bold"
            >
              商品信息
            </th>
            <th
              class="border border-solid border-[#dcdfe6] bg-[#f5f7fa] p-2 text-left font-bold"
            >
              规格信息
            </th>
            <th
              class="border border-solid border-[#dcdfe6] bg-[#f5f7fa] p-2 text-left font-bold"
            >
              账面库存
            </th>
            <th
              class="border border-solid border-[#dcdfe6] bg-[#f5f7fa] p-2 text-left font-bold"
            >
              单价(元)
            </th>
            <th
              class="border border-solid border-[#dcdfe6] bg-[#f5f7fa] p-2 text-left font-bold"
            >
              实际库存
            </th>
            <th
              class="border border-solid border-[#dcdfe6] bg-[#f5f7fa] p-2 text-left font-bold"
            >
              实际金额(元)
            </th>
            <th
              class="border border-solid border-[#dcdfe6] bg-[#f5f7fa] p-2 text-left font-bold"
            >
              盈亏数
            </th>
            <th
              class="border border-solid border-[#dcdfe6] bg-[#f5f7fa] p-2 text-left font-bold"
            >
              实际盈亏金额(元)
            </th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="detail in printRows" :key="detail.id || detail.skuId">
            <td class="border border-solid border-[#dcdfe6] p-2">
              <div>{{ detail.itemName || '-' }}</div>
              <div v-if="detail.itemCode" class="text-xs">
                编号:{{ detail.itemCode }}
              </div>
            </td>
            <td class="border border-solid border-[#dcdfe6] p-2">
              <div>{{ detail.skuName || '-' }}</div>
              <div v-if="detail.skuCode" class="text-xs">
                编号:{{ detail.skuCode }}
              </div>
            </td>
            <td class="border border-solid border-[#dcdfe6] p-2 text-right">
              {{ formatQuantity(detail.quantity) || '-' }}
            </td>
            <td class="border border-solid border-[#dcdfe6] p-2 text-right">
              {{ formatPrice(detail.price) || '-' }}
            </td>
            <td class="border border-solid border-[#dcdfe6] p-2 text-right">
              {{ formatQuantity(detail.checkQuantity) || '-' }}
            </td>
            <td class="border border-solid border-[#dcdfe6] p-2 text-right">
              {{ formatPrice(detail.actualPrice) || '-' }}
            </td>
            <td class="border border-solid border-[#dcdfe6] p-2 text-right">
              <span :class="getLossClass(detail.differenceQuantity)">
                {{ formatQuantity(detail.differenceQuantity) || '-' }}
              </span>
            </td>
            <td class="border border-solid border-[#dcdfe6] p-2 text-right">
              <span :class="getLossClass(detail.differencePrice)">
                {{ formatPrice(detail.differencePrice) || '-' }}
              </span>
            </td>
          </tr>
          <tr v-if="printRows.length > 0">
            <td
              class="border border-solid border-[#dcdfe6] bg-[#f5f7fa] p-2"
              colspan="2"
            >
              合计
            </td>
            <td
              class="border border-solid border-[#dcdfe6] bg-[#f5f7fa] p-2 text-right"
            >
              {{ formatSumQuantity(printRows, (detail) => detail.quantity) }}
            </td>
            <td
              class="border border-solid border-[#dcdfe6] bg-[#f5f7fa] p-2 text-right"
            ></td>
            <td
              class="border border-solid border-[#dcdfe6] bg-[#f5f7fa] p-2 text-right"
            >
              {{
                formatSumQuantity(printRows, (detail) => detail.checkQuantity)
              }}
            </td>
            <td
              class="border border-solid border-[#dcdfe6] bg-[#f5f7fa] p-2 text-right"
            >
              {{ formatSumPrice(printRows, (detail) => detail.actualPrice) }}
            </td>
            <td
              class="border border-solid border-[#dcdfe6] bg-[#f5f7fa] p-2 text-right"
            >
              <span :class="getLossClass(totalDifferenceQuantity)">
                {{ formatQuantity(totalDifferenceQuantity) }}
              </span>
            </td>
            <td
              class="border border-solid border-[#dcdfe6] bg-[#f5f7fa] p-2 text-right"
            >
              <span :class="getLossClass(totalDifferencePrice)">
                {{ formatPrice(totalDifferencePrice) }}
              </span>
            </td>
          </tr>
          <tr v-if="printRows.length === 0">
            <td
              class="border border-solid border-[#dcdfe6] p-2 text-center"
              :colspan="tableColumnCount"
            >
              暂无明细
            </td>
          </tr>
        </tbody>
      </table>
    </div>
  </Teleport>
</template>
 
<style scoped>
@page {
  margin: 8mm 10mm;
}
 
@media print {
  :global(body.wms-check-order-printing) {
    padding: 0 !important;
    margin: 0 !important;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
 
  :global(body.wms-check-order-printing *) {
    visibility: hidden !important;
  }
 
  :global(body.wms-check-order-printing .wms-check-order-print),
  :global(body.wms-check-order-printing .wms-check-order-print *) {
    visibility: visible !important;
  }
 
  :global(body.wms-check-order-printing .wms-check-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>