814 lines
25 KiB
Vue
814 lines
25 KiB
Vue
<template>
|
||
<el-dialog
|
||
v-model="visible"
|
||
:title="isBatchMode ? '批量导入入库' : '导入入库'"
|
||
:width="isBatchMode ? '980px' : '520px'"
|
||
align-center
|
||
append-to-body
|
||
destroy-on-close
|
||
:close-on-click-modal="!submitting"
|
||
@closed="handleClosed"
|
||
>
|
||
<template v-if="!isBatchMode && singleTarget">
|
||
<div class="item-summary">
|
||
<div class="summary-item">
|
||
<span class="summary-label">商品编码</span>
|
||
<span class="summary-value">{{ singleTarget.warehouse_item_code || '—' }}</span>
|
||
</div>
|
||
<div class="summary-item">
|
||
<span class="summary-label">商品名称</span>
|
||
<span class="summary-value">{{ singleTarget.warehouse_item_name || '—' }}</span>
|
||
</div>
|
||
<div class="summary-item">
|
||
<span class="summary-label">计划数量</span>
|
||
<span class="summary-value">{{ singleTarget.count ?? '—' }}</span>
|
||
</div>
|
||
<div class="summary-item">
|
||
<span class="summary-label">剩余可入</span>
|
||
<span class="summary-value">{{ singleTarget.remaining_quantity ?? 0 }}</span>
|
||
</div>
|
||
</div>
|
||
|
||
<el-form ref="singleFormRef" :model="singleForm" :rules="singleFormRules" label-width="100px" size="small">
|
||
<el-form-item label="SKU">
|
||
<el-input :model-value="String(singleTarget.stock_id ?? '')" disabled />
|
||
</el-form-item>
|
||
<el-form-item v-if="!showContainerPicker" label="入库数量" prop="quantity">
|
||
<el-input-number
|
||
v-model="singleForm.quantity"
|
||
:min="1"
|
||
:max="singleMaxQuantity"
|
||
:precision="0"
|
||
controls-position="right"
|
||
style="width: 100%"
|
||
/>
|
||
</el-form-item>
|
||
<el-form-item label="入库方式" prop="inbound_type">
|
||
<el-radio-group v-model="singleForm.inbound_type">
|
||
<el-radio
|
||
v-for="opt in directInboundInboundTypeOptions"
|
||
:key="opt.value"
|
||
:label="opt.value"
|
||
>
|
||
{{ opt.label }}
|
||
</el-radio>
|
||
</el-radio-group>
|
||
</el-form-item>
|
||
<el-form-item v-if="showContainerPicker" label="收货容器" prop="receiving_detail_id">
|
||
<el-select
|
||
v-model="singleForm.receiving_detail_id"
|
||
placeholder="请选择收货容器"
|
||
filterable
|
||
clearable
|
||
:loading="containerLoading"
|
||
style="width: 100%"
|
||
@visible-change="(open: boolean) => open && loadContainers()"
|
||
>
|
||
<el-option
|
||
v-for="item in containerOptions"
|
||
:key="item.detail_id"
|
||
:label="formatContainerLabel(item)"
|
||
:value="Number(item.detail_id)"
|
||
/>
|
||
</el-select>
|
||
</el-form-item>
|
||
<el-form-item label="库位">
|
||
<el-input :model-value="singleSpaceDisplay" readonly>
|
||
<template #append>
|
||
<el-button @click="openSpacePicker('single')">选择系统库位</el-button>
|
||
</template>
|
||
</el-input>
|
||
</el-form-item>
|
||
<el-form-item label="备注" prop="remark">
|
||
<el-input
|
||
v-model="singleForm.remark"
|
||
type="textarea"
|
||
:rows="3"
|
||
placeholder="如:导入文件名"
|
||
/>
|
||
</el-form-item>
|
||
</el-form>
|
||
</template>
|
||
|
||
<template v-else-if="isBatchMode">
|
||
<div class="batch-import-panel">
|
||
<el-alert
|
||
type="info"
|
||
:closable="false"
|
||
show-icon
|
||
title="请上传与入库明细表格字段一致的 Excel 文件,至少填写「SKU」和「入库数量」(或「实收数量」)。解析后可在下方表格中手动修改。"
|
||
class="batch-tip"
|
||
/>
|
||
|
||
<div class="batch-toolbar">
|
||
<el-button size="small" @click="handleDownloadTemplate">下载模板</el-button>
|
||
<el-upload
|
||
accept=".xlsx,.xls"
|
||
:auto-upload="false"
|
||
:show-file-list="false"
|
||
:disabled="submitting || parsing"
|
||
@change="handleFileChange"
|
||
>
|
||
<el-button size="small" type="primary" :loading="parsing">选择 Excel 文件</el-button>
|
||
</el-upload>
|
||
<span v-if="importFileName" class="file-name">{{ importFileName }}</span>
|
||
</div>
|
||
|
||
<el-table
|
||
v-if="parsedRows.length"
|
||
:data="parsedRows"
|
||
border
|
||
size="small"
|
||
max-height="360"
|
||
>
|
||
<el-table-column prop="rowIndex" label="Excel行" width="72" align="center" />
|
||
<el-table-column prop="stock_id" label="SKU" width="90" align="right" />
|
||
<el-table-column prop="warehouse_item_code" label="商品编码" min-width="120" show-overflow-tooltip />
|
||
<el-table-column prop="warehouse_item_name" label="商品名称" min-width="140" show-overflow-tooltip />
|
||
<el-table-column prop="remaining_quantity" label="剩余可入" width="82" align="right" />
|
||
<el-table-column label="入库数量" width="120" align="center">
|
||
<template #default="{ row }">
|
||
<el-input-number
|
||
v-model="row.quantity"
|
||
:min="1"
|
||
:max="getBatchRowMaxQuantity(row)"
|
||
:precision="0"
|
||
size="small"
|
||
controls-position="right"
|
||
style="width: 100%"
|
||
/>
|
||
</template>
|
||
</el-table-column>
|
||
<el-table-column label="入库方式" width="110" align="center">
|
||
<template #default="{ row }">
|
||
<el-select v-model="row.inbound_type" size="small" style="width: 100%">
|
||
<el-option
|
||
v-for="opt in directInboundInboundTypeOptions"
|
||
:key="opt.value"
|
||
:label="opt.label"
|
||
:value="opt.value"
|
||
/>
|
||
</el-select>
|
||
</template>
|
||
</el-table-column>
|
||
<el-table-column label="库位ID" width="168">
|
||
<template #default="{ row }">
|
||
<div class="batch-cell-action">
|
||
<span class="space-keyword-text">{{ row.space_keyword || '--' }}</span>
|
||
<el-button type="primary" link size="small" @click="openBatchRowSpacePicker(row)">
|
||
选择库位
|
||
</el-button>
|
||
</div>
|
||
</template>
|
||
</el-table-column>
|
||
<el-table-column label="备注" min-width="120">
|
||
<template #default="{ row }">
|
||
<el-input v-model="row.remark" size="small" placeholder="可选" clearable />
|
||
</template>
|
||
</el-table-column>
|
||
<el-table-column label="操作" width="64" align="center" fixed="right">
|
||
<template #default="{ $index }">
|
||
<el-button link type="danger" size="small" @click="removeParsedRow($index)">删除</el-button>
|
||
</template>
|
||
</el-table-column>
|
||
</el-table>
|
||
</div>
|
||
</template>
|
||
|
||
<template #footer>
|
||
<el-button :disabled="submitting" @click="visible = false">取消</el-button>
|
||
<el-button
|
||
type="primary"
|
||
:loading="submitting"
|
||
:disabled="isBatchMode && !parsedRows.length"
|
||
@click="handleSubmit"
|
||
>
|
||
确认导入
|
||
</el-button>
|
||
</template>
|
||
</el-dialog>
|
||
|
||
<el-dialog
|
||
v-model="spacePickerVisible"
|
||
title="选择系统库位"
|
||
align-center
|
||
width="900px"
|
||
append-to-body
|
||
destroy-on-close
|
||
>
|
||
<div class="system-product-picker">
|
||
<div class="picker-toolbar">
|
||
<el-input
|
||
v-model="spaceKeyword"
|
||
placeholder="输入库位名称或编码"
|
||
clearable
|
||
style="width: 320px"
|
||
@keyup.enter="querySpaces(1)"
|
||
>
|
||
<template #append>
|
||
<el-button :icon="Search" :loading="spaceLoading" @click="querySpaces(1)">查询</el-button>
|
||
</template>
|
||
</el-input>
|
||
</div>
|
||
|
||
<div class="picker-table-wrap">
|
||
<UpperTable
|
||
:data="spaceResults"
|
||
:loading="spaceLoading"
|
||
:show-selection="true"
|
||
:multiple-selection="spaceSelection"
|
||
:pagination="{
|
||
currentPage: spacePagination.page,
|
||
pageSize: spacePagination.pageSize,
|
||
total: spacePagination.total,
|
||
}"
|
||
:columns="[
|
||
{ prop: 'id', label: '库位ID', width: '120' },
|
||
{ prop: 'display_name', label: '显示名称', minWidth: '220' },
|
||
{ prop: 'space_name', label: '默认名称', minWidth: '220' },
|
||
{ prop: 'space_code', label: '库位编码', width: '180' },
|
||
]"
|
||
@selection-change="handleSpaceSelectionChange"
|
||
@row-click="handleSpaceRowClick"
|
||
@page-change="querySpaces"
|
||
@page-size-change="onSpacePageSizeChange"
|
||
/>
|
||
</div>
|
||
</div>
|
||
<template #footer>
|
||
<el-button @click="spacePickerVisible = false">取消</el-button>
|
||
<el-button type="primary" @click="applySelectedSpace">使用选中库位</el-button>
|
||
</template>
|
||
</el-dialog>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { computed, reactive, ref, watch } from 'vue'
|
||
import { ElMessage, type FormInstance, type FormRules, type UploadFile } from 'element-plus'
|
||
import { Search } from '@element-plus/icons-vue'
|
||
import UpperTable from '@/components/UpperTable/index.vue'
|
||
import * as indexApi from '@/api/index/index'
|
||
import { addItem, containerList, type EntryContainerListItem } from '@/api/storage.entry/entry'
|
||
import { directInboundInboundTypeOptions } from '../entry-config'
|
||
import {
|
||
downloadDirectInboundImportTemplate,
|
||
getSpaceDisplayName,
|
||
parseDirectInboundXlsxRows,
|
||
readDirectInboundXlsxFile,
|
||
resolveParsedRowSpaces,
|
||
resolveSpaceByKeyword,
|
||
type DirectInboundImportRow,
|
||
type ParsedDirectInboundImportRow,
|
||
} from '../utils/direct-inbound-xlsx-import'
|
||
|
||
export type { DirectInboundImportRow }
|
||
|
||
const props = defineProps<{
|
||
modelValue: boolean
|
||
/** 入库单 ID(storage.entry.addItem 必填) */
|
||
entryOrderId?: string | number
|
||
entryOrderCode?: string
|
||
/** 入库单状态,PENDING + PDA 时需选择收货容器 */
|
||
orderStatus?: string
|
||
/** 单条导入目标 */
|
||
targetItem?: DirectInboundImportRow | null
|
||
/** Excel 批量导入模式 */
|
||
batchFileMode?: boolean
|
||
/** 当前入库单明细,用于 Excel 校验与模板导出 */
|
||
entryItems?: DirectInboundImportRow[]
|
||
}>()
|
||
|
||
const emit = defineEmits<{
|
||
'update:modelValue': [value: boolean]
|
||
success: []
|
||
}>()
|
||
|
||
const visible = computed({
|
||
get: () => props.modelValue,
|
||
set: (value) => emit('update:modelValue', value),
|
||
})
|
||
|
||
const submitting = ref(false)
|
||
const parsing = ref(false)
|
||
const singleFormRef = ref<FormInstance>()
|
||
const singleTarget = ref<DirectInboundImportRow | null>(null)
|
||
const importFileName = ref('')
|
||
const parsedRows = ref<ParsedDirectInboundImportRow[]>([])
|
||
|
||
const singleForm = reactive({
|
||
quantity: 1,
|
||
inbound_type: 'import' as 'import' | 'pda',
|
||
space_id: undefined as number | undefined,
|
||
space_keyword: '',
|
||
remark: '',
|
||
receiving_detail_id: undefined as number | undefined,
|
||
})
|
||
|
||
const containerLoading = ref(false)
|
||
const containerOptions = ref<EntryContainerListItem[]>([])
|
||
|
||
const showContainerPicker = computed(() => {
|
||
const status = String(props.orderStatus ?? '').trim().toUpperCase()
|
||
return singleForm.inbound_type === 'pda' && status === 'PENDING'
|
||
})
|
||
|
||
const spacePickerVisible = ref(false)
|
||
const spaceLoading = ref(false)
|
||
const spaceKeyword = ref('')
|
||
const spaceResults = ref<Record<string, unknown>[]>([])
|
||
const spaceSelection = ref<Record<string, unknown>[]>([])
|
||
const spacePagination = reactive({ page: 1, pageSize: 10, total: 0 })
|
||
const spacePickerTarget = ref<'single' | 'batch' | ''>('')
|
||
const batchSpaceEditingRow = ref<ParsedDirectInboundImportRow | null>(null)
|
||
|
||
const singleSpaceDisplay = computed(() => {
|
||
const keyword = singleForm.space_keyword.trim()
|
||
if (keyword) return keyword
|
||
return singleForm.space_id ? `ID ${singleForm.space_id}` : ''
|
||
})
|
||
|
||
const isBatchMode = computed(() => {
|
||
return !!props.batchFileMode && !props.targetItem
|
||
})
|
||
|
||
const singleMaxQuantity = computed(() => {
|
||
const remaining = Number(singleTarget.value?.remaining_quantity ?? 0)
|
||
return Number.isFinite(remaining) && remaining > 0 ? remaining : 1
|
||
})
|
||
|
||
const singleFormRules: FormRules = {
|
||
quantity: [
|
||
{ required: true, message: '请输入入库数量', trigger: 'blur' },
|
||
{
|
||
validator: (_rule, value, callback) => {
|
||
const qty = Number(value)
|
||
if (!Number.isInteger(qty) || qty <= 0) {
|
||
callback(new Error('入库数量须为正整数'))
|
||
return
|
||
}
|
||
if (qty > singleMaxQuantity.value) {
|
||
callback(new Error(`不能超过剩余可入库数量 ${singleMaxQuantity.value}`))
|
||
return
|
||
}
|
||
callback()
|
||
},
|
||
trigger: 'change',
|
||
},
|
||
],
|
||
inbound_type: [{ required: true, message: '请选择入库方式', trigger: 'change' }],
|
||
receiving_detail_id: [
|
||
{
|
||
validator: (_rule, value, callback) => {
|
||
if (!showContainerPicker.value) {
|
||
callback()
|
||
return
|
||
}
|
||
const detailId = Number(value)
|
||
if (!Number.isInteger(detailId) || detailId <= 0) {
|
||
callback(new Error('PDA 上架须选择收货容器'))
|
||
return
|
||
}
|
||
callback()
|
||
},
|
||
trigger: 'change',
|
||
},
|
||
],
|
||
}
|
||
|
||
watch(
|
||
() => [props.modelValue, props.targetItem, props.batchFileMode],
|
||
() => {
|
||
if (!props.modelValue) return
|
||
|
||
if (props.targetItem) {
|
||
singleTarget.value = props.targetItem
|
||
const remaining = Number(props.targetItem.remaining_quantity ?? 0)
|
||
singleForm.quantity = Number.isFinite(remaining) && remaining > 0 ? Math.min(remaining, 1) : 1
|
||
singleForm.inbound_type = 'import'
|
||
singleForm.space_id = undefined
|
||
singleForm.space_keyword = ''
|
||
singleForm.remark = ''
|
||
singleForm.receiving_detail_id = undefined
|
||
containerOptions.value = []
|
||
resetBatchState()
|
||
return
|
||
}
|
||
|
||
singleTarget.value = null
|
||
if (props.batchFileMode) {
|
||
resetBatchState()
|
||
}
|
||
},
|
||
{ immediate: true }
|
||
)
|
||
|
||
watch(
|
||
() => singleForm.inbound_type,
|
||
(type) => {
|
||
if (type !== 'pda') {
|
||
singleForm.receiving_detail_id = undefined
|
||
containerOptions.value = []
|
||
}
|
||
}
|
||
)
|
||
|
||
function formatContainerLabel(item: EntryContainerListItem): string {
|
||
const barcode = item.barcode ? String(item.barcode) : ''
|
||
const name = item.product_name ? String(item.product_name) : ''
|
||
const qty = item.actual_quantity ?? '—'
|
||
const parts = [barcode, name].filter(Boolean)
|
||
const base = parts.length ? parts.join(' / ') : `明细 ${item.detail_id ?? ''}`
|
||
return `${base}(实收 ${qty})`
|
||
}
|
||
|
||
async function loadContainers() {
|
||
if (!showContainerPicker.value) return
|
||
|
||
const entryOrderId = Number(props.entryOrderId)
|
||
const stockId = Number(singleTarget.value?.stock_id)
|
||
if (!Number.isInteger(entryOrderId) || entryOrderId <= 0) return
|
||
if (!Number.isInteger(stockId) || stockId <= 0) {
|
||
ElMessage.warning('当前商品缺少有效 SKU,无法查询收货容器')
|
||
return
|
||
}
|
||
|
||
containerLoading.value = true
|
||
try {
|
||
const res = await containerList({ entry_order_id: entryOrderId, stock_id: stockId })
|
||
if (res?.code !== 200 && res?.code !== undefined) {
|
||
throw new Error(res?.message || '查询收货容器失败')
|
||
}
|
||
const payload = (res.data && typeof res.data === 'object' ? res.data : res) as Record<string, unknown>
|
||
containerOptions.value = Array.isArray(payload.list) ? (payload.list as EntryContainerListItem[]) : []
|
||
} catch (error) {
|
||
console.error('查询收货容器失败:', error)
|
||
containerOptions.value = []
|
||
ElMessage.error(error instanceof Error ? error.message : '查询收货容器失败')
|
||
} finally {
|
||
containerLoading.value = false
|
||
}
|
||
}
|
||
|
||
/** 库位关键词解析缓存:相同关键词只查询一次 */
|
||
const spaceResolveCache = new Map<string, Promise<{ id?: number; displayName?: string }>>()
|
||
|
||
function resetBatchState() {
|
||
importFileName.value = ''
|
||
parsedRows.value = []
|
||
spaceResolveCache.clear()
|
||
}
|
||
|
||
function normalizeSpaceId(value: unknown): number | undefined {
|
||
const spaceId = Number(value)
|
||
return Number.isInteger(spaceId) && spaceId > 0 ? spaceId : undefined
|
||
}
|
||
|
||
function getBatchRowMaxQuantity(row: ParsedDirectInboundImportRow): number {
|
||
const remaining = Number(row.remaining_quantity ?? 0)
|
||
return Number.isFinite(remaining) && remaining > 0 ? remaining : Number.MAX_SAFE_INTEGER
|
||
}
|
||
|
||
function removeParsedRow(index: number) {
|
||
parsedRows.value.splice(index, 1)
|
||
}
|
||
|
||
function openSpacePicker(target: 'single' | 'batch') {
|
||
spacePickerTarget.value = target
|
||
spaceSelection.value = []
|
||
spaceKeyword.value = ''
|
||
spacePickerVisible.value = true
|
||
void querySpaces(1)
|
||
}
|
||
|
||
function openBatchRowSpacePicker(row: ParsedDirectInboundImportRow) {
|
||
batchSpaceEditingRow.value = row
|
||
openSpacePicker('batch')
|
||
}
|
||
|
||
function handleSpaceSelectionChange(val: Record<string, unknown>[]) {
|
||
spaceSelection.value = val
|
||
}
|
||
|
||
function handleSpaceRowClick(row: Record<string, unknown>) {
|
||
spaceSelection.value = [row]
|
||
}
|
||
|
||
function onSpacePageSizeChange(size: number) {
|
||
spacePagination.pageSize = size
|
||
void querySpaces(1)
|
||
}
|
||
|
||
async function querySpaces(page = 1) {
|
||
spaceLoading.value = true
|
||
try {
|
||
const keyword = spaceKeyword.value.trim()
|
||
const res = (await indexApi.searchSpace({
|
||
keyword,
|
||
page,
|
||
pageSize: spacePagination.pageSize,
|
||
})) as unknown as Record<string, unknown>
|
||
const data = (res?.data || res?.list || res) as Record<string, unknown>
|
||
const list = Array.isArray(data?.list) ? data.list : Array.isArray(data) ? data : []
|
||
const pagination = data?.pagination as Record<string, unknown> | undefined
|
||
|
||
spaceResults.value = (list as Record<string, unknown>[]).map((item) => ({
|
||
...item,
|
||
display_name: getSpaceDisplayName(item),
|
||
space_name: item.space_name || item.name || item.space_code || '',
|
||
}))
|
||
spacePagination.total = Number(pagination?.total ?? data?.total ?? spaceResults.value.length)
|
||
spacePagination.page = page
|
||
} catch (error) {
|
||
console.error('查询库位失败:', error)
|
||
ElMessage.error('查询库位失败')
|
||
spaceResults.value = []
|
||
spacePagination.total = 0
|
||
} finally {
|
||
spaceLoading.value = false
|
||
}
|
||
}
|
||
|
||
function applySelectedSpace() {
|
||
if (spaceSelection.value.length !== 1) {
|
||
ElMessage.warning('请选择一个库位')
|
||
return
|
||
}
|
||
|
||
const picked = spaceSelection.value[0]
|
||
const display =
|
||
getSpaceDisplayName(picked) || String(picked?.space_code ?? '').trim() || `ID ${picked?.id ?? ''}`
|
||
const spaceId = Number(picked?.id)
|
||
|
||
if (spacePickerTarget.value === 'batch' && batchSpaceEditingRow.value) {
|
||
batchSpaceEditingRow.value.space_keyword = display
|
||
batchSpaceEditingRow.value.space_id =
|
||
Number.isFinite(spaceId) && spaceId > 0 ? spaceId : undefined
|
||
batchSpaceEditingRow.value = null
|
||
} else if (spacePickerTarget.value === 'single') {
|
||
singleForm.space_keyword = display
|
||
singleForm.space_id = Number.isFinite(spaceId) && spaceId > 0 ? spaceId : undefined
|
||
}
|
||
|
||
spacePickerTarget.value = ''
|
||
spacePickerVisible.value = false
|
||
}
|
||
|
||
async function resolveRowSpaceBeforeSubmit(
|
||
row: { rowIndex?: number; space_id?: number; space_keyword?: string }
|
||
): Promise<string | null> {
|
||
const keyword = row.space_keyword?.trim()
|
||
if (!row.space_id && keyword) {
|
||
let pending = spaceResolveCache.get(keyword)
|
||
if (!pending) {
|
||
pending = resolveSpaceByKeyword(keyword)
|
||
spaceResolveCache.set(keyword, pending)
|
||
}
|
||
const resolved = await pending
|
||
if (!resolved.id) {
|
||
const label = row.rowIndex ? `第 ${row.rowIndex} 行` : '当前行'
|
||
return `${label}库位未匹配: ${keyword}`
|
||
}
|
||
row.space_id = resolved.id
|
||
if (resolved.displayName) row.space_keyword = resolved.displayName
|
||
}
|
||
return null
|
||
}
|
||
|
||
function validateBatchRows(): string | null {
|
||
for (const row of parsedRows.value) {
|
||
const stockId = Number(row.stock_id)
|
||
if (!Number.isInteger(stockId) || stockId <= 0) {
|
||
return `第 ${row.rowIndex} 行缺少有效 SKU`
|
||
}
|
||
|
||
const quantity = Number(row.quantity)
|
||
if (!Number.isInteger(quantity) || quantity <= 0) {
|
||
return `第 ${row.rowIndex} 行入库数量须为正整数`
|
||
}
|
||
|
||
const maxQty = getBatchRowMaxQuantity(row)
|
||
if (quantity > maxQty) {
|
||
return `第 ${row.rowIndex} 行入库数量不能超过剩余可入 ${maxQty}`
|
||
}
|
||
|
||
if (!row.inbound_type) {
|
||
return `第 ${row.rowIndex} 行请选择入库方式`
|
||
}
|
||
}
|
||
return null
|
||
}
|
||
|
||
function handleDownloadTemplate() {
|
||
const items = props.entryItems || []
|
||
if (!items.length) {
|
||
ElMessage.warning('当前入库单没有可导出的明细')
|
||
return
|
||
}
|
||
downloadDirectInboundImportTemplate(items)
|
||
}
|
||
|
||
async function handleFileChange(uploadFile: UploadFile) {
|
||
const file = uploadFile.raw
|
||
if (!file) return
|
||
|
||
parsing.value = true
|
||
resetBatchState()
|
||
importFileName.value = file.name
|
||
|
||
try {
|
||
const rawRows = await readDirectInboundXlsxFile(file)
|
||
parsedRows.value = parseDirectInboundXlsxRows(rawRows, {
|
||
entryItems: props.entryItems || [],
|
||
defaultInboundType: 'import',
|
||
})
|
||
await resolveParsedRowSpaces(parsedRows.value)
|
||
|
||
if (parsedRows.value.length) {
|
||
ElMessage.success(`已解析 ${parsedRows.value.length} 条明细`)
|
||
} else {
|
||
ElMessage.warning('Excel 中未解析到数据行')
|
||
}
|
||
} catch (error) {
|
||
console.error('解析 Excel 失败:', error)
|
||
ElMessage.error('Excel 文件解析失败')
|
||
} finally {
|
||
parsing.value = false
|
||
}
|
||
}
|
||
|
||
function handleClosed() {
|
||
singleTarget.value = null
|
||
singleFormRef.value?.clearValidate()
|
||
resetBatchState()
|
||
spacePickerVisible.value = false
|
||
spacePickerTarget.value = ''
|
||
batchSpaceEditingRow.value = null
|
||
}
|
||
|
||
async function handleSubmit() {
|
||
const entryOrderId = Number(props.entryOrderId)
|
||
if (!Number.isFinite(entryOrderId) || entryOrderId <= 0) {
|
||
ElMessage.warning('缺少入库单 ID')
|
||
return
|
||
}
|
||
|
||
submitting.value = true
|
||
try {
|
||
if (!isBatchMode.value) {
|
||
if (!singleFormRef.value || !singleTarget.value?.stock_id) return
|
||
const valid = await singleFormRef.value.validate().catch(() => false)
|
||
if (!valid) return
|
||
|
||
const spaceError = await resolveRowSpaceBeforeSubmit(singleForm)
|
||
if (spaceError) {
|
||
ElMessage.warning(spaceError)
|
||
return
|
||
}
|
||
if (showContainerPicker.value && !normalizeSpaceId(singleForm.space_id)) {
|
||
ElMessage.warning('PDA 上架须选择存放库位')
|
||
return
|
||
}
|
||
|
||
const res = await addItem({
|
||
entry_order_id: entryOrderId,
|
||
stock_id: Number(singleTarget.value.stock_id),
|
||
quantity: showContainerPicker.value ? undefined : Number(singleForm.quantity),
|
||
inbound_type: singleForm.inbound_type,
|
||
space_id: normalizeSpaceId(singleForm.space_id),
|
||
remark: singleForm.remark?.trim() || undefined,
|
||
receiving_detail_id: showContainerPicker.value
|
||
? Number(singleForm.receiving_detail_id)
|
||
: undefined,
|
||
})
|
||
if (res?.code !== 200 && res?.code !== undefined) {
|
||
throw new Error(res?.message || '导入入库失败')
|
||
}
|
||
ElMessage.success('导入入库成功')
|
||
} else {
|
||
if (!parsedRows.value.length) {
|
||
ElMessage.warning('请先上传 Excel 文件')
|
||
return
|
||
}
|
||
|
||
const validationError = validateBatchRows()
|
||
if (validationError) {
|
||
ElMessage.warning(validationError)
|
||
return
|
||
}
|
||
|
||
const spaceErrors = await Promise.all(
|
||
parsedRows.value.map((row) => resolveRowSpaceBeforeSubmit(row))
|
||
)
|
||
const spaceError = spaceErrors.find((error) => error) || null
|
||
if (spaceError) {
|
||
ElMessage.warning(spaceError)
|
||
return
|
||
}
|
||
|
||
const res = await addItem({
|
||
entry_order_id: entryOrderId,
|
||
items: parsedRows.value.map((row) => ({
|
||
stock_id: Number(row.stock_id),
|
||
quantity: Number(row.quantity),
|
||
inbound_type: row.inbound_type,
|
||
space_id: normalizeSpaceId(row.space_id),
|
||
remark: row.remark?.trim() || undefined,
|
||
})),
|
||
})
|
||
if (res?.code !== 200 && res?.code !== undefined) {
|
||
throw new Error(res?.message || '批量导入入库失败')
|
||
}
|
||
|
||
const payload = res?.data && typeof res.data === 'object' ? res.data : res
|
||
const count = payload?.count ?? parsedRows.value.length
|
||
ElMessage.success(`批量导入成功,共 ${count} 条`)
|
||
}
|
||
|
||
visible.value = false
|
||
emit('success')
|
||
} catch (error) {
|
||
console.error('导入入库失败:', error)
|
||
ElMessage.error(error instanceof Error ? error.message : '导入入库失败')
|
||
} finally {
|
||
submitting.value = false
|
||
}
|
||
}
|
||
</script>
|
||
|
||
<style scoped lang="scss">
|
||
.item-summary {
|
||
display: grid;
|
||
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||
gap: 8px 16px;
|
||
margin-bottom: 16px;
|
||
padding: 12px;
|
||
background: #f5f7fa;
|
||
border-radius: 4px;
|
||
|
||
.summary-item {
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 2px;
|
||
}
|
||
|
||
.summary-label {
|
||
font-size: 12px;
|
||
color: #909399;
|
||
}
|
||
|
||
.summary-value {
|
||
font-size: 13px;
|
||
color: #303133;
|
||
}
|
||
}
|
||
|
||
.batch-import-panel {
|
||
.batch-tip {
|
||
margin-bottom: 12px;
|
||
}
|
||
|
||
.batch-toolbar {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 8px;
|
||
margin-bottom: 12px;
|
||
}
|
||
|
||
.file-name {
|
||
font-size: 12px;
|
||
color: #606266;
|
||
}
|
||
}
|
||
|
||
.batch-cell-action {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 6px;
|
||
|
||
.space-keyword-text {
|
||
flex: 1;
|
||
min-width: 0;
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
white-space: nowrap;
|
||
}
|
||
}
|
||
|
||
.system-product-picker {
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 10px;
|
||
}
|
||
|
||
.picker-toolbar {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 12px;
|
||
flex-wrap: wrap;
|
||
}
|
||
|
||
.picker-table-wrap {
|
||
height: 440px;
|
||
}
|
||
</style>
|