import { Head, Link, useForm, useRemember } from '@inertiajs/react'; import { useEffect, useRef, type RefObject } from 'react'; import { Button } from '@/components/ui/button'; import { FormErrors, PageTitle, Panel, controlClass, } from '@/components/procurement'; import { orderPartMinor } from '@/lib/order-parts'; import { money } from '@/types/procurement'; import ImportTariffSummary, { type FrozenImportTariff, } from '@/components/import-tariff-summary'; type Money = { subtotal_minor: number; tax_minor: number; total_minor: number; reference_subtotal_minor: number; reference_tax_minor: number; reference_total_minor: number; }; type Selection = { origin: string; kind: string; id: number; label?: string; currency?: string; }; type Component = Money & { component_key: string; kind: string; origin: string; }; type PlanData = { budget_id: number; documents: Selection[]; components: Component[]; item_references?: { component_key: string; purchase_order_item_ids: number[]; }[]; }; type OrderItem = { id: number; description: string; unit: string; quantity: string; }; type Plan = { item_references?: Record; document_details: Record< string, { reference: string; creditor: string; currency: string; financial_authority: 'native_invoice' | 'documentary_source_only'; exchange_basis: { rate: string; quoted_on: string; source: string; } | null; } >; components: Component[]; total_minor: number; budget_amount_minor: number; includes_tax: boolean; fingerprint: string; documents: Record< string, { creditor_key: string; currency: string; total_minor: number } >; }; type Version = { id: number; record_key: string; version: number; state: string; reason: string; data: PlanData; snapshot: { contributors: number[]; plan: Plan }; }; type Page = { data: T[]; prev_page_url: string | null; next_page_url: string | null; }; type Doc = { id: number; record_key: string; version: number; data: { issued_on: string; creditor_reference: string; exchange_basis: { rate: string; quoted_on: string; source: string; } | null; reference: string; kind: string; currency: string; subtotal_minor: number; tax_minor: number; total_minor: number; }; }; type Props = { importTariffs: Record; orderItems: OrderItem[]; financialEvents: Page<{ id: number; version: number; reason: string; created_by: number; data: { action: string; financial_plan_version: number }; snapshot: { common_result: { additional_amount_minor: number; reserved_delta_minor?: number; committed_delta_minor?: number; consumed_delta_minor?: number; }; actor?: { name: string }; }; }>; financialReady: boolean; financialState: { revision: number; native_plan_id: number | null; reserved_minor: number; committed_minor: number; consumed_minor: number; origins: { source_key: string; source_version: number; phase: string; documented_amount_minor: number; reserved_minor: number; committed_minor: number; consumed_minor: number; reference: string; evidence_url: string; }[]; version: number; plan_hash: string; phase: string; additional_amount_minor: number; total_reference_minor: number; } | null; importRecord: { id: number; record_key: string; context_changed: boolean; context: Record; version: number; order_id: number; order_number: string; }; documents: Page; invoices: Page<{ id: number; number: string; currency: string; purchase_order_id: number; total_minor: number; }>; budgets: Page<{ id: number; cost_center_id: number; kind: string; includes_tax: boolean; }>; history: Page; latest: Version | null; preview: Plan | null; actorId: number; tenantId: number; }; const fields = [ 'subtotal_minor', 'tax_minor', 'total_minor', 'reference_subtotal_minor', 'reference_tax_minor', 'reference_total_minor', ] as const; const labels = [ 'Base documental', 'Imposto documental', 'Total documental', 'Base em MZN', 'Imposto em MZN', 'Total em MZN', ]; const componentKinds = { goods: 'Mercadoria', freight: 'Frete', insurance: 'Seguro', duty: 'Direitos aduaneiros', ice: 'ICE', vat: 'IVA aduaneiro', other: 'Outro custo documentado', }; const financialActions: Record = { reserve: 'Reserva', commit: 'Compromisso', consume: 'Consumo', amend: 'Reafectação documentada', cancel: 'Libertação de saldo', }; const decimal = (value: number) => `${Math.floor(value / 100)}.${String(value % 100).padStart(2, '0')}`; type DraftComponent = { component_key: string; kind: string; origin: string; values: string[]; }; function useGuard(ref: RefObject, dirty: boolean) { useEffect(() => { const element = ref.current; window.dispatchEvent( new CustomEvent('sgc:context-dirty', { detail: { form: element, dirty }, }), ); return () => { window.dispatchEvent( new CustomEvent('sgc:context-dirty', { detail: { form: element, dirty: false }, }), ); }; }, [ref, dirty]); } function Pagination({ page, }: { page: { prev_page_url: string | null; next_page_url: string | null }; }) { return ( ); } function DocumentForm({ url, historyKey, documents, }: { url: string; historyKey: string; documents: Doc[]; }) { const form = useForm(historyKey, { operation_key: crypto.randomUUID(), record_key: null as string | null, version: 0, kind: 'customs_assessment', reference: '', issued_on: '', creditor_reference: '', currency: 'MZN', subtotal: '', tax: '', total: '', rate: '', quoted_on: '', source: '', reason: '', file: null as File | null, }).dontRemember('file'); const ref = useRef(null); useGuard(ref, form.isDirty); return (

Conserve a referência, o credor e os valores do documento. Este registo não cria uma factura de fornecedor nem executa pagamentos. Depois de recarregar, seleccione novamente o ficheiro.

{ event.preventDefault(); form.transform((data) => ({ operation_key: data.operation_key, record_key: data.record_key, version: data.version, kind: data.kind, reference: data.reference, issued_on: data.issued_on, creditor_reference: data.creditor_reference, currency: data.currency, subtotal_minor: orderPartMinor(data.subtotal), tax_minor: orderPartMinor(data.tax), total_minor: orderPartMinor(data.total), exchange_basis: data.currency === 'MZN' ? null : { rate: data.rate, quoted_on: data.quoted_on, source: data.source, }, reason: data.reason, file: data.file, })); form.post(`${url}/documentos`, { onSuccess: () => { form.reset(); form.setData('operation_key', crypto.randomUUID()); }, }); }} > {( [ 'reference', 'issued_on', 'creditor_reference', 'currency', 'subtotal', 'tax', 'total', ] as const ).map((field, i) => ( ))} {form.data.currency !== 'MZN' && (
Taxa explícita de Finanças, congelada neste documento {(['rate', 'quoted_on', 'source'] as const).map( (field, i) => ( ), )}
)}