Skip to content

[17.0][IMP] base_import_pdf_by_template_account: Define a class for the import process #1148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions base_import_pdf_by_template_account/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@
class AccountMove(models.Model):
_inherit = "account.move"

def _import_base_import_pdf_by_template(self, file_data, new=False):
def _import_base_import_pdf_by_template(self, invoice, file_data, new=False):
"""Method to process the PDF with base_import_pdf_by_template_account
if any template is available (similar to account_edi_ubl_cii)."""
template_model = self.env["base.import.pdf.template"].with_company(
self.company_id.id
invoice.company_id.id
)
total_templates = template_model.search_count([("model", "=", self._name)])
total_templates = template_model.search_count([("model", "=", invoice._name)])
if total_templates == 0:
return False
self.move_type = (
"in_invoice" if self.journal_id.type == "purchase" else "out_invoice"
invoice.move_type = (
"in_invoice" if invoice.journal_id.type == "purchase" else "out_invoice"
)
wizard = self.env["wizard.base.import.pdf.upload"].create(
{
"model": self._name,
"record_ref": f"{self._name},{self.id}",
"model": invoice._name,
"record_ref": f"{invoice._name},{invoice.id}",
"attachment_ids": [(6, 0, file_data["attachment"].ids)],
}
)
Expand All @@ -30,10 +30,5 @@ def _import_base_import_pdf_by_template(self, file_data, new=False):

def _get_edi_decoder(self, file_data, new=False):
if file_data["type"] == "pdf":
res = self._import_base_import_pdf_by_template(file_data, new)
if res:
# If everything worked correctly, we return False to avoid what
# is done in the _extend_with_attachments() method of account
# with the result of the _get_edi_decoder() method.
return False
return self._import_base_import_pdf_by_template
return super()._get_edi_decoder(file_data, new=new)
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from base64 import b64encode
from os import path

from odoo.tools import mute_logger

from odoo.addons.base.tests.common import BaseCommon


Expand Down Expand Up @@ -66,7 +68,7 @@ def setUpClass(cls):
{"seller_ids": [(5, 0, 0)]}
)
# pylint: disable=W1401
cls.env["base.import.pdf.template"].create(
cls.template = cls.env["base.import.pdf.template"].create(
{
"name": "Invoices Tecnativa",
"model_id": cls.env.ref("account.model_account_move").id,
Expand Down Expand Up @@ -232,3 +234,35 @@ def test_account_move_edi_decoder(self):
self.assertEqual(len(invoice.attachment_ids), 1)
self.assertEqual(attachment, invoice.attachment_ids)
self._test_account_invoice_tecnativa_data(invoice)

def test_account_move_from_alias_with_template(self):
invoice = (
self.env["account.move"]
.with_context(default_move_type="in_invoice")
.create({})
)
attachment = self._create_ir_attachment("account_invoice_tecnativa.pdf")
res = invoice.with_context(from_alias=True)._check_and_decode_attachment(
attachment
)
self.assertEqual(res, {attachment: invoice})
self.assertTrue(attachment.exists())
self.assertEqual(len(invoice.attachment_ids), 1)
self.assertEqual(attachment, invoice.attachment_ids)
self._test_account_invoice_tecnativa_data(invoice)

@mute_logger("odoo.models.unlink")
def test_account_move_from_alias_without_template(self):
invoice = (
self.env["account.move"]
.with_context(default_move_type="in_invoice")
.create({})
)
attachment = self._create_ir_attachment("account_invoice_tecnativa.pdf")
self.template.unlink()
res = invoice.with_context(from_alias=True)._check_and_decode_attachment(
attachment
)
self.assertEqual(res, {attachment: invoice})
self.assertTrue(attachment.exists())
self.assertEqual(len(invoice.invoice_line_ids), 0)