feat: Add prefill functionality for pedidos and enhance item matching logic with modalidade support

This commit is contained in:
2025-12-03 11:31:33 -03:00
parent d86d7d8dbb
commit fb78866a0e
3 changed files with 132 additions and 26 deletions

View File

@@ -117,6 +117,8 @@
});
let addingItem = $state(false);
let hasAppliedPrefill = $state(false);
// Edit SEI State
let editingSei = $state(false);
let seiValue = $state('');
@@ -139,6 +141,47 @@
selectedObjeto = null;
}
$effect(() => {
if (hasAppliedPrefill) return;
if (objetosQuery.isLoading || acoesQuery.isLoading) return;
const url = $page.url;
const obj = url.searchParams.get('obj');
const qtdStr = url.searchParams.get('qtd');
const mod = url.searchParams.get('mod') as Modalidade | null;
const acao = url.searchParams.get('acao');
const ata = url.searchParams.get('ata');
if (!obj) return;
const objeto = objetos.find((o) => o._id === obj);
if (!objeto) return;
let quantidade = parseInt(qtdStr || '1', 10);
if (!Number.isFinite(quantidade) || quantidade <= 0) {
quantidade = 1;
}
const modalidade: Modalidade =
mod === 'dispensa' || mod === 'inexgibilidade' || mod === 'adesao' || mod === 'consumo'
? mod
: 'consumo';
showAddItem = true;
newItem = {
objetoId: obj,
valorEstimado: maskCurrencyBRL(objeto.valorEstimado || ''),
quantidade,
modalidade,
acaoId: acao || '',
ataId: ata || ''
};
void loadAtasForObjeto(obj);
hasAppliedPrefill = true;
});
async function handleAddItem() {
if (!newItem.objetoId || !newItem.valorEstimado) return;
addingItem = true;

View File

@@ -125,7 +125,11 @@
numeroSei?: string;
status: string;
criadoEm: number;
matchingItems?: { objetoId: Id<'objetos'>; quantidade: number }[];
matchingItems?: {
objetoId: Id<'objetos'>;
modalidade: SelectedItem['modalidade'];
quantidade: number;
}[];
}[]
>([]);
let checking = $state(false);
@@ -175,20 +179,64 @@
return `Contém: ${details}`;
}
function getFirstMatchingSelectedItem(pedido: (typeof existingPedidos)[0]) {
if (!pedido.matchingItems || pedido.matchingItems.length === 0) return null;
for (const match of pedido.matchingItems) {
const item = selectedItems.find(
(p) => p.objeto._id === match.objetoId && p.modalidade === match.modalidade
);
if (item) {
return item;
}
}
return null;
}
function buildPedidoHref(pedido: (typeof existingPedidos)[0]) {
const matchedItem = getFirstMatchingSelectedItem(pedido);
if (!matchedItem) {
return resolve(`/pedidos/${pedido._id}`);
}
const params = new URLSearchParams();
params.set('obj', matchedItem.objeto._id);
params.set('qtd', String(matchedItem.quantidade));
params.set('mod', matchedItem.modalidade);
if (matchedItem.acaoId) {
params.set('acao', matchedItem.acaoId);
}
if (matchedItem.ataId) {
params.set('ata', matchedItem.ataId);
}
return resolve(`/pedidos/${pedido._id}?${params.toString()}`);
}
async function checkExisting() {
warning = null;
existingPedidos = [];
const hasFilters = formData.numeroSei || selectedObjetoIds.length > 0;
const hasFilters = formData.numeroSei || selectedItems.length > 0;
if (!hasFilters) return;
checking = true;
try {
// Note: checkExisting query might need update to handle item-level acaoId if we want to filter by it.
// Currently we only filter by numeroSei and objetoIds.
const itensFiltro =
selectedItems.length > 0
? selectedItems.map((item) => ({
objetoId: item.objeto._id,
modalidade: item.modalidade
}))
: undefined;
const result = await client.query(api.pedidos.checkExisting, {
numeroSei: formData.numeroSei || undefined,
objetoIds: selectedObjetoIds.length ? (selectedObjetoIds as Id<'objetos'>[]) : undefined
itensFiltro
});
existingPedidos = result;
@@ -407,7 +455,7 @@
{/if}
</div>
<a
href={resolve(`/pedidos/${pedido._id}`)}
href={buildPedidoHref(pedido)}
class="text-sm font-medium text-blue-600 hover:text-blue-800"
>
Abrir