import { Head, Link, router, useForm } from '@inertiajs/react'; import { useEffect, useRef } from 'react'; import { FormErrors, PageTitle, Panel, controlClass, } from '@/components/procurement'; import { Button } from '@/components/ui/button'; import { exactMinorAmount } from '@/components/procurement-payable-insights'; type Account = { id: number; code: string; label: string; currency: string; source_hash: string; }; type Allocation = { account: Account; subtotal_minor: string; total_minor: string; }; type Version = { id: number; version: number; reason: string; data: { requisition_item_id: number; allocations: Allocation[] }; }; type Page = { data: T[]; prev_page_url: string | null; next_page_url: string | null; }; type Props = { requisition: { id: number; number: string; status: string }; items: { id: number; description: string; subtotal_minor: string; total_minor: string; basis_hash: string; }[]; current: Version[]; history: Page; accounts: Page | null; canWrite: boolean; search: string; actorId: number; tenantId: number; }; type InputAllocation = { account_id: number; source_hash: string; label: string; subtotal: string; total: string; }; export default function RequisitionAccounts(props: Props) { const url = `/requisicoes/${props.requisition.id}/contas`; const form = useForm( `req-accounts:${props.tenantId}:${props.actorId}:${props.requisition.id}`, { item_id: '', basis_hash: '', version: 0, reason: '', operation_key: '', allocations: [] as InputAllocation[], }, ); const ref = useRef(null); useEffect(() => { const node = ref.current; window.dispatchEvent( new CustomEvent('sgc:context-dirty', { detail: { form: node, dirty: form.isDirty }, }), ); return () => { window.dispatchEvent( new CustomEvent('sgc:context-dirty', { detail: { form: node, dirty: false }, }), ); }; }, [form.isDirty]); const selected = props.items.find( (line) => String(line.id) === form.data.item_id, ); const current = props.current.find( (row) => String(row.data.requisition_item_id) === form.data.item_id, ); const choose = (id: string) => { if ( form.isDirty && !window.confirm('Descartar a preparação desta linha e abrir outra?') ) return; const line = props.items.find((row) => String(row.id) === id); const old = props.current.find( (row) => String(row.data.requisition_item_id) === id, ); form.setData({ item_id: id, basis_hash: line?.basis_hash ?? '', version: old?.version ?? 0, reason: '', operation_key: '', allocations: [], }); }; return ( <>
Voltar à requisição {props.canWrite ? (
{ event.preventDefault(); const data = { ...form.data, operation_key: form.data.operation_key || crypto.randomUUID(), }; form.setData(data); form.transform(() => data); form.post(url, { preserveScroll: true, onSuccess: () => { const next = { ...data, version: data.version + 1, operation_key: '', }; form.setData(next); form.setDefaults(next); }, }); }} > {selected && (

Base sem IVA:{' '} {exactMinorAmount( selected.subtotal_minor, 'MZN', )}{' '} · Total:{' '} {exactMinorAmount( selected.total_minor, 'MZN', )}

)} {current && (

Versão actual: {current.version}

)} {form.data.allocations.map((row, index) => (
{row.label} {(['subtotal', 'total'] as const).map( (field) => ( ), )}
))}