amount vs value was ambiguous.

This commit is contained in:
Julien Palard 2024-03-28 17:14:54 +01:00
parent 168f1be13c
commit 9cbd7957e2
Signed by: mdk
GPG Key ID: 0EFC1AC1006886F8
2 changed files with 20 additions and 20 deletions

View File

@ -11,7 +11,7 @@ from rich.table import Table
from rich import print as rich_print
from rich.panel import Panel
__version__ = "0.2"
__version__ = "0.3"
DATE_RE = r"([0-9]{1,2}/[0-9]{2}/[0-9]{2,4})"
@ -39,9 +39,9 @@ RE_CARD_OWNER = [ # First pattern is tried first
logger = logging.getLogger(__name__)
def parse_decimal(value: str):
"""Parse a French value like 1.234,56 to a Decimal instance."""
return Decimal(value.replace(".", "").replace(",", "."))
def parse_decimal(amount: str):
"""Parse a French amount like 1.234,56 to a Decimal instance."""
return Decimal(amount.replace(".", "").replace(",", "."))
class Line:
@ -96,17 +96,17 @@ class Line:
return "-" if column < column_at else "+"
@property
def amount(self):
"""Raw value for this line, dependless of its 'debit'/'credit' column"""
def abs_amount(self):
"""Absolute value of the amount for this line."""
return parse_decimal(self.match["amount"])
@property
def value(self):
"""Value for this line. Positive for credits, negative for debits."""
return self.amount if self.direction == "+" else -self.amount
def amount(self):
"""Amount for this line. Positive for credits, negative for debits."""
return self.abs_amount if self.direction == "+" else -self.abs_amount
def __str__(self):
return f"{self.safe_label} {self.value}"
return f"{self.safe_label} {self.amount}"
class AccountLine(Line):
@ -253,9 +253,9 @@ class Statement:
table.add_column("Value", style="magenta")
for line in self.lines:
if line.description and show_desriptions:
table.add_row(line.label + "\n" + line.description, str(line.value))
table.add_row(line.label + "\n" + line.description, str(line.amount))
else:
table.add_row(line.label, str(line.value))
table.add_row(line.label, str(line.amount))
Console().print(table)
@ -273,7 +273,7 @@ class AccountStatement(Statement):
It just verifies that all the lines sum to the right total.
"""
computed = sum(line.value for line in self.lines)
computed = sum(line.amount for line in self.lines)
if self.balance_before + computed != self.balance_after:
raise ValueError(
f"Inconsistent total, found: {self.balance_before + computed!r}, "
@ -289,11 +289,11 @@ class AccountStatement(Statement):
for lineno, text in enumerate(self.text.splitlines()):
line = BalanceBeforeLine(self, text)
if line.match:
self.balance_before = line.value
self.balance_before = line.amount
start = lineno
line = BalanceAfterLine(self, text)
if line.match:
self.balance_after = line.value
self.balance_after = line.amount
stop = lineno
return start, stop
@ -318,7 +318,7 @@ class CardStatement(Statement):
It just verifies that all the lines sum to the right total.
"""
computed = sum(line.value for line in self.lines)
computed = sum(line.amount for line in self.lines)
if computed != self.card_debit:
raise ValueError(
f"Inconsistent total, found: {computed!r}, "
@ -334,12 +334,12 @@ class CardStatement(Statement):
for text in self.text.splitlines():
line = CardLineDebitWithFrancs(self, text)
if line.match:
self.card_debit = line.value
self.card_debit = line.amount
self.LineImpl = CardLineWithFrancs
return
line = CardLineDebit(self, text)
if line.match:
self.card_debit = line.value
self.card_debit = line.amount
return
def _parse_card_owner(self):

View File

@ -54,7 +54,7 @@ def test_parse_cb_line():
"""
)
assert statement.lines
assert statement.lines[0].value == -8
assert statement.lines[0].amount == -8
@pytest.mark.parametrize("pdf", list(Path(__file__).parent.glob("*.pdf")))
@ -64,7 +64,7 @@ def test_cb_consistency_from_files(pdf):
if not isinstance(statement, CardStatement):
return
found = statement.card_debit
computed = sum(line.value for line in statement.lines)
computed = sum(line.amount for line in statement.lines)
assert (
found == computed
), f"Inconsistent total, found: {found!r}, computed: {computed!r}"