From e52b7a202f3f93393174ec17d854342401f4b913 Mon Sep 17 00:00:00 2001 From: Aungkokolin1997 Date: Mon, 26 Feb 2024 14:52:44 +0630 Subject: [PATCH] [ADD] report_qweb_field_converter --- report_qweb_field_converter/README.rst | 102 ++++ report_qweb_field_converter/__init__.py | 1 + report_qweb_field_converter/__manifest__.py | 17 + .../models/__init__.py | 3 + report_qweb_field_converter/models/ir_qweb.py | 36 ++ .../models/ir_qweb_fields.py | 21 + .../models/qweb_field_converter.py | 63 +++ .../readme/CONFIGURE.rst | 11 + .../readme/CONTRIBUTORS.rst | 3 + .../readme/DESCRIPTION.rst | 3 + .../readme/ROADMAP.rst | 6 + report_qweb_field_converter/readme/USAGE.rst | 6 + .../security/ir.model.access.csv | 3 + .../qweb_field_converter_security.xml | 10 + .../static/description/index.html | 447 ++++++++++++++++++ report_qweb_field_converter/tests/__init__.py | 1 + .../test_report_qweb_decimal_precision.py | 50 ++ .../views/qweb_field_converter_views.xml | 77 +++ .../odoo/addons/report_qweb_field_converter | 1 + setup/report_qweb_field_converter/setup.py | 6 + 20 files changed, 867 insertions(+) create mode 100644 report_qweb_field_converter/README.rst create mode 100644 report_qweb_field_converter/__init__.py create mode 100644 report_qweb_field_converter/__manifest__.py create mode 100644 report_qweb_field_converter/models/__init__.py create mode 100644 report_qweb_field_converter/models/ir_qweb.py create mode 100644 report_qweb_field_converter/models/ir_qweb_fields.py create mode 100644 report_qweb_field_converter/models/qweb_field_converter.py create mode 100644 report_qweb_field_converter/readme/CONFIGURE.rst create mode 100644 report_qweb_field_converter/readme/CONTRIBUTORS.rst create mode 100644 report_qweb_field_converter/readme/DESCRIPTION.rst create mode 100644 report_qweb_field_converter/readme/ROADMAP.rst create mode 100644 report_qweb_field_converter/readme/USAGE.rst create mode 100644 report_qweb_field_converter/security/ir.model.access.csv create mode 100644 report_qweb_field_converter/security/qweb_field_converter_security.xml create mode 100644 report_qweb_field_converter/static/description/index.html create mode 100644 report_qweb_field_converter/tests/__init__.py create mode 100644 report_qweb_field_converter/tests/test_report_qweb_decimal_precision.py create mode 100644 report_qweb_field_converter/views/qweb_field_converter_views.xml create mode 120000 setup/report_qweb_field_converter/odoo/addons/report_qweb_field_converter create mode 100644 setup/report_qweb_field_converter/setup.py diff --git a/report_qweb_field_converter/README.rst b/report_qweb_field_converter/README.rst new file mode 100644 index 0000000000..923555ad43 --- /dev/null +++ b/report_qweb_field_converter/README.rst @@ -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 `_. +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 `_. + +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 `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/report_qweb_field_converter/__init__.py b/report_qweb_field_converter/__init__.py new file mode 100644 index 0000000000..0650744f6b --- /dev/null +++ b/report_qweb_field_converter/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/report_qweb_field_converter/__manifest__.py b/report_qweb_field_converter/__manifest__.py new file mode 100644 index 0000000000..c68acc08af --- /dev/null +++ b/report_qweb_field_converter/__manifest__.py @@ -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, +} diff --git a/report_qweb_field_converter/models/__init__.py b/report_qweb_field_converter/models/__init__.py new file mode 100644 index 0000000000..577593c4da --- /dev/null +++ b/report_qweb_field_converter/models/__init__.py @@ -0,0 +1,3 @@ +from . import qweb_field_converter +from . import ir_qweb_fields +from . import ir_qweb diff --git a/report_qweb_field_converter/models/ir_qweb.py b/report_qweb_field_converter/models/ir_qweb.py new file mode 100644 index 0000000000..c54810d8d9 --- /dev/null +++ b/report_qweb_field_converter/models/ir_qweb.py @@ -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( + record, field_name, expression, tagName, field_options, values + ) + qweb_recs = self.env["qweb.field.converter"].search( + [("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}") + return super()._get_field( + record, field_name, expression, tagName, field_options, values + ) diff --git a/report_qweb_field_converter/models/ir_qweb_fields.py b/report_qweb_field_converter/models/ir_qweb_fields.py new file mode 100644 index 0000000000..c40cd81a41 --- /dev/null +++ b/report_qweb_field_converter/models/ir_qweb_fields.py @@ -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) diff --git a/report_qweb_field_converter/models/qweb_field_converter.py b/report_qweb_field_converter/models/qweb_field_converter.py new file mode 100644 index 0000000000..13a937b452 --- /dev/null +++ b/report_qweb_field_converter/models/qweb_field_converter.py @@ -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 + else: + return -1 + if self.uom_id: + if record[self.uom_field_id.sudo().name] == self.uom_id: + score += 1 + else: + return -1 + if self.currency_id: + if record[self.currency_field_id.sudo().name] == self.currency_id: + score += 1 + else: + return -1 + return score diff --git a/report_qweb_field_converter/readme/CONFIGURE.rst b/report_qweb_field_converter/readme/CONFIGURE.rst new file mode 100644 index 0000000000..1ff37e454c --- /dev/null +++ b/report_qweb_field_converter/readme/CONFIGURE.rst @@ -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) diff --git a/report_qweb_field_converter/readme/CONTRIBUTORS.rst b/report_qweb_field_converter/readme/CONTRIBUTORS.rst new file mode 100644 index 0000000000..24237c00ff --- /dev/null +++ b/report_qweb_field_converter/readme/CONTRIBUTORS.rst @@ -0,0 +1,3 @@ +- [Quartile Limited](https://www.quartile.co): + - Yoshi Tashiro + - Aung Ko Ko Lin diff --git a/report_qweb_field_converter/readme/DESCRIPTION.rst b/report_qweb_field_converter/readme/DESCRIPTION.rst new file mode 100644 index 0000000000..4290b100ff --- /dev/null +++ b/report_qweb_field_converter/readme/DESCRIPTION.rst @@ -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. diff --git a/report_qweb_field_converter/readme/ROADMAP.rst b/report_qweb_field_converter/readme/ROADMAP.rst new file mode 100644 index 0000000000..87c894c106 --- /dev/null +++ b/report_qweb_field_converter/readme/ROADMAP.rst @@ -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 diff --git a/report_qweb_field_converter/readme/USAGE.rst b/report_qweb_field_converter/readme/USAGE.rst new file mode 100644 index 0000000000..8688a2b5fd --- /dev/null +++ b/report_qweb_field_converter/readme/USAGE.rst @@ -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. diff --git a/report_qweb_field_converter/security/ir.model.access.csv b/report_qweb_field_converter/security/ir.model.access.csv new file mode 100644 index 0000000000..c627f93276 --- /dev/null +++ b/report_qweb_field_converter/security/ir.model.access.csv @@ -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 diff --git a/report_qweb_field_converter/security/qweb_field_converter_security.xml b/report_qweb_field_converter/security/qweb_field_converter_security.xml new file mode 100644 index 0000000000..ce63f0bcb6 --- /dev/null +++ b/report_qweb_field_converter/security/qweb_field_converter_security.xml @@ -0,0 +1,10 @@ + + + + Qweb Field Converter Multi-company + + ['|', ('company_id', 'in', company_ids), ('company_id', '=', False)] + + diff --git a/report_qweb_field_converter/static/description/index.html b/report_qweb_field_converter/static/description/index.html new file mode 100644 index 0000000000..027ab9c4b0 --- /dev/null +++ b/report_qweb_field_converter/static/description/index.html @@ -0,0 +1,447 @@ + + + + + + +Report Qweb Field Converter + + + +
+

Report Qweb Field Converter

+ + +

Beta License: AGPL-3 OCA/reporting-engine Translate me on Weblate Try me on Runbot

+

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

+ +
+

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. +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.

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • Quartile Limited
  • +
+
+
+

Contributors

+ +
+
+

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.

+

This module is part of the OCA/reporting-engine project on GitHub.

+

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

+
+
+
+ + diff --git a/report_qweb_field_converter/tests/__init__.py b/report_qweb_field_converter/tests/__init__.py new file mode 100644 index 0000000000..f480a3c626 --- /dev/null +++ b/report_qweb_field_converter/tests/__init__.py @@ -0,0 +1 @@ +from . import test_report_qweb_decimal_precision diff --git a/report_qweb_field_converter/tests/test_report_qweb_decimal_precision.py b/report_qweb_field_converter/tests/test_report_qweb_decimal_precision.py new file mode 100644 index 0000000000..f0c67d7faf --- /dev/null +++ b/report_qweb_field_converter/tests/test_report_qweb_decimal_precision.py @@ -0,0 +1,50 @@ +# Copyright 2024 Quartile Limited (https://www.quartile.com) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo.tests.common import TransactionCase + + +class TestQwebFieldConverter(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.FloatConverter = cls.env["ir.qweb.field.float"] + # Create a test currency record + cls.test_currency = cls.env["res.currency"].create( + {"name": "Test Currency", "symbol": "T", "rounding": 0.01} + ) + # Create a test qweb field converter record + cls.dp_qweb_rec = cls.env["qweb.field.converter"].create( + { + "res_model_id": cls.env.ref("base.model_res_currency").id, + "field_id": cls.env["ir.model.fields"] + ._get("res.currency", "rounding") + .id, + "digits": 0, + } + ) + + def test_record_to_html(self): + options = {} + rendered_html = self.FloatConverter.record_to_html( + self.test_currency, "rounding", options + ) + self.assertEqual(rendered_html, "0") + # Test with 2 digits + self.dp_qweb_rec.digits = 2 + rendered_html = self.FloatConverter.record_to_html( + self.test_currency, "rounding", options + ) + self.assertEqual("0.01", rendered_html) + # Test with 3 digits + self.dp_qweb_rec.digits = 3 + rendered_html = self.FloatConverter.record_to_html( + self.test_currency, "rounding", options + ) + self.assertEqual("0.010", rendered_html) + # Test without qweb field converter record (default is 6 digits) + self.dp_qweb_rec.unlink() + rendered_html = self.FloatConverter.record_to_html( + self.test_currency, "rounding", options + ) + self.assertEqual("0.010000", rendered_html) diff --git a/report_qweb_field_converter/views/qweb_field_converter_views.xml b/report_qweb_field_converter/views/qweb_field_converter_views.xml new file mode 100644 index 0000000000..8fc98fc341 --- /dev/null +++ b/report_qweb_field_converter/views/qweb_field_converter_views.xml @@ -0,0 +1,77 @@ + + + + qweb.field.converter.tree + qweb.field.converter + tree + + + + + + + + + + + + + + + + + qweb.field.converter.search + qweb.field.converter + + + + + + + + + + + + + + + + Qweb Field Converter + qweb.field.converter + tree + + + + diff --git a/setup/report_qweb_field_converter/odoo/addons/report_qweb_field_converter b/setup/report_qweb_field_converter/odoo/addons/report_qweb_field_converter new file mode 120000 index 0000000000..256c8a02f0 --- /dev/null +++ b/setup/report_qweb_field_converter/odoo/addons/report_qweb_field_converter @@ -0,0 +1 @@ +../../../../report_qweb_field_converter \ No newline at end of file diff --git a/setup/report_qweb_field_converter/setup.py b/setup/report_qweb_field_converter/setup.py new file mode 100644 index 0000000000..28c57bb640 --- /dev/null +++ b/setup/report_qweb_field_converter/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)