@@ -1,5 +1,14 @@
|
||||
import { Head, Link, useForm } from '@inertiajs/react';
|
||||
import { useState } from 'react';
|
||||
import {
|
||||
Head,
|
||||
Link,
|
||||
router,
|
||||
useForm,
|
||||
usePage,
|
||||
useRemember,
|
||||
} from '@inertiajs/react';
|
||||
import { usePreparationDirty } from '@/hooks/use-preparation-dirty';
|
||||
import { hasPreparationChanges } from '@/lib/requisition-history';
|
||||
import type { WorkContextState } from '@/types/work-context';
|
||||
import {
|
||||
FormErrors,
|
||||
Panel,
|
||||
@@ -44,18 +53,34 @@ type Payment = {
|
||||
evidence_reference: string | null;
|
||||
supplier_bank_detail_id: number;
|
||||
};
|
||||
function Review({ id }: { id: number }) {
|
||||
const form = useForm({
|
||||
function Review({ payment }: { payment: Payment }) {
|
||||
const identity = `${usePaymentIdentity()}:review:${payment.id}:${payment.status}:${payment.amount_minor}:${payment.supplier_bank_detail_id}:${payment.canReview}`;
|
||||
return <ReviewFields key={identity} id={payment.id} identity={identity} />;
|
||||
}
|
||||
function ReviewFields({ id, identity }: { id: number; identity: string }) {
|
||||
const initial = {
|
||||
decision: 'confirmed',
|
||||
reason: '',
|
||||
independently_verified: false,
|
||||
});
|
||||
};
|
||||
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-4 grid gap-4"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
form.post(`/pagamentos/${id}/conferir`);
|
||||
form.post(`/pagamentos/${id}/conferir`, {
|
||||
onSuccess: () => {
|
||||
form.setData(initial);
|
||||
form.clearErrors();
|
||||
router.remember(initial, `${identity}:data`);
|
||||
router.remember({}, `${identity}:errors`);
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
<FormErrors errors={form.errors} />
|
||||
@@ -95,14 +120,28 @@ function Review({ id }: { id: number }) {
|
||||
</form>
|
||||
);
|
||||
}
|
||||
export default function Payments({
|
||||
function usePaymentIdentity() {
|
||||
const { auth, workContext, organizationAccess } = usePage().props;
|
||||
const context = workContext as WorkContextState | null | undefined;
|
||||
const access = organizationAccess as { permissions?: string[] } | null;
|
||||
return `payments:${auth.user?.tenant_id}:${auth.user?.id}:${context?.active ?? 'legacy'}:${[...(access?.permissions ?? [])].sort().join(',')}`;
|
||||
}
|
||||
export default function Payments(
|
||||
props: Omit<Parameters<typeof PaymentFields>[0], 'identity'>,
|
||||
) {
|
||||
const identity = `${usePaymentIdentity()}:${props.canManage}:${props.focusInvoiceId ?? 'all'}:${props.focusPaymentId ?? 'all'}`;
|
||||
return <PaymentFields key={identity} {...props} identity={identity} />;
|
||||
}
|
||||
function PaymentFields({
|
||||
invoices,
|
||||
payments,
|
||||
canManage,
|
||||
expenseAllocationsReady = false,
|
||||
focusPaymentId = null,
|
||||
focusInvoiceId = null,
|
||||
identity,
|
||||
}: {
|
||||
identity: string;
|
||||
invoices: Invoice[];
|
||||
payments: Page<Payment>;
|
||||
canManage: boolean;
|
||||
@@ -110,18 +149,12 @@ export default function Payments({
|
||||
focusPaymentId?: number | null;
|
||||
focusInvoiceId?: number | null;
|
||||
}) {
|
||||
const [invoiceId, setInvoiceId] = useState(
|
||||
const [invoiceId, setInvoiceId] = useRemember(
|
||||
focusInvoiceId === null ? '' : String(focusInvoiceId),
|
||||
`${identity}:invoice`,
|
||||
);
|
||||
const form = useForm({
|
||||
supplier_bank_detail_id:
|
||||
invoices.find((i) => i.id === focusInvoiceId)?.bank_id ?? 0,
|
||||
amount: '',
|
||||
paid_on: '',
|
||||
external_reference: '',
|
||||
evidence_reference: '',
|
||||
});
|
||||
const selected = invoices.find((i) => String(i.id) === invoiceId);
|
||||
const registrationIdentity = `${identity}:registration:${invoiceId}:${selected?.bank_id}:${selected?.bank_revision}:${selected?.bank_state}:${selected?.outstanding_minor}:${selected?.banks?.map((bank) => `${bank.id}.${bank.revision}.${bank.state}`).join(',')}`;
|
||||
return (
|
||||
<>
|
||||
<Head title="Pagamentos externos" />
|
||||
@@ -192,172 +225,46 @@ export default function Payments({
|
||||
title="Registar pagamento externo para conferência"
|
||||
description="Uma referência externa identifica um registo por empresa. Use a referência da operação e a evidência do pagamento; não introduza números de conta nestes campos."
|
||||
>
|
||||
<form
|
||||
className="grid gap-4"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
form.post(`/facturas/${invoiceId}/pagamentos`, {
|
||||
onSuccess: () => {
|
||||
form.reset();
|
||||
setInvoiceId('');
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
<FormErrors errors={form.errors} />
|
||||
<label>
|
||||
Factura
|
||||
<select
|
||||
required
|
||||
className={controlClass}
|
||||
value={invoiceId}
|
||||
onChange={(e) => {
|
||||
setInvoiceId(e.target.value);
|
||||
form.setData(
|
||||
'supplier_bank_detail_id',
|
||||
invoices.find(
|
||||
(i) =>
|
||||
String(i.id) ===
|
||||
e.target.value,
|
||||
)?.bank_id ?? 0,
|
||||
);
|
||||
}}
|
||||
>
|
||||
<option value="">Seleccionar</option>
|
||||
{invoices
|
||||
.filter((i) => i.outstanding_minor > 0)
|
||||
.map((i) => (
|
||||
<option key={i.id} value={i.id}>
|
||||
{i.number} · {i.supplier_name} ·{' '}
|
||||
{money(
|
||||
i.outstanding_minor,
|
||||
i.currency,
|
||||
)}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
{selected?.banks && (
|
||||
<label>
|
||||
Conta bancária na moeda da factura
|
||||
<select
|
||||
className={controlClass}
|
||||
required
|
||||
value={
|
||||
form.data.supplier_bank_detail_id ||
|
||||
''
|
||||
}
|
||||
onChange={(event) =>
|
||||
form.setData(
|
||||
'supplier_bank_detail_id',
|
||||
Number(event.target.value),
|
||||
)
|
||||
}
|
||||
>
|
||||
<option value="">
|
||||
Seleccione uma conta validada
|
||||
</option>
|
||||
{selected.banks.map((bank) => (
|
||||
<option
|
||||
key={bank.id}
|
||||
value={bank.id}
|
||||
disabled={
|
||||
bank.state !== 'approved'
|
||||
}
|
||||
>
|
||||
{bank.label} ·{' '}
|
||||
{bank.state === 'approved'
|
||||
? 'Validada'
|
||||
: 'Por concluir'}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
{selected.banks.length === 0 && (
|
||||
<p className="mt-2 text-sm">
|
||||
Não há conta disponível na moeda
|
||||
desta factura. Consulte os dados
|
||||
bancários do fornecedor.
|
||||
</p>
|
||||
)}
|
||||
</label>
|
||||
)}
|
||||
{selected && (
|
||||
<p className="text-sm">
|
||||
Versão bancária seleccionada:{' '}
|
||||
{selected.banks?.find(
|
||||
(bank) =>
|
||||
bank.id ===
|
||||
form.data.supplier_bank_detail_id,
|
||||
)?.revision ??
|
||||
(selected.bank_revision ||
|
||||
'Por seleccionar')}
|
||||
. Se os dados mudarem, a confirmação será
|
||||
bloqueada.
|
||||
</p>
|
||||
)}
|
||||
<label>
|
||||
Montante do pagamento (
|
||||
{selected?.currency ?? 'MZN'})
|
||||
<Input
|
||||
required
|
||||
inputMode="decimal"
|
||||
value={form.data.amount}
|
||||
onChange={(e) =>
|
||||
form.setData('amount', e.target.value)
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
Data efectiva do pagamento
|
||||
<Input
|
||||
required
|
||||
type="date"
|
||||
value={form.data.paid_on}
|
||||
onChange={(e) =>
|
||||
form.setData('paid_on', e.target.value)
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
Referência externa da operação
|
||||
<Input
|
||||
required
|
||||
autoComplete="off"
|
||||
maxLength={150}
|
||||
value={form.data.external_reference}
|
||||
onChange={(e) =>
|
||||
form.setData(
|
||||
'external_reference',
|
||||
e.target.value,
|
||||
)
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
Referência da evidência
|
||||
<Input
|
||||
required
|
||||
autoComplete="off"
|
||||
maxLength={1000}
|
||||
value={form.data.evidence_reference}
|
||||
onChange={(e) =>
|
||||
form.setData(
|
||||
'evidence_reference',
|
||||
e.target.value,
|
||||
)
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
<Button
|
||||
disabled={
|
||||
!selected ||
|
||||
selected.bank_state !== 'approved' ||
|
||||
form.processing
|
||||
}
|
||||
<label className="mb-4 grid gap-1">
|
||||
Factura
|
||||
<select
|
||||
form="external-payment-registration"
|
||||
required
|
||||
className={controlClass}
|
||||
value={invoiceId}
|
||||
onChange={(e) => {
|
||||
setInvoiceId(e.target.value);
|
||||
}}
|
||||
>
|
||||
Registar para conferência independente
|
||||
</Button>
|
||||
</form>
|
||||
<option value="">Seleccionar</option>
|
||||
{invoiceId &&
|
||||
(!selected ||
|
||||
selected.outstanding_minor <= 0) && (
|
||||
<option value={invoiceId} disabled>
|
||||
Factura seleccionada indisponível ou
|
||||
sem saldo
|
||||
</option>
|
||||
)}
|
||||
{invoices
|
||||
.filter((i) => i.outstanding_minor > 0)
|
||||
.map((i) => (
|
||||
<option key={i.id} value={i.id}>
|
||||
{i.number} · {i.supplier_name} ·{' '}
|
||||
{money(
|
||||
i.outstanding_minor,
|
||||
i.currency,
|
||||
)}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
<RegistrationFields
|
||||
key={registrationIdentity}
|
||||
identity={registrationIdentity}
|
||||
invoiceId={invoiceId}
|
||||
selected={selected}
|
||||
onSaved={() => setInvoiceId('')}
|
||||
/>
|
||||
</Panel>
|
||||
)}
|
||||
{payments.data.map((p) => (
|
||||
@@ -384,7 +291,7 @@ export default function Payments({
|
||||
Evidência: {p.evidence_reference}
|
||||
</p>
|
||||
)}
|
||||
{p.canReview && <Review id={p.id} />}
|
||||
{p.canReview && <Review payment={p} />}
|
||||
{expenseAllocationsReady &&
|
||||
p.status === 'confirmed' && (
|
||||
<Link
|
||||
@@ -402,3 +309,146 @@ export default function Payments({
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
function RegistrationFields({
|
||||
identity,
|
||||
invoiceId,
|
||||
selected,
|
||||
onSaved,
|
||||
}: {
|
||||
identity: string;
|
||||
invoiceId: string;
|
||||
selected?: Invoice;
|
||||
onSaved: () => void;
|
||||
}) {
|
||||
const initial = {
|
||||
supplier_bank_detail_id: selected?.bank_id ?? 0,
|
||||
amount: '',
|
||||
paid_on: '',
|
||||
external_reference: '',
|
||||
evidence_reference: '',
|
||||
};
|
||||
const form = useForm(identity, initial);
|
||||
const dirty = hasPreparationChanges(initial, form.data);
|
||||
const ref = usePreparationDirty(dirty);
|
||||
return (
|
||||
<form
|
||||
id="external-payment-registration"
|
||||
ref={ref}
|
||||
data-preparation-dirty={dirty ? 'true' : 'false'}
|
||||
className="grid gap-4"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
form.post(`/facturas/${invoiceId}/pagamentos`, {
|
||||
onSuccess: () => {
|
||||
form.setData(initial);
|
||||
form.clearErrors();
|
||||
router.remember(initial, `${identity}:data`);
|
||||
router.remember({}, `${identity}:errors`);
|
||||
onSaved();
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
<FormErrors errors={form.errors} />
|
||||
{selected?.banks && (
|
||||
<label>
|
||||
Conta bancária na moeda da factura
|
||||
<select
|
||||
className={controlClass}
|
||||
required
|
||||
value={form.data.supplier_bank_detail_id || ''}
|
||||
onChange={(event) =>
|
||||
form.setData(
|
||||
'supplier_bank_detail_id',
|
||||
Number(event.target.value),
|
||||
)
|
||||
}
|
||||
>
|
||||
<option value="">Seleccione uma conta validada</option>
|
||||
{selected.banks.map((bank) => (
|
||||
<option
|
||||
key={bank.id}
|
||||
value={bank.id}
|
||||
disabled={bank.state !== 'approved'}
|
||||
>
|
||||
{bank.label} ·{' '}
|
||||
{bank.state === 'approved'
|
||||
? 'Validada'
|
||||
: 'Por concluir'}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
{selected.banks.length === 0 && (
|
||||
<p className="mt-2 text-sm">
|
||||
Não há conta disponível na moeda desta factura.
|
||||
Consulte os dados bancários do fornecedor.
|
||||
</p>
|
||||
)}
|
||||
</label>
|
||||
)}
|
||||
{selected && (
|
||||
<p className="text-sm">
|
||||
Versão bancária seleccionada:{' '}
|
||||
{selected.banks?.find(
|
||||
(bank) => bank.id === form.data.supplier_bank_detail_id,
|
||||
)?.revision ??
|
||||
(selected.bank_revision || 'Por seleccionar')}
|
||||
. Se os dados mudarem, a confirmação será bloqueada.
|
||||
</p>
|
||||
)}
|
||||
<label>
|
||||
Montante do pagamento ({selected?.currency ?? 'MZN'})
|
||||
<Input
|
||||
required
|
||||
inputMode="decimal"
|
||||
value={form.data.amount}
|
||||
onChange={(e) => form.setData('amount', e.target.value)}
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
Data efectiva do pagamento
|
||||
<Input
|
||||
required
|
||||
type="date"
|
||||
value={form.data.paid_on}
|
||||
onChange={(e) => form.setData('paid_on', e.target.value)}
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
Referência externa da operação
|
||||
<Input
|
||||
required
|
||||
autoComplete="off"
|
||||
maxLength={150}
|
||||
value={form.data.external_reference}
|
||||
onChange={(e) =>
|
||||
form.setData('external_reference', e.target.value)
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
Referência da evidência
|
||||
<Input
|
||||
required
|
||||
autoComplete="off"
|
||||
maxLength={1000}
|
||||
value={form.data.evidence_reference}
|
||||
onChange={(e) =>
|
||||
form.setData('evidence_reference', e.target.value)
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
<Button
|
||||
disabled={
|
||||
!selected ||
|
||||
selected.outstanding_minor <= 0 ||
|
||||
selected.bank_state !== 'approved' ||
|
||||
form.processing
|
||||
}
|
||||
>
|
||||
Registar para conferência independente
|
||||
</Button>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user