1078 lines
36 KiB
Vue
1078 lines
36 KiB
Vue
<template>
|
||
<div class="panel-stock-detail">
|
||
<div class="main-content">
|
||
<div class="header-actions">
|
||
<div class="left-actions">
|
||
<el-button
|
||
v-if="!groupByBatch"
|
||
size="small"
|
||
type="primary"
|
||
:loading="loading"
|
||
@click="loadData"
|
||
>
|
||
刷新
|
||
</el-button>
|
||
|
||
<el-tooltip
|
||
v-if="!isSpaceView"
|
||
content="请在下方明细列表勾选一条库存记录(含库存记录 ID)"
|
||
:disabled="canTransferByLocation"
|
||
>
|
||
<span class="transfer-btn-wrap">
|
||
<el-button
|
||
size="small"
|
||
type="primary"
|
||
plain
|
||
:disabled="!canTransferByLocation"
|
||
@click="openTransferDialog('location')"
|
||
>
|
||
按库存记录移库
|
||
</el-button>
|
||
</span>
|
||
</el-tooltip>
|
||
<el-tooltip
|
||
v-if="groupByBatch || isSpaceView"
|
||
content="请在下方列表勾选一条库位记录;仅未开启批次管理的商品支持"
|
||
:disabled="canTransferByProduct"
|
||
>
|
||
<span class="transfer-btn-wrap">
|
||
<el-button
|
||
size="small"
|
||
type="primary"
|
||
plain
|
||
:disabled="!canTransferByProduct"
|
||
@click="openTransferDialog('product')"
|
||
>
|
||
按商品移库
|
||
</el-button>
|
||
</span>
|
||
</el-tooltip>
|
||
<el-tooltip
|
||
content="请先在下方明细列表勾选一条库存记录(含库存记录 ID)"
|
||
:disabled="canEditExpiry"
|
||
>
|
||
<span class="transfer-btn-wrap">
|
||
<el-button
|
||
size="small"
|
||
type="primary"
|
||
plain
|
||
:disabled="!canEditExpiry"
|
||
@click="openExpiryDialog"
|
||
>
|
||
维护生产/到期日期
|
||
</el-button>
|
||
</span>
|
||
</el-tooltip>
|
||
</div>
|
||
|
||
<div class="right-actions">
|
||
<!-- 「总数量」数值默认遮挡,点此切换显示/隐藏(仅影响展示) -->
|
||
<el-button
|
||
size="small"
|
||
type="primary"
|
||
plain
|
||
@click="quantityVisible = !quantityVisible"
|
||
>
|
||
{{ quantityVisible ? '隐藏数量' : '显示数量' }}
|
||
</el-button>
|
||
<el-radio-group
|
||
v-if="!groupByBatch"
|
||
v-model="detailViewMode"
|
||
size="small"
|
||
>
|
||
<el-radio-button value="detail">按明细</el-radio-button>
|
||
<el-radio-button value="space">按库位汇总</el-radio-button>
|
||
</el-radio-group>
|
||
</div>
|
||
|
||
</div>
|
||
<el-dialog
|
||
v-model="dialogVisible"
|
||
:title="dialogTitle"
|
||
width="480px"
|
||
align-center
|
||
@closed="resetTransferDialog"
|
||
>
|
||
<p v-if="transferMode === 'location'" class="transfer-hint">
|
||
单条 wms_storage 记录移库;移库数量不填则全量移库。
|
||
</p>
|
||
<p v-else-if="transferMode === 'product'" class="transfer-hint">
|
||
按原库位 + 商品合并移库,自动合并该库位下该商品的所有库存;不适用于开启批次管理且多批次的商品。
|
||
</p>
|
||
<div class="transfer-form">
|
||
<el-input v-model="targetSpace" placeholder="目标库位编码或 ID,扫码或输入" clearable />
|
||
<div class="target-space-tip">
|
||
<span v-if="spaceName">目标库位名称: {{ spaceName }}</span>
|
||
<span v-else-if="targetSpace && targetSpaceError" class="target-space-error">未找到库位</span>
|
||
</div>
|
||
<el-input-number
|
||
v-model="transferQty"
|
||
:min="1"
|
||
:placeholder="transferMode === 'product' ? '移库数量(必填)' : '移库数量(不填则全量移库)'"
|
||
style="width: 100%;"
|
||
/>
|
||
</div>
|
||
<template #footer>
|
||
<el-button @click="dialogVisible = false">取消</el-button>
|
||
<el-button type="primary" :loading="transferLoading" @click="confirmTransfer">
|
||
{{ confirmButtonText }}
|
||
</el-button>
|
||
</template>
|
||
</el-dialog>
|
||
<el-dialog
|
||
v-model="expiryDialogVisible"
|
||
title="维护生产日期 / 到期日期"
|
||
width="560px"
|
||
align-center
|
||
@opened="handleExpiryDialogOpened"
|
||
@closed="resetExpiryForm"
|
||
>
|
||
<el-descriptions class="expiry-context" :column="2" size="small" border>
|
||
<el-descriptions-item label="库存记录ID">{{ expiryRow?.storage_id ?? '--' }}</el-descriptions-item>
|
||
<el-descriptions-item label="批次号">{{ expiryRow?.batch_no || '--' }}</el-descriptions-item>
|
||
<el-descriptions-item label="商品">{{ expiryItemLabel }}</el-descriptions-item>
|
||
<el-descriptions-item label="库位">{{ expiryRow?.space_code || '--' }}</el-descriptions-item>
|
||
</el-descriptions>
|
||
|
||
<p class="expiry-hint">
|
||
输入框已回显库内当前日期:不改动=保持原值(不下发);改选日期=下发新日期;点「清空」清空输入框=下发 null 置空。两个日期至少改一项,且生产日期不得晚于到期日期。
|
||
</p>
|
||
|
||
<el-form label-width="80px" class="expiry-form">
|
||
<el-form-item label="生产日期">
|
||
<div class="expiry-field">
|
||
<el-date-picker
|
||
ref="productionPickerRef"
|
||
v-model="expiryForm.productionDate"
|
||
type="date"
|
||
value-format="YYYY-MM-DD"
|
||
format="YYYY-MM-DD"
|
||
placeholder="请选择生产日期"
|
||
class="expiry-date-picker"
|
||
clearable
|
||
/>
|
||
<el-button
|
||
type="primary"
|
||
link
|
||
class="expiry-clear-btn"
|
||
:disabled="!expiryForm.productionDate"
|
||
@click="expiryForm.productionDate = ''"
|
||
>
|
||
清空
|
||
</el-button>
|
||
</div>
|
||
</el-form-item>
|
||
|
||
<el-form-item label="到期日期">
|
||
<div class="expiry-field">
|
||
<el-date-picker
|
||
ref="expiryPickerRef"
|
||
v-model="expiryForm.expiryDate"
|
||
type="date"
|
||
value-format="YYYY-MM-DD"
|
||
format="YYYY-MM-DD"
|
||
placeholder="请选择到期日期"
|
||
class="expiry-date-picker"
|
||
clearable
|
||
/>
|
||
<el-button
|
||
type="primary"
|
||
link
|
||
class="expiry-clear-btn"
|
||
:disabled="!expiryForm.expiryDate"
|
||
@click="expiryForm.expiryDate = ''"
|
||
>
|
||
清空
|
||
</el-button>
|
||
</div>
|
||
</el-form-item>
|
||
</el-form>
|
||
|
||
<!-- <div class="expiry-preview">
|
||
<span class="expiry-preview-label">提交内容</span>
|
||
<span class="expiry-preview-text">{{ expiryPayloadPreview }}</span>
|
||
</div> -->
|
||
|
||
<template #footer>
|
||
<el-button @click="expiryDialogVisible = false">取消</el-button>
|
||
<el-button
|
||
type="primary"
|
||
:loading="expiryLoading"
|
||
:disabled="!hasExpiryChange"
|
||
@click="confirmExpiry"
|
||
>
|
||
确认维护
|
||
</el-button>
|
||
</template>
|
||
</el-dialog>
|
||
<div ref="tableWrapRef" class="detail-table-wrap">
|
||
<el-table
|
||
ref="detailTableRef"
|
||
:data="pagedDetailList"
|
||
border
|
||
stripe
|
||
size="small"
|
||
v-loading="loading"
|
||
:height="tableHeight"
|
||
:row-key="(row: any) => row.storage_id ?? `${row.space_id}-${row.stock_id}`"
|
||
@selection-change="handleDetailSelectionChange"
|
||
>
|
||
<el-table-column type="selection" width="40" />
|
||
<el-table-column
|
||
v-for="col in detailColumns"
|
||
:key="col.prop"
|
||
:prop="col.prop"
|
||
:label="col.label"
|
||
:width="col.width"
|
||
show-overflow-tooltip
|
||
>
|
||
<!-- 生产日期 / 到期日期:单元格可直接点击打开维护弹窗并展开对应日历 -->
|
||
<template v-if="isExpiryDateColumn(col.prop)" #default="{ row }">
|
||
<el-link
|
||
v-if="getRowStorageId(row)"
|
||
type="primary"
|
||
:underline="false"
|
||
:title="`点击快捷维护${col.label}`"
|
||
@click.stop="openExpiryDialog(row, col.prop === 'production_date' ? 'production' : 'expiry')"
|
||
>
|
||
{{ row[col.prop] || '--' }}
|
||
</el-link>
|
||
<span v-else>{{ row[col.prop] || '--' }}</span>
|
||
</template>
|
||
|
||
<!-- 总数量:默认遮挡具体数值,点右上「显示数量」可查看 -->
|
||
<template v-else-if="col.prop === 'quantity'" #default="{ row }">
|
||
<span>{{ quantityVisible ? (row[col.prop] ?? '--') : QUANTITY_MASK }}</span>
|
||
</template>
|
||
</el-table-column>
|
||
|
||
<template #empty>
|
||
<div class="empty-tip">暂无库存明细</div>
|
||
</template>
|
||
</el-table>
|
||
</div>
|
||
|
||
<div class="pagination-container">
|
||
<el-pagination
|
||
v-model:current-page="pagination.page"
|
||
v-model:page-size="pagination.pageSize"
|
||
:page-sizes="[10, 20, 50, 100]"
|
||
:total="pagination.total"
|
||
layout="total, sizes, prev, pager, next, jumper"
|
||
background
|
||
size="small"
|
||
@size-change="handlePageSizeChange"
|
||
@current-change="handlePageChange"
|
||
/>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { computed, nextTick, onBeforeUnmount, onMounted, reactive, ref, watch } from 'vue'
|
||
import * as inventoryApi from '@/api/storage.inventory/inventory'
|
||
import type { InventoryUpdateExpiryParams } from '@/api/storage.inventory/inventory'
|
||
import * as warehouseApi from '@/api/warehouse/warehouse'
|
||
import * as indexApi from '@/api/index/index'
|
||
import { ElMessage } from 'element-plus'
|
||
import { formatInventorySpaceLabel } from '../inventory-config'
|
||
|
||
type TransferMode = 'location' | 'product'
|
||
|
||
const props = defineProps<{
|
||
id: string | number
|
||
row?: any
|
||
groupByBatch?: boolean
|
||
}>()
|
||
|
||
const emit = defineEmits(['refresh'])
|
||
|
||
const loading = ref(false)
|
||
const detailList = ref<any[]>([])
|
||
const currentRow = ref<any>(null)
|
||
const tableWrapRef = ref<HTMLElement | null>(null)
|
||
const detailTableRef = ref<any>(null)
|
||
const selectedDetailRows = ref<any[]>([])
|
||
|
||
const pagination = reactive({
|
||
page: 1,
|
||
pageSize: 20,
|
||
total: 0
|
||
})
|
||
|
||
/** 前端分页(接口一次性返回该商品的全部库存明细行) */
|
||
const pagedDetailList = computed(() => {
|
||
const start = (pagination.page - 1) * pagination.pageSize
|
||
return detailList.value.slice(start, start + pagination.pageSize)
|
||
})
|
||
|
||
/** 切页后把表格滚动位置归零 */
|
||
function resetTableScroll() {
|
||
nextTick(() => {
|
||
detailTableRef.value?.setScrollTop?.(0)
|
||
})
|
||
}
|
||
|
||
function handlePageChange(page: number) {
|
||
pagination.page = page
|
||
resetTableScroll()
|
||
}
|
||
|
||
function handlePageSizeChange(pageSize: number) {
|
||
pagination.pageSize = pageSize
|
||
pagination.page = 1
|
||
resetTableScroll()
|
||
}
|
||
const tableHeight = ref<number | undefined>(undefined)
|
||
let tableResizeObserver: ResizeObserver | null = null
|
||
const dialogVisible = ref(false)
|
||
const transferMode = ref<TransferMode | null>(null)
|
||
const targetSpace = ref<string | number>('')
|
||
const spaceName = ref<string>('')
|
||
const targetSpaceError = ref<string>('')
|
||
let spaceInfoTimer: ReturnType<typeof setTimeout> | null = null
|
||
const transferQty = ref<number | null>(null)
|
||
const transferLoading = ref(false)
|
||
|
||
const detailViewMode = ref<'detail' | 'space'>('detail')
|
||
|
||
/** 是否按库位汇总视图(仅汇总模式可切换) */
|
||
const isSpaceView = computed(() => !props.groupByBatch && detailViewMode.value === 'space')
|
||
|
||
/** 明细表格勾选变化:单选一行时作为当前操作行(按明细视图驱动按库存记录移库,按库位汇总视图驱动按商品移库) */
|
||
function handleDetailSelectionChange(rows: any[]) {
|
||
selectedDetailRows.value = rows || []
|
||
if (rows && rows.length === 1) {
|
||
currentRow.value = normalizeRow(rows[0])
|
||
} else {
|
||
currentRow.value = props.groupByBatch ? (props.row ? normalizeRow(props.row) : null) : null
|
||
}
|
||
}
|
||
|
||
const detailColumns = computed(() => {
|
||
if (isSpaceView.value) {
|
||
return [
|
||
{ prop: 'space_code', label: '库位', width: '160' },
|
||
{ prop: 'physical_quantity', label: '实物数', width: '100' },
|
||
{ prop: 'quantity', label: '总数量', width: '100' },
|
||
{ prop: 'available_quantity', label: '可用数量', width: '100' },
|
||
{ prop: 'locked_quantity', label: '锁定数量', width: '100' },
|
||
{ prop: 'batch_count', label: '批次数', width: '80' },
|
||
{ prop: 'first_inbound_time', label: '首次入库时间', width: '160' },
|
||
{ prop: 'last_inbound_time', label: '最后入库时间', width: '160' }
|
||
]
|
||
}
|
||
return [
|
||
{ prop: 'storage_id', label: '库存记录ID', width: '110' },
|
||
{ prop: 'stock_id', label: '商品ID', width: '100' },
|
||
{ prop: 'item_code', label: '商品编码', width: '140' },
|
||
{ prop: 'item_name', label: '商品名称', width: '200' },
|
||
{ prop: 'batch_no', label: '批次号', width: '140' },
|
||
{ prop: 'space_code', label: '库位', width: '140' },
|
||
{ prop: 'production_date', label: '生产日期', width: '140' },
|
||
{ prop: 'expiry_date', label: '到期日期', width: '140' },
|
||
{ prop: 'quantity', label: '总数量', width: '100' },
|
||
{ prop: 'available_quantity', label: '可用数量', width: '100' },
|
||
{ prop: 'locked_quantity', label: '锁定数量', width: '100' }
|
||
]
|
||
})
|
||
|
||
/** 「总数量」数值是否可见:默认隐藏(只隐藏数值,列仍在;不影响数据与移库/维护日期逻辑) */
|
||
const quantityVisible = ref(false)
|
||
/** 隐藏时的占位符 */
|
||
const QUANTITY_MASK = '***'
|
||
|
||
const dialogTitle = computed(() => {
|
||
if (transferMode.value === 'location') return '按库存记录移库'
|
||
if (transferMode.value === 'product') return '按商品移库'
|
||
return '移库'
|
||
})
|
||
|
||
const confirmButtonText = computed(() => {
|
||
if (transferMode.value === 'location') return '确认按库存记录移库'
|
||
if (transferMode.value === 'product') return '确认按商品移库'
|
||
return '确认移库'
|
||
})
|
||
|
||
function isBatchMgmtEnabled(row: any) {
|
||
const flag = row?.stock?.is_batch_mgmt_enabled ?? row?.is_batch_mgmt_enabled
|
||
if (flag === true || flag === 1) return true
|
||
if (flag === false || flag === 0) return false
|
||
const s = String(flag || '').toLowerCase()
|
||
return s === 'y' || s === '1' || s === 'true'
|
||
}
|
||
|
||
const activeRow = computed(() => {
|
||
if (currentRow.value) return currentRow.value
|
||
// 批次模式:主列表选中行本身就是明细行;汇总模式:必须勾选明细/库位行才作为操作行
|
||
return props.groupByBatch ? (props.row || {}) : {}
|
||
})
|
||
|
||
const canTransferByLocation = computed(() => {
|
||
// 只要选中了含库存记录 ID 的明细行即可按库存记录移库(批次/汇总模式通用)
|
||
const row = activeRow.value
|
||
const storageId = Number(row.storage_id)
|
||
return Number.isFinite(storageId) && storageId > 0
|
||
})
|
||
|
||
const canTransferByProduct = computed(() => {
|
||
const row = activeRow.value
|
||
const spaceId = Number(row.space_id)
|
||
const stockId = Number(row.stock_id)
|
||
if (!Number.isFinite(spaceId) || spaceId <= 0) return false
|
||
if (!Number.isFinite(stockId) || stockId <= 0) return false
|
||
// 开启批次管理的商品不支持按商品移库(按原库位+商品合并会跨批次)
|
||
if (isBatchMgmtEnabled(row)) return false
|
||
return true
|
||
})
|
||
|
||
function normalizeRow(r: any) {
|
||
if (!r) return null
|
||
const row = { ...r }
|
||
row.stock = row.stock || {}
|
||
if (row.stock.is_batch_mgmt_enabled === undefined || row.stock.is_batch_mgmt_enabled === null) {
|
||
row.stock.is_batch_mgmt_enabled = false
|
||
}
|
||
row.batch_count = Number(row.batch_count || 0)
|
||
return row
|
||
}
|
||
|
||
const loadData = () => {
|
||
if (!props.groupByBatch) {
|
||
void loadDetailListByStockId()
|
||
return
|
||
}
|
||
loading.value = true
|
||
setTimeout(() => {
|
||
currentRow.value = props.row ? normalizeRow(props.row) : null
|
||
detailList.value = currentRow.value ? [currentRow.value] : []
|
||
pagination.page = 1
|
||
pagination.total = detailList.value.length
|
||
loading.value = false
|
||
}, 120)
|
||
}
|
||
|
||
/** 汇总模式:按商品拉取当前仓库下 quantity > 1 的库存明细行 */
|
||
async function loadDetailListByStockId() {
|
||
const stockId = Number(props.id)
|
||
if (!Number.isInteger(stockId) || stockId <= 0) {
|
||
currentRow.value = null
|
||
detailList.value = []
|
||
pagination.total = 0
|
||
return
|
||
}
|
||
loading.value = true
|
||
pagination.page = 1
|
||
try {
|
||
const isSpace = detailViewMode.value === 'space'
|
||
const payload: any = { stock_id: stockId }
|
||
if (isSpace) payload.group_by_space = true
|
||
const res: any = await inventoryApi.getInventoryListByStockId(payload)
|
||
if (res && (res.code === 200 || res.code === 1 || res.flag === 'success')) {
|
||
const data = res.data || res
|
||
const rows = Array.isArray(data.list) ? data.list : []
|
||
detailList.value = rows.map((item: any) =>
|
||
isSpace ? normalizeSpaceRow(item) : normalizeDetailRow(item)
|
||
)
|
||
} else {
|
||
detailList.value = []
|
||
}
|
||
pagination.total = detailList.value.length
|
||
await nextTick()
|
||
detailTableRef.value?.clearSelection?.()
|
||
currentRow.value = null
|
||
} catch (e) {
|
||
console.error('加载库存明细失败', e)
|
||
detailList.value = []
|
||
pagination.total = 0
|
||
} finally {
|
||
loading.value = false
|
||
}
|
||
}
|
||
|
||
/** 将接口明细行归一化为与主列表批次行一致的表格结构 */
|
||
function normalizeDetailRow(item: any) {
|
||
const row = normalizeRow(item)
|
||
row.storage_id = item.id ?? item.storage_id ?? null
|
||
row.stock_id = item.stock_id ?? item.stock?.id ?? null
|
||
row.item_code = item.stock?.item_code || item.item_code || ''
|
||
row.item_name = item.stock?.item_name || item.item_name || ''
|
||
row.batch_no = item.batch_no || ''
|
||
row.production_date = item.production_date || ''
|
||
row.expiry_date = item.expiry_date || ''
|
||
row.space_code = formatInventorySpaceLabel(item.space, '--')
|
||
row.space_id = item.space_id ?? item.space?.id ?? null
|
||
row.quantity = item.quantity ?? 0
|
||
row.available_quantity = item.available_quantity ?? 0
|
||
row.locked_quantity = item.locked_quantity ?? 0
|
||
return row
|
||
}
|
||
|
||
function openTransferDialog(mode: TransferMode) {
|
||
if (mode === 'location' && !canTransferByLocation.value) {
|
||
ElMessage.warning('请先在下方明细列表勾选一条库存记录')
|
||
return
|
||
}
|
||
if (mode === 'product' && !canTransferByProduct.value) {
|
||
ElMessage.warning('当前商品不支持按商品移库(需未开启批次管理,且有库位与商品)')
|
||
return
|
||
}
|
||
transferMode.value = mode
|
||
targetSpace.value = ''
|
||
transferQty.value = null
|
||
spaceName.value = ''
|
||
targetSpaceError.value = ''
|
||
dialogVisible.value = true
|
||
}
|
||
|
||
function resetTransferDialog() {
|
||
transferMode.value = null
|
||
targetSpace.value = ''
|
||
transferQty.value = null
|
||
spaceName.value = ''
|
||
targetSpaceError.value = ''
|
||
}
|
||
|
||
async function resolveTargetSpaceId(): Promise<number | null> {
|
||
const raw = String(targetSpace.value).trim()
|
||
if (!raw) return null
|
||
try {
|
||
const res: any = await indexApi.searchSpace({ keyword: raw })
|
||
const data = res && (res.data || res.list || res)
|
||
const item = Array.isArray(data) ? (data[0] || null) : data
|
||
if (item?.id !== undefined && item?.id !== null) {
|
||
const parsed = Number(item.id)
|
||
if (Number.isFinite(parsed) && parsed > 0) return parsed
|
||
}
|
||
} catch {
|
||
// ignore
|
||
}
|
||
return null
|
||
}
|
||
|
||
function parseOptionalQuantity(): number | null {
|
||
if (transferQty.value === null || transferQty.value === undefined) return null
|
||
if (String(transferQty.value).trim() === '') return null
|
||
const q = Number(transferQty.value)
|
||
if (!Number.isFinite(q) || q <= 0) return NaN
|
||
return q
|
||
}
|
||
|
||
function parseRequiredQuantity(): number | null {
|
||
const q = parseOptionalQuantity()
|
||
if (q === null || Number.isNaN(q)) return null
|
||
return q
|
||
}
|
||
|
||
async function confirmTransferLocation(row: any, targetSpaceId: number) {
|
||
const storageId = Number(row.storage_id)
|
||
const payload: { storage_id: number; target_space_id: number; quantity?: number } = {
|
||
storage_id: storageId,
|
||
target_space_id: targetSpaceId
|
||
}
|
||
const qty = parseOptionalQuantity()
|
||
if (qty !== null) {
|
||
if (Number.isNaN(qty)) {
|
||
ElMessage.warning('移库数量必须为正数')
|
||
return false
|
||
}
|
||
payload.quantity = qty
|
||
}
|
||
const res: any = await inventoryApi.transferLocation(payload)
|
||
ElMessage.success(res?.message || '按库存记录移库成功')
|
||
return true
|
||
}
|
||
|
||
async function confirmTransferByProduct(row: any, targetSpaceId: number) {
|
||
const qty = parseRequiredQuantity()
|
||
if (qty === null) {
|
||
ElMessage.warning('按商品移库时移库数量为必填')
|
||
return false
|
||
}
|
||
if (Number.isNaN(qty)) {
|
||
ElMessage.warning('移库数量必须为正数')
|
||
return false
|
||
}
|
||
const payload = {
|
||
space_id: Number(row.space_id),
|
||
stock_id: Number(row.stock_id),
|
||
target_space_id: targetSpaceId,
|
||
quantity: qty
|
||
}
|
||
const res: any = await inventoryApi.transferByProduct(payload)
|
||
ElMessage.success(res?.message || '按商品移库成功')
|
||
return true
|
||
}
|
||
|
||
async function confirmTransfer() {
|
||
if (!transferMode.value) return
|
||
if (!targetSpace.value) {
|
||
ElMessage.warning('请输入或扫码目标库位')
|
||
return
|
||
}
|
||
const row = activeRow.value
|
||
transferLoading.value = true
|
||
try {
|
||
const targetSpaceId = await resolveTargetSpaceId()
|
||
if (!targetSpaceId) {
|
||
ElMessage.warning('未找到目标库位,请检查输入后重试')
|
||
return
|
||
}
|
||
|
||
let ok = false
|
||
if (transferMode.value === 'location') {
|
||
ok = await confirmTransferLocation(row, targetSpaceId)
|
||
} else {
|
||
ok = await confirmTransferByProduct(row, targetSpaceId)
|
||
}
|
||
|
||
if (!ok) return
|
||
|
||
dialogVisible.value = false
|
||
emit('refresh')
|
||
try {
|
||
window.dispatchEvent(new CustomEvent('transfer-success'))
|
||
} catch {
|
||
// ignore
|
||
}
|
||
} catch (e: any) {
|
||
console.error('移库失败', e)
|
||
ElMessage.error(e?.message || '移库失败')
|
||
} finally {
|
||
transferLoading.value = false
|
||
}
|
||
}
|
||
|
||
/* ───────────────── 生产日期 / 到期日期维护(storage.inventory.updateExpiry) ───────────────── */
|
||
|
||
const expiryDialogVisible = ref(false)
|
||
const expiryLoading = ref(false)
|
||
const expiryRow = ref<any>(null)
|
||
/** 弹窗打开后需自动聚焦并展开日历的字段(由「生产日期 / 到期日期」单元格快捷入口指定) */
|
||
const expiryFocusField = ref<'production' | 'expiry' | null>(null)
|
||
const productionPickerRef = ref<any>(null)
|
||
const expiryPickerRef = ref<any>(null)
|
||
const expiryForm = reactive({
|
||
/** 输入框默认回显库内日期;与库内一致=不修改,清空输入框=下发 null 置空 */
|
||
productionDate: '',
|
||
expiryDate: ''
|
||
})
|
||
|
||
/**
|
||
* 由输入框内容推导单个字段的下发内容(UI 不再有额外开关,三态由「与库内值比较」自动得出):
|
||
* - undefined:与库内一致 —— 不下发,保持原值
|
||
* - null:输入框为空且库内有值 —— 清空该字段
|
||
* - string:下发该日期
|
||
*/
|
||
function resolveExpiryFieldPayload(value: string, current: unknown): string | null | undefined {
|
||
const picked = toComparableDate(value)
|
||
const currentNorm = toComparableDate(current)
|
||
if (picked === currentNorm) return undefined
|
||
return picked || null
|
||
}
|
||
|
||
/** 是否有需要下发的内容(至少一个字段被修改或清空),否则无从提交 */
|
||
const hasExpiryChange = computed(
|
||
() =>
|
||
resolveExpiryFieldPayload(expiryForm.productionDate, expiryRow.value?.production_date) !== undefined ||
|
||
resolveExpiryFieldPayload(expiryForm.expiryDate, expiryRow.value?.expiry_date) !== undefined
|
||
)
|
||
|
||
/** 行上的库存记录 ID(无则返回 null,用于判断该行是否可维护日期) */
|
||
function getRowStorageId(row: any): number | null {
|
||
const id = Number(row?.storage_id)
|
||
return Number.isFinite(id) && id > 0 ? id : null
|
||
}
|
||
|
||
/** 可点击快捷维护的日期列(与明细表列配置中的 prop 对应) */
|
||
const EXPIRY_DATE_COLUMN_PROPS = ['production_date', 'expiry_date']
|
||
|
||
function isExpiryDateColumn(prop: string): boolean {
|
||
return EXPIRY_DATE_COLUMN_PROPS.includes(prop)
|
||
}
|
||
|
||
/** 是否可维护日期:需要有一条含库存记录 ID 的库存明细行 */
|
||
const canEditExpiry = computed(() => getRowStorageId(activeRow.value) !== null)
|
||
|
||
const expiryItemLabel = computed(() => {
|
||
const row = expiryRow.value || {}
|
||
const code = String(row.item_code || '').trim()
|
||
const name = String(row.item_name || '').trim()
|
||
if (code && name) return `${code} ${name}`
|
||
return code || name || '--'
|
||
})
|
||
|
||
/** 日期展示:空值统一为「(空)」 */
|
||
function formatExpiryValue(value: unknown): string {
|
||
const text = value === null || value === undefined ? '' : String(value).trim()
|
||
return text || '(空)'
|
||
}
|
||
|
||
/** 单个字段的提交描述,与接口「未传=保持原值 / null 或空串=清空」语义一一对应 */
|
||
function describeExpiryField(value: string, current: unknown): string {
|
||
const next = resolveExpiryFieldPayload(value, current)
|
||
if (next === undefined) return '不下发,保持库内原值'
|
||
if (next === null) return 'null,清空该字段'
|
||
return next
|
||
}
|
||
|
||
/** 提交内容预览(让用户明确本次请求实际下发了哪些字段) */
|
||
const expiryPayloadPreview = computed(
|
||
() =>
|
||
`production_date:${describeExpiryField(expiryForm.productionDate, expiryRow.value?.production_date)};` +
|
||
`expiry_date:${describeExpiryField(expiryForm.expiryDate, expiryRow.value?.expiry_date)}`
|
||
)
|
||
|
||
/** 归一化为 YYYY-MM-DD 以便比较先后(兼容 YYYY/MM/DD、带时间与 ISO 串) */
|
||
function toComparableDate(value: unknown): string {
|
||
const text = String(value ?? '').trim()
|
||
if (!text) return ''
|
||
const matched = text.match(/^(\d{4})[-/](\d{1,2})[-/](\d{1,2})/)
|
||
if (!matched) return ''
|
||
return `${matched[1]}-${String(matched[2]).padStart(2, '0')}-${String(matched[3]).padStart(2, '0')}`
|
||
}
|
||
|
||
/** 变更前后文案:仅输出实际发生变化的字段 */
|
||
function buildExpiryChangeText(data: any): string {
|
||
const row = expiryRow.value || {}
|
||
const items: Array<[string, unknown, unknown]> = [
|
||
['生产日期', data?.before_production_date ?? row.production_date, data?.production_date],
|
||
['到期日期', data?.before_expiry_date ?? row.expiry_date, data?.expiry_date]
|
||
]
|
||
return items
|
||
.filter(([, before, after]) => formatExpiryValue(before) !== formatExpiryValue(after))
|
||
.map(([label, before, after]) => `${label}:${formatExpiryValue(before)} → ${formatExpiryValue(after)}`)
|
||
.join(';')
|
||
}
|
||
|
||
/**
|
||
* 打开日期维护弹窗(两个入口共用):
|
||
* 1. 工具栏「维护生产/到期日期」按钮:不传参,取当前勾选行;
|
||
* 2. 明细表「生产日期 / 到期日期」单元格:传入该行与目标字段,弹窗打开后自动展开对应日历
|
||
*/
|
||
function openExpiryDialog(row?: any, target?: 'production' | 'expiry') {
|
||
const targetRow = normalizeRow(row) || normalizeRow(activeRow.value) || {}
|
||
if (!getRowStorageId(targetRow)) {
|
||
ElMessage.warning(
|
||
row ? '该行缺少库存记录 ID,无法维护日期' : '请先在下方明细列表勾选一条库存记录'
|
||
)
|
||
return
|
||
}
|
||
expiryRow.value = targetRow
|
||
// 输入框回显库内当前日期(归一化为 YYYY-MM-DD,兼容接口返回的 ISO / 带时间格式)
|
||
expiryForm.productionDate = toComparableDate(targetRow.production_date)
|
||
expiryForm.expiryDate = toComparableDate(targetRow.expiry_date)
|
||
expiryFocusField.value = target || null
|
||
expiryDialogVisible.value = true
|
||
}
|
||
|
||
/** 弹窗打开动画结束后,聚焦并展开快捷入口指定的日期选择器 */
|
||
async function handleExpiryDialogOpened() {
|
||
const target = expiryFocusField.value
|
||
if (!target) return
|
||
await nextTick()
|
||
const picker = target === 'production' ? productionPickerRef.value : expiryPickerRef.value
|
||
picker?.focus?.()
|
||
picker?.handleOpen?.()
|
||
}
|
||
|
||
function resetExpiryForm() {
|
||
expiryRow.value = null
|
||
expiryFocusField.value = null
|
||
expiryForm.productionDate = ''
|
||
expiryForm.expiryDate = ''
|
||
}
|
||
|
||
async function confirmExpiry() {
|
||
const row = expiryRow.value || {}
|
||
const storageId = Number(row.storage_id)
|
||
if (!Number.isInteger(storageId) || storageId <= 0) {
|
||
ElMessage.warning('缺少库存记录 ID,请重新选择一条库存明细行')
|
||
return
|
||
}
|
||
const production = resolveExpiryFieldPayload(expiryForm.productionDate, row.production_date)
|
||
const expiry = resolveExpiryFieldPayload(expiryForm.expiryDate, row.expiry_date)
|
||
if (production === undefined && expiry === undefined) {
|
||
ElMessage.warning('生产日期与到期日期均未改动,请修改后再提交')
|
||
return
|
||
}
|
||
|
||
const mergedProduction = toComparableDate(expiryForm.productionDate)
|
||
const mergedExpiry = toComparableDate(expiryForm.expiryDate)
|
||
if (mergedProduction && mergedExpiry && mergedProduction > mergedExpiry) {
|
||
ElMessage.warning('生产日期不能晚于到期日期,请调整后重试')
|
||
return
|
||
}
|
||
|
||
const payload: InventoryUpdateExpiryParams = { storage_id: storageId }
|
||
if (production !== undefined) payload.production_date = production
|
||
if (expiry !== undefined) payload.expiry_date = expiry
|
||
|
||
expiryLoading.value = true
|
||
try {
|
||
const res: any = await inventoryApi.updateInventoryExpiry(payload)
|
||
if (!(res && (res.code === 200 || res.code === 1 || res.flag === 'success'))) {
|
||
ElMessage.error(res?.message || '维护生产日期/到期日期失败')
|
||
return
|
||
}
|
||
const data = res.data || {}
|
||
if (data.changed === false) {
|
||
ElMessage.info(res.message || '生产日期与到期日期均无变化,未做修改')
|
||
} else {
|
||
const changeText = buildExpiryChangeText(data)
|
||
ElMessage.success(changeText ? `维护成功:${changeText}` : res.message || '维护成功')
|
||
}
|
||
expiryDialogVisible.value = false
|
||
await loadData()
|
||
emit('refresh')
|
||
} catch (e: any) {
|
||
console.error('维护生产日期/到期日期失败', e)
|
||
ElMessage.error(e?.message || '维护生产日期/到期日期失败')
|
||
} finally {
|
||
expiryLoading.value = false
|
||
}
|
||
}
|
||
|
||
/** 实物数 = 可用数量 + 锁定数量 */
|
||
function resolvePhysicalQuantity(row: any): number {
|
||
const available = Number(row?.available_quantity ?? 0)
|
||
const locked = Number(row?.locked_quantity ?? 0)
|
||
return (Number.isFinite(available) ? available : 0) + (Number.isFinite(locked) ? locked : 0)
|
||
}
|
||
|
||
/** 按库位汇总行归一化(无批次字段,含库位/数量/批次数/入库时间) */
|
||
function normalizeSpaceRow(item: any) {
|
||
const row = normalizeRow(item)
|
||
row.space_id = item.space_id ?? item.space?.id ?? null
|
||
row.stock_id = item.stock_id ?? item.stock?.id ?? null
|
||
row.space_code = formatInventorySpaceLabel(item.space, '--')
|
||
row.quantity = item.quantity ?? 0
|
||
row.available_quantity = item.available_quantity ?? 0
|
||
row.locked_quantity = item.locked_quantity ?? 0
|
||
row.physical_quantity = resolvePhysicalQuantity(row)
|
||
row.batch_count = Number(item.batch_count || 0)
|
||
row.first_inbound_time = item.first_inbound_time || ''
|
||
row.last_inbound_time = item.last_inbound_time || ''
|
||
row.storage_id = null
|
||
return row
|
||
}
|
||
|
||
watch(detailViewMode, () => {
|
||
if (!props.groupByBatch) void loadDetailListByStockId()
|
||
})
|
||
|
||
watch(() => [props.id, props.row, props.groupByBatch], loadData, { immediate: true })
|
||
|
||
onMounted(() => {
|
||
const updateTableHeight = () => {
|
||
const el = tableWrapRef.value
|
||
if (!el) return
|
||
const h = el.clientHeight
|
||
if (h > 0) tableHeight.value = h
|
||
}
|
||
updateTableHeight()
|
||
if (typeof ResizeObserver !== 'undefined') {
|
||
tableResizeObserver = new ResizeObserver(updateTableHeight)
|
||
if (tableWrapRef.value) tableResizeObserver.observe(tableWrapRef.value)
|
||
}
|
||
})
|
||
|
||
onBeforeUnmount(() => {
|
||
tableResizeObserver?.disconnect()
|
||
tableResizeObserver = null
|
||
})
|
||
|
||
watch(targetSpace, (val) => {
|
||
if (spaceInfoTimer) clearTimeout(spaceInfoTimer)
|
||
spaceName.value = ''
|
||
targetSpaceError.value = ''
|
||
const v = String(val || '').trim()
|
||
if (!v) return
|
||
spaceInfoTimer = setTimeout(async () => {
|
||
try {
|
||
const id = Number(v)
|
||
let res: any = null
|
||
try {
|
||
res = await indexApi.searchSpace({ keyword: v })
|
||
} catch {
|
||
if (!Number.isNaN(id)) res = await warehouseApi.getSpaceInfo(id)
|
||
}
|
||
|
||
const data = res && (res.data || res.list || res)
|
||
if (data) {
|
||
const item = Array.isArray(data) ? (data[0] || null) : data
|
||
if (item) {
|
||
spaceName.value = formatInventorySpaceLabel(item)
|
||
targetSpaceError.value = ''
|
||
} else {
|
||
spaceName.value = ''
|
||
targetSpaceError.value = 'not_found'
|
||
}
|
||
} else {
|
||
spaceName.value = ''
|
||
targetSpaceError.value = 'not_found'
|
||
}
|
||
} catch (err) {
|
||
console.error('getSpaceInfo failed', err)
|
||
spaceName.value = ''
|
||
targetSpaceError.value = 'error'
|
||
}
|
||
}, 300)
|
||
})
|
||
</script>
|
||
|
||
<style scoped lang="scss">
|
||
.panel-stock-detail {
|
||
width: 100%;
|
||
height: 100%;
|
||
padding: 6px 10px 8px;
|
||
background: #fff;
|
||
display: flex;
|
||
flex-direction: column;
|
||
overflow: hidden;
|
||
box-sizing: border-box;
|
||
|
||
.main-content {
|
||
flex: 1;
|
||
min-height: 0;
|
||
display: flex;
|
||
flex-direction: column;
|
||
overflow: hidden;
|
||
}
|
||
|
||
/* 按钮行(间距 6px 对齐 toolbar)+ 右侧视图切换,样式对齐 stock/external_stock */
|
||
.header-actions {
|
||
display: flex;
|
||
justify-content: space-between;
|
||
align-items: center;
|
||
margin-bottom: 6px;
|
||
flex-shrink: 0;
|
||
gap: 4px;
|
||
flex-wrap: wrap;
|
||
}
|
||
|
||
.left-actions {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 6px;
|
||
|
||
/* 清除 Element Plus 默认按钮左边距,统一由 gap 控制等间距 */
|
||
:deep(.el-button) {
|
||
margin-left: 0;
|
||
margin-right: 0;
|
||
}
|
||
}
|
||
|
||
.right-actions {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 6px;
|
||
}
|
||
|
||
.detail-table-wrap {
|
||
flex: 1;
|
||
min-height: 0;
|
||
overflow: hidden;
|
||
}
|
||
|
||
.pagination-container {
|
||
flex-shrink: 0;
|
||
display: flex;
|
||
justify-content: flex-end;
|
||
align-items: center;
|
||
margin-top: 6px;
|
||
|
||
/* 每页条数下拉框与 UpperTable 的分页器保持一致 */
|
||
:deep(.el-select--small) {
|
||
width: 80px;
|
||
}
|
||
|
||
:deep(.el-input__wrapper) {
|
||
padding: 0 4px;
|
||
}
|
||
}
|
||
|
||
.empty-tip {
|
||
color: #909399;
|
||
font-size: 13px;
|
||
}
|
||
|
||
.transfer-btn-wrap {
|
||
display: inline-block;
|
||
}
|
||
|
||
.transfer-hint {
|
||
margin: 0 0 12px;
|
||
font-size: 12px;
|
||
color: #909399;
|
||
line-height: 1.5;
|
||
}
|
||
|
||
.transfer-form {
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 8px;
|
||
}
|
||
|
||
.target-space-tip {
|
||
min-height: 22px;
|
||
font-size: 12px;
|
||
}
|
||
|
||
.target-space-error {
|
||
color: #f56c6c;
|
||
}
|
||
|
||
.expiry-context {
|
||
margin-bottom: 12px;
|
||
}
|
||
|
||
.expiry-hint {
|
||
margin: 0 0 12px;
|
||
font-size: 12px;
|
||
color: #909399;
|
||
line-height: 1.5;
|
||
}
|
||
|
||
.expiry-form {
|
||
.expiry-field {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 12px;
|
||
flex-wrap: wrap;
|
||
width: 100%;
|
||
}
|
||
|
||
.expiry-date-picker {
|
||
flex: 1;
|
||
min-width: 0;
|
||
}
|
||
|
||
.expiry-clear-btn {
|
||
flex-shrink: 0;
|
||
}
|
||
}
|
||
|
||
.expiry-preview {
|
||
display: flex;
|
||
gap: 8px;
|
||
padding: 8px 10px;
|
||
border-radius: 4px;
|
||
background: #f5f7fa;
|
||
font-size: 12px;
|
||
color: #606266;
|
||
line-height: 1.6;
|
||
|
||
.expiry-preview-label {
|
||
flex-shrink: 0;
|
||
color: #909399;
|
||
}
|
||
|
||
.expiry-preview-text {
|
||
word-break: break-all;
|
||
}
|
||
}
|
||
}
|
||
</style>
|