Skip to content

Commit bdafe0f

Browse files
[ADD] report_qweb_field_converter
1 parent 7b085ad commit bdafe0f

20 files changed

+867
-0
lines changed
+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
===========================
2+
Report Qweb Field Converter
3+
===========================
4+
5+
.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
6+
!! This file is generated by oca-gen-addon-readme !!
7+
!! changes will be overwritten. !!
8+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
9+
10+
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
11+
:target: https://odoo-community.org/page/development-status
12+
:alt: Beta
13+
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
14+
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
15+
:alt: License: AGPL-3
16+
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Freporting--engine-lightgray.png?logo=github
17+
:target: https://github.com/OCA/reporting-engine/tree/16.0/report_qweb_field_converter
18+
:alt: OCA/reporting-engine
19+
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
20+
:target: https://translation.odoo-community.org/projects/reporting-engine-16-0/reporting-engine-16-0-report_qweb_field_converter
21+
:alt: Translate me on Weblate
22+
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
23+
:target: https://runbot.odoo-community.org/runbot/143/16.0
24+
:alt: Try me on Runbot
25+
26+
|badge1| |badge2| |badge3| |badge4| |badge5|
27+
28+
This module allows administrators to define the decimal precision of float fields and to
29+
add option values for fields (e.g., adding a date widget option for datetime fields) for
30+
QWeb report presentation.
31+
32+
**Table of contents**
33+
34+
.. contents::
35+
:local:
36+
37+
Configuration
38+
=============
39+
40+
Go to Settings > Technical > Reporting > Qweb Field Converter, and create records
41+
according to your needs.
42+
43+
For each record:
44+
45+
- choose a model and a field (required)
46+
- set UoM and UoM Field, or Currency and Currency Field for only float-type field
47+
(optional)
48+
- Set options: Add the options for your fields in JSON format (e.g. {"widget": "date"}).
49+
- set Company (optional)
50+
- set Digits (required for only float-type field)
51+
52+
Usage
53+
=====
54+
55+
Print a QWeb report (quotation, invoice, purchase order, etc.), and the value
56+
presentation for fields like line quantity, price unit and date order are adjusted
57+
according to the Qweb Field Converter configuration.
58+
59+
Note that among matching config records, the one with the strictest condition will be
60+
adopted.
61+
62+
Bug Tracker
63+
===========
64+
65+
Bugs are tracked on `GitHub Issues <https://github.com/OCA/reporting-engine/issues>`_.
66+
In case of trouble, please check there if your issue has already been reported.
67+
If you spotted it first, help us smashing it by providing a detailed and welcomed
68+
`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**>`_.
69+
70+
Do not contact contributors directly about support or help with technical issues.
71+
72+
Credits
73+
=======
74+
75+
Authors
76+
~~~~~~~
77+
78+
* Quartile Limited
79+
80+
Contributors
81+
~~~~~~~~~~~~
82+
83+
- [Quartile Limited](https://www.quartile.co):
84+
- Yoshi Tashiro
85+
- Aung Ko Ko Lin
86+
87+
Maintainers
88+
~~~~~~~~~~~
89+
90+
This module is maintained by the OCA.
91+
92+
.. image:: https://odoo-community.org/logo.png
93+
:alt: Odoo Community Association
94+
:target: https://odoo-community.org
95+
96+
OCA, or the Odoo Community Association, is a nonprofit organization whose
97+
mission is to support the collaborative development of Odoo features and
98+
promote its widespread use.
99+
100+
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.
101+
102+
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import models
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright 2024 Quartile Limited (https://www.quartile.com)
2+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
3+
{
4+
"name": "Report Qweb Field Converter",
5+
"version": "16.0.1.0.0",
6+
"category": "Technical Settings",
7+
"license": "AGPL-3",
8+
"author": "Quartile Limited, Odoo Community Association (OCA)",
9+
"website": "https://github.com/OCA/reporting-engine",
10+
"depends": ["uom"],
11+
"data": [
12+
"security/ir.model.access.csv",
13+
"security/qweb_field_converter_security.xml",
14+
"views/qweb_field_converter_views.xml",
15+
],
16+
"installable": True,
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from . import qweb_field_converter
2+
from . import ir_qweb_fields
3+
from . import ir_qweb
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright 2024 Quartile Limited (https://www.quartile.com)
2+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
3+
4+
import json
5+
import logging
6+
7+
from odoo import api, models
8+
9+
_logger = logging.getLogger(__name__)
10+
11+
12+
class IrQweb(models.AbstractModel):
13+
_inherit = "ir.qweb"
14+
15+
@api.model
16+
def _get_field(
17+
self, record, field_name, expression, tagName, field_options, values
18+
):
19+
report_type = values.get("report_type")
20+
if not report_type or report_type != "pdf":
21+
return super()._get_field(
22+
record, field_name, expression, tagName, field_options, values
23+
)
24+
qweb_recs = self.env["qweb.field.converter"].search(
25+
[("res_model_name", "=", record._name), ("field_name", "=", field_name)]
26+
)
27+
options_rec = max(qweb_recs, default=None, key=lambda r: r._get_score(record))
28+
if options_rec and options_rec.field_options:
29+
try:
30+
additional_options = json.loads(options_rec.field_options)
31+
field_options.update(additional_options)
32+
except json.JSONDecodeError as e:
33+
_logger.error(f"JSON decoding error for field options: {e}")
34+
return super()._get_field(
35+
record, field_name, expression, tagName, field_options, values
36+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright 2024 Quartile Limited (https://www.quartile.com)
2+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
3+
4+
from odoo import api, models
5+
6+
7+
class FloatConverter(models.AbstractModel):
8+
_inherit = "ir.qweb.field.float"
9+
10+
@api.model
11+
def record_to_html(self, record, field_name, options):
12+
if "precision" not in options and "decimal_precision" not in options:
13+
qweb_recs = self.env["qweb.field.converter"].search(
14+
[("res_model_name", "=", record._name), ("field_name", "=", field_name)]
15+
)
16+
precision_rec = max(
17+
qweb_recs, default=None, key=lambda r: r._get_score(record)
18+
)
19+
if precision_rec:
20+
options = dict(options, precision=precision_rec.digits)
21+
return super().record_to_html(record, field_name, options)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Copyright 2024 Quartile Limited (https://www.quartile.com)
2+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
3+
4+
from odoo import fields, models
5+
6+
7+
class QwebFieldConverter(models.Model):
8+
_name = "qweb.field.converter"
9+
_description = "Qweb Field Converter"
10+
_order = "res_model_id, field_id"
11+
12+
res_model_id = fields.Many2one(
13+
"ir.model", string="Model", ondelete="cascade", required=True
14+
)
15+
res_model_name = fields.Char("Model Name", related="res_model_id.model", store=True)
16+
field_id = fields.Many2one(
17+
"ir.model.fields",
18+
domain="[('model_id', '=', res_model_id)]",
19+
string="Field",
20+
ondelete="cascade",
21+
required=True,
22+
)
23+
field_type = fields.Selection(related="field_id.ttype", store=True)
24+
field_name = fields.Char("Field Name", related="field_id.name", store=True)
25+
uom_id = fields.Many2one("uom.uom", string="UoM", ondelete="cascade")
26+
uom_field_id = fields.Many2one(
27+
"ir.model.fields",
28+
domain="[('model_id', '=', res_model_id), ('relation', '=', 'uom.uom')]",
29+
string="UoM Field",
30+
ondelete="cascade",
31+
)
32+
currency_id = fields.Many2one("res.currency", string="Currency", ondelete="cascade")
33+
currency_field_id = fields.Many2one(
34+
"ir.model.fields",
35+
domain="[('model_id', '=', res_model_id), ('relation', '=', 'res.currency')]",
36+
string="Currency Field",
37+
ondelete="cascade",
38+
)
39+
field_options = fields.Text(
40+
"Options", help="JSON-formatted string to specify field formatting options"
41+
)
42+
digits = fields.Integer()
43+
company_id = fields.Many2one("res.company", string="Company")
44+
45+
def _get_score(self, record):
46+
self.ensure_one()
47+
score = 1
48+
if self.company_id:
49+
if record.company_id == self.company_id:
50+
score += 1
51+
else:
52+
return -1
53+
if self.uom_id:
54+
if record[self.uom_field_id.sudo().name] == self.uom_id:
55+
score += 1
56+
else:
57+
return -1
58+
if self.currency_id:
59+
if record[self.currency_field_id.sudo().name] == self.currency_id:
60+
score += 1
61+
else:
62+
return -1
63+
return score
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Go to Settings > Technical > Reporting > Qweb Field Converter, and create records
2+
according to your needs.
3+
4+
For each record:
5+
6+
- choose a model and a field (required)
7+
- set UoM and UoM Field, or Currency and Currency Field for only float-type field
8+
(optional)
9+
- set Options: Add the options for your fields in JSON format (e.g. {"widget": "date"}).
10+
- set Company (optional)
11+
- set Digits (required for only float-type field)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- [Quartile Limited](https://www.quartile.co):
2+
- Yoshi Tashiro
3+
- Aung Ko Ko Lin
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This module allows administrators to define the decimal precision of float fields and to
2+
add option values for fields (e.g., adding a date widget option for datetime fields) for
3+
QWeb report presentation.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
The Options attribute in QWeb Field Converter is meant for use in PDF reports
2+
to prevent UI issues in web views.
3+
For example, adding {"widget": "date"} for the date_approve field in a purchase order
4+
can cause two dates to appear under the confirmation date column in the portal view,
5+
due to it being defined twice with different widgets.
6+
https://github.com/odoo/odoo/blob/5eec37961c2170b354ef837b46f94e89ebf37d52/addons/purchase/views/portal_templates.xml#L101-L102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Print a QWeb report (quotation, invoice, purchase order, etc.), and the value
2+
presentation for fields like line quantity, price unit and date order are adjusted
3+
according to the Qweb Field Converter configuration.
4+
5+
Note that among matching config records, the one with the strictest condition will be
6+
adopted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
2+
access_qweb_field_converter_all,access.qweb.field.converter.all,model_qweb_field_converter,,1,0,0,0
3+
access_qweb_field_converter_admin,access.qweb.field.converter.admin,model_qweb_field_converter,base.group_system,1,1,1,1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<odoo noupdate="1">
3+
<record id="qweb_field_converter_company_rule" model="ir.rule">
4+
<field name="name">Qweb Field Converter Multi-company</field>
5+
<field name="model_id" ref="model_qweb_field_converter" />
6+
<field
7+
name="domain_force"
8+
>['|', ('company_id', 'in', company_ids), ('company_id', '=', False)]</field>
9+
</record>
10+
</odoo>

0 commit comments

Comments
 (0)