@@ -0,0 +1,263 @@
|
||||
import { Head, Link, useForm, usePage } from '@inertiajs/react';
|
||||
import { useEffect, useRef } from 'react';
|
||||
import { FormErrors, PageTitle, Panel } from '@/components/procurement';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { money } from '@/types/procurement';
|
||||
|
||||
type Source = {
|
||||
origin: number;
|
||||
version: number;
|
||||
plan_hash: string;
|
||||
allocations: {
|
||||
allocation_key: string;
|
||||
line_id: number;
|
||||
centre_id: number;
|
||||
subtotal: string;
|
||||
total: string;
|
||||
}[];
|
||||
};
|
||||
|
||||
export default function Amendment({
|
||||
order,
|
||||
sources,
|
||||
history,
|
||||
}: {
|
||||
order: { id: number; number: string; currency: string };
|
||||
sources: Source[];
|
||||
history: {
|
||||
id: number;
|
||||
created_at: string;
|
||||
created_by: number;
|
||||
reason: string | null;
|
||||
sources: {
|
||||
origin: number;
|
||||
allocations: {
|
||||
allocation_key: string;
|
||||
subtotal_minor: number;
|
||||
total_minor: number;
|
||||
}[];
|
||||
}[];
|
||||
}[];
|
||||
}) {
|
||||
const actor = usePage().props.auth?.user;
|
||||
const form = useForm(
|
||||
`order-parts-amendment:${actor?.tenant_id}:${actor?.id}:${order.id}:${sources.map((s) => `${s.origin}:${s.version}:${s.plan_hash}`).join(',')}`,
|
||||
{
|
||||
operation_key: crypto.randomUUID(),
|
||||
reason: '',
|
||||
sources: sources.map((source) => ({
|
||||
origin: source.origin,
|
||||
version: source.version,
|
||||
plan_hash: source.plan_hash,
|
||||
allocations: source.allocations.map(
|
||||
({ allocation_key, subtotal, total }) => ({
|
||||
allocation_key,
|
||||
subtotal,
|
||||
total,
|
||||
}),
|
||||
),
|
||||
})),
|
||||
},
|
||||
);
|
||||
const ref = useRef<HTMLFormElement>(null);
|
||||
useEffect(() => {
|
||||
const current = ref.current;
|
||||
window.dispatchEvent(
|
||||
new CustomEvent('sgc:context-dirty', {
|
||||
detail: { form: current, dirty: form.isDirty },
|
||||
}),
|
||||
);
|
||||
return () => {
|
||||
window.dispatchEvent(
|
||||
new CustomEvent('sgc:context-dirty', {
|
||||
detail: { form: current, dirty: false },
|
||||
}),
|
||||
);
|
||||
};
|
||||
}, [form.isDirty]);
|
||||
const update = (
|
||||
origin: number,
|
||||
allocation: number,
|
||||
field: 'subtotal' | 'total',
|
||||
value: string,
|
||||
) => {
|
||||
form.setData(
|
||||
'sources',
|
||||
form.data.sources.map((source, i) =>
|
||||
i !== origin
|
||||
? source
|
||||
: {
|
||||
...source,
|
||||
allocations: source.allocations.map((row, j) =>
|
||||
j !== allocation
|
||||
? row
|
||||
: { ...row, [field]: value },
|
||||
),
|
||||
},
|
||||
),
|
||||
);
|
||||
};
|
||||
return (
|
||||
<>
|
||||
<Head title={`Reafectação · ${order.number}`} />
|
||||
<PageTitle
|
||||
eyebrow="Compras · Finanças"
|
||||
title={`Reafectação · ${order.number}`}
|
||||
description="Confirmação explícita de Finanças para esta ordem."
|
||||
/>
|
||||
<Panel title="Imputações actuais em MZN">
|
||||
<p className="mb-4">
|
||||
Conserve os montantes de cada artigo e origem. Apenas os
|
||||
centros já previstos podem ser reafectados. Valores
|
||||
consumidos, reservas não emitidas e documentos originais são
|
||||
preservados.
|
||||
</p>
|
||||
<form
|
||||
ref={ref}
|
||||
className="grid gap-4"
|
||||
onSubmit={(event) => {
|
||||
event.preventDefault();
|
||||
form.post(`/ordens/${order.id}/reafectacao`);
|
||||
}}
|
||||
>
|
||||
<FormErrors errors={form.errors} />
|
||||
{sources.map((source, i) => (
|
||||
<fieldset
|
||||
key={source.origin}
|
||||
className="grid min-w-0 gap-3 rounded border p-4"
|
||||
>
|
||||
<legend className="px-2 font-semibold">
|
||||
Origem #{source.origin} · Versão{' '}
|
||||
{source.version}
|
||||
</legend>
|
||||
{source.allocations.map((allocation, j) => (
|
||||
<div
|
||||
key={allocation.allocation_key}
|
||||
className="grid gap-3 rounded border p-3 sm:grid-cols-2"
|
||||
>
|
||||
<p className="sm:col-span-2">
|
||||
Artigo #{allocation.line_id} · Centro #
|
||||
{allocation.centre_id}
|
||||
</p>
|
||||
<label className="grid gap-1">
|
||||
Sem IVA (MZN)
|
||||
<Input
|
||||
required
|
||||
inputMode="decimal"
|
||||
value={
|
||||
form.data.sources[i]
|
||||
.allocations[j].subtotal
|
||||
}
|
||||
onChange={(e) =>
|
||||
update(
|
||||
i,
|
||||
j,
|
||||
'subtotal',
|
||||
e.target.value,
|
||||
)
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
<label className="grid gap-1">
|
||||
Com IVA (MZN)
|
||||
<Input
|
||||
required
|
||||
inputMode="decimal"
|
||||
value={
|
||||
form.data.sources[i]
|
||||
.allocations[j].total
|
||||
}
|
||||
onChange={(e) =>
|
||||
update(
|
||||
i,
|
||||
j,
|
||||
'total',
|
||||
e.target.value,
|
||||
)
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
))}
|
||||
</fieldset>
|
||||
))}
|
||||
<label className="grid gap-1">
|
||||
Motivo
|
||||
<textarea
|
||||
required
|
||||
minLength={10}
|
||||
maxLength={4000}
|
||||
className="min-h-24 rounded border p-3"
|
||||
value={form.data.reason}
|
||||
onChange={(e) =>
|
||||
form.setData('reason', e.target.value)
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
<Button disabled={form.processing} type="submit">
|
||||
Confirmar reafectação por Finanças
|
||||
</Button>
|
||||
<Link className="underline" href={`/ordens/${order.id}`}>
|
||||
Voltar à ordem
|
||||
</Link>
|
||||
</form>
|
||||
</Panel>
|
||||
<Panel title="Histórico de reafectações">
|
||||
{history.length === 0 ? (
|
||||
<p>Sem reafectações registadas.</p>
|
||||
) : (
|
||||
history.map((event) => (
|
||||
<div key={event.id} className="mb-4 rounded border p-3">
|
||||
<p>
|
||||
Reafectação #{event.id} · {event.created_at} ·
|
||||
Interveniente #{event.created_by}
|
||||
</p>
|
||||
{event.reason && (
|
||||
<p className="whitespace-pre-wrap">
|
||||
{event.reason}
|
||||
</p>
|
||||
)}
|
||||
<details>
|
||||
<summary>
|
||||
Consultar imputações conservadas
|
||||
</summary>
|
||||
{event.sources.map((source) => (
|
||||
<div key={source.origin}>
|
||||
<p>Origem #{source.origin}</p>
|
||||
{source.allocations.map((row) => (
|
||||
<p key={row.allocation_key}>
|
||||
Centro #
|
||||
{
|
||||
sources
|
||||
.find(
|
||||
(s) =>
|
||||
s.origin ===
|
||||
source.origin,
|
||||
)
|
||||
?.allocations.find(
|
||||
(a) =>
|
||||
a.allocation_key ===
|
||||
row.allocation_key,
|
||||
)?.centre_id
|
||||
}{' '}
|
||||
·{' '}
|
||||
{money(
|
||||
row.subtotal_minor,
|
||||
'MZN',
|
||||
)}{' '}
|
||||
sem IVA ·{' '}
|
||||
{money(row.total_minor, 'MZN')}{' '}
|
||||
com IVA
|
||||
</p>
|
||||
))}
|
||||
</div>
|
||||
))}
|
||||
</details>
|
||||
</div>
|
||||
))
|
||||
)}
|
||||
</Panel>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -22,6 +22,13 @@ type Financial = {
|
||||
origin: number;
|
||||
award_id: number;
|
||||
requires_adoption: boolean;
|
||||
approved_reference?: { subtotal_minor: number; total_minor: number };
|
||||
adoption_lines?: {
|
||||
line_id: number;
|
||||
description: string;
|
||||
subtotal_minor: number;
|
||||
total_minor: number;
|
||||
}[];
|
||||
version?: number;
|
||||
plan_hash?: string;
|
||||
allocations: {
|
||||
@@ -47,6 +54,12 @@ type Preview = {
|
||||
requisition_number?: string | null;
|
||||
award_id: number;
|
||||
currency: string;
|
||||
reference_amounts?: { subtotal_minor: number; total_minor: number };
|
||||
exchange_basis?: {
|
||||
rate: string;
|
||||
quoted_on: string;
|
||||
source: string;
|
||||
} | null;
|
||||
items: {
|
||||
description: string;
|
||||
quotation_item_id: number;
|
||||
@@ -82,8 +95,28 @@ function useGuard(ref: RefObject<HTMLFormElement | null>, dirty: boolean) {
|
||||
}, [dirty, ref]);
|
||||
}
|
||||
|
||||
function Adoption({ award }: { award: number }) {
|
||||
const form = useForm({ operation_key: crypto.randomUUID() });
|
||||
function Adoption({
|
||||
source,
|
||||
currency,
|
||||
}: {
|
||||
source: Financial;
|
||||
currency: string;
|
||||
}) {
|
||||
const foreign = currency !== 'MZN';
|
||||
const form = useForm({
|
||||
operation_key: crypto.randomUUID(),
|
||||
...(foreign
|
||||
? {
|
||||
reference_lines: (source.adoption_lines ?? []).map(
|
||||
(line) => ({
|
||||
line_id: line.line_id,
|
||||
subtotal: '',
|
||||
total: '',
|
||||
}),
|
||||
),
|
||||
}
|
||||
: {}),
|
||||
});
|
||||
return (
|
||||
<div className="grid gap-3 rounded-lg border p-4">
|
||||
<p>
|
||||
@@ -96,11 +129,73 @@ function Adoption({ award }: { award: number }) {
|
||||
da origem. A adopção não cria dotação nem saldo.
|
||||
</p>
|
||||
<FormErrors errors={form.errors} />
|
||||
{foreign && (
|
||||
<>
|
||||
<p>
|
||||
Confira a referência integral aprovada em MZN antes de
|
||||
transferir a reserva:{' '}
|
||||
{source.approved_reference
|
||||
? money(
|
||||
source.approved_reference.total_minor,
|
||||
'MZN',
|
||||
)
|
||||
: 'Referência por conferir'}
|
||||
. Indique todos os artigos da proposta, mesmo os que não
|
||||
serão emitidos nesta parcela.
|
||||
</p>
|
||||
{(source.adoption_lines ?? []).map((line, index) => (
|
||||
<div
|
||||
key={line.line_id}
|
||||
className="grid gap-3 rounded border p-3 sm:grid-cols-2"
|
||||
>
|
||||
<p className="sm:col-span-2">
|
||||
{line.description} ·{' '}
|
||||
{money(line.total_minor, currency)}
|
||||
</p>
|
||||
{(['subtotal', 'total'] as const).map((field) => (
|
||||
<label key={field} className="grid gap-1">
|
||||
{field === 'subtotal'
|
||||
? 'Sem IVA (MZN)'
|
||||
: 'Com IVA (MZN)'}
|
||||
<Input
|
||||
inputMode="decimal"
|
||||
value={
|
||||
form.data.reference_lines?.[index][
|
||||
field
|
||||
] ?? ''
|
||||
}
|
||||
onChange={(event) =>
|
||||
form.setData(
|
||||
'reference_lines',
|
||||
(
|
||||
form.data.reference_lines ??
|
||||
[]
|
||||
).map((row, i) =>
|
||||
i === index
|
||||
? {
|
||||
...row,
|
||||
[field]:
|
||||
event.target
|
||||
.value,
|
||||
}
|
||||
: row,
|
||||
),
|
||||
)
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
))}
|
||||
</div>
|
||||
))}
|
||||
</>
|
||||
)}
|
||||
<Button
|
||||
type="button"
|
||||
disabled={form.processing}
|
||||
onClick={() =>
|
||||
form.post(`/adjudicacoes/${award}/parcelas/adoptar`)
|
||||
form.post(
|
||||
`/adjudicacoes/${source.award_id}/parcelas/adoptar`,
|
||||
)
|
||||
}
|
||||
>
|
||||
Adoptar a reserva existente
|
||||
@@ -219,8 +314,28 @@ function Prepared({
|
||||
{money(line.total_minor, source.currency)}
|
||||
</p>
|
||||
))}
|
||||
{source.reference_amounts &&
|
||||
source.exchange_basis && (
|
||||
<p className="text-sm break-words">
|
||||
Referência desta origem:{' '}
|
||||
{money(
|
||||
source.reference_amounts
|
||||
.total_minor,
|
||||
'MZN',
|
||||
)}
|
||||
{' · '}Taxa:{' '}
|
||||
{source.exchange_basis.rate}
|
||||
{' · '}
|
||||
{source.exchange_basis.quoted_on}
|
||||
{' · '}
|
||||
{source.exchange_basis.source}
|
||||
</p>
|
||||
)}
|
||||
{preview.financial[index].requires_adoption ? (
|
||||
<Adoption award={source.award_id} />
|
||||
<Adoption
|
||||
source={preview.financial[index]}
|
||||
currency={source.currency}
|
||||
/>
|
||||
) : (
|
||||
<>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
@@ -354,8 +469,7 @@ function Prepared({
|
||||
{!preview.can_issue && (
|
||||
<p role="status">
|
||||
Complete a distribuição de todas as origens antes de
|
||||
emitir. A emissão multimoeda por parcelas ainda não
|
||||
está disponível.
|
||||
emitir.
|
||||
</p>
|
||||
)}
|
||||
<p className="text-sm text-muted-foreground">
|
||||
|
||||
@@ -35,12 +35,14 @@ export default function OrderShow({
|
||||
orderCancellation,
|
||||
snapshotComplete = true,
|
||||
orderPartsSources = [],
|
||||
canAmendOrderParts = false,
|
||||
}: {
|
||||
order: PurchaseOrder;
|
||||
history: History[];
|
||||
orderCancellation?: OrderCancellationState;
|
||||
snapshotComplete?: boolean;
|
||||
orderPartsSources?: OrderPartFrame[];
|
||||
canAmendOrderParts?: boolean;
|
||||
}) {
|
||||
const snapshot = order.snapshot;
|
||||
if (!snapshotComplete) {
|
||||
@@ -160,6 +162,14 @@ export default function OrderShow({
|
||||
' Regras de aprovação provisórias.'}
|
||||
</div>
|
||||
<OrderPartsSummary sources={orderPartsSources} />
|
||||
{canAmendOrderParts && (
|
||||
<Link
|
||||
className="underline"
|
||||
href={`/ordens/${order.id}/reafectacao`}
|
||||
>
|
||||
Conferir ou reafectar imputações por Finanças
|
||||
</Link>
|
||||
)}
|
||||
<ImportTariffSummary records={snapshot.import_tariffs} />
|
||||
<div className="grid gap-6 md:grid-cols-2">
|
||||
{snapshot.investment_rules && (
|
||||
|
||||
Reference in New Issue
Block a user