317 lines
13 KiB
TypeScript
317 lines
13 KiB
TypeScript
import { router } from '@inertiajs/react';
|
|
import { useState } from 'react';
|
|
import {
|
|
ReceiptConversionFields,
|
|
type ReceiptConversion,
|
|
} from '@/components/receipt-conversion';
|
|
import { controlClass, Panel } from '@/components/procurement';
|
|
import { convertedQuantity } from '@/components/receipt-conversion';
|
|
import type { FrozenOrderAgreement } from '@/types/purchase';
|
|
import { Button } from '@/components/ui/button';
|
|
import { Input } from '@/components/ui/input';
|
|
import { money } from '@/types/procurement';
|
|
|
|
export type OrderAgreementOption = {
|
|
contract?: {
|
|
reference: string;
|
|
version: number;
|
|
currency: string;
|
|
max_order_value_minor: number | null;
|
|
} | null;
|
|
id: number;
|
|
version: number;
|
|
reference: string;
|
|
stock_item_id: number;
|
|
warehouse: string | null;
|
|
unit: string;
|
|
currency: string;
|
|
max_order_value_minor: number | null;
|
|
max_order_quantity_milli: number | null;
|
|
};
|
|
export type OrderAgreementLine = {
|
|
id: number;
|
|
description: string;
|
|
unit: string;
|
|
stock_item_id: number | null;
|
|
};
|
|
export type OrderAgreementBinding = {
|
|
requisition_item_id: number;
|
|
agreement_id: number;
|
|
conversion?: ReceiptConversion;
|
|
};
|
|
|
|
export function AgreementOrderLimitSummary({
|
|
bindings,
|
|
}: {
|
|
bindings?: Record<string, FrozenOrderAgreement>;
|
|
}) {
|
|
const agreements = Array.from(
|
|
new Map(
|
|
Object.values(bindings ?? {}).map((row) => [row.agreement_id, row]),
|
|
).values(),
|
|
);
|
|
if (!agreements.length) return null;
|
|
return (
|
|
<Panel title="Acordos e limites preservados nesta ordem">
|
|
<p className="mb-3 text-sm">
|
|
Cada ordem pode utilizar o limite completo. A conferência
|
|
abrange apenas as linhas expressamente ligadas ao mesmo acordo,
|
|
sem acumular outras ordens.
|
|
</p>
|
|
{Array.from(
|
|
new Map(
|
|
agreements
|
|
.filter((row) => row.contract)
|
|
.map((row) => [row.contract!.id, row.contract!]),
|
|
).values(),
|
|
).map((contract) => (
|
|
<div key={contract.id} className="border-t py-3 text-sm">
|
|
<p className="font-semibold">
|
|
Contrato global: {contract.reference} · versão{' '}
|
|
{contract.version}
|
|
</p>
|
|
<p>
|
|
Total com impostos dos artigos ligados:{' '}
|
|
{money(
|
|
contract.checked_order_value_minor,
|
|
contract.currency,
|
|
)}{' '}
|
|
· Limite global por ordem:{' '}
|
|
{contract.max_order_value_minor === null
|
|
? 'Não definido'
|
|
: money(
|
|
contract.max_order_value_minor,
|
|
contract.currency,
|
|
)}
|
|
</p>
|
|
</div>
|
|
))}
|
|
{agreements.map((row) => (
|
|
<div
|
|
key={row.agreement_id}
|
|
className="space-y-2 border-t py-3 text-sm"
|
|
>
|
|
<p className="font-semibold">
|
|
{row.reference} · versão {row.version}
|
|
</p>
|
|
<p>
|
|
Total com impostos conferido:{' '}
|
|
{money(row.checked_order_value_minor, row.currency)} ·
|
|
Limite por ordem:{' '}
|
|
{row.max_order_value_minor === null
|
|
? 'Não definido'
|
|
: money(row.max_order_value_minor, row.currency)}
|
|
</p>
|
|
<p>
|
|
Quantidade conferida:{' '}
|
|
{convertedQuantity(row.checked_order_quantity_milli)}{' '}
|
|
{row.unit} · Limite por ordem:{' '}
|
|
{row.max_order_quantity_milli === null
|
|
? 'Não definido'
|
|
: `${convertedQuantity(String(row.max_order_quantity_milli))} ${row.unit}`}
|
|
</p>
|
|
</div>
|
|
))}
|
|
</Panel>
|
|
);
|
|
}
|
|
|
|
export default function AgreementOrderBindings({
|
|
requestId,
|
|
lines,
|
|
options,
|
|
value,
|
|
onChange,
|
|
initialSearch = '',
|
|
}: {
|
|
requestId: number;
|
|
lines: OrderAgreementLine[];
|
|
options: OrderAgreementOption[];
|
|
value: OrderAgreementBinding[];
|
|
onChange: (value: OrderAgreementBinding[]) => void;
|
|
initialSearch?: string;
|
|
}) {
|
|
const [search, setSearch] = useState(initialSearch);
|
|
const change = (line: OrderAgreementLine, id: string) => {
|
|
const remaining = value.filter(
|
|
(binding) => binding.requisition_item_id !== line.id,
|
|
);
|
|
const option = options.find((item) => item.id === Number(id));
|
|
if (!option) {
|
|
onChange(remaining);
|
|
return;
|
|
}
|
|
onChange([
|
|
...remaining,
|
|
{
|
|
requisition_item_id: line.id,
|
|
agreement_id: option.id,
|
|
...(option.unit.toLowerCase() !== line.unit.toLowerCase()
|
|
? {
|
|
conversion: {
|
|
numerator: '',
|
|
denominator: '',
|
|
source: '',
|
|
version: '',
|
|
},
|
|
}
|
|
: {}),
|
|
},
|
|
]);
|
|
};
|
|
return (
|
|
<fieldset className="mt-4 space-y-4 rounded border p-4">
|
|
<legend className="text-sm font-semibold">
|
|
Acordos aplicáveis às linhas desta ordem
|
|
</legend>
|
|
<p className="text-sm">
|
|
A associação é explícita. Cada ordem pode utilizar o limite
|
|
completo do acordo. Quantidade e total com impostos são
|
|
conferidos apenas nas linhas ligadas, sem somar outras ordens.
|
|
</p>
|
|
<div className="flex flex-wrap items-end gap-3">
|
|
<label className="grid gap-2 text-sm">
|
|
Pesquisar referência do acordo
|
|
<Input
|
|
maxLength={100}
|
|
value={search}
|
|
onChange={(event) => setSearch(event.target.value)}
|
|
/>
|
|
</label>
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
onClick={() =>
|
|
router.get(
|
|
`/requisicoes/${requestId}/adjudicacao`,
|
|
{
|
|
agreement_search: search,
|
|
selected_agreement_ids: value.map(
|
|
(binding) => binding.agreement_id,
|
|
),
|
|
},
|
|
{
|
|
only: ['agreementOptions', 'agreementSearch'],
|
|
preserveState: true,
|
|
preserveScroll: true,
|
|
},
|
|
)
|
|
}
|
|
>
|
|
Pesquisar acordos
|
|
</Button>
|
|
</div>
|
|
<p className="text-xs text-muted-foreground">
|
|
São apresentados até 100 resultados da pesquisa, mais as
|
|
selecções ainda elegíveis. Sem limite definido não é criado um
|
|
limite zero.
|
|
</p>
|
|
{lines.map((line) => {
|
|
const binding = value.find(
|
|
(item) => item.requisition_item_id === line.id,
|
|
);
|
|
const selected = options.find(
|
|
(item) => item.id === binding?.agreement_id,
|
|
);
|
|
return (
|
|
<div key={line.id} className="space-y-3 border-t pt-3">
|
|
<label className="grid gap-2 text-sm">
|
|
{line.description} · {line.unit}
|
|
<select
|
|
className={controlClass}
|
|
value={binding?.agreement_id ?? ''}
|
|
disabled={line.stock_item_id === null}
|
|
onChange={(event) =>
|
|
change(line, event.target.value)
|
|
}
|
|
>
|
|
<option value="">Sem acordo associado</option>
|
|
{binding && !selected && (
|
|
<option
|
|
value={binding.agreement_id}
|
|
disabled
|
|
>
|
|
Selecção já não disponível — volte a
|
|
conferir o acordo
|
|
</option>
|
|
)}
|
|
{options
|
|
.filter(
|
|
(item) =>
|
|
item.stock_item_id ===
|
|
line.stock_item_id,
|
|
)
|
|
.map((item) => (
|
|
<option key={item.id} value={item.id}>
|
|
{item.reference} · versão{' '}
|
|
{item.version} ·{' '}
|
|
{item.warehouse ??
|
|
'Local não disponível'}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</label>
|
|
{line.stock_item_id === null && (
|
|
<p className="text-xs text-muted-foreground">
|
|
A associação exige identificar o artigo pelo
|
|
catálogo; o nome da linha não comprova essa
|
|
ligação.
|
|
</p>
|
|
)}
|
|
{selected && (
|
|
<p className="text-sm">
|
|
Limite de valor por ordem:{' '}
|
|
{selected.max_order_value_minor === null
|
|
? 'Não definido'
|
|
: money(
|
|
selected.max_order_value_minor,
|
|
selected.currency,
|
|
)}{' '}
|
|
· Quantidade por ordem:{' '}
|
|
{selected.max_order_quantity_milli === null
|
|
? 'Não definida'
|
|
: `${selected.max_order_quantity_milli / 1000} ${selected.unit}`}
|
|
. As versões e os limites ficam preservados na
|
|
ordem.
|
|
</p>
|
|
)}
|
|
{selected?.contract && (
|
|
<p className="text-sm">
|
|
Contrato global {selected.contract.reference} ·
|
|
versão {selected.contract.version}: limite por
|
|
ordem{' '}
|
|
{selected.contract.max_order_value_minor ===
|
|
null
|
|
? 'Não definido'
|
|
: money(
|
|
selected.contract
|
|
.max_order_value_minor,
|
|
selected.contract.currency,
|
|
)}
|
|
. Soma todos os artigos ligados ao mesmo
|
|
contrato nesta ordem.
|
|
</p>
|
|
)}
|
|
{selected && binding?.conversion && (
|
|
<ReceiptConversionFields
|
|
value={binding.conversion}
|
|
documentUnit={line.unit}
|
|
stockUnit={selected.unit}
|
|
onChange={(conversion) =>
|
|
onChange(
|
|
value.map((item) =>
|
|
item.requisition_item_id === line.id
|
|
? { ...item, conversion }
|
|
: item,
|
|
),
|
|
)
|
|
}
|
|
/>
|
|
)}
|
|
</div>
|
|
);
|
|
})}
|
|
</fieldset>
|
|
);
|
|
}
|