@@ -0,0 +1,290 @@
|
||||
import { Head, Link, router, useForm } from '@inertiajs/react';
|
||||
import {
|
||||
Panel,
|
||||
PageTitle,
|
||||
FormErrors,
|
||||
controlClass,
|
||||
} from '@/components/procurement';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import {
|
||||
ComparisonPolicySummary,
|
||||
type ComparisonPolicy,
|
||||
} from '@/components/comparison-policy-summary';
|
||||
import { useProcurementPreparationIdentity } from '@/hooks/use-procurement-preparation-identity';
|
||||
import { usePreparationDirty } from '@/hooks/use-preparation-dirty';
|
||||
import { hasPreparationChanges } from '@/lib/requisition-history';
|
||||
|
||||
type Item = {
|
||||
id: number;
|
||||
description: string;
|
||||
catalogue_snapshot?: { stock_item_id?: number } | null;
|
||||
};
|
||||
type Policy = {
|
||||
id: number;
|
||||
version: number;
|
||||
stock_item_id: number;
|
||||
item: { name: string; unit: string };
|
||||
warehouse: { name: string };
|
||||
minimum_stock_milli: number;
|
||||
usage_context: string | null;
|
||||
};
|
||||
type Evidence = {
|
||||
id: number;
|
||||
version: number;
|
||||
reason: string;
|
||||
data: { requisition_item_id: number };
|
||||
snapshot: {
|
||||
stock_policy: {
|
||||
minimum_stock_milli: number;
|
||||
usage_context: string | null;
|
||||
warehouse: { name: string };
|
||||
};
|
||||
availability: { free_milli: number | null };
|
||||
author: { name: string };
|
||||
};
|
||||
};
|
||||
type Props = {
|
||||
requisition: {
|
||||
id: number;
|
||||
number: string;
|
||||
priority: string;
|
||||
justification: string;
|
||||
};
|
||||
cycle: number;
|
||||
canRecord: boolean;
|
||||
items: Item[];
|
||||
comparisonPolicy: ComparisonPolicy;
|
||||
policies: {
|
||||
data: Policy[];
|
||||
prev_page_url: string | null;
|
||||
next_page_url: string | null;
|
||||
};
|
||||
evidence: Evidence[];
|
||||
eligible: Evidence[];
|
||||
};
|
||||
|
||||
export default function UrgentStock(props: Props) {
|
||||
const identity = useProcurementPreparationIdentity(
|
||||
`urgent-stock:${props.requisition.id}:${props.cycle}:${props.comparisonPolicy.revision}`,
|
||||
);
|
||||
const enabled =
|
||||
props.requisition.priority === 'urgent' &&
|
||||
props.comparisonPolicy.urgent_minimum_proposals !== undefined;
|
||||
return (
|
||||
<>
|
||||
<Head title="Stock mínimo e cotações urgentes" />
|
||||
<div className="mx-auto flex w-full max-w-5xl flex-col gap-6 p-5 lg:p-8">
|
||||
<PageTitle
|
||||
eyebrow={props.requisition.number}
|
||||
title="Stock mínimo e cotações urgentes"
|
||||
description="Documente a política de stock aplicável ao artigo e ao local de utilização. O servidor confere se o mínimo foi atingido; orçamento, avaliações e aprovações mantêm-se."
|
||||
/>
|
||||
<Link
|
||||
className="underline"
|
||||
href={`/requisicoes/${props.requisition.id}/cotacoes`}
|
||||
>
|
||||
Voltar às cotações
|
||||
</Link>
|
||||
<Panel title="Regra preservada na requisição">
|
||||
<ComparisonPolicySummary policy={props.comparisonPolicy} />
|
||||
<p className="mt-3 text-sm">
|
||||
{!enabled
|
||||
? 'Esta requisição não permite redução urgente de cotações.'
|
||||
: props.eligible.length > 0
|
||||
? 'Stock mínimo documentado para todas as linhas deste ciclo. A comparação pode aplicar o número urgente configurado.'
|
||||
: 'Mantém-se o número normal de propostas enquanto faltar a conferência válida de alguma linha.'}
|
||||
</p>
|
||||
</Panel>
|
||||
{props.items.map((item) => {
|
||||
const previous = props.evidence.find(
|
||||
(record) => record.data.requisition_item_id === item.id,
|
||||
);
|
||||
const policies = props.policies.data.filter(
|
||||
(policy) =>
|
||||
policy.stock_item_id ===
|
||||
item.catalogue_snapshot?.stock_item_id,
|
||||
);
|
||||
return (
|
||||
<Panel title={item.description} key={item.id}>
|
||||
{previous && (
|
||||
<div className="mb-4 space-y-1 text-sm">
|
||||
<p>
|
||||
Conferência #{previous.id} ·{' '}
|
||||
{previous.snapshot.author.name} ·{' '}
|
||||
{
|
||||
previous.snapshot.stock_policy
|
||||
.warehouse.name
|
||||
}
|
||||
</p>
|
||||
<p>
|
||||
Mínimo documentado:{' '}
|
||||
{previous.snapshot.stock_policy
|
||||
.minimum_stock_milli / 1000}
|
||||
; stock livre no momento:{' '}
|
||||
{previous.snapshot.availability
|
||||
.free_milli === null
|
||||
? 'Indeterminado'
|
||||
: previous.snapshot.availability
|
||||
.free_milli / 1000}
|
||||
.
|
||||
</p>
|
||||
{previous.snapshot.stock_policy
|
||||
.usage_context && (
|
||||
<p>
|
||||
{
|
||||
previous.snapshot.stock_policy
|
||||
.usage_context
|
||||
}
|
||||
</p>
|
||||
)}
|
||||
<p>{previous.reason}</p>
|
||||
</div>
|
||||
)}
|
||||
{enabled && props.canRecord && (
|
||||
<EvidenceForm
|
||||
key={`${identity}:${item.id}:${previous?.version ?? 0}`}
|
||||
identity={`${identity}:${item.id}:${previous?.version ?? 0}`}
|
||||
requestId={props.requisition.id}
|
||||
itemId={item.id}
|
||||
version={previous?.version ?? 0}
|
||||
policies={policies}
|
||||
/>
|
||||
)}
|
||||
</Panel>
|
||||
);
|
||||
})}
|
||||
<div className="flex gap-4">
|
||||
{props.policies.prev_page_url && (
|
||||
<Link
|
||||
preserveState
|
||||
preserveScroll
|
||||
href={props.policies.prev_page_url}
|
||||
>
|
||||
Políticas anteriores
|
||||
</Link>
|
||||
)}
|
||||
{props.policies.next_page_url && (
|
||||
<Link
|
||||
preserveState
|
||||
preserveScroll
|
||||
href={props.policies.next_page_url}
|
||||
>
|
||||
Mais políticas
|
||||
</Link>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
function EvidenceForm({
|
||||
identity,
|
||||
requestId,
|
||||
itemId,
|
||||
version,
|
||||
policies,
|
||||
}: {
|
||||
identity: string;
|
||||
requestId: number;
|
||||
itemId: number;
|
||||
version: number;
|
||||
policies: Policy[];
|
||||
}) {
|
||||
const initial = {
|
||||
item_id: itemId,
|
||||
policy_id: policies.length === 1 ? String(policies[0].id) : '',
|
||||
version,
|
||||
reason: '',
|
||||
};
|
||||
const form = useForm(`${identity}:form`, {
|
||||
...initial,
|
||||
operation_key: crypto.randomUUID(),
|
||||
});
|
||||
const dirty = hasPreparationChanges(
|
||||
{ ...initial, operation_key: '' },
|
||||
{ ...form.data, operation_key: '' },
|
||||
);
|
||||
const ref = usePreparationDirty(dirty);
|
||||
return (
|
||||
<form
|
||||
ref={ref}
|
||||
data-preparation-dirty={dirty ? 'true' : 'false'}
|
||||
className="space-y-4"
|
||||
onSubmit={(event) => {
|
||||
event.preventDefault();
|
||||
form.post(`/requisicoes/${requestId}/urgencia-stock`, {
|
||||
preserveScroll: true,
|
||||
preserveState: true,
|
||||
onSuccess: () => {
|
||||
const cleared = {
|
||||
...initial,
|
||||
operation_key: crypto.randomUUID(),
|
||||
};
|
||||
form.setData(cleared);
|
||||
form.clearErrors();
|
||||
router.remember(cleared, `${identity}:form:data`);
|
||||
router.remember({}, `${identity}:form:errors`);
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
<FormErrors errors={form.errors} />
|
||||
<label className="block space-y-1 text-sm">
|
||||
Política de mínimo aplicável ao artigo/local
|
||||
<select
|
||||
className={controlClass}
|
||||
required
|
||||
value={form.data.policy_id}
|
||||
onChange={(event) =>
|
||||
form.setData('policy_id', event.target.value)
|
||||
}
|
||||
>
|
||||
<option value="">Seleccione a política aprovada</option>
|
||||
{form.data.policy_id &&
|
||||
!policies.some(
|
||||
(policy) =>
|
||||
String(policy.id) === form.data.policy_id,
|
||||
) && (
|
||||
<option value={form.data.policy_id}>
|
||||
Política preservada de outra página — confira a
|
||||
fonte
|
||||
</option>
|
||||
)}
|
||||
{policies.map((policy) => (
|
||||
<option key={policy.id} value={policy.id}>
|
||||
{policy.warehouse.name} · mínimo{' '}
|
||||
{policy.minimum_stock_milli / 1000}{' '}
|
||||
{policy.item.unit} ·{' '}
|
||||
{policy.usage_context ?? policy.item.name} · versão{' '}
|
||||
{policy.version}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
<label className="block space-y-1 text-sm">
|
||||
Fundamento da urgência e da utilização desta política
|
||||
<textarea
|
||||
className={`${controlClass} h-24 py-3`}
|
||||
required
|
||||
minLength={10}
|
||||
maxLength={4000}
|
||||
value={form.data.reason}
|
||||
onChange={(event) =>
|
||||
form.setData('reason', event.target.value)
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
<Button
|
||||
disabled={
|
||||
form.processing ||
|
||||
(!form.data.policy_id && policies.length === 0)
|
||||
}
|
||||
>
|
||||
Conferir stock mínimo
|
||||
</Button>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
UrgentStock.layout = {
|
||||
breadcrumbs: [{ title: 'Stock mínimo e cotações urgentes', href: '#' }],
|
||||
};
|
||||
Reference in New Issue
Block a user