[MIG] stock_packaging_calculator: Migration to 17.0

This commit is contained in:
chien
2024-06-10 15:22:42 +07:00
parent 71d6610abb
commit e30a8a6967
12 changed files with 87 additions and 19 deletions

View File

@@ -80,10 +80,7 @@ flag:
Known issues / Roadmap
======================
TODO
1. Fractional quantities (eg: 0.5 Kg) are lost when counting units
2. Maybe rely on packaging_uom
Bug Tracker
===========
@@ -108,6 +105,7 @@ Contributors
- Simone Orsi <simahawk@gmail.com>
- Christopher Ormaza <chris.ormaza@forgeflow.com>
- Nguyen Minh Chien <chien@trobz.com>
Maintainers
-----------

View File

@@ -3,7 +3,7 @@
{
"name": "Stock packaging calculator",
"summary": "Compute product quantity to pick by packaging",
"version": "16.0.1.0.1",
"version": "17.0.1.0.0",
"development_status": "Beta",
"category": "Warehouse Management",
"website": "https://github.com/OCA/stock-logistics-warehouse",

View File

@@ -6,7 +6,7 @@ import unicodedata
from collections import namedtuple
from odoo import api, models
from odoo.tools import float_compare
from odoo.tools import float_compare, float_is_zero, float_round
from odoo.addons.base_sparse_field.models.fields import Serialized
@@ -112,13 +112,21 @@ class Product(models.Model):
def _product_qty_by_packaging(self, pkg_by_qty, qty, with_contained=False):
"""Produce a list of dictionaries of packaging info."""
# TODO: refactor to handle fractional quantities (eg: 0.5 Kg)
res = []
prepare_values = self.env.context.get(
"_packaging_values_handler", self._prepare_qty_by_packaging_values
)
for pkg in pkg_by_qty:
qty_per_pkg, qty = self._qty_by_pkg(pkg.qty, qty)
# To handle fractional quantities (eg: 0.5 Kg)
if pkg.is_unit and not float_is_zero(
qty, precision_rounding=self.uom_id.rounding
):
# `is_unit` package always be the last package by the sorting
# it has the same uom with the product, just sum the quantity
qty_per_pkg += float_round(qty, precision_rounding=self.uom_id.rounding)
qty = 0
if qty_per_pkg:
value = prepare_values(pkg, qty_per_pkg)
if with_contained:
@@ -191,7 +199,7 @@ class Product(models.Model):
)
# Collect all strings representations
as_string = []
for record, info in zip(records, _qty_by_packaging):
for record, info in zip(records, _qty_by_packaging, strict=True):
bit = _qty_by_packaging_as_str(record, info["qty"])
if bit:
as_string.append(bit)

View File

@@ -1,2 +1,3 @@
- Simone Orsi \<<simahawk@gmail.com>\>
- Christopher Ormaza \<<chris.ormaza@forgeflow.com>\>
- Nguyen Minh Chien \<<chien@trobz.com>\>

View File

@@ -1,4 +0,0 @@
TODO
1. Fractional quantities (eg: 0.5 Kg) are lost when counting units
2. Maybe rely on packaging_uom

View File

@@ -426,11 +426,6 @@ flag:</p>
</div>
<div class="section" id="known-issues-roadmap">
<h1><a class="toc-backref" href="#toc-entry-2">Known issues / Roadmap</a></h1>
<p>TODO</p>
<ol class="arabic simple">
<li>Fractional quantities (eg: 0.5 Kg) are lost when counting units</li>
<li>Maybe rely on packaging_uom</li>
</ol>
</div>
<div class="section" id="bug-tracker">
<h1><a class="toc-backref" href="#toc-entry-3">Bug Tracker</a></h1>
@@ -453,6 +448,7 @@ If you spotted it first, help us to smash it by providing a detailed and welcome
<ul class="simple">
<li>Simone Orsi &lt;<a class="reference external" href="mailto:simahawk&#64;gmail.com">simahawk&#64;gmail.com</a>&gt;</li>
<li>Christopher Ormaza &lt;<a class="reference external" href="mailto:chris.ormaza&#64;forgeflow.com">chris.ormaza&#64;forgeflow.com</a>&gt;</li>
<li>Nguyen Minh Chien &lt;<a class="reference external" href="mailto:chien&#64;trobz.com">chien&#64;trobz.com</a>&gt;</li>
</ul>
</div>
<div class="section" id="maintainers">

View File

@@ -1,2 +1,3 @@
from . import test_packaging_calc
from . import test_pkg_qty_str
from . import test_product_qty_by_packaging_mixin

View File

@@ -1,9 +1,9 @@
# Copyright 2020 Camptocamp SA
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl)
from odoo.tests import TransactionCase
from odoo.addons.base.tests.common import BaseCommon
class TestCommon(TransactionCase):
class TestCommon(BaseCommon):
at_install = False
post_install = True
maxDiff = None
@@ -11,7 +11,6 @@ class TestCommon(TransactionCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True))
cls.uom_unit = cls.env.ref("uom.product_uom_unit")
cls.product_a = cls.env["product.product"].create(
{

View File

@@ -0,0 +1,11 @@
from odoo import fields, models
class TestProductQtyByPackagingMixin(models.Model):
_name = "test.product.qty_by_packaging.mixin"
_description = "Test ProductQtyByPackagingMixin"
_inherit = ["product.qty_by_packaging.mixin"]
_qty_by_pkg__qty_field_name = "quantity"
product_id = fields.Many2one("product.product")
quantity = fields.Float()

View File

@@ -55,6 +55,7 @@ class TestCalc(TestCommon):
"""Test fractional qty is lost."""
expected = [
make_pkg_values(self.pkg_box, qty=1),
make_pkg_values(self.uom_unit, qty=0.5),
]
self.assertEqual(self.product_a.product_qty_by_packaging(50.5), expected)

View File

@@ -0,0 +1,56 @@
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
from odoo_test_helper import FakeModelLoader
from .common import TestCommon
class TestPQPackagingMixin(TestCommon):
@classmethod
def setUpClass(cls):
super().setUpClass()
# Load a test model using odoo_test_helper
cls.loader = FakeModelLoader(cls.env, cls.__module__)
cls.loader.backup_registry()
from .models import TestProductQtyByPackagingMixin
cls.loader.update_registry((TestProductQtyByPackagingMixin,))
cls.model = cls.env[TestProductQtyByPackagingMixin._name]
@classmethod
def tearDownClass(cls):
cls.loader.restore_registry()
return super().tearDownClass()
def test_1_quantity_packaging(self):
record = self.model.create({"product_id": self.product_a.id, "quantity": 10})
self.assertEqual(record.product_qty_by_packaging_display, "10 Units")
self.assertEqual(
record.with_context(
qty_by_pkg_only_packaging=True
).product_qty_by_packaging_display,
"",
)
record.quantity = 100
self.assertEqual(record.product_qty_by_packaging_display, "2 Box")
record.quantity = 250
self.assertEqual(record.product_qty_by_packaging_display, "1 Big Box,\xa01 Box")
record.quantity = 255
self.assertEqual(
record.product_qty_by_packaging_display,
"1 Big Box,\xa01 Box,\xa05 Units",
)
# only_packaging has no impact if we get not only units
self.assertEqual(
record.with_context(
qty_by_pkg_only_packaging=True
).product_qty_by_packaging_display,
"1 Big Box,\xa01 Box,\xa05 Units",
)
def test_2_fractional_quantity(self):
record = self.model.create(
{"product_id": self.product_a.id, "quantity": 100.45}
)
self.assertEqual(
record.product_qty_by_packaging_display, "2 Box,\xa00.45 Units"
)

1
test-requirements.txt Normal file
View File

@@ -0,0 +1 @@
odoo_test_helper