Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] repair,mrp_repair: add kit products to confirmed repair orders #163847

Open
wants to merge 1 commit into
base: saas-16.4
Choose a base branch
from

Conversation

naja628
Copy link
Contributor

@naja628 naja628 commented Apr 29, 2024

Problem

When adding a kit manufactured product to a confirmed repair order,
an error is thrown upon saving, because we're working with a move
that was already deleted/exploded

Steps

  • install repair,mrp
  • create a repair order and confirm it
  • add a kit manufactured product to it
  • the error is thrown (Record already deleted)

Fix

When confirming moves, we explode them, so when we're adding directly to
the repair order, we need to make sure we take the new moves.

opw-3889564


I confirm I have signed the CLA and read the PR guidelines at www.odoo.com/submit-pr

@robodoo
Copy link
Contributor

robodoo commented Apr 29, 2024

@C3POdoo C3POdoo added the OE the report is linked to a support ticket (opw-...) label Apr 29, 2024
@naja628 naja628 force-pushed the saas-16.4-opw-3889564-cant_add_kit-naja branch 5 times, most recently from 5b69180 to d76f084 Compare May 2, 2024 08:06
@naja628
Copy link
Contributor Author

naja628 commented May 2, 2024

Note:
When the repair order is not yet confirmed, the flow seems to go through here:

def _action_repair_confirm(self):
""" Repair order state is set to 'Confirmed'.
@param *arg: Arguments
@return: True
"""
repairs_to_confirm = self.filtered(lambda repair: repair.state == 'draft')
repairs_to_confirm._check_company()
repairs_to_confirm.move_ids._check_company()
repairs_to_confirm.move_ids._adjust_procure_method()
repairs_to_confirm.move_ids._action_confirm()
repairs_to_confirm.move_ids._trigger_scheduler()
repairs_to_confirm.write({'state': 'confirmed'})
return True

and I'm not sure I fully understand why we don't have the "same" problem

@naja628 naja628 force-pushed the saas-16.4-opw-3889564-cant_add_kit-naja branch from d76f084 to b6064b1 Compare May 3, 2024 07:53
Copy link
Contributor

@adwid adwid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with the idea of the fix
Just few improvements :)

Note: PR's target is ok, the issue starts from 16.4, since the refactoring (eaa6e1a)


if move.state == 'draft' and move.repair_id.state in ('confirmed', 'under_repair'):
move._check_company()
move._adjust_procure_method()
move._action_confirm()
move._trigger_scheduler()
exploded = move._action_confirm()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Nitpicking)
In a lot of cases, the method will return the same move. So let's make the name simpler

Suggested change
exploded = move._action_confirm()
moves = move._action_confirm()

Comment on lines 81 to 83
repair_moves |= exploded
else:
repair_moves |= move
Copy link
Contributor

@adwid adwid May 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not something like ?

Suggested change
repair_moves |= exploded
else:
repair_moves |= move
repair_moves |= moves

Where moves is initiated before the if-block ?

Comment on lines 20 to 25
repaired = self.env['product.product'].create({'name': 'Repaired'})
part = self.env['product.product'].create({'name': 'Kit Component'})
kit = self.env['product.product'].create({
'name': 'Kit',
'type': 'product',
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always better to work in batch

Suggested change
repaired = self.env['product.product'].create({'name': 'Repaired'})
part = self.env['product.product'].create({'name': 'Kit Component'})
kit = self.env['product.product'].create({
'name': 'Kit',
'type': 'product',
})
repaired, part, kit = self.env['product.product'].create([
{...},
...
])

Comment on lines 30 to 32
})
bom.write({
'bom_line_ids': [self.env['mrp.bom.line'].create({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not directly add the line while creating the BoM ?
We could then avoid a useless call to write
Also, be careful when dealing with 2many fields, you need to use some commands

Suggested change
})
bom.write({
'bom_line_ids': [self.env['mrp.bom.line'].create({
'bom_line_ids': [(0, 0, {

}).id],
})
try:
# create repair order and confirm it
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# create repair order and confirm it

The code is clear enough :)

ro_form.product_id = repaired
ro = ro_form.save()
ro.action_validate()
# add kit
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# add kit

Same

Comment on lines 49 to 50
except AccessError:
self.fail("Cannot add kit manufactured product to existing repair order")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use try-except to test such use cases
Just do your calls directly in the test body, and if one call raises an error, the test will fail
Else, if there isn't any raised error, the test will pass
That being said, it's then a bit weird to have a test without any assert
So it's always worth adding at least one
Here for instance, you could check that the line has been replaced with a new line for the component

Comment on lines 11 to 14
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.env.ref('base.group_user').write({'implied_ids': [(4, cls.env.ref('stock.group_production_lot').id)]})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're not using any lot here, so I don't get the reason why it would be useful ?

Suggested change
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.env.ref('base.group_user').write({'implied_ids': [(4, cls.env.ref('stock.group_production_lot').id)]})

@adwid
Copy link
Contributor

adwid commented May 8, 2024

and I'm not sure I fully understand why we don't have the "same" problem

When we _action_confirm a SM and if it's a kit, we will unlink the SM and rather create/confirm the SM of the components (hence the error you had, and the fix you did). You don't have any issue in the method you mention because, after the confirm, we take the SM thanks to repairs_to_confirm.move_ids: if in the meantime, the moves have changed, you will have the new ones

@adwid
Copy link
Contributor

adwid commented May 8, 2024

Also, for the commit redaction, it's always better to explain why the issue occurs, instead of what the commit does as we can see that directly in the diff

See the guidelines and the section "Write a commit" in our documentation :)

So here, instead of

Fix

make sure we're working with the moves after they are exploded.

You can rather explain that, when confirming the SM and since it's a kit, we will explode it:

def _action_confirm(self, merge=True, merge_into=False):
moves = self.action_explode()

And therefore, keeping a reference to the initial move is incorrect as it has been deleted during its confirmation

@naja628 naja628 force-pushed the saas-16.4-opw-3889564-cant_add_kit-naja branch 3 times, most recently from 23ef92a to d4ee07b Compare May 8, 2024 14:05
Copy link
Contributor

@adwid adwid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few small things, then it should be ok
I think there is an error in the commit description "When confirming moves, we explode them, so when we're adding directly to the repair order, we need to make we take the new moves."

{'name': 'Kit Component2'},
{'name': 'Kit', 'type': 'product'},
])
CREATE = 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from odoo.fields import Command

'bom_line_ids': [
(CREATE, 0, {
'product_id': part1.id,
'product_tmpl_id': part1.product_tmpl_id.id,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting the template is useless, and imo setting the quantity would make it clearer

Suggested change
'product_tmpl_id': part1.product_tmpl_id.id,
'product_qty': 1.0,

ro_line.product_id = kit
ro_form.save()

self.assertEqual(len(ro_form.move_ids), 2, "Repair order moves should correspond to the kit components")
Copy link
Contributor

@adwid adwid May 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the form is a bit weird, it's better to use the record

Also, it could be nice to also ensure that move_ids are based on the components: either by using assertRecordValues (and getting rid of the assertEqual), or by using another assertEqual on ro.move_ids.product_id.
That way, you can also remove the error message as the assert will be clear enough

@naja628 naja628 force-pushed the saas-16.4-opw-3889564-cant_add_kit-naja branch from d4ee07b to af7c0ff Compare May 13, 2024 15:30
})

self.assertEqual(len(ro.move_ids), 2, "Repair order moves should correspond to the kit components")
self.assertEqual(ro.move_ids.mapped('product_id'), part1 | part2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.assertEqual(ro.move_ids.mapped('product_id'), part1 | part2)
self.assertEqual(ro.move_ids.product_id, part1 | part2)

Comment on lines 41 to 45
self.env['stock.move'].create({
'repair_id': ro.id,
'product_id': kit.id,
'repair_line_type': 'add',
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpicking 🙈

Suggested change
self.env['stock.move'].create({
'repair_id': ro.id,
'product_id': kit.id,
'repair_line_type': 'add',
})
self.env['stock.move'].create({
'repair_id': ro.id,
'product_id': kit.id,
'repair_line_type': 'add',
})

@naja628 naja628 force-pushed the saas-16.4-opw-3889564-cant_add_kit-naja branch from af7c0ff to 9d8ee69 Compare May 15, 2024 09:36
@naja628 naja628 changed the title [FIX] repair: add kit products to confirmed repair orders [FIX] repair,mrp_repair: add kit products to confirmed repair orders May 15, 2024
Problem
---
When adding a kit manufactured product to a confirmed repair order,
an error is thrown upon saving, because we're working with a move
that was already deleted/exploded

Steps
---
* install repair,mrp
* create a repair order and confirm it
* add a kit manufactured product to it
* the error is thrown (Record already deleted)

Fix
---
When confirming moves, we explode them, so when we're adding directly to
the repair order, we need to make sure we take the new moves.

opw-3889564
@naja628 naja628 force-pushed the saas-16.4-opw-3889564-cant_add_kit-naja branch from 9d8ee69 to e4bba88 Compare May 15, 2024 13:21
@naja628 naja628 marked this pull request as ready for review May 15, 2024 14:52
@C3POdoo C3POdoo requested a review from a team May 15, 2024 15:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OE the report is linked to a support ticket (opw-...)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants