@@ -1,6 +1,9 @@
|
||||
import { useForm } from '@inertiajs/react';
|
||||
import { useProcurementPreparationIdentity } from '@/hooks/use-procurement-preparation-identity';
|
||||
import { hasPreparationChanges } from '@/lib/requisition-history';
|
||||
import { router, useForm } from '@inertiajs/react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { FormErrors, Panel, controlClass } from '@/components/procurement';
|
||||
import { usePreparationDirty } from '@/hooks/use-preparation-dirty';
|
||||
|
||||
type SavedLevel = {
|
||||
name: string;
|
||||
@@ -33,14 +36,24 @@ const newLevel = () => ({
|
||||
cost_center_ids: [] as number[],
|
||||
});
|
||||
|
||||
export function RequisitionApprovalSettings({
|
||||
export function RequisitionApprovalSettings(
|
||||
props: Omit<Parameters<typeof ApprovalFields>[0], 'identity'>,
|
||||
) {
|
||||
const identity = useProcurementPreparationIdentity(
|
||||
`approval-settings:${props.version}:${props.plan?.revision ?? 0}:${props.ready}`,
|
||||
);
|
||||
return <ApprovalFields key={identity} {...props} identity={identity} />;
|
||||
}
|
||||
function ApprovalFields({
|
||||
plan,
|
||||
ready,
|
||||
profiles,
|
||||
costCenters,
|
||||
version,
|
||||
categoryOptions = {},
|
||||
identity,
|
||||
}: {
|
||||
identity: string;
|
||||
plan?: RequisitionApprovalPlan | null;
|
||||
ready: boolean;
|
||||
profiles: Profile[];
|
||||
@@ -48,7 +61,7 @@ export function RequisitionApprovalSettings({
|
||||
version: number;
|
||||
categoryOptions?: Record<string, string>;
|
||||
}) {
|
||||
const form = useForm({
|
||||
const initial = {
|
||||
version,
|
||||
enabled: plan?.enabled ?? false,
|
||||
reason: '',
|
||||
@@ -62,10 +75,13 @@ export function RequisitionApprovalSettings({
|
||||
procurement_categories: level.procurement_categories ?? [],
|
||||
cost_center_ids: level.cost_center_ids,
|
||||
})) ?? [],
|
||||
});
|
||||
};
|
||||
const form = useForm(identity, initial);
|
||||
const dirty = hasPreparationChanges(initial, form.data);
|
||||
const eligibleProfiles = profiles.filter((profile) =>
|
||||
profile.permissions.includes('requisition_approve'),
|
||||
);
|
||||
const preparationRef = usePreparationDirty(dirty, ready);
|
||||
const change = (
|
||||
index: number,
|
||||
values: Partial<ReturnType<typeof newLevel>>,
|
||||
@@ -88,6 +104,8 @@ export function RequisitionApprovalSettings({
|
||||
</p>
|
||||
) : (
|
||||
<form
|
||||
ref={preparationRef}
|
||||
data-preparation-dirty={dirty ? 'true' : 'false'}
|
||||
className="space-y-5"
|
||||
onSubmit={(event) => {
|
||||
event.preventDefault();
|
||||
@@ -111,7 +129,18 @@ export function RequisitionApprovalSettings({
|
||||
}));
|
||||
form.post(
|
||||
'/administracao/parametrizacao/approval_levels',
|
||||
{ preserveScroll: true },
|
||||
{
|
||||
preserveScroll: true,
|
||||
onSuccess: () => {
|
||||
form.setData(initial);
|
||||
form.clearErrors();
|
||||
router.remember(
|
||||
initial,
|
||||
`${identity}:data`,
|
||||
);
|
||||
router.remember({}, `${identity}:errors`);
|
||||
},
|
||||
},
|
||||
);
|
||||
}}
|
||||
>
|
||||
@@ -417,6 +446,37 @@ export function RequisitionApprovalSettings({
|
||||
>
|
||||
Acrescentar nível
|
||||
</Button>
|
||||
{categoryOptions.capital_equipment && (
|
||||
<div className="space-y-2 rounded border p-3">
|
||||
<p className="text-sm">
|
||||
Para a direcção da maquinaria, prepare um nível
|
||||
aplicável a qualquer valor e centro de custo.
|
||||
Escolha os perfis da empresa com competência de
|
||||
aprovação e confirme a ordem das etapas antes de
|
||||
guardar o plano.
|
||||
</p>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
disabled={form.data.levels.length >= 5}
|
||||
onClick={() =>
|
||||
form.setData('levels', [
|
||||
...form.data.levels,
|
||||
{
|
||||
...newLevel(),
|
||||
name: 'Direcção para maquinaria',
|
||||
scope: 'company',
|
||||
procurement_categories: [
|
||||
'capital_equipment',
|
||||
],
|
||||
},
|
||||
])
|
||||
}
|
||||
>
|
||||
Preparar nível de direcção para maquinaria
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
<label
|
||||
className="grid gap-2 text-sm"
|
||||
htmlFor="approval-plan-reason"
|
||||
|
||||
Reference in New Issue
Block a user