Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
333 lines (284 loc) · 13.4 KB
/
Copy pathscript.js
File metadata and controls
333 lines (284 loc) · 13.4 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
const APP_VERSION = "2.5.0";
let allRecords = [];
let bancoData = [];
let cartoesData = [];
let pieChart, barChart;
// States
let viewLevel = 'macro';
let drilledCard = null;
let installmentPreview = [];
const COLORS = ['#3498db', '#2ecc71', '#f1c40f', '#9b59b6', '#1abc9c', '#f39c12', '#d35400', '#16a085', '#27ae60', '#2980b9', '#8e44ad', '#2c3e50'];
const RED_COLOR = '#e74c3c';
function logDebug(message, data = null) {
const logEl = document.getElementById('debugLog');
if (!logEl) return;
const timestamp = new Date().toLocaleTimeString();
let text = `[${timestamp}] ${message}`;
if (data) try { text += `\n${JSON.stringify(data, null, 2)}`; } catch (e) {}
const div = document.createElement('div');
div.style.borderBottom = "1px solid #34495e";
div.style.padding = "5px 0";
div.textContent = text;
logEl.prepend(div);
}
function switchTab(tabId) {
document.querySelectorAll('.tab-content').forEach(el => el.classList.remove('active'));
document.querySelectorAll('.tab-btn').forEach(el => el.classList.remove('active'));
document.getElementById(`tab-${tabId}`).classList.add('active');
document.querySelector(`button[onclick="switchTab('${tabId}')"]`).classList.add('active');
if (tabId === 'dashboard') updateDashboard();
if (tabId === 'history') updateHistory();
}
function colDataToRows(colData) {
if (!colData || !colData.id) return [];
const rows = [];
const keys = Object.keys(colData);
for (let i = 0; i < colData.id.length; i++) {
const r = { id: colData.id[i] };
keys.forEach(k => { if (k !== 'id') r[k] = colData[k][i]; });
rows.push(r);
}
return rows;
}
function getValue(obj, target) {
if (!obj) return undefined;
const data = obj.fields || obj;
const directKey = target.replace(/ /g, '_');
if (data[directKey] !== undefined) return data[directKey];
const normalize = (s) => s.normalize("NFD").replace(/[\u0300-\u036f]/g, "").replace(/[\s_]/g, "").toLowerCase();
const normalizedTarget = normalize(target);
for (let key in data) { if (normalize(key) === normalizedTarget) return data[key]; }
return undefined;
}
function getCardName(cardRef) {
if (!cardRef) return 'Sem Cartão';
// Se for [ID, Label] (vindo do Grist)
if (Array.isArray(cardRef) && cardRef.length > 1) return cardRef[1];
// Se for um objeto de linha já carregado (fix do Bug [object Object])
if (typeof cardRef === 'object' && !Array.isArray(cardRef)) {
const name = getValue(cardRef, 'Nome_Cartao') || getValue(cardRef, 'NumCartao');
if (name) return name;
}
const id = Array.isArray(cardRef) ? cardRef[0] : (typeof cardRef === 'number' ? cardRef : null);
if (id && cartoesData.length > 0) {
const card = cartoesData.find(c => c.id === id);
if (card) return getValue(card, 'Nome_Cartao') || getValue(card, 'NumCartao') || `ID: ${id}`;
}
return cardRef.label || String(cardRef);
}
function getMonthDiff(startDate, targetDate) {
return (targetDate.getFullYear() - startDate.getFullYear()) * 12 + (targetDate.getMonth() - startDate.getMonth());
}
/**
* DASHBOARD
*/
async function updateDashboard() {
const selectedCard = document.getElementById('cardFilter').value;
const globalLimit = bancoData.reduce((sum, row) => sum + (getValue(row, 'Limite') || 0), 0);
const next6Months = getNextMonths(6);
const today = new Date();
const occupancyByCard = {};
const projectionByCard = {};
const detailItems = [];
let totalOccupied = 0;
const uniqueCardNames = [...new Set(allRecords.map(r => getCardName(r.Cartao)))].sort();
const cardColorMap = {};
uniqueCardNames.forEach((name, i) => cardColorMap[name] = COLORS[i % COLORS.length]);
allRecords.forEach(r => {
const cardName = getCardName(r.Cartao);
if (selectedCard !== 'all' && cardName !== selectedCard) return;
const purchaseDate = new Date(getValue(r, 'Data') || today);
const valParcela = getValue(r, 'Valor_Parcela') || 0;
const totalParc = getValue(r, 'Total_Parcelas') || 1;
const desc = getValue(r, 'Descritivo') || 'Sem Descrição';
const diffDesdeCompra = getMonthDiff(purchaseDate, today);
const parcJaPagas = Math.max(0, diffDesdeCompra);
const parcRestantes = Math.max(0, totalParc - parcJaPagas);
const saldoDevedor = valParcela * parcRestantes;
if (saldoDevedor > 0) {
occupancyByCard[cardName] = (occupancyByCard[cardName] || 0) + saldoDevedor;
totalOccupied += saldoDevedor;
if (viewLevel === 'detail' && cardName === drilledCard) detailItems.push({ label: desc, value: saldoDevedor });
}
if (!projectionByCard[cardName]) projectionByCard[cardName] = Array(6).fill(0);
for (let i = 0; i < 6; i++) {
const diff = getMonthDiff(purchaseDate, next6Months[i].date);
if (diff >= 0 && diff < totalParc) projectionByCard[cardName][i] += valParcela;
}
});
let pieLabels = [], pieData = [], pieColors = [];
if (viewLevel === 'macro') {
document.getElementById('backButtonContainer').style.display = 'none';
pieLabels = Object.keys(occupancyByCard);
pieData = Object.values(occupancyByCard);
pieColors = pieLabels.map(name => cardColorMap[name]);
if (globalLimit > 0) {
pieLabels.push('Limite Disponível');
pieData.push(Math.max(0, globalLimit - totalOccupied));
pieColors.push(RED_COLOR);
}
} else {
document.getElementById('backButtonContainer').style.display = 'block';
detailItems.sort((a, b) => b.value - a.value);
const top10 = detailItems.slice(0, 10);
pieLabels = top10.map(i => i.label);
pieData = top10.map(i => i.value);
const baseColor = cardColorMap[drilledCard];
pieColors = top10.map((_, i) => `${baseColor}${Math.floor((1 - i*0.08) * 255).toString(16).padStart(2, '0')}`);
}
pieChart.data.labels = pieLabels;
pieChart.data.datasets[0].data = pieData;
pieChart.data.datasets[0].backgroundColor = pieColors;
pieChart.update();
barChart.data.labels = next6Months.map(m => m.name);
barChart.data.datasets = Object.keys(projectionByCard).sort().map(name => ({
label: name,
data: projectionByCard[name],
backgroundColor: cardColorMap[name]
}));
barChart.update();
}
/**
* ENTRY
*/
function generatePreview() {
const cardId = parseInt(document.getElementById('inCard').value);
const dateStr = document.getElementById('inDate').value;
const baseDesc = document.getElementById('inDesc').value;
const totalVal = parseFloat(document.getElementById('inTotal').value);
const installments = parseInt(document.getElementById('inInstallments').value);
if (!cardId || !dateStr || !baseDesc || isNaN(totalVal) || isNaN(installments)) {
alert("Preencha todos os campos corretamente."); return;
}
const valorParcela = parseFloat((totalVal / installments).toFixed(2));
const startDate = new Date(dateStr + "T00:00:00");
installmentPreview = [];
const tbody = document.querySelector('#previewTable tbody');
tbody.innerHTML = '';
for (let i = 1; i <= installments; i++) {
const currentDate = new Date(startDate);
currentDate.setMonth(startDate.getMonth() + (i - 1));
const finalDesc = `${baseDesc} ${String(i).padStart(2, '0')} ${String(installments).padStart(2, '0')}`;
const dateIso = currentDate.toISOString().split('T')[0];
installmentPreview.push({ Cartao: cardId, Data: dateIso, Descritivo: finalDesc, Valor_Parcela: valorParcela, Total_Parcelas: installments });
const tr = document.createElement('tr');
tr.innerHTML = `<td>${currentDate.toLocaleDateString('pt-BR')}</td><td>${finalDesc}</td><td class="${valorParcela < 0 ? 'negative-value' : ''}">${formatCurrency(valorParcela)}</td>`;
tbody.appendChild(tr);
}
document.getElementById('previewSection').style.display = 'block';
}
async function saveToGrist() {
try {
await grist.docApi.addRecords('Lancamentos', installmentPreview.map(p => ({ fields: p })));
alert("Lançamento salvo com sucesso!");
document.getElementById('tab-entry').querySelectorAll('input').forEach(i => i.value = '');
document.getElementById('previewSection').style.display = 'none';
switchTab('dashboard');
} catch (e) { alert("Erro ao salvar: " + e.message); }
}
/**
* HISTORY
*/
function updateHistory() {
const tbody = document.querySelector('#historyTable tbody');
tbody.innerHTML = '';
// Mostra os últimos 20 lançamentos ordenados por ID (mais recentes primeiro)
const sorted = [...allRecords].sort((a,b) => b.id - a.id).slice(0, 30);
sorted.forEach(r => {
const val = getValue(r, 'Valor_Parcela') || 0;
const tr = document.createElement('tr');
tr.innerHTML = `
<td>${new Date(getValue(r, 'Data')).toLocaleDateString('pt-BR')}</td>
<td>${getCardName(getValue(r, 'Cartao'))}</td>
<td>${getValue(r, 'Descritivo')}</td>
<td class="${val < 0 ? 'negative-value' : ''}">${formatCurrency(val)}</td>
<td><button class="btn-danger" onclick="deleteEntry(${r.id})">Excluir</button></td>
`;
tbody.appendChild(tr);
});
}
async function deleteEntry(recordId) {
if (confirm("Deseja realmente excluir este lançamento? Esta ação não pode ser desfeita.")) {
try {
await grist.docApi.deleteRecords('Lancamentos', [recordId]);
logDebug(`Registro ${recordId} excluído.`);
updateHistory();
} catch (e) { alert("Erro ao excluir: " + e.message); }
}
}
/**
* UTILS
*/
function getNextMonths(count) {
const months = []; const now = new Date();
for (let i = 0; i < count; i++) {
const d = new Date(now.getFullYear(), now.getMonth() + i, 1);
months.push({ name: d.toLocaleString('pt-BR', { month: 'long' }), date: d });
}
return months;
}
function initCharts() {
if (pieChart) pieChart.destroy();
if (barChart) barChart.destroy();
pieChart = new Chart(document.getElementById('pieChart').getContext('2d'), {
type: 'pie',
data: { labels: [], datasets: [{ data: [], backgroundColor: [] }] },
options: {
responsive: true,
onClick: (evt, elements) => {
if (viewLevel === 'macro' && elements.length > 0) {
const label = pieChart.data.labels[elements[0].index];
if (label !== 'Limite Disponível') { viewLevel = 'detail'; drilledCard = label; updateDashboard(); }
}
}
}
});
barChart = new Chart(document.getElementById('barChart').getContext('2d'), {
type: 'bar',
data: { labels: [], datasets: [] },
options: { responsive: true, scales: { x: { stacked: true }, y: { stacked: true, beginAtZero: true, ticks: { callback: (v) => formatCurrency(v) } } } }
});
}
function formatCurrency(v) { return new Intl.NumberFormat('pt-BR', { style: 'currency', currency: 'BRL' }).format(v); }
grist.ready({ requiredAccess: 'full' });
grist.onRecords(async (records) => {
try {
logDebug(`Iniciando v${APP_VERSION}`);
bancoData = colDataToRows(await grist.docApi.fetchTable('Banco'));
cartoesData = colDataToRows(await grist.docApi.fetchTable('Cartoes'));
allRecords = records.map(r => ({ ...r }));
if (!pieChart || !barChart) initCharts();
// Popula Dropdowns (Correção de Bug)
const inCardSelect = document.getElementById('inCard');
const cardFilter = document.getElementById('cardFilter');
if (cardFilter.options.length <= 1) {
// Usa os dados de cartoesData diretamente para o nome
const cards = cartoesData.map(c => ({ id: c.id, name: getCardName(c) })).sort((a,b) => a.name.localeCompare(b.name));
inCardSelect.innerHTML = '<option value="">Selecione um cartão...</option>';
cardFilter.innerHTML = '<option value="all">Todos os Cartões</option>';
cards.forEach(c => {
const opt1 = document.createElement('option');
opt1.value = c.id; opt1.textContent = c.name;
inCardSelect.appendChild(opt1);
const opt2 = document.createElement('option');
opt2.value = c.name; opt2.textContent = c.name;
cardFilter.appendChild(opt2);
});
}
if (!document.getElementById('inDate').value) {
document.getElementById('inDate').value = new Date().toISOString().split('T')[0];
}
updateDashboard();
if (document.getElementById('tab-history').classList.contains('active')) updateHistory();
} catch (e) { logDebug(`ERRO GERAL: ${e.message}`); }
});
document.getElementById('btnBackToMacro').onclick = () => { viewLevel = 'macro'; drilledCard = null; updateDashboard(); };
document.getElementById('btnPreview').onclick = generatePreview;
document.getElementById('btnSave').onclick = saveToGrist;
document.getElementById('appVersion').textContent = APP_VERSION;
document.getElementById('clearLog').onclick = () => document.getElementById('debugLog').innerHTML = '';
document.getElementById('copyLog').onclick = () => {
navigator.clipboard.writeText(document.getElementById('debugLog').innerText).then(() => alert('Log copiado!'));
};
window.switchTab = switchTab;
window.deleteEntry = deleteEntry;