119 lines
5.2 KiB
TypeScript
119 lines
5.2 KiB
TypeScript
import { Link, router, useForm } from '@inertiajs/react';
|
|
import { useProcurementPreparationIdentity } from '@/hooks/use-procurement-preparation-identity';
|
|
import { usePreparationDirty } from '@/hooks/use-preparation-dirty';
|
|
import { hasPreparationChanges } from '@/lib/requisition-history';
|
|
import { Panel, FormErrors, controlClass } from '@/components/procurement';
|
|
import { Button } from '@/components/ui/button';
|
|
|
|
export type OrderCancellationState = {
|
|
enabled: boolean;
|
|
state: string;
|
|
canRequest: boolean;
|
|
};
|
|
|
|
type Props = { orderId: number; state: OrderCancellationState };
|
|
export default function OrderCancellationRequest(props: Props) {
|
|
const identity = useProcurementPreparationIdentity(
|
|
`order-cancellation:${props.orderId}:${props.state.enabled}:${props.state.state}:${props.state.canRequest}`,
|
|
);
|
|
return <CancellationFields key={identity} {...props} identity={identity} />;
|
|
}
|
|
function CancellationFields({
|
|
orderId,
|
|
state,
|
|
identity,
|
|
}: Props & { identity: string }) {
|
|
const initial = {
|
|
operation_key: crypto.randomUUID(),
|
|
reason: '',
|
|
evidence: '',
|
|
};
|
|
const form = useForm(identity, initial);
|
|
const dirty = hasPreparationChanges(initial, form.data);
|
|
const ref = usePreparationDirty(
|
|
dirty,
|
|
state.enabled && state.canRequest && state.state === 'issued',
|
|
);
|
|
if (!state.enabled && state.state === 'issued') return null;
|
|
return (
|
|
<Panel title="Estado operacional e cancelamento">
|
|
<p>
|
|
{state.state === 'cancelled'
|
|
? 'Ordem cancelada. O documento emitido e as suas aprovações são preservados.'
|
|
: state.state === 'cancellation_pending'
|
|
? 'Cancelamento pendente. Novas recepções e facturas estão bloqueadas.'
|
|
: 'Ordem emitida. Cancelamento sujeito a política própria publicada e decisão independente de Finanças.'}
|
|
</p>
|
|
{state.enabled && (
|
|
<Link
|
|
className="mt-3 block underline"
|
|
href="/ordens/cancelamentos"
|
|
>
|
|
Acompanhar pedidos e política de cancelamento
|
|
</Link>
|
|
)}
|
|
{state.enabled && state.canRequest && state.state === 'issued' && (
|
|
<details className="mt-4 rounded border p-3">
|
|
<summary>Pedir cancelamento integral</summary>
|
|
<form
|
|
ref={ref}
|
|
data-preparation-dirty={dirty ? 'true' : 'false'}
|
|
className="mt-4 space-y-4"
|
|
onSubmit={(event) => {
|
|
event.preventDefault();
|
|
form.post(`/ordens/${orderId}/cancelamento`, {
|
|
onSuccess: () => {
|
|
const next = {
|
|
...initial,
|
|
operation_key: crypto.randomUUID(),
|
|
};
|
|
form.setData(next);
|
|
form.clearErrors();
|
|
router.remember(next, `${identity}:data`);
|
|
router.remember({}, `${identity}:errors`);
|
|
},
|
|
});
|
|
}}
|
|
>
|
|
<FormErrors errors={form.errors} />
|
|
<p className="text-sm">
|
|
Só para ordens sem qualquer recepção, factura ou
|
|
pagamento. O pedido suspende nova execução até à
|
|
decisão.
|
|
</p>
|
|
<label className="block">
|
|
Fundamento
|
|
<textarea
|
|
className={controlClass}
|
|
minLength={10}
|
|
maxLength={4000}
|
|
required
|
|
value={form.data.reason}
|
|
onChange={(event) =>
|
|
form.setData('reason', event.target.value)
|
|
}
|
|
/>
|
|
</label>
|
|
<label className="block">
|
|
Referência e descrição da documentação de suporte
|
|
<textarea
|
|
className={controlClass}
|
|
minLength={10}
|
|
maxLength={4000}
|
|
required
|
|
value={form.data.evidence}
|
|
onChange={(event) =>
|
|
form.setData('evidence', event.target.value)
|
|
}
|
|
/>
|
|
</label>
|
|
<Button disabled={form.processing}>
|
|
Submeter pedido de cancelamento
|
|
</Button>
|
|
</form>
|
|
</details>
|
|
)}
|
|
</Panel>
|
|
);
|
|
}
|