Skip to content

Commit

Permalink
[MIG] mrp_lot_number_propagation: migrate to V17
Browse files Browse the repository at this point in the history
  • Loading branch information
astirpe committed Aug 20, 2024
1 parent 38f14c3 commit bcbb8bb
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 56 deletions.
3 changes: 3 additions & 0 deletions mrp_lot_number_propagation/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ Contributors

- Akim Juillerat <[email protected]>
- Sébastien Alix <[email protected]>
- `360ERP <https://www.360erp.com>`__:

- Andrea Stirpe

Maintainers
-----------
Expand Down
2 changes: 1 addition & 1 deletion mrp_lot_number_propagation/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
{
"name": "MRP Serial Number Propagation",
"version": "16.0.1.0.0",
"version": "17.0.1.0.0",
"development_status": "Beta",
"license": "AGPL-3",
"author": "Camptocamp, Odoo Community Association (OCA)",
Expand Down
77 changes: 29 additions & 48 deletions mrp_lot_number_propagation/models/mrp_production.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
# Copyright 2022 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)

from lxml import etree

from odoo import _, api, fields, models, tools
from odoo.exceptions import UserError
from odoo.osv import expression
from odoo.tools.safe_eval import safe_eval

from odoo.addons.base.models.ir_ui_view import (
transfer_modifiers_to_node,
transfer_node_to_modifiers,
)


class MrpProduction(models.Model):
Expand All @@ -36,19 +28,19 @@ class MrpProduction(models.Model):

@api.depends(
"move_raw_ids.propagate_lot_number",
"move_raw_ids.move_line_ids.qty_done",
"move_raw_ids.move_line_ids.quantity",
"move_raw_ids.move_line_ids.lot_id",
)
def _compute_propagated_lot_producing(self):
for order in self:
order.propagated_lot_producing = False
move_with_lot = order._get_propagating_component_move()
line_with_sn = move_with_lot.move_line_ids.filtered(
lambda l: (
l.lot_id
and l.product_id.tracking == "serial"
lambda ln: (
ln.lot_id
and ln.product_id.tracking == "serial"
and tools.float_compare(
l.qty_done, 1, precision_rounding=l.product_uom_id.rounding
ln.quantity, 1, precision_rounding=ln.product_uom_id.rounding
)
== 0
)
Expand Down Expand Up @@ -107,11 +99,11 @@ def _create_and_assign_propagated_lot_number(self):
if not order.is_lot_number_propagated or order.lot_producing_id:
continue

Check warning on line 100 in mrp_lot_number_propagation/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

mrp_lot_number_propagation/models/mrp_production.py#L100

Added line #L100 was not covered by tests
finish_moves = order.move_finished_ids.filtered(
lambda m: m.product_id == order.product_id
and m.state not in ("done", "cancel")
lambda mv, mo=order: mv.product_id == mo.product_id
and mv.state not in ("done", "cancel")
)
if finish_moves and not finish_moves.quantity_done:
lot_model = self.env["stock.production.lot"]
lot_model = self.env["stock.lot"]
lot = lot_model.search(

Check warning on line 107 in mrp_lot_number_propagation/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

mrp_lot_number_propagation/models/mrp_production.py#L106-L107

Added lines #L106 - L107 were not covered by tests
[
("product_id", "=", order.product_id.id),
Expand All @@ -128,7 +120,7 @@ def _create_and_assign_propagated_lot_number(self):
)
)
if not lot:
lot = self.env["stock.production.lot"].create(
lot = self.env["stock.lot"].create(

Check warning on line 123 in mrp_lot_number_propagation/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

mrp_lot_number_propagation/models/mrp_production.py#L123

Added line #L123 was not covered by tests
{
"product_id": order.product_id.id,
"company_id": order.company_id.id,
Expand All @@ -141,7 +133,7 @@ def write(self, vals):
for order in self:
if (
order.is_lot_number_propagated
and "lot_producing_id" in vals
and vals.get("lot_producing_id")
and not self.env.context.get("lot_propagation")
):
raise UserError(

Check warning on line 139 in mrp_lot_number_propagation/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

mrp_lot_number_propagation/models/mrp_production.py#L139

Added line #L139 was not covered by tests
Expand All @@ -152,42 +144,31 @@ def write(self, vals):
)
return super().write(vals)

def fields_view_get(
self, view_id=None, view_type="form", toolbar=False, submenu=False
):
@api.model
def _get_view(self, view_id=None, view_type="form", **options):
# Override to hide the "lot_producing_id" field + "action_generate_serial"
# button if the MO is configured to propagate a serial number
result = super().fields_view_get(
view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu
)
if result.get("name") in self._views_to_adapt():
result["arch"] = self._fields_view_get_adapt_lot_tags_attrs(result)
return result
arch, view = super()._get_view(view_id, view_type, **options)
if view.name in self._views_to_adapt():
arch = self._fields_view_get_adapt_lot_tags_attrs(arch)
return arch, view

def _views_to_adapt(self):
"""Return the form view names bound to 'mrp.production' to adapt."""
return ["mrp.production.form"]

def _fields_view_get_adapt_lot_tags_attrs(self, view):
def _fields_view_get_adapt_lot_tags_attrs(self, arch):
"""Hide elements related to lot if it is automatically propagated."""
doc = etree.XML(view["arch"])
tags = (
"//label[@for='lot_producing_id']",
"//field[@name='lot_producing_id']/..", # parent <div>
)
for xpath_expr in tags:
attrs_key = "invisible"
nodes = doc.xpath(xpath_expr)
for field in nodes:
attrs = safe_eval(field.attrib.get("attrs", "{}"))
if not attrs[attrs_key]:
continue
invisible_domain = expression.OR(
[attrs[attrs_key], [("is_lot_number_propagated", "=", True)]]

for node in arch.xpath(
"//label[@for='lot_producing_id']"
"|//field[@name='lot_producing_id']/.." # parent <div>
):
attr_invisible = node.attrib.get("invisible", "")
if not attr_invisible:
node.attrib["invisible"] = "is_lot_number_propagated"

Check warning on line 169 in mrp_lot_number_propagation/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

mrp_lot_number_propagation/models/mrp_production.py#L169

Added line #L169 was not covered by tests
else:
node.attrib["invisible"] = (
node.attrib["invisible"] + " or is_lot_number_propagated"
)
attrs[attrs_key] = invisible_domain
field.set("attrs", str(attrs))
modifiers = {}
transfer_node_to_modifiers(field, modifiers, self.env.context)
transfer_modifiers_to_node(modifiers, field)
return etree.tostring(doc, encoding="unicode")
return arch
2 changes: 2 additions & 0 deletions mrp_lot_number_propagation/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
- Akim Juillerat \<<[email protected]>\>
- Sébastien Alix \<<[email protected]>\>
- [360ERP](https://www.360erp.com):
- Andrea Stirpe
4 changes: 4 additions & 0 deletions mrp_lot_number_propagation/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,10 @@ <h2><a class="toc-backref" href="#toc-entry-5">Contributors</a></h2>
<ul class="simple">
<li>Akim Juillerat &lt;<a class="reference external" href="mailto:akim.juillerat&#64;camptocamp.com">akim.juillerat&#64;camptocamp.com</a>&gt;</li>
<li>Sébastien Alix &lt;<a class="reference external" href="mailto:sebastien.alix&#64;camptocamp.com">sebastien.alix&#64;camptocamp.com</a>&gt;</li>
<li><a class="reference external" href="https://www.360erp.com">360ERP</a>:<ul>
<li>Andrea Stirpe</li>
</ul>
</li>
</ul>
</div>
<div class="section" id="maintainers">
Expand Down
2 changes: 1 addition & 1 deletion mrp_lot_number_propagation/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def _update_stock_component_qty(cls, order=None, bom=None, location=None):
"company_id": line.company_id.id,
"name": lot_name,
}
lot = cls.env["stock.production.lot"].create(vals)
lot = cls.env["stock.lot"].create(vals)
cls._update_qty_in_location(

Check warning on line 84 in mrp_lot_number_propagation/tests/common.py

View check run for this annotation

Codecov / codecov/patch

mrp_lot_number_propagation/tests/common.py#L83-L84

Added lines #L83 - L84 were not covered by tests
location,
line.product_id,
Expand Down
6 changes: 3 additions & 3 deletions mrp_lot_number_propagation/tests/test_mrp_production.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def _create_order(cls, product, bom):

def _set_qty_done(self, order):
for line in order.move_raw_ids.move_line_ids:
line.qty_done = line.product_uom_qty
line.quantity = line.product_uom_qty
order.qty_producing = order.product_qty

def test_order_propagated_lot_producing(self):
Expand Down Expand Up @@ -64,7 +64,7 @@ def test_order_post_inventory_lot_already_exists_but_not_used(self):
self.assertEqual(self.order.propagated_lot_producing, self.LOT_NAME)
# Create a lot with the same number for the finished product
# without any stock/quants (so not used at all) before validating the MO
existing_lot = self.env["stock.production.lot"].create(
existing_lot = self.env["stock.lot"].create(
{
"product_id": self.order.product_id.id,
"company_id": self.order.company_id.id,
Expand All @@ -82,7 +82,7 @@ def test_order_post_inventory_lot_already_exists_and_used(self):
# Create a lot with the same number for the finished product
# with some stock/quants (so it is considered as used) before
# validating the MO
existing_lot = self.env["stock.production.lot"].create(
existing_lot = self.env["stock.lot"].create(
{
"product_id": self.order.product_id.id,
"company_id": self.order.company_id.id,
Expand Down
4 changes: 2 additions & 2 deletions mrp_lot_number_propagation/views/mrp_bom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
<field name="display_lot_number_propagation" invisible="1" />
<field
name="lot_number_propagation"
attrs="{'invisible': [('display_lot_number_propagation', '=', False)]}"
invisible="not display_lot_number_propagation"
/>
</field>
<xpath expr="//field[@name='bom_line_ids']/tree" position="inside">
<field name="display_propagate_lot_number" invisible="1" />
<field
name="propagate_lot_number"
attrs="{'column_invisible': ['|', ('parent.display_lot_number_propagation', '=', False), ('parent.lot_number_propagation', '=', False)]}"
column_invisible="not parent.display_lot_number_propagation or not parent.lot_number_propagation"
/>
</xpath>
</field>
Expand Down
2 changes: 1 addition & 1 deletion mrp_lot_number_propagation/views/mrp_production.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<field
name="propagated_lot_producing"
string="Lot/Serial Number"
attrs="{'invisible': [('is_lot_number_propagated', '=', False)]}"
invisible="not is_lot_number_propagated"
/>
</label>
</field>
Expand Down

0 comments on commit bcbb8bb

Please sign in to comment.