diff options
| author | jem-odoo <jem@openerp.com> | 2018-03-02 10:42:07 +0100 |
|---|---|---|
| committer | jem-odoo <jem@openerp.com> | 2018-04-09 10:13:06 +0200 |
| commit | 2d32ff4bb8a31184285cb979e8c491949249d4ca (patch) | |
| tree | 5ab76608ef4f691e40e90e919c9d3cb31668e37f /addons | |
| parent | c71ce46b0eb3509b6805421915a8652bafd3bbbc (diff) | |
[IMP] uom: measurement type on category
In order to restrict some uom field to some uom category, it is required to
add a measurement type on uom.category. Also, this will lessen the use of
master data.
Some technical justifications:
- measure_type is not required, to allow people creating their own
uom.category. Those ones will not interfere with addons business.
- measure_type is unique: we restrict to only one category containing
time, weight, ... in order to avoid mismatch category during conversion.
(2 different weight categories can not be converted).
- measure_type on uom.uom is stored: to allow search and domain on
field, we need to store it. Usually, UoM are not updated every day,
and the number of uom in a database is not that important.
To illustrate this mecanism, a domain is added on
'project_time_mode_id', in project module. This field
is use as timesheet default uom, and we obviously want to
restrict it to time category (timesheeting in liter does
not make sense ...)
Task #1820076
Diffstat (limited to 'addons')
| -rw-r--r-- | addons/project/data/project_data.xml | 5 | ||||
| -rw-r--r-- | addons/project/models/res_company.py | 13 | ||||
| -rw-r--r-- | addons/uom/data/uom_data.xml | 5 | ||||
| -rw-r--r-- | addons/uom/models/uom_uom.py | 12 | ||||
| -rw-r--r-- | addons/uom/views/uom_uom_views.xml | 1 |
5 files changed, 29 insertions, 7 deletions
diff --git a/addons/project/data/project_data.xml b/addons/project/data/project_data.xml index 127b5b1366b..3103992a4e6 100644 --- a/addons/project/data/project_data.xml +++ b/addons/project/data/project_data.xml @@ -2,11 +2,6 @@ <odoo> <data noupdate="1"> - <!-- This will set the unit of measure used in projects and tasks.--> - <record id="base.main_company" model="res.company"> - <field name="project_time_mode_id" ref="uom.product_uom_hour"></field> - </record> - <!-- sale_timesheet and project define the same field without depending on each, which causes the field to be deleted when the module that created it is deleted. To avoid this, we create xmlids manually for this field in both modules to prevent accidental deletion. To fix in saas-7 by moving the field definition--> <record id="duplicate_field_xmlid" model="ir.model.data"> diff --git a/addons/project/models/res_company.py b/addons/project/models/res_company.py index 550bff5ad53..afad6d1b54e 100644 --- a/addons/project/models/res_company.py +++ b/addons/project/models/res_company.py @@ -1,14 +1,23 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -from odoo import fields, models +from odoo import api, fields, models class ResCompany(models.Model): _inherit = 'res.company' + @api.model + def _default_project_time_mode_id(self): + uom = self.env.ref('uom.product_uom_hour', raise_if_not_found=False) + if not uom: + uom = self.env['uom.uom'].search([('measure_type', '=', 'time'), ('uom_type', '=', 'reference')], limit=1) + if not uom: + uom = self.env['uom.uom'].search([('measure_type', '=', 'time')], limit=1) + return uom + project_time_mode_id = fields.Many2one('uom.uom', string='Project Time Unit', - default=lambda s: s.env.ref('uom.product_uom_hour', raise_if_not_found=False), + default=_default_project_time_mode_id, domain=[('measure_type', '=', 'time')], help="This will set the unit of measure used in projects and tasks.\n" "If you use the timesheet linked to projects, don't " "forget to setup the right unit of measure in your employees.") diff --git a/addons/uom/data/uom_data.xml b/addons/uom/data/uom_data.xml index f929564b1e4..59a2c41208c 100644 --- a/addons/uom/data/uom_data.xml +++ b/addons/uom/data/uom_data.xml @@ -6,18 +6,23 @@ --> <record id="product_uom_categ_unit" model="uom.category"> <field name="name">Unit</field> + <field name="measure_type">unit</field> </record> <record id="product_uom_categ_kgm" model="uom.category"> <field name="name">Weight</field> + <field name="measure_type">weight</field> </record> <record id="uom_categ_wtime" model="uom.category"> <field name="name">Working Time</field> + <field name="measure_type">time</field> </record> <record id="uom_categ_length" model="uom.category"> <field name="name">Length / Distance</field> + <field name="measure_type">length</field> </record> <record id="product_uom_categ_vol" model="uom.category"> <field name="name">Volume</field> + <field name="measure_type">volume</field> </record> <!-- Resource: uom.uom diff --git a/addons/uom/models/uom_uom.py b/addons/uom/models/uom_uom.py index 2c01624b5c6..9608894e485 100644 --- a/addons/uom/models/uom_uom.py +++ b/addons/uom/models/uom_uom.py @@ -10,6 +10,17 @@ class UoMCategory(models.Model): _description = 'Product UoM Categories' name = fields.Char('Name', required=True, translate=True) + measure_type = fields.Selection([ + ('unit', 'Units'), + ('weight', 'Weight'), + ('time', 'Time'), + ('length', 'Length'), + ('volume', 'Volume'), + ], string="Type of Measure") + + _sql_constraints = [ + ('uom_category_unique_type', 'UNIQUE(measure_type)', 'You can have only one category per measurement type.'), + ] class UoM(models.Model): @@ -38,6 +49,7 @@ class UoM(models.Model): ('reference', 'Reference Unit of Measure for this category'), ('smaller', 'Smaller than the reference Unit of Measure')], 'Type', default='reference', required=1) + measure_type = fields.Selection(string="Type of measurement category", related='category_id.measure_type', store=True, readonly=True) _sql_constraints = [ ('factor_gt_zero', 'CHECK (factor!=0)', 'The conversion ratio for a unit of measure cannot be 0!'), diff --git a/addons/uom/views/uom_uom_views.xml b/addons/uom/views/uom_uom_views.xml index 582ae8174c2..59acc96e86f 100644 --- a/addons/uom/views/uom_uom_views.xml +++ b/addons/uom/views/uom_uom_views.xml @@ -68,6 +68,7 @@ <form string="Units of Measure categories"> <group> <field name="name"/> + <field name="measure_type"/> </group> </form> </field> |
