1492 lines
71 KiB
PHP
1492 lines
71 KiB
PHP
@php
|
||
// 数据由 Pages::contentForAccountMapping() 注入:
|
||
// $amRows 每条规则 + 该行的校验问题
|
||
// $amSummary 汇总(总数 / 错误行 / 警告行 / 提示数)
|
||
// $amOptions 筛选项(平台 / 大类 / 基准 / 票据)
|
||
// $amError 数据层读取失败时的提示
|
||
$amRows = $amRows ?? [];
|
||
$amSummary = $amSummary ?? ['total' => 0, 'error_rows' => 0, 'warning_rows' => 0, 'ok_rows' => 0, 'error_count' => 0, 'warning_count' => 0, 'tip_count' => 0];
|
||
$amOptions = $amOptions ?? ['platforms' => [], 'categories' => [], 'basis' => [], 'invoices' => []];
|
||
$amError = $amError ?? null;
|
||
|
||
$platforms = $amOptions['platforms'] ?? [];
|
||
$categories = $amOptions['categories'] ?? [];
|
||
$outBase = 0;
|
||
$inBase = 0;
|
||
foreach ($amRows as $r) {
|
||
if (($r['basis'] ?? '') === '支出基准') { $outBase++; }
|
||
if (($r['basis'] ?? '') === '收入基准') { $inBase++; }
|
||
}
|
||
@endphp
|
||
|
||
<div class="upstream-data-page upstream-data-page--spaced" data-account-mapping="true">
|
||
@include('business.pages.partials.upstream-data-styles')
|
||
|
||
<style>
|
||
.am-intro {
|
||
margin: 1.25rem 0 0;
|
||
padding: 1rem 1.25rem;
|
||
border: 1px solid var(--vip-border, #d8d2c7);
|
||
border-radius: 12px;
|
||
background: #fffdf8;
|
||
line-height: 1.75;
|
||
font-size: 0.8125rem;
|
||
color: #3d3a35;
|
||
}
|
||
.am-intro__title { font-weight: 600; font-size: 0.875rem; margin-bottom: 0.5rem; color: #1d1d1f; }
|
||
.am-basis {
|
||
display: inline-block;
|
||
padding: 0.1rem 0.5rem;
|
||
border-radius: 999px;
|
||
font-size: 0.75rem;
|
||
font-weight: 600;
|
||
white-space: nowrap;
|
||
}
|
||
.am-basis--out { background: #fdecec; color: #a32d2d; border: 1px solid #f3c3c3; }
|
||
.am-basis--in { background: #eaf3de; color: #3b6d11; border: 1px solid #c8e0a5; }
|
||
.am-flow { display: grid; gap: 0.5rem; margin-top: 0.5rem; }
|
||
.am-flow__row {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 0.5rem;
|
||
padding: 0.5rem 0.75rem;
|
||
border-radius: 8px;
|
||
background: var(--vip-content-bg, #f8f6f1);
|
||
font-size: 0.8125rem;
|
||
}
|
||
.am-flow__tag { flex: 0 0 auto; padding: 0.1rem 0.45rem; border-radius: 4px; font-size: 0.6875rem; font-weight: 600; }
|
||
.am-flow__tag--out { background: #fdecec; color: #a32d2d; }
|
||
.am-flow__tag--in { background: #eaf3de; color: #3b6d11; }
|
||
.am-flow__entry { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
|
||
.am-flow__note { margin-left: auto; font-size: 0.6875rem; color: #8a8377; white-space: nowrap; }
|
||
.am-item { cursor: pointer; }
|
||
.am-item:hover { background: #faf7f0; }
|
||
.am-item.is-editing { background: #fffaf0; }
|
||
.am-item.is-editing:hover { background: #fffaf0; }
|
||
.am-detail > td { padding: 0 !important; background: #fbfaf6; border-top: 0 !important; }
|
||
.am-detail__box { padding: 0.75rem 1rem 1rem; border-left: 3px solid var(--vip-content-accent, #b9954f); }
|
||
.am-detail__head { font-size: 0.75rem; color: #6b665e; margin-bottom: 0.35rem; }
|
||
.am-empty { padding: 2rem; text-align: center; color: #8a8377; font-size: 0.8125rem; }
|
||
.am-caret { display: inline-block; transition: transform 0.15s; color: #b0a99c; }
|
||
.am-item.is-open .am-caret { transform: rotate(90deg); }
|
||
|
||
/* 校验标记 */
|
||
.am-flag { display: inline-flex; align-items: center; gap: 0.25rem; font-size: 0.6875rem; font-weight: 600; }
|
||
.am-flag--err { color: #a32d2d; }
|
||
.am-flag--warn { color: #9a6b12; }
|
||
.am-flag--tip { color: #4a6a8a; }
|
||
.am-flag--ok { color: #6b8e4e; }
|
||
.am-dot { width: 0.5rem; height: 0.5rem; border-radius: 50%; display: inline-block; }
|
||
.am-dot--err { background: #d05a5a; }
|
||
.am-dot--warn { background: #d0a04a; }
|
||
.am-dot--tip { background: #7a9ab8; }
|
||
.am-dot--ok { background: #8fb26a; }
|
||
.am-issue-list { margin-top: 0.6rem; display: grid; gap: 0.3rem; }
|
||
.am-issue {
|
||
display: flex;
|
||
gap: 0.5rem;
|
||
padding: 0.4rem 0.6rem;
|
||
border-radius: 6px;
|
||
font-size: 0.75rem;
|
||
line-height: 1.6;
|
||
background: #fff;
|
||
border: 1px solid #eae4d8;
|
||
}
|
||
.am-issue__tag { flex: 0 0 auto; font-weight: 600; }
|
||
.am-issue--错误 { border-color: #f0cccc; background: #fdf5f5; }
|
||
.am-issue--错误 .am-issue__tag { color: #a32d2d; }
|
||
.am-issue--警告 { border-color: #f0e0c0; background: #fdfaf2; }
|
||
.am-issue--警告 .am-issue__tag { color: #8a5d10; }
|
||
.am-issue--提示 { border-color: #dce6ef; background: #f8fbfd; }
|
||
.am-issue--提示 .am-issue__tag { color: #40607e; }
|
||
|
||
/* 表格必须锁列宽(table-layout:fixed)。默认的自动列宽会按内容互相挤压,
|
||
「借方/贷方科目」两列会被挤到文字被截断;锁死后按上面 th 的宽度分配,
|
||
窗口不够宽时由外层 .upstream-data-table-scroll 横向滚动。
|
||
115rem = 各列宽度之和,改列宽时要一起改。 */
|
||
/* min-width 必须不小于各列宽度之和,否则窄屏下浏览器会按比例压缩各列,
|
||
固定的 4.5rem 平台列又会被压到装不下「唯品会JX」这类值。 */
|
||
.am-table { table-layout: fixed; min-width: 115rem; }
|
||
/* 只读单元格超长就省略号(编辑态那格要放开,否则下拉会被裁) */
|
||
.am-table > tbody > tr.am-item:not(.is-editing) > td { overflow: hidden; text-overflow: ellipsis; }
|
||
|
||
/* 序号列:3.5rem 才放得下三位数,居中并锁死数字等宽,
|
||
避免宽窗口下被按比例挤压后出现「1…」 */
|
||
.am-table .am-index {
|
||
width: 3.5rem;
|
||
min-width: 3.5rem;
|
||
max-width: 3.5rem;
|
||
text-align: center;
|
||
font-variant-numeric: tabular-nums;
|
||
}
|
||
|
||
/* 行内编辑 */
|
||
/* 注意:td 上有 height:2.5rem + white-space:nowrap(来自 upstream-data-table),
|
||
编辑态必须放开,否则一级/二级两个下拉会被压成一行、挤出单元格 */
|
||
.am-item.is-editing > td {
|
||
height: auto !important;
|
||
white-space: normal !important;
|
||
vertical-align: top;
|
||
padding-top: 0.5rem !important;
|
||
padding-bottom: 0.5rem !important;
|
||
}
|
||
.am-edit-cell input, .am-edit-cell select {
|
||
display: block;
|
||
box-sizing: border-box;
|
||
width: 100%;
|
||
max-width: 100%;
|
||
padding: 0.2rem 0.35rem;
|
||
border: 1px solid #d8d2c7;
|
||
border-radius: 6px;
|
||
font-size: 0.6875rem;
|
||
line-height: 1.5;
|
||
background: #fff;
|
||
color: #2c2a26;
|
||
}
|
||
/* 二级科目下拉:跟一级分开一行,颜色淡一点以示从属 */
|
||
.am-edit-cell select + select { margin-top: 0.25rem; border-color: #e6dfd0; color: #5c574e; }
|
||
.am-edit-cell input:focus, .am-edit-cell select:focus { outline: 2px solid #ddc99a; border-color: #c9ab63; }
|
||
.am-row-actions { display: flex; gap: 0.35rem; white-space: nowrap; }
|
||
.am-btn-mini {
|
||
padding: 0.2rem 0.5rem;
|
||
border-radius: 6px;
|
||
border: 1px solid #d8d2c7;
|
||
background: #fff;
|
||
font-size: 0.6875rem;
|
||
cursor: pointer;
|
||
color: #4a463f;
|
||
}
|
||
.am-btn-mini:hover { background: #f6f2e9; border-color: #c9ab63; }
|
||
.am-btn-mini--danger { color: #a32d2d; border-color: #edcccc; }
|
||
.am-btn-mini--danger:hover { background: #fdf3f3; border-color: #d99; }
|
||
.am-btn-mini--primary { background: #1d1d1f; color: #f0e4bf; border-color: #1d1d1f; }
|
||
.am-btn-mini--primary:hover { background: #333; color: #f7edd2; }
|
||
|
||
/* 工具栏补充按钮 */
|
||
.am-tool-actions { display: flex; gap: 0.5rem; align-items: center; flex-wrap: wrap; }
|
||
.am-btn {
|
||
padding: 0.4rem 0.85rem;
|
||
border-radius: 8px;
|
||
border: 1px solid #d8d2c7;
|
||
background: #fff;
|
||
font-size: 0.78rem;
|
||
cursor: pointer;
|
||
color: #3d3a35;
|
||
white-space: nowrap;
|
||
}
|
||
.am-btn:hover { border-color: #c9ab63; background: #faf6ec; }
|
||
.am-btn--primary { background: #1d1d1f; color: #f0e4bf; border-color: #1d1d1f; }
|
||
.am-btn--primary:hover { background: #333; color: #f7edd2; }
|
||
.am-btn[disabled] { opacity: 0.5; cursor: not-allowed; }
|
||
|
||
.am-summary { display: flex; flex-wrap: wrap; gap: 1.25rem; align-items: center; }
|
||
.am-summary__sep { color: #d8d2c7; }
|
||
|
||
/* 弹窗 */
|
||
.am-modal {
|
||
position: fixed;
|
||
inset: 0;
|
||
background: rgba(20, 18, 15, 0.42);
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
z-index: 9998;
|
||
padding: 1.5rem;
|
||
}
|
||
.am-modal[hidden] { display: none; }
|
||
.am-modal__box {
|
||
width: min(720px, 100%);
|
||
max-height: 86vh;
|
||
overflow: auto;
|
||
background: #fffdf8;
|
||
border-radius: 14px;
|
||
border: 1px solid #e4dccd;
|
||
box-shadow: 0 24px 60px rgba(0, 0, 0, 0.22);
|
||
}
|
||
/* 操作记录表头多、内容长,单开一个更宽的档位 */
|
||
.am-modal__box--wide { width: min(1120px, 100%); }
|
||
.am-modal__head {
|
||
padding: 1rem 1.25rem;
|
||
border-bottom: 1px solid #eee7da;
|
||
font-weight: 600;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
}
|
||
.am-modal__body { padding: 1.25rem; font-size: 0.8125rem; line-height: 1.8; color: #3d3a35; }
|
||
.am-modal__foot { padding: 1rem 1.25rem; border-top: 1px solid #eee7da; display: flex; gap: 0.5rem; justify-content: flex-end; }
|
||
.am-modal__close { border: 0; background: transparent; font-size: 1.1rem; cursor: pointer; color: #8a8377; }
|
||
|
||
/* 操作记录 */
|
||
/* 多选筛选(财务备注 / 平台 / 大类 / 方向基准):点按钮展开复选列表,可同时选多个 */
|
||
.am-ms { position: relative; }
|
||
.am-ms__box { position: relative; display: block; }
|
||
.am-ms__btn {
|
||
display: flex;
|
||
width: 100%;
|
||
height: 2.125rem;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
gap: 0.4rem;
|
||
padding: 0 0.625rem;
|
||
border: 1px solid #d8d2c7;
|
||
border-radius: 0.5rem;
|
||
background: #fff;
|
||
color: #171717;
|
||
font: inherit;
|
||
font-size: 0.8125rem;
|
||
text-align: left;
|
||
cursor: pointer;
|
||
}
|
||
.am-ms__btn:hover { border-color: #c4b79f; }
|
||
.am-ms__btn[aria-expanded="true"] { border-color: #856632; box-shadow: 0 0 0 3px rgba(133, 102, 50, 0.2); }
|
||
.am-ms__btn--on { font-weight: 600; color: #6f5427; }
|
||
.am-ms__label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||
.am-ms__caret { flex: 0 0 auto; font-size: 0.625rem; color: #8a8377; }
|
||
.am-ms__panel {
|
||
position: absolute;
|
||
top: calc(100% + 0.3rem);
|
||
left: 0;
|
||
z-index: 70;
|
||
min-width: 12rem;
|
||
max-height: 16rem;
|
||
overflow: auto;
|
||
padding: 0.35rem;
|
||
background: #fffdf8;
|
||
border: 1px solid #e4dccd;
|
||
border-radius: 8px;
|
||
box-shadow: 0 14px 30px rgba(0, 0, 0, 0.14);
|
||
}
|
||
.am-ms__panel[hidden] { display: none; }
|
||
.am-ms__empty { padding: 0.4rem 0.5rem; font-size: 0.75rem; color: #8a8377; }
|
||
.am-ms__search {
|
||
width: 100%;
|
||
height: 1.9rem;
|
||
min-width: 0;
|
||
padding: 0 0.5rem;
|
||
border: 1px solid #ddd5c7;
|
||
border-radius: 0.4rem;
|
||
background: #fff;
|
||
color: #3d3a35;
|
||
font: inherit;
|
||
font-size: 0.8125rem;
|
||
outline: none;
|
||
}
|
||
.am-ms__search:focus { border-color: #856632; box-shadow: 0 0 0 3px rgba(133, 102, 50, 0.2); }
|
||
.am-ms__opts { display: block; }
|
||
.am-ms__head { display: flex; gap: 0.35rem; padding: 0.15rem 0.15rem 0.4rem; margin-bottom: 0.25rem; border-bottom: 1px solid #efe8db; }
|
||
.am-ms__head button {
|
||
flex: 1 1 auto;
|
||
height: auto;
|
||
padding: 0.2rem 0.35rem;
|
||
border: 1px solid #e4dccd;
|
||
border-radius: 6px;
|
||
background: #fff;
|
||
color: #3d3a35;
|
||
font: inherit;
|
||
font-size: 0.6875rem;
|
||
cursor: pointer;
|
||
}
|
||
.am-ms__head button:hover { background: #f6f2e9; }
|
||
.am-ms__opt {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 0.4rem;
|
||
padding: 0.3rem 0.35rem;
|
||
border-radius: 6px;
|
||
font-size: 0.8125rem;
|
||
color: #3d3a35;
|
||
cursor: pointer;
|
||
}
|
||
.am-ms__opt:hover { background: #f6f2e9; }
|
||
.am-ms__panel .am-ms__opt input {
|
||
position: relative;
|
||
flex: 0 0 auto;
|
||
width: 0.95rem;
|
||
height: 0.95rem;
|
||
min-width: 0;
|
||
margin: 0;
|
||
padding: 0;
|
||
border: 1px solid #cfc6b4;
|
||
border-radius: 4px;
|
||
background: #fff;
|
||
appearance: none;
|
||
-webkit-appearance: none;
|
||
cursor: pointer;
|
||
}
|
||
.am-ms__panel .am-ms__opt input:checked { border-color: #856632; background: #856632; }
|
||
.am-ms__panel .am-ms__opt input:checked::after {
|
||
content: '✓';
|
||
position: absolute;
|
||
inset: 0;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
color: #fff;
|
||
font-size: 0.6875rem;
|
||
line-height: 1;
|
||
}
|
||
.am-ms__panel .am-ms__opt input:focus-visible { box-shadow: 0 0 0 3px rgba(133, 102, 50, 0.25); }
|
||
|
||
.am-hist-tools { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; margin-bottom: 0.75rem; }
|
||
.am-hist-tools select,
|
||
.am-hist-tools input {
|
||
height: 2rem; border: 1px solid #ddd5c7; border-radius: 8px; background: #fff;
|
||
color: #3d3a35; padding: 0 0.5rem; font-size: 0.8125rem;
|
||
}
|
||
.am-hist-tools input { min-width: 12rem; }
|
||
.am-hist-scroll { max-height: 58vh; overflow: auto; border: 1px solid #eee7da; border-radius: 8px; }
|
||
table.am-hist-table { width: 100%; border-collapse: collapse; font-size: 0.8125rem; }
|
||
.am-hist-table th {
|
||
position: sticky; top: 0; z-index: 1; background: #f7f3ea; text-align: left;
|
||
padding: 0.5rem 0.65rem; border-bottom: 1px solid #eee7da; color: #5c5546;
|
||
font-weight: 600; white-space: nowrap;
|
||
}
|
||
.am-hist-table td { padding: 0.55rem 0.65rem; border-bottom: 1px solid #f3eee4; color: #3d3a35; vertical-align: top; }
|
||
.am-hist-table tr:last-child td { border-bottom: 0; }
|
||
.am-hist-at { white-space: nowrap; color: #6b6455; }
|
||
.am-hist-meta { color: #8a8377; font-size: 0.75rem; }
|
||
.am-hist-tag { display: inline-block; padding: 0.1rem 0.45rem; border-radius: 999px; font-size: 0.75rem; font-weight: 600; white-space: nowrap; }
|
||
.am-hist-tag--create { background: #eaf3de; color: #3b6d11; }
|
||
.am-hist-tag--update { background: #eaf0f7; color: #3f6485; }
|
||
.am-hist-tag--delete { background: #fdecec; color: #a32d2d; }
|
||
.am-hist-tag--import { background: #fbf3e2; color: #8a6414; }
|
||
.am-hist-tag--reset { background: #f2efe7; color: #6b6455; }
|
||
.am-hist-changes { margin: 0; padding-left: 1rem; }
|
||
.am-hist-changes li { margin: 0.1rem 0; }
|
||
.am-kv { display: grid; grid-template-columns: 7rem 1fr; gap: 0.4rem 0.75rem; margin: 0.75rem 0; }
|
||
.am-kv__k { color: #8a8377; }
|
||
.am-preview-table { width: 100%; border-collapse: collapse; font-size: 0.75rem; margin-top: 0.5rem; }
|
||
.am-preview-table th, .am-preview-table td { border: 1px solid #eee7da; padding: 0.3rem 0.45rem; text-align: left; }
|
||
.am-preview-table th { background: #f6f2e9; font-weight: 600; }
|
||
.am-check { display: inline-flex; align-items: center; gap: 0.4rem; margin-top: 0.75rem; }
|
||
|
||
/* 轻提示 */
|
||
.am-toast {
|
||
position: fixed;
|
||
right: 1.5rem;
|
||
bottom: 1.5rem;
|
||
z-index: 9999;
|
||
padding: 0.7rem 1rem;
|
||
border-radius: 10px;
|
||
font-size: 0.8125rem;
|
||
box-shadow: 0 12px 30px rgba(0, 0, 0, 0.18);
|
||
max-width: 26rem;
|
||
line-height: 1.6;
|
||
}
|
||
.am-toast[hidden] { display: none; }
|
||
.am-toast--ok { background: #1d1d1f; color: #f0e4bf; }
|
||
.am-toast--err { background: #8e2f2f; color: #fff; }
|
||
|
||
.am-alert {
|
||
margin-top: 1rem;
|
||
padding: 0.75rem 1rem;
|
||
border-radius: 10px;
|
||
border: 1px solid #f0cccc;
|
||
background: #fdf5f5;
|
||
color: #8e2f2f;
|
||
font-size: 0.8125rem;
|
||
}
|
||
|
||
@media (max-width: 768px) {
|
||
.am-flow__note { display: none; }
|
||
.am-intro { font-size: 0.78rem; }
|
||
}
|
||
</style>
|
||
|
||
<section class="am-intro">
|
||
<div class="am-intro__title">这张表的用法:科目对固定,借贷方向随账单收支走</div>
|
||
每条规则只锁定「走哪两个科目」和「方向基准」两件事。实际记账时,按这笔账单在平台上的实际收支代入:
|
||
<br>
|
||
· 基准为 <span class="am-basis am-basis--out">支出基准</span> 的项目 —— 账单<b>支出</b>时<b>照抄</b>表内方向;账单<b>收入</b>(返还 / 退回)时<b>借贷互换</b>。
|
||
<br>
|
||
· 基准为 <span class="am-basis am-basis--in">收入基准</span> 的项目 —— 账单<b>收入</b>时<b>照抄</b>表内方向;账单<b>支出</b>(退款 / 冲减)时<b>借贷互换</b>。
|
||
<br>
|
||
· 同一个账单项目在业务上不是同一件事时,用<b>财务备注</b>/<b>商品名称关键词</b>区分:财务备注匹配账单的二次备注、账务备注等(<b>202607订单</b> 这类月份式备注不参与匹配),商品名称按包含匹配;两个都填最优先,其次商品名称,再次财务备注,都不填就是兜底规则。
|
||
<br>
|
||
<span style="color:#8a8377;">点行展开看两种收支下的最终分录与校验结果;点「编辑」可直接改这一行;有新增账单项目时用「导入」追加。</span>
|
||
</section>
|
||
|
||
@if ($amError)
|
||
<div class="am-alert">读取映射表失败:{{ $amError }}</div>
|
||
@endif
|
||
|
||
<div class="upstream-data-summary">
|
||
<div class="am-summary" id="amSummaryBar">
|
||
<span>共 <b id="amTotal">{{ $amSummary['total'] }}</b> 条规则</span>
|
||
<span>支出基准 <b id="amOut">{{ $outBase }}</b></span>
|
||
<span>收入基准 <b id="amIn">{{ $inBase }}</b></span>
|
||
<span>平台 <b id="amPlatformCount">{{ count($platforms) }}</b> 个</span>
|
||
<span class="am-summary__sep">|</span>
|
||
<span>校验:<b id="amErrRows" style="color:#a32d2d;">{{ $amSummary['error_rows'] }}</b> 行有错误 ·
|
||
<b id="amWarnRows" style="color:#8a5d10;">{{ $amSummary['warning_rows'] }}</b> 行有警告 ·
|
||
<b id="amTipCount" style="color:#40607e;">{{ $amSummary['tip_count'] }}</b> 条提示</span>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="upstream-data-toolbar">
|
||
<label class="upstream-data-field">
|
||
<span>搜索</span>
|
||
<input type="search" id="amKeyword" placeholder="平台 / 账单项目 / 备注 / 科目">
|
||
</label>
|
||
<div class="upstream-data-field am-ms" id="amMemo">
|
||
<span>财务备注</span>
|
||
<span class="am-ms__box">
|
||
<button type="button" class="am-ms__btn" data-role="btn" aria-expanded="false">
|
||
<span class="am-ms__label">全部</span><span class="am-ms__caret">▾</span>
|
||
</button>
|
||
<span class="am-ms__panel" data-role="panel" hidden></span>
|
||
</span>
|
||
</div>
|
||
<div class="upstream-data-field am-ms" id="amPlatform">
|
||
<span>平台</span>
|
||
<span class="am-ms__box">
|
||
<button type="button" class="am-ms__btn" data-role="btn" aria-expanded="false">
|
||
<span class="am-ms__label">全部</span><span class="am-ms__caret">▾</span>
|
||
</button>
|
||
<span class="am-ms__panel" data-role="panel" hidden></span>
|
||
</span>
|
||
</div>
|
||
<div class="upstream-data-field am-ms" id="amCategory">
|
||
<span>财务类型</span>
|
||
<span class="am-ms__box">
|
||
<button type="button" class="am-ms__btn" data-role="btn" aria-expanded="false">
|
||
<span class="am-ms__label">全部</span><span class="am-ms__caret">▾</span>
|
||
</button>
|
||
<span class="am-ms__panel" data-role="panel" hidden></span>
|
||
</span>
|
||
</div>
|
||
<div class="upstream-data-field am-ms" id="amBasis">
|
||
<span>方向基准</span>
|
||
<span class="am-ms__box">
|
||
<button type="button" class="am-ms__btn" data-role="btn" aria-expanded="false">
|
||
<span class="am-ms__label">全部</span><span class="am-ms__caret">▾</span>
|
||
</button>
|
||
<span class="am-ms__panel" data-role="panel" hidden></span>
|
||
</span>
|
||
</div>
|
||
<label class="upstream-data-field">
|
||
<span>只看</span>
|
||
<select id="amOnly">
|
||
<option value="">全部</option>
|
||
<option value="issue">有问题的行</option>
|
||
<option value="error">只看错误</option>
|
||
</select>
|
||
</label>
|
||
<div class="upstream-data-actions am-tool-actions">
|
||
<button type="button" class="am-btn am-btn--primary" id="amAdd">+ 新增一条</button>
|
||
<button type="button" class="am-btn" id="amImportBtn">导入</button>
|
||
<button type="button" class="am-btn" id="amExportBtn">导出 CSV</button>
|
||
<button type="button" class="am-btn" id="amTemplateBtn">导入模板</button>
|
||
<button type="button" class="am-btn" id="amHistoryBtn">操作记录</button>
|
||
<button type="button" class="am-btn" id="amResetFilter">重置筛选</button>
|
||
<span id="amCount" style="font-size:0.75rem;color:#8a8377;"></span>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="upstream-data-table-panel">
|
||
<div class="upstream-data-table-scroll">
|
||
<table class="upstream-data-table am-table">
|
||
<thead>
|
||
<tr>
|
||
<th class="am-index" style="width:3.5rem;">序号</th>
|
||
<th style="width:6.5rem;">平台</th>
|
||
<th style="width:17rem;">账单项目</th>
|
||
<th style="width:11rem;">财务备注</th>
|
||
<th style="width:11rem;">商品名称关键词</th>
|
||
<th style="width:5rem;">票据</th>
|
||
<th style="width:8rem;">财务类型</th>
|
||
<th style="width:9rem;">费用备注</th>
|
||
<th style="width:12rem;">借方科目</th>
|
||
<th style="width:12rem;">贷方科目</th>
|
||
<th style="width:6rem;">方向基准</th>
|
||
<th style="width:6rem;">校验</th>
|
||
<th style="width:8rem;">操作</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody id="amBody"></tbody>
|
||
</table>
|
||
</div>
|
||
<div class="am-empty" id="amEmpty" hidden>没有符合条件的数据。</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="am-modal" id="amModal" hidden>
|
||
<div class="am-modal__box">
|
||
<div class="am-modal__head">
|
||
<span id="amModalTitle">导入科目映射表</span>
|
||
<button type="button" class="am-modal__close" id="amModalClose">✕</button>
|
||
</div>
|
||
<div class="am-modal__body" id="amModalBody"></div>
|
||
<div class="am-modal__foot" id="amModalFoot"></div>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="am-toast" id="amToast" hidden></div>
|
||
|
||
<input type="file" id="amFile" accept=".xlsx,.xls,.csv" hidden>
|
||
|
||
<script>
|
||
(function () {
|
||
var CSRF = '{{ csrf_token() }}';
|
||
var ENDPOINT = @json($amEndpoint ?? url('workspace/financial-rules/account-mapping/actions'));
|
||
|
||
var ROWS = @json($amRows, JSON_UNESCAPED_UNICODE);
|
||
var OPTIONS = @json($amOptions, JSON_UNESCAPED_UNICODE);
|
||
var SUMMARY = @json($amSummary, JSON_UNESCAPED_UNICODE);
|
||
var COA = null;
|
||
var editingId = null;
|
||
var draft = null;
|
||
var openId = null;
|
||
|
||
// 大类 → 预期方向基准:改大类时顺手把基准带过去,避免两者打架
|
||
var CATEGORY_BASIS = {
|
||
'费用': '支出基准', '售后费用': '支出基准', '平台服务费': '支出基准', '平台佣金': '支出基准',
|
||
'推广': '支出基准', '保证金': '支出基准', '暂挂': '支出基准',
|
||
'待处理财产损益': '支出基准', '营业外支出': '支出基准', '提现': '支出基准',
|
||
'税费': '支出基准',
|
||
'收入': '收入基准', '往来': '收入基准', '转账': '收入基准'
|
||
};
|
||
|
||
var body = document.getElementById('amBody');
|
||
var empty = document.getElementById('amEmpty');
|
||
var countEl = document.getElementById('amCount');
|
||
var kw = document.getElementById('amKeyword');
|
||
var fOnly = document.getElementById('amOnly');
|
||
var toastEl = document.getElementById('amToast');
|
||
|
||
/* ---------- 多选筛选(账单项目 / 平台 / 大类 / 方向基准) ----------
|
||
这几个维度经常要同时看几项(例如淘宝+天猫、费用+平台佣金),
|
||
所以用「按钮 + 复选列表」,一个都不勾=全部。
|
||
账单项目额外带一个搜索框:候选太多(三百多个),光靠滚找不到。 */
|
||
var multiSelects = [];
|
||
|
||
function createMultiSelect(root, opts) {
|
||
opts = opts || {};
|
||
var btn = root.querySelector('[data-role="btn"]');
|
||
var labelEl = btn.querySelector('.am-ms__label');
|
||
var panel = root.querySelector('[data-role="panel"]');
|
||
var selectedList = []; // 勾选的值,按勾选先后排
|
||
var fixedOrder = []; // 固定候选(平台 / 大类 / 方向基准)
|
||
var keyword = '';
|
||
var optsEl = null;
|
||
var searchEl = null;
|
||
|
||
function isSel(v) { return selectedList.indexOf(v) >= 0; }
|
||
function values() { return selectedList.slice(); }
|
||
function keywordValue() { return keyword; }
|
||
function source() { return opts.dynamic ? opts.dynamic() : fixedOrder; }
|
||
function listed() {
|
||
var all = source();
|
||
if (!keyword) { return all.slice(); }
|
||
return all.filter(function (v) { return String(v).toLowerCase().indexOf(keyword) >= 0; });
|
||
}
|
||
|
||
// 面板骨架只建一次:搜索框打字时只重画下面的选项,避免把输入框自己重绘掉
|
||
function buildShell() {
|
||
if (optsEl) { return; }
|
||
panel.innerHTML = (opts.searchable
|
||
? '<span class="am-ms__head"><input type="search" class="am-ms__search" placeholder="'
|
||
+ esc(opts.searchPlaceholder || '打字找关键字') + '"></span>'
|
||
: '')
|
||
+ '<span class="am-ms__head">'
|
||
+ '<button type="button" data-ms-act="all">全选</button>'
|
||
+ '<button type="button" data-ms-act="none">清空</button>'
|
||
+ '</span>'
|
||
+ '<span class="am-ms__opts"></span>';
|
||
optsEl = panel.querySelector('.am-ms__opts');
|
||
searchEl = panel.querySelector('.am-ms__search');
|
||
if (searchEl) {
|
||
searchEl.addEventListener('input', function () {
|
||
keyword = searchEl.value.trim().toLowerCase();
|
||
renderOptions();
|
||
sync();
|
||
});
|
||
}
|
||
}
|
||
|
||
function renderOptions() {
|
||
if (!optsEl) { return; }
|
||
var items = listed();
|
||
if (!items.length) {
|
||
optsEl.innerHTML = '<div class="am-ms__empty">'
|
||
+ (keyword ? '没有匹配的项,换个关键字试试' : '暂无可选项') + '</div>';
|
||
return;
|
||
}
|
||
var html = items.slice(0, 300).map(function (v) {
|
||
return '<label class="am-ms__opt"><input type="checkbox" value="' + esc(v) + '"'
|
||
+ (isSel(v) ? ' checked' : '') + '><span>' + esc(v) + '</span></label>';
|
||
}).join('');
|
||
if (items.length > 300) {
|
||
html += '<div class="am-ms__empty">还有 ' + (items.length - 300) + ' 项,再打精确一点</div>';
|
||
}
|
||
optsEl.innerHTML = html;
|
||
}
|
||
|
||
function sync() {
|
||
if (!selectedList.length) {
|
||
labelEl.textContent = '全部';
|
||
btn.classList.remove('am-ms__btn--on');
|
||
} else if (selectedList.length === 1) {
|
||
labelEl.textContent = selectedList[0];
|
||
btn.classList.add('am-ms__btn--on');
|
||
} else {
|
||
labelEl.textContent = selectedList[0] + ' 等 ' + selectedList.length + ' 项';
|
||
btn.classList.add('am-ms__btn--on');
|
||
}
|
||
btn.title = selectedList.length ? selectedList.join('、') : '全部';
|
||
if (typeof opts.onChange === 'function') { opts.onChange(); }
|
||
}
|
||
|
||
function closePanel() {
|
||
panel.hidden = true;
|
||
btn.setAttribute('aria-expanded', 'false');
|
||
}
|
||
|
||
var api = {
|
||
root: root,
|
||
values: values,
|
||
keyword: keywordValue,
|
||
close: closePanel,
|
||
// 刷新候选:固定候选会顺手丢掉已经不存在的已选项
|
||
refresh: function (fixed) {
|
||
if (fixed) {
|
||
fixedOrder = fixed.slice();
|
||
selectedList = selectedList.filter(function (v) { return fixedOrder.indexOf(v) >= 0; });
|
||
}
|
||
renderOptions();
|
||
sync();
|
||
},
|
||
clear: function () {
|
||
selectedList = [];
|
||
keyword = '';
|
||
if (searchEl) { searchEl.value = ''; }
|
||
renderOptions();
|
||
sync();
|
||
}
|
||
};
|
||
|
||
btn.addEventListener('click', function (ev) {
|
||
ev.stopPropagation();
|
||
if (panel.hidden) {
|
||
multiSelects.forEach(function (ms) { if (ms !== api) { ms.close(); } });
|
||
buildShell();
|
||
renderOptions();
|
||
panel.hidden = false;
|
||
btn.setAttribute('aria-expanded', 'true');
|
||
if (searchEl) { searchEl.focus(); }
|
||
} else {
|
||
closePanel();
|
||
}
|
||
});
|
||
panel.addEventListener('click', function (ev) {
|
||
ev.stopPropagation();
|
||
var act = ev.target.closest('button[data-ms-act]');
|
||
if (!act) { return; }
|
||
if (act.getAttribute('data-ms-act') === 'all') {
|
||
listed().forEach(function (v) { if (!isSel(v)) { selectedList.push(v); } });
|
||
} else {
|
||
selectedList = [];
|
||
keyword = '';
|
||
if (searchEl) { searchEl.value = ''; }
|
||
}
|
||
renderOptions();
|
||
sync();
|
||
});
|
||
panel.addEventListener('change', function (ev) {
|
||
var cb = ev.target.closest('input[type="checkbox"]');
|
||
if (!cb) { return; }
|
||
if (cb.checked) {
|
||
if (!isSel(cb.value)) { selectedList.push(cb.value); }
|
||
} else {
|
||
selectedList = selectedList.filter(function (v) { return v !== cb.value; });
|
||
}
|
||
sync();
|
||
});
|
||
|
||
multiSelects.push(api);
|
||
return api;
|
||
}
|
||
|
||
var msPlatform = createMultiSelect(document.getElementById('amPlatform'), {
|
||
onChange: function () { rebuildMemoOptions(); render(); }
|
||
});
|
||
var msCategory = createMultiSelect(document.getElementById('amCategory'), { onChange: render });
|
||
var msBasis = createMultiSelect(document.getElementById('amBasis'), { onChange: render });
|
||
var msMemo = createMultiSelect(document.getElementById('amMemo'), {
|
||
searchable: true,
|
||
searchPlaceholder: '打字找财务备注(可多选)',
|
||
dynamic: memoCandidates,
|
||
onChange: render
|
||
});
|
||
|
||
// 点空白处 / 按 Esc 收起候选面板
|
||
document.addEventListener('click', function (ev) {
|
||
multiSelects.forEach(function (ms) { if (!ms.root.contains(ev.target)) { ms.close(); } });
|
||
});
|
||
document.addEventListener('keydown', function (ev) {
|
||
if (ev.key === 'Escape') { multiSelects.forEach(function (ms) { ms.close(); }); }
|
||
});
|
||
|
||
/* ---------- 小工具 ---------- */
|
||
function esc(s) {
|
||
return String(s == null ? '' : s).replace(/[&<>"']/g, function (c) {
|
||
return { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c];
|
||
});
|
||
}
|
||
function acct(a, b) {
|
||
a = String(a || '').trim();
|
||
b = String(b || '').trim();
|
||
if (!a && !b) { return '—'; }
|
||
if (b && b !== a) { return esc(a) + ' / ' + esc(b); }
|
||
return esc(a || b);
|
||
}
|
||
// 同上,但不转义、不带 HTML:给 title 用(列宽固定,长文本会被截断,悬停看全文)
|
||
function acctText(a, b) {
|
||
a = String(a || '').trim();
|
||
b = String(b || '').trim();
|
||
if (!a && !b) { return '—'; }
|
||
if (b && b !== a) { return a + ' / ' + b; }
|
||
return a || b;
|
||
}
|
||
function levels(issues) {
|
||
var out = { error: 0, warning: 0, tip: 0 };
|
||
(issues || []).forEach(function (i) {
|
||
if (i.level === '错误') { out.error++; }
|
||
else if (i.level === '警告') { out.warning++; }
|
||
else { out.tip++; }
|
||
});
|
||
return out;
|
||
}
|
||
function flagHtml(issues) {
|
||
var n = levels(issues);
|
||
if (n.error) { return '<span class="am-flag am-flag--err"><span class="am-dot am-dot--err"></span>错误 ' + n.error + '</span>'; }
|
||
if (n.warning) { return '<span class="am-flag am-flag--warn"><span class="am-dot am-dot--warn"></span>警告 ' + n.warning + '</span>'; }
|
||
if (n.tip) { return '<span class="am-flag am-flag--tip"><span class="am-dot am-dot--tip"></span>提示 ' + n.tip + '</span>'; }
|
||
return '<span class="am-flag am-flag--ok"><span class="am-dot am-dot--ok"></span>正常</span>';
|
||
}
|
||
function basisTag(b) {
|
||
if (b === '支出基准') { return '<span class="am-basis am-basis--out">支出基准</span>'; }
|
||
if (b === '收入基准') { return '<span class="am-basis am-basis--in">收入基准</span>'; }
|
||
return '<span style="color:#b04a4a;">未标注</span>';
|
||
}
|
||
function toast(msg, kind) {
|
||
toastEl.className = 'am-toast am-toast--' + (kind === 'err' ? 'err' : 'ok');
|
||
toastEl.textContent = msg;
|
||
toastEl.hidden = false;
|
||
clearTimeout(toastEl._t);
|
||
toastEl._t = setTimeout(function () { toastEl.hidden = true; }, 4200);
|
||
}
|
||
|
||
/* ---------- 核心:按账单实际收支 + 本行基准,决定照抄还是互换 ---------- */
|
||
function entry(r, flow) {
|
||
var natural = (r.basis === '支出基准') ? 'out' : 'in';
|
||
var swap = (flow !== natural);
|
||
var d = swap ? [r.cAcct, r.cSub] : [r.dAcct, r.dSub];
|
||
var c = swap ? [r.dAcct, r.dSub] : [r.cAcct, r.cSub];
|
||
return { swap: swap, d: d, c: c };
|
||
}
|
||
|
||
function detailHtml(r) {
|
||
var out = entry(r, 'out');
|
||
var inn = entry(r, 'in');
|
||
function line(tag, label, e) {
|
||
return '<div class="am-flow__row">'
|
||
+ '<span class="am-flow__tag am-flow__tag--' + tag + '">' + label + '</span>'
|
||
+ '<span class="am-flow__entry">借 ' + acct(e.d[0], e.d[1]) + ' / 贷 ' + acct(e.c[0], e.c[1]) + '</span>'
|
||
+ '<span class="am-flow__note">' + (e.swap ? '借贷互换' : '照抄表内方向') + '</span>'
|
||
+ '</div>';
|
||
}
|
||
var issues = r.issues || [];
|
||
var issuesHtml = '';
|
||
if (issues.length) {
|
||
issuesHtml = '<div class="am-issue-list">' + issues.map(function (i) {
|
||
return '<div class="am-issue am-issue--' + esc(i.level) + '">'
|
||
+ '<span class="am-issue__tag">' + esc(i.level) + '</span>'
|
||
+ '<span>' + esc(i.message) + '</span></div>';
|
||
}).join('') + '</div>';
|
||
}
|
||
return '<tr class="am-detail"><td colspan="13"><div class="am-detail__box">'
|
||
+ '<div class="am-detail__head">' + esc(r.platform) + ' · ' + esc(r.item)
|
||
+ ' (费用备注:' + esc(r.feeMemo || '—') + ',基准:' + esc(r.basis) + ',记录 ' + r.id + ')</div>'
|
||
+ '<div class="am-flow">'
|
||
+ line('out', '账单支出', out)
|
||
+ line('in', '账单收入', inn)
|
||
+ '</div>' + issuesHtml + '</div></td></tr>';
|
||
}
|
||
|
||
/* ---------- 渲染 ---------- */
|
||
// 列表顺序:按平台归类(平台顺序=各平台第一条规则的出现顺序),平台内按编号。
|
||
// 序号=这个顺序里的位置,所以增删会自动重排,但筛选不会让序号变化。
|
||
function sortedRows() {
|
||
var order = {};
|
||
var next = 0;
|
||
ROWS.forEach(function (r) {
|
||
if (!Object.prototype.hasOwnProperty.call(order, r.platform)) {
|
||
order[r.platform] = next++;
|
||
}
|
||
});
|
||
return ROWS.slice().sort(function (a, b) {
|
||
if (order[a.platform] !== order[b.platform]) {
|
||
return order[a.platform] - order[b.platform];
|
||
}
|
||
return a.id - b.id;
|
||
});
|
||
}
|
||
|
||
var SEQ = {};
|
||
function rebuildSeq() {
|
||
SEQ = {};
|
||
sortedRows().forEach(function (r, i) { SEQ[r.id] = i + 1; });
|
||
}
|
||
|
||
function visibleRows() {
|
||
var keyword = (kw.value || '').trim().toLowerCase();
|
||
var platforms = msPlatform.values();
|
||
var memos = msMemo.values();
|
||
var memoKeyword = msMemo.keyword();
|
||
var categories = msCategory.values();
|
||
var bases = msBasis.values();
|
||
return sortedRows().filter(function (r) {
|
||
if (platforms.length && platforms.indexOf(r.platform) < 0) { return false; }
|
||
// 财务备注:勾选的按精确匹配(可多选,取并集);候选项列表里打的关键字按「包含」匹配
|
||
if (memos.length && memos.indexOf(String(r.memo || '')) < 0) { return false; }
|
||
if (memoKeyword && String(r.memo || '').toLowerCase().indexOf(memoKeyword) < 0) { return false; }
|
||
if (categories.length && categories.indexOf(r.category) < 0) { return false; }
|
||
if (bases.length && bases.indexOf(r.basis) < 0) { return false; }
|
||
if (fOnly.value) {
|
||
var n = levels(r.issues);
|
||
if (fOnly.value === 'error' && !n.error) { return false; }
|
||
if (fOnly.value === 'issue' && !n.error && !n.warning && !n.tip) { return false; }
|
||
}
|
||
if (keyword) {
|
||
var hay = [r.platform, r.item, r.memo, r.goods, r.dAcct, r.dSub, r.cAcct, r.cSub, r.category, r.feeMemo, r.basis]
|
||
.join(' ').toLowerCase();
|
||
if (hay.indexOf(keyword) < 0) { return false; }
|
||
}
|
||
return true;
|
||
});
|
||
}
|
||
|
||
function rowHtml(r, index) {
|
||
var edit = (editingId === r.id && draft);
|
||
if (edit) {
|
||
return '<tr class="am-item is-editing" data-id="' + r.id + '">'
|
||
+ '<td class="am-index">' + (SEQ[r.id] || r.id) + '</td>'
|
||
+ '<td class="am-edit-cell"><input data-f="platform" value="' + esc(draft.platform) + '" list="amPlatformList"></td>'
|
||
+ '<td class="am-edit-cell"><input data-f="item" value="' + esc(draft.item) + '"></td>'
|
||
+ '<td class="am-edit-cell"><input data-f="memo" value="' + esc(draft.memo) + '" placeholder="不限就填 -"></td>'
|
||
+ '<td class="am-edit-cell"><input data-f="goods" value="' + esc(draft.goods) + '" placeholder="不限就填 -"></td>'
|
||
+ '<td class="am-edit-cell"><select data-f="invoice">' + optionsHtml(OPTIONS.invoices || [], draft.invoice) + '</select></td>'
|
||
+ '<td class="am-edit-cell"><select data-f="category">' + optionsHtml(OPTIONS.categories || [], draft.category) + '</select></td>'
|
||
+ '<td class="am-edit-cell"><input data-f="feeMemo" value="' + esc(draft.feeMemo) + '" placeholder="费用备注"></td>'
|
||
+ '<td class="am-edit-cell">' + acctEditor('dAcct', 'dSub') + '</td>'
|
||
+ '<td class="am-edit-cell">' + acctEditor('cAcct', 'cSub') + '</td>'
|
||
+ '<td class="am-edit-cell"><select data-f="basis">' + optionsHtml(OPTIONS.basis || [], draft.basis) + '</select></td>'
|
||
+ '<td>' + flagHtml(r.issues) + '</td>'
|
||
+ '<td><div class="am-row-actions">'
|
||
+ '<button type="button" class="am-btn-mini am-btn-mini--primary" data-act="save">保存</button>'
|
||
+ '<button type="button" class="am-btn-mini" data-act="cancel">取消</button>'
|
||
+ '</div></td></tr>';
|
||
}
|
||
return '<tr class="am-item" data-id="' + r.id + '" data-index="' + index + '">'
|
||
+ '<td class="am-index">' + (SEQ[r.id] || r.id) + '</td>'
|
||
+ '<td><span title="' + esc(r.platform) + '">' + esc(r.platform) + '</span></td>'
|
||
+ '<td><span class="am-caret">▶</span> <span title="' + esc(r.item) + '">' + esc(r.item) + '</span></td>'
|
||
+ '<td><span title="' + esc(r.memo) + '">' + esc(r.memo || '-') + '</span></td>'
|
||
+ '<td><span title="' + esc(r.goods) + '">' + esc(r.goods || '-') + '</span></td>'
|
||
+ '<td>' + esc(r.invoice || '—') + '</td>'
|
||
+ '<td><span title="' + esc(r.category) + '">' + esc(r.category) + '</span></td>'
|
||
+ '<td><span title="' + esc(r.feeMemo) + '">' + esc(r.feeMemo || '-') + '</span></td>'
|
||
+ '<td><span title="' + esc(acctText(r.dAcct, r.dSub)) + '">' + acct(r.dAcct, r.dSub) + '</span></td>'
|
||
+ '<td><span title="' + esc(acctText(r.cAcct, r.cSub)) + '">' + acct(r.cAcct, r.cSub) + '</span></td>'
|
||
+ '<td>' + basisTag(r.basis) + '</td>'
|
||
+ '<td>' + flagHtml(r.issues) + '</td>'
|
||
+ '<td><div class="am-row-actions">'
|
||
+ '<button type="button" class="am-btn-mini" data-act="edit">编辑</button>'
|
||
+ '<button type="button" class="am-btn-mini am-btn-mini--danger" data-act="delete">删除</button>'
|
||
+ '</div></td></tr>';
|
||
}
|
||
|
||
// placeholder:第一个空 option 的显示文字。不传就是纯空白 —— 但纯空白会让人以为
|
||
// 下拉是坏的(比如「其他货币资金」有 40 个明细,选中项却是空白时看着像个空框)。
|
||
function optionsHtml(values, current, placeholder) {
|
||
var list = values.slice();
|
||
if (current && list.indexOf(current) < 0) { list.push(current); }
|
||
var out = list.indexOf('') < 0
|
||
? '<option value="">' + esc(placeholder == null ? '' : placeholder) + '</option>'
|
||
: '';
|
||
return out + list.map(function (v) {
|
||
return '<option value="' + esc(v) + '"' + (v === current ? ' selected' : '') + '>' + esc(v) + '</option>';
|
||
}).join('');
|
||
}
|
||
|
||
function acctEditor(acctField, subField) {
|
||
var acctValue = draft[acctField] || '';
|
||
var subValue = draft[subField] || '';
|
||
var level1 = (COA && COA.level1) ? COA.level1.map(function (a) { return a.name; }) : [];
|
||
var subList = [];
|
||
if (COA && COA.level1 && draft[acctField]) {
|
||
var hit = COA.level1.filter(function (a) { return a.name === draft[acctField]; })[0];
|
||
if (hit && COA.children && COA.children[hit.code]) {
|
||
subList = COA.children[hit.code].map(function (a) { return a.name; });
|
||
}
|
||
}
|
||
var acctHint = COA ? '' : '<div style="font-size:0.625rem;color:#b0a99c;margin-top:0.15rem;">科目清单加载中…</div>';
|
||
var subPlaceholder = subList.length ? '(不选二级,直接挂一级)' : '(该一级科目下没有明细)';
|
||
return '<select data-f="' + acctField + '">' + optionsHtml(level1, acctValue, '(选一级科目)') + '</select>'
|
||
+ acctHint
|
||
+ '<select data-f="' + subField + '" style="margin-top:0.25rem;">' + optionsHtml(subList, subValue, subPlaceholder) + '</select>';
|
||
}
|
||
|
||
function render() {
|
||
var list = visibleRows();
|
||
var html = '';
|
||
list.forEach(function (r, i) { html += rowHtml(r, i); });
|
||
body.innerHTML = html;
|
||
if (editingId === 'new' && draft) {
|
||
body.insertAdjacentHTML('afterbegin', newRowHtml());
|
||
}
|
||
empty.hidden = list.length > 0;
|
||
countEl.textContent = (editingId === 'new')
|
||
? '正在新增一条,填完点「保存」'
|
||
: ('显示 ' + list.length + ' / ' + ROWS.length + ' 条');
|
||
paintSummary();
|
||
}
|
||
|
||
function paintSummary() {
|
||
document.getElementById('amTotal').textContent = ROWS.length;
|
||
var out = 0, inn = 0, platforms = {};
|
||
ROWS.forEach(function (r) {
|
||
if (r.basis === '支出基准') { out++; }
|
||
if (r.basis === '收入基准') { inn++; }
|
||
if (r.platform) { platforms[r.platform] = true; }
|
||
});
|
||
document.getElementById('amOut').textContent = out;
|
||
document.getElementById('amIn').textContent = inn;
|
||
document.getElementById('amPlatformCount').textContent = Object.keys(platforms).length;
|
||
document.getElementById('amErrRows').textContent = SUMMARY.error_rows || 0;
|
||
document.getElementById('amWarnRows').textContent = SUMMARY.warning_rows || 0;
|
||
document.getElementById('amTipCount').textContent = SUMMARY.tip_count || 0;
|
||
}
|
||
|
||
/* ---------- 与服务端交互 ---------- */
|
||
function api(method, path, body) {
|
||
var headers = { 'X-CSRF-TOKEN': CSRF, 'Accept': 'application/json', 'X-Requested-With': 'XMLHttpRequest' };
|
||
if (body && !(body instanceof FormData)) { headers['Content-Type'] = 'application/json'; }
|
||
return fetch(ENDPOINT + path, {
|
||
method: method,
|
||
headers: headers,
|
||
credentials: 'same-origin',
|
||
body: body ? (body instanceof FormData ? body : JSON.stringify(body)) : undefined
|
||
}).then(function (res) {
|
||
return res.json().catch(function () { return {}; }).then(function (data) {
|
||
if (!res.ok) {
|
||
var msg = data.message || '';
|
||
if (data.errors) {
|
||
msg = Object.keys(data.errors).map(function (k) { return data.errors[k][0]; }).join(' ');
|
||
}
|
||
// 导入接口有 1 次/分钟的限流,撞上了要说清楚,别报「HTTP 429」让人猜
|
||
if (res.status === 429) { msg = '导入操作太频繁了(平台限制每分钟 1 次),请等 1 分钟再试。'; }
|
||
if (res.status === 419) { msg = '登录状态已过期,请刷新页面重新登录。'; }
|
||
if (res.status === 403) { msg = msg || '当前账号没有维护这张表的权限。'; }
|
||
throw new Error(msg || ('请求失败(HTTP ' + res.status + ')'));
|
||
}
|
||
return data;
|
||
});
|
||
});
|
||
}
|
||
|
||
function applyState(data) {
|
||
if (data.rules) {
|
||
ROWS = data.rules;
|
||
editingId = null;
|
||
draft = null;
|
||
openId = null;
|
||
}
|
||
if (data.options) { OPTIONS = data.options; rebuildFilterOptions(); }
|
||
if (data.summary) { SUMMARY = data.summary; }
|
||
rebuildSeq();
|
||
render();
|
||
}
|
||
|
||
function rebuildFilterOptions() {
|
||
msPlatform.refresh(OPTIONS.platforms || []);
|
||
msCategory.refresh(categoryOptions());
|
||
msBasis.refresh(['支出基准', '收入基准']);
|
||
rebuildMemoOptions();
|
||
}
|
||
|
||
// 大类筛选只列「表里真的用到」的大类:清空后剩下 0 条的(例如原来的售后费用/平台佣金/平台服务费)
|
||
// 就不再占位;留空的不显示。已选中的值保留,否则选了以后没法取消。
|
||
// 编辑行里的下拉仍然用完整清单 OPTIONS.categories,想改用别的大类随时能选。
|
||
function categoryOptions() {
|
||
var seen = {};
|
||
var used = [];
|
||
ROWS.forEach(function (r) {
|
||
var v = String(r.category || '').trim();
|
||
if (v && !seen[v]) { seen[v] = true; used.push(v); }
|
||
});
|
||
var order = OPTIONS.categories || [];
|
||
used.sort(function (a, b) {
|
||
var ia = order.indexOf(a);
|
||
var ib = order.indexOf(b);
|
||
if (ia < 0) { ia = order.length; }
|
||
if (ib < 0) { ib = order.length; }
|
||
if (ia !== ib) { return ia - ib; }
|
||
return a < b ? -1 : (a > b ? 1 : 0);
|
||
});
|
||
msCategory.values().forEach(function (v) { if (used.indexOf(v) < 0) { used.push(v); } });
|
||
return used;
|
||
}
|
||
|
||
// 财务备注的候选:按当前选中的平台收窄,列表里再按关键字过滤
|
||
function memoCandidates() {
|
||
var platforms = msPlatform.values();
|
||
var seen = {};
|
||
ROWS.forEach(function (r) {
|
||
if (platforms.length && platforms.indexOf(r.platform) < 0) { return; }
|
||
var memo = String(r.memo || '');
|
||
if (!memo) { return; }
|
||
seen[memo] = true;
|
||
});
|
||
return Object.keys(seen).sort();
|
||
}
|
||
|
||
// 候选列表在数据变化(导入/新增/删除)后刷新
|
||
function rebuildMemoOptions() { msMemo.refresh(); }
|
||
|
||
function loadCoa() {
|
||
if (COA) { return Promise.resolve(COA); }
|
||
return api('GET', '/accounts').then(function (data) {
|
||
COA = data;
|
||
return COA;
|
||
});
|
||
}
|
||
|
||
/* ---------- 行操作 ---------- */
|
||
body.addEventListener('click', function (ev) {
|
||
var btn = ev.target.closest('button[data-act]');
|
||
var tr = ev.target.closest('tr.am-item');
|
||
if (!tr) { return; }
|
||
var id = Number(tr.getAttribute('data-id'));
|
||
|
||
if (btn) {
|
||
ev.stopPropagation();
|
||
var act = btn.getAttribute('data-act');
|
||
var isNew = tr.getAttribute('data-id') === 'new';
|
||
if (act === 'edit') { startEdit(id); }
|
||
else if (act === 'cancel') { editingId = null; draft = null; render(); }
|
||
else if (act === 'save') { if (isNew) { createRow(); } else { saveEdit(id); } }
|
||
else if (act === 'delete') { removeRow(id); }
|
||
return;
|
||
}
|
||
|
||
if (editingId === id) { return; }
|
||
|
||
// 点行 → 展开 / 收起分录与校验
|
||
var r = ROWS.filter(function (x) { return x.id === id; })[0];
|
||
if (!r) { return; }
|
||
var isOpen = (openId === id);
|
||
collapseDetail();
|
||
if (isOpen) { return; }
|
||
openId = id;
|
||
tr.classList.add('is-open');
|
||
tr.insertAdjacentHTML('afterend', detailHtml(r));
|
||
});
|
||
|
||
body.addEventListener('change', function (ev) {
|
||
var el = ev.target;
|
||
var f = el.getAttribute && el.getAttribute('data-f');
|
||
if (!f || !draft) { return; }
|
||
draft[f] = el.value;
|
||
|
||
if (f === 'dAcct' || f === 'cAcct') {
|
||
// 换了一级科目就清掉不属于新科目的二级,避免留下「5601 / 5602.45」这种组合
|
||
draft[(f === 'dAcct') ? 'dSub' : 'cSub'] = '';
|
||
}
|
||
|
||
if (f === 'category') {
|
||
// 大类变了,方向基准跟着走,避免两者打架
|
||
draft.basis = CATEGORY_BASIS[draft.category] || draft.basis;
|
||
}
|
||
|
||
render();
|
||
});
|
||
|
||
body.addEventListener('input', function (ev) {
|
||
var el = ev.target;
|
||
var f = el.getAttribute && el.getAttribute('data-f');
|
||
if (!f || !draft) { return; }
|
||
draft[f] = el.value;
|
||
});
|
||
|
||
function collapseDetail() {
|
||
Array.prototype.forEach.call(body.querySelectorAll('tr.am-item.is-open'), function (el) {
|
||
el.classList.remove('is-open');
|
||
if (el.nextElementSibling && el.nextElementSibling.classList.contains('am-detail')) {
|
||
el.nextElementSibling.remove();
|
||
}
|
||
});
|
||
openId = null;
|
||
}
|
||
|
||
function collectDraft() {
|
||
var values = {};
|
||
['platform', 'item', 'memo', 'goods', 'invoice', 'category', 'feeMemo', 'dAcct', 'dSub', 'cAcct', 'cSub', 'basis'].forEach(function (f) {
|
||
values[f] = draft[f] == null ? '' : draft[f];
|
||
});
|
||
return values;
|
||
}
|
||
|
||
function startEdit(id) {
|
||
loadCoa().then(function () {
|
||
var r = ROWS.filter(function (x) { return x.id === id; })[0];
|
||
if (!r) { return; }
|
||
draft = {
|
||
platform: r.platform, item: r.item, memo: r.memo, goods: r.goods, invoice: r.invoice,
|
||
category: r.category, feeMemo: r.feeMemo, dAcct: r.dAcct, dSub: r.dSub, cAcct: r.cAcct, cSub: r.cSub, basis: r.basis
|
||
};
|
||
editingId = id;
|
||
collapseDetail();
|
||
render();
|
||
}).catch(function (e) { toast('科目清单加载失败:' + e.message, 'err'); });
|
||
}
|
||
|
||
function saveEdit(id) {
|
||
api('PATCH', '/rules/' + id, { values: collectDraft() }).then(function (data) {
|
||
applyState(data);
|
||
toast(data.message || '已保存。');
|
||
}).catch(function (e) { toast(e.message, 'err'); });
|
||
}
|
||
|
||
function removeRow(id) {
|
||
var r = ROWS.filter(function (x) { return x.id === id; })[0];
|
||
if (!r) { return; }
|
||
if (!window.confirm('确定删除「' + r.platform + ' · ' + r.item + '」这条规则?\n\n删掉以后,这个账单项目就匹配不到科目了。')) { return; }
|
||
api('DELETE', '/rules/' + id).then(function (data) {
|
||
applyState(data);
|
||
toast(data.message || '已删除。');
|
||
}).catch(function (e) { toast(e.message, 'err'); });
|
||
}
|
||
|
||
/* ---------- 新增 ---------- */
|
||
document.getElementById('amAdd').addEventListener('click', function () {
|
||
if (editingId) { toast('请先保存或取消正在编辑的那一行。', 'err'); return; }
|
||
loadCoa().then(function () {
|
||
draft = {
|
||
platform: '', item: '', memo: '', goods: '', invoice: '-', category: '费用',
|
||
feeMemo: '', dAcct: '', dSub: '', cAcct: '', cSub: '', basis: '支出基准'
|
||
};
|
||
editingId = 'new';
|
||
collapseDetail();
|
||
render();
|
||
var focus = body.querySelector('tr[data-id="new"] input[data-f="platform"]');
|
||
if (focus) { focus.focus(); }
|
||
}).catch(function (e) { toast('科目清单加载失败:' + e.message, 'err'); });
|
||
});
|
||
|
||
function newRowHtml() {
|
||
return '<tr class="am-item is-editing" data-id="new">'
|
||
+ '<td>新</td>'
|
||
+ '<td class="am-edit-cell"><input data-f="platform" value="' + esc(draft.platform) + '" placeholder="平台"></td>'
|
||
+ '<td class="am-edit-cell"><input data-f="item" value="' + esc(draft.item) + '" placeholder="账单项目上的名称"></td>'
|
||
+ '<td class="am-edit-cell"><input data-f="memo" value="' + esc(draft.memo) + '" placeholder="不限就填 -"></td>'
|
||
+ '<td class="am-edit-cell"><input data-f="goods" value="' + esc(draft.goods) + '" placeholder="不限就填 -"></td>'
|
||
+ '<td class="am-edit-cell"><select data-f="invoice">' + optionsHtml(OPTIONS.invoices || [], draft.invoice) + '</select></td>'
|
||
+ '<td class="am-edit-cell"><select data-f="category">' + optionsHtml(OPTIONS.categories || [], draft.category) + '</select></td>'
|
||
+ '<td class="am-edit-cell"><input data-f="feeMemo" value="' + esc(draft.feeMemo) + '" placeholder="费用备注"></td>'
|
||
+ '<td class="am-edit-cell">' + acctEditor('dAcct', 'dSub') + '</td>'
|
||
+ '<td class="am-edit-cell">' + acctEditor('cAcct', 'cSub') + '</td>'
|
||
+ '<td class="am-edit-cell"><select data-f="basis">' + optionsHtml(OPTIONS.basis || [], draft.basis) + '</select></td>'
|
||
+ '<td>—</td>'
|
||
+ '<td><div class="am-row-actions">'
|
||
+ '<button type="button" class="am-btn-mini am-btn-mini--primary" data-act="save">保存</button>'
|
||
+ '<button type="button" class="am-btn-mini" data-act="cancel">取消</button>'
|
||
+ '</div></td></tr>';
|
||
}
|
||
|
||
function createRow() {
|
||
var values = collectDraft();
|
||
if (!values.platform || !values.item) {
|
||
toast('平台和账单项目不能为空。', 'err');
|
||
return;
|
||
}
|
||
api('POST', '/rules', { values: values }).then(function (data) {
|
||
applyState(data);
|
||
toast(data.message || '已新增。');
|
||
}).catch(function (e) { toast(e.message, 'err'); });
|
||
}
|
||
|
||
/* ---------- 导入 ---------- */
|
||
var fileInput = document.getElementById('amFile');
|
||
document.getElementById('amImportBtn').addEventListener('click', function () { fileInput.click(); });
|
||
|
||
fileInput.addEventListener('change', function () {
|
||
// 关键:File 对象必须在**同步**阶段就抓住。
|
||
// 若在 fetch 回来之后才读 fileInput.files[0],一旦 input.value 被清空
|
||
// (或 change 被触发第二次)就会拿到 undefined,抛 "reading 'name'"。
|
||
var file = fileInput.files && fileInput.files[0];
|
||
if (!file) { return; }
|
||
fileInput.value = ''; // 立刻清空:再选同一个文件也能触发 change
|
||
var form = new FormData();
|
||
form.append('import', file);
|
||
openModal('正在识别文件…', '<p>请稍候。</p>', []);
|
||
api('POST', '/preview', form).then(function (data) {
|
||
showImportPreview(data, file);
|
||
}).catch(function (e) {
|
||
closeModal();
|
||
toast('导入失败:' + e.message, 'err');
|
||
});
|
||
});
|
||
|
||
function showImportPreview(data, file) {
|
||
var rows = (data.sample || []).map(function (r) {
|
||
return '<tr><td>' + esc(r.platform) + '</td><td>' + esc(r.item) + '</td><td>' + esc(r.memo) + '</td>'
|
||
+ '<td>' + esc(r.goods) + '</td>'
|
||
+ '<td>' + esc(r.category) + '</td><td>' + esc(r.feeMemo) + '</td><td>' + acct(r.dAcct, r.dSub) + '</td><td>' + acct(r.cAcct, r.cSub) + '</td>'
|
||
+ '<td>' + esc(r.basis) + '</td></tr>';
|
||
}).join('');
|
||
var dupList = (data.duplicate_keys || []).length
|
||
? '<div style="margin-top:0.5rem;color:#8a5d10;">重复的键(平台 | 项目 | 业务备注 | 商品名称)前几个:<br>' + (data.duplicate_keys || []).map(esc).join('<br>') + '</div>'
|
||
: '';
|
||
var html = '<div>文件:<b>' + esc(file.name) + '</b>(工作表 ' + esc(data.sheet) + ')</div>'
|
||
+ '<div class="am-kv">'
|
||
+ '<div class="am-kv__k">识别到</div><div><b>' + data.total + '</b> 行</div>'
|
||
+ '<div class="am-kv__k">可新增</div><div><b style="color:#3b6d11;">' + data.can_create + '</b> 条</div>'
|
||
+ '<div class="am-kv__k">与现有重复</div><div><b style="color:#8a5d10;">' + data.can_overwrite + '</b> 条</div>'
|
||
+ '</div>'
|
||
+ '<label class="am-check"><input type="checkbox" id="amOverwrite"> 重复的那些用文件里的内容覆盖现有规则(不勾选则跳过)</label>'
|
||
+ dupList
|
||
+ '<div style="margin-top:0.75rem;">前 ' + (data.sample || []).length + ' 行预览:</div>'
|
||
+ '<table class="am-preview-table"><thead><tr><th>平台</th><th>账单项目</th><th>财务备注</th><th>商品名称</th><th>财务类型</th><th>费用备注</th><th>借方</th><th>贷方</th><th>基准</th></tr></thead><tbody>' + rows + '</tbody></table>';
|
||
|
||
var foot = '<button type="button" class="am-btn" id="amImportCancel">取消</button>'
|
||
+ '<button type="button" class="am-btn am-btn--primary" id="amImportConfirm">确认导入</button>';
|
||
openModal('导入预览', html, foot);
|
||
|
||
document.getElementById('amImportCancel').addEventListener('click', closeModal);
|
||
document.getElementById('amImportConfirm').addEventListener('click', function () {
|
||
var overwrite = document.getElementById('amOverwrite').checked;
|
||
var form = new FormData();
|
||
form.append('import', file);
|
||
if (overwrite) { form.append('overwrite', '1'); }
|
||
var btn = document.getElementById('amImportConfirm');
|
||
btn.disabled = true;
|
||
btn.textContent = '导入中…';
|
||
api('POST', '/import', form).then(function (res) {
|
||
closeModal();
|
||
applyState(res);
|
||
toast(res.message || '导入完成。');
|
||
}).catch(function (e) {
|
||
btn.disabled = false;
|
||
btn.textContent = '确认导入';
|
||
toast('导入失败:' + e.message, 'err');
|
||
});
|
||
});
|
||
}
|
||
|
||
/* ---------- 弹窗 ---------- */
|
||
var modal = document.getElementById('amModal');
|
||
function openModal(title, bodyHtml, footButtons) {
|
||
document.getElementById('amModalTitle').textContent = title;
|
||
document.getElementById('amModalBody').innerHTML = bodyHtml;
|
||
// footButtons 既可传数组也可传 HTML 字符串,两种都认
|
||
document.getElementById('amModalFoot').innerHTML = Array.isArray(footButtons)
|
||
? footButtons.join('')
|
||
: String(footButtons || '');
|
||
modal.hidden = false;
|
||
}
|
||
function closeModal() {
|
||
modal.hidden = true;
|
||
var box = modal.querySelector('.am-modal__box');
|
||
if (box) { box.classList.remove('am-modal__box--wide'); }
|
||
}
|
||
document.getElementById('amModalClose').addEventListener('click', closeModal);
|
||
modal.addEventListener('click', function (ev) { if (ev.target === modal) { closeModal(); } });
|
||
|
||
/* ---------- 导出 / 模板 ---------- */
|
||
document.getElementById('amExportBtn').addEventListener('click', function () { window.location.href = ENDPOINT + '/export'; });
|
||
document.getElementById('amTemplateBtn').addEventListener('click', function () { window.location.href = ENDPOINT + '/template'; });
|
||
|
||
/* ---------- 操作记录(变更内容与操作人由服务端算好,前端只渲染) ---------- */
|
||
var HIST = { events: [] };
|
||
|
||
function histChangeHtml(event) {
|
||
var parts = Array.isArray(event.changes) && event.changes.length ? event.changes : ['—'];
|
||
if (parts.length === 1) { return '<div>' + esc(parts[0]) + '</div>'; }
|
||
return '<ul class="am-hist-changes"><li>' + parts.map(esc).join('</li><li>') + '</li></ul>';
|
||
}
|
||
function histRow(event) {
|
||
return '<tr>'
|
||
+ '<td class="am-hist-at">' + esc(event.at || '') + '</td>'
|
||
+ '<td><span class="am-hist-tag am-hist-tag--' + esc(event.action || '') + '">'
|
||
+ esc(event.action_label || event.action || '') + '</span></td>'
|
||
+ '<td>' + esc(event.platform || '—')
|
||
+ '<div class="am-hist-meta">' + esc(event.item || '')
|
||
+ (event.memo ? '(备注 ' + esc(event.memo) + ')' : '') + '</div></td>'
|
||
+ '<td>' + histChangeHtml(event) + '</td>'
|
||
+ '<td>' + esc(event.actor || '') + '</td>'
|
||
+ '<td>' + histRestoreCell(event) + '</td>'
|
||
+ '</tr>';
|
||
}
|
||
// 只有「删除」和「修改」能恢复:删除 → 建回这一行,修改 → 还原成改前内容
|
||
function histRestoreCell(event) {
|
||
if (event.action !== 'delete' && event.action !== 'update') {
|
||
return '<span class="am-hist-meta">—</span>';
|
||
}
|
||
return '<button type="button" class="am-btn-mini am-btn-mini--primary" data-hist-restore="1"'
|
||
+ ' data-index="' + esc(String(event.hist_index)) + '"'
|
||
+ ' data-at="' + esc(event.at || '') + '"'
|
||
+ ' data-action="' + esc(event.action) + '"'
|
||
+ ' data-rule-id="' + esc(String(event.id || '')) + '">恢复</button>';
|
||
}
|
||
function histRestore(btn) {
|
||
var index = Number(btn.getAttribute('data-index'));
|
||
var event = HIST.events[index] || {};
|
||
var what = event.action === 'delete'
|
||
? '恢复被删除的「' + (event.platform || '') + ' · ' + (event.item || '') + '」?'
|
||
: '把「' + (event.platform || '') + ' · ' + (event.item || '') + '」还原成这次修改前的内容?\n\n'
|
||
+ (Array.isArray(event.changes) ? event.changes.join('\n') : '');
|
||
|
||
if (!window.confirm(what)) { return; }
|
||
|
||
btn.disabled = true;
|
||
api('POST', '/history/restore', {
|
||
index: index,
|
||
at: event.at || '',
|
||
action: event.action || '',
|
||
id: Number(event.id || 0)
|
||
}).then(function (data) {
|
||
applyState(data);
|
||
toast(data.message || '已恢复。');
|
||
return histLoad();
|
||
}).catch(function (e) {
|
||
btn.disabled = false;
|
||
toast(e.message, 'err');
|
||
});
|
||
}
|
||
function histFilters() {
|
||
var actionEl = document.getElementById('amHistAction');
|
||
var kwEl = document.getElementById('amHistKeyword');
|
||
return {
|
||
action: actionEl ? actionEl.value : '',
|
||
keyword: kwEl ? kwEl.value.trim() : ''
|
||
};
|
||
}
|
||
function histFiltered() {
|
||
var f = histFilters();
|
||
var needle = f.keyword.toLowerCase();
|
||
return HIST.events.filter(function (event) {
|
||
if (f.action && String(event.action || '') !== f.action) { return false; }
|
||
if (!needle) { return true; }
|
||
var hay = [event.platform, event.item, event.memo, event.actor,
|
||
(Array.isArray(event.changes) ? event.changes.join(' ') : '')].join(' ').toLowerCase();
|
||
return hay.indexOf(needle) >= 0;
|
||
});
|
||
}
|
||
function histRenderTable() {
|
||
var tbody = document.getElementById('amHistBody');
|
||
if (!tbody) { return; }
|
||
var list = histFiltered();
|
||
tbody.innerHTML = list.length
|
||
? list.map(histRow).join('')
|
||
: '<tr><td colspan="6" class="am-hist-meta" style="padding:1rem;">没有符合条件的记录。</td></tr>';
|
||
var meta = document.getElementById('amHistMeta');
|
||
if (meta) {
|
||
meta.textContent = '显示 ' + list.length + ' / ' + HIST.events.length + ' 条'
|
||
+ (HIST.events.length >= 500 ? '(只保留最近 500 条)' : '');
|
||
}
|
||
}
|
||
function histExport() {
|
||
var f = histFilters();
|
||
window.location.href = ENDPOINT + '/history/export?action=' + encodeURIComponent(f.action)
|
||
+ '&keyword=' + encodeURIComponent(f.keyword);
|
||
}
|
||
function histBodyHtml() {
|
||
return ''
|
||
+ '<div class="am-hist-tools">'
|
||
+ '<select id="amHistAction"><option value="">全部操作</option>'
|
||
+ '<option value="create">新增</option><option value="update">修改</option>'
|
||
+ '<option value="delete">删除</option><option value="import">导入</option>'
|
||
+ '<option value="reset">重置</option></select>'
|
||
+ '<input type="search" id="amHistKeyword" placeholder="搜平台 / 账单项目 / 操作人">'
|
||
+ '<button type="button" class="am-btn" id="amHistExport">导出记录 CSV</button>'
|
||
+ '<span class="am-hist-meta" id="amHistMeta"></span>'
|
||
+ '<span class="am-hist-meta">时间为北京时间</span>'
|
||
+ '</div>'
|
||
+ '<div class="am-hist-scroll"><table class="am-hist-table">'
|
||
+ '<thead><tr><th style="width:9.5rem;">时间</th><th style="width:4.5rem;">操作</th>'
|
||
+ '<th style="width:13rem;">平台 / 账单项目</th><th>变更内容</th>'
|
||
+ '<th style="width:7.5rem;">操作人</th><th style="width:5.5rem;">操作</th></tr></thead>'
|
||
+ '<tbody id="amHistBody"></tbody></table></div>';
|
||
}
|
||
function histLoad() {
|
||
return fetch(ENDPOINT + '/history', { headers: { 'Accept': 'application/json', 'X-Requested-With': 'XMLHttpRequest' } })
|
||
.then(function (res) {
|
||
return res.json().catch(function () { return {}; }).then(function (data) {
|
||
if (!res.ok) { throw new Error(data.message || ('读取失败(HTTP ' + res.status + ')')); }
|
||
return data;
|
||
});
|
||
})
|
||
.then(function (data) {
|
||
// 记下位置:恢复时要带着它回服务端核对,避免记录变了恢复错行
|
||
HIST.events = (Array.isArray(data.events) ? data.events : []).map(function (event, i) {
|
||
event.hist_index = i;
|
||
return event;
|
||
});
|
||
histRenderTable();
|
||
})
|
||
.catch(function (err) {
|
||
var tbody = document.getElementById('amHistBody');
|
||
if (tbody) {
|
||
tbody.innerHTML = '<tr><td colspan="6" class="am-hist-meta" style="padding:1rem;color:#a32d2d;">'
|
||
+ '操作记录读取失败:' + esc(err && err.message ? err.message : err) + '</td></tr>';
|
||
}
|
||
});
|
||
}
|
||
|
||
function openHistory() {
|
||
var box = modal.querySelector('.am-modal__box');
|
||
if (box) { box.classList.add('am-modal__box--wide'); }
|
||
openModal('科目映射表 · 操作记录', histBodyHtml(), '');
|
||
document.getElementById('amHistAction').addEventListener('change', histRenderTable);
|
||
document.getElementById('amHistKeyword').addEventListener('input', histRenderTable);
|
||
document.getElementById('amHistExport').addEventListener('click', histExport);
|
||
document.getElementById('amHistBody').addEventListener('click', function (ev) {
|
||
var btn = ev.target.closest('[data-hist-restore]');
|
||
if (btn) { histRestore(btn); }
|
||
});
|
||
histLoad();
|
||
}
|
||
document.getElementById('amHistoryBtn').addEventListener('click', openHistory);
|
||
|
||
/* ---------- 筛选 ---------- */
|
||
kw.addEventListener('input', render);
|
||
fOnly.addEventListener('change', render);
|
||
document.getElementById('amResetFilter').addEventListener('click', function () {
|
||
kw.value = ''; fOnly.value = '';
|
||
msMemo.clear();
|
||
msPlatform.clear();
|
||
msCategory.clear();
|
||
msBasis.clear();
|
||
rebuildMemoOptions();
|
||
render();
|
||
});
|
||
|
||
// 有编辑中的行时,离开前提醒一下
|
||
window.addEventListener('beforeunload', function (ev) {
|
||
if (draft) { ev.preventDefault(); ev.returnValue = ''; }
|
||
});
|
||
|
||
rebuildSeq();
|
||
render();
|
||
// 平台 / 大类 的候选过去由 Blade 直接渲染,现在改成多选组件后统一由这里灌一次
|
||
rebuildFilterOptions();
|
||
|
||
// 后台预取账套科目清单(160KB 左右)。不做这一步的话,第一次点「编辑」要等它下载完
|
||
// 才有反应,用户会以为按钮坏了。
|
||
loadCoa().catch(function () { /* 失败也没关系,点编辑时会再试并给出提示 */ });
|
||
})();
|
||
</script>
|