Files
weiguang-jewelry-ledger/public/index.html

325 lines
20 KiB
HTML
Raw Permalink 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.
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>麦麦黄金台账</title>
<style>
*{margin:0;padding:0;box-sizing:border-box}
body{font-family:"Microsoft YaHei","PingFang SC",sans-serif;background:#eef1f5;color:#1f2328;padding:18px}
.wrap{max-width:1180px;margin:0 auto}
h1{font-size:21px;margin-bottom:14px}
h1 small{font-size:13px;color:#7a828c;font-weight:400;margin-left:10px}
.bar{background:#fff;border-radius:10px;padding:14px 18px;margin-bottom:14px;display:flex;flex-wrap:wrap;gap:10px 26px;align-items:center;border:1px solid #e3e6ea}
.bar b{color:#1a7f37;font-size:17px}
.bar .p{color:#8a9099;font-size:12px}
.bar .sep{width:1px;height:26px;background:#e3e6ea}
.tabs{display:flex;gap:8px;margin-bottom:14px}
.tabs button{padding:11px 26px;border:0;border-radius:9px 9px 0 0;background:#dde3ea;color:#4a5560;font-size:15px;cursor:pointer;font-family:inherit}
.tabs button.on{background:#2f6feb;color:#fff;font-weight:600}
.panel{background:#fff;border-radius:0 10px 10px 10px;padding:22px 24px;border:1px solid #e3e6ea;display:none}
.panel.on{display:block}
.grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(210px,1fr));gap:14px}
.f label{display:block;font-size:13px;color:#57606a;margin-bottom:5px}
input,select{width:100%;padding:10px 12px;border:1px solid #cfd6dd;border-radius:7px;font-size:15px;font-family:inherit;background:#fbfcfd}
input:focus,select:focus{outline:2px solid #a9c5f5;background:#fff}
input[readonly]{background:#f0f3f7;color:#4a5560}
.chk{display:flex;flex-wrap:wrap;gap:8px;margin-top:4px}
.chk label{display:flex;align-items:center;gap:5px;font-size:14px;padding:8px 13px;border:1px solid #cfd6dd;border-radius:18px;background:#fbfcfd;cursor:pointer;margin:0}
.chk input{width:auto;padding:0}
.calc{background:#eef4ff;border:1px solid #c3d6f7;border-radius:9px;padding:14px 18px;margin:18px 0}
.calc div{display:flex;justify-content:space-between;padding:4px 0;font-size:14px}
.calc .big{font-size:19px;font-weight:700;border-top:1px dashed #c3d6f7;margin-top:8px;padding-top:10px;color:#1a7f37}
button.go{width:100%;padding:15px;border:0;border-radius:9px;background:#2f6feb;color:#fff;font-size:16px;font-weight:600;cursor:pointer;font-family:inherit}
button.go.green{background:#1a7f37}
button.go:disabled{background:#a8b3c0;cursor:not-allowed}
button.go:hover:not(:disabled){filter:brightness(1.08)}
.msg{margin-top:14px;padding:13px 16px;border-radius:8px;font-size:14px;line-height:1.7;display:none;white-space:pre-wrap}
.msg.ok{display:block;background:#e6f4ea;border:1px solid #a8d5b5;color:#116329}
.msg.err{display:block;background:#fdeaea;border:1px solid #f0b0b0;color:#a51414}
.hint{color:#8a9099;font-size:12px;margin-top:6px;line-height:1.6}
.report-head{display:flex;gap:12px;align-items:end;flex-wrap:wrap;margin-bottom:18px}
.report-head .f{max-width:220px;flex:1}
.report-head button{width:auto;padding:11px 20px}
.report-status{padding:16px;border-radius:8px;background:#eef4ff;line-height:1.7;margin:16px 0}
.report-status.bad{background:#fdeaea;color:#a51414}
.report-status.warn{background:#fff5da;color:#6c4a05}
.report-status.good{background:#e6f4ea;color:#116329}
.cards{display:grid;grid-template-columns:repeat(auto-fit,minmax(180px,1fr));gap:12px;margin:14px 0}
.card{background:#f7f9fc;border:1px solid #e3e6ea;border-radius:8px;padding:15px}
.card span{display:block;font-size:13px;color:#57606a}.card strong{display:block;font-size:23px;margin-top:8px;font-variant-numeric:tabular-nums}
.report h2{font-size:17px;margin:24px 0 10px}.report p{font-size:14px;line-height:1.8}
.report .issue{padding:13px 15px;border-left:4px solid #d29922;background:#fff9eb;margin:10px 0;overflow-wrap:anywhere}
.report .issue.bad{border-color:#cf222e;background:#fff2f2}
.report small{display:block;color:#57606a;line-height:1.7;margin-top:5px}
.table-wrap{overflow-x:auto;margin:12px 0}table{width:100%;border-collapse:collapse;font-size:13px}
th,td{text-align:left;border-bottom:1px solid #e3e6ea;padding:10px;vertical-align:top;min-width:85px}
th{background:#f4f6f8;white-space:nowrap}td{font-variant-numeric:tabular-nums}td.note{min-width:230px;overflow-wrap:anywhere}
.report details{margin-top:18px}.report summary{cursor:pointer;font-size:15px;font-weight:600}.report a{color:#245cc4}
.status-error{color:#a51414}.status-warn{color:#8a6200}.status-ok{color:#116329}
@media(max-width:600px){body{padding:10px}.panel{padding:16px}.tabs button{padding:11px 13px}.report-head .f{max-width:none}.grid .f[style]{grid-column:auto!important}}
</style>
</head>
<body>
<div class="wrap">
<h1>麦麦黄金台账 <small id="day"></small></h1>
<div class="bar">
<div><span class="p">当前料价(均价)</span><br><b id="avg">—</b> <span class="p">元/克</span></div>
<div class="sep"></div>
<div><span class="p">黄金总克重</span><br><b id="total">—</b> <span class="p">克</span></div>
<div class="sep"></div>
<div id="pockets" class="p"></div>
</div>
<div class="tabs">
<button id="t1" class="on" onclick="tab(1)">① 入库</button>
<button id="t2" onclick="tab(2)">② 开单</button>
<button id="t3" onclick="tab(3)">③ 对账报告</button>
</div>
<!-- ========== 入库 ========== -->
<div class="panel on" id="p1">
<div class="grid">
<div class="f"><label>日期</label><input type="date" id="b_date"></div>
<div class="f"><label>入到哪个货盘</label>
<select id="b_pocket"><option>现货</option><option>金料</option><option>找金</option><option>回收旧料</option><option>君宝汇</option></select>
</div>
<div class="f"><label>克重(克)</label><input type="number" step="0.001" id="b_g" placeholder="如 30"></div>
<div class="f"><label>料价(元/克)</label><input type="number" step="0.01" id="b_price" placeholder="如 910.81"></div>
<div class="f"><label>备注</label><input id="b_note" placeholder="如 老陈那边进的料"></div>
</div>
<div class="hint">入库后均价自动重算:<b id="b_preview">—</b></div>
<br>
<button class="go green" id="b_go" onclick="doBuy()">入 库</button>
<div class="msg" id="b_msg"></div>
</div>
<!-- ========== 开单 ========== -->
<div class="panel" id="p2">
<div class="grid">
<div class="f"><label>日期</label><input type="date" id="s_date"></div>
<div class="f"><label>出哪个货盘</label>
<select id="s_pocket"><option>现货</option><option>金料</option><option>找金</option><option>回收旧料</option><option>君宝汇</option></select>
</div>
<div class="f"><label>客户(可空)</label><input id="s_cust" placeholder="如 小羊"></div>
<div class="f"><label>克重(克)</label><input type="number" step="0.001" id="s_g" value="1"></div>
<div class="f"><label>料价(自动带出,可改)</label><input type="number" step="0.01" id="s_price"></div>
</div>
<div class="grid" style="margin-top:14px">
<div class="f"><label>工费(元/克)</label><input type="number" step="0.01" id="s_labor" value="0"></div>
<div class="f"><label>工费固定额(元)</label><input type="number" step="0.01" id="s_fix" value="0"></div>
<div class="f"><label>附加 · 其他(元)</label><input type="number" step="0.01" id="s_other" value="0" placeholder="黄水晶、银链等"></div>
<div class="f"><label>售价(元)</label><input type="number" step="0.01" id="s_amt" value="0"></div>
</div>
<div class="f" style="margin-top:14px">
<label>附加项(打勾)</label>
<div class="chk">
<label><input type="checkbox" class="add" data-v="4">证书 4</label>
<label><input type="checkbox" class="add" data-v="3">盒子 3</label>
<label><input type="checkbox" class="add" data-v="7">编绳 7</label>
<label><input type="checkbox" class="add" data-v="10">手机贴 10</label>
<label><input type="checkbox" class="add" data-v="8">运费 8</label>
</div>
</div>
<div class="grid" style="margin-top:14px">
<div class="f"><label>收款方式</label>
<select id="s_pay"><option>微信</option><option>支付宝</option><option>现金</option><option>未收款</option></select>
</div>
<div class="f" style="grid-column:span 2"><label>备注</label><input id="s_note" placeholder="如 羊款、含黄水晶"></div>
</div>
<div class="calc">
<div><span>料成本</span><span id="c_mat">—</span></div>
<div><span>工费</span><span id="c_lab">—</span></div>
<div><span>附加</span><span id="c_add">—</span></div>
<div><span>成本总计</span><span id="c_cost">—</span></div>
<div class="big"><span>利润</span><span id="c_profit">—</span></div>
</div>
<button class="go" id="s_go" onclick="doSale()">开单并写入飞书</button>
<div class="msg" id="s_msg"></div>
</div>
<div class="panel" id="p3">
<div class="report-head">
<div class="f"><label for="r_date">对账日期</label><input type="date" id="r_date"></div>
<button class="go" id="r_go" onclick="loadReport()">重新核对</button>
<button class="go green" id="r_save" onclick="saveReport()" disabled>保存报告</button>
</div>
<p class="hint">每次打开对账页都会读取飞书最新记录。只核对并列出差异,不改账。历史日期自动读取对应月份的表。</p>
<div class="msg" id="r_msg" role="status" aria-live="polite"></div>
<div class="report" id="r_result"></div>
</div>
</div>
<script>
let ST = { 均价: 0 };
const $ = id => document.getElementById(id);
const num = id => Number($(id).value) || 0;
function tab(n){
[1,2,3].forEach(i=>{ $('t'+i).className = n===i?'on':''; $('p'+i).className = 'panel'+(n===i?' on':''); });
if(n===3) loadReport();
}
async function load(){
const r = await fetch('/api/state').then(x=>x.json());
if(!r.ok){ alert('读取失败:'+r.msg); return; }
ST = r;
$('day').textContent = r.今天;
$('avg').textContent = r.均价.toFixed(2);
$('total').textContent = r.总克重.toFixed(3);
$('pockets').innerHTML = Object.entries(r.库存).filter(([k,v])=>v!==0)
.map(([k,v])=>`${k} <b style="font-size:14px">${v.toFixed(2)}</b>`).join(' | ');
$('b_date').value = r.今天; $('s_date').value = r.今天;
$('s_price').value = r.均价.toFixed(2);
$('b_price').value = r.均价.toFixed(2);
calc(); previewBuy();
}
function 附加合计(){
let s = 0;
document.querySelectorAll('.add').forEach(c=>{ if(c.checked) s += Number(c.dataset.v); });
return s;
}
function calc(){
const g = num('s_g'), lp = num('s_price');
const mat = g*lp, lab = g*num('s_labor') + num('s_fix');
const add = 附加合计() + num('s_other');
const cost = mat+lab+add, amt = num('s_amt');
$('c_mat').textContent = `${g} × ${lp} = ${mat.toFixed(2)}`;
$('c_lab').textContent = lab.toFixed(2);
$('c_add').textContent = add.toFixed(2);
$('c_cost').textContent = cost.toFixed(2);
const p = amt - cost;
$('c_profit').textContent = `${amt} − ${cost.toFixed(2)} = ${p.toFixed(2)}`;
$('c_profit').style.color = p < 0 ? '#cf222e' : '#1a7f37';
}
function previewBuy(){
const g = num('b_g'), p = num('b_price');
if(!g || !p){ $('b_preview').textContent = '—'; return; }
if(!ST.累计成本){ $('b_preview').textContent = '—'; return; }
const nc = ST.累计成本 + g*p, ng = ST.总克重 + g;
$('b_preview').textContent = `(${ST.累计成本.toFixed(2)} + ${(g*p).toFixed(2)}) ÷ (${ST.总克重} + ${g}) = ${(nc/ng).toFixed(2)} 元/克`;
}
['s_g','s_price','s_labor','s_fix','s_other','s_amt'].forEach(id=>$(id).addEventListener('input',calc));
document.querySelectorAll('.add').forEach(c=>c.addEventListener('change',calc));
['b_g','b_price'].forEach(id=>$(id).addEventListener('input',previewBuy));
function show(id, ok, txt){ const e=$(id); e.className='msg '+(ok?'ok':'err'); e.textContent=txt; }
async function doBuy(){
const b = { 日期:$('b_date').value, 货盘:$('b_pocket').value, 克重:num('b_g'), 料价:num('b_price'), 备注:$('b_note').value };
if(!b.克重 || !b.料价){ show('b_msg', false, '克重和料价都要填'); return; }
$('b_go').disabled = true;
try{
const r = await fetch('/api/purchase',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify(b)}).then(x=>x.json());
if(!r.ok) throw new Error(r.msg);
show('b_msg', true, `✅ 入库成功\n${b.货盘} +${b.克重}g @${b.料价}\n新均价:${r.均价.toFixed(2)} 元/克 | ${b.货盘}余额:${r.货盘余额.toFixed(3)}g`);
$('b_g').value=''; $('b_note').value='';
await load();
invalidateReport();
}catch(e){ show('b_msg', false, '❌ '+e.message); }
$('b_go').disabled = false;
}
async function doSale(){
const b = { 日期:$('s_date').value, 货盘:$('s_pocket').value, 客户:$('s_cust').value,
克重:num('s_g'), 料价:num('s_price'), 工费单价:num('s_labor'), 工费固定:num('s_fix'),
附加:附加合计(), 附加其他:num('s_other'), 售价:num('s_amt'), 收款方式:$('s_pay').value, 备注:$('s_note').value };
if(!b.克重 || !b.售价){ show('s_msg', false, '克重和售价都要填'); return; }
$('s_go').disabled = true;
try{
const r = await fetch('/api/sale',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify(b)}).then(x=>x.json());
if(!r.ok) throw new Error(r.msg);
const ok = r.利润 >= 0;
show('s_msg', ok, `${ok?'✅':'⚠️'} 已写入飞书\n成本 ${r.成本总计.toFixed(2)} | 利润 ${r.利润.toFixed(2)}\n${b.货盘}余额:${r.货盘余额.toFixed(3)}g`);
$('s_g').value='1'; $('s_amt').value='0'; $('s_cust').value=''; $('s_note').value='';
document.querySelectorAll('.add').forEach(c=>c.checked=false);
$('s_labor').value='0'; $('s_fix').value='0'; $('s_other').value='0';
await load();
invalidateReport();
}catch(e){ show('s_msg', false, '❌ '+e.message); }
$('s_go').disabled = false;
}
const esc = v => String(v??'').replace(/[&<>"']/g,c=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[c]));
const fmt = (v,unit='元') => v===null || v===undefined ? '待核' : Number(v).toLocaleString('zh-CN',{minimumFractionDigits:unit==='元'?2:unit==='克'?3:0,maximumFractionDigits:unit==='克'?3:2});
const cssStatus = s => s==='异常'?'status-error':s==='一致'?'status-ok':'status-warn';
let currentReport = null, reportRequest = 0;
function invalidateReport(){
reportRequest++; currentReport=null; $('r_result').replaceChildren(); $('r_save').disabled=true; $('r_go').disabled=false;
show('r_msg',true,'账务或日期已变动,请重新核对。');
}
function sourceText(rows){ return (rows||[]).map(x=>`${x.表} · ${x.记录ID}${x.备注?' · '+x.备注:''}`).join('\n'); }
function renderReport(r){
const g=r.概览;
const cards=[['已录入销售金额',g.销售金额],['明细成本',g.明细成本],['独立售后支出',g.售后调整],['调整后利润',g.调整后利润]];
const checks=r.核对项.filter(x=>!x.项目.startsWith('逐笔利润'));
const perSale=r.核对项.filter(x=>x.项目.startsWith('逐笔利润'));
const row=(cells)=>'<tr>'+cells.map(c=>'<td>'+c+'</td>').join('')+'</tr>';
const table=(heads,rows)=>'<div class="table-wrap"><table><thead><tr>'+heads.map(h=>'<th>'+h+'</th>').join('')+'</tr></thead><tbody>'+rows.join('')+'</tbody></table></div>';
$('r_result').innerHTML=`
<div class="report-status ${r.状态==='存在异常'?'bad':r.状态==='账表一致'?'good':'warn'}">
<strong>${esc(r.日期)} · ${esc(r.状态)}</strong><br>
${g.销售单数} 条销售记录(含冲红) · ${r.异常.length} 项需要查看<br>
<small>读取完成:${esc(new Date(r.生成时间).toLocaleString('zh-CN',{timeZone:'Asia/Shanghai',hour12:false}))}(北京时间)</small>
</div>
<div class="cards">${cards.map(([name,value])=>`<div class="card"><span>${name}(元)</span><strong>${fmt(value)}</strong></div>`).join('')}</div>
<h2>当天现货货款</h2>
<p>现货销售成本:<b>${fmt(g.现货销售成本)} 元</b>;现货退货成本冲回:<b>${fmt(g.现货退货冲回)} 元</b>。</p>
<p class="hint">交款范围及未收款处理正在确认;以上为账面成本,不表示已经交款。混合单、缺字段或待补记时,以异常清单为准。</p>
<h2>需要查看的项目</h2>
${r.异常.length?r.异常.map(x=>`<div class="issue ${x.状态==='异常'?'bad':''}"><b>${esc(x.状态)} · ${esc(x.项目)}</b>
${'差额' in x?`<p>应为 ${fmt(x.应为,x.单位)} ${esc(x.单位)} | 实记 ${fmt(x.实记,x.单位)} ${esc(x.单位)} | 差额 ${fmt(x.差额,x.单位)} ${esc(x.单位)}</p>`:''}
<p>${esc(x.说明)}</p><small style="white-space:pre-wrap">${esc(sourceText(x.来源))}</small></div>`).join(''):'<p>本次已读取记录未发现账表差异;仍需核对实际到账与交款。</p>'}
<h2>核对明细</h2>
<p>逐笔金额−成本=利润:${perSale.filter(x=>x.状态==='一致').length} / ${perSale.length} 条一致。</p>
${table(['核对项目','应为','实记','差额','结果'], checks.map(x=>row([esc(x.项目),fmt(x.应为,x.单位)+(x.单位?' '+esc(x.单位):''),fmt(x.实记,x.单位),fmt(x.差额,x.单位),`<span class="${cssStatus(x.状态)}">${esc(x.状态)}</span>`])))}
<details><summary>销售原记录(${r.销售明细.length} 条)</summary>
${table(['记录 ID / 分类','克重','金额','成本','利润','逐笔校验','备注'],r.销售明细.map(x=>row([esc(x.记录ID)+'<br>'+esc(x.类型)+(x.退货?' · 冲红':''),x.非黄金?'不计黄金克重':fmt(x.克重,'克'),fmt(x.金额),fmt(x.成本),fmt(x.利润),esc(x.核对状态),esc(x.备注)])))}
</details>
<details><summary>库存原记录(${r.库存明细.length} 条)</summary>
${table(['记录 ID','货盘','克重变化','动作'],r.库存明细.map(x=>row([esc(x.记录ID),esc(x.货盘),fmt(x.克重,'克'),esc(x.动作)])))}
</details>
<h2>核对说明</h2>
${r.提示.map(t=>'<p>'+esc(t)+'</p>').join('')}
<p>金额差额 0.01~0.05 元单独显示“小额尾差”,仍需查看;克重按 0.001 克核对。空白字段不当成 0。</p>
<p>${r.数据来源.map(s=>`<a href="${esc(s.链接)}" target="_blank" rel="noopener noreferrer">打开${esc(s.表)}</a>`).join(' | ')}</p>`;
}
async function loadReport(){
const date=$('r_date').value;
if(!date){show('r_msg',false,'请先选择对账日期');return;}
const request=++reportRequest; currentReport=null;
$('r_result').replaceChildren(); $('r_save').disabled=true; $('r_go').disabled=true;
show('r_msg',true,'正在读取飞书并核对…');
try{
const r=await fetch('/api/reconcile?date='+encodeURIComponent(date),{cache:'no-store'}).then(x=>x.json());
if(request!==reportRequest)return;
if(!r.ok)throw new Error(r.msg);
renderReport(r); currentReport=r; $('r_save').disabled=false; $('r_msg').className='msg';
}catch(e){if(request===reportRequest)show('r_msg',false,'本次对账未完成:'+e.message+'。请重新核对。');}
finally{if(request===reportRequest)$('r_go').disabled=false;}
}
function saveReport(){
if(!currentReport)return;
const r=currentReport, lines=[`麦麦黄金台账 · ${r.日期}对账报告`, `结果:${r.状态}`,`生成时间:${new Date(r.生成时间).toLocaleString('zh-CN',{timeZone:'Asia/Shanghai',hour12:false})}(北京时间)`,''];
lines.push('【账面合计】',...Object.entries(r.概览).map(([k,v])=>`${k}:${v===null?'待核':v}`),'','【核对项目】');
r.核对项.forEach(x=>lines.push(`${x.状态} | ${x.项目}${'差额' in x?` | 应为 ${fmt(x.应为,x.单位)} / 实记 ${fmt(x.实记,x.单位)} / 差额 ${fmt(x.差额,x.单位)} ${x.单位}`:''}`,x.说明,sourceText(x.来源),''));
lines.push('【说明】',...r.提示,'本文件是生成时的核对副本;飞书原记录为准。');
const url=URL.createObjectURL(new Blob(['\ufeff'+lines.join('\r\n')],{type:'text/plain;charset=utf-8'}));
const a=document.createElement('a'); a.href=url; a.download=`黄金对账-${r.日期}.txt`; a.click(); setTimeout(()=>URL.revokeObjectURL(url),1000);
}
const clientToday=()=>new Date(Date.now()+28800000).toISOString().slice(0,10);
$('r_date').value=clientToday();
$('r_date').addEventListener('change',()=>{invalidateReport();loadReport();});
load().catch(e=>show('r_msg',false,'库存读取失败:'+e.message));
const _tb = new URLSearchParams(location.search).get('tab');
if(_tb==='2'||_tb==='3') tab(Number(_tb));
</script>
</body>
</html>