398 lines
19 KiB
TypeScript
398 lines
19 KiB
TypeScript
import { inputText } from '../lib/input-text.ts';
|
|
import { useState } from 'react';
|
|
import {
|
|
newCompositionBase,
|
|
newCompositionPart,
|
|
switchCompositionPart,
|
|
compositionBook,
|
|
compositionConditions,
|
|
compositionCurrent,
|
|
compositionReportRows,
|
|
compositionBaseSource,
|
|
compositionTreatments,
|
|
recordCompositions,
|
|
saveCompositionBook,
|
|
type CompositionBook,
|
|
type CompositionRow,
|
|
type CompositionPart,
|
|
} from '@/lib/iva-composition-base';
|
|
|
|
export function SimulationCompositionBase({
|
|
draft,
|
|
onChange,
|
|
}: {
|
|
draft: Record<string, unknown>;
|
|
onChange: (field: string, value: string) => void;
|
|
}) {
|
|
const [error, setError] = useState('');
|
|
let book: CompositionBook;
|
|
try {
|
|
book = compositionBook(draft);
|
|
} catch (cause) {
|
|
return <p role="alert">{String(cause)}</p>;
|
|
}
|
|
const save = (next: CompositionBook) => {
|
|
try {
|
|
onChange('modelo_a_snapshot', saveCompositionBook(draft, next));
|
|
setError('');
|
|
} catch (cause) {
|
|
setError(String(cause));
|
|
}
|
|
};
|
|
const change = (id: string, patch: Partial<CompositionRow>) =>
|
|
save({
|
|
...book,
|
|
rows: book.rows.map((row) =>
|
|
row.id === id ? { ...row, ...patch } : row,
|
|
),
|
|
});
|
|
return (
|
|
<details className="space-y-3 rounded border p-3">
|
|
<summary>
|
|
Base tributável — impostos e despesas acessórias da
|
|
contraprestação
|
|
</summary>
|
|
<p>
|
|
Oportunidade:{' '}
|
|
{inputText(draft.context_opportunity || 'entrada geral')}.
|
|
Prepare uma composição por operação. Parta do montante sem
|
|
próprio IVA e com as exclusões aplicáveis já consideradas.
|
|
Identifique as parcelas e se já estão incluídas;acrescente
|
|
apenas as que ainda não constam do montante inicial.
|
|
</p>
|
|
<p>
|
|
Impostos,direitos,taxas e outras imposições excluem o próprio
|
|
IVA. Nas despesas acessórias,confirme que são debitadas por
|
|
conta do cliente e relativas a comissões,embalagem,transporte ou
|
|
seguros. Respeite as regras especiais da operação,incluindo a
|
|
exclusão da TC nos combustíveis. Base e IVA são resultados
|
|
distintos.
|
|
</p>
|
|
<p>{compositionBaseSource}</p>
|
|
{error && <p role="alert">{error}</p>}
|
|
<button
|
|
type="button"
|
|
className="underline"
|
|
onClick={() =>
|
|
save({
|
|
...book,
|
|
rows: [
|
|
...book.rows,
|
|
newCompositionBase(
|
|
crypto.randomUUID(),
|
|
inputText(draft.currency ?? ''),
|
|
),
|
|
],
|
|
})
|
|
}
|
|
>
|
|
Acrescentar composição de operação
|
|
</button>
|
|
{book.rows.map((row) => {
|
|
const partChange = (
|
|
id: string,
|
|
patch: Partial<CompositionPart>,
|
|
) =>
|
|
change(row.id, {
|
|
parts: row.parts.map((p) =>
|
|
p.id === id ? { ...p, ...patch } : p,
|
|
),
|
|
});
|
|
return (
|
|
<details key={row.id} className="rounded border p-3">
|
|
<summary>
|
|
{row.description || 'Operação por identificar'} —{' '}
|
|
{row.period || 'mês por indicar'}
|
|
</summary>
|
|
<div className="grid gap-3 md:grid-cols-2">
|
|
<label>
|
|
Natureza dos dados
|
|
<select
|
|
className="block rounded border p-2"
|
|
value={row.origin}
|
|
onChange={(e) =>
|
|
change(row.id, {
|
|
origin: e.target
|
|
.value as CompositionRow['origin'],
|
|
})
|
|
}
|
|
>
|
|
<option value="projected">
|
|
Projectados / pressupostos
|
|
</option>
|
|
<option value="effective">
|
|
Efectivos declarados
|
|
</option>
|
|
</select>
|
|
</label>
|
|
{(
|
|
[
|
|
['description', 'Operação identificada'],
|
|
[
|
|
'operationRef',
|
|
'Referência da operação / pressuposto',
|
|
],
|
|
[
|
|
'initialAmount',
|
|
'Montante inicial sem próprio IVA (moeda indicada)',
|
|
],
|
|
[
|
|
'initialRef',
|
|
'Referência do montante inicial e da sua composição',
|
|
],
|
|
[
|
|
'documentRef',
|
|
'Documento da operação / referência da previsão',
|
|
],
|
|
[
|
|
'documentDate',
|
|
'Data do documento / prevista',
|
|
],
|
|
['period', 'Mês da operação'],
|
|
['currency', 'Moeda de origem'],
|
|
[
|
|
'compositionRef',
|
|
'Referência da composição e exclusões aplicáveis',
|
|
],
|
|
[
|
|
'evidence',
|
|
'Fundamento factual / pressupostos',
|
|
],
|
|
] as const
|
|
).map(([field, label]) => (
|
|
<label key={field}>
|
|
{label}
|
|
<input
|
|
className="block rounded border p-2"
|
|
type={
|
|
field === 'documentDate'
|
|
? 'date'
|
|
: field === 'period'
|
|
? 'month'
|
|
: 'text'
|
|
}
|
|
value={row[field]}
|
|
onChange={(e) =>
|
|
change(row.id, {
|
|
[field]: e.target.value,
|
|
})
|
|
}
|
|
/>
|
|
</label>
|
|
))}
|
|
</div>
|
|
<fieldset>
|
|
<legend>Condições da composição</legend>
|
|
{compositionConditions.map(([key, label]) => (
|
|
<label className="block" key={key}>
|
|
{label}
|
|
<select
|
|
className="block rounded border p-2"
|
|
value={row.conditions[key] ?? ''}
|
|
onChange={(e) =>
|
|
change(row.id, {
|
|
conditions: {
|
|
...row.conditions,
|
|
[key]: e.target.value,
|
|
},
|
|
})
|
|
}
|
|
>
|
|
<option value="">Por indicar</option>
|
|
<option value="yes">Sim</option>
|
|
<option value="no">Não</option>
|
|
</select>
|
|
</label>
|
|
))}
|
|
</fieldset>
|
|
<button
|
|
type="button"
|
|
className="underline"
|
|
onClick={() =>
|
|
change(row.id, {
|
|
parts: [
|
|
...row.parts,
|
|
newCompositionPart(crypto.randomUUID()),
|
|
],
|
|
})
|
|
}
|
|
>
|
|
Acrescentar parcela desta operação
|
|
</button>
|
|
{row.parts.map((part) => (
|
|
<fieldset
|
|
key={part.id}
|
|
className="rounded border p-3"
|
|
>
|
|
<legend>
|
|
{part.description ||
|
|
'Parcela por identificar'}
|
|
</legend>
|
|
<label>
|
|
Natureza da parcela
|
|
<select
|
|
className="block rounded border p-2"
|
|
value={part.treatment}
|
|
onChange={(e) =>
|
|
partChange(
|
|
part.id,
|
|
switchCompositionPart(
|
|
part,
|
|
e.target.value,
|
|
),
|
|
)
|
|
}
|
|
>
|
|
<option value="">Por indicar</option>
|
|
{compositionTreatments.map((t) => (
|
|
<option key={t.id} value={t.id}>
|
|
{t.label}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</label>
|
|
{(
|
|
[
|
|
['description', 'Descrição da parcela'],
|
|
[
|
|
'amount',
|
|
'Montante da parcela sem próprio IVA (moeda indicada)',
|
|
],
|
|
[
|
|
'reference',
|
|
'Referência da parcela / pressuposto',
|
|
],
|
|
] as const
|
|
).map(([field, label]) => (
|
|
<label className="block" key={field}>
|
|
{label}
|
|
<input
|
|
className="block rounded border p-2"
|
|
value={part[field]}
|
|
onChange={(e) =>
|
|
partChange(part.id, {
|
|
[field]: e.target.value,
|
|
})
|
|
}
|
|
/>
|
|
</label>
|
|
))}
|
|
<label>
|
|
Esta parcela já está incluída no montante
|
|
inicial?
|
|
<select
|
|
className="block rounded border p-2"
|
|
value={part.included}
|
|
onChange={(e) =>
|
|
partChange(part.id, {
|
|
included: e.target
|
|
.value as CompositionPart['included'],
|
|
})
|
|
}
|
|
>
|
|
<option value="">Por indicar</option>
|
|
<option value="yes">
|
|
Sim — conservar sem somar novamente
|
|
</option>
|
|
<option value="no">
|
|
Não — acrescentar uma vez
|
|
</option>
|
|
</select>
|
|
</label>
|
|
{part.treatment && (
|
|
<label>
|
|
{part.treatment === 'tax_component'
|
|
? 'É imposto,direito,taxa ou outra imposição que integra esta contraprestação nos termos15(5)(a)?'
|
|
: 'É despesa acessória debitada por conta do cliente,relativa a comissões,embalagem,transporte ou seguros nos termos15(5)(b)?'}
|
|
<select
|
|
className="block rounded border p-2"
|
|
value={part.natureConfirmed}
|
|
onChange={(e) =>
|
|
partChange(part.id, {
|
|
natureConfirmed: e.target
|
|
.value as CompositionPart['natureConfirmed'],
|
|
})
|
|
}
|
|
>
|
|
<option value="">
|
|
Por indicar
|
|
</option>
|
|
<option value="yes">Sim</option>
|
|
<option value="no">Não</option>
|
|
</select>
|
|
</label>
|
|
)}
|
|
<label>
|
|
A parcela exclui o próprio IVA?
|
|
<select
|
|
className="block rounded border p-2"
|
|
value={part.ownVatExcluded}
|
|
onChange={(e) =>
|
|
partChange(part.id, {
|
|
ownVatExcluded: e.target
|
|
.value as CompositionPart['ownVatExcluded'],
|
|
})
|
|
}
|
|
>
|
|
<option value="">Por indicar</option>
|
|
<option value="yes">Sim</option>
|
|
<option value="no">Não</option>
|
|
</select>
|
|
</label>
|
|
<p>
|
|
Mudar a natureza conserva os montantes e
|
|
referências,mas exige rever a
|
|
correspondência da parcela. Cada parcela
|
|
será considerada uma única vez nesta
|
|
operação.
|
|
</p>
|
|
</fieldset>
|
|
))}
|
|
</details>
|
|
);
|
|
})}
|
|
{!!book.rows.length && (
|
|
<button
|
|
type="button"
|
|
className="rounded bg-emerald-800 p-2 text-white"
|
|
onClick={() => {
|
|
try {
|
|
save(recordCompositions(draft, book));
|
|
} catch (cause) {
|
|
setError(String(cause));
|
|
}
|
|
}}
|
|
>
|
|
Compor bases e guardar versão
|
|
</button>
|
|
)}
|
|
{book.result && (
|
|
<>
|
|
<p role="status">
|
|
{compositionCurrent(draft, book)
|
|
? 'Composições correspondentes às entradas guardadas.'
|
|
: 'Entradas alteradas;composições históricas conservadas. Reveja e determine novamente.'}{' '}
|
|
{book.history?.length ?? 0} versões anteriores. IVA e
|
|
campo11 não são preenchidos por estas bases.
|
|
</p>
|
|
<dl>
|
|
{compositionReportRows(draft).map(
|
|
([, label, value, origin], index) => (
|
|
<div key={index}>
|
|
<dt className="font-semibold">{label}</dt>
|
|
<dd>
|
|
{value}
|
|
<small className="block">
|
|
{origin}
|
|
</small>
|
|
</dd>
|
|
</div>
|
|
),
|
|
)}
|
|
</dl>
|
|
</>
|
|
)}
|
|
</details>
|
|
);
|
|
}
|