Skip to content

Commit 4de8028

Browse files
authored
♻️ rework financial document so as not to cause breaking changes (#121)
1 parent 30bebd9 commit 4de8028

File tree

12 files changed

+681
-671
lines changed

12 files changed

+681
-671
lines changed

mindee/cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ class CommandConfig(Generic[TypeDoc]):
3434
help="Passport",
3535
doc_class=documents.TypePassportV1,
3636
),
37-
"financial": CommandConfig(
37+
"financial-document": CommandConfig(
3838
help="Financial Document (receipt or invoice)",
39-
doc_class=documents.TypeFinancialV1,
39+
doc_class=documents.TypeFinancialDocumentV1,
4040
),
4141
"proof-of-address": CommandConfig(
4242
help="Proof of Address",

mindee/client.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from mindee.documents import (
55
CropperV1,
66
CustomV1,
7-
FinancialV0,
7+
FinancialDocumentV1,
88
FinancialV1,
99
InvoiceV3,
1010
InvoiceV4,
@@ -230,9 +230,9 @@ def _init_default_endpoints(self) -> None:
230230
)
231231
],
232232
),
233-
(OTS_OWNER, FinancialV0.__name__): DocumentConfig(
233+
(OTS_OWNER, FinancialV1.__name__): DocumentConfig(
234234
document_type="financial_doc",
235-
document_class=FinancialV0,
235+
document_class=FinancialV1,
236236
endpoints=[
237237
StandardEndpoint(
238238
url_name="invoices", version="3", api_key=self.api_key
@@ -242,9 +242,9 @@ def _init_default_endpoints(self) -> None:
242242
),
243243
],
244244
),
245-
(OTS_OWNER, FinancialV1.__name__): DocumentConfig(
246-
document_type="financial_doc",
247-
document_class=FinancialV1,
245+
(OTS_OWNER, FinancialDocumentV1.__name__): DocumentConfig(
246+
document_type="financial_document",
247+
document_class=FinancialDocumentV1,
248248
endpoints=[
249249
StandardEndpoint(
250250
url_name="financial_document", version="1", api_key=self.api_key

mindee/documents/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
from mindee.documents.cropper import CropperV1, TypeCropperV1
33
from mindee.documents.custom import CustomV1, TypeCustomV1
44
from mindee.documents.financial import (
5-
FinancialV0,
5+
FinancialDocumentV1,
66
FinancialV1,
7-
TypeFinancialV0,
7+
TypeFinancialDocumentV1,
88
TypeFinancialV1,
99
)
1010
from mindee.documents.invoice import InvoiceV3, InvoiceV4, TypeInvoiceV3, TypeInvoiceV4
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
from .financial_v0 import FinancialV0, TypeFinancialV0
1+
from .financial_document_v1 import FinancialDocumentV1, TypeFinancialDocumentV1
22
from .financial_v1 import FinancialV1, TypeFinancialV1
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
from typing import List, Optional, TypeVar
2+
3+
from mindee.documents.base import Document, TypeApiPrediction, clean_out_string
4+
from mindee.documents.invoice.line_item import InvoiceLineItem
5+
from mindee.fields.amount import AmountField
6+
from mindee.fields.company_registration import CompanyRegistrationField
7+
from mindee.fields.date import DateField
8+
from mindee.fields.locale import LocaleField
9+
from mindee.fields.payment_details import PaymentDetails
10+
from mindee.fields.tax import TaxField
11+
from mindee.fields.text import TextField
12+
13+
14+
class FinancialDocumentV1(Document):
15+
locale: LocaleField
16+
"""locale information"""
17+
total_amount: AmountField
18+
"""Total including taxes"""
19+
total_net: AmountField
20+
"""Total excluding taxes"""
21+
date: DateField
22+
"""Date the invoice was issued"""
23+
invoice_number: TextField
24+
"""Invoice number"""
25+
reference_numbers: List[TextField]
26+
"""List of Reference numbers including PO number."""
27+
due_date: DateField
28+
"""Date the invoice is due"""
29+
taxes: List[TaxField] = []
30+
"""List of all taxes"""
31+
total_tax: AmountField
32+
"""Sum total of all taxes"""
33+
supplier_name: TextField
34+
"""Supplier 's name"""
35+
supplier_address: TextField
36+
"""Supplier's address"""
37+
supplier_company_registrations: List[CompanyRegistrationField]
38+
"""Company numbers"""
39+
customer_name: TextField
40+
"""Customer's name"""
41+
customer_address: TextField
42+
"""Customer's address"""
43+
customer_company_registrations: List[CompanyRegistrationField]
44+
"""Customer company registration numbers"""
45+
supplier_payment_details: List[PaymentDetails]
46+
"""Payment details"""
47+
line_items: List[InvoiceLineItem]
48+
"""Details of line items"""
49+
tip: AmountField
50+
"""Total amount of tip and gratuity."""
51+
time: TextField
52+
"""Time as seen on the receipt in HH:MM format."""
53+
document_type: TextField
54+
"""A classification field, among predefined classes."""
55+
category: TextField
56+
"""The invoice or receipt category among predefined classes."""
57+
subcategory: TextField
58+
"""The invoice or receipt sub-category among predefined classes."""
59+
60+
def __init__(
61+
self,
62+
api_prediction=None,
63+
input_source=None,
64+
page_n: Optional[int] = None,
65+
document_type="financial_doc",
66+
):
67+
"""
68+
Union of `Invoice` and `Receipt`.
69+
70+
:param api_prediction: Raw prediction from HTTP response
71+
:param input_source: Input object
72+
:param page_n: Page number for multi-page PDF input
73+
"""
74+
# need this for building from prediction
75+
self.input_file = input_source
76+
77+
super().__init__(
78+
input_source=input_source,
79+
document_type=document_type,
80+
api_prediction=api_prediction,
81+
page_n=page_n,
82+
)
83+
self._build_from_api_prediction(api_prediction["prediction"], page_n=page_n)
84+
85+
def _build_from_api_prediction(
86+
self, api_prediction: TypeApiPrediction, page_n: Optional[int] = None
87+
) -> None:
88+
"""
89+
Build the document from an API response JSON.
90+
91+
:param api_prediction: Raw prediction from HTTP response
92+
:param page_n: Page number for multi pages pdf input
93+
"""
94+
self.supplier_company_registrations = [
95+
CompanyRegistrationField(field_dict, page_n=page_n)
96+
for field_dict in api_prediction["supplier_company_registrations"]
97+
]
98+
self.date = DateField(api_prediction["date"], page_n=page_n)
99+
self.due_date = DateField(api_prediction["due_date"], page_n=page_n)
100+
self.invoice_number = TextField(api_prediction["invoice_number"], page_n=page_n)
101+
self.reference_numbers = [
102+
TextField(reference_number, page_n=page_n)
103+
for reference_number in api_prediction["reference_numbers"]
104+
]
105+
self.locale = LocaleField(
106+
api_prediction["locale"], value_key="language", page_n=page_n
107+
)
108+
self.supplier_name = TextField(api_prediction["supplier_name"], page_n=page_n)
109+
self.supplier_address = TextField(
110+
api_prediction["supplier_address"], page_n=page_n
111+
)
112+
self.customer_name = TextField(api_prediction["customer_name"], page_n=page_n)
113+
self.customer_company_registrations = [
114+
CompanyRegistrationField(field_dict, page_n=page_n)
115+
for field_dict in api_prediction["customer_company_registrations"]
116+
]
117+
self.customer_address = TextField(
118+
api_prediction["customer_address"], page_n=page_n
119+
)
120+
121+
self.taxes = [
122+
TaxField(tax_prediction, page_n=page_n, value_key="value")
123+
for tax_prediction in api_prediction["taxes"]
124+
]
125+
self.supplier_payment_details = [
126+
PaymentDetails(payment_detail, page_n=page_n)
127+
for payment_detail in api_prediction["supplier_payment_details"]
128+
]
129+
self.line_items = [
130+
InvoiceLineItem(prediction=line_item, page_n=page_n)
131+
for line_item in api_prediction["line_items"]
132+
]
133+
self.total_amount = AmountField(api_prediction["total_amount"], page_n=page_n)
134+
self.total_net = AmountField(api_prediction["total_net"], page_n=page_n)
135+
self.total_tax = AmountField(api_prediction["total_tax"], page_n=page_n)
136+
self.tip = AmountField(api_prediction["tip"], page_n=page_n)
137+
self.time = TextField(api_prediction["time"], page_n=page_n)
138+
self.document_type = TextField(api_prediction["document_type"], page_n=page_n)
139+
self.category = TextField(api_prediction["category"], page_n=page_n)
140+
self.subcategory = TextField(api_prediction["subcategory"], page_n=page_n)
141+
142+
def __str__(self) -> str:
143+
supplier_company_registrations = "; ".join(
144+
[str(n.value) for n in self.supplier_company_registrations]
145+
)
146+
customer_company_registrations = "; ".join(
147+
[str(n.value) for n in self.customer_company_registrations]
148+
)
149+
reference_numbers = ", ".join([str(n.value) for n in self.reference_numbers])
150+
payment_details = "\n ".join(
151+
[str(p) for p in self.supplier_payment_details]
152+
)
153+
taxes = "\n ".join(f"{t}" for t in self.taxes)
154+
line_items = "\n"
155+
if self.line_items:
156+
line_items = "\n Code | QTY | Price | Amount | Tax (Rate) | Description\n"
157+
for item in self.line_items:
158+
line_items += f" {item}\n"
159+
return clean_out_string(
160+
"----- Financial Document V1 -----\n"
161+
f"Filename: {self.filename or ''}\n"
162+
f"Document type: {self.document_type}\n"
163+
f"Category: {self.category}\n"
164+
f"Subcategory: {self.subcategory}\n"
165+
f"Locale: {self.locale}\n"
166+
f"Invoice number: {self.invoice_number}\n"
167+
f"Reference numbers: {reference_numbers}\n"
168+
f"Date: {self.date}\n"
169+
f"Due date: {self.due_date}\n"
170+
f"Time: {self.time}\n"
171+
f"Supplier name: {self.supplier_name}\n"
172+
f"Supplier address: {self.supplier_address}\n"
173+
f"Supplier company registrations: {supplier_company_registrations}\n"
174+
f"Supplier payment details: {payment_details}\n"
175+
f"Customer name: {self.customer_name}\n"
176+
f"Customer address: {self.customer_address}\n"
177+
f"Customer company registrations: {customer_company_registrations}\n"
178+
f"Tip: {self.tip}\n"
179+
f"Taxes: {taxes}\n"
180+
f"Total tax: {self.total_tax}\n"
181+
f"Total net: {self.total_net}\n"
182+
f"Total amount: {self.total_amount}\n"
183+
f"Line Items: {line_items}"
184+
"----------------------"
185+
)
186+
187+
def _checklist(self) -> None:
188+
pass
189+
190+
191+
TypeFinancialDocumentV1 = TypeVar("TypeFinancialDocumentV1", bound=FinancialDocumentV1)

0 commit comments

Comments
 (0)