@@ -39,6 +39,19 @@ type Line = {
|
||||
unit_price: string;
|
||||
tax_rate: string;
|
||||
};
|
||||
type OrderPartOption = {
|
||||
origin: number;
|
||||
allocation_key: string;
|
||||
line_id: number;
|
||||
centre_id: number;
|
||||
description: string;
|
||||
};
|
||||
type OrderPartInput = {
|
||||
origin: number;
|
||||
allocation_key: string;
|
||||
subtotal: string;
|
||||
total: string;
|
||||
};
|
||||
type Invoice = {
|
||||
id: number;
|
||||
number: string;
|
||||
@@ -81,6 +94,8 @@ export default function Invoices({
|
||||
supplier: string;
|
||||
items: Source[];
|
||||
budgetDistributionOptions?: BudgetDistributionOption[] | null;
|
||||
orderParts?: boolean;
|
||||
orderPartsBudgetOptions?: OrderPartOption[] | null;
|
||||
}[];
|
||||
canManage: boolean;
|
||||
focusInvoiceId?: number | null;
|
||||
@@ -139,8 +154,22 @@ export default function Invoices({
|
||||
})) ?? []) as Line[],
|
||||
};
|
||||
const form = useForm(historyKey, initialData);
|
||||
const [partAllocations, setPartAllocations] = useRemember<OrderPartInput[]>(
|
||||
(
|
||||
orders.find((source) => source.id === focusOrderId)
|
||||
?.orderPartsBudgetOptions ?? []
|
||||
).map((row) => ({
|
||||
origin: row.origin,
|
||||
allocation_key: row.allocation_key,
|
||||
subtotal: '',
|
||||
total: '',
|
||||
})),
|
||||
`${historyKey}:part-allocations`,
|
||||
);
|
||||
const formElement = useRef<HTMLFormElement>(null);
|
||||
const dirty = hasPreparationChanges(initialData, form.data);
|
||||
const dirty =
|
||||
hasPreparationChanges(initialData, form.data) ||
|
||||
partAllocations.some((row) => row.subtotal !== '' || row.total !== '');
|
||||
useEffect(() => {
|
||||
window.dispatchEvent(
|
||||
new CustomEvent('sgc:context-dirty', {
|
||||
@@ -157,9 +186,38 @@ export default function Invoices({
|
||||
(source) => source.id === line.purchase_order_item_id,
|
||||
)?.requisition_item_id,
|
||||
);
|
||||
const activeAllocations = form.data.budget_distribution_preparation.filter(
|
||||
(row) => activeLineIds.includes(row.line_id),
|
||||
);
|
||||
const activeAllocations = (
|
||||
form.data.budget_distribution_preparation ?? []
|
||||
).filter((row) => activeLineIds.includes(row.line_id));
|
||||
const updatePartAmount = (
|
||||
option: OrderPartOption,
|
||||
field: 'subtotal' | 'total',
|
||||
value: string,
|
||||
) => {
|
||||
setPartAllocations((rows) =>
|
||||
rows.some(
|
||||
(row) =>
|
||||
row.origin === option.origin &&
|
||||
row.allocation_key === option.allocation_key,
|
||||
)
|
||||
? rows.map((row) =>
|
||||
row.origin !== option.origin ||
|
||||
row.allocation_key !== option.allocation_key
|
||||
? row
|
||||
: { ...row, [field]: value },
|
||||
)
|
||||
: [
|
||||
...rows,
|
||||
{
|
||||
origin: option.origin,
|
||||
allocation_key: option.allocation_key,
|
||||
subtotal: '',
|
||||
total: '',
|
||||
[field]: value,
|
||||
},
|
||||
],
|
||||
);
|
||||
};
|
||||
return (
|
||||
<>
|
||||
<Head title="Facturas" />
|
||||
@@ -245,13 +303,29 @@ export default function Invoices({
|
||||
e.preventDefault();
|
||||
form.transform((d) => ({
|
||||
...d,
|
||||
budget_distribution_preparation:
|
||||
d.budget_distribution_preparation.filter(
|
||||
(row) =>
|
||||
activeLineIds.includes(
|
||||
row.line_id,
|
||||
),
|
||||
),
|
||||
...(order?.orderParts
|
||||
? {
|
||||
order_part_allocations:
|
||||
partAllocations.filter(
|
||||
(row) =>
|
||||
order.orderPartsBudgetOptions?.some(
|
||||
(option) =>
|
||||
option.origin ===
|
||||
row.origin &&
|
||||
option.allocation_key ===
|
||||
row.allocation_key &&
|
||||
activeLineIds.includes(
|
||||
option.line_id,
|
||||
),
|
||||
),
|
||||
),
|
||||
}
|
||||
: {}),
|
||||
budget_distribution_preparation: (
|
||||
d.budget_distribution_preparation ?? []
|
||||
).filter((row) =>
|
||||
activeLineIds.includes(row.line_id),
|
||||
),
|
||||
items: d.items
|
||||
.filter((l) => l.quantity !== '')
|
||||
.map(
|
||||
@@ -281,6 +355,21 @@ export default function Invoices({
|
||||
value={orderId}
|
||||
onChange={(e) => {
|
||||
setOrderId(e.target.value);
|
||||
setPartAllocations(
|
||||
(
|
||||
orders.find(
|
||||
(source) =>
|
||||
String(source.id) ===
|
||||
e.target.value,
|
||||
)?.orderPartsBudgetOptions ?? []
|
||||
).map((row) => ({
|
||||
origin: row.origin,
|
||||
allocation_key:
|
||||
row.allocation_key,
|
||||
subtotal: '',
|
||||
total: '',
|
||||
})),
|
||||
);
|
||||
form.setData('reason', '');
|
||||
form.setData(
|
||||
'budget_distribution_preparation',
|
||||
@@ -475,7 +564,11 @@ export default function Invoices({
|
||||
onChange={(rows) =>
|
||||
form.setData(
|
||||
'budget_distribution_preparation',
|
||||
form.data.budget_distribution_preparation.map(
|
||||
(
|
||||
form.data
|
||||
.budget_distribution_preparation ??
|
||||
[]
|
||||
).map(
|
||||
(row) =>
|
||||
rows.find(
|
||||
(changed) =>
|
||||
@@ -488,7 +581,126 @@ export default function Invoices({
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
<Button disabled={!orderId || form.processing}>
|
||||
{order?.orderParts && (
|
||||
<fieldset className="grid min-w-0 gap-3 rounded-lg border p-4">
|
||||
<legend className="px-2 font-semibold">
|
||||
Imputação pelas origens da ordem
|
||||
</legend>
|
||||
<p>
|
||||
Indique em MZN a parcela de cada linha
|
||||
efectivamente facturada. A ordem e a
|
||||
factura mantêm um único documento; o
|
||||
consumo fica separado pelas origens
|
||||
aprovadas.
|
||||
</p>
|
||||
{order.orderPartsBudgetOptions == null && (
|
||||
<p role="status">
|
||||
A preparação de novas facturas desta
|
||||
ordem está temporariamente
|
||||
indisponível. Os documentos
|
||||
anteriores continuam em consulta.
|
||||
</p>
|
||||
)}
|
||||
{(order.orderPartsBudgetOptions ?? [])
|
||||
.filter((option) =>
|
||||
activeLineIds.includes(
|
||||
option.line_id,
|
||||
),
|
||||
)
|
||||
.map((option) => {
|
||||
const index =
|
||||
partAllocations.findIndex(
|
||||
(row) =>
|
||||
row.origin ===
|
||||
option.origin &&
|
||||
row.allocation_key ===
|
||||
option.allocation_key,
|
||||
);
|
||||
const row = partAllocations[index];
|
||||
return (
|
||||
<div
|
||||
key={`${option.origin}:${option.allocation_key}`}
|
||||
className="grid min-w-0 gap-3 rounded border p-3 sm:grid-cols-2"
|
||||
>
|
||||
<p className="break-words sm:col-span-2">
|
||||
Origem #{option.origin}{' '}
|
||||
· {option.description} ·
|
||||
Centro #
|
||||
{option.centre_id}
|
||||
</p>
|
||||
<label className="grid gap-1">
|
||||
Sem IVA (MZN)
|
||||
<Input
|
||||
required
|
||||
inputMode="decimal"
|
||||
value={
|
||||
row?.subtotal ??
|
||||
''
|
||||
}
|
||||
onChange={(event) =>
|
||||
updatePartAmount(
|
||||
option,
|
||||
'subtotal',
|
||||
event.target
|
||||
.value,
|
||||
)
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
<label className="grid gap-1">
|
||||
Com IVA (MZN)
|
||||
<Input
|
||||
required
|
||||
inputMode="decimal"
|
||||
value={
|
||||
row?.total ?? ''
|
||||
}
|
||||
onChange={(event) =>
|
||||
updatePartAmount(
|
||||
option,
|
||||
'total',
|
||||
event.target
|
||||
.value,
|
||||
)
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
<label className="grid gap-1">
|
||||
Fundamento da imputação
|
||||
<textarea
|
||||
className="rounded-md border p-2"
|
||||
required
|
||||
minLength={10}
|
||||
maxLength={4000}
|
||||
value={form.data.reason}
|
||||
onChange={(event) =>
|
||||
form.setData(
|
||||
'reason',
|
||||
event.target.value,
|
||||
)
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
Os montantes têm de coincidir com as
|
||||
linhas nativas e com o compromisso ainda
|
||||
disponível. Não há compensação
|
||||
automática. A validação exige outra
|
||||
pessoa de Finanças.
|
||||
</p>
|
||||
</fieldset>
|
||||
)}
|
||||
<Button
|
||||
disabled={
|
||||
!orderId ||
|
||||
form.processing ||
|
||||
(order?.orderParts === true &&
|
||||
order.orderPartsBudgetOptions == null)
|
||||
}
|
||||
>
|
||||
Registar para conferência independente
|
||||
</Button>
|
||||
</form>
|
||||
|
||||
Reference in New Issue
Block a user