diff --git a/base_import_pdf_by_template/wizards/wizard_base_import_pdf_upload.py b/base_import_pdf_by_template/wizards/wizard_base_import_pdf_upload.py index 707d72248e..3caa0ca58a 100644 --- a/base_import_pdf_by_template/wizards/wizard_base_import_pdf_upload.py +++ b/base_import_pdf_by_template/wizards/wizard_base_import_pdf_upload.py @@ -16,6 +16,7 @@ class WizardBaseImportPdfUpload(models.TransientModel): _description = "Wizard Base Import Pdf Upload" model = fields.Char() + record_ref = fields.Reference(selection="_selection_reference_value") attachment_ids = fields.Many2many(comodel_name="ir.attachment", string="Files") allowed_template_ids = fields.Many2many( comodel_name="base.import.pdf.template", compute="_compute_allowed_template_ids" @@ -26,6 +27,15 @@ class WizardBaseImportPdfUpload(models.TransientModel): inverse_name="parent_id", ) + @api.model + def _selection_reference_value(self): + models = ( + self.env["ir.model"] + .sudo() + .search([("transient", "=", False)], order="name asc") + ) + return [(model.model, model.name) for model in models] + @api.depends("model") def _compute_allowed_template_ids(self): template_model = self.env["base.import.pdf.template"] @@ -179,6 +189,7 @@ def _process_form(self): text = self.data template = self.template_id model = self.env[template.model] + model = self.parent_id.record_ref or self.env[template.model] ctx = template._prepare_ctx_from_model(template.model) model_form = Form(model.with_context(**ctx)) # Set the values of the header in Form @@ -216,9 +227,16 @@ def _process_form(self): for key in ctx: if key.startswith("default_"): field = key.replace("default_", "") - if field in vals: + if field in vals and not self.parent_id.record_ref: + vals.update({field: ctx[key]}) + elif self.parent_id.record_ref: vals.update({field: ctx[key]}) - record = model.with_context(**ctx).create(vals) + # Create or update + if self.parent_id.record_ref: + model.with_context(**ctx).write(vals) + record = self.parent_id.record_ref + else: + record = model.with_context(**ctx).create(vals) except AssertionError as err: raise UserError(err) from err if self.log_text: diff --git a/base_import_pdf_by_template_account/README.rst b/base_import_pdf_by_template_account/README.rst new file mode 100644 index 0000000000..a94a7a01d5 --- /dev/null +++ b/base_import_pdf_by_template_account/README.rst @@ -0,0 +1,88 @@ +=================================== +Base Import Pdf by Template Account +=================================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:6b053ca4747568743800079faebd557c69331b093cf1a6bc25fa9886d022dd66 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :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/17.0/base_import_pdf_by_template_account + :alt: OCA/edi +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/edi-17-0/edi-17-0-base_import_pdf_by_template_account + :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=17.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Added support for account to process the PDF attached to the invoice +when creating the invoice from an email alias. + +**Table of contents** + +.. contents:: + :local: + +Bug Tracker +=========== + +Bugs are tracked on `GitHub 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 `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* Tecnativa + +Contributors +------------ + +- `Tecnativa `__: + + - Víctor Martínez + - Pedro M. Baeza + +Maintainers +----------- + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +.. |maintainer-victoralmau| image:: https://github.com/victoralmau.png?size=40px + :target: https://github.com/victoralmau + :alt: victoralmau + +Current `maintainer `__: + +|maintainer-victoralmau| + +This module is part of the `OCA/edi `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/base_import_pdf_by_template_account/__init__.py b/base_import_pdf_by_template_account/__init__.py new file mode 100644 index 0000000000..0650744f6b --- /dev/null +++ b/base_import_pdf_by_template_account/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/base_import_pdf_by_template_account/__manifest__.py b/base_import_pdf_by_template_account/__manifest__.py new file mode 100644 index 0000000000..24a86622fd --- /dev/null +++ b/base_import_pdf_by_template_account/__manifest__.py @@ -0,0 +1,15 @@ +# Copyright 2024 Tecnativa - Víctor Martínez +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +{ + "name": "Base Import Pdf by Template Account", + "version": "17.0.1.0.0", + "website": "https://github.com/OCA/edi", + "author": "Tecnativa, Odoo Community Association (OCA)", + "license": "AGPL-3", + "depends": ["account", "base_import_pdf_by_template"], + "installable": True, + "demo": [ + "demo/base_import_pdf_template.xml", + ], + "maintainers": ["victoralmau"], +} diff --git a/base_import_pdf_by_template_account/demo/base_import_pdf_template.xml b/base_import_pdf_by_template_account/demo/base_import_pdf_template.xml new file mode 100644 index 0000000000..47f37c6a57 --- /dev/null +++ b/base_import_pdf_by_template_account/demo/base_import_pdf_template.xml @@ -0,0 +1,155 @@ + + + + Test Generic Product + GENERIC + + + + Tecnativa + + + + CONS_0001 + + + Test Rotulador + ROTULADOR + + + + + + CONS_0002 + + + Test Boligrafo + BOLIGRAFO + + + + + + FURN_0003 + + + Test Leds + LEDS + + + + + + E-COM10 + + + Test Plastificadora + PLASTIFICADORA + + + + + + FURN_8621 + + + Test Lmaninas + LAMINAS + + + + + + FURN_7023 + + + Test Trituradora + TRITURADORA + + + + + Invoices Tecnativa + + + + Producto,Cantidad,Precio + + + + header + + fixed + + + + + header + + fixed + + + + + lines + + 0 + \[([A-Z\d]+[_|-][A-Z\d]+)\] + variable + + + + + + + lines + + 1 + \[[A-Z\d]+[_|-][A-Z\d]+\] [a-zA-Záí]* ([0-9]{1,3}) + variable + + + + lines + + 2 + \[[A-Z\d]+[_|-][A-Z\d]+\] [a-zA-Záí]* [0-9]{1,3} ([0-9]{1,3}.[0-9]{2}) + variable + + + diff --git a/base_import_pdf_by_template_account/models/__init__.py b/base_import_pdf_by_template_account/models/__init__.py new file mode 100644 index 0000000000..9c0a421385 --- /dev/null +++ b/base_import_pdf_by_template_account/models/__init__.py @@ -0,0 +1 @@ +from . import account_move diff --git a/base_import_pdf_by_template_account/models/account_move.py b/base_import_pdf_by_template_account/models/account_move.py new file mode 100644 index 0000000000..20bfbde562 --- /dev/null +++ b/base_import_pdf_by_template_account/models/account_move.py @@ -0,0 +1,47 @@ +# Copyright 2024 Tecnativa - Víctor Martínez +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from base64 import b64encode + +from odoo import models + + +class AccountMove(models.Model): + _inherit = "account.move" + + def _import_base_import_pdf_by_template(self, 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 + ) + total_templates = template_model.search_count([("model", "=", self._name)]) + if total_templates == 0: + return False + # We need to create the attachment that we will use in the wizard because it + # has not been created yet. + attachment = self.env["ir.attachment"].create( + {"name": file_data["filename"], "datas": b64encode(file_data["content"])} + ) + self.move_type = ( + "in_invoice" if self.journal_id.type == "purchase" else "out_invoice" + ) + # return self._import_record_base_import_pdf_by_template(invoice, attachment) + wizard = self.env["wizard.base.import.pdf.upload"].create( + { + "model": self._name, + "record_ref": f"{self._name},{self.id}", + "attachment_ids": [(6, 0, attachment.ids)], + } + ) + wizard.action_process() + return True + + 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 super()._get_edi_decoder(file_data, new=new) diff --git a/base_import_pdf_by_template_account/pyproject.toml b/base_import_pdf_by_template_account/pyproject.toml new file mode 100644 index 0000000000..4231d0cccb --- /dev/null +++ b/base_import_pdf_by_template_account/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/base_import_pdf_by_template_account/readme/CONTRIBUTORS.md b/base_import_pdf_by_template_account/readme/CONTRIBUTORS.md new file mode 100644 index 0000000000..5fee390427 --- /dev/null +++ b/base_import_pdf_by_template_account/readme/CONTRIBUTORS.md @@ -0,0 +1,3 @@ +- [Tecnativa](https://www.tecnativa.com): + - Víctor Martínez + - Pedro M. Baeza diff --git a/base_import_pdf_by_template_account/readme/DESCRIPTION.md b/base_import_pdf_by_template_account/readme/DESCRIPTION.md new file mode 100644 index 0000000000..2fe7e10340 --- /dev/null +++ b/base_import_pdf_by_template_account/readme/DESCRIPTION.md @@ -0,0 +1 @@ +Added support for account to process the PDF attached to the invoice when creating the invoice from an email alias. diff --git a/base_import_pdf_by_template_account/static/description/icon.png b/base_import_pdf_by_template_account/static/description/icon.png new file mode 100644 index 0000000000..3a0328b516 Binary files /dev/null and b/base_import_pdf_by_template_account/static/description/icon.png differ diff --git a/base_import_pdf_by_template_account/static/description/index.html b/base_import_pdf_by_template_account/static/description/index.html new file mode 100644 index 0000000000..dfaf391644 --- /dev/null +++ b/base_import_pdf_by_template_account/static/description/index.html @@ -0,0 +1,430 @@ + + + + + +Base Import Pdf by Template Account + + + +
+

Base Import Pdf by Template Account

+ + +

Beta License: AGPL-3 OCA/edi Translate me on Weblate Try me on Runboat

+

Added support for account to process the PDF attached to the invoice +when creating the invoice from an email alias.

+

Table of contents

+ +
+

Bug Tracker

+

Bugs are tracked on GitHub 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.

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • Tecnativa
  • +
+
+
+

Contributors

+
    +
  • Tecnativa:
      +
    • Víctor Martínez
    • +
    • Pedro M. Baeza
    • +
    +
  • +
+
+
+

Maintainers

+

This module is maintained by the OCA.

+ +Odoo Community Association + +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

Current maintainer:

+

victoralmau

+

This module is part of the OCA/edi project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/base_import_pdf_by_template_account/tests/__init__.py b/base_import_pdf_by_template_account/tests/__init__.py new file mode 100644 index 0000000000..0622122ff2 --- /dev/null +++ b/base_import_pdf_by_template_account/tests/__init__.py @@ -0,0 +1 @@ +from . import test_base_import_pdf_by_template_account diff --git a/test_base_import_pdf_by_template/tests/data/account_invoice_tecnativa.pdf b/base_import_pdf_by_template_account/tests/data/account_invoice_tecnativa.pdf similarity index 100% rename from test_base_import_pdf_by_template/tests/data/account_invoice_tecnativa.pdf rename to base_import_pdf_by_template_account/tests/data/account_invoice_tecnativa.pdf diff --git a/base_import_pdf_by_template_account/tests/test_base_import_pdf_by_template_account.py b/base_import_pdf_by_template_account/tests/test_base_import_pdf_by_template_account.py new file mode 100644 index 0000000000..3028e56ecf --- /dev/null +++ b/base_import_pdf_by_template_account/tests/test_base_import_pdf_by_template_account.py @@ -0,0 +1,88 @@ +# Copyright 2024 Tecnativa - Víctor Martínez +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from base64 import b64encode +from os import path + +from odoo.addons.base.tests.common import BaseCommon + + +class TestBaseImportPdfByTemplateAccount(BaseCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + generic_product = cls.env.ref( + "base_import_pdf_by_template_account.generic_product" + ) + cls.partner_tecnativa = cls.env.ref( + "base_import_pdf_by_template_account.partner_tecnativa" + ) + cls.template_invoice_tecnativa = cls.env.ref( + "base_import_pdf_by_template_account.invoice_tecnativa" + ) + cls.template_invoice_tecnativa.write( + {"auto_detect_pattern": r"(?<=B 8 7 5 3 0 4 3 2)[\S\s]*"} + ) + product_model_name = "product.product" + cls.env.ref( + "base_import_pdf_by_template_account.invoice_tecnativa_line_product_id" + ).write({"default_value": f"{product_model_name},{generic_product.id}"}) + cls.journal = cls.env["account.journal"].search( + [("type", "=", "purchase"), ("company_id", "=", cls.env.company.id)], + limit=1, + ) + + def _data_file(self, filename, encoding=None): + filename = "data/" + filename + mode = "rt" if encoding else "rb" + with open(path.join(path.dirname(__file__), filename), mode) as file: + data = file.read() + return b64encode(data) + + def _create_ir_attachment(self, filename): + return self.env["ir.attachment"].create( + { + "name": filename, + "datas": self._data_file(filename), + } + ) + + def _create_wizard_base_import_pdf_upload(self, model, attachment): + wizard = self.env["wizard.base.import.pdf.upload"].create( + { + "model": model, + "attachment_ids": attachment.ids, + } + ) + return wizard + + def _test_account_invoice_tecnativa_data(self, record): + self.assertEqual(record.move_type, "in_invoice") + self.assertEqual(record.partner_id, self.partner_tecnativa) + self.assertEqual(len(record.invoice_line_ids), 6) + self.assertEqual(sum(record.invoice_line_ids.mapped("quantity")), 665) + default_codes = record.invoice_line_ids.mapped("product_id.default_code") + self.assertIn("ROTULADOR", default_codes) + self.assertIn("BOLIGRAFO", default_codes) + self.assertIn("LEDS", default_codes) + self.assertIn("PLASTIFICADORA", default_codes) + self.assertIn("LAMINAS", default_codes) + self.assertIn("TRITURADORA", default_codes) + self.assertIn("100.25", record.message_ids[-1].body) + + def test_account_invoice_tecnativa(self): + attachment = self._create_ir_attachment("account_invoice_tecnativa.pdf") + wizard = self._create_wizard_base_import_pdf_upload("account.move", attachment) + res = wizard.action_process() + self.assertEqual(res["res_model"], "account.move") + record = self.env[res["res_model"]].browse(res["res_id"]) + self.assertIn(attachment, record.attachment_ids) + self._test_account_invoice_tecnativa_data(record) + + def test_account_move_edi_decoder(self): + attachment = self._create_ir_attachment("account_invoice_tecnativa.pdf") + invoice = self.journal.with_context( + default_journal_id=self.journal.id + )._create_document_from_attachment(attachment.id) + self.assertIn(attachment, invoice.attachment_ids) + self._test_account_invoice_tecnativa_data(invoice) diff --git a/test_base_import_pdf_by_template/demo/base_import_pdf_template.xml b/test_base_import_pdf_by_template/demo/base_import_pdf_template.xml index 442b1cf6b9..8d933e7429 100644 --- a/test_base_import_pdf_by_template/demo/base_import_pdf_template.xml +++ b/test_base_import_pdf_by_template/demo/base_import_pdf_template.xml @@ -120,150 +120,4 @@ variable - - - Tecnativa - - - - CONS_0001 - - - Test Rotulador - ROTULADOR - - - - - - CONS_0002 - - - Test Boligrafo - BOLIGRAFO - - - - - - FURN_0003 - - - Test Leds - LEDS - - - - - - E-COM10 - - - Test Plastificadora - PLASTIFICADORA - - - - - - FURN_8621 - - - Test Lmaninas - LAMINAS - - - - - - FURN_7023 - - - Test Trituradora - TRITURADORA - - - - - Invoices Tecnativa - - - - Producto,Cantidad,Precio - - - - header - - fixed - - - - - header - - fixed - - - - - lines - - 0 - \[([A-Z\d]+[_|-][A-Z\d]+)\] - variable - - - - - - - lines - - 1 - (^\d{1,3}\n)[0-9]{1,2} - variable - - - - lines - - 2 - (^\d{1,3}\.(?!00)\d{2}\n) - variable - - diff --git a/test_base_import_pdf_by_template/hooks.py b/test_base_import_pdf_by_template/hooks.py index d93c459db7..7cde6a0c3c 100644 --- a/test_base_import_pdf_by_template/hooks.py +++ b/test_base_import_pdf_by_template/hooks.py @@ -11,12 +11,3 @@ def post_init_hook(env): env.ref("test_base_import_pdf_by_template.po_decathlon_line_product_id").write( {"default_value": f"{product_model_name},{generic_product.id}"} ) - template_invoice_tecnativa = env.ref( - "test_base_import_pdf_by_template.invoice_tecnativa" - ) - template_invoice_tecnativa.write( - {"auto_detect_pattern": r"(?<=B 8 7 5 3 0 4 3 2)[\S\s]*"} - ) - env.ref("test_base_import_pdf_by_template.invoice_tecnativa_line_product_id").write( - {"default_value": f"{product_model_name},{generic_product.id}"} - ) diff --git a/test_base_import_pdf_by_template/tests/test_base_import_pdf_by_template.py b/test_base_import_pdf_by_template/tests/test_base_import_pdf_by_template.py index 4822a0f883..a873b695f1 100644 --- a/test_base_import_pdf_by_template/tests/test_base_import_pdf_by_template.py +++ b/test_base_import_pdf_by_template/tests/test_base_import_pdf_by_template.py @@ -17,12 +17,6 @@ def setUpClass(cls): cls.template_po_decathlon = cls.env.ref( "test_base_import_pdf_by_template.po_decathlon" ) - cls.partner_tecnativa = cls.env.ref( - "test_base_import_pdf_by_template.partner_tecnativa" - ) - cls.template_invoice_tecnativa = cls.env.ref( - "test_base_import_pdf_by_template.invoice_tecnativa" - ) def _data_file(self, filename, encoding=None): filename = "data/" + filename @@ -75,26 +69,3 @@ def test_purchase_order_decathlon(self): self.assertIn("BOTIQUIN", default_codes) self.assertIn("GENERIC", default_codes) self.assertIn("66.11", record.message_ids.body) - - def test_account_invoice_tecnativa(self): - attachment = self._create_ir_attachment("account_invoice_tecnativa.pdf") - wizard = self._create_wizard_base_import_pdf_upload("account.move", attachment) - # Similar context from Customer invoices menu - wizard = wizard.with_context(**{"default_move_type": "out_invoice"}) - res = wizard.action_process() - self.assertEqual(res["res_model"], "account.move") - record = self.env[res["res_model"]].browse(res["res_id"]) - attachments = self._get_attachments(record) - self.assertEqual(record.move_type, "in_invoice") - self.assertEqual(record.partner_id, self.partner_tecnativa) - self.assertIn(attachment, attachments) - self.assertEqual(len(record.invoice_line_ids), 6) - self.assertEqual(sum(record.invoice_line_ids.mapped("quantity")), 665) - default_codes = record.invoice_line_ids.mapped("product_id.default_code") - self.assertIn("ROTULADOR", default_codes) - self.assertIn("BOLIGRAFO", default_codes) - self.assertIn("LEDS", default_codes) - self.assertIn("PLASTIFICADORA", default_codes) - self.assertIn("LAMINAS", default_codes) - self.assertIn("TRITURADORA", default_codes) - self.assertIn("100.25", record.message_ids.body)