diff --git a/api.php b/api.php index d00dfe7..0bf5a56 100644 --- a/api.php +++ b/api.php @@ -1198,6 +1198,7 @@ switch ($action) { 'elaskuvalittaja' => trim($input['elaskuvalittaja'] ?? ''), 'ytunnus' => trim($input['ytunnus'] ?? ''), 'lisatiedot' => trim($input['lisatiedot'] ?? ''), + 'priority_emails' => trim($input['priority_emails'] ?? ''), 'liittymat' => parseLiittymat($input), 'luotu' => date('Y-m-d H:i:s'), ]; @@ -1223,7 +1224,7 @@ switch ($action) { foreach ($customers as $c) { if ($c['id'] === $id) { $changes = []; - $fields = ['yritys','yhteyshenkilö','puhelin','sahkoposti','laskutusosoite','laskutuspostinumero','laskutuskaupunki','laskutussahkoposti','elaskuosoite','elaskuvalittaja','ytunnus','lisatiedot']; + $fields = ['yritys','yhteyshenkilö','puhelin','sahkoposti','laskutusosoite','laskutuspostinumero','laskutuskaupunki','laskutussahkoposti','elaskuosoite','elaskuvalittaja','ytunnus','lisatiedot','priority_emails']; foreach ($fields as $f) { if (isset($input[$f])) { $old = $c[$f] ?? ''; diff --git a/db.php b/db.php index c6b6f8d..359e7f9 100644 --- a/db.php +++ b/db.php @@ -194,6 +194,7 @@ function initDatabase(): void { elaskuvalittaja VARCHAR(100), ytunnus VARCHAR(20), lisatiedot TEXT, + priority_emails TEXT DEFAULT '', luotu DATETIME, muokattu DATETIME NULL, muokkaaja VARCHAR(100) DEFAULT '', @@ -373,6 +374,7 @@ function initDatabase(): void { $alters = [ "ALTER TABLE tickets ADD COLUMN cc TEXT DEFAULT '' AFTER mailbox_id", "ALTER TABLE tickets ADD COLUMN priority VARCHAR(20) DEFAULT 'normaali' AFTER cc", + "ALTER TABLE customers ADD COLUMN priority_emails TEXT DEFAULT '' AFTER lisatiedot", ]; foreach ($alters as $sql) { try { $db->query($sql); } catch (\Throwable $e) { /* sarake on jo olemassa */ } @@ -661,10 +663,10 @@ function dbSaveCustomer(string $companyId, array $customer): void { _dbExecute(" INSERT INTO customers (id, company_id, yritys, yhteyshenkilö, puhelin, sahkoposti, laskutusosoite, laskutuspostinumero, laskutuskaupunki, laskutussahkoposti, - elaskuosoite, elaskuvalittaja, ytunnus, lisatiedot, luotu, muokattu, muokkaaja) + elaskuosoite, elaskuvalittaja, ytunnus, lisatiedot, priority_emails, luotu, muokattu, muokkaaja) VALUES (:id, :company_id, :yritys, :yhteyshenkilö, :puhelin, :sahkoposti, :laskutusosoite, :laskutuspostinumero, :laskutuskaupunki, :laskutussahkoposti, - :elaskuosoite, :elaskuvalittaja, :ytunnus, :lisatiedot, :luotu, :muokattu, :muokkaaja) + :elaskuosoite, :elaskuvalittaja, :ytunnus, :lisatiedot, :priority_emails, :luotu, :muokattu, :muokkaaja) ON DUPLICATE KEY UPDATE yritys = VALUES(yritys), yhteyshenkilö = VALUES(yhteyshenkilö), puhelin = VALUES(puhelin), sahkoposti = VALUES(sahkoposti), @@ -672,6 +674,7 @@ function dbSaveCustomer(string $companyId, array $customer): void { laskutuskaupunki = VALUES(laskutuskaupunki), laskutussahkoposti = VALUES(laskutussahkoposti), elaskuosoite = VALUES(elaskuosoite), elaskuvalittaja = VALUES(elaskuvalittaja), ytunnus = VALUES(ytunnus), lisatiedot = VALUES(lisatiedot), + priority_emails = VALUES(priority_emails), muokattu = VALUES(muokattu), muokkaaja = VALUES(muokkaaja) ", [ 'id' => $customer['id'], @@ -688,6 +691,7 @@ function dbSaveCustomer(string $companyId, array $customer): void { 'elaskuvalittaja' => $customer['elaskuvalittaja'] ?? '', 'ytunnus' => $customer['ytunnus'] ?? '', 'lisatiedot' => $customer['lisatiedot'] ?? '', + 'priority_emails' => $customer['priority_emails'] ?? '', 'luotu' => $customer['luotu'] ?? date('Y-m-d H:i:s'), 'muokattu' => $customer['muokattu'] ?? null, 'muokkaaja' => $customer['muokkaaja'] ?? '', @@ -1081,15 +1085,14 @@ function dbDeleteTemplate(string $templateId): void { // ==================== PRIORITY EMAILS (ASIAKKUUDET) ==================== -function dbLoadPriorityEmails(string $companyId): array { - return _dbFetchColumn("SELECT email FROM customer_priority_emails WHERE company_id = ?", [$companyId]); -} - function dbIsPriorityEmail(string $companyId, string $email): bool { $email = strtolower(trim($email)); if (!$email) return false; - return (bool)_dbFetchScalar( - "SELECT COUNT(*) FROM customer_priority_emails WHERE company_id = ? AND LOWER(email) = ?", - [$companyId, $email] - ); + // Hae kaikki asiakkaiden priority_emails kentät ja tarkista onko sähköposti listalla + $rows = _dbFetchAll("SELECT priority_emails FROM customers WHERE company_id = ? AND priority_emails != ''", [$companyId]); + foreach ($rows as $row) { + $emails = array_map('strtolower', array_map('trim', explode("\n", $row['priority_emails']))); + if (in_array($email, $emails)) return true; + } + return false; } diff --git a/index.html b/index.html index 69ff8c2..f39e20b 100644 --- a/index.html +++ b/index.html @@ -423,6 +423,7 @@ + @@ -525,16 +526,6 @@ - -
-

Priority-sähköpostiosoitteet

-

Näiltä osoitteilta saapuvat tiketit saavat automaattisesti "Tärkeä"-prioriteetin.

- -
- -
-
-

Telegram-hälytykset

@@ -766,6 +757,11 @@
+

Priority-sähköpostit

+
+

Näiltä osoitteilta saapuvat tiketit saavat automaattisesti ⚠️ Tärkeä -prioriteetin. Yksi per rivi.

+ +
diff --git a/script.js b/script.js index 209364f..8487626 100644 --- a/script.js +++ b/script.js @@ -635,6 +635,7 @@ function openCustomerForm(customer = null) { document.getElementById('form-elaskuosoite').value = c ? (c.elaskuosoite || '') : ''; document.getElementById('form-elaskuvalittaja').value = c ? (c.elaskuvalittaja || '') : ''; document.getElementById('form-lisatiedot').value = c ? (c.lisatiedot || '') : ''; + document.getElementById('form-priority-emails').value = c ? (c.priority_emails || '') : ''; document.getElementById('form-billing-same').checked = false; document.getElementById('billing-fields').style.display = 'block'; const container = document.getElementById('liittymat-container'); @@ -676,6 +677,7 @@ customerForm.addEventListener('submit', async (e) => { elaskuosoite: document.getElementById('form-elaskuosoite').value, elaskuvalittaja: document.getElementById('form-elaskuvalittaja').value, lisatiedot: document.getElementById('form-lisatiedot').value, + priority_emails: document.getElementById('form-priority-emails').value, liittymat: collectLiittymatFromForm(), }; if (id) { data.id = id; await apiCall('customer_update', 'POST', data); } @@ -1793,8 +1795,6 @@ async function loadSettings() { // Vastauspohjat loadTemplates(); - // Priority emails - loadPriorityEmails(); } // ==================== VASTAUSPOHJAT ==================== @@ -1869,24 +1869,6 @@ window.deleteTemplate = async function(id) { } catch (e) { alert(e.message); } }; -// ==================== PRIORITY EMAILS ==================== - -async function loadPriorityEmails() { - try { - const emails = await apiCall('priority_emails'); - document.getElementById('priority-emails-textarea').value = (emails || []).join('\n'); - } catch (e) { console.error(e); } -} - -document.getElementById('btn-save-priority-emails').addEventListener('click', async () => { - const text = document.getElementById('priority-emails-textarea').value; - const emails = text.split('\n').map(e => e.trim()).filter(e => e); - try { - await apiCall('priority_emails_save', 'POST', { emails }); - alert('Priority-osoitteet tallennettu!'); - } catch (e) { alert(e.message); } -}); - // ==================== TELEGRAM ==================== document.getElementById('btn-save-telegram').addEventListener('click', async () => {