<script lang="ts" setup>
|
import type { MesWmSortTaskApi } from '#/api/mes/wm/sorting';
|
|
import { computed, h, ref } from 'vue';
|
|
import { useVbenModal } from '@vben/common-ui';
|
|
import type { ColumnsType } from 'ant-design-vue/es/table/interface';
|
|
import {
|
Button,
|
Input,
|
InputNumber,
|
message,
|
Modal as AntModal,
|
Table,
|
Tag,
|
} from 'ant-design-vue';
|
|
import {
|
finishSortTask,
|
getSortTask,
|
scanSortLine,
|
submitSortProductionRequest,
|
} from '#/api/mes/wm/sorting';
|
|
import { SORT_TASK_STATUS } from '../data';
|
|
defineOptions({ name: 'MesWmSortTaskSortModal' });
|
|
const emit = defineEmits<{ success: [] }>();
|
|
const detail = ref<MesWmSortTaskApi.SortTask>({});
|
/** 每行的扫码输入 */
|
const scanBarcode = ref<Record<string, string>>({});
|
/** 每行的本次分拣数量(留空则按剩余数量) */
|
const scanQuantity = ref<Record<string, number | undefined>>({});
|
const carrier = ref('');
|
const shippingNumber = ref('');
|
|
const lines = computed<MesWmSortTaskApi.SortTaskLine[]>(
|
() => detail.value.lines ?? [],
|
);
|
|
/** 是否可执行分拣(待分拣 / 分拣中) */
|
const canSort = computed(
|
() =>
|
detail.value.status === SORT_TASK_STATUS.PENDING ||
|
detail.value.status === SORT_TASK_STATUS.SORTING,
|
);
|
|
/** 未完成的行 */
|
const unfinishedLines = computed(() =>
|
lines.value.filter(
|
(line) =>
|
!line.scanned ||
|
Number(line.sortedQuantity ?? 0) < Number(line.planQuantity ?? 0),
|
),
|
);
|
|
/** 是否全部扫码完成 */
|
const allSorted = computed(
|
() => lines.value.length > 0 && unfinishedLines.value.length === 0,
|
);
|
|
/** 剩余待分拣数量 */
|
function remainQuantity(line: MesWmSortTaskApi.SortTaskLine) {
|
return Math.max(
|
Number(line.planQuantity ?? 0) - Number(line.sortedQuantity ?? 0),
|
0,
|
);
|
}
|
|
/** 货位指引文案 */
|
function locationText(line: MesWmSortTaskApi.SortTaskLine) {
|
const parts = [line.warehouseName, line.areaName, line.locationName].filter(
|
Boolean,
|
);
|
return parts.length > 0 ? parts.join(' / ') : '-';
|
}
|
|
/** 重新加载任务详情 */
|
async function reloadDetail() {
|
if (!detail.value.id) {
|
return;
|
}
|
detail.value = await getSortTask(detail.value.id);
|
}
|
|
/** 扫码:校验条码并累加分拣数量 */
|
async function handleScan(line: MesWmSortTaskApi.SortTaskLine) {
|
const barcode = (scanBarcode.value[line.id!] ?? '').trim();
|
if (!barcode) {
|
message.warning(`请先扫描【${line.itemName}】的物料条码或批次号`);
|
return;
|
}
|
const quantity =
|
scanQuantity.value[line.id!] ?? (remainQuantity(line) || undefined);
|
try {
|
await scanSortLine({
|
taskId: detail.value.id!,
|
lineId: line.id!,
|
barcode,
|
quantity,
|
});
|
scanBarcode.value[line.id!] = '';
|
scanQuantity.value[line.id!] = undefined;
|
await reloadDetail();
|
message.success(`【${line.itemName}】扫码成功,已分拣 ${quantity}`);
|
emit('success');
|
} catch (error: any) {
|
// 后端已给出「条码 → 期望条码/批次」的对比说明,直接展示
|
message.error(error?.message || '扫码校验失败');
|
}
|
}
|
|
/** 完成分拣并触发配送 */
|
async function handleFinish() {
|
if (!allSorted.value) {
|
AntModal.warning({
|
title: '还不能完成分拣',
|
content: `以下明细未扫码或未分拣到计划数量:${unfinishedLines.value
|
.map(
|
(line) =>
|
`${line.itemName}(计划 ${line.planQuantity ?? 0},已分拣 ${
|
line.sortedQuantity ?? 0
|
})`,
|
)
|
.join(';')}。请逐条扫码完成后再次提交。`,
|
});
|
return;
|
}
|
AntModal.confirm({
|
title: '确认完成分拣并触发配送?',
|
content:
|
'完成后将自动生成对应的销售出库单(配送出库),并记录承运信息,任务不可再修改。',
|
onOk: async () => {
|
await finishSortTask({
|
id: detail.value.id!,
|
carrier: carrier.value,
|
shippingNumber: shippingNumber.value,
|
});
|
message.success('分拣完成,已生成销售出库单');
|
emit('success');
|
await modalApi.close();
|
},
|
});
|
}
|
|
/** 提交生产申请 */
|
async function handleSubmitProductionRequest() {
|
const result = await submitSortProductionRequest(detail.value.id!);
|
const failures = result.failures ?? [];
|
if (failures.length > 0) {
|
AntModal.warning({
|
title: `生产申请部分未提交成功(成功 ${result.createdCount ?? 0} 条)`,
|
content: h(
|
'div',
|
failures.map((item) => h('div', item)),
|
),
|
});
|
} else if ((result.createdCount ?? 0) === 0) {
|
message.info(
|
`本次没有新增生产申请:${result.skippedCount ?? 0} 条明细已有生产申请,可在「主生产计划」中查看并下发。`,
|
);
|
} else {
|
message.success(
|
`已提交 ${result.createdCount} 条生产申请(MPS 草稿),可在「主生产计划」中审核下发`,
|
);
|
}
|
emit('success');
|
}
|
|
const columns: ColumnsType<MesWmSortTaskApi.SortTaskLine> = [
|
{
|
title: '物料',
|
dataIndex: 'itemName',
|
width: 220,
|
customRender: ({ record }: { record: MesWmSortTaskApi.SortTaskLine }) =>
|
h('div', [
|
h('div', record.itemName ?? '-'),
|
h(
|
'div',
|
{ class: 'text-xs text-gray-500' },
|
`${record.itemCode ?? '-'}|条码 ${record.itemBarCode ?? '未维护'}`,
|
),
|
]),
|
},
|
{
|
title: '批次号',
|
dataIndex: 'batchCode',
|
width: 140,
|
customRender: ({ record }: { record: MesWmSortTaskApi.SortTaskLine }) =>
|
record.batchCode ?? '-',
|
},
|
{
|
title: '货位指引',
|
dataIndex: 'locationName',
|
width: 200,
|
customRender: ({ record }: { record: MesWmSortTaskApi.SortTaskLine }) =>
|
locationText(record),
|
},
|
{
|
title: '计划数量',
|
dataIndex: 'planQuantity',
|
width: 110,
|
align: 'right',
|
},
|
{
|
title: '已分拣',
|
dataIndex: 'sortedQuantity',
|
width: 100,
|
align: 'right',
|
},
|
{
|
title: '状态',
|
dataIndex: 'scanned',
|
width: 90,
|
customRender: ({ record }: { record: MesWmSortTaskApi.SortTaskLine }) =>
|
record.scanned && remainQuantity(record) === 0
|
? h(Tag, { color: 'success' }, '已完成')
|
: h(Tag, { color: 'warning' }, '待分拣'),
|
},
|
{
|
title: '扫码核验',
|
dataIndex: 'scan',
|
width: 320,
|
customRender: ({ record }: { record: MesWmSortTaskApi.SortTaskLine }) =>
|
h('div', { class: 'flex items-center gap-1' }, [
|
h(Input, {
|
value: scanBarcode.value[record.id!] ?? '',
|
size: 'small',
|
placeholder: '扫物料条码 / 批次号',
|
disabled: !canSort.value,
|
allowClear: true,
|
'onUpdate:value': (value: string) => {
|
scanBarcode.value[record.id!] = value;
|
},
|
onPressEnter: () => handleScan(record),
|
}),
|
h(InputNumber, {
|
value: scanQuantity.value[record.id!],
|
class: 'w-[90px]',
|
min: 0,
|
placeholder: String(remainQuantity(record)),
|
disabled: !canSort.value,
|
'onUpdate:value': (value: unknown) => {
|
scanQuantity.value[record.id!] =
|
value === null || value === undefined ? undefined : Number(value);
|
},
|
}),
|
h(
|
Button,
|
{
|
type: 'primary',
|
size: 'small',
|
disabled: !canSort.value,
|
onClick: () => handleScan(record),
|
},
|
() => '扫码',
|
),
|
]),
|
},
|
];
|
|
const [Modal, modalApi] = useVbenModal({
|
confirmText: '关闭',
|
async onConfirm() {
|
await modalApi.close();
|
},
|
async onOpenChange(isOpen: boolean) {
|
if (!isOpen) {
|
detail.value = {};
|
scanBarcode.value = {};
|
scanQuantity.value = {};
|
carrier.value = '';
|
shippingNumber.value = '';
|
return;
|
}
|
const data = modalApi.getData<{ id: number }>();
|
if (!data?.id) {
|
return;
|
}
|
modalApi.lock();
|
try {
|
detail.value = await getSortTask(data.id);
|
carrier.value = detail.value.carrier ?? '';
|
shippingNumber.value = detail.value.shippingNumber ?? '';
|
} finally {
|
modalApi.unlock();
|
}
|
},
|
});
|
</script>
|
|
<template>
|
<Modal
|
:title="`订单分拣 ${detail.code ? `- ${detail.code}` : ''}`"
|
class="w-[1100px]"
|
>
|
<div class="mb-3 grid grid-cols-3 gap-x-6 gap-y-1 text-sm">
|
<div>发货通知单:{{ detail.noticeCode || '-' }}</div>
|
<div>销售订单:{{ detail.saleOrderCode || '-' }}</div>
|
<div>收货人:{{ detail.recipientName || '-' }}</div>
|
<div>联系电话:{{ detail.recipientTelephone || '-' }}</div>
|
<div class="col-span-2">收货地址:{{ detail.recipientAddress || '-' }}</div>
|
<div v-if="detail.status === SORT_TASK_STATUS.FINISHED">
|
承运商:{{ detail.carrier || '-' }}
|
</div>
|
<div v-if="detail.status === SORT_TASK_STATUS.FINISHED">
|
物流单号:{{ detail.shippingNumber || '-' }}
|
</div>
|
</div>
|
<Table
|
:columns="columns"
|
:data-source="lines"
|
:pagination="false"
|
row-key="id"
|
size="small"
|
:scroll="{ y: 380 }"
|
bordered
|
/>
|
<div
|
v-if="canSort"
|
class="mt-3 flex items-center gap-2 rounded border border-solid border-[#dcdfe6] p-3"
|
>
|
<span class="text-sm">配送信息:</span>
|
<Input
|
v-model:value="carrier"
|
class="w-[180px]"
|
placeholder="承运商 / 配送方式"
|
allow-clear
|
/>
|
<Input
|
v-model:value="shippingNumber"
|
class="w-[200px]"
|
placeholder="物流单号"
|
allow-clear
|
/>
|
<div class="ml-auto text-sm">
|
已完成
|
{{ lines.length - unfinishedLines.length }} / {{ lines.length }} 行
|
</div>
|
</div>
|
<template #prepend-footer>
|
<div class="flex flex-auto items-center gap-2">
|
<Button
|
:disabled="!canSort || allSorted"
|
@click="handleSubmitProductionRequest"
|
>
|
提交生产申请
|
</Button>
|
<Button
|
v-if="canSort"
|
type="primary"
|
:disabled="!allSorted"
|
@click="handleFinish"
|
>
|
完成分拣并触发配送
|
</Button>
|
</div>
|
</template>
|
</Modal>
|
</template>
|