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

[16.0][MIG] stock_valuation_fifo_lot: Migration to 16.0 #1676

Open
wants to merge 17 commits into
base: 16.0
Choose a base branch
from

Conversation

AungKoKoLin1997
Copy link
Contributor

@AungKoKoLin1997 AungKoKoLin1997 commented Aug 20, 2024

Superseding of #1527

In this PR, I address the following points:

  • Standard migration
  • Improved code by using the Odoo standard function _get_fifo_candidates() and adjustments to _get_price_unit()
  • Fixed the issue proposed in [15.0][ADD] stock_valuation_fifo_lot #1350 (comment)
  • Avoid completely overriding _run_fifo() and use the standard behavior.
  • Update the product's standard price to the first available lot price.
  • Improve _get_price_unit() to retrieve the value from the most recent incoming move line for the lot for the No PO (e.g. customer returns) stock moves.
  • Add quantity and value-related fields in stock_move_line to cover all cases including multiple lots exist in an SVL record, and it is unclear which lot's remaining quantity is being delivered(Eg. Receive serials 001, 002 and 003 for an incoming move. Deliver 002, return 002 and deliver 002 again.)
  • Add force_fifo_lot_id in stock_move_line to handle cases where a delivery needs to be made for an existing lot created before the installation of this module (e.g., lots 001 and 002 were received, lot 002 was delivered before the module's installation, and lot 001 is delivered after the module is installed).
  • Add a post_init_hook to update the lot_ids in SVL for existing records and to update the field values in existing stock_move_line records.

Depends on odoo/odoo#180245
@qrtl QT4650

@AungKoKoLin1997
Copy link
Contributor Author

AungKoKoLin1997 commented Aug 21, 2024

Test failed from stock_picking_line_sequence is fixed at #1557.

@AungKoKoLin1997 AungKoKoLin1997 force-pushed the 16.0-mig-stock_valuation_fifo_lot branch from b60bb4e to 71c8a67 Compare August 21, 2024 08:19
@AungKoKoLin1997 AungKoKoLin1997 marked this pull request as ready for review August 21, 2024 08:19
@AungKoKoLin1997
Copy link
Contributor Author

@matt454357
Can you please review this PR and bring your ideas from your module OCA/stock-logistics-warehouse#2139 as we talked?

@matt454357
Copy link

matt454357 commented Aug 21, 2024

I started a review, and will post some suggestions.

@AungKoKoLin1997
Copy link
Contributor Author

@matt454357
What I am thinking is that I will handle adding the option for a single lot/serial per SVL record or multiple lots/serials per SVL record in this PR (since some users may not need a single lot/serial per SVL).

After you review and the PR is merged, you can create a new PR to follow up on your idea.
Or would you prefer to include your idea as a new commit in this PR by creating a PR in our repository?

What do you think?

@AungKoKoLin1997 AungKoKoLin1997 force-pushed the 16.0-mig-stock_valuation_fifo_lot branch from 71c8a67 to 07a5f63 Compare August 22, 2024 06:57
@AungKoKoLin1997
Copy link
Contributor Author

I will handle adding the option for a single lot/serial per SVL record or multiple lots/serials per SVL record in this PR (since some users may not need a single lot/serial per SVL).

@matt454357 I will not add this enhancement in the current PR because it is not related to migration, and there is no use case with the current design. So, could you please handle this when you work on revaluation?

@matt454357
Copy link

I think it's a good idea to finish this migration PR, and then do a follow up PR. And, I'm happy to do the follow up PR. However, there a couple of problems with allowing multiple lots/serials per SVL.

Maybe this initial migration should prevent revaluation of tracked products?

Maybe also raise an error when _get_price_unit() is called with multiple lot_ids?

@matt454357
Copy link

The believe PR #1677 supports the idea of enforcing single lot/serial per SVL.

Copy link

@matt454357 matt454357 left a comment

Choose a reason for hiding this comment

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

I'm just commenting on this PR, not suggesting that we make these changes immediately.

return_move = return_picking.move_ids
return_move.move_line_ids.qty_done = 5
return_picking.button_validate()
self.assertEqual(abs(return_move.stock_valuation_layer_ids.value), 1500)

Choose a reason for hiding this comment

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

Suggested change
self.assertEqual(abs(return_move.stock_valuation_layer_ids.value), 1500)
self.assertEqual(abs(return_move.stock_valuation_layer_ids.value), 1500)
def test_customer_partial_return(self):
"""Verify partial customer return of delivery with multiple lots of the
same product"""
receipt_picking_1 = self.create_picking(
self.supplier_location, self.stock_location, self.picking_type_in
)
move = self.create_stock_move(receipt_picking_1, self.product, 100)
self.create_stock_move_line(move, "11111")
receipt_picking_1.action_confirm()
receipt_picking_1.action_assign()
receipt_picking_1._action_done()
receipt_picking_2 = self.create_picking(
self.supplier_location, self.stock_location, self.picking_type_in
)
move = self.create_stock_move(receipt_picking_2, self.product, 200)
self.create_stock_move_line(move, "22222")
receipt_picking_2.action_confirm()
receipt_picking_2.action_assign()
receipt_picking_2._action_done()
delivery_picking_2 = self.create_picking(
self.stock_location, self.customer_location, self.picking_type_out
)
move = self.create_stock_move(delivery_picking_2, self.product)
move_line = self.create_stock_move_line(move)
lot_id = self.env["stock.lot"].search(
[("name", "=", "11111"), ("product_id", "=", self.product.id)]
)
move_line.write({"lot_id": lot_id})
move_line = self.create_stock_move_line(move)
lot_id = self.env["stock.lot"].search(
[("name", "=", "22222"), ("product_id", "=", self.product.id)]
)
move_line.write({"lot_id": lot_id})
delivery_picking_2.action_confirm()
delivery_picking_2.action_assign()
delivery_picking_2._action_done()
return_picking_wizard_form = Form(
self.env["stock.return.picking"].with_context(
active_ids=delivery_picking_2.ids,
active_id=delivery_picking_2.id,
active_model="stock.picking",
)
)
return_picking_wizard = return_picking_wizard_form.save()
return_picking_wizard.product_return_moves.write({"quantity": 5})
return_picking_wizard_action = return_picking_wizard.create_returns()
return_picking = self.env["stock.picking"].browse(
return_picking_wizard_action["res_id"]
)
return_move = return_picking.move_ids
return_move.move_line_ids.lot_id = lot_id
return_move.move_line_ids.qty_done = 5
return_picking.button_validate()
self.assertEqual(abs(return_move.stock_valuation_layer_ids.value), 1000)
def test_revaluation_layer(self):
"""Verify revaluation layers are included in unit price calculation"""
receipt_picking_1 = self.create_picking(
self.supplier_location, self.stock_location, self.picking_type_in
)
move = self.create_stock_move(receipt_picking_1, self.product, 1000)
self.create_stock_move_line(move, "11111")
receipt_picking_1.action_confirm()
receipt_picking_1.action_assign()
receipt_picking_1._action_done()
receipt_picking_2 = self.create_picking(
self.supplier_location, self.stock_location, self.picking_type_in
)
move = self.create_stock_move(receipt_picking_2, self.product, 2000)
self.create_stock_move_line(move, "22222")
receipt_picking_2.action_confirm()
receipt_picking_2.action_assign()
receipt_picking_2._action_done()
wiz = (
self.env["stock.valuation.layer.revaluation"]
.sudo()
.create(
{
"company_id": self.company.id,
"product_id": self.product.id,
"added_value": -500.0,
"account_id": self.cost_account.id,
"reason": "test serial revaluation",
}
)
)
wiz.action_validate_revaluation()
delivery_picking = self.create_picking(
self.stock_location, self.customer_location, self.picking_type_out
)
move = self.create_stock_move(delivery_picking, self.product)
move_line = self.create_stock_move_line(move)
lot_id = self.env["stock.lot"].search(
[("name", "=", "11111"), ("product_id", "=", self.product.id)]
)
move_line.write({"lot_id": lot_id})
delivery_picking.action_confirm()
delivery_picking.action_assign()
delivery_picking._action_done()
self.assertEqual(abs(move.stock_valuation_layer_ids.value), 4500)

Choose a reason for hiding this comment

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

These suggested tests fail because we are not enforcing single lot/serial per SVL.

"license": "AGPL-3",
"author": "Ecosoft, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/stock-logistics-workflow",
"depends": ["stock_account", "stock_no_negative", "mrp_landed_costs"],

Choose a reason for hiding this comment

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

I'm not sure we should depend on mrp_landed_costs. Could that be handled in a separate module?

Choose a reason for hiding this comment

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

Maybe move to a separate module?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. I will handle it.

if not self.company_id.use_lot_get_price_unit_fifo:
return super()._get_price_unit()
if (
not self.purchase_line_id

Choose a reason for hiding this comment

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

Depends on purchase module, which I guess is okay.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added hasattr(self, "purchase_line_id") in the conditon. So, there is no issue I guess.

if (
not self.purchase_line_id
and self.product_id.cost_method == "fifo"
and len(self.lot_ids) == 1

Choose a reason for hiding this comment

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

Should we raise an error when len(self.lot_ids) > 1?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you have any concerns or inconveniences if we use Odoo's standard behavior for this case?

Copy link
Sponsor Member

Choose a reason for hiding this comment

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

Should we raise an error when len(self.lot_ids) > 1?

I think we could make it configurable if the community requires it. I don't have a strong need for that at the moment.

Choose a reason for hiding this comment

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

Okay, single lot per SVL would not work when migrating/installing with existing data. So, I don't think we can do that.

@AungKoKoLin1997
Copy link
Contributor Author

I think it's a good idea to finish this migration PR, and then do a follow up PR. And, I'm happy to do the follow up PR. However, there a couple of problems with allowing multiple lots/serials per SVL.

Thanks for the pointer and your interest in following up. What do you think of my idea of adding an option to choose a single or multiple lot/serial per SVL? As I mentioned, I don't want to make this adjustment in the migration PR because it would make the module bigger, and the community might be less interested in reviewing it. So, my idea is to get this PR merged quickly and then follow up with the changes.

@matt454357
Copy link

Yes, keep this PR small.

Regarding the option to choose between multiple or single lot per SVL: I'm not sure why someone would want multiple, but it sounds like you have a use case in mind. So, I suppose it's fine, as long as we raise errors for the issues described above.

@AungKoKoLin1997 AungKoKoLin1997 force-pushed the 16.0-mig-stock_valuation_fifo_lot branch from 07a5f63 to 2a2dfb9 Compare August 23, 2024 08:58
@AungKoKoLin1997
Copy link
Contributor Author

@TheerayutEncoder
I have separated the module for passing lot_producing_id of mrp_production_ids to the SVL when adding the landed cost for these MOs, as you proposed in this commit.
What do you think about it?
Additionally, I am not entirely sure of the purpose of this, since the remaining_qty of the SVL for landed costs is 0.0, and I believe these SVLs will not be considered when _run_fifo() is called, if I understand correctly. Is this intended for user experience, to see the related lot_ids in the SVL? Could you please explain the initial purpose?

@AungKoKoLin1997
Copy link
Contributor Author

What do you think of my idea of adding an option to choose a single or multiple lot/serial per SVL?

@matt454357 Please let me handle this part and include it in this PR because we urgently need this module, and I don't know when this PR will be merged.

@matt454357
Copy link

@AungKoKoLin1997 any contribution you make is very much appreciated, Do it the way you feel is best, and I will work with it.

Copy link

@matt454357 matt454357 left a comment

Choose a reason for hiding this comment

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

Looks good to me

- Adapt Odoo's standard method _get_fifo_candidates.
- Update _get_price_unit logic and make it configurable to also use Odoo's standard behavior.
This commit fixes the incorrect stock_valuation_layer values for deliveries when the product was received before this module was installed.
The issue occurs because the old stock_valuation_layer records do not have lot_ids values, which leads to incorrect valuation during the delivery process.
Issue link: OCA#1350 (comment)
"in",
self.lot_ids.ids,
),
("remaining_qty", ">", 0),
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

I think we misunderstood the usage of this logic. I now think this doesn't work. The original logic isn't optimal either. I'm thinking of a different implementation...

if (
not self.purchase_line_id
and self.product_id.cost_method == "fifo"
and len(self.lot_ids) == 1
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

Should we raise an error when len(self.lot_ids) > 1?

I think we could make it configurable if the community requires it. I don't have a strong need for that at the moment.

@rousseldenis
Copy link
Sponsor Contributor

/ocabot migration stock_valuation_fifo_lot

@OCA-git-bot OCA-git-bot added this to the 16.0 milestone Sep 20, 2024
@OCA-git-bot OCA-git-bot mentioned this pull request Sep 20, 2024
65 tasks
@matt454357
Copy link

We will also need odoo/odoo#179447, in order to do revaluation (later).

- Avoid completely overriding _run_fifo and use the standard behavior.
- Update the product's standard price to the first available lot price.
- Improve _get_price_unit to retrieve the value from the most recent incoming move line for the lot for the No PO (e.g. customer returns) stock moves.
- Add quantity and value-related fields in stock_move_line to cover all cases including multiple lots exist in an SVL record, and it is unclear which lot's remaining quantity is being delivered(Eg. Receive serials 001, 002 and 003 for an incoming move. Deliver 002, return 002 and deliver 002 again.)
- Add force_fifo_lot_id in stock_move_line to handle cases where a delivery needs to be made for an existing lot created before the installation of this module (e.g., lots 001 and 002 were received, lot 002 was delivered before the module's installation, and lot 001 is delivered after the module is installed).
- Add a post_init_hook to update the lot_ids in SVL for existing records and to update the field values in existing stock_move_line records.
- Remove stock_no_negative from dependency and check negative inventory is created when SVL is created for delivery.
forced_quantity=forced_quantity
)
layers |= layer
# Calculate standard price (sorted by lot created date)
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

This comment is in the wrong position.

Suggested change
# Calculate standard price (sorted by lot created date)

continue
for ml in layer.stock_move_id.move_line_ids:
ml.qty_base = ml.qty_done
# Change product standard price to the first available lot price.
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

Suggested change
# Change product standard price to the first available lot price.
# Update product standard price to the first available lot price (by sorting by lot create date).

continue
product = product.with_company(move.company_id.id)
product = product.with_context(disable_auto_svl=True)
product.sudo().standard_price = candidate.unit_cost
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

Suggested change
product.sudo().standard_price = candidate.unit_cost
# `candidate.unit_cost` is not totally accurate in 16.0 because of https://github.com/odoo/odoo/issues/171464
product.sudo().standard_price = candidate.remaining_value / candidate.remaining_qty

Comment on lines 77 to 86
# Change product standard price to the first available lot price.
product = product.with_context(sort_by="lot_create_date")
candidate = product._get_fifo_candidates(move.company_id)[:1]
if not candidate:
continue
product = product.with_company(move.company_id.id)
product = product.with_context(disable_auto_svl=True)
product.sudo().standard_price = candidate.unit_cost
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

I'm actually not clear about the business rationale why we need this part.

@ps-tubtim @Saran440 Could you please explain what business needs you had to have this logic? In my view, product standard price doesn't mean much in FIFO (it doesn't affect the transaction except for the case where inventory is created with a new lot with no purchase order) and this logic can be removed.

Copy link
Sponsor Member

Choose a reason for hiding this comment

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

Copy link
Sponsor Member

Choose a reason for hiding this comment

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

Copy link
Sponsor Member

Choose a reason for hiding this comment

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

I remember we have a case where it will be incorrect without this fix. I can't remember the case. @newtratip do you remember the case or any where issue is open?

"Stock valuation after return should be 1500.0",
)

def test_receive_deliver_return_lot(self):
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

I didn't quite get the point of this test. Does it prove anything when all lots have the same cost?

Copy link
Contributor Author

@AungKoKoLin1997 AungKoKoLin1997 Sep 26, 2024

Choose a reason for hiding this comment

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

This was for the case like we receive serials 001, 002 and 003 for an incoming move.Then, Deliver 002, return 002 and deliver 002 again. In this case, we want to check the remaining_qty was taken from return move's SVL for latest delivery instead of first incoming move's SVL because there remaining qty of first incoming move's SVL is only for both 001 and 002 serials now.

)
self.assertEqual(move_in_2.stock_valuation_layer_ids.remaining_qty, 0.0)

def test_cost_tracking_by_lot(self):
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

This could be combined with the first test? It looks redundant...

@AungKoKoLin1997 AungKoKoLin1997 force-pushed the 16.0-mig-stock_valuation_fifo_lot branch 2 times, most recently from 925a63f to 0c9e845 Compare September 27, 2024 07:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants