import { Link, useForm } from '@inertiajs/react'; import { useEffect, useRef } from 'react'; import { FormErrors, Panel, controlClass } from '@/components/procurement'; import { Button } from '@/components/ui/button'; import { estimateMinor, estimateMoney } from '@/lib/import-estimate-money'; export type EstimateSources = { data: { id: number; version: number; requisition_id: number; budget_id: number; revision: number | null; reserved_minor: number | null; committed_minor: number | null; }[]; prev_page_url: string | null; next_page_url: string | null; }; export default function EstimateReconciliation({ sources, planId, url, historyKey, financialRevision, }: { sources: EstimateSources; planId: number; url: string; historyKey: string; financialRevision: number; }) { const form = useForm( `${historyKey}:estimate-reconciliation:${planId}:${financialRevision}`, { operation_key: '', actual_operation_key: '', estimate_id: '', amount: '', reason: '', }, ); 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]); if ( sources.data.length === 0 && !sources.prev_page_url && !sources.next_page_url ) return null; const selected = sources.data.find( (source) => String(source.id) === form.data.estimate_id, ); return (
{ event.preventDefault(); if (!selected) return; const operation = form.data.operation_key || crypto.randomUUID(); const actual = form.data.actual_operation_key || crypto.randomUUID(); form.setData((data) => ({ ...data, operation_key: operation, actual_operation_key: actual, })); form.transform((data) => ({ operation_key: operation, actual_operation_key: actual, action: 'reconcile_estimate', financial_revision: financialRevision, estimate_id: selected.id, estimate_revision: selected.revision, estimate_release_minor: estimateMinor(data.amount), sources: [], reason: data.reason, })); form.post(`${url}/${planId}/orcamento`, { preserveScroll: true, onSuccess: () => form.reset(), }); }} >