+
-
+
diff --git a/addons/uom/data/uom_data.xml b/addons/uom/data/uom_data.xml
index badda0920c1..aab34756533 100644
--- a/addons/uom/data/uom_data.xml
+++ b/addons/uom/data/uom_data.xml
@@ -6,23 +6,18 @@
-->
Unit
- unit
Weight
- weight
Working Time
- working_time
Length / Distance
- length
Volume
- volume
diff --git a/addons/uom/models/uom_uom.py b/addons/uom/models/uom_uom.py
index 5d18143b4f3..c5d105c0fb2 100644
--- a/addons/uom/models/uom_uom.py
+++ b/addons/uom/models/uom_uom.py
@@ -10,20 +10,11 @@ class UoMCategory(models.Model):
_description = 'Product UoM Categories'
name = fields.Char('Unit of Measure Category', required=True, translate=True)
- measure_type = fields.Selection([
- ('unit', 'Default Units'),
- ('weight', 'Default Weight'),
- ('working_time', 'Default Working Time'),
- ('length', 'Default Length'),
- ('volume', 'Default Volume'),
- ], string="Type of Measure")
-
- _sql_constraints = [
- ('uom_category_unique_type', 'UNIQUE(measure_type)', 'You can have only one category per measurement type.'),
- ]
def unlink(self):
- if self.filtered(lambda categ: categ.measure_type == 'working_time'):
+ uom_categ_unit = self.env.ref('uom.product_uom_categ_unit')
+ uom_categ_wtime = self.env.ref('uom.uom_categ_wtime')
+ if any(categ.id in (uom_categ_unit + uom_categ_wtime).ids for categ in self):
raise UserError(_("You cannot delete this UoM Category as it is used by the system."))
return super(UoMCategory, self).unlink()
@@ -54,7 +45,6 @@ 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!'),
@@ -93,6 +83,15 @@ class UoM(models.Model):
if uom_data['uom_count'] > 1:
raise ValidationError(_("UoM category %s should only have one reference unit of measure.") % (self.env['uom.category'].browse(uom_data['category_id']).name,))
+ @api.constrains('category_id')
+ def _validate_uom_category(self):
+ for uom in self:
+ reference_uoms = self.env['uom.uom'].search([
+ ('category_id', '=', uom.category_id.id),
+ ('uom_type', '=', 'reference')])
+ if len(reference_uoms) > 1:
+ raise ValidationError(_("UoM category %s should only have one reference unit of measure.") % (self.category_id.name))
+
@api.model_create_multi
def create(self, vals_list):
for values in vals_list:
@@ -108,7 +107,9 @@ class UoM(models.Model):
return super(UoM, self).write(values)
def unlink(self):
- if self.filtered(lambda uom: uom.measure_type == 'working_time'):
+ uom_categ_unit = self.env.ref('uom.product_uom_categ_unit')
+ uom_categ_wtime = self.env.ref('uom.uom_categ_wtime')
+ if any(uom.category_id.id in (uom_categ_unit + uom_categ_wtime).ids and uom.uom_type == 'reference' for uom in self):
raise UserError(_("You cannot delete this UoM as it is used by the system. You should rather archive it."))
return super(UoM, self).unlink()
diff --git a/addons/uom/tests/test_uom.py b/addons/uom/tests/test_uom.py
index 935bb6a12cb..48728b18bcd 100644
--- a/addons/uom/tests/test_uom.py
+++ b/addons/uom/tests/test_uom.py
@@ -51,7 +51,7 @@ class TestUom(TransactionCase):
def test_30_reference_uniqueness(self):
""" Check the uniqueness of the reference UoM in a category """
- time_category = self.env['uom.category'].search([('measure_type', '=', 'working_time')], limit=1)
+ time_category = self.env.ref('uom.product_uom_categ_unit')
with self.assertRaises(ValidationError):
self.env['uom.uom'].create({
@@ -62,12 +62,6 @@ class TestUom(TransactionCase):
'category_id': time_category.id
})
- def test_31_reference_uniqueness(self):
- """ Check the uniqueness of the reference UoM in a category """
- time_category = self.env['uom.category'].search([('measure_type', '=', 'working_time')], limit=1)
- with self.assertRaises(ValidationError):
- self.env['uom.uom'].search([('measure_type', '=', 'unit'), ('uom_type', '=', 'reference')]).category_id = time_category
-
def test_40_custom_uom(self):
""" A custom UoM is an UoM in a category without measurement type. It should behave like a normal UoM """
category = self.env['uom.category'].create({
diff --git a/addons/uom/views/uom_uom_views.xml b/addons/uom/views/uom_uom_views.xml
index 84154a055f7..cf835c1dfb1 100644
--- a/addons/uom/views/uom_uom_views.xml
+++ b/addons/uom/views/uom_uom_views.xml
@@ -90,7 +90,6 @@
-
@@ -103,7 +102,6 @@
-