Files
PackControl/pirp/public/search_api.php

82 lines
2.6 KiB
PHP

<?php
require_once __DIR__ . '/../src/config.php';
require_once __DIR__ . '/../src/auth.php';
require_once __DIR__ . '/../src/db.php';
require_login();
header('Content-Type: application/json');
$q = trim($_GET['q'] ?? '');
if (mb_strlen($q) < 2) {
echo json_encode(['ok' => true, 'results' => []]);
exit;
}
$pdo = get_db();
$like = '%' . $q . '%';
$results = [];
// Rechnungen
$stmt = $pdo->prepare("SELECT i.id, i.invoice_number, i.total_gross, c.name AS customer_name
FROM invoices i
JOIN customers c ON c.id = i.customer_id
WHERE i.invoice_number ILIKE :q OR c.name ILIKE :q2
ORDER BY i.created_at DESC
LIMIT 5");
$stmt->execute([':q' => $like, ':q2' => $like]);
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
$results[] = [
'type' => 'invoice',
'title' => $row['invoice_number'],
'subtitle' => $row['customer_name'] . ' - ' . number_format((float)$row['total_gross'], 2, ',', '.') . ' €',
'url' => url_for('invoice_pdf.php?id=' . $row['id']),
];
}
// Kunden
$stmt = $pdo->prepare("SELECT id, name, city FROM customers
WHERE name ILIKE :q OR city ILIKE :q2
ORDER BY name
LIMIT 5");
$stmt->execute([':q' => $like, ':q2' => $like]);
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
$results[] = [
'type' => 'customer',
'title' => $row['name'],
'subtitle' => $row['city'] ?? '',
'url' => url_for('customers.php?action=edit&id=' . $row['id']),
];
}
// Ausgaben
$stmt = $pdo->prepare("SELECT id, description, amount, category FROM expenses
WHERE description ILIKE :q OR category ILIKE :q2
ORDER BY expense_date DESC
LIMIT 5");
$stmt->execute([':q' => $like, ':q2' => $like]);
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
$results[] = [
'type' => 'expense',
'title' => $row['description'],
'subtitle' => ($row['category'] ?? '') . ' - ' . number_format((float)$row['amount'], 2, ',', '.') . ' €',
'url' => url_for('expenses.php?action=edit&id=' . $row['id']),
];
}
// Journal-Einträge
$stmt = $pdo->prepare("SELECT id, description, amount, entry_date FROM journal_entries
WHERE description ILIKE :q
ORDER BY entry_date DESC
LIMIT 5");
$stmt->execute([':q' => $like]);
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
$results[] = [
'type' => 'journal',
'title' => $row['description'],
'subtitle' => date('d.m.Y', strtotime($row['entry_date'])) . ' - ' . number_format((float)$row['amount'], 2, ',', '.') . ' €',
'url' => url_for('journal_entry.php?id=' . $row['id']),
];
}
echo json_encode(['ok' => true, 'results' => $results]);