WEB/src/views/storage/entry/component/entrydetail.vue
liufumingren cde142f113 1
2026-06-29 03:40:57 +08:00

418 lines
12 KiB
Vue

<template>
<div class="entry-details">
<div class="header-actions">
<div class="left-actions">
<el-button size="small" @click="reloadAll">刷新</el-button>
<el-button
size="small"
type="primary"
:disabled="!canDirectInbound"
:loading="commitLoading"
@click="handleCommit"
>
提交完成入库
</el-button>
<el-button
size="small"
:disabled="!canBatchImport"
@click="openBatchImportDialog"
>
批量导入
</el-button>
</div>
<div class="right-actions">
<el-tag v-if="canBatchImport" size="small" type="success">可导入/PDA 入库</el-tag>
<span v-if="directSummary.total_direct_inbound !== undefined" class="summary-text">
已导入 {{ directSummary.total_direct_inbound ?? 0 }} / 计划 {{ directSummary.total_planned ?? '—' }}
</span>
</div>
</div>
<div class="table-content">
<UpperTable
:data="tableData"
:columns="detailColumns"
:show-selection="true"
:pagination="pagination"
:loading="loading"
@selection-change="handleSelectionChange"
@page-size-change="handlePageSizeChange"
@page-change="handlePageChange"
>
<template #column-operation="{ row }">
<el-button
link
type="primary"
size="small"
:disabled="!canImportRow(row)"
@click="openImportDialog(row)"
>
导入
</el-button>
</template>
<template #column-direct_inbound_quantity="{ row }">
<el-tag size="small" :type="Number(row.direct_inbound_quantity) > 0 ? 'success' : 'info'">
{{ row.direct_inbound_quantity ?? 0 }}
</el-tag>
</template>
<template #column-remaining_quantity="{ row }">
<el-tag size="small" :type="Number(row.remaining_quantity) > 0 ? 'warning' : 'info'">
{{ row.remaining_quantity ?? 0 }}
</el-tag>
</template>
<template #column-pending_total="{ row }">
<span>{{ row.pending_total ?? 0 }}</span>
</template>
</UpperTable>
</div>
<DirectInboundImportDialog
v-model="importDialogVisible"
:entry-order-code="entryOrderCode"
:target-item="importTargetItem"
:batch-file-mode="batchFileMode"
:entry-items="tableData as DirectInboundImportRow[]"
@success="handleImportSuccess"
/>
</div>
</template>
<script setup lang="ts">
import { computed, reactive, ref, watch, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import UpperTable from '@/components/UpperTable/index.vue'
import * as entryApi from '@/api/storage.entry/entry'
import {
commitDirectInbound,
getDirectInboundItems,
type DirectInboundItem,
} from '@/api/storage.directinbound/directinbound'
import DirectInboundImportDialog, {
type DirectInboundImportRow,
} from './DirectInboundImportDialog.vue'
import {
canDirectInboundOperate,
entrydetail,
} from '../entry-config'
const props = defineProps<{
id?: string | number
row?: Record<string, unknown> | null
/** 由父级递增,用于跨 Tab 触发明细刷新 */
entryDetailRefreshTick?: number
}>()
const emit = defineEmits(['refresh'])
const tableData = ref<Record<string, unknown>[]>([])
const loading = ref(false)
const commitLoading = ref(false)
const multipleSelection = ref<Record<string, unknown>[]>([])
const entryOrderStatus = ref('')
const entryOrderCode = ref('')
const directSummary = ref<Record<string, unknown>>({})
const pagination = reactive({
currentPage: 1,
pageSize: 20,
total: 0,
})
const canBatchImport = computed(() => {
const status = props.row?.order_status ?? entryOrderStatus.value
if (status === null || status === undefined || status === '') return false
return String(status).trim().toUpperCase() === 'CREATED'
})
const canDirectInbound = computed(() => {
const status = props.row?.order_status ?? entryOrderStatus.value
return canDirectInboundOperate(status)
})
const detailColumns = computed(() => {
const columns = [...entrydetail]
const countIndex = columns.findIndex((col) => col.prop === 'count')
const insertAt = countIndex >= 0 ? countIndex : columns.length
columns.splice(insertAt, 0,
{ prop: 'direct_inbound_quantity', label: '已导入', width: '90', align: 'right' },
{ prop: 'remaining_quantity', label: '剩余可入', width: '90', align: 'right' },
)
if (canBatchImport.value) {
columns.push({
prop: 'operation',
label: '操作',
width: '90',
fixed: 'right',
align: 'center',
} as unknown as (typeof entrydetail)[number])
}
return columns
})
const importDialogVisible = ref(false)
const importTargetItem = ref<DirectInboundImportRow | null>(null)
const batchFileMode = ref(false)
watch(importDialogVisible, (visible) => {
if (!visible) {
batchFileMode.value = false
importTargetItem.value = null
}
})
watch(
() => props.entryDetailRefreshTick,
() => {
if (props.id && props.entryDetailRefreshTick) {
void reloadAll()
}
}
)
watch(
() => props.id,
(newVal) => {
if (newVal) {
void reloadAll()
}
},
{ immediate: true }
)
function unwrapPayload(res: Record<string, unknown> | null | undefined) {
if (!res) return null
if (res.data && typeof res.data === 'object') {
return res.data as Record<string, unknown>
}
return res
}
function mergeDirectInboundFields(
items: Record<string, unknown>[],
directItems: DirectInboundItem[]
) {
const directMap = new Map<number, DirectInboundItem>()
directItems.forEach((item) => {
if (item.stock_id != null) {
directMap.set(Number(item.stock_id), item)
}
})
return items.map((item) => {
const stockId = item.stock_id != null ? Number(item.stock_id) : NaN
const direct = Number.isFinite(stockId) ? directMap.get(stockId) : undefined
return {
...item,
direct_inbound_quantity: direct?.direct_inbound_quantity ?? 0,
remaining_quantity: direct?.remaining_quantity ?? item.count ?? 0,
pending_total: direct?.pending_binding_total ?? item.pending_total ?? 0,
bindings: direct?.bindings ?? item.bindings,
}
})
}
async function loadEntryDetail() {
if (!props.id) return
loading.value = true
try {
const res = await entryApi.getEntryDetail({ id: Number(props.id) })
if (res && res.code === 200 && res.data?.entryOrder) {
const entryOrder = res.data.entryOrder
entryOrderStatus.value = entryOrder.order_status || ''
entryOrderCode.value = entryOrder.entry_order_code || ''
const items = entryOrder.items || []
tableData.value = items.map((item: Record<string, unknown>) => ({
...item,
entry_order_code: entryOrder.entry_order_code || '',
purchase_order_code: entryOrder.purchase_order_code || '',
store_name: entryOrder.store_name || '',
}))
pagination.total = tableData.value.length
} else {
throw new Error(res?.message || '接口返回数据异常')
}
} catch (error) {
console.error('获取明细失败:', error)
ElMessage.error('获取明细失败')
tableData.value = []
pagination.total = 0
entryOrderStatus.value = ''
entryOrderCode.value = ''
} finally {
loading.value = false
}
}
async function loadDirectInboundMeta() {
const code = entryOrderCode.value || (props.row?.entry_order_code ? String(props.row.entry_order_code) : '')
if (!code) {
directSummary.value = {}
return
}
try {
const res = await getDirectInboundItems({ entry_order_code: code })
if (res?.code !== 200 && res?.code !== undefined) {
throw new Error(res?.message || '获取导入入库信息失败')
}
const payload = unwrapPayload(res as Record<string, unknown>)
const directItems = Array.isArray(payload?.items) ? (payload.items as DirectInboundItem[]) : []
directSummary.value = (payload?.summary as Record<string, unknown>) || {}
if (tableData.value.length) {
tableData.value = mergeDirectInboundFields(tableData.value, directItems)
}
} catch (error) {
console.error('获取导入入库信息失败:', error)
}
}
async function reloadAll() {
await loadEntryDetail()
await loadDirectInboundMeta()
}
function canImportRow(row: DirectInboundImportRow) {
const status = props.row?.order_status ?? entryOrderStatus.value
if (status === null || status === undefined || status === '') return false
if (String(status).trim().toUpperCase() !== 'CREATED') return false
if (!canDirectInbound.value || !row.stock_id) return false
const available = Number(row.remaining_quantity ?? row.count ?? 0)
return Number.isFinite(available) && available > 0
}
function openImportDialog(row: DirectInboundImportRow) {
if (!canImportRow(row)) {
ElMessage.warning('该商品无可导入数量')
return
}
batchFileMode.value = false
importTargetItem.value = row
importDialogVisible.value = true
}
function openBatchImportDialog() {
if (!canBatchImport.value) {
ElMessage.warning('仅「已创建」状态可使用批量导入')
return
}
importTargetItem.value = null
batchFileMode.value = true
importDialogVisible.value = true
}
async function handleImportSuccess() {
await reloadAll()
emit('refresh')
}
async function handleCommit() {
const code = entryOrderCode.value
if (!code) {
ElMessage.warning('缺少入库单号')
return
}
if (!canDirectInbound.value) {
ElMessage.warning('当前入库单状态不允许提交')
return
}
try {
await ElMessageBox.confirm(
`确认提交并完成入库单「${code}」的导入/PDA 入库?提交后将写入库存并更新入库单状态为已完成。`,
'提交完成入库',
{ type: 'warning', confirmButtonText: '确认提交', cancelButtonText: '取消' }
)
} catch {
return
}
commitLoading.value = true
try {
const res = await commitDirectInbound({ entry_order_code: code })
if (res?.code !== 200 && res?.code !== undefined) {
throw new Error(res?.message || '提交完成入库失败')
}
const payload = unwrapPayload(res as Record<string, unknown>)
ElMessage.success(
`入库完成:共 ${payload?.items_count ?? '—'} 种商品,总量 ${payload?.total_quantity ?? '—'}`
)
await reloadAll()
emit('refresh')
} catch (error) {
console.error('提交完成入库失败:', error)
ElMessage.error(error instanceof Error ? error.message : '提交完成入库失败')
} finally {
commitLoading.value = false
}
}
const handleSelectionChange = (val: Record<string, unknown>[]) => {
multipleSelection.value = val
}
const handlePageSizeChange = () => {
void reloadAll()
}
const handlePageChange = () => {
void reloadAll()
}
defineExpose({
openBatchImportDialog,
reloadAll,
})
onMounted(() => {
if (props.id) {
void reloadAll()
}
})
</script>
<style scoped lang="scss">
.entry-details {
display: flex;
flex-direction: column;
height: 100%;
background: #fff;
.header-actions {
display: flex;
justify-content: space-between;
align-items: center;
padding-bottom: 10px;
border-bottom: 1px solid #ebeef5;
margin-bottom: 10px;
.left-actions {
display: flex;
gap: 8px;
}
.right-actions {
display: flex;
align-items: center;
gap: 8px;
}
.summary-text {
font-size: 12px;
color: #606266;
}
}
.table-content {
flex: 1;
min-height: 200px;
overflow: hidden;
}
}
</style>