import {
Head,
Link,
router,
useForm,
usePage,
useRemember,
} from '@inertiajs/react';
import { usePreparationDirty } from '@/hooks/use-preparation-dirty';
import { hasPreparationChanges } from '@/lib/requisition-history';
import {
FormErrors,
Panel,
PageTitle,
controlClass,
} from '@/components/procurement';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
type Detail = {
id: number;
revision: number;
bank_name: string;
country: string;
currency: string;
beneficiary: string;
account_identifier: string;
evidence_reference: string;
reason: string;
};
type Props = {
accountKey?: string | null;
actorId?: number;
multiAccountsAvailable?: boolean;
accounts?: {
id: number;
key: string | null;
revision: number;
label: string;
state: string;
}[];
supplier: { id: number; name: string };
current: Detail | null;
currentId: number;
state: string;
history: {
id: number;
revision: number;
created_by: number;
created_at: string;
}[];
reviews: {
id: number;
supplier_bank_detail_id: number;
reviewed_by: number;
decision: string;
}[];
canPropose: boolean;
canReview: boolean;
};
export default function BankPage(props: Props) {
const actor = usePage().props.auth?.user;
return (
);
}
function Bank({
supplier,
current,
currentId,
state,
history,
reviews,
canPropose,
canReview,
accountKey = null,
accounts = [],
multiAccountsAvailable = false,
actorId,
}: Props) {
const actor = usePage().props.auth?.user;
const identity = `supplier-bank:${actor?.tenant_id}:${actorId ?? actor?.id ?? 0}:${supplier.id}:${accountKey ?? 'legacy'}:${currentId}:${canPropose}:${canReview}`;
const [addingAccount, setAddingAccount] = useRemember(
false,
`${identity}:adding`,
);
const initial = {
previous_id: currentId,
account_key: accountKey,
bank_name: '',
country: '',
currency: '',
beneficiary: '',
account_identifier: '',
evidence_reference: '',
reason: '',
};
const form = useForm(`${identity}:proposal`, initial);
const initialReview = {
decision: 'approved',
reason: '',
independently_verified: false,
};
const review = useForm(`${identity}:review`, initialReview);
const dirty = Object.values({
bank_name: form.data.bank_name,
country: form.data.country,
currency: form.data.currency,
beneficiary: form.data.beneficiary,
account_identifier: form.data.account_identifier,
evidence_reference: form.data.evidence_reference,
reason: form.data.reason,
}).some((value) => value !== '');
const proposalRef = usePreparationDirty(dirty, canPropose);
const reviewDirty = hasPreparationChanges(initialReview, review.data);
const reviewRef = usePreparationDirty(
reviewDirty,
canReview && current !== null && state === 'pending',
);
const labels = {
bank_name: 'Banco',
country: 'Código do país (2 letras)',
currency: 'Moeda da conta (3 letras)',
beneficiary: 'Titular / beneficiário',
account_identifier: 'Identificador da conta fornecido pelo banco',
evidence_reference:
'Referência da evidência e confirmação independente',
reason: 'Fundamento da alteração',
};
const states: Record = {
missing: 'Sem dados',
pending: 'Pendente de duas validações',
approved: 'Duplamente validado',
rejected: 'Rejeitado',
blocked: 'Bloqueado por elegibilidade ou conflito',
};
return (
<>
← Ficha do fornecedor
{multiAccountsAvailable && (
{accounts.map((account) => (
{account.label} ·{' '}
{states[account.state] ?? account.state}
))}
{canPropose && (
)}
)}
{current && (
{Object.entries(labels).map(([key, label]) => (
-
{label}
-
{current[key as keyof typeof labels]}
))}
)}
{!current && (
Os identificadores bancários são apresentados apenas
a Procurement e Finanças. Administração consulta o
estado e o histórico.
)}
{canReview && current && state === 'pending' && (
)}
{canPropose && (
)}
{history.map((h) => (
Versão {h.revision} · Autor #{h.created_by}{' '}
· {h.created_at}
{reviews
.filter(
(r) =>
r.supplier_bank_detail_id === h.id,
)
.map((r) => (
{r.decision === 'approved'
? 'Aprovado'
: 'Rejeitado'}{' '}
· Finanças #{r.reviewed_by}
))}
))}
{history.length === 0 && (
Sem versões bancárias registadas.
)}
>
);
}