From f6b512c6862ffe2506b954cf6f6b53b7aaf1fdf2 Mon Sep 17 00:00:00 2001 From: Quentin THEURET Date: Wed, 23 May 2012 13:04:39 +0200 Subject: [PATCH] [FIX] Fix no UoM restriction on Min/Max rules lp bug: https://launchpad.net/bugs/10003350 fixed bzr revid: qt@tempo-consulting.fr-20120523110439-zwq3zbq41kty7w8g --- addons/procurement/procurement.py | 35 ++++++++++++++++++++++++- addons/procurement/procurement_view.xml | 2 +- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/addons/procurement/procurement.py b/addons/procurement/procurement.py index aeab35be39b..9a27c425ed9 100644 --- a/addons/procurement/procurement.py +++ b/addons/procurement/procurement.py @@ -503,6 +503,19 @@ class stock_warehouse_orderpoint(osv.osv): result[orderpoint.id] = procurement_ids return result + def _check_product_uom(self, cr, uid, ids, context=None): + ''' + Check if the UoM has the same category as the product standard UoM + ''' + if not context: + context = {} + + for rule in self.browse(cr, uid, ids, context=context): + if rule.product_id.uom_id.category_id.id != rule.product_uom.category_id.id: + return False + + return True + _columns = { 'name': fields.char('Name', size=32, required=True), 'active': fields.boolean('Active', help="If the active field is set to False, it will allow you to hide the orderpoint without removing it."), @@ -535,6 +548,9 @@ class stock_warehouse_orderpoint(osv.osv): _sql_constraints = [ ('qty_multiple_check', 'CHECK( qty_multiple > 0 )', 'Qty Multiple must be greater than zero.'), ] + _constraints = [ + (_check_product_uom, 'You have to select a product UOM in the same category than the purchase UOM of the product', ['product_id', 'product_uom']), + ] def default_get(self, cr, uid, fields, context=None): res = super(stock_warehouse_orderpoint, self).default_get(cr, uid, fields, context) @@ -565,8 +581,25 @@ class stock_warehouse_orderpoint(osv.osv): """ if product_id: prod = self.pool.get('product.product').browse(cr, uid, product_id, context=context) + d = {'product_uom': [('category_id', '=', prod.uom_id.category_id.id)]} v = {'product_uom': prod.uom_id.id} - return {'value': v} + return {'value': v, 'domain': d} + return {'domain': {'product_uom': []}} + + def onchange_uom(self, cr, uid, ids, product_id, uom_id, context=None): + ''' + Check if the UoM is compatible with product standard UoM + ''' + if uom_id and product_id: + product_obj = self.pool.get('product.product') + uom_obj = self.pool.get('product.uom') + + product = product_obj.browse(cr, uid, product_id, context=context) + uom = uom_obj.browse(cr, uid, uom_id, context=context) + + if product.uom_id.category_id.id != uom.category_id.id: + raise osv.except_osv(_('Wrong Product UOM !'), _('You have to select a product UOM in the same category than the purchase UOM of the product')) + return {} def copy(self, cr, uid, id, default=None, context=None): diff --git a/addons/procurement/procurement_view.xml b/addons/procurement/procurement_view.xml index c82032f2ee4..868231886cc 100644 --- a/addons/procurement/procurement_view.xml +++ b/addons/procurement/procurement_view.xml @@ -206,7 +206,7 @@ - +