gaoluyang
2 天以前 b64a0deae5b5d33f9e20671a68936b27f0b9b00b
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
<script lang="ts" setup>
import type { WmsShipmentOrderApi } from '#/api/wms/order/shipment';
import type { WmsShipmentOrderDetailApi } from '#/api/wms/order/shipment/detail';
 
import { computed, nextTick, ref } from 'vue';
 
import { Barcode, BarcodeFormatEnum } from '@vben/common-ui';
import { DICT_TYPE } from '@vben/constants';
import { getDictLabel } from '@vben/hooks';
import { formatDate, formatDateTime } from '@vben/utils';
 
import {
  getShipmentOrder,
  getShipmentOrderDetailListByOrderId,
} from '#/api/wms/order/shipment';
import {
  formatPrice,
  formatQuantity,
  formatSumPrice,
  formatSumQuantity,
  multiplyPrice,
} from '#/views/wms/utils/format';
 
interface PrintRow extends WmsShipmentOrderDetailApi.ShipmentOrderDetail {
  totalPrice?: number;
}
 
defineOptions({ name: 'WmsShipmentOrderPrint' });
 
const printData = ref<WmsShipmentOrderApi.ShipmentOrder>({});
const tableColumnCount = 5;
 
const printRows = computed<PrintRow[]>(() =>
  (printData.value.details || []).map((detail) => ({
    ...detail,
    totalPrice:
      detail.totalPrice ?? multiplyPrice(detail.quantity, detail.price),
  })),
);
 
/** 等待条码和打印 DOM 完成绘制,避免浏览器打印到旧内容 */
function waitForPaint() {
  return new Promise<void>((resolve) => {
    requestAnimationFrame(() => {
      requestAnimationFrame(() => resolve());
    });
  });
}
 
/** 退出打印模式,恢复当前页面显示 */
function removePrintMode() {
  document.body.classList.remove('wms-shipment-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 getShipmentOrder(id);
  const details =
    order.details || (await getShipmentOrderDetailListByOrderId(id));
  printData.value = { ...order, details };
  await nextTick();
  await waitForPaint();
  document.body.classList.add('wms-shipment-order-printing');
  window.addEventListener('afterprint', removePrintMode, { once: true });
  window.print();
}
 
defineExpose({ print });
</script>
 
<template>
  <Teleport to="body">
    <div
      id="wmsShipmentOrderPrint"
      class="wms-shipment-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>
          出库类型:{{
            getPrintDictLabel(DICT_TYPE.WMS_SHIPMENT_ORDER_TYPE, printData.type)
          }}
        </div>
        <div>仓库:{{ printData.warehouseName || '-' }}</div>
        <div>
          出库状态:{{
            getPrintDictLabel(DICT_TYPE.WMS_ORDER_STATUS, printData.status)
          }}
        </div>
        <div>
          单据日期:{{ formatDate(printData.orderTime, 'YYYY-MM-DD') || '-' }}
        </div>
        <div>客户:{{ printData.merchantName || '-' }}</div>
        <div>业务单号:{{ printData.bizOrderNo || '-' }}</div>
        <div>总数量:{{ formatQuantity(printData.totalQuantity) || '-' }}</div>
        <div>总金额:{{ formatPrice(printData.totalPrice) || '-' }}</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>
          </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">
              {{ formatPrice(detail.totalPrice) || '-' }}
            </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"
            >
              {{ formatSumPrice(printRows, (detail) => detail.totalPrice) }}
            </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-shipment-order-printing) {
    padding: 0 !important;
    margin: 0 !important;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
 
  :global(body.wms-shipment-order-printing *) {
    visibility: hidden !important;
  }
 
  :global(body.wms-shipment-order-printing .wms-shipment-order-print),
  :global(body.wms-shipment-order-printing .wms-shipment-order-print *) {
    visibility: visible !important;
  }
 
  :global(body.wms-shipment-order-printing .wms-shipment-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>