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][ADD] report_qweb_field_converter #857

Open
wants to merge 1 commit into
base: 16.0
Choose a base branch
from
Open
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
102 changes: 102 additions & 0 deletions report_qweb_field_converter/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
===========================
Report Qweb Field Converter
===========================

.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |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%2Freporting--engine-lightgray.png?logo=github
:target: https://github.com/OCA/reporting-engine/tree/16.0/report_qweb_field_converter
:alt: OCA/reporting-engine
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/reporting-engine-16-0/reporting-engine-16-0-report_qweb_field_converter
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/143/16.0
:alt: Try me on Runbot

|badge1| |badge2| |badge3| |badge4| |badge5|

This module allows administrators to define the decimal precision of float fields and to
add option values for fields (e.g., adding a date widget option for datetime fields) for
QWeb report presentation.

**Table of contents**

.. contents::
:local:

Configuration
=============

Go to Settings > Technical > Reporting > Qweb Field Converter, and create records
according to your needs.

For each record:

- choose a model and a field (required)
- set UoM and UoM Field, or Currency and Currency Field for only float-type field
(optional)
- Set options: Add the options for your fields in JSON format (e.g. {"widget": "date"}).
- set Company (optional)
- set Digits (required for only float-type field)

Usage
=====

Print a QWeb report (quotation, invoice, purchase order, etc.), and the value
presentation for fields like line quantity, price unit and date order are adjusted
according to the Qweb Field Converter configuration.

Note that among matching config records, the one with the strictest condition will be
adopted.

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/reporting-engine/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/reporting-engine/issues/new?body=module:%20report_qweb_field_converter%0Aversion:%2016.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
~~~~~~~

* Quartile Limited

Contributors
~~~~~~~~~~~~

- [Quartile Limited](https://www.quartile.co):
- Yoshi Tashiro
- Aung Ko Ko Lin

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.

This module is part of the `OCA/reporting-engine <https://github.com/OCA/reporting-engine/tree/16.0/report_qweb_field_converter>`_ 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 report_qweb_field_converter/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
17 changes: 17 additions & 0 deletions report_qweb_field_converter/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright 2024 Quartile Limited (https://www.quartile.com)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
{
"name": "Report Qweb Field Converter",
"version": "16.0.1.0.0",
"category": "Technical Settings",
"license": "AGPL-3",
"author": "Quartile Limited, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/reporting-engine",
"depends": ["uom"],
"data": [
"security/ir.model.access.csv",
"security/qweb_field_converter_security.xml",
"views/qweb_field_converter_views.xml",
],
"installable": True,
}
3 changes: 3 additions & 0 deletions report_qweb_field_converter/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import qweb_field_converter
from . import ir_qweb_fields
from . import ir_qweb
36 changes: 36 additions & 0 deletions report_qweb_field_converter/models/ir_qweb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2024 Quartile Limited (https://www.quartile.com)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

import json
import logging

from odoo import api, models

_logger = logging.getLogger(__name__)


class IrQweb(models.AbstractModel):
_inherit = "ir.qweb"

@api.model
def _get_field(
self, record, field_name, expression, tagName, field_options, values
):
report_type = values.get("report_type")
if not report_type or report_type != "pdf":
return super()._get_field(

Check warning on line 21 in report_qweb_field_converter/models/ir_qweb.py

View check run for this annotation

Codecov / codecov/patch

report_qweb_field_converter/models/ir_qweb.py#L21

Added line #L21 was not covered by tests
record, field_name, expression, tagName, field_options, values
)
qweb_recs = self.env["qweb.field.converter"].search(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
qweb_recs = self.env["qweb.field.converter"].search(
qweb_recs = self.sudo().env["qweb.field.converter"].search(

We should either do this or make qweb.field.converter globally accessible. Please check and suggest which way is better. Portal users get 403 forbidden error when accessing their documents otherwise.

[("res_model_name", "=", record._name), ("field_name", "=", field_name)]
)
options_rec = max(qweb_recs, default=None, key=lambda r: r._get_score(record))
if options_rec and options_rec.field_options:
try:
additional_options = json.loads(options_rec.field_options)
field_options.update(additional_options)
except json.JSONDecodeError as e:
_logger.error(f"JSON decoding error for field options: {e}")

Check warning on line 33 in report_qweb_field_converter/models/ir_qweb.py

View check run for this annotation

Codecov / codecov/patch

report_qweb_field_converter/models/ir_qweb.py#L29-L33

Added lines #L29 - L33 were not covered by tests
return super()._get_field(
record, field_name, expression, tagName, field_options, values
)
21 changes: 21 additions & 0 deletions report_qweb_field_converter/models/ir_qweb_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2024 Quartile Limited (https://www.quartile.com)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import api, models


class FloatConverter(models.AbstractModel):
_inherit = "ir.qweb.field.float"

@api.model
def record_to_html(self, record, field_name, options):
if "precision" not in options and "decimal_precision" not in options:
qweb_recs = self.env["qweb.field.converter"].search(
[("res_model_name", "=", record._name), ("field_name", "=", field_name)]
)
precision_rec = max(
qweb_recs, default=None, key=lambda r: r._get_score(record)
)
if precision_rec:
options = dict(options, precision=precision_rec.digits)
return super().record_to_html(record, field_name, options)
63 changes: 63 additions & 0 deletions report_qweb_field_converter/models/qweb_field_converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Copyright 2024 Quartile Limited (https://www.quartile.com)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import fields, models


class QwebFieldConverter(models.Model):
_name = "qweb.field.converter"
_description = "Qweb Field Converter"
_order = "res_model_id, field_id"

res_model_id = fields.Many2one(
"ir.model", string="Model", ondelete="cascade", required=True
)
res_model_name = fields.Char("Model Name", related="res_model_id.model", store=True)
field_id = fields.Many2one(
"ir.model.fields",
domain="[('model_id', '=', res_model_id)]",
string="Field",
ondelete="cascade",
required=True,
)
field_type = fields.Selection(related="field_id.ttype", store=True)
field_name = fields.Char("Field Name", related="field_id.name", store=True)
uom_id = fields.Many2one("uom.uom", string="UoM", ondelete="cascade")
uom_field_id = fields.Many2one(
"ir.model.fields",
domain="[('model_id', '=', res_model_id), ('relation', '=', 'uom.uom')]",
string="UoM Field",
ondelete="cascade",
)
currency_id = fields.Many2one("res.currency", string="Currency", ondelete="cascade")
currency_field_id = fields.Many2one(
"ir.model.fields",
domain="[('model_id', '=', res_model_id), ('relation', '=', 'res.currency')]",
string="Currency Field",
ondelete="cascade",
)
field_options = fields.Text(
"Options", help="JSON-formatted string to specify field formatting options"
)
digits = fields.Integer()
company_id = fields.Many2one("res.company", string="Company")

def _get_score(self, record):
self.ensure_one()
score = 1
if self.company_id:
if record.company_id == self.company_id:
score += 1

Check warning on line 50 in report_qweb_field_converter/models/qweb_field_converter.py

View check run for this annotation

Codecov / codecov/patch

report_qweb_field_converter/models/qweb_field_converter.py#L50

Added line #L50 was not covered by tests
else:
return -1

Check warning on line 52 in report_qweb_field_converter/models/qweb_field_converter.py

View check run for this annotation

Codecov / codecov/patch

report_qweb_field_converter/models/qweb_field_converter.py#L52

Added line #L52 was not covered by tests
if self.uom_id:
if record[self.uom_field_id.sudo().name] == self.uom_id:
score += 1

Check warning on line 55 in report_qweb_field_converter/models/qweb_field_converter.py

View check run for this annotation

Codecov / codecov/patch

report_qweb_field_converter/models/qweb_field_converter.py#L55

Added line #L55 was not covered by tests
else:
return -1

Check warning on line 57 in report_qweb_field_converter/models/qweb_field_converter.py

View check run for this annotation

Codecov / codecov/patch

report_qweb_field_converter/models/qweb_field_converter.py#L57

Added line #L57 was not covered by tests
if self.currency_id:
if record[self.currency_field_id.sudo().name] == self.currency_id:
score += 1

Check warning on line 60 in report_qweb_field_converter/models/qweb_field_converter.py

View check run for this annotation

Codecov / codecov/patch

report_qweb_field_converter/models/qweb_field_converter.py#L60

Added line #L60 was not covered by tests
else:
return -1

Check warning on line 62 in report_qweb_field_converter/models/qweb_field_converter.py

View check run for this annotation

Codecov / codecov/patch

report_qweb_field_converter/models/qweb_field_converter.py#L62

Added line #L62 was not covered by tests
return score
11 changes: 11 additions & 0 deletions report_qweb_field_converter/readme/CONFIGURE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Go to Settings > Technical > Reporting > Qweb Field Converter, and create records
according to your needs.

For each record:

- choose a model and a field (required)
- set UoM and UoM Field, or Currency and Currency Field for only float-type field
(optional)
- set Options: Add the options for your fields in JSON format (e.g. {"widget": "date"}).
- set Company (optional)
- set Digits (required for only float-type field)
3 changes: 3 additions & 0 deletions report_qweb_field_converter/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- [Quartile Limited](https://www.quartile.co):
- Yoshi Tashiro
- Aung Ko Ko Lin
3 changes: 3 additions & 0 deletions report_qweb_field_converter/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This module allows administrators to define the decimal precision of float fields and to
add option values for fields (e.g., adding a date widget option for datetime fields) for
QWeb report presentation.
6 changes: 6 additions & 0 deletions report_qweb_field_converter/readme/ROADMAP.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
The Options attribute in QWeb Field Converter is meant for use in PDF reports
to prevent UI issues in web views.
For example, adding {"widget": "date"} for the date_approve field in a purchase order
can cause two dates to appear under the confirmation date column in the portal view,
due to it being defined twice with different widgets.
https://github.com/odoo/odoo/blob/5eec37961c2170b354ef837b46f94e89ebf37d52/addons/purchase/views/portal_templates.xml#L101-L102
6 changes: 6 additions & 0 deletions report_qweb_field_converter/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Print a QWeb report (quotation, invoice, purchase order, etc.), and the value
presentation for fields like line quantity, price unit and date order are adjusted
according to the Qweb Field Converter configuration.

Note that among matching config records, the one with the strictest condition will be
adopted.
3 changes: 3 additions & 0 deletions report_qweb_field_converter/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_qweb_field_converter_all,access.qweb.field.converter.all,model_qweb_field_converter,,1,0,0,0
access_qweb_field_converter_admin,access.qweb.field.converter.admin,model_qweb_field_converter,base.group_system,1,1,1,1
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo noupdate="1">
<record id="qweb_field_converter_company_rule" model="ir.rule">
<field name="name">Qweb Field Converter Multi-company</field>
<field name="model_id" ref="model_qweb_field_converter" />
<field
name="domain_force"
>['|', ('company_id', 'in', company_ids), ('company_id', '=', False)]</field>
</record>
</odoo>
Loading
Loading