Files
compras/resources/js/components/budget-reinforcements.tsx
T
nala eb32c74c56
tests / ci (push) Waiting to run
commit compras1 2811
2026-11-28 07:11:35 +02:00

143 lines
5.4 KiB
TypeScript

import { router, useForm } from '@inertiajs/react';
import { useBudgetPreparationIdentity } from '@/hooks/use-budget-preparation-identity';
import { usePreparationDirty } from '@/hooks/use-preparation-dirty';
import { hasPreparationChanges } from '@/lib/requisition-history';
import { FormErrors, controlClass } from '@/components/procurement';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
export function ReinforcementProposal({ budgetId }: { budgetId: number }) {
const identity = `${useBudgetPreparationIdentity()}:reinforcement:${budgetId}`;
return (
<ProposalFields
key={identity}
budgetId={budgetId}
identity={identity}
/>
);
}
function ProposalFields({
budgetId,
identity,
}: {
budgetId: number;
identity: string;
}) {
const initial = { reference: '', amount: '', reason: '' };
const form = useForm(identity, initial);
const dirty = hasPreparationChanges(initial, form.data);
const ref = usePreparationDirty(dirty);
return (
<details className="mt-4 rounded-lg border p-4">
<summary className="cursor-pointer font-semibold">
Propor reforço desta dotação
</summary>
<form
ref={ref}
data-preparation-dirty={dirty ? 'true' : 'false'}
className="mt-4 grid gap-3"
onSubmit={(e) => {
e.preventDefault();
form.post(`/orcamentos/${budgetId}/reforcos`, {
onSuccess: () => {
form.setData(initial);
form.clearErrors();
router.remember(initial, `${identity}:data`);
router.remember({}, `${identity}:errors`);
},
});
}}
>
<FormErrors errors={form.errors} />
<p className="text-sm">
Aumenta o saldo apenas depois da aprovação por outra pessoa.
Mantém a moeda e a base de IVA da dotação.
</p>
<label>
Referência interna única na empresa
<Input
required
maxLength={100}
value={form.data.reference}
onChange={(e) =>
form.setData('reference', e.target.value)
}
/>
</label>
<label>
Montante do reforço (MZN)
<Input
required
inputMode="decimal"
value={form.data.amount}
onChange={(e) => form.setData('amount', e.target.value)}
/>
</label>
<label>
Fundamento
<textarea
required
className="w-full rounded border p-3"
value={form.data.reason}
onChange={(e) => form.setData('reason', e.target.value)}
/>
</label>
<Button disabled={form.processing}>
Enviar para aprovação independente
</Button>
</form>
</details>
);
}
export function ReinforcementReview({ id }: { id: number }) {
const identity = `${useBudgetPreparationIdentity()}:reinforcement-review:${id}`;
return <ReviewFields key={identity} id={id} identity={identity} />;
}
function ReviewFields({ id, identity }: { id: number; identity: string }) {
const initial = { decision: 'approved', reason: '' };
const form = useForm(identity, initial);
const dirty = hasPreparationChanges(initial, form.data);
const ref = usePreparationDirty(dirty);
return (
<form
ref={ref}
data-preparation-dirty={dirty ? 'true' : 'false'}
className="mt-4 grid gap-3"
onSubmit={(e) => {
e.preventDefault();
form.post(`/reforcos/${id}/decidir`, {
onSuccess: () => {
form.setData(initial);
form.clearErrors();
router.remember(initial, `${identity}:data`);
router.remember({}, `${identity}:errors`);
},
});
}}
>
<FormErrors errors={form.errors} />
<label>
Decisão
<select
className={controlClass}
value={form.data.decision}
onChange={(e) => form.setData('decision', e.target.value)}
>
<option value="approved">Aprovar reforço</option>
<option value="rejected">Rejeitar reforço</option>
</select>
</label>
<label>
Fundamento da decisão
<textarea
required
className="w-full rounded border p-3"
value={form.data.reason}
onChange={(e) => form.setData('reason', e.target.value)}
/>
</label>
<Button disabled={form.processing}>Guardar decisão imutável</Button>
</form>
);
}