2026-10-26 14:44:07 +08:00
'use strict' ;
const { test } = require ( 'node:test' ), assert = require ( 'node:assert/strict' ),{ randomUUID } = require ( 'node:crypto' );
const fs = require ( 'node:fs' ), os = require ( 'node:os' ), path = require ( 'node:path' );
const { Ledger } = require ( '../ledger' ),{ createUser , createApp } = require ( '../server' ), Calc = require ( '../public/calculations' ),{ FeishuSync } = require ( '../sync' );
const D = '2026-10-20' , D2 = '2026-10-21' ;
function fixture ( t ){
const dir = fs . mkdtempSync ( path . join ( os . tmpdir (), 'ledger-recovery-' )), l = new Ledger ( path . join ( dir , 'synthetic.sqlite' ));
t . after (()=>{ l . close (); fs . rmSync ( dir ,{ recursive : true , force : true });});
const actors = {}; for ( const member of [ 'owner' , 'assistant' , 'assistant2' ]){ const role = member === 'owner' ? 'owner' : 'assistant' , name = '合成' + member ; actors [ member ] = { id : createUser ( l , name , role , 'synthetic-recovery-password' , member ), name , role , member };}
l . enableTeamMode ();
const run = ( payload , fn , member = 'owner' , key = randomUUID ())=> l . scope ( actors [ member ],()=> l . transact ( key , actors [ member ], payload , fn ));
const create = ( input , member = 'assistant' , key = randomUUID ())=> run ( input ,()=> l . createRecovery ( input , actors [ member ]), member , key );
const stock = ( quantity = '100' )=> run ({ quantity },()=> l . stock ({ date : D , kind : 'opening' , pocket : '现货' , grams : quantity , note : '合成库存' }, actors . owner ));
const report = ( date = D , member = 'owner' )=> l . scope ( actors [ member ],()=> l . reportByOrderDate ( date , date , actors [ member ]));
const funds = ( member = 'owner' )=> l . scope ( actors [ member ],()=> l . teamFunds ({ from : D , to : D2 }, actors [ member ]));
return { l , actors , run , create , stock , report , funds };
}
const old = ( quantity = '10' , purity = '100' )=>[{ name : 'Au旧料' , incoming_weight : quantity , melted_weight : quantity , purity }];
const direct = ( extra = {})=>({ mode : 'direct' , date : D , customer : '仅合成回收客户' , salesperson : '合成开单人' , old_lines : old (), buy_rate : '900' , payer : 'owner' , payment_account : '麦麦微信' , verified : true ,... extra });
const exchange = ( extra = {}, newExtra = {})=> direct ({ mode : 'exchange' , old_lines : old ( '20' ), exchange : { name : '足金合成新货' , quantity : '30' , pocket : '现货' , gold_price : '1000' , labor_rate : '20' , labor_mode : 'gram' , depreciation_rate : '10' ,
cost_material : { mode : 'gram' , rate : '900' }, cost_labor : { mode : 'gram' , rate : '5' }, cost_payer : 'owner' , discount : '0' , rounding_mode : 'cent' ,... newExtra },... extra });
function content ( l ){ return JSON . stringify ( l . rows ( "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name" ). map (({ name })=>[ name , l . rows ( 'SELECT * FROM "' + name + '"' )]));}
function balance ( f , member ){ return f . funds (). balances . find ( row => row . member === member );}
test ( '直接回收10克99%只入9.9克,手改最终总额保留到分,购料支出只记一次' , t =>{
2026-11-04 22:18:56 +08:00
const f = fixture ( t ), r = f . create ( direct ({ old_lines : old ( '10' , '99' ), buy_total : '8999.91' })), pool = f . l . materialPoolDetail ( D ). personalPools . assistant , report = f . report ();
2026-10-26 14:44:07 +08:00
assert . equal ( r . oldMg , 9900 ); assert . equal ( r . purchaseMg , 9900 ); assert . equal ( r . purchaseAmount , 899991 ); assert . equal ( r . cashPayment , 899991 ); assert . equal ( r . cashReceipt , 0 );
assert . equal ( pool . totalMg , 9900 ); assert . equal ( pool . totalAmount , 899991 ); assert . equal ( pool . usedMg , 0 ); assert . equal ( pool . remainingMg , 9900 );
assert . equal ( f . l . inventory (). find ( row => row . pocket === '回收旧料' ). mg , 9900 ); assert . equal ( f . l . rows ( 'SELECT * FROM orders' ). length , 0 );
assert . equal ( report . outlayGoods , 899991 ); assert . equal ( report . sales , 0 ); assert . equal ( report . profit , 0 ); assert . equal ( balance ( f , 'owner' ). outlays , 899991 );
assert . equal ( f . l . materialPurchaseDetail ( r . purchase_id ). purchase . actualRate , r . actualBuyRate );
const changes = f . l . one ( 'SELECT total_changes() AS n' ). n ; f . report (); f . funds (); f . l . recoveries ({ date : D }, f . actors . owner ); assert . equal ( f . l . one ( 'SELECT total_changes() AS n' ). n , changes );
});
test ( '多行旧料分别折后相加,小于5克和等重置换无门槛,零应收无需现金与刷卡费' , t =>{
const f = fixture ( t ); f . stock ();
const tiny = f . create ( direct ({ old_lines : [... old ( '1.234' , '99.9' ),... old ( '0.5' , '98' )]}));
assert . equal ( tiny . oldMg , 1723 ); assert . equal ( tiny . old_lines . length , 2 );
const r = f . create ( exchange ({ old_lines : old ( '2' ), buy_rate : '0' , payment_account : '麦麦信用卡' },{ quantity : '2' , gold_price : '0' , labor_rate : '0' , depreciation_rate : '0' , cost_labor : { mode : 'total' , amount : '0' }}));
assert . equal ( r . newMg , 2000 ); assert . equal ( r . matchedMg , 2000 ); assert . equal ( r . netSettlement , 0 ); assert . equal ( r . receivable , 0 ); assert . equal ( r . cardFee , 0 ); assert . deepEqual ( r . paymentMethods ,[]);
assert . equal ( f . l . detail ( r . order_id ). balance , 0 ); assert . equal ( f . l . detail ( r . order_id ). status , 'delivered' ); assert . equal ( f . l . rows ( 'SELECT * FROM payments' ). length , 0 ); assert . equal ( f . l . materialPoolDetail ( D ). usedMg , 0 );
});
test ( '20换30仅10克进入差重成本与池耗用,新货30克全部扣货盘并计进货工费' , t =>{
const f = fixture ( t ); f . stock ();
f . run ({},()=> f . l . createMaterialPurchase ({ date : D , grams : '50' , total : '45000' , payer : 'owner' , note : '合成日料' }, f . actors . owner ));
const r = f . create ( exchange ({ payment_account : '助理2微信' },{ cost_material : { mode : 'pool' , poolId : D }})), o = f . l . detail ( r . order_id ), pool = f . l . materialPoolDetail ( D ), book = f . l . orderBook ({ date : D });
assert . equal ( r . matchedMg , 20000 ); assert . equal ( r . differenceMg , 10000 ); assert . equal ( r . surplusMg , 0 ); assert . equal ( r . goldAmount , 1000000 ); assert . equal ( r . laborAmount , 60000 ); assert . equal ( r . depreciationAmount , 20000 );
assert . equal ( o . total , 1080000 ); assert . equal ( o . received , 1080000 ); assert . equal ( o . balance , 0 ); assert . equal ( o . goodsCost , 915000 ); assert . equal ( o . profit , 165000 );
assert . equal ( pool . usedMg , 10000 ); assert . equal ( pool . remainingMg , 40000 ); assert . equal ( pool . consumedCost , 900000 ); assert . equal ( pool . usages [ 0 ]. mg , 10000 );
assert . equal ( Calc . orderCosts ( o ). rows . find ( row => row . materialPoolId ). calculation . quantity , 10000 ); assert . equal ( o . lines [ 0 ]. quantity , 30000 );
assert . equal ( f . l . inventory (). find ( row => row . pocket === '现货' ). mg , 70000 ); assert . equal ( f . l . inventory (). find ( row => row . pocket === '回收旧料' ). mg , 20000 );
assert . equal ( balance ( f , 'assistant2' ). receipts , 1080000 ); assert . equal ( balance ( f , 'owner' ). outlays , 4515000 ); assert . equal ( book . rows [ 0 ]. recovery_id , r . id ); assert . equal ( f . report (). outlayGoods , 4515000 );
});
2026-11-04 22:18:56 +08:00
test ( '历史共享置换余料保留跨成员耗用,负净额和非现金抵扣不随个人池上线迁移' , t =>{
2026-10-26 14:44:07 +08:00
const f = fixture ( t ); f . stock ();
2026-11-04 22:18:56 +08:00
const r = f . create ( exchange ({ old_lines : old ( '30' ), buy_rate : '850' },{ quantity : '20' }));
// 合成上线前已有的回收批:旧批无个人来源标记,不能自动改写为个人料。
f . l . run ( 'DELETE FROM recovery_material_batches WHERE purchase_id=?' , r . purchase_id );
const o = f . l . detail ( r . order_id ), pool = f . l . materialPoolDetail ( D );
2026-10-26 14:44:07 +08:00
assert . equal ( r . receivable , 60000 ); assert . equal ( r . purchaseAmount , 850000 ); assert . equal ( r . offsetAmount , 60000 ); assert . equal ( r . cashPayment , 790000 ); assert . equal ( r . cashReceipt , 0 ); assert . equal ( r . netSettlement , - 790000 );
assert . equal ( o . received , 0 ); assert . equal ( o . balance , 0 ); assert . equal ( o . goodsCost , 10000 ); assert . equal ( o . profit , 50000 ); assert . equal ( f . l . rows ( 'SELECT * FROM payments' ). length , 0 );
assert . equal ( pool . totalMg , 10000 ); assert . equal ( pool . totalAmount , 850000 ); assert . equal ( pool . byPayer . assistant . purchased , 60000 ); assert . equal ( pool . byPayer . owner . purchased , 790000 );
assert . equal ( balance ( f , 'owner' ). outlays , 800000 ); assert . equal ( f . report (). outlayGoods , 800000 ); assert . equal ( f . report (). materialPoolActivity . summary . materialOutlay , 790000 );
const input = { date : D2 , customer : '合成次日客户' , salesperson : '合成开单人' , payment_account : '助理2微信' , rounding_mode : 'cent' , fee_items : [], lines : [{ name : '足金合成耗完余料' , unit : 'g' , quantity : '10' , pocket : '' , payer : 'assistant2' , sale : '10000' , costParts : { material : '' , labor : '0' , accessories : '0' , other : '0' }, costMaterial : { mode : 'pool' , poolId : D }}]};
f . run ( input ,()=> f . l . issueOrder ( input , f . actors . assistant2 ), 'assistant2' );
const consumed = f . l . materialPoolDetail ( D ); assert . equal ( consumed . remainingMg , 0 ); assert . equal ( consumed . remainingCost , 0 ); assert . equal ( consumed . byPayer . assistant . consumed , 60000 ); assert . equal ( consumed . byPayer . owner . consumed , 790000 );
for ( const source of Object . values ( consumed . byPayer )) assert . equal ( source . consumed + source . remaining , source . purchased );
assert . equal ( balance ( f , 'owner' ). outlays , 800000 , '之后耗用不再次记采购付款' );
const pair = f . funds (). pairs . find ( row => row . id === 'assistant:assistant2' ); assert . equal ( pair . net , - 60000 ); assert . equal ( f . report ( D2 ). outlayGoods , 0 );
});
test ( '余料金额小于应收时只收净额,余料全额为非现金;正负零净额跨成员实际现金一致' , t =>{
for ( const [ total , net , receipt , payout , offset ] of [[ '100' , 50000 , 50000 , 0 , 10000 ],[ '600' , 0 , 0 , 0 , 60000 ],[ '1000' , - 40000 , 0 , 40000 , 60000 ]]){
const f = fixture ( t ); f . stock (); const r = f . create ( exchange ({ old_lines : old ( '30' ), buy_total : total , payer : 'assistant2' , payment_account : receipt ? '麦麦微信' : payout ? '助理2现金' : '麦麦现金' },{ quantity : '20' , cost_payer : 'assistant' , cost_labor : { mode : 'total' , amount : '0' }}));
assert . equal ( r . netSettlement , net ); assert . equal ( r . cashReceipt , receipt ); assert . equal ( r . cashPayment , payout ); assert . equal ( r . offsetAmount , offset ); assert . equal ( f . l . detail ( r . order_id ). balance , 0 );
assert . equal ( balance ( f , 'owner' ). receipts , receipt ); assert . equal ( balance ( f , 'assistant2' ). outlays , payout ); assert . equal ( f . report (). outlayGoods , payout );
2026-11-04 22:18:56 +08:00
const pool = f . l . materialPoolDetail ( D ). personalPools . assistant ; assert . equal ( pool . byPayer . assistant . purchased , offset ); assert . equal ( pool . byPayer . assistant2 ? . purchased ?? 0 , payout );
2026-10-26 14:44:07 +08:00
assert . equal ( f . funds (). pairs . find ( row => row . id === 'owner:assistant' ). net , receipt );
}
});
test ( '置换收费优惠合并后只取整一次,关联购料金额保留分;刷卡只收真实差额且麦麦承担6‰' , t =>{
const f = fixture ( t ); f . stock (); const r = f . create ( exchange ({ old_lines : old ( '30' ), buy_total : '100.13' , payment_account : '助理信用卡' },
{ quantity : '20' , charges : [{ name : '合成附加服务' , amount : '7.45' }], discount : '2.10' , rounding_mode : 'yuan' , cost_labor : { mode : 'gram' , rate : '5' }, fee_items : [{ key : 'other' , note : '合成实付费' , amount : '3.21' , payer : 'assistant2' }]}));
assert . equal ( r . receivable , 60500 ); assert . equal ( r . purchaseAmount , 10013 ); assert . equal ( r . cashReceipt , 50487 ); assert . equal ( r . payment_account , '麦麦信用卡' ); assert . equal ( r . cardFee , 303 );
const o = f . l . detail ( r . order_id ); assert . equal ( o . lines [ 0 ]. charges [ 0 ]. amount , 745 ); assert . equal ( o . lines [ 0 ]. costLabor . rate , 500 ); assert . equal ( o . received , 50487 ); assert . equal ( o . nonCashOffset , 10013 ); assert . equal ( o . balance , 0 );
assert . equal ( o . directCost , 624 ); assert . equal ( o . profit , 49876 ); assert . equal ( balance ( f , 'owner' ). receipts , 50487 ); assert . equal ( balance ( f , 'assistant2' ). outlays , 321 );
2026-11-04 22:18:56 +08:00
assert . equal ( f . report (). outlayGoods , 10000 ); assert . equal ( f . report (). outlayExpense , 624 ); assert . equal ( f . l . materialPoolDetail ( D ). personalPools . assistant . totalAmount , 10013 );
2026-10-26 14:44:07 +08:00
assert . equal ( Calc . orderCosts ( o ). rows . find ( row => row . name === '进货工费' ). calculation . quantity , 20000 );
});
test ( '幂等重试、库存不足和末尾异常全部回滚;只读quote不占号不改池' , t =>{
const f = fixture ( t ), input = direct ({ old_lines : old ( '10' , '99' )}), key = randomUUID (), first = f . create ( input , 'assistant' , key ); assert . deepEqual ( f . create ( input , 'assistant' , key ), first ); assert . equal ( f . l . rows ( 'SELECT * FROM recoveries' ). length , 1 );
const before = content ( f . l ); assert . throws (()=> f . create ( exchange ()), /库存不足/ ); assert . equal ( content ( f . l ), before ); assert . throws (()=> f . create ( direct ({ buy_total : '1' }), 'assistant' , key ), /操作号已用于其他内容/ ); assert . equal ( content ( f . l ), before );
const quote = f . l . recoveryQuote ({... exchange (), customer : '' , salesperson : '' , verified : false }, f . actors . owner ); assert . equal ( quote . differenceMg , 10000 ); assert . equal ( content ( f . l ), before );
const original = f . l . audit ; f . l . audit = function ( entity , action ,... args ){ original . call ( this , entity , action ,... args ); if ( entity . startsWith ( 'recovery:' )) throw new Error ( '合成末尾故障' );};
try { assert . throws (()=> f . create ( direct ()), /合成末尾故障/ );} finally { f . l . audit = original ;} assert . equal ( content ( f . l ), before );
assert . equal ( f . create ( direct ()). number , 'HS202610200002' , '失败不消耗序号' );
});
test ( '助理只能查自身回收及关联单,老板代录归属一致;关联购料库存和普通订单变更均受保护' , t =>{
const f = fixture ( t ); f . stock (); const r = f . create ( exchange ({ old_lines : old ( '30' )},{ quantity : '20' }));
f . l . scope ( f . actors . assistant2 ,()=>{ assert . throws (()=> f . l . recovery ( r . id , f . actors . assistant2 ), /无权访问/ ); assert . throws (()=> f . l . publicRecovery ( r . id , f . actors . assistant2 ), /无权访问/ ); assert . equal ( f . l . recoveries ({ date : D }, f . actors . assistant2 ). rows . length , 0 ); assert . throws (()=> f . l . detail ( r . order_id ), /无权访问/ ); assert . throws (()=> f . l . materialPurchaseDetail ( r . purchase_id ), /无权访问/ );});
assert . throws (()=> f . create ( exchange ({ member : 'assistant2' })), /只能登记归属于本人/ );
const owned = f . create ( direct ({ member : 'assistant2' }), 'owner' ); assert . equal ( f . l . entityMember ( 'material_purchase' , owned . purchase_id ), 'assistant2' ); assert . equal ( f . l . entityMember ( 'movement' , f . l . one ( 'SELECT id FROM movements WHERE group_id=(SELECT inventory_group FROM recoveries WHERE id=?)' , owned . id ). id ), 'assistant2' );
const before = content ( f . l ), o = f . l . detail ( r . order_id );
for ( const action of [ 'expense' , 'fees' , 'cost' , 'correct' , 'refund' , 'return' , 'material' , 'confirm' , 'complete' , 'deliver' , 'settlement' ]) assert . throws (()=> f . run ({},()=> f . l . action ( r . order_id , action ,{ date : D , verified : true }, f . actors . owner )), /关联黄金回收/置换/ );
assert . throws (()=> f . run ({},()=> f . l . editOrder ( r . order_id ,{ revision : o . revision }, f . actors . owner )), /关联黄金回收/置换/ );
assert . throws (()=> f . run ({},()=> f . l . requestOrderCancellation ( r . order_id ,{ reason : '合成' }, f . actors . owner )), /关联黄金回收/置换/ );
assert . throws (()=> f . run ({},()=> f . l . requestOrderChange ( r . order_id ,{ reason : '合成' }, f . actors . owner )), /关联黄金回收/置换/ );
assert . throws (()=> f . run ({},()=> f . l . editMaterialPurchase ( r . purchase_id ,{ revision : 1 }, f . actors . owner )), /关联回收/置换/ );
assert . throws (()=> f . run ({},()=> f . l . deleteMaterialPurchase ( r . purchase_id ,{ revision : 1 }, f . actors . owner )), /关联回收/置换/ ); assert . equal ( content ( f . l ), before );
const movement = f . l . one ( 'SELECT id FROM movements WHERE kind=\'recovery\' AND order_id=?' , r . order_id ); assert . equal ( f . l . inventoryMovement ( movement . id , f . actors . owner ). canEdit , false );
assert . equal ( f . l . orderEdit ( r . order_id , f . actors . owner ). eligible , false ); assert . equal ( f . l . orderCancellation ( r . order_id , f . actors . owner ). eligible , false ); assert . equal ( f . l . orderChange ( r . order_id , f . actors . owner ). eligible , false );
const dto = f . l . publicRecovery ( r . id , f . actors . owner ), serialized = JSON . stringify ( dto ); assert . ok ( ! serialized . includes ( 'costMaterial' )); assert . ok ( ! serialized . includes ( 'costPayer' )); assert . ok ( ! serialized . includes ( 'pocket' ));
const fields = new FeishuSync ( f . l , null ). fields ( 'order:' + r . order_id , o . revision ); assert . equal ( fields [ '类型' ], '置换订单' ); assert . equal ( JSON . parse ( fields [ '明细' ]). nonCashOffset , r . offsetAmount );
});
test ( '普通销售伪造置换耗用字段不能减少扣料;历史数据重开不重写' , t =>{
const f = fixture ( t ); f . run ({},()=> f . l . createMaterialPurchase ({ date : D , grams : '10' , total : '9000' , payer : 'owner' , note : '合成池' }, f . actors . owner ));
const lines = f . l . validateLines ([{ name : '足金' , unit : 'g' , quantity : '10' , pocket : '' , payer : 'owner' , sale : '10000' , costParts : { material : '' , labor : '0' , accessories : '0' , other : '0' }, costMaterial : { mode : 'pool' , poolId : D , consumptionMg : 1 }, recoveryMaterialMg : 1 , recovery_id : randomUUID ()}],[], D );
assert . equal ( lines [ 0 ]. materialPool . quantity , 10000 ); assert . equal ( lines [ 0 ]. costParts . material , 900000 ); assert . equal ( lines [ 0 ]. recovery_id , undefined );
const before = content ( f . l ), file = f . l . filename , other = new Ledger ( file ); try { assert . equal ( content ( other ), before );} finally { other . close ();}
});
test ( 'HTTP回收接口登录CSRF幂等与成员隔离,普通打印也输出置换模板,客户单不泄漏成本' , async t =>{
const f = fixture ( t ); f . stock (); const app = createApp ({ mode : 'trial' , ledger : f . l , dataDir : path . dirname ( f . l . filename ), port : 0 , feishuConfig : { enabled : false }});
await new Promise ( resolve => app . server . listen ( 0 , '127.0.0.1' , resolve )); t . after (()=> new Promise ( resolve => app . server . close ( resolve )));
const base = 'http://127.0.0.1:' + app . server . address (). port , cookies = {};
async function call ( member , route , body ,{ key = randomUUID (), origin = base } = {}){
const response = await fetch ( base + route ,{ method : body === undefined ? 'GET' : 'POST' , headers : {...( cookies [ member ] ? { Cookie : cookies [ member ]} : {}),...( body === undefined ? {} : { 'Content-Type' : 'application/json' , Origin : origin , 'X-Requested-With' : 'maimai-ledger' ,...( key ? { 'Idempotency-Key' : key } : {})})},...( body === undefined ? {} : { body : JSON . stringify ( body )})});
return { status : response . status , body : response . headers . get ( 'content-type' ) ? . includes ( 'json' ) ? await response . json () : await response . text (), response };
}
assert . equal (( await call ( 'none' , '/api/recoveries' )). status , 401 ); assert . equal (( await call ( 'none' , '/api/recoveries/quote' , exchange ())). status , 401 );
for ( const member of [ 'owner' , 'assistant' , 'assistant2' ]){ const result = await call ( member , '/api/login' ,{ name : f . actors [ member ]. name , password : 'synthetic-recovery-password' }); assert . equal ( result . status , 200 ); cookies [ member ] = result . response . headers . get ( 'set-cookie' ). split ( ';' )[ 0 ];}
assert . equal (( await call ( 'assistant' , '/api/recoveries' , direct (),{ origin : 'https://evil.example' })). status , 403 );
const quote = await call ( 'assistant' , '/api/recoveries/quote' , exchange ({ buy_rate : '' , customer : '' , salesperson : '' }),{ key : null }); assert . equal ( quote . status , 200 , JSON . stringify ( quote . body )); assert . equal ( quote . body . differenceMg , 10000 ); assert . equal ( f . l . rows ( 'SELECT * FROM recoveries' ). length , 0 );
assert . equal (( await call ( 'assistant' , '/api/recoveries' , direct (),{ key : null })). status , 400 );
const key = randomUUID (), input = exchange ({ old_lines : old ( '30' ), customer : '不得向另一位助理公开' , buy_total : '8500' },{ quantity : '20' , cost_labor : { mode : 'total' , amount : '37.29' }});
const [ a , b ] = await Promise . all ([ call ( 'assistant' , '/api/recoveries' , input ,{ key }), call ( 'assistant' , '/api/recoveries' , input ,{ key })]); assert . equal ( a . status , 200 , JSON . stringify ( a . body )); assert . equal ( b . status , 200 , JSON . stringify ( b . body )); assert . deepEqual ( a . body , b . body ); const r = a . body ;
assert . equal (( await call ( 'assistant2' , '/api/recoveries/' + r . id )). status , 404 ); assert . equal (( await call ( 'assistant2' , '/print/recoveries/' + r . id )). status , 404 ); assert . equal (( await call ( 'assistant2' , '/print/' + r . order_id )). status , 404 );
assert . equal (( await call ( 'assistant2' , '/api/recoveries?date=' + D )). body . rows . length , 0 ); assert . equal (( await call ( 'owner' , '/api/recoveries?date=' + D + '&member=assistant' )). body . rows . length , 1 );
const report = ( await call ( 'owner' , '/api/report?basis=orderDate&date=' + D + '&member=assistant2' )). body ; assert . equal ( report . outlayGoods , 0 ); assert . equal ( report . sales , 0 );
for ( const route of [ '/print/recoveries/' + r . id , '/print/' + r . order_id ]){ const printed = await call ( 'assistant' , route ); assert . equal ( printed . status , 200 ); assert . match ( printed . body , /黄金置换单/ ); assert . doesNotMatch ( printed . body , /37\.29|进货|利润|成本付款人|costMaterial|costPayer/ );}
const movement = f . l . one ( "SELECT id FROM movements WHERE kind='recovery' AND order_id=?" , r . order_id );
for ( const route of [ '/api/material-purchases/' + r . purchase_id + '/delete' , '/api/inventory/movements/' + movement . id + '/delete' , '/api/orders/' + r . order_id + '/edit' , '/api/orders/' + r . order_id + '/canceltoday' ]) assert . ok (( await call ( 'assistant' , route ,{ revision : 1 , date : D , verified : true })). status >= 400 , route );
const read = ( await call ( 'assistant' , '/api/orders/' + r . order_id )). body ; assert . equal ( read . balance , 0 ); assert . equal ( read . received , 0 ); assert . equal ( read . recovery_id , r . id ); assert . equal ( read . nonCashOffset , 60000 );
});
2026-11-04 22:18:56 +08:00
test ( '回收采购现金和抵扣纳入日报快照,个人回收料不被公共补购混价或重记收付款实物' , t =>{
2026-10-26 14:44:07 +08:00
const f = fixture ( t ); f . stock ();
f . run ({},()=> f . l . closeDay ( D , f . actors . owner , 'orderDate' )); const emptyHash = f . report (). reportRevision ;
f . create ( direct ({ old_lines : old ( '10' ), buy_total : '9000' })); assert . equal ( f . report (). close . stale , true ); assert . notEqual ( f . report (). reportRevision , emptyHash );
f . run ({},()=> f . l . closeDay ( D , f . actors . owner , 'orderDate' )); const r = f . create ( exchange ({ date : D2 , buy_rate : '' },{ cost_material : { mode : 'pool' , poolId : D }}));
assert . equal ( f . report (). close . stale , false , '次日耗用不重写已核对原采购日' );
const beforeMovements = f . l . rows ( 'SELECT * FROM movements' ), beforePayments = f . l . rows ( 'SELECT * FROM payments' ), beforeCash = balance ( f , 'owner' ). outlays ;
f . run ({},()=> f . l . createMaterialPurchase ({ date : D , grams : '10' , total : '8000' , payer : 'assistant2' , note : '合成补购' }, f . actors . owner ));
2026-11-04 22:18:56 +08:00
assert . equal ( f . l . detail ( r . order_id ). goodsCost , 915000 ); assert . equal ( f . l . detail ( r . order_id ). profit , 165000 ); assert . deepEqual ( f . l . rows ( 'SELECT * FROM movements' ), beforeMovements ); assert . deepEqual ( f . l . rows ( 'SELECT * FROM payments' ), beforePayments );
2026-10-26 14:44:07 +08:00
assert . equal ( balance ( f , 'owner' ). outlays , beforeCash ); assert . equal ( f . report ( D2 ). outlayGoods , 15000 ); assert . equal ( f . report (). close . stale , true );
});