This commit is contained in:
2026-11-03 11:51:44 +02:00
parent badaada043
commit e753f7a0a4
309 changed files with 14537 additions and 529 deletions
+107 -25
View File
@@ -1,5 +1,5 @@
import { Head, Link, useForm } from '@inertiajs/react';
import { useState } from 'react';
import { Head, Link, router, useForm } from '@inertiajs/react';
import { useEffect, useState } from 'react';
import {
FormErrors,
PageTitle,
@@ -7,6 +7,9 @@ import {
controlClass,
} from '@/components/procurement';
import { Button } from '@/components/ui/button';
import SupplyPagination, {
type SupplyPageInfo,
} from '@/components/supply-pagination';
type Policy = {
key: string;
@@ -20,6 +23,11 @@ type Policy = {
unit?: string;
};
type Suggestion = {
canApprove?: boolean;
canReturn?: boolean;
canConvert?: boolean;
canCancel?: boolean;
canClose?: boolean;
id: number;
record_key: string;
version: number;
@@ -73,6 +81,11 @@ type Suggestion = {
};
};
type Props = {
policySearch?: string;
policyFormKey?: string | null;
recordPagination?: SupplyPageInfo | null;
focusRecordId?: number | null;
focusChanged?: boolean;
actorId: number;
ready: boolean;
records: Suggestion[];
@@ -98,7 +111,14 @@ const quantity = (value: number) =>
function Prepare({ props, old }: { props: Props; old?: Suggestion }) {
const [selected, setSelected] = useState(old?.data.policy_key ?? '');
const option = props.policies.find((p) => p.key === selected);
const [policies, setPolicies] = useState(props.policies);
const [policySearch, setPolicySearch] = useState(props.policySearch ?? '');
const formKey = `suggestion:${old?.id ?? 'new'}`;
useEffect(() => {
if (!props.policyFormKey || props.policyFormKey === formKey)
setPolicies(props.policies);
}, [props.policyFormKey, props.policies, formKey]);
const option = policies.find((p) => p.key === selected);
const form = useForm({
operation_key: crypto.randomUUID(),
record_key: old?.record_key,
@@ -115,6 +135,25 @@ function Prepare({ props, old }: { props: Props; old?: Suggestion }) {
delivery_on: old?.data.manual_plan ? old.data.delivery_on : '',
},
});
const searchPolicies = () =>
router.get(
'/compras/fornecimento',
{
view: 'suggestions',
records_page: props.recordPagination?.current_page,
...(props.focusRecordId
? { record_id: props.focusRecordId }
: {}),
policy_search: policySearch,
selected_policy_key: selected || undefined,
policy_form_key: formKey,
},
{
only: ['policies', 'policySearch', 'policyFormKey'],
preserveState: true,
preserveScroll: true,
},
);
return (
<form
className="mt-4 space-y-4"
@@ -141,6 +180,42 @@ function Prepare({ props, old }: { props: Props; old?: Suggestion }) {
}}
>
<FormErrors errors={form.errors} />
<details className="rounded border p-3">
<summary className="cursor-pointer text-sm">
Procurar política aprovada
</summary>
<div className="mt-3 flex flex-wrap items-end gap-3">
<label className="grid gap-1 text-sm">
Código ou descrição do artigo ou armazém
<input
className={controlClass}
value={policySearch}
maxLength={100}
onChange={(event) =>
setPolicySearch(event.target.value)
}
onKeyDown={(event) => {
if (event.key === 'Enter') {
event.preventDefault();
searchPolicies();
}
}}
/>
</label>
<Button
type="button"
variant="outline"
onClick={searchPolicies}
>
Procurar
</Button>
</div>
<p className="mt-2 text-sm text-muted-foreground">
A selecção elegível e os dados introduzidos são conservados.
As condições da política continuam a ser verificadas antes
de preparar a compra.
</p>
</details>
<label className="grid gap-1 text-sm">
Artigo e local com política aprovada
<select
@@ -149,7 +224,7 @@ function Prepare({ props, old }: { props: Props; old?: Suggestion }) {
className={controlClass}
value={selected}
onChange={(e) => {
const p = props.policies.find(
const p = policies.find(
(o) => o.key === e.target.value,
);
setSelected(e.target.value);
@@ -163,7 +238,7 @@ function Prepare({ props, old }: { props: Props; old?: Suggestion }) {
}}
>
<option value="">Seleccionar política existente</option>
{props.policies.map((p) => (
{policies.map((p) => (
<option key={p.key} value={p.key}>
{p.label}
</option>
@@ -455,26 +530,14 @@ function Decisions({ props, record: r }: { props: Props; record: Suggestion }) {
reason: '',
data: { cost_center_id: '', confirm_exception: false },
});
const independent = !r.snapshot.contributors.includes(props.actorId);
const choices: [string, string][] = [];
if (props.canReview && independent) {
if (
['draft', 'returned'].includes(r.state) &&
!props.excess[r.record_key]
)
choices.push(['approve', 'Aprovar para requisitar']);
if (['draft', 'approved'].includes(r.state))
choices.push(['return', 'Devolver para actualização']);
if (r.state === 'converted')
choices.push(['close', 'Conferir e encerrar acompanhamento']);
}
if (props.canPrepare && ['draft', 'returned'].includes(r.state))
choices.push(['cancel', 'Cancelar sugestão pendente']);
if (
props.canConvert &&
r.state === 'approved' &&
r.snapshot.reviewed_by !== props.actorId
)
if (r.canApprove && !props.excess[r.record_key])
choices.push(['approve', 'Aprovar para requisitar']);
if (r.canReturn) choices.push(['return', 'Devolver para actualização']);
if (r.canClose)
choices.push(['close', 'Conferir e encerrar acompanhamento']);
if (r.canCancel) choices.push(['cancel', 'Cancelar sugestão pendente']);
if (r.canConvert)
choices.push(['convert', 'Criar requisição a partir desta aprovação']);
return (
<div className="mt-4 space-y-4">
@@ -620,7 +683,24 @@ export default function Suggestions(props: Props) {
{!props.ready && (
<p role="status">Instalação do fornecimento pendente.</p>
)}
{props.canPrepare && (
{props.focusRecordId && (
<div className="space-y-2 text-sm">
<Link
href="/compras/fornecimento?view=suggestions"
className="underline"
>
Ver todas as sugestões
</Link>
{props.focusChanged && (
<p role="status">
Esta sugestão foi actualizada. Está a consultar
a versão actual; as versões anteriores
permanecem no histórico.
</p>
)}
</div>
)}
{props.canPrepare && !props.focusRecordId && (
<Panel title="Preparar sugestão">
<details>
<summary className="cursor-pointer font-medium">
@@ -645,6 +725,7 @@ export default function Suggestions(props: Props) {
title={`${r.snapshot.item.code} · ${r.snapshot.item.name}`}
description={`${r.snapshot.warehouse.name} · ${states[r.state]} · Versão ${r.version}`}
>
<div id={`supply-suggestion-${r.id}`} />
<Facts record={r} detailed />
<p className="mt-3 text-sm">{r.reason}</p>
{r.data.requisition_id && (
@@ -690,6 +771,7 @@ export default function Suggestions(props: Props) {
</details>
</Panel>
))}
<SupplyPagination page={props.recordPagination} />
<p className="flex flex-wrap gap-5 text-sm">
<Link
className="underline"