Can only procure on a UoM in the same dimension as the base UoM

This commit is contained in:
jbeficent
2016-10-24 19:26:06 +02:00
committed by lreficent
parent 16fe74b0b4
commit ad99c3a251
2 changed files with 46 additions and 14 deletions

View File

@@ -3,7 +3,8 @@
# (http://www.eficent.com)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from openerp import api, fields, models
from openerp import api, fields, models, _
from openerp.exceptions import Warning as UserError
class StockWarehouseOrderpoint(models.Model):
@@ -18,3 +19,15 @@ class StockWarehouseOrderpoint(models.Model):
for rec in self:
if rec.procure_uom_id:
rec.procure_uom_id = False
@api.constrains('product_uom', 'procure_uom_id')
def _check_procure_uom(self):
if any(orderpoint.product_uom
and orderpoint.procure_uom_id
and orderpoint.product_uom.category_id
!= orderpoint.procure_uom_id.category_id
for orderpoint in self):
raise UserError(_('Error: The product default Unit of Measure and '
'the procurement Unit of Measure must be in '
'the same category.'))
return True

View File

@@ -4,36 +4,55 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
import openerp.tests.common as common
from openerp.tools import mute_logger
from openerp.exceptions import ValidationError
class TestStockOrderpointProcureUom(common.TransactionCase):
def test_stock_orderpoont_procure_uom(self):
def setUp(self):
super(TestStockOrderpointProcureUom, self).setUp()
productObj = self.env['product.product']
warehouse = self.env.ref('stock.warehouse0')
location_stock = self.env.ref('stock.stock_location_stock')
uom_unit = self.env.ref('product.product_uom_unit')
uom_dozen = self.env.ref('product.product_uom_dozen')
self.warehouse = self.env.ref('stock.warehouse0')
self.location_stock = self.env.ref('stock.stock_location_stock')
self.uom_unit = self.env.ref('product.product_uom_unit')
self.uom_dozen = self.env.ref('product.product_uom_dozen')
self.uom_kg = self.env.ref('product.product_uom_kgm')
productA = productObj.create(
self.productA = productObj.create(
{'name': 'product A',
'standard_price': 1,
'type': 'product',
'uom_id': uom_unit.id,
'uom_id': self.uom_unit.id,
'default_code': 'A',
})
def test_stock_orderpoint_procure_uom(self):
self.env['stock.warehouse.orderpoint'].create({
'warehouse_id': warehouse.id,
'location_id': location_stock.id,
'product_id': productA.id,
'warehouse_id': self.warehouse.id,
'location_id': self.location_stock.id,
'product_id': self.productA.id,
'product_max_qty': 24,
'product_min_qty': 12,
'procure_uom_id': uom_dozen.id,
'procure_uom_id': self.uom_dozen.id,
})
sched = self.env['procurement.order']
sched.run_scheduler()
proc = sched.search([('product_id', '=', productA.id)])
self.assertEqual(proc.product_uom, uom_dozen)
proc = sched.search([('product_id', '=', self.productA.id)])
self.assertEqual(proc.product_uom, self.uom_dozen)
self.assertEqual(proc.product_qty, 2)
def test_stock_orderpoint_wrong_uom(self):
with mute_logger('openerp.sql_db'):
with self.assertRaises(ValidationError):
self.env['stock.warehouse.orderpoint'].create({
'warehouse_id': self.warehouse.id,
'location_id': self.location_stock.id,
'product_id': self.productA.id,
'product_max_qty': 24,
'product_min_qty': 12,
'procure_uom_id': self.uom_kg.id,
})