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,44 +1,80 @@
|
||||
import { useState, useEffect, useRef } from "react";
|
||||
import { useParams, useNavigate, Link } from "react-router-dom";
|
||||
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { useState, useEffect, useRef, useId } from "react";
|
||||
import { useParams, useNavigate, useLocation, Link } 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 } from "framer-motion";
|
||||
import FormField from "../components/FormField";
|
||||
import WarehouseMovementTable, {
|
||||
type MovementLine,
|
||||
type MovementItem,
|
||||
} 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";
|
||||
import { warehouseIssueDetailOptions } from "../lib/queries/warehouse";
|
||||
import { projectListOptions, type Project } from "../lib/queries/projects";
|
||||
import { useApiMutation } from "../lib/queries/mutations";
|
||||
|
||||
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 location = useLocation();
|
||||
|
||||
const baseId = useId();
|
||||
const counterRef = useRef(0);
|
||||
const nextKey = () => `${baseId}-item-${counterRef.current++}`;
|
||||
|
||||
// Pre-fill data from reservation ("Create výdej" button)
|
||||
const prefill = location.state as {
|
||||
reservationId?: number;
|
||||
itemId?: number;
|
||||
projectId?: number;
|
||||
quantity?: number;
|
||||
itemName?: string;
|
||||
} | null;
|
||||
|
||||
const isEdit = id !== undefined && id !== "new";
|
||||
|
||||
const [form, setForm] = useState<IssueForm>({
|
||||
project_id: null,
|
||||
project_id: prefill?.projectId ?? null,
|
||||
notes: "",
|
||||
});
|
||||
const [lines, setLines] = useState<MovementLine[]>([]);
|
||||
const [items, setItems] = useState<MovementItem[]>(() =>
|
||||
isEdit
|
||||
? []
|
||||
: prefill?.itemId
|
||||
? [
|
||||
{
|
||||
key: nextKey(),
|
||||
item_id: prefill.itemId,
|
||||
item_name: prefill.itemName,
|
||||
quantity: prefill.quantity ?? 0,
|
||||
unit_price: 0,
|
||||
location_id: null,
|
||||
batch_id: null,
|
||||
reservation_id: prefill.reservationId ?? null,
|
||||
notes: null,
|
||||
},
|
||||
]
|
||||
: [
|
||||
{
|
||||
key: nextKey(),
|
||||
item_id: null,
|
||||
quantity: 0,
|
||||
unit_price: 0,
|
||||
location_id: null,
|
||||
batch_id: null,
|
||||
reservation_id: null,
|
||||
notes: null,
|
||||
},
|
||||
],
|
||||
);
|
||||
const [errors, setErrors] = useState<Record<string, string>>({});
|
||||
const [saving, setSaving] = useState(false);
|
||||
|
||||
@@ -48,8 +84,6 @@ export default function WarehouseIssueForm() {
|
||||
warehouseIssueDetailOptions(isEdit ? id : undefined),
|
||||
);
|
||||
|
||||
const { data: locations = [] } = useQuery(warehouseLocationListOptions());
|
||||
|
||||
const { data: projectsData } = useQuery(projectListOptions({ perPage: 100 }));
|
||||
const projects = projectsData?.data ?? [];
|
||||
|
||||
@@ -63,18 +97,18 @@ export default function WarehouseIssueForm() {
|
||||
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,
|
||||
if (issue.items && issue.items.length > 0) {
|
||||
setItems(
|
||||
issue.items.map((item) => ({
|
||||
key: nextKey(),
|
||||
item_id: item.item_id,
|
||||
item_name: item.item?.name,
|
||||
quantity: Number(item.quantity),
|
||||
unit_price: item.batch ? Number(item.batch.unit_price) : 0,
|
||||
location_id: item.location_id,
|
||||
batch_id: item.batch_id,
|
||||
reservation_id: item.reservation_id,
|
||||
notes: item.notes,
|
||||
})),
|
||||
);
|
||||
}
|
||||
@@ -84,11 +118,11 @@ export default function WarehouseIssueForm() {
|
||||
|
||||
if (!hasPermission("warehouse.operate")) return <Forbidden />;
|
||||
|
||||
const addEmptyLine = () => {
|
||||
setLines((prev) => [
|
||||
const addItem = () => {
|
||||
setItems((prev) => [
|
||||
...prev,
|
||||
{
|
||||
key: `line-${++lineCounter}`,
|
||||
key: nextKey(),
|
||||
item_id: null,
|
||||
quantity: 0,
|
||||
unit_price: 0,
|
||||
@@ -100,25 +134,18 @@ export default function WarehouseIssueForm() {
|
||||
]);
|
||||
};
|
||||
|
||||
// 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";
|
||||
const validItems = items.filter((l) => l.item_id !== null);
|
||||
if (validItems.length === 0) {
|
||||
newErrors.items = "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";
|
||||
for (const item of validItems) {
|
||||
if (item.quantity <= 0) {
|
||||
newErrors.items = "Množství musí být větší než 0";
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -129,7 +156,7 @@ export default function WarehouseIssueForm() {
|
||||
const buildPayload = () => ({
|
||||
project_id: form.project_id,
|
||||
notes: form.notes.trim() || null,
|
||||
lines: lines
|
||||
items: items
|
||||
.filter((l) => l.item_id !== null)
|
||||
.map((l) => ({
|
||||
item_id: l.item_id!,
|
||||
@@ -141,47 +168,51 @@ export default function WarehouseIssueForm() {
|
||||
})),
|
||||
});
|
||||
|
||||
const saveIssue = async (): Promise<number | null> => {
|
||||
const url = isEdit
|
||||
? `/api/admin/warehouse/issues/${id}`
|
||||
: "/api/admin/warehouse/issues";
|
||||
const method = isEdit ? "PUT" : "POST";
|
||||
const saveMutation = useApiMutation<
|
||||
ReturnType<typeof buildPayload>,
|
||||
{ id?: number }
|
||||
>({
|
||||
url: () =>
|
||||
isEdit
|
||||
? `/api/admin/warehouse/issues/${id}`
|
||||
: "/api/admin/warehouse/issues",
|
||||
method: () => (isEdit ? "PUT" : "POST"),
|
||||
invalidate: ["warehouse"],
|
||||
});
|
||||
|
||||
const [confirmIssueId, setConfirmIssueId] = useState<number | null>(null);
|
||||
const confirmMutation = useApiMutation<void, void>({
|
||||
url: () =>
|
||||
confirmIssueId
|
||||
? `/api/admin/warehouse/issues/${confirmIssueId}/confirm`
|
||||
: "/api/admin/warehouse/issues/0/confirm",
|
||||
method: () => "POST",
|
||||
invalidate: ["warehouse"],
|
||||
onSuccess: () => {
|
||||
alert.success("Výdejka byla potvrzena");
|
||||
},
|
||||
});
|
||||
|
||||
const saveIssue = async (): Promise<number | null> => {
|
||||
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í");
|
||||
const data = await saveMutation.mutateAsync(buildPayload());
|
||||
return data?.id ?? Number(id);
|
||||
} catch (e) {
|
||||
alert.error(
|
||||
e instanceof Error ? e.message : "Nepodařilo se uložit výdejku",
|
||||
);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
const confirmIssue = async (issueId: number) => {
|
||||
setConfirmIssueId(issueId);
|
||||
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í");
|
||||
await confirmMutation.mutateAsync(undefined);
|
||||
} catch (e) {
|
||||
alert.error(e instanceof Error ? e.message : "Chyba připojení");
|
||||
} finally {
|
||||
setConfirmIssueId(null);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -297,22 +328,14 @@ export default function WarehouseIssueForm() {
|
||||
className="admin-form-select"
|
||||
>
|
||||
<option value="">Vyberte projekt...</option>
|
||||
{projects.map((p) => (
|
||||
{projects.map((p: Project) => (
|
||||
<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>
|
||||
<div className="admin-form-error">{errors.project_id}</div>
|
||||
)}
|
||||
</FormField>
|
||||
<FormField label="Poznámky">
|
||||
@@ -338,23 +361,19 @@ export default function WarehouseIssueForm() {
|
||||
transition={{ duration: 0.25, delay: 0.08 }}
|
||||
>
|
||||
<div className="admin-card-body">
|
||||
<h3 className="admin-card-title">Řádky výdejky</h3>
|
||||
{errors.lines && (
|
||||
<h3 className="admin-card-title">Položky</h3>
|
||||
{errors.items && (
|
||||
<div
|
||||
style={{
|
||||
color: "var(--danger)",
|
||||
fontSize: "0.875rem",
|
||||
marginBottom: "0.75rem",
|
||||
}}
|
||||
className="admin-form-error"
|
||||
style={{ marginBottom: "0.75rem" }}
|
||||
>
|
||||
{errors.lines}
|
||||
{errors.items}
|
||||
</div>
|
||||
)}
|
||||
<WarehouseMovementTable
|
||||
lines={lines}
|
||||
onChange={setLines}
|
||||
items={items}
|
||||
onChange={setItems}
|
||||
mode="issue"
|
||||
locations={locations as WarehouseLocation[]}
|
||||
/>
|
||||
</div>
|
||||
</motion.div>
|
||||
|
||||
Reference in New Issue
Block a user