mirror of
https://github.com/OCA/stock-logistics-warehouse.git
synced 2025-01-21 14:27:28 +02:00
[IMP] Adds the glue which fill in the product packaging in stock
When a sale order is confirmed and product packaging is filled in sale order line(s), the information is propagated through procurement orders and stock moves. This is conditioned by a parameter on procurement rule.
This commit is contained in:
committed by
Thomas Binsfeld
parent
41af45de9a
commit
77870ee252
@@ -34,42 +34,44 @@ class ProductPackaging(models.Model):
|
||||
readonly=True
|
||||
)
|
||||
|
||||
@api.one
|
||||
@api.multi
|
||||
@api.depends('uom_id', 'product_tmpl_id.uom_id')
|
||||
def _compute_qty(self):
|
||||
"""
|
||||
Compute the quantity by package based on uom
|
||||
"""
|
||||
if self.uom_id and self.product_tmpl_id:
|
||||
self.qty = self.uom_id._compute_quantity(
|
||||
1, to_unit=self.product_tmpl_id.uom_id)
|
||||
else:
|
||||
self.qty = 0
|
||||
for packaging in self:
|
||||
if packaging.uom_id and packaging.product_tmpl_id:
|
||||
packaging.qty = packaging.uom_id._compute_quantity(
|
||||
1, to_unit=packaging.product_tmpl_id.uom_id)
|
||||
else:
|
||||
packaging.qty = 0
|
||||
|
||||
@api.one
|
||||
@api.multi
|
||||
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
|
||||
for packaging in self:
|
||||
category_id = packaging.product_tmpl_id.uom_id.category_id
|
||||
uom_id = packaging.uom_id.search([
|
||||
("factor", "=", 1.0 / self.qty),
|
||||
('category_id', '=', category_id.id)])
|
||||
if not uom_id:
|
||||
uom_id = packaging.uom_id.create({
|
||||
'name': "%s %s" % (category_id.name, packaging.qty),
|
||||
'category_id': category_id.id,
|
||||
'rounding': packaging.product_tmpl_id.uom_id.rounding,
|
||||
'uom_type': 'bigger',
|
||||
'factor_inv': packaging.qty,
|
||||
'active': True
|
||||
})
|
||||
packaging.uom_id = uom_id
|
||||
|
||||
@api.multi
|
||||
@api.constrains
|
||||
@api.constrains('uom_id')
|
||||
def _check_uom_id(self):
|
||||
""" Check uom_id is not null
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
# Copyright 2015-2017 ACSONE SA/NV (<http://acsone.eu>)
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
import odoo.tests.common as common
|
||||
from odoo.exceptions import ValidationError
|
||||
|
||||
|
||||
class TestProductPackaging(common.TransactionCase):
|
||||
@@ -10,12 +11,13 @@ class TestProductPackaging(common.TransactionCase):
|
||||
super(TestProductPackaging, self).setUp()
|
||||
self.uom_unit = self.env.ref('product.product_uom_unit')
|
||||
self.uom_dozen = self.env.ref('product.product_uom_dozen')
|
||||
self.product_tmpl_dozen = self.env[
|
||||
'product.template'].new(
|
||||
{'uom_id': self.uom_dozen})
|
||||
self.product_tmpl_unit = self.env[
|
||||
'product.template'].new(
|
||||
{'uom_id': self.uom_unit})
|
||||
self.product_tmpl_dozen = self.env['product.template'].create(
|
||||
{'name': 'PRODUCT DOZEN',
|
||||
'uom_id': self.uom_dozen.id})
|
||||
self.product_tmpl_unit = self.env['product.template'].create(
|
||||
{'name': 'PRODUCT UNIT',
|
||||
'uom_id': self.uom_unit.id}
|
||||
)
|
||||
|
||||
def test_compute_quantity_by_package(self):
|
||||
""" Create a packagings with uom product_uom_dozen on
|
||||
@@ -41,14 +43,22 @@ class TestProductPackaging(common.TransactionCase):
|
||||
"""
|
||||
|
||||
packaging_obj = self.env['product.packaging']
|
||||
product_packaging_dozen = packaging_obj.new(
|
||||
{'product_tmpl_id': self.product_tmpl_dozen,
|
||||
'uom_id': self.uom_dozen})
|
||||
product_packaging_dozen = packaging_obj.create(
|
||||
{'name': 'PACKAGING 1',
|
||||
'product_tmpl_id': self.product_tmpl_dozen.id,
|
||||
'uom_id': self.uom_dozen.id})
|
||||
self.assertAlmostEqual(product_packaging_dozen.qty, 1)
|
||||
product_packaging_unit = packaging_obj.new(
|
||||
{'product_tmpl_id': self.product_tmpl_unit,
|
||||
'uom_id': self.uom_dozen})
|
||||
product_packaging_unit = packaging_obj.with_context(
|
||||
get_uom_categ_from_uom=self.uom_dozen.category_id.id).create(
|
||||
{'name': 'PACKAGING 2',
|
||||
'product_tmpl_id': self.product_tmpl_unit.id,
|
||||
'uom_id': self.uom_dozen.id})
|
||||
self.assertAlmostEqual(product_packaging_unit.qty, 12)
|
||||
self.assertEqual(
|
||||
self.uom_dozen.category_id,
|
||||
product_packaging_unit.uom_categ_domain_id,
|
||||
'The UOM domain is not well set'
|
||||
)
|
||||
product_uom_24 = self.env['product.uom'].create(
|
||||
{'category_id': self.env.ref('product.product_uom_categ_unit').id,
|
||||
'name': 'Double Dozens',
|
||||
@@ -69,3 +79,35 @@ class TestProductPackaging(common.TransactionCase):
|
||||
self.assertAlmostEqual(product_packaging_dozen.qty, 2)
|
||||
product_packaging_unit.uom_id = product_uom_6
|
||||
self.assertAlmostEqual(product_packaging_unit.qty, 6)
|
||||
# Set Packaging Quantity
|
||||
product_packaging_dozen.qty = 1
|
||||
self.assertEquals(
|
||||
self.uom_unit,
|
||||
product_packaging_dozen.uom_id
|
||||
)
|
||||
# Try to set null on uom
|
||||
with self.assertRaises(ValidationError):
|
||||
product_packaging_dozen.uom_id = None
|
||||
|
||||
# Define a new packaging unit
|
||||
uom_524 = self.env['product.uom'].search([
|
||||
('category_id', '=',
|
||||
product_packaging_dozen.product_tmpl_id.uom_id.category_id.id),
|
||||
('name',
|
||||
'=',
|
||||
'%s %s' %
|
||||
(product_packaging_dozen.product_tmpl_id.uom_id.category_id.name,
|
||||
float(524)))
|
||||
])
|
||||
self.assertEqual(0, len(uom_524))
|
||||
product_packaging_dozen.qty = 524
|
||||
uom_524 = self.env['product.uom'].search([
|
||||
('category_id', '=',
|
||||
product_packaging_dozen.product_tmpl_id.uom_id.category_id.id),
|
||||
('name',
|
||||
'=',
|
||||
'%s %s' %
|
||||
(product_packaging_dozen.product_tmpl_id.uom_id.category_id.name,
|
||||
float(524)))
|
||||
])
|
||||
self.assertEqual(1, len(uom_524))
|
||||
|
||||
Reference in New Issue
Block a user