@@ -0,0 +1,507 @@
|
||||
import { Head, Link, router, useForm } from '@inertiajs/react';
|
||||
import { useEffect, useRef } from 'react';
|
||||
import {
|
||||
FormErrors,
|
||||
PageTitle,
|
||||
Panel,
|
||||
controlClass,
|
||||
} from '@/components/procurement';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { exactMinorAmount } from '@/components/procurement-payable-insights';
|
||||
|
||||
type Account = {
|
||||
id: number;
|
||||
code: string;
|
||||
label: string;
|
||||
currency: string;
|
||||
source_hash: string;
|
||||
};
|
||||
type Allocation = {
|
||||
account: Account;
|
||||
subtotal_minor: string;
|
||||
total_minor: string;
|
||||
};
|
||||
type Version = {
|
||||
id: number;
|
||||
version: number;
|
||||
reason: string;
|
||||
data: { requisition_item_id: number; allocations: Allocation[] };
|
||||
};
|
||||
type Page<T> = {
|
||||
data: T[];
|
||||
prev_page_url: string | null;
|
||||
next_page_url: string | null;
|
||||
};
|
||||
type Props = {
|
||||
requisition: { id: number; number: string; status: string };
|
||||
items: {
|
||||
id: number;
|
||||
description: string;
|
||||
subtotal_minor: string;
|
||||
total_minor: string;
|
||||
basis_hash: string;
|
||||
}[];
|
||||
current: Version[];
|
||||
history: Page<Version>;
|
||||
accounts: Page<Account> | null;
|
||||
canWrite: boolean;
|
||||
search: string;
|
||||
actorId: number;
|
||||
tenantId: number;
|
||||
};
|
||||
type InputAllocation = {
|
||||
account_id: number;
|
||||
source_hash: string;
|
||||
label: string;
|
||||
subtotal: string;
|
||||
total: string;
|
||||
};
|
||||
|
||||
export default function RequisitionAccounts(props: Props) {
|
||||
const url = `/requisicoes/${props.requisition.id}/contas`;
|
||||
const form = useForm(
|
||||
`req-accounts:${props.tenantId}:${props.actorId}:${props.requisition.id}`,
|
||||
{
|
||||
item_id: '',
|
||||
basis_hash: '',
|
||||
version: 0,
|
||||
reason: '',
|
||||
operation_key: '',
|
||||
allocations: [] as InputAllocation[],
|
||||
},
|
||||
);
|
||||
const ref = useRef<HTMLFormElement>(null);
|
||||
useEffect(() => {
|
||||
const node = ref.current;
|
||||
window.dispatchEvent(
|
||||
new CustomEvent('sgc:context-dirty', {
|
||||
detail: { form: node, dirty: form.isDirty },
|
||||
}),
|
||||
);
|
||||
return () => {
|
||||
window.dispatchEvent(
|
||||
new CustomEvent('sgc:context-dirty', {
|
||||
detail: { form: node, dirty: false },
|
||||
}),
|
||||
);
|
||||
};
|
||||
}, [form.isDirty]);
|
||||
const selected = props.items.find(
|
||||
(line) => String(line.id) === form.data.item_id,
|
||||
);
|
||||
const current = props.current.find(
|
||||
(row) => String(row.data.requisition_item_id) === form.data.item_id,
|
||||
);
|
||||
const choose = (id: string) => {
|
||||
if (
|
||||
form.isDirty &&
|
||||
!window.confirm('Descartar a preparação desta linha e abrir outra?')
|
||||
)
|
||||
return;
|
||||
const line = props.items.find((row) => String(row.id) === id);
|
||||
const old = props.current.find(
|
||||
(row) => String(row.data.requisition_item_id) === id,
|
||||
);
|
||||
form.setData({
|
||||
item_id: id,
|
||||
basis_hash: line?.basis_hash ?? '',
|
||||
version: old?.version ?? 0,
|
||||
reason: '',
|
||||
operation_key: '',
|
||||
allocations: [],
|
||||
});
|
||||
};
|
||||
return (
|
||||
<>
|
||||
<Head title="Contas da requisição" />
|
||||
<main className="mx-auto flex w-full max-w-6xl flex-col gap-6 p-5 lg:p-8">
|
||||
<PageTitle
|
||||
eyebrow="Requisição de compra"
|
||||
title="Contas contabilísticas por linha"
|
||||
description={props.requisition.number}
|
||||
/>
|
||||
<Link
|
||||
className="underline"
|
||||
href={`/requisicoes/${props.requisition.id}`}
|
||||
>
|
||||
Voltar à requisição
|
||||
</Link>
|
||||
<Panel
|
||||
title="Imputação da necessidade"
|
||||
description="Contas de movimento do núcleo comum na base própria de Compras. As parcelas são explícitas; o registo não cria lançamentos contabilísticos, pagamentos ou alterações da distribuição orçamental."
|
||||
>
|
||||
<FormErrors errors={form.errors} />
|
||||
{props.canWrite ? (
|
||||
<form
|
||||
ref={ref}
|
||||
className="space-y-4"
|
||||
onSubmit={(event) => {
|
||||
event.preventDefault();
|
||||
const data = {
|
||||
...form.data,
|
||||
operation_key:
|
||||
form.data.operation_key ||
|
||||
crypto.randomUUID(),
|
||||
};
|
||||
form.setData(data);
|
||||
form.transform(() => data);
|
||||
form.post(url, {
|
||||
preserveScroll: true,
|
||||
onSuccess: () => {
|
||||
const next = {
|
||||
...data,
|
||||
version: data.version + 1,
|
||||
operation_key: '',
|
||||
};
|
||||
form.setData(next);
|
||||
form.setDefaults(next);
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
<label className="block">
|
||||
Linha
|
||||
<select
|
||||
className={controlClass}
|
||||
value={form.data.item_id}
|
||||
onChange={(event) =>
|
||||
choose(event.target.value)
|
||||
}
|
||||
>
|
||||
<option value="">Seleccione a linha</option>
|
||||
{props.items.map((line) => (
|
||||
<option key={line.id} value={line.id}>
|
||||
{line.description}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
{selected && (
|
||||
<p>
|
||||
Base sem IVA:{' '}
|
||||
{exactMinorAmount(
|
||||
selected.subtotal_minor,
|
||||
'MZN',
|
||||
)}{' '}
|
||||
· Total:{' '}
|
||||
{exactMinorAmount(
|
||||
selected.total_minor,
|
||||
'MZN',
|
||||
)}
|
||||
</p>
|
||||
)}
|
||||
{current && (
|
||||
<div>
|
||||
<p>Versão actual: {current.version}</p>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
onClick={() =>
|
||||
form.setData(
|
||||
'allocations',
|
||||
current.data.allocations.map(
|
||||
(row) => ({
|
||||
account_id:
|
||||
row.account.id,
|
||||
source_hash:
|
||||
row.account
|
||||
.source_hash,
|
||||
label: `${row.account.code} · ${row.account.label}`,
|
||||
subtotal:
|
||||
(
|
||||
BigInt(
|
||||
row.subtotal_minor,
|
||||
) / 100n
|
||||
).toString() +
|
||||
'.' +
|
||||
(
|
||||
BigInt(
|
||||
row.subtotal_minor,
|
||||
) % 100n
|
||||
)
|
||||
.toString()
|
||||
.padStart(
|
||||
2,
|
||||
'0',
|
||||
),
|
||||
total:
|
||||
(
|
||||
BigInt(
|
||||
row.total_minor,
|
||||
) / 100n
|
||||
).toString() +
|
||||
'.' +
|
||||
(
|
||||
BigInt(
|
||||
row.total_minor,
|
||||
) % 100n
|
||||
)
|
||||
.toString()
|
||||
.padStart(
|
||||
2,
|
||||
'0',
|
||||
),
|
||||
}),
|
||||
),
|
||||
)
|
||||
}
|
||||
>
|
||||
Reutilizar parcelas actuais para revisão
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
{form.data.allocations.map((row, index) => (
|
||||
<fieldset
|
||||
key={row.account_id}
|
||||
className="grid gap-3 rounded-xl border p-4 sm:grid-cols-2"
|
||||
>
|
||||
<legend className="px-2 break-words">
|
||||
{row.label}
|
||||
</legend>
|
||||
{(['subtotal', 'total'] as const).map(
|
||||
(field) => (
|
||||
<label key={field}>
|
||||
{field === 'subtotal'
|
||||
? 'Base sem IVA (MZN)'
|
||||
: 'Total com IVA (MZN)'}
|
||||
<input
|
||||
className={controlClass}
|
||||
inputMode="decimal"
|
||||
value={row[field]}
|
||||
onChange={(event) =>
|
||||
form.setData(
|
||||
'allocations',
|
||||
form.data.allocations.map(
|
||||
(value, at) =>
|
||||
at === index
|
||||
? {
|
||||
...value,
|
||||
[field]:
|
||||
event
|
||||
.target
|
||||
.value,
|
||||
}
|
||||
: value,
|
||||
),
|
||||
)
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
),
|
||||
)}
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
onClick={() => {
|
||||
if (
|
||||
window.confirm(
|
||||
'Remover esta parcela da preparação?',
|
||||
)
|
||||
)
|
||||
form.setData(
|
||||
'allocations',
|
||||
form.data.allocations.filter(
|
||||
(_, at) => at !== index,
|
||||
),
|
||||
);
|
||||
}}
|
||||
>
|
||||
Remover parcela
|
||||
</Button>
|
||||
</fieldset>
|
||||
))}
|
||||
<label className="block">
|
||||
Motivo da imputação ou correcção
|
||||
<textarea
|
||||
className={controlClass}
|
||||
required
|
||||
minLength={10}
|
||||
maxLength={4000}
|
||||
value={form.data.reason}
|
||||
onChange={(event) =>
|
||||
form.setData(
|
||||
'reason',
|
||||
event.target.value,
|
||||
)
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
<Button
|
||||
disabled={
|
||||
form.processing ||
|
||||
!selected ||
|
||||
form.data.allocations.length === 0
|
||||
}
|
||||
>
|
||||
Guardar versão
|
||||
</Button>
|
||||
</form>
|
||||
) : (
|
||||
<p>
|
||||
Consulta das imputações preservadas; a edição
|
||||
pertence ao requisitante enquanto o pedido está em
|
||||
rascunho.
|
||||
</p>
|
||||
)}
|
||||
</Panel>
|
||||
{props.accounts && (
|
||||
<Panel title="Escolher contas do plano de Compras">
|
||||
<form
|
||||
method="get"
|
||||
action={url}
|
||||
onSubmit={(event) => {
|
||||
event.preventDefault();
|
||||
const search = new FormData(
|
||||
event.currentTarget,
|
||||
).get('account_search');
|
||||
router.get(
|
||||
url,
|
||||
{
|
||||
account_search:
|
||||
typeof search === 'string'
|
||||
? search
|
||||
: '',
|
||||
},
|
||||
{
|
||||
preserveState: true,
|
||||
preserveScroll: true,
|
||||
},
|
||||
);
|
||||
}}
|
||||
className="mb-4 flex flex-wrap gap-3"
|
||||
>
|
||||
<label>
|
||||
Código ou designação
|
||||
<input
|
||||
className={controlClass}
|
||||
name="account_search"
|
||||
defaultValue={props.search}
|
||||
maxLength={100}
|
||||
/>
|
||||
</label>
|
||||
<Button variant="outline">Pesquisar contas</Button>
|
||||
</form>
|
||||
<ul className="space-y-3">
|
||||
{props.accounts.data.map((account) => (
|
||||
<li
|
||||
key={account.id}
|
||||
className="flex flex-wrap items-center justify-between gap-3 border-b pb-3"
|
||||
>
|
||||
<span>
|
||||
{account.code} · {account.label} ·{' '}
|
||||
{account.currency}
|
||||
</span>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
disabled={
|
||||
!selected ||
|
||||
form.data.allocations.length >=
|
||||
20 ||
|
||||
form.data.allocations.some(
|
||||
(row) =>
|
||||
row.account_id ===
|
||||
account.id,
|
||||
)
|
||||
}
|
||||
onClick={() =>
|
||||
form.setData('allocations', [
|
||||
...form.data.allocations,
|
||||
{
|
||||
account_id: account.id,
|
||||
source_hash:
|
||||
account.source_hash,
|
||||
label: `${account.code} · ${account.label}`,
|
||||
subtotal: '',
|
||||
total: '',
|
||||
},
|
||||
])
|
||||
}
|
||||
>
|
||||
Adicionar conta à linha
|
||||
</Button>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
{props.accounts.data.length === 0 && (
|
||||
<p>Sem contas de movimento nesta pesquisa.</p>
|
||||
)}
|
||||
<nav className="mt-4 flex gap-4">
|
||||
{props.accounts.prev_page_url && (
|
||||
<Link
|
||||
preserveState
|
||||
preserveScroll
|
||||
className="underline"
|
||||
href={props.accounts.prev_page_url}
|
||||
>
|
||||
Anterior
|
||||
</Link>
|
||||
)}
|
||||
{props.accounts.next_page_url && (
|
||||
<Link
|
||||
preserveState
|
||||
preserveScroll
|
||||
className="underline"
|
||||
href={props.accounts.next_page_url}
|
||||
>
|
||||
Seguinte
|
||||
</Link>
|
||||
)}
|
||||
</nav>
|
||||
</Panel>
|
||||
)}
|
||||
<Panel title="Versões preservadas">
|
||||
<ul className="space-y-4">
|
||||
{props.history.data.map((row) => (
|
||||
<li key={row.id}>
|
||||
<p>
|
||||
Linha #{row.data.requisition_item_id} ·
|
||||
Versão {row.version} · {row.reason}
|
||||
</p>
|
||||
<ul>
|
||||
{row.data.allocations.map((part) => (
|
||||
<li key={part.account.id}>
|
||||
{part.account.code} ·{' '}
|
||||
{part.account.label} · Base sem IVA:{' '}
|
||||
{exactMinorAmount(
|
||||
part.subtotal_minor,
|
||||
'MZN',
|
||||
)}{' '}
|
||||
· Total:{' '}
|
||||
{exactMinorAmount(
|
||||
part.total_minor,
|
||||
'MZN',
|
||||
)}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
<nav className="mt-4 flex gap-4">
|
||||
{props.history.prev_page_url && (
|
||||
<Link
|
||||
preserveState
|
||||
preserveScroll
|
||||
className="underline"
|
||||
href={props.history.prev_page_url}
|
||||
>
|
||||
Histórico anterior
|
||||
</Link>
|
||||
)}
|
||||
{props.history.next_page_url && (
|
||||
<Link
|
||||
preserveState
|
||||
preserveScroll
|
||||
className="underline"
|
||||
href={props.history.next_page_url}
|
||||
>
|
||||
Histórico seguinte
|
||||
</Link>
|
||||
)}
|
||||
</nav>
|
||||
</Panel>
|
||||
</main>
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user