123 lines
4 KiB
PHP
123 lines
4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Steuerkram\Models;
|
|
|
|
use Steuerkram\Database;
|
|
|
|
class Account
|
|
{
|
|
public static function all(array $filter = []): array
|
|
{
|
|
$where = ['1=1'];
|
|
$params = [];
|
|
|
|
if (!empty($filter['search'])) {
|
|
$where[] = '(kontonummer LIKE ? OR bezeichnung LIKE ?)';
|
|
$params[] = '%' . $filter['search'] . '%';
|
|
$params[] = '%' . $filter['search'] . '%';
|
|
}
|
|
if (!empty($filter['kontenart'])) {
|
|
$where[] = 'kontenart = ?';
|
|
$params[] = $filter['kontenart'];
|
|
}
|
|
if (isset($filter['aktiv'])) {
|
|
$where[] = 'aktiv = ?';
|
|
$params[] = (int)$filter['aktiv'];
|
|
}
|
|
|
|
$sql = 'SELECT * FROM accounts WHERE ' . implode(' AND ', $where) . ' ORDER BY kontonummer';
|
|
return Database::fetchAll($sql, $params);
|
|
}
|
|
|
|
public static function find(int $id): array|false
|
|
{
|
|
return Database::fetchOne('SELECT * FROM accounts WHERE id = ?', [$id]);
|
|
}
|
|
|
|
public static function findByKontonummer(string $nr): array|false
|
|
{
|
|
return Database::fetchOne('SELECT * FROM accounts WHERE kontonummer = ?', [$nr]);
|
|
}
|
|
|
|
public static function abschreibungskonten(): array
|
|
{
|
|
return Database::fetchAll(
|
|
"SELECT id, kontonummer, bezeichnung FROM accounts
|
|
WHERE kontonummer BETWEEN '4815' AND '4899' AND aktiv = 1
|
|
ORDER BY kontonummer ASC"
|
|
);
|
|
}
|
|
|
|
public static function search(string $q, int $limit = 20): array
|
|
{
|
|
return Database::fetchAll(
|
|
'SELECT id, kontonummer, bezeichnung, kontenart FROM accounts
|
|
WHERE aktiv = 1 AND (kontonummer LIKE ? OR bezeichnung LIKE ?)
|
|
ORDER BY kontonummer LIMIT ?',
|
|
['%' . $q . '%', '%' . $q . '%', $limit]
|
|
);
|
|
}
|
|
|
|
public static function create(array $data): int
|
|
{
|
|
Database::execute(
|
|
'INSERT INTO accounts (kontonummer, bezeichnung, kontenart, beschreibung, steuercode, ustva_kz, euer_zeile)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?)',
|
|
[
|
|
$data['kontonummer'],
|
|
$data['bezeichnung'],
|
|
$data['kontenart'],
|
|
$data['beschreibung'] ?? null,
|
|
$data['steuercode'] ?? null,
|
|
$data['ustva_kz'] ?? null,
|
|
$data['euer_zeile'] !== '' ? ($data['euer_zeile'] ?? null) : null,
|
|
]
|
|
);
|
|
return (int)Database::lastInsertId();
|
|
}
|
|
|
|
public static function update(int $id, array $data): void
|
|
{
|
|
Database::execute(
|
|
'UPDATE accounts SET kontonummer=?, bezeichnung=?, kontenart=?, beschreibung=?, steuercode=?, ustva_kz=?, euer_zeile=?, aktiv=?
|
|
WHERE id=?',
|
|
[
|
|
$data['kontonummer'],
|
|
$data['bezeichnung'],
|
|
$data['kontenart'],
|
|
$data['beschreibung'] ?? null,
|
|
$data['steuercode'] ?? null,
|
|
$data['ustva_kz'] ?? null,
|
|
$data['euer_zeile'] !== '' ? ($data['euer_zeile'] ?? null) : null,
|
|
(int)($data['aktiv'] ?? 1),
|
|
$id,
|
|
]
|
|
);
|
|
}
|
|
|
|
public static function delete(int $id): bool
|
|
{
|
|
// Check if any bookings reference this account
|
|
$used = Database::fetchOne(
|
|
'SELECT COUNT(*) as cnt FROM bookings WHERE kategorie_konto_id = ? OR zahlungskonto_id = ?',
|
|
[$id, $id]
|
|
);
|
|
if ($used && $used['cnt'] > 0) {
|
|
return false; // cannot delete
|
|
}
|
|
Database::execute('DELETE FROM accounts WHERE id = ? AND is_system = 0', [$id]);
|
|
return true;
|
|
}
|
|
|
|
public static function isKontonummerTaken(string $nr, ?int $excludeId = null): bool
|
|
{
|
|
if ($excludeId) {
|
|
$row = Database::fetchOne('SELECT id FROM accounts WHERE kontonummer = ? AND id != ?', [$nr, $excludeId]);
|
|
} else {
|
|
$row = Database::fetchOne('SELECT id FROM accounts WHERE kontonummer = ?', [$nr]);
|
|
}
|
|
return (bool)$row;
|
|
}
|
|
}
|