// Vendas — lista, detalhe, novo const { cnpjs, vendedores, clientes, tipos, statusList, formasPagamento } = window.MP_DATA; function calcFaturado(venda) { return (venda.faturamentos || []).reduce((s, f) => s + (f.valor || 0), 0); } function calcEntradas(venda) { return (venda.entradas || []).reduce((s, e) => s + (e.valor || 0), 0); } function vendaPorCnpj(venda) { const m = {}; (venda.faturamentos || []).forEach(f => { m[f.cnpjId] = (m[f.cnpjId] || 0) + (f.valor || 0); }); return m; } // ───────────────────────────────────────────────────────────────────────────── // LISTA DE VENDAS // ───────────────────────────────────────────────────────────────────────────── function VendasList({ vendas, onOpen, onNew, view }) { const [filtroStatus, setFiltroStatus] = React.useState("todos"); const [filtroVendedor, setFiltroVendedor] = React.useState("todos"); const [busca, setBusca] = React.useState(""); const [periodo, setPeriodo] = React.useState("maio-2026"); // Apply filters let lista = vendas.slice(); if (filtroStatus !== "todos") lista = lista.filter(v => v.status === filtroStatus); if (filtroVendedor !== "todos") lista = lista.filter(v => v.vendedorId === filtroVendedor); if (busca) { const q = busca.toLowerCase(); lista = lista.filter(v => { const c = clientes.find(x => x.id === v.clienteId); return v.op.includes(q) || (c?.nome.toLowerCase().includes(q)) || v.descricao.toLowerCase().includes(q); }); } // KPIs const totalVenda = lista.reduce((s, v) => s + v.valor, 0); const totalFaturado = lista.reduce((s, v) => s + calcFaturado(v), 0); const totalEntradas = lista.reduce((s, v) => s + calcEntradas(v), 0); const totalPeso = lista.reduce((s, v) => s + v.peso, 0); const saldoReceber = totalFaturado - totalEntradas; // Faturamento por CNPJ const fatPorCnpj = {}; lista.forEach(v => { Object.entries(vendaPorCnpj(v)).forEach(([id, val]) => { fatPorCnpj[id] = (fatPorCnpj[id] || 0) + val; }); }); return ( <>
Módulo Vendas

Vendas de Maio · 2026

{lista.length} {lista.length === 1 ? "operação" : "operações"} no período · Substitui a planilha “VENDAS 2026”.

setPeriodo("abril-2026")}>Abril
setPeriodo("maio-2026")}>Maio
setPeriodo("junho-2026")}>Junho
{/* CNPJ split bar */}
{cnpjs.map(c => { const v = fatPorCnpj[c.id] || 0; const pct = totalFaturado > 0 ? (v / totalFaturado) * 100 : 0; return (
{fmtBRL(v)} {pct.toFixed(0)}%
); })}
setBusca(e.target.value)} placeholder="Buscar OP, cliente ou descrição..." style={{ marginLeft: 8 }} />
setFiltroStatus("todos")}>Todos
{statusList.map(s => (
setFiltroStatus(s.id)}> {s.nome}
))}