Verified Commit 929ffd40 authored by Jakob Moser's avatar Jakob Moser
Browse files

Use Decimal instead of storing values as cents

parent 48833571
Loading
Loading
Loading
Loading
+2 −12
Original line number Diff line number Diff line
from dataclasses import dataclass
from decimal import Decimal


@dataclass(frozen=True, eq=False)
@@ -20,21 +21,10 @@ class PaymentOrder:
    petitioner_contact: str  # “Für Nachfragen: E-Mail, gerne Tel.-Nr.”

    reference: str  # Verwendungszweck
    amount_cent: int  # Betrag
    amount: Decimal  # Betrag

    payee_name: str  # Zahlungsempfänger
    payee_iban: str  # IBAN
    payee_bic: str  # BIC

    budgetary_item_id: str  # Abzurechnen unter Posten

    @property
    def amount_euro(self) -> float:
        """Return the total amount in Euro (e.g. 133.70).

        This converts the total amount in Cent (e.g. 13370) to Euro, by dividing by 100.
        The true amount is always stored in Cent, and all calculations should be done
        using the amount in Cent, to avoid floating-point precision issues; but for
        presentation on the UI, this property might be more helpful.
        """
        return self.amount_cent / 100
+2 −6
Original line number Diff line number Diff line
@@ -3,11 +3,7 @@ from pathlib import Path
from pypdf import PdfReader

from .PaymentOrder import PaymentOrder


def to_amount_cent(amount_rendered: str) -> int:
    # TODO Improve this clunky heuristic
    return int(amount_rendered.replace(",", ""))
from ..amount.AmountParser import parse_euro_amount


def load_pdf(pdf_path: Path | str) -> PaymentOrder:
@@ -22,7 +18,7 @@ def load_pdf(pdf_path: Path | str) -> PaymentOrder:
        petitioner_name=fields["Name"].value,
        petitioner_contact=fields["Kontaktdaten"].value,
        reference=fields["Text6"].value + "\n" + fields["Text7"].value,
        amount_cent=to_amount_cent(fields["Text8"].value),
        amount_cent=parse_euro_amount(fields["Text8"].value),
        payee_name=fields["Text9"].value,
        payee_iban="DE" + fields["Text10"].value,
        payee_bic=fields["Text11"].value,