520 lines
14 KiB
Vue
520 lines
14 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="canBindReceiving" size="small" type="warning">待入库,可绑定收货明细</el-tag>
|
||
<el-tag v-if="canDirectInbound" 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 v-if="canBindReceiving" #column-operation="{ row }">
|
||
<el-button
|
||
link
|
||
type="primary"
|
||
size="small"
|
||
:disabled="!row.stock_id"
|
||
@click="openBindDialog(row)"
|
||
>
|
||
绑定
|
||
</el-button>
|
||
<!-- <el-button
|
||
v-if="canDirectInbound"
|
||
link
|
||
type="primary"
|
||
size="small"
|
||
:disabled="!canImportRow(row)"
|
||
@click="openImportDialog(row)"
|
||
>
|
||
导入
|
||
</el-button> -->
|
||
</template>
|
||
<template v-else-if="canDirectInbound" #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 }">
|
||
<el-button
|
||
v-if="canBindReceiving && hasPendingBindings(row)"
|
||
link
|
||
type="primary"
|
||
size="small"
|
||
@click="openUnbindDialog(row)"
|
||
>
|
||
{{ row.pending_total ?? 0 }}
|
||
</el-button>
|
||
<span v-else>{{ row.pending_total ?? 0 }}</span>
|
||
</template>
|
||
</UpperTable>
|
||
</div>
|
||
|
||
<BindReceivingDetailsDialog
|
||
v-model="bindDialogVisible"
|
||
:entry-item="bindTargetItem"
|
||
@success="handleBindSuccess"
|
||
/>
|
||
|
||
<UnbindReceivingDetailsDialog
|
||
v-model="unbindDialogVisible"
|
||
:entry-item="unbindTargetItem"
|
||
:entry-order-id="props.id"
|
||
@success="handleUnbindSuccess"
|
||
/>
|
||
|
||
<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 BindReceivingDetailsDialog, {
|
||
type EntryDetailBindRow,
|
||
} from './BindReceivingDetailsDialog.vue'
|
||
import UnbindReceivingDetailsDialog from './UnbindReceivingDetailsDialog.vue'
|
||
import DirectInboundImportDialog, {
|
||
type DirectInboundImportRow,
|
||
} from './DirectInboundImportDialog.vue'
|
||
import {
|
||
canDirectInboundOperate,
|
||
entrydetail,
|
||
isPendingEntryOrderStatus,
|
||
} 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 canBindReceiving = computed(() => {
|
||
const status = props.row?.order_status ?? entryOrderStatus.value
|
||
return isPendingEntryOrderStatus(status)
|
||
})
|
||
|
||
const canBatchImport = computed(() => canBindReceiving.value)
|
||
|
||
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 (canBindReceiving.value || canDirectInbound.value) {
|
||
columns.push({
|
||
prop: 'operation',
|
||
label: '操作',
|
||
width: canBindReceiving.value && canDirectInbound.value ? '120' : '90',
|
||
fixed: 'right',
|
||
align: 'center',
|
||
} as unknown as (typeof entrydetail)[number])
|
||
}
|
||
return columns
|
||
})
|
||
|
||
const bindDialogVisible = ref(false)
|
||
const bindTargetItem = ref<EntryDetailBindRow | null>(null)
|
||
const unbindDialogVisible = ref(false)
|
||
const unbindTargetItem = ref<EntryDetailBindRow | null>(null)
|
||
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) {
|
||
if (!canDirectInbound.value || !row.stock_id) return false
|
||
const remaining = Number(row.remaining_quantity ?? 0)
|
||
return Number.isFinite(remaining) && remaining > 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
|
||
}
|
||
}
|
||
|
||
function openBindDialog(row: EntryDetailBindRow) {
|
||
if (!canBindReceiving.value) {
|
||
ElMessage.warning('仅待入库状态的入库单支持绑定收货明细')
|
||
return
|
||
}
|
||
if (!row?.id) {
|
||
ElMessage.warning('入库明细 ID 无效')
|
||
return
|
||
}
|
||
if (!row?.stock_id) {
|
||
ElMessage.warning('当前明细缺少 stock_id,无法查询收货单')
|
||
return
|
||
}
|
||
bindTargetItem.value = row
|
||
bindDialogVisible.value = true
|
||
}
|
||
|
||
function openUnbindDialog(row: EntryDetailBindRow) {
|
||
if (!canBindReceiving.value) {
|
||
ElMessage.warning('仅待入库状态的入库单支持解绑收货明细')
|
||
return
|
||
}
|
||
if (!row?.id) {
|
||
ElMessage.warning('入库明细 ID 无效')
|
||
return
|
||
}
|
||
if (!hasPendingBindings(row)) {
|
||
ElMessage.warning('当前明细没有可解绑的收货绑定')
|
||
return
|
||
}
|
||
unbindTargetItem.value = row
|
||
unbindDialogVisible.value = true
|
||
}
|
||
|
||
async function handleBindSuccess() {
|
||
await reloadAll()
|
||
emit('refresh')
|
||
}
|
||
|
||
async function handleUnbindSuccess() {
|
||
await reloadAll()
|
||
emit('refresh')
|
||
}
|
||
|
||
function hasPendingBindings(row: EntryDetailBindRow): boolean {
|
||
const pendingTotal = Number(row.pending_total)
|
||
if (Number.isFinite(pendingTotal) && pendingTotal > 0) return true
|
||
const bindings = row.bindings
|
||
return Array.isArray(bindings) && bindings.length > 0
|
||
}
|
||
|
||
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>
|