Skip to content

Commit

Permalink
[MIG] account_invoice_import_invoice2data: Migration to 16.0
Browse files Browse the repository at this point in the history
  • Loading branch information
V3st1g3 committed Dec 11, 2024
1 parent 91da522 commit d75e0fe
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 53 deletions.
10 changes: 5 additions & 5 deletions account_invoice_import_invoice2data/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ Account Invoice Import Invoice2data
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fedi-lightgray.png?logo=github
:target: https://github.com/OCA/edi/tree/14.0/account_invoice_import_invoice2data
:target: https://github.com/OCA/edi/tree/16.0/account_invoice_import_invoice2data
:alt: OCA/edi
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/edi-14-0/edi-14-0-account_invoice_import_invoice2data
:target: https://translation.odoo-community.org/projects/edi-16-0/edi-16-0-account_invoice_import_invoice2data
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/edi&target_branch=14.0
:target: https://runboat.odoo-community.org/builds?repo=OCA/edi&target_branch=16.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|
Expand Down Expand Up @@ -245,7 +245,7 @@ Bug Tracker
Bugs are tracked on `GitHub Issues <https://github.com/OCA/edi/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/edi/issues/new?body=module:%20account_invoice_import_invoice2data%0Aversion:%2014.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
`feedback <https://github.com/OCA/edi/issues/new?body=module:%20account_invoice_import_invoice2data%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Expand Down Expand Up @@ -286,6 +286,6 @@ Current `maintainers <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-alexis-via| |maintainer-bosd|

This module is part of the `OCA/edi <https://github.com/OCA/edi/tree/14.0/account_invoice_import_invoice2data>`_ project on GitHub.
This module is part of the `OCA/edi <https://github.com/OCA/edi/tree/16.0/account_invoice_import_invoice2data>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
2 changes: 1 addition & 1 deletion account_invoice_import_invoice2data/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

{
"name": "Account Invoice Import Invoice2data",
"version": "14.0.2.4.0",
"version": "16.0.1.0.0",
"category": "Accounting/Accounting",
"license": "AGPL-3",
"summary": "Import supplier invoices using the invoice2data lib",
Expand Down
2 changes: 1 addition & 1 deletion account_invoice_import_invoice2data/demo/demo_data.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<field name="zip">75008</field>
<field name="city">Paris</field>
<field name="country_id" ref="base.fr" />
<field name="website">http://www.free.fr</field>
<field name="website">https://www.free.fr</field>
<field name="vat">FR60421938861</field>
</record>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ <h1 class="title">Account Invoice Import Invoice2data</h1>
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:c7d1d2a38227a9bf81bc6132091b5285a4afe0b1280bea7541ebab212f57f16a
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/edi/tree/14.0/account_invoice_import_invoice2data"><img alt="OCA/edi" src="https://img.shields.io/badge/github-OCA%2Fedi-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/edi-14-0/edi-14-0-account_invoice_import_invoice2data"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/edi&amp;target_branch=14.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/edi/tree/16.0/account_invoice_import_invoice2data"><img alt="OCA/edi" src="https://img.shields.io/badge/github-OCA%2Fedi-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/edi-16-0/edi-16-0-account_invoice_import_invoice2data"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/edi&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This module is an extension of the module <em>account_invoice_import</em>: it adds support for regular PDF invoices i.e. PDF invoice that don’t have an embedded XML file. It uses the <a class="reference external" href="https://github.com/invoice-x/invoice2data">invoice2data library</a> which takes care of extracting the text of the PDF invoice, find an existing invoice template and execute the invoice template to extract the useful information from the invoice.</p>
<p>To know the full story behind the development of this module, read this <a class="reference external" href="http://www.akretion.com/blog/akretions-christmas-present-for-the-odoo-community">blog post</a>.</p>
<p>More information for creating the templates can be found in <a class="reference external" href="https://github.com/invoice-x/invoice2data/blob/master/TUTORIAL.md">tutorial of the invoice2data library</a>. The templates have to be created manually. An graphical template creator for odoo is a work in progress.</p>
Expand Down Expand Up @@ -570,7 +570,7 @@ <h1><a class="toc-backref" href="#toc-entry-7">Bug Tracker</a></h1>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/edi/issues">GitHub Issues</a>.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
<a class="reference external" href="https://github.com/OCA/edi/issues/new?body=module:%20account_invoice_import_invoice2data%0Aversion:%2014.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<a class="reference external" href="https://github.com/OCA/edi/issues/new?body=module:%20account_invoice_import_invoice2data%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
<div class="section" id="credits">
Expand Down Expand Up @@ -598,7 +598,7 @@ <h2><a class="toc-backref" href="#toc-entry-11">Maintainers</a></h2>
promote its widespread use.</p>
<p>Current <a class="reference external" href="https://odoo-community.org/page/maintainer-role">maintainers</a>:</p>
<p><a class="reference external image-reference" href="https://github.com/alexis-via"><img alt="alexis-via" src="https://github.com/alexis-via.png?size=40px" /></a> <a class="reference external image-reference" href="https://github.com/bosd"><img alt="bosd" src="https://github.com/bosd.png?size=40px" /></a></p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/edi/tree/14.0/account_invoice_import_invoice2data">OCA/edi</a> project on GitHub.</p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/edi/tree/16.0/account_invoice_import_invoice2data">OCA/edi</a> project on GitHub.</p>
<p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>
</div>
</div>
Expand Down
85 changes: 47 additions & 38 deletions account_invoice_import_invoice2data/tests/test_invoice_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
# @author: Alexis de Lattre <alexis.delattre@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import base64
import logging
from base64 import b64encode
from logging import getLogger
from unittest import mock

from odoo import fields
from odoo.tests.common import SavepointCase
from odoo.tests.common import TransactionCase
from odoo.tools import file_open, float_compare

# TODO v16: use
Expand All @@ -21,19 +21,21 @@
}


class TestInvoiceImport(SavepointCase):
class TestInvoiceImport(TransactionCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.env = cls.env(context=dict(cls.env.context, **DISABLED_MAIL_CONTEXT))
frtax = cls.env["account.tax"].create(
{
"name": "French VAT purchase 20.0%",
"description": "FR-VAT-buy-20.0",
"amount": 20,
"amount_type": "percent",
"type_tax_use": "purchase",
}
[
{
"name": "French VAT purchase 20.0%",
"description": "FR-VAT-buy-20.0",
"amount": 20,
"amount_type": "percent",
"type_tax_use": "purchase",
}
]
)
# Set this tax on Internet access product
internet_product = cls.env.ref(
Expand All @@ -44,25 +46,27 @@ def setUpClass(cls):
def test_have_invoice2data_unavailable(self):
with mock.patch.dict("sys.modules", {"invoice2data": None}):
with self.assertLogs("", level="DEBUG") as cm:
logging.getLogger("").debug("Cannot import invoice2data")
getLogger("").debug("Cannot import invoice2data")
self.assertEqual(cm.output, ["DEBUG:root:Cannot import invoice2data"])

def test_have_tesseract_unavailable(self):
with mock.patch.dict("sys.modules", {"tesseract": None}):
with self.assertLogs("", level="DEBUG") as cm:
logging.getLogger("").debug("Cannot import tesseract")
getLogger("").debug("Cannot import tesseract")
self.assertEqual(cm.output, ["DEBUG:root:Cannot import tesseract"])

def test_import_free_invoice(self):
filename = "invoice_free_fiber_201507.pdf"
f = file_open("account_invoice_import_invoice2data/tests/pdf/" + filename, "rb")
pdf_file = f.read()
pdf_file_b64 = base64.b64encode(pdf_file)
pdf_file_b64 = b64encode(pdf_file)
wiz = self.env["account.invoice.import"].create(
{
"invoice_file": pdf_file_b64,
"invoice_filename": filename,
}
[
{
"invoice_file": pdf_file_b64,
"invoice_filename": filename,
}
]
)
f.close()
wiz.import_invoice()
Expand Down Expand Up @@ -107,10 +111,12 @@ def test_import_free_invoice(self):

# New import with update of an existing draft invoice
wiz2 = self.env["account.invoice.import"].create(
{
"invoice_file": pdf_file_b64,
"invoice_filename": "invoice_free_fiber_201507.pdf",
}
[
{
"invoice_file": pdf_file_b64,
"invoice_filename": "invoice_free_fiber_201507.pdf",
}
]
)
action = wiz2.import_invoice()
self.assertEqual(action["res_model"], "account.invoice.import")
Expand All @@ -130,21 +136,24 @@ def test_import_free_invoice(self):
def test_import_azure_interior_invoice(self):
"""Function for testing almost all supported fields"""
filename = "AzureInterior.pdf"
invoice_file = file_open(

with file_open(
"account_invoice_import_invoice2data/tests/pdf/" + filename, "rb"
)
pdf_file = invoice_file.read()
pdf_file_b64 = base64.b64encode(pdf_file)
wiz = self.env["account.invoice.import"].create(
{
"invoice_file": pdf_file_b64,
"invoice_filename": filename,
}
)
invoice_file.close()
) as invoice_file:
pdf_file = invoice_file.read()
pdf_file_b64 = b64encode(pdf_file)
wiz = self.env["account.invoice.import"].create(
[
{
"invoice_file": pdf_file_b64,
"invoice_filename": filename,
}
]
)

wiz.import_invoice()
# create_invoice_action_button
wiz.create_invoice_action(origin="BOSD Import Vendor Bill wizard")
# wiz.create_invoice_action(origin="BOSD Import Vendor Bill wizard")
# Check result of invoice creation
invoices = self.env["account.move"].search(
[
Expand All @@ -167,7 +176,7 @@ def test_import_azure_interior_invoice(self):

self.assertEqual(
inv.narration,
"Due to global inflation our payment term has changed to 15 days.",
"<p>Due to global inflation our payment term has changed to 15 days.</p>",
)

# Following tests are disabled. Not yet implemented in account_invoice_import
Expand All @@ -179,7 +188,7 @@ def test_import_azure_interior_invoice(self):
self.assertEqual(iline.name, "--- Non Food ---")
self.assertEqual(iline.display_type, "line_section")
iline = inv.invoice_line_ids[1]
self.assertEqual(iline.name, "Beeswax XL\nAcme beeswax")
self.assertEqual(iline.name, "[17589684] Beeswax XL")
self.assertEqual(
iline.product_id,
self.env.ref("account_invoice_import_invoice2data.product_beeswax_xl"),
Expand All @@ -188,7 +197,7 @@ def test_import_azure_interior_invoice(self):
self.assertEqual(float_compare(iline.price_unit, 42.00, precision_digits=2), 0)

iline = inv.invoice_line_ids[2]
self.assertEqual(iline.name, "Office Chair")
self.assertEqual(iline.name, "[FURN_7777] Office Chair")
self.assertEqual(
iline.product_id,
self.env.ref("product.product_delivery_01"),
Expand All @@ -214,7 +223,7 @@ def test_import_azure_interior_invoice(self):
)
self.assertEqual(iline.display_type, "line_note")
iline = inv.invoice_line_ids[6]
self.assertEqual(iline.name, "Luxury Truffles")
self.assertEqual(iline.name, "[LUX_TRF] Luxury Truffles")
self.assertEqual(
iline.product_id,
self.env.ref("account_invoice_import_invoice2data.luxury_truffles"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def fallback_parse_pdf_invoice(self, file_data):

@api.model
def parse_invoice2data_taxes(self, line):
amount = 0.0
amount_type = False
taxes = []
type_code = "VAT"
price_include = False
Expand Down Expand Up @@ -69,10 +71,12 @@ def parse_invoice2data_taxes(self, line):
)
return taxes

def _clean_string(self, string):
@staticmethod
def _clean_string(string):
return re.sub(r"\W+", "", string)

def _clean_digits(self, string):
@staticmethod
def _clean_digits(string):
return re.sub(r"\D+", "", string)

@api.model
Expand All @@ -98,7 +102,9 @@ def invoice2data_parse_invoice(self, file_data):
invoice2data_res = extract_data(fileobj.name, templates=templates)
except Exception as e:
fileobj.close()
raise UserError(_("PDF Invoice parsing failed. Error message: %s") % e)
raise UserError(
_("PDF Invoice parsing failed. Error message: %s") % e
) from None
if not invoice2data_res:
if not shutil.which("tesseract"):
logger.warning(
Expand All @@ -116,7 +122,9 @@ def invoice2data_parse_invoice(self, file_data):
)
except Exception as e:
fileobj.close()
raise UserError(_("PDF Invoice parsing failed. Error message: %s") % e)
raise UserError(
_("PDF Invoice parsing failed. Error message: %s") % e
) from None
if not invoice2data_res:
fileobj.close()
return False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<li><b>regular PDF</b> file without any embedded XML file via the <a
href="https://github.com/invoice-x/invoice2data/"
target="_blank"
>invoice2data</a> library (reads the text of the PDF and get the information). It will work only if the invoice2data library has a template for the invoices of that supplier.</li>
>invoice2data</a> library (reads the text of the PDF and get the information). It will work only if the invoice2data library has a template for the invoices for that supplier.</li>
</ul>
</field>
</record>
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# generated from manifests external_dependencies
dateparser
dotty-dict
factur-x<=3.1
invoice2data
phonenumbers
pydifact
pytz
Expand Down
6 changes: 6 additions & 0 deletions setup/account_invoice_import_invoice2data/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)

0 comments on commit d75e0fe

Please sign in to comment.