183 lines
7.0 KiB
TypeScript
183 lines
7.0 KiB
TypeScript
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<HTMLFormElement>(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 (
|
|
<Panel
|
|
title="Reconciliar a estimativa anterior"
|
|
description="Seleccione a estimativa aprovada de uma origem desta ordem e a parcela que corresponde aos documentos conferidos. A mesma operação liberta essa parcela e aplica os custos efectivos; conserva o saldo para as restantes ordens. As facturas mantêm o seu principal nativo."
|
|
>
|
|
<form
|
|
ref={ref}
|
|
className="space-y-3"
|
|
onSubmit={(event) => {
|
|
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(),
|
|
});
|
|
}}
|
|
>
|
|
<FormErrors errors={form.errors} />
|
|
<label className="grid gap-1">
|
|
Estimativa da origem
|
|
<select
|
|
className={controlClass}
|
|
required
|
|
value={form.data.estimate_id}
|
|
onChange={(e) =>
|
|
form.setData('estimate_id', e.target.value)
|
|
}
|
|
>
|
|
<option value="">Seleccione</option>
|
|
{sources.data.map((source) => (
|
|
<option
|
|
key={source.id}
|
|
value={source.id}
|
|
disabled={
|
|
source.revision === null ||
|
|
(source.reserved_minor ?? 0) +
|
|
(source.committed_minor ?? 0) ===
|
|
0
|
|
}
|
|
>
|
|
Requisição {source.requisition_id} · versão{' '}
|
|
{source.version} · orçamento {source.budget_id}{' '}
|
|
· saldo{' '}
|
|
{source.reserved_minor === null ||
|
|
source.committed_minor === null
|
|
? 'indeterminado'
|
|
: estimateMoney(
|
|
source.reserved_minor +
|
|
source.committed_minor,
|
|
)}{' '}
|
|
MZN
|
|
</option>
|
|
))}
|
|
</select>
|
|
</label>
|
|
<label className="grid gap-1">
|
|
Parcela estimada a reconciliar (MZN)
|
|
<input
|
|
className={controlClass}
|
|
required
|
|
inputMode="decimal"
|
|
value={form.data.amount}
|
|
onChange={(e) => form.setData('amount', e.target.value)}
|
|
/>
|
|
</label>
|
|
<label className="grid gap-1">
|
|
Motivo e diferença face aos custos efectivos
|
|
<textarea
|
|
className={controlClass}
|
|
required
|
|
minLength={10}
|
|
maxLength={4000}
|
|
value={form.data.reason}
|
|
onChange={(e) => form.setData('reason', e.target.value)}
|
|
/>
|
|
</label>
|
|
<Button disabled={form.processing || !selected}>
|
|
Reconciliar com os documentos conferidos
|
|
</Button>
|
|
</form>
|
|
<nav className="mt-3 flex gap-4">
|
|
{sources.prev_page_url && (
|
|
<Link preserveState href={sources.prev_page_url}>
|
|
Anterior
|
|
</Link>
|
|
)}
|
|
{sources.next_page_url && (
|
|
<Link preserveState href={sources.next_page_url}>
|
|
Seguinte
|
|
</Link>
|
|
)}
|
|
</nav>
|
|
</Panel>
|
|
);
|
|
}
|