@@ -1,4 +1,7 @@
|
||||
import { Head, Link, useForm } from '@inertiajs/react';
|
||||
import { Head, Link, router, useForm, usePage } from '@inertiajs/react';
|
||||
import { usePreparationDirty } from '@/hooks/use-preparation-dirty';
|
||||
import { hasPreparationChanges } from '@/lib/requisition-history';
|
||||
import type { WorkContextState } from '@/types/work-context';
|
||||
import { PageTitle, Panel } from '@/components/procurement';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
@@ -42,19 +45,53 @@ const states: Record<string, string> = {
|
||||
cancelled: 'Cancelado',
|
||||
};
|
||||
|
||||
function ReasonForm({
|
||||
function useControlIdentity() {
|
||||
const page = usePage().props;
|
||||
const actor = page.auth?.user;
|
||||
const actorId = typeof page.actorId === 'number' ? page.actorId : actor?.id;
|
||||
const context = page.workContext as WorkContextState | null | undefined;
|
||||
const permissions = Array.isArray(page.permissions)
|
||||
? page.permissions
|
||||
.filter((p) => typeof p === 'string')
|
||||
.sort()
|
||||
.join(',')
|
||||
: '';
|
||||
return `purchase-controls:${actor?.tenant_id}:${actorId}:${context?.active ?? 'legacy'}:${permissions}`;
|
||||
}
|
||||
|
||||
function ReasonForm(input: { url: string; decisions?: boolean }) {
|
||||
const identity = `${useControlIdentity()}:reason:${input.url}:${input.decisions ?? false}`;
|
||||
return <ReasonFields key={identity} {...input} identity={identity} />;
|
||||
}
|
||||
|
||||
function ReasonFields({
|
||||
url,
|
||||
decisions = false,
|
||||
identity,
|
||||
}: {
|
||||
url: string;
|
||||
decisions?: boolean;
|
||||
identity: string;
|
||||
}) {
|
||||
const form = useForm({ reason: '', decision: 'approved' });
|
||||
const initial = { reason: '', decision: 'approved' };
|
||||
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'}
|
||||
onSubmit={(event) => {
|
||||
event.preventDefault();
|
||||
form.post(url, { preserveScroll: true });
|
||||
form.post(url, {
|
||||
preserveScroll: true,
|
||||
onSuccess: () => {
|
||||
form.setData(initial);
|
||||
form.clearErrors();
|
||||
router.remember(initial, `${identity}:data`);
|
||||
router.remember({}, `${identity}:errors`);
|
||||
},
|
||||
});
|
||||
}}
|
||||
className="mt-3 space-y-3"
|
||||
>
|
||||
@@ -98,20 +135,48 @@ function ReasonForm({
|
||||
}
|
||||
|
||||
function PolicyForm({ previousId }: { previousId: number }) {
|
||||
const form = useForm({
|
||||
const identity = `${useControlIdentity()}:policy:${previousId}`;
|
||||
return (
|
||||
<PolicyFields
|
||||
key={identity}
|
||||
previousId={previousId}
|
||||
identity={identity}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function PolicyFields({
|
||||
previousId,
|
||||
identity,
|
||||
}: {
|
||||
previousId: number;
|
||||
identity: string;
|
||||
}) {
|
||||
const initial = {
|
||||
previous_id: previousId,
|
||||
mode: 'unissued_award',
|
||||
enabled: false,
|
||||
valid_from: '',
|
||||
valid_until: '',
|
||||
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'}
|
||||
onSubmit={(event) => {
|
||||
event.preventDefault();
|
||||
form.post('/revisao-compras/politicas', {
|
||||
preserveScroll: true,
|
||||
onSuccess: () => {
|
||||
form.setData(initial);
|
||||
form.clearErrors();
|
||||
router.remember(initial, `${identity}:data`);
|
||||
router.remember({}, `${identity}:errors`);
|
||||
},
|
||||
});
|
||||
}}
|
||||
className="grid gap-4 sm:grid-cols-2"
|
||||
@@ -170,12 +235,29 @@ function PolicyForm({ previousId }: { previousId: number }) {
|
||||
}
|
||||
|
||||
function RevisionForm({ id }: { id: number }) {
|
||||
const form = useForm({ needed_by: '', justification: '' });
|
||||
const identity = `${useControlIdentity()}:revision:${id}`;
|
||||
return <RevisionFields key={identity} id={id} identity={identity} />;
|
||||
}
|
||||
|
||||
function RevisionFields({ id, identity }: { id: number; identity: string }) {
|
||||
const initial = { needed_by: '', justification: '' };
|
||||
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'}
|
||||
onSubmit={(event) => {
|
||||
event.preventDefault();
|
||||
form.post(`/cancelamentos/${id}/revisao`);
|
||||
form.post(`/cancelamentos/${id}/revisao`, {
|
||||
onSuccess: () => {
|
||||
form.setData(initial);
|
||||
form.clearErrors();
|
||||
router.remember(initial, `${identity}:data`);
|
||||
router.remember({}, `${identity}:errors`);
|
||||
},
|
||||
});
|
||||
}}
|
||||
className="mt-3 grid gap-3"
|
||||
>
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import { Head, Link, useForm } from '@inertiajs/react';
|
||||
import { Head, Link, router, useForm, usePage } from '@inertiajs/react';
|
||||
import { usePreparationDirty } from '@/hooks/use-preparation-dirty';
|
||||
import { hasPreparationChanges } from '@/lib/requisition-history';
|
||||
import type { WorkContextState } from '@/types/work-context';
|
||||
import {
|
||||
Panel,
|
||||
PageTitle,
|
||||
@@ -31,14 +34,38 @@ type Policy = {
|
||||
};
|
||||
|
||||
function Decision({ url }: { url: string }) {
|
||||
const form = useForm({ decision: 'rejected', reason: '' });
|
||||
const identity = `${useOrderControlIdentity()}:decision:${url}`;
|
||||
return <DecisionFields key={identity} url={url} identity={identity} />;
|
||||
}
|
||||
|
||||
function useOrderControlIdentity() {
|
||||
const page = usePage().props;
|
||||
const actor = page.auth?.user;
|
||||
const context = page.workContext as WorkContextState | null | undefined;
|
||||
return `order-cancellations:${actor?.tenant_id}:${actor?.id}:${context?.active ?? 'legacy'}:${page.canAdminister === true}`;
|
||||
}
|
||||
|
||||
function DecisionFields({ url, identity }: { url: string; identity: string }) {
|
||||
const initial = { decision: 'rejected', 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-3 space-y-3"
|
||||
onSubmit={(event) => {
|
||||
event.preventDefault();
|
||||
if (window.confirm('Confirmar esta decisão definitiva?'))
|
||||
form.post(url);
|
||||
form.post(url, {
|
||||
onSuccess: () => {
|
||||
form.setData(initial);
|
||||
form.clearErrors();
|
||||
router.remember(initial, `${identity}:data`);
|
||||
router.remember({}, `${identity}:errors`);
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
<FormErrors errors={form.errors} />
|
||||
@@ -74,13 +101,38 @@ function Decision({ url }: { url: string }) {
|
||||
}
|
||||
|
||||
function Revision({ record }: { record: Record }) {
|
||||
const form = useForm({ needed_by: '', reason: '' });
|
||||
const identity = `${useOrderControlIdentity()}:revision:${record.id}:${record.status}:${record.revised_requisition_id ?? 0}`;
|
||||
return (
|
||||
<RevisionFields key={identity} record={record} identity={identity} />
|
||||
);
|
||||
}
|
||||
|
||||
function RevisionFields({
|
||||
record,
|
||||
identity,
|
||||
}: {
|
||||
record: Record;
|
||||
identity: string;
|
||||
}) {
|
||||
const initial = { needed_by: '', 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-3 space-y-3"
|
||||
onSubmit={(event) => {
|
||||
event.preventDefault();
|
||||
form.post(`/ordens/cancelamentos/${record.id}/rever`);
|
||||
form.post(`/ordens/cancelamentos/${record.id}/rever`, {
|
||||
onSuccess: () => {
|
||||
form.setData(initial);
|
||||
form.clearErrors();
|
||||
router.remember(initial, `${identity}:data`);
|
||||
router.remember({}, `${identity}:errors`);
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
<FormErrors errors={form.errors} />
|
||||
@@ -118,11 +170,7 @@ function Revision({ record }: { record: Record }) {
|
||||
);
|
||||
}
|
||||
|
||||
export default function OrderCancellations({
|
||||
records,
|
||||
policies,
|
||||
canAdminister,
|
||||
}: {
|
||||
type Props = {
|
||||
records: {
|
||||
data: Record[];
|
||||
prev_page_url: string | null;
|
||||
@@ -130,15 +178,37 @@ export default function OrderCancellations({
|
||||
};
|
||||
policies: Policy[];
|
||||
canAdminister: boolean;
|
||||
}) {
|
||||
const policy = useForm({
|
||||
};
|
||||
|
||||
export default function OrderCancellationsPage(props: Props) {
|
||||
const identity = useOrderControlIdentity();
|
||||
return (
|
||||
<OrderCancellations
|
||||
key={`${identity}:policy:${props.policies[0]?.id ?? 0}`}
|
||||
{...props}
|
||||
identity={identity}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function OrderCancellations({
|
||||
records,
|
||||
policies,
|
||||
canAdminister,
|
||||
identity,
|
||||
}: Props & { identity: string }) {
|
||||
const policyKey = `${identity}:policy:${policies[0]?.id ?? 0}`;
|
||||
const initial = {
|
||||
previous_id: policies[0]?.id ?? 0,
|
||||
mode: 'issued_order',
|
||||
enabled: false,
|
||||
valid_from: '',
|
||||
valid_until: '',
|
||||
reason: '',
|
||||
});
|
||||
};
|
||||
const policy = useForm(policyKey, initial);
|
||||
const dirty = hasPreparationChanges(initial, policy.data);
|
||||
const ref = usePreparationDirty(dirty, canAdminister);
|
||||
const labels: { [key: string]: string } = {
|
||||
pending: 'Pendente',
|
||||
approved: 'Aprovado',
|
||||
@@ -242,6 +312,10 @@ export default function OrderCancellations({
|
||||
<details className="mt-4">
|
||||
<summary>Propor nova versão</summary>
|
||||
<form
|
||||
ref={ref}
|
||||
data-preparation-dirty={
|
||||
dirty ? 'true' : 'false'
|
||||
}
|
||||
className="mt-3 space-y-3"
|
||||
onSubmit={(event) => {
|
||||
event.preventDefault();
|
||||
@@ -251,6 +325,20 @@ export default function OrderCancellations({
|
||||
}));
|
||||
policy.post(
|
||||
'/ordens/cancelamentos/politicas',
|
||||
{
|
||||
onSuccess: () => {
|
||||
policy.setData(initial);
|
||||
policy.clearErrors();
|
||||
router.remember(
|
||||
initial,
|
||||
`${policyKey}:data`,
|
||||
);
|
||||
router.remember(
|
||||
{},
|
||||
`${policyKey}:errors`,
|
||||
);
|
||||
},
|
||||
},
|
||||
);
|
||||
}}
|
||||
>
|
||||
@@ -325,7 +413,7 @@ export default function OrderCancellations({
|
||||
</>
|
||||
);
|
||||
}
|
||||
OrderCancellations.layout = {
|
||||
OrderCancellationsPage.layout = {
|
||||
breadcrumbs: [
|
||||
{ title: 'Ordens', href: '/ordens' },
|
||||
{ title: 'Cancelamentos', href: '/ordens/cancelamentos' },
|
||||
|
||||
Reference in New Issue
Block a user