diff --git a/setup/web_widget_uom/odoo/addons/web_widget_uom b/setup/web_widget_uom/odoo/addons/web_widget_uom new file mode 120000 index 000000000..120729044 --- /dev/null +++ b/setup/web_widget_uom/odoo/addons/web_widget_uom @@ -0,0 +1 @@ +../../../../web_widget_uom \ No newline at end of file diff --git a/setup/web_widget_uom/setup.py b/setup/web_widget_uom/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/web_widget_uom/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) diff --git a/web_widget_uom/README.rst b/web_widget_uom/README.rst new file mode 100644 index 000000000..30c04aa2c --- /dev/null +++ b/web_widget_uom/README.rst @@ -0,0 +1,109 @@ +============== +Web Widget UoM +============== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! 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%2Fweb-lightgray.png?logo=github + :target: https://github.com/OCA/web/tree/14.0/web_widget_uom + :alt: OCA/web + +|badge1| |badge2| |badge3| + +This module aims to allow the user to decide how many +decimal places should be displayed in the Odoo User +Interface for each Unit of Measure. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +In the UoM form view specify the number of decimal places that +should be displayed for each unit of Measure: + +.. image:: https://raw.githubusercontent.com/OCA/web/14.0/web_widget_uom/static/description/UoMDecimalPlaces.png + :alt: Decimal Places in UoM form view + +In the view declaration, put widget='uom' attribute in the field tag:: + + ... + + + ... + + ... + + + ... + +
+ ... + + ... + +
+ ... + +Widget Options:: + + ... + + + ... + + ... + + + ... + +If the UoM field in the model is not declared under the name uom_id, the +option uom_field must be specified with the appropriate field name. + +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 +======= + +Contributors +~~~~~~~~~~~~ + +* Adria Gil Sorribes +* Giovanni Serra + +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/web `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/web_widget_uom/__init__.py b/web_widget_uom/__init__.py new file mode 100644 index 000000000..fe1551806 --- /dev/null +++ b/web_widget_uom/__init__.py @@ -0,0 +1 @@ +from . import models # noqa diff --git a/web_widget_uom/__manifest__.py b/web_widget_uom/__manifest__.py new file mode 100644 index 000000000..aa900b959 --- /dev/null +++ b/web_widget_uom/__manifest__.py @@ -0,0 +1,19 @@ +{ + "name": "Web Widget UoM", + "summary": """Allow user to to decide how many decimal places should be displayed for UoM. + """, + "category": "web", + "version": "14.0.1.0.0", + "author": "Odoo Community Association (OCA), Giovanni Serra", + "license": "AGPL-3", + "website": "https://github.com/OCA/web", + "depends": ["web", "uom", "product"], + "data": [ + "views/uom_uom_views.xml", + "views/assets.xml", + ], + "auto_install": False, + "application": False, + "installable": True, + "web_preload": True, +} diff --git a/web_widget_uom/i18n/it.po b/web_widget_uom/i18n/it.po new file mode 100644 index 000000000..e69de29bb diff --git a/web_widget_uom/models/__init__.py b/web_widget_uom/models/__init__.py new file mode 100644 index 000000000..7753e8e76 --- /dev/null +++ b/web_widget_uom/models/__init__.py @@ -0,0 +1 @@ +from . import uom_uom diff --git a/web_widget_uom/models/uom_uom.py b/web_widget_uom/models/uom_uom.py new file mode 100644 index 000000000..968b3cb96 --- /dev/null +++ b/web_widget_uom/models/uom_uom.py @@ -0,0 +1,60 @@ +# Copyright 2019 Eficent Business and IT Consulting Services S.L. +# (http://www.eficent.com) +# Copyright 2022 - Giovanni Serra +import decimal + +from odoo import _, api, fields, models + + +class UoM(models.Model): + _inherit = "uom.uom" + + decimal_places = fields.Integer( + string="Decimal Places", + default=2, + ) + + show_only_inputed_decimals = fields.Boolean( + "Show only inputed decimals", + default=False, + help="It shows only inputed decimals up to Decimal Places", + ) + + @api.onchange("decimal_places") + def _onchange_decimal_places(self): + decimal_accuracy = self.env["decimal.precision"].precision_get( + "Product Unit of Measure" + ) + + if self.decimal_places > decimal_accuracy: + return { + "warning": { + "title": _("Warning!"), + "message": _( + "The Decimal places is higher than the Decimal Accuracy" + " (%s digits).\nThis may cause inconsistencies in computations.\n" + "Please set Decimal Places between 0 and %s." + ) + % (decimal_accuracy, decimal_accuracy), + } + } + + _sql_constraints = [ + ( + "uom_decimal_places_positive", + "CHECK(decimal_places >= 0)", + "Decimal places must be strictly bigger or equal than zero", + ), + ] + + def get_decimal_places(self, uom_id, value): + uom = self.env["uom.uom"].browse(uom_id) + decimal_places = uom.decimal_places + if uom.show_only_inputed_decimals: + digits = ( + 0 + if isinstance(value, int) + else abs(decimal.Decimal(str(value)).as_tuple().exponent) + ) + decimal_places = min(decimal_places, digits) + return decimal_places diff --git a/web_widget_uom/readme/CONTRIBUTORS.rst b/web_widget_uom/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..c8cdb8cd3 --- /dev/null +++ b/web_widget_uom/readme/CONTRIBUTORS.rst @@ -0,0 +1,2 @@ +* Adria Gil Sorribes +* Giovanni Serra diff --git a/web_widget_uom/readme/DESCRIPTION.rst b/web_widget_uom/readme/DESCRIPTION.rst new file mode 100644 index 000000000..c265fe666 --- /dev/null +++ b/web_widget_uom/readme/DESCRIPTION.rst @@ -0,0 +1,6 @@ +This module allows the user to decide maximum +how many decimal places should be displayed in the Odoo User +Interface for each Unit of Measure. + +If "Show only inputed decimals" is enabled on the UoM, +the widget will show inputed decimals up to Decimal Places. diff --git a/web_widget_uom/readme/ROADMAP.rst b/web_widget_uom/readme/ROADMAP.rst new file mode 100644 index 000000000..c46f1cf43 --- /dev/null +++ b/web_widget_uom/readme/ROADMAP.rst @@ -0,0 +1,3 @@ +TODO + +1. Add caching by uom.id diff --git a/web_widget_uom/readme/USAGE.rst b/web_widget_uom/readme/USAGE.rst new file mode 100644 index 000000000..52e836715 --- /dev/null +++ b/web_widget_uom/readme/USAGE.rst @@ -0,0 +1,40 @@ +In the UoM form view specify the number of decimal places that +should be displayed for each unit of Measure: + +.. image:: ../static/description/UoMDecimalPlaces.png + :alt: Decimal Places in UoM form view + +In the view declaration, put widget='uom' attribute in the field tag:: + + ... + + + ... + + ... + + + ... + +
+ ... + + ... + +
+ ... + +Widget Options:: + + ... + + + ... + + ... + + + ... + +If the UoM field in the model is not declared under the name uom_id, the +option uom_field must be specified with the appropriate field name. diff --git a/web_widget_uom/static/description/UoMDecimalPlaces.png b/web_widget_uom/static/description/UoMDecimalPlaces.png new file mode 100644 index 000000000..dd4bfb226 Binary files /dev/null and b/web_widget_uom/static/description/UoMDecimalPlaces.png differ diff --git a/web_widget_uom/static/description/index.html b/web_widget_uom/static/description/index.html new file mode 100644 index 000000000..6b248a0aa --- /dev/null +++ b/web_widget_uom/static/description/index.html @@ -0,0 +1,456 @@ + + + + + + +Web Widget UoM + + + +
+

Web Widget UoM

+ + +

Beta License: AGPL-3 OCA/web

+

This module aims to allow the user to decide how many +decimal places should be displayed in the Odoo User +Interface for each Unit of Measure.

+

Table of contents

+ +
+

Usage

+

In the UoM form view specify the number of decimal places that +should be displayed for each unit of Measure:

+Decimal Places in UoM form view +

In the view declaration, put widget=’uom’ attribute in the field tag:

+
+...
+<field name="arch" type="xml">
+    <tree string="View name">
+        ...
+        <field name="" widget="uom"/>
+        ...
+    </tree>
+</field>
+...
+<field name="arch" type="xml">
+    <form string="View name">
+        ...
+        <field name="" widget="uom"/>
+        ...
+    </form>
+</field>
+...
+
+

Widget Options:

+
+...
+<field name="arch" type="xml">
+    <tree string="View name">
+        ...
+        <field name="color" widget="color" options="{'uom_field': 'product_uom'}"/>
+        ...
+    </tree>
+</field>
+...
+
+

If the UoM field in the model is not declared under the name uom_id, the +option uom_field must be specified with the appropriate field name.

+
+
+

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

+
+

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/web project on GitHub.

+

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

+
+
+
+ + diff --git a/web_widget_uom/static/src/js/widget.js b/web_widget_uom/static/src/js/widget.js new file mode 100644 index 000000000..c24138556 --- /dev/null +++ b/web_widget_uom/static/src/js/widget.js @@ -0,0 +1,44 @@ +/* Copyright 2022 Giovanni Serra +License LGPLv3.0 or later (https://www.gnu.org/licenses/lgpl-3.0.en.html). */ + +/* Global jscolor */ +odoo.define("web.web_widget_uom", function (require) { + "use strict"; + + var basic_fields = require("web.basic_fields"); + var field_registry = require("web.field_registry"); + + var FieldUoM = basic_fields.FieldFloat.extend({ + willStart: function () { + return Promise.all([ + this._getDecimalPlaces(), + this._super.apply(this, arguments), + ]); + }, + _getDecimalPlaces: function () { + var self = this; + var uomField = + this.nodeOptions.uom_field || this.field.uom_field || "uom_id"; + var uomID = this.record.data[uomField] && this.record.data[uomField].res_id; + + var qtyField = this.nodeOptions.qty_field || "product_uom_qty"; + var quantity = this.record.data[qtyField] || 0.0; + + return this._rpc({ + model: "uom.uom", + method: "get_decimal_places", + args: [, uomID, quantity], + }).then(function (result) { + if (result !== null) { + self.nodeOptions.digits = [32, result]; + } + }); + }, + }); + + field_registry.add("uom", FieldUoM); + + return { + FieldUoM: FieldUoM, + }; +}); diff --git a/web_widget_uom/tests/__init__.py b/web_widget_uom/tests/__init__.py new file mode 100644 index 000000000..418fe18ae --- /dev/null +++ b/web_widget_uom/tests/__init__.py @@ -0,0 +1 @@ +from . import test_decimal_precision diff --git a/web_widget_uom/tests/test_decimal_precision.py b/web_widget_uom/tests/test_decimal_precision.py new file mode 100644 index 000000000..89af3515c --- /dev/null +++ b/web_widget_uom/tests/test_decimal_precision.py @@ -0,0 +1,14 @@ +from odoo.tests import common + + +class TestWebWidgetUom(common.TransactionCase): + def test_web_widget_uom(self): + Uom = self.env["uom.uom"] + uom_unit = self.env.ref("uom.product_uom_unit") + + uom_unit.write({"decimal_places": 5}) + self.assertTrue(isinstance(uom_unit._onchange_decimal_places(), dict)) + self.assertEqual(Uom.get_decimal_places(uom_unit.id, 0.1234567), 5) + + uom_unit.write({"show_only_inputed_decimals": True}) + self.assertEqual(Uom.get_decimal_places(uom_unit.id, 0.1), 1) diff --git a/web_widget_uom/views/assets.xml b/web_widget_uom/views/assets.xml new file mode 100644 index 000000000..119e6e773 --- /dev/null +++ b/web_widget_uom/views/assets.xml @@ -0,0 +1,15 @@ + + +