新增回收置换记账及打印,完善订单标志和购料单价

This commit is contained in:
summer committed 2026-10-26 14:44:07 +08:00
1 parent 844a1fa860
commit 505110ab0e
26 files changed
+1201 -64

No files matched your search

+10 -2
View File
@@ -7,6 +7,7 @@ const {DatabaseSync,backup}=require('node:sqlite');
const TeamMembers=require('./team-members');
const {Ledger,ACTIVE_POCKETS,ACCOUNTS,today,fail,text,owner}=require('./ledger');
const {receipt,settings,defaults}=require('./printing');
const {recoveryCustomerPrintDocument,exchangeCustomerPrintDocument}=require('./recovery-printing');
const {FeishuSync}=require('./sync');
const sha=s=>crypto.createHash('sha256').update(s).digest('hex');
const derive=(p,s)=>crypto.scryptSync(p,s,64).toString('hex');
@@ -109,6 +110,9 @@ function createApp(options={}){
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)actorLedger.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=actorLedger.visibleRows('order',actorLedger.rows('SELECT id FROM orders ORDER BY created DESC')).map(r=>actorLedger.detail(r.id));return send(res,200,{today:today(),mode,user:actor,members:TeamMembers.MEMBERS,pockets:ACTIVE_POCKETS,accounts:ACCOUNTS,inventory:actorLedger.inventory(),orders:orders.map(({audit,payments,expenses,settlements,...o})=>o),priceLockCounts:actorLedger.priceLocks({status:'pending'}).counts,materialPoolSummary:actorLedger.materialPools().summary,materialPoolSettlement:actorLedger.materialPoolSettlement(),materialPoolAllocation:actorLedger.materialPoolAllocationStatus(),sync:actor.role==='owner'?sync.status():{enabled:sync.enabled},printing:settings(actorLedger.meta('printing',defaults)),backup:actor.role==='owner'?{last:lastBackup,error:backupError}:null,movements:actorLedger.inventoryMovements({},actor).movements});}
if(route==='/api/material-pools'&&!isPost)return send(res,200,actorLedger.materialPools(Object.fromEntries(url.searchParams)));
if(route==='/api/recoveries'&&!isPost)return send(res,200,actorLedger.recoveries(Object.fromEntries(url.searchParams),actor));
const recoveryMatch=route.match(/^\/api\/recoveries\/([a-f0-9-]{36})$/);
if(recoveryMatch&&!isPost)return send(res,200,actorLedger.recovery(recoveryMatch[1],actor));
const materialPurchaseMatch=route.match(/^\/api\/material-purchases\/([a-f0-9-]+)(?:\/(edit|delete))?$/);
if(materialPurchaseMatch&&!isPost){if(materialPurchaseMatch[2])fail('地址不存在',404);return send(res,200,actorLedger.materialPurchaseDetail(materialPurchaseMatch[1]));}
const materialPoolMatch=route.match(/^\/api\/material-pools\/(\d{4}-\d{2}-\d{2})(?:\/(settlement))?$/);
@@ -143,7 +147,9 @@ function createApp(options={}){
if(route==='/api/paper-number'&&!isPost)return send(res,200,{number:actorLedger.paperNumber(url.searchParams.get('date')||today())});
const match=route.match(/^\/api\/orders\/([a-f0-9-]+)(?:\/([a-z]+))?$/);
if(match&&!isPost){if(match[2]==='public')return send(res,200,actorLedger.publicOrder(match[1]));if(match[2]==='edit')return send(res,200,actorLedger.orderEdit(match[1],actor));if(match[2]==='change')return send(res,200,actorLedger.orderChange(match[1],actor));if(match[2]==='cancellation')return send(res,200,actorLedger.orderCancellation(match[1],actor));if(!match[2])return send(res,200,actorLedger.detail(match[1]));fail('地址不存在',404);}
if(route.startsWith('/print/')&&!isPost){const id=route.slice(7);return send(res,200,receipt(actorLedger.publicOrder(id),actorLedger.meta('printing',defaults),url.searchParams.get('mode')),'text/html; charset=utf-8');}
const recoveryPrintMatch=route.match(/^\/print\/recoveries\/([a-f0-9-]{36})$/);
if(recoveryPrintMatch&&!isPost){const dto=actorLedger.publicRecovery(recoveryPrintMatch[1],actor),print=dto.mode==='exchange'?exchangeCustomerPrintDocument:recoveryCustomerPrintDocument;return send(res,200,print(dto,actorLedger.meta('printing',defaults)),'text/html; charset=utf-8');}
if(route.startsWith('/print/')&&!isPost){const id=route.slice(7),order=actorLedger.detail(id);if(order.recovery_id){const dto=actorLedger.publicRecovery(order.recovery_id,actor);return send(res,200,exchangeCustomerPrintDocument(dto,actorLedger.meta('printing',defaults)),'text/html; charset=utf-8');}return send(res,200,receipt(actorLedger.publicOrder(id),actorLedger.meta('printing',defaults),url.searchParams.get('mode')),'text/html; charset=utf-8');}
if(route==='/api/report'&&!isPost){
return send(res,200,actorLedger.reportFor(Object.fromEntries(url.searchParams),actor));
}
@@ -153,7 +159,9 @@ function createApp(options={}){
if(route==='/api/sync'&&isPost){owner(actor);await read(req);await sync.tick();return send(res,200,sync.status());}
if(!isPost)fail('地址不存在',404);
const b=await read(req);const key=req.headers['idempotency-key'];
if(route==='/api/recoveries/quote')return send(res,200,actorLedger.recoveryQuote(b,actor));
const result=actorLedger.transact(key,actor,{route,body:b},()=>{
if(route==='/api/recoveries')return actorLedger.createRecovery(b,actor);
if(memberPasswordMatch){owner(actor);if(b.verified!==true)fail('请确认要为该成员设置新密码');const member=memberPasswordMatch[1];if(String(b.password||'').length<12)fail('新密码至少12个字符');const mapped=actorLedger.one('SELECT user_id FROM team_user_members WHERE member=?',member);if(!mapped)fail('成员账号不存在',404);const salt=crypto.randomBytes(24).toString('hex');actorLedger.run('UPDATE users SET salt=?,hash=? WHERE id=?',salt,derive(String(b.password),salt),mapped.user_id);actorLedger.run('UPDATE team_user_members SET password_set=1 WHERE user_id=?',mapped.user_id);actorLedger.run('DELETE FROM sessions WHERE user_id=?',mapped.user_id);actorLedger.audit('team-member:'+member,'设置成员密码',actor,{member});return {ok:true,member};}
if(['/api/team-funds/transfers','/api/team-funds/transfer'].includes(route))return actorLedger.transferTeamFunds(b,actor);
if(route==='/api/reimbursements')return actorLedger.createReimbursement(b,actor);
@@ -193,7 +201,7 @@ function createApp(options={}){
fail('地址不存在',404);
});return send(res,200,route==='/api/people-count'?peopleCountResponse(actorLedger,result,actor):result);
}
const assets={'/':'index.html','/app.js':'app.js','/order-copy.js':'order-copy.js','/order-templates.js':'order-templates.js','/order-templates-ui.js':'order-templates-ui.js','/order-edit-ui.js':'order-edit-ui.js','/order-cancellations-ui.js':'order-cancellations-ui.js','/order-changes-ui.js':'order-changes-ui.js','/daily-funds-ui.js':'daily-funds-ui.js','/team-funds-ui.js':'team-funds-ui.js','/cost-funding-ui.js':'cost-funding-ui.js','/price-locks-ui.js':'price-locks-ui.js','/material-pools-ui.js':'material-pools-ui.js','/inventory-changes-ui.js':'inventory-changes-ui.js','/credit-card-fees.js':'credit-card-fees.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','/order-copy.js':'order-copy.js','/order-templates.js':'order-templates.js','/order-templates-ui.js':'order-templates-ui.js','/order-edit-ui.js':'order-edit-ui.js','/order-cancellations-ui.js':'order-cancellations-ui.js','/order-changes-ui.js':'order-changes-ui.js','/daily-funds-ui.js':'daily-funds-ui.js','/team-funds-ui.js':'team-funds-ui.js','/cost-funding-ui.js':'cost-funding-ui.js','/price-locks-ui.js':'price-locks-ui.js','/material-pools-ui.js':'material-pools-ui.js','/recoveries-ui.js':'recoveries-ui.js','/inventory-changes-ui.js':'inventory-changes-ui.js','/credit-card-fees.js':'credit-card-fees.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();}
});