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; }) { const agreements = Array.from( new Map( Object.values(bindings ?? {}).map((row) => [row.agreement_id, row]), ).values(), ); if (!agreements.length) return null; return (

Cada ordem pode utilizar o limite completo. A conferência abrange apenas as linhas expressamente ligadas ao mesmo acordo, sem acumular outras ordens.

{Array.from( new Map( agreements .filter((row) => row.contract) .map((row) => [row.contract!.id, row.contract!]), ).values(), ).map((contract) => (

Contrato global: {contract.reference} · versão{' '} {contract.version}

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, )}

))} {agreements.map((row) => (

{row.reference} · versão {row.version}

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)}

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}`}

))}
); } 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 (
Acordos aplicáveis às linhas desta ordem

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.

São apresentados até 100 resultados da pesquisa, mais as selecções ainda elegíveis. Sem limite definido não é criado um limite zero.

{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 (
{line.stock_item_id === null && (

A associação exige identificar o artigo pelo catálogo; o nome da linha não comprova essa ligação.

)} {selected && (

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.

)} {selected?.contract && (

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.

)} {selected && binding?.conversion && ( onChange( value.map((item) => item.requisition_item_id === line.id ? { ...item, conversion } : item, ), ) } /> )}
); })}
); }