Files
jinniu-daikuan/app/views/acceptance_discount_calculator.php
T
4e302a7071 HEL-327: restore interest copy units and keep discount date on reset.
Match frozen calculator semantics so copied interest text uses 元 and reset no longer overwrites the current discount date.

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: multica-agent <github@multica.ai>
2026-09-01 11:27:01 +08:00

361 lines
22 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
/** @var array $me */
?>
<div class="page-head">
<div>
<h1 class="page-title">计算工具</h1>
<div class="page-sub">利息计算 · 承兑贴现(与原系统同口径本地试算,结果仅供参考)</div>
</div>
</div>
<?php fusionToolsTabs('acceptance_discount_calculator'); ?>
<div class="calc-grid">
<div class="editor-card">
<div class="cr-group">
<div class="section-title" style="margin-top:0;">票据信息</div>
<div class="cr"><span class="cr-label">票据类型</span><div class="cr-ctl"><div class="opt-row">
<label><input type="radio" name="dc_bill" value="bank" checked> 银行承兑汇票</label>
<label><input type="radio" name="dc_bill" value="commercial"> 商业承兑汇票</label>
<span class="cr-hint" style="margin:0;" id="dc_risk_hint">银承:流通性较好,贴现利率通常低于商承。</span>
</div></div></div>
<div class="cr"><label for="dc_face">票面金额(元)</label><div class="cr-ctl">
<input id="dc_face" type="text" inputmode="decimal" autocomplete="off" style="max-width:230px">
<div class="cr-hint"><span id="dc_face_fmt"></span> <span id="dc_face_cap"></span></div>
</div></div>
<div class="cr"><span class="cr-label">出票 / 贴现日期</span><div class="cr-ctl"><div class="cr-inline" style="gap:16px;">
<span style="display:inline-flex;align-items:center;gap:8px;">
<span style="font-size:12.5px;color:var(--text-2);">出票日期</span>
<input id="dc_issue" type="date" style="width:150px">
</span>
<span style="display:inline-flex;align-items:center;gap:8px;">
<span style="font-size:12.5px;color:var(--text-2);">贴现日期</span>
<input id="dc_discount" type="date" style="width:150px">
</span>
<span class="cr-hint" style="margin:0;">贴现日留空取当天</span>
</div></div></div>
<div class="cr"><span class="cr-label">到期日</span><div class="cr-ctl"><div class="cr-inline" style="gap:16px;">
<span class="opt-row" style="min-height:0;gap:14px;">
<label><input type="radio" name="dc_mat_mode" value="date" checked> 指定日期</label>
<label><input type="radio" name="dc_mat_mode" value="after"> 见票后定期</label>
</span>
<span id="dc_row_mat_date" class="cr-pair">
<span style="font-size:12.5px;color:var(--text-2);">到期日期</span>
<input id="dc_maturity" type="date" style="width:150px">
</span>
</div></div></div>
<div class="cr" id="dc_row_mat_after" hidden><label for="dc_months">期限(月)</label><div class="cr-ctl"><div class="cr-inline">
<input id="dc_months" type="number" min="1" max="12" step="1" value="6" style="width:84px">
<button type="button" class="btn btn--plain btn--sm" id="dc_apply_months">推算到期日</button>
<span class="cr-hint" style="margin:0" id="dc_mat_preview"></span>
</div></div></div>
</div>
<div class="cr-group">
<div class="section-title">贴现参数</div>
<div class="cr"><label for="dc_rate">贴现年利率 %</label><div class="cr-ctl"><div class="cr-inline">
<input id="dc_rate" type="text" inputmode="decimal" autocomplete="off" style="width:100px">
<span class="ref-chips">
<button type="button" class="btn btn--plain btn--sm" data-ref="1.85">国股参考</button>
<button type="button" class="btn btn--plain btn--sm" data-ref="2.15">城商参考</button>
<button type="button" class="btn btn--plain btn--sm" data-ref="2.45">农商参考</button>
</span>
</div></div></div>
<div class="cr"><label for="dc_day_rule">计息方式</label><div class="cr-ctl"><div class="cr-inline">
<select id="dc_day_rule" style="width:280px;max-width:100%;">
<option value="exclusive" selected>记首不记尾(贴现日至到期前一日)</option>
<option value="inclusive">记首记尾(对比用)</option>
</select>
<span class="cr-hint" style="margin:0;">计息基准:360 天/年(票据贴现常用)</span>
</div></div></div>
</div>
<div class="cr-group">
<div class="section-title">加天规则</div>
<div class="cr"><span class="cr-label">顺延与在途</span><div class="cr-ctl"><div class="cr-inline" style="gap:26px;">
<span style="display:inline-flex;align-items:center;gap:9px;">
<button type="button" class="switch" role="switch" aria-checked="false" data-switch data-bind-check="dc_remote" aria-label="异地加 3 天在途"></button>
<span style="font-size:13px;">异地(+3 天在途)</span>
<input type="checkbox" class="visually-hidden" id="dc_remote" tabindex="-1">
</span>
<span style="display:inline-flex;align-items:center;gap:9px;">
<button type="button" class="switch" role="switch" aria-checked="false" data-switch data-bind-check="dc_holiday" aria-label="到期日遇双休日或法定节假日顺延"></button>
<span style="font-size:13px;">到期日遇双休日 / 法定节假日顺延至下一工作日</span>
<input type="checkbox" class="visually-hidden" id="dc_holiday" tabindex="-1">
</span>
</div></div></div>
</div>
<div class="cr-group">
<div class="section-title">反向推算<span style="font-weight:500;font-size:11.5px;color:var(--text-3);margin-left:8px;">选填其一,优先按实付金额</span></div>
<div class="cr"><span class="cr-label">反推利率</span><div class="cr-ctl"><div class="cr-inline" style="gap:16px;">
<span style="display:inline-flex;align-items:center;gap:8px;">
<span style="font-size:12.5px;color:var(--text-2);">实付金额(元)</span>
<input id="dc_net_in" type="text" inputmode="decimal" autocomplete="off" placeholder="反推利率" style="width:150px">
</span>
<span style="display:inline-flex;align-items:center;gap:8px;">
<span style="font-size:12.5px;color:var(--text-2);">每10万扣息(元)</span>
<input id="dc_per10" type="text" inputmode="decimal" autocomplete="off" placeholder="反推利率" style="width:150px">
</span>
</div></div></div>
</div>
<div class="cr-actions">
<button type="button" class="btn btn--primary" id="dc_calc">计算</button>
<button type="button" class="btn btn--plain" id="dc_copy">复制结果</button>
<button type="button" class="btn btn--plain" id="dc_save">保存记录</button>
<button type="button" class="btn btn--plain" id="dc_reset">重置</button>
</div>
</div>
<div class="calc-side">
<div class="summary-card" id="dc_result">
<div class="section-title" style="margin:0 0 8px;">计算结果</div>
<div id="dc_result_body">
<div style="padding:6px 0 4px;">
<div style="font-size:11px;font-weight:700;letter-spacing:.12em;color:var(--gold)">实付金额</div>
<div class="calc-result-hero num" id="dc_out_net">—</div>
<div class="calc-caps-block">
<span class="caps-label">大写</span>
<span class="calc-caps" id="dc_out_cap">—</span>
<button type="button" class="caps-copy" id="dc_caps_copy">复制</button>
</div>
</div>
<div class="summary-row"><span>贴现利息</span><b class="num" id="dc_out_int">—</b></div>
<div class="summary-row"><span>贴现天数</span><b class="num"><span id="dc_out_days">—</span> <span style="font-weight:500;font-size:11px;color:var(--text-3);" id="dc_out_day_note"></span></b></div>
<div class="summary-row"><span>天数拆解</span><b class="num" id="dc_out_break" style="font-weight:600;font-size:12px;">—</b></div>
<div class="summary-row"><span>年化成本率</span><b class="num" id="dc_out_ann">—</b></div>
<div class="summary-row"><span>每10万扣息</span><b class="num" id="dc_out_p10">—</b></div>
<div class="summary-row" id="dc_row_shift" hidden><span>到期日顺延至</span><b class="num" id="dc_out_shift">—</b></div>
<div class="rev-line" id="dc_rev_line"></div>
<div class="calc-note" id="dc_out_note">填写左侧并点击「计算」后,结果区保持此骨架即时更新</div>
<div class="calc-formula num" id="dc_out_formula">票面 × 年利率 ÷ 360 × 天数 = 贴现利息</div>
<button type="button" class="btn btn--plain" id="dc_copy_side" style="width:100%;margin-top:12px;">复制结果</button>
</div>
</div>
<div class="editor-card">
<div class="section-title" style="margin-top:0;">本地历史记录</div>
<div class="calc-group-note" style="margin:-4px 0 6px;">保存在本浏览器,可对比多次试算(最多 30 条)</div>
<div id="dc_hist_list"></div>
<div style="margin-top:10px;"><button type="button" class="btn btn--plain btn--sm" id="dc_hist_clear">清空历史</button></div>
</div>
</div>
</div>
<script>
(function(){
var H2025=['2025-01-01','2025-01-28','2025-01-29','2025-01-30','2025-01-31','2025-02-01','2025-02-02','2025-02-03','2025-02-04','2025-04-04','2025-04-05','2025-04-06','2025-05-01','2025-05-02','2025-05-03','2025-05-04','2025-05-05','2025-05-31','2025-06-01','2025-06-02','2025-10-01','2025-10-02','2025-10-03','2025-10-04','2025-10-05','2025-10-06','2025-10-07','2025-10-08'];
var H2026=['2026-01-01','2026-01-02','2026-01-03','2026-02-15','2026-02-16','2026-02-17','2026-02-18','2026-02-19','2026-02-20','2026-02-21','2026-02-22','2026-02-23','2026-04-04','2026-04-05','2026-04-06','2026-05-01','2026-05-02','2026-05-03','2026-05-04','2026-05-05','2026-06-19','2026-06-20','2026-06-21','2026-09-25','2026-09-26','2026-09-27','2026-10-01','2026-10-02','2026-10-03','2026-10-04','2026-10-05','2026-10-06','2026-10-07'];
var HSET={}; [].concat(H2025,H2026).forEach(function(x){HSET[x]=1;});
function pad(n){return (n<10?'0':'')+n;}
function ymd(d){return d.getFullYear()+'-'+pad(d.getMonth()+1)+'-'+pad(d.getDate());}
function parseYmd(s){
var p=s.split('-'); if(p.length!==3) return null;
var d=new Date(+p[0],+p[1]-1,+p[2]); return isNaN(d.getTime())?null:d;
}
function isWeekend(d){var w=d.getDay(); return w===0||w===6;}
function isHoliday(d){return !!HSET[ymd(d)];}
function isNonWork(d){return isWeekend(d)||isHoliday(d);}
function nextWorkdayFrom(ymdStr){
var d=parseYmd(ymdStr); if(!d) return ymdStr;
for(var i=0;i<20;i++){
if(!isNonWork(d)) return ymd(d);
d.setDate(d.getDate()+1);
}
return ymd(d);
}
function addMonthsKeepDom(y,m,day,addM){
var nm=m-1+addM, ny=y+Math.floor(nm/12), nmo=((nm%12)+12)%12;
var last=new Date(ny,nmo+1,0).getDate();
var nd=Math.min(day,last);
return new Date(ny,nmo,nd);
}
function toast(msg){ if(window.fusionToast) window.fusionToast(msg); else alert(msg); }
function copyText(t){
if(!t) return;
if(navigator.clipboard) navigator.clipboard.writeText(t).then(function(){toast('已复制');},function(){alert(t);});
else alert(t);
}
function syncBillHint(){
var v=document.querySelector('input[name="dc_bill"]:checked').value;
document.getElementById('dc_risk_hint').textContent=v==='bank'?'银承:流通性较好,贴现利率通常低于商承。':'商承:请重点评估承兑人信用;贴现利率通常高于商承。';
}
document.querySelectorAll('input[name="dc_bill"]').forEach(function(r){r.addEventListener('change',syncBillHint);});
syncBillHint();
function parseNum(s){s=String(s||'').replace(/,/g,'').trim(); if(s==='')return NaN; var v=parseFloat(s); return isFinite(v)?v:NaN;}
function fmtMoney(n){if(!isFinite(n))return''; var p=(Math.round(n*100)/100).toFixed(2).split('.'); p[0]=p[0].replace(/\B(?=(\d{3})+(?!\d))/g,','); return p.join('.');}
var DIG='零壹贰叁肆伍陆柒捌玖', UNIT=['','拾','佰','仟'];
function sectionFour(n){var s='',str=('0000'+n).slice(-4); for(var i=0;i<4;i++){var d=+str[i]; if(d===0){s+=DIG[0];continue;} s=s.replace(/零+$/,''); s+=DIG[d]+UNIT[3-i];} return (s.replace(/零+/g,'零').replace(/零$/,''))||DIG[0];}
function toRmbUpper(x){if(!isFinite(x)||x<=0)return''; var yi=Math.floor(x/1e8),wan=Math.floor((x%1e8)/1e4),ge=Math.floor(x%1e4); var out=''; if(yi>0)out+=sectionFour(yi)+'亿'; if(wan>0||(yi>0&&ge>0))out+=sectionFour(wan)+'万'; return (out+sectionFour(ge)+'元整').replace(/^壹拾/g,'拾');}
function syncFace(){var v=parseNum(document.getElementById('dc_face').value); document.getElementById('dc_face_fmt').textContent=isFinite(v)&&v>0?('格式化:'+fmtMoney(v)):'';
document.getElementById('dc_face_cap').textContent=isFinite(v)&&v>0?('大写:'+toRmbUpper(Math.floor(v))):'';}
document.getElementById('dc_face').addEventListener('input',syncFace);
function syncMatRows(){
var m=document.querySelector('input[name="dc_mat_mode"]:checked').value;
document.getElementById('dc_row_mat_date').hidden=m!=='date';
document.getElementById('dc_row_mat_after').hidden=m!=='after';
}
document.querySelectorAll('input[name="dc_mat_mode"]').forEach(function(r){
r.addEventListener('change',syncMatRows);
});
document.getElementById('dc_apply_months').addEventListener('click',function(){
var iss=document.getElementById('dc_issue').value; if(!iss){alert('请先填出票日');return;}
var mo=parseInt(document.getElementById('dc_months').value,10); if(!(mo>=1&&mo<=12)){alert('期限月数须为1-12');return;}
var p=iss.split('-').map(Number); var d=addMonthsKeepDom(p[0],p[1],p[2],mo);
document.getElementById('dc_maturity').value=ymd(d);
document.getElementById('dc_mat_preview').textContent='推算到期:'+ymd(d);
});
document.querySelectorAll('[data-ref]').forEach(function(btn){
btn.addEventListener('click',function(){ document.getElementById('dc_rate').value=this.getAttribute('data-ref'); });
});
if(!document.getElementById('dc_discount').value){
var t=new Date(); document.getElementById('dc_discount').value=ymd(t);
}
function effectiveMaturity(){
var m=document.querySelector('input[name="dc_mat_mode"]:checked').value;
var mat=document.getElementById('dc_maturity').value;
if(m==='after' && !mat){ alert('请先「推算到期日」或改选指定到期日'); return null; }
if(!mat){ alert('请填写到期日'); return null; }
if(document.getElementById('dc_holiday').checked) return nextWorkdayFrom(mat);
return mat;
}
function discountDays(dis, matEff, rule, remote){
var d0=parseYmd(dis), d1=parseYmd(matEff);
if(!d0||!d1||d1<=d0) return NaN;
var diff=Math.round((d1-d0)/86400000);
var base=rule==='inclusive'?diff+1:diff;
if(base<0) return NaN;
var extra=remote?3:0;
return base+extra;
}
function calcForward(face,ratePct,days){
return face*(ratePct/100)*(days/360);
}
function rateFromNet(face,net,days){
if(!(face>0)||!(net>0)||!(days>0)||net>=face) return NaN;
return (face-net)*360/(face*days)*100;
}
function clearResult(){
document.getElementById('dc_out_net').textContent='—';
document.getElementById('dc_out_cap').textContent='—';
document.getElementById('dc_out_int').textContent='—';
document.getElementById('dc_out_days').textContent='—';
document.getElementById('dc_out_day_note').textContent='';
document.getElementById('dc_out_break').textContent='—';
document.getElementById('dc_out_ann').textContent='—';
document.getElementById('dc_out_p10').textContent='—';
document.getElementById('dc_row_shift').hidden=true;
document.getElementById('dc_rev_line').textContent='';
document.getElementById('dc_out_note').textContent='填写左侧并点击「计算」后,结果区保持此骨架即时更新';
document.getElementById('dc_out_formula').textContent='票面 × 年利率 ÷ 360 × 天数 = 贴现利息';
window._dc_last=null;
}
function calc(){
document.getElementById('dc_rev_line').textContent='';
var face=parseNum(document.getElementById('dc_face').value);
var dis=document.getElementById('dc_discount').value;
var matRaw=document.getElementById('dc_maturity').value;
var matEff=effectiveMaturity();
var rule=document.getElementById('dc_day_rule').value;
var remote=document.getElementById('dc_remote').checked;
var holiday=document.getElementById('dc_holiday').checked;
var days=discountDays(dis,matEff,rule,remote);
if(!(face>0)){ alert('票面金额须大于0'); return; }
if(!dis||!matEff){ alert('请完善贴现日与到期日'); return; }
if(!isFinite(days)||days<=0){ alert('贴现日须早于到期日,且计息天数须为正'); return; }
var netIn=parseNum(document.getElementById('dc_net_in').value);
var per10=parseNum(document.getElementById('dc_per10').value);
var rate=parseNum(document.getElementById('dc_rate').value);
var intr;
var revLine='';
if(isFinite(netIn)&&netIn>0&&netIn<face){
rate=rateFromNet(face,netIn,days);
if(!isFinite(rate)||!(rate>0)){ alert('无法根据实付金额反推利率,请检查天数与金额'); return; }
intr=face-netIn;
document.getElementById('dc_rate').value=rate.toFixed(4);
revLine='已按实付金额反推贴现年利率:'+rate.toFixed(4)+'%';
} else if(isFinite(per10)&&per10>0){
intr=per10*(face/100000);
rate=(intr*360/(face*days))*100;
if(!isFinite(rate)||!(rate>0)){ alert('无法根据每10万扣息反推利率'); return; }
document.getElementById('dc_rate').value=rate.toFixed(4);
revLine='已按每10万扣息反推贴现年利率:'+rate.toFixed(4)+'%';
} else {
if(!(rate>0)){ alert('请输入贴现年利率,或填写反推字段'); return; }
intr=calcForward(face,rate,days);
}
var net=face-intr;
var ann=net>0 ? (intr/net)*(360/days)*100 : NaN;
var p10=face>0? intr/(face/100000):NaN;
var extra=remote?3:0;
var base=days-extra;
var shiftDays=0;
if(holiday && matRaw && matEff){
var dRaw=parseYmd(matRaw), dEff=parseYmd(matEff);
if(dRaw&&dEff) shiftDays=Math.max(0, Math.round((dEff-dRaw)/86400000));
}
var dayNote=(remote?'(含异地+3天)':'')+(holiday?';到期日已按工作日顺延':'');
var bill=document.querySelector('input[name="dc_bill"]:checked').value==='commercial'?'商业承兑汇票':'银行承兑汇票';
document.getElementById('dc_out_net').textContent=fmtMoney(net);
document.getElementById('dc_out_cap').textContent=toRmbUpper(Math.floor(net))||'—';
document.getElementById('dc_out_int').textContent=fmtMoney(intr)+' 元';
document.getElementById('dc_out_days').textContent=String(days)+' 天';
document.getElementById('dc_out_day_note').textContent=dayNote;
document.getElementById('dc_out_break').textContent='计息 '+base+(shiftDays?('(含顺延 '+shiftDays+')'):'')+(remote?' + 异地 3':'');
document.getElementById('dc_out_ann').textContent=isFinite(ann)?(ann.toFixed(4)+'%'):'—';
document.getElementById('dc_out_p10').textContent=isFinite(p10)?(fmtMoney(p10)+' 元'):'—';
document.getElementById('dc_row_shift').hidden=!shiftDays;
if(shiftDays) document.getElementById('dc_out_shift').textContent=matEff;
document.getElementById('dc_rev_line').textContent=revLine;
document.getElementById('dc_out_note').textContent='票据类型:'+bill+' · '+(rule==='inclusive'?'记首记尾':'记首不记尾')+' · 360 天/年';
document.getElementById('dc_out_formula').textContent=fmtMoney(face)+' × '+(+rate).toFixed(4)+'% ÷ 360 × '+days+' 天 = '+fmtMoney(intr);
window._dc_last={face:face,intr:intr,net:net,days:days,w:face/10000,rate:(+rate).toFixed(4),
text:'票面'+(face/10000).toFixed(2)+'万 | 贴现息'+fmtMoney(intr)+'元 | 实付'+fmtMoney(net)+'元 | 天数'+days+'天'};
}
function copyRes(){
if(!window._dc_last){ alert('请先计算'); return; }
copyText(window._dc_last.text);
}
function saveHist(){
if(!window._dc_last){ alert('请先计算'); return; }
var key='dc_hist_v1', arr=JSON.parse(localStorage.getItem(key)||'[]');
arr.unshift({t:Date.now(),face:window._dc_last.face,intr:window._dc_last.intr,net:window._dc_last.net,days:window._dc_last.days,rate:window._dc_last.rate});
arr=arr.slice(0,30); localStorage.setItem(key,JSON.stringify(arr)); renderHist();
}
function renderHist(){
var arr=JSON.parse(localStorage.getItem('dc_hist_v1')||'[]');
var box=document.getElementById('dc_hist_list');
if(!arr.length){
box.innerHTML='<div class="calc-note" style="padding:10px 0 4px;">暂无历史记录,计算后点击「保存记录」</div>';
return;
}
box.innerHTML=arr.map(function(r,i){
var wan=((r.face||0)/10000).toFixed(2);
var when=new Date(r.t);
var ts=isNaN(when.getTime())?String(r.t):when.toLocaleString();
return '<div class="hist-row"><div><div class="hist-main num">'+wan+' 万 · '+(r.rate||'')+'%</div><div class="hist-sub num">'+ts+' · '+(r.days||'')+' 天</div></div><div class="hist-amt num">'+fmtMoney(r.net)+'</div><button type="button" class="btn btn--plain btn--sm" data-hist-fill="'+i+'">回填</button></div>';
}).join('');
}
document.getElementById('dc_hist_list').addEventListener('click',function(e){
var b=e.target.closest('[data-hist-fill]');
if(!b) return;
var arr=JSON.parse(localStorage.getItem('dc_hist_v1')||'[]');
var r=arr[+b.getAttribute('data-hist-fill')];
if(!r) return;
if(r.face) document.getElementById('dc_face').value=String(r.face);
if(r.rate) document.getElementById('dc_rate').value=String(r.rate);
syncFace();
});
document.getElementById('dc_calc').addEventListener('click',calc);
document.getElementById('dc_copy').addEventListener('click',copyRes);
document.getElementById('dc_copy_side').addEventListener('click',copyRes);
document.getElementById('dc_caps_copy').addEventListener('click',function(){
var t=document.getElementById('dc_out_cap').textContent;
if(!t||t==='—'){ alert('请先计算'); return; }
copyText(t);
});
document.getElementById('dc_save').addEventListener('click',saveHist);
document.getElementById('dc_hist_clear').addEventListener('click',function(){ localStorage.removeItem('dc_hist_v1'); renderHist(); });
document.getElementById('dc_reset').addEventListener('click',function(){
document.getElementById('dc_face').value=''; document.getElementById('dc_rate').value='';
document.getElementById('dc_issue').value=''; document.getElementById('dc_maturity').value='';
document.getElementById('dc_net_in').value=''; document.getElementById('dc_per10').value='';
syncFace(); clearResult();
});
renderHist();
})();
</script>