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>
244 lines
13 KiB
PHP
244 lines
13 KiB
PHP
<?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('interest_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"><label for="ic_principal">本金(元)</label><div class="cr-ctl">
|
||
<input id="ic_principal" type="text" inputmode="decimal" autocomplete="off" placeholder="如 1000000" style="max-width:230px">
|
||
<div class="cr-hint"><span id="ic_principal_fmt"></span> <span id="ic_principal_cap"></span></div>
|
||
</div></div>
|
||
<div class="cr"><label for="ic_rate">利率数值</label><div class="cr-ctl"><div class="cr-inline">
|
||
<input id="ic_rate" type="text" inputmode="decimal" style="width:110px">
|
||
<select id="ic_rate_kind" aria-label="利率种类" style="width:126px">
|
||
<option value="annual">年利率 %</option>
|
||
<option value="monthly">月利率 %</option>
|
||
<option value="daily">日利率 %</option>
|
||
</select>
|
||
</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="opt-row">
|
||
<label><input type="radio" name="ic_day_mode" value="manual" checked> 模式A:直接输入天数</label>
|
||
<label><input type="radio" name="ic_day_mode" value="range"> 模式B:起止日期</label>
|
||
</div></div></div>
|
||
<div class="cr" id="ic_row_manual"><label for="ic_days">天数</label><div class="cr-ctl"><div class="cr-inline">
|
||
<input id="ic_days" type="number" min="0" step="1" style="width:110px" placeholder="正整数">
|
||
<span class="cr-hint" style="margin:0;" id="ic_days_hint">正整数;与起止日期互斥</span>
|
||
</div></div></div>
|
||
<div class="cr" id="ic_row_range" hidden><span class="cr-label">起止日期</span><div class="cr-ctl"><div class="cr-inline">
|
||
<input id="ic_d1" type="date">
|
||
<span style="color:var(--text-3);">至</span>
|
||
<input id="ic_d2" type="date">
|
||
<span class="cr-hint" style="margin:0;" id="ic_range_days_label"></span>
|
||
</div></div></div>
|
||
</div>
|
||
<div class="cr-group">
|
||
<div class="section-title">计息口径</div>
|
||
<div class="cr"><label for="ic_count_mode">结息方式</label><div class="cr-ctl">
|
||
<select id="ic_count_mode" style="max-width:320px">
|
||
<option value="exclusive" selected>记首不记尾(算头不算尾)</option>
|
||
<option value="inclusive">记首记尾(算头又算尾,天数+1)</option>
|
||
</select>
|
||
</div></div>
|
||
<div class="cr"><label for="ic_basis">计息基准</label><div class="cr-ctl">
|
||
<select id="ic_basis" style="max-width:320px">
|
||
<option value="360" selected>360 天/年(银行常用)</option>
|
||
<option value="365">365 天/年</option>
|
||
</select>
|
||
</div></div>
|
||
</div>
|
||
<div class="cr-actions">
|
||
<button type="button" class="btn btn--primary" id="ic_calc">计算</button>
|
||
<button type="button" class="btn btn--plain" id="ic_copy">一键复制结果</button>
|
||
<button type="button" class="btn btn--plain" id="ic_reset">重置</button>
|
||
</div>
|
||
</div>
|
||
<div class="calc-side">
|
||
<div class="summary-card" id="ic_result_wrap">
|
||
<div class="section-title" style="margin:0 0 8px;">计算结果</div>
|
||
<div id="ic_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="ic_out_interest">—</div>
|
||
<div class="calc-caps-block" id="ic_caps_block">
|
||
<span class="caps-label">大写</span>
|
||
<span class="calc-caps" id="ic_out_cap">—</span>
|
||
<button type="button" class="caps-copy" id="ic_caps_copy">复制</button>
|
||
</div>
|
||
</div>
|
||
<div class="summary-row"><span>本息合计</span><b class="num" id="ic_out_total">—</b></div>
|
||
<div class="summary-row"><span>实际计息天数</span><b class="num" id="ic_out_days">—</b></div>
|
||
<div class="summary-row"><span>参考日利率</span><b class="num" id="ic_out_daily" style="font-weight:600;font-size:12px;">—</b></div>
|
||
<div class="calc-note" id="ic_out_note">填写左侧数据并点击「计算」后,结果区保持此骨架即时更新</div>
|
||
<div class="calc-formula num" id="ic_out_formula">本金 × 利率 ÷ 基准 × 天数</div>
|
||
<button type="button" class="btn btn--plain" id="ic_copy_side" style="width:100%;margin-top:12px;">复制结果</button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<script>
|
||
(function(){
|
||
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];
|
||
}
|
||
s=s.replace(/零+/g,'零').replace(/零$/,'');
|
||
return s||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)+'万';
|
||
out+=sectionFour(ge)+'元整';
|
||
return out.replace(/^壹拾/g,'拾');
|
||
}
|
||
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('.');
|
||
}
|
||
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 clearResult(){
|
||
document.getElementById('ic_out_interest').textContent='—';
|
||
document.getElementById('ic_out_cap').textContent='—';
|
||
document.getElementById('ic_out_total').textContent='—';
|
||
document.getElementById('ic_out_days').textContent='—';
|
||
document.getElementById('ic_out_daily').textContent='—';
|
||
document.getElementById('ic_out_note').textContent='填写左侧数据并点击「计算」后,结果区保持此骨架即时更新';
|
||
document.getElementById('ic_out_formula').textContent='本金 × 利率 ÷ 基准 × 天数';
|
||
window._ic_last=null;
|
||
}
|
||
function syncPrincipalFmt(){
|
||
var raw=document.getElementById('ic_principal').value;
|
||
var v=parseNum(raw);
|
||
document.getElementById('ic_principal_fmt').textContent=isFinite(v)&&v>0?('格式化:'+fmtMoney(v)+' 元'):'';
|
||
document.getElementById('ic_principal_cap').textContent=isFinite(v)&&v>0?('大写:'+toRmbUpper(Math.floor(v))):'';
|
||
}
|
||
function daysFromRange(d1,d2,mode){
|
||
if(!d1||!d2) return NaN;
|
||
var t0=new Date(d1+'T12:00:00').getTime(), t1=new Date(d2+'T12:00:00').getTime();
|
||
if(t1<t0) return NaN;
|
||
var diff=Math.round((t1-t0)/86400000);
|
||
return mode==='inclusive'?diff+1:diff;
|
||
}
|
||
function getEffectiveDays(){
|
||
var mode=document.querySelector('input[name="ic_day_mode"]:checked').value;
|
||
var cm=document.getElementById('ic_count_mode').value;
|
||
if(mode==='manual'){
|
||
var n=parseInt(document.getElementById('ic_days').value,10);
|
||
return isFinite(n)&&n>=0?n:NaN;
|
||
}
|
||
return daysFromRange(document.getElementById('ic_d1').value,document.getElementById('ic_d2').value,cm);
|
||
}
|
||
function dailyRef(Rk,rate,basis){
|
||
var b=+basis;
|
||
if(Rk==='annual') return (rate/100)/b;
|
||
if(Rk==='monthly') return (rate/100)/30;
|
||
return rate/100;
|
||
}
|
||
function interestAmount(P,Rk,rate,days,basis){
|
||
if(!(P>0)||!(days>=0)||!(rate>0)) return NaN;
|
||
var b=+basis;
|
||
if(Rk==='annual') return P*(rate/100)*(days/b);
|
||
if(Rk==='monthly') return P*(rate/100)*(days/30);
|
||
return P*(rate/100)*days;
|
||
}
|
||
function resultText(){
|
||
return '利息 '+document.getElementById('ic_out_interest').textContent+' | 本息合计 '+document.getElementById('ic_out_total').textContent+' | 天数 '+document.getElementById('ic_out_days').textContent;
|
||
}
|
||
function calc(){
|
||
var P=parseNum(document.getElementById('ic_principal').value);
|
||
var rate=parseNum(document.getElementById('ic_rate').value);
|
||
var Rk=document.getElementById('ic_rate_kind').value;
|
||
var basis=document.getElementById('ic_basis').value;
|
||
var days=getEffectiveDays();
|
||
if(!(P>0)){ alert('请输入有效本金'); return; }
|
||
if(!(rate>0)){ alert('请输入有效利率'); return; }
|
||
if(!isFinite(days)||days<0){ alert('请检查计息天数或起止日期'); return; }
|
||
var intr=interestAmount(P,Rk,rate,days,basis);
|
||
var dref=dailyRef(Rk,rate,basis);
|
||
var cm=document.getElementById('ic_count_mode').value;
|
||
var kindTxt={annual:'年利率',monthly:'月利率',daily:'日利率'}[Rk];
|
||
var formula=Rk==='annual'?(fmtMoney(P)+' × '+rate+'% ÷ '+basis+' × '+days+' 天'):(Rk==='monthly'?(fmtMoney(P)+' × '+rate+'% ÷ 30 × '+days+' 天'):(fmtMoney(P)+' × '+rate+'% × '+days+' 天'));
|
||
document.getElementById('ic_out_interest').textContent=fmtMoney(intr)+' 元';
|
||
document.getElementById('ic_out_total').textContent=fmtMoney(P+intr)+' 元';
|
||
document.getElementById('ic_out_days').textContent=String(days);
|
||
document.getElementById('ic_out_daily').textContent=(dref*100).toFixed(6)+'%';
|
||
document.getElementById('ic_out_cap').textContent=toRmbUpper(Math.floor(intr))||'—';
|
||
document.getElementById('ic_out_note').textContent='参考日利率按当前利率种类换算,仅供参考 · '+kindTxt+' '+rate+'% · '+(cm==='inclusive'?'记首记尾':'记首不记尾')+' · '+basis+' 天/年';
|
||
document.getElementById('ic_out_formula').textContent=formula;
|
||
window._ic_last={text:resultText()};
|
||
}
|
||
function resetAll(){
|
||
document.getElementById('ic_principal').value='';
|
||
document.getElementById('ic_rate').value='';
|
||
document.getElementById('ic_days').value='';
|
||
document.getElementById('ic_d1').value='';
|
||
document.getElementById('ic_d2').value='';
|
||
syncPrincipalFmt();
|
||
clearResult();
|
||
}
|
||
document.getElementById('ic_principal').addEventListener('input',syncPrincipalFmt);
|
||
document.getElementById('ic_principal').addEventListener('paste',function(){setTimeout(syncPrincipalFmt,0);});
|
||
document.querySelectorAll('input[name="ic_day_mode"]').forEach(function(r){
|
||
r.addEventListener('change',function(){
|
||
var m=document.querySelector('input[name="ic_day_mode"]:checked').value;
|
||
document.getElementById('ic_row_manual').hidden=m!=='manual';
|
||
document.getElementById('ic_row_range').hidden=m!=='range';
|
||
});
|
||
});
|
||
function syncRangeLabel(){
|
||
var cm=document.getElementById('ic_count_mode').value;
|
||
var d=daysFromRange(document.getElementById('ic_d1').value,document.getElementById('ic_d2').value,cm);
|
||
document.getElementById('ic_range_days_label').textContent=isFinite(d)?('→ 计息天数 '+d):'';
|
||
}
|
||
['ic_d1','ic_d2','ic_count_mode'].forEach(function(id){
|
||
var el=document.getElementById(id);
|
||
if(el) el.addEventListener('change',syncRangeLabel);
|
||
});
|
||
document.getElementById('ic_days').addEventListener('input',function(){
|
||
if(this.value && document.getElementById('ic_d1').value){ document.getElementById('ic_d1').value=''; document.getElementById('ic_d2').value=''; document.getElementById('ic_range_days_label').textContent=''; }
|
||
});
|
||
document.getElementById('ic_d1').addEventListener('change',function(){ if(this.value) document.getElementById('ic_days').value=''; });
|
||
document.getElementById('ic_d2').addEventListener('change',function(){ if(this.value) document.getElementById('ic_days').value=''; });
|
||
document.getElementById('ic_calc').addEventListener('click',calc);
|
||
document.getElementById('ic_copy').addEventListener('click',function(){ if(!window._ic_last){ alert('请先计算'); return; } copyText(window._ic_last.text); });
|
||
document.getElementById('ic_copy_side').addEventListener('click',function(){ if(!window._ic_last){ alert('请先计算'); return; } copyText(window._ic_last.text); });
|
||
document.getElementById('ic_caps_copy').addEventListener('click',function(){
|
||
var t=document.getElementById('ic_out_cap').textContent;
|
||
if(!t||t==='—'){ alert('请先计算'); return; }
|
||
copyText(t);
|
||
});
|
||
document.getElementById('ic_reset').addEventListener('click',resetAll);
|
||
})();
|
||
</script>
|