diff --git a/script.js b/script.js index 6f49d6e..f82b633 100644 --- a/script.js +++ b/script.js @@ -4877,6 +4877,10 @@ function showDocEditView() { async function loadDocuments() { try { + // Varmista että asiakkaat on ladattu (tarvitaan kansionäkymässä) + if (!customers || customers.length === 0) { + try { customers = await apiCall('customers'); } catch (e2) {} + } allDocuments = await apiCall('documents'); // Lataa kansiot asiakaskohtaisesti if (currentDocCustomerId) { @@ -4901,26 +4905,38 @@ function renderDocCustomerFolders() { customers.forEach(c => { customerNameMap[c.id] = c.yritys; }); } - // Laske dokumenttien määrä per asiakas + // Laske dokumenttien ja kokousmuistioiden määrä per asiakas const docCountMap = {}; + const meetingCountMap = {}; allDocuments.forEach(d => { if (d.customer_id) { - docCountMap[d.customer_id] = (docCountMap[d.customer_id] || 0) + 1; + if (d.category === 'kokousmuistio') { + meetingCountMap[d.customer_id] = (meetingCountMap[d.customer_id] || 0) + 1; + } else { + docCountMap[d.customer_id] = (docCountMap[d.customer_id] || 0) + 1; + } } }); // Näytä kaikki asiakkaat joilla on dokumentteja TAI kaikki aktiiviset asiakkaat let folderList = []; - if (typeof customers !== 'undefined') { + if (typeof customers !== 'undefined' && customers.length > 0) { customers.forEach(c => { - const count = docCountMap[c.id] || 0; - folderList.push({ id: c.id, name: c.yritys || c.id, count }); + const docs = docCountMap[c.id] || 0; + const meetings = meetingCountMap[c.id] || 0; + folderList.push({ id: c.id, name: c.yritys || c.id, count: docs, meetings }); }); } // Lisää asiakkaat jotka ovat dokumenteissa mutta eivät customers-listassa + const allCustIds = new Set(folderList.map(f => f.id)); Object.keys(docCountMap).forEach(custId => { - if (!folderList.find(f => f.id === custId)) { - folderList.push({ id: custId, name: customerNameMap[custId] || custId, count: docCountMap[custId] }); + if (!allCustIds.has(custId)) { + folderList.push({ id: custId, name: customerNameMap[custId] || custId, count: docCountMap[custId], meetings: meetingCountMap[custId] || 0 }); + } + }); + Object.keys(meetingCountMap).forEach(custId => { + if (!allCustIds.has(custId) && !docCountMap[custId]) { + folderList.push({ id: custId, name: customerNameMap[custId] || custId, count: 0, meetings: meetingCountMap[custId] }); } }); @@ -4929,9 +4945,11 @@ function renderDocCustomerFolders() { folderList = folderList.filter(f => f.name.toLowerCase().includes(search)); } - // Järjestä: ensin ne joilla on dokumentteja (count desc), sitten aakkosjärjestys + // Järjestä: ensin ne joilla on sisältöä (count+meetings desc), sitten aakkosjärjestys folderList.sort((a, b) => { - if (b.count !== a.count) return b.count - a.count; + const totalA = (a.count || 0) + (a.meetings || 0); + const totalB = (b.count || 0) + (b.meetings || 0); + if (totalB !== totalA) return totalB - totalA; return a.name.localeCompare(b.name, 'fi'); }); @@ -4942,13 +4960,18 @@ function renderDocCustomerFolders() { } noFolders.style.display = 'none'; - grid.innerHTML = folderList.map(f => ` -
+ grid.innerHTML = folderList.map(f => { + const total = (f.count || 0) + (f.meetings || 0); + const parts = []; + if (f.count > 0) parts.push(`${f.count} dok.`); + if (f.meetings > 0) parts.push(`${f.meetings} kok.`); + const desc = parts.length > 0 ? parts.join(', ') : 'Tyhjä'; + return `
🏢
${esc(f.name)}
-
${f.count} ${f.count === 1 ? 'dokumentti' : 'dokumenttia'}
-
- `).join(''); +
${desc}
+
`; + }).join(''); } function openDocCustomerFolder(customerId) {