feat(warehouse): add reservations, inventory, reports, and dashboard pages
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
375
src/admin/pages/Warehouse.tsx
Normal file
375
src/admin/pages/Warehouse.tsx
Normal file
@@ -0,0 +1,375 @@
|
|||||||
|
import { useState, useEffect } from "react";
|
||||||
|
import { Link } from "react-router-dom";
|
||||||
|
import { useQuery } from "@tanstack/react-query";
|
||||||
|
import { useAuth } from "../context/AuthContext";
|
||||||
|
import Forbidden from "../components/Forbidden";
|
||||||
|
import { motion } from "framer-motion";
|
||||||
|
import {
|
||||||
|
warehouseStockStatusOptions,
|
||||||
|
warehouseBelowMinimumOptions,
|
||||||
|
warehouseReservationListOptions,
|
||||||
|
type WarehouseItem,
|
||||||
|
type WarehouseReservation,
|
||||||
|
} from "../lib/queries/warehouse";
|
||||||
|
import apiFetch from "../utils/api";
|
||||||
|
import { formatCurrency, formatDate } from "../utils/formatters";
|
||||||
|
|
||||||
|
interface MovementLogRow {
|
||||||
|
date: string;
|
||||||
|
type: string;
|
||||||
|
document_number: string;
|
||||||
|
item_name: string;
|
||||||
|
quantity: number;
|
||||||
|
unit_price: number;
|
||||||
|
supplier_or_project: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const TYPE_LABEL: Record<string, string> = {
|
||||||
|
receipt: "Příjem",
|
||||||
|
issue: "Výdej",
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function Warehouse() {
|
||||||
|
const { hasPermission } = useAuth();
|
||||||
|
|
||||||
|
const { data: stockItems, isPending: stockPending } = useQuery(
|
||||||
|
warehouseStockStatusOptions(),
|
||||||
|
);
|
||||||
|
const { data: belowMinItems, isPending: belowMinPending } = useQuery(
|
||||||
|
warehouseBelowMinimumOptions(),
|
||||||
|
);
|
||||||
|
const { data: reservationsData, isPending: reservationsPending } = useQuery(
|
||||||
|
warehouseReservationListOptions({ status: "ACTIVE", perPage: 1 }),
|
||||||
|
);
|
||||||
|
|
||||||
|
const [recentMovements, setRecentMovements] = useState<MovementLogRow[]>([]);
|
||||||
|
const [movementsLoading, setMovementsLoading] = useState(true);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
async function fetchMovements() {
|
||||||
|
try {
|
||||||
|
const response = await apiFetch(
|
||||||
|
"/api/admin/warehouse/reports/movement-log?limit=10",
|
||||||
|
);
|
||||||
|
const result = await response.json();
|
||||||
|
if (result.success) {
|
||||||
|
setRecentMovements(result.data ?? []);
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// Non-fatal, show empty
|
||||||
|
} finally {
|
||||||
|
setMovementsLoading(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fetchMovements();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
if (!hasPermission("warehouse.view")) return <Forbidden />;
|
||||||
|
|
||||||
|
const items = (stockItems as WarehouseItem[] | undefined) ?? [];
|
||||||
|
const belowMin = (belowMinItems as WarehouseItem[] | undefined) ?? [];
|
||||||
|
const activeReservationCount =
|
||||||
|
(reservationsData as { pagination?: { total: number } } | undefined)
|
||||||
|
?.pagination?.total ?? 0;
|
||||||
|
|
||||||
|
const totalItems = items.length;
|
||||||
|
const totalStockValue = items.reduce(
|
||||||
|
(sum, item) => sum + (item.stock_value ?? 0),
|
||||||
|
0,
|
||||||
|
);
|
||||||
|
const belowMinimumCount = belowMin.length;
|
||||||
|
|
||||||
|
const isLoading =
|
||||||
|
stockPending || belowMinPending || reservationsPending || movementsLoading;
|
||||||
|
|
||||||
|
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">Sklad</h1>
|
||||||
|
<p className="admin-page-subtitle">Přehled skladových zásob</p>
|
||||||
|
</div>
|
||||||
|
<div className="admin-page-actions">
|
||||||
|
<Link to="/warehouse/items" className="admin-btn admin-btn-secondary">
|
||||||
|
Položky
|
||||||
|
</Link>
|
||||||
|
<Link
|
||||||
|
to="/warehouse/reports"
|
||||||
|
className="admin-btn admin-btn-secondary"
|
||||||
|
>
|
||||||
|
Reporty
|
||||||
|
</Link>
|
||||||
|
</div>
|
||||||
|
</motion.div>
|
||||||
|
|
||||||
|
{/* KPI cards */}
|
||||||
|
<div
|
||||||
|
className="dash-kpi-cards"
|
||||||
|
style={{
|
||||||
|
display: "grid",
|
||||||
|
gridTemplateColumns: "repeat(auto-fit, minmax(220px, 1fr))",
|
||||||
|
gap: "1rem",
|
||||||
|
marginBottom: "1.5rem",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<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" style={{ textAlign: "center" }}>
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
fontSize: "0.8rem",
|
||||||
|
color: "var(--text-secondary)",
|
||||||
|
marginBottom: "0.25rem",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Celkem položek
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
fontSize: "1.75rem",
|
||||||
|
fontWeight: 600,
|
||||||
|
color: "var(--text-primary)",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{isLoading ? "—" : totalItems}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</motion.div>
|
||||||
|
|
||||||
|
<motion.div
|
||||||
|
className="admin-card"
|
||||||
|
initial={{ opacity: 0, y: 12 }}
|
||||||
|
animate={{ opacity: 1, y: 0 }}
|
||||||
|
transition={{ duration: 0.25, delay: 0.08 }}
|
||||||
|
>
|
||||||
|
<div className="admin-card-body" style={{ textAlign: "center" }}>
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
fontSize: "0.8rem",
|
||||||
|
color: "var(--text-secondary)",
|
||||||
|
marginBottom: "0.25rem",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Hodnota zásob
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
fontSize: "1.75rem",
|
||||||
|
fontWeight: 600,
|
||||||
|
color: "var(--text-primary)",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{isLoading ? "—" : formatCurrency(totalStockValue, "CZK")}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</motion.div>
|
||||||
|
|
||||||
|
<motion.div
|
||||||
|
className="admin-card"
|
||||||
|
initial={{ opacity: 0, y: 12 }}
|
||||||
|
animate={{ opacity: 1, y: 0 }}
|
||||||
|
transition={{ duration: 0.25, delay: 0.1 }}
|
||||||
|
style={
|
||||||
|
belowMinimumCount > 0
|
||||||
|
? { border: "2px solid var(--danger)" }
|
||||||
|
: undefined
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<div className="admin-card-body" style={{ textAlign: "center" }}>
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
fontSize: "0.8rem",
|
||||||
|
color:
|
||||||
|
belowMinimumCount > 0
|
||||||
|
? "var(--danger)"
|
||||||
|
: "var(--text-secondary)",
|
||||||
|
marginBottom: "0.25rem",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Pod minimem
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
fontSize: "1.75rem",
|
||||||
|
fontWeight: 600,
|
||||||
|
color:
|
||||||
|
belowMinimumCount > 0
|
||||||
|
? "var(--danger)"
|
||||||
|
: "var(--text-primary)",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{isLoading ? "—" : belowMinimumCount}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</motion.div>
|
||||||
|
|
||||||
|
<motion.div
|
||||||
|
className="admin-card"
|
||||||
|
initial={{ opacity: 0, y: 12 }}
|
||||||
|
animate={{ opacity: 1, y: 0 }}
|
||||||
|
transition={{ duration: 0.25, delay: 0.12 }}
|
||||||
|
>
|
||||||
|
<div className="admin-card-body" style={{ textAlign: "center" }}>
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
fontSize: "0.8rem",
|
||||||
|
color: "var(--text-secondary)",
|
||||||
|
marginBottom: "0.25rem",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Aktivní rezervace
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
fontSize: "1.75rem",
|
||||||
|
fontWeight: 600,
|
||||||
|
color: "var(--text-primary)",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{isLoading ? "—" : activeReservationCount}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</motion.div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Below minimum alert */}
|
||||||
|
{belowMin.length > 0 && (
|
||||||
|
<motion.div
|
||||||
|
className="admin-card"
|
||||||
|
initial={{ opacity: 0, y: 12 }}
|
||||||
|
animate={{ opacity: 1, y: 0 }}
|
||||||
|
transition={{ duration: 0.25, delay: 0.14 }}
|
||||||
|
style={{ border: "2px solid var(--danger)" }}
|
||||||
|
>
|
||||||
|
<div className="admin-card-header flex-between">
|
||||||
|
<h2 className="admin-card-title" style={{ color: "var(--danger)" }}>
|
||||||
|
Položky pod minimem
|
||||||
|
</h2>
|
||||||
|
<Link
|
||||||
|
to="/warehouse/reports"
|
||||||
|
className="admin-btn admin-btn-primary admin-btn-sm"
|
||||||
|
>
|
||||||
|
Reporty →
|
||||||
|
</Link>
|
||||||
|
</div>
|
||||||
|
<div className="admin-card-body" style={{ padding: 0 }}>
|
||||||
|
<div className="admin-table-responsive">
|
||||||
|
<table className="admin-table">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Název</th>
|
||||||
|
<th className="text-right">K dispozici</th>
|
||||||
|
<th className="text-right">Min. množství</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{belowMin.map((item) => (
|
||||||
|
<tr key={item.id}>
|
||||||
|
<td className="fw-500">{item.name}</td>
|
||||||
|
<td
|
||||||
|
className="admin-mono text-right"
|
||||||
|
style={{ color: "var(--danger)" }}
|
||||||
|
>
|
||||||
|
{item.available_quantity ?? 0}
|
||||||
|
</td>
|
||||||
|
<td className="admin-mono text-right">
|
||||||
|
{item.min_quantity ?? 0}
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
))}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</motion.div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Recent movements */}
|
||||||
|
<motion.div
|
||||||
|
className="admin-card"
|
||||||
|
initial={{ opacity: 0, y: 12 }}
|
||||||
|
animate={{ opacity: 1, y: 0 }}
|
||||||
|
transition={{ duration: 0.25, delay: 0.16 }}
|
||||||
|
style={{ marginTop: "1.5rem" }}
|
||||||
|
>
|
||||||
|
<div className="admin-card-header flex-between">
|
||||||
|
<h2 className="admin-card-title">Poslední pohyby</h2>
|
||||||
|
<Link
|
||||||
|
to="/warehouse/reports"
|
||||||
|
className="admin-btn admin-btn-primary admin-btn-sm"
|
||||||
|
>
|
||||||
|
Vše →
|
||||||
|
</Link>
|
||||||
|
</div>
|
||||||
|
<div className="admin-card-body" style={{ padding: 0 }}>
|
||||||
|
{movementsLoading ? (
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
textAlign: "center",
|
||||||
|
padding: "2rem",
|
||||||
|
color: "var(--text-tertiary)",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<div className="admin-spinner admin-spinner-sm" />
|
||||||
|
</div>
|
||||||
|
) : recentMovements.length === 0 ? (
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
textAlign: "center",
|
||||||
|
padding: "2rem",
|
||||||
|
color: "var(--text-tertiary)",
|
||||||
|
fontSize: "0.875rem",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Žádné nedávné pohyby
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<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>Dodavatel / Projekt</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{recentMovements.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>{row.supplier_or_project || "—"}</td>
|
||||||
|
</tr>
|
||||||
|
))}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</motion.div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
229
src/admin/pages/WarehouseInventory.tsx
Normal file
229
src/admin/pages/WarehouseInventory.tsx
Normal file
@@ -0,0 +1,229 @@
|
|||||||
|
import { useState } from "react";
|
||||||
|
import { useNavigate } from "react-router-dom";
|
||||||
|
import { useAuth } from "../context/AuthContext";
|
||||||
|
import Forbidden from "../components/Forbidden";
|
||||||
|
import { motion, AnimatePresence } from "framer-motion";
|
||||||
|
import Pagination from "../components/Pagination";
|
||||||
|
import { usePaginatedQuery } from "../hooks/usePaginatedQuery";
|
||||||
|
|
||||||
|
import { formatDate } from "../utils/formatters";
|
||||||
|
import {
|
||||||
|
warehouseInventoryListOptions,
|
||||||
|
type WarehouseInventorySession,
|
||||||
|
} from "../lib/queries/warehouse";
|
||||||
|
|
||||||
|
const PER_PAGE = 20;
|
||||||
|
|
||||||
|
const STATUS_BADGE: Record<string, string> = {
|
||||||
|
DRAFT: "admin-badge-warning",
|
||||||
|
CONFIRMED: "admin-badge-active",
|
||||||
|
};
|
||||||
|
|
||||||
|
const STATUS_LABEL: Record<string, string> = {
|
||||||
|
DRAFT: "Návrh",
|
||||||
|
CONFIRMED: "Potvrzeno",
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function WarehouseInventory() {
|
||||||
|
const { hasPermission } = useAuth();
|
||||||
|
|
||||||
|
const [page, setPage] = useState(1);
|
||||||
|
const [statusFilter, setStatusFilter] = useState<string>("");
|
||||||
|
|
||||||
|
const { items, pagination, isPending, isFetching } =
|
||||||
|
usePaginatedQuery<WarehouseInventorySession>(
|
||||||
|
warehouseInventoryListOptions({
|
||||||
|
page,
|
||||||
|
perPage: PER_PAGE,
|
||||||
|
status: statusFilter || undefined,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
const navigate = useNavigate();
|
||||||
|
|
||||||
|
if (!hasPermission("warehouse.inventory")) return <Forbidden />;
|
||||||
|
|
||||||
|
const handleRowClick = (session: WarehouseInventorySession) => {
|
||||||
|
navigate(`/warehouse/inventory/${session.id}`);
|
||||||
|
};
|
||||||
|
|
||||||
|
const resetFilters = () => {
|
||||||
|
setStatusFilter("");
|
||||||
|
setPage(1);
|
||||||
|
};
|
||||||
|
|
||||||
|
if (isPending) {
|
||||||
|
return (
|
||||||
|
<div className="admin-loading">
|
||||||
|
<div className="admin-spinner" />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
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">Inventury</h1>
|
||||||
|
<p className="admin-page-subtitle">
|
||||||
|
{pagination?.total ?? items.length}{" "}
|
||||||
|
{pagination?.total === 1
|
||||||
|
? "inventura"
|
||||||
|
: pagination?.total !== undefined &&
|
||||||
|
pagination.total >= 2 &&
|
||||||
|
pagination.total <= 4
|
||||||
|
? "inventury"
|
||||||
|
: "inventur"}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div className="admin-page-actions">
|
||||||
|
<button
|
||||||
|
onClick={() => navigate("/warehouse/inventory/new")}
|
||||||
|
className="admin-btn admin-btn-primary"
|
||||||
|
>
|
||||||
|
<svg
|
||||||
|
width="20"
|
||||||
|
height="20"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth="2"
|
||||||
|
>
|
||||||
|
<line x1="12" y1="5" x2="12" y2="19" />
|
||||||
|
<line x1="5" y1="12" x2="19" y2="12" />
|
||||||
|
</svg>
|
||||||
|
Nová inventura
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</motion.div>
|
||||||
|
|
||||||
|
<motion.div
|
||||||
|
className="admin-card"
|
||||||
|
initial={{ opacity: 0, y: 12 }}
|
||||||
|
animate={{ opacity: 1, y: 0 }}
|
||||||
|
transition={{ duration: 0.25, delay: 0.06 }}
|
||||||
|
style={{ opacity: isFetching ? 0.6 : 1, transition: "opacity 0.2s" }}
|
||||||
|
>
|
||||||
|
<div className="admin-card-body">
|
||||||
|
<div className="admin-search-bar mb-4">
|
||||||
|
<select
|
||||||
|
value={statusFilter}
|
||||||
|
onChange={(e) => {
|
||||||
|
setStatusFilter(e.target.value);
|
||||||
|
setPage(1);
|
||||||
|
}}
|
||||||
|
className="admin-form-select"
|
||||||
|
style={{ minWidth: 140 }}
|
||||||
|
>
|
||||||
|
<option value="">Všechny stavy</option>
|
||||||
|
<option value="DRAFT">Návrh</option>
|
||||||
|
<option value="CONFIRMED">Potvrzeno</option>
|
||||||
|
</select>
|
||||||
|
{statusFilter && (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="admin-btn admin-btn-secondary"
|
||||||
|
onClick={resetFilters}
|
||||||
|
style={{ whiteSpace: "nowrap" }}
|
||||||
|
>
|
||||||
|
Zrušit filtry
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<AnimatePresence mode="wait">
|
||||||
|
<motion.div
|
||||||
|
key={`${statusFilter}-${page}-${items.length}`}
|
||||||
|
initial={{ opacity: 0, y: 6 }}
|
||||||
|
animate={{ opacity: 1, y: 0 }}
|
||||||
|
exit={{ opacity: 0 }}
|
||||||
|
transition={{ duration: 0.15 }}
|
||||||
|
>
|
||||||
|
{items.length === 0 && (
|
||||||
|
<div className="admin-empty-state">
|
||||||
|
<div className="admin-empty-icon">
|
||||||
|
<svg
|
||||||
|
width="28"
|
||||||
|
height="28"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth="1.5"
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
>
|
||||||
|
<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z" />
|
||||||
|
<polyline points="14 2 14 8 20 8" />
|
||||||
|
<line x1="16" y1="13" x2="8" y2="13" />
|
||||||
|
<line x1="16" y1="17" x2="8" y2="17" />
|
||||||
|
</svg>
|
||||||
|
</div>
|
||||||
|
<p>
|
||||||
|
{statusFilter
|
||||||
|
? "Žádné inventury pro zadaný filtr."
|
||||||
|
: "Zatím nejsou žádné inventury."}
|
||||||
|
</p>
|
||||||
|
{!statusFilter && (
|
||||||
|
<button
|
||||||
|
onClick={() => navigate("/warehouse/inventory/new")}
|
||||||
|
className="admin-btn admin-btn-primary"
|
||||||
|
>
|
||||||
|
Vytvořit první inventuru
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{items.length > 0 && (
|
||||||
|
<div className="admin-table-responsive">
|
||||||
|
<table className="admin-table">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Číslo inventury</th>
|
||||||
|
<th>Stav</th>
|
||||||
|
<th>Vytvořeno</th>
|
||||||
|
<th className="text-right">Řádků</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{items.map((session) => (
|
||||||
|
<tr
|
||||||
|
key={session.id}
|
||||||
|
onClick={() => handleRowClick(session)}
|
||||||
|
style={{ cursor: "pointer" }}
|
||||||
|
>
|
||||||
|
<td className="admin-mono fw-500">
|
||||||
|
{session.session_number || "—"}
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<span
|
||||||
|
className={`admin-badge ${STATUS_BADGE[session.status] ?? ""}`}
|
||||||
|
>
|
||||||
|
{STATUS_LABEL[session.status] ?? session.status}
|
||||||
|
</span>
|
||||||
|
</td>
|
||||||
|
<td className="admin-mono">
|
||||||
|
{formatDate(session.created_at)}
|
||||||
|
</td>
|
||||||
|
<td className="admin-mono text-right">
|
||||||
|
{session.lines?.length ?? 0}
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
))}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</motion.div>
|
||||||
|
</AnimatePresence>
|
||||||
|
|
||||||
|
<Pagination pagination={pagination} onPageChange={setPage} />
|
||||||
|
</div>
|
||||||
|
</motion.div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
280
src/admin/pages/WarehouseInventoryDetail.tsx
Normal file
280
src/admin/pages/WarehouseInventoryDetail.tsx
Normal file
@@ -0,0 +1,280 @@
|
|||||||
|
import { useState } from "react";
|
||||||
|
import { useParams, Link } from "react-router-dom";
|
||||||
|
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
||||||
|
import { useAlert } from "../context/AlertContext";
|
||||||
|
import { useAuth } from "../context/AuthContext";
|
||||||
|
import Forbidden from "../components/Forbidden";
|
||||||
|
import { motion } from "framer-motion";
|
||||||
|
import ConfirmModal from "../components/ConfirmModal";
|
||||||
|
import FormField from "../components/FormField";
|
||||||
|
|
||||||
|
import apiFetch from "../utils/api";
|
||||||
|
import { formatDate } from "../utils/formatters";
|
||||||
|
import {
|
||||||
|
warehouseInventoryDetailOptions,
|
||||||
|
type WarehouseInventorySession,
|
||||||
|
} from "../lib/queries/warehouse";
|
||||||
|
|
||||||
|
const STATUS_BADGE: Record<string, string> = {
|
||||||
|
DRAFT: "admin-badge-warning",
|
||||||
|
CONFIRMED: "admin-badge-active",
|
||||||
|
};
|
||||||
|
|
||||||
|
const STATUS_LABEL: Record<string, string> = {
|
||||||
|
DRAFT: "Návrh",
|
||||||
|
CONFIRMED: "Potvrzeno",
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function WarehouseInventoryDetail() {
|
||||||
|
const { id } = useParams();
|
||||||
|
const alert = useAlert();
|
||||||
|
const { hasPermission } = useAuth();
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
|
const [confirming, setConfirming] = useState(false);
|
||||||
|
const [showConfirmModal, setShowConfirmModal] = useState(false);
|
||||||
|
|
||||||
|
const {
|
||||||
|
data: session,
|
||||||
|
isPending,
|
||||||
|
error,
|
||||||
|
} = useQuery(warehouseInventoryDetailOptions(id));
|
||||||
|
|
||||||
|
if (!hasPermission("warehouse.inventory")) return <Forbidden />;
|
||||||
|
|
||||||
|
const handleConfirm = async () => {
|
||||||
|
if (!session) return;
|
||||||
|
setConfirming(true);
|
||||||
|
try {
|
||||||
|
const response = await apiFetch(
|
||||||
|
`/api/admin/warehouse/inventory-sessions/${session.id}/confirm`,
|
||||||
|
{ method: "POST" },
|
||||||
|
);
|
||||||
|
const result = await response.json();
|
||||||
|
if (result.success) {
|
||||||
|
queryClient.invalidateQueries({ queryKey: ["warehouse", "inventory"] });
|
||||||
|
setShowConfirmModal(false);
|
||||||
|
alert.success("Inventura byla potvrzena");
|
||||||
|
} else {
|
||||||
|
alert.error(result.error || "Nepodařilo se potvrdit inventuru");
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
alert.error("Chyba připojení");
|
||||||
|
} finally {
|
||||||
|
setConfirming(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (isPending) {
|
||||||
|
return (
|
||||||
|
<div className="admin-loading">
|
||||||
|
<div className="admin-spinner" />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (error || !session) {
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<div className="admin-page-header">
|
||||||
|
<div>
|
||||||
|
<Link
|
||||||
|
to="/warehouse/inventory"
|
||||||
|
className="admin-btn-icon"
|
||||||
|
title="Zpět na seznam"
|
||||||
|
>
|
||||||
|
<svg
|
||||||
|
width="20"
|
||||||
|
height="20"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth="2"
|
||||||
|
>
|
||||||
|
<path d="M19 12H5M12 19l-7-7 7-7" />
|
||||||
|
</svg>
|
||||||
|
</Link>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="admin-card">
|
||||||
|
<div className="admin-card-body">
|
||||||
|
<div className="admin-empty-state">
|
||||||
|
<p>Inventura nebyla nalezena.</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const s = session as WarehouseInventorySession;
|
||||||
|
const lines = s.lines ?? [];
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
{/* Header */}
|
||||||
|
<motion.div
|
||||||
|
className="admin-page-header"
|
||||||
|
initial={{ opacity: 0, y: 12 }}
|
||||||
|
animate={{ opacity: 1, y: 0 }}
|
||||||
|
transition={{ duration: 0.25 }}
|
||||||
|
>
|
||||||
|
<div style={{ display: "flex", alignItems: "center", gap: "1rem" }}>
|
||||||
|
<Link
|
||||||
|
to="/warehouse/inventory"
|
||||||
|
className="admin-btn-icon"
|
||||||
|
title="Zpět na seznam"
|
||||||
|
aria-label="Zpět na seznam"
|
||||||
|
>
|
||||||
|
<svg
|
||||||
|
width="20"
|
||||||
|
height="20"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth="2"
|
||||||
|
>
|
||||||
|
<path d="M19 12H5M12 19l-7-7 7-7" />
|
||||||
|
</svg>
|
||||||
|
</Link>
|
||||||
|
<div>
|
||||||
|
<h1 className="admin-page-title">
|
||||||
|
{s.session_number || "Inventura"}
|
||||||
|
</h1>
|
||||||
|
</div>
|
||||||
|
<span
|
||||||
|
className={`admin-badge ${STATUS_BADGE[s.status] ?? ""}`}
|
||||||
|
style={{ marginLeft: "0.5rem" }}
|
||||||
|
>
|
||||||
|
{STATUS_LABEL[s.status] ?? s.status}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div className="admin-page-actions">
|
||||||
|
{s.status === "DRAFT" && (
|
||||||
|
<button
|
||||||
|
onClick={() => setShowConfirmModal(true)}
|
||||||
|
className="admin-btn admin-btn-primary"
|
||||||
|
disabled={confirming}
|
||||||
|
>
|
||||||
|
{confirming ? "Potvrzování..." : "Potvrdit inventuru"}
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</motion.div>
|
||||||
|
|
||||||
|
{/* Header info */}
|
||||||
|
<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">
|
||||||
|
<h3 className="admin-card-title">Základní údaje</h3>
|
||||||
|
<div className="admin-form">
|
||||||
|
<div className="admin-form-row">
|
||||||
|
<FormField label="Číslo inventury">
|
||||||
|
<div className="admin-mono" style={{ fontWeight: 500 }}>
|
||||||
|
{s.session_number || "—"}
|
||||||
|
</div>
|
||||||
|
</FormField>
|
||||||
|
<FormField label="Vytvořeno">
|
||||||
|
<div className="admin-mono">{formatDate(s.created_at)}</div>
|
||||||
|
</FormField>
|
||||||
|
</div>
|
||||||
|
{s.notes && (
|
||||||
|
<FormField label="Poznámky">
|
||||||
|
<div style={{ whiteSpace: "pre-wrap" }}>{s.notes}</div>
|
||||||
|
</FormField>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</motion.div>
|
||||||
|
|
||||||
|
{/* Lines table */}
|
||||||
|
<motion.div
|
||||||
|
className="admin-card"
|
||||||
|
initial={{ opacity: 0, y: 12 }}
|
||||||
|
animate={{ opacity: 1, y: 0 }}
|
||||||
|
transition={{ duration: 0.25, delay: 0.08 }}
|
||||||
|
>
|
||||||
|
<div className="admin-card-body">
|
||||||
|
<h3 className="admin-card-title">Řádky inventury</h3>
|
||||||
|
{lines.length === 0 ? (
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
color: "var(--text-tertiary)",
|
||||||
|
fontSize: "0.875rem",
|
||||||
|
textAlign: "center",
|
||||||
|
padding: "1.5rem 0",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Žádné řádky
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className="admin-table-responsive">
|
||||||
|
<table className="admin-table">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Položka</th>
|
||||||
|
<th>Lokace</th>
|
||||||
|
<th className="text-right">Systémové množství</th>
|
||||||
|
<th className="text-right">Skutečné množství</th>
|
||||||
|
<th className="text-right">Rozdíl</th>
|
||||||
|
<th>Poznámka</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{lines.map((line) => {
|
||||||
|
const diff = Number(line.difference);
|
||||||
|
const diffColor =
|
||||||
|
diff > 0
|
||||||
|
? "var(--success)"
|
||||||
|
: diff < 0
|
||||||
|
? "var(--danger)"
|
||||||
|
: "var(--text-secondary)";
|
||||||
|
return (
|
||||||
|
<tr key={line.id}>
|
||||||
|
<td className="fw-500">
|
||||||
|
{line.item?.name || `ID: ${line.item_id}`}
|
||||||
|
</td>
|
||||||
|
<td className="admin-mono">
|
||||||
|
{line.location?.code || "—"}
|
||||||
|
</td>
|
||||||
|
<td className="admin-mono text-right">
|
||||||
|
{Number(line.system_qty)}
|
||||||
|
</td>
|
||||||
|
<td className="admin-mono text-right">
|
||||||
|
{Number(line.actual_qty)}
|
||||||
|
</td>
|
||||||
|
<td
|
||||||
|
className="admin-mono text-right fw-500"
|
||||||
|
style={{ color: diffColor }}
|
||||||
|
>
|
||||||
|
{diff > 0 ? `+${diff}` : diff}
|
||||||
|
</td>
|
||||||
|
<td>{line.notes || "—"}</td>
|
||||||
|
</tr>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</motion.div>
|
||||||
|
|
||||||
|
{/* Confirm modal */}
|
||||||
|
<ConfirmModal
|
||||||
|
isOpen={showConfirmModal}
|
||||||
|
onClose={() => setShowConfirmModal(false)}
|
||||||
|
onConfirm={handleConfirm}
|
||||||
|
title="Potvrdit inventuru"
|
||||||
|
message={`Opravdu chcete potvrdit inventuru „${s.session_number || s.id}"? Potvrzením se aktualizují skladové zásoby.`}
|
||||||
|
confirmText="Potvrdit inventuru"
|
||||||
|
type="warning"
|
||||||
|
loading={confirming}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
257
src/admin/pages/WarehouseInventoryForm.tsx
Normal file
257
src/admin/pages/WarehouseInventoryForm.tsx
Normal file
@@ -0,0 +1,257 @@
|
|||||||
|
import { useState } from "react";
|
||||||
|
import { useNavigate, Link } from "react-router-dom";
|
||||||
|
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
||||||
|
import { useAlert } from "../context/AlertContext";
|
||||||
|
import { useAuth } from "../context/AuthContext";
|
||||||
|
import Forbidden from "../components/Forbidden";
|
||||||
|
import { motion } from "framer-motion";
|
||||||
|
import FormField from "../components/FormField";
|
||||||
|
import ItemPicker from "../components/warehouse/ItemPicker";
|
||||||
|
import LocationSelect from "../components/warehouse/LocationSelect";
|
||||||
|
import { warehouseLocationListOptions } from "../lib/queries/warehouse";
|
||||||
|
import type { WarehouseLocation } from "../lib/queries/warehouse";
|
||||||
|
|
||||||
|
import apiFetch from "../utils/api";
|
||||||
|
|
||||||
|
interface InventoryLine {
|
||||||
|
key: string;
|
||||||
|
item_id: number | null;
|
||||||
|
location_id: number | null;
|
||||||
|
actual_qty: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
let lineCounter = 0;
|
||||||
|
|
||||||
|
export default function WarehouseInventoryForm() {
|
||||||
|
const alert = useAlert();
|
||||||
|
const { hasPermission } = useAuth();
|
||||||
|
const navigate = useNavigate();
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
|
const [lines, setLines] = useState<InventoryLine[]>([]);
|
||||||
|
const [notes, setNotes] = useState("");
|
||||||
|
const [saving, setSaving] = useState(false);
|
||||||
|
|
||||||
|
const { data: locations = [] } = useQuery(warehouseLocationListOptions());
|
||||||
|
|
||||||
|
if (!hasPermission("warehouse.inventory")) return <Forbidden />;
|
||||||
|
|
||||||
|
const addEmptyLine = () => {
|
||||||
|
setLines((prev) => [
|
||||||
|
...prev,
|
||||||
|
{
|
||||||
|
key: `line-${++lineCounter}`,
|
||||||
|
item_id: null,
|
||||||
|
location_id: null,
|
||||||
|
actual_qty: 0,
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Start with one empty line
|
||||||
|
if (lines.length === 0) {
|
||||||
|
addEmptyLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
const removeLine = (index: number) => {
|
||||||
|
setLines((prev) => prev.filter((_, i) => i !== index));
|
||||||
|
};
|
||||||
|
|
||||||
|
const updateLine = (index: number, field: string, value: unknown) => {
|
||||||
|
setLines((prev) => {
|
||||||
|
const updated = [...prev];
|
||||||
|
updated[index] = { ...updated[index], [field]: value };
|
||||||
|
return updated;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const validate = (): boolean => {
|
||||||
|
const validLines = lines.filter((l) => l.item_id !== null);
|
||||||
|
if (validLines.length === 0) {
|
||||||
|
alert.error("Přidejte alespoň jednu položku");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSubmit = async () => {
|
||||||
|
if (!validate()) return;
|
||||||
|
setSaving(true);
|
||||||
|
try {
|
||||||
|
const payload = {
|
||||||
|
notes: notes.trim() || null,
|
||||||
|
lines: lines
|
||||||
|
.filter((l) => l.item_id !== null)
|
||||||
|
.map((l) => ({
|
||||||
|
item_id: l.item_id!,
|
||||||
|
location_id: l.location_id,
|
||||||
|
actual_qty: l.actual_qty,
|
||||||
|
})),
|
||||||
|
};
|
||||||
|
|
||||||
|
const response = await apiFetch(
|
||||||
|
"/api/admin/warehouse/inventory-sessions",
|
||||||
|
{
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify(payload),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
const result = await response.json();
|
||||||
|
if (result.success) {
|
||||||
|
queryClient.invalidateQueries({ queryKey: ["warehouse", "inventory"] });
|
||||||
|
alert.success("Inventura byla vytvořena");
|
||||||
|
navigate(`/warehouse/inventory/${result.data?.id}`);
|
||||||
|
} else {
|
||||||
|
alert.error(result.error || "Nepodařilo se vytvořit inventuru");
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
alert.error("Chyba připojení");
|
||||||
|
} finally {
|
||||||
|
setSaving(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
{/* Header */}
|
||||||
|
<motion.div
|
||||||
|
className="admin-page-header"
|
||||||
|
initial={{ opacity: 0, y: 12 }}
|
||||||
|
animate={{ opacity: 1, y: 0 }}
|
||||||
|
transition={{ duration: 0.25 }}
|
||||||
|
>
|
||||||
|
<div style={{ display: "flex", alignItems: "center", gap: "1rem" }}>
|
||||||
|
<Link
|
||||||
|
to="/warehouse/inventory"
|
||||||
|
className="admin-btn-icon"
|
||||||
|
title="Zpět na seznam"
|
||||||
|
aria-label="Zpět na seznam"
|
||||||
|
>
|
||||||
|
<svg
|
||||||
|
width="20"
|
||||||
|
height="20"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth="2"
|
||||||
|
>
|
||||||
|
<path d="M19 12H5M12 19l-7-7 7-7" />
|
||||||
|
</svg>
|
||||||
|
</Link>
|
||||||
|
<div>
|
||||||
|
<h1 className="admin-page-title">Nová inventura</h1>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="admin-page-actions">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={handleSubmit}
|
||||||
|
className="admin-btn admin-btn-primary"
|
||||||
|
disabled={saving}
|
||||||
|
>
|
||||||
|
{saving ? "Ukládání..." : "Vytvořit inventuru"}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</motion.div>
|
||||||
|
|
||||||
|
{/* Notes */}
|
||||||
|
<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">
|
||||||
|
<h3 className="admin-card-title">Základní údaje</h3>
|
||||||
|
<FormField label="Poznámky">
|
||||||
|
<textarea
|
||||||
|
value={notes}
|
||||||
|
onChange={(e) => setNotes(e.target.value)}
|
||||||
|
className="admin-form-input"
|
||||||
|
rows={3}
|
||||||
|
placeholder="Volitelné poznámky k inventuře"
|
||||||
|
/>
|
||||||
|
</FormField>
|
||||||
|
</div>
|
||||||
|
</motion.div>
|
||||||
|
|
||||||
|
{/* Lines */}
|
||||||
|
<motion.div
|
||||||
|
className="admin-card"
|
||||||
|
initial={{ opacity: 0, y: 12 }}
|
||||||
|
animate={{ opacity: 1, y: 0 }}
|
||||||
|
transition={{ duration: 0.25, delay: 0.08 }}
|
||||||
|
>
|
||||||
|
<div className="admin-card-body">
|
||||||
|
<h3 className="admin-card-title">Řádky inventury</h3>
|
||||||
|
<div className="admin-table-responsive">
|
||||||
|
<table className="admin-table">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Položka</th>
|
||||||
|
<th>Lokace</th>
|
||||||
|
<th>Skutečné množství</th>
|
||||||
|
<th></th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{lines.map((line, index) => (
|
||||||
|
<tr key={line.key}>
|
||||||
|
<td>
|
||||||
|
<ItemPicker
|
||||||
|
value={line.item_id}
|
||||||
|
onChange={(id) => updateLine(index, "item_id", id)}
|
||||||
|
/>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<LocationSelect
|
||||||
|
value={line.location_id}
|
||||||
|
onChange={(locId) =>
|
||||||
|
updateLine(index, "location_id", locId)
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
className="admin-form-input"
|
||||||
|
value={line.actual_qty || ""}
|
||||||
|
onChange={(e) =>
|
||||||
|
updateLine(
|
||||||
|
index,
|
||||||
|
"actual_qty",
|
||||||
|
Number(e.target.value),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
min="0"
|
||||||
|
step="0.001"
|
||||||
|
/>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="admin-btn-danger-sm"
|
||||||
|
onClick={() => removeLine(index)}
|
||||||
|
>
|
||||||
|
×
|
||||||
|
</button>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
))}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="admin-btn-secondary"
|
||||||
|
onClick={addEmptyLine}
|
||||||
|
style={{ marginTop: "0.75rem" }}
|
||||||
|
>
|
||||||
|
+ Přidat řádek
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</motion.div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
582
src/admin/pages/WarehouseReports.tsx
Normal file
582
src/admin/pages/WarehouseReports.tsx
Normal 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>
|
||||||
|
);
|
||||||
|
}
|
||||||
517
src/admin/pages/WarehouseReservations.tsx
Normal file
517
src/admin/pages/WarehouseReservations.tsx
Normal file
@@ -0,0 +1,517 @@
|
|||||||
|
import { useState } from "react";
|
||||||
|
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
||||||
|
import { useAlert } from "../context/AlertContext";
|
||||||
|
import { useAuth } from "../context/AuthContext";
|
||||||
|
import Forbidden from "../components/Forbidden";
|
||||||
|
import { motion, AnimatePresence } from "framer-motion";
|
||||||
|
import Pagination from "../components/Pagination";
|
||||||
|
import ConfirmModal from "../components/ConfirmModal";
|
||||||
|
import FormField from "../components/FormField";
|
||||||
|
import ItemPicker from "../components/warehouse/ItemPicker";
|
||||||
|
import useDebounce from "../hooks/useDebounce";
|
||||||
|
import { usePaginatedQuery } from "../hooks/usePaginatedQuery";
|
||||||
|
|
||||||
|
import apiFetch from "../utils/api";
|
||||||
|
import { formatDate } from "../utils/formatters";
|
||||||
|
import {
|
||||||
|
warehouseReservationListOptions,
|
||||||
|
type WarehouseReservation,
|
||||||
|
} from "../lib/queries/warehouse";
|
||||||
|
import { projectListOptions } from "../lib/queries/projects";
|
||||||
|
|
||||||
|
const PER_PAGE = 20;
|
||||||
|
|
||||||
|
const STATUS_BADGE: Record<string, string> = {
|
||||||
|
ACTIVE: "admin-badge-active",
|
||||||
|
FULFILLED: "admin-badge-info",
|
||||||
|
CANCELLED: "admin-badge-danger",
|
||||||
|
};
|
||||||
|
|
||||||
|
const STATUS_LABEL: Record<string, string> = {
|
||||||
|
ACTIVE: "Aktivní",
|
||||||
|
FULFILLED: "Splněna",
|
||||||
|
CANCELLED: "Zrušena",
|
||||||
|
};
|
||||||
|
|
||||||
|
interface ReservationForm {
|
||||||
|
item_id: number | null;
|
||||||
|
project_id: number | null;
|
||||||
|
quantity: number;
|
||||||
|
notes: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function WarehouseReservations() {
|
||||||
|
const alert = useAlert();
|
||||||
|
const { hasPermission } = useAuth();
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
|
const [page, setPage] = useState(1);
|
||||||
|
const [statusFilter, setStatusFilter] = useState<string>("");
|
||||||
|
const [projectId, setProjectId] = useState<number | "">("");
|
||||||
|
|
||||||
|
const [showCreateModal, setShowCreateModal] = useState(false);
|
||||||
|
const [saving, setSaving] = useState(false);
|
||||||
|
const [form, setForm] = useState<ReservationForm>({
|
||||||
|
item_id: null,
|
||||||
|
project_id: null,
|
||||||
|
quantity: 0,
|
||||||
|
notes: "",
|
||||||
|
});
|
||||||
|
|
||||||
|
const [cancelTarget, setCancelTarget] = useState<WarehouseReservation | null>(
|
||||||
|
null,
|
||||||
|
);
|
||||||
|
const [cancelling, setCancelling] = useState(false);
|
||||||
|
|
||||||
|
const { data: projectsData } = useQuery(projectListOptions({ perPage: 100 }));
|
||||||
|
const projects =
|
||||||
|
(projectsData?.data as {
|
||||||
|
id: number;
|
||||||
|
project_number: string;
|
||||||
|
name: string;
|
||||||
|
}[]) ?? [];
|
||||||
|
|
||||||
|
const { items, pagination, isPending, isFetching } =
|
||||||
|
usePaginatedQuery<WarehouseReservation>(
|
||||||
|
warehouseReservationListOptions({
|
||||||
|
page,
|
||||||
|
perPage: PER_PAGE,
|
||||||
|
status: statusFilter || undefined,
|
||||||
|
project_id: projectId ? Number(projectId) : undefined,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!hasPermission("warehouse.view")) return <Forbidden />;
|
||||||
|
|
||||||
|
const canOperate = hasPermission("warehouse.operate");
|
||||||
|
|
||||||
|
const resetFilters = () => {
|
||||||
|
setStatusFilter("");
|
||||||
|
setProjectId("");
|
||||||
|
setPage(1);
|
||||||
|
};
|
||||||
|
|
||||||
|
const hasActiveFilters = statusFilter || projectId;
|
||||||
|
|
||||||
|
const handleCreate = async () => {
|
||||||
|
if (!form.item_id || !form.project_id || form.quantity <= 0) {
|
||||||
|
alert.error("Vyplňte položku, projekt a množství");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setSaving(true);
|
||||||
|
try {
|
||||||
|
const response = await apiFetch("/api/admin/warehouse/reservations", {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({
|
||||||
|
item_id: form.item_id,
|
||||||
|
project_id: form.project_id,
|
||||||
|
quantity: form.quantity,
|
||||||
|
notes: form.notes.trim() || null,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
const result = await response.json();
|
||||||
|
if (result.success) {
|
||||||
|
queryClient.invalidateQueries({
|
||||||
|
queryKey: ["warehouse", "reservations"],
|
||||||
|
});
|
||||||
|
setShowCreateModal(false);
|
||||||
|
setForm({ item_id: null, project_id: null, quantity: 0, notes: "" });
|
||||||
|
alert.success("Rezervace byla vytvořena");
|
||||||
|
} else {
|
||||||
|
alert.error(result.error || "Nepodařilo se vytvořit rezervaci");
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
alert.error("Chyba připojení");
|
||||||
|
} finally {
|
||||||
|
setSaving(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleCancel = async () => {
|
||||||
|
if (!cancelTarget) return;
|
||||||
|
setCancelling(true);
|
||||||
|
try {
|
||||||
|
const response = await apiFetch(
|
||||||
|
`/api/admin/warehouse/reservations/${cancelTarget.id}/cancel`,
|
||||||
|
{ method: "POST" },
|
||||||
|
);
|
||||||
|
const result = await response.json();
|
||||||
|
if (result.success) {
|
||||||
|
queryClient.invalidateQueries({
|
||||||
|
queryKey: ["warehouse", "reservations"],
|
||||||
|
});
|
||||||
|
setCancelTarget(null);
|
||||||
|
alert.success("Rezervace byla zrušena");
|
||||||
|
} else {
|
||||||
|
alert.error(result.error || "Nepodařilo se zrušit rezervaci");
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
alert.error("Chyba připojení");
|
||||||
|
} finally {
|
||||||
|
setCancelling(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (isPending) {
|
||||||
|
return (
|
||||||
|
<div className="admin-loading">
|
||||||
|
<div className="admin-spinner" />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
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">Rezervace</h1>
|
||||||
|
<p className="admin-page-subtitle">
|
||||||
|
{pagination?.total ?? items.length}{" "}
|
||||||
|
{pagination?.total === 1
|
||||||
|
? "rezervace"
|
||||||
|
: pagination?.total !== undefined &&
|
||||||
|
pagination.total >= 2 &&
|
||||||
|
pagination.total <= 4
|
||||||
|
? "rezervace"
|
||||||
|
: "rezervací"}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div className="admin-page-actions">
|
||||||
|
{canOperate && (
|
||||||
|
<button
|
||||||
|
onClick={() => setShowCreateModal(true)}
|
||||||
|
className="admin-btn admin-btn-primary"
|
||||||
|
>
|
||||||
|
<svg
|
||||||
|
width="20"
|
||||||
|
height="20"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth="2"
|
||||||
|
>
|
||||||
|
<line x1="12" y1="5" x2="12" y2="19" />
|
||||||
|
<line x1="5" y1="12" x2="19" y2="12" />
|
||||||
|
</svg>
|
||||||
|
Nová rezervace
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</motion.div>
|
||||||
|
|
||||||
|
<motion.div
|
||||||
|
className="admin-card"
|
||||||
|
initial={{ opacity: 0, y: 12 }}
|
||||||
|
animate={{ opacity: 1, y: 0 }}
|
||||||
|
transition={{ duration: 0.25, delay: 0.06 }}
|
||||||
|
style={{ opacity: isFetching ? 0.6 : 1, transition: "opacity 0.2s" }}
|
||||||
|
>
|
||||||
|
<div className="admin-card-body">
|
||||||
|
<div className="admin-search-bar mb-4">
|
||||||
|
<select
|
||||||
|
value={statusFilter}
|
||||||
|
onChange={(e) => {
|
||||||
|
setStatusFilter(e.target.value);
|
||||||
|
setPage(1);
|
||||||
|
}}
|
||||||
|
className="admin-form-select"
|
||||||
|
style={{ minWidth: 140 }}
|
||||||
|
>
|
||||||
|
<option value="">Všechny stavy</option>
|
||||||
|
<option value="ACTIVE">Aktivní</option>
|
||||||
|
<option value="FULFILLED">Splněna</option>
|
||||||
|
<option value="CANCELLED">Zrušena</option>
|
||||||
|
</select>
|
||||||
|
{projects.length > 0 && (
|
||||||
|
<select
|
||||||
|
value={projectId}
|
||||||
|
onChange={(e) => {
|
||||||
|
setProjectId(
|
||||||
|
e.target.value === "" ? "" : Number(e.target.value),
|
||||||
|
);
|
||||||
|
setPage(1);
|
||||||
|
}}
|
||||||
|
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>
|
||||||
|
)}
|
||||||
|
{hasActiveFilters && (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="admin-btn admin-btn-secondary"
|
||||||
|
onClick={resetFilters}
|
||||||
|
style={{ whiteSpace: "nowrap" }}
|
||||||
|
>
|
||||||
|
Zrušit filtry
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<AnimatePresence mode="wait">
|
||||||
|
<motion.div
|
||||||
|
key={`${statusFilter}-${projectId}-${page}-${items.length}`}
|
||||||
|
initial={{ opacity: 0, y: 6 }}
|
||||||
|
animate={{ opacity: 1, y: 0 }}
|
||||||
|
exit={{ opacity: 0 }}
|
||||||
|
transition={{ duration: 0.15 }}
|
||||||
|
>
|
||||||
|
{items.length === 0 && (
|
||||||
|
<div className="admin-empty-state">
|
||||||
|
<div className="admin-empty-icon">
|
||||||
|
<svg
|
||||||
|
width="28"
|
||||||
|
height="28"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth="1.5"
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
>
|
||||||
|
<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z" />
|
||||||
|
<polyline points="14 2 14 8 20 8" />
|
||||||
|
<line x1="16" y1="13" x2="8" y2="13" />
|
||||||
|
<line x1="16" y1="17" x2="8" y2="17" />
|
||||||
|
</svg>
|
||||||
|
</div>
|
||||||
|
<p>
|
||||||
|
{hasActiveFilters
|
||||||
|
? "Žádné rezervace pro zadaný filtr."
|
||||||
|
: "Zatím nejsou žádné rezervace."}
|
||||||
|
</p>
|
||||||
|
{canOperate && !hasActiveFilters && (
|
||||||
|
<button
|
||||||
|
onClick={() => setShowCreateModal(true)}
|
||||||
|
className="admin-btn admin-btn-primary"
|
||||||
|
>
|
||||||
|
Vytvořit první rezervaci
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{items.length > 0 && (
|
||||||
|
<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">Zbývá</th>
|
||||||
|
<th>Stav</th>
|
||||||
|
<th>Rezervoval</th>
|
||||||
|
<th>Vytvořeno</th>
|
||||||
|
{canOperate && <th></th>}
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{items.map((res) => (
|
||||||
|
<tr key={res.id}>
|
||||||
|
<td className="fw-500">
|
||||||
|
{res.item?.name || `ID: ${res.item_id}`}
|
||||||
|
</td>
|
||||||
|
<td>{res.project?.name || "—"}</td>
|
||||||
|
<td className="admin-mono text-right">
|
||||||
|
{Number(res.quantity)}
|
||||||
|
</td>
|
||||||
|
<td className="admin-mono text-right">
|
||||||
|
{Number(res.remaining_qty)}
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<span
|
||||||
|
className={`admin-badge ${STATUS_BADGE[res.status] ?? ""}`}
|
||||||
|
>
|
||||||
|
{STATUS_LABEL[res.status] ?? res.status}
|
||||||
|
</span>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
{res.reserved_by_user
|
||||||
|
? `${res.reserved_by_user.first_name} ${res.reserved_by_user.last_name}`
|
||||||
|
: "—"}
|
||||||
|
</td>
|
||||||
|
<td className="admin-mono">
|
||||||
|
{formatDate(res.created_at)}
|
||||||
|
</td>
|
||||||
|
{canOperate && (
|
||||||
|
<td>
|
||||||
|
{res.status === "ACTIVE" && (
|
||||||
|
<button
|
||||||
|
onClick={() => setCancelTarget(res)}
|
||||||
|
className="admin-btn-icon danger"
|
||||||
|
title="Zrušit rezervaci"
|
||||||
|
aria-label="Zrušit rezervaci"
|
||||||
|
>
|
||||||
|
<svg
|
||||||
|
width="16"
|
||||||
|
height="16"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth="2"
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
>
|
||||||
|
<circle cx="12" cy="12" r="10" />
|
||||||
|
<line x1="15" y1="9" x2="9" y2="15" />
|
||||||
|
<line x1="9" y1="9" x2="15" y2="15" />
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</td>
|
||||||
|
)}
|
||||||
|
</tr>
|
||||||
|
))}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</motion.div>
|
||||||
|
</AnimatePresence>
|
||||||
|
|
||||||
|
<Pagination pagination={pagination} onPageChange={setPage} />
|
||||||
|
</div>
|
||||||
|
</motion.div>
|
||||||
|
|
||||||
|
{/* Create reservation modal */}
|
||||||
|
<AnimatePresence>
|
||||||
|
{showCreateModal && (
|
||||||
|
<motion.div
|
||||||
|
className="admin-modal-overlay"
|
||||||
|
initial={{ opacity: 0 }}
|
||||||
|
animate={{ opacity: 1 }}
|
||||||
|
exit={{ opacity: 0 }}
|
||||||
|
transition={{ duration: 0.2 }}
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
className="admin-modal-backdrop"
|
||||||
|
onClick={() => setShowCreateModal(false)}
|
||||||
|
/>
|
||||||
|
<motion.div
|
||||||
|
className="admin-modal"
|
||||||
|
role="dialog"
|
||||||
|
aria-modal="true"
|
||||||
|
initial={{ opacity: 0, scale: 0.95, y: 20 }}
|
||||||
|
animate={{ opacity: 1, scale: 1, y: 0 }}
|
||||||
|
exit={{ opacity: 0, scale: 0.95, y: 20 }}
|
||||||
|
transition={{ duration: 0.2 }}
|
||||||
|
>
|
||||||
|
<div className="admin-modal-header">
|
||||||
|
<h2 className="admin-modal-title">Nová rezervace</h2>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="admin-modal-close"
|
||||||
|
onClick={() => setShowCreateModal(false)}
|
||||||
|
aria-label="Zavřít"
|
||||||
|
>
|
||||||
|
×
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div className="admin-modal-body">
|
||||||
|
<div className="admin-form">
|
||||||
|
<FormField label="Položka" required>
|
||||||
|
<ItemPicker
|
||||||
|
value={form.item_id}
|
||||||
|
onChange={(id) =>
|
||||||
|
setForm((prev) => ({ ...prev, item_id: id }))
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</FormField>
|
||||||
|
<FormField label="Projekt" required>
|
||||||
|
<select
|
||||||
|
value={form.project_id ?? ""}
|
||||||
|
onChange={(e) =>
|
||||||
|
setForm((prev) => ({
|
||||||
|
...prev,
|
||||||
|
project_id: e.target.value
|
||||||
|
? Number(e.target.value)
|
||||||
|
: null,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
className="admin-form-select"
|
||||||
|
>
|
||||||
|
<option value="">Vyberte projekt</option>
|
||||||
|
{projects.map((p) => (
|
||||||
|
<option key={p.id} value={p.id}>
|
||||||
|
{p.project_number} – {p.name}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
</FormField>
|
||||||
|
<FormField label="Množství" required>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
value={form.quantity || ""}
|
||||||
|
onChange={(e) =>
|
||||||
|
setForm((prev) => ({
|
||||||
|
...prev,
|
||||||
|
quantity: Number(e.target.value),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
className="admin-form-input"
|
||||||
|
min="0"
|
||||||
|
step="0.001"
|
||||||
|
/>
|
||||||
|
</FormField>
|
||||||
|
<FormField label="Poznámky">
|
||||||
|
<textarea
|
||||||
|
value={form.notes}
|
||||||
|
onChange={(e) =>
|
||||||
|
setForm((prev) => ({ ...prev, notes: e.target.value }))
|
||||||
|
}
|
||||||
|
className="admin-form-input"
|
||||||
|
rows={3}
|
||||||
|
placeholder="Volitelné poznámky"
|
||||||
|
/>
|
||||||
|
</FormField>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="admin-modal-footer">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => setShowCreateModal(false)}
|
||||||
|
className="admin-btn admin-btn-secondary"
|
||||||
|
disabled={saving}
|
||||||
|
>
|
||||||
|
Zrušit
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={handleCreate}
|
||||||
|
className="admin-btn admin-btn-primary"
|
||||||
|
disabled={saving}
|
||||||
|
>
|
||||||
|
{saving ? "Ukládání..." : "Vytvořit rezervaci"}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</motion.div>
|
||||||
|
</motion.div>
|
||||||
|
)}
|
||||||
|
</AnimatePresence>
|
||||||
|
|
||||||
|
{/* Cancel confirmation modal */}
|
||||||
|
<ConfirmModal
|
||||||
|
isOpen={!!cancelTarget}
|
||||||
|
onClose={() => setCancelTarget(null)}
|
||||||
|
onConfirm={handleCancel}
|
||||||
|
title="Zrušit rezervaci"
|
||||||
|
message={`Opravdu chcete zrušit rezervaci pro položku „${cancelTarget?.item?.name ?? cancelTarget?.item_id}"?`}
|
||||||
|
confirmText="Zrušit rezervaci"
|
||||||
|
confirmVariant="danger"
|
||||||
|
loading={cancelling}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user