Connect production orders to stock movements
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||
import { type FormEvent, useCallback, useEffect, useMemo, useState } from 'react';
|
||||
import { Link, useOutletContext } from 'react-router-dom';
|
||||
import { ArrowLeft, CalendarDays, CheckCircle2, ClipboardList, Clock3, Download, PackageCheck, Search } from 'lucide-react';
|
||||
import DateRangePicker from '../components/DateRangePicker';
|
||||
import PaginationControls from '../components/PaginationControls';
|
||||
import RefreshStatus from '../components/RefreshStatus';
|
||||
import { exportToCSV, fetchProductionOrders } from '../dataService';
|
||||
import type { DateRange, ProductionOrderItem, ProductionOrderStatus, ProductionOrderSummary } from '../types';
|
||||
import { consumeSupplyLotForProduction, exportToCSV, fetchProductionOrders, fetchSupplySummary } from '../dataService';
|
||||
import type { DateRange, ProductionOrderItem, ProductionOrderStatus, ProductionOrderSummary, SupplyLot } from '../types';
|
||||
|
||||
type ProductionOrderStatusTab = 'all' | 'open' | 'in_progress' | 'finished' | 'canceled';
|
||||
|
||||
@@ -117,9 +117,18 @@ const ProductionOrders = () => {
|
||||
const [searchTerm, setSearchTerm] = useState('');
|
||||
const [statusFilter, setStatusFilter] = useState<ProductionOrderStatusTab>('all');
|
||||
const [summary, setSummary] = useState<ProductionOrderSummary>(emptySummary);
|
||||
const [supplyLots, setSupplyLots] = useState<SupplyLot[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [isSupplyBusy, setIsSupplyBusy] = useState(false);
|
||||
const [supplyMessage, setSupplyMessage] = useState('');
|
||||
const [currentPage, setCurrentPage] = useState(1);
|
||||
const [itemsPerPage, setItemsPerPage] = useState(20);
|
||||
const [exitForm, setExitForm] = useState({
|
||||
orderId: '',
|
||||
lotId: '',
|
||||
quantity: '',
|
||||
reason: '',
|
||||
});
|
||||
|
||||
const loadProductionOrders = useCallback(async (options?: { force?: boolean }) => {
|
||||
setIsLoading(true);
|
||||
@@ -133,9 +142,13 @@ const ProductionOrders = () => {
|
||||
|
||||
const load = async () => {
|
||||
setIsLoading(true);
|
||||
const nextSummary = await fetchProductionOrders(dateRange, { search: searchTerm });
|
||||
const [nextSummary, nextSupplySummary] = await Promise.all([
|
||||
fetchProductionOrders(dateRange, { search: searchTerm }),
|
||||
fetchSupplySummary()
|
||||
]);
|
||||
if (isMounted) {
|
||||
setSummary(nextSummary);
|
||||
setSupplyLots(nextSupplySummary.lots);
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
@@ -177,6 +190,38 @@ const ProductionOrders = () => {
|
||||
void loadProductionOrders({ force: true });
|
||||
};
|
||||
|
||||
const refreshSupplyLots = async () => {
|
||||
const supplySummary = await fetchSupplySummary();
|
||||
setSupplyLots(supplySummary.lots);
|
||||
};
|
||||
|
||||
const selectedOrder = summary.orders.find(order => `${order.id}` === exitForm.orderId);
|
||||
const selectedLot = supplyLots.find(lot => `${lot.id}` === exitForm.lotId);
|
||||
|
||||
const handleProductionExit = async (event: FormEvent<HTMLFormElement>) => {
|
||||
event.preventDefault();
|
||||
const lotId = Number(exitForm.lotId);
|
||||
const quantity = Number(exitForm.quantity.replace(',', '.'));
|
||||
if (!lotId || !selectedOrder || !Number.isFinite(quantity) || quantity <= 0) return;
|
||||
|
||||
setIsSupplyBusy(true);
|
||||
setSupplyMessage('');
|
||||
try {
|
||||
await consumeSupplyLotForProduction(lotId, {
|
||||
quantity,
|
||||
productionOrderNumber: selectedOrder.number || `${selectedOrder.id}`,
|
||||
reason: exitForm.reason.trim() || selectedOrder.productDescription,
|
||||
});
|
||||
await refreshSupplyLots();
|
||||
setExitForm({ orderId: '', lotId: '', quantity: '', reason: '' });
|
||||
setSupplyMessage('Saída de material registrada no estoque.');
|
||||
} catch (error) {
|
||||
setSupplyMessage(error instanceof Error ? error.message : 'Não foi possível baixar o material.');
|
||||
} finally {
|
||||
setIsSupplyBusy(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleExport = () => {
|
||||
const exportData = filteredOrders.map(order => ({
|
||||
'Numero': order.number,
|
||||
@@ -274,6 +319,84 @@ const ProductionOrders = () => {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<form onSubmit={handleProductionExit} className="rounded-2xl border border-dark-border bg-dark-card p-5 shadow-sm">
|
||||
<div className="flex flex-col gap-2 lg:flex-row lg:items-start lg:justify-between">
|
||||
<div>
|
||||
<h2 className="text-base font-bold text-dark-text">Baixa de material da OP</h2>
|
||||
<p className="mt-1 text-sm font-semibold text-dark-muted">Consome um lote do estoque e registra a saída nas movimentações.</p>
|
||||
</div>
|
||||
{selectedLot && (
|
||||
<span className="w-fit rounded-full border border-dark-border bg-dark-input px-2.5 py-1 text-xs font-bold text-dark-muted">
|
||||
Saldo lote #{selectedLot.id}: {formatQuantity(selectedLot.quantity)} {selectedLot.unit}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="mt-4 grid grid-cols-1 gap-3 xl:grid-cols-[1.4fr_1.4fr_120px_1fr_auto]">
|
||||
<label className="text-xs font-bold text-dark-muted">
|
||||
Ordem de produção
|
||||
<select
|
||||
value={exitForm.orderId}
|
||||
onChange={(event) => setExitForm(current => ({ ...current, orderId: event.target.value }))}
|
||||
className="mt-1 h-10 w-full rounded-lg border border-dark-border bg-dark-input px-3 text-sm font-semibold text-dark-text outline-none focus:border-brand-primary"
|
||||
>
|
||||
<option value="">Selecione...</option>
|
||||
{summary.orders.filter(order => order.status !== 'finished' && order.status !== 'canceled').map(order => (
|
||||
<option key={order.id} value={order.id}>
|
||||
{order.number || `#${order.id}`} · {order.productDescription}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
<label className="text-xs font-bold text-dark-muted">
|
||||
Lote de material
|
||||
<select
|
||||
value={exitForm.lotId}
|
||||
onChange={(event) => setExitForm(current => ({ ...current, lotId: event.target.value }))}
|
||||
className="mt-1 h-10 w-full rounded-lg border border-dark-border bg-dark-input px-3 text-sm font-semibold text-dark-text outline-none focus:border-brand-primary"
|
||||
>
|
||||
<option value="">Selecione...</option>
|
||||
{supplyLots.map(lot => (
|
||||
<option key={lot.id} value={lot.id}>
|
||||
#{lot.id} · {lot.product} · {formatQuantity(lot.quantity)} {lot.unit}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
<label className="text-xs font-bold text-dark-muted">
|
||||
Quantidade
|
||||
<input
|
||||
inputMode="decimal"
|
||||
value={exitForm.quantity}
|
||||
onChange={(event) => setExitForm(current => ({ ...current, quantity: event.target.value }))}
|
||||
className="mt-1 h-10 w-full rounded-lg border border-dark-border bg-dark-input px-3 text-sm font-semibold text-dark-text outline-none placeholder:text-dark-muted focus:border-brand-primary"
|
||||
placeholder="kg"
|
||||
/>
|
||||
</label>
|
||||
<label className="text-xs font-bold text-dark-muted">
|
||||
Motivo
|
||||
<input
|
||||
value={exitForm.reason}
|
||||
onChange={(event) => setExitForm(current => ({ ...current, reason: event.target.value }))}
|
||||
className="mt-1 h-10 w-full rounded-lg border border-dark-border bg-dark-input px-3 text-sm font-semibold text-dark-text outline-none placeholder:text-dark-muted focus:border-brand-primary"
|
||||
placeholder="ex: corte do pedido"
|
||||
/>
|
||||
</label>
|
||||
<button
|
||||
type="submit"
|
||||
disabled={isSupplyBusy || !supplyLots.length}
|
||||
className="mt-5 inline-flex h-10 items-center justify-center gap-2 rounded-lg border border-dark-border bg-dark-input px-3 text-sm font-bold text-dark-text transition-colors hover:border-brand-primary disabled:cursor-not-allowed disabled:opacity-50"
|
||||
>
|
||||
<PackageCheck className="h-4 w-4 text-brand-primary" />
|
||||
Baixar
|
||||
</button>
|
||||
</div>
|
||||
{supplyMessage && (
|
||||
<div className="mt-3 rounded-lg border border-dark-border bg-dark-input px-3 py-2 text-sm font-bold text-dark-muted">
|
||||
{supplyMessage}
|
||||
</div>
|
||||
)}
|
||||
</form>
|
||||
|
||||
<div className="overflow-hidden rounded-2xl border border-dark-border bg-dark-card shadow-sm">
|
||||
<div className="border-b border-dark-border p-5">
|
||||
<div className="flex flex-col gap-4 xl:flex-row xl:items-center xl:justify-between">
|
||||
|
||||
Reference in New Issue
Block a user