import { Head, Link, useForm, usePage } from '@inertiajs/react'; import { useEffect, useRef } from 'react'; import { FormErrors, PageTitle, Panel } from '@/components/procurement'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { money } from '@/types/procurement'; type Source = { origin: number; version: number; plan_hash: string; allocations: { allocation_key: string; line_id: number; centre_id: number; subtotal: string; total: string; }[]; }; export default function Amendment({ order, sources, history, }: { order: { id: number; number: string; currency: string }; sources: Source[]; history: { id: number; created_at: string; created_by: number; reason: string | null; sources: { origin: number; allocations: { allocation_key: string; subtotal_minor: number; total_minor: number; }[]; }[]; }[]; }) { const actor = usePage().props.auth?.user; const form = useForm( `order-parts-amendment:${actor?.tenant_id}:${actor?.id}:${order.id}:${sources.map((s) => `${s.origin}:${s.version}:${s.plan_hash}`).join(',')}`, { operation_key: crypto.randomUUID(), reason: '', sources: sources.map((source) => ({ origin: source.origin, version: source.version, plan_hash: source.plan_hash, allocations: source.allocations.map( ({ allocation_key, subtotal, total }) => ({ allocation_key, subtotal, total, }), ), })), }, ); const ref = useRef(null); useEffect(() => { const current = ref.current; window.dispatchEvent( new CustomEvent('sgc:context-dirty', { detail: { form: current, dirty: form.isDirty }, }), ); return () => { window.dispatchEvent( new CustomEvent('sgc:context-dirty', { detail: { form: current, dirty: false }, }), ); }; }, [form.isDirty]); const update = ( origin: number, allocation: number, field: 'subtotal' | 'total', value: string, ) => { form.setData( 'sources', form.data.sources.map((source, i) => i !== origin ? source : { ...source, allocations: source.allocations.map((row, j) => j !== allocation ? row : { ...row, [field]: value }, ), }, ), ); }; return ( <>

Conserve os montantes de cada artigo e origem. Apenas os centros já previstos podem ser reafectados. Valores consumidos, reservas não emitidas e documentos originais são preservados.

{ event.preventDefault(); form.post(`/ordens/${order.id}/reafectacao`); }} > {sources.map((source, i) => (
Origem #{source.origin} · Versão{' '} {source.version} {source.allocations.map((allocation, j) => (

Artigo #{allocation.line_id} · Centro # {allocation.centre_id}

))}
))}