bom stock: small improvements and add test for field choice

This commit is contained in:
Cyril Gaudin
2016-03-16 13:06:46 +01:00
parent dcaf2f82f3
commit 6cd5533485
2 changed files with 85 additions and 8 deletions

View File

@@ -6,7 +6,6 @@ from collections import Counter
from openerp import models, fields, api
from openerp.addons import decimal_precision as dp
from openerp.tools.safe_eval import safe_eval
class ProductProduct(models.Model):
@@ -36,12 +35,6 @@ class ProductProduct(models.Model):
bom_obj = self.env['mrp.bom']
uom_obj = self.env['product.uom']
icp = self.env['ir.config_parameter']
stock_available_mrp_based_on = safe_eval(
icp.get_param('stock_available_mrp_based_on', 'False'))
if not stock_available_mrp_based_on:
stock_available_mrp_based_on = 'qty_available'
for product in self:
bom_id = bom_obj._bom_find(product_id=product.id)
if not bom_id:
@@ -60,7 +53,7 @@ class ProductProduct(models.Model):
else:
# Find the lowest quantity we can make with the stock at hand
components_potential_qty = min(
[getattr(component, stock_available_mrp_based_on) // need
[self._get_component_qty(component) // need
for component, need in component_needs.items()]
)
@@ -72,6 +65,19 @@ class ProductProduct(models.Model):
)
product.potential_qty = bom_qty * components_potential_qty
def _get_component_qty(self, component):
""" Return the component qty to use based en company settings.
:type component: product_product
:rtype: float
"""
icp = self.env['ir.config_parameter']
stock_available_mrp_based_on = icp.get_param(
'stock_available_mrp_based_on', 'qty_available'
)
return component[stock_available_mrp_based_on]
def _get_components_needs(self, product, bom):
""" Return the needed qty of each compoments in the *bom* of *product*.

View File

@@ -16,6 +16,7 @@ class TestPotentialQty(TransactionCase):
self.bom_model = self.env["mrp.bom"]
self.bom_line_model = self.env["mrp.bom.line"]
self.stock_quant_model = self.env["stock.quant"]
self.config = self.env['ir.config_parameter']
self.setup_demo_data()
@@ -394,3 +395,73 @@ class TestPotentialQty(TransactionCase):
p1.refresh()
self.assertEqual(24, p1.potential_qty)
def test_component_stock_choice(self):
# Test to change component stock for compute BOM stock
# Get a demo product with outgoing move (qty: 3)
imac = self.browse_ref('product.product_product_8')
# Set on hand qty
self.create_inventory(imac.id, 3)
# Create a product with BOM
p1 = self.product_model.create({
'name': 'Test product with BOM',
})
bom_p1 = self.bom_model.create({
'product_tmpl_id': p1.product_tmpl_id.id,
'product_id': p1.id,
'product_qty': 1,
'product_uom': self.ref('product.product_uom_unit'),
})
# Need 1 iMac for that
p1_bom_line = self.bom_line_model.create({
'bom_id': bom_p1.id,
'product_id': imac.id,
'product_qty': 1,
'product_uom': self.ref('product.product_uom_unit'),
})
# Default component is qty_available
p1.refresh()
self.assertEqual(3.0, p1.potential_qty)
# Change to immediately usable
self.config.set_param('stock_available_mrp_based_on',
'immediately_usable_qty')
p1.refresh()
self.assertEqual(0.0, p1.potential_qty)
# If iMac has a Bom and can be manufactured
imac_component = self.product_model.create({
'name': 'iMac component',
})
self.create_inventory(imac_component.id, 5)
imac_bom = self.bom_model.create({
'product_tmpl_id': imac.product_tmpl_id.id,
'product_id': imac.id,
'product_qty': 1,
'product_uom': self.ref('product.product_uom_unit'),
})
p1_bom_line.type = 'phantom'
# Need 1 imac_component for iMac
self.bom_line_model.create({
'bom_id': imac_bom.id,
'product_id': imac_component.id,
'product_qty': 1,
'product_uom': self.ref('product.product_uom_unit'),
})
p1.refresh()
self.assertEqual(5.0, p1.potential_qty)
# Changing to virtual (same as immediately in current config)
self.config.set_param('stock_available_mrp_based_on',
'virtual_available')
p1.refresh()
self.assertEqual(5.0, p1.potential_qty)