增加货盘新增改名与启停管理,保留历史库存关联

This commit is contained in:
summer committed 2026-11-08 03:30:58 +08:00
1 parent 337b2a709f
commit ba15795f0a
29 files changed
+748 -218

No files matched your search

+8 -6
View File
@@ -17,12 +17,13 @@ function weightSearch(q){
return {mg:Number.isSafeInteger(mg)&&mg>0?mg:null,unit:!!match[3]};
}
function matchesWeight(query,weights,total=weights.reduce((n,mg)=>n+mg,0)){return query?.mg>0&&(total===query.mg||weights.includes(query.mg));}
function lineType(line){
const source=line.pocket==='现货'?'货盘现货':line.pocket?'货盘·'+line.pocket:'配货';
function lineType(line,pocketLabel=key=>key){
const name=pocketLabel(line.pocket);
const source=line.pocket==='现货'&&name==='现货'?'货盘现货':line.pocket?'货盘·'+name:'配货';
return source+(Calc.isSilver(line.name)?' · 银饰':'');
}
function orderRow(order){
function orderRow(order,pocketLabel){
const draft=order.status==='draft',excluded=['returned','cancelled'].includes(order.status);
const materialPoolIds=[...new Set(order.lines.flatMap(line=>[line.costMaterial?.mode==='pool'?line.costMaterial.poolId:null,line.priceLockPool?.poolId].filter(Boolean)))];
const materialCostPending=!draft&&!excluded&&Boolean(order.materialCostPending||order.lines.some(line=>line.materialPool?.status==='pending'||line.materialPool?.amount===null||line.priceLockPool?.status==='pending'||line.priceLockPool?.amount===null));
@@ -78,7 +79,7 @@ function orderRow(order){
...(order.cancellationApproved?{cancellationApproved:order.cancellationApproved}:{}),
...(materialPoolIds.length?{materialPoolIds,materialCostPending}:{}),
customer:order.customer,salesperson:order.salesperson,status:order.status,note:order.note,
types:[...new Set(order.lines.map(lineType))],
types:[...new Set(order.lines.map(line=>lineType(line,pocketLabel)))],
mg:add(order.lines.filter(line=>line.unit==='g'),'quantity'),
summaryMg:add(order.lines.filter(line=>line.unit==='g'&&!Calc.isSilver(line.name)),'quantity'),
pieces:add(order.lines.filter(line=>line.unit==='件'),'quantity'),
@@ -204,6 +205,7 @@ function materialPoolActivity(ledger,from,date){
}
function orderBook(ledger,query,{today,validateDay,accounts,pockets}){
const pocketLabel=typeof ledger.pocketLabel==='function'?key=>ledger.pocketLabel(key):key=>key;
const date=validateDay(query.date??today),from=validateDay(query.from??date);
if(from>date)invalid('开始日期不能晚于结束日期');
const status=String(query.status??'all');if(!STATUSES.includes(status))invalid('订单状态无效');
@@ -223,14 +225,14 @@ function orderBook(ledger,query,{today,validateDay,accounts,pockets}){
else if(status!=='all'&&order.status!==status)return false;
// 一个商品行匹配就纳入整单,克重、金额和收款仍按整单汇总。
if(pocket&&!order.lines.some(line=>pocket==='external'?!line.pocket:line.pocket===pocket))return false;
return matchesWeight(weight,order.lines.filter(line=>line.unit==='g').map(line=>line.quantity))||matchesText([order.number,order.paper_number,order.customer,order.note,order.salesperson,...(order.recovery_id?['置换']:[]),...order.lines.flatMap(line=>[line.name,line.code,line.pocket])]);
return matchesWeight(weight,order.lines.filter(line=>line.unit==='g').map(line=>line.quantity))||matchesText([order.number,order.paper_number,order.customer,order.note,order.salesperson,...(order.recovery_id?['置换']:[]),...order.lines.flatMap(line=>[line.name,line.code,line.pocket,pocketLabel(line.pocket)])]);
});
const afterSales=!pocket&&['all','after_sale'].includes(status)?ledger.visibleRows('after_sale',ledger.rows("SELECT s.*,COALESCE(v.revision,1) AS revision,s.rowid AS position,(SELECT MIN(a.id) FROM audit a WHERE a.entity='after-sale:'||s.id AND a.action='登记售后') AS creation_sequence FROM after_sales s LEFT JOIN after_sale_revisions v ON v.after_sale_id=s.id WHERE s.date>=? AND s.date<=? ORDER BY s.date DESC,s.created DESC,s.id",from,date))
.filter(record=>matchesText([record.project,record.customer,record.actor,'售后'])):[];
const recoveries=!pocket&&['all','recovery'].includes(status)?ledger.visibleRows('recovery',ledger.rows("SELECT r.*,r.rowid AS position,(SELECT MIN(a.id) FROM audit a WHERE a.entity='recovery:'||r.id AND a.action='登记直接回收') AS creation_sequence FROM recoveries r WHERE r.mode='direct' AND r.status='active' AND r.date>=? AND r.date<=? ORDER BY r.created,r.rowid",from,date))
.filter(record=>{const lines=JSON.parse(record.old_lines);return matchesWeight(weight,lines.map(line=>line.foldedMg),JSON.parse(record.data).oldMg)||matchesText([record.number,record.customer,record.salesperson,record.note,'直接回收',...lines.map(line=>line.name)]);}):[];
// 同毫秒创建的记录按原创建审计编号排序;没有旧审计时以原表插入次序稳定回退。
const displayed=[...orders.map(order=>({row:orderRow(order),sequence:positions.get(order.id)})),
const displayed=[...orders.map(order=>({row:orderRow(order,pocketLabel),sequence:positions.get(order.id)})),
...afterSales.map(record=>({row:afterSaleRow(record),sequence:record.creation_sequence??record.position}))];
displayed.sort(compareDisplay);
const rows=displayed.map(record=>record.row);