2026-10-26 14:44:07 +08:00
|
|
|
|
'use strict';
|
|
|
|
|
|
const {escape,settings,uppercaseMoney}=require('./printing');
|
|
|
|
|
|
|
|
|
|
|
|
// 仅逐字段读取 publicRecovery 客户白名单;不遍历内部采购、成本、料池或成员往来对象。
|
|
|
|
|
|
const 整数=(值,名称,允许负数=false)=>{
|
|
|
|
|
|
if(!Number.isSafeInteger(值)||(!允许负数&&值<0))throw new Error(`打印${名称}须为${允许负数?'':'非负'}整数`);
|
|
|
|
|
|
return 值;
|
|
|
|
|
|
};
|
|
|
|
|
|
const 金额=分=>{
|
|
|
|
|
|
整数(分,'金额',true);const 绝对值=Math.abs(分);
|
|
|
|
|
|
return `${分<0?'-':''}${Math.floor(绝对值/100)}.${String(绝对值%100).padStart(2,'0')}`;
|
|
|
|
|
|
};
|
|
|
|
|
|
const 克重=毫克=>{
|
|
|
|
|
|
if(毫克===null||毫克===undefined)return '—';
|
|
|
|
|
|
const 百分克=Math.round(整数(毫克,'重量')/10);
|
|
|
|
|
|
return `${Math.floor(百分克/100)}.${String(百分克%100).padStart(2,'0')}`;
|
|
|
|
|
|
};
|
|
|
|
|
|
const 成色=值=>{
|
|
|
|
|
|
if(值===null||值===undefined)return '—';
|
|
|
|
|
|
if(typeof 值!=='number'||!Number.isFinite(值)||值<0||值>100)throw new Error('打印成色须为0至100的百分比');
|
|
|
|
|
|
return String(值)+'%';
|
|
|
|
|
|
};
|
|
|
|
|
|
const 单价=分=>分===null||分===undefined?'—':`¥${金额(整数(分,'单价'))}/克`;
|
|
|
|
|
|
const 单号=单据=>String(单据.number||'未分配');
|
|
|
|
|
|
const 附加项目=单据=>(单据.charges||[]).map(项目=>({name:String(项目.name||'附加项目'),amount:整数(项目.amount,'附加金额')}));
|
|
|
|
|
|
function 回收调整(单据,毫克){
|
|
|
|
|
|
const 最终额=整数(单据.purchaseAmount,'回收金额'),单价分=整数(单据.buyRate,'回收单价');
|
|
|
|
|
|
// 优先取后端保存的计算额;旧DTO缺字段时仅复算对照说明,绝不改变已保存的最终结算。
|
|
|
|
|
|
const 计算额=整数(单据.calculatedPurchaseAmount??Number((BigInt(整数(毫克,'回收重量'))*BigInt(单价分)+500n)/1000n),'按单价计算金额');
|
|
|
|
|
|
return {最终额,计算额,已调整:最终额!==计算额};
|
|
|
|
|
|
}
|
|
|
|
|
|
function 旧料表(单据,{回收=false}={}){
|
|
|
|
|
|
const 行=(单据.old_lines||[]).map((旧料,序号)=>`<tr>${[
|
|
|
|
|
|
序号+1,`Au 黄金 · ${escape(旧料.name||'旧料')}`,克重(旧料.incomingMg),克重(旧料.meltedMg),成色(旧料.purity),克重(旧料.foldedMg),...(回收?[单价(单据.buyRate)]:[])
|
|
|
|
|
|
].map(值=>`<td>${值}</td>`).join('')}</tr>`).join('');
|
|
|
|
|
|
const 标题=['序号','旧料名称','来料克重(克)','熔后克重(克)','成色','折后克重(克)',...(回收?['回收单价']:[])];
|
|
|
|
|
|
return `<section class="transaction-section"><h2>旧料(Au 黄金)</h2><table class="old-material"><colgroup><col style="width:6%"><col style="width:${回收?'24':'29'}%">${Array.from({length:回收?5:4},()=>`<col style="width:${回收?'14':'16.25'}%">`).join('')}</colgroup><thead><tr>${标题.map(值=>`<th>${值}</th>`).join('')}</tr></thead><tbody>${行||`<tr><td colspan="${标题.length}">未登记旧料明细</td></tr>`}</tbody></table><div class="weight-summary">旧料折后合计:<strong>${克重(单据.oldMg)} 克</strong></div></section>`;
|
|
|
|
|
|
}
|
|
|
|
|
|
function 新货表(单据){
|
2026-11-24 21:11:32 +08:00
|
|
|
|
// 按客户白名单逐件输出新货,历史单件记录继续使用原字段。
|
2026-10-26 14:44:07 +08:00
|
|
|
|
const 新货=Array.isArray(单据.new_lines)?单据.new_lines:单据.newProduct?[单据.newProduct]:[];
|
|
|
|
|
|
return `<section class="transaction-section"><h2>换走新货</h2><table class="new-products"><thead><tr><th style="width:8%">序号</th><th>新货名称</th><th style="width:28%">全克重(克)</th></tr></thead><tbody>${新货.map((商品,序号)=>`<tr><td>${序号+1}</td><td>${escape(商品.name||'黄金新货')}</td><td>${克重(商品.quantity)}</td></tr>`).join('')||'<tr><td colspan="3">未登记新货明细</td></tr>'}</tbody></table><div class="weight-summary">换走新货全克重合计:<strong>${克重(单据.newMg)} 克</strong></div></section>`;
|
|
|
|
|
|
}
|
|
|
|
|
|
function 付款方式(单据,方向){
|
|
|
|
|
|
if(方向==='zero')return '结算方式:无需补退';
|
|
|
|
|
|
const 方式=[...new Set((单据.paymentMethods||[]).filter(值=>typeof 值==='string'&&值.trim()).map(值=>值.trim()))];
|
|
|
|
|
|
return `${方向==='receipt'?'收款':'付款'}方式:${escape(方式.join('、')||'尚未登记')}`;
|
|
|
|
|
|
}
|
|
|
|
|
|
function 回收正文(单据){
|
|
|
|
|
|
const 调整=回收调整(单据,单据.oldMg),总额=调整.最终额;
|
|
|
|
|
|
const 说明=调整.已调整?`<div class="price-adjustment">按回收单价计算:¥${金额(调整.计算额)};核实后调整为:¥${金额(总额)}。</div>`:'';
|
|
|
|
|
|
return `${旧料表(单据,{回收:true})}${说明}<div class="totals"><div class="uppercase">回收人民币大写:<strong>${uppercaseMoney(总额)}</strong></div><div class="amount">最终回收金额:<strong>¥${金额(总额)}</strong></div></div>`;
|
|
|
|
|
|
}
|
|
|
|
|
|
function 置换正文(单据){
|
|
|
|
|
|
const 差额=整数(单据.netSettlement,'结算金额',true),余料款=整数(单据.purchaseAmount,'余料回收款');
|
|
|
|
|
|
const 调整=回收调整(单据,单据.surplusMg),余料说明=调整.已调整?`${克重(单据.surplusMg)} 克;回收单价 ${单价(单据.buyRate)};按单价计算 ¥${金额(调整.计算额)},核实后调整为 ¥${金额(余料款)}`:`${克重(单据.surplusMg)} 克 × ${单价(单据.buyRate)}`;
|
2026-11-24 21:56:05 +08:00
|
|
|
|
const 分件说明=(重量,单价键,金额键)=>单据.exchange_items?.map((p,i)=>'商品'+(i+1)+' '+escape(p.newProduct.name)+':'+克重(p[重量])+'克 × '+单价(p[单价键])+' = ¥'+金额(p[金额键])).join('<br>');
|
2026-11-24 21:11:32 +08:00
|
|
|
|
const 附加=附加项目(单据),工费说明=单据.new_lines?.every(商品=>Number.isSafeInteger(商品.laborAmount)&&Number.isSafeInteger(商品.laborRate))?单据.new_lines.map(商品=>escape(商品.name)+':'+(商品.laborMode==='total'?'总额':克重(商品.quantity)+'克 × '+单价(商品.laborRate))+' = ¥'+金额(商品.laborAmount)).join('<br>'):单据.laborMode==='total'?`整单工费 ¥${金额(整数(单据.laborRate,'工费金额'))}`:单据.laborMode==='piece'?`¥${金额(整数(单据.laborRate,'工费单价'))}/件`:`${克重(单据.newMg)} 克 × ${单价(单据.laborRate)}`;
|
2026-10-26 14:44:07 +08:00
|
|
|
|
const 费用=[
|
2026-11-24 21:56:05 +08:00
|
|
|
|
['补金差价',分件说明('differenceMg','goldRate','goldAmount')||`${克重(单据.differenceMg)} 克 × ${单价(单据.goldRate)}`,整数(单据.goldAmount,'补金差价'),'+'],
|
2026-10-26 14:44:07 +08:00
|
|
|
|
['新货工费',工费说明,整数(单据.laborAmount,'新货工费'),'+'],
|
2026-11-24 21:56:05 +08:00
|
|
|
|
['折旧费',分件说明('depreciationMg','depreciationRate','depreciationAmount')||`${克重(单据.depreciationMg)} 克 × ${单价(单据.depreciationRate)}`,整数(单据.depreciationAmount,'折旧费'),'+'],
|
2026-10-26 14:44:07 +08:00
|
|
|
|
...(附加.length?附加.map(项目=>['附加:'+项目.name,'',项目.amount,'+']):[['附加项目','',整数(单据.chargesAmount??0,'附加金额'),'+']]),
|
|
|
|
|
|
['优惠','',整数(单据.discount??0,'优惠金额'),'−'],
|
2026-11-26 01:15:41 +08:00
|
|
|
|
...(单据.depositMg?[['余料寄存',`${(整数(单据.depositMg,'寄存重量')/1000).toFixed(3)} 克,暂不结算,保留供以后置换使用`,0,'']]:[]),
|
|
|
|
|
|
['余料回收款',单据.exchange_items?.map((p,i)=>p.depositMg?'':'商品'+(i+1)+':'+克重(p.surplusMg)+'克 × '+单价(p.buyRate)+',实际回收 ¥'+金额(p.purchaseAmount)).filter(Boolean).join('<br>')||(单据.depositMg?'寄存部分不折价、不抵扣本单费用':余料说明),余料款,'−']
|
2026-10-26 14:44:07 +08:00
|
|
|
|
];
|
|
|
|
|
|
if(单据.rounding_mode==='yuan'&&单据.rounding_adjustment){
|
|
|
|
|
|
const 调整=整数(单据.rounding_adjustment,'取整调整',true);
|
|
|
|
|
|
费用.push(['整单取整调整','',Math.abs(调整),调整<0?'−':'+']);
|
|
|
|
|
|
}
|
|
|
|
|
|
const 方向=差额>0?'客户补款':差额<0?'付客户':'无需补退';
|
|
|
|
|
|
return `${旧料表(单据)}${新货表(单据)}<section class="transaction-section"><h2>置换结算明细</h2><table class="settlement-lines"><thead><tr><th style="width:26%">项目</th><th>计价说明</th><th style="width:24%">金额(元)</th></tr></thead><tbody>${费用.map(([名称,说明,分,符号])=>`<tr><td>${escape(名称)}</td><td>${说明}</td><td class="number">${符号}¥${金额(分)}</td></tr>`).join('')}</tbody></table></section><div class="totals"><div class="uppercase">结算人民币大写:<strong>${uppercaseMoney(Math.abs(差额))}</strong></div><div class="amount">${方向}:<strong>¥${金额(Math.abs(差额))}</strong></div></div>`;
|
|
|
|
|
|
}
|
|
|
|
|
|
function 客户打印文档(单据,原设置={},类型){
|
2026-11-04 22:18:56 +08:00
|
|
|
|
const 设置=settings(原设置),标题=(类型==='buy'?'黄金回收单':'黄金置换单')+(单据.status==='cancelled'?'(已取消)':'');
|
2026-10-26 14:44:07 +08:00
|
|
|
|
const 内容=类型==='buy'?回收正文(单据):置换正文(单据);
|
|
|
|
|
|
const 方向=类型==='buy'?'payment':单据.netSettlement>0?'receipt':单据.netSettlement<0?'payment':'zero';
|
|
|
|
|
|
const 左边距=Math.max(3,12+设置.offsetX),右边距=Math.max(3,12-设置.offsetX),上边距=Math.max(3,设置.contentTop+设置.offsetY);
|
|
|
|
|
|
const 内容宽度=Math.min(192,设置.width-左边距-右边距),内容高度=设置.height-上边距-8;
|
|
|
|
|
|
return `<!doctype html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>${escape(单号(单据))} · ${标题}</title><style>
|
|
|
|
|
|
*{box-sizing:border-box}body{margin:0;background:#e8e7e4;color:#181818;font-family:"Microsoft YaHei","PingFang SC",sans-serif;font-size:12px}.toolbar{padding:12px 20px;background:white;position:sticky;top:0;z-index:2;display:flex;gap:12px;align-items:center;flex-wrap:wrap;box-shadow:0 1px 5px #aaa}.toolbar button{padding:9px 16px;border:1px solid #444;border-radius:5px;background:#254d42;color:#fff;cursor:pointer}.toolbar button:disabled{opacity:.5}.toolbar small{color:#555}.sheet{width:241mm;height:140mm;background:white;margin:12px auto;position:relative;box-shadow:0 3px 16px #bbb}.print-frame{position:absolute;max-width:192mm;left:${左边距}mm;right:${右边距}mm;top:${上边距}mm;bottom:8mm}.content{width:100%;transform-origin:top left;font-size:13.2px}.print-canvas{display:none}header{text-align:center;padding:0 0 3mm}h1{font-size:27.5px;letter-spacing:5px;margin:0 0 3px;overflow-wrap:anywhere}.subtitle{letter-spacing:3px;font-size:17.6px;overflow-wrap:anywhere}.meta{display:flex;justify-content:space-between;gap:10px;margin:5px 0;overflow-wrap:anywhere}.meta span,.meta strong{max-width:65%}.secondary{font-size:12.1px;margin-bottom:7px}.transaction-section{margin-top:5px}h2{font-size:13.2px;margin:0 0 3px}table{border-collapse:collapse;width:100%;table-layout:fixed}th,td{border:1px solid #333;padding:4px;overflow-wrap:anywhere;text-align:center}th{height:8mm;font-size:12.1px}td{height:${设置.rowHeight}mm;font-size:13.2px}td:nth-child(2){text-align:left}.weight-summary{border:1px solid #333;border-top:0;padding:3px 5px;text-align:right;line-height:1.4}.price-adjustment{border:1px solid #333;border-top:0;padding:4px 5px;line-height:1.4}.settlement-lines td{height:auto;min-height:6mm}.settlement-lines td:first-child{text-align:left}.settlement-lines td.number{text-align:right;white-space:nowrap}.totals{display:flex;align-items:baseline;justify-content:space-between;gap:10px;flex-wrap:wrap;padding:5px;border:1px solid #333;border-top:0;line-height:1.5;overflow-wrap:anywhere}.uppercase{flex:1;min-width:0}.amount{flex-shrink:0}.amount strong{font-size:17.6px}.note{flex:1;min-width:0;white-space:pre-wrap;overflow-wrap:anywhere;line-height:inherit}.signatures{display:flex;align-items:flex-start;justify-content:space-between;gap:12px;padding-top:8px;line-height:1.6;overflow-wrap:anywhere}.signatures .salesperson{margin-left:auto;flex-shrink:0;white-space:nowrap}.compact td{height:6mm;padding:2px 3px}.compact th{height:6mm;padding:2px 3px}.compact header{padding-bottom:1mm}.compact .transaction-section{margin-top:3px}.compact .signatures{padding-top:1px;padding-bottom:1px;line-height:1.4}
|
|
|
|
|
|
@page{size:241mm 140mm;margin:0}@media print{html,body{width:auto;height:auto;margin:0;padding:0;background:white}.toolbar{display:none}.sheet{width:241mm;height:calc(140mm - 1px);max-width:241mm;max-height:140mm;box-shadow:none;margin:0;break-inside:avoid;page-break-inside:avoid}.print-frame>.content{display:none}.print-canvas{display:block;width:100%;height:100%;position:absolute;inset:0}body.fit-error .sheet{visibility:hidden}}
|
|
|
|
|
|
</style></head><body><div class="toolbar"><button id="print" disabled>打印/保存 PDF</button><small>固定241×140mm横版;打印窗口也选241×140mm,关闭页眉页脚。</small><small id="fit-status" role="status">正在调整为一页…</small></div><section class="sheet"><div class="print-frame" data-layout-width-mm="${内容宽度}" data-layout-height-mm="${内容高度}"><div class="content"><header><h1>${escape(设置.store)}</h1><div class="subtitle">${标题}</div></header><div class="meta"><span>客户:${escape(单据.customer||'—')}</span><span>日期:${escape(单据.date||'—')}</span><strong>单号:${escape(单号(单据))}</strong></div><div class="meta secondary"><span>开单人:${escape(单据.salesperson||'—')}</span><span>${付款方式(单据,方向)}</span></div>${内容}<div class="signatures"><div class="note">备注:${escape(单据.note||'—')}${设置.contact?`<div>联系电话:${escape(设置.contact)}</div>`:''}</div><span class="salesperson">经办人:${escape(单据.salesperson||'')} ________________</span></div></div><svg class="print-canvas" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMin meet" aria-hidden="true"><foreignObject x="0" y="0"></foreignObject></svg></div></section><script src="/print.js"></script></body></html>`;
|
|
|
|
|
|
}
|
|
|
|
|
|
// 新回收、置换表格与销售预印纸不同,始终输出完整事实单据,不套用销售条款或销售预印底板。
|
|
|
|
|
|
function recoveryCustomerPrintDocument(单据,原设置={}){return 客户打印文档(单据,原设置,'buy');}
|
|
|
|
|
|
function exchangeCustomerPrintDocument(单据,原设置={}){return 客户打印文档(单据,原设置,'exchange');}
|
|
|
|
|
|
module.exports={recoveryCustomerPrintDocument,exchangeCustomerPrintDocument};
|