@@ -0,0 +1,262 @@
|
||||
import { Head, Link, useForm, usePage } from '@inertiajs/react';
|
||||
import { useEffect, useRef } from 'react';
|
||||
import { hasPreparationChanges } from '@/lib/requisition-history';
|
||||
import {
|
||||
FormErrors,
|
||||
PageTitle,
|
||||
Panel,
|
||||
controlClass,
|
||||
} from '@/components/procurement';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { money } from '@/types/procurement';
|
||||
import type { Requisition } from '@/types/procurement';
|
||||
|
||||
type Allocation = {
|
||||
allocation_key: string;
|
||||
line_id: number;
|
||||
centre_id: number;
|
||||
subtotal_minor: number;
|
||||
total_minor: number;
|
||||
};
|
||||
const decimal = (value: number) =>
|
||||
`${Math.floor(value / 100)}.${String(value % 100).padStart(2, '0')}`;
|
||||
|
||||
export default function Distributions({
|
||||
requisition: r,
|
||||
centres,
|
||||
preparation,
|
||||
}: {
|
||||
requisition: Omit<Requisition, 'items'> & {
|
||||
items: (Requisition['items'][number] & { id: number })[];
|
||||
};
|
||||
centres: { id: number; code: string; name: string }[];
|
||||
preparation: { allocations: Allocation[]; reason: string } | null;
|
||||
}) {
|
||||
const actor = usePage().props.auth?.user;
|
||||
const initial = {
|
||||
operation_key: crypto.randomUUID(),
|
||||
process_version: r.process_version,
|
||||
reason: '',
|
||||
budget_distribution_preparation: (preparation?.allocations ?? []).map(
|
||||
(row) => ({
|
||||
allocation_key: row.allocation_key,
|
||||
line_id: row.line_id,
|
||||
centre_id: row.centre_id,
|
||||
subtotal: decimal(row.subtotal_minor),
|
||||
total: decimal(row.total_minor),
|
||||
}),
|
||||
),
|
||||
};
|
||||
const form = useForm(
|
||||
`distribution:${actor?.tenant_id}:${actor?.id}:${r.id}:${r.process_version}`,
|
||||
initial,
|
||||
);
|
||||
const formElement = useRef<HTMLFormElement>(null);
|
||||
const dirty = hasPreparationChanges(initial, form.data);
|
||||
useEffect(() => {
|
||||
window.dispatchEvent(
|
||||
new CustomEvent('sgc:context-dirty', {
|
||||
detail: { form: formElement.current, dirty },
|
||||
}),
|
||||
);
|
||||
}, [dirty]);
|
||||
const rows = form.data.budget_distribution_preparation;
|
||||
return (
|
||||
<>
|
||||
<Head title={`Distribuição · ${r.number}`} />
|
||||
<div className="mx-auto flex w-full max-w-5xl flex-col gap-6 p-5 lg:p-8">
|
||||
<PageTitle
|
||||
eyebrow="Requisição de compra"
|
||||
title="Distribuir por centros de custo"
|
||||
description={r.number}
|
||||
/>
|
||||
<p>
|
||||
Indique os valores de cada artigo por centro. As parcelas
|
||||
devem corresponder exactamente ao valor sem IVA e ao total
|
||||
do artigo. Guardar prepara a distribuição; a reserva ocorre
|
||||
ao submeter o pedido.
|
||||
</p>
|
||||
<FormErrors errors={form.errors} />
|
||||
<form
|
||||
ref={formElement}
|
||||
className="flex flex-col gap-6"
|
||||
onSubmit={(event) => {
|
||||
event.preventDefault();
|
||||
form.post(`/requisicoes/${r.id}/distribuicoes`);
|
||||
}}
|
||||
>
|
||||
{r.items.map((line) => (
|
||||
<Panel key={line.id} title={line.description}>
|
||||
<p>
|
||||
Sem IVA: {money(line.subtotal_minor)} · Total:{' '}
|
||||
{money(line.total_minor)}
|
||||
</p>
|
||||
{rows.map((row, index) =>
|
||||
row.line_id !== line.id ? null : (
|
||||
<fieldset
|
||||
key={row.allocation_key}
|
||||
className="my-4 grid gap-3 rounded border p-3 sm:grid-cols-3"
|
||||
>
|
||||
<legend>
|
||||
Parcela{' '}
|
||||
{
|
||||
rows.filter(
|
||||
(item, i) =>
|
||||
i <= index &&
|
||||
item.line_id ===
|
||||
line.id,
|
||||
).length
|
||||
}
|
||||
</legend>
|
||||
<label>
|
||||
Centro de custo
|
||||
<select
|
||||
required
|
||||
className={controlClass}
|
||||
value={row.centre_id || ''}
|
||||
onChange={(event) =>
|
||||
form.setData(
|
||||
'budget_distribution_preparation',
|
||||
rows.map((item, i) =>
|
||||
i === index
|
||||
? {
|
||||
...item,
|
||||
centre_id:
|
||||
Number(
|
||||
event
|
||||
.target
|
||||
.value,
|
||||
),
|
||||
}
|
||||
: item,
|
||||
),
|
||||
)
|
||||
}
|
||||
>
|
||||
<option value="">
|
||||
Seleccione
|
||||
</option>
|
||||
{centres.map((centre) => (
|
||||
<option
|
||||
key={centre.id}
|
||||
value={centre.id}
|
||||
>
|
||||
{centre.code} ·{' '}
|
||||
{centre.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
{(['subtotal', 'total'] as const).map(
|
||||
(field) => (
|
||||
<label key={field}>
|
||||
{field === 'subtotal'
|
||||
? 'Sem IVA (MZN)'
|
||||
: 'Total (MZN)'}
|
||||
<input
|
||||
required
|
||||
inputMode="decimal"
|
||||
pattern="[0-9]+([.][0-9]{1,2})?"
|
||||
className={controlClass}
|
||||
value={row[field]}
|
||||
onChange={(event) =>
|
||||
form.setData(
|
||||
'budget_distribution_preparation',
|
||||
rows.map(
|
||||
(
|
||||
item,
|
||||
i,
|
||||
) =>
|
||||
i ===
|
||||
index
|
||||
? {
|
||||
...item,
|
||||
[field]:
|
||||
event
|
||||
.target
|
||||
.value,
|
||||
}
|
||||
: item,
|
||||
),
|
||||
)
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
),
|
||||
)}
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
onClick={() =>
|
||||
form.setData(
|
||||
'budget_distribution_preparation',
|
||||
rows.filter(
|
||||
(_, i) => i !== index,
|
||||
),
|
||||
)
|
||||
}
|
||||
>
|
||||
Remover parcela
|
||||
</Button>
|
||||
</fieldset>
|
||||
),
|
||||
)}
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
disabled={rows.length >= 500}
|
||||
onClick={() =>
|
||||
form.setData(
|
||||
'budget_distribution_preparation',
|
||||
[
|
||||
...rows,
|
||||
{
|
||||
allocation_key:
|
||||
crypto.randomUUID(),
|
||||
line_id: line.id,
|
||||
centre_id: 0,
|
||||
subtotal: '',
|
||||
total: '',
|
||||
},
|
||||
],
|
||||
)
|
||||
}
|
||||
>
|
||||
Adicionar parcela
|
||||
</Button>
|
||||
</Panel>
|
||||
))}
|
||||
<label>
|
||||
Motivo da distribuição
|
||||
<textarea
|
||||
required
|
||||
minLength={10}
|
||||
maxLength={4000}
|
||||
className={controlClass}
|
||||
value={form.data.reason}
|
||||
onChange={(event) =>
|
||||
form.setData('reason', event.target.value)
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
<p className="text-sm">
|
||||
Se os artigos ou valores forem alterados, será
|
||||
necessário conferir novamente a distribuição. Use ponto
|
||||
para separar os centavos. Nenhuma diferença será
|
||||
compensada automaticamente.
|
||||
</p>
|
||||
<div className="flex flex-wrap gap-3">
|
||||
<Button disabled={form.processing || rows.length === 0}>
|
||||
Guardar distribuição
|
||||
</Button>
|
||||
<Button asChild variant="outline">
|
||||
<Link href={`/requisicoes/${r.id}`}>
|
||||
Voltar ao pedido
|
||||
</Link>
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Head, Link, useForm, usePage } from '@inertiajs/react';
|
||||
import { hasPreparationChanges } from '@/lib/requisition-history';
|
||||
import { Plus, Save, Trash2 } from 'lucide-react';
|
||||
import { useEffect, useRef } from 'react';
|
||||
import {
|
||||
@@ -100,7 +101,10 @@ export default function RequisitionForm({
|
||||
catalogue?: CatalogueOptions;
|
||||
incompleteDraftsAvailable?: boolean;
|
||||
}) {
|
||||
const ready = usePage().props.internalRequisitionReady === true;
|
||||
const page = usePage();
|
||||
const ready = page.props.internalRequisitionReady === true;
|
||||
const actor = page.props.auth?.user;
|
||||
const historyKey = `requisition:${String(actor?.tenant_id ?? 'none')}:${actor?.id ?? 'none'}:${requisition?.id ?? 'new'}:${requisition?.process_version ?? 0}`;
|
||||
const prepared = requisition?.draft_input?.values;
|
||||
const preparedLines = prepared?.items?.map(
|
||||
(row) =>
|
||||
@@ -113,7 +117,7 @@ export default function RequisitionForm({
|
||||
]),
|
||||
) as Line,
|
||||
);
|
||||
const form = useForm({
|
||||
const initialData = {
|
||||
operation_key: ready ? crypto.randomUUID() : null,
|
||||
process_version: requisition?.process_version ?? null,
|
||||
cost_center_id:
|
||||
@@ -149,15 +153,17 @@ export default function RequisitionForm({
|
||||
production_area: i.production_area ?? '',
|
||||
downtime_started_at: i.downtime_started_at?.slice(0, 16) ?? '',
|
||||
})) ?? [emptyLine(defaultTaxRate)],
|
||||
});
|
||||
};
|
||||
const form = useForm(historyKey, initialData);
|
||||
const preparationDirty = hasPreparationChanges(initialData, form.data);
|
||||
const formElement = useRef<HTMLFormElement>(null);
|
||||
useEffect(() => {
|
||||
window.dispatchEvent(
|
||||
new CustomEvent('sgc:context-dirty', {
|
||||
detail: { form: formElement.current, dirty: form.isDirty },
|
||||
detail: { form: formElement.current, dirty: preparationDirty },
|
||||
}),
|
||||
);
|
||||
}, [form.isDirty]);
|
||||
}, [preparationDirty]);
|
||||
const changeLine = (index: number, field: keyof Line, value: string) =>
|
||||
form.setData(
|
||||
'items',
|
||||
@@ -319,7 +325,7 @@ export default function RequisitionForm({
|
||||
</Panel>
|
||||
<Panel
|
||||
title="Artigos e serviços"
|
||||
description="Preços em MZN. Indique a taxa de IVA aplicável; o valor inicial de 0% deve ser revisto por Finanças."
|
||||
description="Preços em MZN. Confira a taxa de IVA aplicável a cada artigo antes de submeter."
|
||||
>
|
||||
<div className="space-y-5">
|
||||
{catalogue?.ready && (
|
||||
|
||||
@@ -26,6 +26,13 @@ const actions: Record<string, string> = {
|
||||
'requisition.automatically_created':
|
||||
'Rascunho de reposição criado automaticamente',
|
||||
'requisition.updated': 'Rascunho actualizado',
|
||||
'requisition.allocations_prepared': 'Distribuição preparada',
|
||||
'budget.distribution_reserve': 'Reserva distribuída registada',
|
||||
'budget.distribution_award': 'Distribuição da adjudicação confirmada',
|
||||
'budget.distribution_commit': 'Compromisso distribuído registado',
|
||||
'budget.distribution_release': 'Reserva distribuída libertada',
|
||||
'budget.distribution_cancel': 'Compromisso distribuído cancelado',
|
||||
'budget.distribution_amended': 'Compromisso reafectado por Finanças',
|
||||
'requisition.submitted': 'Pedido submetido',
|
||||
'approval.approved': 'Etapa aprovada',
|
||||
'approval.rejected': 'Pedido rejeitado',
|
||||
@@ -44,10 +51,13 @@ export default function Show({
|
||||
automaticSource,
|
||||
approvalRouting,
|
||||
canRefreshRouting = false,
|
||||
canPrepareDistributions = false,
|
||||
}: {
|
||||
canPrepareDistributions?: boolean;
|
||||
requisition: Requisition;
|
||||
canRefreshRouting?: boolean;
|
||||
approvalRouting?: {
|
||||
label?: string | null;
|
||||
status: string;
|
||||
step_id: number | null;
|
||||
role: string | null;
|
||||
@@ -150,6 +160,13 @@ export default function Show({
|
||||
}
|
||||
/>
|
||||
<FormErrors errors={submission.errors} />
|
||||
{canPrepareDistributions && (
|
||||
<Button asChild variant="outline">
|
||||
<Link href={`/requisicoes/${r.id}/distribuicoes`}>
|
||||
Distribuir por centros de custo
|
||||
</Link>
|
||||
</Button>
|
||||
)}
|
||||
{automaticSource && (
|
||||
<Panel title="Origem na reposição automática">
|
||||
<p>
|
||||
@@ -584,9 +601,11 @@ export default function Show({
|
||||
>
|
||||
<p className="text-sm">
|
||||
Etapa:{' '}
|
||||
{roleLabels[
|
||||
approvalRouting.role ?? ''
|
||||
] ?? approvalRouting.role}
|
||||
{approvalRouting.label ??
|
||||
roleLabels[
|
||||
approvalRouting.role ?? ''
|
||||
] ??
|
||||
approvalRouting.role}
|
||||
.
|
||||
</p>
|
||||
{canRefreshRouting && (
|
||||
@@ -702,7 +721,9 @@ export default function Show({
|
||||
</span>
|
||||
<div>
|
||||
<p className="text-sm font-medium">
|
||||
{roleLabels[s.role]}
|
||||
{s.routing?.name ??
|
||||
roleLabels[s.role] ??
|
||||
s.role}
|
||||
</p>
|
||||
<p className="mt-1 text-xs text-muted-foreground">
|
||||
{s.decision === 'pending'
|
||||
|
||||
Reference in New Issue
Block a user