expected.html 5.68 KB
{% extends "base.html" %}
{% block body %}
<div class="card"><h3>期望库存表 <span class="muted">(补货 = 期望 −(库存 + 在途);触发时只查这里启用的配件)</span></h3>
  <p class="muted">改完数量点该行的「保存」即生效,下次在企业微信说「检查库存」时按新期望计算。手机上表格可以左右滑动。</p>
  <div style="overflow-x:auto;-webkit-overflow-scrolling:touch"><table id="tbl" style="min-width:760px"></table></div>
  <p class="muted" id="snap"></p>
</div>
<div class="card"><h3>新增配件</h3>
  <div class="row">
    <input id="pn" placeholder="配件码 *" style="width:160px">
    <input id="nm" placeholder="名称" style="width:160px">
    <input id="ex" type="number" min="0" placeholder="期望数量 *" style="width:110px">
    <label><input id="en" type="checkbox" checked> 启用</label>
    <input id="nt" placeholder="备注" style="width:200px">
    <button onclick="save()">保存</button>
  </div>
  <h3 style="margin-top:16px">从全量库存里找配件</h3>
  <div class="row"><input id="q" placeholder="配件码 / 名称 / 库位" style="width:260px" onkeydown="if(event.key==='Enter')search()"><button class="ghost" onclick="search()">搜索</button>
    <span class="muted">(需要先在总览页「同步全量库存」)</span></div>
  <div class="scroll"><table id="res"></table></div>
</div>
{% endblock %}
{% block script %}
<script>
var ROWS = {};
async function load() {
  var d = await api("/api/admin/expected"), h = "<tr><th>配件码</th><th>名称</th><th>期望数量</th><th>快照库存</th><th>启用</th><th>备注</th><th>更新时间</th><th></th></tr>";
  ROWS = {};
  d.rows.forEach(function (r, i) {
    ROWS[i] = r;
    h += "<tr id='r" + i + "'><td>" + esc(r.part_no) + "</td><td><input id='n" + i + "' value='" + esc(r.name || "").replace(/'/g, "&#39;") + "' style='width:120px' oninput='dirty(" + i + ")'></td>" +
      "<td><div class='row' style='gap:0;flex-wrap:nowrap'><button class='ghost' style='padding:8px 14px;font-size:16px' onclick='bump(" + i + ",-1)'>−</button>" +
      "<input id='e" + i + "' type='number' min='0' value='" + r.expected + "' style='width:70px;text-align:center;border-radius:0;font-size:16px;padding:8px 6px' oninput='dirty(" + i + ")'>" +
      "<button class='ghost' style='padding:8px 14px;font-size:16px' onclick='bump(" + i + ",1)'>+</button></div></td>" +
      "<td>" + (r.snapshot_stock === undefined || r.snapshot_stock === null ? "<span class=muted>—</span>" : r.snapshot_stock) + "</td>" +
      "<td><input id='a" + i + "' type='checkbox' " + (r.enabled ? "checked" : "") + " onchange='dirty(" + i + ")'></td>" +
      "<td><input id='t" + i + "' value='" + esc(r.note || "").replace(/'/g, "&#39;") + "' style='width:150px' oninput='dirty(" + i + ")'></td><td>" + fmtTs(r.updated_at) + "</td>" +
      "<td><button id='s" + i + "' onclick='saveRow(" + i + ")' disabled>保存</button> <a href='#' class=err onclick='del(\"" + esc(r.part_no) + "\");return false'>删除</a></td></tr>";
  });
  $("#tbl").innerHTML = h;
  $("#snap").textContent = d.batch ? ("快照库存取自全量库存批次 #" + d.batch.id + "(" + fmtTs(d.batch.finished_at || d.batch.started_at) + "," + d.batch.rows_count + " 行);补货时用的是触发那一刻 DMS 的实时库存。") : "还没有全量库存快照(到总览页点「同步全量库存」)。";
}
function dirty(i) { $("#s" + i).disabled = false; $("#r" + i).style.background = "#fffbe6"; }
function bump(i, d) { var e = $("#e" + i); e.value = Math.max(0, (parseInt(e.value, 10) || 0) + d); dirty(i); }
async function saveRow(i) {
  var r = ROWS[i], v = parseInt($("#e" + i).value, 10);
  if (isNaN(v) || v < 0) { toast("期望数量必须是 0 或正整数"); return; }
  try {
    await api("/api/admin/expected", { method: "POST", body: { part_no: r.part_no, name: $("#n" + i).value, expected: v, enabled: $("#a" + i).checked, note: $("#t" + i).value } });
    toast("已保存 " + r.part_no + " = " + v); load();
  } catch (e) { toast("保存失败:" + e.message, 5000); }
}
function edit(r) { $("#pn").value = r.part_no; $("#nm").value = r.name || ""; $("#ex").value = r.expected; $("#en").checked = !!r.enabled; $("#nt").value = r.note || ""; window.scrollTo(0, document.body.scrollHeight); }
async function save() {
  try { await api("/api/admin/expected", { method: "POST", body: { part_no: $("#pn").value, name: $("#nm").value, expected: $("#ex").value, enabled: $("#en").checked, note: $("#nt").value } }); toast("已保存"); load(); }
  catch (e) { toast("保存失败:" + e.message, 5000); }
}
async function del(pn) { if (!confirm("删除 " + pn + " ?")) return; await api("/api/admin/expected/" + encodeURIComponent(pn), { method: "DELETE" }); load(); }
async function search() {
  var d = await api("/api/admin/stock/rows?size=50&q=" + encodeURIComponent($("#q").value)), h = "<tr><th>配件码</th><th>名称</th><th>库位</th><th>库存</th><th>在途</th><th>零售价</th><th></th></tr>";
  if (!d.batch) { $("#res").innerHTML = "<tr><td class=warn>还没有全量库存快照</td></tr>"; return; }
  d.rows.forEach(function (r) { h += "<tr><td>" + esc(r.partNo) + "</td><td>" + esc(r.partName) + "</td><td>" + esc(r.locationNo) + "</td><td>" + r.storageQty + "</td><td>" + r.transShippedQty + "</td><td>" + r.srpPrice + "</td><td><a href='#' onclick='pick(\"" + esc(r.partNo) + "\",\"" + esc(r.partName) + "\");return false'>加入期望表</a></td></tr>"; });
  $("#res").innerHTML = h + (d.total > d.rows.length ? "<tr><td colspan=7 class=muted>共 " + d.total + " 行,只显示前 50 行</td></tr>" : "");
}
function pick(pn, nm) { $("#pn").value = pn; $("#nm").value = nm; $("#ex").value = ""; $("#ex").focus(); }
load();
</script>
{% endblock %}