Dokumenttikansiot asiakaskohtaisiksi

- Lisää customer_id sarake document_folders-tauluun (ALTER TABLE migraatio)
- dbLoadFolders() tukee nyt customer_id suodatusta
- dbSaveFolder() tallentaa customer_id:n kansioon
- API document_folders endpoint vastaanottaa customer_id parametrin
- JS: kansiot ladataan ja luodaan asiakaskohtaisesti (currentDocCustomerId)
- Jokaisen asiakkaan kansiorakenne on nyt itsenäinen

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-12 10:04:03 +02:00
parent 711193e1ce
commit 3fe45b217c
3 changed files with 15 additions and 8 deletions

View File

@@ -4287,7 +4287,8 @@ switch ($action) {
case 'document_folders': case 'document_folders':
requireAuth(); requireAuth();
$companyId = requireCompany(); $companyId = requireCompany();
echo json_encode(dbLoadFolders($companyId)); $customerId = $_GET['customer_id'] ?? null;
echo json_encode(dbLoadFolders($companyId, $customerId));
break; break;
case 'document_folder_save': case 'document_folder_save':

11
db.php
View File

@@ -615,6 +615,7 @@ function initDatabase(): void {
"ALTER TABLE documents ADD COLUMN max_versions INT DEFAULT 10 AFTER current_version", "ALTER TABLE documents ADD COLUMN max_versions INT DEFAULT 10 AFTER current_version",
"ALTER TABLE document_versions ADD COLUMN content MEDIUMTEXT DEFAULT NULL AFTER mime_type", "ALTER TABLE document_versions ADD COLUMN content MEDIUMTEXT DEFAULT NULL AFTER mime_type",
"ALTER TABLE devices ADD COLUMN laitetila_id VARCHAR(20) DEFAULT NULL AFTER site_id", "ALTER TABLE devices ADD COLUMN laitetila_id VARCHAR(20) DEFAULT NULL AFTER site_id",
"ALTER TABLE document_folders ADD COLUMN customer_id VARCHAR(20) DEFAULT NULL AFTER company_id",
]; ];
foreach ($alters as $sql) { foreach ($alters as $sql) {
try { $db->query($sql); } catch (\Throwable $e) { /* sarake on jo olemassa / jo ajettu */ } try { $db->query($sql); } catch (\Throwable $e) { /* sarake on jo olemassa / jo ajettu */ }
@@ -1749,7 +1750,10 @@ function dbDeleteTodoSubtask(string $subtaskId): void {
// ==================== DOKUMENTTIKANSIOT ==================== // ==================== DOKUMENTTIKANSIOT ====================
function dbLoadFolders(string $companyId): array { function dbLoadFolders(string $companyId, ?string $customerId = null): array {
if ($customerId) {
return _dbFetchAll("SELECT * FROM document_folders WHERE company_id = ? AND customer_id = ? ORDER BY name", [$companyId, $customerId]);
}
return _dbFetchAll("SELECT * FROM document_folders WHERE company_id = ? ORDER BY name", [$companyId]); return _dbFetchAll("SELECT * FROM document_folders WHERE company_id = ? ORDER BY name", [$companyId]);
} }
@@ -1757,12 +1761,13 @@ function dbSaveFolder(string $companyId, array $folder): string {
$id = $folder['id'] ?? generateId(); $id = $folder['id'] ?? generateId();
$now = date('Y-m-d H:i:s'); $now = date('Y-m-d H:i:s');
_dbExecute(" _dbExecute("
INSERT INTO document_folders (id, company_id, name, parent_id, created_by, luotu) INSERT INTO document_folders (id, company_id, customer_id, name, parent_id, created_by, luotu)
VALUES (:id, :companyId, :name, :parentId, :createdBy, :luotu) VALUES (:id, :companyId, :customerId, :name, :parentId, :createdBy, :luotu)
ON DUPLICATE KEY UPDATE name = VALUES(name), parent_id = VALUES(parent_id) ON DUPLICATE KEY UPDATE name = VALUES(name), parent_id = VALUES(parent_id)
", [ ", [
'id' => $id, 'id' => $id,
'companyId' => $companyId, 'companyId' => $companyId,
'customerId' => !empty($folder['customer_id']) ? $folder['customer_id'] : null,
'name' => $folder['name'] ?? '', 'name' => $folder['name'] ?? '',
'parentId' => !empty($folder['parent_id']) ? $folder['parent_id'] : null, 'parentId' => !empty($folder['parent_id']) ? $folder['parent_id'] : null,
'createdBy' => $folder['created_by'] ?? '', 'createdBy' => $folder['created_by'] ?? '',

View File

@@ -4877,13 +4877,13 @@ function showDocEditView() {
async function loadDocuments() { async function loadDocuments() {
try { try {
allDocuments = await apiCall('documents'); allDocuments = await apiCall('documents');
try { allDocFolders = await apiCall('document_folders'); } catch (e2) { allDocFolders = []; } // Lataa kansiot asiakaskohtaisesti
if (currentDocCustomerId) { if (currentDocCustomerId) {
// Ollaan asiakkaan kansion sisällä → näytä dokumenttilista try { allDocFolders = await apiCall('document_folders&customer_id=' + currentDocCustomerId); } catch (e2) { allDocFolders = []; }
renderDocFolderBar(); renderDocFolderBar();
renderDocumentsList(); renderDocumentsList();
} else { } else {
// Näytä asiakaskansiot allDocFolders = [];
renderDocCustomerFolders(); renderDocCustomerFolders();
} }
} catch (e) { console.error('Dokumenttien lataus epäonnistui:', e); } } catch (e) { console.error('Dokumenttien lataus epäonnistui:', e); }
@@ -5341,7 +5341,8 @@ document.getElementById('btn-new-folder')?.addEventListener('click', async () =>
try { try {
await apiCall('document_folder_save', 'POST', { await apiCall('document_folder_save', 'POST', {
name: name.trim(), name: name.trim(),
parent_id: currentDocFolderId || null parent_id: currentDocFolderId || null,
customer_id: currentDocCustomerId || null
}); });
await loadDocuments(); await loadDocuments();
} catch (e) { alert('Kansion luonti epäonnistui: ' + e.message); } } catch (e) { alert('Kansion luonti epäonnistui: ' + e.message); }