固定销售单据为241×140毫米并自动缩放长单

This commit is contained in:
summer committed 2026-10-22 12:59:56 +08:00
1 parent b9154a752d
commit 5265a2e568
8 files changed
+70 -24

No files matched your search

+11 -8
View File
@@ -1,6 +1,6 @@
'use strict';
const {test}=require('node:test'),assert=require('node:assert/strict'),fs=require('node:fs'),path=require('node:path'),vm=require('node:vm');
const {receipt,settings}=require('../printing');
const {receipt,settings,defaults}=require('../printing');
const script=fs.readFileSync(path.join(__dirname,'../public/print.js'),'utf8');
const mm=value=>Number.parseFloat(value)*96/25.4;
function classList(initial=[]){const values=new Set(initial);return {add:value=>values.add(value),remove:value=>values.delete(value),contains:value=>values.has(value)};}
@@ -31,15 +31,18 @@ function page({long=false,fontsReady=true}={}){
vm.runInNewContext(script,{window,document});
return {body,content,foreignObject,canvas,button,status,window,media,events,mediaEvents,click:()=>clicks.get('click')()};
}
test('打印纸型和方向由浏览器决定,旧宽高只保留为预览及来源排版尺寸',()=>{
test('默认、旧设置和客户端传入宽高均固定为241×140mm横版,不能放大到A4页面',()=>{
const sample={lines:[],total:0,paper_number:'20261022001',status:'delivered'};
for(const raw of [{width:241,height:140},{width:241,height:279},{width:140,height:241}]){
assert.equal(defaults.width,241);assert.equal(defaults.height,140);
for(const raw of [{},{width:241,height:140},{width:241,height:279},{width:140,height:241},{width:300,height:400},{width:'无效旧宽度',height:-1},{width:null,height:''}]){
const before=JSON.stringify(raw);
const saved=settings(raw),html=receipt(sample,saved),print=html.slice(html.indexOf('@page'));
assert.equal(saved.width,raw.width);assert.equal(saved.height,raw.height);
assert.match(print,/@page\{size:auto;margin:0\}/);assert.match(print,/html,body\{width:auto;height:auto/);
assert.match(print,/\.sheet\{width:100%;height:calc\(100vh - 1px\)/);
assert.doesNotMatch(print,/@page\{size:\d|rotate\(|landscape|portrait/);
assert.match(html,new RegExp(`data-layout-width-mm="${raw.width-24}"`));
assert.equal(saved.width,241);assert.equal(saved.height,140);assert.equal(JSON.stringify(raw),before);
assert.match(print,/@page\{size:241mm 140mm;margin:0\}/);
assert.match(print,/\.sheet\{width:241mm;height:calc\(140mm - 1px\);max-width:241mm;max-height:140mm/);
assert.doesNotMatch(print,/size:auto|100vh|100vw|rotate\(/);
assert.match(html,/\.sheet\{width:241mm;height:140mm/);
assert.match(html,/data-layout-width-mm="217" data-layout-height-mm="120"/);
assert.match(html,/preserveAspectRatio="xMinYMin meet"/);
}
});
@@ -0,0 +1,40 @@
'use strict';
const test=require('node:test'),assert=require('node:assert/strict'),{randomUUID}=require('node:crypto'),fs=require('node:fs'),os=require('node:os'),path=require('node:path');
const {createApp}=require('../server');
function businessSnapshot(ledger){
const tables=['orders','payments','expenses','journal','movements','settlements','sync','closes','inventory_snapshots','price_locks','price_lock_events','material_purchases','material_pools','material_pool_usages','material_pool_settlements'];
return Object.fromEntries(tables.filter(name=>ledger.one("SELECT name FROM sqlite_master WHERE type='table' AND name=?",name)).map(name=>[name,ledger.rows('SELECT * FROM '+name)]));
}
async function fixture(t){
const dir=fs.mkdtempSync(path.join(os.tmpdir(),'maimai-print-settings-')),app=createApp({mode:'demo',dataDir:dir,port:0});
await new Promise(resolve=>app.server.listen(0,'127.0.0.1',resolve));
t.after(async()=>{await new Promise(resolve=>app.server.close(resolve));app.ledger.close();fs.rmSync(dir,{recursive:true,force:true});});
const base='http://127.0.0.1:'+app.server.address().port;let cookie='';
const call=(route,input)=>fetch(base+route,{method:input?'POST':'GET',headers:{Cookie:cookie,...(input?{'Content-Type':'application/json',Origin:base,'X-Requested-With':'maimai-ledger','Idempotency-Key':randomUUID()}:{} )},...(input?{body:JSON.stringify(input)}:{})});
cookie=(await call('/api/demo-login',{role:'owner'})).headers.get('set-cookie').split(';')[0];
return {app,call};
}
test('读取旧打印设置只规范化返回241×140,不回写旧meta或业务账',async t=>{
const {app,call}=await fixture(t),old={store:'历史店名',subtitle:'历史标题',width:300,height:400,offsetX:2,offsetY:3,contact:'测试联系信息',rows:5,rowHeight:12,contentTop:14};
app.ledger.setMeta('printing',old);
const before=businessSnapshot(app.ledger),audit=app.ledger.rows('SELECT * FROM audit');
const response=await call('/api/state');assert.equal(response.status,200);const state=await response.json();
assert.deepEqual(state.printing,{...old,width:241,height:140});
assert.deepEqual(app.ledger.meta('printing'),old);assert.deepEqual(businessSnapshot(app.ledger),before);assert.deepEqual(app.ledger.rows('SELECT * FROM audit'),audit);
const html=await(await call('/print/'+state.orders[0].id)).text();
assert.match(html,/@page\{size:241mm 140mm;margin:0\}/);assert.match(html,/历史店名/);
assert.match(html,/data-layout-width-mm="217" data-layout-height-mm="115"/);
assert.deepEqual(app.ledger.meta('printing'),old);assert.deepEqual(businessSnapshot(app.ledger),before);
});
test('新表单不带尺寸和旧客户端只改尺寸均保存固定纸型,保留可调设置及所有业务账',async t=>{
const {app,call}=await fixture(t),old={store:'原店名',subtitle:'保留标题',contact:'保留联系信息',width:140,height:241,offsetX:3,offsetY:4,contentTop:16,rowHeight:11,rows:6};
app.ledger.setMeta('printing',old);const before=businessSnapshot(app.ledger);
const input={store:'更新店名',offsetY:5};assert.equal((await call('/api/printing',input)).status,200);
const expected={...old,...input,width:241,height:140};assert.deepEqual(app.ledger.meta('printing'),expected);
for(const sizes of [{width:300,height:400},{width:'无效旧宽度',height:-1}]){
assert.equal((await call('/api/printing',sizes)).status,200);
assert.deepEqual(app.ledger.meta('printing'),expected);
const state=await(await call('/api/state')).json();assert.deepEqual(state.printing,expected);
}
assert.deepEqual(businessSnapshot(app.ledger),before);
});
+3 -2
View File
@@ -28,10 +28,11 @@ test('注意事项按最新要求去掉维修承诺,取消退货仍注明原
assert.ok(!html.includes('免费维修'));assert.ok(!html.includes('仅收取配件费用'));
for(const status of ['returned','cancelled'])assert.ok(receipt({...sample,status}).includes('原单成交额'));
});
test('单页模板接受紧凑纸张和长备注,所有商品与收费完整保留',()=>{
test('固定横版单页保留超多商品、收费和长备注,不受旧尺寸设置影响',()=>{
const lines=Array.from({length:60},(_,i)=>({...sample.lines[0],name:'测试商品'+i,charges:[{name:'附加服务'+i,amount:100}]}));
const html=receipt({...sample,lines,note:'长备注'.repeat(80)},settings({width:241,height:140,rows:12,rowHeight:16}));
const html=receipt({...sample,lines,note:'长备注'.repeat(80)},settings({width:300,height:400,rows:12,rowHeight:16}));
assert.equal((html.match(/<section class="sheet /g)||[]).length,1);assert.ok(html.includes('测试商品59'));assert.ok(html.includes('附加服务59'));assert.ok(html.includes('长备注'.repeat(80)));assert.ok(!html.includes('overflow:hidden'));assert.ok(html.includes('/print.js'));
assert.match(html,/@page\{size:241mm 140mm;margin:0\}/);
for(const mode of ['full','overlay']){
const printed=receipt({...sample,lines},{},mode);
const numbers=[...printed.matchAll(/<tr><td>([^<]+)<\/td>/g)].map(m=>m[1]);