Skip to content

Commit

Permalink
[ADD] report_qweb_decimal_precision
Browse files Browse the repository at this point in the history
  • Loading branch information
yostashiro committed Jan 22, 2024
1 parent 622a3ae commit 6c57cfe
Show file tree
Hide file tree
Showing 19 changed files with 870 additions and 0 deletions.
124 changes: 124 additions & 0 deletions report_qweb_decimal_precision/README.rst
Original file line number Diff line number Diff line change
@@ -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 <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 to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/reporting-engine/issues/new?body=module:%20report_qweb_decimal_precision%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_decimal_precision>`_ 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_decimal_precision/__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_decimal_precision/__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 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,
}
2 changes: 2 additions & 0 deletions report_qweb_decimal_precision/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import decimal_precision_qweb
from . import ir_qweb_fields
59 changes: 59 additions & 0 deletions report_qweb_decimal_precision/models/decimal_precision_qweb.py
Original file line number Diff line number Diff line change
@@ -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

Check warning on line 46 in report_qweb_decimal_precision/models/decimal_precision_qweb.py

View check run for this annotation

Codecov / codecov/patch

report_qweb_decimal_precision/models/decimal_precision_qweb.py#L46

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

Check warning on line 48 in report_qweb_decimal_precision/models/decimal_precision_qweb.py

View check run for this annotation

Codecov / codecov/patch

report_qweb_decimal_precision/models/decimal_precision_qweb.py#L48

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

Check warning on line 51 in report_qweb_decimal_precision/models/decimal_precision_qweb.py

View check run for this annotation

Codecov / codecov/patch

report_qweb_decimal_precision/models/decimal_precision_qweb.py#L51

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

Check warning on line 53 in report_qweb_decimal_precision/models/decimal_precision_qweb.py

View check run for this annotation

Codecov / codecov/patch

report_qweb_decimal_precision/models/decimal_precision_qweb.py#L53

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

Check warning on line 56 in report_qweb_decimal_precision/models/decimal_precision_qweb.py

View check run for this annotation

Codecov / codecov/patch

report_qweb_decimal_precision/models/decimal_precision_qweb.py#L56

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

Check warning on line 58 in report_qweb_decimal_precision/models/decimal_precision_qweb.py

View check run for this annotation

Codecov / codecov/patch

report_qweb_decimal_precision/models/decimal_precision_qweb.py#L58

Added line #L58 was not covered by tests
return score
21 changes: 21 additions & 0 deletions report_qweb_decimal_precision/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:
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)
9 changes: 9 additions & 0 deletions report_qweb_decimal_precision/readme/CONFIGURE.md
Original file line number Diff line number Diff line change
@@ -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)
15 changes: 15 additions & 0 deletions report_qweb_decimal_precision/readme/CONTEXT.md
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 3 additions & 0 deletions report_qweb_decimal_precision/readme/CONTRIBUTORS.md
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
2 changes: 2 additions & 0 deletions report_qweb_decimal_precision/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This module allows administrators to define the decimal precision of float fields for
QWeb report presentation.
6 changes: 6 additions & 0 deletions report_qweb_decimal_precision/readme/USAGE.md
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 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.
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="decimal_precision_qweb_company_rule" model="ir.rule">
<field name="name">Decimal Precision QWeb Multi-company</field>
<field name="model_id" ref="model_decimal_precision_qweb" />
<field
name="domain_force"
>['|', ('company_id', 'in', company_ids), ('company_id', '=', False)]</field>
</record>
</odoo>
3 changes: 3 additions & 0 deletions report_qweb_decimal_precision/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_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
Loading

0 comments on commit 6c57cfe

Please sign in to comment.