Fix #51: Belegsuche auf Lieferant, Datum, Bewirtungsgrund, Teilnehmer und Brutto erweitert

This commit is contained in:
fds2610-bot 2026-04-26 10:03:10 +02:00
parent 10c2817c2e
commit 6b1cecf465
4 changed files with 116 additions and 5 deletions

View file

@ -14,9 +14,14 @@ class Beleg
$params = [];
if (!empty($filter['search'])) {
$where[] = '(b.beleg_nr LIKE ? OR b.filename LIKE ? OR b.rechnungs_nr LIKE ? OR b.notes LIKE ?)';
$where[] = '(b.beleg_nr LIKE ? OR b.filename LIKE ? OR b.rechnungs_nr LIKE ? OR b.notes LIKE ?
OR CAST(b.beleg_datum AS CHAR) LIKE ?
OR s.name LIKE ?
OR b.bewirtungsgrund LIKE ? OR b.teilnehmerliste LIKE ?
OR (SELECT CAST(SUM(bk2s.betrag_brutto) AS CHAR) FROM bookings bk2s
WHERE bk2s.beleg_id = b.id AND bk2s.is_reversal = 0) LIKE ?)';
$s = '%' . $filter['search'] . '%';
$params = array_merge($params, [$s, $s, $s, $s]);
$params = array_merge($params, [$s, $s, $s, $s, $s, $s, $s, $s, $s]);
}
if (!empty($filter['year'])) {
$where[] = '(YEAR(b.beleg_datum) = ? OR b.beleg_datum IS NULL)';
@ -59,7 +64,7 @@ class Beleg
$rows = Database::fetchAll($sql, $params);
// Count total
$countSql = 'SELECT COUNT(*) as cnt FROM belege b WHERE ' . implode(' AND ', $where);
$countSql = 'SELECT COUNT(*) as cnt FROM belege b LEFT JOIN suppliers s ON b.supplier_id = s.id WHERE ' . implode(' AND ', $where);
$countParams = array_slice($params, 0, -2);
$total = (int)(Database::fetchOne($countSql, $countParams)['cnt'] ?? 0);

View file

@ -92,7 +92,7 @@
<div class="toolbar">
<div class="toolbar-left">
<form method="get" style="display:flex;gap:.5rem;flex-wrap:wrap;align-items:center">
<input type="search" name="search" placeholder="Belegnr., Dateiname, Rechnungsnr.…"
<input type="search" name="search" placeholder="Belegnr., Lieferant, Datum, Rechnungsnr., Betrag…"
value="<?= $h($filter['search'] ?? '') ?>" style="width:240px">
<select name="year" style="width:80px">
<option value="">Alle Jahre</option>

View file

@ -1 +1 @@
1-260425
1-260426

View file

@ -0,0 +1,106 @@
<?php
declare(strict_types=1);
/**
* Test 21: Belegsuche – erweiterte Felder (Lieferant, Datum, Bewirtungsgrund, Teilnehmer, Brutto).
*/
class BelegSucheErweitertTest extends TestCase
{
protected string $testName = 'Test 21: Belegsuche – erweiterte Felder';
public function run(): void
{
$bankId = $this->findAccountId('1200');
$ausgabeId = $this->findAccountId('4920');
if (!$bankId || !$ausgabeId) {
$this->assert(false, 'Benötigte Konten fehlen – SKR03-Seed vorhanden?');
return;
}
$uid = uniqid('suchtest_', true);
// Lieferant anlegen
$supplierName = 'TestLieferant_' . $uid;
\Steuerkram\Database::execute(
'INSERT INTO suppliers (name) VALUES (?)',
[$supplierName]
);
$supplierId = (int)\Steuerkram\Database::fetchOne('SELECT LAST_INSERT_ID() AS id')['id'];
// Beleg mit allen Suchfeldern anlegen
$belegNr = 'SUCH-' . substr(md5($uid), 0, 8);
$belegDatum = '2025-07-15';
$bewirtung = 'Kundentermin_' . $uid;
$teilnehmer = 'Alice_' . $uid . ', Bob';
$belegId = \Steuerkram\Models\Beleg::create([
'beleg_nr' => $belegNr,
'filename' => 'suchtest.pdf',
'storage_path' => '',
'mime_type' => 'application/pdf',
'file_hash' => hash('sha256', $uid),
'beleg_datum' => $belegDatum,
'belegtyp' => 'Eingangsrechnung',
]);
// Lieferant, Bewirtungsgrund und Teilnehmerliste setzen
\Steuerkram\Database::execute(
'UPDATE belege SET supplier_id = ?, bewirtungsgrund = ?, teilnehmerliste = ? WHERE id = ?',
[$supplierId, $bewirtung, $teilnehmer, $belegId]
);
// Buchung anlegen → betrag_brutto für Brutto-Suche
\Steuerkram\Models\Booking::create([
'beleg_id' => $belegId,
'buchungs_datum' => $belegDatum,
'buchungstyp' => 'Ausgabe',
'kategorie_konto_id' => $ausgabeId,
'zahlungskonto_id' => $bankId,
'betrag_netto' => 299.00,
'ust_satz' => 19.0,
'buchungstext' => 'Suchtest',
'is_reversal' => false,
]);
// ── Suche nach Lieferantenname ────────────────────────────────────
$res = \Steuerkram\Models\Beleg::all(['search' => $supplierName], 1, 50);
$found = $this->containsBeleg($res['rows'], $belegId);
$this->assert($found, 'Suche nach Lieferantenname findet den Beleg');
// ── Suche nach Datum (YYYY-MM-DD) ────────────────────────────────
$res = \Steuerkram\Models\Beleg::all(['search' => '2025-07-15'], 1, 50);
$found = $this->containsBeleg($res['rows'], $belegId);
$this->assert($found, 'Suche nach Belegdatum findet den Beleg');
// ── Suche nach Bewirtungsgrund ────────────────────────────────────
$res = \Steuerkram\Models\Beleg::all(['search' => $bewirtung], 1, 50);
$found = $this->containsBeleg($res['rows'], $belegId);
$this->assert($found, 'Suche nach Bewirtungsgrund findet den Beleg');
// ── Suche nach Teilnehmerliste ────────────────────────────────────
$res = \Steuerkram\Models\Beleg::all(['search' => 'Alice_' . $uid], 1, 50);
$found = $this->containsBeleg($res['rows'], $belegId);
$this->assert($found, 'Suche nach Teilnehmer findet den Beleg');
// ── Suche nach Bruttobetrag ────────────────────────────────────────
// 299 * 1.19 = 355.81
$res = \Steuerkram\Models\Beleg::all(['search' => '355.81'], 1, 50);
$found = $this->containsBeleg($res['rows'], $belegId);
$this->assert($found, 'Suche nach Bruttobetrag findet den Beleg');
// ── Bestehende Felder funktionieren noch ──────────────────────────
$res = \Steuerkram\Models\Beleg::all(['search' => $belegNr], 1, 50);
$found = $this->containsBeleg($res['rows'], $belegId);
$this->assert($found, 'Suche nach Belegnr. funktioniert weiterhin');
}
private function containsBeleg(array $rows, int $id): bool
{
foreach ($rows as $row) {
if ((int)$row['id'] === $id) return true;
}
return false;
}
}