v1.8.0: warehouse module (16 commits), docházka mzda model, leave_type=holiday removal, api.ts race fix
Highlights: - Warehouse module: receipts, issues, reservations, inventory, reports, dashboard, master data (categories, suppliers, locations), FIFO service, integration tests - Docházka: mzda PDF counting model (Odpracováno / Vč. svátků / Přesčas / Svátek / So/Ne / Noc) with Czech weekday names and decimal hours - AttendanceAdmin/AttendanceHistory KPI cards unified to mzda formula with fund bar colored by delta, badges for Práce/Dov/Nem/Sv/Nep - Remove leave_type=holiday entirely (auto-computed from Czech public holidays) - Allow multiple work shifts per day (overlap detection only) - Pre-flight refresh in api.ts eliminates spurious 401s on fresh page loads - Prisma: company_settings gets 6 nullable columns for warehouse numbering (PRI/VYD/INV prefixes, default patterns); migration seeds defaults
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { useState, useEffect, useRef } from "react";
|
||||
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { useParams, useNavigate, Link } from "react-router-dom";
|
||||
import { useAlert } from "../context/AlertContext";
|
||||
import { useAuth } from "../context/AuthContext";
|
||||
@@ -8,13 +8,13 @@ import { motion } from "framer-motion";
|
||||
import FormField from "../components/FormField";
|
||||
import useModalLock from "../hooks/useModalLock";
|
||||
|
||||
import apiFetch from "../utils/api";
|
||||
import { formatCurrency, formatDate } from "../utils/formatters";
|
||||
import {
|
||||
warehouseItemDetailOptions,
|
||||
warehouseCategoryListOptions,
|
||||
type WarehouseItem,
|
||||
} from "../lib/queries/warehouse";
|
||||
import { useApiMutation } from "../lib/queries/mutations";
|
||||
|
||||
interface Batch {
|
||||
id: number;
|
||||
@@ -50,7 +50,6 @@ interface ItemForm {
|
||||
unit: string;
|
||||
min_quantity: string;
|
||||
notes: string;
|
||||
is_active: boolean;
|
||||
}
|
||||
|
||||
export default function WarehouseItemDetail() {
|
||||
@@ -58,10 +57,8 @@ export default function WarehouseItemDetail() {
|
||||
const alert = useAlert();
|
||||
const { hasPermission } = useAuth();
|
||||
const navigate = useNavigate();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const [editing, setEditing] = useState(false);
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [form, setForm] = useState<ItemForm>({
|
||||
item_number: "",
|
||||
name: "",
|
||||
@@ -70,11 +67,11 @@ export default function WarehouseItemDetail() {
|
||||
unit: "ks",
|
||||
min_quantity: "",
|
||||
notes: "",
|
||||
is_active: true,
|
||||
});
|
||||
const [errors, setErrors] = useState<Record<string, string>>({});
|
||||
|
||||
const itemQuery = useQuery(warehouseItemDetailOptions(id));
|
||||
const isNew = id === "new";
|
||||
const itemQuery = useQuery(warehouseItemDetailOptions(id, !!id && !isNew));
|
||||
const item = itemQuery.data as ItemDetail | undefined;
|
||||
const isPending = itemQuery.isPending;
|
||||
|
||||
@@ -96,14 +93,13 @@ export default function WarehouseItemDetail() {
|
||||
min_quantity:
|
||||
item.min_quantity != null ? String(item.min_quantity) : "",
|
||||
notes: item.notes || "",
|
||||
is_active: item.is_active,
|
||||
});
|
||||
formInitialized.current = true;
|
||||
}
|
||||
}, [item]);
|
||||
|
||||
useEffect(() => {
|
||||
if (itemQuery.error) {
|
||||
if (itemQuery.error && !isNew) {
|
||||
alert.error("Položka nenalezena");
|
||||
navigate("/warehouse/items");
|
||||
}
|
||||
@@ -118,6 +114,32 @@ export default function WarehouseItemDetail() {
|
||||
const updateForm = (field: keyof ItemForm, value: string | boolean) =>
|
||||
setForm((prev) => ({ ...prev, [field]: value }));
|
||||
|
||||
const saveMutation = useApiMutation<
|
||||
Record<string, unknown>,
|
||||
{ id?: number; message?: string }
|
||||
>({
|
||||
url: () => (isNew ? API_BASE : `${API_BASE}/${id}`),
|
||||
method: () => (isNew ? "POST" : "PUT"),
|
||||
invalidate: ["warehouse"],
|
||||
onSuccess: (data) => {
|
||||
setEditing(false);
|
||||
alert.success(data?.message || "Položka byla uložena");
|
||||
if (isNew && data?.id) {
|
||||
navigate(`/warehouse/items/${data.id}`, { replace: true });
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
const deleteMutation = useApiMutation<void, void>({
|
||||
url: () => `${API_BASE}/${id ?? ""}`,
|
||||
method: () => "DELETE",
|
||||
invalidate: ["warehouse"],
|
||||
onSuccess: () => {
|
||||
navigate("/warehouse/items");
|
||||
setTimeout(() => alert.success("Položka byla deaktivována"), 300);
|
||||
},
|
||||
});
|
||||
|
||||
const handleSave = async () => {
|
||||
const newErrors: Record<string, string> = {};
|
||||
if (!form.name.trim()) newErrors.name = "Zadejte název položky";
|
||||
@@ -125,93 +147,35 @@ export default function WarehouseItemDetail() {
|
||||
setErrors(newErrors);
|
||||
if (Object.keys(newErrors).length > 0) return;
|
||||
|
||||
setSaving(true);
|
||||
const payload: Record<string, unknown> = {
|
||||
item_number: form.item_number.trim() || null,
|
||||
name: form.name.trim(),
|
||||
description: form.description.trim() || null,
|
||||
category_id: form.category_id ? Number(form.category_id) : null,
|
||||
unit: form.unit.trim(),
|
||||
min_quantity: form.min_quantity ? Number(form.min_quantity) : null,
|
||||
notes: form.notes.trim() || null,
|
||||
};
|
||||
|
||||
try {
|
||||
const isNew = id === "new";
|
||||
const url = isNew ? API_BASE : `${API_BASE}/${id}`;
|
||||
const method = isNew ? "POST" : "PUT";
|
||||
|
||||
const payload: Record<string, unknown> = {
|
||||
item_number: form.item_number.trim() || null,
|
||||
name: form.name.trim(),
|
||||
description: form.description.trim() || null,
|
||||
category_id: form.category_id ? Number(form.category_id) : null,
|
||||
unit: form.unit.trim(),
|
||||
min_quantity: form.min_quantity ? Number(form.min_quantity) : null,
|
||||
notes: form.notes.trim() || null,
|
||||
is_active: form.is_active,
|
||||
};
|
||||
|
||||
const response = await apiFetch(url, {
|
||||
method,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
|
||||
const result = await response.json();
|
||||
|
||||
if (result.success) {
|
||||
setEditing(false);
|
||||
queryClient.invalidateQueries({ queryKey: ["warehouse"] });
|
||||
alert.success(result.message || "Položka byla uložena");
|
||||
|
||||
if (isNew && result.data?.id) {
|
||||
navigate(`/warehouse/items/${result.data.id}`, { replace: true });
|
||||
}
|
||||
} else {
|
||||
alert.error(result.error || "Nepodařilo se uložit položku");
|
||||
}
|
||||
} catch {
|
||||
alert.error("Chyba připojení");
|
||||
} finally {
|
||||
setSaving(false);
|
||||
await saveMutation.mutateAsync(payload);
|
||||
} catch (e) {
|
||||
alert.error(e instanceof Error ? e.message : "Chyba připojení");
|
||||
}
|
||||
};
|
||||
|
||||
const handleDelete = async () => {
|
||||
if (!item) return;
|
||||
try {
|
||||
const response = await apiFetch(`${API_BASE}/${id}`, {
|
||||
method: "DELETE",
|
||||
});
|
||||
const result = await response.json();
|
||||
if (result.success) {
|
||||
queryClient.invalidateQueries({ queryKey: ["warehouse"] });
|
||||
navigate("/warehouse/items");
|
||||
setTimeout(() => alert.success("Položka byla smazána"), 300);
|
||||
} else {
|
||||
alert.error(result.error || "Nepodařilo se smazat položku");
|
||||
}
|
||||
} catch {
|
||||
alert.error("Chyba připojení");
|
||||
await deleteMutation.mutateAsync(undefined);
|
||||
} catch (e) {
|
||||
alert.error(e instanceof Error ? e.message : "Chyba připojení");
|
||||
}
|
||||
};
|
||||
|
||||
const handleToggleActive = async () => {
|
||||
if (!item) return;
|
||||
try {
|
||||
const response = await apiFetch(`${API_BASE}/${id}`, {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ is_active: !item.is_active }),
|
||||
});
|
||||
const result = await response.json();
|
||||
if (result.success) {
|
||||
queryClient.invalidateQueries({ queryKey: ["warehouse"] });
|
||||
alert.success(
|
||||
item.is_active
|
||||
? "Položka byla deaktivována"
|
||||
: "Položka byla aktivována",
|
||||
);
|
||||
} else {
|
||||
alert.error(result.error || "Nepodařilo se změnit stav");
|
||||
}
|
||||
} catch {
|
||||
alert.error("Chyba připojení");
|
||||
}
|
||||
};
|
||||
const saving = saveMutation.isPending;
|
||||
|
||||
if (isPending && id !== "new") {
|
||||
if (isPending && !isNew) {
|
||||
return (
|
||||
<div className="admin-loading">
|
||||
<div className="admin-spinner" />
|
||||
@@ -264,35 +228,36 @@ export default function WarehouseItemDetail() {
|
||||
</div>
|
||||
{canManage && (
|
||||
<div className="admin-page-actions">
|
||||
{editing ? (
|
||||
{editing || isNew ? (
|
||||
<>
|
||||
<button
|
||||
onClick={() => {
|
||||
setEditing(false);
|
||||
setErrors({});
|
||||
if (item) {
|
||||
setForm({
|
||||
item_number: item.item_number || "",
|
||||
name: item.name,
|
||||
description: item.description || "",
|
||||
category_id: item.category_id
|
||||
? String(item.category_id)
|
||||
: "",
|
||||
unit: item.unit,
|
||||
min_quantity:
|
||||
item.min_quantity != null
|
||||
? String(item.min_quantity)
|
||||
{!isNew && (
|
||||
<button
|
||||
onClick={() => {
|
||||
setEditing(false);
|
||||
setErrors({});
|
||||
if (item) {
|
||||
setForm({
|
||||
item_number: item.item_number || "",
|
||||
name: item.name,
|
||||
description: item.description || "",
|
||||
category_id: item.category_id
|
||||
? String(item.category_id)
|
||||
: "",
|
||||
notes: item.notes || "",
|
||||
is_active: item.is_active,
|
||||
});
|
||||
}
|
||||
}}
|
||||
className="admin-btn admin-btn-secondary"
|
||||
disabled={saving}
|
||||
>
|
||||
Zrušit
|
||||
</button>
|
||||
unit: item.unit,
|
||||
min_quantity:
|
||||
item.min_quantity != null
|
||||
? String(item.min_quantity)
|
||||
: "",
|
||||
notes: item.notes || "",
|
||||
});
|
||||
}
|
||||
}}
|
||||
className="admin-btn admin-btn-secondary"
|
||||
disabled={saving}
|
||||
>
|
||||
Zrušit
|
||||
</button>
|
||||
)}
|
||||
<button
|
||||
onClick={handleSave}
|
||||
className="admin-btn admin-btn-primary"
|
||||
@@ -303,6 +268,8 @@ export default function WarehouseItemDetail() {
|
||||
<div className="admin-spinner admin-spinner-sm" />
|
||||
Ukládání...
|
||||
</>
|
||||
) : isNew ? (
|
||||
"Vytvořit"
|
||||
) : (
|
||||
"Uložit"
|
||||
)}
|
||||
@@ -310,21 +277,14 @@ export default function WarehouseItemDetail() {
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
{id !== "new" && item && (
|
||||
<>
|
||||
<button
|
||||
onClick={handleToggleActive}
|
||||
className="admin-btn admin-btn-secondary"
|
||||
>
|
||||
{item.is_active ? "Deaktivovat" : "Aktivovat"}
|
||||
</button>
|
||||
<button
|
||||
onClick={handleDelete}
|
||||
className="admin-btn admin-btn-secondary"
|
||||
>
|
||||
Smazat
|
||||
</button>
|
||||
</>
|
||||
{item && (
|
||||
<button
|
||||
onClick={handleDelete}
|
||||
className="admin-btn admin-btn-secondary"
|
||||
style={{ color: "var(--danger)" }}
|
||||
>
|
||||
Smazat
|
||||
</button>
|
||||
)}
|
||||
<button
|
||||
onClick={() => setEditing(true)}
|
||||
@@ -484,19 +444,6 @@ export default function WarehouseItemDetail() {
|
||||
</div>
|
||||
</FormField>
|
||||
</div>
|
||||
<div className="admin-form-row">
|
||||
<FormField label="Stav">
|
||||
{item?.is_active ? (
|
||||
<span className="admin-badge admin-badge-active">
|
||||
Aktivní
|
||||
</span>
|
||||
) : (
|
||||
<span className="admin-badge admin-badge-inactive">
|
||||
Neaktivní
|
||||
</span>
|
||||
)}
|
||||
</FormField>
|
||||
</div>
|
||||
{item?.notes && (
|
||||
<FormField label="Poznámky">
|
||||
<div style={{ whiteSpace: "pre-wrap" }}>{item.notes}</div>
|
||||
@@ -563,26 +510,17 @@ export default function WarehouseItemDetail() {
|
||||
<div className="admin-card-body">
|
||||
<h3 className="admin-card-title">Dávky (FIFO)</h3>
|
||||
{batches.length === 0 ? (
|
||||
<div
|
||||
style={{
|
||||
color: "var(--text-tertiary)",
|
||||
fontSize: "0.875rem",
|
||||
textAlign: "center",
|
||||
padding: "1.5rem 0",
|
||||
}}
|
||||
>
|
||||
Zatím žádné dávky
|
||||
</div>
|
||||
<div className="admin-empty-state">Zatím žádné dávky</div>
|
||||
) : (
|
||||
<div className="admin-table-responsive">
|
||||
<table className="admin-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Datum příjmu</th>
|
||||
<th className="text-right">Původní množství</th>
|
||||
<th className="text-right">Zůstatek</th>
|
||||
<th className="text-right">Cena za jednotku</th>
|
||||
<th className="text-right">Hodnota</th>
|
||||
<th>Původní množství</th>
|
||||
<th>Zůstatek</th>
|
||||
<th>Cena za jednotku</th>
|
||||
<th>Hodnota</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@@ -591,16 +529,16 @@ export default function WarehouseItemDetail() {
|
||||
<td className="admin-mono">
|
||||
{formatDate(batch.received_at)}
|
||||
</td>
|
||||
<td className="admin-mono text-right">
|
||||
<td className="admin-mono">
|
||||
{Number(batch.original_qty)}
|
||||
</td>
|
||||
<td className="admin-mono text-right fw-500">
|
||||
<td className="admin-mono fw-500">
|
||||
{Number(batch.quantity)}
|
||||
</td>
|
||||
<td className="admin-mono text-right">
|
||||
<td className="admin-mono">
|
||||
{formatCurrency(Number(batch.unit_price), "CZK")}
|
||||
</td>
|
||||
<td className="admin-mono text-right">
|
||||
<td className="admin-mono">
|
||||
{formatCurrency(
|
||||
Number(batch.quantity) * Number(batch.unit_price),
|
||||
"CZK",
|
||||
@@ -627,16 +565,7 @@ export default function WarehouseItemDetail() {
|
||||
<div className="admin-card-body">
|
||||
<h3 className="admin-card-title">Umístění</h3>
|
||||
{locations.length === 0 ? (
|
||||
<div
|
||||
style={{
|
||||
color: "var(--text-tertiary)",
|
||||
fontSize: "0.875rem",
|
||||
textAlign: "center",
|
||||
padding: "1.5rem 0",
|
||||
}}
|
||||
>
|
||||
Zatím žádná umístění
|
||||
</div>
|
||||
<div className="admin-empty-state">Zatím žádná umístění</div>
|
||||
) : (
|
||||
<div className="admin-table-responsive">
|
||||
<table className="admin-table">
|
||||
@@ -644,7 +573,7 @@ export default function WarehouseItemDetail() {
|
||||
<tr>
|
||||
<th>Kód</th>
|
||||
<th>Název</th>
|
||||
<th className="text-right">Množství</th>
|
||||
<th>Množství</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@@ -654,7 +583,7 @@ export default function WarehouseItemDetail() {
|
||||
{loc.location?.code || "—"}
|
||||
</td>
|
||||
<td>{loc.location?.name || "—"}</td>
|
||||
<td className="admin-mono text-right fw-500">
|
||||
<td className="admin-mono fw-500">
|
||||
{Number(loc.quantity)}
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
Reference in New Issue
Block a user