refactor: simplify pedidos item management by removing modalidade from item configuration and validation, ensuring all items use the same ata while enhancing code clarity and maintainability

This commit is contained in:
2025-12-18 08:48:40 -03:00
parent 69914170bf
commit 94373c6b94
3 changed files with 91 additions and 295 deletions

View File

@@ -514,30 +514,23 @@
if (!pedido || !newItem.objetoId || !newItem.valorEstimado) return;
// Validação no front: garantir que todos os itens existentes do pedido
// utilizem a mesma combinação de modalidade e ata (quando houver).
// utilizem a mesma ata (quando houver).
if (items.length > 0) {
const referenceItem = items[0];
const referenceModalidade = (referenceItem.modalidade as Modalidade | undefined) ?? undefined;
const referenceAtaId = (('ataId' in referenceItem ? referenceItem.ataId : undefined) ??
null) as string | null;
const newAtaId = newItem.ataId || null;
const sameModalidade = !referenceModalidade || newItem.modalidade === referenceModalidade;
const sameAta = referenceAtaId === newAtaId;
if (!sameModalidade || !sameAta) {
const refModalidadeLabel = referenceModalidade
? formatModalidade(referenceModalidade)
: 'Não definida';
if (!sameAta) {
const refAtaLabel =
referenceAtaId === null ? 'sem Ata vinculada' : 'com uma Ata específica';
toast.error(
`Não é possível adicionar este item com esta combinação de modalidade e ata.\n\n` +
`Este pedido já está utilizando Modalidade: ${refModalidadeLabel} e está ${refAtaLabel}.\n` +
`Todos os itens do pedido devem usar a mesma modalidade e a mesma ata (quando houver).`
`Não é possível adicionar este item com esta ata.\n\n` +
`Este pedido já está vinculado a: ${refAtaLabel}.\n` +
`Todos os itens do pedido devem usar a mesma ata (quando houver).`
);
return;
}
@@ -550,7 +543,6 @@
objetoId: newItem.objetoId as Id<'objetos'>,
valorEstimado: newItem.valorEstimado,
quantidade: newItem.quantidade,
modalidade: newItem.modalidade,
acaoId: newItem.acaoId ? (newItem.acaoId as Id<'acoes'>) : undefined,
ataId: newItem.ataId ? (newItem.ataId as Id<'atas'>) : undefined
});
@@ -1664,22 +1656,8 @@
placeholder="R$ 0,00"
/>
</div>
<div>
<label for="modalidade-select" class="mb-1 block text-xs font-medium text-gray-500"
>Modalidade</label
>
<select
id="modalidade-select"
bind:value={newItem.modalidade}
class="w-full rounded-md border-gray-300 text-sm shadow-sm"
>
<option value="consumo">Consumo</option>
<option value="dispensa">Dispensa</option>
<option value="inexgibilidade">Inexigibilidade</option>
<option value="adesao">Adesão</option>
</select>
</div>
{#if newItem.objetoId}
{#if newItem.objetoId && permissions?.canEditAta}
<div>
<label for="ata-select" class="mb-1 block text-xs font-medium text-gray-500"
>Ata (Opcional)</label
@@ -1900,7 +1878,7 @@
{/if}
</td>
<td class="px-6 py-4 text-sm whitespace-nowrap text-gray-600">
{#if pedido.status === 'em_rascunho' || pedido.status === 'precisa_ajustes' || pedido.status === 'em_analise' || pedido.status === 'aguardando_aceite'}
{#if permissions?.canEditModalidade}
<select
class="rounded border px-2 py-1 text-xs"
value={ensureEditingItem(item).modalidade}
@@ -1919,7 +1897,7 @@
<option value="adesao">Adesão</option>
</select>
{:else}
{item.modalidade}
{formatModalidade(item.modalidade as Modalidade) || '-'}
{/if}
</td>
<td class="px-6 py-4 text-sm whitespace-nowrap text-gray-600">
@@ -1942,7 +1920,7 @@
{/if}
</td>
<td class="px-6 py-4 text-sm whitespace-nowrap text-gray-600">
{#if pedido.status === 'em_rascunho' || pedido.status === 'precisa_ajustes' || pedido.status === 'em_analise' || pedido.status === 'aguardando_aceite'}
{#if permissions?.canEditAta}
<select
class="rounded border px-2 py-1 text-xs"
value={ensureEditingItem(item).ataId}