Skip to content

Commit c18a59b

Browse files
committed
✨ update to Invoices v3
1 parent db70bb0 commit c18a59b

File tree

16 files changed

+510
-878
lines changed

16 files changed

+510
-878
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Mindee Python API Helper Changelog
22

3+
## v2.1.0 (2022-03-02)
4+
5+
### Changes
6+
7+
* :sparkles: update to Invoices v3
8+
* :recycle: refactor `Endpoint` classes and document building
9+
* :arrow_up: Update PikePDF to 5.0.1
10+
* :sparkles: add a basic logger
11+
312
## v2.0.2 (2022-02-21)
413

514
### Fixes

mindee/documents/financial_document.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
from typing import List
1+
from typing import List, Optional
22

33
from mindee.fields import Field
4+
from mindee.fields.orientation import Orientation
5+
from mindee.fields.locale import Locale
46
from mindee.http import Endpoint
57
from mindee.documents.base import Document
68
from mindee.documents.invoice import Invoice
79
from mindee.documents.receipt import Receipt
810

911

1012
class FinancialDocument(Document):
13+
locale: Locale
14+
# orientation is only present on page-level, not document-level
15+
orientation: Optional[Orientation] = None
16+
1117
def __init__(
1218
self,
1319
api_prediction=None,
@@ -20,7 +26,6 @@ def __init__(
2026
:param input_file: Input object
2127
:param page_n: Page number for multi pages pdf input
2228
"""
23-
self.locale = None
2429
self.total_incl = None
2530
self.total_excl = None
2631
self.date = None
@@ -31,7 +36,6 @@ def __init__(
3136
self.payment_details = None
3237
self.company_number = None
3338
self.vat_number = None
34-
self.orientation = None
3539
self.total_tax = None
3640
self.time = None
3741

mindee/documents/invoice.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ class Invoice(Document):
2020
due_date: Date
2121
taxes: List[Tax] = []
2222
supplier: Field
23+
supplier_address: Field
24+
customer_name: Field
25+
customer_company_registration: Field
26+
customer_address: Field
2327
payment_details: List[PaymentDetails] = []
2428
company_number: List[Field] = []
2529
total_tax: Amount
@@ -51,6 +55,9 @@ def build_from_api_prediction(self, api_prediction: dict, page_n=0):
5155
:param page_n: Page number for multi pages pdf input
5256
:return: (void) set the object attributes with api prediction values
5357
"""
58+
if page_n is not None:
59+
self.orientation = Orientation(api_prediction["orientation"], page_n=page_n)
60+
5461
self.company_number = [
5562
Field(company_reg, extra_fields={"type"}, page_n=page_n)
5663
for company_reg in api_prediction["company_registration"]
@@ -65,9 +72,14 @@ def build_from_api_prediction(self, api_prediction: dict, page_n=0):
6572
self.locale = Locale(
6673
api_prediction["locale"], value_key="language", page_n=page_n
6774
)
68-
if page_n is not None:
69-
self.orientation = Orientation(api_prediction["orientation"], page_n=page_n)
7075
self.supplier = Field(api_prediction["supplier"], page_n=page_n)
76+
self.supplier_address = Field(api_prediction["supplier_address"], page_n=page_n)
77+
self.customer_name = Field(api_prediction["customer"], page_n=page_n)
78+
self.customer_company_registration = Field(
79+
api_prediction["customer_company_registration"], page_n=page_n
80+
)
81+
self.customer_address = Field(api_prediction["customer_address"], page_n=page_n)
82+
7183
self.taxes = [
7284
Tax(tax_prediction, page_n=page_n, value_key="value")
7385
for tax_prediction in api_prediction["taxes"]
@@ -93,16 +105,20 @@ def __str__(self) -> str:
93105
return (
94106
"-----Invoice data-----\n"
95107
f"Filename: {self.filename}\n"
96-
f"Invoice number: {self.invoice_number.value}\n"
97-
f"Total amount including taxes: {self.total_incl.value}\n"
98-
f"Total amount excluding taxes: {self.total_excl.value}\n"
99-
f"Invoice date: {self.invoice_date.value}\n"
100-
f"Invoice due date: {self.due_date.value}\n"
101-
f"Supplier name: {self.supplier.value}\n"
108+
f"Invoice number: {self.invoice_number}\n"
109+
f"Total amount including taxes: {self.total_incl}\n"
110+
f"Total amount excluding taxes: {self.total_excl}\n"
111+
f"Invoice date: {self.invoice_date}\n"
112+
f"Invoice due date: {self.due_date}\n"
113+
f"Supplier name: {self.supplier}\n"
114+
f"Supplier address: {self.supplier_address}\n"
115+
f"Customer name: {self.customer_name}\n"
116+
f"Customer company registration: {self.customer_company_registration}\n"
117+
f"Customer address: {self.customer_address}\n"
102118
f"Payment details: {payments}\n"
103119
f"Company numbers: {company_numbers}\n"
104120
f"Taxes: {taxes}\n"
105-
f"Total taxes: {self.total_tax.value}\n"
121+
f"Total taxes: {self.total_tax}\n"
106122
f"Locale: {self.locale}\n"
107123
"----------------------"
108124
)

mindee/documents/receipt.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ def build_from_api_prediction(self, api_prediction, page_n=0):
6565
:param page_n: Page number for multi pages pdf input
6666
:return: (void) set the object attributes with api prediction values
6767
"""
68+
if page_n is not None:
69+
self.orientation = Orientation(api_prediction["orientation"], page_n=page_n)
70+
6871
self.locale = Locale(api_prediction["locale"], page_n=page_n)
6972
self.total_incl = Amount(
7073
api_prediction["total_incl"], value_key="value", page_n=page_n
@@ -85,8 +88,6 @@ def build_from_api_prediction(self, api_prediction, page_n=0):
8588
)
8689
for tax_prediction in api_prediction["taxes"]
8790
]
88-
if page_n is not None:
89-
self.orientation = Orientation(api_prediction["orientation"], page_n=page_n)
9091
self.total_tax = Amount(
9192
{"value": None, "confidence": 0.0}, value_key="value", page_n=page_n
9293
)

mindee/fields/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def __init__(
1818
:param abstract_prediction: Prediction object from HTTP response
1919
:param value_key: Key to use in the abstract_prediction dict
2020
:param reconstructed: Bool for reconstructed object (not extracted in the API)
21-
:param page_n: Page number for multi pages pdf
21+
:param page_n: Page number for multi pages PDF
2222
:param extra_fields: extra field to get from the abstract_prediction and to set as attribute of the Field
2323
"""
2424
self.page_n = page_n

mindee/http.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
PLATFORM = get_platform()
1111
USER_AGENT = f"mindee-api-python@v{__version__} python-v{python_version} {PLATFORM}"
1212

13-
INVOICE_VERSION = "2"
13+
INVOICE_VERSION = "3"
1414
INVOICE_URL_NAME = "invoices"
1515

1616
RECEIPT_VERSION = "3"
@@ -146,8 +146,8 @@ def annotation_request(
146146
class InvoiceEndpoint(Endpoint):
147147
def __init__(self, api_key: Optional[str] = None):
148148
owner = "mindee"
149-
url_name = os.getenv("MINDEE_INVOICE_URL_NAME", INVOICE_URL_NAME)
150-
version = os.getenv("MINDEE_INVOICE_VERSION", INVOICE_VERSION)
149+
url_name = INVOICE_URL_NAME
150+
version = INVOICE_VERSION
151151
key_name = "invoice"
152152
super().__init__(
153153
owner=owner,
@@ -161,8 +161,8 @@ def __init__(self, api_key: Optional[str] = None):
161161
class ReceiptEndpoint(Endpoint):
162162
def __init__(self, api_key: Optional[str] = None):
163163
owner = "mindee"
164-
url_name = os.getenv("MINDEE_RECEIPT_URL_NAME", RECEIPT_URL_NAME)
165-
version = os.getenv("MINDEE_RECEIPT_VERSION", RECEIPT_VERSION)
164+
url_name = RECEIPT_URL_NAME
165+
version = RECEIPT_VERSION
166166
key_name = "receipt"
167167
super().__init__(
168168
owner=owner,
@@ -176,8 +176,8 @@ def __init__(self, api_key: Optional[str] = None):
176176
class PassportEndpoint(Endpoint):
177177
def __init__(self, api_key: Optional[str] = None):
178178
owner = "mindee"
179-
url_name = os.getenv("MINDEE_PASSPORT_URL_NAME", PASSPORT_URL_NAME)
180-
version = os.getenv("MINDEE_PASSPORT_VERSION", PASSPORT_VERSION)
179+
url_name = PASSPORT_URL_NAME
180+
version = PASSPORT_VERSION
181181
super().__init__(
182182
owner=owner,
183183
url_name=url_name,

mindee/response.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
from typing import Dict, Any
1+
from typing import Dict, Any, List
22

33
from mindee.logger import logger
44
from mindee.document_config import DocumentConfig
5+
from mindee.documents.base import TypeDocument, Document
56

67

78
def format_response(
@@ -57,9 +58,9 @@ def __init__(
5758
self,
5859
doc_config: DocumentConfig,
5960
http_response: dict,
60-
pages: list,
61+
pages: List[Document],
6162
document_type: str,
62-
document=None,
63+
document=TypeDocument,
6364
):
6465
"""
6566
:param http_response: HTTP response object

tests/data/expense_receipts/v3/receipt_all_na.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@
7474
},
7575
"locale": {
7676
"confidence": 0.15,
77-
"country": "FR",
78-
"currency": "EUR",
79-
"language": "fr",
80-
"value": "fr-FR"
77+
"country": null,
78+
"currency": null,
79+
"language": null,
80+
"value": null
8181
},
8282
"supplier": {
8383
"confidence": 0.0,

0 commit comments

Comments
 (0)