Skip to content

Commit

Permalink
[IMP] product_set: add packaging for product.set.line
Browse files Browse the repository at this point in the history
  • Loading branch information
trisdoan committed Dec 31, 2024
1 parent 00e9629 commit 489ff0a
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 1 deletion.
47 changes: 46 additions & 1 deletion product_set/models/product_set_line.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Copyright 2015 Anybox S.A.S
# Copyright 2016-2018 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import fields, models
from odoo import api, fields, models
from odoo.exceptions import UserError
from odoo.tools import float_is_zero


class ProductSetLine(models.Model):
Expand Down Expand Up @@ -35,3 +37,46 @@ class ProductSetLine(models.Model):
"res.company", related="product_set_id.company_id", store=True, readonly=True
)
name = fields.Char()
product_packaging_id = fields.Many2one(
"product.packaging", domain="[('product_id', '=', product_id)]"
)
product_packaging_qty = fields.Float(
compute="_compute_product_packaging_qty",
inverse="_inverse_product_packaging_qty",
digits="Product Unit of Measure",
)

@api.depends(
"quantity",
"product_packaging_id",
"product_packaging_id.qty",
"product_id.packaging_ids",
)
def _compute_product_packaging_qty(self):
for line in self:
uom_rounding = line.product_id.uom_id.rounding
if not line.product_packaging_id or float_is_zero(
line.quantity, precision_rounding=uom_rounding
):
line.product_packaging_qty = 0
continue

Check warning on line 62 in product_set/models/product_set_line.py

View check run for this annotation

Codecov / codecov/patch

product_set/models/product_set_line.py#L61-L62

Added lines #L61 - L62 were not covered by tests
line.product_packaging_qty = line.quantity / line.product_packaging_id.qty
line.update(line._prepare_product_packaging_qty_values())

def _inverse_product_packaging_qty(self):
for line in self:
if line.product_packaging_qty and not line.product_packaging_id:
raise UserError(
self.env._(
"You must define a package before setting a quantity "
"of said package."
)
)
if line.product_packaging_id and line.product_packaging_qty:
line.write(line._prepare_product_packaging_qty_values())

def _prepare_product_packaging_qty_values(self):
self.ensure_one()
return {
"quantity": self.product_packaging_id.qty * self.product_packaging_qty,
}
3 changes: 3 additions & 0 deletions product_set/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@
- [Tecnativa](https://www.tecnativa.com):
- Pilar Vargas
- Nils Coenen \<<[email protected]>\>
- Akim Juillerat \<<[email protected]>\>
- Son (Ho Dac) \<<[email protected]>\>
- Tris Doan \<<[email protected]>\>
3 changes: 3 additions & 0 deletions product_set/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,9 @@ <h2><a class="toc-backref" href="#toc-entry-5">Contributors</a></h2>
</ul>
</li>
<li>Nils Coenen &lt;<a class="reference external" href="mailto:nils.coenen&#64;nico-solutions.de">nils.coenen&#64;nico-solutions.de</a>&gt;</li>
<li>Akim Juillerat &lt;<a class="reference external" href="mailto:akim.juillerat&#64;camptocamp.com">akim.juillerat&#64;camptocamp.com</a>&gt;</li>
<li>Son (Ho Dac) &lt;<a class="reference external" href="mailto:hodacson.6491&#64;gmail.com">hodacson.6491&#64;gmail.com</a>&gt;</li>
<li>Tris Doan &lt;<a class="reference external" href="mailto:tridm&#64;trobz.com">tridm&#64;trobz.com</a>&gt;</li>
</ul>
</div>
<div class="section" id="other-credits">
Expand Down
1 change: 1 addition & 0 deletions product_set/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from . import test_product_set_wizard
from . import test_product_set
from . import test_product_set_line
56 changes: 56 additions & 0 deletions product_set/tests/test_product_set_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright 2024 Camptocamp
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)

from odoo import exceptions
from odoo.tests.common import TransactionCase


class TestProductSetLine(TransactionCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.line = cls.env.ref("product_set.product_set_line_computer_3")
cls.packaging = cls.env["product.packaging"].create(
{"name": "Box", "product_id": cls.line.product_id.id, "qty": 10}
)

def test_with_packaging(self):
line = self.line
line.quantity = 50
line.product_packaging_id = self.packaging
# 50 units of packages with 10 units each means 5 packages
self.assertEqual(line.product_packaging_qty, 5)

def test_write_packaging_qty(self):
line = self.line
line.product_packaging_id = self.packaging
line.product_packaging_qty = 8
# 8 packages with 10 units each means 80 units
self.assertEqual(line.quantity, 80)

def test_error_write_qty_but_no_packaging(self):
line = self.line
with self.assertRaises(exceptions.UserError):
line.product_packaging_qty = 10

def test_packaging_qty_update(self):
line = self.line
line.product_packaging_id = self.packaging

# set packaging qty and check product.set.line quantity is correctly updated
line.product_packaging_qty = 2
self.assertEqual(self.packaging.qty, 10)
# qty on line is 20: 2 packages of 10 units each
self.assertEqual(line.quantity, 20)

# change qty on packaging
# and check product.set.line quantity is correctly updated
self.packaging.qty = 5
self.line.product_packaging_qty = 2

self.assertEqual(self.packaging.qty, 5)
self.assertEqual(line.product_id.packaging_ids.qty, 5)
self.assertEqual(line.product_id.product_tmpl_id.packaging_ids.qty, 5)

# qty on line is 10: 2 packages of 5 units each
self.assertEqual(line.quantity, 10)
9 changes: 9 additions & 0 deletions product_set/views/product_set.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@
<list editable="top">
<field name="sequence" widget="handle" />
<field name="product_id" required="not display_type" />
<field
name="product_packaging_id"
context="{'default_product_id': product_id}"
groups="product.group_stock_packaging"
/>
<field
name="product_packaging_qty"
groups="product.group_stock_packaging"
/>
<field name="quantity" />
<field name="display_type" invisible="1" />
<field name="name" widget="section_and_note_text" />
Expand Down
5 changes: 5 additions & 0 deletions product_set/views/product_set_line.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
<list editable="top">
<field name="product_set_id" />
<field name="product_id" />
<field
name="product_packaging_id"
context="{'default_product_id': product_id}"
/>
<field name="product_packaging_qty" />
<field name="quantity" />
</list>
</field>
Expand Down

0 comments on commit 489ff0a

Please sign in to comment.