feat(warehouse): add issue pages
This commit is contained in:
363
src/admin/pages/WarehouseIssueDetail.tsx
Normal file
363
src/admin/pages/WarehouseIssueDetail.tsx
Normal file
@@ -0,0 +1,363 @@
|
||||
import { useState } from "react";
|
||||
import { useParams, 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 ConfirmModal from "../components/ConfirmModal";
|
||||
import FormField from "../components/FormField";
|
||||
|
||||
import apiFetch from "../utils/api";
|
||||
import { formatCurrency, formatDate } from "../utils/formatters";
|
||||
import {
|
||||
warehouseIssueDetailOptions,
|
||||
type WarehouseIssue,
|
||||
} from "../lib/queries/warehouse";
|
||||
|
||||
const STATUS_BADGE: Record<string, string> = {
|
||||
DRAFT: "admin-badge-warning",
|
||||
CONFIRMED: "admin-badge-active",
|
||||
CANCELLED: "admin-badge-danger",
|
||||
};
|
||||
|
||||
const STATUS_LABEL: Record<string, string> = {
|
||||
DRAFT: "Návrh",
|
||||
CONFIRMED: "Potvrzeno",
|
||||
CANCELLED: "Zrušeno",
|
||||
};
|
||||
|
||||
export default function WarehouseIssueDetail() {
|
||||
const { id } = useParams();
|
||||
const alert = useAlert();
|
||||
const { hasPermission } = useAuth();
|
||||
const navigate = useNavigate();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const [cancelConfirm, setCancelConfirm] = useState(false);
|
||||
const [confirming, setConfirming] = useState(false);
|
||||
const [cancelling, setCancelling] = useState(false);
|
||||
|
||||
const {
|
||||
data: issue,
|
||||
isPending,
|
||||
error,
|
||||
} = useQuery(warehouseIssueDetailOptions(id));
|
||||
|
||||
if (!hasPermission("warehouse.view")) return <Forbidden />;
|
||||
|
||||
const canOperate = hasPermission("warehouse.operate");
|
||||
|
||||
const handleConfirm = async () => {
|
||||
if (!issue) return;
|
||||
setConfirming(true);
|
||||
try {
|
||||
const response = await apiFetch(
|
||||
`/api/admin/warehouse/issues/${issue.id}/confirm`,
|
||||
{ method: "POST" },
|
||||
);
|
||||
const result = await response.json();
|
||||
if (result.success) {
|
||||
queryClient.invalidateQueries({ queryKey: ["warehouse", "issues"] });
|
||||
alert.success("Výdejka byla potvrzena");
|
||||
} else {
|
||||
alert.error(result.error || "Nepodařilo se potvrdit výdejku");
|
||||
}
|
||||
} catch {
|
||||
alert.error("Chyba připojení");
|
||||
} finally {
|
||||
setConfirming(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleCancel = async () => {
|
||||
if (!issue) return;
|
||||
setCancelling(true);
|
||||
try {
|
||||
const response = await apiFetch(
|
||||
`/api/admin/warehouse/issues/${issue.id}/cancel`,
|
||||
{ method: "POST" },
|
||||
);
|
||||
const result = await response.json();
|
||||
if (result.success) {
|
||||
queryClient.invalidateQueries({ queryKey: ["warehouse", "issues"] });
|
||||
setCancelConfirm(false);
|
||||
alert.success("Výdejka byla zrušena");
|
||||
} else {
|
||||
alert.error(result.error || "Nepodařilo se zrušit výdejku");
|
||||
}
|
||||
} catch {
|
||||
alert.error("Chyba připojení");
|
||||
} finally {
|
||||
setCancelling(false);
|
||||
}
|
||||
};
|
||||
|
||||
if (isPending) {
|
||||
return (
|
||||
<div className="admin-loading">
|
||||
<div className="admin-spinner" />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (error || !issue) {
|
||||
return (
|
||||
<div>
|
||||
<div className="admin-page-header">
|
||||
<div>
|
||||
<Link
|
||||
to="/warehouse/issues"
|
||||
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>Výdejka nebyla nalezena.</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const iss = issue as WarehouseIssue;
|
||||
const lines = iss.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/issues"
|
||||
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">
|
||||
{iss.issue_number || "Nová výdejka"}
|
||||
</h1>
|
||||
{iss.project && (
|
||||
<p className="admin-page-subtitle">
|
||||
{iss.project.project_number} – {iss.project.name}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
<span
|
||||
className={`admin-badge ${STATUS_BADGE[iss.status] ?? ""}`}
|
||||
style={{ marginLeft: "0.5rem" }}
|
||||
>
|
||||
{STATUS_LABEL[iss.status] ?? iss.status}
|
||||
</span>
|
||||
</div>
|
||||
{canOperate && (
|
||||
<div className="admin-page-actions">
|
||||
{iss.status === "DRAFT" && (
|
||||
<>
|
||||
<button
|
||||
onClick={() => navigate(`/warehouse/issues/${iss.id}/edit`)}
|
||||
className="admin-btn admin-btn-secondary"
|
||||
>
|
||||
Upravit
|
||||
</button>
|
||||
<button
|
||||
onClick={handleConfirm}
|
||||
className="admin-btn admin-btn-primary"
|
||||
disabled={confirming}
|
||||
>
|
||||
{confirming ? "Potvrzování..." : "Potvrdit"}
|
||||
</button>
|
||||
<button
|
||||
onClick={() => setCancelConfirm(true)}
|
||||
className="admin-btn admin-btn-secondary"
|
||||
style={{ color: "var(--danger)" }}
|
||||
>
|
||||
Zrušit
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
{iss.status === "CONFIRMED" && (
|
||||
<button
|
||||
onClick={() => setCancelConfirm(true)}
|
||||
className="admin-btn admin-btn-secondary"
|
||||
style={{ color: "var(--danger)" }}
|
||||
>
|
||||
Zrušit
|
||||
</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 dokladu">
|
||||
<div className="admin-mono" style={{ fontWeight: 500 }}>
|
||||
{iss.issue_number || "—"}
|
||||
</div>
|
||||
</FormField>
|
||||
<FormField label="Projekt">
|
||||
<div style={{ fontWeight: 500 }}>
|
||||
{iss.project
|
||||
? `${iss.project.project_number} – ${iss.project.name}`
|
||||
: "—"}
|
||||
</div>
|
||||
</FormField>
|
||||
</div>
|
||||
<div className="admin-form-row">
|
||||
<FormField label="Vydal">
|
||||
<div>
|
||||
{iss.issued_by_user
|
||||
? `${iss.issued_by_user.first_name} ${iss.issued_by_user.last_name}`
|
||||
: "—"}
|
||||
</div>
|
||||
</FormField>
|
||||
<FormField label="Vytvořeno">
|
||||
<div className="admin-mono">{formatDate(iss.created_at)}</div>
|
||||
</FormField>
|
||||
</div>
|
||||
{iss.notes && (
|
||||
<FormField label="Poznámky">
|
||||
<div style={{ whiteSpace: "pre-wrap" }}>{iss.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 výdejky</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>Šarže</th>
|
||||
<th className="text-right">Množství</th>
|
||||
<th className="text-right">Cena/ks</th>
|
||||
<th className="text-right">Celkem</th>
|
||||
<th>Lokace</th>
|
||||
<th>Rezervace</th>
|
||||
<th>Poznámka</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{lines.map((line) => (
|
||||
<tr key={line.id}>
|
||||
<td className="fw-500">
|
||||
{line.item?.name || `ID: ${line.item_id}`}
|
||||
</td>
|
||||
<td className="admin-mono">
|
||||
{line.batch
|
||||
? `${formatDate(line.batch.received_at)} – ${formatCurrency(Number(line.batch.unit_price), "CZK")}`
|
||||
: "—"}
|
||||
</td>
|
||||
<td className="admin-mono text-right">
|
||||
{Number(line.quantity)}
|
||||
</td>
|
||||
<td className="admin-mono text-right">
|
||||
{line.batch
|
||||
? formatCurrency(Number(line.batch.unit_price), "CZK")
|
||||
: "—"}
|
||||
</td>
|
||||
<td className="admin-mono text-right fw-500">
|
||||
{line.batch
|
||||
? formatCurrency(
|
||||
Number(line.quantity) *
|
||||
Number(line.batch.unit_price),
|
||||
"CZK",
|
||||
)
|
||||
: "—"}
|
||||
</td>
|
||||
<td className="admin-mono">
|
||||
{line.location?.code || "—"}
|
||||
</td>
|
||||
<td className="admin-mono">
|
||||
{line.reservation
|
||||
? `ID: ${line.reservation.id} (${Number(line.reservation.remaining_qty)} ks)`
|
||||
: "—"}
|
||||
</td>
|
||||
<td>{line.notes || "—"}</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</motion.div>
|
||||
|
||||
{/* Cancel confirmation modal */}
|
||||
<ConfirmModal
|
||||
isOpen={cancelConfirm}
|
||||
onClose={() => setCancelConfirm(false)}
|
||||
onConfirm={handleCancel}
|
||||
title="Zrušit výdejku"
|
||||
message={`Opravdu chcete zrušit výdejku „${iss.issue_number || iss.id}"?`}
|
||||
confirmText="Zrušit výdejku"
|
||||
confirmVariant="danger"
|
||||
loading={cancelling}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
363
src/admin/pages/WarehouseIssueForm.tsx
Normal file
363
src/admin/pages/WarehouseIssueForm.tsx
Normal file
@@ -0,0 +1,363 @@
|
||||
import { useState, useEffect, useRef } from "react";
|
||||
import { useParams, 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 WarehouseMovementTable, {
|
||||
type MovementLine,
|
||||
} from "../components/warehouse/WarehouseMovementTable";
|
||||
import { warehouseLocationListOptions } from "../lib/queries/warehouse";
|
||||
|
||||
import apiFetch from "../utils/api";
|
||||
import {
|
||||
warehouseIssueDetailOptions,
|
||||
type WarehouseLocation,
|
||||
} from "../lib/queries/warehouse";
|
||||
import { projectListOptions } from "../lib/queries/projects";
|
||||
|
||||
interface IssueForm {
|
||||
project_id: number | null;
|
||||
notes: string;
|
||||
}
|
||||
|
||||
let lineCounter = 0;
|
||||
|
||||
export default function WarehouseIssueForm() {
|
||||
const { id } = useParams();
|
||||
const alert = useAlert();
|
||||
const { hasPermission } = useAuth();
|
||||
const navigate = useNavigate();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const isEdit = id !== undefined && id !== "new";
|
||||
|
||||
const [form, setForm] = useState<IssueForm>({
|
||||
project_id: null,
|
||||
notes: "",
|
||||
});
|
||||
const [lines, setLines] = useState<MovementLine[]>([]);
|
||||
const [errors, setErrors] = useState<Record<string, string>>({});
|
||||
const [saving, setSaving] = useState(false);
|
||||
|
||||
const formInitialized = useRef(false);
|
||||
|
||||
const { data: issue, isPending } = useQuery(
|
||||
warehouseIssueDetailOptions(isEdit ? id : undefined),
|
||||
);
|
||||
|
||||
const { data: locations = [] } = useQuery(warehouseLocationListOptions());
|
||||
|
||||
const { data: projectsData } = useQuery(projectListOptions({ perPage: 100 }));
|
||||
const projects = projectsData?.data ?? [];
|
||||
|
||||
useEffect(() => {
|
||||
formInitialized.current = false;
|
||||
}, [id]);
|
||||
|
||||
useEffect(() => {
|
||||
if (isEdit && issue && !formInitialized.current) {
|
||||
setForm({
|
||||
project_id: issue.project_id,
|
||||
notes: issue.notes || "",
|
||||
});
|
||||
if (issue.lines && issue.lines.length > 0) {
|
||||
setLines(
|
||||
issue.lines.map((line) => ({
|
||||
key: `line-${++lineCounter}`,
|
||||
item_id: line.item_id,
|
||||
item_name: line.item?.name,
|
||||
quantity: Number(line.quantity),
|
||||
unit_price: line.batch ? Number(line.batch.unit_price) : 0,
|
||||
location_id: line.location_id,
|
||||
batch_id: line.batch_id,
|
||||
reservation_id: line.reservation_id,
|
||||
notes: line.notes,
|
||||
})),
|
||||
);
|
||||
}
|
||||
formInitialized.current = true;
|
||||
}
|
||||
}, [isEdit, issue]);
|
||||
|
||||
if (!hasPermission("warehouse.operate")) return <Forbidden />;
|
||||
|
||||
const addEmptyLine = () => {
|
||||
setLines((prev) => [
|
||||
...prev,
|
||||
{
|
||||
key: `line-${++lineCounter}`,
|
||||
item_id: null,
|
||||
quantity: 0,
|
||||
unit_price: 0,
|
||||
location_id: null,
|
||||
batch_id: null,
|
||||
reservation_id: null,
|
||||
notes: null,
|
||||
},
|
||||
]);
|
||||
};
|
||||
|
||||
// Start with one empty line in create mode
|
||||
useEffect(() => {
|
||||
if (!isEdit && lines.length === 0) {
|
||||
addEmptyLine();
|
||||
}
|
||||
}, []); // eslint-disable-line react-hooks/exhaustive-deps
|
||||
|
||||
const validate = (): boolean => {
|
||||
const newErrors: Record<string, string> = {};
|
||||
if (!form.project_id) {
|
||||
newErrors.project_id = "Projekt je povinný";
|
||||
}
|
||||
const validLines = lines.filter((l) => l.item_id !== null);
|
||||
if (validLines.length === 0) {
|
||||
newErrors.lines = "Přidejte alespoň jednu položku";
|
||||
}
|
||||
for (const line of validLines) {
|
||||
if (line.quantity <= 0) {
|
||||
newErrors.lines = "Množství musí být větší než 0";
|
||||
break;
|
||||
}
|
||||
}
|
||||
setErrors(newErrors);
|
||||
return Object.keys(newErrors).length === 0;
|
||||
};
|
||||
|
||||
const buildPayload = () => ({
|
||||
project_id: form.project_id,
|
||||
notes: form.notes.trim() || null,
|
||||
lines: lines
|
||||
.filter((l) => l.item_id !== null)
|
||||
.map((l) => ({
|
||||
item_id: l.item_id!,
|
||||
batch_id: l.batch_id,
|
||||
quantity: l.quantity,
|
||||
location_id: l.location_id,
|
||||
reservation_id: l.reservation_id,
|
||||
notes: l.notes?.trim() || null,
|
||||
})),
|
||||
});
|
||||
|
||||
const saveIssue = async (): Promise<number | null> => {
|
||||
const url = isEdit
|
||||
? `/api/admin/warehouse/issues/${id}`
|
||||
: "/api/admin/warehouse/issues";
|
||||
const method = isEdit ? "PUT" : "POST";
|
||||
|
||||
try {
|
||||
const response = await apiFetch(url, {
|
||||
method,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(buildPayload()),
|
||||
});
|
||||
const result = await response.json();
|
||||
if (result.success) {
|
||||
queryClient.invalidateQueries({ queryKey: ["warehouse", "issues"] });
|
||||
return result.data?.id ?? Number(id);
|
||||
} else {
|
||||
alert.error(result.error || "Nepodařilo se uložit výdejku");
|
||||
return null;
|
||||
}
|
||||
} catch {
|
||||
alert.error("Chyba připojení");
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
const confirmIssue = async (issueId: number) => {
|
||||
try {
|
||||
const response = await apiFetch(
|
||||
`/api/admin/warehouse/issues/${issueId}/confirm`,
|
||||
{ method: "POST" },
|
||||
);
|
||||
const result = await response.json();
|
||||
if (result.success) {
|
||||
queryClient.invalidateQueries({ queryKey: ["warehouse", "issues"] });
|
||||
alert.success("Výdejka byla potvrzena");
|
||||
} else {
|
||||
alert.error(result.error || "Nepodařilo se potvrdit výdejku");
|
||||
}
|
||||
} catch {
|
||||
alert.error("Chyba připojení");
|
||||
}
|
||||
};
|
||||
|
||||
const handleSaveDraft = async () => {
|
||||
if (!validate()) return;
|
||||
setSaving(true);
|
||||
try {
|
||||
const savedId = await saveIssue();
|
||||
if (savedId) {
|
||||
alert.success("Výdejka byla uložena jako návrh");
|
||||
navigate(`/warehouse/issues/${savedId}`);
|
||||
}
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleSaveAndConfirm = async () => {
|
||||
if (!validate()) return;
|
||||
setSaving(true);
|
||||
try {
|
||||
const savedId = await saveIssue();
|
||||
if (savedId) {
|
||||
await confirmIssue(savedId);
|
||||
navigate(`/warehouse/issues/${savedId}`);
|
||||
}
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
};
|
||||
|
||||
if (isEdit && isPending) {
|
||||
return (
|
||||
<div className="admin-loading">
|
||||
<div className="admin-spinner" />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
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/issues"
|
||||
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">
|
||||
{isEdit ? "Upravit výdejku" : "Nová výdejka"}
|
||||
</h1>
|
||||
</div>
|
||||
</div>
|
||||
<div className="admin-page-actions">
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleSaveDraft}
|
||||
className="admin-btn admin-btn-secondary"
|
||||
disabled={saving}
|
||||
>
|
||||
{saving ? "Ukládání..." : "Uložit jako návrh"}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleSaveAndConfirm}
|
||||
className="admin-btn admin-btn-primary"
|
||||
disabled={saving}
|
||||
>
|
||||
{saving ? "Ukládání..." : "Uložit a potvrdit"}
|
||||
</button>
|
||||
</div>
|
||||
</motion.div>
|
||||
|
||||
{/* Header fields */}
|
||||
<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">
|
||||
<FormField label="Projekt">
|
||||
<select
|
||||
value={form.project_id ?? ""}
|
||||
onChange={(e) =>
|
||||
setForm((prev) => ({
|
||||
...prev,
|
||||
project_id:
|
||||
e.target.value === "" ? null : Number(e.target.value),
|
||||
}))
|
||||
}
|
||||
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>
|
||||
{errors.project_id && (
|
||||
<div
|
||||
style={{
|
||||
color: "var(--danger)",
|
||||
fontSize: "0.875rem",
|
||||
marginTop: "0.25rem",
|
||||
}}
|
||||
>
|
||||
{errors.project_id}
|
||||
</div>
|
||||
)}
|
||||
</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>
|
||||
</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 výdejky</h3>
|
||||
{errors.lines && (
|
||||
<div
|
||||
style={{
|
||||
color: "var(--danger)",
|
||||
fontSize: "0.875rem",
|
||||
marginBottom: "0.75rem",
|
||||
}}
|
||||
>
|
||||
{errors.lines}
|
||||
</div>
|
||||
)}
|
||||
<WarehouseMovementTable
|
||||
lines={lines}
|
||||
onChange={setLines}
|
||||
mode="issue"
|
||||
locations={locations as WarehouseLocation[]}
|
||||
/>
|
||||
</div>
|
||||
</motion.div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
337
src/admin/pages/WarehouseIssues.tsx
Normal file
337
src/admin/pages/WarehouseIssues.tsx
Normal file
@@ -0,0 +1,337 @@
|
||||
import { useState } from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { useQuery } 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 useDebounce from "../hooks/useDebounce";
|
||||
import { usePaginatedQuery } from "../hooks/usePaginatedQuery";
|
||||
|
||||
import { formatDate } from "../utils/formatters";
|
||||
import {
|
||||
warehouseIssueListOptions,
|
||||
type WarehouseIssue,
|
||||
} from "../lib/queries/warehouse";
|
||||
import { projectListOptions } from "../lib/queries/projects";
|
||||
|
||||
const PER_PAGE = 20;
|
||||
|
||||
const STATUS_BADGE: Record<string, string> = {
|
||||
DRAFT: "admin-badge-warning",
|
||||
CONFIRMED: "admin-badge-active",
|
||||
CANCELLED: "admin-badge-danger",
|
||||
};
|
||||
|
||||
const STATUS_LABEL: Record<string, string> = {
|
||||
DRAFT: "Návrh",
|
||||
CONFIRMED: "Potvrzeno",
|
||||
CANCELLED: "Zrušeno",
|
||||
};
|
||||
|
||||
export default function WarehouseIssues() {
|
||||
const { hasPermission } = useAuth();
|
||||
|
||||
const [page, setPage] = useState(1);
|
||||
const [search, setSearch] = useState("");
|
||||
const [statusFilter, setStatusFilter] = useState<string>("");
|
||||
const [projectId, setProjectId] = useState<number | "">("");
|
||||
const [dateFrom, setDateFrom] = useState("");
|
||||
const [dateTo, setDateTo] = useState("");
|
||||
const debouncedSearch = useDebounce(search, 300);
|
||||
|
||||
const { data: projectsData } = useQuery(projectListOptions({ perPage: 100 }));
|
||||
const projects = projectsData?.data ?? [];
|
||||
|
||||
const { items, pagination, isPending, isFetching } =
|
||||
usePaginatedQuery<WarehouseIssue>(
|
||||
warehouseIssueListOptions({
|
||||
search: debouncedSearch || undefined,
|
||||
page,
|
||||
perPage: PER_PAGE,
|
||||
status: statusFilter || undefined,
|
||||
project_id: projectId ? Number(projectId) : undefined,
|
||||
date_from: dateFrom || undefined,
|
||||
date_to: dateTo || undefined,
|
||||
}),
|
||||
);
|
||||
|
||||
const navigate = useNavigate();
|
||||
|
||||
if (!hasPermission("warehouse.view")) return <Forbidden />;
|
||||
|
||||
const canOperate = hasPermission("warehouse.operate");
|
||||
|
||||
const handleRowClick = (issue: WarehouseIssue) => {
|
||||
navigate(`/warehouse/issues/${issue.id}`);
|
||||
};
|
||||
|
||||
const resetFilters = () => {
|
||||
setSearch("");
|
||||
setStatusFilter("");
|
||||
setProjectId("");
|
||||
setDateFrom("");
|
||||
setDateTo("");
|
||||
setPage(1);
|
||||
};
|
||||
|
||||
const hasActiveFilters = statusFilter || projectId || dateFrom || dateTo;
|
||||
|
||||
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">Výdejky</h1>
|
||||
<p className="admin-page-subtitle">
|
||||
{pagination?.total ?? items.length}{" "}
|
||||
{pagination?.total === 1
|
||||
? "doklad"
|
||||
: pagination?.total !== undefined &&
|
||||
pagination.total >= 2 &&
|
||||
pagination.total <= 4
|
||||
? "doklady"
|
||||
: "dokladů"}
|
||||
</p>
|
||||
</div>
|
||||
<div className="admin-page-actions">
|
||||
{canOperate && (
|
||||
<button
|
||||
onClick={() => navigate("/warehouse/issues/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á výdejka
|
||||
</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">
|
||||
<div className="admin-search">
|
||||
<svg
|
||||
width="16"
|
||||
height="16"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="2"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
>
|
||||
<circle cx="11" cy="11" r="8" />
|
||||
<line x1="21" y1="21" x2="16.65" y2="16.65" />
|
||||
</svg>
|
||||
<input
|
||||
type="text"
|
||||
value={search}
|
||||
onChange={(e) => {
|
||||
setSearch(e.target.value);
|
||||
setPage(1);
|
||||
}}
|
||||
placeholder="Hledat podle čísla dokladu..."
|
||||
className="admin-form-input"
|
||||
/>
|
||||
</div>
|
||||
<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>
|
||||
<option value="CANCELLED">Zrušeno</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>
|
||||
)}
|
||||
<input
|
||||
type="date"
|
||||
value={dateFrom}
|
||||
onChange={(e) => {
|
||||
setDateFrom(e.target.value);
|
||||
setPage(1);
|
||||
}}
|
||||
className="admin-form-input"
|
||||
style={{ width: 140 }}
|
||||
placeholder="Od"
|
||||
/>
|
||||
<input
|
||||
type="date"
|
||||
value={dateTo}
|
||||
onChange={(e) => {
|
||||
setDateTo(e.target.value);
|
||||
setPage(1);
|
||||
}}
|
||||
className="admin-form-input"
|
||||
style={{ width: 140 }}
|
||||
placeholder="Do"
|
||||
/>
|
||||
{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={`${debouncedSearch}-${statusFilter}-${projectId}-${dateFrom}-${dateTo}-${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>
|
||||
{search || hasActiveFilters
|
||||
? "Žádné doklady pro zadaný filtr."
|
||||
: "Zatím nejsou žádné výdejky."}
|
||||
</p>
|
||||
{canOperate && !search && !hasActiveFilters && (
|
||||
<button
|
||||
onClick={() => navigate("/warehouse/issues/new")}
|
||||
className="admin-btn admin-btn-primary"
|
||||
>
|
||||
Vytvořit první výdejku
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
{items.length > 0 && (
|
||||
<div className="admin-table-responsive">
|
||||
<table className="admin-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Číslo dokladu</th>
|
||||
<th>Projekt</th>
|
||||
<th>Stav</th>
|
||||
<th>Vydal</th>
|
||||
<th>Vytvořeno</th>
|
||||
<th className="text-right">Řádků</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{items.map((issue) => (
|
||||
<tr
|
||||
key={issue.id}
|
||||
onClick={() => handleRowClick(issue)}
|
||||
style={{ cursor: "pointer" }}
|
||||
>
|
||||
<td className="admin-mono fw-500">
|
||||
{issue.issue_number || "—"}
|
||||
</td>
|
||||
<td>
|
||||
{issue.project
|
||||
? `${issue.project.project_number} – ${issue.project.name}`
|
||||
: "—"}
|
||||
</td>
|
||||
<td>
|
||||
<span
|
||||
className={`admin-badge ${STATUS_BADGE[issue.status] ?? ""}`}
|
||||
>
|
||||
{STATUS_LABEL[issue.status] ?? issue.status}
|
||||
</span>
|
||||
</td>
|
||||
<td>
|
||||
{issue.issued_by_user
|
||||
? `${issue.issued_by_user.first_name} ${issue.issued_by_user.last_name}`
|
||||
: "—"}
|
||||
</td>
|
||||
<td className="admin-mono">
|
||||
{formatDate(issue.created_at)}
|
||||
</td>
|
||||
<td className="admin-mono text-right">
|
||||
{issue.lines?.length ?? 0}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
</motion.div>
|
||||
</AnimatePresence>
|
||||
|
||||
<Pagination pagination={pagination} onPageChange={setPage} />
|
||||
</div>
|
||||
</motion.div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user