Skip to content

Commit

Permalink
[ADD] base_import_pdf_by_template_account: New module
Browse files Browse the repository at this point in the history
TT50003
  • Loading branch information
victoralmau committed Dec 2, 2024
1 parent 4aab29a commit 1dcebb0
Show file tree
Hide file tree
Showing 18 changed files with 853 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
88 changes: 88 additions & 0 deletions base_import_pdf_by_template_account/README.rst
Original file line number Diff line number Diff line change
@@ -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 <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:%20base_import_pdf_by_template_account%0Aversion:%2017.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.

Credits
=======

Authors
-------

* Tecnativa

Contributors
------------

- `Tecnativa <https://www.tecnativa.com>`__:

- 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 <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-victoralmau|

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

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
1 change: 1 addition & 0 deletions base_import_pdf_by_template_account/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
15 changes: 15 additions & 0 deletions base_import_pdf_by_template_account/__manifest__.py
Original file line number Diff line number Diff line change
@@ -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"],
}
155 changes: 155 additions & 0 deletions base_import_pdf_by_template_account/demo/base_import_pdf_template.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo noupdate="1">
<record id="generic_product" model="product.product">
<field name="name">Test Generic Product</field>
<field name="default_code">GENERIC</field>
</record>
<!-- Account invoice Template Data !-->
<record id="partner_tecnativa" model="res.partner">
<field name="name">Tecnativa</field>
</record>
<record id="supplierinfo_tecnativa_CONS_0001" model="product.supplierinfo">
<field name="partner_id" ref="partner_tecnativa" />
<field name="product_code">CONS_0001</field>
</record>
<record id="product_rotulador" model="product.product">
<field name="name">Test Rotulador</field>
<field name="default_code">ROTULADOR</field>
<field name="purchase_ok" eval="True" />
<field
name="seller_ids"
eval="[(6, 0, [ref('supplierinfo_tecnativa_CONS_0001')])]"
/>
</record>
<record id="supplierinfo_tecnativa_CONS_0002" model="product.supplierinfo">
<field name="partner_id" ref="partner_tecnativa" />
<field name="product_code">CONS_0002</field>
</record>
<record id="product_boligrafo" model="product.product">
<field name="name">Test Boligrafo</field>
<field name="default_code">BOLIGRAFO</field>
<field name="purchase_ok" eval="True" />
<field
name="seller_ids"
eval="[(6, 0, [ref('supplierinfo_tecnativa_CONS_0002')])]"
/>
</record>
<record id="supplierinfo_tecnativa_FURN_0003" model="product.supplierinfo">
<field name="partner_id" ref="partner_tecnativa" />
<field name="product_code">FURN_0003</field>
</record>
<record id="product_leds" model="product.product">
<field name="name">Test Leds</field>
<field name="default_code">LEDS</field>
<field name="purchase_ok" eval="True" />
<field
name="seller_ids"
eval="[(6, 0, [ref('supplierinfo_tecnativa_FURN_0003')])]"
/>
</record>
<record id="supplierinfo_tecnativa_E_COM10" model="product.supplierinfo">
<field name="partner_id" ref="partner_tecnativa" />
<field name="product_code">E-COM10</field>
</record>
<record id="product_plastificadora" model="product.product">
<field name="name">Test Plastificadora</field>
<field name="default_code">PLASTIFICADORA</field>
<field name="purchase_ok" eval="True" />
<field
name="seller_ids"
eval="[(6, 0, [ref('supplierinfo_tecnativa_E_COM10')])]"
/>
</record>
<record id="supplierinfo_tecnativa_FURN_8621" model="product.supplierinfo">
<field name="partner_id" ref="partner_tecnativa" />
<field name="product_code">FURN_8621</field>
</record>
<record id="product_laminas" model="product.product">
<field name="name">Test Lmaninas</field>
<field name="default_code">LAMINAS</field>
<field name="purchase_ok" eval="True" />
<field
name="seller_ids"
eval="[(6, 0, [ref('supplierinfo_tecnativa_FURN_8621')])]"
/>
</record>
<record id="supplierinfo_tecnativa_FURN_7023" model="product.supplierinfo">
<field name="partner_id" ref="partner_tecnativa" />
<field name="product_code">FURN_7023</field>
</record>
<record id="product_trituradora" model="product.product">
<field name="name">Test Trituradora</field>
<field name="default_code">TRITURADORA</field>
<field name="purchase_ok" eval="True" />
<field
name="seller_ids"
eval="[(6, 0, [ref('supplierinfo_tecnativa_FURN_7023')])]"
/>
</record>
<record id="invoice_tecnativa" model="base.import.pdf.template">
<field name="name">Invoices Tecnativa</field>
<field name="model_id" ref="account.model_account_move" />
<field
name="child_field_id"
ref="account.field_account_move__invoice_line_ids"
/>
<!-- <field name="auto_detect_pattern">(?<=B 8 7 5 3 0 4 3 2)[\S\s]*</field> -->
<field name="header_items">Producto,Cantidad,Precio</field>
</record>
<record
id="invoice_tecnativa_line_partner_id"
model="base.import.pdf.template.line"
>
<field name="template_id" ref="invoice_tecnativa" />
<field name="related_model">header</field>
<field name="field_id" ref="account.field_account_move__partner_id" />
<field name="value_type">fixed</field>
<field name="fixed_value" ref="partner_tecnativa" />
</record>
<record id="invoice_tecnativa_line_move_type" model="base.import.pdf.template.line">
<field name="template_id" ref="invoice_tecnativa" />
<field name="related_model">header</field>
<field name="field_id" ref="account.field_account_move__move_type" />
<field name="value_type">fixed</field>
<field
name="fixed_value_selection"
ref="account.selection__account_invoice_report__move_type__in_invoice"
/>
</record>
<record
id="invoice_tecnativa_line_product_id"
model="base.import.pdf.template.line"
>
<field name="template_id" ref="invoice_tecnativa" />
<field name="related_model">lines</field>
<field name="field_id" ref="account.field_account_move_line__product_id" />
<field name="column">0</field>
<field name="pattern">\[([A-Z\d]+[_|-][A-Z\d]+)\]</field>
<field name="value_type">variable</field>
<field name="search_field_id" ref="product.field_product_product__seller_ids" />
<field
name="search_subfield_id"
ref="product.field_product_supplierinfo__product_code"
/>
<!-- <field name="default_value">product.product,cls.env.ref('base_import_pdf_by_template_account.generic_product').id</field> -->
</record>
<record id="invoice_tecnativa_line_quantity" model="base.import.pdf.template.line">
<field name="template_id" ref="invoice_tecnativa" />
<field name="related_model">lines</field>
<field name="field_id" ref="account.field_account_move_line__quantity" />
<field name="column">1</field>
<field name="pattern">\[[A-Z\d]+[_|-][A-Z\d]+\] [a-zA-Záí]* ([0-9]{1,3})</field>
<field name="value_type">variable</field>
</record>
<record id="invoice_tecnativa_price_unit" model="base.import.pdf.template.line">
<field name="template_id" ref="invoice_tecnativa" />
<field name="related_model">lines</field>
<field name="field_id" ref="account.field_account_move_line__price_unit" />
<field name="column">2</field>
<field
name="pattern"
>\[[A-Z\d]+[_|-][A-Z\d]+\] [a-zA-Záí]* [0-9]{1,3} ([0-9]{1,3}.[0-9]{2})</field>
<field name="value_type">variable</field>
<field name="log_distinct_value" eval="True" />
</record>
</odoo>
1 change: 1 addition & 0 deletions base_import_pdf_by_template_account/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import account_move
47 changes: 47 additions & 0 deletions base_import_pdf_by_template_account/models/account_move.py
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 3 additions & 0 deletions base_import_pdf_by_template_account/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["whool"]
build-backend = "whool.buildapi"
3 changes: 3 additions & 0 deletions base_import_pdf_by_template_account/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- [Tecnativa](https://www.tecnativa.com):
- Víctor Martínez
- Pedro M. Baeza
1 change: 1 addition & 0 deletions base_import_pdf_by_template_account/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added support for account to process the PDF attached to the invoice when creating the invoice from an email alias.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 1dcebb0

Please sign in to comment.