-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
525 lines (482 loc) · 21.3 KB
/
app.js
File metadata and controls
525 lines (482 loc) · 21.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
// ===== BANCO DE DADOS (localStorage) =====
const DB = {
get: (key) => JSON.parse(localStorage.getItem(key) || '[]'),
set: (key, data) => localStorage.setItem(key, JSON.stringify(data)),
uid: () => Date.now().toString(36) + Math.random().toString(36).slice(2, 7)
};
const KEYS = { veiculos: 'ag_veiculos', compras: 'ag_compras', vendas: 'ag_vendas' };
// ===== UTILITÁRIOS =====
const fmt = {
currency: (v) => {
const n = parseFloat(v) || 0;
return n.toLocaleString('pt-BR', { style: 'currency', currency: 'BRL' });
},
date: (s) => {
if (!s) return '—';
const [y, m, d] = s.split('-');
return `${d}/${m}/${y}`;
},
parseCurrency: (s) => {
if (!s) return 0;
return parseFloat(s.toString().replace(/\./g, '').replace(',', '.')) || 0;
}
};
function toast(msg, type = 'info') {
const tc = document.getElementById('toastContainer');
const el = document.createElement('div');
el.className = `toast ${type}`;
const icons = { success: 'fa-check-circle', error: 'fa-circle-xmark', info: 'fa-circle-info' };
el.innerHTML = `<i class="fa-solid ${icons[type] || icons.info}"></i> ${msg}`;
tc.appendChild(el);
setTimeout(() => el.remove(), 3200);
}
function openModal(id) { document.getElementById(id).classList.add('open'); }
function closeModal(id) { document.getElementById(id).classList.remove('open'); }
// Formata inputs de moeda
function setupCurrencyInputs() {
document.querySelectorAll('.currency-input:not([readonly])').forEach(inp => {
inp.addEventListener('input', function () {
let raw = this.value.replace(/\D/g, '');
if (!raw) { this.value = ''; return; }
let num = parseInt(raw, 10) / 100;
this.value = num.toLocaleString('pt-BR', { minimumFractionDigits: 2, maximumFractionDigits: 2 });
});
});
}
function currencyVal(id) { return fmt.parseCurrency(document.getElementById(id).value); }
function setCurrencyVal(id, v) {
const el = document.getElementById(id);
if (!el) return;
el.value = v ? v.toLocaleString('pt-BR', { minimumFractionDigits: 2, maximumFractionDigits: 2 }) : '';
}
// ===== NAVEGAÇÃO =====
const pageTitles = {
dashboard: 'Dashboard', veiculos: 'Veículos', compras: 'Compras',
vendas: 'Vendas', relatorios: 'Relatórios'
};
function navigate(page) {
document.querySelectorAll('.page').forEach(p => p.classList.remove('active'));
document.querySelectorAll('.nav-item').forEach(n => n.classList.remove('active'));
document.getElementById(`page-${page}`).classList.add('active');
document.querySelector(`[data-page="${page}"]`).classList.add('active');
document.getElementById('pageTitle').textContent = pageTitles[page];
if (page === 'dashboard') renderDashboard();
if (page === 'veiculos') renderVeiculos();
if (page === 'compras') renderCompras();
if (page === 'vendas') renderVendas();
if (page === 'relatorios') renderRelatorios();
}
// ===== VEÍCULOS =====
function renderVeiculos(filter = '') {
const veiculos = DB.get(KEYS.veiculos);
const tbody = document.getElementById('veiculosBody');
const list = filter
? veiculos.filter(v => `${v.marca} ${v.modelo} ${v.placa}`.toLowerCase().includes(filter.toLowerCase()))
: veiculos;
if (!list.length) {
tbody.innerHTML = `<tr><td colspan="9" class="empty-row">Nenhum veículo cadastrado</td></tr>`;
return;
}
tbody.innerHTML = list.map(v => `
<tr>
<td><strong>${v.placa}</strong></td>
<td>${v.marca} ${v.modelo}</td>
<td>${v.anoFab}${v.anoMod && v.anoMod !== v.anoFab ? `/${v.anoMod}` : ''}</td>
<td>${v.cor}</td>
<td>${v.combustivel}</td>
<td><span class="badge ${statusBadge(v.status)}">${v.status}</span></td>
<td>${fmt.currency(v.precoCompra)}</td>
<td>${v.precoVenda ? fmt.currency(v.precoVenda) : '—'}</td>
<td>
<button class="btn-icon" onclick="editarVeiculo('${v.id}')" title="Editar"><i class="fa-solid fa-pen"></i></button>
<button class="btn-icon delete" onclick="deletarRegistro('veiculo','${v.id}')" title="Excluir"><i class="fa-solid fa-trash"></i></button>
</td>
</tr>`).join('');
}
function statusBadge(s) {
const map = { 'Em estoque': 'badge-blue', 'Vendido': 'badge-success', 'Reservado': 'badge-gray' };
return map[s] || 'badge-gray';
}
function abrirModalVeiculo() {
document.getElementById('formVeiculo').reset();
document.getElementById('veiculoId').value = '';
document.getElementById('modalVeiculoTitle').textContent = 'Cadastrar Veículo';
openModal('modalVeiculo');
}
function editarVeiculo(id) {
const v = DB.get(KEYS.veiculos).find(x => x.id === id);
if (!v) return;
document.getElementById('veiculoId').value = v.id;
document.getElementById('vPlaca').value = v.placa;
document.getElementById('vMarca').value = v.marca;
document.getElementById('vModelo').value = v.modelo;
document.getElementById('vAnoFab').value = v.anoFab;
document.getElementById('vAnoMod').value = v.anoMod || '';
document.getElementById('vCor').value = v.cor;
document.getElementById('vCombustivel').value = v.combustivel;
document.getElementById('vCambio').value = v.cambio;
document.getElementById('vKm').value = v.km || '';
setCurrencyVal('vPrecoCompra', v.precoCompra);
setCurrencyVal('vPrecoVenda', v.precoVenda);
document.getElementById('vObs').value = v.obs || '';
document.getElementById('modalVeiculoTitle').textContent = 'Editar Veículo';
openModal('modalVeiculo');
}
function salvarVeiculo() {
const placa = document.getElementById('vPlaca').value.trim();
const marca = document.getElementById('vMarca').value.trim();
const modelo = document.getElementById('vModelo').value.trim();
const anoFab = document.getElementById('vAnoFab').value;
const cor = document.getElementById('vCor').value.trim();
const preco = currencyVal('vPrecoCompra');
if (!placa || !marca || !modelo || !anoFab || !cor || !preco) {
toast('Preencha todos os campos obrigatórios (*)', 'error'); return;
}
const veiculos = DB.get(KEYS.veiculos);
const id = document.getElementById('veiculoId').value;
const dados = {
id: id || DB.uid(),
placa: placa.toUpperCase(),
marca, modelo,
anoFab: parseInt(anoFab),
anoMod: parseInt(document.getElementById('vAnoMod').value) || parseInt(anoFab),
cor,
combustivel: document.getElementById('vCombustivel').value,
cambio: document.getElementById('vCambio').value,
km: parseInt(document.getElementById('vKm').value) || 0,
precoCompra: preco,
precoVenda: currencyVal('vPrecoVenda'),
obs: document.getElementById('vObs').value,
status: 'Em estoque',
criadoEm: new Date().toISOString()
};
if (id) {
const idx = veiculos.findIndex(x => x.id === id);
dados.status = veiculos[idx].status; // mantém status
veiculos[idx] = { ...veiculos[idx], ...dados };
toast('Veículo atualizado!', 'success');
} else {
veiculos.push(dados);
toast('Veículo cadastrado!', 'success');
}
DB.set(KEYS.veiculos, veiculos);
closeModal('modalVeiculo');
renderVeiculos();
}
// ===== COMPRAS =====
function renderCompras() {
const compras = DB.get(KEYS.compras);
const veiculos = DB.get(KEYS.veiculos);
const tbody = document.getElementById('comprasBody');
if (!compras.length) {
tbody.innerHTML = `<tr><td colspan="6" class="empty-row">Nenhuma compra registrada</td></tr>`;
return;
}
tbody.innerHTML = compras.map(c => {
const v = veiculos.find(x => x.id === c.veiculoId);
const nomeV = v ? `${v.placa} — ${v.marca} ${v.modelo}` : 'Veículo removido';
return `<tr>
<td>${fmt.date(c.data)}</td>
<td>${nomeV}</td>
<td>${c.fornecedor || '—'}</td>
<td><strong>${fmt.currency(c.valor)}</strong></td>
<td>${c.obs || '—'}</td>
<td>
<button class="btn-icon delete" onclick="deletarRegistro('compra','${c.id}')" title="Excluir"><i class="fa-solid fa-trash"></i></button>
</td>
</tr>`;
}).join('');
}
function abrirModalCompra() {
document.getElementById('formCompra').reset();
document.getElementById('compraId').value = '';
document.getElementById('cData').value = new Date().toISOString().split('T')[0];
preencherSelectVeiculos('cVeiculo', false);
openModal('modalCompra');
}
function salvarCompra() {
const data = document.getElementById('cData').value;
const veiculoId = document.getElementById('cVeiculo').value;
const valor = currencyVal('cValor');
if (!data || !veiculoId || !valor) {
toast('Preencha todos os campos obrigatórios (*)', 'error'); return;
}
const compras = DB.get(KEYS.compras);
const compra = {
id: DB.uid(), data, veiculoId,
fornecedor: document.getElementById('cFornecedor').value.trim(),
valor,
obs: document.getElementById('cObs').value.trim(),
criadoEm: new Date().toISOString()
};
// Atualiza preço de compra no veículo
const veiculos = DB.get(KEYS.veiculos);
const vi = veiculos.findIndex(x => x.id === veiculoId);
if (vi !== -1) {
veiculos[vi].precoCompra = valor;
DB.set(KEYS.veiculos, veiculos);
}
compras.push(compra);
DB.set(KEYS.compras, compras);
toast('Compra registrada!', 'success');
closeModal('modalCompra');
renderCompras();
}
// ===== VENDAS =====
function renderVendas() {
const vendas = DB.get(KEYS.vendas);
const veiculos = DB.get(KEYS.veiculos);
const tbody = document.getElementById('vendasBody');
if (!vendas.length) {
tbody.innerHTML = `<tr><td colspan="8" class="empty-row">Nenhuma venda registrada</td></tr>`;
return;
}
tbody.innerHTML = vendas.map(vd => {
const v = veiculos.find(x => x.id === vd.veiculoId);
const nomeV = v ? `${v.placa} — ${v.marca} ${v.modelo}` : 'Veículo removido';
const lucro = vd.valorVenda - vd.custo;
const margem = vd.custo > 0 ? ((lucro / vd.custo) * 100).toFixed(1) : '—';
const lucroClass = lucro >= 0 ? 'color:#166534' : 'color:#991b1b';
return `<tr>
<td>${fmt.date(vd.data)}</td>
<td>${nomeV}</td>
<td>${vd.comprador || '—'}</td>
<td>${fmt.currency(vd.custo)}</td>
<td><strong>${fmt.currency(vd.valorVenda)}</strong></td>
<td style="${lucroClass}"><strong>${fmt.currency(lucro)}</strong></td>
<td>${margem !== '—' ? margem + '%' : '—'}</td>
<td>
<button class="btn-icon delete" onclick="deletarRegistro('venda','${vd.id}')" title="Excluir"><i class="fa-solid fa-trash"></i></button>
</td>
</tr>`;
}).join('');
}
function abrirModalVenda() {
document.getElementById('formVenda').reset();
document.getElementById('vendaId').value = '';
document.getElementById('vdData').value = new Date().toISOString().split('T')[0];
document.getElementById('vdCusto').value = '';
document.getElementById('vdLucro').value = '';
preencherSelectVeiculos('vdVeiculo', true);
openModal('modalVenda');
}
function salvarVenda() {
const data = document.getElementById('vdData').value;
const veiculoId = document.getElementById('vdVeiculo').value;
const valorVenda = currencyVal('vdValor');
if (!data || !veiculoId || !valorVenda) {
toast('Preencha todos os campos obrigatórios (*)', 'error'); return;
}
const veiculos = DB.get(KEYS.veiculos);
const vi = veiculos.findIndex(x => x.id === veiculoId);
const custo = vi !== -1 ? veiculos[vi].precoCompra : 0;
const vendas = DB.get(KEYS.vendas);
const venda = {
id: DB.uid(), data, veiculoId,
comprador: document.getElementById('vdComprador').value.trim(),
valorVenda, custo,
obs: document.getElementById('vdObs').value.trim(),
criadoEm: new Date().toISOString()
};
// Marca veículo como vendido
if (vi !== -1) {
veiculos[vi].status = 'Vendido';
veiculos[vi].precoVenda = valorVenda;
DB.set(KEYS.veiculos, veiculos);
}
vendas.push(venda);
DB.set(KEYS.vendas, vendas);
toast('Venda registrada com sucesso!', 'success');
closeModal('modalVenda');
renderVendas();
}
// ===== SELECT DE VEÍCULOS =====
function preencherSelectVeiculos(selectId, apenasEstoque) {
const sel = document.getElementById(selectId);
const veiculos = DB.get(KEYS.veiculos);
const lista = apenasEstoque ? veiculos.filter(v => v.status === 'Em estoque') : veiculos;
sel.innerHTML = `<option value="">Selecione...</option>` +
lista.map(v => `<option value="${v.id}">${v.placa} — ${v.marca} ${v.modelo} (${v.anoFab})</option>`).join('');
}
// ===== DASHBOARD =====
function renderDashboard() {
const veiculos = DB.get(KEYS.veiculos);
const compras = DB.get(KEYS.compras);
const vendas = DB.get(KEYS.vendas);
const estoque = veiculos.filter(v => v.status === 'Em estoque').length;
const totalInvestido = compras.reduce((s, c) => s + c.valor, 0);
const totalRecebido = vendas.reduce((s, v) => s + v.valorVenda, 0);
const totalCusto = vendas.reduce((s, v) => s + v.custo, 0);
const lucro = totalRecebido - totalCusto;
document.getElementById('dash-estoque').textContent = estoque;
document.getElementById('dash-investido').textContent = fmt.currency(totalInvestido);
document.getElementById('dash-recebido').textContent = fmt.currency(totalRecebido);
document.getElementById('dash-lucro').textContent = fmt.currency(lucro);
// Últimas movimentações
const movs = [
...compras.map(c => ({ data: c.data, tipo: 'Compra', veiculoId: c.veiculoId, valor: c.valor, criadoEm: c.criadoEm })),
...vendas.map(v => ({ data: v.data, tipo: 'Venda', veiculoId: v.veiculoId, valor: v.valorVenda, criadoEm: v.criadoEm }))
].sort((a, b) => new Date(b.criadoEm) - new Date(a.criadoEm)).slice(0, 10);
const veics = DB.get(KEYS.veiculos);
const tbody = document.getElementById('dash-movimentacoes-body');
if (!movs.length) {
tbody.innerHTML = `<tr><td colspan="4" class="empty-row">Nenhuma movimentação</td></tr>`;
return;
}
tbody.innerHTML = movs.map(m => {
const v = veics.find(x => x.id === m.veiculoId);
const nomeV = v ? `${v.placa} — ${v.marca} ${v.modelo}` : 'Unknown';
const tipoClass = m.tipo === 'Venda' ? 'badge-success' : 'badge-blue';
return `<tr>
<td>${fmt.date(m.data)}</td>
<td><span class="badge ${tipoClass}">${m.tipo}</span></td>
<td>${nomeV}</td>
<td><strong>${fmt.currency(m.valor)}</strong></td>
</tr>`;
}).join('');
}
// ===== RELATÓRIOS =====
function renderRelatorios() {
const vendas = DB.get(KEYS.vendas);
const veiculos = DB.get(KEYS.veiculos);
const compras = DB.get(KEYS.compras);
const totalVendidos = vendas.length;
let somaMargens = 0, maiorLucro = 0;
let estoqueValor = veiculos
.filter(v => v.status === 'Em estoque')
.reduce((s, v) => s + (v.precoCompra || 0), 0);
const tbody = document.getElementById('relatoriosBody');
if (!vendas.length) {
tbody.innerHTML = `<tr><td colspan="7" class="empty-row">Nenhuma venda registrada</td></tr>`;
document.getElementById('rel-vendidos').textContent = 0;
document.getElementById('rel-margem').textContent = '0%';
document.getElementById('rel-maior-lucro').textContent = fmt.currency(0);
document.getElementById('rel-estoque-valor').textContent = fmt.currency(estoqueValor);
return;
}
let rows = '';
vendas.forEach(vd => {
const v = veiculos.find(x => x.id === vd.veiculoId);
const nomeV = v ? `${v.placa} — ${v.marca} ${v.modelo}` : 'Unknown';
const lucro = vd.valorVenda - vd.custo;
const margem = vd.custo > 0 ? (lucro / vd.custo) * 100 : 0;
somaMargens += margem;
if (lucro > maiorLucro) maiorLucro = lucro;
// Data de compra
const compra = compras.find(c => c.veiculoId === vd.veiculoId);
const dataCompra = compra ? fmt.date(compra.data) : '—';
const lucroClass = lucro >= 0 ? 'color:#166534' : 'color:#991b1b';
rows += `<tr>
<td>${dataCompra}</td>
<td>${fmt.date(vd.data)}</td>
<td>${nomeV}</td>
<td>${fmt.currency(vd.custo)}</td>
<td>${fmt.currency(vd.valorVenda)}</td>
<td style="${lucroClass}"><strong>${fmt.currency(lucro)}</strong></td>
<td>${margem.toFixed(1)}%</td>
</tr>`;
});
tbody.innerHTML = rows;
const margemMedia = totalVendidos > 0 ? (somaMargens / totalVendidos).toFixed(1) : 0;
document.getElementById('rel-vendidos').textContent = totalVendidos;
document.getElementById('rel-margem').textContent = `${margemMedia}%`;
document.getElementById('rel-maior-lucro').textContent = fmt.currency(maiorLucro);
document.getElementById('rel-estoque-valor').textContent = fmt.currency(estoqueValor);
}
// ===== EXCLUSÃO =====
let deleteCb = null;
function deletarRegistro(tipo, id) {
const msgs = {
veiculo: 'Tem certeza que deseja excluir este veículo? Isso não removerá compras ou vendas associadas.',
compra: 'Tem certeza que deseja excluir esta compra?',
venda: 'Tem certeza que deseja excluir esta venda? O status do veículo voltará para "Em estoque".'
};
document.getElementById('confirmMsg').textContent = msgs[tipo];
deleteCb = () => {
if (tipo === 'veiculo') {
const arr = DB.get(KEYS.veiculos).filter(x => x.id !== id);
DB.set(KEYS.veiculos, arr); renderVeiculos();
} else if (tipo === 'compra') {
const arr = DB.get(KEYS.compras).filter(x => x.id !== id);
DB.set(KEYS.compras, arr); renderCompras();
} else if (tipo === 'venda') {
const venda = DB.get(KEYS.vendas).find(x => x.id === id);
if (venda) {
const veics = DB.get(KEYS.veiculos);
const vi = veics.findIndex(x => x.id === venda.veiculoId);
if (vi !== -1) { veics[vi].status = 'Em estoque'; DB.set(KEYS.veiculos, veics); }
}
const arr = DB.get(KEYS.vendas).filter(x => x.id !== id);
DB.set(KEYS.vendas, arr); renderVendas();
}
toast('Registro excluído', 'success');
closeModal('modalConfirm');
};
openModal('modalConfirm');
}
// ===== INICIALIZAÇÃO =====
document.addEventListener('DOMContentLoaded', () => {
setupCurrencyInputs();
// Data no topo
const now = new Date();
document.getElementById('dateDisplay').textContent =
now.toLocaleDateString('pt-BR', { weekday: 'long', day: 'numeric', month: 'long', year: 'numeric' });
// Navegação
document.querySelectorAll('.nav-item').forEach(item => {
item.addEventListener('click', e => {
e.preventDefault();
navigate(item.dataset.page);
});
});
// Sidebar toggle
const sidebar = document.getElementById('sidebar');
const main = document.querySelector('.main-content');
document.getElementById('btnToggle').addEventListener('click', () => {
sidebar.classList.toggle('hidden');
main.classList.toggle('expanded');
});
// Fechar modais com overlay
document.querySelectorAll('.modal-overlay').forEach(overlay => {
overlay.addEventListener('click', e => {
if (e.target === overlay) overlay.classList.remove('open');
});
});
// Botões de fechar modal
document.querySelectorAll('[data-close]').forEach(btn => {
btn.addEventListener('click', () => closeModal(btn.dataset.close));
});
// Botões de ação
document.getElementById('btnNovoVeiculo').addEventListener('click', abrirModalVeiculo);
document.getElementById('btnSalvarVeiculo').addEventListener('click', salvarVeiculo);
document.getElementById('btnNovaCompra').addEventListener('click', abrirModalCompra);
document.getElementById('btnSalvarCompra').addEventListener('click', salvarCompra);
document.getElementById('btnNovaVenda').addEventListener('click', abrirModalVenda);
document.getElementById('btnSalvarVenda').addEventListener('click', salvarVenda);
document.getElementById('btnConfirmDelete').addEventListener('click', () => { if (deleteCb) deleteCb(); });
// Busca de veículos
document.getElementById('searchVeiculos').addEventListener('input', e => {
renderVeiculos(e.target.value);
});
// Calcular lucro na venda ao selecionar veículo ou digitar valor
document.getElementById('vdVeiculo').addEventListener('change', function () {
const veiculos = DB.get(KEYS.veiculos);
const v = veiculos.find(x => x.id === this.value);
if (v) {
setCurrencyVal('vdCusto', v.precoCompra);
atualizarLucroVenda();
if (v.precoVenda) setCurrencyVal('vdValor', v.precoVenda);
} else {
document.getElementById('vdCusto').value = '';
document.getElementById('vdLucro').value = '';
}
});
document.getElementById('vdValor').addEventListener('input', atualizarLucroVenda);
// Render inicial
renderDashboard();
});
function atualizarLucroVenda() {
const venda = currencyVal('vdValor');
const custo = currencyVal('vdCusto');
if (venda && custo) setCurrencyVal('vdLucro', venda - custo);
else document.getElementById('vdLucro').value = '';
}