From 0a4844afb1fb3542f8d9ffd45740ec7924c909b5 Mon Sep 17 00:00:00 2001
From: rsi-odoo <rsi@openerp.com>
Date: Mon, 8 Sep 2014 18:27:37 +0530
Subject: [PATCH] [IMP] product: remove UoM constraint from product module and
 give it in stock and account module

Closes #2440
---
 addons/account/product.py   | 8 ++++++++
 addons/product/product.py   | 6 ------
 addons/stock/product.py     | 7 +++++++
 addons/stock/stock_demo.yml | 1 -
 4 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/addons/account/product.py b/addons/account/product.py
index b7bffee43a9a..1562ff006958 100644
--- a/addons/account/product.py
+++ b/addons/account/product.py
@@ -20,6 +20,7 @@
 ##############################################################################
 
 from openerp.osv import fields, osv
+from openerp.tools.translate import _
 
 class product_category(osv.osv):
     _inherit = "product.category"
@@ -61,5 +62,12 @@ class product_template(osv.osv):
             help="This account will be used for invoices instead of the default one to value expenses for the current product."),
     }
 
+    def write(self, cr, uid, ids, vals, context=None):
+        if 'uom_po_id' in vals:
+            product_ids = self.pool.get('product.product').search(cr, uid, [('product_tmpl_id', 'in', ids)], context=context)
+            if self.pool.get('account.move.line').search(cr, uid, [('product_id', 'in', product_ids)], context=context, limit=1):
+                raise osv.except_osv(_('Error!'), _("You can not change the unit of measure of a product that has been already used in an account journal item. If you need to change the unit of measure, you may deactivate this product.") % ())
+        return super(product_template, self).write(cr, uid, ids, vals, context=context)
+
 
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
diff --git a/addons/product/product.py b/addons/product/product.py
index cc8d1affb3ab..25ed44ffd73a 100644
--- a/addons/product/product.py
+++ b/addons/product/product.py
@@ -697,12 +697,6 @@ class product_template(osv.osv):
         ''' Store the standard price change in order to be able to retrieve the cost of a product template for a given date'''
         if isinstance(ids, (int, long)):
             ids = [ids]
-        if 'uom_po_id' in vals:
-            new_uom = self.pool.get('product.uom').browse(cr, uid, vals['uom_po_id'], context=context)
-            for product in self.browse(cr, uid, ids, context=context):
-                old_uom = product.uom_po_id
-                if old_uom.category_id.id != new_uom.category_id.id:
-                    raise osv.except_osv(_('Unit of Measure categories Mismatch!'), _("New Unit of Measure '%s' must belong to same Unit of Measure category '%s' as of old Unit of Measure '%s'. If you need to change the unit of measure, you may deactivate this product from the 'Procurements' tab and create a new one.") % (new_uom.name, old_uom.category_id.name, old_uom.name,))
         if 'standard_price' in vals:
             for prod_template_id in ids:
                 self._set_standard_price(cr, uid, prod_template_id, vals['standard_price'], context=context)
diff --git a/addons/stock/product.py b/addons/stock/product.py
index bb60b77ba1da..02e6e773e057 100644
--- a/addons/stock/product.py
+++ b/addons/stock/product.py
@@ -442,6 +442,13 @@ class product_template(osv.osv):
             result['context'] = "{'tree_view_ref':'stock.view_move_tree'}"
         return result
 
+    def write(self, cr, uid, ids, vals, context=None):
+        if 'uom_po_id' in vals:
+            product_ids = self.pool.get('product.product').search(cr, uid, [('product_tmpl_id', 'in', ids)], context=context)
+            if self.pool.get('stock.move').search(cr, uid, [('product_id', 'in', product_ids)], context=context, limit=1):
+                raise osv.except_osv(_('Error!'), _("You can not change the unit of measure of a product that has already been used in a stock move. If you need to change the unit of measure, you may deactivate this product.") % ())
+        return super(product_template, self).write(cr, uid, ids, vals, context=context)
+
 
 class product_removal_strategy(osv.osv):
     _name = 'product.removal'
diff --git a/addons/stock/stock_demo.yml b/addons/stock/stock_demo.yml
index a9a4d64c5649..4f72b66982fa 100644
--- a/addons/stock/stock_demo.yml
+++ b/addons/stock/stock_demo.yml
@@ -28,7 +28,6 @@
     list_price: 100.0
     standard_price: 70.0
     uom_id: product.product_uom_kgm
-    uom_po_id: product.product_uom_kgm
     property_stock_inventory: location_inventory
     description: Ice cream can be mass-produced and thus is widely available in developed parts of the world. Ice cream can be purchased in large cartons (vats and squrounds) from supermarkets and grocery stores, in smaller quantities from ice cream shops, convenience stores, and milk bars, and in individual servings from small carts or vans at public events.
 -
-- 
GitLab