refactor: unify variance calculation and implement backend Excel export with borrowed assets sheet
This commit is contained in:
@ -356,7 +356,6 @@ import { ElMessage, ElMessageBox } from 'element-plus'
|
||||
import { Search, VideoPlay, VideoPause, List, Checked, Download, ArrowRight, Cloudy, Edit, EditPen, CameraFilled, Close } from '@element-plus/icons-vue'
|
||||
import request from '@/utils/request'
|
||||
import { useUserStore } from '@/stores/user'
|
||||
import * as XLSX from 'xlsx'
|
||||
|
||||
const userStore = useUserStore()
|
||||
const currentUser = userStore.username || 'admin'
|
||||
@ -748,75 +747,12 @@ const closeOverlays = () => {
|
||||
showQtyDialog.value = false
|
||||
}
|
||||
|
||||
// --- 导出 Excel 逻辑 ---
|
||||
// --- 导出 Excel 逻辑 (调用后端API) ---
|
||||
const exportToExcel = () => {
|
||||
try {
|
||||
// 1. 已盘点 Sheet
|
||||
const scannedData = allData.value.filter(i => i.scanned).map(item => {
|
||||
const key = item.source_table && item.stock_id ? `${item.source_table}_${item.stock_id}` : ''
|
||||
const borrowedQty = borrowedQuantities.value[key] || 0
|
||||
const actualTotal = item.qty_actual + borrowedQty
|
||||
const diff = actualTotal - item.qty_stock
|
||||
const result = diff === 0 ? '正常' : diff < 0 ? '盘亏/差异' : '盘盈'
|
||||
return {
|
||||
'物品名称': item.name,
|
||||
'类型': item.type || item.material_type || '-',
|
||||
'类别': item.category || '-',
|
||||
'规格型号': item.spec_model || item.standard || '-', // ★ 双重保险
|
||||
'SKU': item.sku,
|
||||
'批次/SN': item.serial_number || item.batch_no || '-',
|
||||
'单位': item.unit || '个',
|
||||
'单价': item.price || item.unit_price || 0,
|
||||
'账面库存': parseFloat(item.qty_stock as any),
|
||||
'实盘数量': item.qty_actual,
|
||||
'借出未还数量': borrowedQty,
|
||||
'盘点结果': result,
|
||||
'差异数': diff
|
||||
}
|
||||
})
|
||||
|
||||
// 2. 未盘点 Sheet
|
||||
const missingData = allData.value.filter(i => !i.scanned).map(item => {
|
||||
const key = item.source_table && item.stock_id ? `${item.source_table}_${item.stock_id}` : ''
|
||||
const borrowedQty = borrowedQuantities.value[key] || 0
|
||||
return {
|
||||
'物品名称': item.name,
|
||||
'类型': item.type || item.material_type || '-',
|
||||
'类别': item.category || '-',
|
||||
'规格型号': item.spec_model || item.standard || '-', // ★ 双重保险
|
||||
'SKU': item.sku,
|
||||
'批次/SN': item.serial_number || item.batch_no || '-',
|
||||
'单位': item.unit || '个',
|
||||
'单价': item.price || item.unit_price || 0,
|
||||
'账面库存': parseFloat(item.qty_stock as any),
|
||||
'借出未还数量': borrowedQty,
|
||||
'状态': '未盘点'
|
||||
}
|
||||
})
|
||||
|
||||
const wb = XLSX.utils.book_new()
|
||||
const ws1 = XLSX.utils.json_to_sheet(scannedData)
|
||||
const ws2 = XLSX.utils.json_to_sheet(missingData)
|
||||
|
||||
const wscols = [
|
||||
{wch: 20}, {wch: 10}, {wch: 10}, {wch: 15},
|
||||
{wch: 15}, {wch: 15}, {wch: 5}, {wch: 8},
|
||||
{wch: 8}, {wch: 8}, {wch: 8}, {wch: 8}, {wch: 8}
|
||||
]
|
||||
ws1['!cols'] = wscols
|
||||
ws2['!cols'] = wscols
|
||||
|
||||
XLSX.utils.book_append_sheet(wb, ws1, "已盘点明细")
|
||||
XLSX.utils.book_append_sheet(wb, ws2, "未盘点明细")
|
||||
|
||||
const fileName = `库存盘点报告_${new Date().toISOString().slice(0,10)}.xlsx`
|
||||
XLSX.writeFile(wb, fileName)
|
||||
|
||||
ElMessage.success('Excel 报表已生成')
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
ElMessage.error('导出失败,请检查 xlsx 插件是否安装')
|
||||
}
|
||||
// 调用后端API下载盘点报告
|
||||
const baseUrl = import.meta.env.VITE_APP_BASE_API || ''
|
||||
window.open(`${baseUrl}/v1/inbound/stock/export-stocktake`, '_blank')
|
||||
ElMessage.success('正在下载盘点报告...')
|
||||
}
|
||||
|
||||
const filteredList = computed(() => {
|
||||
|
||||
Reference in New Issue
Block a user