Skip to content

Commit

Permalink
[MIG] stock_packaging_calculator: Migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenminhchien committed Jun 12, 2024
1 parent 71d6610 commit e30a8a6
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 19 deletions.
4 changes: 1 addition & 3 deletions stock_packaging_calculator/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
===========
Expand All @@ -108,6 +105,7 @@ Contributors

- Simone Orsi <simahawk@gmail.com>
- Christopher Ormaza <chris.ormaza@forgeflow.com>
- Nguyen Minh Chien <chien@trobz.com>

Maintainers
-----------
Expand Down
2 changes: 1 addition & 1 deletion stock_packaging_calculator/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 11 additions & 3 deletions stock_packaging_calculator/models/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
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

Expand Down Expand Up @@ -112,13 +112,21 @@ def _packaging_name_getter(self, packaging):

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:
Expand Down Expand Up @@ -191,7 +199,7 @@ def product_qty_by_packaging_as_str(
)
# 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)
Expand Down
1 change: 1 addition & 0 deletions stock_packaging_calculator/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
- Simone Orsi \<<simahawk@gmail.com>\>
- Christopher Ormaza \<<chris.ormaza@forgeflow.com>\>
- Nguyen Minh Chien \<<chien@trobz.com>\>
4 changes: 0 additions & 4 deletions stock_packaging_calculator/readme/ROADMAP.md
Original file line number Diff line number Diff line change
@@ -1,4 +0,0 @@
TODO

1. Fractional quantities (eg: 0.5 Kg) are lost when counting units
2. Maybe rely on packaging_uom
6 changes: 1 addition & 5 deletions stock_packaging_calculator/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -426,11 +426,6 @@ <h1><a class="toc-backref" href="#toc-entry-1">Usage</a></h1>
</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>
Expand All @@ -453,6 +448,7 @@ <h2><a class="toc-backref" href="#toc-entry-6">Contributors</a></h2>
<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">
Expand Down
1 change: 1 addition & 0 deletions stock_packaging_calculator/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from . import test_packaging_calc
from . import test_pkg_qty_str
from . import test_product_qty_by_packaging_mixin
5 changes: 2 additions & 3 deletions stock_packaging_calculator/tests/common.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
# 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

@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(
{
Expand Down
11 changes: 11 additions & 0 deletions stock_packaging_calculator/tests/models.py
Original file line number Diff line number Diff line change
@@ -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()
1 change: 1 addition & 0 deletions stock_packaging_calculator/tests/test_packaging_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def test_calc_6(self):
"""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)

Expand Down
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
odoo_test_helper

0 comments on commit e30a8a6

Please sign in to comment.