Skip to content

Commit

Permalink
[MIG] repair_type: Migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
miquelalzanillas committed May 29, 2024
1 parent c2b6153 commit 573126f
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 79 deletions.
4 changes: 4 additions & 0 deletions repair_type/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ Contributors

- Bernat Puig <[email protected]>

- `APSL-Nagarro <https://apsl.tech>`__:

- Miquel Alzanillas <[email protected]>

Maintainers
-----------

Expand Down
2 changes: 1 addition & 1 deletion repair_type/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

{
"name": "Repair Type",
"version": "16.0.1.0.1",
"version": "17.0.1.0.1",
"author": "ForgeFlow, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/repair",
"summary": "Repair type",
Expand Down
1 change: 1 addition & 0 deletions repair_type/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from . import repair
from . import repair_type
from . import stock_move
88 changes: 38 additions & 50 deletions repair_type/models/repair.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,56 +20,44 @@ def _compute_location_id(self):
rec.location_id = rec.repair_type_id.source_location_id
return res

@api.onchange("repair_type_id")
def _onchange_repair_type_id(self):
for rec in self:
rec.set_location_id_on_parts()

Check warning on line 26 in repair_type/models/repair.py

View check run for this annotation

Codecov / codecov/patch

repair_type/models/repair.py#L26

Added line #L26 was not covered by tests

class RepairLine(models.Model):
_inherit = "repair.line"

location_id = fields.Many2one(
compute="_compute_location_id", store=True, readonly=False
)
location_dest_id = fields.Many2one(
compute="_compute_location_id", store=True, readonly=False
)
def write(self, vals):
res = super().write(vals)
if any(rec.repair_type_id for rec in self) and "move_ids" not in vals:
for rec in self:
if rec.repair_type_id:
rec.set_location_id_on_parts()
return res

@api.depends("type", "repair_id.repair_type_id")
def _compute_location_id(self):
res = super()._compute_location_id()
@api.depends("repair_type_id")
def set_location_id_on_parts(self):
for rec in self:
if (
rec.type == "add"
and rec.repair_id.repair_type_id.source_location_add_part_id
):
rec.location_id = (
rec.repair_id.repair_type_id.source_location_add_part_id
)
if (
rec.type == "add"
and rec.repair_id.repair_type_id.destination_location_add_part_id
):
rec.location_dest_id = (
rec.repair_id.repair_type_id.destination_location_add_part_id
)
if (
rec.type == "remove"
and rec.repair_id.repair_type_id.source_location_remove_part_id
):
rec.location_id = (
rec.repair_id.repair_type_id.source_location_remove_part_id
)
if (
rec.type == "remove"
and rec.repair_id.repair_type_id.destination_location_remove_part_id
):
rec.location_dest_id = (
rec.repair_id.repair_type_id.destination_location_remove_part_id
)
return res

@api.onchange("type")
def onchange_operation_type(self):
# this onchange was overriding the changes from the compute
# method `_compute_location_id`, we ensure that the locations
# in the types have more priority by explicit calling the compute.
res = super().onchange_operation_type()
self._compute_location_id()
return res
for part in rec.move_ids:
if (
part.repair_line_type == "add"
and rec.repair_type_id.source_location_add_part_id
):
part.location_id = rec.repair_type_id.source_location_add_part_id
if (
part.repair_line_type == "add"
and rec.repair_type_id.destination_location_add_part_id
):
part.location_dest_id = (
rec.repair_type_id.destination_location_add_part_id
)
if (
part.repair_line_type == "remove"
and rec.repair_type_id.source_location_remove_part_id
):
part.location_id = rec.repair_type_id.source_location_remove_part_id
if (
part.repair_line_type == "remove"
and rec.repair_type_id.destination_location_remove_part_id
):
part.location_dest_id = (
rec.repair_type_id.destination_location_remove_part_id
)
41 changes: 41 additions & 0 deletions repair_type/models/stock_move.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright (C) 2024 APSL-Nagarro (Advanced Programming Solutions, S.L.)
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)

from odoo import api, models


class StockMove(models.Model):
_inherit = "stock.move"

@api.depends("repair_line_type", "repair_id.repair_type_id")
def _compute_location_id(self):
for rec in self:
repair_type_id = rec.repair_id.repair_type_id
if rec.repair_line_type == "add":
if repair_type_id.source_location_add_part_id:
rec.location_id = repair_type_id.source_location_add_part_id

Check warning on line 16 in repair_type/models/stock_move.py

View check run for this annotation

Codecov / codecov/patch

repair_type/models/stock_move.py#L16

Added line #L16 was not covered by tests
if repair_type_id.destination_location_add_part_id:
rec.location_dest_id = (

Check warning on line 18 in repair_type/models/stock_move.py

View check run for this annotation

Codecov / codecov/patch

repair_type/models/stock_move.py#L18

Added line #L18 was not covered by tests
repair_type_id.destination_location_add_part_id
)
elif rec.repair_line_type == "remove":
if repair_type_id.source_location_remove_part_id:
rec.location_id = repair_type_id.source_location_remove_part_id
if repair_type_id.destination_location_remove_part_id:
rec.location_dest_id = (
repair_type_id.destination_location_remove_part_id
)

@api.onchange("repair_line_type")
def onchange_operation_type(self):
# this onchange was overriding the changes from the compute
# method `_compute_location_id`, we ensure that the locations
# in the types have more priority by explicit calling the compute.
return self._compute_location_id()

Check warning on line 34 in repair_type/models/stock_move.py

View check run for this annotation

Codecov / codecov/patch

repair_type/models/stock_move.py#L34

Added line #L34 was not covered by tests

def write(self, vals):
res = super().write(vals)
for rec in self:
if "repair_line_type" in vals:
rec._compute_location_id()
return res
4 changes: 4 additions & 0 deletions repair_type/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
- [ForgeFlow](https://forgeflow.com):

> - Bernat Puig \<<[email protected]>\>
- [APSL-Nagarro](https://apsl.tech):

> - Miquel Alzanillas \<<[email protected]>\>
7 changes: 7 additions & 0 deletions repair_type/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,13 @@ <h2><a class="toc-backref" href="#toc-entry-7">Contributors</a></h2>
</ul>
</blockquote>
</li>
<li><p class="first"><a class="reference external" href="https://apsl.tech">APSL-Nagarro</a>:</p>
<blockquote>
<ul class="simple">
<li>Miquel Alzanillas &lt;<a class="reference external" href="mailto:malzanillas&#64;apsl.net">malzanillas&#64;apsl.net</a>&gt;</li>
</ul>
</blockquote>
</li>
</ul>
</div>
<div class="section" id="maintainers">
Expand Down
57 changes: 30 additions & 27 deletions repair_type/tests/test_repair_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@

class TestRepairType(TransactionCase):
def setUp(self, *args, **kwargs):
super(TestRepairType, self).setUp(*args, **kwargs)
super().setUp(*args, **kwargs)

# First of all we create a repair to work with
self.repair_r1 = self.env.ref("repair.repair_r1")

# Now we will create a location scrap for the destination location of removed components
# Now we will create a location scrap for the destination location
# of removed components
stock_location_locations_virtual = self.env["stock.location"].create(
{"name": "Virtual Locations", "usage": "view", "posz": 1}
)
Expand Down Expand Up @@ -62,12 +63,12 @@ def setUp(self, *args, **kwargs):

# Finally we add two line components to the repair order,
# one adding a component and the other one removing
self.add_component = self.env["repair.line"].create(
self.add_component = self.env["stock.move"].create(
{
"name": "Add Component 1",
"repair_id": 1,
"price_unit": 2.0,
"type": "add",
"repair_line_type": "add",
"product_id": self.env.ref("product.product_product_3").id,
"product_uom": self.env.ref("product.product_product_3").uom_id.id,
"product_uom_qty": 1.0,
Expand All @@ -78,12 +79,12 @@ def setUp(self, *args, **kwargs):
"company_id": self.env.company.id,
}
)
self.remove_component = self.env["repair.line"].create(
self.remove_component = self.env["stock.move"].create(
{
"name": "Add Component 2",
"repair_id": 1,
"price_unit": 2.0,
"type": "remove",
"repair_line_type": "remove",
"product_id": self.env.ref("product.product_product_11").id,
"product_uom": self.env.ref("product.product_product_11").uom_id.id,
"product_uom_qty": 1.0,
Expand All @@ -94,8 +95,8 @@ def setUp(self, *args, **kwargs):
"company_id": self.env.company.id,
}
)
self.repair_r1.operations |= self.add_component
self.repair_r1.operations |= self.remove_component
self.repair_r1.move_ids |= self.add_component
self.repair_r1.move_ids |= self.remove_component

def test_compute_location_id(self):
# First we associate the repair with the repair type
Expand All @@ -107,61 +108,63 @@ def test_compute_location_id(self):
self.repair_r1.location_id, self.repair_type_1.source_location_id
)

# Next we assert if the source and destination locations of the components are correct
# Next we assert if the source and destination locations
# of the components are correct
self.assertEqual(
self.repair_r1.operations[0].location_id,
self.repair_r1.move_ids[0].location_id,
self.repair_type_1.source_location_add_part_id,
)
self.assertEqual(
self.repair_r1.operations[0].location_dest_id,
self.repair_r1.move_ids[0].location_dest_id,
self.repair_type_1.destination_location_add_part_id,
)
self.assertEqual(
self.repair_r1.operations[1].location_id,
self.repair_r1.move_ids[1].location_id,
self.repair_type_1.source_location_add_part_id,
)
self.assertEqual(
self.repair_r1.operations[1].location_dest_id,
self.repair_r1.move_ids[1].location_dest_id,
self.repair_type_1.destination_location_add_part_id,
)
self.assertEqual(
self.repair_r1.operations[2].location_id,
self.repair_r1.move_ids[2].location_id,
self.repair_type_1.source_location_remove_part_id,
)
self.assertEqual(
self.repair_r1.operations[2].location_dest_id,
self.repair_r1.move_ids[2].location_dest_id,
self.repair_type_1.destination_location_remove_part_id,
)

# We change the repair type to repair_type_2 and check if all the locations changed
# We change the repair type to repair_type_2 and
# check if all the locations changed
self.repair_r1.repair_type_id = self.repair_type_2

self.assertEqual(
self.repair_r1.location_id, self.repair_type_2.source_location_id
)

self.assertEqual(
self.repair_r1.operations[0].location_id,
self.repair_r1.move_ids[0].location_id,
self.repair_type_2.source_location_add_part_id,
)
self.assertEqual(
self.repair_r1.operations[0].location_dest_id,
self.repair_r1.move_ids[0].location_dest_id,
self.repair_type_2.destination_location_add_part_id,
)
self.assertEqual(
self.repair_r1.operations[1].location_id,
self.repair_r1.move_ids[1].location_id,
self.repair_type_2.source_location_add_part_id,
)
self.assertEqual(
self.repair_r1.operations[1].location_dest_id,
self.repair_r1.move_ids[1].location_dest_id,
self.repair_type_2.destination_location_add_part_id,
)
self.assertEqual(
self.repair_r1.operations[2].location_id,
self.repair_r1.move_ids[2].location_id,
self.repair_type_2.source_location_remove_part_id,
)
self.assertEqual(
self.repair_r1.operations[2].location_dest_id,
self.repair_r1.move_ids[2].location_dest_id,
self.repair_type_2.destination_location_remove_part_id,
)

Expand All @@ -171,23 +174,23 @@ def test_compute_location_id_2(self):
self.repair_r1.repair_type_id = self.repair_type_1

self.assertEqual(
self.repair_r1.operations[0].location_id,
self.repair_r1.move_ids[0].location_id,
self.repair_type_1.source_location_add_part_id,
)
self.assertEqual(
self.repair_r1.operations[0].location_dest_id,
self.repair_r1.move_ids[0].location_dest_id,
self.repair_type_1.destination_location_add_part_id,
)

# Then we change the type of operation
self.repair_r1.operations[0].type = "remove"
self.repair_r1.move_ids[0].repair_line_type = "remove"

# And finally we assert that the locations of that operation changed
self.assertEqual(
self.repair_r1.operations[0].location_id,
self.repair_r1.move_ids[0].location_id,
self.repair_type_1.source_location_remove_part_id,
)
self.assertEqual(
self.repair_r1.operations[0].location_dest_id,
self.repair_r1.move_ids[0].location_dest_id,
self.repair_type_1.destination_location_remove_part_id,
)
2 changes: 1 addition & 1 deletion repair_type/views/repair.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<field name="inherit_id" ref="repair.view_repair_order_form" />
<field name="arch" type="xml">
<field name="product_id" position="after">
<field name="repair_type_id" attrs="{'required': True}" />
<field name="repair_type_id" required="1" />
</field>
</field>
</record>
Expand Down

0 comments on commit 573126f

Please sign in to comment.