Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docsource/modules170-180.rst
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ Module coverage 17.0 -> 18.0
+---------------------------------------------------+----------------------+-------------------------------------------------+
| microsoft_outlook | |No DB layout changes. |
+---------------------------------------------------+----------------------+-------------------------------------------------+
| mrp | | |
| mrp | | Done |
+---------------------------------------------------+----------------------+-------------------------------------------------+
| mrp_account | | |
+---------------------------------------------------+----------------------+-------------------------------------------------+
Expand Down Expand Up @@ -1024,7 +1024,7 @@ Module coverage 17.0 -> 18.0
+---------------------------------------------------+----------------------+-------------------------------------------------+
| spreadsheet_dashboard_website_sale_slides | | |
+---------------------------------------------------+----------------------+-------------------------------------------------+
| stock | | |
| stock | Done | |
+---------------------------------------------------+----------------------+-------------------------------------------------+
| stock_account | | |
+---------------------------------------------------+----------------------+-------------------------------------------------+
Expand Down
150 changes: 150 additions & 0 deletions openupgrade_scripts/scripts/mrp/18.0.2.0/post-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
# Copyright 2025 Akretion
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).


from openupgradelib import openupgrade


def _migrate_mrp_documents(env):
if not openupgrade.table_exists(env.cr, "product_document_mrp_legacy"):
return

# Fetch old mrp_document data: ir_attachment_id, active, priority.
# `product_document_mrp_legacy` contains fields from old `mrp_document`.
# `ir_attachment_id` was M2O to `ir.attachment`.
# `id` was the own ID of the `mrp_document` record.
env.cr.execute(
"""
SELECT ir_attachment_id, active, priority
FROM product_document_mrp_legacy
WHERE ir_attachment_id IS NOT NULL
"""
)
old_docs_data = env.cr.dictfetchall()

if not old_docs_data:
openupgrade.drop_tables(env.cr, ["product_document_mrp_legacy"])
return

# Mapping from old priorities to new sequence values.
# Default sequence on product.document is 10. Lower sequence means higher priority.
priority_to_sequence = {
"0": 15, # Normal
"1": 20, # Low
"2": 10, # High
"3": 5, # Very High
}

ProductDocument = env["product.document"]
attachment_ids_to_migrate = [
r["ir_attachment_id"] for r in old_docs_data if r["ir_attachment_id"]
]

# Pre-fetch existing product.document records to avoid duplicates and update them.
# Handles cases where product module might have already created product.document.
existing_product_docs = ProductDocument.search(
[("ir_attachment_id", "in", attachment_ids_to_migrate)]
)
attachments_to_existing_pd = {
pd.ir_attachment_id.id: pd for pd in existing_product_docs
}

pd_vals_list_create = []
updated_count = 0
created_count = 0

for old_doc_data in old_docs_data:
ir_attachment_id = old_doc_data["ir_attachment_id"]
if not ir_attachment_id:
continue

# Ensure priority key is a string for dict lookup
sequence_val = priority_to_sequence.get(str(old_doc_data["priority"]), 10)
active_val = old_doc_data["active"]

existing_pd = attachments_to_existing_pd.get(ir_attachment_id)

if existing_pd:
# Update existing product.document if necessary
write_vals = {}
# 'bom' takes precedence. If it was 'hidden' or not set, update to 'bom'.
if existing_pd.attached_on_mrp != "bom":
write_vals["attached_on_mrp"] = "bom"

# Update sequence if it was the default (10) and new mapping
# suggests different,
# or if explicitly set priority implies a new sequence different
# from current.
if (existing_pd.sequence == 10 and sequence_val != 10) or (
old_doc_data["priority"] is not None
and existing_pd.sequence != sequence_val
):
write_vals["sequence"] = sequence_val

if existing_pd.active != active_val:
write_vals["active"] = active_val

if write_vals:
existing_pd.write(write_vals)
updated_count += 1
else:
# Create new product.document
pd_vals_list_create.append(
{
"ir_attachment_id": ir_attachment_id,
"active": active_val,
"attached_on_mrp": "bom", # Assume all old docs are 'bom' relevant
"sequence": sequence_val,
}
)
created_count += 1

if pd_vals_list_create:
ProductDocument.create(pd_vals_list_create)

openupgrade.drop_tables(env.cr, ["product_document_mrp_legacy"])
log_message = (
f"Migrated mrp.document records to product.document: "
f"{created_count} created, {updated_count} updated."
)
openupgrade.log(env.cr, "MRP", log_message)


def _set_mrp_bom_line_manual_consumption(env):
# Old logic for mrp.bom.line.manual_consumption (v17):
# line.manual_consumption = (line.tracking != 'none' or line.operation_id)
# `product_id.tracking` (on product.product) is related
# to `product_tmpl_id.tracking`.
# `product.template.tracking` depends on `is_storable` in v18.
# The `stock` module migration scripts handle `type` -> `is_storable`
# and adjusts `tracking`. So, reading `product_id.tracking` here should give
# the v18-compatible value. `mrp.bom.line` has a `tracking` field related to
# `product_id.tracking`.
openupgrade.logged_query(
env.cr,
"""
UPDATE mrp_bom_line
SET manual_consumption = TRUE
WHERE (tracking IS NOT NULL AND tracking != 'none') OR operation_id IS NOT NULL;
""",
reason=(
"Set manual_consumption on mrp.bom.line based on v17 logic "
"(tracked product or specific operation)."
),
)


@openupgrade.migrate()
def migrate(env, version):
_migrate_mrp_documents(env)
_set_mrp_bom_line_manual_consumption(env)

xml_ids_to_delete = [
"mrp.act_assign_serial_numbers_production",
"mrp.view_mrp_workorder_view_gantt", # Gantt views often refactored/removed
"mrp.view_assign_serial_numbers_production",
"mrp.view_document_file_kanban_mrp", # Replaced by product.document views
"mrp.view_mrp_document_form", # Replaced by product.document views
"mrp.workcenter_line_gantt_production", # Gantt views often refactored/removed
]
openupgrade.delete_records_safely_by_xml_id(env, xml_ids_to_delete)
56 changes: 56 additions & 0 deletions openupgrade_scripts/scripts/mrp/18.0.2.0/pre-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright 2025 Akretion
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).


from openupgradelib import openupgrade

_renamed_fields = [
("mrp.production", "mrp_production", "date_planned_start", "date_start"),
("mrp.production", "mrp_production", "date_planned_finished", "date_finished"),
("mrp.workorder", "mrp_workorder", "date_planned_start", "date_start"),
("mrp.workorder", "mrp_workorder", "date_planned_finished", "date_finished"),
]


@openupgrade.migrate()
def migrate(env, version):
# Handle mrp.document model removal and merge into product.document
# The old mrp_document model inherited ir.attachment via 'ir_attachment_id' M2O.
# The product_document_mrp_legacy table will contain fields specific
# to the old mrp_document.
if openupgrade.table_exists(env.cr, "mrp_document"):
openupgrade.rename_tables(
env.cr, [("mrp_document", "product_document_mrp_legacy")]
)

openupgrade.rename_fields(env, _renamed_fields)

# Attempt to get column type for 'produce_delay'
# If get_pg_column_type is not available, this part might need adjustment
# based on your specific openupgradelib version or available helpers.
# For now, proceeding with a direct check and alter.
if openupgrade.column_exists(env.cr, "mrp_bom", "produce_delay"):
# A more robust check would be to inspect information_schema.columns.data_type
# but that's more complex without a helper.
# Assuming if it exists and needs conversion, it's likely 'double precision'.
try:
openupgrade.alter_column_type(
env.cr,
"mrp_bom",
"produce_delay",
"integer",
"USING ROUND(produce_delay)::integer",
)
openupgrade.log(
env.cr,
"MRP",
"Altered column type of mrp_bom.produce_delay to integer.",
)
except Exception as e:
openupgrade.log(
env.cr,
"MRP",
"Could not alter column type of mrp_bom.produce_delay: %s. "
"Manual check might be needed." % e,
level="warning",
)
116 changes: 116 additions & 0 deletions openupgrade_scripts/scripts/mrp/18.0.2.0/upgrade_analysis_work.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
---Models in module 'mrp'---
obsolete model mrp.document (merged to product.document)
# DONE: pre-migration: renamed table `mrp_document` to `product_document_mrp_legacy`.
# DONE: post-migration: data from `product_document_mrp_legacy` (ir_attachment_id, active, priority as sequence)
# migrated to `product.document`, setting `attached_on_mrp` = 'bom'.
# Old table `product_document_mrp_legacy` is dropped.

new model mrp.batch.produce [transient]
# NOTHING TO DO: New transient model.

---Fields in module 'mrp'---
mrp / mrp.document / _inherits : DEL _inherits: {'ir.attachment': 'ir_attachment_id'}
mrp / mrp.document / active (boolean) : DEL
mrp / mrp.document / ir_attachment_id (many2one) : DEL relation: ir.attachment, required
mrp / mrp.document / priority (selection) : DEL selection_keys: ['0', '1', '2', '3']
# DONE: Fields of the deleted `mrp.document` model. Data migrated in post-migration.

mrp / mrp.production / location_final_id (many2one) : NEW relation: stock.location
mrp / mrp.production / never_product_template_attribute_value_ids (many2many): NEW relation: product.template.attribute.value
mrp / mrp.production / search_date_category (selection): NEW selection_keys: ['after', 'before', 'day_1', 'day_2', 'today', 'yesterday']
# NOTHING TO DO: New fields, new functionality.

mrp / mrp.routing.workcenter / activity_ids (one2many) : NEW relation: mail.activity
mrp / mrp.routing.workcenter / message_follower_ids (one2many): NEW relation: mail.followers
mrp / mrp.routing.workcenter / message_ids (one2many) : NEW relation: mail.message
mrp / mrp.routing.workcenter / rating_ids (one2many) : NEW relation: rating.rating
mrp / mrp.routing.workcenter / website_message_ids (one2many): NEW relation: mail.message
# NOTHING TO DO: New mail-related fields due to inheritance changes.

mrp / mrp.unbuild / bom_id (many2one) : now a function
mrp / mrp.unbuild / lot_id (many2one) : now a function
# NOTHING TO DO: Change from stored to compute. ORM will handle recomputation.

mrp / mrp.workcenter / message_follower_ids (one2many): NEW relation: mail.followers
mrp / mrp.workcenter / message_ids (one2many) : NEW relation: mail.message
mrp / mrp.workcenter / rating_ids (one2many) : NEW relation: rating.rating
mrp / mrp.workcenter / website_message_ids (one2many): NEW relation: mail.message
# NOTHING TO DO: New mail-related fields due to inheritance changes.

mrp / mrp.workorder / _order : _order is now 'sequence, leave_id, date_start, id' ('leave_id, date_start, id')
# NOTHING TO DO: Change in default sort order.

mrp / mrp.workorder / production_date (datetime) : not related anymore
mrp / mrp.workorder / production_date (datetime) : now a function
# NOTHING TO DO: Change from related to compute. ORM will handle recomputation.

mrp / mrp.workorder / sequence (integer) : NEW hasdefault: default
# NOTHING TO DO: New field with default.

mrp / product.document / attached_on_mrp (selection) : NEW required, selection_keys: ['bom', 'hidden'], hasdefault: default
# DONE: New field on `product.document`. Populated in post-migration for migrated `mrp.document` records.

mrp / stock.move / manual_consumption (boolean) : not a function anymore
# DONE: This field's computation now relies on `mrp.bom.line.manual_consumption`.
# The `mrp.bom.line.manual_consumption` field itself was a compute and is now stored;
# its value is set in post-migration script for `mrp.bom.line` based on old logic.
# The stock module migration will then correctly compute and store `stock.move.manual_consumption`.

mrp / stock.picking.type / use_auto_consume_components_lots (boolean): DEL
# DONE: Field removed. The logic is now handled by `mrp.bom.line.manual_consumption`. No data migration needed for this field itself.

mrp / mrp.production / date_planned_start (datetime) : RENAMED to date_start
mrp / mrp.production / date_planned_finished (datetime) : RENAMED to date_finished
mrp / mrp.workorder / date_planned_start (datetime) : RENAMED to date_start
mrp / mrp.workorder / date_planned_finished (datetime) : RENAMED to date_finished
# DONE: Renamed in pre-migration.py.

mrp / mrp.bom / produce_delay (float) : type changed to integer
# DONE: Type changed from float to integer. Handled in pre-migration by rounding.

mrp / mrp.bom.line / manual_consumption (boolean) : logic changed from compute to stored with default.
# DONE: Populated in post-migration based on v17 logic: (line.tracking != 'none' or line.operation_id).

mrp / mrp.workorder / next_work_order_id (many2one) : DEL relation: mrp.workorder
# NOTHING TO DO: Field removed. Replaced by `blocked_by_workorder_ids` and `needed_by_workorder_ids`. Logic for linking workorders is in `mrp.production._link_workorders_and_moves`.

---XML records in module 'mrp'---
NEW ir.actions.act_window: mrp.action_mrp_batch_produce
NEW ir.actions.act_window: mrp.action_picking_tree_mrp_operation_graph
# NOTHING TO DO: New records.

DEL ir.actions.act_window: mrp.act_assign_serial_numbers_production
# DONE: Deleted in post-migration.

NEW ir.actions.server: mrp.action_pause_workorders
NEW ir.actions.server: mrp.action_print_labels
NEW ir.actions.server: mrp.action_production_order_lock_unlock
NEW ir.actions.server: mrp.action_production_order_scrap
NEW ir.actions.server: mrp.action_start_workorders
NEW ir.model.access: mrp.access_mrp_batch_produce
NEW ir.model.access: mrp.access_stock_move_mrp_user
NEW ir.ui.view: mrp.mrp_production_workorder_tree_editable_view_mo_form
NEW ir.ui.view: mrp.product_document_form
NEW ir.ui.view: mrp.product_view_search_catalog
NEW ir.ui.view: mrp.view_mrp_batch_produce_form
NEW ir.ui.view: mrp.view_mrp_stock_move_operations
NEW ir.ui.view: mrp.view_stock_rule_form
# NOTHING TO DO: New records.

DEL ir.ui.view: mrp.mrp_workorder_view_gantt
DEL ir.ui.view: mrp.view_assign_serial_numbers_production
# DONE: Deleted in post-migration.

DEL ir.ui.view: mrp.view_document_file_kanban_mrp
DEL ir.ui.view: mrp.view_mrp_document_form
# DONE: Related to `mrp.document` removal. Deleted in post-migration.

DEL ir.ui.view: mrp.workcenter_line_gantt_production
# DONE: Deleted in post-migration.

NEW mail.message.subtype: mrp.mrp_mo_in_cancelled (noupdate)
NEW mail.message.subtype: mrp.mrp_mo_in_confirmed (noupdate)
NEW mail.message.subtype: mrp.mrp_mo_in_done (noupdate)
NEW mail.message.subtype: mrp.mrp_mo_in_progress (noupdate)
NEW mail.message.subtype: mrp.mrp_mo_in_to_close (noupdate)
# NOTHING TO DO: New records.
Loading
Loading