Skip to content

Commit

Permalink
[OU-IMP] stock: review followups
Browse files Browse the repository at this point in the history
  • Loading branch information
hbrunn committed Nov 29, 2024
1 parent 5fff466 commit 5f86f8e
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 11 deletions.
70 changes: 69 additions & 1 deletion openupgrade_scripts/scripts/stock/17.0.1.1/post-migration.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# Copyright 2024 Viindoo Technology Joint Stock Company (Viindoo)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from openupgradelib import openupgrade
Expand All @@ -13,7 +12,76 @@ def _stock_scrap_convert_move_id_m2o_to_o2m(env):
)


def fix_move_quantity(env):
"""
Recompute move quantity for move lines that have been changed in pre-migration
"""
env.cr.execute(
"""
SELECT DISTINCT move_id FROM stock_move_line
WHERE
state IN ('assigned', 'partially_available')
AND reserved_qty <> 0
"""
)
moves = env["stock.move"].browse(_id for (_id,) in env.cr.fetchall())
env.add_to_compute(moves._fields["quantity"], moves)
moves._recompute_recordset(["quantity"])


def link_returned_pickings(env):
"""
Link pickings containing returned moves to the pickings containing the moves
being returned
"""
openupgrade.logged_query(
env.cr,
"""
UPDATE stock_picking
SET
return_id = returned_move.picking_id
FROM
stock_move
JOIN stock_move returned_move
ON stock_move.origin_returned_move_id = returned_move.id
WHERE
stock_move.picking_id = stock_picking.id
""",
)


def set_picking_type_return_location(env):
"""
Set default_location_return_id on picking types from the destination location
of the warehouse's return picking type
"""
openupgrade.logged_query(
env.cr,
"""
UPDATE stock_picking_type
SET
default_location_return_id = COALESCE(
picking_return_type.default_location_dest_id,
warehouse_return_type.default_location_dest_id
)
FROM
stock_picking_type self
JOIN stock_warehouse
ON self.warehouse_id=stock_warehouse.id
LEFT JOIN stock_picking_type picking_return_type
ON self.return_picking_type_id=picking_return_type.id
LEFT JOIN stock_picking_type warehouse_return_type
ON stock_warehouse.return_type_id=warehouse_return_type.id
WHERE
stock_picking_type.id=self.id
""",
)


@openupgrade.migrate()
def migrate(env, version):
openupgrade.load_data(env, "stock", "17.0.1.1/noupdate_changes.xml")
_stock_scrap_convert_move_id_m2o_to_o2m(env)
fix_move_quantity(env)
link_returned_pickings(env)
set_picking_type_return_location(env)
33 changes: 31 additions & 2 deletions openupgrade_scripts/scripts/stock/17.0.1.1/pre-migration.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,43 @@
# Copyright 2024 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from openupgradelib import openupgrade

_field_renames = [
("stock.move", "stock_move", "quantity_done", "quantity"),
("stock.move.line", "stock_move_line", "qty_done", "quantity"),
]

_column_copies = {
"stock_move_line": [
("qty_done", "quantity", None),
]
}


def fix_move_line_quantity(env):
"""
v17 combines what used to be reserved_qty and qty_done.
We assume that we shouldn't touch an original qty_done on
done moves, but that we can best reflect the v16 state of
lines being worked on by adding reserved_qty to the new
quantity column, which was qty_done in v16
In post-migration, we'll recompute the quantity field of
moves affected.
"""
openupgrade.logged_query(
env.cr,
"""
UPDATE stock_move_line
SET quantity = quantity + reserved_qty
WHERE
state IN ('assigned', 'partially_available')
AND reserved_qty <> 0
""",
)


@openupgrade.migrate()
def migrate(env, version):
openupgrade.rename_fields(env, _field_renames)
openupgrade.copy_columns(env.cr, _column_copies)
fix_move_line_quantity(env)
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ stock / stock.move / picking_type_id (many2one) : not a

stock / stock.move / quantity (float) : NEW isfunction: function, stored
stock / stock.move / quantity_done (float) : DEL
# DONE: pre-migration
# DONE: renamed in pre-migration

stock / stock.move / scrap_id (many2one) : NEW relation: stock.scrap
stock / stock.move / scrap_ids (one2many) : DEL relation: stock.scrap
# NOTHING TO DO
stock / stock.scrap / move_id (many2one) : DEL relation: stock.move
stock / stock.scrap / move_ids (one2many) : NEW relation: stock.move
# DONE post-migration: convert data many2one to one2many

stock / stock.move.line / qty_done (float) : DEL
stock / stock.move.line / quantity (float) : NEW hasdefault: compute
Expand All @@ -45,8 +47,12 @@ stock / stock.move.line / quantity (float) : NEW ha
stock / stock.move.line / picked (boolean) : NEW hasdefault: compute
stock / stock.move.line / quant_id (many2one) : NEW relation: stock.quant
stock / stock.move.line / quantity_product_uom (float) : NEW isfunction: function, stored
# NOTHING TO DO

stock / stock.move.line / reserved_qty (float) : DEL
stock / stock.move.line / reserved_uom_qty (float) : DEL required
# DONE: added to quantity in pre-migration, recompute stock.move#quantity in post-migration


stock / stock.package.type / height (integer) : type is now 'float' ('integer')
stock / stock.package.type / packaging_length (integer) : type is now 'float' ('integer')
Expand All @@ -62,10 +68,13 @@ stock / stock.picking / move_ids_without_package (one2many): n
stock / stock.picking / move_line_nosuggest_ids (one2many): DEL relation: stock.move.line
stock / stock.picking / picking_properties (properties): NEW hasdefault: compute
stock / stock.picking / rating_ids (one2many) : NEW relation: rating.rating
stock / stock.picking / return_id (many2one) : NEW relation: stock.picking
stock / stock.picking / return_ids (one2many) : NEW relation: stock.picking
stock / stock.picking / show_operations (boolean) : not a function anymore
stock / stock.picking / show_operations (boolean) : now related
# NOTHING TO DO

stock / stock.picking / return_id (many2one) : NEW relation: stock.picking
stock / stock.picking / return_ids (one2many) : NEW relation: stock.picking
# DONE filled from returned moves in post-migration

stock / stock.picking.type / auto_print_delivery_slip (boolean): NEW
stock / stock.picking.type / auto_print_lot_labels (boolean): NEW
Expand All @@ -75,17 +84,15 @@ stock / stock.picking.type / auto_print_product_labels (boolean): N
stock / stock.picking.type / auto_print_reception_report (boolean): NEW
stock / stock.picking.type / auto_print_reception_report_labels (boolean): NEW
stock / stock.picking.type / auto_print_return_slip (boolean): NEW
stock / stock.picking.type / default_location_return_id (many2one): NEW relation: stock.location
stock / stock.picking.type / lot_label_format (selection) : NEW selection_keys: ['4x12_lots', '4x12_units', 'zpl_lots', 'zpl_units'], hasdefault: default
stock / stock.picking.type / package_label_to_print (selection): NEW selection_keys: ['pdf', 'zpl'], hasdefault: default
stock / stock.picking.type / picking_properties_definition (properties_definition): NEW
stock / stock.picking.type / product_label_format (selection): NEW selection_keys: ['2x7xprice', '4x12', '4x12xprice', '4x7xprice', 'dymo', 'zpl', 'zplxprice'], hasdefault: default
stock / stock.picking.type / show_reserved (boolean) : now a function
# NOTHING TO DO: fields in new model

stock / stock.scrap / move_id (many2one) : DEL relation: stock.move
stock / stock.scrap / move_ids (one2many) : NEW relation: stock.move
# DONE post-migration: convert data many2one to one2many
stock / stock.picking.type / default_location_return_id (many2one): NEW relation: stock.location
# DONE set from warehouse's previous return picking type's destination location

stock / stock.scrap / message_main_attachment_id (many2one): DEL relation: ir.attachment
stock / stock.scrap / rating_ids (one2many) : NEW relation: rating.rating
Expand Down
16 changes: 16 additions & 0 deletions openupgrade_scripts/scripts/stock/tests/data_stock_migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
env = locals().get("env")
# return a picking so that we have something to migrate
picking = env.ref("stock.outgoing_shipment_main_warehouse1")
return_wizard = (
env["stock.return.picking"]
.with_context(
active_id=picking.id,
active_ids=picking.ids,
active_model=picking._name,
)
.create({})
)
return_wizard._onchange_picking_id()
return_wizard._create_returns()

env.cr.commit()
31 changes: 31 additions & 0 deletions openupgrade_scripts/scripts/stock/tests/test_stock_migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from odoo.tests import TransactionCase

from odoo.addons.openupgrade_framework import openupgrade_test


@openupgrade_test
class TestStockMigration(TransactionCase):
def test_move_quantity(self):
"""
Test that we add reserved_qty to quantity for assigned moves
"""
picking = self.env.ref("stock.outgoing_shipment_main_warehouse4")
self.assertEqual(picking.move_ids.quantity, 16)
picking = self.env.ref("stock.incomming_shipment2")
self.assertEqual(picking.move_ids.quantity, 125)

def test_returned_picking(self):
"""
Test that we correctly link returned pickings to their origin picking
"""
returned_picking = self.env.ref("stock.outgoing_shipment_main_warehouse1")
self.assertTrue(returned_picking.return_ids)

def test_return_location(self):
"""
Test that we set the default return location for pickings from the warehouse's
return picking type from v16
"""
picking_type = self.env.ref("stock.picking_type_in")
stock_location = self.env.ref("stock.stock_location_stock")
self.assertEqual(picking_type.default_location_return_id, stock_location)

0 comments on commit 5f86f8e

Please sign in to comment.