@@ -1,5 +1,8 @@
|
||||
import { Head, Link, router, useForm } from '@inertiajs/react';
|
||||
import { Head, Link, router, useForm, usePage } from '@inertiajs/react';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { usePreparationDirty } from '@/hooks/use-preparation-dirty';
|
||||
import { hasPreparationChanges } from '@/lib/requisition-history';
|
||||
import type { WorkContextState } from '@/types/work-context';
|
||||
import {
|
||||
FormErrors,
|
||||
PageTitle,
|
||||
@@ -110,7 +113,33 @@ const quantity = (value: number) =>
|
||||
(value / 1000).toLocaleString('pt-MZ', { maximumFractionDigits: 3 });
|
||||
|
||||
function Prepare({ props, old }: { props: Props; old?: Suggestion }) {
|
||||
const [selected, setSelected] = useState(old?.data.policy_key ?? '');
|
||||
const identity = `${useSuggestionIdentity(props, old)}:prepare`;
|
||||
return (
|
||||
<PrepareForm
|
||||
key={identity}
|
||||
props={props}
|
||||
old={old}
|
||||
identity={identity}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function useSuggestionIdentity(props: Props, record?: Suggestion) {
|
||||
const page = usePage().props;
|
||||
const actor = page.auth?.user;
|
||||
const context = page.workContext as WorkContextState | null | undefined;
|
||||
return `supply-suggestion:${actor?.tenant_id}:${props.actorId ?? actor?.id}:${props.ready}:${props.canPrepare}:${props.canReview}:${props.canConvert}:${record?.record_key ?? 'new'}:${record?.version ?? 0}:${record?.state ?? 'new'}${context?.active ? `:context:${context.active}` : ''}`;
|
||||
}
|
||||
|
||||
function PrepareForm({
|
||||
props,
|
||||
old,
|
||||
identity,
|
||||
}: {
|
||||
props: Props;
|
||||
old?: Suggestion;
|
||||
identity: string;
|
||||
}) {
|
||||
const [policies, setPolicies] = useState(props.policies);
|
||||
const [policySearch, setPolicySearch] = useState(props.policySearch ?? '');
|
||||
const formKey = `suggestion:${old?.id ?? 'new'}`;
|
||||
@@ -118,8 +147,8 @@ function Prepare({ props, old }: { props: Props; old?: Suggestion }) {
|
||||
if (!props.policyFormKey || props.policyFormKey === formKey)
|
||||
setPolicies(props.policies);
|
||||
}, [props.policyFormKey, props.policies, formKey]);
|
||||
const option = policies.find((p) => p.key === selected);
|
||||
const form = useForm({
|
||||
const originalOption = policies.find((p) => p.key === old?.data.policy_key);
|
||||
const initial = {
|
||||
operation_key: crypto.randomUUID(),
|
||||
record_key: old?.record_key,
|
||||
version: old?.version ?? 0,
|
||||
@@ -127,14 +156,20 @@ function Prepare({ props, old }: { props: Props; old?: Suggestion }) {
|
||||
reason: '',
|
||||
data: {
|
||||
policy_key: old?.data.policy_key ?? '',
|
||||
unit_price: option?.unit_price ?? old?.data.unit_price ?? '',
|
||||
tax_rate: option?.tax_rate ?? old?.data.tax_rate ?? '',
|
||||
unit_price:
|
||||
originalOption?.unit_price ?? old?.data.unit_price ?? '',
|
||||
tax_rate: originalOption?.tax_rate ?? old?.data.tax_rate ?? '',
|
||||
quantity: old?.data.manual_plan
|
||||
? String(old.data.quantity_milli / 1000)
|
||||
: '',
|
||||
delivery_on: old?.data.manual_plan ? old.data.delivery_on : '',
|
||||
},
|
||||
});
|
||||
};
|
||||
const form = useForm(identity, initial);
|
||||
const selected = form.data.data.policy_key;
|
||||
const option = policies.find((p) => p.key === selected);
|
||||
const dirty = hasPreparationChanges(initial, form.data);
|
||||
const ref = usePreparationDirty(dirty);
|
||||
const searchPolicies = () =>
|
||||
router.get(
|
||||
'/compras/fornecimento',
|
||||
@@ -156,6 +191,8 @@ function Prepare({ props, old }: { props: Props; old?: Suggestion }) {
|
||||
);
|
||||
return (
|
||||
<form
|
||||
ref={ref}
|
||||
data-preparation-dirty={dirty ? 'true' : 'false'}
|
||||
className="mt-4 space-y-4"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
@@ -176,6 +213,16 @@ function Prepare({ props, old }: { props: Props; old?: Suggestion }) {
|
||||
}));
|
||||
form.post('/compras/fornecimento/suggestion', {
|
||||
preserveScroll: true,
|
||||
onSuccess: () => {
|
||||
const clean = {
|
||||
...initial,
|
||||
operation_key: crypto.randomUUID(),
|
||||
};
|
||||
form.setData(clean);
|
||||
form.clearErrors();
|
||||
router.remember(clean, `${identity}:data`);
|
||||
router.remember({}, `${identity}:errors`);
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
@@ -227,7 +274,6 @@ function Prepare({ props, old }: { props: Props; old?: Suggestion }) {
|
||||
const p = policies.find(
|
||||
(o) => o.key === e.target.value,
|
||||
);
|
||||
setSelected(e.target.value);
|
||||
form.setData('data', {
|
||||
policy_key: e.target.value,
|
||||
unit_price: p?.unit_price ?? '',
|
||||
@@ -249,6 +295,11 @@ function Prepare({ props, old }: { props: Props; old?: Suggestion }) {
|
||||
indisponível
|
||||
</option>
|
||||
)}
|
||||
{!old && selected && !option && (
|
||||
<option value={selected}>
|
||||
Política seleccionada — procurar para conferir
|
||||
</option>
|
||||
)}
|
||||
</select>
|
||||
</label>
|
||||
{option?.issue && (
|
||||
@@ -522,14 +573,37 @@ function Facts({
|
||||
}
|
||||
|
||||
function Decisions({ props, record: r }: { props: Props; record: Suggestion }) {
|
||||
const form = useForm({
|
||||
const identity = `${useSuggestionIdentity(props, r)}:decision:${r.canApprove}:${r.canReturn}:${r.canConvert}:${r.canCancel}:${r.canClose}:${props.excess[r.record_key] ?? false}`;
|
||||
return (
|
||||
<DecisionsForm
|
||||
key={identity}
|
||||
props={props}
|
||||
record={r}
|
||||
identity={identity}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function DecisionsForm({
|
||||
props,
|
||||
record: r,
|
||||
identity,
|
||||
}: {
|
||||
props: Props;
|
||||
record: Suggestion;
|
||||
identity: string;
|
||||
}) {
|
||||
const initial = {
|
||||
operation_key: crypto.randomUUID(),
|
||||
record_key: r.record_key,
|
||||
version: r.version,
|
||||
action: '',
|
||||
reason: '',
|
||||
data: { cost_center_id: '', confirm_exception: false },
|
||||
});
|
||||
};
|
||||
const form = useForm(identity, initial);
|
||||
const dirty = hasPreparationChanges(initial, form.data);
|
||||
const ref = usePreparationDirty(dirty, Boolean(form.data.action));
|
||||
const choices: [string, string][] = [];
|
||||
if (r.canApprove && !props.excess[r.record_key])
|
||||
choices.push(['approve', 'Aprovar para requisitar']);
|
||||
@@ -574,11 +648,23 @@ function Decisions({ props, record: r }: { props: Props; record: Suggestion }) {
|
||||
)}
|
||||
{form.data.action && (
|
||||
<form
|
||||
ref={ref}
|
||||
data-preparation-dirty={dirty ? 'true' : 'false'}
|
||||
className="space-y-3"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
form.post('/compras/fornecimento/suggestion', {
|
||||
preserveScroll: true,
|
||||
onSuccess: () => {
|
||||
const clean = {
|
||||
...initial,
|
||||
operation_key: crypto.randomUUID(),
|
||||
};
|
||||
form.setData(clean);
|
||||
form.clearErrors();
|
||||
router.remember(clean, `${identity}:data`);
|
||||
router.remember({}, `${identity}:errors`);
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
|
||||
Reference in New Issue
Block a user