110 lines
4.9 KiB
PHP
110 lines
4.9 KiB
PHP
<?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 (Dezimalpunkt)');
|
||
|
||
$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 (Dezimalkomma)');
|
||
|
||
// ── 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;
|
||
}
|
||
}
|