feat: tiketin asiakas-automaattitunnistus + lisää liidi -nappi

- Kun tiketti avataan, tarkistetaan lähettäjän sähköposti
  asiakasrekisteristä (sahkoposti + laskutussahkoposti)
- Jos asiakas löytyy → linkitetään automaattisesti + vihreä badge
- Jos ei löydy → näytetään "Lisää liidi" -nappi lähettäjätietojen
  vieressä, joka avaa liidilomakkeen esitäytetyillä tiedoilla
  (yhteyshenkilö, sähköposti, muistiinpanot)
- Jo linkitetyllä tiketillä näytetään vihreä asiakas-badge

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-10 21:23:05 +02:00
parent 0029256f03
commit f6602fb81f

View File

@@ -1291,7 +1291,7 @@ async function showTicketDetail(id, companyId = '') {
<div style="display:flex;justify-content:space-between;align-items:flex-start;flex-wrap:wrap;gap:1rem;margin-bottom:1.25rem;">
<div>
<h2 style="color:#0f3460;margin-bottom:0.25rem;font-size:1.2rem;">${esc(ticket.subject)}</h2>
<div style="font-size:0.85rem;color:#888;">
<div style="font-size:0.85rem;color:#888;" id="ticket-sender-line">
${esc(ticket.from_name)} &lt;${esc(ticket.from_email)}&gt; · Luotu ${esc(ticket.created)}
</div>
</div>
@@ -1369,7 +1369,7 @@ async function showTicketDetail(id, companyId = '') {
} catch (e) { alert(e.message); }
});
// Customer link — load customers dropdown
// Customer link — load customers dropdown + automaattinen tunnistus
try {
const custSelect = document.getElementById('ticket-customer-select');
customers.forEach(c => {
@@ -1379,6 +1379,45 @@ async function showTicketDetail(id, companyId = '') {
if (c.id === ticket.customer_id) opt.selected = true;
custSelect.appendChild(opt);
});
// Automaattinen asiakastunnistus sähköpostin perusteella
const senderEmail = (ticket.from_email || '').toLowerCase().trim();
const senderLine = document.getElementById('ticket-sender-line');
if (senderEmail && !ticket.customer_id) {
const matchedCustomer = customers.find(c =>
(c.sahkoposti || '').toLowerCase().trim() === senderEmail ||
(c.laskutussahkoposti || '').toLowerCase().trim() === senderEmail
);
if (matchedCustomer) {
// Löytyi asiakas → linkitä automaattisesti
custSelect.value = matchedCustomer.id;
custSelect.dispatchEvent(new Event('change'));
if (senderLine) {
senderLine.insertAdjacentHTML('beforeend',
` <span style="background:#e6f9ee;color:#1a7d42;padding:2px 8px;border-radius:10px;font-size:0.78rem;font-weight:600;">✓ ${esc(matchedCustomer.yritys)}</span>`);
}
} else {
// Ei löytynyt → näytä "Lisää liidi" -nappi
if (senderLine) {
senderLine.insertAdjacentHTML('beforeend',
` <button class="btn-link" id="btn-ticket-add-lead" style="font-size:0.82rem;color:#2563eb;font-weight:600;margin-left:0.5rem;" title="Luo liidi tämän lähettäjän tiedoilla">+ Lisää liidi</button>`);
document.getElementById('btn-ticket-add-lead')?.addEventListener('click', () => {
// Avaa liidilomake esitäytetyillä tiedoilla
openLeadForm(null);
document.getElementById('lead-form-yhteyshenkilo').value = ticket.from_name || '';
document.getElementById('lead-form-sahkoposti').value = ticket.from_email || '';
document.getElementById('lead-form-muistiinpanot').value = 'Tiketti: ' + (ticket.subject || '') + '\\nLähettäjä: ' + (ticket.from_email || '');
});
}
}
} else if (senderEmail && ticket.customer_id) {
// Asiakas jo linkitetty — näytä badge
const linked = customers.find(c => c.id === ticket.customer_id);
if (linked && senderLine) {
senderLine.insertAdjacentHTML('beforeend',
` <span style="background:#e6f9ee;color:#1a7d42;padding:2px 8px;border-radius:10px;font-size:0.78rem;font-weight:600;">✓ ${esc(linked.yritys)}</span>`);
}
}
} catch (e) {}
document.getElementById('ticket-customer-select').addEventListener('change', async function() {