ph-bom.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * wc/ph-bom.js —— BOM 拆解(详情页与实验室共用)。
  3. * 桌面:按 group 分组的表格(序号 / 件名 / 规格 / 供应商 / 单价 / 数量 / 小计);
  4. * 手机:每组一行「件名 + 右侧小计」,点开展开规格与其余字段。
  5. * 可变宽度自适应切换用容器内 CSS 容器查询(实验室坞很窄时同样切成卡片)。
  6. */
  7. import { html, LitElement } from 'lit';
  8. // 样式见 wc/styles/bom-style.js(vendor lit-html 的 css`` 在部分源码上解析异常,改为静态文本注入)
  9. import { bomGroups } from '../services/products.js';
  10. import { money, num } from '../utils/format.js';
  11. import { applyCss } from '../lib/css.js';
  12. import { bomStyle } from './styles/bom-style.js';
  13. class PhBom extends LitElement {
  14. static properties = { bom: { attribute: false } };
  15. createRenderRoot() {
  16. applyCss(this, ...bomStyle); // light DOM + 页面级样式文本
  17. return this;
  18. }
  19. render() {
  20. const groups = bomGroups(this.bom);
  21. const total = this.bom?.bomTotal ?? groups.reduce((s, g) => s + g.subtotal, 0);
  22. return html`
  23. ${groups.map((g) => html`
  24. <section class="group">
  25. <div class="ghead">
  26. <h3>${g.group}</h3>
  27. <span class="gsum">${g.rows.length} 项 · <b>${money(g.subtotal)}</b></span>
  28. </div>
  29. <table>
  30. <thead><tr>
  31. <th class="seq">序号</th><th>件名</th><th>规格</th><th>供应商</th>
  32. <th class="r">单价</th><th class="r">数量</th><th class="r">小计</th>
  33. </tr></thead>
  34. <tbody>
  35. ${g.rows.map((r) => html`<tr>
  36. <td class="seq">${r.seq}</td>
  37. <td class="nm">${r.name}</td>
  38. <td class="sp">${r.spec}${r.notes ? html`<small>${r.notes}</small>` : ''}</td>
  39. <td class="sup">${r.supplier}</td>
  40. <td class="r">${money(r.price, 2)}</td>
  41. <td class="r">${r.qty}${r.unit ? html` <span style="color:var(--ink-3)">${r.unit}</span>` : ''}</td>
  42. <td class="r sub">${money(r.subtotal)}</td>
  43. </tr>`)}
  44. </tbody>
  45. </table>
  46. <div class="cards">
  47. ${g.rows.map((r) => html`<details>
  48. <summary>
  49. <span class="idx">${r.seq}</span>
  50. <span class="t">${r.name}</span>
  51. <span class="p">${money(r.subtotal)}</span>
  52. <span class="chev" aria-hidden="true">›</span>
  53. </summary>
  54. <div class="det">
  55. <dl>
  56. <dt>规格</dt><dd>${r.spec}${r.notes ? html`<br /><span style="color:var(--ink-3)">${r.notes}</span>` : ''}</dd>
  57. <dt>供应商</dt><dd>${r.supplier}</dd>
  58. <dt>单价 × 数量</dt><dd>${money(r.price, 2)} × ${r.qty}${r.unit || ''}</dd>
  59. </dl>
  60. </div>
  61. </details>`)}
  62. </div>
  63. </section>`)}
  64. <div class="total">
  65. <span>BOM 物料成本合计</span>
  66. <span class="sum">${money(total)}</span>
  67. </div>
  68. ${this.bom?.notes ? html`<p class="bomnote">${this.bom.notes}</p>` : ''}
  69. `;
  70. }
  71. }
  72. customElements.define('ph-bom', PhBom);
  73. export { PhBom };