From b51f2d5a6b6358b3ef5b6e326afb982898156546 Mon Sep 17 00:00:00 2001 From: SimoRubi Date: Wed, 9 Jan 2019 18:05:14 +0100 Subject: [PATCH] [ADD] quality_control_formula --- quality_control_formula/README.rst | 89 ++++ quality_control_formula/__init__.py | 4 + quality_control_formula/__manifest__.py | 23 + quality_control_formula/i18n/it.po | 90 ++++ quality_control_formula/models/__init__.py | 6 + .../models/qc_inspection.py | 24 + .../models/qc_inspection_line.py | 62 +++ .../models/qc_test_question.py | 18 + .../readme/DESCRIPTION.rst | 1 + quality_control_formula/readme/USAGE.rst | 17 + .../static/description/index.html | 435 ++++++++++++++++++ quality_control_formula/tests/__init__.py | 5 + .../tests/test_quality_control_formula.py | 79 ++++ .../views/qc_inspection_line_views.xml | 22 + .../views/qc_inspection_views.xml | 49 ++ .../views/qc_test_question_views.xml | 49 ++ .../views/qc_test_views.xml | 18 + 17 files changed, 991 insertions(+) create mode 100644 quality_control_formula/README.rst create mode 100644 quality_control_formula/__init__.py create mode 100644 quality_control_formula/__manifest__.py create mode 100644 quality_control_formula/i18n/it.po create mode 100644 quality_control_formula/models/__init__.py create mode 100644 quality_control_formula/models/qc_inspection.py create mode 100644 quality_control_formula/models/qc_inspection_line.py create mode 100644 quality_control_formula/models/qc_test_question.py create mode 100644 quality_control_formula/readme/DESCRIPTION.rst create mode 100644 quality_control_formula/readme/USAGE.rst create mode 100644 quality_control_formula/static/description/index.html create mode 100644 quality_control_formula/tests/__init__.py create mode 100644 quality_control_formula/tests/test_quality_control_formula.py create mode 100644 quality_control_formula/views/qc_inspection_line_views.xml create mode 100644 quality_control_formula/views/qc_inspection_views.xml create mode 100644 quality_control_formula/views/qc_test_question_views.xml create mode 100644 quality_control_formula/views/qc_test_views.xml diff --git a/quality_control_formula/README.rst b/quality_control_formula/README.rst new file mode 100644 index 000000000..4a9704b88 --- /dev/null +++ b/quality_control_formula/README.rst @@ -0,0 +1,89 @@ +======================= +Quality control formula +======================= + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! 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%2Fmanufacture-lightgray.png?logo=github + :target: https://github.com/OCA/manufacture/tree/10.0/quality_control_formula + :alt: OCA/manufacture +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/manufacture-10-0/manufacture-10-0-quality_control_formula + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/129/10.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Allow the user to use formulas to get the quantitative / qualitative value for an inspection line. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +1. Create a new test with one or more questions; + +2. In a test question, fill in the *formula* field with some python computation, the values that can be used are: + + * *input_value*: a new field of the inspection line; + * variables from the previous inspection lines (use the handle to change the order), + where the variable name is given by the *code* field; + +3. Create a new inspection from the test; + +4. The inspection lines are recomputed as soon as any field concerning the formula is modified. + + The inspection lines can also be recomputed with the *Compute lines* button in the inspection, + or the *Compute* button on each line. + +Note that formula dependencies can be managed in the new menu *Quality control > Test > Test questions*, +the sequence value will be maintained when the question becomes an inspection. + +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 +~~~~~~~ + +* Agile Business Group + +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/manufacture `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/quality_control_formula/__init__.py b/quality_control_formula/__init__.py new file mode 100644 index 000000000..83f4ade7a --- /dev/null +++ b/quality_control_formula/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from . import models diff --git a/quality_control_formula/__manifest__.py b/quality_control_formula/__manifest__.py new file mode 100644 index 000000000..fa9240557 --- /dev/null +++ b/quality_control_formula/__manifest__.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- +# Copyright 2019 Simone Rubino - Agile Business Group +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +{ + "name": "Quality control formula", + "summary": "Use formulas in inspection lines.", + "version": "10.0.1.0.0", + "development_status": "Beta", + "category": "Quality control", + "website": "https://github.com/OCA/manufacture/tree/" + "10.0/quality_control_formula", + "author": "Agile Business Group, Odoo Community Association (OCA)", + "license": "AGPL-3", + "depends": [ + "quality_control", + ], + "data": [ + "views/qc_inspection_line_views.xml", + "views/qc_inspection_views.xml", + "views/qc_test_question_views.xml", + "views/qc_test_views.xml" + ] +} diff --git a/quality_control_formula/i18n/it.po b/quality_control_formula/i18n/it.po new file mode 100644 index 000000000..36e844ec7 --- /dev/null +++ b/quality_control_formula/i18n/it.po @@ -0,0 +1,90 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * quality_control_formula +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 10.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2019-01-09 17:11+0000\n" +"PO-Revision-Date: 2019-01-09 17:11+0000\n" +"Last-Translator: Simone Rubino \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: quality_control_formula +#: model:ir.model.fields,field_description:quality_control_formula.field_qc_inspection_line_code +#: model:ir.model.fields,field_description:quality_control_formula.field_qc_test_question_code +msgid "Code" +msgstr "Codice" + +#. module: quality_control_formula +#: model:ir.ui.view,arch_db:quality_control_formula.qc_inspection_form_view +msgid "Compute" +msgstr "Calcola" + +#. module: quality_control_formula +#: model:ir.ui.view,arch_db:quality_control_formula.qc_inspection_form_view +msgid "Compute lines" +msgstr "Calcola righe" + +#. module: quality_control_formula +#: model:ir.model.fields,field_description:quality_control_formula.field_qc_inspection_line_formula +#: model:ir.model.fields,field_description:quality_control_formula.field_qc_test_question_formula +msgid "Formula" +msgstr "Formula" + +#. module: quality_control_formula +#: model:ir.model.fields,field_description:quality_control_formula.field_qc_inspection_line_input_value +msgid "Input value" +msgstr "Valore in input" + +#. module: quality_control_formula +#: model:ir.model.fields,help:quality_control_formula.field_qc_inspection_line_formula +msgid "Python formula for evaluating the current inspection line.Provided values are 'input_value' and the values of all the previous inspection lines (the variable name is the 'code' field)." +msgstr "Formula Python per valutare la riga di ispezione.I valori disponibili sono 'input_value' e i valori di tutte le righe di ispezione precedenti (il nome della variabile equivale al campo 'codice')." + +#. module: quality_control_formula +#: model:ir.model.fields,help:quality_control_formula.field_qc_test_question_formula +msgid "Python formula for evaluating the current question.Provided values are 'input_value' and the values of all the previous questions (the variable name is the 'code' field)." +msgstr "Formula Python formula per valutare la domanda del test.I valori disponibili sono 'input_value' e i valori di tutte le domande precedenti (il nome della variabile equivale al campo 'codice')." + +#. module: quality_control_formula +#: model:ir.model,name:quality_control_formula.model_qc_inspection +msgid "Quality control inspection" +msgstr "Ispezione controllo di qualità" + +#. module: quality_control_formula +#: model:ir.model,name:quality_control_formula.model_qc_inspection_line +msgid "Quality control inspection line" +msgstr "Riga ispezione controllo qualità" + +#. module: quality_control_formula +#: model:ir.model,name:quality_control_formula.model_qc_test_question +msgid "Quality control question" +msgstr "Domanda di controllo qualità" + +#. module: quality_control_formula +#: model:ir.model.fields,field_description:quality_control_formula.field_qc_inspection_line_sequence +msgid "Sequence" +msgstr "Sequenza" + +#. module: quality_control_formula +#: model:ir.actions.act_window,name:quality_control_formula.action_qc_test_questions +#: model:ir.ui.menu,name:quality_control_formula.qc_test_questions_menu +#: model:ir.ui.view,arch_db:quality_control_formula.qc_test_question_tree_view +msgid "Test questions" +msgstr "Domande dei test" + +#. module: quality_control_formula +#: model:ir.model.fields,help:quality_control_formula.field_qc_inspection_line_code +msgid "Used for the evaluation of formulas: name of the variable corresponding to this inspection line's value." +msgstr "Utilizzato per la valutazione delle formule: il nome della variabile corrispondente al valore di questa ispezione." + +#. module: quality_control_formula +#: model:ir.model.fields,help:quality_control_formula.field_qc_test_question_code +msgid "Used for the evaluation of formulas: name of the variable corresponding to this question's value." +msgstr "Utilizzato per la valutazione delle formule: il nome della variabile corrispondente al valore di questa domanda." diff --git a/quality_control_formula/models/__init__.py b/quality_control_formula/models/__init__.py new file mode 100644 index 000000000..6af62e75e --- /dev/null +++ b/quality_control_formula/models/__init__.py @@ -0,0 +1,6 @@ +# -*- coding: utf-8 -*- +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from . import qc_inspection +from . import qc_inspection_line +from . import qc_test_question diff --git a/quality_control_formula/models/qc_inspection.py b/quality_control_formula/models/qc_inspection.py new file mode 100644 index 000000000..96c1ee3ad --- /dev/null +++ b/quality_control_formula/models/qc_inspection.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +# Copyright 2019 Simone Rubino - Agile Business Group +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from odoo import api, models + + +class QcInspection(models.Model): + _inherit = 'qc.inspection' + + @api.multi + def _prepare_inspection_line(self, test, line, fill=None): + line_data = super(QcInspection, self) \ + ._prepare_inspection_line(test, line, fill=fill) + line_data.update({ + 'code': line.code, + 'sequence': line.sequence, + 'formula': line.formula + }) + return line_data + + @api.multi + def calculate_lines_values(self): + self.mapped('inspection_lines').calculate_value() diff --git a/quality_control_formula/models/qc_inspection_line.py b/quality_control_formula/models/qc_inspection_line.py new file mode 100644 index 000000000..4f804e9a8 --- /dev/null +++ b/quality_control_formula/models/qc_inspection_line.py @@ -0,0 +1,62 @@ +# -*- coding: utf-8 -*- +# Copyright 2019 Simone Rubino - Agile Business Group +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from odoo import api, fields, models +from odoo.tools.safe_eval import safe_eval + + +class QcInspectionLine(models.Model): + _inherit = 'qc.inspection.line' + _order = 'sequence, id' + + code = fields.Char( + help="Used for the evaluation of formulas: " + "name of the variable corresponding to " + "this inspection line's value.") + sequence = fields.Integer() + formula = fields.Text( + help="Python formula for evaluating the current inspection line." + "Provided values are 'input_value' and " + "the values of all the previous inspection lines " + "(the variable name is the 'code' field).") + input_value = fields.Char() + + @api.multi + def calculate_value(self): + for line in self.filtered('formula'): + formula_res = safe_eval( + line.formula, line._prepare_formula_dict()) + if line.question_type == 'qualitative': + line.qualitative_value = formula_res + elif line.question_type == 'quantitative': + line.quantitative_value = formula_res + + @api.multi + def _prepare_formula_dict(self): + self.ensure_one() + formula_dict = {'input_value': self.input_value} + # Compute all the previous inspection lines + previous_lines = self.inspection_id.inspection_lines.filtered( + lambda l: l.sequence <= self.sequence) - self + for line in previous_lines: + line_value = 0 + if line.question_type == 'qualitative': + line_value = line.qualitative_value + elif line.question_type == 'quantitative': + line_value = line.quantitative_value + formula_dict.update({line.code: line_value}) + + return formula_dict + + @api.multi + def write(self, vals): + res = super(QcInspectionLine, self).write(vals) + # If any of the following fields is modified, + # recompute all the inspection lines of its inspection + # because the result might change / be invalid + formula_fields = ['inspection_id', + 'code', 'input_value', 'formula', 'sequence'] + if any(ff in vals for ff in formula_fields): + self.mapped('inspection_id').calculate_lines_values() + return res diff --git a/quality_control_formula/models/qc_test_question.py b/quality_control_formula/models/qc_test_question.py new file mode 100644 index 000000000..802b2f5b0 --- /dev/null +++ b/quality_control_formula/models/qc_test_question.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +# Copyright 2019 Simone Rubino - Agile Business Group +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from odoo import fields, models + + +class QcTestQuestion(models.Model): + _inherit = 'qc.test.question' + + code = fields.Char( + help="Used for the evaluation of formulas: " + "name of the variable corresponding to this question's value.") + formula = fields.Text( + help="Python formula for evaluating the current question." + "Provided values are 'input_value' and " + "the values of all the previous questions " + "(the variable name is the 'code' field).") diff --git a/quality_control_formula/readme/DESCRIPTION.rst b/quality_control_formula/readme/DESCRIPTION.rst new file mode 100644 index 000000000..ba343e84e --- /dev/null +++ b/quality_control_formula/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +Allow the user to use formulas to get the quantitative / qualitative value for an inspection line. diff --git a/quality_control_formula/readme/USAGE.rst b/quality_control_formula/readme/USAGE.rst new file mode 100644 index 000000000..eeb9a16a5 --- /dev/null +++ b/quality_control_formula/readme/USAGE.rst @@ -0,0 +1,17 @@ +1. Create a new test with one or more questions; + +2. In a test question, fill in the *formula* field with some python computation, the values that can be used are: + + * *input_value*: a new field of the inspection line; + * variables from the previous inspection lines (use the handle to change the order), + where the variable name is given by the *code* field; + +3. Create a new inspection from the test; + +4. The inspection lines are recomputed as soon as any field concerning the formula is modified. + + The inspection lines can also be recomputed with the *Compute lines* button in the inspection, + or the *Compute* button on each line. + +Note that formula dependencies can be managed in the new menu *Quality control > Test > Test questions*, +the sequence value will be maintained when the question becomes an inspection. diff --git a/quality_control_formula/static/description/index.html b/quality_control_formula/static/description/index.html new file mode 100644 index 000000000..96d9ed992 --- /dev/null +++ b/quality_control_formula/static/description/index.html @@ -0,0 +1,435 @@ + + + + + + +Quality control formula + + + +
+

Quality control formula

+ + +

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

+

Allow the user to use formulas to get the quantitative / qualitative value for an inspection line.

+

Table of contents

+ +
+

Usage

+
    +
  1. Create a new test with one or more questions;

    +
  2. +
  3. In a test question, fill in the formula field with some python computation, the values that can be used are:

    +
      +
    • input_value: a new field of the inspection line;
    • +
    • variables from the previous inspection lines (use the handle to change the order), +where the variable name is given by the code field;
    • +
    +
  4. +
  5. Create a new inspection from the test;

    +
  6. +
  7. The inspection lines are recomputed as soon as any field concerning the formula is modified.

    +

    The inspection lines can also be recomputed with the Compute lines button in the inspection, +or the Compute button on each line.

    +
  8. +
+

Note that formula dependencies can be managed in the new menu Quality control > Test > Test questions, +the sequence value will be maintained when the question becomes an inspection.

+
+
+

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

+
    +
  • Agile Business Group
  • +
+
+
+

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

+

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

+
+
+
+ + diff --git a/quality_control_formula/tests/__init__.py b/quality_control_formula/tests/__init__.py new file mode 100644 index 000000000..de2835aed --- /dev/null +++ b/quality_control_formula/tests/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# Copyright 2019 Simone Rubino - Agile Business Group +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from . import test_quality_control_formula diff --git a/quality_control_formula/tests/test_quality_control_formula.py b/quality_control_formula/tests/test_quality_control_formula.py new file mode 100644 index 000000000..a8039561c --- /dev/null +++ b/quality_control_formula/tests/test_quality_control_formula.py @@ -0,0 +1,79 @@ +# -*- coding: utf-8 -*- +# Copyright 2019 Simone Rubino - Agile Business Group +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from odoo.tests.common import TransactionCase + + +class TestQualityControlFormula(TransactionCase): + + def setUp(self): + super(TestQualityControlFormula, self).setUp() + test_model = self.env['qc.test'] + self.inspection_model = self.env['qc.inspection'] + self.wizard_model = self.env['qc.inspection.set.test'] + + self.test = test_model.create({ + 'name': 'Test', + 'test_lines': [ + (0, 0, self._prepare_question_values('A')), + (0, 0, self._prepare_question_values('B')), + (0, 0, self._prepare_question_values( + 'C', 'A + B + int(input_value)')), + (0, 0, self._prepare_question_values( + 'D', 'C * int(input_value)')) + ] + }) + + @staticmethod + def _prepare_question_values(code, formula=''): + return { + 'code': code, + 'name': 'Question ' + code, + 'formula': formula, + 'type': 'quantitative', + 'min_value': 0.0, + 'max_value': 10.0, + 'uom_id': 1 + } + + def _create_inspection(self, test): + inspection = self.inspection_model.create({'name': 'Test inspection'}) + wizard = self.wizard_model.with_context(active_id=inspection.id) \ + .create({'test': test.id}) + wizard.action_create_test() + return inspection + + def test_values_propagation(self): + inspection = self._create_inspection(self.test) + + for inspection_line in inspection.inspection_lines: + test_line = inspection_line.test_line + self.assertEqual(test_line.code, inspection_line.code) + self.assertEqual(test_line.formula, inspection_line.formula) + self.assertEqual(test_line.sequence, inspection_line.sequence) + + def test_formula_evaluation_ok(self): + inspection = self._create_inspection(self.test) + + value_a, value_b, value_c, value_d = 1, 2, "3", "4" + + # Initialize lines values + for line in inspection.inspection_lines: + if line.code == 'A': + line.quantitative_value = value_a + elif line.code == 'B': + line.quantitative_value = value_b + elif line.code == 'C': + line.input_value = value_c + elif line.code == 'D': + line.input_value = value_d + + inspection.calculate_lines_values() + + inspection_line_d = inspection.inspection_lines \ + .filtered(lambda l: l.code == 'D') + expected_res = (value_a + value_b + int(value_c)) * int(value_d) + self.assertEqual( + int(inspection_line_d.quantitative_value), + expected_res) diff --git a/quality_control_formula/views/qc_inspection_line_views.xml b/quality_control_formula/views/qc_inspection_line_views.xml new file mode 100644 index 000000000..0ff93815f --- /dev/null +++ b/quality_control_formula/views/qc_inspection_line_views.xml @@ -0,0 +1,22 @@ + + + + + + Add formula + qc.inspection.line + + + + + + + + + + + + diff --git a/quality_control_formula/views/qc_inspection_views.xml b/quality_control_formula/views/qc_inspection_views.xml new file mode 100644 index 000000000..36ad05b62 --- /dev/null +++ b/quality_control_formula/views/qc_inspection_views.xml @@ -0,0 +1,49 @@ + + + + + + Add formula + qc.inspection + + + +