Add leads (liidit) tab for tracking potential customers
- New Liidit tab with table, search, add/edit/delete - Lead fields: company, contact, phone, email, address, city, status, notes - Status workflow: Uusi → Kontaktoitu → Kiinnostunut → Odottaa toimitusta - Color-coded status badges - Detail view with notes display - "Muuta asiakkaaksi" converts lead to customer with pre-filled data - Lead CRUD endpoints in api.php with changelog logging - leads.json added to .gitignore Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
128
api.php
128
api.php
@@ -14,6 +14,7 @@ define('DATA_FILE', DATA_DIR . '/customers.json');
|
||||
define('USERS_FILE', DATA_DIR . '/users.json');
|
||||
define('CHANGELOG_FILE', DATA_DIR . '/changelog.json');
|
||||
define('ARCHIVE_FILE', DATA_DIR . '/archive.json');
|
||||
define('LEADS_FILE', DATA_DIR . '/leads.json');
|
||||
define('TOKENS_FILE', DATA_DIR . '/reset_tokens.json');
|
||||
define('RATE_FILE', DATA_DIR . '/login_attempts.json');
|
||||
define('SITE_URL', 'https://intra.cuitunet.fi');
|
||||
@@ -24,7 +25,7 @@ define('MAIL_FROM_NAME', 'CuituNet Intra');
|
||||
|
||||
// Varmista data-kansio ja tiedostot
|
||||
if (!file_exists(DATA_DIR)) mkdir(DATA_DIR, 0755, true);
|
||||
foreach ([DATA_FILE, USERS_FILE, CHANGELOG_FILE, ARCHIVE_FILE, TOKENS_FILE, RATE_FILE] as $f) {
|
||||
foreach ([DATA_FILE, USERS_FILE, CHANGELOG_FILE, ARCHIVE_FILE, LEADS_FILE, TOKENS_FILE, RATE_FILE] as $f) {
|
||||
if (!file_exists($f)) file_put_contents($f, '[]');
|
||||
}
|
||||
|
||||
@@ -660,6 +661,131 @@ switch ($action) {
|
||||
echo json_encode(['success' => true]);
|
||||
break;
|
||||
|
||||
// ---------- LEADS ----------
|
||||
case 'leads':
|
||||
requireAuth();
|
||||
$leads = json_decode(file_get_contents(LEADS_FILE), true) ?: [];
|
||||
echo json_encode($leads);
|
||||
break;
|
||||
|
||||
case 'lead_create':
|
||||
requireAuth();
|
||||
if ($method !== 'POST') break;
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$lead = [
|
||||
'id' => generateId(),
|
||||
'yritys' => trim($input['yritys'] ?? ''),
|
||||
'yhteyshenkilo' => trim($input['yhteyshenkilo'] ?? ''),
|
||||
'puhelin' => trim($input['puhelin'] ?? ''),
|
||||
'sahkoposti' => trim($input['sahkoposti'] ?? ''),
|
||||
'osoite' => trim($input['osoite'] ?? ''),
|
||||
'kaupunki' => trim($input['kaupunki'] ?? ''),
|
||||
'tila' => trim($input['tila'] ?? 'uusi'),
|
||||
'muistiinpanot' => trim($input['muistiinpanot'] ?? ''),
|
||||
'luotu' => date('Y-m-d H:i:s'),
|
||||
'luoja' => currentUser(),
|
||||
];
|
||||
if (empty($lead['yritys'])) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Yrityksen nimi vaaditaan']);
|
||||
break;
|
||||
}
|
||||
$leads = json_decode(file_get_contents(LEADS_FILE), true) ?: [];
|
||||
$leads[] = $lead;
|
||||
file_put_contents(LEADS_FILE, json_encode($leads, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
|
||||
addLog('lead_create', $lead['id'], $lead['yritys'], 'Lisäsi liidin');
|
||||
echo json_encode($lead);
|
||||
break;
|
||||
|
||||
case 'lead_update':
|
||||
requireAuth();
|
||||
if ($method !== 'POST') break;
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$id = $input['id'] ?? '';
|
||||
$leads = json_decode(file_get_contents(LEADS_FILE), true) ?: [];
|
||||
$found = false;
|
||||
foreach ($leads as &$l) {
|
||||
if ($l['id'] === $id) {
|
||||
$fields = ['yritys','yhteyshenkilo','puhelin','sahkoposti','osoite','kaupunki','tila','muistiinpanot'];
|
||||
foreach ($fields as $f) {
|
||||
if (isset($input[$f])) $l[$f] = trim($input[$f]);
|
||||
}
|
||||
$l['muokattu'] = date('Y-m-d H:i:s');
|
||||
$l['muokkaaja'] = currentUser();
|
||||
$found = true;
|
||||
addLog('lead_update', $l['id'], $l['yritys'], 'Muokkasi liidiä');
|
||||
echo json_encode($l);
|
||||
break;
|
||||
}
|
||||
}
|
||||
unset($l);
|
||||
if (!$found) {
|
||||
http_response_code(404);
|
||||
echo json_encode(['error' => 'Liidiä ei löydy']);
|
||||
break;
|
||||
}
|
||||
file_put_contents(LEADS_FILE, json_encode($leads, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
|
||||
break;
|
||||
|
||||
case 'lead_delete':
|
||||
requireAuth();
|
||||
if ($method !== 'POST') break;
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$id = $input['id'] ?? '';
|
||||
$leads = json_decode(file_get_contents(LEADS_FILE), true) ?: [];
|
||||
$deleted = null;
|
||||
foreach ($leads as $l) {
|
||||
if ($l['id'] === $id) { $deleted = $l; break; }
|
||||
}
|
||||
$leads = array_values(array_filter($leads, fn($l) => $l['id'] !== $id));
|
||||
file_put_contents(LEADS_FILE, json_encode($leads, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
|
||||
if ($deleted) addLog('lead_delete', $id, $deleted['yritys'] ?? '', 'Poisti liidin');
|
||||
echo json_encode(['success' => true]);
|
||||
break;
|
||||
|
||||
case 'lead_to_customer':
|
||||
requireAuth();
|
||||
if ($method !== 'POST') break;
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$id = $input['id'] ?? '';
|
||||
$leads = json_decode(file_get_contents(LEADS_FILE), true) ?: [];
|
||||
$lead = null;
|
||||
foreach ($leads as $l) {
|
||||
if ($l['id'] === $id) { $lead = $l; break; }
|
||||
}
|
||||
if (!$lead) {
|
||||
http_response_code(404);
|
||||
echo json_encode(['error' => 'Liidiä ei löydy']);
|
||||
break;
|
||||
}
|
||||
// Luo asiakas liidistä
|
||||
$customer = [
|
||||
'id' => generateId(),
|
||||
'yritys' => $lead['yritys'],
|
||||
'yhteyshenkilö' => $lead['yhteyshenkilo'] ?? '',
|
||||
'puhelin' => $lead['puhelin'] ?? '',
|
||||
'sahkoposti' => $lead['sahkoposti'] ?? '',
|
||||
'laskutusosoite' => '',
|
||||
'laskutuspostinumero' => '',
|
||||
'laskutuskaupunki' => '',
|
||||
'laskutussahkoposti' => '',
|
||||
'elaskuosoite' => '',
|
||||
'elaskuvalittaja' => '',
|
||||
'ytunnus' => '',
|
||||
'lisatiedot' => $lead['muistiinpanot'] ?? '',
|
||||
'liittymat' => [['asennusosoite' => $lead['osoite'] ?? '', 'postinumero' => '', 'kaupunki' => $lead['kaupunki'] ?? '', 'liittymanopeus' => '', 'hinta' => 0, 'sopimuskausi' => '', 'alkupvm' => '']],
|
||||
'luotu' => date('Y-m-d H:i:s'),
|
||||
];
|
||||
$customers = loadCustomers();
|
||||
$customers[] = $customer;
|
||||
saveCustomers($customers);
|
||||
// Poista liidi
|
||||
$leads = array_values(array_filter($leads, fn($l) => $l['id'] !== $id));
|
||||
file_put_contents(LEADS_FILE, json_encode($leads, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
|
||||
addLog('lead_to_customer', $customer['id'], $customer['yritys'], 'Muutti liidin asiakkaaksi');
|
||||
echo json_encode($customer);
|
||||
break;
|
||||
|
||||
// ---------- FILES ----------
|
||||
case 'file_upload':
|
||||
requireAuth();
|
||||
|
||||
Reference in New Issue
Block a user