Skip to content
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

[16.0][MIG] mgmtsystem_indicators_report_pdf2data_import: migration to 16.0 #546

Open
wants to merge 8 commits into
base: 16.0
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class MgmtsystemIndicatorAbstract(models.AbstractModel):
help="Technical field for UX purpose.",
)

item_blocked = fields.Boolean(default=False)
item_blocked = fields.Boolean()

_sql_constraints = [
(
Expand Down
63 changes: 63 additions & 0 deletions mgmtsystem_indicators_report_pdf2data_import/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3

============================================
Mgmtsystem Indicators Report Pdf2data Import
============================================

This addon allows to create a indicators report extracting the data from a pdf

Usage
=====

To use this module you have to add in the configuration file a field called "pdf2data_templates_dir". Here the yml templates will be saved.
For example I used:
pdf2data_templates_dir=/home/REINACB/alba.riera/demoextract/templates_pdf2data

It is also needed to write the model in which this template will be used in the field "issuer".

Here you have an example of a template to use:

.. code-block:: python

issuer: mgmtsystem.indicators.report
keywords:
- TeleTest
fields:
report_id: Informe\s(\d{3,}v\d)
name: Nombre\sMuestra\s+\#\s+(\w.+)
sample_date: Fecha\sToma\sMuestra\s+\#\s+Dia\s+(\d{2}/\d{2}/\d{2})
date: Fecha\sValidacion\sy\sEmision\sInforme:\s+(\d{2}/\d{2}/\d{2})
validation_partner: Informe\sValidado\spor\s(\w.+)\s+Jefe
options:
decimal_separator: ","
remove_whitespace: False
remove_accents: True
date_formats:
- '%d/%m/%Y'
lines:
start: Control\sHigienico
end: Informe\sValidado\spor
first_line: (?P<concept>Recuento\s.+)
line: (?P<value>\d+,\d+)\s*u.f.c./\s*cm2\s+\(VR
last_line: (?P<value>\d+,\d+)\s*u.f.c./\s*m3
types:
value: float
required_fields:
- issuer


To use this module first you have to define a template with its yml file. To do so go to:

EDI -> Pdf2data templates

a) Select the model you want to generate with this template ( for example Mgmtsystem indicators report)
b) Copy and paste the template above (in the future the issuer field will be autogenerated with the field model)

Now you can import a PDF of the mgmtsystem.indicators.report model going to:

Settings -> Technical (add the permission to do so ) -> Pdf2dataimport -> Import PDF

a) Select the PDF anc click import
b) A indicator report will be generated with the PDF file
2 changes: 2 additions & 0 deletions mgmtsystem_indicators_report_pdf2data_import/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import components
from . import models
20 changes: 20 additions & 0 deletions mgmtsystem_indicators_report_pdf2data_import/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2021 Creu Blanca
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
"name": "Mgmtsystem Indicators Report Pdf2data Import",
"summary": """
This addon allows to create a indicators report
extracting the data from a pdf""",
"version": "16.0.1.0.0",
"license": "AGPL-3",
"author": "CreuBlanca,Odoo Community Association (OCA)",
"website": "https://github.com/tegin/cb-addons",
"depends": ["mgmtsystem_indicators_report", "edi_pdf2data_oca"],
"data": [
"views/pdf2data_template.xml",
"views/mgmtsystem_menu.xml",
"data/edi_pdf2data_type.xml",
],
"demo": [],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import edi_mgmtsystem_indicators_report_process_data
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Copyright 2020 ACSONE
# @author: Simone Orsi <[email protected]>
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

from odoo.addons.component.core import Component


class EDIBackendInputComponentMixin(Component):
_inherit = "edi.input.process.pdf2data.base"
_name = "edi.component.process_data.mgmtsystem.indicators.report"
_exchange_type = "pdf2data_mgmtsystem_indicators_report"

def _get_parsed_pdf2data_values(self, model, data_extracted):
report_values = {}
for field in data_extracted:
if field in model._fields:
report_values[field] = data_extracted[field]
line_values = [
(0, 0, self._get_parsed_pdf2data_indicator_ids(line))
for line in data_extracted.get("lines")
]
report_values.update({"indicator_ids": line_values})
return report_values

def _get_parsed_pdf2data_indicator_ids(self, line):
return {"name": line.get("concept"), "value": line.get("value")}

def _generate_from_template(self, data, template):
report_template = template.mgmtsystem_indicator_template_id
report = report_template._generate_report()
vals = {}
for field in data:
if field in report._fields:
vals[field] = data[field]
line_vals = []
for line in data.get("lines"):
if line.get("concept"):
template_indicator = report.indicator_ids.filtered(
lambda r: r.name == line.get("concept")
)
if template_indicator:
if template_indicator.value_type and line.get("value"):
line_vals.append(
(
1,
template_indicator.id,
{
"value_%s"
% template_indicator.value_type: line.get("value")
},
)
)
elif line.get("value"):
line_vals.append(
(
1,
template_indicator.id,
{"value": line.get("value")},
)
)
else:
line_vals.append(
(0, 0, self._get_parsed_pdf2data_indicator_ids(line))
)
vals.update({"indicator_ids": line_vals})
report.write(vals)
return report

def process_data(self, data, template, file):
if not template.mgmtsystem_indicator_template_id:
model = self.env["mgmtsystem.indicators.report"]
record = model.create(self._get_parsed_pdf2data_values(model, data))
else:
record = self._generate_from_template(data, template)
record.update({"report_pdf": file})
self.exchange_record.write({"model": record._name, "res_id": record.id})
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>

<record id="pdf2data_exchange_type" model="edi.exchange.type">
<field
name="name"
>Pdf2Data Management System Indicators Report Exchange type</field>
<field name="code">pdf2data_mgmtsystem_indicators_report</field>
<field name="backend_type_id" ref="edi_pdf2data_oca.backend_type" />
<field name="direction">input</field>
<field name="exchange_filename_pattern">{record_name}{dt}</field>
<field name="exchange_file_ext">pdf</field>
</record>

</odoo>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import pdf2data_template
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2021 Creu Blanca
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models


class Pdf2dataTemplate(models.Model):

_inherit = "pdf2data.template"

mgmtsystem_indicator_template_id = fields.Many2one(
"mgmtsystem.indicators.report.template"
)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2021 Creu Blanca
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo>
<record id="edi_pdf2data_wizard_action_mgmtsystem" model="ir.actions.act_window">
<field name="name">Import PDF</field>
<field name="res_model">pdf2data.import</field>
<field name="view_mode">form</field>
<field name="target">new</field>
<field name="context">{}</field>
</record>
<menuitem
id="edi_pdf2data_wizard_mgmtsystem_menu"
parent="mgmtsystem.menu_mgmtsystem_root"
action="edi_pdf2data_oca.edi_pdf2data_wizard_action"
sequence="45"
/>
</odoo>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2021 Creu Blanca
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo>
<record model="ir.ui.view" id="pdf2data_template_form_view">
<field
name="name"
>pdf2data.template.form (in mgmtsystem_indicators_report_pdf2data_import)</field>
<field name="model">pdf2data.template</field>
<field name="inherit_id" ref="edi_pdf2data_oca.pdf2data_template_form_view" />
<field name="arch" type="xml">
<field name="exchange_type_id" position="after">
<field name="mgmtsystem_indicator_template_id" />
</field>
</field>
</record>
</odoo>
6 changes: 6 additions & 0 deletions setup/mgmtsystem_indicators_report_pdf2data_import/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,
)
Loading