diff --git a/addons/stock/migrations/13.0.1.1/post-migration.py b/addons/stock/migrations/13.0.1.1/post-migration.py index 12d0f60f8833..67dc6e2aacbb 100644 --- a/addons/stock/migrations/13.0.1.1/post-migration.py +++ b/addons/stock/migrations/13.0.1.1/post-migration.py @@ -4,6 +4,8 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from openupgradelib import openupgrade import re +import logging as _logging_module +_logger = _logging_module.getLogger(__name__) def _get_main_company(cr): @@ -185,6 +187,53 @@ def map_stock_location_usage(env): ) +def map_stock_picking_responsible_responsible_id_to_user_id(env): + """ + responsible_id (partner_id) field in stock_picking_responsible is replaced by user_id (res.users) + We map the partner to their user and log the partners missing a user + """ + legacy_column_name = openupgrade.get_legacy_name("responsible_id") + if not openupgrade.column_exists(env.cr, "stock_picking", legacy_column_name): + return + + env.cr.execute( + f""" + SELECT sp.id AS picking_id, + sp.name as picking_name, + rp.id AS partner_id, + rp.name as partner_name + FROM stock_picking sp + JOIN res_partner rp ON rp.id = sp.{legacy_column_name} + LEFT JOIN res_users ru ON rp.id = ru.partner_id + WHERE ru.id IS NULL + """ + ) + partner_wo_user = env.cr.fetchall() + for picking_id, picking_name, partner_id, partner_name in partner_wo_user: + _logger.warning( + f"Picking (%{picking_id}) {picking_name}:" + f" cannot match user to ({partner_id}) {partner_name}" + ) + + # map responsible_id to user_id + openupgrade.logged_query( + env.cr, + f""" + WITH partner_user AS ( + SELECT sp.id AS picking_id, + rp.id AS partner_id, + ru.id AS user_id + FROM stock_picking sp + JOIN res_partner rp ON rp.id = sp.{legacy_column_name} + LEFT join res_users ru ON rp.id = ru.partner_id) + UPDATE stock_picking + SET user_id = partner_user.user_id + FROM partner_user + WHERE stock_picking.id = partner_user.picking_id; + """ + ) + + def fill_stock_picking_type_sequence_code(env): """Deduce sequence code from current sequence pattern """ picking_types = env["stock.picking.type"].with_context(active_text=False).search([]) @@ -411,6 +460,7 @@ def migrate(env, version): fill_propagate_date_minimum_delta(env) fill_stock_inventory_start_empty(env) map_stock_location_usage(env) + map_stock_picking_responsible_responsible_id_to_user_id(env) fill_stock_picking_type_sequence_code(env) handle_stock_scrap_sequence(env, main_company) map_stock_locations(env, main_company) diff --git a/addons/stock/migrations/13.0.1.1/pre-migration.py b/addons/stock/migrations/13.0.1.1/pre-migration.py index 71b3c3d5188c..42259d74e01c 100644 --- a/addons/stock/migrations/13.0.1.1/pre-migration.py +++ b/addons/stock/migrations/13.0.1.1/pre-migration.py @@ -85,6 +85,18 @@ def prefill_stock_picking_type_sequence_code(env): ) +def merge_stock_picking_responsible_module(env): + """ + The stock_picking_responsible module is now merged in stock. + We copy the legacy column to convert data in post-migration script. + """ + if openupgrade.column_exists(env.cr, "stock_picking", "responsible_id"): + openupgrade.copy_columns( + env.cr, + {'stock_picking': [('responsible_id', None, None)]} + ) + + @openupgrade.migrate() def migrate(env, version): openupgrade.copy_columns(env.cr, _column_copies) @@ -107,3 +119,4 @@ def migrate(env, version): assure_stock_rule_company_is_correct(env) fill_inventory_line_categ(env) prefill_stock_picking_type_sequence_code(env) + merge_stock_picking_responsible_module(env) diff --git a/odoo/addons/openupgrade_records/lib/apriori.py b/odoo/addons/openupgrade_records/lib/apriori.py index fd9cf3ad789f..9e5b57c9207a 100644 --- a/odoo/addons/openupgrade_records/lib/apriori.py +++ b/odoo/addons/openupgrade_records/lib/apriori.py @@ -30,6 +30,8 @@ 'sale_product_classification': 'product_abc_classification_sale', # OCA/stock-logistics-warehouse 'stock_putaway_product_form': 'stock_putaway_product_template', + # OCA/stock-logistics-workflow + 'stock_picking_responsible': 'stock', # OCA/l10n-netherlands -> OCA/account-financial-reporting 'l10n_nl_mis_reports': 'mis_template_financial_report', }