- Split address into street, postal code, city (sortable) - Add billing postal code/city fields - Add e-invoice address and operator fields - Add trivia stats (top postal code, top speed, avg price) - Improved layout with stat cards grid and max-width container - Sticky header, modal animations, search icon - Auto-backup on every save (keeps last 30 backups) - Footer added Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
178 lines
7.0 KiB
PHP
178 lines
7.0 KiB
PHP
<?php
|
|
session_start();
|
|
header('Content-Type: application/json');
|
|
|
|
define('ADMIN_PASSWORD', 'cuitunet2024');
|
|
define('DATA_FILE', __DIR__ . '/data/customers.json');
|
|
|
|
// Varmista data-kansio
|
|
if (!file_exists(__DIR__ . '/data')) {
|
|
mkdir(__DIR__ . '/data', 0755, true);
|
|
}
|
|
if (!file_exists(DATA_FILE)) {
|
|
file_put_contents(DATA_FILE, '[]');
|
|
}
|
|
|
|
$method = $_SERVER['REQUEST_METHOD'];
|
|
$action = $_GET['action'] ?? '';
|
|
|
|
// Auth-tarkistus (paitsi login)
|
|
function requireAuth() {
|
|
if (!isset($_SESSION['authenticated']) || $_SESSION['authenticated'] !== true) {
|
|
http_response_code(401);
|
|
echo json_encode(['error' => 'Kirjaudu sisään']);
|
|
exit;
|
|
}
|
|
}
|
|
|
|
function loadCustomers(): array {
|
|
$data = file_get_contents(DATA_FILE);
|
|
return json_decode($data, true) ?: [];
|
|
}
|
|
|
|
function saveCustomers(array $customers): void {
|
|
// Automaattinen backup ennen tallennusta
|
|
if (file_exists(DATA_FILE) && filesize(DATA_FILE) > 2) {
|
|
$backupDir = __DIR__ . '/data/backups';
|
|
if (!file_exists($backupDir)) mkdir($backupDir, 0755, true);
|
|
copy(DATA_FILE, $backupDir . '/customers_' . date('Y-m-d_His') . '.json');
|
|
// Säilytä vain 30 viimeisintä backuppia
|
|
$backups = glob($backupDir . '/customers_*.json');
|
|
if (count($backups) > 30) {
|
|
sort($backups);
|
|
array_map('unlink', array_slice($backups, 0, count($backups) - 30));
|
|
}
|
|
}
|
|
file_put_contents(DATA_FILE, json_encode($customers, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
|
|
}
|
|
|
|
function generateId(): string {
|
|
return bin2hex(random_bytes(8));
|
|
}
|
|
|
|
switch ($action) {
|
|
case 'login':
|
|
if ($method !== 'POST') break;
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
$password = $input['password'] ?? '';
|
|
if ($password === ADMIN_PASSWORD) {
|
|
$_SESSION['authenticated'] = true;
|
|
echo json_encode(['success' => true]);
|
|
} else {
|
|
http_response_code(401);
|
|
echo json_encode(['error' => 'Väärä salasana']);
|
|
}
|
|
break;
|
|
|
|
case 'logout':
|
|
session_destroy();
|
|
echo json_encode(['success' => true]);
|
|
break;
|
|
|
|
case 'check_auth':
|
|
echo json_encode(['authenticated' => isset($_SESSION['authenticated']) && $_SESSION['authenticated'] === true]);
|
|
break;
|
|
|
|
case 'customers':
|
|
requireAuth();
|
|
if ($method === 'GET') {
|
|
$customers = loadCustomers();
|
|
echo json_encode($customers);
|
|
}
|
|
break;
|
|
|
|
case 'customer':
|
|
requireAuth();
|
|
if ($method === 'POST') {
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
$customers = loadCustomers();
|
|
$customer = [
|
|
'id' => generateId(),
|
|
'yritys' => trim($input['yritys'] ?? ''),
|
|
'asennusosoite' => trim($input['asennusosoite'] ?? ''),
|
|
'postinumero' => trim($input['postinumero'] ?? ''),
|
|
'kaupunki' => trim($input['kaupunki'] ?? ''),
|
|
'liittymanopeus' => trim($input['liittymanopeus'] ?? ''),
|
|
'hinta' => floatval($input['hinta'] ?? 0),
|
|
'yhteyshenkilö' => trim($input['yhteyshenkilö'] ?? ''),
|
|
'puhelin' => trim($input['puhelin'] ?? ''),
|
|
'sahkoposti' => trim($input['sahkoposti'] ?? ''),
|
|
'laskutusosoite' => trim($input['laskutusosoite'] ?? ''),
|
|
'laskutuspostinumero' => trim($input['laskutuspostinumero'] ?? ''),
|
|
'laskutuskaupunki' => trim($input['laskutuskaupunki'] ?? ''),
|
|
'laskutussahkoposti' => trim($input['laskutussahkoposti'] ?? ''),
|
|
'elaskuosoite' => trim($input['elaskuosoite'] ?? ''),
|
|
'elaskuvalittaja' => trim($input['elaskuvalittaja'] ?? ''),
|
|
'ytunnus' => trim($input['ytunnus'] ?? ''),
|
|
'lisatiedot' => trim($input['lisatiedot'] ?? ''),
|
|
'luotu' => date('Y-m-d H:i:s'),
|
|
];
|
|
if (empty($customer['yritys'])) {
|
|
http_response_code(400);
|
|
echo json_encode(['error' => 'Yrityksen nimi vaaditaan']);
|
|
break;
|
|
}
|
|
$customers[] = $customer;
|
|
saveCustomers($customers);
|
|
echo json_encode($customer);
|
|
}
|
|
break;
|
|
|
|
case 'customer_update':
|
|
requireAuth();
|
|
if ($method !== 'POST') break;
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
$id = $input['id'] ?? '';
|
|
$customers = loadCustomers();
|
|
$found = false;
|
|
foreach ($customers as &$c) {
|
|
if ($c['id'] === $id) {
|
|
$c['yritys'] = trim($input['yritys'] ?? $c['yritys']);
|
|
$c['asennusosoite'] = trim($input['asennusosoite'] ?? $c['asennusosoite']);
|
|
$c['postinumero'] = trim($input['postinumero'] ?? ($c['postinumero'] ?? ''));
|
|
$c['kaupunki'] = trim($input['kaupunki'] ?? ($c['kaupunki'] ?? ''));
|
|
$c['liittymanopeus'] = trim($input['liittymanopeus'] ?? $c['liittymanopeus']);
|
|
$c['hinta'] = floatval($input['hinta'] ?? $c['hinta']);
|
|
$c['yhteyshenkilö'] = trim($input['yhteyshenkilö'] ?? $c['yhteyshenkilö']);
|
|
$c['puhelin'] = trim($input['puhelin'] ?? $c['puhelin']);
|
|
$c['sahkoposti'] = trim($input['sahkoposti'] ?? $c['sahkoposti']);
|
|
$c['laskutusosoite'] = trim($input['laskutusosoite'] ?? $c['laskutusosoite']);
|
|
$c['laskutuspostinumero'] = trim($input['laskutuspostinumero'] ?? ($c['laskutuspostinumero'] ?? ''));
|
|
$c['laskutuskaupunki'] = trim($input['laskutuskaupunki'] ?? ($c['laskutuskaupunki'] ?? ''));
|
|
$c['laskutussahkoposti'] = trim($input['laskutussahkoposti'] ?? $c['laskutussahkoposti']);
|
|
$c['elaskuosoite'] = trim($input['elaskuosoite'] ?? ($c['elaskuosoite'] ?? ''));
|
|
$c['elaskuvalittaja'] = trim($input['elaskuvalittaja'] ?? ($c['elaskuvalittaja'] ?? ''));
|
|
$c['ytunnus'] = trim($input['ytunnus'] ?? $c['ytunnus']);
|
|
$c['lisatiedot'] = trim($input['lisatiedot'] ?? $c['lisatiedot']);
|
|
$c['muokattu'] = date('Y-m-d H:i:s');
|
|
$found = true;
|
|
echo json_encode($c);
|
|
break;
|
|
}
|
|
}
|
|
unset($c);
|
|
if (!$found) {
|
|
http_response_code(404);
|
|
echo json_encode(['error' => 'Asiakasta ei löydy']);
|
|
break;
|
|
}
|
|
saveCustomers($customers);
|
|
break;
|
|
|
|
case 'customer_delete':
|
|
requireAuth();
|
|
if ($method !== 'POST') break;
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
$id = $input['id'] ?? '';
|
|
$customers = loadCustomers();
|
|
$customers = array_values(array_filter($customers, fn($c) => $c['id'] !== $id));
|
|
saveCustomers($customers);
|
|
echo json_encode(['success' => true]);
|
|
break;
|
|
|
|
default:
|
|
http_response_code(404);
|
|
echo json_encode(['error' => 'Tuntematon toiminto']);
|
|
break;
|
|
}
|