feat(mui): migrate Warehouse Reports (Reporty) onto MUI kit

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
BOHA
2026-06-06 23:59:18 +02:00
parent b54fa84ac7
commit ff527ca577

View File

@@ -3,8 +3,7 @@ import { useQuery } from "@tanstack/react-query";
import { useAuth } from "../context/AuthContext"; import { useAuth } from "../context/AuthContext";
import Forbidden from "../components/Forbidden"; import Forbidden from "../components/Forbidden";
import { motion } from "framer-motion"; import { motion } from "framer-motion";
import FormField from "../components/FormField"; import Box from "@mui/material/Box";
import AdminDatePicker from "../components/AdminDatePicker";
import { projectListOptions } from "../lib/queries/projects"; import { projectListOptions } from "../lib/queries/projects";
import { import {
warehouseStockStatusOptions, warehouseStockStatusOptions,
@@ -14,6 +13,22 @@ import {
} from "../lib/queries/warehouse"; } from "../lib/queries/warehouse";
import apiFetch from "../utils/api"; import apiFetch from "../utils/api";
import { formatCurrency } from "../utils/formatters"; import { formatCurrency } from "../utils/formatters";
import {
Button,
Card,
DataTable,
Select,
DateField,
StatusChip,
PageHeader,
FilterBar,
EmptyState,
LoadingState,
Tabs,
TabPanel,
type DataColumn,
type TabDef,
} from "../ui";
type TabId = type TabId =
| "stock-status" | "stock-status"
@@ -38,17 +53,18 @@ interface MovementLogRow {
supplier_or_project: string; supplier_or_project: string;
} }
const TABS: { id: TabId; label: string }[] = [ const TABS: TabDef[] = [
{ id: "stock-status", label: "Stav zásob" }, { value: "stock-status", label: "Stav zásob" },
{ id: "project-consumption", label: "Spotřeba projektů" }, { value: "project-consumption", label: "Spotřeba projektů" },
{ id: "movement-log", label: "Pohyby" }, { value: "movement-log", label: "Pohyby" },
{ id: "below-minimum", label: "Pod minimem" }, { value: "below-minimum", label: "Pod minimem" },
]; ];
const MOVEMENT_BADGE: Record<string, string> = { // Movement type → StatusChip color (legacy: incoming=success, outgoing=info, inventory=info)
receipt: "admin-badge-incoming", const MOVEMENT_COLOR: Record<string, "success" | "info"> = {
issue: "admin-badge-outgoing", receipt: "success",
inventory: "admin-badge-info", issue: "info",
inventory: "info",
}; };
export default function WarehouseReports() { export default function WarehouseReports() {
@@ -73,68 +89,57 @@ export default function WarehouseReports() {
if (!hasPermission("warehouse.view")) return <Forbidden />; if (!hasPermission("warehouse.view")) return <Forbidden />;
return ( return (
<div> <Box>
<motion.div <motion.div
className="admin-page-header"
initial={{ opacity: 0, y: 12 }} initial={{ opacity: 0, y: 12 }}
animate={{ opacity: 1, y: 0 }} animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.25 }} transition={{ duration: 0.25 }}
> >
<div> <PageHeader title="Skladové reporty" />
<h1 className="admin-page-title">Skladové reporty</h1>
</div>
</motion.div> </motion.div>
{/* Tab navigation */}
<motion.div <motion.div
className="admin-card"
initial={{ opacity: 0, y: 12 }} initial={{ opacity: 0, y: 12 }}
animate={{ opacity: 1, y: 0 }} animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.25, delay: 0.06 }} transition={{ duration: 0.25, delay: 0.06 }}
> >
<div className="admin-card-body"> <Tabs
<div className="admin-tabs" style={{ marginBottom: "1.5rem" }}> value={activeTab}
{TABS.map((tab) => ( onChange={(v) => setActiveTab(v as TabId)}
<button tabs={TABS}
key={tab.id} />
className={`admin-tab ${activeTab === tab.id ? "active" : ""}`}
onClick={() => setActiveTab(tab.id)}
>
{tab.label}
</button>
))}
</div>
{activeTab === "stock-status" && (
<StockStatusTab
categoryId={categoryId}
setCategoryId={setCategoryId}
/>
)}
{activeTab === "project-consumption" && (
<ProjectConsumptionTab
projectId={consumptionProjectId}
setProjectId={setConsumptionProjectId}
dateFrom={consumptionDateFrom}
setDateFrom={setConsumptionDateFrom}
dateTo={consumptionDateTo}
setDateTo={setConsumptionDateTo}
/>
)}
{activeTab === "movement-log" && (
<MovementLogTab
type={movementType}
setType={setMovementType}
dateFrom={movementDateFrom}
setDateFrom={setMovementDateFrom}
dateTo={movementDateTo}
setDateTo={setMovementDateTo}
/>
)}
{activeTab === "below-minimum" && <BelowMinimumTab />}
</div>
</motion.div> </motion.div>
</div>
<TabPanel value="stock-status" current={activeTab}>
<StockStatusTab categoryId={categoryId} setCategoryId={setCategoryId} />
</TabPanel>
<TabPanel value="project-consumption" current={activeTab}>
<ProjectConsumptionTab
projectId={consumptionProjectId}
setProjectId={setConsumptionProjectId}
dateFrom={consumptionDateFrom}
setDateFrom={setConsumptionDateFrom}
dateTo={consumptionDateTo}
setDateTo={setConsumptionDateTo}
/>
</TabPanel>
<TabPanel value="movement-log" current={activeTab}>
<MovementLogTab
type={movementType}
setType={setMovementType}
dateFrom={movementDateFrom}
setDateFrom={setMovementDateFrom}
dateTo={movementDateTo}
setDateTo={setMovementDateTo}
/>
</TabPanel>
<TabPanel value="below-minimum" current={activeTab}>
<BelowMinimumTab />
</TabPanel>
</Box>
); );
} }
@@ -154,79 +159,91 @@ function StockStatusTab({
}), }),
); );
const columns: DataColumn<WarehouseItem>[] = [
{
key: "name",
header: "Název",
width: "30%",
bold: true,
render: (item) => item.name,
},
{
key: "total_quantity",
header: "Celkem",
width: "12%",
mono: true,
render: (item) => String(item.total_quantity ?? 0),
},
{
key: "available_quantity",
header: "K dispozici",
width: "13%",
mono: true,
render: (item) => String(item.available_quantity ?? 0),
},
{
key: "stock_value",
header: "Hodnota",
width: "17%",
mono: true,
render: (item) =>
item.stock_value != null
? formatCurrency(item.stock_value, "CZK")
: "0,00 Kč",
},
{
key: "min_quantity",
header: "Min. množství",
width: "14%",
mono: true,
render: (item) => (item.min_quantity ?? "—").toString(),
},
{
key: "status",
header: "Stav",
width: "14%",
render: (item) =>
item.below_minimum ? (
<StatusChip label="Pod min." color="error" />
) : (
<StatusChip label="OK" color="success" />
),
},
];
return ( return (
<div> <Box>
<div className="admin-search-bar mb-4"> <FilterBar>
{categories.length > 0 && ( {categories.length > 0 && (
<select <Box sx={{ flex: "0 0 220px" }}>
value={categoryId} <Select
onChange={(e) => value={categoryId === "" ? "" : String(categoryId)}
setCategoryId(e.target.value === "" ? "" : Number(e.target.value)) onChange={(val) => setCategoryId(val === "" ? "" : Number(val))}
} options={[
className="admin-form-select" { value: "", label: "Všechny kategorie" },
> ...categories.map((cat) => ({
<option value="">Všechny kategorie</option> value: String(cat.id),
{categories.map((cat) => ( label: cat.name,
<option key={cat.id} value={cat.id}> })),
{cat.name} ]}
</option> />
))} </Box>
</select>
)} )}
</div> </FilterBar>
{isPending ? ( {isPending ? (
<div className="admin-loading"> <LoadingState />
<div className="admin-spinner" />
</div>
) : ( ) : (
<div className="admin-table-responsive"> <Card>
<table className="admin-table"> <DataTable<WarehouseItem>
<thead> columns={columns}
<tr> rows={(items as WarehouseItem[] | undefined) ?? []}
<th>Název</th> rowKey={(item) => item.id}
<th>Celkem</th> empty={<EmptyState title="Žádné položky" />}
<th>K dispozici</th> />
<th>Hodnota</th> </Card>
<th>Min. množství</th>
<th>Stav</th>
</tr>
</thead>
<tbody>
{(items as WarehouseItem[] | undefined)?.length === 0 && (
<tr>
<td colSpan={6} className="admin-empty-state">
Žádné položky
</td>
</tr>
)}
{(items as WarehouseItem[] | undefined)?.map((item) => (
<tr key={item.id}>
<td className="fw-500">{item.name}</td>
<td className="admin-mono">{item.total_quantity ?? 0}</td>
<td className="admin-mono">{item.available_quantity ?? 0}</td>
<td className="admin-mono">
{item.stock_value != null
? formatCurrency(item.stock_value, "CZK")
: "0,00 Kč"}
</td>
<td className="admin-mono">{item.min_quantity ?? "—"}</td>
<td>
{item.below_minimum ? (
<span className="admin-badge admin-badge-danger">
Pod min.
</span>
) : (
<span className="admin-badge admin-badge-active">OK</span>
)}
</td>
</tr>
))}
</tbody>
</table>
</div>
)} )}
</div> </Box>
); );
} }
@@ -284,93 +301,80 @@ function ProjectConsumptionTab({
} }
}; };
const columns: DataColumn<ProjectConsumptionRow & { _idx: number }>[] = [
{
key: "item_name",
header: "Položka",
width: "32%",
bold: true,
render: (row) => row.item_name,
},
{
key: "project_name",
header: "Projekt",
width: "32%",
render: (row) => row.project_name,
},
{
key: "total_qty",
header: "Množství",
width: "18%",
mono: true,
render: (row) => String(row.total_qty),
},
{
key: "total_value",
header: "Hodnota",
width: "18%",
mono: true,
render: (row) => formatCurrency(row.total_value, "CZK"),
},
];
return ( return (
<div> <Box>
<div className="admin-search-bar mb-4"> <FilterBar>
{projects.length > 0 && ( {projects.length > 0 && (
<select <Box sx={{ flex: "0 0 280px" }}>
value={projectId} <Select
onChange={(e) => value={projectId === "" ? "" : String(projectId)}
setProjectId(e.target.value === "" ? "" : Number(e.target.value)) onChange={(val) => setProjectId(val === "" ? "" : Number(val))}
} options={[
className="admin-form-select" { value: "", label: "Všechny projekty" },
> ...projects.map((p) => ({
<option value="">Všechny projekty</option> value: String(p.id),
{projects.map((p) => ( label: `${p.project_number} ${p.name}`,
<option key={p.id} value={p.id}> })),
{p.project_number} {p.name} ]}
</option> />
))} </Box>
</select>
)} )}
<AdminDatePicker <Box sx={{ flex: "0 0 150px" }}>
mode="date" <DateField value={dateFrom} onChange={setDateFrom} label="Od" />
value={dateFrom} </Box>
onChange={setDateFrom} <Box sx={{ flex: "0 0 150px" }}>
placeholder="Od" <DateField value={dateTo} onChange={setDateTo} label="Do" />
/> </Box>
<AdminDatePicker <Button type="button" onClick={handleFetch} disabled={loading}>
mode="date"
value={dateTo}
onChange={setDateTo}
placeholder="Do"
/>
<button
type="button"
onClick={handleFetch}
className="admin-btn admin-btn-primary"
disabled={loading}
>
{loading ? "Načítání..." : "Zobrazit"} {loading ? "Načítání..." : "Zobrazit"}
</button> </Button>
</div> </FilterBar>
{!fetched && ( {!fetched && <EmptyState title="Zvolte filtr a klikněte na Zobrazit" />}
<div className="admin-empty-state">
<p>Zvolte filtr a klikněte na Zobrazit</p>
</div>
)}
{fetched && loading && ( {fetched && loading && <LoadingState />}
<div className="admin-loading">
<div className="admin-spinner" />
</div>
)}
{fetched && !loading && data && ( {fetched && !loading && data && (
<div className="admin-table-responsive"> <Card>
<table className="admin-table"> <DataTable<ProjectConsumptionRow & { _idx: number }>
<thead> columns={columns}
<tr> rows={data.map((row, i) => ({ ...row, _idx: i }))}
<th>Položka</th> rowKey={(row) => row._idx}
<th>Projekt</th> empty={<EmptyState title="Žádná data" />}
<th>Množství</th> />
<th>Hodnota</th> </Card>
</tr>
</thead>
<tbody>
{data.length === 0 && (
<tr>
<td colSpan={4} className="admin-empty-state">
Žádná data
</td>
</tr>
)}
{data.map((row, i) => (
<tr key={i}>
<td className="fw-500">{row.item_name}</td>
<td>{row.project_name}</td>
<td className="admin-mono">{row.total_qty}</td>
<td className="admin-mono">
{formatCurrency(row.total_value, "CZK")}
</td>
</tr>
))}
</tbody>
</table>
</div>
)} )}
</div> </Box>
); );
} }
@@ -425,98 +429,101 @@ function MovementLogTab({
issue: "Výdej", issue: "Výdej",
}; };
const columns: DataColumn<MovementLogRow & { _idx: number }>[] = [
{
key: "date",
header: "Datum",
width: "12%",
mono: true,
render: (row) => row.date,
},
{
key: "type",
header: "Typ",
width: "12%",
render: (row) => (
<StatusChip
label={TYPE_LABEL[row.type] ?? row.type}
color={MOVEMENT_COLOR[row.type] ?? "info"}
/>
),
},
{
key: "document_number",
header: "Doklad",
width: "14%",
mono: true,
render: (row) => row.document_number || "—",
},
{
key: "item_name",
header: "Položka",
width: "22%",
bold: true,
render: (row) => row.item_name,
},
{
key: "quantity",
header: "Množství",
width: "10%",
mono: true,
render: (row) => String(row.quantity),
},
{
key: "unit_price",
header: "Cena/ks",
width: "12%",
mono: true,
render: (row) => formatCurrency(row.unit_price, "CZK"),
},
{
key: "supplier_or_project",
header: "Dodavatel / Projekt",
width: "18%",
render: (row) => row.supplier_or_project || "—",
},
];
return ( return (
<div> <Box>
<div className="admin-search-bar mb-4"> <FilterBar>
<select <Box sx={{ flex: "0 0 160px" }}>
value={type} <Select
onChange={(e) => setType(e.target.value)} value={type}
className="admin-form-select" onChange={setType}
> options={[
<option value="">Všechny typy</option> { value: "", label: "Všechny typy" },
<option value="receipt">Příjem</option> { value: "receipt", label: "Příjem" },
<option value="issue">Výdej</option> { value: "issue", label: "Výdej" },
</select> ]}
<AdminDatePicker />
mode="date" </Box>
value={dateFrom} <Box sx={{ flex: "0 0 150px" }}>
onChange={setDateFrom} <DateField value={dateFrom} onChange={setDateFrom} label="Od" />
placeholder="Od" </Box>
/> <Box sx={{ flex: "0 0 150px" }}>
<AdminDatePicker <DateField value={dateTo} onChange={setDateTo} label="Do" />
mode="date" </Box>
value={dateTo} <Button type="button" onClick={handleFetch} disabled={loading}>
onChange={setDateTo}
placeholder="Do"
/>
<button
type="button"
onClick={handleFetch}
className="admin-btn admin-btn-primary"
disabled={loading}
>
{loading ? "Načítání..." : "Zobrazit"} {loading ? "Načítání..." : "Zobrazit"}
</button> </Button>
</div> </FilterBar>
{!fetched && ( {!fetched && <EmptyState title="Zvolte filtr a klikněte na Zobrazit" />}
<div className="admin-empty-state">
<p>Zvolte filtr a klikněte na Zobrazit</p>
</div>
)}
{fetched && loading && ( {fetched && loading && <LoadingState />}
<div className="admin-loading">
<div className="admin-spinner" />
</div>
)}
{fetched && !loading && data && ( {fetched && !loading && data && (
<div className="admin-table-responsive"> <Card>
<table className="admin-table"> <DataTable<MovementLogRow & { _idx: number }>
<thead> columns={columns}
<tr> rows={data.map((row, i) => ({ ...row, _idx: i }))}
<th>Datum</th> rowKey={(row) => row._idx}
<th>Typ</th> empty={<EmptyState title="Žádná data" />}
<th>Doklad</th> />
<th>Položka</th> </Card>
<th>Množství</th>
<th>Cena/ks</th>
<th>Dodavatel / Projekt</th>
</tr>
</thead>
<tbody>
{data.length === 0 && (
<tr>
<td colSpan={7} className="admin-empty-state">
Žádná data
</td>
</tr>
)}
{data.map((row, i) => (
<tr key={i}>
<td className="admin-mono">{row.date}</td>
<td>
<span
className={`admin-badge ${MOVEMENT_BADGE[row.type] ?? "admin-badge-info"}`}
>
{TYPE_LABEL[row.type] ?? row.type}
</span>
</td>
<td className="admin-mono">{row.document_number || "—"}</td>
<td className="fw-500">{row.item_name}</td>
<td className="admin-mono">{row.quantity}</td>
<td className="admin-mono">
{formatCurrency(row.unit_price, "CZK")}
</td>
<td>{row.supplier_or_project || "—"}</td>
</tr>
))}
</tbody>
</table>
</div>
)} )}
</div> </Box>
); );
} }
@@ -525,45 +532,51 @@ function MovementLogTab({
function BelowMinimumTab() { function BelowMinimumTab() {
const { data: items, isPending } = useQuery(warehouseBelowMinimumOptions()); const { data: items, isPending } = useQuery(warehouseBelowMinimumOptions());
const columns: DataColumn<WarehouseItem>[] = [
{
key: "name",
header: "Název",
width: "40%",
bold: true,
render: (item) => item.name,
},
{
key: "total_quantity",
header: "Celkem",
width: "20%",
mono: true,
render: (item) => String(item.total_quantity ?? 0),
},
{
key: "available_quantity",
header: "K dispozici",
width: "20%",
mono: true,
render: (item) => String(item.available_quantity ?? 0),
},
{
key: "min_quantity",
header: "Min. množství",
width: "20%",
mono: true,
bold: true,
render: (item) => String(item.min_quantity ?? 0),
},
];
if (isPending) { if (isPending) {
return ( return <LoadingState />;
<div className="admin-loading">
<div className="admin-spinner" />
</div>
);
} }
return ( return (
<div className="admin-table-responsive"> <Card>
<table className="admin-table"> <DataTable<WarehouseItem>
<thead> columns={columns}
<tr> rows={(items as WarehouseItem[] | undefined) ?? []}
<th>Název</th> rowKey={(item) => item.id}
<th>Celkem</th> rowDanger={() => true}
<th>K dispozici</th> empty={<EmptyState title="Všechny položky jsou nad minimem" />}
<th>Min. množství</th> />
</tr> </Card>
</thead>
<tbody>
{(items as WarehouseItem[] | undefined)?.length === 0 && (
<tr>
<td colSpan={4} className="admin-empty-state">
Všechny položky jsou nad minimem
</td>
</tr>
)}
{(items as WarehouseItem[] | undefined)?.map((item) => (
<tr key={item.id} className="admin-warehouse-row-danger">
<td className="fw-500">{item.name}</td>
<td className="admin-mono">{item.total_quantity ?? 0}</td>
<td className="admin-mono">{item.available_quantity ?? 0}</td>
<td className="admin-mono fw-500 admin-warehouse-danger-value">
{item.min_quantity ?? 0}
</td>
</tr>
))}
</tbody>
</table>
</div>
); );
} }