feat(warehouse): add reservations, inventory, reports, and dashboard pages

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
BOHA
2026-05-29 14:36:31 +02:00
parent fdc7037e60
commit f389c4b3cf
6 changed files with 2240 additions and 0 deletions

View File

@@ -0,0 +1,582 @@
import { useState } from "react";
import { useQuery } from "@tanstack/react-query";
import { useAuth } from "../context/AuthContext";
import Forbidden from "../components/Forbidden";
import { motion } from "framer-motion";
import FormField from "../components/FormField";
import { projectListOptions } from "../lib/queries/projects";
import {
warehouseStockStatusOptions,
warehouseBelowMinimumOptions,
warehouseCategoryListOptions,
type WarehouseItem,
} from "../lib/queries/warehouse";
import apiFetch from "../utils/api";
import { formatCurrency } from "../utils/formatters";
type TabId =
| "stock-status"
| "project-consumption"
| "movement-log"
| "below-minimum";
interface ProjectConsumptionRow {
item_name: string;
project_name: string;
total_qty: number;
total_value: number;
}
interface MovementLogRow {
date: string;
type: string;
document_number: string;
item_name: string;
quantity: number;
unit_price: number;
supplier_or_project: string;
}
const TABS: { id: TabId; label: string }[] = [
{ id: "stock-status", label: "Stav zásob" },
{ id: "project-consumption", label: "Spotřeba projektů" },
{ id: "movement-log", label: "Pohyby" },
{ id: "below-minimum", label: "Pod minimem" },
];
export default function WarehouseReports() {
const { hasPermission } = useAuth();
const [activeTab, setActiveTab] = useState<TabId>("stock-status");
// Stock status filters
const [categoryId, setCategoryId] = useState<number | "">("");
// Project consumption filters
const [consumptionProjectId, setConsumptionProjectId] = useState<number | "">(
"",
);
const [consumptionDateFrom, setConsumptionDateFrom] = useState("");
const [consumptionDateTo, setConsumptionDateTo] = useState("");
// Movement log filters
const [movementType, setMovementType] = useState<string>("");
const [movementDateFrom, setMovementDateFrom] = useState("");
const [movementDateTo, setMovementDateTo] = useState("");
if (!hasPermission("warehouse.view")) return <Forbidden />;
return (
<div>
<motion.div
className="admin-page-header"
initial={{ opacity: 0, y: 12 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.25 }}
>
<div>
<h1 className="admin-page-title">Skladové reporty</h1>
</div>
</motion.div>
{/* Tab navigation */}
<motion.div
className="admin-card"
initial={{ opacity: 0, y: 12 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.25, delay: 0.06 }}
>
<div className="admin-card-body">
<div className="admin-tabs" style={{ marginBottom: "1.5rem" }}>
{TABS.map((tab) => (
<button
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>
</div>
);
}
/* ---------- Tab 1: Stock Status ---------- */
function StockStatusTab({
categoryId,
setCategoryId,
}: {
categoryId: number | "";
setCategoryId: (v: number | "") => void;
}) {
const { data: categories = [] } = useQuery(warehouseCategoryListOptions());
const { data: items, isPending } = useQuery(
warehouseStockStatusOptions({
category_id: categoryId ? Number(categoryId) : undefined,
}),
);
return (
<div>
<div className="admin-search-bar mb-4">
{categories.length > 0 && (
<select
value={categoryId}
onChange={(e) =>
setCategoryId(e.target.value === "" ? "" : Number(e.target.value))
}
className="admin-form-select"
style={{ minWidth: 180 }}
>
<option value="">Všechny kategorie</option>
{categories.map((cat) => (
<option key={cat.id} value={cat.id}>
{cat.name}
</option>
))}
</select>
)}
</div>
{isPending ? (
<div className="admin-loading">
<div className="admin-spinner" />
</div>
) : (
<div className="admin-table-responsive">
<table className="admin-table">
<thead>
<tr>
<th>Název</th>
<th className="text-right">Celkem</th>
<th className="text-right">K dispozici</th>
<th className="text-right">Hodnota</th>
<th className="text-right">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 text-right">
{item.total_quantity ?? 0}
</td>
<td className="admin-mono text-right">
{item.available_quantity ?? 0}
</td>
<td className="admin-mono text-right">
{item.stock_value != null
? formatCurrency(item.stock_value, "CZK")
: "0,00 Kč"}
</td>
<td className="admin-mono text-right">
{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>
);
}
/* ---------- Tab 2: Project Consumption ---------- */
function ProjectConsumptionTab({
projectId,
setProjectId,
dateFrom,
setDateFrom,
dateTo,
setDateTo,
}: {
projectId: number | "";
setProjectId: (v: number | "") => void;
dateFrom: string;
setDateFrom: (v: string) => void;
dateTo: string;
setDateTo: (v: string) => void;
}) {
const { data: projectsData } = useQuery(projectListOptions({ perPage: 100 }));
const projects =
(projectsData?.data as {
id: number;
project_number: string;
name: string;
}[]) ?? [];
const [data, setData] = useState<ProjectConsumptionRow[] | null>(null);
const [loading, setLoading] = useState(false);
const [fetched, setFetched] = useState(false);
const handleFetch = async () => {
setLoading(true);
setFetched(true);
try {
const params = new URLSearchParams();
if (projectId) params.set("project_id", String(projectId));
if (dateFrom) params.set("date_from", dateFrom);
if (dateTo) params.set("date_to", dateTo);
const qs = params.toString();
const response = await apiFetch(
`/api/admin/warehouse/reports/project-consumption${qs ? `?${qs}` : ""}`,
);
const result = await response.json();
if (result.success) {
setData(result.data ?? []);
} else {
setData([]);
}
} catch {
setData([]);
} finally {
setLoading(false);
}
};
return (
<div>
<div className="admin-search-bar mb-4">
{projects.length > 0 && (
<select
value={projectId}
onChange={(e) =>
setProjectId(e.target.value === "" ? "" : Number(e.target.value))
}
className="admin-form-select"
style={{ minWidth: 180 }}
>
<option value="">Všechny projekty</option>
{projects.map((p) => (
<option key={p.id} value={p.id}>
{p.project_number} {p.name}
</option>
))}
</select>
)}
<input
type="date"
value={dateFrom}
onChange={(e) => setDateFrom(e.target.value)}
className="admin-form-input"
style={{ width: 140 }}
/>
<input
type="date"
value={dateTo}
onChange={(e) => setDateTo(e.target.value)}
className="admin-form-input"
style={{ width: 140 }}
/>
<button
type="button"
onClick={handleFetch}
className="admin-btn admin-btn-primary"
disabled={loading}
>
{loading ? "Načítání..." : "Zobrazit"}
</button>
</div>
{!fetched && (
<div className="admin-empty-state">
<p>Zvolte filtr a klikněte na Zobrazit</p>
</div>
)}
{fetched && loading && (
<div className="admin-loading">
<div className="admin-spinner" />
</div>
)}
{fetched && !loading && data && (
<div className="admin-table-responsive">
<table className="admin-table">
<thead>
<tr>
<th>Položka</th>
<th>Projekt</th>
<th className="text-right">Množství</th>
<th className="text-right">Hodnota</th>
</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 text-right">{row.total_qty}</td>
<td className="admin-mono text-right">
{formatCurrency(row.total_value, "CZK")}
</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</div>
);
}
/* ---------- Tab 3: Movement Log ---------- */
function MovementLogTab({
type,
setType,
dateFrom,
setDateFrom,
dateTo,
setDateTo,
}: {
type: string;
setType: (v: string) => void;
dateFrom: string;
setDateFrom: (v: string) => void;
dateTo: string;
setDateTo: (v: string) => void;
}) {
const [data, setData] = useState<MovementLogRow[] | null>(null);
const [loading, setLoading] = useState(false);
const [fetched, setFetched] = useState(false);
const handleFetch = async () => {
setLoading(true);
setFetched(true);
try {
const params = new URLSearchParams();
if (type) params.set("type", type);
if (dateFrom) params.set("date_from", dateFrom);
if (dateTo) params.set("date_to", dateTo);
const qs = params.toString();
const response = await apiFetch(
`/api/admin/warehouse/reports/movement-log${qs ? `?${qs}` : ""}`,
);
const result = await response.json();
if (result.success) {
setData(result.data ?? []);
} else {
setData([]);
}
} catch {
setData([]);
} finally {
setLoading(false);
}
};
const TYPE_LABEL: Record<string, string> = {
receipt: "Příjem",
issue: "Výdej",
};
return (
<div>
<div className="admin-search-bar mb-4">
<select
value={type}
onChange={(e) => setType(e.target.value)}
className="admin-form-select"
style={{ minWidth: 140 }}
>
<option value="">Všechny typy</option>
<option value="receipt">Příjem</option>
<option value="issue">Výdej</option>
</select>
<input
type="date"
value={dateFrom}
onChange={(e) => setDateFrom(e.target.value)}
className="admin-form-input"
style={{ width: 140 }}
/>
<input
type="date"
value={dateTo}
onChange={(e) => setDateTo(e.target.value)}
className="admin-form-input"
style={{ width: 140 }}
/>
<button
type="button"
onClick={handleFetch}
className="admin-btn admin-btn-primary"
disabled={loading}
>
{loading ? "Načítání..." : "Zobrazit"}
</button>
</div>
{!fetched && (
<div className="admin-empty-state">
<p>Zvolte filtr a klikněte na Zobrazit</p>
</div>
)}
{fetched && loading && (
<div className="admin-loading">
<div className="admin-spinner" />
</div>
)}
{fetched && !loading && data && (
<div className="admin-table-responsive">
<table className="admin-table">
<thead>
<tr>
<th>Datum</th>
<th>Typ</th>
<th>Doklad</th>
<th>Položka</th>
<th className="text-right">Množství</th>
<th className="text-right">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 ${row.type === "receipt" ? "admin-badge-active" : "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 text-right">{row.quantity}</td>
<td className="admin-mono text-right">
{formatCurrency(row.unit_price, "CZK")}
</td>
<td>{row.supplier_or_project || "—"}</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</div>
);
}
/* ---------- Tab 4: Below Minimum ---------- */
function BelowMinimumTab() {
const { data: items, isPending } = useQuery(warehouseBelowMinimumOptions());
if (isPending) {
return (
<div className="admin-loading">
<div className="admin-spinner" />
</div>
);
}
return (
<div className="admin-table-responsive">
<table className="admin-table">
<thead>
<tr>
<th>Název</th>
<th className="text-right">Celkem</th>
<th className="text-right">K dispozici</th>
<th className="text-right">Min. množství</th>
</tr>
</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} style={{ background: "var(--danger-light)" }}>
<td className="fw-500">{item.name}</td>
<td className="admin-mono text-right">
{item.total_quantity ?? 0}
</td>
<td className="admin-mono text-right">
{item.available_quantity ?? 0}
</td>
<td
className="admin-mono text-right fw-500"
style={{ color: "var(--danger)" }}
>
{item.min_quantity ?? 0}
</td>
</tr>
))}
</tbody>
</table>
</div>
);
}