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,4 +1,4 @@
|
||||
import { useState, useEffect, useRef, useCallback } from "react";
|
||||
import { useState, useEffect, useRef, useCallback, useId } from "react";
|
||||
import { useParams, useNavigate, Link } from "react-router-dom";
|
||||
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { useAlert } from "../context/AlertContext";
|
||||
@@ -6,17 +6,15 @@ import { useAuth } from "../context/AuthContext";
|
||||
import Forbidden from "../components/Forbidden";
|
||||
import { motion } from "framer-motion";
|
||||
import FormField from "../components/FormField";
|
||||
import AdminDatePicker from "../components/AdminDatePicker";
|
||||
import SupplierSelect from "../components/warehouse/SupplierSelect";
|
||||
import WarehouseMovementTable, {
|
||||
type MovementLine,
|
||||
type MovementItem,
|
||||
} from "../components/warehouse/WarehouseMovementTable";
|
||||
import { warehouseLocationListOptions } from "../lib/queries/warehouse";
|
||||
|
||||
import apiFetch from "../utils/api";
|
||||
import {
|
||||
warehouseReceiptDetailOptions,
|
||||
type WarehouseLocation,
|
||||
} from "../lib/queries/warehouse";
|
||||
import { warehouseReceiptDetailOptions } from "../lib/queries/warehouse";
|
||||
import { useApiMutation } from "../lib/queries/mutations";
|
||||
|
||||
interface ReceiptForm {
|
||||
supplier_id: number | null;
|
||||
@@ -25,8 +23,6 @@ interface ReceiptForm {
|
||||
notes: string;
|
||||
}
|
||||
|
||||
let lineCounter = 0;
|
||||
|
||||
export default function WarehouseReceiptForm() {
|
||||
const { id } = useParams();
|
||||
const alert = useAlert();
|
||||
@@ -36,13 +32,32 @@ export default function WarehouseReceiptForm() {
|
||||
|
||||
const isEdit = id !== undefined && id !== "new";
|
||||
|
||||
const baseId = useId();
|
||||
const counterRef = useRef(0);
|
||||
const nextKey = () => `${baseId}-item-${counterRef.current++}`;
|
||||
|
||||
const [form, setForm] = useState<ReceiptForm>({
|
||||
supplier_id: null,
|
||||
delivery_note_number: "",
|
||||
delivery_note_date: "",
|
||||
notes: "",
|
||||
});
|
||||
const [lines, setLines] = useState<MovementLine[]>([]);
|
||||
const [items, setItems] = useState<MovementItem[]>(() =>
|
||||
isEdit
|
||||
? []
|
||||
: [
|
||||
{
|
||||
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);
|
||||
const [uploadingFiles, setUploadingFiles] = useState(false);
|
||||
@@ -53,8 +68,6 @@ export default function WarehouseReceiptForm() {
|
||||
warehouseReceiptDetailOptions(isEdit ? id : undefined),
|
||||
);
|
||||
|
||||
const { data: locations = [] } = useQuery(warehouseLocationListOptions());
|
||||
|
||||
useEffect(() => {
|
||||
formInitialized.current = false;
|
||||
}, [id]);
|
||||
@@ -69,18 +82,18 @@ export default function WarehouseReceiptForm() {
|
||||
: "",
|
||||
notes: receipt.notes || "",
|
||||
});
|
||||
if (receipt.lines && receipt.lines.length > 0) {
|
||||
setLines(
|
||||
receipt.lines.map((line) => ({
|
||||
key: `line-${++lineCounter}`,
|
||||
item_id: line.item_id,
|
||||
item_name: line.item?.name,
|
||||
quantity: Number(line.quantity),
|
||||
unit_price: Number(line.unit_price),
|
||||
location_id: line.location_id,
|
||||
if (receipt.items && receipt.items.length > 0) {
|
||||
setItems(
|
||||
receipt.items.map((item) => ({
|
||||
key: nextKey(),
|
||||
item_id: item.item_id,
|
||||
item_name: item.item?.name,
|
||||
quantity: Number(item.quantity),
|
||||
unit_price: Number(item.unit_price),
|
||||
location_id: item.location_id,
|
||||
batch_id: null,
|
||||
reservation_id: null,
|
||||
notes: line.notes,
|
||||
notes: item.notes,
|
||||
})),
|
||||
);
|
||||
}
|
||||
@@ -90,11 +103,11 @@ export default function WarehouseReceiptForm() {
|
||||
|
||||
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,
|
||||
@@ -106,22 +119,15 @@ export default function WarehouseReceiptForm() {
|
||||
]);
|
||||
};
|
||||
|
||||
// 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> = {};
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -134,7 +140,7 @@ export default function WarehouseReceiptForm() {
|
||||
delivery_note_number: form.delivery_note_number.trim() || null,
|
||||
delivery_note_date: form.delivery_note_date || null,
|
||||
notes: form.notes.trim() || null,
|
||||
lines: lines
|
||||
items: items
|
||||
.filter((l) => l.item_id !== null)
|
||||
.map((l) => ({
|
||||
item_id: l.item_id!,
|
||||
@@ -145,47 +151,51 @@ export default function WarehouseReceiptForm() {
|
||||
})),
|
||||
});
|
||||
|
||||
const saveReceipt = async (): Promise<number | null> => {
|
||||
const url = isEdit
|
||||
? `/api/admin/warehouse/receipts/${id}`
|
||||
: "/api/admin/warehouse/receipts";
|
||||
const method = isEdit ? "PUT" : "POST";
|
||||
const saveMutation = useApiMutation<
|
||||
ReturnType<typeof buildPayload>,
|
||||
{ id?: number }
|
||||
>({
|
||||
url: () =>
|
||||
isEdit
|
||||
? `/api/admin/warehouse/receipts/${id}`
|
||||
: "/api/admin/warehouse/receipts",
|
||||
method: () => (isEdit ? "PUT" : "POST"),
|
||||
invalidate: ["warehouse"],
|
||||
});
|
||||
|
||||
const [confirmReceiptId, setConfirmReceiptId] = useState<number | null>(null);
|
||||
const confirmMutation = useApiMutation<void, void>({
|
||||
url: () =>
|
||||
confirmReceiptId
|
||||
? `/api/admin/warehouse/receipts/${confirmReceiptId}/confirm`
|
||||
: "/api/admin/warehouse/receipts/0/confirm",
|
||||
method: () => "POST",
|
||||
invalidate: ["warehouse"],
|
||||
onSuccess: () => {
|
||||
alert.success("Doklad byl potvrzen");
|
||||
},
|
||||
});
|
||||
|
||||
const saveReceipt = 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", "receipts"] });
|
||||
return result.data?.id ?? Number(id);
|
||||
} else {
|
||||
alert.error(result.error || "Nepodařilo se uložit doklad");
|
||||
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 doklad",
|
||||
);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
const confirmReceipt = async (receiptId: number) => {
|
||||
setConfirmReceiptId(receiptId);
|
||||
try {
|
||||
const response = await apiFetch(
|
||||
`/api/admin/warehouse/receipts/${receiptId}/confirm`,
|
||||
{ method: "POST" },
|
||||
);
|
||||
const result = await response.json();
|
||||
if (result.success) {
|
||||
queryClient.invalidateQueries({ queryKey: ["warehouse", "receipts"] });
|
||||
alert.success("Doklad byl potvrzen");
|
||||
} else {
|
||||
alert.error(result.error || "Nepodařilo se potvrdit doklad");
|
||||
}
|
||||
} catch {
|
||||
alert.error("Chyba připojení");
|
||||
await confirmMutation.mutateAsync(undefined);
|
||||
} catch (e) {
|
||||
alert.error(e instanceof Error ? e.message : "Chyba připojení");
|
||||
} finally {
|
||||
setConfirmReceiptId(null);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -239,7 +249,7 @@ export default function WarehouseReceiptForm() {
|
||||
);
|
||||
}
|
||||
}
|
||||
queryClient.invalidateQueries({ queryKey: ["warehouse", "receipts"] });
|
||||
queryClient.invalidateQueries({ queryKey: ["warehouse"] });
|
||||
alert.success("Soubory byly nahrány");
|
||||
} catch {
|
||||
alert.error("Chyba připojení");
|
||||
@@ -359,16 +369,16 @@ export default function WarehouseReceiptForm() {
|
||||
/>
|
||||
</FormField>
|
||||
<FormField label="Datum dodacího listu">
|
||||
<input
|
||||
type="date"
|
||||
<AdminDatePicker
|
||||
mode="date"
|
||||
value={form.delivery_note_date}
|
||||
onChange={(e) =>
|
||||
onChange={(val) =>
|
||||
setForm((prev) => ({
|
||||
...prev,
|
||||
delivery_note_date: e.target.value,
|
||||
delivery_note_date: val,
|
||||
}))
|
||||
}
|
||||
className="admin-form-input"
|
||||
placeholder="dd.mm.yyyy"
|
||||
/>
|
||||
</FormField>
|
||||
</div>
|
||||
@@ -395,23 +405,19 @@ export default function WarehouseReceiptForm() {
|
||||
transition={{ duration: 0.25, delay: 0.08 }}
|
||||
>
|
||||
<div className="admin-card-body">
|
||||
<h3 className="admin-card-title">Řádky dokladu</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="receipt"
|
||||
locations={locations as WarehouseLocation[]}
|
||||
/>
|
||||
</div>
|
||||
</motion.div>
|
||||
@@ -427,18 +433,10 @@ export default function WarehouseReceiptForm() {
|
||||
<div className="admin-card-body">
|
||||
<h3 className="admin-card-title">Přílohy</h3>
|
||||
<div
|
||||
className="admin-file-dropzone"
|
||||
className="admin-warehouse-upload"
|
||||
onDrop={handleDrop}
|
||||
onDragOver={handleDragOver}
|
||||
style={{
|
||||
border: "2px dashed var(--border)",
|
||||
borderRadius: "8px",
|
||||
padding: "2rem",
|
||||
textAlign: "center",
|
||||
cursor: "pointer",
|
||||
opacity: uploadingFiles ? 0.6 : 1,
|
||||
transition: "opacity 0.2s",
|
||||
}}
|
||||
style={{ opacity: uploadingFiles ? 0.6 : 1 }}
|
||||
onClick={() => {
|
||||
const input = document.createElement("input");
|
||||
input.type = "file";
|
||||
@@ -467,25 +465,16 @@ export default function WarehouseReceiptForm() {
|
||||
strokeWidth="1.5"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
style={{
|
||||
color: "var(--text-tertiary)",
|
||||
margin: "0 auto 0.5rem",
|
||||
}}
|
||||
className="admin-warehouse-upload-icon"
|
||||
>
|
||||
<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4" />
|
||||
<polyline points="17 8 12 3 7 8" />
|
||||
<line x1="12" y1="3" x2="12" y2="15" />
|
||||
</svg>
|
||||
<p style={{ color: "var(--text-secondary)" }}>
|
||||
<p className="admin-warehouse-upload-text">
|
||||
Přetáhněte soubory sem nebo klikněte pro výběr
|
||||
</p>
|
||||
<p
|
||||
style={{
|
||||
color: "var(--text-tertiary)",
|
||||
fontSize: "0.8rem",
|
||||
marginTop: "0.25rem",
|
||||
}}
|
||||
>
|
||||
<p className="admin-warehouse-upload-hint">
|
||||
Dodací listy, faktury a další dokumenty
|
||||
</p>
|
||||
</>
|
||||
|
||||
Reference in New Issue
Block a user