Fix: betrag_brutto in Beleg-Metadaten nach jeder Buchung aktualisieren
This commit is contained in:
parent
c399a917ce
commit
700dc2952f
2 changed files with 19 additions and 0 deletions
|
|
@ -183,6 +183,13 @@ class BookingController
|
|||
}
|
||||
}
|
||||
|
||||
// Belegbetrag in Metadaten aktualisieren
|
||||
$totalBrutto = Booking::sumBruttoForBeleg($belegId);
|
||||
\Steuerkram\Database::execute(
|
||||
'UPDATE belege SET betrag_brutto = ?, updated_at = NOW() WHERE id = ?',
|
||||
[$totalBrutto, $belegId]
|
||||
);
|
||||
|
||||
// Bankbuchung verknüpfen wenn aus Bank-Kontext
|
||||
$bankTxId = (int)($data['bank_tx_id'] ?? 0);
|
||||
if ($bankTxId > 0) {
|
||||
|
|
|
|||
|
|
@ -407,6 +407,18 @@ class Booking
|
|||
return sprintf('BU-%s-%06d', $year, $next);
|
||||
}
|
||||
|
||||
/** Summiert betrag_brutto aller aktiven Hauptbuchungen eines Belegs. */
|
||||
public static function sumBruttoForBeleg(int $belegId): float
|
||||
{
|
||||
$row = Database::fetchOne(
|
||||
'SELECT COALESCE(SUM(betrag_brutto), 0) AS total
|
||||
FROM bookings
|
||||
WHERE beleg_id = ? AND is_reversal = 0 AND parent_booking_id IS NULL',
|
||||
[$belegId]
|
||||
);
|
||||
return round((float)($row['total'] ?? 0), 2);
|
||||
}
|
||||
|
||||
public static function sumByYear(int $year): array
|
||||
{
|
||||
return Database::fetchAll(
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue