修改bom表逻辑和出库选单内容

This commit is contained in:
dxc
2026-02-12 10:39:21 +08:00
parent b93a565c82
commit d61668bc4b
8 changed files with 838 additions and 296 deletions

View File

@ -22,233 +22,595 @@
</div>
</template>
<el-alert
v-if="selectedItems.length === 0"
title="清单为空,请点击右上角按钮添加物品"
type="info"
center
show-icon
style="margin-bottom: 20px"
<el-alert
v-if="selectedItems.length === 0"
title="清单为空,请点击右上角按钮添加物品"
type="info"
center
show-icon
style="margin-bottom: 20px"
/>
<el-table
v-else
:data="selectedItems"
border
style="width: 100%"
row-key="uniqueKey"
<el-table
v-else
:data="selectedItems"
border
style="width: 100%"
row-key="uniqueKey"
>
<el-table-column type="index" label="序号" width="50" align="center" />
<el-table-column prop="typeLabel" label="类型" width="80" />
<el-table-column prop="name" label="名称" />
<el-table-column prop="standard" label="规格" />
<el-table-column prop="available_quantity" label="当前库存" width="100" />
<el-table-column label="本次出库" width="180" align="center">
<el-table-column label="类型" width="100" align="center">
<template #default="{ row }">
<el-input-number
v-model="row.export_quantity"
:min="1"
:max="row.available_quantity"
size="small"
/>
<el-tag :type="getTypeTag(row.type)">{{ row.typeLabel }}</el-tag>
</template>
</el-table-column>
<el-table-column label="操作" width="80" align="center">
<el-table-column prop="name" label="名称" min-width="150" show-overflow-tooltip />
<el-table-column prop="standard" label="规格型号" min-width="150" show-overflow-tooltip />
<el-table-column prop="available_quantity" label="当前库存" width="120" align="right">
<template #default="{ row }">
<span style="color: green; font-weight: bold;">{{ row.available_quantity }}</span>
</template>
</el-table-column>
<el-table-column label="本次出库数" width="180" align="center" fixed="right">
<template #default="{ row }">
<el-input-number
v-model="row.export_quantity"
:min="0"
:max="row.available_quantity"
size="small"
style="width: 100%"
/>
</template>
</el-table-column>
<el-table-column label="操作" width="80" align="center" fixed="right">
<template #default="{ $index }">
<el-button type="danger" link @click="removeRow($index)">移除</el-button>
</template>
</el-table-column>
</el-table>
<div v-if="selectedItems.length > 0" style="margin-top: 15px; text-align: right; color: #606266;">
<span style="color: red; font-weight: bold;">{{ selectedItems.length }}</span> 种物品
合计出库 <span style="color: red; font-weight: bold;">{{ totalExportCount }}</span>
</div>
</el-card>
<el-dialog v-model="manualDialogVisible" title="选择库存物品" width="80%" top="5vh">
<div class="filter-container">
<el-input v-model="searchKeyword" placeholder="搜索名称/规格" style="width: 300px" @input="filterStock" />
</div>
<el-table
:data="filteredStockData"
height="500"
border
<el-dialog v-model="manualDialogVisible" title="选择库存物品" width="85%" top="5vh" destroy-on-close>
<div class="filter-container">
<el-input
v-model="searchKeyword"
placeholder="请输入物料名称 或 规格型号 进行搜索"
style="width: 300px"
:prefix-icon="Search"
clearable
@input="filterStock"
/>
<span style="margin-left: 15px; color: #909399; font-size: 12px;">
提示勾选物品后可直接在表格中修改本次出库数量
</span>
</div>
<el-table
:data="filteredStockData"
height="500"
border
row-key="uniqueKey"
@selection-change="handleStockSelection"
>
<el-table-column type="selection" width="55" />
>
<el-table-column type="selection" width="55" align="center" :reserve-selection="true" />
<el-table-column label="类型" width="90" align="center">
<template #default="{ row }">
<el-tag size="small" :type="getTypeTag(row.type)">{{ row.typeLabel }}</el-tag>
</template>
</el-table-column>
<el-table-column prop="name" label="名称" show-overflow-tooltip />
<el-table-column prop="standard" label="规格" show-overflow-tooltip />
<el-table-column prop="available_quantity" label="可用库存" width="100" align="right" />
<el-table-column label="本次出库" width="160" align="center" fixed="right">
<template #default="{ row }">
<el-input-number
v-model="row.export_quantity"
:min="1"
:max="row.available_quantity"
size="small"
style="width: 100%"
placeholder="数量"
@click.stop
/>
</template>
</el-table-column>
</el-table>
<template #footer>
<span style="float: left; line-height: 32px; color: #909399;">
已勾选 {{ tempSelection.length }}
</span>
<el-button @click="manualDialogVisible = false">取消</el-button>
<el-button type="primary" @click="confirmManualAdd">确认添加</el-button>
</template>
</el-dialog>
<el-dialog v-model="bomSelectVisible" title="按 BOM 套餐添加" width="600px">
<el-form label-width="100px">
<el-form-item label="选择产品">
<el-select v-model="selectedBomNo" filterable placeholder="请选择产品BOM配方" style="width: 100%">
<el-option
v-for="b in bomOptions"
:key="b.bom_no"
:label="`${b.parent_name} - ${b.version}`"
:value="b.bom_no"
/>
</el-select>
</el-form-item>
<el-form-item label="生产套数">
<el-input-number v-model="bomSets" :min="1" label="套" style="width: 200px;" />
</el-form-item>
</el-form>
<div style="margin-left: 100px; color: #909399; font-size: 12px;">
注意系统将自动计算所需原料数量 ( 配方用量 × 套数 )
</div>
<template #footer>
<el-button @click="bomSelectVisible = false">取消</el-button>
<el-button type="primary" @click="confirmBomAdd">一键计算并添加</el-button>
</template>
</el-dialog>
<el-dialog
v-model="previewVisible"
title="出库单核对与打印"
width="800px"
destroy-on-close
class="no-print-content"
>
<div class="print-preview-content">
<el-alert title="请核对以下清单,确认无误后点击【确认打印】" type="info" :closable="false" style="margin-bottom: 10px;" />
<el-table :data="validSelectedItems" border size="small" style="width: 100%">
<el-table-column type="index" label="序号" width="60" align="center" />
<el-table-column prop="typeLabel" label="类型" width="80" />
<el-table-column prop="name" label="名称" />
<el-table-column prop="standard" label="规格" />
<el-table-column prop="available_quantity" label="可用库存" />
<el-table-column label="本次出库" width="150">
<el-table-column prop="export_quantity" label="本次出库" width="120" align="center">
<template #default="{ row }">
<el-input-number
v-model="row.export_quantity"
:min="1"
:max="row.available_quantity"
size="small"
style="width: 100%"
/>
<span style="font-weight: bold; color: #F56C6C; font-size: 16px;">{{ row.export_quantity }}</span>
</template>
</el-table-column>
</el-table>
<template #footer>
<el-button @click="manualDialogVisible = false">取消</el-button>
<el-button type="primary" @click="confirmManualAdd">确认添加</el-button>
</template>
</el-table>
<div class="summary-info" style="margin-top: 20px; text-align: right; font-weight: bold;">
总计出库: <span style="color: red; font-size: 18px;">{{ totalExportCount }}</span>
</div>
</div>
<template #footer>
<span class="dialog-footer">
<el-button @click="previewVisible = false">取消</el-button>
<el-button type="warning" :icon="Download" :loading="exportLoading" @click="confirmExport">
导出 Excel
</el-button>
<el-button type="primary" :icon="Printer" :loading="printLoading" @click="confirmPrint">
确认打印 (A4)
</el-button>
</span>
</template>
</el-dialog>
<el-dialog v-model="bomSelectVisible" title="选择 BOM 套餐" width="600px">
<el-form label-width="80px">
<el-form-item label="选择产品">
<el-select v-model="selectedBomNo" filterable placeholder="选择要出库的产品BOM" style="width: 100%">
<el-option
v-for="b in bomOptions"
:key="b.bom_no"
:label="`${b.parent_name} (${b.version})`"
:value="b.bom_no"
/>
</el-select>
</el-form-item>
<el-form-item label="生产套数">
<el-input-number v-model="bomSets" :min="1" label="套" />
</el-form-item>
</el-form>
<template #footer>
<el-button @click="bomSelectVisible = false">取消</el-button>
<el-button type="primary" @click="confirmBomAdd">一键添加</el-button>
</template>
</el-dialog>
<div id="print-area">
<div class="print-header">
<h1>IRIS出库拣货确认单</h1>
<div class="print-meta-row">
<span>打印时间: {{ currentTime }}</span>
<span>单据编号: {{ currentOrderNo }}</span>
</div>
<div class="header-line"></div>
</div>
<el-dialog v-model="previewVisible" title="打印预览" width="800px">
<el-button @click="window.print()">确认打印</el-button>
</el-dialog>
<table class="print-table">
<thead>
<tr>
<th style="width: 60px;">序号</th>
<th>物料名称</th>
<th>规格型号</th>
<th style="width: 60px;">单位</th>
<th style="width: 100px;">出库数量</th>
<th style="width: 60px;">备注</th>
</tr>
</thead>
<tbody>
<tr v-for="(item, index) in validSelectedItems" :key="index">
<td style="text-align: center;">{{ index + 1 }}</td>
<td class="cell-padding">{{ item.name }}</td>
<td class="cell-padding">{{ item.standard }}</td>
<td style="text-align: center;"></td>
<td style="text-align: center; font-weight: bold; font-size: 16px;">{{ item.export_quantity }}</td>
<td></td>
</tr>
<tr v-if="validSelectedItems.length === 0">
<td colspan="6" style="text-align: center; padding: 20px;">无数据</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="4" style="text-align: right; font-weight: bold; padding-right: 15px;">合计:</td>
<td style="text-align: center; font-weight: bold; font-size: 18px;">{{ totalExportCount }}</td>
<td></td>
</tr>
</tfoot>
</table>
<div class="print-footer">
<div class="signature-item">
<span class="sig-label">库管员签字:</span>
<span class="sig-line"></span>
</div>
<div class="signature-item">
<span class="sig-label">领料人签字:</span>
<span class="sig-line"></span>
</div>
<div class="signature-item">
<span class="sig-label">日期:</span>
<span class="sig-line"></span>
</div>
</div>
</div>
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import { Plus, List, Printer } from '@element-plus/icons-vue'
import { ref, computed } from 'vue'
import { Printer, Search, Plus, Download, List } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { getAllStock } from '@/api/inbound/stock'
import { getAllStock, printSelectionList } from '@/api/inbound/stock'
import { getBomList, getBomDetail } from '@/api/bom'
// 购物车数据
// --- 状态变量 ---
// 核心:购物车数据
const selectedItems = ref<any[]>([])
// 手动选择相关
// 弹窗与加载状态
const manualDialogVisible = ref(false)
const allStockData = ref<any[]>([])
const bomSelectVisible = ref(false)
const previewVisible = ref(false)
const exportLoading = ref(false)
const printLoading = ref(false)
// 数据缓存
const allStockData = ref<any[]>([])
const filteredStockData = ref<any[]>([])
const searchKeyword = ref('')
const tempSelection = ref<any[]>([])
const tempSelection = ref<any[]>([]) // 手动添加时的临时勾选
// BOM 相关
const bomSelectVisible = ref(false)
const bomOptions = ref<any[]>([])
const selectedBomNo = ref('')
const bomSets = ref(1)
const previewVisible = ref(false)
// 打印相关
const currentTime = ref('')
const currentOrderNo = ref('')
// --- 方法 ---
// --- 计算属性 ---
// 有效的出库项 (数量 > 0)
const validSelectedItems = computed(() => {
return selectedItems.value.filter(item => item.export_quantity > 0)
})
const totalExportCount = computed(() => {
return validSelectedItems.value.reduce((sum, item) => sum + (item.export_quantity || 0), 0)
})
// --- 辅助方法 ---
const getTypeTag = (type: string) => {
switch (type) {
case 'material': return 'info'
case 'semi': return 'warning'
case 'product': return 'success'
default: return ''
}
}
const generateOrderNo = () => {
const now = new Date();
const dateStr = now.getFullYear() + (now.getMonth()+1).toString().padStart(2,'0') + now.getDate().toString().padStart(2,'0');
const random = Math.floor(Math.random()*1000).toString().padStart(3, '0');
return 'OUT' + dateStr + '-' + random;
}
// --- 核心逻辑 1手动添加库存 (支持弹窗内修改数量) ---
// 1. 打开手动选择
const openManualSelect = async () => {
manualDialogVisible.value = true
if (allStockData.value.length === 0) {
const res:any = await getAllStock()
// 简单处理数据,实际应复用之前的 normalizeItem 逻辑
const list = res.materials ? [...res.materials, ...res.products, ...res.semis] : []
allStockData.value = list.map((i:any) => ({
...i,
name: i.name || i.material_name || i.product_name,
standard: i.standard || i.spec_model,
uniqueKey: i.id + '_' + i.type,
export_quantity: 1,
typeLabel: i.type === 'material' ? '采购件' : i.type === 'semi' ? '半成品' : i.type === 'product' ? '成品' : '未知'
}))
filteredStockData.value = allStockData.value
try {
const res: any = await getAllStock()
// 1. 分类处理并打上标记
const rawMaterials = (res.materials || []).map((i: any) => ({ ...i, type: 'material', typeLabel: '采购件' }))
const rawSemis = (res.semis || []).map((i: any) => ({ ...i, type: 'semi', typeLabel: '半成品' }))
const rawProducts = (res.products || []).map((i: any) => ({ ...i, type: 'product', typeLabel: '成品' }))
// 2. 合并
const list = [...rawMaterials, ...rawSemis, ...rawProducts]
// 3. 规范化并生成 UniqueKey
allStockData.value = list.map((i: any) => ({
...i,
name: i.name || i.material_name || i.product_name || '未知名称',
standard: i.standard || i.spec_model || '',
// ★ 确保 Key 唯一格式类型_ID
uniqueKey: `${i.type}_${i.id}`,
available_quantity: parseFloat(i.available_quantity) || 0,
export_quantity: 1 // ★ 默认初始化为1方便用户在弹窗里看到并修改
}))
filteredStockData.value = allStockData.value
} catch (e) {
ElMessage.error('加载库存数据失败')
}
} else {
// 每次打开时重置筛选并将所有项的“本次出库”重置为1或保留上次视需求而定这里重置为1以防混淆
searchKeyword.value = ''
allStockData.value.forEach(item => item.export_quantity = 1)
filteredStockData.value = allStockData.value
}
}
const filterStock = () => {
const kw = searchKeyword.value.toLowerCase()
filteredStockData.value = allStockData.value.filter(i =>
(i.name && i.name.toLowerCase().includes(kw)) || (i.standard && i.standard.toLowerCase().includes(kw))
)
const kw = searchKeyword.value.trim().toLowerCase()
if (!kw) {
filteredStockData.value = allStockData.value
return
}
filteredStockData.value = allStockData.value.filter(i =>
(i.name && i.name.toLowerCase().includes(kw)) ||
(i.standard && i.standard.toLowerCase().includes(kw))
)
}
const handleStockSelection = (val: any[]) => { tempSelection.value = val }
const handleStockSelection = (val: any[]) => {
tempSelection.value = val
}
const confirmManualAdd = () => {
const newItems = tempSelection.value.filter(item =>
if (tempSelection.value.length === 0) {
return ElMessage.warning('请先勾选需要添加的物品')
}
// 1. 过滤已存在的 (避免重复)
const newItems = tempSelection.value.filter(item =>
!selectedItems.value.find(existing => existing.uniqueKey === item.uniqueKey)
)
selectedItems.value.push(...newItems)
manualDialogVisible.value = false
tempSelection.value = []
ElMessage.success(`添加了 ${newItems.length}`)
)
if (newItems.length === 0) {
manualDialogVisible.value = false
return ElMessage.warning('选中的物品已全部在清单中')
}
// 2. 深拷贝加入购物车 (防止引用关联)
const itemsToAdd = newItems.map(item => {
const copy = JSON.parse(JSON.stringify(item))
// ★ 关键修改:直接使用用户在弹窗里输入的 export_quantity
// 如果用户把输入框清空了(undefined)则默认为1
copy.export_quantity = (item.export_quantity && item.export_quantity > 0) ? item.export_quantity : 1
return copy
})
selectedItems.value.push(...itemsToAdd)
manualDialogVisible.value = false
tempSelection.value = [] // 清空临时勾选
ElMessage.success(`成功添加 ${itemsToAdd.length} 项物品`)
}
// 2. 打开 BOM 选择
// --- 核心逻辑 2按 BOM 添加 ---
const openBomSelect = async () => {
bomSelectVisible.value = true
const res = await getBomList()
bomOptions.value = res.data || []
// 每次打开 BOM 弹窗重置套数为 1
bomSets.value = 1
try {
const res = await getBomList()
bomOptions.value = res.data || []
} catch (e) {
ElMessage.error('加载 BOM 列表失败')
}
}
const confirmBomAdd = async () => {
if(!selectedBomNo.value) return;
const detailRes = await getBomDetail(selectedBomNo.value)
const bomRows = detailRes.data || []
if(!selectedBomNo.value) return ElMessage.warning('请选择 BOM');
// 确保库存数据已加载,用于匹配
if (allStockData.value.length === 0) {
// 确保有库存数据用于匹配
await openManualSelect()
manualDialogVisible.value = false // 仅加载数据,不显示弹窗
await openManualSelect()
manualDialogVisible.value = false // 仅加载数据,不显示弹窗
}
let addedCount = 0;
bomRows.forEach((bomItem:any) => {
const needQty = bomItem.dosage * bomSets.value
// 简单匹配逻辑通过名称或ID匹配
const stockCandidate = allStockData.value.find(s =>
(s.base_id == bomItem.child_id)
try {
const detailRes = await getBomDetail(selectedBomNo.value)
const bomRows = detailRes.data || []
let addedCount = 0;
// 遍历 BOM 子件
bomRows.forEach((bomItem: any) => {
// ★ 这里本身就是“选择数量”的逻辑:用量 * 套数
const needQty = (parseFloat(bomItem.dosage) || 0) * bomSets.value
// 简单匹配逻辑:匹配 base_id (最准确)
const stockCandidate = allStockData.value.find(s =>
(s.base_id && s.base_id == bomItem.child_id)
)
if (stockCandidate) {
const existing = selectedItems.value.find(e => e.uniqueKey === stockCandidate.uniqueKey)
if (existing) {
existing.export_quantity += needQty
} else {
const newItem = JSON.parse(JSON.stringify(stockCandidate))
newItem.export_quantity = needQty
selectedItems.value.push(newItem)
}
addedCount++
const existing = selectedItems.value.find(e => e.uniqueKey === stockCandidate.uniqueKey)
if (existing) {
// 如果已存在,累加数量
existing.export_quantity += needQty
} else {
const newItem = JSON.parse(JSON.stringify(stockCandidate))
newItem.export_quantity = needQty
selectedItems.value.push(newItem)
}
addedCount++
}
})
if(addedCount > 0) {
ElMessage.success(`添加 BOM 相关物料`)
})
if(addedCount > 0) {
ElMessage.success(`成功添加 BOM 相关物料,共 ${addedCount}`)
bomSelectVisible.value = false
} else {
ElMessage.warning('未在库存中找到匹配的物料')
} else {
ElMessage.warning('库存中找到该 BOM 所需的任何原料')
}
} catch(e) {
ElMessage.error('获取 BOM 详情失败')
}
}
// --- 通用逻辑 ---
const removeRow = (index: number) => {
selectedItems.value.splice(index, 1)
selectedItems.value.splice(index, 1)
}
const handlePreview = () => {
previewVisible.value = true
setTimeout(() => window.print(), 500)
if (validSelectedItems.value.length === 0) {
ElMessage.warning('请先添加物品并填写出库数量')
return
}
const now = new Date();
currentTime.value = `${now.getFullYear()}-${now.getMonth()+1}-${now.getDate()} ${now.getHours()}:${now.getMinutes()}`;
currentOrderNo.value = generateOrderNo();
previewVisible.value = true
}
const confirmPrint = async () => {
previewVisible.value = false;
// 记录日志
try {
const payload = validSelectedItems.value.map(item => ({
name: item.name, standard: item.standard, quantity: item.export_quantity
}));
// 不阻塞打印
printSelectionList(JSON.parse(JSON.stringify(payload))).catch(() => {});
} catch (e) {}
setTimeout(() => {
window.print();
}, 300);
}
const confirmExport = () => {
if (validSelectedItems.value.length === 0) return;
exportLoading.value = true;
try {
let csvContent = "\uFEFF";
csvContent += "类型,名称,规格型号,本次出库数量\n";
validSelectedItems.value.forEach(item => {
const safeName = (item.name || '').replace(/,/g, ' ');
const safeStd = (item.standard || '').replace(/,/g, ' ');
csvContent += `${item.typeLabel},${safeName},${safeStd},${item.export_quantity}\n`;
});
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
const link = document.createElement("a");
link.href = URL.createObjectURL(blob);
const timestamp = new Date().toISOString().slice(0,19).replace(/[-T:]/g, "");
link.download = `出库单_${timestamp}.csv`;
link.click();
ElMessage.success('导出成功');
previewVisible.value = false;
} catch (err) {
ElMessage.error('导出文件失败');
} finally {
exportLoading.value = false;
}
}
</script>
<style scoped>
/* ================= 屏幕显示样式 ================= */
.card-header { display: flex; justify-content: space-between; align-items: center; }
.header-left .title { font-size: 18px; font-weight: bold; margin-right: 10px; }
.header-left .subtitle { font-size: 12px; color: #909399; }
.filter-container { margin-bottom: 10px; }
</style>
.filter-container { margin-bottom: 20px; }
.app-container { height: 100vh; overflow: hidden; display: flex; flex-direction: column; padding: 20px; box-sizing: border-box; }
.app-container .el-card { flex: 1; display: flex; flex-direction: column; overflow: hidden; }
::v-deep(.el-card__body) { flex: 1; display: flex; flex-direction: column; overflow: hidden; }
/* ================= ★★★ 打印专用样式 ★★★ ================= */
/* 1. 默认状态:屏幕上隐藏打印区域 */
#print-area { display: none; }
/* 2. 打印状态:隐藏所有非打印内容,独显 #print-area */
@media print {
@page { margin: 0; size: auto; }
body * { visibility: hidden; }
.el-dialog__wrapper, .v-modal, .el-message, .no-print-content { display: none !important; }
#print-area, #print-area * { visibility: visible; }
#print-area {
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
margin: 0;
padding: 20mm;
background-color: white;
display: block !important;
z-index: 99999;
}
.print-header { text-align: center; margin-bottom: 20px; }
.print-header h1 { font-size: 24px; margin: 0 0 10px 0; font-weight: bold; color: #000; }
.print-meta-row { display: flex; justify-content: space-between; font-size: 12px; margin-bottom: 5px; }
.header-line { border-bottom: 2px solid #000; margin-top: 5px; }
.print-table {
width: 100%;
border-collapse: collapse;
margin-bottom: 40px;
border: 1px solid #000;
}
.print-table th, .print-table td {
border: 1px solid #000;
padding: 12px 8px;
text-align: left;
font-size: 14px;
color: #000;
}
.print-table th { text-align: center; font-weight: bold; }
.cell-padding { padding-left: 10px; }
.print-footer {
display: flex;
justify-content: space-between;
margin-top: 60px;
padding: 0 20px;
}
.signature-item {
display: flex;
flex-direction: column;
align-items: center;
width: 30%;
}
.sig-label { font-size: 14px; margin-bottom: 40px; text-align: left; width: 100%; }
.sig-line { border-bottom: 1px solid #000; width: 100%; height: 1px; display: block; }
}
</style>