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:
49
script.js
49
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) {
|
||||
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 => `
|
||||
<div class="doc-customer-folder${f.count === 0 ? ' empty' : ''}" onclick="openDocCustomerFolder('${f.id}')">
|
||||
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 `<div class="doc-customer-folder${total === 0 ? ' empty' : ''}" onclick="openDocCustomerFolder('${f.id}')">
|
||||
<div class="doc-customer-folder-icon">🏢</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>
|
||||
`).join('');
|
||||
<div class="doc-customer-folder-count">${desc}</div>
|
||||
</div>`;
|
||||
}).join('');
|
||||
}
|
||||
|
||||
function openDocCustomerFolder(customerId) {
|
||||
|
||||
Reference in New Issue
Block a user