@@ -14,6 +14,9 @@ import {
|
||||
} from '@/components/budget-reinforcements';
|
||||
import { InventoryPagination } from '@/components/inventory';
|
||||
import type { Page } from '@/types/inventory';
|
||||
import { useBudgetPreparationIdentity } from '@/hooks/use-budget-preparation-identity';
|
||||
import { usePreparationDirty } from '@/hooks/use-preparation-dirty';
|
||||
import { hasPreparationChanges } from '@/lib/requisition-history';
|
||||
|
||||
type Budget = {
|
||||
id: number;
|
||||
@@ -32,11 +35,20 @@ type Budget = {
|
||||
approved_total_minor: number;
|
||||
kind: string;
|
||||
};
|
||||
export default function Budgets({
|
||||
export default function Budgets(
|
||||
props: Omit<Parameters<typeof BudgetFields>[0], 'identity'>,
|
||||
) {
|
||||
const identity = useBudgetPreparationIdentity();
|
||||
return <BudgetFields key={identity} {...props} identity={identity} />;
|
||||
}
|
||||
|
||||
function BudgetFields({
|
||||
centers,
|
||||
budgets,
|
||||
reinforcements,
|
||||
identity,
|
||||
}: {
|
||||
identity: string;
|
||||
centers: { id: number; code: string; name: string }[];
|
||||
budgets: Budget[];
|
||||
reinforcements: Page<{
|
||||
@@ -52,14 +64,17 @@ export default function Budgets({
|
||||
}>;
|
||||
}) {
|
||||
const { auth } = usePage().props;
|
||||
const form = useForm({
|
||||
const initial = {
|
||||
cost_center_id: '',
|
||||
year: new Date().getFullYear(),
|
||||
amount: '',
|
||||
reason: '',
|
||||
includes_tax: '',
|
||||
kind: '',
|
||||
});
|
||||
};
|
||||
const form = useForm(`${identity}:allocation`, initial);
|
||||
const dirty = hasPreparationChanges(initial, form.data);
|
||||
const ref = usePreparationDirty(dirty);
|
||||
return (
|
||||
<>
|
||||
<Head title="Orçamentos" />
|
||||
@@ -82,6 +97,8 @@ export default function Budgets({
|
||||
description="Outra pessoa de Administração ou Finanças deve aprovar. A dotação inicial é preservada; reforços têm aprovação própria."
|
||||
>
|
||||
<form
|
||||
ref={ref}
|
||||
data-preparation-dirty={dirty ? 'true' : 'false'}
|
||||
className="grid gap-4"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
@@ -89,7 +106,20 @@ export default function Budgets({
|
||||
...data,
|
||||
includes_tax: data.includes_tax === '1',
|
||||
}));
|
||||
form.post('/orcamentos');
|
||||
form.post('/orcamentos', {
|
||||
onSuccess: () => {
|
||||
form.setData(initial);
|
||||
form.clearErrors();
|
||||
router.remember(
|
||||
initial,
|
||||
`${identity}:allocation:data`,
|
||||
);
|
||||
router.remember(
|
||||
{},
|
||||
`${identity}:allocation:errors`,
|
||||
);
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
<FormErrors errors={form.errors} />
|
||||
|
||||
Reference in New Issue
Block a user