Skip to content

Commit f6c7170

Browse files
authored
♻️ update printing of receipt, invoice, financial doc (#137)
1 parent b5bfdde commit f6c7170

File tree

15 files changed

+221
-213
lines changed

15 files changed

+221
-213
lines changed

mindee/documents/financial/financial_document_v1.py

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from mindee.fields.date import DateField
99
from mindee.fields.locale import LocaleField
1010
from mindee.fields.payment_details import PaymentDetails
11-
from mindee.fields.tax import TaxField
11+
from mindee.fields.tax import Taxes
1212
from mindee.fields.text import TextField
1313

1414

@@ -27,7 +27,7 @@ class FinancialDocumentV1(Document):
2727
"""List of Reference numbers including PO number."""
2828
due_date: DateField
2929
"""Date the invoice is due"""
30-
taxes: List[TaxField] = []
30+
taxes: Taxes
3131
"""List of all taxes"""
3232
total_tax: AmountField
3333
"""Sum total of all taxes"""
@@ -117,11 +117,7 @@ def _build_from_api_prediction(
117117
self.customer_address = TextField(
118118
api_prediction["customer_address"], page_n=page_n
119119
)
120-
121-
self.taxes = [
122-
TaxField(tax_prediction, page_n=page_n, value_key="value")
123-
for tax_prediction in api_prediction["taxes"]
124-
]
120+
self.taxes = Taxes(api_prediction["taxes"], page_id=page_n)
125121
self.supplier_payment_details = [
126122
PaymentDetails(payment_detail, page_n=page_n)
127123
for payment_detail in api_prediction["supplier_payment_details"]
@@ -143,6 +139,15 @@ def _build_from_api_prediction(
143139
api_prediction["subcategory"], page_n=page_n
144140
)
145141

142+
@staticmethod
143+
def _line_items_separator(char: str):
144+
out_str = " "
145+
out_str += f"+{char * 38}"
146+
out_str += f"+{char * 10}"
147+
out_str += f"+{char * 14}"
148+
out_str += f"+{char * 12}"
149+
return out_str + "+"
150+
146151
def __str__(self) -> str:
147152
supplier_company_registrations = "; ".join(
148153
[str(n.value) for n in self.supplier_company_registrations]
@@ -154,38 +159,38 @@ def __str__(self) -> str:
154159
payment_details = "\n ".join(
155160
[str(p) for p in self.supplier_payment_details]
156161
)
157-
taxes = "\n ".join(f"{t}" for t in self.taxes)
158162
line_items = "\n"
159163
if self.line_items:
160164
line_items = "\n Code | QTY | Price | Amount | Tax (Rate) | Description\n"
161165
for item in self.line_items:
162166
line_items += f" {item}\n"
167+
163168
return clean_out_string(
164-
"----- Financial Document V1 -----\n"
165-
f"Filename: {self.filename or ''}\n"
166-
f"Document type: {self.document_type}\n"
167-
f"Category: {self.category}\n"
168-
f"Subcategory: {self.subcategory}\n"
169-
f"Locale: {self.locale}\n"
170-
f"Invoice number: {self.invoice_number}\n"
171-
f"Reference numbers: {reference_numbers}\n"
172-
f"Date: {self.date}\n"
173-
f"Due date: {self.due_date}\n"
174-
f"Time: {self.time}\n"
175-
f"Supplier name: {self.supplier_name}\n"
176-
f"Supplier address: {self.supplier_address}\n"
177-
f"Supplier company registrations: {supplier_company_registrations}\n"
178-
f"Supplier payment details: {payment_details}\n"
179-
f"Customer name: {self.customer_name}\n"
180-
f"Customer address: {self.customer_address}\n"
181-
f"Customer company registrations: {customer_company_registrations}\n"
182-
f"Tip: {self.tip}\n"
183-
f"Taxes: {taxes}\n"
184-
f"Total tax: {self.total_tax}\n"
185-
f"Total net: {self.total_net}\n"
186-
f"Total amount: {self.total_amount}\n"
187-
f"Line Items: {line_items}"
188-
"----------------------"
169+
"Financial Document V1 Prediction\n"
170+
"================================\n"
171+
f":Filename: {self.filename or ''}\n"
172+
f":Document type: {self.document_type}\n"
173+
f":Category: {self.category}\n"
174+
f":Subcategory: {self.subcategory}\n"
175+
f":Locale: {self.locale}\n"
176+
f":Invoice number: {self.invoice_number}\n"
177+
f":Reference numbers: {reference_numbers}\n"
178+
f":Date: {self.date}\n"
179+
f":Due date: {self.due_date}\n"
180+
f":Time: {self.time}\n"
181+
f":Supplier name: {self.supplier_name}\n"
182+
f":Supplier address: {self.supplier_address}\n"
183+
f":Supplier company registrations: {supplier_company_registrations}\n"
184+
f":Supplier payment details: {payment_details}\n"
185+
f":Customer name: {self.customer_name}\n"
186+
f":Customer address: {self.customer_address}\n"
187+
f":Customer company registrations: {customer_company_registrations}\n"
188+
f":Tip: {self.tip}\n"
189+
f":Taxes: {self.taxes}\n"
190+
f":Total tax: {self.total_tax}\n"
191+
f":Total net: {self.total_net}\n"
192+
f":Total amount: {self.total_amount}\n"
193+
f":Line Items: {line_items}"
189194
)
190195

191196
def _checklist(self) -> None:

mindee/documents/invoice/invoice_v3.py

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from mindee.fields.date import DateField
99
from mindee.fields.locale import LocaleField
1010
from mindee.fields.payment_details import PaymentDetails
11-
from mindee.fields.tax import TaxField
11+
from mindee.fields.tax import Taxes
1212
from mindee.fields.text import TextField
1313

1414

@@ -27,7 +27,7 @@ class InvoiceV3(Document):
2727
"""Invoice number"""
2828
due_date: DateField
2929
"""Date the invoice is due"""
30-
taxes: List[TaxField] = []
30+
taxes: Taxes
3131
"""List of all taxes"""
3232
total_tax: AmountField
3333
"""Sum total of all taxes"""
@@ -103,11 +103,7 @@ def _build_from_api_prediction(
103103
self.customer_address = TextField(
104104
api_prediction["customer_address"], page_n=page_n
105105
)
106-
107-
self.taxes = [
108-
TaxField(tax_prediction, page_n=page_n, value_key="value")
109-
for tax_prediction in api_prediction["taxes"]
110-
]
106+
self.taxes = Taxes(api_prediction["taxes"], page_id=page_n)
111107
self.payment_details = [
112108
PaymentDetails(payment_detail, page_n=page_n)
113109
for payment_detail in api_prediction["payment_details"]
@@ -142,27 +138,26 @@ def __str__(self) -> str:
142138
payment_details = "\n ".join(
143139
[str(p) for p in self.payment_details]
144140
)
145-
taxes = "\n ".join(f"{t}" for t in self.taxes)
146141

147142
return clean_out_string(
148-
"----- Invoice V3 -----\n"
149-
f"Filename: {self.filename or ''}\n"
150-
f"Invoice number: {self.invoice_number}\n"
151-
f"Total amount including taxes: {self.total_amount}\n"
152-
f"Total amount excluding taxes: {self.total_net}\n"
153-
f"Invoice date: {self.invoice_date}\n"
154-
f"Invoice due date: {self.due_date}\n"
155-
f"Supplier name: {self.supplier}\n"
156-
f"Supplier address: {self.supplier_address}\n"
157-
f"Customer name: {self.customer_name}\n"
158-
f"Customer company registration: {customer_company_registration}\n"
159-
f"Customer address: {self.customer_address}\n"
160-
f"Payment details: {payment_details}\n"
161-
f"Company numbers: {company_numbers}\n"
162-
f"Taxes: {taxes}\n"
163-
f"Total taxes: {self.total_tax}\n"
164-
f"Locale: {self.locale}\n"
165-
"----------------------"
143+
"Invoice V3 Prediction\n"
144+
"=====================\n"
145+
f":Filename: {self.filename or ''}\n"
146+
f":Invoice number: {self.invoice_number}\n"
147+
f":Total amount: {self.total_amount}\n"
148+
f":Total net: {self.total_net}\n"
149+
f":Invoice date: {self.invoice_date}\n"
150+
f":Invoice due date: {self.due_date}\n"
151+
f":Supplier name: {self.supplier}\n"
152+
f":Supplier address: {self.supplier_address}\n"
153+
f":Customer name: {self.customer_name}\n"
154+
f":Customer company registration: {customer_company_registration}\n"
155+
f":Customer address: {self.customer_address}\n"
156+
f":Payment details: {payment_details}\n"
157+
f":Company numbers: {company_numbers}\n"
158+
f":Taxes: {self.taxes}\n"
159+
f":Total tax: {self.total_tax}\n"
160+
f":Locale: {self.locale}"
166161
)
167162

168163
def _reconstruct(self) -> None:

mindee/documents/invoice/invoice_v4.py

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from mindee.fields.date import DateField
1010
from mindee.fields.locale import LocaleField
1111
from mindee.fields.payment_details import PaymentDetails
12-
from mindee.fields.tax import TaxField
12+
from mindee.fields.tax import Taxes
1313
from mindee.fields.text import TextField
1414

1515

@@ -30,7 +30,7 @@ class InvoiceV4(Document):
3030
"""List of Reference numbers including PO number."""
3131
due_date: DateField
3232
"""Date the invoice is due"""
33-
taxes: List[TaxField] = []
33+
taxes: Taxes
3434
"""List of all taxes"""
3535
total_tax: AmountField
3636
"""Sum total of all taxes"""
@@ -113,10 +113,7 @@ def _build_from_api_prediction(
113113
api_prediction["customer_address"], page_n=page_n
114114
)
115115

116-
self.taxes = [
117-
TaxField(tax_prediction, page_n=page_n, value_key="value")
118-
for tax_prediction in api_prediction["taxes"]
119-
]
116+
self.taxes = Taxes(api_prediction["taxes"], page_id=page_n)
120117
self.supplier_payment_details = [
121118
PaymentDetails(payment_detail, page_n=page_n)
122119
for payment_detail in api_prediction["supplier_payment_details"]
@@ -140,34 +137,33 @@ def __str__(self) -> str:
140137
payment_details = "\n ".join(
141138
[str(p) for p in self.supplier_payment_details]
142139
)
143-
taxes = "\n ".join(f"{t}" for t in self.taxes)
144140
line_items = "\n"
145141
if self.line_items:
146142
line_items = "\n Code | QTY | Price | Amount | Tax (Rate) | Description\n"
147143
for item in self.line_items:
148144
line_items += f" {item}\n"
149145

150146
return clean_out_string(
151-
"----- Invoice V4 -----\n"
152-
f"Filename: {self.filename or ''}\n"
153-
f"Locale: {self.locale}\n"
154-
f"Invoice number: {self.invoice_number}\n"
155-
f"Reference numbers: {reference_numbers}\n"
156-
f"Invoice date: {self.invoice_date}\n"
157-
f"Invoice due date: {self.due_date}\n"
158-
f"Supplier name: {self.supplier_name}\n"
159-
f"Supplier address: {self.supplier_address}\n"
160-
f"Supplier company registrations: {supplier_company_registrations}\n"
161-
f"Supplier payment details: {payment_details}\n"
162-
f"Customer name: {self.customer_name}\n"
163-
f"Customer company registrations: {customer_company_registrations}\n"
164-
f"Customer address: {self.customer_address}\n"
165-
f"Line Items: {line_items}"
166-
f"Taxes: {taxes}\n"
167-
f"Total taxes: {self.total_tax}\n"
168-
f"Total amount excluding taxes: {self.total_net}\n"
169-
f"Total amount including taxes: {self.total_amount}\n"
170-
"----------------------"
147+
"Invoice V4 Prediction\n"
148+
"=====================\n"
149+
f":Filename: {self.filename or ''}\n"
150+
f":Locale: {self.locale}\n"
151+
f":Invoice number: {self.invoice_number}\n"
152+
f":Reference numbers: {reference_numbers}\n"
153+
f":Invoice date: {self.invoice_date}\n"
154+
f":Invoice due date: {self.due_date}\n"
155+
f":Supplier name: {self.supplier_name}\n"
156+
f":Supplier address: {self.supplier_address}\n"
157+
f":Supplier company registrations: {supplier_company_registrations}\n"
158+
f":Supplier payment details: {payment_details}\n"
159+
f":Customer name: {self.customer_name}\n"
160+
f":Customer company registrations: {customer_company_registrations}\n"
161+
f":Customer address: {self.customer_address}\n"
162+
f":Line Items: {line_items}"
163+
f":Taxes: {self.taxes}\n"
164+
f":Total tax: {self.total_tax}\n"
165+
f":Total net: {self.total_net}\n"
166+
f":Total amount: {self.total_amount}"
171167
)
172168

173169
def _reconstruct(self) -> None:

mindee/documents/receipt/line_item_v5.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ def __str__(self) -> str:
5050
if len(description) > 32:
5151
description = description[:32] + "..."
5252
row = [
53+
description,
5354
float_to_string(self.quantity),
54-
float_to_string(self.unit_price),
5555
float_to_string(self.total_amount),
56-
description,
56+
float_to_string(self.unit_price),
5757
]
58-
return "| {:<8} | {:<8} | {:<9} | {:<34} |".format(*row)
58+
return "| {:<36} | {:<8} | {:<12} | {:<10} |".format(*row)

mindee/documents/receipt/receipt_v3.py

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
from typing import List, Optional, TypeVar
1+
from typing import Optional, TypeVar
22

33
from mindee.documents.base import Document, TypeApiPrediction, clean_out_string
44
from mindee.fields.amount import AmountField
55
from mindee.fields.base import field_array_confidence, field_array_sum
66
from mindee.fields.classification import ClassificationField
77
from mindee.fields.date import DateField
88
from mindee.fields.locale import LocaleField
9-
from mindee.fields.tax import TaxField
9+
from mindee.fields.tax import Taxes
1010
from mindee.fields.text import TextField
1111

1212

@@ -23,7 +23,7 @@ class ReceiptV3(Document):
2323
"""Service category"""
2424
merchant_name: TextField
2525
"""Merchant's name"""
26-
taxes: List[TaxField]
26+
taxes: Taxes
2727
"""List of all taxes"""
2828
total_tax: AmountField
2929
"""Sum total of all taxes"""
@@ -54,20 +54,19 @@ def __init__(
5454
self._reconstruct()
5555

5656
def __str__(self) -> str:
57-
taxes = "\n ".join(f"{t}" for t in self.taxes)
5857
return clean_out_string(
59-
"-----Receipt data-----\n"
60-
f"Filename: {self.filename or ''}\n"
61-
f"Total amount including taxes: {self.total_incl}\n"
62-
f"Total amount excluding taxes: {self.total_excl}\n"
63-
f"Date: {self.date}\n"
64-
f"Category: {self.category}\n"
65-
f"Time: {self.time}\n"
66-
f"Merchant name: {self.merchant_name}\n"
67-
f"Taxes: {taxes}\n"
68-
f"Total taxes: {self.total_tax}\n"
69-
f"Locale: {self.locale}\n"
70-
"----------------------"
58+
"Receipt V3 Prediction\n"
59+
"=====================\n"
60+
f":Filename: {self.filename or ''}\n"
61+
f":Total amount: {self.total_incl}\n"
62+
f":Total net: {self.total_excl}\n"
63+
f":Date: {self.date}\n"
64+
f":Category: {self.category}\n"
65+
f":Time: {self.time}\n"
66+
f":Merchant name: {self.merchant_name}\n"
67+
f":Taxes: {self.taxes}\n"
68+
f":Total tax: {self.total_tax}\n"
69+
f":Locale: {self.locale}"
7170
)
7271

7372
def _build_from_api_prediction(
@@ -87,16 +86,7 @@ def _build_from_api_prediction(
8786
api_prediction["supplier"], value_key="value", page_n=page_n
8887
)
8988
self.time = TextField(api_prediction["time"], value_key="value", page_n=page_n)
90-
self.taxes = [
91-
TaxField(
92-
tax_prediction,
93-
page_n=page_n,
94-
value_key="value",
95-
rate_key="rate",
96-
code_key="code",
97-
)
98-
for tax_prediction in api_prediction["taxes"]
99-
]
89+
self.taxes = Taxes(api_prediction["taxes"], page_id=page_n)
10090
self.total_tax = AmountField({"value": None, "confidence": 0.0}, page_n=page_n)
10191
self.total_excl = AmountField({"value": None, "confidence": 0.0}, page_n=page_n)
10292

0 commit comments

Comments
 (0)