order_full.html 18 KB
<!doctype html>
<html lang="zh-CN"><head><meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1">
<title>库存补货单</title>
<style>
:root{--red:#c8102e;--line:#e6e8eb;--muted:#8a8f98;--ok:#18a058;--warn:#c27c0e;--err:#d03050;
  --bg-red:#fdecef;--bg-org:#fff6e5;--bg-grey:#f5f6f8}
*{box-sizing:border-box}
body{margin:0;font-family:-apple-system,"PingFang SC","Microsoft YaHei",sans-serif;background:#f4f5f7;color:#1f2329;font-size:13px}
body.edit{padding-bottom:72px}
a{color:#1677ff;text-decoration:none}
.hd{background:var(--red);color:#fff;padding:12px 14px;display:flex;align-items:flex-start;gap:10px;flex-wrap:wrap}
.hd b{font-size:16px}.hd .m{opacity:.85;font-size:12px;margin-top:3px}.hd .sp{flex:1}
.hd a{color:#fff;border:1px solid #ffffff99;border-radius:14px;padding:3px 10px;font-size:12px;white-space:nowrap}
.mock{display:inline-block;background:#ffd666;color:#7a4b00;border-radius:8px;padding:0 6px;font-size:11px;margin-left:6px}
.card{background:#fff;margin:8px;border-radius:8px;padding:10px 12px}
.st{font-size:14px;font-weight:600}.sum{font-size:13px;color:#333;margin-top:4px}
.ok{color:var(--ok)}.warn{color:var(--warn)}.err{color:var(--err)}.muted{color:var(--muted)}
.alerts{display:flex;flex-wrap:wrap;gap:6px;margin:8px}
.chip{border-radius:14px;padding:4px 10px;font-size:12px;cursor:pointer;border:1px solid transparent}
.chip.red{background:var(--bg-red);color:var(--err);border-color:#f5c2cc}.chip.org{background:var(--bg-org);color:var(--warn);border-color:#f6dca8}
.chip.okc{background:#e8f7ee;color:var(--ok)}
.dirty{background:var(--bg-org);color:var(--warn);font-size:12px;padding:6px 12px;margin:0 8px;border-radius:6px;display:none}
.tw{margin:8px;background:#fff;border-radius:8px;overflow:auto;max-height:calc(100vh - 150px);-webkit-overflow-scrolling:touch}
table{border-collapse:separate;border-spacing:0;width:max-content;min-width:100%}
th,td{padding:7px 8px;border-bottom:1px solid var(--line);text-align:right;white-space:nowrap;background:inherit}
th{position:sticky;top:0;z-index:2;background:#fafbfc;font-weight:600;color:#555;font-size:12px}
th.l,td.l{text-align:left}
.c0{position:sticky;left:0;z-index:1;background:#fff;text-align:left;min-width:110px;max-width:160px;white-space:normal;box-shadow:1px 0 0 var(--line)}
th.c0{z-index:3;background:#fafbfc}
.nm{font-weight:600;display:block}.pn{color:var(--muted);font-size:11px}
tr.r-red td,tr.r-red td.c0{background:var(--bg-red)}tr.r-org td,tr.r-org td.c0{background:var(--bg-org)}
tr.r-grey td,tr.r-grey td.c0{background:var(--bg-grey);color:#999}
tr.flash td{animation:fl 1.6s}@keyframes fl{0%{background:#ffe58f}100%{}}
.y{color:var(--ok);font-weight:600}.n{color:var(--err);font-weight:600}
.gap{color:var(--err);font-weight:600}
.tip{text-align:left;white-space:normal;min-width:180px;max-width:280px;font-size:12px;line-height:1.5}
.qty{display:inline-flex;align-items:center}
.qty button{width:30px;height:30px;font-size:18px;border:1px solid #d0d3d9;background:#f7f8fa;color:#333;padding:0;line-height:1}
.qty button:first-child{border-radius:6px 0 0 6px}.qty button:last-child{border-radius:0 6px 6px 0}
.qty input{width:50px;height:30px;border:1px solid #d0d3d9;border-left:0;border-right:0;text-align:center;font-size:15px;border-radius:0;-moz-appearance:textfield}
.qty input::-webkit-outer-spin-button,.qty input::-webkit-inner-spin-button{-webkit-appearance:none;margin:0}
.bq{font-size:14px;font-weight:600}
.bar{position:fixed;left:0;right:0;bottom:0;background:#fff;box-shadow:0 -2px 8px #0000001a;padding:8px 10px;display:none;align-items:center;gap:8px;z-index:9}
.bar .t{flex:1;font-size:13px}.bar button{padding:10px 16px;font-size:15px;border-radius:8px;border:0}
.b1{background:var(--red);color:#fff}.b2{background:#fff;color:var(--red);border:1px solid var(--red)!important}
.po table{width:100%}.po td,.po th{text-align:left}
img{max-width:100%;border:1px solid #eee;border-radius:6px;margin-top:8px}
details summary{cursor:pointer;font-weight:600;font-size:13px}.ev{font-size:12px;color:#666;line-height:1.7;margin-top:6px}
.legend{font-size:11px;color:var(--muted);margin:0 10px 6px}
.legend i{display:inline-block;width:10px;height:10px;border-radius:2px;vertical-align:-1px;margin:0 3px 0 8px}
</style></head>
<body>
<div class="hd"><div><b id="doc">库存补货单</b><span id="mk"></span><div class="m" id="meta">加载中…</div></div>
  <span class="sp"></span><a href="/m/expected?o={{ token }}" target="_blank">修改期望值</a></div>
<div class="card"><div class="st" id="status"></div><div class="sum" id="sum"></div></div>
<div class="alerts" id="alerts"></div>
<div class="legend"><i style="background:var(--bg-red)"></i>订完仍低于期望 / 缺货 / 查询失败<i style="background:var(--bg-org)"></i>关注仓不够或按当日上限<i style="background:var(--bg-grey)"></i>库存充足 仓库 ✔/✘ = 能否单独满足本次订购量</div>
<div class="dirty" id="dirty">数量已修改,确认无误后点下方「确认订购」</div>
<div class="tw" id="tw"><table id="tbl"></table></div>
<div class="card po" id="po" style="display:none"></div>
<div class="card"><details><summary>处理记录</summary><div class="ev" id="ev"></div></details></div>
<div class="bar" id="bar"><div class="t" id="bartext"></div><button class="b2" onclick="cancelToday()">今天不订</button><button class="b1" id="okbtn" onclick="confirmOrder()">确认订购</button></div>
<script>
var TOKEN = "{{ token }}", STATE = null, editing = false, QTY = {}, LINES = {}, WH = [], CAN = false;
var STAGE = {scanning: "正在查询 DMS 库存…", awaiting_confirm: "请核对清单,可修改数量后点「确认订购」", creating: "已确认,正在 DMS 创建采购单(待提交)…",
  screenshotting: "采购单已创建,正在截取 DMS 页面…", done: "已完成", cancelled: "今天不订(已取消)", error: "出错,流程已停止", superseded: "已作废(有更新的查询)"};
function esc(s){return s===null||s===undefined?"":String(s).replace(/[&<>"]/g,function(c){return {"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;"}[c];});}
function g(n){return Math.round((n||0)*100)/100;}
function idOf(pn){return pn.replace(/[^A-Za-z0-9_-]/g,"_");}
function whName(w){return (w.replace("GTMC","")||w) + "仓";}
function req(method, url, body, cb){var x=new XMLHttpRequest();x.open(method,url);x.setRequestHeader("Content-Type","application/json");
  x.onload=function(){var j=null;try{j=JSON.parse(x.responseText);}catch(e){} cb(x.status, j);};x.onerror=function(){cb(0,null);};x.send(body?JSON.stringify(body):null);}

/* 按当前数量重新算一行的提示(数量改了,订后是否仍不足、仓库能否满足都会变) */
function flags(l){
  var q = (l.pn in QTY) ? QTY[l.pn] : (l.orderable ? l.final_qty : 0);
  var have = (l.stock||0) + (l.in_transit||0), need = q || l.suggested || 0, tot = 0, ok = {};
  for (var w in (l.wh||{})) { var v = l.wh[w]||0; tot += v; ok[w] = need > 0 ? v >= need : v > 0; }
  var err = l.tag === "error";                       /* 查询失败:库存未知,不判断是否低于期望 */
  var below = !err && have < l.expected, after = below && have + q < l.expected;
  var whShort = need > 0 && tot > 0 && tot < need && l.tag !== "oos";
  var capped = l.cap !== null && l.cap !== undefined && l.suggested > l.cap && q >= l.cap;
  var cls = (after || l.tag === "oos" || l.tag === "error") ? "r-red" : ((whShort || capped || l.tag === "capped") ? "r-org" : ((l.suggested <= 0 && !below) ? "r-grey" : ""));
  var tips = [];
  if (err) tips.push('<span class=err>查询失败:' + esc(String(l.error || "").replace(/^\w+Error:\s*/, "")) + '</span>');
  else if (l.tag === "oos") tips.push('<span class=err>关注仓都没货,厂家缺货不订</span>');
  if (after) tips.push('<span class=err>订完仍低于期望,差 ' + g(l.expected - have - q) + '</span>');
  if (capped || l.tag === "capped") tips.push('<span class=warn>今日剩余可订仅 ' + l.cap + (l.suggested > l.cap ? ',建议 ' + l.suggested + ' 只能订 ' + l.cap : '') + '</span>');
  if (whShort) tips.push('<span class=warn>关注仓合计 ' + g(tot) + ',不够 ' + need + '</span>');
  if (q > 0 && l.suggested > 0 && q !== Math.min(l.suggested, l.cap === null ? l.suggested : l.cap)) tips.push('与建议 ' + l.suggested + ' 不同');
  if (q === 0 && l.suggested > 0 && l.tag !== "oos" && l.tag !== "error") tips.push('<span class=warn>本次不订</span>');
  if (!tips.length && l.note) tips.push(esc(l.note));
  return {q: q, have: have, need: need, ok: ok, below: below, after: after, whShort: whShort, cls: cls, tips: tips.join(";")};
}

function rowHtml(l){
  var f = flags(l), gap = l.tag === "error" ? 0 : Math.max(l.expected - f.have, 0), id = idOf(l.pn);
  var h = '<tr id="r_' + id + '" class="' + f.cls + '"><td class="c0"><span class="nm">' + (l.seq ? l.seq + '. ' : '') + esc(l.name) + '</span><span class="pn">' + esc(l.pn) + '</span></td>';
  if (CAN && l.editable) {
    h += '<td><span class="qty"><button type="button" onclick="step(\'' + esc(l.pn) + '\',-1)">−</button><input id="q_' + id + '" type="number" inputmode="numeric" min="0" data-pn="' + esc(l.pn) + '" value="' + f.q + '" oninput="setQty(this.getAttribute(\'data-pn\'),this.value,true)" onblur="setQty(this.getAttribute(\'data-pn\'),this.value)"><button type="button" onclick="step(\'' + esc(l.pn) + '\',1)">+</button></span></td>';
  } else h += '<td class="bq">' + (f.q > 0 ? f.q : '<span class=muted>0</span>') + '</td>';
  h += '<td' + (gap > 0 ? ' class="gap"' : '') + '>' + (gap > 0 ? g(gap) : '—') + '</td>';
  WH.forEach(function(w){
    if (!l.wh || !(w in l.wh)) { h += '<td class="muted">—</td>'; return; }
    h += '<td id="w_' + id + '_' + idOf(w) + '">' + g(l.wh[w]) + ' <span class="' + (f.ok[w] ? 'y">✔' : 'n">✘') + '</span></td>';
  });
  h += '<td>' + (l.suggested > 0 ? '<b>' + l.suggested + '</b>' : '0') + '</td><td>' + (l.cap === null || l.cap === undefined ? '不限' : l.cap) + '</td>'
    + '<td>' + l.expected + '</td><td>' + g(l.stock) + '</td><td title="调拨在途 ' + g(l.trans_shipped) + ' / 未收货 ' + g(l.unreceived) + '">' + g(l.in_transit) + '</td>';
  h += '<td>¥' + g(l.unit_price) + '</td><td id="a_' + id + '">' + (f.q > 0 ? '¥' + g(f.q * (l.unit_price||0)) : '—') + '</td><td class="tip" id="t_' + id + '">' + f.tips + '</td></tr>';
  return h;
}

function render(s){
  STATE = s; var r = s.run; CAN = s.is_current && r.status === "awaiting_confirm";
  document.getElementById("doc").textContent = "库存补货单 " + r.doc_no;
  document.getElementById("mk").innerHTML = s.mock ? '<span class="mock">模拟DMS</span>' : "";
  document.getElementById("meta").textContent = "查询时间 " + String(r.created_at).slice(0,16) + " 截单 " + s.cutoff + " 在途按「" + (s.in_transit_label || "调拨在途") + "」";
  document.getElementById("status").innerHTML = '<span class="' + (r.status==="error"?"err":(r.status==="done"?"ok":"warn")) + '">' + esc(STAGE[r.status]||r.status) + "</span>" + (r.message && r.status!=="done" ? '<div class="err" style="font-size:12px">' + esc(r.message) + "</div>" : "");
  document.body.className = CAN ? "edit" : "";
  document.getElementById("bar").style.display = CAN ? "flex" : "none";
  if (!editing) {
    QTY = {}; LINES = {}; WH = [];
    var lines = s.lines.map(function(l, i){ l.pn = l.part_no; l.i = i; l.editable = l.tag !== "oos" && l.tag !== "error"; return l; });
    lines.forEach(function(l){ for (var w in (l.wh||{})) if (WH.indexOf(w) < 0) WH.push(w); LINES[l.pn] = l; if (CAN && l.editable && (l.suggested > 0 || l.final_qty > 0)) QTY[l.pn] = l.final_qty; else if (CAN && l.editable) QTY[l.pn] = 0; });
    /* 要订且需留意的 → 正常要订的 → 缺货 / 剔除 / 查询失败 → 库存充足(灰色沉底);同组内按清单序号 */
    function grp(l){ return l.orderable ? (l.attention ? 0 : 1) : (l.attention || l.tag === "removed" ? 2 : 3); }
    lines.sort(function(a, b){ return grp(a) - grp(b) || ((a.seq||999) - (b.seq||999)) || (a.i - b.i); });
    var h = '<thead><tr><th class="c0">配件</th><th>订购数量</th><th>缺口</th>';
    WH.forEach(function(w){ h += '<th title="' + esc(w) + '">' + esc(whName(w)) + '</th>'; });
    h += '<th>建议</th><th>今日可订</th><th>期望</th><th>库存</th><th>在途</th><th>单价</th><th>小计</th><th class="l">提示</th></tr></thead><tbody>';
    lines.forEach(function(l){ h += rowHtml(l); });
    document.getElementById("tbl").innerHTML = h + (lines.length ? "" : '<tr><td class="l">没有配件</td></tr>') + "</tbody>";
    totals();
  }
  if (!CAN) { editing = false; document.getElementById("dirty").style.display = "none"; }
  renderPo(s, r);
  document.getElementById("ev").innerHTML = s.events.map(function(e){return '<div class="' + (e.level==="error"?"err":(e.level==="warn"?"warn":"")) + '">' + esc(String(e.ts).slice(11,19)) + " " + esc(e.detail) + "</div>";}).join("");
}

/* 汇总 + 顶部提示栏(随数量修改实时更新) */
function totals(){
  var n = 0, qty = 0, amt = 0, below = 0, after = 0, whs = 0, cap = 0, oos = 0, err = 0, first = {};
  function mark(k, pn){ if (!first[k]) first[k] = pn; }
  for (var pn in LINES) {
    var l = LINES[pn], f = flags(l);
    if (f.q > 0) { n++; qty += f.q; amt += f.q * (l.unit_price || 0); }
    if (f.below) { below++; mark("below", pn); }
    if (f.after) { after++; mark("after", pn); }
    if (f.whShort) { whs++; mark("wh", pn); }
    if (l.tag === "capped") { cap++; mark("cap", pn); }
    if (l.tag === "oos") { oos++; mark("oos", pn); }
    if (l.tag === "error") { err++; mark("err", pn); }
  }
  var total = Object.keys(LINES).length, a = [];
  if (after) a.push(chip("red", "after", after + " 项订完仍低于期望", first));
  if (below) a.push(chip(after ? "org" : "okc", "below", below + " 项当前低于期望" + (after ? "" : "(订完都能补足)"), first));
  if (whs) a.push(chip("org", "wh", whs + " 项关注仓库存不够订购量", first));
  if (cap) a.push(chip("org", "cap", cap + " 项按当日剩余可订上限", first));
  if (oos) a.push(chip("red", "oos", oos + " 项厂家缺货不订", first));
  if (err) a.push(chip("red", "err", err + " 项查询失败", first));
  if (!a.length && total) a.push('<span class="chip okc">全部配件都满足期望</span>');
  document.getElementById("alerts").innerHTML = a.join("");
  var t = "共 " + total + " 个配件 本次订 " + n + " 项 / " + qty + " 件,约 ¥" + g(amt);
  document.getElementById("sum").textContent = t;
  document.getElementById("bartext").textContent = "订 " + n + " 项 / " + qty + " 件 ≈ ¥" + g(amt);
}
function chip(color, key, text, first){ return '<span class="chip ' + color + '" onclick="jump(\'' + esc(first[key]) + '\')">' + esc(text) + ' ›</span>'; }
function jump(pn){ var tr = document.getElementById("r_" + idOf(pn)); if (!tr) return; tr.scrollIntoView({block: "center"}); tr.classList.remove("flash"); void tr.offsetWidth; tr.classList.add("flash"); }

function repaint(pn){
  var l = LINES[pn], f = flags(l), id = idOf(pn), tr = document.getElementById("r_" + id), inp = document.getElementById("q_" + id);
  if (!tr) return;
  tr.className = f.cls;
  if (inp && document.activeElement !== inp) inp.value = f.q;
  document.getElementById("a_" + id).textContent = f.q > 0 ? "¥" + g(f.q * (l.unit_price || 0)) : "—";
  document.getElementById("t_" + id).innerHTML = f.tips;
  WH.forEach(function(w){ var c = document.getElementById("w_" + id + "_" + idOf(w)); if (c) c.innerHTML = g(l.wh[w]) + ' <span class="' + (f.ok[w] ? 'y">✔' : 'n">✘') + '</span>'; });
  totals();
}
function setQty(pn, v, typing){
  var l = LINES[pn]; if (!l || !CAN || !l.editable) return;
  if (typing && (v === "" || v === null)) return;
  var q = parseInt(v, 10); if (isNaN(q) || q < 0) q = 0;
  if (l.cap !== null && l.cap !== undefined && q > l.cap) q = l.cap;
  QTY[pn] = q; editing = true;
  document.getElementById("dirty").style.display = "block";
  repaint(pn);
}
function step(pn, d){ setQty(pn, (QTY[pn] || 0) + d); }

function renderPo(s, r){
  var po = s.po, pe = document.getElementById("po");
  if (!po) { pe.style.display = "none"; return; }
  var ph = '<div class="st">DMS 采购单 ' + esc(po.order_no) + '(' + esc(po.status) + ')</div><div class="muted" style="margin:4px 0 8px">供应商 ' + esc(po.supplier_code) + ' 订单类型 ' + esc(po.order_type) + ' 金额 ¥' + esc(po.total_amount) + ' 创建 ' + esc(po.created_at) + '</div>';
  ph += '<div style="overflow-x:auto"><table><thead><tr><th>配件码</th><th>名称</th><th>采购数量</th></tr></thead><tbody>';
  (po.lines||[]).forEach(function(d){ ph += '<tr><td>' + esc(d.partNo) + '</td><td>' + esc(d.partName) + '</td><td>' + esc(d.purchaseQty) + '</td></tr>'; });
  ph += '</tbody></table></div>';
  (po.shots||[]).forEach(function(f){ ph += '<a href="' + esc(f.url) + '" target="_blank"><img src="' + esc(f.url) + '"></a>'; });
  if (r.status === "done") ph += '<div class="warn" style="font-size:12px;margin-top:6px">' + (s.mock ? "模拟 DMS 演示单据。" : "这是 DMS 里的真实单据(待提交,未提交)。") + "流程到此结束,不会自动提交或删除。</div>";
  pe.innerHTML = ph; pe.style.display = "block";
}

function load(){req("GET","/api/order/"+TOKEN,null,function(st,j){ if(st===200) render(j); else document.getElementById("meta").textContent = "加载失败(" + st + ")"; });}
function confirmOrder(){
  var qty = {}, n = 0;
  for (var pn in QTY) { qty[pn] = QTY[pn]; if (QTY[pn] > 0) n++; }
  if (!n) { alert("所有配件数量都是 0;如果今天不订,请点「今天不订」"); return; }
  var b = document.getElementById("okbtn"); b.disabled = true; b.textContent = "提交中…";
  req("POST","/api/order/"+TOKEN+"/confirm",{qty:qty},function(st,j){ b.disabled=false; b.textContent="确认订购";
    if(st===200){ editing=false; document.getElementById("dirty").style.display="none"; }
    else alert((j&&(j.message||j.detail))||"确认失败("+st+")"); load(); });
}
function cancelToday(){ if(!confirm("确定今天不订?")) return; req("POST","/api/order/"+TOKEN+"/cancel",null,function(st,j){ editing=false; if(st!==200) alert((j&&(j.message||j.detail))||"失败"); load(); }); }
load(); setInterval(function(){ if(!editing) load(); }, 3000);
</script></body></html>