1
0
Fork 0
mirror of https://github.com/s-frick/effigenix.git synced 2026-03-28 15:29:34 +01:00

feat(tui): TUI für Produktionsauftrag-Freigabe, Bestandsreservierung und Reservierungs-Freigabe

- ProductionOrderCreateScreen: Nach Anlage Freigabe per [F] mit Statusanzeige
- StockDetailScreen: Reservierungen-Tabelle, Menü für Reservieren/Freigeben
- ReserveStockScreen: Neues Formular (Referenztyp, Referenz-ID, Menge, Einheit, Priorität)
- API-Client: release(), reserveStock(), releaseReservation() Methoden
- Hooks: releaseProductionOrder(), reserveStock(), releaseReservation()
- Types: ReservationDTO, StockBatchAllocationDTO, ReserveStockRequest exportiert
- DB: Migration 027 erweitert chk_production_order_status um RELEASED
This commit is contained in:
Sebastian Frick 2026-02-24 00:57:40 +01:00
parent fb8387c10e
commit 376557925a
15 changed files with 585 additions and 13 deletions

View file

@ -55,6 +55,7 @@ import { ProductionOrderCreateScreen } from './components/production/ProductionO
import { StockListScreen } from './components/inventory/StockListScreen.js';
import { StockDetailScreen } from './components/inventory/StockDetailScreen.js';
import { StockCreateScreen } from './components/inventory/StockCreateScreen.js';
import { ReserveStockScreen } from './components/inventory/ReserveStockScreen.js';
function ScreenRouter() {
const { isAuthenticated, loading } = useAuth();
@ -122,6 +123,7 @@ function ScreenRouter() {
{current === 'stock-list' && <StockListScreen />}
{current === 'stock-detail' && <StockDetailScreen />}
{current === 'stock-create' && <StockCreateScreen />}
{current === 'stock-reserve' && <ReserveStockScreen />}
{/* Produktion */}
{current === 'production-menu' && <ProductionMenu />}
{current === 'recipe-list' && <RecipeListScreen />}

View file

@ -0,0 +1,207 @@
import React, { useState } from 'react';
import { Box, Text, useInput } from 'ink';
import { useNavigation } from '../../state/navigation-context.js';
import { useStocks } from '../../hooks/useStocks.js';
import { FormInput } from '../shared/FormInput.js';
import { LoadingSpinner } from '../shared/LoadingSpinner.js';
import { ErrorDisplay } from '../shared/ErrorDisplay.js';
import {
UOM_VALUES,
UOM_LABELS,
REFERENCE_TYPE_LABELS,
RESERVATION_PRIORITY_LABELS,
} from '@effigenix/api-client';
import type { UoM, ReferenceType, ReservationPriority } from '@effigenix/api-client';
type Field = 'referenceType' | 'referenceId' | 'quantity' | 'unit' | 'priority';
const FIELDS: Field[] = ['referenceType', 'referenceId', 'quantity', 'unit', 'priority'];
const FIELD_LABELS: Record<Field, string> = {
referenceType: 'Referenztyp (←→ wechseln)',
referenceId: 'Referenz-ID *',
quantity: 'Menge *',
unit: 'Mengeneinheit * (←→ wechseln)',
priority: 'Priorität (←→ wechseln)',
};
const REFERENCE_TYPE_VALUES: ReferenceType[] = ['PRODUCTION_ORDER', 'SALES_ORDER', 'TRANSFER'];
const PRIORITY_VALUES: ReservationPriority[] = ['LOW', 'NORMAL', 'HIGH', 'URGENT'];
export function ReserveStockScreen() {
const { params, back } = useNavigation();
const { reserveStock, loading, error, clearError } = useStocks();
const stockId = params.stockId ?? '';
const [refTypeIdx, setRefTypeIdx] = useState(0);
const [referenceId, setReferenceId] = useState('');
const [quantity, setQuantity] = useState('');
const [uomIdx, setUomIdx] = useState(0);
const [priorityIdx, setPriorityIdx] = useState(1); // NORMAL default
const [activeField, setActiveField] = useState<Field>('referenceType');
const [fieldErrors, setFieldErrors] = useState<Partial<Record<Field, string>>>({});
const [success, setSuccess] = useState(false);
const handleSubmit = async () => {
const errors: Partial<Record<Field, string>> = {};
if (!referenceId.trim()) errors.referenceId = 'Referenz-ID ist erforderlich.';
if (!quantity.trim()) errors.quantity = 'Menge ist erforderlich.';
setFieldErrors(errors);
if (Object.keys(errors).length > 0) return;
const result = await reserveStock(stockId, {
referenceType: REFERENCE_TYPE_VALUES[refTypeIdx] as string,
referenceId: referenceId.trim(),
quantityAmount: quantity.trim(),
quantityUnit: UOM_VALUES[uomIdx] as string,
priority: PRIORITY_VALUES[priorityIdx] as string,
});
if (result) setSuccess(true);
};
const handleFieldSubmit = (field: Field) => (_value: string) => {
const idx = FIELDS.indexOf(field);
if (idx < FIELDS.length - 1) {
setActiveField(FIELDS[idx + 1] ?? field);
} else {
void handleSubmit();
}
};
useInput((_input, key) => {
if (loading) return;
if (success) {
if (key.return || key.escape) back();
return;
}
if (activeField === 'referenceType') {
if (key.leftArrow || key.rightArrow) {
const dir = key.rightArrow ? 1 : -1;
setRefTypeIdx((i) => (i + dir + REFERENCE_TYPE_VALUES.length) % REFERENCE_TYPE_VALUES.length);
return;
}
if (key.return || key.tab) {
setActiveField('referenceId');
return;
}
if (key.escape) { back(); return; }
}
if (activeField === 'unit') {
if (key.leftArrow || key.rightArrow) {
const dir = key.rightArrow ? 1 : -1;
setUomIdx((i) => (i + dir + UOM_VALUES.length) % UOM_VALUES.length);
return;
}
if (key.return) {
handleFieldSubmit('unit')('');
return;
}
}
if (activeField === 'priority') {
if (key.leftArrow || key.rightArrow) {
const dir = key.rightArrow ? 1 : -1;
setPriorityIdx((i) => (i + dir + PRIORITY_VALUES.length) % PRIORITY_VALUES.length);
return;
}
if (key.return) {
void handleSubmit();
return;
}
}
if (key.tab || key.downArrow) {
setActiveField((f) => {
const idx = FIELDS.indexOf(f);
return FIELDS[(idx + 1) % FIELDS.length] ?? f;
});
}
if (key.upArrow) {
setActiveField((f) => {
const idx = FIELDS.indexOf(f);
return FIELDS[(idx - 1 + FIELDS.length) % FIELDS.length] ?? f;
});
}
if (key.escape) back();
});
if (loading) {
return (
<Box flexDirection="column" alignItems="center" paddingY={2}>
<LoadingSpinner label="Bestand wird reserviert..." />
</Box>
);
}
if (success) {
return (
<Box flexDirection="column" gap={1} paddingY={1}>
<Text color="green" bold>Bestand erfolgreich reserviert!</Text>
<Text color="gray" dimColor>Enter/Escape zum Zurückkehren</Text>
</Box>
);
}
const refTypeLabel = REFERENCE_TYPE_LABELS[REFERENCE_TYPE_VALUES[refTypeIdx] as ReferenceType] ?? REFERENCE_TYPE_VALUES[refTypeIdx];
const uomLabel = UOM_LABELS[UOM_VALUES[uomIdx] as UoM] ?? UOM_VALUES[uomIdx];
const priorityLabel = RESERVATION_PRIORITY_LABELS[PRIORITY_VALUES[priorityIdx] as ReservationPriority] ?? PRIORITY_VALUES[priorityIdx];
return (
<Box flexDirection="column" gap={1}>
<Text color="cyan" bold>Bestand reservieren</Text>
{error && <ErrorDisplay message={error} onDismiss={clearError} />}
<Box flexDirection="column" gap={1} width={60}>
{/* Reference Type */}
<Box flexDirection="column">
<Text color={activeField === 'referenceType' ? 'cyan' : 'gray'}>
{FIELD_LABELS.referenceType}: <Text bold color="white">{activeField === 'referenceType' ? `< ${refTypeLabel} >` : refTypeLabel}</Text>
</Text>
</Box>
{/* Reference ID */}
<FormInput
label={FIELD_LABELS.referenceId}
value={referenceId}
onChange={setReferenceId}
onSubmit={handleFieldSubmit('referenceId')}
focus={activeField === 'referenceId'}
{...(fieldErrors.referenceId ? { error: fieldErrors.referenceId } : {})}
/>
{/* Quantity */}
<FormInput
label={FIELD_LABELS.quantity}
value={quantity}
onChange={setQuantity}
onSubmit={handleFieldSubmit('quantity')}
focus={activeField === 'quantity'}
{...(fieldErrors.quantity ? { error: fieldErrors.quantity } : {})}
/>
{/* Unit */}
<Box flexDirection="column">
<Text color={activeField === 'unit' ? 'cyan' : 'gray'}>
{FIELD_LABELS.unit}: <Text bold color="white">{activeField === 'unit' ? `< ${uomLabel} >` : uomLabel}</Text>
</Text>
</Box>
{/* Priority */}
<Box flexDirection="column">
<Text color={activeField === 'priority' ? 'cyan' : 'gray'}>
{FIELD_LABELS.priority}: <Text bold color="white">{activeField === 'priority' ? `< ${priorityLabel} >` : priorityLabel}</Text>
</Text>
</Box>
</Box>
<Box marginTop={1}>
<Text color="gray" dimColor>
Tab/ Feld wechseln · Typ/Einheit/Priorität · Enter bestätigen/speichern · Escape Abbrechen
</Text>
</Box>
</Box>
);
}

View file

@ -6,10 +6,10 @@ import { useStocks } from '../../hooks/useStocks.js';
import { LoadingSpinner } from '../shared/LoadingSpinner.js';
import { ErrorDisplay } from '../shared/ErrorDisplay.js';
import { SuccessDisplay } from '../shared/SuccessDisplay.js';
import { STOCK_BATCH_STATUS_LABELS } from '@effigenix/api-client';
import type { StockBatchStatus } from '@effigenix/api-client';
import { STOCK_BATCH_STATUS_LABELS, REFERENCE_TYPE_LABELS, RESERVATION_PRIORITY_LABELS } from '@effigenix/api-client';
import type { StockBatchStatus, ReferenceType, ReservationPriority } from '@effigenix/api-client';
type Mode = 'view' | 'menu' | 'batch-actions' | 'block-reason' | 'remove-amount' | 'remove-unit';
type Mode = 'view' | 'menu' | 'batch-actions' | 'block-reason' | 'remove-amount' | 'remove-unit' | 'reservation-actions' | 'confirm-release-reservation';
const BATCH_STATUS_COLORS: Record<string, string> = {
AVAILABLE: 'green',
@ -19,8 +19,8 @@ const BATCH_STATUS_COLORS: Record<string, string> = {
};
export function StockDetailScreen() {
const { params, back } = useNavigation();
const { stock, loading, error, fetchStock, blockBatch, unblockBatch, removeBatch, clearError } = useStocks();
const { params, back, navigate } = useNavigation();
const { stock, loading, error, fetchStock, blockBatch, unblockBatch, removeBatch, releaseReservation, clearError } = useStocks();
const [mode, setMode] = useState<Mode>('view');
const [menuIndex, setMenuIndex] = useState(0);
const [batchIndex, setBatchIndex] = useState(0);
@ -28,6 +28,7 @@ export function StockDetailScreen() {
const [blockReason, setBlockReason] = useState('');
const [removeAmount, setRemoveAmount] = useState('');
const [removeUnit, setRemoveUnit] = useState('');
const [reservationIndex, setReservationIndex] = useState(0);
const [success, setSuccess] = useState<string | null>(null);
const stockId = params.stockId ?? '';
@ -38,6 +39,8 @@ export function StockDetailScreen() {
const batches = stock?.batches ?? [];
const selectedBatch = batches[batchIndex];
const reservations = stock?.reservations ?? [];
const selectedReservation = reservations[reservationIndex];
const getBatchActions = () => {
if (!selectedBatch) return [];
@ -104,8 +107,20 @@ export function StockDetailScreen() {
}
};
const handleReleaseReservation = async () => {
if (!selectedReservation?.id) return;
const result = await releaseReservation(stockId, selectedReservation.id);
if (result) {
setSuccess('Reservierung freigegeben.');
setMode('view');
setReservationIndex(0);
}
};
const MENU_ITEMS = [
{ label: 'Chargen verwalten', action: 'batches' },
{ label: 'Bestand reservieren', action: 'reserve' },
...(reservations.length > 0 ? [{ label: 'Reservierung freigeben', action: 'release-reservation' }] : []),
];
useInput((input, key) => {
@ -126,6 +141,24 @@ export function StockDetailScreen() {
return;
}
if (mode === 'confirm-release-reservation') {
if (input.toLowerCase() === 'j') {
void handleReleaseReservation();
}
if (input.toLowerCase() === 'n' || key.escape) setMode('reservation-actions');
return;
}
if (mode === 'reservation-actions') {
if (key.upArrow) setReservationIndex((i) => Math.max(0, i - 1));
if (key.downArrow) setReservationIndex((i) => Math.min(reservations.length - 1, i + 1));
if (key.return && selectedReservation) {
setMode('confirm-release-reservation');
}
if (key.escape) setMode('view');
return;
}
if (mode === 'batch-actions') {
if (key.upArrow) setBatchActionIndex((i) => Math.max(0, i - 1));
if (key.downArrow) setBatchActionIndex((i) => Math.min(batchActions.length - 1, i + 1));
@ -142,10 +175,16 @@ export function StockDetailScreen() {
if (key.upArrow) setMenuIndex((i) => Math.max(0, i - 1));
if (key.downArrow) setMenuIndex((i) => Math.min(MENU_ITEMS.length - 1, i + 1));
if (key.return && MENU_ITEMS[menuIndex]) {
if (MENU_ITEMS[menuIndex].action === 'batches' && batches.length > 0) {
const action = MENU_ITEMS[menuIndex].action;
if (action === 'batches' && batches.length > 0) {
setMode('batch-actions');
setBatchIndex(0);
setBatchActionIndex(0);
} else if (action === 'reserve') {
navigate('stock-reserve', { stockId });
} else if (action === 'release-reservation') {
setMode('reservation-actions');
setReservationIndex(0);
}
}
if (key.escape) setMode('view');
@ -228,6 +267,37 @@ export function StockDetailScreen() {
)}
</Box>
{/* Reservations table */}
{reservations.length > 0 && (
<Box flexDirection="column" borderStyle="round" borderColor="gray" paddingX={1}>
<Text color="cyan" bold>Reservierungen ({reservations.length})</Text>
<Box paddingX={1}>
<Text color="gray" bold>{' Typ'.padEnd(22)}</Text>
<Text color="gray" bold>{'Referenz'.padEnd(18)}</Text>
<Text color="gray" bold>{'Menge'.padEnd(14)}</Text>
<Text color="gray" bold>{'Priorität'.padEnd(12)}</Text>
<Text color="gray" bold>Reserviert am</Text>
</Box>
{reservations.map((r, i) => {
const isSelected = mode === 'reservation-actions' && i === reservationIndex;
const textColor = isSelected ? 'cyan' : 'white';
const refLabel = REFERENCE_TYPE_LABELS[(r.referenceType ?? '') as ReferenceType] ?? r.referenceType;
const prioLabel = RESERVATION_PRIORITY_LABELS[(r.priority ?? '') as ReservationPriority] ?? r.priority;
const dateStr = r.reservedAt ? new Date(r.reservedAt).toLocaleDateString('de-DE') : '';
return (
<Box key={r.id} paddingX={1}>
<Text color={textColor}>{isSelected ? '▶ ' : ' '}</Text>
<Text color={textColor}>{(refLabel ?? '').padEnd(20)}</Text>
<Text color={isSelected ? 'cyan' : 'gray'}>{(r.referenceId ?? '').substring(0, 16).padEnd(16)}</Text>
<Text color={isSelected ? 'cyan' : 'gray'}>{`${r.quantityAmount ?? ''} ${r.quantityUnit ?? ''}`.padEnd(14)}</Text>
<Text color={isSelected ? 'cyan' : 'gray'}>{(prioLabel ?? '').padEnd(12)}</Text>
<Text color={isSelected ? 'cyan' : 'gray'}>{dateStr}</Text>
</Box>
);
})}
</Box>
)}
{/* Modes */}
{mode === 'menu' && (
<Box flexDirection="column" borderStyle="round" borderColor="cyan" paddingX={1}>
@ -305,6 +375,22 @@ export function StockDetailScreen() {
</Box>
)}
{mode === 'reservation-actions' && (
<Box flexDirection="column" borderStyle="round" borderColor="cyan" paddingX={1}>
<Text color="cyan" bold>Reservierung auswählen</Text>
<Text color="gray" dimColor> Reservierung wählen · Enter freigeben · Escape zurück</Text>
</Box>
)}
{mode === 'confirm-release-reservation' && selectedReservation && (
<Box flexDirection="column" borderStyle="round" borderColor="yellow" paddingX={1}>
<Text color="yellow" bold>Reservierung freigeben?</Text>
<Text>Referenz: {selectedReservation.referenceType} / {selectedReservation.referenceId}</Text>
<Text>Menge: {selectedReservation.quantityAmount} {selectedReservation.quantityUnit}</Text>
<Text color="gray" dimColor>[J] Ja · [N] Nein</Text>
</Box>
)}
<Box marginTop={1}>
<Text color="gray" dimColor>
[m] Aktionsmenü · Backspace Zurück

View file

@ -25,7 +25,7 @@ const PRIORITY_VALUES: Priority[] = ['LOW', 'NORMAL', 'HIGH', 'URGENT'];
export function ProductionOrderCreateScreen() {
const { back } = useNavigation();
const { createProductionOrder, loading, error, clearError } = useProductionOrders();
const { createProductionOrder, releaseProductionOrder, productionOrder, loading, error, clearError } = useProductionOrders();
const { recipes, fetchRecipes } = useRecipes();
const [quantity, setQuantity] = useState('');
@ -37,6 +37,7 @@ export function ProductionOrderCreateScreen() {
const [activeField, setActiveField] = useState<Field>('recipe');
const [fieldErrors, setFieldErrors] = useState<Partial<Record<Field, string>>>({});
const [success, setSuccess] = useState(false);
const [released, setReleased] = useState(false);
useEffect(() => {
void fetchRecipes('ACTIVE');
@ -74,11 +75,23 @@ export function ProductionOrderCreateScreen() {
useInput((_input, key) => {
if (loading) return;
if (success) {
if (released) {
if (key.return || key.escape) back();
return;
}
if (success) {
if (_input.toLowerCase() === 'f' && productionOrder?.id) {
void (async () => {
const result = await releaseProductionOrder(productionOrder.id);
if (result) setReleased(true);
})();
return;
}
if (key.escape) back();
return;
}
if (activeField === 'recipe') {
if (key.upArrow) setRecipeIdx((i) => Math.max(0, i - 1));
if (key.downArrow) setRecipeIdx((i) => Math.min(recipes.length - 1, i + 1));
@ -134,11 +147,34 @@ export function ProductionOrderCreateScreen() {
);
}
if (released) {
return (
<Box flexDirection="column" gap={1} paddingY={1}>
<Text color="green" bold>Produktionsauftrag freigegeben!</Text>
{productionOrder && (
<Box flexDirection="column" borderStyle="round" borderColor="gray" paddingX={1}>
<Box><Text color="gray">ID: </Text><Text>{productionOrder.id}</Text></Box>
<Box><Text color="gray">Status: </Text><Text color="green">{productionOrder.status}</Text></Box>
</Box>
)}
<Text color="gray" dimColor>Enter/Escape zum Zurückkehren</Text>
</Box>
);
}
if (success) {
return (
<Box flexDirection="column" gap={1} paddingY={1}>
<Text color="green" bold>Produktionsauftrag erfolgreich erstellt!</Text>
<Text color="gray" dimColor>Enter/Escape zum Zurückkehren</Text>
{productionOrder && (
<Box flexDirection="column" borderStyle="round" borderColor="gray" paddingX={1}>
<Box><Text color="gray">ID: </Text><Text>{productionOrder.id}</Text></Box>
<Box><Text color="gray">Rezept: </Text><Text>{productionOrder.recipeId}</Text></Box>
<Box><Text color="gray">Menge: </Text><Text>{productionOrder.plannedQuantity} {productionOrder.plannedQuantityUnit}</Text></Box>
<Box><Text color="gray">Status: </Text><Text>{productionOrder.status}</Text></Box>
</Box>
)}
<Text color="gray" dimColor>[F] Freigeben · Escape Zurück</Text>
</Box>
);
}

View file

@ -31,6 +31,18 @@ export function useProductionOrders() {
}
}, []);
const releaseProductionOrder = useCallback(async (id: string) => {
setState((s) => ({ ...s, loading: true, error: null }));
try {
const productionOrder = await client.productionOrders.release(id);
setState({ productionOrder, loading: false, error: null });
return productionOrder;
} catch (err) {
setState((s) => ({ ...s, loading: false, error: errorMessage(err) }));
return null;
}
}, []);
const clearError = useCallback(() => {
setState((s) => ({ ...s, error: null }));
}, []);
@ -38,6 +50,7 @@ export function useProductionOrders() {
return {
...state,
createProductionOrder,
releaseProductionOrder,
clearError,
};
}

View file

@ -6,6 +6,7 @@ import type {
CreateStockRequest,
UpdateStockRequest,
StockFilter,
ReserveStockRequest,
} from '@effigenix/api-client';
import { client } from '../utils/api-client.js';
@ -123,6 +124,32 @@ export function useStocks() {
}
}, []);
const reserveStock = useCallback(async (stockId: string, request: ReserveStockRequest) => {
setState((s) => ({ ...s, loading: true, error: null }));
try {
const reservation = await client.stocks.reserveStock(stockId, request);
const stock = await client.stocks.getById(stockId);
setState((s) => ({ ...s, stock, loading: false, error: null }));
return reservation;
} catch (err) {
setState((s) => ({ ...s, loading: false, error: errorMessage(err) }));
return null;
}
}, []);
const releaseReservation = useCallback(async (stockId: string, reservationId: string) => {
setState((s) => ({ ...s, loading: true, error: null }));
try {
await client.stocks.releaseReservation(stockId, reservationId);
const stock = await client.stocks.getById(stockId);
setState((s) => ({ ...s, stock, loading: false, error: null }));
return true;
} catch (err) {
setState((s) => ({ ...s, loading: false, error: errorMessage(err) }));
return false;
}
}, []);
const clearError = useCallback(() => {
setState((s) => ({ ...s, error: null }));
}, []);
@ -137,6 +164,8 @@ export function useStocks() {
removeBatch,
blockBatch,
unblockBatch,
reserveStock,
releaseReservation,
clearError,
};
}

View file

@ -51,7 +51,8 @@ export type Screen =
| 'batch-plan'
| 'batch-record-consumption'
| 'batch-complete'
| 'production-order-create';
| 'production-order-create'
| 'stock-reserve';
interface NavigationState {
current: Screen;