新增锁价与每日购料管理

支持锁价交付、每日加权成本、购料修改与软删除,完善付款归属、结算及利润重算。

244项测试和隔离浏览器验收通过;记录尚未实施的剩料跨日结转与当天资金结清需求。
This commit is contained in:
summer committed 2026-10-21 01:32:49 +08:00
1 parent 42d75a4968
commit 20bf6e7d24
23 files changed
+2330 -101

No files matched your search

+16 -2
View File
@@ -68,7 +68,15 @@ function createApp(options={}){
if(route.startsWith('/api/')||route.startsWith('/print/')){
if(!actor)fail('请先登录',401);
if(route==='/api/logout'&&isPost){const token=req.headers.cookie?.split(';').map(x=>x.trim()).find(x=>x.startsWith('mm_session='))?.slice(11);if(token)ledger.run('DELETE FROM sessions WHERE hash=?',sha(token));res.setHeader('Set-Cookie','mm_session=; HttpOnly; SameSite=Strict; Path=/; Max-Age=0');return send(res,200,{ok:true});}
if(route==='/api/state'&&!isPost){const orders=ledger.rows('SELECT id FROM orders ORDER BY created DESC').map(r=>ledger.detail(r.id));return send(res,200,{today:today(),mode,user:actor,pockets:ACTIVE_POCKETS,accounts:ACCOUNTS,inventory:ledger.inventory(),orders:orders.map(({audit,payments,expenses,settlements,...o})=>o),sync:sync.status(),printing:ledger.meta('printing',defaults),backup:{last:lastBackup,error:backupError},movements:ledger.rows('SELECT * FROM movements ORDER BY created DESC LIMIT 100')});}
if(route==='/api/state'&&!isPost){const orders=ledger.rows('SELECT id FROM orders ORDER BY created DESC').map(r=>ledger.detail(r.id));return send(res,200,{today:today(),mode,user:actor,pockets:ACTIVE_POCKETS,accounts:ACCOUNTS,inventory:ledger.inventory(),orders:orders.map(({audit,payments,expenses,settlements,...o})=>o),priceLockCounts:ledger.priceLocks({status:'pending'}).counts,materialPoolSummary:ledger.materialPools().summary,materialPoolSettlement:ledger.materialPoolSettlement(),sync:sync.status(),printing:ledger.meta('printing',defaults),backup:{last:lastBackup,error:backupError},movements:ledger.rows('SELECT * FROM movements ORDER BY created DESC LIMIT 100')});}
if(route==='/api/material-pools'&&!isPost)return send(res,200,ledger.materialPools(Object.fromEntries(url.searchParams)));
const materialPurchaseMatch=route.match(/^\/api\/material-purchases\/([a-f0-9-]+)(?:\/(edit|delete))?$/);
if(materialPurchaseMatch&&!isPost){if(materialPurchaseMatch[2])fail('地址不存在',404);return send(res,200,ledger.materialPurchaseDetail(materialPurchaseMatch[1]));}
const materialPoolMatch=route.match(/^\/api\/material-pools\/(\d{4}-\d{2}-\d{2})(?:\/(settlement))?$/);
if(materialPoolMatch&&!isPost){if(materialPoolMatch[2])fail('地址不存在',404);return send(res,200,ledger.materialPoolDetail(materialPoolMatch[1]));}
if(route==='/api/price-locks'&&!isPost)return send(res,200,ledger.priceLocks(Object.fromEntries(url.searchParams)));
const lockMatch=route.match(/^\/api\/price-locks\/([a-f0-9-]+)(?:\/(deliver))?$/);
if(lockMatch&&!isPost){if(lockMatch[2])fail('地址不存在',404);return send(res,200,ledger.priceLockDetail(lockMatch[1]));}
if(route==='/api/inventory'&&!isPost)return send(res,200,ledger.inventoryStatus(actor));
if(route==='/api/inventory/snapshots'&&!isPost)return send(res,200,ledger.inventorySnapshots(Object.fromEntries(url.searchParams)));
if(route==='/api/order-book'&&!isPost)return send(res,200,ledger.orderBook(Object.fromEntries(url.searchParams)));
@@ -91,6 +99,12 @@ function createApp(options={}){
const result=ledger.transact(key,actor,{route,body:b},()=>{
if(route==='/api/orders/issue')return ledger.issueOrder(b,actor);
if(route==='/api/orders')return ledger.saveOrder(b,actor);
if(route==='/api/material-purchases')return ledger.createMaterialPurchase(b,actor);
if(materialPurchaseMatch&&materialPurchaseMatch[2]==='edit')return ledger.editMaterialPurchase(materialPurchaseMatch[1],b,actor);
if(materialPurchaseMatch&&materialPurchaseMatch[2]==='delete')return ledger.deleteMaterialPurchase(materialPurchaseMatch[1],b,actor);
if(materialPoolMatch&&materialPoolMatch[2]==='settlement')return ledger.settleMaterialPool(materialPoolMatch[1],b,actor);
if(route==='/api/price-locks')return ledger.createPriceLock(b,actor);
if(lockMatch&&lockMatch[2]==='deliver')return ledger.deliverPriceLock(lockMatch[1],b,actor);
if(route==='/api/after-sales')return ledger.saveAfterSale(b,actor);
if(route==='/api/people-count')return ledger.savePeopleCount(b,actor);
if(match&&match[2])return ledger.action(match[1],match[2],b,actor);
@@ -102,7 +116,7 @@ function createApp(options={}){
fail('地址不存在',404);
});return send(res,200,result);
}
const assets={'/':'index.html','/app.js':'app.js','/calculations.js':'calculations.js','/fee-estimates.js':'fee-estimates.js','/app.css':'app.css','/print.js':'print.js'};const file=assets[route];if(!file||isPost)fail('地址不存在',404);
const assets={'/':'index.html','/app.js':'app.js','/price-locks-ui.js':'price-locks-ui.js','/material-pools-ui.js':'material-pools-ui.js','/calculations.js':'calculations.js','/fee-estimates.js':'fee-estimates.js','/app.css':'app.css','/print.js':'print.js'};const file=assets[route];if(!file||isPost)fail('地址不存在',404);
send(res,200,fs.readFileSync(path.join(__dirname,'public',file)),file.endsWith('.js')?'application/javascript; charset=utf-8':file.endsWith('.css')?'text/css; charset=utf-8':'text/html; charset=utf-8');
}catch(e){if(!res.headersSent)send(res,e.status||400,{error:e.status||e.message?.startsWith('打印')?e.message:(e.code?.startsWith('SQLITE')?'保存失败,请保留操作内容并联系维护者':e.message||'操作失败')});else res.end();}
});