Merge remote-tracking branch 'origin/project' into project

This commit is contained in:
zhanghao 2026-11-25 14:19:21 +08:00
commit 8f3ffaa1a9
3 changed files with 77 additions and 4 deletions

View File

@ -1,11 +1,11 @@
<template> <template>
<div :class="['sheet-cell', {'cell-numeric':['quantity','actualUnitPrice','finalUnitPrice'].includes(column.field),'cell-invalid':owner.cellError(row,column.field),'cell-editable':owner.editableCell(row,column.field),'cell-active':active&&owner.editableCell(row,column.field),'cell-selected':selected}]" :style="owner.livePeer?.(row,column.field)?{boxShadow:'inset 0 0 0 2px '+owner.liveColor(owner.livePeer(row,column.field))}:null" tabindex="0" @mousedown="owner.startCellSelection?.(row,column.field,$event)" @mouseenter="owner.extendCellSelection?.(row,column.field,$event)" @focus="owner.focusGridCell?.(row,column.field)" @dblclick="owner.activateCell(row,column.field,true,$event)" :data-row="index" :data-field="column.field" :aria-label="'第'+(index+1)+'行 '+column.label" :title="owner.liveCellHint?.(row,column.field) || owner.cellError(row,column.field) || (owner.isEditing&&owner.fieldLocked(row,column.field)?column.field==='finalUnitPrice'?'已有到货,请退出编辑后选择该行,使用更正价格同步关联金额':'已有到货或入库,请先按流程退回后修改':String(owner.displayCell(row,column.field) ?? ''))" @focusin="active&&owner.rememberCell(index,column.field)" @focusout="commitDraft" @copy="owner.copyGridEvent?.($event)" @paste="owner.paste($event,index,column.field)" @keydown.esc.capture.stop="owner.navigate($event,index,column.field)" @keydown="owner.navigate($event,index,column.field)"> <div :class="['sheet-cell', {'cell-numeric':['quantity','actualUnitPrice','finalUnitPrice'].includes(column.field),'cell-invalid':owner.cellError(row,column.field),'cell-editable':owner.editableCell(row,column.field),'cell-active':active&&owner.editableCell(row,column.field),'cell-selected':selected,'cell-price-warning':!!priceWarning}]" :style="owner.livePeer?.(row,column.field)?{boxShadow:'inset 0 0 0 2px '+owner.liveColor(owner.livePeer(row,column.field))}:null" tabindex="0" @mousedown="owner.startCellSelection?.(row,column.field,$event)" @mouseenter="owner.extendCellSelection?.(row,column.field,$event)" @focus="owner.focusGridCell?.(row,column.field)" @dblclick="owner.activateCell(row,column.field,true,$event)" :data-row="index" :data-field="column.field" :aria-label="'第'+(index+1)+'行 '+column.label" :title="cellTitle" @focusin="active&&owner.rememberCell(index,column.field)" @focusout="commitDraft" @copy="owner.copyGridEvent?.($event)" @paste="owner.paste($event,index,column.field)" @keydown.esc.capture.stop="owner.navigate($event,index,column.field)" @keydown="owner.navigate($event,index,column.field)">
<span v-if="owner.livePeer?.(row,column.field)" class="live-cell-label" :style="{background:owner.liveColor(owner.livePeer(row,column.field))}">{{owner.livePeer(row,column.field).name}} · 编辑中</span> <span v-if="owner.livePeer?.(row,column.field)" class="live-cell-label" :style="{background:owner.liveColor(owner.livePeer(row,column.field))}">{{owner.livePeer(row,column.field).name}} · 编辑中</span>
<input v-if="!active && typing" class="sheet-keyboard-input" :aria-label="'输入第'+(index+1)+'行 '+column.label" autocomplete="off" @compositionstart="composing=true" @compositionend="compositionEnd" @input="typeInput" /> <input v-if="!active && typing" class="sheet-keyboard-input" :aria-label="'输入第'+(index+1)+'行 '+column.label" autocomplete="off" @compositionstart="composing=true" @compositionend="compositionEnd" @input="typeInput" />
<template v-if="!owner.editableCell(row,column.field) || !active"> <template v-if="!owner.editableCell(row,column.field) || !active">
<a v-if="owner.readonly(row) && column.field==='purchaseLink' && owner.linkUrl(row.purchaseLink)" class="sheet-link" :href="owner.linkUrl(row.purchaseLink)" :title="row.purchaseLink" target="_blank" rel="noopener noreferrer" @click.stop>{{ purchaseLinkLabel(row.purchaseLink) }}</a> <a v-if="owner.readonly(row) && column.field==='purchaseLink' && owner.linkUrl(row.purchaseLink)" class="sheet-link" :href="owner.linkUrl(row.purchaseLink)" :title="row.purchaseLink" target="_blank" rel="noopener noreferrer" @click.stop>{{ purchaseLinkLabel(row.purchaseLink) }}</a>
<span v-else-if="column.field==='category'&&pendingCategory" class="sheet-pending-category"><span class="sheet-value">{{ row.category }}</span><el-tag type="warning" size="small">待确认</el-tag></span> <span v-else-if="column.field==='category'&&pendingCategory" class="sheet-pending-category"><span class="sheet-value">{{ row.category }}</span><el-tag type="warning" size="small">待确认</el-tag></span>
<span v-else class="sheet-value" :title="String(owner.displayCell(row,column.field))">{{ column.field==='purchaseLink' && row.purchaseLink ? purchaseLinkLabel(row.purchaseLink) : owner.displayCell(row,column.field) }}</span> <span v-else class="sheet-value" :title="priceWarning || String(owner.displayCell(row,column.field))">{{ column.field==='purchaseLink' && row.purchaseLink ? purchaseLinkLabel(row.purchaseLink) : owner.displayCell(row,column.field) }}</span>
</template> </template>
<el-select v-else-if="column.field==='projectId'" v-model="row.projectId" filterable :disabled="owner.saving || owner.fieldLocked(row,column.field) || !!(row.id && row.productionTaskId)" @visible-change="owner.menu(row,column.field,$event)" @change="owner.projectChanged(row)"><el-option v-for="p in owner.menuOpen(row,column.field)?owner.projects:owner.projects.filter(p=>String(p.id)===String(row.projectId))" :key="p.id" :value="p.id" :label="p.projectName" /></el-select> <el-select v-else-if="column.field==='projectId'" v-model="row.projectId" filterable :disabled="owner.saving || owner.fieldLocked(row,column.field) || !!(row.id && row.productionTaskId)" @visible-change="owner.menu(row,column.field,$event)" @change="owner.projectChanged(row)"><el-option v-for="p in owner.menuOpen(row,column.field)?owner.projects:owner.projects.filter(p=>String(p.id)===String(row.projectId))" :key="p.id" :value="p.id" :label="p.projectName" /></el-select>
<el-select v-else-if="column.field==='expenseCategoryId'" v-model="row.expenseCategoryId" filterable :disabled="owner.saving || owner.fieldLocked(row,column.field)" @visible-change="owner.menu(row,column.field,$event)"><el-option v-for="c in owner.menuOpen(row,column.field)?owner.categories:owner.categories.filter(c=>String(c.id)===String(row.expenseCategoryId))" :key="c.id" :value="c.id" :label="c.categoryName" /></el-select> <el-select v-else-if="column.field==='expenseCategoryId'" v-model="row.expenseCategoryId" filterable :disabled="owner.saving || owner.fieldLocked(row,column.field)" @visible-change="owner.menu(row,column.field,$event)"><el-option v-for="c in owner.menuOpen(row,column.field)?owner.categories:owner.categories.filter(c=>String(c.id)===String(row.expenseCategoryId))" :key="c.id" :value="c.id" :label="c.categoryName" /></el-select>
@ -16,7 +16,7 @@
<el-select v-else-if="column.field==='unit'" v-model="row.unit" filterable :disabled="owner.saving || owner.fieldLocked(row,column.field)" @visible-change="owner.menu(row,column.field,$event)"><el-option v-for="unit in owner.menuOpen(row,column.field)?owner.rowUnits(row):(row.unit?[row.unit]:[])" :key="unit" :value="unit" :label="unit" /></el-select> <el-select v-else-if="column.field==='unit'" v-model="row.unit" filterable :disabled="owner.saving || owner.fieldLocked(row,column.field)" @visible-change="owner.menu(row,column.field,$event)"><el-option v-for="unit in owner.menuOpen(row,column.field)?owner.rowUnits(row):(row.unit?[row.unit]:[])" :key="unit" :value="unit" :label="unit" /></el-select>
<el-input-number v-else-if="column.field==='quantity'" v-model="row.quantity" :precision="4" :min="Math.max(0.0001,Number(row.acceptedQuantity||0)+Number(row.pendingArrivalQuantity||0))" :controls="false" :disabled="owner.saving || owner.fieldLocked(row,column.field)" /> <el-input-number v-else-if="column.field==='quantity'" v-model="row.quantity" :precision="4" :min="Math.max(0.0001,Number(row.acceptedQuantity||0)+Number(row.pendingArrivalQuantity||0))" :controls="false" :disabled="owner.saving || owner.fieldLocked(row,column.field)" />
<el-input v-else-if="column.field==='finalUnitPrice'" v-model="row.finalUnitPrice" placeholder="未报价" :disabled="owner.saving || owner.fieldLocked(row,column.field)" /> <el-input v-else-if="column.field==='finalUnitPrice'" v-model="row.finalUnitPrice" placeholder="未报价" :disabled="owner.saving || owner.fieldLocked(row,column.field)" />
<el-input v-else-if="column.field==='actualUnitPrice'" v-model="row.actualUnitPrice" maxlength="21" placeholder="内部参考,选填" :disabled="owner.saving" /> <el-input v-else-if="column.field==='actualUnitPrice'" v-model="row.actualUnitPrice" :title="priceWarning || undefined" maxlength="21" placeholder="内部参考,选填" :disabled="owner.saving" />
<el-select v-else-if="column.field==='productionTaskId'" v-model="row.productionTaskId" filterable remote :remote-method="q=>owner.searchPlans(row,q)" clearable :disabled="owner.saving || !!row.id" placeholder="自研可不选" @visible-change="owner.menu(row,column.field,$event)"><el-option v-for="p in owner.menuOpen(row,column.field)?owner.rowPlans(row):owner.plans.filter(p=>String(p.id)===String(row.productionTaskId))" :key="p.id" :value="p.id" :label="[p.taskNo,p.productName].filter(Boolean).join(' / ')" /></el-select> <el-select v-else-if="column.field==='productionTaskId'" v-model="row.productionTaskId" filterable remote :remote-method="q=>owner.searchPlans(row,q)" clearable :disabled="owner.saving || !!row.id" placeholder="自研可不选" @visible-change="owner.menu(row,column.field,$event)"><el-option v-for="p in owner.menuOpen(row,column.field)?owner.rowPlans(row):owner.plans.filter(p=>String(p.id)===String(row.productionTaskId))" :key="p.id" :value="p.id" :label="[p.taskNo,p.productName].filter(Boolean).join(' / ')" /></el-select>
<span v-else-if="column.field==='category' && !owner.canEditCategory(row)" class="sheet-value">{{ row.category }}</span> <span v-else-if="column.field==='category' && !owner.canEditCategory(row)" class="sheet-value">{{ row.category }}</span>
<item-category-select v-else-if="column.field==='category'" v-model="row.category" :item-name="row.materialName" :item-spec="row.materialParams" :item-brand="row.materialBrand" :disabled="owner.saving || owner.fieldLocked(row,column.field)" /> <item-category-select v-else-if="column.field==='category'" v-model="row.category" :item-name="row.materialName" :item-spec="row.materialParams" :item-brand="row.materialBrand" :disabled="owner.saving || owner.fieldLocked(row,column.field)" />
@ -26,9 +26,20 @@
</div> </div>
</template> </template>
<script setup> <script setup>
import { ref, watch } from 'vue' import { computed, ref, watch } from 'vue'
import { purchasePriceDiffers } from '@/utils/purchase-price-warning'
import { purchaseLinkLabel } from '@/utils/purchase-link-label' import { purchaseLinkLabel } from '@/utils/purchase-link-label'
const props=defineProps({row:Object,column:Object,index:Number,owner:Object,active:Boolean,selected:Boolean,typing:Boolean,pendingCategory:Boolean}) const props=defineProps({row:Object,column:Object,index:Number,owner:Object,active:Boolean,selected:Boolean,typing:Boolean,pendingCategory:Boolean})
const priceWarning=computed(()=>{
if(props.column.field!=='actualUnitPrice'||!props.owner.canViewQuotes||props.owner.compactAmounts)return ''
const value=field=>props.owner.displayCell(props.row,field)
return purchasePriceDiffers(value('actualUnitPrice'),value('finalUnitPrice'),value('quantity'))?'可能金额有问题:实际与报价的单价或金额相差达到5倍,请核对。':''
})
const cellTitle=computed(()=>{
const {owner,row,column}=props,field=column.field
const detail=owner.liveCellHint?.(row,field)||owner.cellError(row,field)||(owner.isEditing&&owner.fieldLocked(row,field)?field==='finalUnitPrice'?'已有到货,请退出编辑后选择该行,使用更正价格同步关联金额':'已有到货或入库,请先按流程退回后修改':String(owner.displayCell(row,field)??''))
return [priceWarning.value,detail].filter(Boolean).join('\n')
})
const composing=ref(false) const composing=ref(false)
const specDraft=ref(props.row?.materialParams || '') const specDraft=ref(props.row?.materialParams || '')
const nameDraft=ref(props.row?.materialName || ''),brandDraft=ref(props.row?.materialBrand || '') const nameDraft=ref(props.row?.materialName || ''),brandDraft=ref(props.row?.materialBrand || '')
@ -70,6 +81,8 @@ watch(()=>props.active,active=>{if(active&&props.column.field==='materialName')p
.live-cell-label{position:absolute;right:0;top:-9px;max-width:110px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;color:#fff;font-size:10px;line-height:13px;padding:0 3px;border-radius:3px 3px 0 0;pointer-events:none;z-index:2}.sheet-cell:has(.live-cell-label){position:relative;overflow:visible} .live-cell-label{position:absolute;right:0;top:-9px;max-width:110px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;color:#fff;font-size:10px;line-height:13px;padding:0 3px;border-radius:3px 3px 0 0;pointer-events:none;z-index:2}.sheet-cell:has(.live-cell-label){position:relative;overflow:visible}
.sheet-pending-category{display:flex;align-items:center;justify-content:center;gap:5px;width:100%;min-width:0}.sheet-pending-category>.sheet-value{flex:0 1 auto;min-width:0}.sheet-pending-category>.el-tag{flex:none;font-size:11px;padding:0 3px} .sheet-pending-category{display:flex;align-items:center;justify-content:center;gap:5px;width:100%;min-width:0}.sheet-pending-category>.sheet-value{flex:0 1 auto;min-width:0}.sheet-pending-category>.el-tag{flex:none;font-size:11px;padding:0 3px}
.sheet-cell.cell-price-warning:not(.cell-invalid){background:var(--el-color-warning-light-9,#fdf6ec)!important;color:var(--el-color-warning-dark-2,#b88230);border-radius:3px}
.sheet-cell.cell-price-warning:not(.cell-invalid) :deep(.el-input__wrapper){background:var(--el-color-warning-light-9,#fdf6ec)}
.cell-numeric{font-variant-numeric:tabular-nums} .cell-numeric{font-variant-numeric:tabular-nums}
.sheet-keyboard-input{position:absolute;width:1px;height:1px;opacity:0;pointer-events:none;padding:0;border:0} .sheet-keyboard-input{position:absolute;width:1px;height:1px;opacity:0;pointer-events:none;padding:0;border:0}
</style> </style>

View File

@ -0,0 +1,14 @@
import { purchaseActualAmount } from './purchase-reference-amounts'
const amountFormatter=new Intl.NumberFormat('en-US',{useGrouping:false,minimumFractionDigits:2,maximumFractionDigits:2})
const differs=(a,b)=>Number.isFinite(a)&&Number.isFinite(b)&&a>0&&b>0&&Math.max(a,b)/Math.min(a,b)>=5-Number.EPSILON*5
// Display-only comparison; use the same two-decimal amounts as the detail grid.
export function purchasePriceDiffers(actualPrice,quotePrice,quantity){
const actual=Number(actualPrice),quote=Number(quotePrice)
if(!Number.isFinite(actual)||!Number.isFinite(quote)||actual<=0||quote<=0)return false
if(differs(actual,quote))return true
const actualAmount=purchaseActualAmount({actualUnitPrice:actualPrice,quantity})
const quoteAmount=Number(amountFormatter.format(quote*Number(quantity)))
return differs(actualAmount,quoteAmount)
}

View File

@ -0,0 +1,46 @@
import {afterEach,expect,it} from 'vitest'
import {mount} from '@vue/test-utils'
import ElementPlus from 'element-plus'
import Cell from '@/components/PurchaseSheet/Cell.vue'
import {purchasePriceDiffers} from '@/utils/purchase-price-warning'
const wrappers=[]
afterEach(()=>wrappers.splice(0).forEach(w=>w.unmount()))
const owner={canViewQuotes:true,compactAmounts:false,cellError:()=>'',editableCell:()=>false,readonly:()=>false,fieldLocked:()=>false,displayCell:(r,f)=>r[f],isEditing:false,saving:false}
function render(row,overrides={},field='actualUnitPrice'){
const w=mount(Cell,{props:{row,column:{field,label:field},index:0,owner:{...owner,...overrides}},global:{plugins:[ElementPlus]}})
wrappers.push(w);return w
}
it('compares both directions at the fivefold boundary and ignores missing, zero and invalid prices',()=>{
for(const [a,b] of [[5,1],[1,5],['0.1','0.02'],['0.02','0.1'],[50,1]])expect(purchasePriceDiffers(a,b,1000)).toBe(true)
for(const [a,b] of [[4.999,1],[1,4.999],[1,1],[0,10],[10,0],[null,10],[10,''],[undefined,10],['abc',10],[-10,1],[Infinity,1]])expect(purchasePriceDiffers(a,b,1000)).toBe(false)
// The grid rounds line amounts to cents; their ratio can reach five before the unit prices do.
expect(purchasePriceDiffers('0.049','0.01',1)).toBe(true)
expect(purchasePriceDiffers('0.01','0.049',1)).toBe(true)
expect(purchasePriceDiffers('0.014','0.003',1)).toBe(false)
})
it('highlights only the actual unit-price cell, with a warning on the value and without changing row data',()=>{
const row=Object.freeze({actualUnitPrice:100,finalUnitPrice:10,quantity:2,totalAmount:20}),before=JSON.stringify(row)
const w=render(row)
expect(w.classes()).toContain('cell-price-warning');expect(w.attributes('title')).toContain('可能金额有问题')
expect(w.get('.sheet-value').attributes('title')).toContain('可能金额有问题')
expect(w.get('.sheet-value').text()).toBe('100');expect(JSON.stringify(row)).toBe(before)
for(const field of ['finalUnitPrice','quantity'])expect(render(row,{},field).classes()).not.toContain('cell-price-warning')
})
it('updates immediately with prices, amount display toggle and quotation permissions',async()=>{
const row={actualUnitPrice:100,finalUnitPrice:10,quantity:1},w=render(row)
await w.setProps({row:{...row,actualUnitPrice:40}});expect(w.classes()).not.toContain('cell-price-warning')
await w.setProps({row});expect(w.classes()).toContain('cell-price-warning')
await w.setProps({owner:{...owner,compactAmounts:true}});expect(w.classes()).not.toContain('cell-price-warning');expect(w.attributes('title')).not.toContain('可能金额有问题')
const hiddenQuote={...owner,canViewQuotes:false,displayCell:(r,f)=>{if(f==='finalUnitPrice')throw new Error('must not inspect hidden quotes');return r[f]}}
await w.setProps({owner:hiddenQuote});expect(w.classes()).not.toContain('cell-price-warning')
await w.setProps({owner});expect(w.classes()).toContain('cell-price-warning')
})
it('keeps the warning in the active editor and preserves collaboration and validation messages',async()=>{
const row={actualUnitPrice:100,finalUnitPrice:10,quantity:1},w=render(row,{editableCell:()=>true,isEditing:true,rememberCell:()=>{},liveCellHint:()=> '同事正在编辑'})
expect(w.attributes('title')).toContain('同事正在编辑')
await w.setProps({active:true});expect(w.find('input').exists()).toBe(true)
expect(w.get('input').attributes('title')).toContain('可能金额有问题')
await w.setProps({owner:{...owner,cellError:()=> '输入格式有误'}})
expect(w.classes()).toContain('cell-invalid');expect(w.attributes('title')).toContain('输入格式有误')
})