From 6c57cfe5301b9e9cbc81cfc8045e8184df2ff052 Mon Sep 17 00:00:00 2001 From: Yoshi Tashiro Date: Sat, 13 Jan 2024 14:41:16 +0000 Subject: [PATCH] [ADD] report_qweb_decimal_precision --- report_qweb_decimal_precision/README.rst | 124 +++++ report_qweb_decimal_precision/__init__.py | 1 + report_qweb_decimal_precision/__manifest__.py | 17 + .../models/__init__.py | 2 + .../models/decimal_precision_qweb.py | 59 +++ .../models/ir_qweb_fields.py | 21 + .../readme/CONFIGURE.md | 9 + .../readme/CONTEXT.md | 15 + .../readme/CONTRIBUTORS.md | 3 + .../readme/DESCRIPTION.md | 2 + report_qweb_decimal_precision/readme/USAGE.md | 6 + .../decimal_precision_qweb_security.xml | 10 + .../security/ir.model.access.csv | 3 + .../static/description/index.html | 468 ++++++++++++++++++ .../tests/__init__.py | 1 + .../test_report_qweb_decimal_precision.py | 50 ++ .../views/decimal_precision_qweb_views.xml | 72 +++ .../odoo/addons/report_qweb_decimal_precision | 1 + setup/report_qweb_decimal_precision/setup.py | 6 + 19 files changed, 870 insertions(+) create mode 100644 report_qweb_decimal_precision/README.rst create mode 100644 report_qweb_decimal_precision/__init__.py create mode 100644 report_qweb_decimal_precision/__manifest__.py create mode 100644 report_qweb_decimal_precision/models/__init__.py create mode 100644 report_qweb_decimal_precision/models/decimal_precision_qweb.py create mode 100644 report_qweb_decimal_precision/models/ir_qweb_fields.py create mode 100644 report_qweb_decimal_precision/readme/CONFIGURE.md create mode 100644 report_qweb_decimal_precision/readme/CONTEXT.md create mode 100644 report_qweb_decimal_precision/readme/CONTRIBUTORS.md create mode 100644 report_qweb_decimal_precision/readme/DESCRIPTION.md create mode 100644 report_qweb_decimal_precision/readme/USAGE.md create mode 100644 report_qweb_decimal_precision/security/decimal_precision_qweb_security.xml create mode 100644 report_qweb_decimal_precision/security/ir.model.access.csv create mode 100644 report_qweb_decimal_precision/static/description/index.html create mode 100644 report_qweb_decimal_precision/tests/__init__.py create mode 100644 report_qweb_decimal_precision/tests/test_report_qweb_decimal_precision.py create mode 100644 report_qweb_decimal_precision/views/decimal_precision_qweb_views.xml create mode 120000 setup/report_qweb_decimal_precision/odoo/addons/report_qweb_decimal_precision create mode 100644 setup/report_qweb_decimal_precision/setup.py diff --git a/report_qweb_decimal_precision/README.rst b/report_qweb_decimal_precision/README.rst new file mode 100644 index 0000000000..0e90c52592 --- /dev/null +++ b/report_qweb_decimal_precision/README.rst @@ -0,0 +1,124 @@ +============================= +Report Qweb Decimal Precision +============================= + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:88e7df18a281dcca7d418a47e7a1a6130516e0352493ba792061d9ad97cfa7a0 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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_decimal_precision + :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_decimal_precision + :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/reporting-engine&target_branch=16.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module allows administrators to define the decimal precision of +float fields for QWeb report presentation. + +**Table of contents** + +.. contents:: + :local: + +Use Cases / Context +=================== + +Odoo's QWeb reports display values of float fields based on the decimal +accuracy settings, which are global and sometimes bring inconveniences. + +For example: + +- You have some products which require a fine precision with inventory + management (e.g. liquid, powder, etc.) and you set 'Product Unit of + Measure' to 4, but you don't want to show '2.0000 Units' on your + quotation to sell assembled products (you may just want to show '2 + Units'). +- You purchase some raw materials in bulk in USD whose unit price comes + down to the 4th decimal place and you set 'Product Price' to this + level or precision, but you don't want to show the unit price of a + product as '120,000.0000' on your JPY quotation (you would just want + to show '120,000'). + +This module is designed to address these inconveniences. + +Configuration +============= + +Go to *Settings > Technical > Reporting > Decimal Precision QWeb*, and +create records according to your needs. + +For each record: + +- choose a model and a float-type field (required) +- set UoM and UoM Field, or Currency and Currency Field (optional) +- set Company (optional) +- set Digits (required) + +Usage +===== + +Print a QWeb report (quotation, invoice, purchase order, etc.), and the +value presentation for fields like line quantity and price unit are +adjusted according to the Decimal Precision QWeb 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 to smash 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 `__: + + - 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_decimal_precision/__init__.py b/report_qweb_decimal_precision/__init__.py new file mode 100644 index 0000000000..0650744f6b --- /dev/null +++ b/report_qweb_decimal_precision/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/report_qweb_decimal_precision/__manifest__.py b/report_qweb_decimal_precision/__manifest__.py new file mode 100644 index 0000000000..9f989f26b1 --- /dev/null +++ b/report_qweb_decimal_precision/__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 Decimal Precision", + "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/decimal_precision_qweb_security.xml", + "views/decimal_precision_qweb_views.xml", + ], + "installable": True, +} diff --git a/report_qweb_decimal_precision/models/__init__.py b/report_qweb_decimal_precision/models/__init__.py new file mode 100644 index 0000000000..199a30b8a8 --- /dev/null +++ b/report_qweb_decimal_precision/models/__init__.py @@ -0,0 +1,2 @@ +from . import decimal_precision_qweb +from . import ir_qweb_fields diff --git a/report_qweb_decimal_precision/models/decimal_precision_qweb.py b/report_qweb_decimal_precision/models/decimal_precision_qweb.py new file mode 100644 index 0000000000..4ff7ef1ce0 --- /dev/null +++ b/report_qweb_decimal_precision/models/decimal_precision_qweb.py @@ -0,0 +1,59 @@ +# 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 DecimalPrecisioQweb(models.Model): + _name = "decimal.precision.qweb" + _description = "Decimal Precision Qweb" + _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), ('ttype', '=', 'float')]", + string="Field", + ondelete="cascade", + required=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", + ) + digits = fields.Integer(required=True) + 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.name] == self.uom_id: + score += 1 + else: + return -1 + if self.currency_id: + if record[self.currency_field_id.name] == self.currency_id: + score += 1 + else: + return -1 + return score diff --git a/report_qweb_decimal_precision/models/ir_qweb_fields.py b/report_qweb_decimal_precision/models/ir_qweb_fields.py new file mode 100644 index 0000000000..774f2ba52b --- /dev/null +++ b/report_qweb_decimal_precision/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: + dp_qweb_recs = self.env["decimal.precision.qweb"].search( + [("res_model_name", "=", record._name), ("field_name", "=", field_name)] + ) + precision_rec = max( + dp_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_decimal_precision/readme/CONFIGURE.md b/report_qweb_decimal_precision/readme/CONFIGURE.md new file mode 100644 index 0000000000..8680d03469 --- /dev/null +++ b/report_qweb_decimal_precision/readme/CONFIGURE.md @@ -0,0 +1,9 @@ +Go to *Settings > Technical > Reporting > Decimal Precision QWeb*, and create records +according to your needs. + +For each record: + +* choose a model and a float-type field (required) +* set UoM and UoM Field, or Currency and Currency Field (optional) +* set Company (optional) +* set Digits (required) diff --git a/report_qweb_decimal_precision/readme/CONTEXT.md b/report_qweb_decimal_precision/readme/CONTEXT.md new file mode 100644 index 0000000000..ad13c3a377 --- /dev/null +++ b/report_qweb_decimal_precision/readme/CONTEXT.md @@ -0,0 +1,15 @@ +Odoo's QWeb reports display values of float fields based on the decimal accuracy +settings, which are global and sometimes bring inconveniences. + +For example: + +* You have some products which require a fine precision with inventory management (e.g. + liquid, powder, etc.) and you set 'Product Unit of Measure' to 4, but you don't want + to show '2.0000 Units' on your quotation to sell assembled products (you may just want + to show '2 Units'). +* You purchase some raw materials in bulk in USD whose unit price comes down to the 4th + decimal place and you set 'Product Price' to this level or precision, but you don't + want to show the unit price of a product as '120,000.0000' on your JPY quotation (you + would just want to show '120,000'). + +This module is designed to address these inconveniences. diff --git a/report_qweb_decimal_precision/readme/CONTRIBUTORS.md b/report_qweb_decimal_precision/readme/CONTRIBUTORS.md new file mode 100644 index 0000000000..0b0707926e --- /dev/null +++ b/report_qweb_decimal_precision/readme/CONTRIBUTORS.md @@ -0,0 +1,3 @@ +* [Quartile Limited](https://www.quartile.co): + * Yoshi Tashiro + * Aung Ko Ko Lin diff --git a/report_qweb_decimal_precision/readme/DESCRIPTION.md b/report_qweb_decimal_precision/readme/DESCRIPTION.md new file mode 100644 index 0000000000..45e6b1dc48 --- /dev/null +++ b/report_qweb_decimal_precision/readme/DESCRIPTION.md @@ -0,0 +1,2 @@ +This module allows administrators to define the decimal precision of float fields for +QWeb report presentation. diff --git a/report_qweb_decimal_precision/readme/USAGE.md b/report_qweb_decimal_precision/readme/USAGE.md new file mode 100644 index 0000000000..bd4412a81c --- /dev/null +++ b/report_qweb_decimal_precision/readme/USAGE.md @@ -0,0 +1,6 @@ +Print a QWeb report (quotation, invoice, purchase order, etc.), and the value +presentation for fields like line quantity and price unit are adjusted according to the +Decimal Precision QWeb configuration. + +Note that among matching config records, the one with the strictest condition will be +adopted. diff --git a/report_qweb_decimal_precision/security/decimal_precision_qweb_security.xml b/report_qweb_decimal_precision/security/decimal_precision_qweb_security.xml new file mode 100644 index 0000000000..2060b18d4b --- /dev/null +++ b/report_qweb_decimal_precision/security/decimal_precision_qweb_security.xml @@ -0,0 +1,10 @@ + + + + Decimal Precision QWeb Multi-company + + ['|', ('company_id', 'in', company_ids), ('company_id', '=', False)] + + diff --git a/report_qweb_decimal_precision/security/ir.model.access.csv b/report_qweb_decimal_precision/security/ir.model.access.csv new file mode 100644 index 0000000000..f53f64ac90 --- /dev/null +++ b/report_qweb_decimal_precision/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_decimal_precision_qweb_user,access.decimal.precisoon.qweb.user,model_decimal_precision_qweb,base.group_user,1,0,0,0 +access_decimal_precision_qweb_admin,access.decimal.precisoon.qweb.admin,model_decimal_precision_qweb,base.group_system,1,1,1,1 diff --git a/report_qweb_decimal_precision/static/description/index.html b/report_qweb_decimal_precision/static/description/index.html new file mode 100644 index 0000000000..bcab6fbb63 --- /dev/null +++ b/report_qweb_decimal_precision/static/description/index.html @@ -0,0 +1,468 @@ + + + + + + +Report Qweb Decimal Precision + + + +
+

Report Qweb Decimal Precision

+ + +

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

+

This module allows administrators to define the decimal precision of +float fields for QWeb report presentation.

+

Table of contents

+ +
+

Use Cases / Context

+

Odoo’s QWeb reports display values of float fields based on the decimal +accuracy settings, which are global and sometimes bring inconveniences.

+

For example:

+
    +
  • You have some products which require a fine precision with inventory +management (e.g. liquid, powder, etc.) and you set ‘Product Unit of +Measure’ to 4, but you don’t want to show ‘2.0000 Units’ on your +quotation to sell assembled products (you may just want to show ‘2 +Units’).
  • +
  • You purchase some raw materials in bulk in USD whose unit price comes +down to the 4th decimal place and you set ‘Product Price’ to this +level or precision, but you don’t want to show the unit price of a +product as ‘120,000.0000’ on your JPY quotation (you would just want +to show ‘120,000’).
  • +
+

This module is designed to address these inconveniences.

+
+
+

Configuration

+

Go to Settings > Technical > Reporting > Decimal Precision QWeb, and +create records according to your needs.

+

For each record:

+
    +
  • choose a model and a float-type field (required)
  • +
  • set UoM and UoM Field, or Currency and Currency Field (optional)
  • +
  • set Company (optional)
  • +
  • set Digits (required)
  • +
+
+
+

Usage

+

Print a QWeb report (quotation, invoice, purchase order, etc.), and the +value presentation for fields like line quantity and price unit are +adjusted according to the Decimal Precision QWeb 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 to smash 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_decimal_precision/tests/__init__.py b/report_qweb_decimal_precision/tests/__init__.py new file mode 100644 index 0000000000..f480a3c626 --- /dev/null +++ b/report_qweb_decimal_precision/tests/__init__.py @@ -0,0 +1 @@ +from . import test_report_qweb_decimal_precision diff --git a/report_qweb_decimal_precision/tests/test_report_qweb_decimal_precision.py b/report_qweb_decimal_precision/tests/test_report_qweb_decimal_precision.py new file mode 100644 index 0000000000..bd2d7900d9 --- /dev/null +++ b/report_qweb_decimal_precision/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 TestQwebDecimalPrecision(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 decimal precision record + cls.dp_qweb_rec = cls.env["decimal.precision.qweb"].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 decimal precision 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_decimal_precision/views/decimal_precision_qweb_views.xml b/report_qweb_decimal_precision/views/decimal_precision_qweb_views.xml new file mode 100644 index 0000000000..9b7bc46a8f --- /dev/null +++ b/report_qweb_decimal_precision/views/decimal_precision_qweb_views.xml @@ -0,0 +1,72 @@ + + + + decimal.precision.qweb.tree + decimal.precision.qweb + tree + + + + + + + + + + + + + + + decimal.precision.qweb.search + decimal.precision.qweb + + + + + + + + + + + + + + + + Decimal Precision QWeb + decimal.precision.qweb + tree + + + + diff --git a/setup/report_qweb_decimal_precision/odoo/addons/report_qweb_decimal_precision b/setup/report_qweb_decimal_precision/odoo/addons/report_qweb_decimal_precision new file mode 120000 index 0000000000..650b00f7ac --- /dev/null +++ b/setup/report_qweb_decimal_precision/odoo/addons/report_qweb_decimal_precision @@ -0,0 +1 @@ +../../../../report_qweb_decimal_precision \ No newline at end of file diff --git a/setup/report_qweb_decimal_precision/setup.py b/setup/report_qweb_decimal_precision/setup.py new file mode 100644 index 0000000000..28c57bb640 --- /dev/null +++ b/setup/report_qweb_decimal_precision/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)