Skip to content

Commit

Permalink
[IMP] packaging_uom: declare 'inverse' method on qty
Browse files Browse the repository at this point in the history
This change is required to make the code working with existing addons without breaking tets. By declaring an invers on qty, we are able to assign/create the required uom to the package based on the expected qty
  • Loading branch information
lmignon committed Apr 18, 2017
1 parent e49dca1 commit 487db73
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
43 changes: 40 additions & 3 deletions packaging_uom/models/product_packaging.py
@@ -1,7 +1,8 @@
# -*- coding: utf-8 -*-
# Copyright 2015-2017 ACSONE SA/NV (<http://acsone.eu>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import api, fields, models
from odoo import api, fields, models, _
from odoo.exceptions import ValidationError


class ProductPackaging(models.Model):
Expand All @@ -18,16 +19,17 @@ def _default_uom_categ_domain_id(self):
uom_id = fields.Many2one(
'product.uom',
'Unit of Measure',
required=True,
help="It must be in the same category than "
"the default unit of measure."
"the default unit of measure.",
required=False
)
uom_categ_domain_id = fields.Many2one(
default=_default_uom_categ_domain_id,
comodel_name='product.uom.categ'
)
qty = fields.Float(
compute="_compute_qty",
inverse="_inverse_qty",
store=True,
readonly=True
)
Expand All @@ -43,3 +45,38 @@ def _compute_qty(self):
1, to_unit=self.product_tmpl_id.uom_id)
else:
self.qty = 0

@api.one
def _inverse_qty(self):
"""
The inverse method is defined to make the code compatible with
existing modules and to not break tests...
:return:
"""
category_id = self.product_tmpl_id.uom_id.category_id
uom_id = self.uom_id.search([
("factor", "=", 1.0 / self.qty),
('category_id', '=', category_id.id)])
if not uom_id:
uom_id = self.uom_id .create({
'name': "%s %s" % (category_id.name, self.qty),
'category_id': category_id.id,
'rounding': self.product_tmpl_id.uom_id.rounding,
'uom_type': 'bigger',
'factor_inv': self.qty,
'active': True
})
self.uom_id = uom_id

@api.multi
@api.constrains
def _check_uom_id(self):
""" Check uom_id is not null
Since the field can be computed by the inverse method on 'qty',
it's no more possible to add a sql constrains on the column uom_id.
"""
for rec in self:
if not rec.uom_id:
raise ValidationError(_("The field Unit of Measure is "
"required"))
2 changes: 1 addition & 1 deletion packaging_uom/views/product_packaging_views.xml
Expand Up @@ -25,7 +25,7 @@
<field name="arch" type="xml">
<field name="qty" position="before">
<field name="uom_categ_domain_id" invisible="1"/>
<field name="uom_id" domain="[('category_id', '=', uom_categ_domain_id)]"/>
<field name="uom_id" domain="[('category_id', '=', uom_categ_domain_id)]" required="True"/>
</field>
</field>
</record>
Expand Down

0 comments on commit 487db73

Please sign in to comment.