1156 lines
48 KiB
TypeScript
1156 lines
48 KiB
TypeScript
import { Head, Link, useForm } from '@inertiajs/react';
|
|
import { useEffect, useRef } from 'react';
|
|
import {
|
|
FormErrors,
|
|
PageTitle,
|
|
Panel,
|
|
controlClass,
|
|
} from '@/components/procurement';
|
|
import { Button } from '@/components/ui/button';
|
|
import LegalChargeFields, {
|
|
LegalChargeSummary,
|
|
chargeDrafts,
|
|
type LegalCharge,
|
|
type LegalChargeDraft,
|
|
} from '@/components/import-legal-charges';
|
|
import { estimateMinor, estimateMoney } from '@/lib/import-estimate-money';
|
|
import { date, money } from '@/types/procurement';
|
|
|
|
type LegalData = {
|
|
title: string;
|
|
official_reference: string;
|
|
official_url: string | null;
|
|
citation: string;
|
|
valid_from: string;
|
|
valid_until: string | null;
|
|
match_code: string;
|
|
suggested_code: string;
|
|
origin_country: string | null;
|
|
sector: string | null;
|
|
obligations: string;
|
|
document_types: string[];
|
|
charges?: LegalCharge[];
|
|
};
|
|
type LegalRule = {
|
|
id: number;
|
|
record_key: string;
|
|
version: number;
|
|
state: string;
|
|
data: LegalData;
|
|
reason: string;
|
|
source_url: string;
|
|
};
|
|
type Page<T> = {
|
|
data: T[];
|
|
prev_page_url: string | null;
|
|
next_page_url: string | null;
|
|
};
|
|
type Props = {
|
|
sources?: Page<{
|
|
id: number;
|
|
data: { official_reference: string; official_url: string | null };
|
|
}>;
|
|
requisition: { id: number; number: string };
|
|
rules: Page<LegalRule>;
|
|
history: Page<LegalRule>;
|
|
suggestions: {
|
|
id: number;
|
|
version: number;
|
|
data: LegalData;
|
|
source_sha256: string;
|
|
}[];
|
|
items: { id: number; description: string }[];
|
|
classifications: {
|
|
version: number;
|
|
data: { requisition_item_id: number };
|
|
}[];
|
|
canManage: boolean;
|
|
estimates?: {
|
|
id: number;
|
|
version: number;
|
|
state: string;
|
|
data: {
|
|
requisition_item_id: number;
|
|
bases: Record<
|
|
string,
|
|
{
|
|
value_minor: number | null;
|
|
quantity: string | null;
|
|
source: string | null;
|
|
}
|
|
>;
|
|
};
|
|
snapshot: {
|
|
legal_rule: {
|
|
id: number;
|
|
context: {
|
|
code: string;
|
|
origin_country: string;
|
|
sector: string;
|
|
date: string;
|
|
};
|
|
};
|
|
item: { description: string };
|
|
result: {
|
|
total_minor: number | null;
|
|
known_subtotal_minor: number;
|
|
lines: {
|
|
key: string;
|
|
label: string;
|
|
amount_minor: number | null;
|
|
}[];
|
|
};
|
|
};
|
|
}[];
|
|
estimateHistory?: Page<{
|
|
id: number;
|
|
version: number;
|
|
state: string;
|
|
reason: string;
|
|
}>;
|
|
canPropose: boolean;
|
|
actorId: number;
|
|
tenantId: number;
|
|
types: Record<string, string>;
|
|
filters: { code?: string; origin?: string; sector?: string; date: string };
|
|
};
|
|
const states: Record<string, string> = {
|
|
draft: 'Em preparação',
|
|
published: 'Publicada pela área aduaneira',
|
|
withdrawn: 'Retirada de novas sugestões',
|
|
};
|
|
function useDirtyForm(dirty: boolean) {
|
|
const ref = useRef<HTMLFormElement>(null);
|
|
useEffect(() => {
|
|
const form = ref.current;
|
|
window.dispatchEvent(
|
|
new CustomEvent('sgc:context-dirty', { detail: { form, dirty } }),
|
|
);
|
|
return () => {
|
|
window.dispatchEvent(
|
|
new CustomEvent('sgc:context-dirty', {
|
|
detail: { form, dirty: false },
|
|
}),
|
|
);
|
|
};
|
|
}, [dirty]);
|
|
return ref;
|
|
}
|
|
function Pages({ page }: { page: Page<unknown> }) {
|
|
return (
|
|
<nav className="flex flex-wrap gap-4" aria-label="Páginas">
|
|
{page.prev_page_url && (
|
|
<Link preserveState href={page.prev_page_url}>
|
|
Anterior
|
|
</Link>
|
|
)}
|
|
{page.next_page_url && (
|
|
<Link preserveState href={page.next_page_url}>
|
|
Seguinte
|
|
</Link>
|
|
)}
|
|
</nav>
|
|
);
|
|
}
|
|
function Proposal({
|
|
rule,
|
|
props,
|
|
}: {
|
|
rule: Props['suggestions'][number];
|
|
props: Props;
|
|
}) {
|
|
const form = useForm(
|
|
`legal-proposal:${props.tenantId}:${props.actorId}:${props.requisition.id}:${rule.id}:${JSON.stringify(props.filters)}`,
|
|
{ operation_key: '', requisition_item_id: '', reason: '', version: 0 },
|
|
);
|
|
const ref = useDirtyForm(form.isDirty);
|
|
return (
|
|
<form
|
|
ref={ref}
|
|
className="space-y-3"
|
|
onSubmit={(event) => {
|
|
event.preventDefault();
|
|
const operation =
|
|
form.data.operation_key || crypto.randomUUID();
|
|
form.setData('operation_key', operation);
|
|
form.transform((data) => ({
|
|
...data,
|
|
operation_key: operation,
|
|
action: 'propose',
|
|
code: rule.data.suggested_code,
|
|
source_reference: `${rule.data.official_reference} — ${rule.data.citation}`,
|
|
legal_rule_id: rule.id,
|
|
legal_match_code: props.filters.code,
|
|
legal_origin: props.filters.origin ?? '',
|
|
legal_sector: props.filters.sector ?? '',
|
|
legal_date: props.filters.date,
|
|
}));
|
|
form.post(
|
|
`/requisicoes/${props.requisition.id}/classificacao-pautal`,
|
|
{ onSuccess: () => form.reset() },
|
|
);
|
|
}}
|
|
>
|
|
<FormErrors errors={form.errors} />
|
|
<label className="grid gap-1">
|
|
Artigo a classificar
|
|
<select
|
|
required
|
|
className={controlClass}
|
|
value={form.data.requisition_item_id}
|
|
onChange={(e) =>
|
|
form.setData((data) => ({
|
|
...data,
|
|
requisition_item_id: e.target.value,
|
|
version:
|
|
props.classifications.find(
|
|
(row) =>
|
|
String(row.data.requisition_item_id) ===
|
|
e.target.value,
|
|
)?.version ?? 0,
|
|
}))
|
|
}
|
|
>
|
|
<option value="">Seleccione</option>
|
|
{props.items.map((item) => (
|
|
<option key={item.id} value={item.id}>
|
|
{item.description}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</label>
|
|
<label className="grid gap-1">
|
|
Motivo da proposta ou da substituição
|
|
<textarea
|
|
required
|
|
minLength={10}
|
|
maxLength={4000}
|
|
className={controlClass}
|
|
value={form.data.reason}
|
|
onChange={(e) => form.setData('reason', e.target.value)}
|
|
/>
|
|
</label>
|
|
<Button disabled={form.processing}>
|
|
Preparar proposta com esta fonte
|
|
</Button>
|
|
<p>
|
|
A proposta conserva a fonte e o contexto. Confirme ou justifique
|
|
o desvio no percurso de classificação pautal.
|
|
</p>
|
|
</form>
|
|
);
|
|
}
|
|
function ChargeEstimate({
|
|
rule,
|
|
props,
|
|
}: {
|
|
rule: Props['suggestions'][number];
|
|
props: Props;
|
|
}) {
|
|
const form = useForm(
|
|
`legal-amounts:${props.tenantId}:${props.actorId}:${props.requisition.id}:${rule.id}:${JSON.stringify(props.filters)}`,
|
|
{
|
|
operation_key: '',
|
|
requisition_item_id: '',
|
|
version: 0,
|
|
reason: '',
|
|
bases: Object.fromEntries(
|
|
(rule.data.charges ?? []).map((charge) => [
|
|
charge.key,
|
|
{ value: '', quantity: '', source: '' },
|
|
]),
|
|
),
|
|
},
|
|
);
|
|
const ref = useDirtyForm(form.isDirty);
|
|
const previous = props.estimates?.find(
|
|
(estimate) =>
|
|
String(estimate.data.requisition_item_id) ===
|
|
form.data.requisition_item_id &&
|
|
estimate.snapshot.legal_rule.id === rule.id &&
|
|
estimate.snapshot.legal_rule.context.code === props.filters.code &&
|
|
estimate.snapshot.legal_rule.context.origin_country ===
|
|
(props.filters.origin ?? '') &&
|
|
estimate.snapshot.legal_rule.context.sector ===
|
|
(props.filters.sector ?? '') &&
|
|
estimate.snapshot.legal_rule.context.date === props.filters.date,
|
|
);
|
|
return (
|
|
<form
|
|
ref={ref}
|
|
className="space-y-3 rounded border p-4"
|
|
onSubmit={(event) => {
|
|
event.preventDefault();
|
|
const operation =
|
|
form.data.operation_key || crypto.randomUUID();
|
|
form.setData('operation_key', operation);
|
|
form.transform((data) => ({
|
|
operation_key: operation,
|
|
requisition_item_id: data.requisition_item_id,
|
|
version: data.version,
|
|
reason: data.reason,
|
|
rule_id: rule.id,
|
|
match_code: props.filters.code,
|
|
origin: props.filters.origin ?? '',
|
|
sector: props.filters.sector ?? '',
|
|
date: props.filters.date,
|
|
bases: Object.fromEntries(
|
|
(rule.data.charges ?? []).map((charge) => {
|
|
const base = data.bases[charge.key];
|
|
return [
|
|
charge.key,
|
|
{
|
|
value_minor:
|
|
base.value === ''
|
|
? null
|
|
: estimateMinor(base.value),
|
|
quantity: base.quantity || null,
|
|
quantity_unit: charge.quantity_unit,
|
|
source: base.source || null,
|
|
},
|
|
];
|
|
}),
|
|
),
|
|
}));
|
|
form.post(
|
|
`/requisicoes/${props.requisition.id}/base-legal/estimativas`,
|
|
{ onSuccess: () => form.reset() },
|
|
);
|
|
}}
|
|
>
|
|
<h3 className="font-medium">Estimar as imposições desta regra</h3>
|
|
{previous && (
|
|
<div className="space-y-2">
|
|
<p>
|
|
Há bases preservadas na estimativa #{previous.id},
|
|
versão {previous.version}, para este contexto. Pode
|
|
reutilizá-las e revê-las antes de calcular.
|
|
</p>
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
onClick={() =>
|
|
form.setData(
|
|
'bases',
|
|
Object.fromEntries(
|
|
(rule.data.charges ?? []).map((charge) => {
|
|
const base =
|
|
previous.data.bases[charge.key];
|
|
return [
|
|
charge.key,
|
|
{
|
|
value: estimateMoney(
|
|
base?.value_minor ?? null,
|
|
),
|
|
quantity: base?.quantity ?? '',
|
|
source: base?.source ?? '',
|
|
},
|
|
];
|
|
}),
|
|
),
|
|
)
|
|
}
|
|
>
|
|
Reutilizar bases preservadas
|
|
</Button>
|
|
</div>
|
|
)}
|
|
<FormErrors errors={form.errors} />
|
|
<label className="grid gap-1">
|
|
Artigo
|
|
<select
|
|
required
|
|
className={controlClass}
|
|
value={form.data.requisition_item_id}
|
|
onChange={(e) =>
|
|
form.setData((data) => ({
|
|
...data,
|
|
requisition_item_id: e.target.value,
|
|
version:
|
|
props.estimates?.find(
|
|
(row) =>
|
|
String(row.data.requisition_item_id) ===
|
|
e.target.value,
|
|
)?.version ?? 0,
|
|
}))
|
|
}
|
|
>
|
|
<option value="">Seleccione</option>
|
|
{props.items.map((item) => (
|
|
<option key={item.id} value={item.id}>
|
|
{item.description}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</label>
|
|
{(rule.data.charges ?? []).map((charge) => (
|
|
<fieldset
|
|
key={charge.key}
|
|
className="grid gap-3 rounded border p-3 sm:grid-cols-2"
|
|
>
|
|
<legend>{charge.label}</legend>
|
|
<p className="sm:col-span-2">{charge.basis_reference}</p>
|
|
{['ad_valorem', 'maximum'].includes(charge.method) && (
|
|
<label className="grid gap-1">
|
|
Base de incidência em MZN · taxa{' '}
|
|
{charge.rate_percent}%
|
|
<input
|
|
inputMode="decimal"
|
|
className={controlClass}
|
|
value={form.data.bases[charge.key].value}
|
|
onChange={(e) =>
|
|
form.setData('bases', {
|
|
...form.data.bases,
|
|
[charge.key]: {
|
|
...form.data.bases[charge.key],
|
|
value: e.target.value,
|
|
},
|
|
})
|
|
}
|
|
/>
|
|
</label>
|
|
)}
|
|
{['specific', 'maximum'].includes(charge.method) && (
|
|
<label className="grid gap-1">
|
|
Quantidade em {charge.quantity_unit} · taxa{' '}
|
|
{money(charge.specific_minor)} por unidade
|
|
<input
|
|
inputMode="decimal"
|
|
className={controlClass}
|
|
value={form.data.bases[charge.key].quantity}
|
|
onChange={(e) =>
|
|
form.setData('bases', {
|
|
...form.data.bases,
|
|
[charge.key]: {
|
|
...form.data.bases[charge.key],
|
|
quantity: e.target.value,
|
|
},
|
|
})
|
|
}
|
|
/>
|
|
</label>
|
|
)}
|
|
{[
|
|
'ad_valorem',
|
|
'specific',
|
|
'maximum',
|
|
'not_applicable',
|
|
].includes(charge.method) ? (
|
|
<label className="grid gap-1 sm:col-span-2">
|
|
{charge.method === 'not_applicable'
|
|
? 'Fundamento documental de que a não aplicação e os seus requisitos se verificam neste artigo'
|
|
: 'Origem do valor ou da quantidade'}
|
|
<input
|
|
maxLength={1000}
|
|
className={controlClass}
|
|
value={form.data.bases[charge.key].source}
|
|
onChange={(e) =>
|
|
form.setData('bases', {
|
|
...form.data.bases,
|
|
[charge.key]: {
|
|
...form.data.bases[charge.key],
|
|
source: e.target.value,
|
|
},
|
|
})
|
|
}
|
|
/>
|
|
</label>
|
|
) : (
|
|
<p>Imposição por determinar.</p>
|
|
)}
|
|
</fieldset>
|
|
))}
|
|
<p>
|
|
Deixe por indicar as bases desconhecidas. O subtotal conhecido
|
|
não representa o custo integral da importação.
|
|
</p>
|
|
<label className="grid gap-1">
|
|
Motivo e pressupostos
|
|
<textarea
|
|
required
|
|
minLength={10}
|
|
maxLength={4000}
|
|
className={controlClass}
|
|
value={form.data.reason}
|
|
onChange={(e) => form.setData('reason', e.target.value)}
|
|
/>
|
|
</label>
|
|
<Button disabled={form.processing}>
|
|
Calcular e preservar estimativa
|
|
</Button>
|
|
</form>
|
|
);
|
|
}
|
|
function SourceForm({ url, scope }: { url: string; scope: string }) {
|
|
const form = useForm(`legal-source:${scope}`, {
|
|
operation_key: '',
|
|
action: 'source',
|
|
record_key: null,
|
|
version: 0,
|
|
official_reference: '',
|
|
official_url: '',
|
|
reason: '',
|
|
file: null as File | null,
|
|
});
|
|
const ref = useDirtyForm(form.isDirty);
|
|
return (
|
|
<form
|
|
ref={ref}
|
|
className="space-y-3"
|
|
onSubmit={(event) => {
|
|
event.preventDefault();
|
|
const operation =
|
|
form.data.operation_key || crypto.randomUUID();
|
|
form.setData('operation_key', operation);
|
|
form.transform((data) => ({
|
|
...data,
|
|
operation_key: operation,
|
|
}));
|
|
form.post(url, {
|
|
forceFormData: true,
|
|
onSuccess: () => form.reset(),
|
|
});
|
|
}}
|
|
>
|
|
<FormErrors errors={form.errors} />
|
|
<label className="grid gap-1">
|
|
Diploma e edição do exemplar
|
|
<input
|
|
required
|
|
minLength={3}
|
|
maxLength={1000}
|
|
className={controlClass}
|
|
value={form.data.official_reference}
|
|
onChange={(e) =>
|
|
form.setData('official_reference', e.target.value)
|
|
}
|
|
/>
|
|
</label>
|
|
<label className="grid gap-1">
|
|
Ligação oficial, quando disponível
|
|
<input
|
|
type="url"
|
|
maxLength={2000}
|
|
className={controlClass}
|
|
value={form.data.official_url}
|
|
onChange={(e) =>
|
|
form.setData('official_url', e.target.value)
|
|
}
|
|
/>
|
|
</label>
|
|
<label className="grid gap-1">
|
|
Exemplar integral (PDF ou imagem, até 64 MB)
|
|
<input
|
|
type="file"
|
|
required
|
|
accept=".pdf,.jpg,.jpeg,.png"
|
|
onChange={(e) =>
|
|
form.setData('file', e.target.files?.[0] ?? null)
|
|
}
|
|
/>
|
|
</label>
|
|
<label className="grid gap-1">
|
|
Motivo do registo
|
|
<textarea
|
|
required
|
|
minLength={10}
|
|
maxLength={4000}
|
|
className={controlClass}
|
|
value={form.data.reason}
|
|
onChange={(e) => form.setData('reason', e.target.value)}
|
|
/>
|
|
</label>
|
|
<Button disabled={form.processing}>
|
|
Conservar fonte para consulta
|
|
</Button>
|
|
</form>
|
|
);
|
|
}
|
|
export default function ImportLegal(props: Props) {
|
|
const url = `/requisicoes/${props.requisition.id}/base-legal`;
|
|
const form = useForm(
|
|
`legal-rule:${props.tenantId}:${props.actorId}:${props.requisition.id}`,
|
|
{
|
|
operation_key: '',
|
|
record_key: null as string | null,
|
|
version: 0,
|
|
action: 'save',
|
|
title: '',
|
|
official_reference: '',
|
|
official_url: '',
|
|
citation: '',
|
|
valid_from: '',
|
|
valid_until: '',
|
|
match_code: '',
|
|
suggested_code: '',
|
|
origin_country: '',
|
|
sector: '',
|
|
obligations: '',
|
|
document_types: [] as string[],
|
|
reason: '',
|
|
file: null as File | null,
|
|
source_id: '',
|
|
charges: [] as LegalChargeDraft[],
|
|
},
|
|
);
|
|
const action = useForm(
|
|
`legal-action:${props.tenantId}:${props.actorId}:${props.requisition.id}`,
|
|
{
|
|
operation_key: '',
|
|
record_key: '',
|
|
version: 0,
|
|
action: '',
|
|
reason: '',
|
|
},
|
|
);
|
|
const actionRef = useDirtyForm(action.isDirty);
|
|
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 edit = (rule: LegalRule) => {
|
|
form.setData({
|
|
...form.data,
|
|
...rule.data,
|
|
official_url: rule.data.official_url ?? '',
|
|
valid_until: rule.data.valid_until ?? '',
|
|
origin_country: rule.data.origin_country ?? '',
|
|
sector: rule.data.sector ?? '',
|
|
operation_key: '',
|
|
action: 'save',
|
|
record_key: rule.record_key,
|
|
version: rule.version,
|
|
file: null,
|
|
source_id: '',
|
|
charges: chargeDrafts(rule.data.charges ?? []),
|
|
reason: '',
|
|
});
|
|
form.clearErrors();
|
|
};
|
|
const fields = [
|
|
['title', 'Título da regra'],
|
|
['official_reference', 'Diploma e edição oficial'],
|
|
['official_url', 'Ligação da fonte oficial'],
|
|
['citation', 'Artigo, página e citação aplicável'],
|
|
['valid_from', 'Início da vigência'],
|
|
['valid_until', 'Fim da vigência conhecido (opcional)'],
|
|
['match_code', 'Código a consultar'],
|
|
['suggested_code', 'Código sugerido'],
|
|
[
|
|
'origin_country',
|
|
'País de origem específico (vazio: todas as origens)',
|
|
],
|
|
['sector', 'Sector específico (vazio: todos os sectores)'],
|
|
] as const;
|
|
return (
|
|
<main className="mx-auto max-w-6xl space-y-6 px-5 py-8">
|
|
<Head title="Base legal de importação" />
|
|
<PageTitle
|
|
eyebrow={`Requisição ${props.requisition.number}`}
|
|
title="Base legal de importação"
|
|
description="Consulte as fontes publicadas pela área aduaneira e prepare uma proposta no contexto e período aplicáveis."
|
|
/>
|
|
<Link
|
|
className="underline"
|
|
href={`/requisicoes/${props.requisition.id}/classificacao-pautal`}
|
|
>
|
|
Voltar à classificação pautal
|
|
</Link>
|
|
<Panel
|
|
title="Consultar sugestões"
|
|
description="Não havendo uma correspondência documentada, a regra permanece por determinar. As imposições descritas não são valores calculados nem activam taxas."
|
|
>
|
|
<form
|
|
method="get"
|
|
action={url}
|
|
className="grid gap-3 sm:grid-cols-2"
|
|
>
|
|
{(['code', 'origin', 'sector', 'date'] as const).map(
|
|
(key) => (
|
|
<label key={key} className="grid gap-1">
|
|
{
|
|
{
|
|
code: 'Código previsto',
|
|
origin: 'País de origem',
|
|
sector: 'Sector',
|
|
date: 'Data de aplicação',
|
|
}[key]
|
|
}
|
|
<input
|
|
className={controlClass}
|
|
name={key}
|
|
type={key === 'date' ? 'date' : 'text'}
|
|
required={key === 'code' || key === 'date'}
|
|
defaultValue={props.filters[key] ?? ''}
|
|
maxLength={key === 'code' ? 40 : 160}
|
|
/>
|
|
</label>
|
|
),
|
|
)}
|
|
<Button>Consultar fontes aplicáveis</Button>
|
|
</form>
|
|
{props.filters.code && props.suggestions.length === 0 && (
|
|
<p role="status" className="mt-3">
|
|
Sem regra publicada correspondente a este contexto e
|
|
período. Não foram presumidos código, taxa ou isenção.
|
|
</p>
|
|
)}
|
|
{props.suggestions.map((rule) => (
|
|
<article key={rule.id} className="space-y-3 border-b py-4">
|
|
<h2 className="font-semibold">
|
|
{rule.data.title} · versão {rule.version}
|
|
</h2>
|
|
<p>Código sugerido: {rule.data.suggested_code}</p>
|
|
<p>
|
|
{rule.data.official_reference} ·{' '}
|
|
{rule.data.citation}
|
|
</p>
|
|
<p>{rule.data.obligations}</p>
|
|
<p>
|
|
Documentos indicados:{' '}
|
|
{rule.data.document_types
|
|
.map((type) => props.types[type])
|
|
.join(', ') ||
|
|
'Nenhum segundo a fonte registada'}
|
|
</p>
|
|
<LegalChargeSummary charges={rule.data.charges ?? []} />
|
|
{props.canPropose && (
|
|
<Proposal rule={rule} props={props} />
|
|
)}
|
|
{props.canPropose && (
|
|
<ChargeEstimate rule={rule} props={props} />
|
|
)}
|
|
</article>
|
|
))}
|
|
</Panel>
|
|
<Panel
|
|
title="Regras e fontes preservadas"
|
|
description="Uma nova preparação conserva a última publicação até ser validada. Retirar uma regra impede novas sugestões e conserva as decisões anteriores."
|
|
>
|
|
{props.rules.data.length === 0 && (
|
|
<p>Ainda não existem fontes neste âmbito.</p>
|
|
)}
|
|
{props.rules.data.map((rule) => (
|
|
<article key={rule.id} className="space-y-2 border-b py-4">
|
|
<h2 className="font-semibold">
|
|
{rule.data.title} · versão {rule.version}
|
|
</h2>
|
|
<p>{states[rule.state]}</p>
|
|
<p>
|
|
{rule.data.official_reference} ·{' '}
|
|
{rule.data.citation}
|
|
</p>
|
|
<p>
|
|
{date(rule.data.valid_from)} até{' '}
|
|
{rule.data.valid_until
|
|
? date(rule.data.valid_until)
|
|
: 'sem termo final indicado'}
|
|
</p>
|
|
<LegalChargeSummary charges={rule.data.charges ?? []} />
|
|
<a className="underline" href={rule.source_url}>
|
|
Descarregar exemplar preservado
|
|
</a>
|
|
{props.canManage && (
|
|
<div className="flex flex-wrap gap-3">
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
onClick={() => edit(rule)}
|
|
>
|
|
Preparar nova versão
|
|
</Button>
|
|
{rule.state === 'draft' && (
|
|
<Button
|
|
type="button"
|
|
onClick={() =>
|
|
action.setData({
|
|
operation_key: '',
|
|
record_key: rule.record_key,
|
|
version: rule.version,
|
|
action: 'publish',
|
|
reason: '',
|
|
})
|
|
}
|
|
>
|
|
Preparar publicação
|
|
</Button>
|
|
)}
|
|
{rule.state !== 'withdrawn' && (
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
onClick={() =>
|
|
action.setData({
|
|
operation_key: '',
|
|
record_key: rule.record_key,
|
|
version: rule.version,
|
|
action: 'withdraw',
|
|
reason: '',
|
|
})
|
|
}
|
|
>
|
|
Preparar retirada
|
|
</Button>
|
|
)}
|
|
</div>
|
|
)}
|
|
</article>
|
|
))}
|
|
<Pages page={props.rules} />
|
|
</Panel>
|
|
{props.canManage && action.data.record_key && (
|
|
<Panel
|
|
title={
|
|
action.data.action === 'publish'
|
|
? 'Validar e publicar a versão preparada'
|
|
: 'Retirar de novas sugestões'
|
|
}
|
|
>
|
|
<form
|
|
ref={actionRef}
|
|
className="space-y-3"
|
|
onSubmit={(e) => {
|
|
e.preventDefault();
|
|
const operation =
|
|
action.data.operation_key ||
|
|
crypto.randomUUID();
|
|
action.setData('operation_key', operation);
|
|
action.transform((data) => ({
|
|
...data,
|
|
operation_key: operation,
|
|
}));
|
|
action.post(url, {
|
|
onSuccess: () => action.reset(),
|
|
});
|
|
}}
|
|
>
|
|
<FormErrors errors={action.errors} />
|
|
<label className="grid gap-1">
|
|
Motivo da decisão
|
|
<textarea
|
|
className={controlClass}
|
|
required
|
|
minLength={10}
|
|
maxLength={4000}
|
|
value={action.data.reason}
|
|
onChange={(e) =>
|
|
action.setData('reason', e.target.value)
|
|
}
|
|
/>
|
|
</label>
|
|
<Button disabled={action.processing}>
|
|
Registar decisão da área aduaneira
|
|
</Button>
|
|
</form>
|
|
</Panel>
|
|
)}
|
|
{props.canManage && (
|
|
<Panel
|
|
title={
|
|
form.data.record_key
|
|
? `Preparar revisão da versão ${form.data.version}`
|
|
: 'Preparar uma regra documentada'
|
|
}
|
|
>
|
|
<form
|
|
ref={ref}
|
|
className="space-y-4"
|
|
onSubmit={(e) => {
|
|
e.preventDefault();
|
|
const operation =
|
|
form.data.operation_key || crypto.randomUUID();
|
|
form.setData('operation_key', operation);
|
|
form.transform((data) => ({
|
|
...data,
|
|
operation_key: operation,
|
|
valid_until: data.valid_until || null,
|
|
source_id: data.source_id || null,
|
|
origin_country: data.origin_country || null,
|
|
sector: data.sector || null,
|
|
charges: data.charges.map((charge) => ({
|
|
key: charge.key,
|
|
kind: charge.kind,
|
|
label: charge.label,
|
|
method: charge.method,
|
|
rate_percent: charge.rate_percent || null,
|
|
specific_minor:
|
|
charge.specific_amount === ''
|
|
? null
|
|
: estimateMinor(
|
|
charge.specific_amount,
|
|
),
|
|
quantity_unit: charge.quantity_unit || null,
|
|
basis_reference: charge.basis_reference,
|
|
})),
|
|
}));
|
|
form.post(url, {
|
|
forceFormData: true,
|
|
onSuccess: () => form.reset(),
|
|
});
|
|
}}
|
|
>
|
|
<FormErrors errors={form.errors} />
|
|
<label className="grid gap-1">
|
|
Reutilizar exemplar preservado
|
|
<select
|
|
className={controlClass}
|
|
value={form.data.source_id}
|
|
onChange={(e) => {
|
|
const source = props.sources?.data.find(
|
|
(row) =>
|
|
String(row.id) === e.target.value,
|
|
);
|
|
form.setData((data) => ({
|
|
...data,
|
|
source_id: e.target.value,
|
|
...(source
|
|
? {
|
|
official_reference:
|
|
source.data
|
|
.official_reference,
|
|
official_url:
|
|
source.data
|
|
.official_url ?? '',
|
|
file: null,
|
|
}
|
|
: {}),
|
|
}));
|
|
}}
|
|
>
|
|
<option value="">
|
|
{form.data.record_key
|
|
? 'Conservar exemplar anterior ou carregar um novo'
|
|
: 'Carregar um novo exemplar'}
|
|
</option>
|
|
{props.sources?.data.map((source) => (
|
|
<option key={source.id} value={source.id}>
|
|
{source.data.official_reference}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</label>
|
|
<div className="grid gap-3 md:grid-cols-2">
|
|
{fields.map(([key, label]) => (
|
|
<label key={key} className="grid gap-1">
|
|
{label}
|
|
<input
|
|
className={controlClass}
|
|
type={
|
|
key.startsWith('valid_')
|
|
? 'date'
|
|
: key === 'official_url'
|
|
? 'url'
|
|
: 'text'
|
|
}
|
|
required={
|
|
![
|
|
'valid_until',
|
|
'origin_country',
|
|
'sector',
|
|
'official_url',
|
|
].includes(key)
|
|
}
|
|
value={form.data[key]}
|
|
onChange={(e) =>
|
|
form.setData(key, e.target.value)
|
|
}
|
|
/>
|
|
</label>
|
|
))}
|
|
</div>
|
|
<label className="grid gap-1">
|
|
Imposições, condições, isenções e procedimentos
|
|
documentados
|
|
<textarea
|
|
required
|
|
minLength={3}
|
|
maxLength={4000}
|
|
className={controlClass}
|
|
value={form.data.obligations}
|
|
onChange={(e) =>
|
|
form.setData('obligations', e.target.value)
|
|
}
|
|
/>
|
|
</label>
|
|
<fieldset className="grid gap-2 sm:grid-cols-2">
|
|
<legend>Documentos exigidos na fonte</legend>
|
|
{Object.entries(props.types).map(
|
|
([type, label]) => (
|
|
<label key={type} className="flex gap-2">
|
|
<input
|
|
type="checkbox"
|
|
checked={form.data.document_types.includes(
|
|
type,
|
|
)}
|
|
onChange={(e) =>
|
|
form.setData(
|
|
'document_types',
|
|
e.target.checked
|
|
? [
|
|
...form.data
|
|
.document_types,
|
|
type,
|
|
]
|
|
: form.data.document_types.filter(
|
|
(value) =>
|
|
value !==
|
|
type,
|
|
),
|
|
)
|
|
}
|
|
/>
|
|
{label}
|
|
</label>
|
|
),
|
|
)}
|
|
</fieldset>
|
|
<LegalChargeFields
|
|
charges={form.data.charges}
|
|
onChange={(charges) =>
|
|
form.setData('charges', charges)
|
|
}
|
|
/>
|
|
<label className="grid gap-1">
|
|
Exemplar oficial (PDF ou imagem, até 64 MB)
|
|
<input
|
|
type="file"
|
|
required={
|
|
!form.data.record_key &&
|
|
!form.data.source_id
|
|
}
|
|
disabled={!!form.data.source_id}
|
|
accept=".pdf,.png,.jpg,.jpeg"
|
|
onChange={(e) =>
|
|
form.setData(
|
|
'file',
|
|
e.target.files?.[0] ?? null,
|
|
)
|
|
}
|
|
/>
|
|
</label>
|
|
<label className="grid gap-1">
|
|
Motivo da preparação
|
|
<textarea
|
|
required
|
|
minLength={10}
|
|
maxLength={4000}
|
|
className={controlClass}
|
|
value={form.data.reason}
|
|
onChange={(e) =>
|
|
form.setData('reason', e.target.value)
|
|
}
|
|
/>
|
|
</label>
|
|
<Button disabled={form.processing}>
|
|
Guardar preparação para validação aduaneira
|
|
</Button>
|
|
{form.data.record_key && (
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
onClick={() => form.reset()}
|
|
>
|
|
Preparar outra regra
|
|
</Button>
|
|
)}
|
|
</form>
|
|
</Panel>
|
|
)}
|
|
<Panel
|
|
title="Diplomas disponíveis para consulta"
|
|
description="Conservar um diploma não publica uma regra nem activa um cálculo. Cada regra usa a citação e o período próprios."
|
|
>
|
|
{props.sources?.data.map((source) => (
|
|
<p key={source.id}>
|
|
<a
|
|
className="underline"
|
|
href={`${url}/fontes/${source.id}`}
|
|
>
|
|
{source.data.official_reference}
|
|
</a>
|
|
</p>
|
|
))}
|
|
{props.sources && <Pages page={props.sources} />}
|
|
{props.canManage && (
|
|
<SourceForm
|
|
url={url}
|
|
scope={`${props.tenantId}:${props.actorId}:${props.requisition.id}`}
|
|
/>
|
|
)}
|
|
</Panel>
|
|
<Panel
|
|
title="Estimativas documentadas das imposições"
|
|
description="Valores calculados pelas taxas e bases preservadas. Não registam dívida, reserva, pagamento ou custo efectivo."
|
|
>
|
|
{props.estimates?.map((estimate) => (
|
|
<article
|
|
key={estimate.id}
|
|
className="space-y-2 border-b py-3"
|
|
>
|
|
<h2 className="font-medium">
|
|
{estimate.snapshot.item.description} · versão{' '}
|
|
{estimate.version}
|
|
</h2>
|
|
<p>
|
|
Total das imposições parametrizadas:{' '}
|
|
{estimate.snapshot.result.total_minor === null
|
|
? 'Por determinar'
|
|
: money(estimate.snapshot.result.total_minor)}
|
|
</p>
|
|
<p>
|
|
Subtotal conhecido:{' '}
|
|
{money(
|
|
estimate.snapshot.result.known_subtotal_minor,
|
|
)}
|
|
</p>
|
|
{estimate.snapshot.result.lines.map((line) => (
|
|
<p key={line.key}>
|
|
{line.label}:{' '}
|
|
{line.amount_minor === null
|
|
? 'Por determinar'
|
|
: money(line.amount_minor)}
|
|
</p>
|
|
))}
|
|
</article>
|
|
))}
|
|
<Link
|
|
className="underline"
|
|
href={`/requisicoes/${props.requisition.id}/estimativa-importacao`}
|
|
>
|
|
Abrir plano de custo da importação
|
|
</Link>
|
|
{props.estimateHistory?.data.map((estimate) => (
|
|
<p key={estimate.id}>
|
|
Estimativa #{estimate.id} · versão {estimate.version} ·{' '}
|
|
{estimate.reason}
|
|
</p>
|
|
))}
|
|
{props.estimateHistory && (
|
|
<Pages page={props.estimateHistory} />
|
|
)}
|
|
</Panel>
|
|
<Panel title="Histórico preservado">
|
|
{props.history.data.map((rule) => (
|
|
<p key={rule.id}>
|
|
{rule.data.title} · v{rule.version} ·{' '}
|
|
{states[rule.state]} · {rule.reason}
|
|
</p>
|
|
))}
|
|
<Pages page={props.history} />
|
|
</Panel>
|
|
</main>
|
|
);
|
|
}
|