Skip to content

Commit bafd32a

Browse files
ianardeefharper
authored andcommitted
🐛 fix handling of tax printing
1 parent ca65189 commit bafd32a

File tree

4 files changed

+28
-22
lines changed

4 files changed

+28
-22
lines changed

mindee/documents/invoice.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def build_from_api_prediction(self, api_prediction: dict, page_n=0):
105105
def __str__(self) -> str:
106106
company_numbers = "; ".join([str(n.value) for n in self.company_number])
107107
payments = ", ".join([str(p) for p in self.payment_details])
108-
taxes = ", ".join([f"{t.value} {t.rate}%" for t in self.taxes])
108+
taxes = ", ".join(f"{t}" for t in self.taxes)
109109
return (
110110
"-----Invoice data-----\n"
111111
f"Filename: {self.filename}\n"

mindee/documents/receipt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def __init__(
4545
)
4646

4747
def __str__(self) -> str:
48-
taxes = ", ".join([f"{t.value} {t.rate}%" for t in self.taxes])
48+
taxes = ", ".join(f"{t}" for t in self.taxes)
4949
return (
5050
"-----Receipt data-----\n"
5151
f"Filename: {self.filename}\n"

mindee/fields/tax.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ class Tax(Field):
1010
def __init__(
1111
self,
1212
tax_prediction: dict,
13-
value_key: str = "amount",
13+
value_key: str = "value",
1414
rate_key: str = "rate",
1515
code_key: str = "code",
1616
reconstructed: bool = False,
1717
page_n=None,
1818
):
1919
"""
20+
Tax field object.
21+
2022
:param tax_prediction: Tax prediction object from HTTP response
2123
:param value_key: Key to use in the tax_prediction dict
2224
:param rate_key: Key to use for getting the Tax rate in the tax_prediction dict
@@ -38,10 +40,10 @@ def __init__(
3840

3941
try:
4042
self.code = str(tax_prediction[code_key])
41-
if self.code == "N/A":
42-
self.code = None
4343
except (TypeError, KeyError):
4444
self.code = None
45+
if self.code in ("N/A", "None"):
46+
self.code = None
4547

4648
try:
4749
self.value = float(tax_prediction[value_key])
@@ -54,17 +56,8 @@ def __str__(self):
5456
tax_str = ""
5557
if self.value is not None:
5658
tax_str += str(self.value)
57-
else:
58-
tax_str += "_"
59-
6059
if self.rate is not None:
61-
tax_str += "; " + str(self.rate) + "%"
62-
else:
63-
tax_str += "; _"
64-
60+
tax_str += f" {self.rate}%"
6561
if self.code is not None:
66-
tax_str += "; " + str(self.code)
67-
else:
68-
tax_str += "; _"
69-
70-
return tax_str
62+
tax_str += f" {self.code}"
63+
return tax_str.strip()

tests/fields/test_tax.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,42 @@
33

44
def test_constructor():
55
field_dict = {
6-
"value": "2",
6+
"value": 2,
77
"rate": 0.2,
88
"code": "QST",
99
"confidence": 0.1,
1010
"polygon": [[0.016, 0.707], [0.414, 0.707], [0.414, 0.831], [0.016, 0.831]],
1111
}
12-
tax = Tax(field_dict, value_key="value")
12+
tax = Tax(field_dict)
1313
assert tax.value == 2
1414
assert tax.confidence == 0.1
1515
assert tax.rate == 0.2
1616
assert len(tax.bbox) > 0
17-
assert type(str(tax)) == str
17+
assert str(tax) == "2.0 0.2% QST"
1818

1919

2020
def test_constructor_no_rate():
21-
field_dict = {"value": "2", "rate": "AA", "confidence": 0.1}
21+
field_dict = {"value": 2.0, "confidence": 0.1}
2222
tax = Tax(field_dict)
2323
assert tax.rate is None
2424
assert len(tax.bbox) == 0
25+
assert str(tax) == "2.0"
2526

2627

2728
def test_constructor_no_amount():
2829
field_dict = {"value": "NA", "rate": "AA", "code": "N/A", "confidence": 0.1}
2930
tax = Tax(field_dict)
3031
assert tax.value is None
31-
assert type(str(tax)) == str
32+
assert str(tax) == ""
33+
34+
35+
def test_constructor_only_code():
36+
field_dict = {
37+
"value": "NA",
38+
"rate": "None",
39+
"code": "TAXES AND FEES",
40+
"confidence": 0.1,
41+
}
42+
tax = Tax(field_dict)
43+
assert tax.value is None
44+
assert str(tax) == "TAXES AND FEES"

0 commit comments

Comments
 (0)