Skip to content

Commit

Permalink
chg: ✨ Invoice v2 upgrade
Browse files Browse the repository at this point in the history
chg: ✨ Invoice v2 upgrade
  • Loading branch information
jonathanMindee authored Dec 3, 2020
2 parents ccea204 + 1953ea3 commit 64388aa
Show file tree
Hide file tree
Showing 12 changed files with 780 additions and 95 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Mindee python SDK

## v1.1.0 (2020-12-02)

### Chg

* Updated invoice API endpoint to V2

## v1.0.2 (2020-12-01)

### Fix
Expand Down
2 changes: 1 addition & 1 deletion mindee/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def parse_invoice(
self,
file,
input_type="path",
version="1",
version="2",
cut_pdf=True,
include_words=False
):
Expand Down
2 changes: 0 additions & 2 deletions mindee/documents/financial_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ def build_from_api_prediction(self, api_prediction, input_file, page_n=0):
self.company_number = invoice.company_number
self.orientation = invoice.orientation
self.total_tax = invoice.total_tax
self.vat_number = invoice.vat_number
self.time = Field({"value": None, "probability": 0.})
else:
receipt = Receipt(api_prediction, input_file, page_n=page_n)
Expand All @@ -134,7 +133,6 @@ def build_from_api_prediction(self, api_prediction, input_file, page_n=0):
self.merchant_name = receipt.merchant_name
self.time = receipt.time
self.total_tax = receipt.total_tax
self.vat_number = Field({"value": None, "probability": 0.})
self.invoice_number = Field({"value": None, "probability": 0.})
self.payment_details = Field({"value": None, "probability": 0.})
self.company_number = Field({"value": None, "probability": 0.})
Expand Down
34 changes: 21 additions & 13 deletions mindee/documents/invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from mindee.fields.amount import Amount
from mindee.fields.locale import Locale
from mindee.fields.orientation import Orientation
from mindee.fields.payment_details import PaymentDetails
from mindee.fields.tax import Tax
from mindee.http import request
from mindee.benchmark import Benchmark
Expand Down Expand Up @@ -59,7 +60,6 @@ def __init__(
self.supplier = None
self.payment_details = None
self.company_number = None
self.vat_number = None
self.orientation = None
self.total_tax = None

Expand All @@ -82,7 +82,6 @@ def __init__(
self.invoice_number = Field({"value": invoice_number}, value_key="value", page_n=page_n)
self.payment_details = Field({"value": payment_details}, value_key="value", page_n=page_n)
self.company_number = Field({"value": company_number}, value_key="value", page_n=page_n)
self.vat_number = Field({"value": vat_number}, value_key="value", page_n=page_n)

# Invoke Document constructor
super(Invoice, self).__init__(input_file)
Expand All @@ -99,18 +98,27 @@ def build_from_api_prediction(self, api_prediction, page_n=0):
:param page_n: Page number for multi pages pdf input
:return: (void) set the object attributes with api prediction values
"""
self.locale = Locale(api_prediction["locale"], value_key="language", page_n=page_n)
self.total_incl = Amount(api_prediction["total_incl"], page_n=page_n)
self.total_excl = Amount(api_prediction["total_excl"], page_n=page_n)
self.invoice_date = Date(api_prediction["invoice_date"], page_n=page_n)
self.company_number = [
Field(company_reg, extra_fields={"type"}, page_n=page_n) for company_reg in
api_prediction["company_registration"]
]
self.invoice_date = Date(api_prediction["date"], value_key="value", page_n=page_n)
self.due_date = Date(api_prediction["due_date"], value_key="value", page_n=page_n)
self.invoice_number = Field(api_prediction["invoice_number"], page_n=page_n)
self.due_date = Date(api_prediction["due_date"], page_n=page_n)
self.taxes = [Tax(tax_prediction, page_n=page_n) for tax_prediction in api_prediction["taxes"]]
self.supplier = Field(api_prediction["supplier"], page_n=page_n)
self.payment_details = Field(api_prediction["payment_details"], value_key="iban", page_n=page_n)
self.vat_number = Field(api_prediction["tax_id"], page_n=page_n)
self.company_number = Field(api_prediction["company_number"], extra_fields={"type"}, page_n=page_n)
self.locale = Locale(api_prediction["locale"], value_key="language", page_n=page_n)
self.orientation = Orientation(api_prediction["orientation"], page_n=page_n)
self.supplier = Field(api_prediction["supplier"], page_n=page_n)
self.taxes = [
Tax(tax_prediction, page_n=page_n, value_key="value") for tax_prediction in api_prediction["taxes"]
]
self.payment_details = [
PaymentDetails(
payment_detail,
page_n=page_n
) for payment_detail in api_prediction["payment_details"]
]
self.total_incl = Amount(api_prediction["total_incl"], value_key="value", page_n=page_n)
self.total_excl = Amount(api_prediction["total_excl"], value_key="value", page_n=page_n)
self.total_tax = Amount({"value": None, "probability": 0.}, value_key="value", page_n=page_n)

def __str__(self):
Expand Down Expand Up @@ -162,7 +170,7 @@ def request(
input_file,
base_url,
invoice_token=None,
version="1",
version="2",
include_words=False
):
"""
Expand Down
86 changes: 86 additions & 0 deletions mindee/fields/payment_details.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
from mindee.fields import Field


class PaymentDetails(Field):
def __init__(
self,
payment_details_prediction,
value_key="iban",
account_number_key="account_number",
iban_key="iban",
routing_number_key="routing_number",
swift_key="swift",
reconstructed=False,
page_n=None
):
"""
:param payment_details_prediction: Payment detail prediction object from HTTP response
:param value_key: Corresponds to iban
:param account_number_key: Key to use for getting the account number in the payment_details_prediction dict
:param iban_key: Key to use for getting the IBAN in the payment_details_prediction dict
:param routing_number_key: Key to use for getting the Routing number in the payment_details_prediction dict
:param swift_key: Key to use for getting the SWIFT in the payment_details_prediction dict
:param reconstructed: Bool for reconstructed object (not extracted in the API)
:param page_n: Page number for multi pages pdf
"""
super(PaymentDetails, self).__init__(
payment_details_prediction,
value_key=value_key,
reconstructed=reconstructed,
page_n=page_n
)

self.account_number_key = None
self.iban_key = None
self.routing_number_key = None
self.swift_key = None

try:
assert type(payment_details_prediction[account_number_key]) == str
self.account_number = str(payment_details_prediction[account_number_key])
if self.account_number == "N/A":
self.account_number = None
except:
self.account_number = None

try:
assert type(payment_details_prediction[iban_key]) == str
self.iban = str(payment_details_prediction[iban_key])
if self.iban == "N/A":
self.iban = None
except:
self.iban = None

try:
assert type(payment_details_prediction[routing_number_key]) == str
self.routing_number = str(payment_details_prediction[routing_number_key])
if self.routing_number == "N/A":
self.routing_number = None
except:
self.routing_number = None

try:
assert type(payment_details_prediction[swift_key]) == str
self.swift = str(payment_details_prediction[swift_key])
if self.swift == "N/A":
self.swift = None
except:
self.swift = None

def __str__(self):
payment_str = ""
if self.account_number is not None:
payment_str += str(self.account_number)+"; "

if self.iban is not None:
payment_str += str(self.iban)+"; "

if self.routing_number is not None:
payment_str += str(self.routing_number)+"; "

if self.swift is not None:
payment_str += str(self.swift)+"; "

return payment_str


2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def make_requirements_list(file="requirements.txt", only_regular=True):
python_requires=">=3.0",
name=f"{PACKAGE_NAME}",
description="Mindee API helper library for python",
version="v1.0.1",
version="v1.1.0",
long_description=long_description,
long_description_content_type="text/markdown",
url=GIT_URL,
Expand Down
Loading

0 comments on commit 64388aa

Please sign in to comment.