wrolpi/app/src/components/inventory/InventoryPrint.js
Roland 947ee01b25
Add CSV/PDF export and a search filter to Inventory
New Export tab with two client-side formats (no new dependencies):
- CSV: the raw items table (RFC-4180 quoting, UTF-8 BOM for Excel), generated
  entirely in the browser.  The download anchor is attached to the DOM and the
  object URL is revoked on the next tick for cross-browser robustness.
- PDF: a printable full-inventory table followed by a group-by summary table,
  via the browser's print dialog ("Save as PDF").  The summary's grouping is
  chosen in the Export tab.

Search filter: a box on its own line under the inventory selector filters the
active inventory across every column's displayed value (case-insensitive;
whitespace-separated terms are AND-ed).  It narrows the whole view — Items
table, Summary aggregation, and Export/PDF — while edits/adds in the table
still operate on the full inventory, so filtering never drops data.

Summary/export grouping:
- The group-by aggregation is extracted into a shared summarize.js (field-role
  detection + count-aware totals via units.js + calories), now the single
  source of truth for the Summary tab, the PDF summary, and the ration
  calculator.  findCaloriesKey/findCountKey moved here from RationCalculator
  (re-exported for back-compat).
- The Sum selector now offers number and calories fields in addition to
  quantity: number/calories are summed as a plain column total, while quantity
  stays count-aware and unit-compacted and the dedicated Calories column stays
  count-aware.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-19 11:06:38 -06:00

57 lines
2.4 KiB
JavaScript

import React from "react";
import {formatValue} from "./InventoryTable";
import {findCaloriesKey, findCountKey, summarizeInventory} from "./summarize";
/**
* Printable rendering of a full inventory (every field, every item) plus a summary table grouped by `groupKey`,
* used for the "PDF" export via the browser's print dialog. It is hidden on screen
* (`.inventory-print { display: none }` in App.css) and becomes the only visible content when printing — so the
* user can "Save as PDF" without the app chrome.
*/
export function InventoryPrint({name, fields, items, groupKey, sumKey}) {
const cols = [...(fields || [])].sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
const list = items || [];
const printed = new Date().toLocaleString();
const caloriesKey = findCaloriesKey(fields);
const countKey = findCountKey(fields);
const summaryRows = summarizeInventory(list, {fields, groupKey, sumKey, countKey, caloriesKey});
const groupLabel = cols.find(f => f.key === groupKey)?.label || 'Group';
return <div className='inventory-print'>
<h1>{name}</h1>
<p className='inventory-print-meta'>{list.length} item{list.length === 1 ? '' : 's'} · printed {printed}</p>
<table>
<thead>
<tr>{cols.map(f => <th key={f.key}>{f.label || f.key}</th>)}</tr>
</thead>
<tbody>
{list.map(item => <tr key={item.id}>
{cols.map(f => <td key={f.key}>{formatValue(item, f)}</td>)}
</tr>)}
</tbody>
</table>
{summaryRows.length > 0 && <>
<h2>Summary by {groupLabel}</h2>
<table>
<thead>
<tr>
<th>{groupLabel}</th>
<th>Items</th>
{sumKey && <th>Total</th>}
{caloriesKey && <th>Calories</th>}
</tr>
</thead>
<tbody>
{summaryRows.map(row => <tr key={row.name}>
<td>{row.name}</td>
<td>{row.count}</td>
{sumKey && <td>{row.total}</td>}
{caloriesKey && <td>{row.calories > 0 ? `${row.calories.toLocaleString()} kcal` : '—'}</td>}
</tr>)}
</tbody>
</table>
</>}
</div>;
}