alcantarm commited on
Commit
649fbb5
·
verified ·
1 Parent(s): a791e9b

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +9 -26
script.js CHANGED
@@ -19,7 +19,6 @@
19
  const records = [];
20
  let budgetFile = null;
21
 
22
- /** UTILIDADES **/
23
  function fmtDate(iso) {
24
  const d = new Date(iso);
25
  return d.toLocaleString(undefined, { year:'numeric', month:'2-digit', day:'2-digit', hour:'2-digit', minute:'2-digit' });
@@ -42,7 +41,6 @@
42
  return isNaN(monto) ? null : monto;
43
  }
44
 
45
- /** RENDER **/
46
  function renderDocs() {
47
  total.textContent = `${docs.length} en total`;
48
  tbody.innerHTML = '';
@@ -107,7 +105,6 @@
107
  }
108
  }
109
 
110
- /** OCR (con fallback simulado) **/
111
  async function extractAmountFromFile(file) {
112
  try {
113
  const { createWorker } = Tesseract;
@@ -128,7 +125,6 @@
128
  }
129
  }
130
 
131
- /** AGREGAR ARCHIVOS **/
132
  async function addFiles(fileList) {
133
  errorEl.textContent = '';
134
  const nowIso = new Date().toISOString();
@@ -136,9 +132,9 @@
136
  if (!incoming.length) return;
137
 
138
  incoming.forEach(f => {
139
- if (f && (f.type === 'application/pdf' || f.type.startsWith('image/'))) {
140
- const amount = randomAmount(10000, 250000); // monto inmediato
141
- const doc = {
142
  name: f.name,
143
  loadedAt: nowIso,
144
  file: f,
@@ -147,7 +143,6 @@
147
  };
148
  docs.unshift(doc);
149
 
150
- // OCR en background
151
  (async () => {
152
  const realAmount = await extractAmountFromFile(f);
153
  doc.amount = realAmount;
@@ -156,7 +151,7 @@
156
  })();
157
 
158
  } else {
159
- errorEl.textContent = 'Solo se permiten archivos PDF o imágenes.';
160
  }
161
  });
162
 
@@ -164,21 +159,18 @@
164
  updateSummary();
165
  }
166
 
167
- /** PRESUPUESTO CSV **/
168
  function setBudgetFromFile(f) {
169
  if (!f) return;
170
- if (!f.name.toLowerCase().endsWith('.csv')) {
171
- budgetError.textContent = 'Solo se permite un archivo CSV.';
172
- return;
173
  }
174
  budgetError.textContent = '';
175
  const reader = new FileReader();
176
  reader.onload = (e) => {
177
  const content = e.target.result;
178
  let amount = parseCSV(content);
179
- if (amount === null) {
180
- amount = randomAmount(250000, 1000000); // simulación
181
- }
182
  budgetFile = { name: f.name, loadedAt: new Date().toISOString(), amount };
183
  renderBudget();
184
  updateSummary();
@@ -186,7 +178,6 @@
186
  reader.readAsText(f);
187
  }
188
 
189
- /** EVENTOS **/
190
  input.addEventListener('change', () => { addFiles(input.files); input.value=''; });
191
  ['dragenter','dragover'].forEach(evt=>dropzone.addEventListener(evt, e=>{ e.preventDefault(); e.stopPropagation(); dropzone.classList.add('drag'); }));
192
  ['dragleave','drop'].forEach(evt=>dropzone.addEventListener(evt, e=>{ e.preventDefault(); e.stopPropagation(); dropzone.classList.remove('drag'); }));
@@ -204,15 +195,9 @@
204
 
205
  processBtn.addEventListener('click', async () => {
206
  if (!budgetFile || !docs.length) { alert('Carga al menos un PDF/imagen y un CSV de presupuesto.'); return; }
207
-
208
- // Actualiza OCR real si quieres (opcional)
209
- for (const d of docs) {
210
- if (d.file) d.amount = await extractAmountFromFile(d.file);
211
- }
212
-
213
  renderDocs();
214
  updateSummary();
215
-
216
  const totalDocsAmount = docs.reduce((s,d)=> s+(d.amount||0),0);
217
  const diff = budgetFile.amount - totalDocsAmount;
218
  records.push({ docsCount: docs.length, docsSum: totalDocsAmount, budget: budgetFile.amount, diff });
@@ -227,9 +212,7 @@
227
  if (!Number.isNaN(idx)) { records.splice(idx,1); renderRecords(); }
228
  });
229
 
230
- /** INICIAL **/
231
  renderDocs();
232
  renderBudget();
233
  renderRecords();
234
-
235
  })();
 
19
  const records = [];
20
  let budgetFile = null;
21
 
 
22
  function fmtDate(iso) {
23
  const d = new Date(iso);
24
  return d.toLocaleString(undefined, { year:'numeric', month:'2-digit', day:'2-digit', hour:'2-digit', minute:'2-digit' });
 
41
  return isNaN(monto) ? null : monto;
42
  }
43
 
 
44
  function renderDocs() {
45
  total.textContent = `${docs.length} en total`;
46
  tbody.innerHTML = '';
 
105
  }
106
  }
107
 
 
108
  async function extractAmountFromFile(file) {
109
  try {
110
  const { createWorker } = Tesseract;
 
125
  }
126
  }
127
 
 
128
  async function addFiles(fileList) {
129
  errorEl.textContent = '';
130
  const nowIso = new Date().toISOString();
 
132
  if (!incoming.length) return;
133
 
134
  incoming.forEach(f => {
135
+ if (f && f.name) { // Acepta cualquier archivo con nombre
136
+ const amount = randomAmount(10000, 80000);
137
+ const doc = {
138
  name: f.name,
139
  loadedAt: nowIso,
140
  file: f,
 
143
  };
144
  docs.unshift(doc);
145
 
 
146
  (async () => {
147
  const realAmount = await extractAmountFromFile(f);
148
  doc.amount = realAmount;
 
151
  })();
152
 
153
  } else {
154
+ errorEl.textContent = 'Archivo no válido.';
155
  }
156
  });
157
 
 
159
  updateSummary();
160
  }
161
 
 
162
  function setBudgetFromFile(f) {
163
  if (!f) return;
164
+ if (!f.name.toLowerCase().endsWith('.csv')) {
165
+ budgetError.textContent = 'Solo se permite un archivo CSV.';
166
+ return;
167
  }
168
  budgetError.textContent = '';
169
  const reader = new FileReader();
170
  reader.onload = (e) => {
171
  const content = e.target.result;
172
  let amount = parseCSV(content);
173
+ if (amount === null) amount = randomAmount(200000, 500000);
 
 
174
  budgetFile = { name: f.name, loadedAt: new Date().toISOString(), amount };
175
  renderBudget();
176
  updateSummary();
 
178
  reader.readAsText(f);
179
  }
180
 
 
181
  input.addEventListener('change', () => { addFiles(input.files); input.value=''; });
182
  ['dragenter','dragover'].forEach(evt=>dropzone.addEventListener(evt, e=>{ e.preventDefault(); e.stopPropagation(); dropzone.classList.add('drag'); }));
183
  ['dragleave','drop'].forEach(evt=>dropzone.addEventListener(evt, e=>{ e.preventDefault(); e.stopPropagation(); dropzone.classList.remove('drag'); }));
 
195
 
196
  processBtn.addEventListener('click', async () => {
197
  if (!budgetFile || !docs.length) { alert('Carga al menos un PDF/imagen y un CSV de presupuesto.'); return; }
198
+ for (const d of docs) { if (d.file) d.amount = await extractAmountFromFile(d.file); }
 
 
 
 
 
199
  renderDocs();
200
  updateSummary();
 
201
  const totalDocsAmount = docs.reduce((s,d)=> s+(d.amount||0),0);
202
  const diff = budgetFile.amount - totalDocsAmount;
203
  records.push({ docsCount: docs.length, docsSum: totalDocsAmount, budget: budgetFile.amount, diff });
 
212
  if (!Number.isNaN(idx)) { records.splice(idx,1); renderRecords(); }
213
  });
214
 
 
215
  renderDocs();
216
  renderBudget();
217
  renderRecords();
 
218
  })();