Skip to content

Commit

Permalink
account_invoice_import: improve handling of simple PDF invoices
Browse files Browse the repository at this point in the history
Implement what is described in issue OCA#610:
- account_invoice_import_simple_pdf and account_invoice_import_invoice2data can now be use
d together
- when importing an invoice for an unknown partner, it creates an empty invoice with PDF a
s attachment
- when importing an invoice via mail gateway and the from email is set
on a partner that doesn't have any import config, create a supplier
invoice with PDF as attachment and with that partner
- when importing an invoice via mail gateway and the from email is set
on a partner that has a single-line import config, create a supplier
invoice with PDF as attachment, that partner and 1 invoice line with price_unit=0.
  • Loading branch information
alexis-via authored and kevinxdev committed Dec 26, 2022
1 parent 28fbd5c commit 5f44860
Show file tree
Hide file tree
Showing 4 changed files with 339 additions and 148 deletions.
Binary file not shown.
107 changes: 107 additions & 0 deletions account_invoice_import/tests/test_invoice_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@

from odoo import fields
from odoo.tests.common import SavepointCase
from odoo.tools import file_open, float_is_zero


class TestInvoiceImport(SavepointCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.company = cls.env.ref("base.main_company")
cls.company.invoice_import_email = "alexis.delattre@testme.com"
cls.expense_account = cls.env["account.account"].create(
{
"code": "612AII",
Expand Down Expand Up @@ -96,6 +98,35 @@ def setUpClass(cls):
"company_id": cls.company.id,
}
)
cls.partner_with_email = cls.env["res.partner"].create(
{
"is_company": True,
"name": "AgroMilk",
"email": "invoicing@agromilk.com",
"country_id": cls.env.ref("base.fr").id,
}
)
cls.partner_with_email_with_inv_config = cls.env["res.partner"].create(
{
"is_company": True,
"name": "Anevia",
"email": "invoicing@anevia.com",
"country_id": cls.env.ref("base.fr").id,
"invoice_import_ids": [
(
0,
0,
{
"name": "Import config for Anevia",
"company_id": cls.company.id,
"invoice_line_method": "1line_static_product",
"static_product_id": cls.product.id,
"label": "Flamingo 220S",
},
)
],
}
)

def test_import_in_invoice(self):
parsed_inv = {
Expand Down Expand Up @@ -268,3 +299,79 @@ def test_email_gateway_multi_comp_none_matching(self):
"No destination found for message_id ="
)
self.assertIn(expected_msg, watcher.output[0])

def prepare_email_with_attachment(self, sender_email):
file_name = "unknown_invoice.pdf"
file_path = "account_invoice_import/tests/pdf/%s" % file_name
with file_open(file_path, "rb") as f:
pdf_file = f.read()
msg_dict = {
"email_from": '"My supplier" <%s>' % sender_email,
"to": self.company.invoice_import_email,
"subject": "Invoice n°1242",
"body": "Please find enclosed your PDF invoice",
"message_id": "<v0214040cad98743824@foo.com>",
"attachments": [
self.env["mail.thread"]._Attachment(file_name, pdf_file, {})
],
}
return msg_dict

def test_email_no_partner_match(self):
sender_email = "invoicing@unknownsupplier.com"
msg_dict = self.prepare_email_with_attachment(sender_email)
self.env["account.invoice.import"].message_new(msg_dict)
move = self.env["account.move"].search(
[
("company_id", "=", self.company.id),
("move_type", "=", "in_invoice"),
("invoice_source_email", "like", sender_email),
("state", "=", "draft"),
]
)
self.assertEqual(len(move), 1)
self.assertFalse(move.partner_id)
self.assertTrue(self.company.currency_id.is_zero(move.amount_total))
self.assertFalse(move.invoice_date)

def test_email_partner_no_invoice_config(self):
sender_email = self.partner_with_email.email
msg_dict = self.prepare_email_with_attachment(sender_email)
self.env["account.invoice.import"].message_new(msg_dict)
move = self.env["account.move"].search(
[
("company_id", "=", self.company.id),
("move_type", "=", "in_invoice"),
("partner_id", "=", self.partner_with_email.id),
("state", "=", "draft"),
]
)
self.assertEqual(len(move), 1)
self.assertTrue(self.company.currency_id.is_zero(move.amount_total))
self.assertFalse(move.invoice_date)
self.assertFalse(move.invoice_line_ids)

def test_email_partner_invoice_config(self):
partner = self.partner_with_email_with_inv_config
sender_email = partner.email
msg_dict = self.prepare_email_with_attachment(sender_email)
self.env["account.invoice.import"].message_new(msg_dict)
move = self.env["account.move"].search(
[
("company_id", "=", self.company.id),
("move_type", "=", "in_invoice"),
("partner_id", "=", partner.id),
("state", "=", "draft"),
]
)
self.assertEqual(len(move), 1)
self.assertTrue(self.company.currency_id.is_zero(move.amount_total))
self.assertFalse(move.invoice_date)
self.assertEqual(len(move.invoice_line_ids), 1)
iline = move.invoice_line_ids
self.assertEqual(iline.product_id.id, self.product.id)
self.assertEqual(iline.quantity, 1)
self.assertEqual(iline.name, partner.invoice_import_ids[0].label)
price_prec = self.env["decimal.precision"].precision_get("Product Price")
self.assertTrue(float_is_zero(iline.price_unit, precision_digits=price_prec))
self.assertTrue(self.company.currency_id.is_zero(iline.price_subtotal))
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@
name="label"
attrs="{'invisible': [('invoice_line_method', 'not in', ('1line_no_product', '1line_static_product'))]}"
/>
<field name="journal_id" />
<field
name="journal_id"
context="{'default_type': 'purchase', 'default_company_id': company_id}"
/>
</group>
</sheet>
</form>
Expand Down
Loading

0 comments on commit 5f44860

Please sign in to comment.