2026-10-28 17:34:33 +08:00
|
|
|
|
/* 独立演示页面:业务金额由接口返回,页面只负责录入、筛选与展示。 */
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
|
|
const 页面 = {
|
|
|
|
|
|
数据: null,
|
|
|
|
|
|
视角: 'assistant',
|
|
|
|
|
|
明细: 'orders',
|
|
|
|
|
|
日期: '2026-11-01',
|
|
|
|
|
|
载入序号: 0,
|
|
|
|
|
|
写入中: false,
|
|
|
|
|
|
重置模式: 'default',
|
|
|
|
|
|
单价序号: 0,
|
|
|
|
|
|
单价定时器: null,
|
|
|
|
|
|
提示定时器: null,
|
|
|
|
|
|
};
|
|
|
|
|
|
const $ = (选择器, 根 = document) => 根.querySelector(选择器);
|
|
|
|
|
|
const $$ = (选择器, 根 = document) => [...根.querySelectorAll(选择器)];
|
|
|
|
|
|
const 转义 = 值 => String(值 ?? '').replace(/[&<>"']/g, 字符 => ({'&':'&','<':'<','>':'>','"':'"',"'":'''}[字符]));
|
|
|
|
|
|
const 数字格式 = new Intl.NumberFormat('zh-CN', {minimumFractionDigits:2, maximumFractionDigits:2});
|
|
|
|
|
|
const 克重格式 = new Intl.NumberFormat('zh-CN', {minimumFractionDigits:3, maximumFractionDigits:3});
|
|
|
|
|
|
const 金额 = 分 => Number.isFinite(分) ? `¥${数字格式.format(分 / 100)}` : '—';
|
|
|
|
|
|
const 元输入 = 分 => Number.isFinite(分) ? (分 / 100).toFixed(2) : '';
|
|
|
|
|
|
const 克重 = 毫克 => Number.isFinite(毫克) ? 克重格式.format(毫克 / 1000) : '—';
|
|
|
|
|
|
const 单价 = 元 => Number.isFinite(元) ? `¥${数字格式.format(元)} / g` : '—';
|
|
|
|
|
|
const 渠道名 = 渠道 => ({wechat:'微信',alipay:'支付宝',bank:'银行卡',cash:'现金',credit_card:'信用卡'}[渠道] || 渠道 || '未注明');
|
|
|
|
|
|
const 成员名 = 编号 => 页面.数据?.members?.find(成员 => 成员.id === 编号)?.name || ({owner:'麦麦',assistant:'助理1',assistant2:'助理2'}[编号]) || '未注明';
|
2026-11-02 13:24:53 +08:00
|
|
|
|
const 本人 = () => 页面.数据?.report?.daily?.members?.find(成员 => 成员.member === 页面.视角);
|
2026-10-28 17:34:33 +08:00
|
|
|
|
const 是总览 = () => 页面.视角 === 'owner';
|
|
|
|
|
|
const 当日订单 = () => (页面.数据?.report?.orders || []).filter(订单 => 订单.date === 页面.日期 && (是总览() || 订单.member === 页面.视角));
|
|
|
|
|
|
const 相关订单 = () => (页面.数据?.report?.orders || []).filter(订单 => 是总览() || 订单.member === 页面.视角);
|
|
|
|
|
|
|
|
|
|
|
|
async function 请求(路径, 选项 = {}) {
|
|
|
|
|
|
let 响应;
|
|
|
|
|
|
try {
|
|
|
|
|
|
响应 = await fetch(路径, {cache:'no-store', ...选项, headers:{'Content-Type':'application/json', ...(选项.headers || {})}});
|
|
|
|
|
|
} catch (_) {
|
|
|
|
|
|
throw new Error('暂时连不上本机演示服务,请确认演示已启动后重试。');
|
|
|
|
|
|
}
|
|
|
|
|
|
let 结果;
|
|
|
|
|
|
try { 结果 = await 响应.json(); }
|
|
|
|
|
|
catch (_) { throw new Error('演示服务没有返回完整结果,请重试。'); }
|
|
|
|
|
|
if (!响应.ok || 结果.ok !== true) {
|
|
|
|
|
|
const 错误 = new Error(结果.error?.message || '这条记录没有保存,请检查输入后重试。');
|
|
|
|
|
|
错误.field = 结果.error?.field;
|
|
|
|
|
|
throw 错误;
|
|
|
|
|
|
}
|
|
|
|
|
|
return 结果.data;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function 提示(文字, 错误 = false) {
|
|
|
|
|
|
clearTimeout(页面.提示定时器);
|
|
|
|
|
|
const 容器 = $('#toast');
|
|
|
|
|
|
容器.textContent = 文字;
|
|
|
|
|
|
容器.classList.toggle('error-toast', 错误);
|
|
|
|
|
|
容器.hidden = false;
|
|
|
|
|
|
页面.提示定时器 = setTimeout(() => { 容器.hidden = true; }, 4500);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function 载入(日期 = 页面.日期) {
|
|
|
|
|
|
const 序号 = ++页面.载入序号;
|
|
|
|
|
|
$('#load-error').hidden = true;
|
|
|
|
|
|
if (!页面.数据) $('#loading').hidden = false;
|
|
|
|
|
|
$('#report-date').disabled = true;
|
|
|
|
|
|
try {
|
|
|
|
|
|
const 数据 = await 请求(`/api/demo/state?date=${encodeURIComponent(日期)}`);
|
|
|
|
|
|
if (序号 !== 页面.载入序号) return;
|
|
|
|
|
|
页面.数据 = 数据;
|
|
|
|
|
|
页面.日期 = 数据.currentDate || 数据.report.date || 日期;
|
|
|
|
|
|
$('#report-date').value = 页面.日期;
|
|
|
|
|
|
渲染();
|
|
|
|
|
|
} catch (错误) {
|
|
|
|
|
|
if (序号 !== 页面.载入序号) return;
|
|
|
|
|
|
$('#load-error span').textContent = 错误.message;
|
|
|
|
|
|
$('#load-error').hidden = false;
|
|
|
|
|
|
if (页面.数据) $('#report-date').value = 页面.日期;
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
if (序号 === 页面.载入序号) {
|
|
|
|
|
|
$('#loading').hidden = true;
|
|
|
|
|
|
$('#report-date').disabled = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function 渲染() {
|
|
|
|
|
|
const 数据 = 页面.数据;
|
|
|
|
|
|
if (!数据?.report) return;
|
|
|
|
|
|
$('#dashboard').hidden = false;
|
|
|
|
|
|
$('#view-tabs').innerHTML = 数据.members.map(成员 => `<button type="button" data-view="${转义(成员.id)}" class="${页面.视角 === 成员.id ? 'active' : ''}" aria-pressed="${页面.视角 === 成员.id}">${转义(成员.id === 'owner' ? '麦麦总览' : 成员.name)}</button>`).join('');
|
|
|
|
|
|
$('#day-caption').textContent = `${页面.日期} · ${是总览() ? '麦麦总览' : `${成员名(页面.视角)}的日结`}`;
|
|
|
|
|
|
$('#page-title').textContent = 是总览() ? '每个人的钱,沿同一张账对清。' : '今天赚多少,该转多少。';
|
|
|
|
|
|
const 说明 = $('#scenario-note');
|
|
|
|
|
|
说明.hidden = 数据.demo?.name !== 'complex';
|
2026-11-02 13:24:53 +08:00
|
|
|
|
说明.textContent = '复杂场景:含两人购料、信用卡代收和跨日余料。可切换 11 月 1 日、11 月 2 日,分别核对当天订单、收款与成本。';
|
2026-10-28 17:34:33 +08:00
|
|
|
|
渲染日结();
|
|
|
|
|
|
渲染资金();
|
|
|
|
|
|
渲染材料();
|
|
|
|
|
|
渲染明细();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-02 13:24:53 +08:00
|
|
|
|
function 统计卡(标题, 分, 注释, 类名 = '', 收入成员 = '') {
|
|
|
|
|
|
const 标签 = 收入成员 ? 'button' : 'div';
|
|
|
|
|
|
return `<${标签} class="stat ${类名} ${收入成员 ? 'stat-income' : ''}" ${收入成员 ? `type="button" data-income="${转义(收入成员)}" aria-label="查看收入客户与渠道明细"` : ''}><small>${转义(标题)}</small><strong class="money">${金额(分)}</strong><p>${转义(注释)}${收入成员 ? ' · 点击核对明细' : ''}</p></${标签}>`;
|
2026-10-28 17:34:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function 小行(标题, 分, 强调 = false) {
|
2026-11-02 13:24:53 +08:00
|
|
|
|
return `<div class="mini-row ${强调 ? 'highlight' : ''}"><span>${转义(标题)}</span><strong class="money">${金额(分)}</strong></div>`;
|
2026-10-28 17:34:33 +08:00
|
|
|
|
}
|
2026-11-02 13:24:53 +08:00
|
|
|
|
function 收款身份(项) {
|
|
|
|
|
|
return 项.collectionType === 'self' ? '本人收款' : 项.collectionType === 'owner' ? '麦麦代收' : `${成员名(项.collector)}代收(其他成员)`;
|
2026-10-28 17:34:33 +08:00
|
|
|
|
}
|
2026-11-02 13:24:53 +08:00
|
|
|
|
function 渲染收入明细(成员) {
|
|
|
|
|
|
return `<details class="income-details section-details" id="income-details-${成员.member}"><summary>归属订单收入 ${金额(成员.salesCents)} · 点击按客户和渠道核对</summary><p>只含 ${转义(页面.日期)} 归属${转义(成员.name)}的已收齐订单;归属收入包含代收,不等于本人账户到账。</p><div class="income-breakdown">${小行('本人收款',成员.selfCollectedCents)}${小行('麦麦代收',成员.ownerCollectedCents)}${小行('其他成员代收',成员.otherCollectedCents)}</div><div class="table-scroll"><table><thead><tr><th>客户订单</th><th>收款归类</th><th>实际渠道</th><th class="number">实收金额</th></tr></thead><tbody>${成员.incomeDetails.map(项 => `<tr><td>${转义(项.customer)}</td><td>${转义(收款身份(项))}</td><td>${转义(项.channelName)} · ${转义(项.collectorName)}</td><td class="number">${金额(项.amountCents)}</td></tr>`).join('') || '<tr><td colspan="4">当天没有本人订单收入。</td></tr>'}</tbody><tfoot><tr><td colspan="3">归属订单实收合计</td><td class="number">${金额(成员.salesCents)}</td></tr></tfoot></table></div><div class="cash-detail-rows">${成员.channelTotals.map(项 => 小行(`${项.label}合计(含代收)`,项.amountCents)).join('')}</div></details>`;
|
2026-10-28 17:34:33 +08:00
|
|
|
|
}
|
2026-11-02 13:24:53 +08:00
|
|
|
|
function 日报公式(成员) {
|
|
|
|
|
|
return `麦麦垫付成本 ${金额(成员.ownerAdvancedCents)} + 当天净利润 ${金额(成员.profitCents)} − 麦麦代收 ${金额(成员.ownerCollectedCents)}`;
|
2026-10-28 17:34:33 +08:00
|
|
|
|
}
|
2026-11-02 13:24:53 +08:00
|
|
|
|
function 渲染日结() {
|
|
|
|
|
|
const 报告 = 页面.数据.report.daily, 成员 = 本人(), 摘要 = 是总览() ? 报告.overview : 成员;
|
|
|
|
|
|
const 主标题 = 是总览() ? '当天全店净利润' : 成员.reviewRequired ? '当天应转金额待复核' : 成员.toOwnerCents < 0 ? '当天麦麦应返/核对' : '当天应转麦麦';
|
|
|
|
|
|
const 主金额 = 是总览() ? 摘要.profitCents : 成员.toOwnerCents;
|
|
|
|
|
|
$('#daily-summary').innerHTML = `${是总览() ? '' : `<div class="opening-banner">${转义(成员.name)} · 初期余额 <strong class="money">${金额(成员.openingCents)}</strong><small>初始周转金设置,仅作对账参考</small></div>`}<div class="day-stats">${统计卡(是总览() ? '当天归属订单收入' : '本人当天订单收入',摘要.salesCents,'已收齐订单实收合计,包含代收','',是总览() ? 'all' : 页面.视角)}${统计卡('当天订单成本',摘要.orderCostCents,'已用材料+工费等+刷卡手续费')}${统计卡('当天全部净利润',摘要.profitCents,'合并当天盈利和亏损订单','profit')}</div>${是总览() ? '' : 渲染收入明细(成员)}<div class="settlement-hero"><div class="hero-top"><div><div class="hero-heading">${转义(主标题)}</div><p class="hero-amount money">${主金额 === null ? '待复核' : 金额(是总览() ? 主金额 : Math.abs(主金额))}</p></div><span class="badge">仅 ${转义(页面.日期)}</span></div><div class="hero-foot"><p>${是总览() ? '当天全店收入减去当天订单成本。' : 日报公式(成员) + (成员.toOwnerCents === null ? ';含亏损订单,核实后再确定应转。' : ` = ${金额(成员.toOwnerCents)}`)}</p><p>只核对所选日,不累计历史待转,不记录或推断付款完成。未用材料不计本日偿还。${!是总览() && 成员.toOwnerCents < 0 ? '代收抵扣后为负,请核对麦麦应返金额;此处没有登记付款。' : ''}</p></div></div>${!是总览() && 成员.reviewRequired ? '<div class="error">当天包含亏损订单,净利润保留真实金额;应转待复核,不自动反向补利润。</div>' : ''}`;
|
2026-10-28 17:34:33 +08:00
|
|
|
|
}
|
2026-11-02 13:24:53 +08:00
|
|
|
|
function 渲染成本明细(成员) {
|
|
|
|
|
|
return `<section class="panel"><div class="panel-heading"><div><h2>${转义(成员.name)}当天订单成本</h2><p>只含本人当天订单;麦麦垫付按已耗用材料及实际费用归属计算。</p></div><strong class="money">${金额(成员.orderCostCents)}</strong></div><div class="cash-detail-rows">${成员.costsByPayer.map(项 => 小行(`${项.name}${项.member === 'owner' ? '垫付(应转构成)' : '支付'}`,项.amountCents,项.member === 'owner')).join('')}</div><details class="section-details"><summary>查看各订单的成本项目与付款人</summary><div class="table-scroll"><table><thead><tr><th>客户订单</th><th>成本项目</th><th>实际付款人</th><th class="number">金额</th></tr></thead><tbody>${成员.costDetails.map(项 => `<tr class="${项.payer === 'owner' ? 'owner-advance' : ''}"><td>${转义(项.customer)}</td><td>${转义(项.label)}</td><td>${转义(项.payerName)}${项.payer === 'owner' ? '垫付' : ''}</td><td class="number">${金额(项.amountCents)}</td></tr>`).join('') || '<tr><td colspan="4">当天没有订单成本。</td></tr>'}</tbody><tfoot><tr><td colspan="3">本人当天订单成本合计</td><td class="number">${金额(成员.orderCostCents)}</td></tr></tfoot></table></div></details>${成员.otherAdvancedCents ? '<p class="form-note">含其他成员代付成本,请按付款人明细单独核对。</p>' : ''}</section>`;
|
|
|
|
|
|
}
|
|
|
|
|
|
function 渲染资金() {
|
|
|
|
|
|
const 报告 = 页面.数据.report.daily;
|
|
|
|
|
|
if (!是总览()) { $('#member-summary').innerHTML = 渲染成本明细(本人()); return; }
|
|
|
|
|
|
$('#member-summary').innerHTML = `<div class="panel"><div class="panel-heading"><div><h2>各成员当天对账</h2><p>期初只作参考;收入、成本、净利润和应转均按所选日。</p></div></div><div class="member-grid">${报告.members.map(成员 => `<article class="member-card"><h3>${转义(成员.name)}</h3>${小行('初期余额',成员.openingCents)}${小行('当天归属收入',成员.salesCents)}${小行('本人收款',成员.selfCollectedCents)}${小行('麦麦代收',成员.ownerCollectedCents)}${小行('其他成员代收',成员.otherCollectedCents)}${小行('当天成本',成员.orderCostCents)}${小行('麦麦垫付成本',成员.ownerAdvancedCents,true)}${小行('当天净利润',成员.profitCents)}${成员.member === 'owner' ? '' : `<p class="member-due">${成员.reviewRequired ? '亏损待复核' : 成员.toOwnerCents < 0 ? '麦麦应返/核对 ' + 金额(-成员.toOwnerCents) : '当天应转麦麦 ' + 金额(成员.toOwnerCents)}</p><p class="small">${日报公式(成员)}</p>`}${渲染收入明细(成员)}</article>`).join('')}</div></div>${报告.members.map(成员 => 渲染成本明细(成员)).join('')}`;
|
2026-10-28 17:34:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function 渲染材料() {
|
|
|
|
|
|
const 池 = 页面.数据.report.materialPool;
|
|
|
|
|
|
const 我的订单 = 相关订单();
|
|
|
|
|
|
const 相关购料编号 = new Set(我的订单.flatMap(订单 => (订单.funding || []).map(项 => 项.purchaseId)));
|
|
|
|
|
|
const 付款来源 = (池.funding || []).filter(项 => 是总览() || 项.payer === 页面.视角 || 相关购料编号.has(项.purchaseId));
|
|
|
|
|
|
const 用料 = 我的订单.filter(订单 => 订单.date === 页面.日期 && 订单.materialSource !== 'direct' && (订单.funding || []).length);
|
|
|
|
|
|
$('#material-pool').innerHTML = `<div class="panel"><div class="panel-heading"><div><h2>材料还在,就继续留在池里。</h2><p>共享材料池 · 截至 ${转义(页面.日期)} 的余料状态</p></div><button type="button" class="compact" data-open="purchase-dialog">+ 新增购料</button></div><div class="pool-totals"><div class="pool-total"><span>所选日加权料价</span><strong class="money">${单价(池.weightedCostYuanPerGram)}</strong></div><div class="pool-total"><span>剩余克重</span><strong class="money">${克重(池.closingWeightMg)}<small>g</small></strong></div><div class="pool-total"><span>剩余材料价值</span><strong class="money">${金额(池.closingCostCents)}</strong></div></div><p class="pool-rule">未用的料不需要还款。订单用了谁出钱买的料,才向谁归还已用部分;自己买的未用料,仍计入自己的资金占用。</p><details class="section-details"><summary>查看付款来源余额与当日用料去向</summary>${!是总览() ? '<p class="pool-overview-label">材料池余料是共享库存;下方只列本人购料,以及本人订单用到的付款来源。</p>' : ''}${付款来源.length ? `<div class="source-grid">${付款来源.map(来源 => `<article class="source-card"><h3>${转义(成员名(来源.payer))}付款 · ${转义(来源.date)}</h3><div class="mini-rows">${小行('购料实付',来源.totalCents)}${小行('累计已用',来源.consumedCents)}${小行('未用余额',来源.remainingCents)}</div></article>`).join('')}</div>` : '<div class="empty">暂无与你有关的材料付款来源。</div>'}<div class="panel-section"><h3>当日用料的还款来源</h3>${用料.length ? `<div class="pool-use-list">${用料.flatMap(订单 => 订单.funding.map(来源 => `<div class="pool-use-row"><b>${转义(订单.customer || '客户')} · ${转义(成员名(订单.member))}的订单</b><span>使用${转义(成员名(来源.payer))}付款的材料</span><span class="use-amount">${来源.payer === 订单.member ? '自有料款,无需还给别人' : `向${转义(成员名(来源.payer))}归还`} ${金额(来源.amountCents)}</span></div>`)).join('')}</div>` : '<div class="empty">所选日暂无共用材料池用料。</div>'}</div></details></div>`;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function 渲染明细() {
|
|
|
|
|
|
const 订单 = 当日订单();
|
|
|
|
|
|
const 购料 = (页面.数据.purchases || []).filter(记录 => 记录.date === 页面.日期 && (是总览() || 记录.payer === 页面.视角));
|
2026-11-02 13:24:53 +08:00
|
|
|
|
const 标签 = [{编号:'orders',标题:'销售订单',数量:订单.length},{编号:'purchases',标题:'购料记录',数量:购料.length}];
|
2026-10-28 17:34:33 +08:00
|
|
|
|
let 内容;
|
2026-11-02 13:24:53 +08:00
|
|
|
|
if (页面.明细 === 'purchases') 内容 = 渲染购料表(购料);
|
|
|
|
|
|
else { 页面.明细 = 'orders'; 内容 = 渲染订单表(订单); }
|
|
|
|
|
|
$('#activity').innerHTML = `<div class="panel"><div class="panel-heading"><div><h2>当日明细</h2><p>${是总览() ? '查看全部成员的当日记录。' : '只显示本人订单、本人购料。'}</p></div><span class="badge">${转义(页面.日期)}</span></div><div class="activity-tabs" role="group" aria-label="选择明细类型">${标签.map(项 => `<button type="button" data-activity="${项.编号}" class="${页面.明细 === 项.编号 ? 'active' : ''}" aria-pressed="${页面.明细 === 项.编号}">${项.标题} <span>${项.数量}</span></button>`).join('')}</div>${内容}</div>`;
|
2026-10-28 17:34:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function 渲染订单表(列表) {
|
|
|
|
|
|
if (!列表.length) return '<div class="empty"><strong>这一天还没有销售记录</strong><p>只有已交付、已收齐的普通销售进入这份日结。</p></div>';
|
|
|
|
|
|
return `<p class="activity-subtitle">材料成本来自所选日料池加权成本;单独购料按该笔实付计算。</p><div class="table-scroll" tabindex="0" aria-label="销售订单明细,可横向滚动"><table class="orders-table"><thead><tr><th>客户 / 成员</th><th class="number">克重</th><th class="number">销售实收</th><th>实际收款</th><th class="number">材料成本</th><th class="number">工费等 / 刷卡费</th><th class="number">订单利润</th></tr></thead><tbody>${列表.map(订单 => `<tr><td><strong>${转义(订单.customer)}</strong><small>${转义(成员名(订单.member))}${订单.materialSource === 'direct' ? ' · 单独购料' : ''}</small></td><td class="number">${克重(订单.weightMg)}g</td><td class="number">${金额(订单.saleCents)}</td><td>${转义(渠道名(订单.channel))}<small>${转义(成员名(订单.collector))}${订单.collector === 订单.member ? '自收' : '代收'}</small></td><td class="number">${金额(订单.materialCostCents)}</td><td class="number">${金额(订单.expenseCents)}<small>${转义(成员名(订单.expensePayer))}支付${订单.cardFeeCents ? `;刷卡费 ${金额(订单.cardFeeCents)} 由麦麦支付` : ''}</small></td><td class="number"><strong class="profit-number">${金额(订单.profitCents)}</strong><small>${订单.reviewRequired ? '亏损待复核' : '销售减材料、费用及手续费'}</small></td></tr><tr class="order-formula-row"><td colspan="7"><p class="order-formula">${转义(订单.formula || '')}</p></td></tr>`).join('')}</tbody></table></div>`;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function 渲染购料表(列表) {
|
|
|
|
|
|
if (!列表.length) return '<div class="empty">这一天没有本人购料记录。共享材料余额仍保留在上方。</div>';
|
|
|
|
|
|
return `<div class="table-scroll" tabindex="0" aria-label="购料明细,可横向滚动"><table class="purchases-table"><thead><tr><th>日期</th><th>付款人</th><th class="number">购料克重</th><th class="number">实付总额</th><th>备注</th></tr></thead><tbody>${列表.map(记录 => `<tr><td>${转义(记录.date)}</td><td>${转义(成员名(记录.payer))}</td><td class="number">${克重(记录.weightMg)}g</td><td class="number">${金额(记录.totalCents)}</td><td>${转义(记录.note || '—')}</td></tr>`).join('')}</tbody></table></div>`;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function 填成员选项(表单) {
|
|
|
|
|
|
$$('select', 表单).filter(下拉 => ['payer','member','collector','expensePayer','directMaterialPayer'].includes(下拉.name)).forEach(下拉 => {
|
|
|
|
|
|
下拉.innerHTML = 页面.数据.members.map(成员 => `<option value="${转义(成员.id)}">${转义(成员.name)}</option>`).join('');
|
|
|
|
|
|
下拉.value = 页面.视角;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function 清空表单错误(表单) {
|
|
|
|
|
|
const 错误栏 = $('.form-error', 表单);
|
|
|
|
|
|
if (错误栏) { 错误栏.textContent = ''; 错误栏.hidden = true; }
|
|
|
|
|
|
$$('[aria-invalid]', 表单).forEach(字段 => 字段.removeAttribute('aria-invalid'));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function 打开表单(编号) {
|
|
|
|
|
|
if (页面.写入中 || !页面.数据) return;
|
|
|
|
|
|
const 弹窗 = $(`#${编号}`);
|
|
|
|
|
|
const 表单 = $('form', 弹窗);
|
|
|
|
|
|
表单.reset();
|
|
|
|
|
|
delete 表单.dataset.requestId;
|
|
|
|
|
|
清空表单错误(表单);
|
|
|
|
|
|
填成员选项(表单);
|
|
|
|
|
|
const 日期字段 = $('[name="date"]', 表单);
|
|
|
|
|
|
if (日期字段) 日期字段.value = 页面.日期;
|
|
|
|
|
|
if (编号 === 'purchase-dialog') { clearTimeout(页面.单价定时器); 页面.单价序号 += 1; $('#purchase-unit-price').textContent = '填入克重和总额后显示'; }
|
|
|
|
|
|
if (编号 === 'order-dialog') 更新销售字段();
|
|
|
|
|
|
if (编号 === 'opening-dialog') 更新期初();
|
|
|
|
|
|
弹窗.showModal();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function 更新销售字段() {
|
|
|
|
|
|
const 表单 = $('#order-form');
|
|
|
|
|
|
const 信用卡 = 表单.elements.channel.value === 'credit_card';
|
|
|
|
|
|
const 收款人 = 表单.elements.collector;
|
|
|
|
|
|
if (信用卡) 收款人.value = 'owner';
|
|
|
|
|
|
收款人.disabled = 信用卡;
|
|
|
|
|
|
$('#credit-card-note').hidden = !信用卡;
|
|
|
|
|
|
const 单独购料 = 表单.elements.materialSource.value === 'direct';
|
|
|
|
|
|
$('#direct-material-fields').hidden = !单独购料;
|
|
|
|
|
|
表单.elements.directMaterialCost.required = 单独购料;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function 更新期初() {
|
|
|
|
|
|
const 表单 = $('#opening-form');
|
|
|
|
|
|
const 期初 = (页面.数据.opening || []).find(项 => 项.member === 表单.elements.member.value);
|
|
|
|
|
|
表单.elements.amount.value = 元输入(期初?.amount);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function 打开重置(复杂) {
|
|
|
|
|
|
if (页面.写入中) return;
|
|
|
|
|
|
页面.重置模式 = 复杂 ? 'complex' : 'default';
|
|
|
|
|
|
const 表单 = $('#reset-form');
|
|
|
|
|
|
清空表单错误(表单);
|
|
|
|
|
|
$('#reset-title').textContent = 复杂 ? '换一组复杂场景试算?' : '重新开始这次试算?';
|
2026-11-02 13:24:53 +08:00
|
|
|
|
$('#reset-description').textContent = 复杂 ? '将清空当前演示里的新增记录和示例记录,载入“两人购料、信用卡代收、次日余料”的虚构示例。' : '将清空当前演示里的新增记录和示例记录,恢复“助理1应转麦麦1800元”的最简示例。当前正式账目不受影响。';
|
2026-10-28 17:34:33 +08:00
|
|
|
|
$('#reset-confirm').textContent = 复杂 ? '清空并载入复杂场景' : '重置为最简示例';
|
|
|
|
|
|
$('#reset-dialog').showModal();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function 提交(表单, 路径, 参数, 成功文字) {
|
|
|
|
|
|
if (页面.写入中) return;
|
|
|
|
|
|
清空表单错误(表单);
|
|
|
|
|
|
页面.写入中 = true;
|
|
|
|
|
|
const 提交键 = $('button[type="submit"]', 表单);
|
|
|
|
|
|
const 原文字 = 提交键.textContent;
|
|
|
|
|
|
const 控件 = $$('button,input,select', 表单);
|
|
|
|
|
|
const 原禁用 = 控件.map(字段 => 字段.disabled);
|
|
|
|
|
|
控件.forEach(字段 => { 字段.disabled = true; });
|
|
|
|
|
|
提交键.textContent = '正在记录…';
|
|
|
|
|
|
try {
|
|
|
|
|
|
const 数据 = await 请求(路径, {method:'POST', body:JSON.stringify(参数)});
|
|
|
|
|
|
页面.数据 = 数据;
|
|
|
|
|
|
页面.日期 = 数据.currentDate || 数据.report.date || 页面.日期;
|
|
|
|
|
|
$('#report-date').value = 页面.日期;
|
|
|
|
|
|
// 使写入前尚未完成的读取失效,避免旧结果盖住新余额。
|
|
|
|
|
|
页面.载入序号 += 1;
|
|
|
|
|
|
渲染();
|
|
|
|
|
|
表单.closest('dialog').close();
|
|
|
|
|
|
提示(成功文字);
|
|
|
|
|
|
} catch (错误) {
|
|
|
|
|
|
const 错误栏 = $('.form-error', 表单);
|
|
|
|
|
|
错误栏.textContent = 错误.message;
|
|
|
|
|
|
错误栏.hidden = false;
|
|
|
|
|
|
const 字段 = 错误.field && 表单.elements.namedItem(错误.field);
|
|
|
|
|
|
if (字段?.setAttribute) 字段.setAttribute('aria-invalid', 'true');
|
|
|
|
|
|
错误栏.scrollIntoView({block:'nearest'});
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
控件.forEach((字段, 序号) => { 字段.disabled = 原禁用[序号]; });
|
|
|
|
|
|
提交键.textContent = 原文字;
|
|
|
|
|
|
页面.写入中 = false;
|
|
|
|
|
|
$('#report-date').disabled = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function 表单参数(表单) { return Object.fromEntries(new FormData(表单).entries()); }
|
|
|
|
|
|
function 记录编号(表单) {
|
|
|
|
|
|
const 输入签名 = JSON.stringify([...new FormData(表单).entries()]);
|
|
|
|
|
|
if (!表单.dataset.requestId || 表单.dataset.requestSignature !== 输入签名) {
|
|
|
|
|
|
表单.dataset.requestId = typeof crypto.randomUUID === 'function' ? crypto.randomUUID() : `demo-${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
|
|
|
|
|
表单.dataset.requestSignature = 输入签名;
|
|
|
|
|
|
}
|
|
|
|
|
|
return 表单.dataset.requestId;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function 安排单价预览() {
|
|
|
|
|
|
clearTimeout(页面.单价定时器);
|
|
|
|
|
|
const 表单 = $('#purchase-form');
|
|
|
|
|
|
const 克 = 表单.elements.grams.value;
|
|
|
|
|
|
const 元 = 表单.elements.total.value;
|
|
|
|
|
|
const 序号 = ++页面.单价序号;
|
|
|
|
|
|
const 输出 = $('#purchase-unit-price');
|
|
|
|
|
|
if (!克 || !元 || !表单.elements.grams.validity.valid || !表单.elements.total.validity.valid) {
|
|
|
|
|
|
输出.textContent = '填入有效克重和总额后显示';
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
输出.textContent = '正在计算…';
|
|
|
|
|
|
页面.单价定时器 = setTimeout(async () => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const 结果 = await 请求(`/api/demo/purchase-preview?grams=${encodeURIComponent(克)}&total=${encodeURIComponent(元)}`);
|
|
|
|
|
|
if (序号 === 页面.单价序号) 输出.textContent = `${金额(结果.unitPriceCents)} / g`;
|
|
|
|
|
|
} catch (错误) {
|
|
|
|
|
|
if (序号 === 页面.单价序号) 输出.textContent = 错误.message;
|
|
|
|
|
|
}
|
|
|
|
|
|
}, 220);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
document.addEventListener('click', 事件 => {
|
|
|
|
|
|
const 按钮 = 事件.target.closest('button');
|
|
|
|
|
|
if (!按钮) return;
|
2026-11-02 13:24:53 +08:00
|
|
|
|
if (按钮.dataset.income) {
|
|
|
|
|
|
const 明细 = 按钮.dataset.income === 'all' ? $$('.income-details') : [$('#income-details-' + 按钮.dataset.income)].filter(Boolean);
|
|
|
|
|
|
明细.forEach(项 => { 项.open = true; });
|
|
|
|
|
|
明细[0]?.scrollIntoView?.({behavior:'smooth',block:'start'});
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2026-10-28 17:34:33 +08:00
|
|
|
|
if (按钮.hasAttribute('data-close')) { if (!页面.写入中) 按钮.closest('dialog').close(); return; }
|
|
|
|
|
|
if (按钮.dataset.view) { 页面.视角 = 按钮.dataset.view; 渲染(); return; }
|
|
|
|
|
|
if (按钮.dataset.open) { 打开表单(按钮.dataset.open); return; }
|
|
|
|
|
|
if (按钮.dataset.activity) { 页面.明细 = 按钮.dataset.activity; 渲染明细(); }
|
|
|
|
|
|
});
|
|
|
|
|
|
$('#report-date').addEventListener('change', 事件 => { if (事件.target.value) 载入(事件.target.value); });
|
|
|
|
|
|
$('#retry-load').addEventListener('click', () => 载入());
|
|
|
|
|
|
$('#reset-demo').addEventListener('click', () => 打开重置(false));
|
|
|
|
|
|
$('#complex-scenario').addEventListener('click', () => 打开重置(true));
|
|
|
|
|
|
$('#purchase-form').addEventListener('input', 事件 => { if (['grams','total'].includes(事件.target.name)) 安排单价预览(); });
|
|
|
|
|
|
$('#order-form').addEventListener('change', 事件 => {
|
|
|
|
|
|
if (事件.target.name === 'member') {
|
|
|
|
|
|
$('#order-form').elements.collector.value = 事件.target.value;
|
|
|
|
|
|
$('#order-form').elements.expensePayer.value = 事件.target.value;
|
|
|
|
|
|
$('#order-form').elements.directMaterialPayer.value = 事件.target.value;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (['member','channel','materialSource'].includes(事件.target.name)) 更新销售字段();
|
|
|
|
|
|
});
|
|
|
|
|
|
$('#opening-form [name="member"]').addEventListener('change', 更新期初);
|
|
|
|
|
|
$('#purchase-form').addEventListener('submit', 事件 => { 事件.preventDefault(); const 表单 = 事件.currentTarget; 提交(表单,'/api/demo/purchase',{...表单参数(表单),requestId:记录编号(表单)},'购料已记录,材料余额与日结已更新。'); });
|
|
|
|
|
|
$('#order-form').addEventListener('submit', 事件 => { 事件.preventDefault(); const 表单 = 事件.currentTarget; const 参数 = 表单参数(表单); 参数.allowLoss = 表单.elements.allowLoss.checked; if (参数.channel === 'credit_card') 参数.collector = 'owner'; if (参数.materialSource !== 'direct') { delete 参数.directMaterialCost; delete 参数.directMaterialPayer; } 提交(表单,'/api/demo/order',{...参数,requestId:记录编号(表单)},'销售已记录,收入、利润与应转金额已更新。'); });
|
2026-11-02 13:24:53 +08:00
|
|
|
|
$('#opening-form').addEventListener('submit', 事件 => { 事件.preventDefault(); const 表单 = 事件.currentTarget; 提交(表单,'/api/demo/opening',{...表单参数(表单),date:页面.数据.demo.startDate},'期初参考金额已更新。'); });
|
2026-10-28 17:34:33 +08:00
|
|
|
|
$('#reset-form').addEventListener('submit', 事件 => { 事件.preventDefault(); const 复杂 = 页面.重置模式 === 'complex'; 页面.视角 = 'assistant'; 页面.明细 = 'orders'; 提交(事件.currentTarget,复杂 ? '/api/demo/scenario' : '/api/demo/reset',复杂 ? {name:'complex'} : {},复杂 ? '已载入复杂场景,可切换日期和视角查看。' : '已恢复最简示例,可以重新试算。'); });
|
|
|
|
|
|
$$('dialog').forEach(弹窗 => { 弹窗.addEventListener('cancel', 事件 => { if (页面.写入中) 事件.preventDefault(); }); });
|
|
|
|
|
|
|
|
|
|
|
|
载入();
|