Korjaa dokumentit-tab: lataa asiakkaat automaattisesti

- loadDocuments() lataa nyt customers-listan jos se on tyhjä
- Kansionäkymä näyttää dokumenttien ja kokousmuistioiden määrät erikseen
- Korjaa tyhjä kansionäkymä kun Dokumentit-tab avataan ennen Asiakkaat-tabia

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-12 10:13:11 +02:00
parent 86b8e42485
commit 474aa2cbce

View File

@@ -4877,6 +4877,10 @@ function showDocEditView() {
async function loadDocuments() { async function loadDocuments() {
try { 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'); allDocuments = await apiCall('documents');
// Lataa kansiot asiakaskohtaisesti // Lataa kansiot asiakaskohtaisesti
if (currentDocCustomerId) { if (currentDocCustomerId) {
@@ -4901,26 +4905,38 @@ function renderDocCustomerFolders() {
customers.forEach(c => { customerNameMap[c.id] = c.yritys; }); 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 docCountMap = {};
const meetingCountMap = {};
allDocuments.forEach(d => { allDocuments.forEach(d => {
if (d.customer_id) { if (d.customer_id) {
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; docCountMap[d.customer_id] = (docCountMap[d.customer_id] || 0) + 1;
} }
}
}); });
// Näytä kaikki asiakkaat joilla on dokumentteja TAI kaikki aktiiviset asiakkaat // Näytä kaikki asiakkaat joilla on dokumentteja TAI kaikki aktiiviset asiakkaat
let folderList = []; let folderList = [];
if (typeof customers !== 'undefined') { if (typeof customers !== 'undefined' && customers.length > 0) {
customers.forEach(c => { customers.forEach(c => {
const count = docCountMap[c.id] || 0; const docs = docCountMap[c.id] || 0;
folderList.push({ id: c.id, name: c.yritys || c.id, count }); 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 // Lisää asiakkaat jotka ovat dokumenteissa mutta eivät customers-listassa
const allCustIds = new Set(folderList.map(f => f.id));
Object.keys(docCountMap).forEach(custId => { Object.keys(docCountMap).forEach(custId => {
if (!folderList.find(f => f.id === custId)) { if (!allCustIds.has(custId)) {
folderList.push({ id: custId, name: customerNameMap[custId] || custId, count: docCountMap[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)); 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) => { 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'); return a.name.localeCompare(b.name, 'fi');
}); });
@@ -4942,13 +4960,18 @@ function renderDocCustomerFolders() {
} }
noFolders.style.display = 'none'; noFolders.style.display = 'none';
grid.innerHTML = folderList.map(f => ` grid.innerHTML = folderList.map(f => {
<div class="doc-customer-folder${f.count === 0 ? ' empty' : ''}" onclick="openDocCustomerFolder('${f.id}')"> 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 `<div class="doc-customer-folder${total === 0 ? ' empty' : ''}" onclick="openDocCustomerFolder('${f.id}')">
<div class="doc-customer-folder-icon">🏢</div> <div class="doc-customer-folder-icon">🏢</div>
<div class="doc-customer-folder-name">${esc(f.name)}</div> <div class="doc-customer-folder-name">${esc(f.name)}</div>
<div class="doc-customer-folder-count">${f.count} ${f.count === 1 ? 'dokumentti' : 'dokumenttia'}</div> <div class="doc-customer-folder-count">${desc}</div>
</div> </div>`;
`).join(''); }).join('');
} }
function openDocCustomerFolder(customerId) { function openDocCustomerFolder(customerId) {