-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
297 lines (272 loc) · 15.5 KB
/
Copy pathpopup.js
File metadata and controls
297 lines (272 loc) · 15.5 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
let masterStaff = {};
let currentData = [];
let historial = {};
let currentMonthData = { year: new Date().getFullYear(), month: new Date().getMonth() + 1, label: "" };
chrome.storage.local.get(['masterStaff'], (result) => {
if (result.masterStaff) {
masterStaff = result.masterStaff;
renderDB();
}
});
function saveDB() {
chrome.storage.local.set({ 'masterStaff': masterStaff }, () => {
const status = document.getElementById('status');
if (status) {
status.innerHTML = "✅ Cambios guardados";
setTimeout(() => { status.innerText = "Datos sincronizados"; }, 2000);
}
});
}
function renderDB() {
const tbody = document.getElementById('dbBody');
if (!tbody) return;
tbody.innerHTML = '';
const ids = Object.keys(masterStaff).filter(id => !id.startsWith('XXX')).sort((a,b) => masterStaff[a].nombre.localeCompare(masterStaff[b].nombre));
ids.forEach(id => {
const p = masterStaff[id];
const tr = document.createElement('tr');
const checks = ['esp_ped','exp_ped','exp_urg','form_trj'].map(k =>
`<td><input type="checkbox" ${p[k]?'checked':''} data-id="${id}" data-key="${k}"></td>`
).join('');
tr.innerHTML = `<td style="text-align:left; font-weight:600">${p.nombre}</td>${checks}
<td><select data-id="${id}" data-key="fijo"><option value="">-</option><option value="tri1" ${p.fijo==='tri1'?'selected':''}>Tr1</option><option value="tri2" ${p.fijo==='tri2'?'selected':''}>Tr2</option><option value="obs" ${p.fijo==='obs'?'selected':''}>Obs</option></select></td>
<td><select data-id="${id}" data-key="rotar"><option value="si" ${p.rotar==='si'?'selected':''}>Si</option><option value="no" ${p.rotar==='no'?'selected':''}>No</option></select></td>`;
tbody.appendChild(tr);
});
tbody.querySelectorAll('input, select').forEach(el => {
el.onchange = (e) => {
const {id, key} = e.target.dataset;
masterStaff[id][key] = e.target.type === 'checkbox' ? e.target.checked : e.target.value;
saveDB();
};
});
}
const btnExpandir = document.getElementById('btnExpandir');
if (btnExpandir) {
btnExpandir.onclick = () => chrome.tabs.create({ url: chrome.runtime.getURL('popup.html') });
}
// FIX: Usar chrome.tabs.create para evitar el bloqueo de ventanas emergentes de Chrome
document.getElementById('btnImprimir').onclick = async () => {
const label = currentMonthData.label || "Cuadrante";
const htmlDiurno = getDistribucionHTML(false);
const htmlNoche = getDistribucionHTML(true);
chrome.storage.local.set({
'print_payload': { titulo: `DIURNO - ${label}`, html: htmlDiurno },
'print_payload_noche': { titulo: `NOCHE - ${label}`, html: htmlNoche }
}, () => {
// Abrir ambas pestañas de forma segura
chrome.tabs.create({ url: chrome.runtime.getURL('print.html?type=payload'), active: false });
chrome.tabs.create({ url: chrome.runtime.getURL('print.html?type=payload_noche'), active: true });
});
};
document.getElementById('btnSinc').onclick = () => {
const btn = document.getElementById('btnSinc');
const originalText = btn.innerText;
btn.innerText = "⏳ Extrayendo...";
btn.disabled = true;
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
if (!tabs[0]) return;
chrome.tabs.sendMessage(tabs[0].id, {action: "get_data"}, (response) => {
btn.innerText = originalText;
btn.disabled = false;
if (chrome.runtime.lastError) {
alert("Error: Refresca la página de aTurnos antes de importar.");
return;
}
if (response && response.exito) {
currentData = response.personal;
const title = response.mes || "";
const match = title.match(/(enero|febrero|marzo|abril|mayo|junio|julio|agosto|septiembre|octubre|noviembre|diciembre)\s+(20\d{2})/i);
if (match) {
const meses = ["enero","febrero","marzo","abril","mayo","junio","julio","agosto","septiembre","octubre","noviembre","diciembre"];
currentMonthData.month = meses.indexOf(match[1].toLowerCase()) + 1;
currentMonthData.year = parseInt(match[2]);
currentMonthData.label = `${match[1].toUpperCase()} ${match[2]}`;
} else { currentMonthData.label = title || "Mes Detectado"; }
document.getElementById('mesLabel').innerText = currentMonthData.label;
currentData.forEach(p => {
if (!masterStaff[p.nombre]) {
masterStaff[p.nombre] = { nombre: p.nombre, esp_ped: false, exp_ped: false, exp_urg: false, form_trj: false, fijo: '', rotar: 'si' };
}
});
renderDB(); saveDB();
alert("¡Éxito! " + currentData.length + " enfermeros/as listos.");
} else { alert("Error: " + (response?.error || "Asegúrate de estar en el cuadrante mensual de aTurnos.")); }
});
});
};
// Navegación
document.getElementById('step1').onclick = () => {
document.getElementById('page-staff').classList.add('active');
document.getElementById('page-gen').classList.remove('active');
document.getElementById('step1').classList.add('active');
document.getElementById('step2').classList.remove('active');
};
document.getElementById('step2').onclick = () => {
document.getElementById('page-staff').classList.remove('active');
document.getElementById('page-gen').classList.add('active');
document.getElementById('step1').classList.remove('active');
document.getElementById('step2').classList.add('active');
};
document.getElementById('btnGenerar').onclick = () => {
if (currentData.length === 0) { alert("Primero importa los datos."); return; }
document.getElementById('resultZone').style.display = 'block';
document.getElementById('tablaDiv').innerHTML = getDistribucionHTML(document.getElementById('turnoSel').value === "NOCHE");
};
document.getElementById('turnoSel').onchange = () => {
document.getElementById('tablaDiv').innerHTML = getDistribucionHTML(document.getElementById('turnoSel').value === "NOCHE");
};
function getCandidate(list, post, exclude = null) {
if (!list || list.length === 0) return null;
let pool = exclude ? list.filter(x => Array.isArray(exclude) ? !exclude.includes(x.id) : x.id !== exclude) : list;
if (pool.length === 0) pool = list;
pool.sort((a, b) => {
const cA = masterStaff[a.id] || {}, cB = masterStaff[b.id] || {};
if (cA.fijo === post && cB.fijo !== post) return -1;
if (cB.fijo === post && cA.fijo !== post) return 1;
const score = (p, pto) => {
if (pto.includes('tri1')) return (p.exp_urg?10:0) + (p.form_trj?5:0);
if (pto.includes('tri2') || pto.includes('val2') || pto.includes('v2')) return (p.esp_ped?10:0) + (p.exp_ped?5:0);
return 0;
};
const sA = score(cA, post), sB = score(cB, post);
if (sA !== sB) return sB - sA;
return (historial[a.id]?.[post] || 0) - (historial[b.id]?.[post] || 0);
});
const el = pool[0];
if(!historial[el.id]) historial[el.id] = {}; historial[el.id][post] = (historial[el.id][post] || 0) + 1;
return el;
}
function shortName(nombre) {
const parts = nombre.split(',');
if (parts.length >= 2) {
// "APELLIDO APELLIDO2, Nombre" → "APELLIDO, Nombre"
const apellido = parts[0].trim().split(/\s+/)[0];
const nom = parts[1].trim().split(/\s+/)[0];
return `${apellido}, ${nom}`;
}
// "Nombre APELLIDO1 APELLIDO2" → "Nombre APELLIDO1"
const words = nombre.trim().split(/\s+/);
return words.slice(0, 2).join(' ');
}
function renderP(p, post) {
if(!p) return '-';
const c = masterStaff[p.id] || {};
let warn = "";
if (post === 'tri1' && (!c.exp_urg || !c.form_trj)) warn = '<span class="alert-skill">⚠️ !Skill</span>';
if ((post === 'tri2' || post === 'val2' || post === 'v2') && (!c.esp_ped && !c.exp_ped)) warn = '<span class="alert-skill">⚠️ !Esp</span>';
return `<div><b style="${warn?'color:var(--danger)':''}">${shortName(p.nombre)}</b>${warn}</div>`;
}
function getDistribucionHTML(modoNoche) {
historial = {};
const year = currentMonthData.year;
const month = currentMonthData.month;
const turnoLabel = modoNoche ? 'TURNO NOCHE' : 'MAÑANA / TARDE';
let html = `<div style="text-align:center;font-weight:bold;font-size:0.85rem;margin-bottom:6px;letter-spacing:0.05em">${currentMonthData.label} — ${turnoLabel}</div><table class="res-table"><thead><tr><th class="dia-col">D</th><th class="num-col">N</th><th>T1</th><th>V1</th><th>V2</th><th>OBS</th>${!modoNoche?'<th>T2</th>':''}<th>ACTIVIDADES</th></tr></thead><tbody>`;
for (let d = 0; d < 31; d++) {
const date = new Date(year, month - 1, d + 1);
if (date.getMonth() !== month - 1) break;
const dayOfWeek = date.getDay();
let working = currentData.filter(p => {
if (p.nombre.startsWith('XXX')) return false;
let t = (p.turnos[d] || "").toUpperCase();
const deBaja = t.includes('(-') || t.includes('BC') || t.includes('AL') || t.includes('PIH') || t.includes('PFA') || t.includes('BR');
if (deBaja || t === 'V' || t === 'L' || !t || t.includes('DESCANSO')) return false;
const isN = t.includes('N');
return modoNoche ? isN : !isN;
}).map(p => ({ id: p.nombre, nombre: masterStaff[p.nombre]?.nombre || p.nombre, turnoCode: (p.turnos[d]||"").toUpperCase() }));
if (working.length === 0) continue;
let asM = { tri1: null, tri2: null, v1: [], v2: [], obs: [] };
let pM = [...working];
asM.tri1 = getCandidate(pM, 'tri1'); if(asM.tri1) pM = pM.filter(x => x.id !== asM.tri1.id);
if(!modoNoche) { asM.tri2 = getCandidate(pM, 'tri2'); if(asM.tri2) pM = pM.filter(x => x.id !== asM.tri2.id); }
const pasos = modoNoche
? [{k:'v1', p:'v1'}, {k:'v2', p:'v2'}, {k:'obs', p:'obs'}, {k:'obs', p:'obs'}, {k:'v1', p:'v1'}]
: [{k:'v1', p:'v1'}, {k:'v2', p:'v2'}, {k:'obs', p:'obs'}, {k:'v1', p:'v1'}, {k:'v2', p:'v2'}, {k:'obs', p:'obs'}];
pasos.forEach(s => {
let c = getCandidate(pM, s.p);
if(c) { asM[s.k].push(c); pM = pM.filter(x => x.id !== c.id); }
});
let dIdx = 0, dests = modoNoche ? ['v1', 'obs', 'v2'] : ['v1', 'v2', 'obs'];
while(pM.length > 0) { let e = pM.shift(); asM[dests[dIdx % dests.length]].push(e); dIdx++; }
// Enforce D/D1 rule: if a multi-person puesto has all D (10-22h), swap one for D1 (8-20h)
const enforceD1 = (arr, pool) => {
if (!Array.isArray(arr) || arr.length < 2) return;
if (arr.every(x => x.turnoCode === 'D')) {
const d1Idx = pool.findIndex(x => x.turnoCode === 'D1');
if (d1Idx === -1) return;
const d1 = pool.splice(d1Idx, 1)[0];
const replaced = arr.pop();
arr.push(d1);
pool.push(replaced);
}
};
enforceD1(asM.v1, pM); enforceD1(asM.v2, pM); enforceD1(asM.obs, pM);
let asT = null;
if (!modoNoche) {
let pTardeTotal = working.filter(p => !p.turnoCode.startsWith('MM') && !p.turnoCode.startsWith('M'));
asT = { tri1: null, tri2: null, v1: [], v2: [], obs: [] };
let pPoolT = [...pTardeTotal];
const fix = (k, p) => {
if(p && pTardeTotal.some(x => x.id === p.id) && masterStaff[p.id]?.rotar === 'no') {
if(Array.isArray(asT[k])) asT[k].push(p); else asT[k] = p; pPoolT = pPoolT.filter(x => x.id !== p.id);
}
};
fix('tri1', asM.tri1); fix('tri2', asM.tri2);
asM.v1.forEach(p => fix('v1', p)); asM.v2.forEach(p => fix('v2', p)); asM.obs.forEach(p => fix('obs', p));
const fill = (k, ex, c=1) => {
while((Array.isArray(asT[k])?asT[k].length:(asT[k]?1:0)) < c) {
let cnd = getCandidate(pPoolT, k, ex); if(!cnd) break;
if(Array.isArray(asT[k])) asT[k].push(cnd); else asT[k] = cnd; pPoolT = pPoolT.filter(x => x.id !== cnd.id);
}
};
fill('tri1', [asM.tri1?.id, asM.tri2?.id].filter(x=>x), 1); fill('tri2', [asM.tri1?.id, asM.tri2?.id].filter(x=>x), 1);
fill('obs', null, 2); fill('v1', null, 2); fill('v2', null, 2);
let dIdxT = 0; while(pPoolT.length > 0) { let e = pPoolT.shift(); asT[dests[dIdxT%3]].push(e); dIdxT++; }
}
let tasks = [];
if(modoNoche) {
if(dayOfWeek === 0) tasks.push("📦 <b>Pedido Farmacia</b>");
if(dayOfWeek === 3) tasks.push("🩺 <b>Ok Desf/Carro</b>");
if(d+1 === (new Date(year, month, 0).getDate() - (month%3))) tasks.push("📆 <b>Caducidad</b>");
} else {
if(dayOfWeek === 1) tasks.push("💊 <b>Colocar Farmacia</b>");
if(dayOfWeek === 4) tasks.push("📦 <b>Pedido Farmacia (M)</b><br>💊 <b>Colocar Farm. (T)</b>");
if(dayOfWeek === 5) {
let pCarro = [...working]; let r = []; let seed = d + month + year;
for(let i=0; i<2; i++) { if(pCarro.length>0) { let idx = (seed + i) % pCarro.length; r.push(shortName(pCarro[idx].nombre)); pCarro.splice(idx,1); } }
tasks.push(`🚑 <b>Carro Paradas:</b><br><small style="color:#2563eb">${r.join(' / ')}</small>`);
}
}
const gc = v => Array.isArray(v) ? v.length : (v ? 1 : 0);
const short = (assigned, exp) => gc(assigned) < exp;
const shortM = { tri1: short(asM.tri1,1), v1: short(asM.v1,1), v2: short(asM.v2,1), obs: short(asM.obs,2), tri2: short(asM.tri2,1) };
const shortT = asT ? { tri1: short(asT.tri1,1), v1: short(asT.v1,1), v2: short(asT.v2,1), obs: short(asT.obs,2), tri2: short(asT.tri2,1) } : {};
const anyShort = Object.values(shortM).some(Boolean) || Object.values(shortT).some(Boolean);
const cellTd = (m, t, post, expCount) => {
const mShort = short(m, expCount);
const tShort = t !== undefined && short(t, expCount);
const cls = (mShort || tShort) ? ' class="shortage-cell"' : '';
const mH = Array.isArray(m) ? m.map(x => renderP(x, post)).join('') : renderP(m, post);
if (t === undefined) return `<td${cls}><div class="morning-content">${mH}</div></td>`;
const tH = Array.isArray(t) ? t.map(x => renderP(x, post)).join('') : renderP(t, post);
return `<td${cls}><div class="morning-content">${mH}</div><div class="rotation-box"><span class="time-mark">15:00h ➔</span>${tH}</div></td>`;
};
let row = `<tr><td class="dia-col">${d+1}</td><td class="num-col${anyShort?' shortage-num':''}">${working.length}</td>`;
if (modoNoche) {
row += cellTd(asM.tri1, undefined, 'tri1', 1);
row += cellTd(asM.v1, undefined, 'v1', 1);
row += cellTd(asM.v2, undefined, 'v2', 1);
row += cellTd(asM.obs, undefined, 'obs', 2);
} else {
row += cellTd(asM.tri1, asT.tri1, 'tri1', 1);
row += cellTd(asM.v1, asT.v1, 'v1', 1);
row += cellTd(asM.v2, asT.v2, 'v2', 1);
row += cellTd(asM.obs, asT.obs, 'obs', 2);
row += cellTd(asM.tri2, asT.tri2, 'tri2', 1);
}
row += `<td>${tasks.length ? `<div class="task-box">${tasks.join('')}</div>` : ''}</td></tr>`;
html += row;
}
return html + '</tbody></table>';
}