feat: enhance ata and objeto management by adding configuration options for quantity limits and usage tracking, improving data integrity and user feedback in pedidos
This commit is contained in:
@@ -44,6 +44,11 @@
|
|||||||
dataFim: ''
|
dataFim: ''
|
||||||
});
|
});
|
||||||
let selectedObjetos = $state<Id<'objetos'>[]>([]);
|
let selectedObjetos = $state<Id<'objetos'>[]>([]);
|
||||||
|
type ObjetoAtaConfig = {
|
||||||
|
quantidadeTotal: number | undefined;
|
||||||
|
limitePercentual: number | undefined;
|
||||||
|
};
|
||||||
|
let objetosConfig = $state<Record<string, ObjetoAtaConfig>>({});
|
||||||
let searchObjeto = $state('');
|
let searchObjeto = $state('');
|
||||||
let saving = $state(false);
|
let saving = $state(false);
|
||||||
|
|
||||||
@@ -73,6 +78,15 @@
|
|||||||
const linkedObjetos = await client.query(api.atas.getObjetos, { id: ata._id });
|
const linkedObjetos = await client.query(api.atas.getObjetos, { id: ata._id });
|
||||||
selectedObjetos = linkedObjetos.map((o) => o._id);
|
selectedObjetos = linkedObjetos.map((o) => o._id);
|
||||||
|
|
||||||
|
const linkedConfigs = await client.query(api.atas.getObjetosConfig, { id: ata._id });
|
||||||
|
objetosConfig = {};
|
||||||
|
for (const cfg of linkedConfigs) {
|
||||||
|
objetosConfig[String(cfg.objetoId)] = {
|
||||||
|
quantidadeTotal: cfg.quantidadeTotal ?? undefined,
|
||||||
|
limitePercentual: cfg.limitePercentual ?? 50
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// Fetch attachments
|
// Fetch attachments
|
||||||
attachments = await client.query(api.atas.getDocumentos, { ataId: ata._id });
|
attachments = await client.query(api.atas.getDocumentos, { ataId: ata._id });
|
||||||
} else {
|
} else {
|
||||||
@@ -85,6 +99,7 @@
|
|||||||
dataFim: ''
|
dataFim: ''
|
||||||
};
|
};
|
||||||
selectedObjetos = [];
|
selectedObjetos = [];
|
||||||
|
objetosConfig = {};
|
||||||
attachments = [];
|
attachments = [];
|
||||||
}
|
}
|
||||||
attachmentFiles = [];
|
attachmentFiles = [];
|
||||||
@@ -97,11 +112,22 @@
|
|||||||
editingId = null;
|
editingId = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getObjetoConfig(id: Id<'objetos'>): ObjetoAtaConfig {
|
||||||
|
const key = String(id);
|
||||||
|
if (!objetosConfig[key]) {
|
||||||
|
objetosConfig[key] = { quantidadeTotal: 1, limitePercentual: 50 };
|
||||||
|
}
|
||||||
|
return objetosConfig[key];
|
||||||
|
}
|
||||||
|
|
||||||
function toggleObjeto(id: Id<'objetos'>) {
|
function toggleObjeto(id: Id<'objetos'>) {
|
||||||
|
const key = String(id);
|
||||||
if (selectedObjetos.includes(id)) {
|
if (selectedObjetos.includes(id)) {
|
||||||
selectedObjetos = selectedObjetos.filter((oid) => oid !== id);
|
selectedObjetos = selectedObjetos.filter((oid) => oid !== id);
|
||||||
|
delete objetosConfig[key];
|
||||||
} else {
|
} else {
|
||||||
selectedObjetos = [...selectedObjetos, id];
|
selectedObjetos = [...selectedObjetos, id];
|
||||||
|
objetosConfig[key] = { quantidadeTotal: 1, limitePercentual: 50 };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,13 +150,37 @@
|
|||||||
}
|
}
|
||||||
saving = true;
|
saving = true;
|
||||||
try {
|
try {
|
||||||
|
const objetos = selectedObjetos.map((objetoId) => {
|
||||||
|
const cfg = objetosConfig[String(objetoId)];
|
||||||
|
if (
|
||||||
|
!cfg ||
|
||||||
|
cfg.quantidadeTotal === undefined ||
|
||||||
|
!Number.isFinite(cfg.quantidadeTotal) ||
|
||||||
|
cfg.quantidadeTotal <= 0
|
||||||
|
) {
|
||||||
|
throw new Error(
|
||||||
|
'Informe a quantidade (maior que zero) para todos os objetos vinculados.'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const limitePercentual =
|
||||||
|
cfg.limitePercentual === undefined || !Number.isFinite(cfg.limitePercentual)
|
||||||
|
? 50
|
||||||
|
: cfg.limitePercentual;
|
||||||
|
|
||||||
|
return {
|
||||||
|
objetoId,
|
||||||
|
quantidadeTotal: cfg.quantidadeTotal,
|
||||||
|
limitePercentual
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
const payload = {
|
const payload = {
|
||||||
numero: formData.numero,
|
numero: formData.numero,
|
||||||
numeroSei: formData.numeroSei,
|
numeroSei: formData.numeroSei,
|
||||||
empresaId: formData.empresaId as Id<'empresas'>,
|
empresaId: formData.empresaId as Id<'empresas'>,
|
||||||
dataInicio: formData.dataInicio || undefined,
|
dataInicio: formData.dataInicio || undefined,
|
||||||
dataFim: formData.dataFim || undefined,
|
dataFim: formData.dataFim || undefined,
|
||||||
objetosIds: selectedObjetos
|
objetos
|
||||||
};
|
};
|
||||||
|
|
||||||
let ataId: Id<'atas'>;
|
let ataId: Id<'atas'>;
|
||||||
@@ -537,6 +587,54 @@
|
|||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{#if selectedObjetos.length > 0}
|
||||||
|
<div class="border-base-300 border-t pt-4">
|
||||||
|
<div class="font-semibold">Configuração por objeto</div>
|
||||||
|
<p class="text-base-content/60 mt-1 text-xs">
|
||||||
|
Defina a quantidade total do objeto na ata e o limite de uso em % (padrão 50%).
|
||||||
|
</p>
|
||||||
|
<div class="mt-3 max-h-52 space-y-3 overflow-y-auto">
|
||||||
|
{#each selectedObjetos as objetoId (objetoId)}
|
||||||
|
{@const obj = objetos.find((o) => o._id === objetoId)}
|
||||||
|
{@const cfg = getObjetoConfig(objetoId)}
|
||||||
|
<div class="border-base-300 rounded-lg border p-3">
|
||||||
|
<div class="text-sm font-semibold">{obj?.nome || 'Objeto'}</div>
|
||||||
|
<div class="mt-3 grid grid-cols-1 gap-3 md:grid-cols-2">
|
||||||
|
<div class="form-control w-full">
|
||||||
|
<label class="label" for={`qtd_${objetoId}`}>
|
||||||
|
<span class="label-text text-xs font-semibold">Quantidade na ata</span
|
||||||
|
>
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
id={`qtd_${objetoId}`}
|
||||||
|
class="input input-bordered input-sm focus:input-primary w-full"
|
||||||
|
type="number"
|
||||||
|
min="1"
|
||||||
|
required
|
||||||
|
bind:value={cfg.quantidadeTotal}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div class="form-control w-full">
|
||||||
|
<label class="label" for={`limite_${objetoId}`}>
|
||||||
|
<span class="label-text text-xs font-semibold">Limite (%)</span>
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
id={`limite_${objetoId}`}
|
||||||
|
class="input input-bordered input-sm focus:input-primary w-full"
|
||||||
|
type="number"
|
||||||
|
min="0"
|
||||||
|
max="100"
|
||||||
|
placeholder="50"
|
||||||
|
bind:value={cfg.limitePercentual}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{/each}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
|
||||||
<div class="border-base-300 border-t pt-4">
|
<div class="border-base-300 border-t pt-4">
|
||||||
<div class="font-semibold">Anexos</div>
|
<div class="font-semibold">Anexos</div>
|
||||||
<div class="mt-2 space-y-2">
|
<div class="mt-2 space-y-2">
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { api } from '@sgse-app/backend/convex/_generated/api';
|
import { api } from '@sgse-app/backend/convex/_generated/api';
|
||||||
import type { Doc, Id } from '@sgse-app/backend/convex/_generated/dataModel';
|
import type { Doc, Id } from '@sgse-app/backend/convex/_generated/dataModel';
|
||||||
|
import type { FunctionReturnType } from 'convex/server';
|
||||||
import { useConvexClient, useQuery } from 'convex-svelte';
|
import { useConvexClient, useQuery } from 'convex-svelte';
|
||||||
import { SvelteSet } from 'svelte/reactivity';
|
import { SvelteSet } from 'svelte/reactivity';
|
||||||
import { toast } from 'svelte-sonner';
|
import { toast } from 'svelte-sonner';
|
||||||
@@ -54,7 +55,12 @@
|
|||||||
type Modalidade = 'dispensa' | 'inexgibilidade' | 'adesao' | 'consumo';
|
type Modalidade = 'dispensa' | 'inexgibilidade' | 'adesao' | 'consumo';
|
||||||
|
|
||||||
function coerceModalidade(value: string): Modalidade {
|
function coerceModalidade(value: string): Modalidade {
|
||||||
if (value === 'dispensa' || value === 'inexgibilidade' || value === 'adesao' || value === 'consumo') {
|
if (
|
||||||
|
value === 'dispensa' ||
|
||||||
|
value === 'inexgibilidade' ||
|
||||||
|
value === 'adesao' ||
|
||||||
|
value === 'consumo'
|
||||||
|
) {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
return 'consumo';
|
return 'consumo';
|
||||||
@@ -77,7 +83,8 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Atas por objeto (carregadas sob demanda)
|
// Atas por objeto (carregadas sob demanda)
|
||||||
let atasPorObjeto = $state<Record<string, Array<Doc<'atas'>>>>({});
|
type AtasComLimite = FunctionReturnType<typeof api.objetos.getAtasComLimite>;
|
||||||
|
let atasPorObjeto = $state<Record<string, AtasComLimite>>({});
|
||||||
|
|
||||||
let editingItems = $state<Record<string, EditingItem>>({});
|
let editingItems = $state<Record<string, EditingItem>>({});
|
||||||
|
|
||||||
@@ -235,7 +242,9 @@
|
|||||||
let solicitacaoDocsRequestId = $state<Id<'solicitacoesItens'> | null>(null);
|
let solicitacaoDocsRequestId = $state<Id<'solicitacoesItens'> | null>(null);
|
||||||
let solicitacaoDocsSolicitadoPor = $state<Id<'funcionarios'> | null>(null);
|
let solicitacaoDocsSolicitadoPor = $state<Id<'funcionarios'> | null>(null);
|
||||||
let solicitacaoDocsTipo = $state<string | null>(null);
|
let solicitacaoDocsTipo = $state<string | null>(null);
|
||||||
let solicitacaoDocs = $state<any[]>([]);
|
let solicitacaoDocs = $state<FunctionReturnType<typeof api.pedidos.listSolicitacaoDocumentos>>(
|
||||||
|
[]
|
||||||
|
);
|
||||||
let carregandoSolicitacaoDocs = $state(false);
|
let carregandoSolicitacaoDocs = $state(false);
|
||||||
|
|
||||||
let solicitacaoDocumentoDescricao = $state('');
|
let solicitacaoDocumentoDescricao = $state('');
|
||||||
@@ -651,19 +660,16 @@
|
|||||||
async function loadAtasForObjeto(objetoId: string) {
|
async function loadAtasForObjeto(objetoId: string) {
|
||||||
if (atasPorObjeto[objetoId]) return;
|
if (atasPorObjeto[objetoId]) return;
|
||||||
try {
|
try {
|
||||||
const linkedAtas = await client.query(api.objetos.getAtas, {
|
const linkedAtas = await client.query(api.objetos.getAtasComLimite, {
|
||||||
objetoId: objetoId as Id<'objetos'>
|
objetoId: objetoId as Id<'objetos'>
|
||||||
});
|
});
|
||||||
atasPorObjeto = {
|
atasPorObjeto[objetoId] = linkedAtas;
|
||||||
...atasPorObjeto,
|
|
||||||
[objetoId]: linkedAtas
|
|
||||||
};
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Erro ao carregar atas para objeto', objetoId, e);
|
console.error('Erro ao carregar atas para objeto', objetoId, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getAtasForObjeto(objetoId: string): Array<Doc<'atas'>> {
|
function getAtasForObjeto(objetoId: string): AtasComLimite {
|
||||||
return atasPorObjeto[objetoId] || [];
|
return atasPorObjeto[objetoId] || [];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1464,7 +1470,9 @@
|
|||||||
<XCircle size={16} />
|
<XCircle size={16} />
|
||||||
</button>
|
</button>
|
||||||
{:else}
|
{:else}
|
||||||
<span class="ml-2 self-center text-xs text-gray-400">Aguardando Análise</span>
|
<span class="ml-2 self-center text-xs text-gray-400"
|
||||||
|
>Aguardando Análise</span
|
||||||
|
>
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
</td>
|
</td>
|
||||||
@@ -1563,7 +1571,15 @@
|
|||||||
>
|
>
|
||||||
<option value="">Nenhuma</option>
|
<option value="">Nenhuma</option>
|
||||||
{#each getAtasForObjeto(newItem.objetoId) as ata (ata._id)}
|
{#each getAtasForObjeto(newItem.objetoId) as ata (ata._id)}
|
||||||
<option value={ata._id}>Ata {ata.numero} (SEI: {ata.numeroSei})</option>
|
{@const isSelectedAta = String(ata._id) === newItem.ataId}
|
||||||
|
{@const reason = !ata.quantidadeTotal
|
||||||
|
? 'não configurada'
|
||||||
|
: ata.quantidadeUsada >= ata.limitePermitido
|
||||||
|
? 'limite atingido'
|
||||||
|
: null}
|
||||||
|
<option value={ata._id} disabled={ata.isLocked && !isSelectedAta}>
|
||||||
|
Ata {ata.numero} (SEI: {ata.numeroSei}){reason ? ` (${reason})` : ''}
|
||||||
|
</option>
|
||||||
{/each}
|
{/each}
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
@@ -1810,7 +1826,16 @@
|
|||||||
>
|
>
|
||||||
<option value="">Nenhuma</option>
|
<option value="">Nenhuma</option>
|
||||||
{#each getAtasForObjeto(item.objetoId) as ata (ata._id)}
|
{#each getAtasForObjeto(item.objetoId) as ata (ata._id)}
|
||||||
<option value={ata._id}>Ata {ata.numero} (SEI: {ata.numeroSei})</option>
|
{@const currentAtaId = ensureEditingItem(item).ataId}
|
||||||
|
{@const isSelectedAta = String(ata._id) === currentAtaId}
|
||||||
|
{@const reason = !ata.quantidadeTotal
|
||||||
|
? 'não configurada'
|
||||||
|
: ata.quantidadeUsada >= ata.limitePermitido
|
||||||
|
? 'limite atingido'
|
||||||
|
: null}
|
||||||
|
<option value={ata._id} disabled={ata.isLocked && !isSelectedAta}>
|
||||||
|
Ata {ata.numero} (SEI: {ata.numeroSei}){reason ? ` (${reason})` : ''}
|
||||||
|
</option>
|
||||||
{/each}
|
{/each}
|
||||||
</select>
|
</select>
|
||||||
{:else if item.ataId}
|
{:else if item.ataId}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { api } from '@sgse-app/backend/convex/_generated/api';
|
import { api } from '@sgse-app/backend/convex/_generated/api';
|
||||||
import type { Doc, Id } from '@sgse-app/backend/convex/_generated/dataModel';
|
import type { Doc, Id } from '@sgse-app/backend/convex/_generated/dataModel';
|
||||||
|
import type { FunctionReturnType } from 'convex/server';
|
||||||
import { useConvexClient, useQuery } from 'convex-svelte';
|
import { useConvexClient, useQuery } from 'convex-svelte';
|
||||||
import { Plus, Trash2, X, Info } from 'lucide-svelte';
|
import { Plus, Trash2, X, Info } from 'lucide-svelte';
|
||||||
import { goto } from '$app/navigation';
|
import { goto } from '$app/navigation';
|
||||||
@@ -32,7 +33,7 @@
|
|||||||
acaoId?: Id<'acoes'>;
|
acaoId?: Id<'acoes'>;
|
||||||
ataId?: Id<'atas'>;
|
ataId?: Id<'atas'>;
|
||||||
ataNumero?: string; // For display
|
ataNumero?: string; // For display
|
||||||
ata?: Doc<'atas'>; // Full ata object for details
|
ata?: FunctionReturnType<typeof api.objetos.getAtasComLimite>[number]; // dados mínimos p/ exibir detalhes
|
||||||
};
|
};
|
||||||
|
|
||||||
let selectedItems = $state<SelectedItem[]>([]);
|
let selectedItems = $state<SelectedItem[]>([]);
|
||||||
@@ -55,7 +56,8 @@
|
|||||||
ataId: ''
|
ataId: ''
|
||||||
});
|
});
|
||||||
|
|
||||||
let availableAtas = $state<Doc<'atas'>[]>([]);
|
type AtasComLimite = FunctionReturnType<typeof api.objetos.getAtasComLimite>;
|
||||||
|
let availableAtas = $state<AtasComLimite>([]);
|
||||||
|
|
||||||
// Item Details Modal
|
// Item Details Modal
|
||||||
let showDetailsModal = $state(false);
|
let showDetailsModal = $state(false);
|
||||||
@@ -73,7 +75,7 @@
|
|||||||
|
|
||||||
async function openItemModal(objeto: Doc<'objetos'>) {
|
async function openItemModal(objeto: Doc<'objetos'>) {
|
||||||
// Fetch linked Atas for this object
|
// Fetch linked Atas for this object
|
||||||
const linkedAtas = await client.query(api.objetos.getAtas, { objetoId: objeto._id });
|
const linkedAtas = await client.query(api.objetos.getAtasComLimite, { objetoId: objeto._id });
|
||||||
availableAtas = linkedAtas;
|
availableAtas = linkedAtas;
|
||||||
|
|
||||||
itemConfig = {
|
itemConfig = {
|
||||||
@@ -226,11 +228,11 @@
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildPedidoHref(pedido: (typeof existingPedidos)[0]) {
|
function buildPedidoHref(pedido: (typeof existingPedidos)[0]): `/pedidos/${string}` {
|
||||||
const matchedItem = getFirstMatchingSelectedItem(pedido);
|
const matchedItem = getFirstMatchingSelectedItem(pedido);
|
||||||
|
|
||||||
if (!matchedItem) {
|
if (!matchedItem) {
|
||||||
return resolve(`/pedidos/${pedido._id}`);
|
return `/pedidos/${pedido._id}` as `/pedidos/${string}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
const params = new URLSearchParams();
|
const params = new URLSearchParams();
|
||||||
@@ -246,7 +248,7 @@
|
|||||||
params.set('ata', matchedItem.ataId);
|
params.set('ata', matchedItem.ataId);
|
||||||
}
|
}
|
||||||
|
|
||||||
return resolve(`/pedidos/${pedido._id}?${params.toString()}`);
|
return `/pedidos/${pedido._id}?${params.toString()}` as `/pedidos/${string}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function checkExisting() {
|
async function checkExisting() {
|
||||||
@@ -505,26 +507,22 @@
|
|||||||
<p class="mb-3 font-semibold text-yellow-900">Pedidos similares encontrados:</p>
|
<p class="mb-3 font-semibold text-yellow-900">Pedidos similares encontrados:</p>
|
||||||
<ul class="space-y-2">
|
<ul class="space-y-2">
|
||||||
{#each existingPedidos as pedido (pedido._id)}
|
{#each existingPedidos as pedido (pedido._id)}
|
||||||
|
{@const first = getFirstMatchingSelectedItem(pedido)}
|
||||||
<li class="flex flex-col rounded-lg bg-white px-4 py-3 shadow-sm">
|
<li class="flex flex-col rounded-lg bg-white px-4 py-3 shadow-sm">
|
||||||
<div class="flex items-center justify-between gap-3">
|
<div class="flex items-center justify-between gap-3">
|
||||||
<div class="space-y-1">
|
<div class="space-y-1">
|
||||||
<p class="text-sm font-medium text-gray-900">
|
<p class="text-sm font-medium text-gray-900">
|
||||||
Pedido {pedido.numeroSei || 'sem número SEI'} — {formatStatus(pedido.status)}
|
Pedido {pedido.numeroSei || 'sem número SEI'} — {formatStatus(pedido.status)}
|
||||||
</p>
|
</p>
|
||||||
{#if getFirstMatchingSelectedItem(pedido)}
|
{#if first}
|
||||||
{#key pedido._id}
|
|
||||||
{#if getFirstMatchingSelectedItem(pedido)}
|
|
||||||
<span
|
<span
|
||||||
class={`inline-flex items-center rounded-full px-2.5 py-0.5 text-xs font-semibold ${getModalidadeBadgeClasses(
|
class={`inline-flex items-center rounded-full px-2.5 py-0.5 text-xs font-semibold ${getModalidadeBadgeClasses(
|
||||||
getFirstMatchingSelectedItem(pedido).modalidade
|
first.modalidade
|
||||||
)}`}
|
)}`}
|
||||||
>
|
>
|
||||||
Modalidade:{' '}
|
Modalidade: {formatModalidade(first.modalidade)}
|
||||||
{formatModalidade(getFirstMatchingSelectedItem(pedido).modalidade)}
|
|
||||||
</span>
|
</span>
|
||||||
{/if}
|
{/if}
|
||||||
{/key}
|
|
||||||
{/if}
|
|
||||||
{#if getMatchingInfo(pedido)}
|
{#if getMatchingInfo(pedido)}
|
||||||
<p class="mt-1 text-xs text-blue-700">
|
<p class="mt-1 text-xs text-blue-700">
|
||||||
{getMatchingInfo(pedido)}
|
{getMatchingInfo(pedido)}
|
||||||
@@ -532,7 +530,7 @@
|
|||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
<a
|
<a
|
||||||
href={buildPedidoHref(pedido)}
|
href={resolve(buildPedidoHref(pedido))}
|
||||||
class="text-sm font-medium text-blue-600 hover:text-blue-800"
|
class="text-sm font-medium text-blue-600 hover:text-blue-800"
|
||||||
>
|
>
|
||||||
Abrir
|
Abrir
|
||||||
@@ -637,7 +635,14 @@
|
|||||||
>
|
>
|
||||||
<option value="">Nenhuma</option>
|
<option value="">Nenhuma</option>
|
||||||
{#each availableAtas as ata (ata._id)}
|
{#each availableAtas as ata (ata._id)}
|
||||||
<option value={ata._id}>Ata {ata.numero} (SEI: {ata.numeroSei})</option>
|
{@const reason = !ata.quantidadeTotal
|
||||||
|
? 'não configurada'
|
||||||
|
: ata.quantidadeUsada >= ata.limitePermitido
|
||||||
|
? 'limite atingido'
|
||||||
|
: null}
|
||||||
|
<option value={ata._id} disabled={ata.isLocked}>
|
||||||
|
Ata {ata.numero} (SEI: {ata.numeroSei}){reason ? ` (${reason})` : ''}
|
||||||
|
</option>
|
||||||
{/each}
|
{/each}
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -4,6 +4,21 @@ import type { Id } from './_generated/dataModel';
|
|||||||
import { getCurrentUserFunction } from './auth';
|
import { getCurrentUserFunction } from './auth';
|
||||||
import { internal } from './_generated/api';
|
import { internal } from './_generated/api';
|
||||||
|
|
||||||
|
function normalizeLimitePercentual(value: number | undefined): number {
|
||||||
|
const fallback = 50;
|
||||||
|
if (value === undefined) return fallback;
|
||||||
|
if (!Number.isFinite(value)) return fallback;
|
||||||
|
if (value < 0) return 0;
|
||||||
|
if (value > 100) return 100;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
function assertQuantidadeTotalValida(value: number) {
|
||||||
|
if (!Number.isFinite(value) || value <= 0) {
|
||||||
|
throw new Error('Quantidade do produto na ata deve ser maior que zero.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export const list = query({
|
export const list = query({
|
||||||
args: {
|
args: {
|
||||||
periodoInicio: v.optional(v.string()),
|
periodoInicio: v.optional(v.string()),
|
||||||
@@ -71,6 +86,34 @@ export const getObjetos = query({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const getObjetosConfig = query({
|
||||||
|
args: { id: v.id('atas') },
|
||||||
|
returns: v.array(
|
||||||
|
v.object({
|
||||||
|
objetoId: v.id('objetos'),
|
||||||
|
quantidadeTotal: v.union(v.number(), v.null()),
|
||||||
|
limitePercentual: v.union(v.number(), v.null())
|
||||||
|
})
|
||||||
|
),
|
||||||
|
handler: async (ctx, args) => {
|
||||||
|
await ctx.runQuery(internal.permissoesAcoes.assertPermissaoAcaoAtual, {
|
||||||
|
recurso: 'atas',
|
||||||
|
acao: 'ver'
|
||||||
|
});
|
||||||
|
|
||||||
|
const links = await ctx.db
|
||||||
|
.query('atasObjetos')
|
||||||
|
.withIndex('by_ataId', (q) => q.eq('ataId', args.id))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
return links.map((l) => ({
|
||||||
|
objetoId: l.objetoId,
|
||||||
|
quantidadeTotal: l.quantidadeTotal ?? null,
|
||||||
|
limitePercentual: l.limitePercentual ?? null
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
export const listByObjetoIds = query({
|
export const listByObjetoIds = query({
|
||||||
args: {
|
args: {
|
||||||
objetoIds: v.array(v.id('objetos'))
|
objetoIds: v.array(v.id('objetos'))
|
||||||
@@ -108,7 +151,13 @@ export const create = mutation({
|
|||||||
dataFim: v.optional(v.string()),
|
dataFim: v.optional(v.string()),
|
||||||
empresaId: v.id('empresas'),
|
empresaId: v.id('empresas'),
|
||||||
numeroSei: v.string(),
|
numeroSei: v.string(),
|
||||||
objetosIds: v.array(v.id('objetos'))
|
objetos: v.array(
|
||||||
|
v.object({
|
||||||
|
objetoId: v.id('objetos'),
|
||||||
|
quantidadeTotal: v.number(),
|
||||||
|
limitePercentual: v.optional(v.number())
|
||||||
|
})
|
||||||
|
)
|
||||||
},
|
},
|
||||||
handler: async (ctx, args) => {
|
handler: async (ctx, args) => {
|
||||||
await ctx.runQuery(internal.permissoesAcoes.assertPermissaoAcaoAtual, {
|
await ctx.runQuery(internal.permissoesAcoes.assertPermissaoAcaoAtual, {
|
||||||
@@ -131,10 +180,16 @@ export const create = mutation({
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Vincular objetos
|
// Vincular objetos
|
||||||
for (const objetoId of args.objetosIds) {
|
for (const cfg of args.objetos) {
|
||||||
|
assertQuantidadeTotalValida(cfg.quantidadeTotal);
|
||||||
|
const limitePercentual = normalizeLimitePercentual(cfg.limitePercentual);
|
||||||
|
|
||||||
await ctx.db.insert('atasObjetos', {
|
await ctx.db.insert('atasObjetos', {
|
||||||
ataId,
|
ataId,
|
||||||
objetoId
|
objetoId: cfg.objetoId,
|
||||||
|
quantidadeTotal: cfg.quantidadeTotal,
|
||||||
|
limitePercentual,
|
||||||
|
quantidadeUsada: 0
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,7 +205,13 @@ export const update = mutation({
|
|||||||
empresaId: v.id('empresas'),
|
empresaId: v.id('empresas'),
|
||||||
dataInicio: v.optional(v.string()),
|
dataInicio: v.optional(v.string()),
|
||||||
dataFim: v.optional(v.string()),
|
dataFim: v.optional(v.string()),
|
||||||
objetosIds: v.array(v.id('objetos'))
|
objetos: v.array(
|
||||||
|
v.object({
|
||||||
|
objetoId: v.id('objetos'),
|
||||||
|
quantidadeTotal: v.number(),
|
||||||
|
limitePercentual: v.optional(v.number())
|
||||||
|
})
|
||||||
|
)
|
||||||
},
|
},
|
||||||
handler: async (ctx, args) => {
|
handler: async (ctx, args) => {
|
||||||
await ctx.runQuery(internal.permissoesAcoes.assertPermissaoAcaoAtual, {
|
await ctx.runQuery(internal.permissoesAcoes.assertPermissaoAcaoAtual, {
|
||||||
@@ -171,24 +232,73 @@ export const update = mutation({
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Atualizar objetos vinculados
|
// Atualizar objetos vinculados
|
||||||
// Primeiro remove todos os vínculos existentes
|
|
||||||
const existingLinks = await ctx.db
|
const existingLinks = await ctx.db
|
||||||
.query('atasObjetos')
|
.query('atasObjetos')
|
||||||
.withIndex('by_ataId', (q) => q.eq('ataId', args.id))
|
.withIndex('by_ataId', (q) => q.eq('ataId', args.id))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
const existingByObjeto = new Map<Id<'objetos'>, (typeof existingLinks)[number]>();
|
||||||
for (const link of existingLinks) {
|
for (const link of existingLinks) {
|
||||||
await ctx.db.delete(link._id);
|
existingByObjeto.set(link.objetoId, link);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adiciona os novos vínculos
|
const desiredObjetoIds = new Set<Id<'objetos'>>(args.objetos.map((o) => o.objetoId));
|
||||||
for (const objetoId of args.objetosIds) {
|
|
||||||
|
// Upsert dos vínculos desejados (preserva quantidadeUsada quando já existe)
|
||||||
|
for (const cfg of args.objetos) {
|
||||||
|
assertQuantidadeTotalValida(cfg.quantidadeTotal);
|
||||||
|
const limitePercentual = normalizeLimitePercentual(cfg.limitePercentual);
|
||||||
|
|
||||||
|
const existing = existingByObjeto.get(cfg.objetoId);
|
||||||
|
if (existing) {
|
||||||
|
await ctx.db.patch(existing._id, {
|
||||||
|
quantidadeTotal: cfg.quantidadeTotal,
|
||||||
|
limitePercentual
|
||||||
|
});
|
||||||
|
} else {
|
||||||
await ctx.db.insert('atasObjetos', {
|
await ctx.db.insert('atasObjetos', {
|
||||||
ataId: args.id,
|
ataId: args.id,
|
||||||
objetoId
|
objetoId: cfg.objetoId,
|
||||||
|
quantidadeTotal: cfg.quantidadeTotal,
|
||||||
|
limitePercentual,
|
||||||
|
quantidadeUsada: 0
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Remoção de vínculos não selecionados (somente se não houver uso em pedidos não-cancelados)
|
||||||
|
for (const link of existingLinks) {
|
||||||
|
if (desiredObjetoIds.has(link.objetoId)) continue;
|
||||||
|
|
||||||
|
const items = await ctx.db
|
||||||
|
.query('objetoItems')
|
||||||
|
.withIndex('by_ataId_and_objetoId', (q) =>
|
||||||
|
q.eq('ataId', args.id).eq('objetoId', link.objetoId)
|
||||||
|
)
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
// Se existe qualquer item em pedido não cancelado, bloquear remoção do vínculo
|
||||||
|
let inUse = false;
|
||||||
|
const seenPedidos = new Set<Id<'pedidos'>>();
|
||||||
|
for (const item of items) {
|
||||||
|
if (seenPedidos.has(item.pedidoId)) continue;
|
||||||
|
seenPedidos.add(item.pedidoId);
|
||||||
|
const pedido = await ctx.db.get(item.pedidoId);
|
||||||
|
if (pedido && pedido.status !== 'cancelado') {
|
||||||
|
inUse = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (inUse) {
|
||||||
|
throw new Error(
|
||||||
|
'Não é possível remover este objeto da ata porque já existe uso em pedidos não cancelados.'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
await ctx.db.delete(link._id);
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
export const remove = mutation({
|
export const remove = mutation({
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { v } from 'convex/values';
|
import { v } from 'convex/values';
|
||||||
import { mutation, query } from './_generated/server';
|
import { mutation, query } from './_generated/server';
|
||||||
|
import type { Id } from './_generated/dataModel';
|
||||||
import { getCurrentUserFunction } from './auth';
|
import { getCurrentUserFunction } from './auth';
|
||||||
|
|
||||||
export const list = query({
|
export const list = query({
|
||||||
@@ -145,6 +146,93 @@ export const getAtas = query({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const getAtasComLimite = query({
|
||||||
|
args: { objetoId: v.id('objetos') },
|
||||||
|
returns: v.array(
|
||||||
|
v.object({
|
||||||
|
_id: v.id('atas'),
|
||||||
|
numero: v.string(),
|
||||||
|
numeroSei: v.string(),
|
||||||
|
dataInicio: v.union(v.string(), v.null()),
|
||||||
|
dataFim: v.union(v.string(), v.null()),
|
||||||
|
quantidadeTotal: v.union(v.number(), v.null()),
|
||||||
|
limitePercentual: v.number(),
|
||||||
|
quantidadeUsada: v.number(),
|
||||||
|
limitePermitido: v.number(),
|
||||||
|
restante: v.number(),
|
||||||
|
isLocked: v.boolean()
|
||||||
|
})
|
||||||
|
),
|
||||||
|
handler: async (ctx, args) => {
|
||||||
|
const links = await ctx.db
|
||||||
|
.query('atasObjetos')
|
||||||
|
.withIndex('by_objetoId', (q) => q.eq('objetoId', args.objetoId))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
const results = [];
|
||||||
|
for (const link of links) {
|
||||||
|
const ata = await ctx.db.get(link.ataId);
|
||||||
|
if (!ata) continue;
|
||||||
|
|
||||||
|
let quantidadeUsada = link.quantidadeUsada ?? 0;
|
||||||
|
if (link.quantidadeUsada === undefined) {
|
||||||
|
const items = await ctx.db
|
||||||
|
.query('objetoItems')
|
||||||
|
.withIndex('by_ataId_and_objetoId', (q) =>
|
||||||
|
q.eq('ataId', link.ataId).eq('objetoId', link.objetoId)
|
||||||
|
)
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
const sumByPedidoId = new Map<Id<'pedidos'>, number>();
|
||||||
|
for (const item of items) {
|
||||||
|
const prev = sumByPedidoId.get(item.pedidoId) ?? 0;
|
||||||
|
sumByPedidoId.set(item.pedidoId, prev + item.quantidade);
|
||||||
|
}
|
||||||
|
|
||||||
|
let total = 0;
|
||||||
|
for (const [pedidoId, sum] of sumByPedidoId.entries()) {
|
||||||
|
const pedido = await ctx.db.get(pedidoId);
|
||||||
|
if (pedido && pedido.status !== 'cancelado') {
|
||||||
|
total += sum;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
quantidadeUsada = total;
|
||||||
|
}
|
||||||
|
|
||||||
|
const quantidadeTotal = link.quantidadeTotal ?? null;
|
||||||
|
const limitePercentualRaw = link.limitePercentual ?? 50;
|
||||||
|
const limitePercentual = Number.isFinite(limitePercentualRaw)
|
||||||
|
? Math.min(100, Math.max(0, limitePercentualRaw))
|
||||||
|
: 50;
|
||||||
|
|
||||||
|
const limitePermitido =
|
||||||
|
quantidadeTotal && quantidadeTotal > 0
|
||||||
|
? Math.floor(quantidadeTotal * (limitePercentual / 100))
|
||||||
|
: 0;
|
||||||
|
|
||||||
|
const restante = Math.max(0, limitePermitido - quantidadeUsada);
|
||||||
|
const isLocked =
|
||||||
|
!quantidadeTotal || quantidadeTotal <= 0 || quantidadeUsada >= limitePermitido;
|
||||||
|
|
||||||
|
results.push({
|
||||||
|
_id: ata._id,
|
||||||
|
numero: ata.numero,
|
||||||
|
numeroSei: ata.numeroSei,
|
||||||
|
dataInicio: ata.dataInicio ?? null,
|
||||||
|
dataFim: ata.dataFim ?? null,
|
||||||
|
quantidadeTotal,
|
||||||
|
limitePercentual,
|
||||||
|
quantidadeUsada,
|
||||||
|
limitePermitido,
|
||||||
|
restante,
|
||||||
|
isLocked
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
export const remove = mutation({
|
export const remove = mutation({
|
||||||
args: {
|
args: {
|
||||||
id: v.id('objetos')
|
id: v.id('objetos')
|
||||||
|
|||||||
@@ -192,6 +192,152 @@ async function ensurePedidoModalidadeAtaConsistency(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type AtaObjetoUsageInfo = {
|
||||||
|
linkId: Id<'atasObjetos'>;
|
||||||
|
quantidadeTotal: number;
|
||||||
|
limitePercentual: number;
|
||||||
|
limitePermitido: number;
|
||||||
|
quantidadeUsada: number;
|
||||||
|
restante: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
function normalizeLimitePercentual(value: number | undefined): number {
|
||||||
|
const fallback = 50;
|
||||||
|
if (value === undefined) return fallback;
|
||||||
|
if (!Number.isFinite(value)) return fallback;
|
||||||
|
// Normalizar para faixa 0..100
|
||||||
|
if (value < 0) return 0;
|
||||||
|
if (value > 100) return 100;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
function computeLimitePermitido(quantidadeTotal: number, limitePercentual: number): number {
|
||||||
|
if (!Number.isFinite(quantidadeTotal) || quantidadeTotal <= 0) return 0;
|
||||||
|
const pct = normalizeLimitePercentual(limitePercentual);
|
||||||
|
return Math.floor(quantidadeTotal * (pct / 100));
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getAtaObjetoLinkOrThrow(
|
||||||
|
ctx: MutationCtx,
|
||||||
|
ataId: Id<'atas'>,
|
||||||
|
objetoId: Id<'objetos'>
|
||||||
|
): Promise<Doc<'atasObjetos'>> {
|
||||||
|
const link = await ctx.db
|
||||||
|
.query('atasObjetos')
|
||||||
|
.withIndex('by_ataId_and_objetoId', (q) => q.eq('ataId', ataId).eq('objetoId', objetoId))
|
||||||
|
.unique();
|
||||||
|
|
||||||
|
if (!link) {
|
||||||
|
throw new Error('Esta ata não está vinculada a este objeto.');
|
||||||
|
}
|
||||||
|
return link;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function computeQuantidadeUsadaFromDb(
|
||||||
|
ctx: MutationCtx,
|
||||||
|
ataId: Id<'atas'>,
|
||||||
|
objetoId: Id<'objetos'>
|
||||||
|
): Promise<number> {
|
||||||
|
const items = await ctx.db
|
||||||
|
.query('objetoItems')
|
||||||
|
.withIndex('by_ataId_and_objetoId', (q) => q.eq('ataId', ataId).eq('objetoId', objetoId))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
const sumByPedidoId = new Map<Id<'pedidos'>, number>();
|
||||||
|
for (const item of items) {
|
||||||
|
const prev = sumByPedidoId.get(item.pedidoId) ?? 0;
|
||||||
|
sumByPedidoId.set(item.pedidoId, prev + item.quantidade);
|
||||||
|
}
|
||||||
|
|
||||||
|
let total = 0;
|
||||||
|
for (const [pedidoId, sum] of sumByPedidoId.entries()) {
|
||||||
|
const pedido = await ctx.db.get(pedidoId);
|
||||||
|
if (pedido && pedido.status !== 'cancelado') {
|
||||||
|
total += sum;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getAtaObjetoUsageInfo(
|
||||||
|
ctx: MutationCtx,
|
||||||
|
ataId: Id<'atas'>,
|
||||||
|
objetoId: Id<'objetos'>,
|
||||||
|
requireConfigured: boolean
|
||||||
|
): Promise<AtaObjetoUsageInfo> {
|
||||||
|
const link = await getAtaObjetoLinkOrThrow(ctx, ataId, objetoId);
|
||||||
|
|
||||||
|
const quantidadeTotalRaw = link.quantidadeTotal;
|
||||||
|
const hasQuantidadeTotal =
|
||||||
|
quantidadeTotalRaw !== undefined &&
|
||||||
|
Number.isFinite(quantidadeTotalRaw) &&
|
||||||
|
quantidadeTotalRaw > 0;
|
||||||
|
|
||||||
|
if (requireConfigured && !hasQuantidadeTotal) {
|
||||||
|
throw new Error(
|
||||||
|
'Esta ata está vinculada ao objeto, mas a quantidade do produto na ata não foi configurada. Configure a quantidade/limite antes de usar esta ata.'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const limitePercentual = normalizeLimitePercentual(link.limitePercentual);
|
||||||
|
const quantidadeTotal = hasQuantidadeTotal ? (quantidadeTotalRaw as number) : 0;
|
||||||
|
const limitePermitido = computeLimitePermitido(quantidadeTotal, limitePercentual);
|
||||||
|
|
||||||
|
let quantidadeUsada = link.quantidadeUsada;
|
||||||
|
if (quantidadeUsada === undefined) {
|
||||||
|
quantidadeUsada = await computeQuantidadeUsadaFromDb(ctx, ataId, objetoId);
|
||||||
|
await ctx.db.patch(link._id, { quantidadeUsada });
|
||||||
|
}
|
||||||
|
|
||||||
|
const restante = Math.max(0, limitePermitido - quantidadeUsada);
|
||||||
|
|
||||||
|
return {
|
||||||
|
linkId: link._id,
|
||||||
|
quantidadeTotal,
|
||||||
|
limitePercentual,
|
||||||
|
limitePermitido,
|
||||||
|
quantidadeUsada,
|
||||||
|
restante
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async function assertAtaObjetoCanConsume(
|
||||||
|
ctx: MutationCtx,
|
||||||
|
ataId: Id<'atas'>,
|
||||||
|
objetoId: Id<'objetos'>,
|
||||||
|
delta: number
|
||||||
|
) {
|
||||||
|
if (!Number.isFinite(delta) || delta <= 0) return;
|
||||||
|
const info = await getAtaObjetoUsageInfo(ctx, ataId, objetoId, true);
|
||||||
|
if (info.quantidadeUsada + delta > info.limitePermitido) {
|
||||||
|
throw new Error(
|
||||||
|
`Limite de uso da ata atingido para este objeto. Limite permitido: ${info.limitePermitido}. Usado: ${info.quantidadeUsada}. Tentativa de adicionar: ${delta}. Restante: ${info.restante}.`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function applyAtaObjetoUsageDelta(
|
||||||
|
ctx: MutationCtx,
|
||||||
|
ataId: Id<'atas'>,
|
||||||
|
objetoId: Id<'objetos'>,
|
||||||
|
delta: number
|
||||||
|
) {
|
||||||
|
if (!Number.isFinite(delta) || delta === 0) return;
|
||||||
|
const info = await getAtaObjetoUsageInfo(ctx, ataId, objetoId, delta > 0);
|
||||||
|
|
||||||
|
const novoUsadoRaw = info.quantidadeUsada + delta;
|
||||||
|
const novoUsado = Math.max(0, novoUsadoRaw);
|
||||||
|
|
||||||
|
if (delta > 0 && novoUsado > info.limitePermitido) {
|
||||||
|
throw new Error(
|
||||||
|
`Limite de uso da ata atingido para este objeto. Limite permitido: ${info.limitePermitido}. Usado: ${info.quantidadeUsada}. Tentativa de adicionar: ${delta}. Restante: ${info.restante}.`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
await ctx.db.patch(info.linkId, { quantidadeUsada: novoUsado });
|
||||||
|
}
|
||||||
|
|
||||||
async function isFuncionarioInComprasSector(
|
async function isFuncionarioInComprasSector(
|
||||||
ctx: QueryCtx | MutationCtx,
|
ctx: QueryCtx | MutationCtx,
|
||||||
funcionarioId: Id<'funcionarios'>
|
funcionarioId: Id<'funcionarios'>
|
||||||
@@ -1194,6 +1340,10 @@ export const addItem = mutation({
|
|||||||
// Em pedidos em análise, a inclusão de itens deve passar por fluxo de aprovação.
|
// Em pedidos em análise, a inclusão de itens deve passar por fluxo de aprovação.
|
||||||
// Em rascunho ou aguardando aceite, a inclusão é direta, sem necessidade de aprovação.
|
// Em rascunho ou aguardando aceite, a inclusão é direta, sem necessidade de aprovação.
|
||||||
if (pedido.status === 'em_analise') {
|
if (pedido.status === 'em_analise') {
|
||||||
|
if (args.ataId) {
|
||||||
|
// Não altera consumo aqui (ainda é só solicitação), mas valida limite/configuração.
|
||||||
|
await assertAtaObjetoCanConsume(ctx, args.ataId, args.objetoId, args.quantidade);
|
||||||
|
}
|
||||||
await ctx.db.insert('solicitacoesItens', {
|
await ctx.db.insert('solicitacoesItens', {
|
||||||
pedidoId: args.pedidoId,
|
pedidoId: args.pedidoId,
|
||||||
tipo: 'adicao',
|
tipo: 'adicao',
|
||||||
@@ -1234,6 +1384,9 @@ export const addItem = mutation({
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (existingItem) {
|
if (existingItem) {
|
||||||
|
if (args.ataId) {
|
||||||
|
await applyAtaObjetoUsageDelta(ctx, args.ataId, args.objetoId, args.quantidade);
|
||||||
|
}
|
||||||
// Increment quantity
|
// Increment quantity
|
||||||
const novaQuantidade = existingItem.quantidade + args.quantidade;
|
const novaQuantidade = existingItem.quantidade + args.quantidade;
|
||||||
await ctx.db.patch(existingItem._id, { quantidade: novaQuantidade });
|
await ctx.db.patch(existingItem._id, { quantidade: novaQuantidade });
|
||||||
@@ -1250,6 +1403,9 @@ export const addItem = mutation({
|
|||||||
data: Date.now()
|
data: Date.now()
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
if (args.ataId) {
|
||||||
|
await applyAtaObjetoUsageDelta(ctx, args.ataId, args.objetoId, args.quantidade);
|
||||||
|
}
|
||||||
// Insert new item
|
// Insert new item
|
||||||
await ctx.db.insert('objetoItems', {
|
await ctx.db.insert('objetoItems', {
|
||||||
pedidoId: args.pedidoId,
|
pedidoId: args.pedidoId,
|
||||||
@@ -1302,8 +1458,14 @@ export const updateItemQuantity = mutation({
|
|||||||
const pedido = await ctx.db.get(item.pedidoId);
|
const pedido = await ctx.db.get(item.pedidoId);
|
||||||
if (!pedido) throw new Error('Pedido não encontrado.');
|
if (!pedido) throw new Error('Pedido não encontrado.');
|
||||||
|
|
||||||
|
const delta = args.novaQuantidade - item.quantidade;
|
||||||
|
|
||||||
// --- CHECK ANALYSIS / ACCEPTANCE MODE ---
|
// --- CHECK ANALYSIS / ACCEPTANCE MODE ---
|
||||||
if (pedido.status === 'em_analise' || pedido.status === 'aguardando_aceite') {
|
if (pedido.status === 'em_analise' || pedido.status === 'aguardando_aceite') {
|
||||||
|
if (delta > 0 && item.ataId) {
|
||||||
|
// Não altera consumo aqui (ainda é só solicitação), mas valida limite/configuração.
|
||||||
|
await assertAtaObjetoCanConsume(ctx, item.ataId, item.objetoId, delta);
|
||||||
|
}
|
||||||
await ctx.db.insert('solicitacoesItens', {
|
await ctx.db.insert('solicitacoesItens', {
|
||||||
pedidoId: item.pedidoId,
|
pedidoId: item.pedidoId,
|
||||||
tipo: 'alteracao_quantidade',
|
tipo: 'alteracao_quantidade',
|
||||||
@@ -1324,6 +1486,11 @@ export const updateItemQuantity = mutation({
|
|||||||
throw new Error('Apenas quem adicionou este item pode alterar a quantidade.');
|
throw new Error('Apenas quem adicionou este item pode alterar a quantidade.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Atualiza consumo (ata+objeto) antes de persistir a nova quantidade
|
||||||
|
if (item.ataId) {
|
||||||
|
await applyAtaObjetoUsageDelta(ctx, item.ataId, item.objetoId, delta);
|
||||||
|
}
|
||||||
|
|
||||||
// Update quantity
|
// Update quantity
|
||||||
await ctx.db.patch(args.itemId, { quantidade: args.novaQuantidade });
|
await ctx.db.patch(args.itemId, { quantidade: args.novaQuantidade });
|
||||||
await ctx.db.patch(item.pedidoId, { atualizadoEm: Date.now() });
|
await ctx.db.patch(item.pedidoId, { atualizadoEm: Date.now() });
|
||||||
@@ -1371,6 +1538,10 @@ export const removeItem = mutation({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (item.ataId) {
|
||||||
|
await applyAtaObjetoUsageDelta(ctx, item.ataId, item.objetoId, -item.quantidade);
|
||||||
|
}
|
||||||
|
|
||||||
await ctx.db.delete(args.itemId);
|
await ctx.db.delete(args.itemId);
|
||||||
await ctx.db.patch(item.pedidoId, { atualizadoEm: Date.now() });
|
await ctx.db.patch(item.pedidoId, { atualizadoEm: Date.now() });
|
||||||
|
|
||||||
@@ -1436,6 +1607,10 @@ export const removeItemsBatch = mutation({
|
|||||||
throw new Error('Você só pode remover itens que você adicionou.');
|
throw new Error('Você só pode remover itens que você adicionou.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (item.ataId) {
|
||||||
|
await applyAtaObjetoUsageDelta(ctx, item.ataId, item.objetoId, -item.quantidade);
|
||||||
|
}
|
||||||
|
|
||||||
await ctx.db.delete(itemId);
|
await ctx.db.delete(itemId);
|
||||||
|
|
||||||
await ctx.db.insert('historicoPedidos', {
|
await ctx.db.insert('historicoPedidos', {
|
||||||
@@ -1596,6 +1771,13 @@ export const updateItem = mutation({
|
|||||||
|
|
||||||
// Em pedidos em análise ou aguardando aceite, geramos uma solicitação em vez de alterar diretamente
|
// Em pedidos em análise ou aguardando aceite, geramos uma solicitação em vez de alterar diretamente
|
||||||
if (pedido.status === 'em_analise' || pedido.status === 'aguardando_aceite') {
|
if (pedido.status === 'em_analise' || pedido.status === 'aguardando_aceite') {
|
||||||
|
const oldAtaId = oldValues.ataId;
|
||||||
|
const newAtaId = args.ataId;
|
||||||
|
if (newAtaId && newAtaId !== oldAtaId) {
|
||||||
|
// Não altera consumo aqui (ainda é só solicitação), mas valida limite/configuração.
|
||||||
|
await assertAtaObjetoCanConsume(ctx, newAtaId, item.objetoId, item.quantidade);
|
||||||
|
}
|
||||||
|
|
||||||
await ctx.db.insert('solicitacoesItens', {
|
await ctx.db.insert('solicitacoesItens', {
|
||||||
pedidoId: item.pedidoId,
|
pedidoId: item.pedidoId,
|
||||||
tipo: 'alteracao_detalhes',
|
tipo: 'alteracao_detalhes',
|
||||||
@@ -1617,6 +1799,18 @@ export const updateItem = mutation({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Se a ata mudou, mover consumo entre vínculos (ata antiga -> nova ata)
|
||||||
|
const oldAtaId = oldValues.ataId;
|
||||||
|
const newAtaId = args.ataId;
|
||||||
|
if ((oldAtaId ?? null) !== (newAtaId ?? null)) {
|
||||||
|
if (newAtaId) {
|
||||||
|
await applyAtaObjetoUsageDelta(ctx, newAtaId, item.objetoId, item.quantidade);
|
||||||
|
}
|
||||||
|
if (oldAtaId) {
|
||||||
|
await applyAtaObjetoUsageDelta(ctx, oldAtaId, item.objetoId, -item.quantidade);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
await ctx.db.patch(args.itemId, {
|
await ctx.db.patch(args.itemId, {
|
||||||
valorEstimado: args.valorEstimado,
|
valorEstimado: args.valorEstimado,
|
||||||
modalidade: args.modalidade,
|
modalidade: args.modalidade,
|
||||||
@@ -1696,8 +1890,7 @@ export const getPermissions = query({
|
|||||||
const isCreator = pedido.criadoPor === user._id;
|
const isCreator = pedido.criadoPor === user._id;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
canSendToAcceptance:
|
canSendToAcceptance: pedido.status === 'em_rascunho' && hasAddedItems,
|
||||||
(pedido.status === 'em_rascunho' || pedido.status === 'precisa_ajustes') && hasAddedItems,
|
|
||||||
canStartAnalysis: pedido.status === 'aguardando_aceite' && isInComprasSector,
|
canStartAnalysis: pedido.status === 'aguardando_aceite' && isInComprasSector,
|
||||||
canConclude: pedido.status === 'em_analise' && isInComprasSector,
|
canConclude: pedido.status === 'em_analise' && isInComprasSector,
|
||||||
canRequestAdjustments:
|
canRequestAdjustments:
|
||||||
@@ -1726,7 +1919,7 @@ export const enviarParaAceite = mutation({
|
|||||||
const pedido = await ctx.db.get(args.pedidoId);
|
const pedido = await ctx.db.get(args.pedidoId);
|
||||||
if (!pedido) throw new Error('Pedido não encontrado.');
|
if (!pedido) throw new Error('Pedido não encontrado.');
|
||||||
|
|
||||||
if (pedido.status !== 'em_rascunho' && pedido.status !== 'precisa_ajustes') {
|
if (pedido.status !== 'em_rascunho') {
|
||||||
throw new Error('Status inválido para envio.');
|
throw new Error('Status inválido para envio.');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2015,6 +2208,18 @@ export const cancelarPedido = mutation({
|
|||||||
const oldStatus = pedido.status;
|
const oldStatus = pedido.status;
|
||||||
const newStatus = 'cancelado';
|
const newStatus = 'cancelado';
|
||||||
|
|
||||||
|
// Ao cancelar, liberar consumo de todas as combinações (ataId,objetoId) deste pedido
|
||||||
|
const items = await ctx.db
|
||||||
|
.query('objetoItems')
|
||||||
|
.withIndex('by_pedidoId', (q) => q.eq('pedidoId', args.pedidoId))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
for (const item of items) {
|
||||||
|
if (item.ataId) {
|
||||||
|
await applyAtaObjetoUsageDelta(ctx, item.ataId, item.objetoId, -item.quantidade);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
await ctx.db.patch(args.pedidoId, {
|
await ctx.db.patch(args.pedidoId, {
|
||||||
status: newStatus,
|
status: newStatus,
|
||||||
atualizadoEm: Date.now()
|
atualizadoEm: Date.now()
|
||||||
@@ -2253,6 +2458,10 @@ export const approveItemRequest = mutation({
|
|||||||
i.modalidade === newItem.modalidade
|
i.modalidade === newItem.modalidade
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (newItem.ataId) {
|
||||||
|
await applyAtaObjetoUsageDelta(ctx, newItem.ataId, newItem.objetoId, newItem.quantidade);
|
||||||
|
}
|
||||||
|
|
||||||
if (existingItem) {
|
if (existingItem) {
|
||||||
await ctx.db.patch(existingItem._id, {
|
await ctx.db.patch(existingItem._id, {
|
||||||
quantidade: existingItem.quantidade + newItem.quantidade
|
quantidade: existingItem.quantidade + newItem.quantidade
|
||||||
@@ -2273,15 +2482,25 @@ export const approveItemRequest = mutation({
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else if (request.tipo === 'alteracao_quantidade') {
|
} else if (request.tipo === 'alteracao_quantidade') {
|
||||||
const { itemId, novaQuantidade } = data;
|
const { itemId, novaQuantidade } = data as {
|
||||||
const item = await ctx.db.get(itemId);
|
itemId: Id<'objetoItems'>;
|
||||||
|
novaQuantidade: number;
|
||||||
|
};
|
||||||
|
const item = (await ctx.db.get(itemId)) as Doc<'objetoItems'> | null;
|
||||||
if (item) {
|
if (item) {
|
||||||
|
const delta = novaQuantidade - item.quantidade;
|
||||||
|
if (item.ataId) {
|
||||||
|
await applyAtaObjetoUsageDelta(ctx, item.ataId, item.objetoId, delta);
|
||||||
|
}
|
||||||
await ctx.db.patch(itemId, { quantidade: novaQuantidade });
|
await ctx.db.patch(itemId, { quantidade: novaQuantidade });
|
||||||
}
|
}
|
||||||
} else if (request.tipo === 'exclusao') {
|
} else if (request.tipo === 'exclusao') {
|
||||||
const { itemId } = data;
|
const { itemId } = data as { itemId: Id<'objetoItems'> };
|
||||||
const item = await ctx.db.get(itemId);
|
const item = (await ctx.db.get(itemId)) as Doc<'objetoItems'> | null;
|
||||||
if (item) {
|
if (item) {
|
||||||
|
if (item.ataId) {
|
||||||
|
await applyAtaObjetoUsageDelta(ctx, item.ataId, item.objetoId, -item.quantidade);
|
||||||
|
}
|
||||||
await ctx.db.delete(itemId);
|
await ctx.db.delete(itemId);
|
||||||
}
|
}
|
||||||
} else if (request.tipo === 'alteracao_detalhes') {
|
} else if (request.tipo === 'alteracao_detalhes') {
|
||||||
@@ -2306,6 +2525,17 @@ export const approveItemRequest = mutation({
|
|||||||
itemId
|
itemId
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const oldAtaId = ('ataId' in item ? item.ataId : undefined) ?? undefined;
|
||||||
|
const newAtaId = para.ataId;
|
||||||
|
if ((oldAtaId ?? null) !== (newAtaId ?? null)) {
|
||||||
|
if (newAtaId) {
|
||||||
|
await applyAtaObjetoUsageDelta(ctx, newAtaId, item.objetoId, item.quantidade);
|
||||||
|
}
|
||||||
|
if (oldAtaId) {
|
||||||
|
await applyAtaObjetoUsageDelta(ctx, oldAtaId, item.objetoId, -item.quantidade);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
await ctx.db.patch(itemId, {
|
await ctx.db.patch(itemId, {
|
||||||
valorEstimado: para.valorEstimado,
|
valorEstimado: para.valorEstimado,
|
||||||
modalidade: para.modalidade,
|
modalidade: para.modalidade,
|
||||||
|
|||||||
@@ -18,10 +18,16 @@ export const atasTables = {
|
|||||||
|
|
||||||
atasObjetos: defineTable({
|
atasObjetos: defineTable({
|
||||||
ataId: v.id('atas'),
|
ataId: v.id('atas'),
|
||||||
objetoId: v.id('objetos')
|
objetoId: v.id('objetos'),
|
||||||
|
// Configuração de limite de uso por (ataId, objetoId)
|
||||||
|
quantidadeTotal: v.optional(v.number()),
|
||||||
|
limitePercentual: v.optional(v.number()), // padrão lógico: 50
|
||||||
|
// Controle transacional para evitar corrida; se ausente, pode ser inicializado via rebuild.
|
||||||
|
quantidadeUsada: v.optional(v.number())
|
||||||
})
|
})
|
||||||
.index('by_ataId', ['ataId'])
|
.index('by_ataId', ['ataId'])
|
||||||
.index('by_objetoId', ['objetoId']),
|
.index('by_objetoId', ['objetoId'])
|
||||||
|
.index('by_ataId_and_objetoId', ['ataId', 'objetoId']),
|
||||||
|
|
||||||
atasDocumentos: defineTable({
|
atasDocumentos: defineTable({
|
||||||
ataId: v.id('atas'),
|
ataId: v.id('atas'),
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ export const pedidosTables = {
|
|||||||
})
|
})
|
||||||
.index('by_pedidoId', ['pedidoId'])
|
.index('by_pedidoId', ['pedidoId'])
|
||||||
.index('by_objetoId', ['objetoId'])
|
.index('by_objetoId', ['objetoId'])
|
||||||
|
.index('by_ataId_and_objetoId', ['ataId', 'objetoId'])
|
||||||
.index('by_adicionadoPor', ['adicionadoPor'])
|
.index('by_adicionadoPor', ['adicionadoPor'])
|
||||||
.index('by_acaoId', ['acaoId']),
|
.index('by_acaoId', ['acaoId']),
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user